Skip to content

Commit

Permalink
fudge depth offsets on Webgl
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed May 17, 2023
1 parent 8a50ff2 commit fd6e25c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/re_renderer/shader/utils/depth_offset.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ fn apply_depth_offset(position: Vec4, offset: f32) -> Vec4 {

// 1.0 * eps _should_ be enough, but in practice it causes Z-fighting for unknown reasons.
// Maybe because of GPU interpolation of vertex coordinates?
let eps = 5.0 * f32eps;
let BACKEND_SPECIFIC_DEPTH_FUDGE_FACTOR = 1.0; // Patched by shader processing on WebGL.
let eps = BACKEND_SPECIFIC_DEPTH_FUDGE_FACTOR * 5.0 * f32eps;

return Vec4(
position.xy,
Expand Down
13 changes: 13 additions & 0 deletions crates/re_renderer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ impl RenderContext {
"@invariant @builtin(position)".to_owned(),
"@builtin(position)".to_owned(),
));
} else if adapter.get_info().backend == wgpu::Backend::Gl {
// On GLES/WebGL, the NDC clipspace range for depth is from -1 to 1.
// wgpu/Naga counteracts this by patching all vertex shader with:
// "gl_Position.yz = vec2(-gl_Position.y, gl_Position.z * 2.0 - gl_Position.w);",
// This is great, since it means that we can pretend depth is 0 to 1 as specified by WebGPU.
// But it considerably messes up depth precision.
gpu_resources
.shader_modules
.shader_text_workaround_replacements
.push((
"let BACKEND_SPECIFIC_DEPTH_FUDGE_FACTOR = 1.0;".to_owned(),
"let BACKEND_SPECIFIC_DEPTH_FUDGE_FACTOR = 100.0;".to_owned(),
));
}

RenderContext {
Expand Down

0 comments on commit fd6e25c

Please sign in to comment.