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::eUploadmemory from the CPU (throughbuffer.mapped), recordcmdCopyBufferto copy the data into a buffer inMemoryKind::eGPUmemory, and submit the command list. - Readback: record
cmdCopyBufferto copy fromMemoryKind::eGPUmemory into a buffer inMemoryKind::eReadbackmemory, 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¶
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¶
const VkBufferCopy copyIn{.srcOffset = 0, .dstOffset = 0, .size = kBytes};
vkCmdCopyBuffer(cmd, staging, input, 1, ©In);
const VkBufferCopy copyOut{.srcOffset = 0, .dstOffset = 0, .size = kBytes};
vkCmdCopyBuffer(cmd, output, result, 1, ©Out);
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.
Rules and errors¶
- Call
cmdCopyBufferoutside 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 + sizeinsrc,dstOffset + sizeindst) must lie inside their buffers, and must not overlap ifsrcanddstare 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, andcopyLayout.