From 38f03fdf5cf79ffaec1399a9d1f1e23a494617b9 Mon Sep 17 00:00:00 2001 From: Sam <16504129+sagudev@users.noreply.github.com> Date: Mon, 17 Aug 2026 14:42:28 +0200 Subject: [PATCH] [core-remote-types] Add binding_model types (#10092) --- wgpu-core-remote-types/src/binding_model.rs | 64 +++++++++++++++++++++ wgpu-core-remote-types/src/lib.rs | 7 +++ wgpu-core-remote/src/global/device.rs | 50 +--------------- 3 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 wgpu-core-remote-types/src/binding_model.rs diff --git a/wgpu-core-remote-types/src/binding_model.rs b/wgpu-core-remote-types/src/binding_model.rs new file mode 100644 index 000000000..dd9a0a2ac --- /dev/null +++ b/wgpu-core-remote-types/src/binding_model.rs @@ -0,0 +1,64 @@ +use alloc::borrow::Cow; + +use serde::{Deserialize, Serialize}; + +use crate::id::{BindGroupLayoutId, BufferId, ExternalTextureId, SamplerId, TextureViewId}; +use crate::Label; + +#[repr(C)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)] +pub struct BufferBinding { + pub buffer: BufferId, + pub offset: wgt::BufferAddress, + + /// Size of the binding. If `None`, the binding spans from `offset` to the + /// end of the buffer. + /// + /// We use `BufferAddress` to allow a size of zero on this `wgpu_core` type, + /// because JavaScript bindings cannot readily express `Option`. + /// The `wgpu` API uses `Option` (i.e. `NonZeroU64`) for this + /// field. + pub size: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum BindingResource { + Buffer(BufferBinding), + Sampler(SamplerId), + TextureView(TextureViewId), + ExternalTexture(ExternalTextureId), +} + +/// Bindable resource and the slot to bind it to. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BindGroupEntry { + /// Slot for which binding provides resource. Corresponds to an entry of the same + /// binding index in the [`BindGroupLayoutDescriptor`]. + pub binding: u32, + /// Resource to attach to the binding + pub resource: BindingResource, +} + +/// Describes a group of bindings and the resources to be bound. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BindGroupDescriptor<'a> { + /// Debug label of the bind group. + /// + /// This will show up in graphics debuggers for easy identification. + pub label: Label<'a>, + /// The BindGroupLayout that corresponds to this bind group. + pub layout: BindGroupLayoutId, + /// The resources to bind to this bind group. + pub entries: Cow<'a, [BindGroupEntry]>, +} + +/// Describes a [`BindGroupLayout`]. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BindGroupLayoutDescriptor<'a> { + /// Debug label of the bind group layout. + /// + /// This will show up in graphics debuggers for easy identification. + pub label: Label<'a>, + /// Array of entries in this BindGroupLayout + pub entries: Cow<'a, [wgt::BindGroupLayoutEntry]>, +} diff --git a/wgpu-core-remote-types/src/lib.rs b/wgpu-core-remote-types/src/lib.rs index cff504ad3..1d594d427 100644 --- a/wgpu-core-remote-types/src/lib.rs +++ b/wgpu-core-remote-types/src/lib.rs @@ -1,7 +1,14 @@ extern crate alloc; +extern crate wgpu_types as wgt; + +use alloc::borrow::Cow; pub type Index = u32; pub type Epoch = u32; pub mod id; pub mod identity; + +pub mod binding_model; + +pub type Label<'a> = Option>; diff --git a/wgpu-core-remote/src/global/device.rs b/wgpu-core-remote/src/global/device.rs index 4fd736d5c..8ba72f24d 100644 --- a/wgpu-core-remote/src/global/device.rs +++ b/wgpu-core-remote/src/global/device.rs @@ -24,35 +24,7 @@ use crate::{ use wgt::BufferAddress; -pub type BindGroupDescriptor<'a> = binding_model::BindGroupDescriptor< - 'a, - id::BindGroupLayoutId, - id::BufferId, - id::SamplerId, - id::TextureViewId, - id::TlasId, - id::ExternalTextureId, ->; - -pub type BindGroupEntry<'a> = binding_model::BindGroupEntry< - 'a, - id::BufferId, - id::SamplerId, - id::TextureViewId, - id::TlasId, - id::ExternalTextureId, ->; - -pub type BufferBinding = binding_model::BufferBinding; - -pub type BindingResource<'a> = binding_model::BindingResource< - 'a, - id::BufferId, - id::SamplerId, - id::TextureViewId, - id::TlasId, - id::ExternalTextureId, ->; +pub use wgpu_core_remote_types::binding_model::*; pub type ComputePipelineDescriptor<'a> = pipeline::ComputePipelineDescriptor< 'a, @@ -551,7 +523,7 @@ impl Global { let layout = bind_group_layouts.get(desc.layout); fn resolve_entry<'a>( - e: &BindGroupEntry<'a>, + e: &'a BindGroupEntry, buffers: &mut Registry>, samplers: &mut Registry>, texture_views: &mut Registry>, @@ -572,30 +544,12 @@ impl Global { BindingResource::Buffer(ref buffer) => { binding_model::BindingResource::Buffer(resolve_buffer(buffer)) } - BindingResource::BufferArray(ref buffers) => { - let buffers = buffers.iter().map(resolve_buffer).collect::>(); - binding_model::BindingResource::BufferArray(Cow::Owned(buffers)) - } BindingResource::Sampler(ref sampler) => { binding_model::BindingResource::Sampler(resolve_sampler(sampler)) } - BindingResource::SamplerArray(ref samplers) => { - let samplers = samplers.iter().map(resolve_sampler).collect::>(); - binding_model::BindingResource::SamplerArray(Cow::Owned(samplers)) - } BindingResource::TextureView(ref view) => { binding_model::BindingResource::TextureView(resolve_view(view)) } - BindingResource::TextureViewArray(ref views) => { - let views = views.iter().map(resolve_view).collect::>(); - binding_model::BindingResource::TextureViewArray(Cow::Owned(views)) - } - BindingResource::AccelerationStructure(_) => { - unimplemented!() - } - BindingResource::AccelerationStructureArray(_) => { - unimplemented!() - } BindingResource::ExternalTexture(ref et) => { binding_model::BindingResource::ExternalTexture(resolve_external_texture(et)) }