GPUCommandEncoder: copyBufferToBuffer() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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.

Syntax

js
copyBufferToBuffer(source, sourceOffset, destination, destinationOffset, size)

Parameters

source

The GPUBuffer to copy from.

sourceOffset

The offset, in bytes, into the source to begin copying from.

destination

The GPUBuffer to copy to.

destinationOffset

The offset, in bytes, into the destination to begin copying to.

size

The number of bytes to copy.

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'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 GPUBuffers (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

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
copyBufferToBuffer
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.
User must explicitly enable this feature.
Has more compatibility info.

See also