Shader_Minifier icon indicating copy to clipboard operation
Shader_Minifier copied to clipboard

Loop index renaming and bracket removal overwrites fragment output in main block scope

Open LeStahL opened this issue 1 year ago • 1 comments

Ok this one is kinda obscure to track down - I'm using shader_minifier v1.4.0. Minifying the shader

#version 450

out vec3 v;

void main()
{
    vec3 y;
    for (int i = 0; i < 500; i++) {
        if (i > 1)
            y += 9;
    }
    v = abs(gl_FragCoord.y) < .3 
        ? y * 1.1
        : y;
}

results in the invalid minified shader

#version 450

out vec3 v;
void main()
{
  vec3 i;
  for(int v=0;v<500;v++)
    if(v>1)
      i+=9;
  v=abs(gl_FragCoord.y)<.3?
    i*1.1:
    i;
}

which has the glslangValidator output

ERROR: 0:10: 'assign' :  cannot convert from ' temp 3-component vector of float' to ' temp int'
ERROR: 0:12: '' : compilation terminated 
ERROR: 2 compilation errors.  No code generated.

The problem has two causes:

  • loop index variable gets renamed to the same id as the fragment output
  • loop { scope gets removed because it's a single-statement loop and thus loop variable declaration overwrites fragment output variable

LeStahL avatar Aug 08 '24 12:08 LeStahL