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.02k stars 558 forks source link

MSL: An error ResType does not refer to a value when converting GLSL to Metal shader #1100

Closed jiradeto closed 5 years ago

jiradeto commented 5 years ago

On MacOS 10.14, I tried to convert SPIR-V to MSL and found an issue that when converting the following GLSL fragment shader, the MSL compiler gives an error:

rogram_source:24:36: error: 'ResType' does not refer to a value
    for (; reverse_bits(1) < 1024; ResType _37, _37._m0 = 10u - 1u, _37._m1 = select(uint(1), uint(0), 10u >= 1u), _26 = float(_37._m0))
                                   ^
program_source:6:8: note: declared here
struct ResType
       ^
program_source:24:44: error: expected ')'
    for (; reverse_bits(1) < 1024; ResType _37, _37._m0 = 10u - 1u, _37._m1 = select(uint(1), uint(0), 10u >= 1u), _26 = float(_37._m0))
                                           ^

The GLSL fragment shader:

void main()
{
 uint uselessOutVariable;
 float data[10];

 for(int i = 1; bitfieldReverse(1) < 1024; 1) {
   if (gl_FragCoord.y < 0.0) {
     _GLF_color = vec4(1.0);
    }
   data[i] = float(usubBorrow(uint(10), 1u, uselessOutVariable));
  }

 if (1.0 < data[1]) {
   _GLF_color = vec4(1.0);
  }
}

MSL obtained by SPIRV-Cross:

fragment main0_out main0(float4 gl_FragCoord [[position]])
{
    main0_out out = {};
    float _57;
    _57 = _58;
    for (; reverse_bits(1) < 1024; ResType _44, _44._m0 = 10u - 1u, _44._m1 = select(uint(1), uint(0), 10u >= 1u), _57 = float(_44._m0))
    {
        if (gl_FragCoord.y < 0.0)
        {
            out._GLF_color = float4(1.0);
            continue;
        }
        else
        {
            continue;
        }
        continue;
    }
    if (1.0 < _57)
    {
        out._GLF_color = float4(1.0);
    }
    return out;
}

Versions:

Steps to reproduce:

This Archive.zip contains the original fragment shader, the associated optimized SPIR-V, and MSL shader.

Issue found using GraphicsFuzz.

HansKristian-Work commented 5 years ago

Likely need to use the recent emit_uninitized_temporary for all opcodes which need to emit temporaries, which was designed to support hoisting temporaries out of continue blocks like this.