diff --git a/wgpu-core-remote-types/src/encoders.rs b/wgpu-core-remote-types/src/encoders.rs index a32b0012a..09000a6f8 100644 --- a/wgpu-core-remote-types/src/encoders.rs +++ b/wgpu-core-remote-types/src/encoders.rs @@ -6,6 +6,45 @@ use crate::{assert_ffi_safe, id, Label}; pub type RenderBundleDescriptor<'a> = wgt::RenderBundleDescriptor>; pub type CommandBufferDescriptor<'a> = wgt::CommandBufferDescriptor>; +/// Operation to perform to the output attachment at the start of a render pass. +/// +/// Corresponds to [WebGPU `GPULoadOp`](https://gpuweb.github.io/gpuweb/#enumdef-gpuloadop), +/// plus the corresponding clearValue. +#[repr(u8)] +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum LoadOp { + /// 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) = 0, + /// Loads the existing value for this attachment into the render pass. + Load = 1, +} + +impl LoadOp { + pub fn to_wgt(self) -> wgt::LoadOp { + match self { + Self::Clear(value) => wgt::LoadOp::Clear(value), + Self::Load => wgt::LoadOp::Load, + } + } + + pub fn map_clear_value(self, f: impl FnOnce(T) -> U) -> LoadOp { + match self { + Self::Clear(value) => LoadOp::Clear(f(value)), + Self::Load => LoadOp::Load, + } + } +} + /// Describes a color attachment to a render pass. /// /// Corresponds to [`GPURenderColorAttachment`](https://gpuweb.github.io/gpuweb/#dictdef-gpurenderpasscolorattachment) @@ -23,12 +62,12 @@ pub struct RenderPassColorAttachment { /// /// This must be clear if it is the first renderpass rendering to a swap /// chain image. - pub load_op: wgt::LoadOp, + pub load_op: LoadOp, /// Operation to perform to the output attachment at the end of a renderpass. pub store_op: wgt::StoreOp, } -//assert_ffi_safe!(RenderPassColorAttachment); +assert_ffi_safe!(RenderPassColorAttachment); /// Describes an individual channel within a render pass, such as color, depth, or stencil. /// @@ -42,7 +81,7 @@ pub struct PassChannel { /// /// This must be clear if it is the first renderpass rendering to a swap /// chain image. - pub load_op: FfiOption>, + pub load_op: FfiOption>, /// Operation to perform to the output attachment at the end of a renderpass. pub store_op: FfiOption, /// If true, the relevant channel is not changed by a renderpass, and the @@ -65,7 +104,7 @@ pub struct RenderPassDepthStencilAttachment { pub stencil: PassChannel>, } -//assert_ffi_safe!(RenderPassDepthStencilAttachment); +assert_ffi_safe!(RenderPassDepthStencilAttachment); /// Describes the writing of timestamp values in a render or compute pass. /// diff --git a/wgpu-core-remote/src/global/render_pass.rs b/wgpu-core-remote/src/global/render_pass.rs index d85e9426b..436abb829 100644 --- a/wgpu-core-remote/src/global/render_pass.rs +++ b/wgpu-core-remote/src/global/render_pass.rs @@ -14,14 +14,6 @@ use crate::global::Global; use crate::hub::Hub; use crate::id; -fn map_clear_value(load_op: wgt::LoadOp, f: impl FnOnce(V1) -> V2) -> wgt::LoadOp { - match load_op { - wgt::LoadOp::Clear(value) => wgt::LoadOp::Clear(f(value)), - wgt::LoadOp::Load => wgt::LoadOp::Load, - wgt::LoadOp::DontCare(token) => wgt::LoadOp::DontCare(token), - } -} - impl Global { /// Creates a render pass. /// @@ -61,7 +53,7 @@ impl Global { .resolve_target .as_ref() .map(|rt| texture_views.get(*rt)), - load_op: at.load_op, + load_op: at.load_op.to_wgt(), store_op: at.store_op, }) }) @@ -75,7 +67,7 @@ impl Global { .depth .load_op .to_std() - .map(|x| map_clear_value(x, FfiOption::to_std)), + .map(|x| x.map_clear_value(FfiOption::to_std).to_wgt()), store_op: at.depth.store_op.to_std(), read_only: at.depth.read_only, }, @@ -84,7 +76,7 @@ impl Global { .stencil .load_op .to_std() - .map(|x| map_clear_value(x, FfiOption::to_std)), + .map(|x| x.map_clear_value(FfiOption::to_std).to_wgt()), store_op: at.stencil.store_op.to_std(), read_only: at.stencil.read_only, },