The writeBuffer()
method of the GPUQueue
interface writes a provided data source into a given GPUBuffer
.
This is a convenience function, which provides an alternative to setting buffer data via buffer mapping and buffer-to-buffer copies. It lets the user agent determine the most efficient way to copy the data over.
writeBuffer(buffer, bufferOffset, data, dataOffset, size)
The following criteria must be met when calling writeBuffer()
, otherwise a GPUValidationError
is generated and the GPUQueue
becomes invalid:
-
buffer
is available for use, i.e. not unavailable (GPUBuffer
s are unavailable if they are currently mapped) or destroyed (with the GPUBuffer.destroy()
method). - The
buffer
's GPUBuffer.usage
includes the GPUBufferUsage.COPY_DST
flag. -
bufferOffset
, when converted to bytes, is a multiple of 4. - The size of
data
- dataOffset
+ bufferOffset
, when converted to bytes, is equal to or less than the buffer
's GPUBuffer.size
.
In our basic render demo, we define some vertex data in a Float32Array
that we'll use to draw a triangle:
const vertices = new Float32Array([
0.0, 0.6, 0, 1, 1, 0, 0, 1, -0.5, -0.6, 0, 1, 0, 1, 0, 1, 0.5, -0.6, 0, 1, 0,
0, 1, 1,
]);
To use this data in a render pipeline, we need to put it into a GPUBuffer
. First we'll create the buffer:
const vertexBuffer = device.createBuffer({
size: vertices.byteLength,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
});
To get the data into the buffer we can use writeBuffer()
:
device.queue.writeBuffer(vertexBuffer, 0, vertices, 0, vertices.length);