wgpu/api/bind_group.rs
1use std::{sync::Arc, thread};
2
3use crate::*;
4
5/// Handle to a binding group.
6///
7/// A `BindGroup` represents the set of resources bound to the bindings described by a
8/// [`BindGroupLayout`]. It can be created with [`Device::create_bind_group`]. A `BindGroup` can
9/// be bound to a particular [`RenderPass`] with [`RenderPass::set_bind_group`], or to a
10/// [`ComputePass`] with [`ComputePass::set_bind_group`].
11///
12/// Corresponds to [WebGPU `GPUBindGroup`](https://gpuweb.github.io/gpuweb/#gpubindgroup).
13#[derive(Debug)]
14pub struct BindGroup {
15 pub(crate) context: Arc<C>,
16 pub(crate) data: Box<Data>,
17}
18#[cfg(send_sync)]
19static_assertions::assert_impl_all!(BindGroup: Send, Sync);
20
21super::impl_partialeq_eq_hash!(BindGroup);
22
23impl Drop for BindGroup {
24 fn drop(&mut self) {
25 if !thread::panicking() {
26 self.context.bind_group_drop(self.data.as_ref());
27 }
28 }
29}
30
31/// Resource that can be bound to a pipeline.
32///
33/// Corresponds to [WebGPU `GPUBindingResource`](
34/// https://gpuweb.github.io/gpuweb/#typedefdef-gpubindingresource).
35#[non_exhaustive]
36#[derive(Clone, Debug)]
37pub enum BindingResource<'a> {
38 /// Binding is backed by a buffer.
39 ///
40 /// Corresponds to [`wgt::BufferBindingType::Uniform`] and [`wgt::BufferBindingType::Storage`]
41 /// with [`BindGroupLayoutEntry::count`] set to None.
42 Buffer(BufferBinding<'a>),
43 /// Binding is backed by an array of buffers.
44 ///
45 /// [`Features::BUFFER_BINDING_ARRAY`] must be supported to use this feature.
46 ///
47 /// Corresponds to [`wgt::BufferBindingType::Uniform`] and [`wgt::BufferBindingType::Storage`]
48 /// with [`BindGroupLayoutEntry::count`] set to Some.
49 BufferArray(&'a [BufferBinding<'a>]),
50 /// Binding is a sampler.
51 ///
52 /// Corresponds to [`wgt::BindingType::Sampler`] with [`BindGroupLayoutEntry::count`] set to None.
53 Sampler(&'a Sampler),
54 /// Binding is backed by an array of samplers.
55 ///
56 /// [`Features::TEXTURE_BINDING_ARRAY`] must be supported to use this feature.
57 ///
58 /// Corresponds to [`wgt::BindingType::Sampler`] with [`BindGroupLayoutEntry::count`] set
59 /// to Some.
60 SamplerArray(&'a [&'a Sampler]),
61 /// Binding is backed by a texture.
62 ///
63 /// Corresponds to [`wgt::BindingType::Texture`] and [`wgt::BindingType::StorageTexture`] with
64 /// [`BindGroupLayoutEntry::count`] set to None.
65 TextureView(&'a TextureView),
66 /// Binding is backed by an array of textures.
67 ///
68 /// [`Features::TEXTURE_BINDING_ARRAY`] must be supported to use this feature.
69 ///
70 /// Corresponds to [`wgt::BindingType::Texture`] and [`wgt::BindingType::StorageTexture`] with
71 /// [`BindGroupLayoutEntry::count`] set to Some.
72 TextureViewArray(&'a [&'a TextureView]),
73}
74#[cfg(send_sync)]
75static_assertions::assert_impl_all!(BindingResource<'_>: Send, Sync);
76
77/// Describes the segment of a buffer to bind.
78///
79/// Corresponds to [WebGPU `GPUBufferBinding`](
80/// https://gpuweb.github.io/gpuweb/#dictdef-gpubufferbinding).
81#[derive(Clone, Debug)]
82pub struct BufferBinding<'a> {
83 /// The buffer to bind.
84 pub buffer: &'a Buffer,
85
86 /// Base offset of the buffer, in bytes.
87 ///
88 /// If the [`has_dynamic_offset`] field of this buffer's layout entry is
89 /// `true`, the offset here will be added to the dynamic offset passed to
90 /// [`RenderPass::set_bind_group`] or [`ComputePass::set_bind_group`].
91 ///
92 /// If the buffer was created with [`BufferUsages::UNIFORM`], then this
93 /// offset must be a multiple of
94 /// [`Limits::min_uniform_buffer_offset_alignment`].
95 ///
96 /// If the buffer was created with [`BufferUsages::STORAGE`], then this
97 /// offset must be a multiple of
98 /// [`Limits::min_storage_buffer_offset_alignment`].
99 ///
100 /// [`has_dynamic_offset`]: BindingType::Buffer::has_dynamic_offset
101 pub offset: BufferAddress,
102
103 /// Size of the binding in bytes, or `None` for using the rest of the buffer.
104 pub size: Option<BufferSize>,
105}
106#[cfg(send_sync)]
107static_assertions::assert_impl_all!(BufferBinding<'_>: Send, Sync);
108
109/// An element of a [`BindGroupDescriptor`], consisting of a bindable resource
110/// and the slot to bind it to.
111///
112/// Corresponds to [WebGPU `GPUBindGroupEntry`](
113/// https://gpuweb.github.io/gpuweb/#dictdef-gpubindgroupentry).
114#[derive(Clone, Debug)]
115pub struct BindGroupEntry<'a> {
116 /// Slot for which binding provides resource. Corresponds to an entry of the same
117 /// binding index in the [`BindGroupLayoutDescriptor`].
118 pub binding: u32,
119 /// Resource to attach to the binding
120 pub resource: BindingResource<'a>,
121}
122#[cfg(send_sync)]
123static_assertions::assert_impl_all!(BindGroupEntry<'_>: Send, Sync);
124
125/// Describes a group of bindings and the resources to be bound.
126///
127/// For use with [`Device::create_bind_group`].
128///
129/// Corresponds to [WebGPU `GPUBindGroupDescriptor`](
130/// https://gpuweb.github.io/gpuweb/#dictdef-gpubindgroupdescriptor).
131#[derive(Clone, Debug)]
132pub struct BindGroupDescriptor<'a> {
133 /// Debug label of the bind group. This will show up in graphics debuggers for easy identification.
134 pub label: Label<'a>,
135 /// The [`BindGroupLayout`] that corresponds to this bind group.
136 pub layout: &'a BindGroupLayout,
137 /// The resources to bind to this bind group.
138 pub entries: &'a [BindGroupEntry<'a>],
139}
140#[cfg(send_sync)]
141static_assertions::assert_impl_all!(BindGroupDescriptor<'_>: Send, Sync);