Used to format uniforms or storages struct inputs and create a single typed array that will hold all those inputs values. The array needs to be correctly padded depending on every value type, so it can be safely used as a GPUBuffer input.

It will also create WGSL Structs and variables according to the BufferBinding inputs parameters.

The WGSL structs and variables declaration may vary based on the input types, especially if there's one or more arrays involved (i.e. array<f32>, array<vec3f> etc.).

It is possible to create complex WGSL structs with children structs by using the childrenBindings parameter.

A BufferBinding can also have a parent BufferBinding, in which case it won't create a GPUBuffer but use its parent GPUBuffer at the right offset. Useful to create a unique BufferBinding with a single GPUBuffer to handle multiple BufferBinding and update them with a single writeBuffer call.

Example

// create a GPU buffer binding
const bufferBinding = new BufferBinding({
name: 'params', // name of the WGSL object
bindingType: 'uniform', // should be 'storage' for large arrays
struct: {
opacity: {
type: 'f32',
value: 1,
},
mousePosition: {
type: 'vec2f',
value: new Vec2(),
},
},
})

Hierarchy (view full)

Constructors

Properties

label: string

The label of the Binding

name: string

The name/key of the Binding

visibility: number

The visibility of the Binding in the shaders

shouldResetBindGroup: boolean

Flag indicating whether we should recreate the parentMesh bind group, usually when a resource has changed

shouldResetBindGroupLayout: boolean

Flag indicating whether we should recreate the parentMesh GPU bind group layout, usually when a resource layout has changed

cacheKey: string

A cache key allowing to get / set bindings from the device manager map cache. Used for BufferBinding only at the moment.

bindingType: BufferBindingType

The binding type of the BufferBinding

useStruct: boolean

Flag to indicate whether this BufferBinding buffer elements should be packed in a single structured object or if each one of them should be a separate binding.

inputs: Record<string, BufferBindingInput>

All the BufferBinding data inputs

childrenBindings: BufferBinding[]

Array of children BufferBinding used as struct children.

shouldUpdate: boolean

Flag to indicate whether one of the inputs value has changed and we need to update the GPUBuffer linked to the arrayBuffer array

bufferElements: AllowedBufferElement[]

An array describing how each corresponding inputs should be inserted into our arrayView array

arrayBufferSize: number

Total size of our arrayBuffer array in bytes

arrayBuffer: ArrayBuffer

Array buffer that will be sent to the GPUBuffer

arrayView: DataView

Data view of our array buffer

parentView: DataView

DataView inside the parent arrayBuffer if set.

parentViewSetBufferEls: {
    bufferElement: AllowedBufferElement;
    viewSetFunction: DataViewSetFunction;
}[]

Array of bufferElements and according view set functions to use if the parent is set.

Type declaration

buffer: Buffer

The Buffer holding the GPUBuffer

wgslStructFragment: string

A string to append to our shaders code describing the WGSL structure representing this BufferBinding

wgslGroupFragment: string[]

An array of strings to append to our shaders code declaring all the WGSL variables representing this BufferBinding

Options used to create this BufferBinding

Accessors

  • get resourceLayout(): {
        buffer: GPUBufferBindingLayout;
        offset?: number;
        size?: number;
    }
  • Get GPUBindGroupLayoutEntry#buffer | bind group layout entry resource.

    Returns {
        buffer: GPUBufferBindingLayout;
        offset?: number;
        size?: number;
    }

    • buffer: GPUBufferBindingLayout

      GPUBindGroupLayout | bind group layout resource

    • Optional offset?: number

      Offset in bytes in the parent buffer if set.

    • Optional size?: number

      Size in bytes in the parent buffer if set.

  • get resource(): {
        buffer: GPUBuffer;
        offset?: number;
        size?: number;
    }
  • Get GPUBindGroupEntry#resource | bind group resource.

    Returns {
        buffer: GPUBuffer;
        offset?: number;
        size?: number;
    }

    • buffer: GPUBuffer

      GPUBindGroup | bind group resource

    • Optional offset?: number

      Offset in bytes in the parent buffer if set.

    • Optional size?: number

      Size in bytes in the parent buffer if set.

Methods

  • Round the given size value to the nearest minimum GPUDevice buffer offset alignment.

    Parameters

    • value: number

      Size to round.

    Returns number

  • Set the WGSL code snippet to append to the shaders code. It consists of variable (and Struct structures if needed) declarations.

    Returns void

  • Extract the data corresponding to a specific BufferElement from a Float32Array holding the GPU buffer data of this BufferBinding

    Parameters

    • parameters: {
          result: Float32Array;
          bufferElementName: string;
      }

      parameters used to extract the data

      • result: Float32Array

        Float32Array holding GPUBuffer data

      • bufferElementName: string

        name of the BufferElement to use to extract the data

    Returns Float32Array

    • extracted data from the Float32Array