laurentlb / shader-minifier

Minify and obfuscate GLSL or HLSL code
https://ctrl-alt-test.fr/minifier/
Apache License 2.0
424 stars 29 forks source link

Ternary operator with side effects (bug) #385

Closed therontarigo closed 1 month ago

therontarigo commented 1 month ago

Encountered when trying to minify a shader that already used some codegolf tricks. This legal GLSL:

out vec4 O;
uniform int k;
void main() {
  int d=0,e=0;
  k==0 ? d=1 : e=2 ;
  // if(k==0) d=1 ; else e=2 ;  // semantically equivalent
  O.x=d;
  O.y=e;
}

results in both assignments being evaluated:

out vec4 O;
uniform int k;
void main()
{
  int d=0,e=0;
  d=1,e=2;
  O.x=d;
  O.y=e;
}

using the commented line instead, result is correct, but misses the opportunity to use ternary operator trick.