From 30e510c6d0b0224581ac749a6da47ddfa319dfe2 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 19 May 2022 18:45:36 +0200 Subject: [PATCH 1/2] Fix clearing depth and stencil at the same time --- wgpu-hal/src/gles/command.rs | 13 ++++++++++--- wgpu-hal/src/gles/mod.rs | 1 + wgpu-hal/src/gles/queue.rs | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/wgpu-hal/src/gles/command.rs b/wgpu-hal/src/gles/command.rs index e316844b66..f0f6028c62 100644 --- a/wgpu-hal/src/gles/command.rs +++ b/wgpu-hal/src/gles/command.rs @@ -520,12 +520,19 @@ impl crate::CommandEncoder for super::CommandEncoder { } } if let Some(ref dsat) = desc.depth_stencil_attachment { - if !dsat.depth_ops.contains(crate::AttachmentOps::LOAD) { + let clear_depth = !dsat.depth_ops.contains(crate::AttachmentOps::LOAD); + let clear_stencil = !dsat.stencil_ops.contains(crate::AttachmentOps::LOAD); + + if clear_depth && clear_stencil { + self.cmd_buffer.commands.push(C::ClearDepthAndStencil( + dsat.clear_value.0, + dsat.clear_value.1, + )); + } else if clear_depth { self.cmd_buffer .commands .push(C::ClearDepth(dsat.clear_value.0)); - } - if !dsat.stencil_ops.contains(crate::AttachmentOps::LOAD) { + } else if clear_stencil { self.cmd_buffer .commands .push(C::ClearStencil(dsat.clear_value.1)); diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index 321db19027..f214db1170 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -667,6 +667,7 @@ enum Command { ClearColorI(u32, [i32; 4]), ClearDepth(f32), ClearStencil(u32), + ClearDepthAndStencil(f32, u32), BufferBarrier(glow::Buffer, crate::BufferUses), TextureBarrier(crate::TextureUses), SetViewport { diff --git a/wgpu-hal/src/gles/queue.rs b/wgpu-hal/src/gles/queue.rs index c223c2a851..fdc9268676 100644 --- a/wgpu-hal/src/gles/queue.rs +++ b/wgpu-hal/src/gles/queue.rs @@ -758,6 +758,9 @@ impl super::Queue { C::ClearStencil(value) => { gl.clear_buffer_i32_slice(glow::STENCIL, 0, &[value as i32]); } + C::ClearDepthAndStencil(depth, stencil_value) => { + gl.clear_buffer_depth_stencil(glow::DEPTH_STENCIL, 0, depth, stencil_value as i32); + } C::BufferBarrier(raw, usage) => { let mut flags = 0; if usage.contains(crate::BufferUses::VERTEX) { From 67bf8e7c25eefd7ffbb1b433ddeac1dfabfb7fec Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 19 May 2022 22:18:28 +0200 Subject: [PATCH 2/2] Add some documentation --- wgpu-hal/src/gles/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index f214db1170..338cbca774 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -667,6 +667,10 @@ enum Command { ClearColorI(u32, [i32; 4]), ClearDepth(f32), ClearStencil(u32), + // Clearing both the depth and stencil buffer individually appears to + // result in the stencil buffer failing to clear, atleast in WebGL. + // It is also more efficient to emit a single command instead of two for + // this. ClearDepthAndStencil(f32, u32), BufferBarrier(glow::Buffer, crate::BufferUses), TextureBarrier(crate::TextureUses),