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