Skip to content

RI::submit, RI::wait, RI::isReached

What it does

  • RI::submit(queue, cmd) sends a closed command list (ended with cmdEnd) to the GPU and returns immediately with a TimelinePoint: the value the queue's timeline reaches when the GPU finishes executing this list.
  • RI::wait(queue, point) blocks the calling thread until point is reached.
  • RI::isReached(queue, point) returns true if point has been reached and false otherwise, without blocking.

The queue is device.graphicsQueue, the only queue in this version. Timeline points on a queue only increase: each submit call returns a larger value than the previous one, and reaching a point means every earlier submission on that queue has finished too. A default-constructed point ({0}) is always reached.

Timeline points tell you when you may read results, reuse a command list, overwrite a descriptor, or destroy an object; see Timeline points.

Signature

// <RI/RI.hpp>, namespace RI
TimelinePoint submit(Queue &queue, CommandList &cmd);

From the header: The list must be ended. Returns the point reached when its work is done (P20).

// <RI/RI.hpp>, namespace RI
void wait(Queue &queue, TimelinePoint point);

From the header: Blocks the CPU until point is reached; isReached only asks.

// <RI/RI.hpp>, namespace RI
bool isReached(Queue &queue, TimelinePoint point);
// <RI/RI.hpp>, namespace RI
struct TimelinePoint {
    uint64 value = 0;
};

From the header: A point on a queue's GPU timeline (P20): reached when the GPU has finished the submission that returned it. Values only grow; 0 is always reached.

Field Type Default Description
value uint64 0
// <RI/RI.hpp>, namespace RI
struct Queue {
    // internal: backend-private state (not part of the API)
};
  • D3D12 header: The graphics (DIRECT) queue (P37). Submissions return timeline points (P20).
  • Vulkan header: The graphics queue (P37: the only queue in v1). Submissions return timeline points (P20).

Example

RI::Queue &queue = device.graphicsQueue;
const RI::TimelinePoint done = RI::submit(queue, cmd);
RI::wait(queue, done); // blocks until the GPU has finished this submission
// Readback memory is mapped too; after the point is reached, reads see the GPU's writes.
const auto *out = static_cast<const float *>(result.mapped);
std::printf("out[1] = %g (expected 3.25)\n", out[1]);

Polling instead of blocking:

// Do other CPU work while the GPU runs; isReached never blocks.
uint32 polls = 0;
while (!RI::isReached(queue, again)) {
    ++polls;
}
std::printf("reached after %u polls (point %llu)\n", polls, static_cast<unsigned long long>(again.value));

The same in native code

RI::Queue &queue = device.graphicsQueue;
const RI::TimelinePoint done = RI::submit(queue, cmd);
RI::wait(queue, done); // blocks until the GPU has finished this submission
// A timeline semaphore: a 64-bit counter the GPU raises as work completes.
const VkSemaphoreTypeCreateInfo timelineType{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
                                             .semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
                                             .initialValue = 0};
const VkSemaphoreCreateInfo semaphoreInfo{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, .pNext = &timelineType};
VkSemaphore timeline = VK_NULL_HANDLE;
check(vkCreateSemaphore(device, &semaphoreInfo, nullptr, &timeline), "vkCreateSemaphore");
uint64_t lastSignaled = 0;
// Submit, asking the GPU to raise the counter to the next value when done.
const uint64_t done = ++lastSignaled;
const VkCommandBufferSubmitInfo commandSubmit{.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO,
                                              .commandBuffer = cmd};
const VkSemaphoreSubmitInfo signal{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
                                   .semaphore = timeline,
                                   .value = done,
                                   .stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT};
const VkSubmitInfo2 submitInfo{.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2,
                               .commandBufferInfoCount = 1,
                               .pCommandBufferInfos = &commandSubmit,
                               .signalSemaphoreInfoCount = 1,
                               .pSignalSemaphoreInfos = &signal};
check(vkQueueSubmit2(queue, 1, &submitInfo, VK_NULL_HANDLE), "vkQueueSubmit2");
const VkSemaphoreWaitInfo waitInfo{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO,
                                   .semaphoreCount = 1,
                                   .pSemaphores = &timeline,
                                   .pValues = &done};
check(vkWaitSemaphores(device, &waitInfo, UINT64_MAX), "vkWaitSemaphores");
uint64_t reached = 0;
check(vkGetSemaphoreCounterValue(device, timeline, &reached), "vkGetSemaphoreCounterValue");
const bool finished = reached >= done; // never blocks

What to watch for: the signal value goes in the submission itself (VkSemaphoreSubmitInfo in synchronization2), and signaled values must strictly increase.

// A fence: a 64-bit counter the queue raises as work completes.
ComPtr<ID3D12Fence> fence;
check(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)), "CreateFence");
UINT64 lastSignaled = 0;
ID3D12CommandList *lists[] = {cmd.Get()};
queue->ExecuteCommandLists(1, lists);
const UINT64 done = ++lastSignaled;
check(queue->Signal(fence.Get(), done), "Signal"); // raised when the work before it is done
if (fence->GetCompletedValue() < done) {
    // A null event: the call blocks until the fence reaches the value.
    check(fence->SetEventOnCompletion(done, nullptr), "SetEventOnCompletion");
}
const bool finished = fence->GetCompletedValue() >= done; // never blocks (UINT64_MAX: device removed)

What to watch for: Signal is a separate queue command after ExecuteCommandLists. SetEventOnCompletion with a null event blocks until the value is reached.

Rules and errors

  • Submit a command list only after cmdEnd, and only once per recording.
  • Call submit on the device thread (the thread that called createDevice). wait and isReached may be called from any thread.
  • In validation builds, submitting a destroyed command list or calling submit from another thread reports a programmer error.
  • If the device is lost while submitting, waiting, or polling, the call reports a device lost error through the error channel.
  • wait has no timeout in this version.