evanw / glslx

A GLSL type checker, code formatter, and minifier for WebGL
http://evanw.github.io/glslx/
MIT License
408 stars 28 forks source link

no constant replacement in array declarations #12

Closed cauthmann closed 3 years ago

cauthmann commented 5 years ago

Hello,

a hypothetical shader example:

const int COUNT = 100;
uniform float data[COUNT];

export void pixelShader() {
  float sum;
  for (int i = 0; i < COUNT; i++)
    sum += data[i];
  gl_FragColor = vec4(sum, data[0], 0.0, float(COUNT));
}

will compile pixelShader to

uniform float data[COUNT];

void main(){
  float sum;
  for(int i=0;i<100;i++)
    sum+=data[i];
  gl_FragColor=vec4(sum,data[0],0.,float(100));
}

Note how the COUNT constant was removed and replaced with its value almost everywhere, except in the first line. Thus, glslx outputs an invalid shader.

(Aside: it would be great if I could prevent just this constant from being inlined at all. I need to recompile my shader with different values of COUNT depending on the data points I have. So far, my workaround is shader = shader.replace(/COUNT/g, count).replace(/100/g, count);, but I can imagine a few scenarios where that will break.)