Bind Groups
A bind group is a collection of resources that are bound to a shader. These resources can be buffers, textures, or samplers. It’s a way to define what resources are available to a shader and how they are accessed.
In this example, we create a bind group that contains a buffer and a texture. Binding indices are determined based on the order of properties in the layout.
Now, during command encoding, we can assign this bind group to a shader.
Available resource types
Each property in the layout object represents a resource as seen by a shader. We recommend keeping the names of
these properties the same as the corresponding @group(...) @binding(...) ...;
statements in WGSL.
Uniforms
To interpret a buffer as a uniform, create a property with the value matching:
Simple example
Matching WGSL statement:
Storage
To get readonly/mutable access to a buffer, create a property with the value matching:
Simple example
Matching WGSL statement:
Runtime-sized example
Apart from being able to specify any data type, we can signal that the shader is generalized to work on arbitrarily sized data by passing a function.
Matching WGSL code:
Samplers
Samplers can be made accessible to shaders with a property that matches the following:
Textures
To be able to sample a texture in a shader, create a property with the value matching:
Storage Textures
To be able to operate on textures more directly in a shader, create a property with the value matching:
You can see the list of supported storage texture formats here.
Bind Groups
Before execution of a pipeline, any bind group that matches a given layout can be put in its place and used by the shader.
To create a bind group, you can call the populate
method on the bind group layout and associate each named key with
a proper resource.
If you accidentally pass the wrong type of resource, the TypeScript compiler will catch the error at compile time.
- Uniform bindings with schema
TData
accept:TgpuBuffer<TData> & Uniform
- buffers of typeTData
with'uniform'
usage,GPUBuffer
- raw WebGPU buffers.
- Storage bindings with schema
TData
accept:TgpuBuffer<TData> & Storage
- buffers of typeTData
with'storage'
usage,GPUBuffer
- raw WebGPU buffers.
- Texture bindings:
GPUTextureView
- views of raw WebGPU textures.
- Storage Texture bindings:
GPUTextureView
- views of raw WebGPU textures.
- Sampler bindings:
sampler === 'comparison'
GPUSampler
- raw WebGPU samplers created with acompare
function.
sampler === 'filtering'
orsampler === 'non-filtering'
GPUSampler
- raw WebGPU samplers created without acompare
function.