GPUCommandEncoder: copyBufferToBuffer() method
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The copyBufferToBuffer()
method of the
GPUCommandEncoder
interface encodes a command that copies data from one GPUBuffer
to another.
Syntax
js
copyBufferToBuffer(source, sourceOffset, destination, destinationOffset, size)
Parameters
Return value
None (Undefined
).
Validation
The following criteria must be met when calling copyBufferToBuffer()
, otherwise a GPUValidationError
is generated and the GPUCommandEncoder
becomes invalid:
- The
source
'sGPUBuffer.usage
includes theGPUBufferUsage.COPY_SRC
flag. - The
destination
'sGPUBuffer.usage
includes theGPUBufferUsage.COPY_DST
flag. size
,sourceOffset
, anddestinationOffset
are all multiples of 4.- The
source
'sGPUBuffer.size
is greater than or equal tosourceOffset
+size
. - The
destination
'sGPUBuffer.size
is greater than or equal todestinationOffset
+size
. source
anddestination
are differentGPUBuffer
s (you can't copy from and to the same buffer).
Examples
In our basic compute demo, we use copyBufferToBuffer()
to copy the contents of our output
buffer to the stagingBuffer
.
js
// ...
// 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,
});
// ...
// Create GPUCommandEncoder to encode commands to issue to the GPU
const commandEncoder = device.createCommandEncoder();
// ...
// Copy output buffer to staging buffer
commandEncoder.copyBufferToBuffer(
output,
0, // Source offset
stagingBuffer,
0, // Destination offset
BUFFER_SIZE
);
// ...
Specifications
Specification |
---|
WebGPU # dom-gpucommandencoder-copybuffertobuffer |
Browser compatibility
BCD tables only load in the browser
See also
- The WebGPU API