galacean / engine

A typescript interactive engine, support 2D, 3D, animation, physics, built on WebGL and glTF.
https://galacean.antgroup.com/
MIT License
4.04k stars 289 forks source link

Extact "mattered" macro list from shader source #2094

Open Sway007 opened 1 month ago

Sway007 commented 1 month ago

Why

Extracting all "mattered" macros from the shader source code helps avoid unnecessary compilation of shader variants.


The definition for "mattered" macro in version 1.3 is simple: macros defined after #if, #ifdef, #ifndef, and defined operator.

The macros defined by the above standards are not actually "mattered" macros. For example, the following conditional macro expansion is a variable declaration, but it may not be referenced by the entry main function, so it should not be defined as a "mattered" macro.

\#ifdef SOME_DEFINED_MACRO
    float useless_variable;
\#endif

Future optimization

TODO: Use a seperate compiler to analyze and extract the real list of all "mattered" macros after Shader.create but before shader variant compilation.