Transfers¶
What it does¶
<RI/Helpers/Transfers.hpp> provides one-call uploads and readbacks in RI::Helpers:
uploadBuffer(device, dst, offset, data, size)copiessizebytes fromdataintodstatoffset(dstrequiresBufferUsage::eCopyDst).readbackBuffer(device, src, offset, size)copiessizebytes fromsrcatoffsetand returns them as astd::vector<std::byte>(srcrequiresBufferUsage::eCopySrc).uploadTexture(device, dst, texels)uploads tightly packed texels into texture viewdst(its texture requiresTextureUsage::eCopyDst).readbackTexture(device, src)reads the texels of texture viewsrcback and returns them tightly packed as astd::vector<std::byte>(its texture requiresTextureUsage::eCopySrc).
Each call allocates its own temporary staging Memory (MemoryKind::eUpload or
MemoryKind::eReadback) and staging Buffer, creates a CommandList, records a Stage::eAll to
Stage::eCopy barrier, the copy command, and a Stage::eCopy to Stage::eAll barrier, submits
on device.graphicsQueue, waits for completion, and frees those temporary objects before
returning.
For uploadTexture and readbackTexture, texels holds the view's single mip and all of its
layers (or 3D slices) with rows tightly packed: layout.width * layout.bytesPerTexel bytes per
row (where layout is the RI::CopyLayout returned by RI::copyLayout),
with no padding between rows and layers stored one after the other. Internally, the helper copies
each row to or from layer * layout.layerPitch + y * layout.rowPitch in the staging buffer so you
do not have to handle backend alignment rules.
RI does not read image files (such as KTX, PNG, or DDS). Decode them with a library of your choice
(such as libktx or stb_image) into uncompressed texels, then upload them one mip at a time through
a view of that mip (RI::createView with baseMip set to the mip index and mipCount = 1).
Because each call submits and waits for the GPU to finish, these helpers suit asset loading and tools rather than per-frame streaming.
Example¶
const std::vector<uint32> image(256 * 256, 0xff00ff00u); // rows tightly packed
RI::Helpers::uploadTexture(device, texture.view, image.data());
const std::vector<std::byte> texels = RI::Helpers::readbackTexture(device, texture.view);
How to roll your own¶
For a texture upload, call RI::copyLayout(device, view) to get the staging layout, allocate
MemoryKind::eUpload memory sized via RI::bufferRequirements for a Buffer of layout.size
bytes (BufferUsage::eCopySrc), and create the staging buffer. Write each row of each layer to
buffer.mapped at offset layer * layout.layerPitch + y * layout.rowPitch, record
RI::cmdCopyBufferToTexture (or RI::cmdCopyBuffer for a buffer upload) with any needed
Stage::eCopy barriers, and submit the command list on device.graphicsQueue.
For a readback, allocate MemoryKind::eReadback memory and a BufferUsage::eCopyDst staging
buffer, record RI::cmdCopyTextureToBuffer or RI::cmdCopyBuffer, submit on device.graphicsQueue,
wait for the returned TimelinePoint with RI::wait, and then read from buffer.mapped.
To stream transfers across frames without stalling the CPU, keep a ring of staging buffers and
reuse or free each region only after its submission's TimelinePoint has been reached instead of
waiting immediately.