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

Simplify depth reprojection code #99755

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Flarkk
Copy link
Contributor

@Flarkk Flarkk commented Nov 27, 2024

This PR rewrites ndc-to-view space depth reprojection code in all effects and environment shaders : GI, SSS, SSR, SSIL, SSAO, SS downsampler, Bokeh, Copy effects and Cubemap to DP

The new code typically looks like this :

float depth = texture(source_depth, uv).r;
depth = (proj_zw[1][0] - proj_zw[1][1] * depth) / (proj_zw[0][1] * depth - proj_zw[0][0]);
// with `proj_zw` a `mat2` that is the bottom-right 2x2 corner of the projection matrix.

It replaces old reprojection code which typically looks like that :

float depth = texture(source_depth, uv).r;
depth = depth * 2.0 - 1.0;
if (is_orthogonal) {
	depth = ((depth + (z_far + z_near) / (z_far - z_near)) * (z_far - z_near)) / 2.0;
} else {
	depth = 2.0 * z_near * z_far / (z_far + z_near - depth * (z_far - z_near));
}

There are some slight variations depending on whether the shader expects negative or positive depth, and whether the ndc depth is reversed.

Benefits

1. The new code doesn't depend on camera attributes anymore, which avoids issues under certain circumstances.

Camera attributes are typically retrieved from the projection matrix (with Projection::is_orthogonal(), Projection::get_z_far(), ...) then passed to the shaders.
The above functions can break under certain circumstances.
Typically when zfar is much bigger than znear, the perspective matrix that derives from it is infinite, then it becomes mathematically impossible to retrieve zfar back.

This change is part of the groundwork I'm carrying out to support wider ranges of zfar / znear ratios in single precision (today limited to ~1e6, typically zfar = 10km with znear = 0.01) which is important in open worlds and space games.

2. It prepares the support of custom projection matrices where it cannot be determined whether the projection is orthographic (for example with blended perspective and orthographic projections)

Non-regression tests

Test project : reprojection.zip

There shouldn't be any visual change after this PR except for SSS, which is broken in master (see #99220 - this PR fixes it too). The screen captures for SSS below are made with 4.2. [edit : #99220 was merged]

Visual reference before this PR (all visuals do match after the PR - see test project) :

GI SSS SSR SSIL SSAO Bokeh Sky Shadows
Persp Capture d’écran du 2024-11-27 10-33-09 Capture d’écran du 2024-11-27 11-18-55 Capture d’écran du 2024-11-27 12-00-22 Capture d’écran du 2024-11-27 10-27-06 Capture d’écran du 2024-11-27 11-31-48 Capture d’écran du 2024-11-27 10-27-15 Capture d’écran du 2024-11-27 13-24-04 Capture d’écran du 2024-11-27 11-53-22
Ortho Capture d’écran du 2024-11-27 15-55-06 Capture d’écran du 2024-11-27 15-56-05 Capture d’écran du 2024-11-27 15-53-15 Capture d’écran du 2024-11-27 15-53-39 Capture d’écran du 2024-11-27 15-59-03 Capture d’écran du 2024-11-27 15-55-20 Capture d’écran du 2024-11-27 15-54-20 Capture d’écran du 2024-11-27 15-54-37

@Flarkk Flarkk requested a review from a team as a code owner November 27, 2024 12:57
@AThousandShips AThousandShips added this to the 4.x milestone Nov 27, 2024
@Flarkk Flarkk force-pushed the simplify_reprojection branch 2 times, most recently from ac68e34 to 5351e95 Compare December 2, 2024 12:18
@Flarkk
Copy link
Contributor Author

Flarkk commented Dec 2, 2024

Rebased on top of #99220

Covers GI, SSS, SSR, SSIL, SSAO, SS downsampler, Bokeh, Copy effects, Cubemap to DP
@Flarkk Flarkk force-pushed the simplify_reprojection branch from 5351e95 to ec095c0 Compare December 15, 2024 17:46
@Flarkk
Copy link
Contributor Author

Flarkk commented Dec 15, 2024

Rebased

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.

2 participants