-
-
Notifications
You must be signed in to change notification settings - Fork 21.9k
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
Fix space transformations in WorldPositionFromDepth visual shader node generation #100350
Fix space transformations in WorldPositionFromDepth visual shader node generation #100350
Conversation
} else { | ||
code += " vec4 __depth_view = INV_PROJECTION_MATRIX * vec4(vec3(" + uv + ", __log_depth) * 2.0 - 1.0, 1.0);\n"; | ||
code += " vec4 __ndc = vec4(vec3(" + uv + ", __log_depth) * 2.0 - 1.0, 1.0);\n"; | ||
} |
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.
Is z still supposed to be not reversed in low end ?
I thought reverse-z was introduced in all 3 renderers.
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 believe this isn't reverse-z, but rather the difference in how z is represented in NDC-space between OpenGL [-1, 1] and everything else [0, 1]
https://gamedev.stackexchange.com/questions/29018/why-does-opengl-require-all-coordinates-in-1-1-ndc
I'm not sure specifically what Godot does with depth in the OpenGL renderer (without a floating point depth buffer in [0,1] range reverse-z wouldn't really help, but it also wouldn't hurt and would keep things consistent) but these changes do appear to work in all 3 renderers.
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.
It seems like is_low_end()
returns false
in all cases :
godot/servers/rendering/renderer_compositor.cpp
Lines 39 to 40 in ba2c5c1
bool RendererCompositor::low_end = false; | |
I'm wondering if we shouldn't completely remove this branch.
Anybody please step in.
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.
It does appear to be overridden in the GLES3 backend:
godot/drivers/gles3/rasterizer_gles3.h
Line 124 in ba2c5c1
low_end = true; |
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.
Ah yeah good catch.
But isn't GLES supposed to have [0,1] Zs too ? Sorry if this brings confusion, but I'm confused myself now 😄
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 it be worth adding a comment for this branch directly in generate_code()
just to make it clearer? The same NDC branch is used in a couple of other nodes in this file.
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.
Do you mean a comment in the C++ code or a comment in the generated shader (that will be visible to end users)? Either way I'm not opposed, but I'm also not sure if that's idiomatic for these visual shader nodes. So let's also get a second opinion.
If you add a comment for it, I think you should only add it to this specific shader node, not the others, to keep the changes localized.
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.
Yeah just a comment in the C++ code, the branch wouldn't exist in the generated output. I think most of the confusion is the use of RenderingServer::is_low_end()
- would be nice if there was an equivalent to the above mentioned CLIP_SPACE_FAR
in C++ (or even better-er to use CLIP_SPACE_FAR
directly in the generated output itself so people copying it get correct outputs regardless of renderer).
I'll leave it as-is for now though as it does match the behaviour in the existing nodes.
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 will make it a bit clearer already #100384
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 think it's fine to defer the potential comments or other code changes for later.
6288e94
to
01c7ecb
Compare
01c7ecb
to
a85b027
Compare
any changes that need to be made here, or are we just waiting for review and merge at this point? |
This is waiting on a review from the shaders team, at least, and whatever other changes they will ask for, if any. |
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.
Looks good. Not sure how that slipped through my PR tests when I added that node. Thanks for making the PR and fixing it!
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.
While I'm not a member of the shaders team, this looks good to me too, as far as my user's understanding of shaders goes. The new formula matches the one in the documentation, and the variable names are more accurate.
I also tested locally with multiple renderers and it works as expected.
Thanks! |
WorldPositionFromDepth
Visual Shader node was only normalizingxyz
components after transforming from NDC -> view-space, then transforming view-space -> world-space using the still homogenousw
component, giving incorrect results.Fix would be to simply divide
__depth_view.xyzw / __depth_view.w
before view->world transformation, however at the recommendation of @tetrapod00 I have opted to align the node's generated code with the example given in Advanced post-processing.Fixes #100345