GPUCommandEncoder: copyBufferToBuffer() 方法
GPUCommandEncoder 接口的 copyBufferToBuffer() 方法会编码一个命令,该命令将数据从一个 GPUBuffer 复制到另一个。
语法
js
copyBufferToBuffer(source, destination)
copyBufferToBuffer(source, destination, size)
copyBufferToBuffer(source, sourceOffset, destination, destinationOffset, size)
参数
source-
要从中复制的
GPUBuffer。 sourceOffset可选-
从
source开始复制的偏移量(以字节为单位)。 destination-
要复制到的
GPUBuffer。 destinationOffset可选-
复制到
destination的偏移量(以字节为单位)。 size可选-
要复制的字节数。
注意: 如果您要在源缓冲区和目标缓冲区都以 0 偏移量复制源缓冲区的某一部分,则可以省略 sourceOffset 和 destinationOffset。如果您要将整个源缓冲区复制到目标缓冲区,则可以省略 sourceOffset、destinationOffset 和 size。
返回值
无 (Undefined)。
验证
调用 copyBufferToBuffer() 时必须满足以下条件,否则将生成 GPUValidationError,并且 GPUCommandEncoder 将失效。
source的GPUBuffer.usage包含GPUBufferUsage.COPY_SRC标志。destination的GPUBuffer.usage包含GPUBufferUsage.COPY_DST标志。size、sourceOffset和destinationOffset都必须是 4 的倍数。source的GPUBuffer.size大于或等于sourceOffset+size。destination的GPUBuffer.size大于或等于destinationOffset+size。source和destination是不同的GPUBuffer(不能从同一缓冲区复制到同一缓冲区)。
示例
在我们 基本的计算演示中,我们使用 copyBufferToBuffer() 将 outputBuffer 的内容复制到 stagingBuffer。
js
// …
// 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);
// …
规范
| 规范 |
|---|
| WebGPU # dom-gpucommandencoder-copybuffertobuffer |
浏览器兼容性
加载中…