The createBuffer() method of the GPUDevice interface creates a GPUBuffer in which to store raw data to use in GPU operations.
A GPUBuffer object instance.
The following criteria must be met when calling createBuffer(), otherwise a GPUValidationError is generated and an invalid GPUBuffer object is returned:
- A valid
usage is specified. -
GPUBufferUsage.MAP_READ is specified, and no additional flags are specified other than GPUBufferUsage.COPY_DST. -
GPUBufferUsage.MAP_WRITE is specified, and no additional flags are specified other than GPUBufferUsage.COPY_SRC. -
mappedAtCreation: true is specified, and the specified size is a multiple of 4.
Note: If the buffer allocation fails without any specific side-effects, a GPUOutOfMemoryError object is generated.
In our basic compute demo, we create an output buffer to read GPU calculations to, and a staging buffer to be mapped for JavaScript access.
const output = device.createBuffer({
size: BUFFER_SIZE,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
});
const stagingBuffer = device.createBuffer({
size: BUFFER_SIZE,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});