Class: BufferResource

BufferResource

A resource that can be bound to a bind group and used in a shader. Whilst a buffer can be used as a resource, this class allows you to specify an offset and size of the buffer to use. This is useful if you have a large buffer and only part of it is used in a shader.

This resource, will listen for changes on the underlying buffer and emit a itself if the buffer changes shape.

new BufferResource (options)

Create a new Buffer Resource.

Name Type Description
options {
   buffer: Buffer,
   offset?: number,
   size?: number
}

The options for the buffer resource

options.buffer Buffer

The underlying buffer that this resource is using

options.offset number

The offset of the buffer this resource is using. If not provided, then it will use the offset of the buffer.

options.size number

The size of the buffer this resource is using. If not provided, then it will use the size of the buffer.

Example

const buffer = new Buffer({
    data: new Float32Array(1000),
   usage: BufferUsage.UNIFORM,
});
// Create a buffer resource that uses the first 100 bytes of a buffer
const bufferResource = new BufferResource({
   buffer,
   offset: 0,
   size: 100,
});

Extends

  • EventEmitter

Implements

Members

buffer Buffer

the underlying buffer that this resource is using

destroyed boolean readonly

Has the Buffer resource been destroyed?

Default Value:
  • false

offset number readonly

the offset of the buffer this resource is using. If not provided, then it will use the offset of the buffer.

size number readonly

the size of the buffer this resource is using. If not provided, then it will use the size of the buffer.

Methods

destroy (destroyBuffer) void

Destroys this resource. Make sure the underlying buffer is not used anywhere else if you want to destroy it as well, or code will explode

Name Type Default Description
destroyBuffer boolean false

Should the underlying buffer be destroyed as well?

Events

change

emits when the underlying buffer has changed shape (i.e. resized) letting the renderer know that it needs to discard the old buffer on the GPU and create a new one