Skip to content

Commit

Permalink
[core] Allow depthClearValue to be empty (#6753)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagudev authored Dec 16, 2024
1 parent 0d927c2 commit f6fec82
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
4 changes: 2 additions & 2 deletions deno_webgpu/command_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ pub fn op_webgpu_command_encoder_begin_render_pass(
depth: wgpu_core::command::PassChannel {
load_op: attachment.depth_load_op,
store_op: attachment.depth_store_op,
clear_value: attachment.depth_clear_value,
clear_value: Some(attachment.depth_clear_value),
read_only: attachment.depth_read_only,
},
stencil: wgpu_core::command::PassChannel {
load_op: attachment.stencil_load_op,
store_op: attachment.stencil_store_op,
clear_value: attachment.stencil_clear_value,
clear_value: Some(attachment.stencil_clear_value),
read_only: attachment.stencil_read_only,
},
});
Expand Down
24 changes: 20 additions & 4 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<V: Default> Default for PassChannel<V, LoadOp, StoreOp> {
}
}

impl<V: Copy> PassChannel<V, Option<LoadOp>, Option<StoreOp>> {
impl<V: Copy + Default> PassChannel<Option<V>, Option<LoadOp>, Option<StoreOp>> {
fn resolve(&self) -> Result<PassChannel<V, LoadOp, StoreOp>, AttachmentError> {
let load_op = if self.read_only {
if self.load_op.is_some() {
Expand All @@ -160,7 +160,7 @@ impl<V: Copy> PassChannel<V, Option<LoadOp>, Option<StoreOp>> {
Ok(PassChannel {
load_op,
store_op,
clear_value: self.clear_value,
clear_value: self.clear_value.unwrap_or_default(),
read_only: self.read_only,
})
}
Expand Down Expand Up @@ -216,10 +216,11 @@ pub struct RenderPassDepthStencilAttachment {
/// The view to use as an attachment.
pub view: id::TextureViewId,
/// What operations will be performed on the depth part of the attachment.
pub depth: PassChannel<f32, Option<LoadOp>, Option<StoreOp>>,
pub depth: PassChannel<Option<f32>, Option<LoadOp>, Option<StoreOp>>,
/// What operations will be performed on the stencil part of the attachment.
pub stencil: PassChannel<u32, Option<LoadOp>, Option<StoreOp>>,
pub stencil: PassChannel<Option<u32>, Option<LoadOp>, Option<StoreOp>>,
}

/// Describes a depth/stencil attachment to a render pass.
#[derive(Debug)]
pub struct ArcRenderPassDepthStencilAttachment {
Expand Down Expand Up @@ -649,6 +650,10 @@ pub enum AttachmentError {
NoLoad,
#[error("Attachment without store")]
NoStore,
#[error("LoadOp is `Clear` but no clear value was provided")]
NoClearValue,
#[error("Clear value ({0}) must be between 0.0 and 1.0, inclusive")]
ClearValueOutOfRange(f32),
}

/// Error encountered when performing a render pass.
Expand Down Expand Up @@ -1473,6 +1478,17 @@ impl Global {
)));
}

// If this.depthLoadOp is "clear", this.depthClearValue must be provided and must be between 0.0 and 1.0, inclusive.
if depth_stencil_attachment.depth.load_op == Some(LoadOp::Clear) {
if let Some(clear_value) = depth_stencil_attachment.depth.clear_value {
if !(0.0..=1.0).contains(&clear_value) {
return Err(CommandEncoderError::InvalidAttachment(AttachmentError::ClearValueOutOfRange(clear_value)));
}
} else {
return Err(CommandEncoderError::InvalidAttachment(AttachmentError::NoClearValue));
}
}

Some(ArcRenderPassDepthStencilAttachment {
view,
depth: if format.has_depth_aspect() {
Expand Down
14 changes: 6 additions & 8 deletions wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,14 @@ fn map_store_op(op: StoreOp) -> wgc::command::StoreOp {
}
}

fn map_load_op<V: Default>(op: LoadOp<V>) -> (wgc::command::LoadOp, V) {
fn map_load_op<V>(op: LoadOp<V>) -> (wgc::command::LoadOp, Option<V>) {
match op {
LoadOp::Clear(v) => (wgc::command::LoadOp::Clear, v),
LoadOp::Load => (wgc::command::LoadOp::Load, V::default()),
LoadOp::Clear(v) => (wgc::command::LoadOp::Clear, Some(v)),
LoadOp::Load => (wgc::command::LoadOp::Load, None),
}
}

fn map_pass_channel<V: Copy + Default>(
ops: Option<&Operations<V>>,
) -> wgc::command::PassChannel<V> {
fn map_pass_channel<V: Copy>(ops: Option<&Operations<V>>) -> wgc::command::PassChannel<Option<V>> {
match ops {
Some(&Operations { load, store }) => {
let (load_op, clear_value) = map_load_op(load);
Expand All @@ -427,7 +425,7 @@ fn map_pass_channel<V: Copy + Default>(
None => wgc::command::PassChannel {
load_op: None,
store_op: None,
clear_value: V::default(),
clear_value: None,
read_only: true,
},
}
Expand Down Expand Up @@ -2254,7 +2252,7 @@ impl dispatch::CommandEncoderInterface for CoreCommandEncoder {
resolve_target: at.resolve_target.map(|view| view.inner.as_core().id),
load_op,
store_op: map_store_op(at.ops.store),
clear_value,
clear_value: clear_value.unwrap_or_default(),
}
})
})
Expand Down

0 comments on commit f6fec82

Please sign in to comment.