Skip to main content

bevy_render/render_resource/
bindless.rs

1//! Types and functions relating to bindless resources.
2
3use alloc::borrow::Cow;
4use core::{
5    num::{NonZeroU32, NonZeroU64},
6    ops::Range,
7};
8
9use bevy_derive::{Deref, DerefMut};
10use wgpu::{
11    BindGroupLayoutEntry, SamplerBindingType, ShaderStages, TextureSampleType, TextureViewDimension,
12};
13
14use bevy_material::bind_group_layout_entries::binding_types::{
15    sampler, storage_buffer_read_only_sized, texture_1d, texture_2d, texture_2d_array, texture_3d,
16    texture_cube, texture_cube_array,
17};
18
19/// The default value for the number of resources that can be stored in a slab
20/// on this platform.
21///
22/// See the documentation for [`BindlessSlabResourceLimit`] for more
23/// information.
24#[cfg(any(target_os = "macos", target_os = "ios"))]
25pub const AUTO_BINDLESS_SLAB_RESOURCE_LIMIT: u32 = 64;
26/// The default value for the number of resources that can be stored in a slab
27/// on this platform.
28///
29/// See the documentation for [`BindlessSlabResourceLimit`] for more
30/// information.
31#[cfg(not(any(target_os = "macos", target_os = "ios")))]
32pub const AUTO_BINDLESS_SLAB_RESOURCE_LIMIT: u32 = 2048;
33
34/// The binding numbers for the built-in binding arrays of each bindless
35/// resource type.
36///
37/// In the case of materials, the material allocator manages these binding
38/// arrays.
39///
40/// `bindless.wgsl` contains declarations of these arrays for use in your
41/// shaders. If you change these, make sure to update that file as well.
42pub static BINDING_NUMBERS: [(BindlessResourceType, BindingNumber); 9] = [
43    (BindlessResourceType::SamplerFiltering, BindingNumber(1)),
44    (BindlessResourceType::SamplerNonFiltering, BindingNumber(2)),
45    (BindlessResourceType::SamplerComparison, BindingNumber(3)),
46    (BindlessResourceType::Texture1d, BindingNumber(4)),
47    (BindlessResourceType::Texture2d, BindingNumber(5)),
48    (BindlessResourceType::Texture2dArray, BindingNumber(6)),
49    (BindlessResourceType::Texture3d, BindingNumber(7)),
50    (BindlessResourceType::TextureCube, BindingNumber(8)),
51    (BindlessResourceType::TextureCubeArray, BindingNumber(9)),
52];
53
54/// The maximum number of resources that can be stored in a slab.
55///
56/// This limit primarily exists in order to work around `wgpu` performance
57/// problems involving large numbers of bindless resources. Also, some
58/// platforms, such as Metal, currently enforce limits on the number of
59/// resources in use.
60///
61/// This corresponds to `LIMIT` in the `#[bindless(LIMIT)]` attribute when
62/// deriving [`crate::render_resource::AsBindGroup`].
63#[derive(Clone, Copy, Default, PartialEq, Debug)]
64pub enum BindlessSlabResourceLimit {
65    /// Allows the renderer to choose a reasonable value for the resource limit
66    /// based on the platform.
67    ///
68    /// This value has been tuned, so you should default to this value unless
69    /// you have special platform-specific considerations that prevent you from
70    /// using it.
71    #[default]
72    Auto,
73
74    /// A custom value for the resource limit.
75    ///
76    /// Bevy will allocate no more than this number of resources in a slab,
77    /// unless exceeding this value is necessary in order to allocate at all
78    /// (i.e. unless the number of bindless resources in your bind group exceeds
79    /// this value), in which case Bevy can exceed it.
80    Custom(u32),
81}
82
83/// Information about the bindless resources in this object.
84///
85/// The material bind group allocator uses this descriptor in order to create
86/// and maintain bind groups. The fields within this bindless descriptor are
87/// [`Cow`]s in order to support both the common case in which the fields are
88/// simply `static` constants and the more unusual case in which the fields are
89/// dynamically generated efficiently. An example of the latter case is
90/// `ExtendedMaterial`, which needs to assemble a bindless descriptor from those
91/// of the base material and the material extension at runtime.
92///
93/// This structure will only be present if this object is bindless.
94pub struct BindlessDescriptor {
95    /// The bindless resource types that this object uses, in order of bindless
96    /// index.
97    ///
98    /// The resource assigned to binding index 0 will be at index 0, the
99    /// resource assigned to binding index will be at index 1 in this array, and
100    /// so on. Unused binding indices are set to [`BindlessResourceType::None`].
101    pub resources: Cow<'static, [BindlessResourceType]>,
102    /// The [`BindlessBufferDescriptor`] for each bindless buffer that this
103    /// object uses.
104    ///
105    /// The order of this array is irrelevant.
106    pub buffers: Cow<'static, [BindlessBufferDescriptor]>,
107    /// The [`BindlessIndexTableDescriptor`]s describing each bindless index
108    /// table.
109    ///
110    /// This list must be sorted by the first bindless index.
111    pub index_tables: Cow<'static, [BindlessIndexTableDescriptor]>,
112}
113
114/// The type of potentially-bindless resource.
115#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
116pub enum BindlessResourceType {
117    /// No bindless resource.
118    ///
119    /// This is used as a placeholder to fill holes in the
120    /// [`BindlessDescriptor::resources`] list.
121    None,
122    /// A storage buffer.
123    Buffer,
124    /// A filtering sampler.
125    SamplerFiltering,
126    /// A non-filtering sampler (nearest neighbor).
127    SamplerNonFiltering,
128    /// A comparison sampler (typically used for shadow maps).
129    SamplerComparison,
130    /// A 1D texture.
131    Texture1d,
132    /// A 2D texture.
133    Texture2d,
134    /// A 2D texture array.
135    ///
136    /// Note that this differs from a binding array. 2D texture arrays must all
137    /// have the same size and format.
138    Texture2dArray,
139    /// A 3D texture.
140    Texture3d,
141    /// A cubemap texture.
142    TextureCube,
143    /// A cubemap texture array.
144    ///
145    /// Note that this differs from a binding array. Cubemap texture arrays must
146    /// all have the same size and format.
147    TextureCubeArray,
148    /// Multiple instances of plain old data concatenated into a single buffer.
149    ///
150    /// This corresponds to the `#[data]` declaration in
151    /// [`crate::render_resource::AsBindGroup`].
152    ///
153    /// Note that this resource doesn't itself map to a GPU-level binding
154    /// resource and instead depends on the `MaterialBindGroupAllocator` to
155    /// create a binding resource for it.
156    DataBuffer,
157}
158
159/// Describes a bindless buffer.
160///
161/// Unlike samplers and textures, each buffer in a bind group gets its own
162/// unique bind group entry. That is, there isn't any `bindless_buffers` binding
163/// array to go along with `bindless_textures_2d`,
164/// `bindless_samplers_filtering`, etc. Therefore, this descriptor contains two
165/// indices: the *binding number* and the *bindless index*. The binding number
166/// is the `@binding` number used in the shader, while the bindless index is the
167/// index of the buffer in the bindless index table (which is itself
168/// conventionally bound to binding number 0).
169///
170/// When declaring the buffer in a derived implementation
171/// [`crate::render_resource::AsBindGroup`] with syntax like
172/// `#[uniform(BINDLESS_INDEX, StandardMaterialUniform,
173/// bindless(BINDING_NUMBER)]`, the bindless index is `BINDLESS_INDEX`, and the
174/// binding number is `BINDING_NUMBER`. Note the order.
175#[derive(Clone, Copy, Debug)]
176pub struct BindlessBufferDescriptor {
177    /// The actual binding number of the buffer.
178    ///
179    /// This is declared with `@binding` in WGSL. When deriving
180    /// [`crate::render_resource::AsBindGroup`], this is the `BINDING_NUMBER` in
181    /// `#[uniform(BINDLESS_INDEX, StandardMaterialUniform,
182    /// bindless(BINDING_NUMBER)]`.
183    pub binding_number: BindingNumber,
184    /// The index of the buffer in the bindless index table.
185    ///
186    /// In the shader, this is the index into the table bound to binding 0. When
187    /// deriving [`crate::render_resource::AsBindGroup`], this is the
188    /// `BINDLESS_INDEX` in `#[uniform(BINDLESS_INDEX, StandardMaterialUniform,
189    /// bindless(BINDING_NUMBER)]`.
190    pub bindless_index: BindlessIndex,
191    /// The size of the buffer in bytes, if known.
192    pub size: Option<usize>,
193}
194
195/// Describes the layout of the bindless index table, which maps bindless
196/// indices to indices within the binding arrays.
197#[derive(Clone)]
198pub struct BindlessIndexTableDescriptor {
199    /// The range of bindless indices that this descriptor covers.
200    pub indices: Range<BindlessIndex>,
201    /// The binding at which the index table itself will be bound.
202    ///
203    /// By default, this is binding 0, but it can be changed with the
204    /// `#[bindless(index_table(binding(B)))]` attribute.
205    pub binding_number: BindingNumber,
206}
207
208/// The index of the actual binding in the bind group.
209///
210/// This is the value specified in WGSL as `@binding`.
211#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Deref, DerefMut)]
212pub struct BindingNumber(pub u32);
213
214/// The index in the bindless index table.
215///
216/// This table is conventionally bound to binding number 0.
217#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Hash, Debug, Deref, DerefMut)]
218pub struct BindlessIndex(pub u32);
219
220/// Creates the bind group layout entries common to all shaders that use
221/// bindless bind groups.
222///
223/// `used_resource_types` limits which binding arrays are created,
224/// reducing argument buffer slot usage on constrained platforms.
225pub fn create_bindless_bind_group_layout_entries(
226    bindless_index_table_length: u32,
227    bindless_slab_resource_limit: u32,
228    bindless_index_table_binding_number: BindingNumber,
229    used_resource_types: &[BindlessResourceType],
230) -> Vec<BindGroupLayoutEntry> {
231    let bindless_slab_resource_limit =
232        NonZeroU32::new(bindless_slab_resource_limit).expect("Bindless slot count must be nonzero");
233
234    let stages = ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE;
235
236    // Start the bindless index table; remaining entries are added
237    // below based on which resource types the material actually uses.
238
239    let mut entries = vec![
240        // Start with the bindless index table.
241        storage_buffer_read_only_sized(
242            false,
243            NonZeroU64::new(bindless_index_table_length as u64 * size_of::<u32>() as u64),
244        )
245        .build(*bindless_index_table_binding_number, stages),
246    ];
247
248    // Create binding arrays only for types that this material uses.
249    // This is important for platforms like Metal where each binding array uses a buffer slot
250    // (limited to 31 per the Metal Feature Set Tables:
251    // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf)
252    for &(resource_type, ref binding_number) in BINDING_NUMBERS.iter() {
253        if !used_resource_types.contains(&resource_type) {
254            continue;
255        }
256        let Some(binding_type) = (match resource_type {
257            BindlessResourceType::SamplerFiltering => Some(sampler(SamplerBindingType::Filtering)),
258            BindlessResourceType::SamplerNonFiltering => {
259                Some(sampler(SamplerBindingType::NonFiltering))
260            }
261            BindlessResourceType::SamplerComparison => {
262                Some(sampler(SamplerBindingType::Comparison))
263            }
264            BindlessResourceType::Texture1d => {
265                Some(texture_1d(TextureSampleType::Float { filterable: true }))
266            }
267            BindlessResourceType::Texture2d => {
268                Some(texture_2d(TextureSampleType::Float { filterable: true }))
269            }
270            BindlessResourceType::Texture2dArray => {
271                Some(texture_2d_array(TextureSampleType::Float {
272                    filterable: true,
273                }))
274            }
275            BindlessResourceType::Texture3d => {
276                Some(texture_3d(TextureSampleType::Float { filterable: true }))
277            }
278            BindlessResourceType::TextureCube => {
279                Some(texture_cube(TextureSampleType::Float { filterable: true }))
280            }
281            BindlessResourceType::TextureCubeArray => {
282                Some(texture_cube_array(TextureSampleType::Float {
283                    filterable: true,
284                }))
285            }
286            BindlessResourceType::None
287            | BindlessResourceType::Buffer
288            | BindlessResourceType::DataBuffer => None,
289        }) else {
290            continue;
291        };
292        entries.push(
293            binding_type
294                .count(bindless_slab_resource_limit)
295                .build(**binding_number, stages),
296        );
297    }
298
299    entries
300}
301
302impl BindlessSlabResourceLimit {
303    /// Determines the actual bindless slab resource limit on this platform.
304    pub fn resolve(&self) -> u32 {
305        match *self {
306            BindlessSlabResourceLimit::Auto => AUTO_BINDLESS_SLAB_RESOURCE_LIMIT,
307            BindlessSlabResourceLimit::Custom(limit) => limit,
308        }
309    }
310}
311
312impl BindlessResourceType {
313    /// Returns the binding number for the common array of this resource type.
314    ///
315    /// For example, if you pass `BindlessResourceType::Texture2d`, this will
316    /// return 5, in order to match the `@group(2) @binding(5) var
317    /// bindless_textures_2d: binding_array<texture_2d<f32>>` declaration in
318    /// `bindless.wgsl`.
319    ///
320    /// Not all resource types have fixed binding numbers. If you call
321    /// [`Self::binding_number`] on such a resource type, it returns `None`.
322    ///
323    /// Note that this returns a static reference to the binding number, not the
324    /// binding number itself. This is to conform to an idiosyncratic API in
325    /// `wgpu` whereby binding numbers for binding arrays are taken by `&u32`
326    /// *reference*, not by `u32` value.
327    pub fn binding_number(&self) -> Option<&'static BindingNumber> {
328        match BINDING_NUMBERS.binary_search_by_key(self, |(key, _)| *key) {
329            Ok(binding_number) => Some(&BINDING_NUMBERS[binding_number].1),
330            Err(_) => None,
331        }
332    }
333}
334
335impl From<TextureViewDimension> for BindlessResourceType {
336    fn from(texture_view_dimension: TextureViewDimension) -> Self {
337        match texture_view_dimension {
338            TextureViewDimension::D1 => BindlessResourceType::Texture1d,
339            TextureViewDimension::D2 => BindlessResourceType::Texture2d,
340            TextureViewDimension::D2Array => BindlessResourceType::Texture2dArray,
341            TextureViewDimension::Cube => BindlessResourceType::TextureCube,
342            TextureViewDimension::CubeArray => BindlessResourceType::TextureCubeArray,
343            TextureViewDimension::D3 => BindlessResourceType::Texture3d,
344        }
345    }
346}
347
348impl From<SamplerBindingType> for BindlessResourceType {
349    fn from(sampler_binding_type: SamplerBindingType) -> Self {
350        match sampler_binding_type {
351            SamplerBindingType::Filtering => BindlessResourceType::SamplerFiltering,
352            SamplerBindingType::NonFiltering => BindlessResourceType::SamplerNonFiltering,
353            SamplerBindingType::Comparison => BindlessResourceType::SamplerComparison,
354        }
355    }
356}
357
358impl From<u32> for BindlessIndex {
359    fn from(value: u32) -> Self {
360        Self(value)
361    }
362}
363
364impl From<u32> for BindingNumber {
365    fn from(value: u32) -> Self {
366        Self(value)
367    }
368}