From d13dc0ce4733f37af1356a61a6ada30e4d80170a Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Tue, 16 Jun 2020 12:54:22 +0200 Subject: [PATCH] HLSL: Fix texProj in legacy HLSL. --- .../frag/legacy-tex-modifiers.sm30.frag | 32 +++++++++++++++ .../frag/legacy-tex-modifiers.sm30.frag | 32 +++++++++++++++ .../frag/legacy-tex-modifiers.sm30.frag | 13 +++++++ spirv_hlsl.cpp | 39 ++++++++++++------- 4 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 reference/opt/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag create mode 100644 reference/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag create mode 100644 shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag diff --git a/reference/opt/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag b/reference/opt/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag new file mode 100644 index 00000000..2af0e513 --- /dev/null +++ b/reference/opt/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag @@ -0,0 +1,32 @@ +uniform sampler2D uSampler; + +static float4 FragColor; +static float2 vUV; + +struct SPIRV_Cross_Input +{ + float2 vUV : TEXCOORD0; +}; + +struct SPIRV_Cross_Output +{ + float4 FragColor : COLOR0; +}; + +void frag_main() +{ + float3 _23 = float3(vUV, 5.0f); + FragColor = tex2Dproj(uSampler, float4(_23.xy, 0.0, _23.z)); + FragColor += tex2Dbias(uSampler, float4(vUV, 0.0, 3.0f)); + FragColor += tex2Dlod(uSampler, float4(vUV, 0.0, 2.0f)); + FragColor += tex2Dgrad(uSampler, vUV, 4.0f.xx, 5.0f.xx); +} + +SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input) +{ + vUV = stage_input.vUV; + frag_main(); + SPIRV_Cross_Output stage_output; + stage_output.FragColor = float4(FragColor); + return stage_output; +} diff --git a/reference/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag b/reference/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag new file mode 100644 index 00000000..2af0e513 --- /dev/null +++ b/reference/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag @@ -0,0 +1,32 @@ +uniform sampler2D uSampler; + +static float4 FragColor; +static float2 vUV; + +struct SPIRV_Cross_Input +{ + float2 vUV : TEXCOORD0; +}; + +struct SPIRV_Cross_Output +{ + float4 FragColor : COLOR0; +}; + +void frag_main() +{ + float3 _23 = float3(vUV, 5.0f); + FragColor = tex2Dproj(uSampler, float4(_23.xy, 0.0, _23.z)); + FragColor += tex2Dbias(uSampler, float4(vUV, 0.0, 3.0f)); + FragColor += tex2Dlod(uSampler, float4(vUV, 0.0, 2.0f)); + FragColor += tex2Dgrad(uSampler, vUV, 4.0f.xx, 5.0f.xx); +} + +SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input) +{ + vUV = stage_input.vUV; + frag_main(); + SPIRV_Cross_Output stage_output; + stage_output.FragColor = float4(FragColor); + return stage_output; +} diff --git a/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag b/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag new file mode 100644 index 00000000..30b957b2 --- /dev/null +++ b/shaders-hlsl/frag/legacy-tex-modifiers.sm30.frag @@ -0,0 +1,13 @@ +#version 450 + +layout(location = 0) in vec2 vUV; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 0) uniform sampler2D uSampler; + +void main() +{ + FragColor = textureProj(uSampler, vec3(vUV, 5.0)); + FragColor += texture(uSampler, vUV, 3.0); + FragColor += textureLod(uSampler, vUV, 2.0); + FragColor += textureGrad(uSampler, vUV, vec2(4.0), vec2(5.0)); +} diff --git a/spirv_hlsl.cpp b/spirv_hlsl.cpp index cbe68d0b..d4074e7c 100644 --- a/spirv_hlsl.cpp +++ b/spirv_hlsl.cpp @@ -2946,24 +2946,37 @@ void CompilerHLSL::emit_texture_op(const Instruction &i, bool sparse) if (proj && hlsl_options.shader_model >= 40) // Legacy HLSL has "proj" operations which do this for us. coord_expr = coord_expr + " / " + to_extract_component_expression(coord, coord_components); - if (hlsl_options.shader_model < 40 && lod) + if (hlsl_options.shader_model < 40) { string coord_filler; - for (uint32_t size = coord_components; size < 3; ++size) - { - coord_filler += ", 0.0"; - } - coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_expression(lod) + ")"; - } + uint32_t modifier_count = 0; - if (hlsl_options.shader_model < 40 && bias) - { - string coord_filler; - for (uint32_t size = coord_components; size < 3; ++size) + if (lod) { - coord_filler += ", 0.0"; + for (uint32_t size = coord_components; size < 3; ++size) + coord_filler += ", 0.0"; + coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_expression(lod) + ")"; + modifier_count++; } - coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_expression(bias) + ")"; + + if (bias) + { + for (uint32_t size = coord_components; size < 3; ++size) + coord_filler += ", 0.0"; + coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_expression(bias) + ")"; + modifier_count++; + } + + if (proj) + { + for (uint32_t size = coord_components; size < 3; ++size) + coord_filler += ", 0.0"; + coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_extract_component_expression(coord, coord_components) + ")"; + modifier_count++; + } + + if (modifier_count > 1) + SPIRV_CROSS_THROW("Legacy HLSL can only use one of lod/bias/proj modifiers."); } if (op == OpImageFetch)