Skip to content

Added shader variant (multi-compile) system #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

adriengivry
Copy link
Member

@adriengivry adriengivry commented Apr 16, 2025

Description

  • Added a system to create shader variants by using the keyword #feature FEATURE_NAME in a shader code.
    Each possible variant gets compiled (2^n, with n being the feature count: how many times #feature is used in a single shader).
  • Improved #include directive parsing by preventing commented includes (i.e. `// #include "...") from being evaluated. Instead of checking if a line "contains" a directive, we now trim the line, and checks if it starts with the directive (safer)
  • Added a system to enable/disable logging for shader compilation, used by the AssetBrowser to enable logging temporarily (when a manual compilation is requested by the user).
  • Updated shader compilation logging in editor to only display the shader name instead of the full path (easier to read output).

Limitations

  • This system only modifies Shader and ShaderLoader to enable the multi-compile feature. Some additional work needs to be done at the material level to expose these features in the editor, and use them when a material is bound.
  • No support for "incompatible feature combinations" (to reduce variant count). Could be added further down the road as an optimization.
  • Still no support for parsing shaders compiled from sources (not from file), so features cannot be resolved for these shaders (only used for the default fallback shader, so that's fine for now)
  • Other pre-processor directives (like #include) cannot be added/excluded from a variant using #ifdef)

Additional notes

The current system is similar to what unity describes as shader_feature:
https://docs.unity3d.com/Manual/SL-MultipleProgramVariants-declare.html

@adriengivry adriengivry self-assigned this Apr 16, 2025
@adriengivry adriengivry added the Graphics Graphical feature label Apr 16, 2025
@adriengivry adriengivry marked this pull request as ready for review April 17, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Graphics Graphical feature
Development

Successfully merging this pull request may close these issues.

1 participant