laurentlb / shader-minifier

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

Unexpected '{' within function call in variable initialization #448

Open remage opened 2 weeks ago

remage commented 2 weeks ago

Input:

vec3 cam_position = { 0.0, 0.0, 5.0 };
vec3 cam_target   = { 0.0, 0.0, 0.0 };
vec3 cam_dir = normalize(cam_target - cam_position);
vec3 cam_h = { 1.0f, 0.0f, 0.0f }; // fov_h = 90
vec3 cam_v = { 0.0f, 9.0/16.0, 0.0f };

It gets simplified to this code:

vec3 d={0.,0.,5.},c=normalize({0.,0.,0.}-d),l={1.f,0.f,0.f},r={0.f,.5625,0.f};

Shader compilation fails inside the normalize() call, with:

error C0000: syntax error, unexpected '{', expecting ')' at token "{"

remage commented 2 weeks ago

Another experiment:

vec3 cam_position = { 0.0, 0.0, 5.0 };
vec3 cam_target   = { 0.0, 0.0, 0.0 };
vec3 cam_dir = { 0.0, 0.0, -1.0 }; // normalize(cam_target - cam_position);

This simplifies to:

vec3 d={0.,0.,5.},i={0.,0.,0.};
i={0.,0.,-1.};

It seems to try to reuse the variable i, but that assignment syntax fails to compile:

error C0000: syntax error, unexpected '{', expecting "::" at token "{"

It compiles when the assignment is fixed (by hand) to:

vec3 d={0.,0.,5.},i={0.,0.,0.};
i=vec3(0.,0.,-1.);
remage commented 2 weeks ago

Further experiment: realizing that the fact cam_target gets reused means that it's not used anywhere else in the code... So, in this case I can do this (and that is my workaround for now):

vec3 cam_position = { 0.0, 0.0, 5.0 };
// vec3 cam_target   = { 0.0, 0.0, 0.0 };
vec3 cam_dir = normalize(vec3(0.0, 0.0, 0.0) - cam_position);

This gets minified and then compiles correctly.