-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
MeshDepthMaterial: Compute depth manually rather than using gl_FragCoord.z #18696
Conversation
Hmm, would this allow distance based fog? #17316 |
@mrdoob Looks like the distance-based fog is already using a custom varying rather than relying on builtins, so this has no bearing on that, even if there is some similarity. |
|
…ord.z Some platforms compute gl_FragCoord at a lower precision which makes the manually computed value better for depth-based postprocessing effects. The shader precisions that implementations report through the getShaderPrecisionFormat API are not reliable in practice, so the manually computed value is always used rather than trying to enable it conditionally on those platforms which have various shader precisions. WebGL 2.0 should in principle compute gl_FragCoord at a higher precision, but this may also be affected by driver bugs on some platforms. Improvement from this patch can be seen for example on iPad with A10 processor / iPadOS 13.3.1, when using depth based postprocessing effects on a scene with a high far/near plane ratio.
dc98843
to
11a92d0
Compare
I changed the variable naming. I also removed usage of gl_FragCoord.z from the logdepthbuf shader, though it doesn't make a lot of difference since it was only being used in orthographic projection mode in case EXT_frag_depth is available. |
@Mugen87 do you prefer the patch with the logdepthbuf change, or do you think we'd be better off without it like @WestLangley suggested? I don't think it makes much of a difference in practice, since platforms that support EXT_frag_depth likely also have higher gl_FragCoord.z precision, and the change also only affects orthographic projection. |
I think the PR is okay as it is. |
To make myself clear...
Without it, since @Oletus provided no evidence of a problem with the |
11a92d0
to
fb7edb9
Compare
I removed the logdepthbuf change now, I agree that there's no strong enough reason to include it. |
Thanks! |
If I remember correctly, the issue was that the fog is in frustum space, instead of based on distance:
We tried to change it to based on distance but we ran on precision issues and we had to revert. |
Thanks for merging this! |
Is there any solution for distance based fog? AFAIK it had issues on some mobile platforms but for me it is enough if it works on desktop. Could we enable it using some feature flag until it gets sorted out? |
Unfortunately, I have no iOS device at hand to verify how webgl_materials_channels is rendered now. But yes, the issue mentioned here should be solved by this PR. |
I can confirm webgl_materials_channels is fixed on the iPad with A10 at least, I'd think that older iPads/iPhones probably behave the same as well. |
Hi! I have a depth peeling experiment that ran on R98: https://raw.githack.com/pailhead/three.js/depth-peel-stencil/examples/webgl_materials_depthpeel.html I've added a block of GLSL to every surface shader that's present in the scene:
And with this, i'm able to compare the I've tried bumping the library, and noticed artifacts that appear when moving to R114, and it seems to be caused by this. If you could shed some light on how this change impacts my code i'd really appreciate it. I'm also curious, my demo never ran properly on iOS. I don't remember the discussion about this, but i know i've seen at least one issue where it was discussed. When running on an iphone, it looked like there was a serious precision mismatch between the target and the I always thought the texture is not being encoded propely (the discussion i think was about some storage format on ios), but with what this PR mentions, now i'm thinking that my If i can figure out a way to get rid of the artifacts, the way this PR works is, i could expect iOS to just work? |
So after this change you probably shouldn't compare gl_FragCoord.z directly to the RGBA encoded depth, since they have different precision. Instead you should compute a replacement value for gl_FragCoord.z similarly to how the encoded depth is computed in this patch. Adding a uniform set to zero shouldn't fix things either, but that might be disabling some faulty optimization in a driver. And yes, I think there was some confusion before - some people thought that encoding to RGBA didn't work properly, even though the root cause of the issue was the poor precision of gl_FragCoord.z on some platforms, like iOS. |
Omg, this answered some year long questions. Thank you! |
Just to be sure i understand, I should expect the equality check to pass in the shader, |
@Oletus i just want to send an infinite amount of gratitude your way. Just to confirm, not only that the equality check seems to work, but i can remove the |
(@mrdoob, @BarthaBRW) #17592 is supposed to fix all of this, and more. |
Some platforms compute gl_FragCoord at a lower precision which makes the manually computed value better for depth-based postprocessing effects.
The shader precisions that implementations report through the getShaderPrecisionFormat API are not reliable in practice, so the manually computed value is always used rather than trying to enable it conditionally on those platforms which have various shader precisions.
WebGL 2.0 should in principle compute gl_FragCoord at a higher precision, but this may also be affected by driver bugs on some platforms.
Improvement from this patch can be seen for example on iPad with A10 processor / iPadOS 13.3.1, when using depth based postprocessing effects on a scene with a high far/near plane ratio.