-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Gate diffuse and specular transmission behind shader defs #11627
Conversation
@@ -43,7 +43,7 @@ fn fragment( | |||
double_sided, | |||
is_front, | |||
#ifdef VERTEX_TANGENTS | |||
#ifdef STANDARDMATERIAL_NORMAL_MAP | |||
#ifdef STANDARD_MATERIAL_NORMAL_MAP |
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.
This is a small drive-by fix for consistency with the instances of STANDARD_MATERIAL
in the codebase.
#ifdef STANDARD_MATERIAL_DIFFUSE_TRANSMISSION | ||
if diffuse_transmission > 0.0 { |
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.
the shader def being present should mean it's > 0.0, could we remove the if from the shader? (same for the others)
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.
Hmm, we could, but with the use of diffuse_transmission_texture
and specular_transmission_texture
it's possible for the per-fragment value of diffuse_transmission
and specular_transmission
to be 0, even when the shader def is present. In those cases without the if
we'd do a bunch of computations or texture reads that would be immediately discarded.
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'm not sure how much that really matters. I suspect the overhead of the if statement would be worse than any potential extra work on average. It's not a big deal either way, but it's probably worth removing still.
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.
would be nice to get confirmation from someone with the issue on android
#ifdef STANDARD_MATERIAL_DIFFUSE_TRANSMISSION | ||
if diffuse_transmission > 0.0 { |
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'm not sure how much that really matters. I suspect the overhead of the if statement would be worse than any potential extra work on average. It's not a big deal either way, but it's probably worth removing still.
Okay, after thinking about it for a bit, I ended up being convinced that it will be a rare enough situation (having a texture to control the transmission levels, and having the majority of it not be transmissive) that's probably not worth it to give the much more common scenario a small penalty every time just for this unlikely benefit. Removed the Since that affects indentation, it might make sense to review this with "hide whitespace changes" on, otherwise it looks like a much bigger change than it really is. |
…#11627) # Objective - Address bevyengine#10338 ## Solution - When implementing specular and diffuse transmission, I inadvertently introduced a performance regression. On high-end hardware it is barely noticeable, but **for lower-end hardware it can be pretty brutal**. If I understand it correctly, this is likely due to use of masking by the GPU to implement control flow, which means that you still pay the price for the branches you don't take; - To avoid that, this PR introduces new shader defs (controlled via `StandardMaterialKey`) that conditionally include the transmission logic, that way the shader code for both types of transmission isn't even sent to the GPU if you're not using them; - This PR also renames ~~`STANDARDMATERIAL_NORMAL_MAP`~~ to `STANDARD_MATERIAL_NORMAL_MAP` for consistency with the naming convention used elsewhere in the codebase. (Drive-by fix) --- ## Changelog - Added new shader defs, set when using transmission in the `StandardMaterial`: - `STANDARD_MATERIAL_SPECULAR_TRANSMISSION`; - `STANDARD_MATERIAL_DIFFUSE_TRANSMISSION`; - `STANDARD_MATERIAL_SPECULAR_OR_DIFFUSE_TRANSMISSION`. - Fixed performance regression caused by the introduction of transmission, by gating transmission shader logic behind the newly introduced shader defs; - Renamed ~~`STANDARDMATERIAL_NORMAL_MAP`~~ to `STANDARD_MATERIAL_NORMAL_MAP` for consistency; ## Migration Guide - If you were using `#ifdef STANDARDMATERIAL_NORMAL_MAP` on your shader code, make sure to update the name to `STANDARD_MATERIAL_NORMAL_MAP`; (with an underscore between `STANDARD` and `MATERIAL`)
Objective
Solution
StandardMaterialKey
) that conditionally include the transmission logic, that way the shader code for both types of transmission isn't even sent to the GPU if you're not using them;toSTANDARDMATERIAL_NORMAL_MAP
STANDARD_MATERIAL_NORMAL_MAP
for consistency with the naming convention used elsewhere in the codebase. (Drive-by fix)Changelog
StandardMaterial
:STANDARD_MATERIAL_SPECULAR_TRANSMISSION
;STANDARD_MATERIAL_DIFFUSE_TRANSMISSION
;STANDARD_MATERIAL_SPECULAR_OR_DIFFUSE_TRANSMISSION
.toSTANDARDMATERIAL_NORMAL_MAP
STANDARD_MATERIAL_NORMAL_MAP
for consistency;Migration Guide
#ifdef STANDARDMATERIAL_NORMAL_MAP
on your shader code, make sure to update the name toSTANDARD_MATERIAL_NORMAL_MAP
; (with an underscore betweenSTANDARD
andMATERIAL
)