From ca767f7d74de0a91339da89ac81d3914001ec6c4 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Tue, 23 Jul 2024 21:55:46 -0400 Subject: [PATCH] WIP: feat: only require depth config. for fmts. w/ depth TODO: Ensure that the following changes and discussion get represented in code: - https://github.com/gpuweb/gpuweb/pull/4318/files - https://github.com/gpuweb/gpuweb/issues/3905 - https://github.com/gpuweb/gpuweb/pull/3849/files#r1122411287 --- wgpu-core/src/device/resource.rs | 19 +++++++++++++++++-- wgpu-types/src/lib.rs | 16 ++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index afbf73bc03..509a0b5a83 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -3146,8 +3146,23 @@ impl Device { } let aspect = hal::FormatAspects::from(ds.format); - if ds.is_depth_enabled() && !aspect.contains(hal::FormatAspects::DEPTH) { - break 'error Some(pipeline::DepthStencilStateError::FormatNotDepth(ds.format)); + match ( + aspect.contains(hal::FormatAspects::DEPTH), + ds.depth_write_enabled.zip(ds.depth_compare), + ) { + (true, Some(_)) | (false, None) => (), + (true, None) => { + break 'error Some(pipeline::DepthStencilStateError::DepthOpsNotSpecified { + format: ds.format, + depth_write_enabled: ds.depth_write_enabled, + depth_compare: ds.depth_compare, + }) + } + (false, Some(_)) => { + break 'error Some(pipeline::DepthStencilStateError::FormatNotDepth( + ds.format, + )) + } } if ds.stencil.is_enabled() && !aspect.contains(hal::FormatAspects::STENCIL) { break 'error Some(pipeline::DepthStencilStateError::FormatNotStencil( diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 92ff6da74e..a884487e3c 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -4884,10 +4884,12 @@ pub struct DepthStencilState { /// /// [CEbrp]: ../wgpu/struct.CommandEncoder.html#method.begin_render_pass pub format: TextureFormat, - /// If disabled, depth will not be written to. - pub depth_write_enabled: bool, - /// Comparison function used to compare depth values in the depth test. - pub depth_compare: CompareFunction, + /// If disabled, depth will not be written to. Must be specified if `format` has a depth + /// component. + pub depth_write_enabled: Option, + /// Comparison function used to compare depth values in the depth test. Must be specified if + /// `format` has a depth component. + pub depth_compare: Option, /// Stencil state. #[cfg_attr(feature = "serde", serde(default))] pub stencil: StencilState, @@ -4900,13 +4902,15 @@ impl DepthStencilState { /// Returns true if the depth testing is enabled. #[must_use] pub fn is_depth_enabled(&self) -> bool { - self.depth_compare != CompareFunction::Always || self.depth_write_enabled + self.depth_compare + .map_or(false, |dc| dc != CompareFunction::Always) + || self.depth_write_enabled } /// Returns true if the state doesn't mutate the depth buffer. #[must_use] pub fn is_depth_read_only(&self) -> bool { - !self.depth_write_enabled + self.depth_write_enabled.map_or(false, |enabled| !enabled) } /// Returns true if the state doesn't mutate the stencil.