大网数据广告活动
Web 标准与前端开发

GPUCommandEncoder: copyBufferToBuffer() method

· 52CE 我爱测

The **`copyBufferToBuffer()`** method of the GPUCommandEncoder interface encodes a command that copies data from one GPUBuffer to another.

WebGPU API

The copyBufferToBuffer() method of the GPUCommandEncoder interface encodes a command that copies data from one GPUBuffer to another.

Syntax

copyBufferToBuffer(source, destination)
copyBufferToBuffer(source, destination, size)
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.

[!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.

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 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);

// …

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。