wgpu/api/
bind_group.rs

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