Skip to content
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

Merged

Conversation

Namey5
Copy link
Contributor

@Namey5 Namey5 commented Dec 13, 2024

WorldPositionFromDepth Visual Shader node was only normalizing xyz components after transforming from NDC -> view-space, then transforming view-space -> world-space using the still homogenous w 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.

working world position

Fixes #100345

@Namey5 Namey5 requested a review from a team as a code owner December 13, 2024 07:58
@AThousandShips AThousandShips added this to the 4.4 milestone Dec 13, 2024
Comment on lines 1751 to 1753
} 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";
}
Copy link
Contributor

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.

Copy link
Contributor Author

@Namey5 Namey5 Dec 13, 2024

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.

Copy link
Contributor

@Flarkk Flarkk Dec 13, 2024

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 :

bool RendererCompositor::low_end = false;

I'm wondering if we shouldn't completely remove this branch.
Anybody please step in.

Copy link
Contributor Author

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:

low_end = true;

Copy link
Contributor

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 😄

Copy link
Contributor Author

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.

Copy link
Contributor

@tetrapod00 tetrapod00 Dec 13, 2024

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.

Copy link
Contributor Author

@Namey5 Namey5 Dec 13, 2024

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.

Copy link
Contributor

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

Copy link
Contributor

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.

@akien-mga akien-mga changed the title fix: space transformations in WorldPositionFromDepth visual shader node generation Fix space transformations in WorldPositionFromDepth visual shader node generation Dec 13, 2024
@Namey5 Namey5 force-pushed the visualshader-worldposfromdepth-fix branch from 6288e94 to 01c7ecb Compare December 13, 2024 20:25
@Namey5 Namey5 force-pushed the visualshader-worldposfromdepth-fix branch from 01c7ecb to a85b027 Compare December 13, 2024 21:52
@michaelharmonart
Copy link

any changes that need to be made here, or are we just waiting for review and merge at this point?

@tetrapod00
Copy link
Contributor

This is waiting on a review from the shaders team, at least, and whatever other changes they will ask for, if any.

@maxpiepenbrink
Copy link
Contributor

maxpiepenbrink commented Jan 22, 2025

Just pulled, built & tested this branch with a simple distance field alpha glow effect and it worked as expected for Forward+, Mobile and Compatibility. Windows 10, GeForce 3090 32.0.15.6614
godot windows editor x86_64_qZ4jwPgFyF

godot.windows.editor.x86_64_NYqUcyNxQQ.mp4

Copy link
Contributor

@paddy-exe paddy-exe left a 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!

Copy link
Contributor

@tetrapod00 tetrapod00 left a 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.

@Repiteo Repiteo merged commit e62be79 into godotengine:master Jan 22, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Jan 22, 2025

Thanks!

@Namey5 Namey5 deleted the visualshader-worldposfromdepth-fix branch January 23, 2025 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Visual Shader WorldPositionFromDepth not working correctly
8 participants