When are semaphores in Vulkan used to insert dependency between a queue operation and the host?

Viewed 885

The spec says about VkSemaphore:

Semaphores are a synchronization primitive that can be used to insert a dependency between queue operations or between a queue operation and the host.

In what situation would you use a semaphore to place a dependency between a queue operation and the host? I have only ever seen VkSemaphore used to place dependencies between queue operations, and fences to synchronize between queues and the host.

1 Answers

Fences are used to signal when a queue operation has completed up to a point. The communication is one-way: from the GPU to the host.

Timeline semaphores are a bit different. They represent a continually increasing counter value. The counter gets bumped when a timeline semaphore is "signaled", and wait operations can wait for the counter to reach a certain value.

Because timeline semaphores are just counters, it is entirely possible for the host to "signal" one and/or wait for one to reach a given counter value. Both of these are useful, as fences represent very coarse-grained operations: an entire queue submission and the like. By contrast, semaphores are attached to individual batches within a single submit.

Note that this is only possible for timeline semaphores (which is why you probably haven't seen it before; timeline semaphores are a Vulkan 1.2 feature). The host can't interact with binary semaphores directly, though it may be possible to extract an implementation-specific handle from them and interact with them that way.

As for the uses of it, well, you can replace fences and many uses of events with just timeline semaphores. Semaphores are more fine-grained than fences, and you don't have the bother of resetting them and cycling through multiple fence objects. If you want to know if you've executed past frame X, you just ask the timeline semaphore if its counter has advanced beyond the counter value for frame X.

Events are still something you need, as semaphore signaling and wait operations can only go between batches. But many events used for host-to-GPU transfer operations can be made into timeline semaphores if the GPU wait operation can be put into the start of a batch. So once again, you reduce the amount of object resetting and juggling you have to do.

Related