Skip to content

RI::cmdCopyBuffer

What it does

RI::cmdCopyBuffer(cmd, src, srcOffset, dst, dstOffset, size) records a command on cmd to copy size bytes from src (starting at byte offset srcOffset) to dst (starting at byte offset dstOffset). The GPU performs the copy when the command list runs.

Copies move data between memory kinds:

  • Upload: write into a buffer in MemoryKind::eUpload memory from the CPU (through buffer.mapped), record cmdCopyBuffer to copy the data into a buffer in MemoryKind::eGPU memory, and submit the command list.
  • Readback: record cmdCopyBuffer to copy from MemoryKind::eGPU memory into a buffer in MemoryKind::eReadback memory, submit the command list, wait for its timeline point to complete, and then read the data on the CPU.

The source buffer must be created with BufferUsage::eCopySrc, and the destination buffer with BufferUsage::eCopyDst.

Signature

// <RI/RI.hpp>, namespace RI
void cmdCopyBuffer(CommandList &cmd, const Buffer &src, uint64 srcOffset, const Buffer &dst, uint64 dstOffset,
                   uint64 size);

From the header: Counts as Stage::eCopy.

Example

RI::cmdCopyBuffer(cmd, staging, 0, input, 0, kBytes);

For synchronization, cmdCopyBuffer counts as Stage::eCopy: put a barrier between an earlier command that writes src and the copy, and between the copy and a later command that reads or writes dst.

RI::cmdBegin(cmd);
RI::cmdCopyBuffer(cmd, staging, 0, input, 0, kBytes);
RI::cmdBarrier(cmd, RI::Stage::eCopy, RI::Stage::eCompute); // the copy's writes, then the shader's reads
RI::cmdSetHeap(cmd, heap);
RI::cmdSetPipeline(cmd, pipeline);
RI::Helpers::cmdSetRootData(cmd, Root{.inputSlot = 0, .outputSlot = 1, .paramsSlot = 2, .scale = 3.0f, .count = kCount});
RI::cmdDispatch(cmd, (kCount + 63) / 64, 1, 1); // 64 threads per group, see [numthreads]
RI::cmdBarrier(cmd, RI::Stage::eCompute, RI::Stage::eCopy); // the shader's writes, then the copy's reads
RI::cmdCopyBuffer(cmd, output, 0, result, 0, kBytes);
RI::cmdEnd(cmd);

The same in native code

RI::cmdCopyBuffer(cmd, staging, 0, input, 0, kBytes);
const VkBufferCopy copyIn{.srcOffset = 0, .dstOffset = 0, .size = kBytes};
vkCmdCopyBuffer(cmd, staging, input, 1, &copyIn);
const VkBufferCopy copyOut{.srcOffset = 0, .dstOffset = 0, .size = kBytes};
vkCmdCopyBuffer(cmd, output, result, 1, &copyOut);

What to watch for: vkCmdCopyBuffer takes the source buffer before the destination buffer, and accepts an array of VkBufferCopy regions so one call can copy several ranges.

cmd->CopyBufferRegion(input.Get(), 0, staging.Get(), 0, kBytes); // destination first
cmd->CopyBufferRegion(result.Get(), 0, output.Get(), 0, kBytes);

What to watch for: ID3D12GraphicsCommandList::CopyBufferRegion takes the destination buffer and offset first, before the source.

Rules and errors

  • Call cmdCopyBuffer outside rendering sections, on the device thread. Validation builds report calls from other threads as a programmer error, and the native validation layers report copies inside a rendering section.
  • Both byte ranges (srcOffset + size in src, dstOffset + size in dst) must lie inside their buffers, and must not overlap if src and dst are the same buffer. The native validation layers report violations.
  • In validation builds, passing a destroyed, uninitialized, or foreign-device buffer is a programmer error (ErrorCategory::eProgrammerError).
  • For copies between buffers and textures (and the staging-layout query), see cmdCopyBufferToTexture, cmdCopyTextureToBuffer, and copyLayout.