-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Material can specify defines for the shader #6865
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand why you're exposing defines without values?
We don't need values, in shader code we only test if it exists. |
What do you mean by "we don't need values in shader code"? As you know, defines are very useful as compile time parameters, we use them everywhere. Here is an example: https://github.com/playcanvas/engine/blob/main/src/scene/shader-lib/chunks/common/frag/reproject.js#L14C8-L14C15 The alternatives (hard-coded value or uniform) are both worse options. |
I see your point, but these are more like a boolean switches for shader features. |
So my is question is why particularly limit it this way? Adding support later is possible, but that would (presumably) break public API. I'm also still curious what doesn't work with defines, given we have lots of this in the engine: https://github.com/playcanvas/engine/blob/main/src/scene/shader-lib/chunks/lit/frag/shadowPCSS.js#L10 |
for example this does not work:
we do not handle expressions inside And if defines would have values, it'd be expected that they can be used this way. We could add this without breaking API in the future though. |
What would happen in the case above? WebGL2 shader works because of glsl preprocessor, but WebGPU fails to compile? |
Yep, the preprocessor would assert and fail here: engine/src/core/preprocessor.js Line 385 in 7355d30
|
actually, even WebGL would fail here, we preprocess WebGL shader the same way. |
Ok thanks for explaining. This limitation doesn't change or mitigate the usefulness of define values as constants IMO. And the error would help stop developers from trying it quite early on. So unless it requires major restructuring work or similar, I'd still vote to support arbitrary define values in material, otherwise it seems like a half feature. |
Yeah, I think I agree with @slimbuck on that. |
I'll add it tomorrow, but I'm not sure it's that useful for shaders used by the forward renderer. For example we do not have a single user case like this currently in whole standard material shader generator. This is more useful for postprocessing and similar shader, for which ShaderMaterial is not applicable. The boolean only format would allow us to easily expose these to the Inspector in the Editor as boolean toggles for material features. Generic data (that user needs to type in) is less ideal here in the Inspector. |
Added support for define values. As discussed, what can be done with the values is limited. |
Fixes #2962
New public API
This allows to set values to user defines on a shader. The define is then added to both vertex and fragment shader, allowing a user defined behaviour.
Usage:
StandardMaterial, ShaderMaterial, GSplat materials and LitMaterial support this.
Examples of shader customisations: