laurentlb/shader-minifier

Ternary operator with side effects (bug)

therontarigo opened this issue · 0 comments

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.