wgpu_types/
binding.rs

1//! Bind groups and the bindings in them.
2
3use core::num::NonZeroU32;
4
5#[cfg(any(feature = "serde", test))]
6use serde::{Deserialize, Serialize};
7
8use crate::{link_to_wgpu_docs, link_to_wgpu_item, BufferSize};
9
10#[cfg(doc)]
11use crate::Features;
12
13/// Type of a binding in a [bind group layout][`BindGroupLayoutEntry`].
14///
15/// For each binding in a layout, a [`BindGroup`] must provide a [`BindingResource`] of the
16/// corresponding type.
17///
18/// Corresponds to WebGPU's mutually exclusive fields within [`GPUBindGroupLayoutEntry`](
19/// https://gpuweb.github.io/gpuweb/#dictdef-gpubindgrouplayoutentry).
20///
21#[doc = link_to_wgpu_item!(enum BindingResource)]
22#[doc = link_to_wgpu_item!(struct BindGroup)]
23#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
24#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25pub enum BindingType {
26    /// A buffer binding.
27    ///
28    /// Corresponds to [WebGPU `GPUBufferBindingLayout`](
29    /// https://gpuweb.github.io/gpuweb/#dictdef-gpubufferbindinglayout).
30    Buffer {
31        /// Sub-type of the buffer binding.
32        ty: BufferBindingType,
33
34        /// Indicates that the binding has a dynamic offset.
35        ///
36        /// One offset must be passed to [`RenderPass::set_bind_group`][RPsbg]
37        /// for each dynamic binding in increasing order of binding number.
38        ///
39        #[doc = link_to_wgpu_docs!(["RPsbg"]: "struct.RenderPass.html#method.set_bind_group")]
40        #[cfg_attr(feature = "serde", serde(default))]
41        has_dynamic_offset: bool,
42
43        /// The minimum size for a [`BufferBinding`] matching this entry, in bytes.
44        ///
45        /// If this is `Some(size)`:
46        ///
47        /// - When calling [`create_bind_group`], the resource at this bind point
48        ///   must be a [`BindingResource::Buffer`] whose effective size is at
49        ///   least `size`.
50        ///
51        /// - When calling [`create_render_pipeline`] or [`create_compute_pipeline`],
52        ///   `size` must be at least the [minimum buffer binding size] for the
53        ///   shader module global at this bind point: large enough to hold the
54        ///   global's value, along with one element of a trailing runtime-sized
55        ///   array, if present.
56        ///
57        /// If this is `None`:
58        ///
59        /// - Each draw or dispatch command checks that the buffer range at this
60        ///   bind point satisfies the [minimum buffer binding size].
61        ///
62        #[doc = link_to_wgpu_item!(struct BufferBinding)]
63        #[doc = link_to_wgpu_docs!(["`create_bind_group`"]: "struct.Device.html#method.create_bind_group")]
64        #[doc = link_to_wgpu_docs!(["`BindingResource::Buffer`"]: "enum.BindingResource.html#variant.Buffer")]
65        /// [minimum buffer binding size]: https://www.w3.org/TR/webgpu/#minimum-buffer-binding-size
66        #[doc = link_to_wgpu_docs!(["`create_render_pipeline`"]: "struct.Device.html#method.create_render_pipeline")]
67        #[doc = link_to_wgpu_docs!(["`create_compute_pipeline`"]: "struct.Device.html#method.create_compute_pipeline")]
68        #[cfg_attr(feature = "serde", serde(default))]
69        min_binding_size: Option<BufferSize>,
70    },
71    /// A sampler that can be used to sample a texture.
72    ///
73    /// Example WGSL syntax:
74    /// ```rust,ignore
75    /// @group(0) @binding(0)
76    /// var s: sampler;
77    /// ```
78    ///
79    /// Example GLSL syntax:
80    /// ```cpp,ignore
81    /// layout(binding = 0)
82    /// uniform sampler s;
83    /// ```
84    ///
85    /// Corresponds to [WebGPU `GPUSamplerBindingLayout`](
86    /// https://gpuweb.github.io/gpuweb/#dictdef-gpusamplerbindinglayout).
87    Sampler(SamplerBindingType),
88    /// A texture binding.
89    ///
90    /// Example WGSL syntax:
91    /// ```rust,ignore
92    /// @group(0) @binding(0)
93    /// var t: texture_2d<f32>;
94    /// ```
95    ///
96    /// Example GLSL syntax:
97    /// ```cpp,ignore
98    /// layout(binding = 0)
99    /// uniform texture2D t;
100    /// ```
101    ///
102    /// Corresponds to [WebGPU `GPUTextureBindingLayout`](
103    /// https://gpuweb.github.io/gpuweb/#dictdef-gputexturebindinglayout).
104    Texture {
105        /// Sample type of the texture binding.
106        sample_type: crate::TextureSampleType,
107        /// Dimension of the texture view that is going to be sampled.
108        view_dimension: crate::TextureViewDimension,
109        /// True if the texture has a sample count greater than 1. If this is true,
110        /// the texture must be declared as `texture_multisampled_2d` or
111        /// `texture_depth_multisampled_2d` in the shader, and read using `textureLoad`.
112        multisampled: bool,
113    },
114    /// A storage texture.
115    ///
116    /// Example WGSL syntax:
117    /// ```rust,ignore
118    /// @group(0) @binding(0)
119    /// var my_storage_image: texture_storage_2d<r32float, write>;
120    /// ```
121    ///
122    /// Example GLSL syntax:
123    /// ```cpp,ignore
124    /// layout(set=0, binding=0, r32f) writeonly uniform image2D myStorageImage;
125    /// ```
126    /// Note that the texture format must be specified in the shader, along with the
127    /// access mode. For WGSL, the format must be one of the enumerants in the list
128    /// of [storage texel formats](https://gpuweb.github.io/gpuweb/wgsl/#storage-texel-formats).
129    ///
130    /// Corresponds to [WebGPU `GPUStorageTextureBindingLayout`](
131    /// https://gpuweb.github.io/gpuweb/#dictdef-gpustoragetexturebindinglayout).
132    StorageTexture {
133        /// Allowed access to this texture.
134        access: crate::StorageTextureAccess,
135        /// Format of the texture.
136        format: crate::TextureFormat,
137        /// Dimension of the texture view that is going to be sampled.
138        view_dimension: crate::TextureViewDimension,
139    },
140
141    /// A ray-tracing acceleration structure binding.
142    ///
143    /// Example WGSL syntax:
144    /// ```rust,ignore
145    /// @group(0) @binding(0)
146    /// var as: acceleration_structure;
147    /// ```
148    ///
149    /// or with vertex return enabled
150    /// ```rust,ignore
151    /// @group(0) @binding(0)
152    /// var as: acceleration_structure<vertex_return>;
153    /// ```
154    ///
155    /// Example GLSL syntax:
156    /// ```cpp,ignore
157    /// layout(binding = 0)
158    /// uniform accelerationStructureEXT as;
159    /// ```
160    AccelerationStructure {
161        /// Whether this acceleration structure can be used to
162        /// create a ray query that has flag vertex return in the shader
163        ///
164        /// If enabled requires [`Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN`]
165        vertex_return: bool,
166    },
167
168    /// An external texture binding.
169    ///
170    /// Example WGSL syntax:
171    /// ```rust,ignore
172    /// @group(0) @binding(0)
173    /// var t: texture_external;
174    /// ```
175    ///
176    /// Corresponds to [WebGPU `GPUExternalTextureBindingLayout`](
177    /// https://gpuweb.github.io/gpuweb/#dictdef-gpuexternaltexturebindinglayout).
178    ///
179    /// Requires [`Features::EXTERNAL_TEXTURE`]
180    ExternalTexture,
181}
182
183impl BindingType {
184    /// Returns true for buffer bindings with dynamic offset enabled.
185    #[must_use]
186    pub fn has_dynamic_offset(&self) -> bool {
187        match *self {
188            Self::Buffer {
189                has_dynamic_offset, ..
190            } => has_dynamic_offset,
191            _ => false,
192        }
193    }
194}
195
196bitflags::bitflags! {
197    /// Describes the shader stages that a binding will be visible from.
198    ///
199    /// These can be combined so something that is visible from both vertex and fragment shaders can be defined as:
200    ///
201    /// `ShaderStages::VERTEX | ShaderStages::FRAGMENT`
202    ///
203    /// Corresponds to [WebGPU `GPUShaderStageFlags`](
204    /// https://gpuweb.github.io/gpuweb/#typedefdef-gpushaderstageflags).
205    #[repr(transparent)]
206    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
207    #[cfg_attr(feature = "serde", serde(transparent))]
208    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
209    pub struct ShaderStages: u32 {
210        /// Binding is not visible from any shader stage.
211        const NONE = 0;
212        /// Binding is visible from the vertex shader of a render pipeline.
213        const VERTEX = 1 << 0;
214        /// Binding is visible from the fragment shader of a render pipeline.
215        const FRAGMENT = 1 << 1;
216        /// Binding is visible from the compute shader of a compute pipeline.
217        const COMPUTE = 1 << 2;
218        /// Binding is visible from the vertex and fragment shaders of a render pipeline.
219        const VERTEX_FRAGMENT = Self::VERTEX.bits() | Self::FRAGMENT.bits();
220        /// Binding is visible from the task shader of a mesh pipeline.
221        const TASK = 1 << 3;
222        /// Binding is visible from the mesh shader of a mesh pipeline.
223        const MESH = 1 << 4;
224        /// Binding is visible from the ray generation shader of a ray tracing pipeline.
225        const RAY_GENERATION = 1 << 5;
226        /// Binding is visible from the ray any hit shader of a ray tracing pipeline.
227        const ANY_HIT = 1 << 6;
228        /// Binding is visible from the ray closest hit shader of a ray tracing pipeline.
229        const CLOSEST_HIT = 1 << 7;
230        /// Binding is visible from the ray miss shader of a ray tracing pipeline.
231        const MISS = 1 << 8;
232    }
233}
234
235/// Specific type of a buffer binding.
236///
237/// Corresponds to [WebGPU `GPUBufferBindingType`](
238/// https://gpuweb.github.io/gpuweb/#enumdef-gpubufferbindingtype).
239#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
240#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
241pub enum BufferBindingType {
242    /// A buffer for uniform values.
243    ///
244    /// Example WGSL syntax:
245    /// ```rust,ignore
246    /// struct Globals {
247    ///     a_uniform: vec2<f32>,
248    ///     another_uniform: vec2<f32>,
249    /// }
250    /// @group(0) @binding(0)
251    /// var<uniform> globals: Globals;
252    /// ```
253    ///
254    /// Example GLSL syntax:
255    /// ```cpp,ignore
256    /// layout(std140, binding = 0)
257    /// uniform Globals {
258    ///     vec2 aUniform;
259    ///     vec2 anotherUniform;
260    /// };
261    /// ```
262    #[default]
263    Uniform,
264    /// A storage buffer.
265    ///
266    /// Example WGSL syntax:
267    /// ```rust,ignore
268    /// @group(0) @binding(0)
269    /// var<storage, read_write> my_element: array<vec4<f32>>;
270    /// ```
271    ///
272    /// Example GLSL syntax:
273    /// ```cpp,ignore
274    /// layout (set=0, binding=0) buffer myStorageBuffer {
275    ///     vec4 myElement[];
276    /// };
277    /// ```
278    Storage {
279        /// If `true`, the buffer can only be read in the shader,
280        /// and it:
281        /// - may or may not be annotated with `read` (WGSL).
282        /// - must be annotated with `readonly` (GLSL).
283        ///
284        /// Example WGSL syntax:
285        /// ```rust,ignore
286        /// @group(0) @binding(0)
287        /// var<storage, read> my_element: array<vec4<f32>>;
288        /// ```
289        ///
290        /// Example GLSL syntax:
291        /// ```cpp,ignore
292        /// layout (set=0, binding=0) readonly buffer myStorageBuffer {
293        ///     vec4 myElement[];
294        /// };
295        /// ```
296        read_only: bool,
297    },
298}
299
300/// Specific type of a sampler binding.
301///
302/// For use in [`BindingType::Sampler`].
303///
304/// Corresponds to [WebGPU `GPUSamplerBindingType`](
305/// https://gpuweb.github.io/gpuweb/#enumdef-gpusamplerbindingtype).
306#[repr(C)]
307#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
308#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
309#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
310pub enum SamplerBindingType {
311    /// The sampling result is produced based on more than a single color sample from a texture,
312    /// e.g. when bilinear interpolation is enabled.
313    Filtering,
314    /// The sampling result is produced based on a single color sample from a texture.
315    NonFiltering,
316    /// Use as a comparison sampler instead of a normal sampler.
317    /// For more info take a look at the analogous functionality in OpenGL: <https://www.khronos.org/opengl/wiki/Sampler_Object#Comparison_mode>.
318    Comparison,
319}
320
321/// Describes a single binding inside a bind group.
322///
323/// Corresponds to [WebGPU `GPUBindGroupLayoutEntry`](
324/// https://gpuweb.github.io/gpuweb/#dictdef-gpubindgrouplayoutentry).
325#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
326#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
327pub struct BindGroupLayoutEntry {
328    /// Binding index. Must match shader index and be unique inside a `BindGroupLayout`. A binding
329    /// of index 1, would be described as `@group(0) @binding(1)` in shaders.
330    pub binding: u32,
331    /// Which shader stages can see this binding.
332    pub visibility: ShaderStages,
333    /// The type of the binding
334    pub ty: BindingType,
335    /// If the binding is an array of multiple resources. Corresponds to `binding_array<T>` in the shader.
336    ///
337    /// When this is `Some` the following validation applies:
338    /// - Count must be of value 1 or greater, this corresponds to the length of the array of resources that will be bound.
339    /// - When `ty == BindingType::Texture`, [`Features::TEXTURE_BINDING_ARRAY`] must be supported.
340    /// - When `ty == BindingType::Sampler`, [`Features::TEXTURE_BINDING_ARRAY`] must be supported.
341    /// - When `ty == BindingType::Buffer`, [`Features::BUFFER_BINDING_ARRAY`] must be supported.
342    /// - When `ty == BindingType::Buffer` and `ty.ty == BufferBindingType::Storage`, [`Features::STORAGE_RESOURCE_BINDING_ARRAY`] must be supported.
343    /// - When `ty == BindingType::StorageTexture`, [`Features::STORAGE_RESOURCE_BINDING_ARRAY`] must be supported.
344    /// - When any binding in the group is an array, no `BindingType::Buffer` in the group may have `has_dynamic_offset == true`
345    /// - When any binding in the group is an array, no `BindingType::Buffer` in the group may have `ty.ty == BufferBindingType::Uniform`.
346    /// - If [`Features::PARTIALLY_BOUND_BINDING_ARRAY`] is enabled, the specified count becomes the upper bound instead.
347    ///
348    #[cfg_attr(feature = "serde", serde(default))]
349    pub count: Option<NonZeroU32>,
350}