Threading Behavior
vk-graph is intended to provide scalable performance when used on multiple host threads.
Resources are externally synchronized, and mutable graph-building APIs such as Graph::begin_cmd
require exclusive access to the Graph itself.
API docs: Submission,
RecordedSubmission,
Submission::queue_submit,
Submission::record_resource,
Submission::record_resource_dependencies,
RecordedSubmission::queue_submit,
CommandBuffer::has_executed.
More precisely, vk-graph stores the most recent access type of each subresource of a resource. As
commands are submitted to the Vulkan implementation queue, the internal state of these resources is
updated.
Resource state is updated during the following function calls:
Submission::queue_submitSubmission::record_resourceSubmission::record_resource_dependenciesRecordedSubmission::queue_submit
Caution
Do not call any
Submissionrecording or queue function that accesses buffers, images, or acceleration structures currently being submitted on other threads.
Execution
The provided Submission recording and queue functions are designed to support a typical
frame-presentation workflow:
- Queue all commands the presented frame depends on
- Acquire the presentation image, usually through
Graphchain - Queue frame commands that write the presentation image
- Present the frame
- Submit any final unrelated commands
Safe Patterns
Resources (buffers, images, or acceleration structures) are the only mutable types which require any
thread safety notes. All other types provided by vk-graph are immutable data structures or Vulkan
handle smart pointers.
For example, there is no race condition or thread contention caused by using the same pipeline on two threads.1 In fact, there is no runtime overhead at all from this.
Additionally, it is safe to build Graph instances, bind resources, record command buffers, and
call Graph::finalize at any time on any thread, as long as each Graph instance is not
mutably shared across threads at the same time.
These patterns are safe:
- Build
GraphandSendto another thread for submission - Build
GraphandDropit without submission Sendresources to other threads or share asArc<T>Clonedevices or pipelines andSendthem to other threads
Risky Patterns
Host-mappable buffers require extra understanding to use properly.
The contents of a buffer are undefined from the time of submission until the returned Fence is
signaled. Use Fence::status or Fence::wait before reading or writing host-mapped memory touched
by that submission.
-
The internal implementation of
GraphicsPipelinedoes do a bit of caching in order to improve performance, however this behavior should not generate issues with any reasonable workload. ↩