Skip to content

Fix array initializers and unused functions in generated GLSL - #1070

Open
Coastrnaut wants to merge 2 commits into
projectM-visualizer:masterfrom
Coastrnaut:fix/static-array-initializers
Open

Coastrnaut wants to merge 2 commits into
projectM-visualizer:masterfrom
Coastrnaut:fix/static-array-initializers

Conversation

@Coastrnaut

@Coastrnaut Coastrnaut commented Nov 9, 2026 •

Copy link
Copy Markdown

Two GLSL generator fixes for preset shaders that failed to compile on every platform. Together they fix all 64 presets in the four packs below whose shaders compiled on neither desktop GL nor GLES.

Array initializers (57 presets). Many presets declare an array with a flat list of values, which HLSL allows:

const float4 samples[5] = { 0, 0, 0, 11.0/3.0, 0, 1, 0, -2.0/3.0, ... };

The generated GLSL had two errors. Static globals are initialized at the start of main(), but the assignment was written with the declaration syntax, samples[5] = ..., which assigns to an element out of bounds instead of the array. And the values were passed to vec4[](...) as is, while GLSL's array constructor needs one value per element. The assignment now only writes the name, and values are grouped into element constructors when their number of components doesn't match.

Unused functions (7 presets). HLSL compilers only compile functions that the entry point uses, so some presets contain unused functions that aren't valid, e.g. float2 rsp(float2 uv_in) { } without a return statement. GLSL compilers reject these. The new HideUnusedFunctions() hides top-level functions that aren't reachable from the entry function or a global initializer before the GLSL is generated. Unlike PruneTree(), it leaves all other declarations alone.

Results

  • All 64 shaders compile on desktop GL and GLES.
  • For all other presets, shader compile/link results and rendered frames are identical to master.
  • New HLSLArrayInitializerTest and HLSLUnusedFunctionTest. All unit tests pass.

How I tested it

I loaded all 14,536 presets from cream-of-the-crop, en-d, milkdrop-original and projectm-classic one after another (Windows, RTX A6000, GL 3.3 core and GLES contexts), with the preset folders as texture search path, rendered 30 frames each with fixed audio and frame times, and recorded shader compile/link results, failed loads and frame hashes. For the pixel comparison both builds had their random seeds fixed (a local patch only, identical on both sides). The presets this PR fixes are excluded from the pixel comparison, since loading them changes shared state (e.g. the expression evaluator's random generator) for every preset after them.

Retested on current master (2026-11-12)

Many presets declare an array with a flat list of values, e.g.
"const float4 samples[5] = { 0, 0, 0, 11.0/3.0, ... };", which HLSL
allows. The GLSL generator produced two errors for these:

- Static globals are initialized at the start of main(), but the
  assignment was written with the declaration syntax, "samples[5] = ...",
  assigning to an element out of bounds instead of the whole array.
- The values were passed to the array constructor as is, but GLSL's
  array constructor needs one value per element, e.g. vec4 values for a
  vec4 array.

Write only the variable name for the assignment, and group values into
element constructors when the number of components doesn't match.
HLSL compilers only compile functions that are reachable from the entry
point, so some presets contain unused functions that aren't valid, e.g.
"float2 rsp(float2 uv_in) { }" without a return statement. GLSL
compilers check every function and reject these shaders.

Add HideUnusedFunctions(), which hides all top-level functions that
aren't called from the entry function or a global initializer, directly
or indirectly, and call it before generating GLSL. Unlike PruneTree(), it
leaves all other declarations untouched.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant