Skip to content

Commit

Permalink
New StoreOp enum, improve LoadOp documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Sep 16, 2023
1 parent 7c575a0 commit 941c596
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
21 changes: 10 additions & 11 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
BufferDescriptor, CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor,
DownlevelCapabilities, Features, Label, Limits, LoadOp, MapMode, Operations,
PipelineLayoutDescriptor, RenderBundleEncoderDescriptor, RenderPipelineDescriptor,
SamplerDescriptor, ShaderModuleDescriptor, ShaderModuleDescriptorSpirV, ShaderSource,
SamplerDescriptor, ShaderModuleDescriptor, ShaderModuleDescriptorSpirV, ShaderSource, StoreOp,
SurfaceStatus, TextureDescriptor, TextureViewDescriptor, UncapturedErrorHandler,
};

Expand Down Expand Up @@ -392,6 +392,13 @@ fn map_texture_tagged_copy_view(
}
}

fn map_store_op(op: StoreOp) -> wgc::command::StoreOp {
match op {
StoreOp::Store => wgc::command::StoreOp::Store,
StoreOp::Discard => wgc::command::StoreOp::Discard,
}
}

fn map_pass_channel<V: Copy + Default>(
ops: Option<&Operations<V>>,
) -> wgc::command::PassChannel<V> {
Expand All @@ -401,11 +408,7 @@ fn map_pass_channel<V: Copy + Default>(
store,
}) => wgc::command::PassChannel {
load_op: wgc::command::LoadOp::Clear,
store_op: if store {
wgc::command::StoreOp::Store
} else {
wgc::command::StoreOp::Discard
},
store_op: map_store_op(store),
clear_value,
read_only: false,
},
Expand All @@ -414,11 +417,7 @@ fn map_pass_channel<V: Copy + Default>(
store,
}) => wgc::command::PassChannel {
load_op: wgc::command::LoadOp::Load,
store_op: if store {
wgc::command::StoreOp::Store
} else {
wgc::command::StoreOp::Discard
},
store_op: map_store_op(store),
clear_value: V::default(),
read_only: false,
},
Expand Down
42 changes: 34 additions & 8 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,16 +1006,22 @@ static_assertions::assert_impl_all!(BufferBinding: Send, Sync);

/// Operation to perform to the output attachment at the start of a render pass.
///
/// The render target must be cleared at least once before its content is loaded.
///
/// Corresponds to [WebGPU `GPULoadOp`](https://gpuweb.github.io/gpuweb/#enumdef-gpuloadop).
/// Corresponds to [WebGPU `GPULoadOp`](https://gpuweb.github.io/gpuweb/#enumdef-gpuloadop),
/// plus the corresponding clearValue.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "trace", derive(serde::Serialize))]
#[cfg_attr(feature = "replay", derive(serde::Deserialize))]
pub enum LoadOp<V> {
/// Clear with a specified value.
/// Loads the specified value for this attachment into the render pass.
///
/// On some GPU hardware (primarily mobile), "clear" is significantly cheaper
/// because it avoids loading data from main memory into tile-local memory.
/// On other GPU hardware, there isn’t a significant difference.
/// As a result, it is recommended to use "clear" rather than "load" in cases
/// where the initial value doesn’t matter
/// (e.g. the render target will be cleared using a skybox).
Clear(V),
/// Load from memory.
/// Loads the existing value for this attachment into the render pass.
Load,
}

Expand All @@ -1025,6 +1031,25 @@ impl<V: Default> Default for LoadOp<V> {
}
}

/// Operation to perform to the output attachment at the end of a render pass.
///
/// Corresponds to [WebGPU `GPUStoreOp`](https://gpuweb.github.io/gpuweb/#enumdef-gpustoreop).
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Default)]
#[cfg_attr(feature = "trace", derive(serde::Serialize))]
#[cfg_attr(feature = "replay", derive(serde::Deserialize))]
pub enum StoreOp {
/// Stores the resulting value of the render pass for this attachment.
#[default]
Store,
/// Discards the resulting value of the render pass for this attachment.
///
/// The attachment/affected texture aspect(s) will be treated as uninitialized afterwards.
///
/// This can be significantly faster on tile-based render hardware.
/// Prefer this if the attachment is not read by subsequent passes.
Discard,
}

/// Pair of load and store operations for an attachment aspect.
///
/// This type is unique to the Rust API of `wgpu`. In the WebGPU specification,
Expand All @@ -1036,14 +1061,15 @@ pub struct Operations<V> {
/// How data should be read through this attachment.
pub load: LoadOp<V>,
/// Whether data will be written to through this attachment.
pub store: bool,
pub store: StoreOp,
}

impl<V: Default> Default for Operations<V> {
#[inline]
fn default() -> Self {
Self {
load: Default::default(),
store: true,
load: LoadOp::<V>::default(),
store: StoreOp::default(),
}
}
}
Expand Down

0 comments on commit 941c596

Please sign in to comment.