KhronosGroup / SPIRV-Cross

SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
Apache License 2.0
2.03k stars 560 forks source link

Bug : GLSL combined texture sampler #744

Closed Roweax closed 5 years ago

Roweax commented 5 years ago

commit cf5e1c280132d1ac519cd98ca64d3173342f7e2b

when i convert such HLSL shader

Texture2D Texture0;
Texture2D Texture1;
SamplerState Sampler;

float A(Texture2D _texture)
{
    return _texture.Sample(Sampler, float2(0, 0)).r;
}

float Sum(Texture2D _texture)
{
    return A(_texture);
}

[numthreads(1, 1, 1)]
void Main(uint2 DispatchThreadId : SV_DispatchThreadID)
{
    float a = Sum(Texture0);
    a += Sum(Texture1);
}

convert to spv, then convert to glsl

./glslangValidator  -V -e Main  -o comp.spv ./texture.comp.hlsl
./spirv-cross  comp.spv  --output texture.comp

output glsl, the 'Sum' function lose argument

#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

uniform sampler2D SPIRV_Cross_CombinedparamSampler;

float A(sampler2D SPIRV_Cross_Combined_textureSampler)
{
    return textureLod(SPIRV_Cross_Combined_textureSampler, vec2(0.0), 0.0).x;
}

float Sum()
{
    return A(SPIRV_Cross_CombinedparamSampler);
}

void _Main(uvec2 DispatchThreadId)
{
    float a = Sum();
    a += Sum();
}

void main()
{
    uvec2 DispatchThreadId = uvec2(gl_GlobalInvocationID.xy);
    uvec2 param = DispatchThreadId;
    _Main(param);
}
HansKristian-Work commented 5 years ago

Reproduced. Strange ...

HansKristian-Work commented 5 years ago

Oh, on further inspection, this is illegal SPIR-V. You have this sequence:

     %_Main_ = OpFunction %float None %16
         %18 = OpLabel
          %a = OpVariable %_ptr_Function_float Function
    %param_0 = OpVariable %_ptr_Function_7 Function
    %param_1 = OpVariable %_ptr_Function_7 Function
         %46 = OpLoad %7 %Texture0                      <--- Load texture
               OpStore %param_0 %46                        <--- Store texture to stack, illegal!
         %47 = OpFunctionCall %float %Sum_t21_ %param_0

If I patch up the SPIR-V to be correct, i.e. pass the texture pointer around directly, it works as expected, you need to run the HLSL legalizer passes on this shader.

spirv-val doesn't catch this, so I'll file a spirv-tools bug.