X Tutup
Skip to content

Use correct screen-space to ndc equation in Compatibility refraction#110684

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
Kaleb-Reid:fix-compat-refraction
Oct 6, 2025
Merged

Use correct screen-space to ndc equation in Compatibility refraction#110684
Repiteo merged 1 commit intogodotengine:masterfrom
Kaleb-Reid:fix-compat-refraction

Conversation

@Kaleb-Reid
Copy link
Contributor

Converting from screen-space coordinates to ndcs use a different equation in OpenGL as compared to Vulkan. This incorrect equation breaks refraction in Compatibility.

Closes #106213

@AThousandShips AThousandShips added bug topic:rendering topic:3d cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release cherrypick:4.5 Considered for cherry-picking into a future 4.5.x release labels Sep 19, 2025
@AThousandShips AThousandShips added this to the 4.6 milestone Sep 19, 2025
@AThousandShips AThousandShips requested a review from a team September 19, 2025 09:30
Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using RS::get_singleton()->is_low_end() will change the created code depending on the current rendering driver, which means that if you convert a BaseMaterial3D to a ShaderMaterial while using Compatibility, it will break on Forward+/Mobile and vice versa. This is an issue if you switch rendering methods during development, or even if an automatic fallback is applied on the user's device.

We should use the shader preprocessor instead, but it's currently broken when used in BaseMaterial3D (as I mentioned in #87486 (comment)).

@Kaleb-Reid
Copy link
Contributor Author

@Calinou Ah yes forgot about that. Unfortunately I don't think the shader preprocessor is ran for materials as they call RS::shader_create_from_code directly, which does not run the preprocessor as apposed to calling Shader::set_code which does and then calls RS::shader_create_from_code internally.

The way I am thinking of working around this would be:

  • Move code responsible for generating the shader to it's own function that can be called when converting to shader material / sending to RenderingServer.
  • Calling preprocessor manually in material.cpp before sending to RenderingServer.

Unsure if this is the best solution though.

@Kaleb-Reid Kaleb-Reid force-pushed the fix-compat-refraction branch from 5587a89 to 6faade1 Compare September 20, 2025 04:56
@Kaleb-Reid Kaleb-Reid requested a review from a team September 20, 2025 04:56
@Kaleb-Reid
Copy link
Contributor Author

Kaleb-Reid commented Sep 20, 2025

Alright so I've added a method to BaseMaterial3D which generates the shader code including the preprocessor directives so that when you press "Convert to ShaderMaterial" in the editor, instead of dumping the already processed shader code from RenderingServer, it generates new shader code with the directives still in it. The method is just code which used to be inside BaseMaterial3D::_update_shader, but with the updated fix for refraction. The shader code is also now preprocessed inside BaseMaterial3D::_update_shader.

I didn't add this to any of the other material types, but I can if it makes sense. If I need to then I could just make another pr which adds this method to all of the materials and remove it from this one.

EDIT: Went with clayjohn's solution of using OUTPUT_IS_SRGB instead to determine if Compatibility is being used.

@Kaleb-Reid Kaleb-Reid force-pushed the fix-compat-refraction branch from 6faade1 to 96ca7a2 Compare October 4, 2025 09:03
Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@clayjohn clayjohn removed cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release cherrypick:4.5 Considered for cherry-picking into a future 4.5.x release labels Oct 6, 2025
@Repiteo Repiteo merged commit 6ece891 into godotengine:master Oct 6, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Oct 6, 2025

Thanks!

@Kaleb-Reid Kaleb-Reid deleted the fix-compat-refraction branch October 6, 2025 20:21
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.

Refraction is broken in Compatibility renderer

5 participants

X Tutup