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 c08c123
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/re_renderer/shader/device_info.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// True if we're running on Gles/WebGL.
// (patched accordingly if necessary)
const GLES = false;
13 changes: 12 additions & 1 deletion crates/re_renderer/shader/utils/depth_offset.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <../global_bindings.wgsl>
#import <../types.wgsl>
#import <../device_info.wgsl>

fn apply_depth_offset(position: Vec4, offset: f32) -> Vec4 {
// We're using inverse z, i.e. 0.0 is far, 1.0 is near.
Expand All @@ -16,7 +17,17 @@ 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;
var eps = 5.0 * f32eps;

if GLES {
// On GLES/WebGL, the NDC clipspace range for depth is from -1 to 1 and y is flipped.
// 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 completely messes up depth precision, in particular since we use
// an inverse depth projection that tries to make use of the high float precision closer to zero.
eps *= 1000.0;
}

return Vec4(
position.xy,
Expand Down
5 changes: 5 additions & 0 deletions crates/re_renderer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ impl RenderContext {
"@invariant @builtin(position)".to_owned(),
"@builtin(position)".to_owned(),
));
} else if adapter.get_info().backend == wgpu::Backend::Gl {
gpu_resources
.shader_modules
.shader_text_workaround_replacements
.push(("GLES = false".to_owned(), "GLES = true".to_owned()));
}

RenderContext {
Expand Down
6 changes: 6 additions & 0 deletions crates/re_renderer/src/workspace_shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ pub fn init() {
fs.create_file(virtpath, content).unwrap();
}

{
let virtpath = Path::new("shader/device_info.wgsl");
let content = include_str!("../shader/device_info.wgsl").into();
fs.create_file(virtpath, content).unwrap();
}

{
let virtpath = Path::new("shader/generic_skybox.wgsl");
let content = include_str!("../shader/generic_skybox.wgsl").into();
Expand Down

0 comments on commit c08c123

Please sign in to comment.