Conditional discard in standard shadersets #1151
Replies: 2 comments 4 replies
-
I don't know which desktop GPUs use tiled rendering, but I totally agree that the conditional discard disables the early-Z optimization and I think it's worth a pipeline switch to avoid that. Another possibility is clamping the alpha value to 0 and blending. |
Beta Was this translation helpful? Give feedback.
-
Relying upon alpha testing used to be standard thing in vis-sim, it used to help with performance avoiding the blending & writing to the colour and depth buffer. You can also get different visual results if you are writing an fragment that is alpha blending out so that it still writes to the depth buffer but can't be seen, but then prevents subsequent fragments at further depths being visible when they should be. I understand the motivation for optionally disabling it, In OpenGL you have to explicitly enable the alpha test so perhaps something similar would be natural to do with the VSG's ShaderSet's with the fragment discard code path only compiled in when something like VSG_ALPHA_TEST #define is set. This way converting from old OSG scene graphs that relied upon alpha test still have a natural replacement. @AnyOldName3 have you do some performance benchmarks to access the difference? |
Beta Was this translation helpful? Give feedback.
-
Modern GPUs are all tiled renderers, and tiled renderers hate conditional discards as they build on early depth testing, which can't be done if a fragment potentially won't do a depth write because it's been discarded. On desktop-class GPUs, a draw with a conditional discard (whether or not it ends up happening) will at the minimum disable the tiled renderer optimisation for that draw call, and for mobile GPUs, it can disable it for every single draw after that point to the same render target. Some drivers are smart enough to generate separate variants when uniforms alone are enough to determine that the discard won't happen, but that's more of a thing in older graphics APIs that don't have tools like specialisation constants to do it explicitly, and don't have explicit pipelines, and even then, couldn't be relied on.
The VSG's built-in shadersets use a discard for alpha testing, and it's only gated off by the materials alpha mask. That's a dynamically-uniform expression, so at least in theory could be used to select a variant pipeline, but it definitely won't work like that everywhere, and there's a good chance it doesn't work like that anywhere.
As an alternative, if the alpha mask were replaced with a define, there may be a framerate improvement of several times in scenes with lots of overdraw and expensive fragment shaders (e.g. soft shadows with lots of samples). This would mean more pipeline switches, but in theory, some drivers are doing this implicitly anyway, and the presence of discards is expensive enough that it can be worth many pipeline switches to avoid a few of them.
Beta Was this translation helpful? Give feedback.
All reactions