Skip to content

RI::cmdBegin, RI::cmdEnd

What it does

RI::cmdBegin(cmd) opens a command list for recording, and RI::cmdEnd(cmd) closes it. Every cmd* recording call goes between the two, and only a closed list can be submitted.

cmdBegin also clears the list's previous state: no descriptor heap, no pipeline, and no root data are set, so set them again in each recording.

On Vulkan, cmdEnd also records a barrier from all GPU writes to the host (the CPU). Once the submission's timeline point is reached, mapped memory therefore shows the GPU's writes, just as on D3D12 without a barrier. You never write a barrier to the CPU yourself.

Signature

// <RI/RI.hpp>, namespace RI
void cmdBegin(CommandList &cmd);
// <RI/RI.hpp>, namespace RI
void cmdEnd(CommandList &cmd);

From the header: After cmdEnd's submission is reached, CPU reads see the GPU's writes (P94 (6)).

Example

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::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);
const VkCommandBufferBeginInfo beginInfo{.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
                                         .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT};
check(vkBeginCommandBuffer(cmd, &beginInfo), "vkBeginCommandBuffer");
// Make the copy's writes visible to the CPU (the host stage) before ending.
barrier(VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_2_HOST_BIT, VK_ACCESS_2_HOST_READ_BIT);
check(vkEndCommandBuffer(cmd), "vkEndCommandBuffer");

What to watch for: VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT tells the driver that the command buffer is submitted once before being reset, which is always true for RI command lists. Without the host barrier, CPU reads of mapped memory after waiting on the timeline semaphore are not guaranteed to see the GPU's writes.

check(cmd->Reset(allocator.Get(), nullptr), "ID3D12GraphicsCommandList::Reset"); // opens it for recording
// No barrier to the CPU: once the fence value is reached, mapped reads see the writes.
check(cmd->Close(), "ID3D12GraphicsCommandList::Close");

What to watch for: D3D12 has no separate begin call. A new command list is created closed, Reset (with its command allocator) opens it for recording, and Close ends it.

Rules and errors

  • Begin only a list that is not being executed: if it was submitted, wait for its timeline point and call resetCommandList first.
  • Record commands only between cmdBegin and cmdEnd, and submit only after cmdEnd.
  • Call cmdBegin and cmdEnd on the device thread.
  • Close any open marker regions (cmdEndMarker) and rendering section (cmdEndRendering) before calling cmdEnd. In validation builds, leaving a marker region open (or an open rendering section on Vulkan), calling cmdBegin on a destroyed list, or calling from another thread reports a programmer error.
  • A failing native call (for example, running out of memory while closing) reports through the error channel.