This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The copyBufferToBuffer() method of the GPUCommandEncoder interface encodes a command that copies data from one GPUBuffer to another.
copyBufferToBuffer(source, destination) copyBufferToBuffer(source, destination, size) copyBufferToBuffer(source, sourceOffset, destination, destinationOffset, size)
sourceThe GPUBuffer to copy from.
sourceOffset OptionalThe offset, in bytes, into the source to begin copying from.
destinationThe GPUBuffer to copy to.
destinationOffset OptionalThe offset, in bytes, into the destination to begin copying to.
size OptionalThe number of bytes to copy.
Note: The sourceOffset and destinationOffset can be omitted if you are copying part of the source buffer at a 0 offset in both the source and destination buffers. The sourceOffset, destinationOffset, and size can be omitted if you are copying the entire source buffer to the destination buffer.
None (Undefined).
The following criteria must be met when calling copyBufferToBuffer(), otherwise a GPUValidationError is generated and the GPUCommandEncoder becomes invalid:
source's GPUBuffer.usage includes the GPUBufferUsage.COPY_SRC flag.destination's GPUBuffer.usage includes the GPUBufferUsage.COPY_DST flag.size, sourceOffset, and destinationOffset are all multiples of 4.source's GPUBuffer.size is greater than or equal to sourceOffset + size.destination's GPUBuffer.size is greater than or equal to destinationOffset + size.source and destination are different GPUBuffers (you can't copy from and to the same buffer).In our basic compute demo, we use copyBufferToBuffer() to copy the contents of our outputBuffer to the stagingBuffer.
// …
// Create an output buffer to read GPU calculations to, and a staging buffer to be mapped for JavaScript access
const outputBuffer = 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(
outputBuffer,
0, // Source offset
stagingBuffer,
0, // Destination offset
BUFFER_SIZE,
);
// Since we are copying the entire buffer, this can be shortened to
// commandEncoder.copyBufferToBuffer(outputBuffer, stagingBuffer);
// …
| Specification |
|---|
| WebGPU> # dom-gpucommandencoder-copybuffertobuffer> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
copyBufferToBuffer |
113Currently supported on ChromeOS, macOS, and Windows only. |
113Currently supported on ChromeOS, macOS, and Windows only. |
141Currently supported on Windows only, in all contexts except for service workers. |
99Currently supported on ChromeOS, macOS, and Windows only. |
26 | 121 | No | 81 | 26 | 25.0 | 121 | 26 |
offset_and_size_parameter_optional |
137Currently supported on ChromeOS, macOS, and Windows only. |
137Currently supported on ChromeOS, macOS, and Windows only. |
No | 121Currently supported on ChromeOS, macOS, and Windows only. |
No | 137 | No | 90 | No | No | 137 | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/GPUCommandEncoder/copyBufferToBuffer