The copyBufferToBuffer()
method of the GPUCommandEncoder
interface encodes a command that copies data from one GPUBuffer
to another.
copyBufferToBuffer(source, sourceOffset, destination, destinationOffset, size)
The following criteria must be met when calling copyBufferToBuffer()
, otherwise a GPUValidationError
is generated and the GPUCommandEncoder
becomes invalid:
- The
source
's GPUBuffer.usage
includes the GPUBufferUsage.COPY_SRC
flag. - The
destination
's GPUBuffer.usage
includes the GPUBufferUsage.COPY_DST
flag. -
size
, sourceOffset
, and destinationOffset
are all multiples of 4. - The
source
's GPUBuffer.size
is greater than or equal to sourceOffset
+ size
. - The
destination
's GPUBuffer.size
is greater than or equal to destinationOffset
+ size
. -
source
and destination
are different GPUBuffer
s (you can't copy from and to the same buffer).
In our basic compute demo, we use copyBufferToBuffer()
to copy the contents of our output
buffer to the stagingBuffer
.
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,
});
const commandEncoder = device.createCommandEncoder();
commandEncoder.copyBufferToBuffer(
output,
0,
stagingBuffer,
0,
BUFFER_SIZE,
);