webpack-glsl-minify icon indicating copy to clipboard operation
webpack-glsl-minify copied to clipboard

preserveUniforms clash with function parameters

Open mariusgabris opened this issue 5 years ago • 3 comments

Hi Leo,

I've noticed the following issue while have preserveUniforms on true, it's not a critical issue but I think you might be interested in this one. Input 1: uniform float scale; float test(float scale){ // do something with the parameter }

Expected result 1: uniform float scale;float A(float B){}

Actual result 1: uniform float scale;float A(float scale){}

Input 2: float test(float scale){ // do something with the parameter } uniform float scale;

Expected result 2: float A(float B){}uniform float scale;

Actual result 2: float A(float B){}uniform float B;

The problem I see is that the reserveKeywords should get an argument of variableType and mark the ones from the function parameters with a type. Then the condition from minifyToken should first filter non uniforms out and then check for existing. What do you think?

Keep up the good work, Marius

mariusgabris avatar Mar 29 '20 09:03 mariusgabris

Interesting catch. Unfortunately, I don't see an easy fix here--it's really just a limitation of the simple one-pass parser I used for the minification. As you've seen from the code, it's not actually building up a full AST and parsing the GLSL language--it's just a quick one-pass translation using 2 lookback tokens. The parser isn't aware that the scale token may be a uniform in some places and a local variable in another.

There's quite a few other optimizations that could be done with a more sophisticated parser that parses the full tree and understands block scopes, like reusing variable names or eliminating dead code. But I haven't really had a need for it myself.

leosingleton avatar Mar 29 '20 13:03 leosingleton

I know, I tried to look for an easy fix myself before I posted the issue. But as I said, it's not a major issue and if we pay attention not to fall in the second scenario, it's fine.

mariusgabris avatar Mar 30 '20 07:03 mariusgabris

@mariusgabris , we ran into this as well and ended up throwing en error whenever a uniform is conflated with a different variable and minifed. You can see the code here: https://github.com/befunkyinc/esbuild-plugin-glslify-minify/blob/main/lib/minify.js#L263

Not an amazing solution, but at least this way it doesn't happen without you realizing it.

micahjon avatar Jun 16 '21 21:06 micahjon