Threads¶
RI objects are not thread-safe. Every device has one device thread, the thread that called
createDevice. Except for a short list of calls, all work on a device and its objects must run
on that thread.
The any-thread list¶
These calls may run on any thread, at the same time as each other and as recording and submission on the device thread:
| Calls | Why they are on the list |
|---|---|
createComputePipeline, createGraphicsPipeline, createWorkGraph |
Pipeline creation is the slow part of loading. On a cold driver, a work graph takes about half a second per node on one core. |
workGraphEntry |
The thread that creates a work graph usually needs to look up its entry node indices. |
wait, isReached |
A readback or capture thread can wait for a timeline point while the device thread keeps recording. |
bufferRequirements, textureRequirements, formatSupported, copyLayout, pipelineStatisticsLayout, blasSizes, tlasSizes, accelerationStructureRequirements |
These are read-only queries that touch no mutable state, so loader threads can plan sizes and layouts. |
You can name a pipeline or work graph when you create it via the name field of its descriptor
(names at creation), so the creating thread never needs to call
setName.
Every other call must run on the device thread. That includes recording and submission,
creating and destroying resources, writing descriptors, destroying pipelines and work graphs,
and setName.
// Worker threads build pipelines while the device thread records frames.
std::vector<std::future<RI::Pipeline>> jobs;
for (const Kernel &kernel : kernels) {
jobs.push_back(std::async(std::launch::async, [&device, &kernel] {
RI::Helpers::ShaderCompiler compiler = RI::Helpers::createShaderCompiler(); // one per thread
const RI::Helpers::CompiledShader shader = RI::Helpers::compileShader(compiler, kernel.source);
RI::Helpers::destroyShaderCompiler(compiler);
return RI::createComputePipeline(device, {.shader = shader.bytecode, .name = kernel.name});
}));
}
The helpers are not thread-safe either. Use one
ShaderCompiler per thread; several compilers may exist at
once so worker threads can compile shaders in parallel. The memory allocator, transfers,
frame ring, and profiler call device-thread RI functions, so they must run on the device
thread; each slot allocator only calls isReached, though it is not thread-safe either and
must be used by only one thread at a time.
What is checked¶
In validation builds, every call outside the any-thread list checks that it runs on the device thread. Calling one from any other thread triggers a programmer error that names the call and both threads:
[EquineRI] programmer error: createCommandList: called from thread 140213, but the device thread
is 140207 (the thread that called createDevice). Only the any-thread list (...) may run on
other threads (P106)
Release builds perform no thread check and have no overhead.
What stays out, for now¶
- Recording or submitting on several threads. Research frames record few commands, and RI fills some caches while recording.
- Creating or destroying resources on a loader thread. Texture creation submits RI's own internal commands on the queue, and Vulkan descriptor writes need a lock per heap.
- Handing the device thread to another thread. A device created on one thread and driven from another would need a hand-over call; none exists yet.