Make subtypes FFI safe

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
sagudev authored and Teodor Tanasoaia committed 2026-08-25 19:48:47 +02:00
1 parent 45ace31cb9
commit bbe2a86154
7 files changed
+72 -18

No files matched your search

+4 -1
View File
@@ -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<NonZeroU64>`.
/// The `wgpu` API uses `Option<BufferSize>` (i.e. `NonZeroU64`) for this
/// field.
pub size: Option<wgt::BufferAddress>,
pub size: FfiOption<wgt::BufferAddress>,
}
/// 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
+8 -7
View File
@@ -1,5 +1,6 @@
use alloc::borrow::Cow;
use crate::ffi::FfiOption;
use crate::{id, Label};
pub type RenderBundleDescriptor<'a> = wgt::RenderBundleDescriptor<Label<'a>>;
@@ -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<u32>,
pub depth_slice: FfiOption<u32>,
/// The view that will receive the resolved output if multisampling is used.
pub resolve_target: Option<id::TextureViewId>,
/// Operation to perform to the output attachment at the start of a
@@ -39,9 +40,9 @@ pub struct PassChannel<V> {
///
/// This must be clear if it is the first renderpass rendering to a swap
/// chain image.
pub load_op: Option<wgt::LoadOp<V>>,
pub load_op: FfiOption<wgt::LoadOp<V>>,
/// Operation to perform to the output attachment at the end of a renderpass.
pub store_op: Option<wgt::StoreOp>,
pub store_op: FfiOption<wgt::StoreOp>,
/// 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<Option<f32>>,
pub depth: PassChannel<FfiOption<f32>>,
/// What operations will be performed on the stencil part of the attachment.
pub stencil: PassChannel<Option<u32>>,
pub stencil: PassChannel<FfiOption<u32>>,
}
/// 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<u32>,
pub beginning_of_pass_write_index: FfiOption<u32>,
/// 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<u32>,
pub end_of_pass_write_index: FfiOption<u32>,
}
/// Describes the attachments of a render pass.
+32
View File
@@ -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<T>` map
/// to the same C++ type. For example, both `&U` and `Option<&U>` can be represented as `U *` in
/// C++.
///
/// For other types, `Option<T>` may not be FFI-safe. For such cases, this type is a `repr(u8)`
/// analog to the standard
///
/// See also: <https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization>
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum FfiOption<T> {
Some(T),
None,
}
impl<T> FfiOption<T> {
pub fn to_std(self) -> std::option::Option<T> {
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,
}
}
}
+1
View File
@@ -27,5 +27,6 @@ pub mod identity;
pub mod binding_model;
pub mod encoders;
pub mod ffi;
pub type Label<'a> = Option<Cow<'a, str>>;
+2 -2
View File
@@ -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(),
}),
};
+1 -1
View File
@@ -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);
+24 -7
View File
@@ -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<V1, V2>(load_op: wgt::LoadOp<V1>, f: impl FnOnce(V1) -> V2) -> wgt::LoadOp<V2> {
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