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 /// Binding is backed by an external texture.
85 ///
86 /// [`Features::EXTERNAL_TEXTURE`] must be supported to use this feature.
87 ///
88 /// Corresponds to [`wgt::BindingType::ExternalTexture`].
89 ExternalTexture(&'a ExternalTexture),
90}
91#[cfg(send_sync)]
92static_assertions::assert_impl_all!(BindingResource<'_>: Send, Sync);
93
94/// Describes the segment of a buffer to bind.
95///
96/// Corresponds to [WebGPU `GPUBufferBinding`](
97/// https://gpuweb.github.io/gpuweb/#dictdef-gpubufferbinding).
98#[derive(Clone, Debug)]
99pub struct BufferBinding<'a> {
100 /// The buffer to bind.
101 pub buffer: &'a Buffer,
102
103 /// Base offset of the buffer, in bytes.
104 ///
105 /// If the [`has_dynamic_offset`] field of this buffer's layout entry is
106 /// `true`, the offset here will be added to the dynamic offset passed to
107 /// [`RenderPass::set_bind_group`] or [`ComputePass::set_bind_group`].
108 ///
109 /// If the buffer was created with [`BufferUsages::UNIFORM`], then this
110 /// offset must be a multiple of
111 /// [`Limits::min_uniform_buffer_offset_alignment`].
112 ///
113 /// If the buffer was created with [`BufferUsages::STORAGE`], then this
114 /// offset must be a multiple of
115 /// [`Limits::min_storage_buffer_offset_alignment`].
116 ///
117 /// [`has_dynamic_offset`]: BindingType::Buffer::has_dynamic_offset
118 pub offset: BufferAddress,
119
120 /// Size of the binding in bytes, or `None` for using the rest of the buffer.
121 pub size: Option<BufferSize>,
122}
123#[cfg(send_sync)]
124static_assertions::assert_impl_all!(BufferBinding<'_>: Send, Sync);
125
126/// An element of a [`BindGroupDescriptor`], consisting of a bindable resource
127/// and the slot to bind it to.
128///
129/// Corresponds to [WebGPU `GPUBindGroupEntry`](
130/// https://gpuweb.github.io/gpuweb/#dictdef-gpubindgroupentry).
131#[derive(Clone, Debug)]
132pub struct BindGroupEntry<'a> {
133 /// Slot for which binding provides resource. Corresponds to an entry of the same
134 /// binding index in the [`BindGroupLayoutDescriptor`].
135 pub binding: u32,
136 /// Resource to attach to the binding
137 pub resource: BindingResource<'a>,
138}
139#[cfg(send_sync)]
140static_assertions::assert_impl_all!(BindGroupEntry<'_>: Send, Sync);
141
142/// Describes a group of bindings and the resources to be bound.
143///
144/// For use with [`Device::create_bind_group`].
145///
146/// Corresponds to [WebGPU `GPUBindGroupDescriptor`](
147/// https://gpuweb.github.io/gpuweb/#dictdef-gpubindgroupdescriptor).
148#[derive(Clone, Debug)]
149pub struct BindGroupDescriptor<'a> {
150 /// Debug label of the bind group. This will show up in graphics debuggers for easy identification.
151 pub label: Label<'a>,
152 /// The [`BindGroupLayout`] that corresponds to this bind group.
153 pub layout: &'a BindGroupLayout,
154 /// The resources to bind to this bind group.
155 pub entries: &'a [BindGroupEntry<'a>],
156}
157#[cfg(send_sync)]
158static_assertions::assert_impl_all!(BindGroupDescriptor<'_>: Send, Sync);