From bbe2a86154122ee69884a1e9690533beacea21ae Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Fri, 21 Aug 2026 18:14:54 +0200 Subject: [PATCH] Make subtypes FFI safe Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu-core-remote-types/src/binding_model.rs | 5 +++- wgpu-core-remote-types/src/encoders.rs | 15 +++++----- wgpu-core-remote-types/src/ffi.rs | 32 +++++++++++++++++++++ wgpu-core-remote-types/src/lib.rs | 1 + wgpu-core-remote/src/global/compute_pass.rs | 4 +-- wgpu-core-remote/src/global/device.rs | 2 +- wgpu-core-remote/src/global/render_pass.rs | 31 +++++++++++++++----- 7 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 wgpu-core-remote-types/src/ffi.rs diff --git a/wgpu-core-remote-types/src/binding_model.rs b/wgpu-core-remote-types/src/binding_model.rs index 5cac34146..9b67a9594 100644 --- a/wgpu-core-remote-types/src/binding_model.rs +++ b/wgpu-core-remote-types/src/binding_model.rs @@ -2,6 +2,7 @@ use alloc::borrow::Cow; use serde::{Deserialize, Serialize}; +use crate::ffi::FfiOption; use crate::id::{BindGroupLayoutId, BufferId, ExternalTextureId, SamplerId, TextureViewId}; use crate::Label; @@ -19,10 +20,11 @@ pub struct BufferBinding { /// because JavaScript bindings cannot readily express `Option`. /// The `wgpu` API uses `Option` (i.e. `NonZeroU64`) for this /// field. - pub size: Option, + pub size: FfiOption, } /// Corresponds to [`GPUBindingResource`](https://www.w3.org/TR/webgpu/#typedefdef-gpubindingresource). +#[repr(C)] #[derive(Debug, Clone, Serialize, Deserialize)] pub enum BindingResource { Buffer(BufferBinding), @@ -34,6 +36,7 @@ pub enum BindingResource { /// Bindable resource and the slot to bind it to. /// /// This corresponds to [`GPUBindGroupEntry`](https://www.w3.org/TR/webgpu/#dictdef-gpubindgroupentry). +#[repr(C)] #[derive(Clone, Debug, Serialize, Deserialize)] pub struct BindGroupEntry { /// Slot for which binding provides resource. Corresponds to an entry of the same diff --git a/wgpu-core-remote-types/src/encoders.rs b/wgpu-core-remote-types/src/encoders.rs index 2d0a484e1..7045bb44f 100644 --- a/wgpu-core-remote-types/src/encoders.rs +++ b/wgpu-core-remote-types/src/encoders.rs @@ -1,5 +1,6 @@ use alloc::borrow::Cow; +use crate::ffi::FfiOption; use crate::{id, Label}; pub type RenderBundleDescriptor<'a> = wgt::RenderBundleDescriptor>; @@ -14,7 +15,7 @@ pub struct RenderPassColorAttachment { /// The view to use as an attachment. pub view: id::TextureViewId, /// The depth slice index of a 3D view. It must not be provided if the view is not 3D. - pub depth_slice: Option, + pub depth_slice: FfiOption, /// The view that will receive the resolved output if multisampling is used. pub resolve_target: Option, /// Operation to perform to the output attachment at the start of a @@ -39,9 +40,9 @@ pub struct PassChannel { /// /// This must be clear if it is the first renderpass rendering to a swap /// chain image. - pub load_op: Option>, + pub load_op: FfiOption>, /// Operation to perform to the output attachment at the end of a renderpass. - pub store_op: Option, + pub store_op: FfiOption, /// If true, the relevant channel is not changed by a renderpass, and the /// corresponding attachment can be used inside the pass by other read-only /// usages. @@ -57,9 +58,9 @@ 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>, + pub depth: PassChannel>, /// What operations will be performed on the stencil part of the attachment. - pub stencil: PassChannel>, + pub stencil: PassChannel>, } /// Describes the writing of timestamp values in a render or compute pass. @@ -71,9 +72,9 @@ pub struct PassTimestampWrites { /// The query set to write the timestamps to. pub query_set: id::QuerySetId, /// The index of the query set at which a start timestamp of this pass is written, if any. - pub beginning_of_pass_write_index: Option, + pub beginning_of_pass_write_index: FfiOption, /// The index of the query set at which an end timestamp of this pass is written, if any. - pub end_of_pass_write_index: Option, + pub end_of_pass_write_index: FfiOption, } /// Describes the attachments of a render pass. diff --git a/wgpu-core-remote-types/src/ffi.rs b/wgpu-core-remote-types/src/ffi.rs new file mode 100644 index 000000000..cafcf053b --- /dev/null +++ b/wgpu-core-remote-types/src/ffi.rs @@ -0,0 +1,32 @@ +/// FFI-friendly analogue of [`std::option::Option`]. +/// +/// In some cases, Rust's standard `Option` type is FFI-friendly, in that `T` and `Option` map +/// to the same C++ type. For example, both `&U` and `Option<&U>` can be represented as `U *` in +/// C++. +/// +/// For other types, `Option` may not be FFI-safe. For such cases, this type is a `repr(u8)` +/// analog to the standard +/// +/// See also: +#[repr(u8)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub enum FfiOption { + Some(T), + None, +} + +impl FfiOption { + pub fn to_std(self) -> std::option::Option { + match self { + Self::Some(value) => Some(value), + Self::None => None, + } + } + + pub fn as_ref(&self) -> std::option::Option<&T> { + match *self { + Self::Some(ref value) => Some(value), + Self::None => None, + } + } +} diff --git a/wgpu-core-remote-types/src/lib.rs b/wgpu-core-remote-types/src/lib.rs index eaed1114c..06a804fd5 100644 --- a/wgpu-core-remote-types/src/lib.rs +++ b/wgpu-core-remote-types/src/lib.rs @@ -27,5 +27,6 @@ pub mod identity; pub mod binding_model; pub mod encoders; +pub mod ffi; pub type Label<'a> = Option>; diff --git a/wgpu-core-remote/src/global/compute_pass.rs b/wgpu-core-remote/src/global/compute_pass.rs index 5f4389d63..f4b5e67f8 100644 --- a/wgpu-core-remote/src/global/compute_pass.rs +++ b/wgpu-core-remote/src/global/compute_pass.rs @@ -42,8 +42,8 @@ impl Global { .as_ref() .map(|tw| PassTimestampWrites { query_set: query_sets.get(tw.query_set), - beginning_of_pass_write_index: tw.beginning_of_pass_write_index, - end_of_pass_write_index: tw.end_of_pass_write_index, + beginning_of_pass_write_index: tw.beginning_of_pass_write_index.to_std(), + end_of_pass_write_index: tw.end_of_pass_write_index.to_std(), }), }; diff --git a/wgpu-core-remote/src/global/device.rs b/wgpu-core-remote/src/global/device.rs index c02b0f980..4db44c72a 100644 --- a/wgpu-core-remote/src/global/device.rs +++ b/wgpu-core-remote/src/global/device.rs @@ -525,7 +525,7 @@ impl Global { binding_model::BufferBinding { buffer, offset: bb.offset, - size: bb.size, + size: bb.size.to_std(), } }; let resolve_sampler = |id: &id::SamplerId| samplers.get(*id); diff --git a/wgpu-core-remote/src/global/render_pass.rs b/wgpu-core-remote/src/global/render_pass.rs index 325be84f1..d85e9426b 100644 --- a/wgpu-core-remote/src/global/render_pass.rs +++ b/wgpu-core-remote/src/global/render_pass.rs @@ -7,12 +7,21 @@ use wgpu_core::command::{ PassTimestampWrites, RenderPassColorAttachment, RenderPassDepthStencilAttachment, ResolvedRenderPassDescriptor, }; +use wgpu_core_remote_types::ffi::FfiOption; use wgt::{BufferAddress, BufferSize, Color, DynamicOffset, IndexFormat}; 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. /// @@ -47,7 +56,7 @@ impl Global { .map(|at| { at.as_ref().map(|at| RenderPassColorAttachment { view: texture_views.get(at.view), - depth_slice: at.depth_slice, + depth_slice: at.depth_slice.to_std(), resolve_target: at .resolve_target .as_ref() @@ -62,13 +71,21 @@ impl Global { RenderPassDepthStencilAttachment { view: texture_views.get(at.view), depth: wgpu_core::command::PassChannel { - load_op: at.depth.load_op, - store_op: at.depth.store_op, + load_op: at + .depth + .load_op + .to_std() + .map(|x| map_clear_value(x, FfiOption::to_std)), + store_op: at.depth.store_op.to_std(), read_only: at.depth.read_only, }, stencil: wgpu_core::command::PassChannel { - load_op: at.stencil.load_op, - store_op: at.stencil.store_op, + load_op: at + .stencil + .load_op + .to_std() + .map(|x| map_clear_value(x, FfiOption::to_std)), + store_op: at.stencil.store_op.to_std(), read_only: at.stencil.read_only, }, } @@ -78,8 +95,8 @@ impl Global { .as_ref() .map(|tw| PassTimestampWrites { query_set: query_sets.get(tw.query_set), - beginning_of_pass_write_index: tw.beginning_of_pass_write_index, - end_of_pass_write_index: tw.end_of_pass_write_index, + beginning_of_pass_write_index: tw.beginning_of_pass_write_index.to_std(), + end_of_pass_write_index: tw.end_of_pass_write_index.to_std(), }), occlusion_query_set: desc .occlusion_query_set