GPUDevice: createBindGroup() method
{{APIRef("WebGPU API")}}
{{SeeCompatTable}}
{{SecureContext_Header}}
{{AvailableInWorkers}}
The createBindGroup()
method of the
{{domxref("GPUDevice")}}
interface creates a {{domxref("GPUBindGroup")}}
based on a {{domxref("GPUBindGroupLayout")}}
that defines a set of resources to be bound together in a group and how those resources are used in shader stages.
Syntax
createBindGroup(descriptor)
Parameters
descriptor
- : An object containing the following properties:
entries
- : An array of entry objects describing the resources to expose to the shader. There will be one for each corresponding entry described by the
{{domxref("GPUBindGroupLayout")}}
referenced inlayout
. Each entry object has the following properties:binding
- : A number representing a unique identifier for this resource binding, which matches the
binding
value of a corresponding{{domxref("GPUBindGroupLayout")}}
entry. In addition, it matches then
index value of the corresponding@binding(n)
attribute in the shader ({{domxref("GPUShaderModule")}}
) used in the related pipeline.
- : A number representing a unique identifier for this resource binding, which matches the
resource
- : The resource to bind. This can be one of the following:
GPUBufferBinding
(which wraps a{{domxref("GPUBuffer")}}
; see GPUBufferBinding objects for a definition){{domxref("GPUExternalTexture")}}
{{domxref("GPUSampler")}}
{{domxref("GPUTextureView")}}
- : The resource to bind. This can be one of the following:
- : An array of entry objects describing the resources to expose to the shader. There will be one for each corresponding entry described by the
label
{{optional_inline}}
- : A string providing a label that can be used to identify the object, for example in
{{domxref("GPUError")}}
messages or console warnings.
- : A string providing a label that can be used to identify the object, for example in
layout
- : The
{{domxref("GPUBindGroupLayout")}}
that theentries
of this bind group will conform to.
- : The
- : An object containing the following properties:
GPUBufferBinding objects
A GPUBufferBinding
object can contain the following properties:
buffer
- : The
{{domxref("GPUBuffer")}}
object you want to bind.
- : The
offset
{{optional_inline}}
- : The offset, in bytes, from the beginning of the
buffer
to the beginning of the range exposed to the shader by the buffer binding. If omitted,offset
defaults to 0.
- : The offset, in bytes, from the beginning of the
size
{{optional_inline}}
- : The size, in bytes, of the buffer binding. If omitted,
size
will be the range starting atoffset
and ending at the end of thebuffer
. If bothoffset
andsize
are omitted, the entire buffer is exposed to the shader.
- : The size, in bytes, of the buffer binding. If omitted,
Return value
A {{domxref("GPUBindGroup")}}
object instance.
Validation
The following criteria must be met when calling createBindGroup()
, otherwise a {{domxref("GPUValidationError")}}
is generated and an invalid {{domxref("GPUBindGroup")}}
object is returned:
- The number of entries in the
layout
{{domxref("GPUBindGroupLayout")}}
equals the number of entry objects inentries
. - For each entry in the
layout
{{domxref("GPUBindGroupLayout")}}
, the corresponding entry object inentries
binds the correct resource type. For example, abuffer
resource layout object has aGPUBufferBinding
object specified in the corresponding binding. - If the resource layout object is a
buffer
:- The corresponding bound
{{domxref("GPUBuffer")}}
:- Has its bound part (as specified by
offset
andsize
) contained inside it completely, with a non-zero size. - Has a size bigger than the
buffer
resource layout’sminBindingSize
.
- Has its bound part (as specified by
- If the resource layout object
type
is"uniform"
:- The bound
{{domxref("GPUBuffer")}}
has ausage
that includesGPUBufferUsage.UNIFORM
. - The effective size of the bound buffer segment is less than or equal to the
{{domxref("GPUDevice")}}
'smaxUniformBufferBindingSize
{{domxref("GPUSupportedLimits", "limit", "", "nocode")}}
. - The specified
GPUBufferBinding
offset
is a multiple of the{{domxref("GPUDevice")}}
'sminUniformBufferOffsetAlignment
{{domxref("GPUSupportedLimits", "limit", "", "nocode")}}
.
- The bound
- If the resource layout object
type
is"storage"
or"read-only-storage"
:- The bound
{{domxref("GPUBuffer")}}
has ausage
that includesGPUBufferUsage.STORAGE
. - The effective size of the bound buffer segment is less than or equal to the
{{domxref("GPUDevice")}}
'smaxStorageBufferBindingSize
{{domxref("GPUSupportedLimits", "limit", "", "nocode")}}
. - The effective size of the bound buffer segment is a multiple of 4.
- The specified
GPUBufferBinding
offset
is a multiple of the{{domxref("GPUDevice")}}
'sminStorageBufferOffsetAlignment
{{domxref("GPUSupportedLimits", "limit", "", "nocode")}}
.
- The bound
- The corresponding bound
- If the resource layout object is a
storageTexture
, the corresponding bound{{domxref("GPUTextureView")}}
:- Has a
dimension
equal to the resource layout object’sviewDimension
(see{{domxref("GPUTexture.createView()")}}
for more details of a texture view’s settings). - Has a
format
equal to the resource layout object’ssampleType
. - Has a
mipLevelCount
equal to 1. - Is a view of a
{{domxref("GPUTexture")}}
with ausage
that includesGPUTextureUsage.STORAGE_BINDING
.
- Has a
- If the resource layout object is a
texture
, the corresponding bound{{domxref("GPUTextureView")}}
:- Has a
dimension
equal to the resource layout object’sviewDimension
(see{{domxref("GPUTexture.createView()")}}
for more details of a texture view’s settings). - Has a
format
compatible with the resource layout object’ssampleType
. - Is a view of a
{{domxref("GPUTexture")}}
with ausage
that includesGPUTextureUsage.TEXTURE_BINDING
. - is a view of a
{{domxref("GPUTexture")}}
with asampleCount
greater than 1 if the resource layout object’smultisampled
property istrue
, or equal to 1 if it isfalse
.
- Has a
Examples
[!NOTE] The WebGPU samples feature many more examples.
Basic example
Our basic compute demo shows an example of creating a bind group layout and then using that as a template when creating a bind group.
// ...
const bindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "storage",
},
},
],
});
const bindGroup = device.createBindGroup({
layout: bindGroupLayout,
entries: [
{
binding: 0,
resource: {
buffer: output,
},
},
],
});
// ...
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- The WebGPU API