From 4689fa4ccba86c3f9f0a3b7b40cc2d0b93e64f8a Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Sat, 12 Sep 2026 17:12:51 +0200 Subject: [PATCH] [remote-types] make `BindGroupLayoutEntry` FFI safe Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu-core-remote-types/src/binding_model.rs | 66 ++++++--------------- wgpu-core-remote/src/global/device.rs | 37 +++++++----- wgpu-types/src/binding.rs | 1 + wgpu-types/src/texture.rs | 2 + 4 files changed, 44 insertions(+), 62 deletions(-) diff --git a/wgpu-core-remote-types/src/binding_model.rs b/wgpu-core-remote-types/src/binding_model.rs index 407131223..8a950c1d9 100644 --- a/wgpu-core-remote-types/src/binding_model.rs +++ b/wgpu-core-remote-types/src/binding_model.rs @@ -71,6 +71,7 @@ pub struct BindGroupDescriptor<'a> { /// /// Corresponds to [WebGPU `GPUBufferBindingLayout`]( /// https://gpuweb.github.io/gpuweb/#dictdef-gpubufferbindinglayout). +#[repr(C)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct BufferBindingLayout { /// Sub-type of the buffer binding. @@ -108,6 +109,8 @@ pub struct BufferBindingLayout { pub min_binding_size: Option, } +assert_ffi_safe!(BufferBindingLayout); + /// A sampler that can be used to sample a texture. /// /// Example WGSL syntax: @@ -124,12 +127,15 @@ pub struct BufferBindingLayout { /// /// Corresponds to [WebGPU `GPUSamplerBindingLayout`]( /// https://gpuweb.github.io/gpuweb/#dictdef-gpusamplerbindinglayout). +#[repr(C)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct SamplerBindingLayout { /// Type of a sampler binding. pub ty: wgt::SamplerBindingType, } +assert_ffi_safe!(SamplerBindingLayout); + /// A texture binding. /// /// Example WGSL syntax: @@ -146,6 +152,7 @@ pub struct SamplerBindingLayout { /// /// Corresponds to [WebGPU `GPUTextureBindingLayout`]( /// https://gpuweb.github.io/gpuweb/#dictdef-gputexturebindinglayout). +#[repr(C)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct TextureBindingLayout { /// Sample type of the texture binding. @@ -158,6 +165,8 @@ pub struct TextureBindingLayout { pub multisampled: bool, } +assert_ffi_safe!(TextureBindingLayout); + /// A storage texture. /// /// Example WGSL syntax: @@ -176,6 +185,7 @@ pub struct TextureBindingLayout { /// /// Corresponds to [WebGPU `GPUStorageTextureBindingLayout`]( /// https://gpuweb.github.io/gpuweb/#dictdef-gpustoragetexturebindinglayout). +#[repr(C)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct StorageTextureBindingLayout { /// Allowed access to this texture. @@ -186,53 +196,13 @@ pub struct StorageTextureBindingLayout { pub view_dimension: wgt::TextureViewDimension, } -/// A ray-tracing acceleration structure binding. -/// -/// Example WGSL syntax: -/// ```rust,ignore -/// @group(0) @binding(0) -/// var as: acceleration_structure; -/// ``` -/// -/// or with vertex return enabled -/// ```rust,ignore -/// @group(0) @binding(0) -/// var as: acceleration_structure; -/// ``` -/// -/// Example GLSL syntax: -/// ```cpp,ignore -/// layout(binding = 0) -/// uniform accelerationStructureEXT as; -/// ``` -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] -pub struct AccelerationStructureBindingLayout { - /// Whether this acceleration structure can be used to - /// create a ray query that has flag vertex return in the shader - /// - /// If enabled requires [`Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN`] - pub vertex_return: bool, -} - -/// An external texture binding. -/// -/// Example WGSL syntax: -/// ```rust,ignore -/// @group(0) @binding(0) -/// var t: texture_external; -/// ``` -/// -/// Corresponds to [WebGPU `GPUExternalTextureBindingLayout`]( -/// https://gpuweb.github.io/gpuweb/#dictdef-gpuexternaltexturebindinglayout). -/// -/// Requires [`Features::EXTERNAL_TEXTURE`] -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] -pub struct ExternalTextureBindingLayout; +assert_ffi_safe!(StorageTextureBindingLayout); /// Describes a single binding inside a bind group. /// /// Corresponds to [WebGPU `GPUBindGroupLayoutEntry`]( /// https://gpuweb.github.io/gpuweb/#dictdef-gpubindgrouplayoutentry). +#[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct BindGroupLayoutEntry { /// Binding index. Must match shader index and be unique inside a `BindGroupLayout`. A binding @@ -241,13 +211,15 @@ pub struct BindGroupLayoutEntry { /// Which shader stages can see this binding. pub visibility: wgt::ShaderStagesWebGPU, // The type of the binding - pub buffer: Option, - pub sampler: Option, - pub texture: Option, - pub storage_texture: Option, - pub external_texture: Option, + pub buffer: FfiOption, + pub sampler: FfiOption, + pub texture: FfiOption, + pub storage_texture: FfiOption, + pub external_texture: bool, } +assert_ffi_safe!(BindGroupLayoutEntry); + /// Corresponds to [`GPUBindGroupLayoutDescriptor`](https://www.w3.org/TR/webgpu/#dictdef-gpubindgrouplayoutdescriptor). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct BindGroupLayoutDescriptor<'a> { diff --git a/wgpu-core-remote/src/global/device.rs b/wgpu-core-remote/src/global/device.rs index 4f633e567..11ec645bc 100644 --- a/wgpu-core-remote/src/global/device.rs +++ b/wgpu-core-remote/src/global/device.rs @@ -515,7 +515,7 @@ impl Global { ty, has_dynamic_offset, min_binding_size, - }) = buffer + }) = buffer.to_std() { binding_ty = Some(wgt::BindingType::Buffer { ty, @@ -523,7 +523,7 @@ impl Global { min_binding_size, }); } - if let Some(SamplerBindingLayout { ty }) = sampler { + if let Some(SamplerBindingLayout { ty }) = sampler.to_std() { if binding_ty.is_some() { return Err(binding_model::CreateBindGroupLayoutError::Entry { binding, @@ -536,7 +536,7 @@ impl Global { sample_type, view_dimension, multisampled, - }) = texture + }) = texture.to_std() { if binding_ty.is_some() { return Err(binding_model::CreateBindGroupLayoutError::Entry { @@ -554,7 +554,7 @@ impl Global { access, format, view_dimension, - }) = storage_texture + }) = storage_texture.to_std() { if binding_ty.is_some() { return Err(binding_model::CreateBindGroupLayoutError::Entry { @@ -568,7 +568,7 @@ impl Global { view_dimension, }) } - if let Some(ExternalTextureBindingLayout {}) = external_texture { + if external_texture { if binding_ty.is_some() { return Err(binding_model::CreateBindGroupLayoutError::Entry { binding, @@ -609,26 +609,33 @@ impl Global { visibility, wgt::ShaderStagesWGPU::empty(), ), - buffer: buffer.map(|b| binding_model::BufferBindingLayout { + buffer: buffer.to_std().map(|b| binding_model::BufferBindingLayout { ty: b.ty, has_dynamic_offset: b.has_dynamic_offset, min_binding_size: b.min_binding_size, }), - sampler: sampler.map(|s| binding_model::SamplerBindingLayout { ty: s.ty }), - texture: texture.map(|t| binding_model::TextureBindingLayout { - sample_type: t.sample_type, - view_dimension: t.view_dimension, - multisampled: t.multisampled, - }), - storage_texture: storage_texture.map(|s| { + sampler: sampler + .to_std() + .map(|s| binding_model::SamplerBindingLayout { ty: s.ty }), + texture: texture + .to_std() + .map(|t| binding_model::TextureBindingLayout { + sample_type: t.sample_type, + view_dimension: t.view_dimension, + multisampled: t.multisampled, + }), + storage_texture: storage_texture.to_std().map(|s| { binding_model::StorageTextureBindingLayout { access: s.access, format: s.format, view_dimension: s.view_dimension, } }), - external_texture: external_texture - .map(|_| binding_model::ExternalTextureBindingLayout {}), + external_texture: if external_texture { + Some(binding_model::ExternalTextureBindingLayout {}) + } else { + None + }, acceleration_structure: None, count: None, } diff --git a/wgpu-types/src/binding.rs b/wgpu-types/src/binding.rs index 18b7b3600..8808205fd 100644 --- a/wgpu-types/src/binding.rs +++ b/wgpu-types/src/binding.rs @@ -254,6 +254,7 @@ crate::bitflags_array! { /// /// Corresponds to [WebGPU `GPUBufferBindingType`]( /// https://gpuweb.github.io/gpuweb/#enumdef-gpubufferbindingtype). +#[repr(C)] #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum BufferBindingType { diff --git a/wgpu-types/src/texture.rs b/wgpu-types/src/texture.rs index 2e35654d7..4056e84bb 100644 --- a/wgpu-types/src/texture.rs +++ b/wgpu-types/src/texture.rs @@ -301,6 +301,7 @@ pub struct TextureSelector { /// /// Corresponds to [WebGPU `GPUTextureSampleType`]( /// https://gpuweb.github.io/gpuweb/#enumdef-gputexturesampletype). +#[repr(C)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TextureSampleType { @@ -383,6 +384,7 @@ impl Default for TextureSampleType { /// /// Corresponds to [WebGPU `GPUStorageTextureAccess`]( /// https://gpuweb.github.io/gpuweb/#enumdef-gpustoragetextureaccess). +#[repr(C)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]