Skip to content
systemcoding.dev
GPU

Create a fence signalled with VK_FENCE_CREATE_SIGNALED_BIT

Create a fence already signalled with VK_FENCE_CREATE_SIGNALED_BIT so the first vkWaitForFences in a frame loop returns instead of hanging.

1 min read
Summarize in AI

A frame loop waits on a fence at the top, then resets it, then submits work that signals it again. On the very first frame nothing has signalled it yet, so a fence created in the default state makes the first wait hang forever. Create it already signalled.

fence.cc
VkFenceCreateInfo fi = {
    .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
    .flags = VK_FENCE_CREATE_SIGNALED_BIT,
};
for (int i = 0; i < FRAMES; i++)
    vkCreateFence(device, &fi, NULL, &in_flight[i]);

This is the one-line fix for the most common first-frame freeze in a new Vulkan renderer. With the flag set, the first vkWaitForFences returns at once, vkResetFences puts the fence in the unsignalled state, and the submit signals it when the GPU finishes.

A fence is the CPU-visible sync object. It tells the host that a submission has completed, and it does nothing for ordering between GPU commands, which is the job of semaphores and barriers. Keep one per frame in flight, and reuse it each lap.

Resetting a fence that a pending submit will signal is invalid, so you always wait before you reset. Do not create a fence per frame in the loop. That allocates driver memory every frame, and vkCreateFence is not free.

Taggedcvulkan
Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.