GPUDevice: createBindGroup() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Note: This feature is available inWeb Workers.
ThecreateBindGroup() method of theGPUDevice interface creates aGPUBindGroup based on aGPUBindGroupLayout that defines a set of resources to be bound together in a group and how those resources are used in shader stages.
In this article
Syntax
createBindGroup(descriptor)Parameters
descriptorAn object containing the following properties:
entriesAn array of entry objects describing the resources to expose to the shader. There will be one for each corresponding entry described by the
GPUBindGroupLayoutreferenced inlayout. Each entry object has the following properties:bindingA number representing a unique identifier for this resource binding, which matches the
bindingvalue of a correspondingGPUBindGroupLayoutentry. In addition, it matches thenindex value of the corresponding@binding(n)attribute in the shader (GPUShaderModule) used in the related pipeline.resourceThe resource to bind. This can be one of the following:
GPUBufferBinding: Wraps aGPUBuffer; seeGPUBufferBinding objects for a definition.GPUBuffer: Can be used directly rather than being wrapped in aGPUBufferBinding, provided the defaultoffsetandsizevalues are being used.GPUExternalTextureGPUTextureView: Can be used in place of aGPUExternalTextureprovided it is compatible (a 2D format with a single subresource, that is,dimension: "2d").GPUTexture: Can be used in place of aGPUTextureView, provided a default view is desired. When used in this context,GPUTextureis equivalent to aGPUTextureViewobject created using aGPUTexture.createView()call with no argument specified.GPUSampler
labelOptionalA string providing a label that can be used to identify the object, for example in
GPUErrormessages or console warnings.layoutThe
GPUBindGroupLayoutthat theentriesof this bind group will conform to.
GPUBufferBinding objects
AGPUBufferBinding object can contain the following properties:
bufferThe
GPUBufferobject you want to bind.offsetOptionalThe offset, in bytes, from the beginning of the
bufferto the beginning of the range exposed to the shader by the buffer binding. If omitted,offsetdefaults to 0.sizeOptionalThe size, in bytes, of the buffer binding. If omitted,
sizewill be the range starting atoffsetand ending at the end of thebuffer. If bothoffsetandsizeare omitted, the entire buffer is exposed to the shader.
Return value
AGPUBindGroup object instance.
Validation
The following criteria must be met when callingcreateBindGroup(), otherwise aGPUValidationError is generated and an invalidGPUBindGroup object is returned:
- The number of entries in the
layoutGPUBindGroupLayoutequals the number of entry objects inentries. - For each entry in the
layoutGPUBindGroupLayout, the corresponding entry object inentriesbinds the correct resource type. For example, abufferresource layout object has aGPUBufferBindingobject specified in the corresponding binding. - If the resource layout object is a
buffer:- The corresponding bound
GPUBuffer:- Has its bound part (as specified by
offsetandsize) contained inside it completely, with a non-zero size. - Has a size bigger than the
bufferresource layout'sminBindingSize.
- Has its bound part (as specified by
- If the resource layout object
typeis"uniform": - If the resource layout object
typeis"storage"or"read-only-storage":- The bound
GPUBufferhas ausagethat includesGPUBufferUsage.STORAGE. - The effective size of the bound buffer segment is less than or equal to the
GPUDevice'smaxStorageBufferBindingSizelimit. - The effective size of the bound buffer segment is a multiple of 4.
- The specified
GPUBufferBindingoffsetis a multiple of theGPUDevice'sminStorageBufferOffsetAlignmentlimit.
- The bound
- The corresponding bound
- If the resource layout object is a
storageTexture, the corresponding boundGPUTextureView:- Has a
dimensionequal to the resource layout object'sviewDimension(seeGPUTexture.createView()for more details of a texture view's settings). - Has a
formatequal to the resource layout object'ssampleType. - Has a
mipLevelCountequal to 1. - Is a view of a
GPUTexturewith ausagethat includesGPUTextureUsage.STORAGE_BINDING.
- Has a
- If the resource layout object is a
texture, the corresponding boundGPUTextureView:- Has a
dimensionequal to the resource layout object'sviewDimension(seeGPUTexture.createView()for more details of a texture view's settings). - Has a
formatcompatible with the resource layout object'ssampleType. - Is a view of a
GPUTexturewith ausagethat includesGPUTextureUsage.TEXTURE_BINDING. - is a view of a
GPUTexturewith asampleCountgreater than 1 if the resource layout object'smultisampledproperty istrue, or equal to 1 if it isfalse.
- Has a
Examples
Note:TheWebGPU samples feature many more examples.
Basic example
Ourbasic 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
| Specification |
|---|
| WebGPU> # dom-gpudevice-createbindgroup> |
Browser compatibility
See also
- TheWebGPU API