Skip to content

KDGpu::Fence

Module: Public API

Synchronization primitive for CPU-GPU synchronization. More...

#include <KDGpu/fence.h>

Public Functions

Name
Fence()
~Fence()
Fence(Fence && other)
Fence & operator=(Fence && other)
Fence(const Fence & ) =delete
Fence & operator=(const Fence & ) =delete
const Handle< Fence_t > & handle() const
bool isValid() const
operator Handle< Fence_t >() const
void reset()
void wait()
FenceStatus status() const
HandleOrFD externalFenceHandle() const

Friends

Name
class Device
class Queue
KDGPU_EXPORT bool operator==(const Fence & a, const Fence & b)

Detailed Description

1
class KDGpu::Fence;

Synchronization primitive for CPU-GPU synchronization.

Vulkan equivalent:VkFence

Fence is used to synchronize CPU and GPU execution. The GPU signals a fence when it completes work, and the CPU can wait for that signal. This is essential for knowing when GPU operations have finished.

Key features:

  • CPU-side waiting for GPU completion
  • Signaling when GPU work is done
  • Resetting for reuse
  • Checking status without blocking

Lifetime: Fences are created by Device and should remain valid while GPU work that signals them is in flight. They use RAII and clean up automatically.

Usage

Basic fence usage:

1
    KDGpu::Fence fence = device.createFence();

Filename: kdgpu_doc_snippets.cpp

Submit and wait:

1
2
3
4
5
6
    queue.submit(KDGpu::SubmitOptions{
            .commandBuffers = { commandBuffer },
            .signalFence = fence,
    });

    fence.wait(); // Block until GPU work completes

Filename: kdgpu_doc_snippets.cpp

Checking status:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    queue.submit(KDGpu::SubmitOptions{
            .commandBuffers = { commandBuffer },
            .signalFence = fence,
    });

    // Do other work while GPU executes...

    if (fence.status() == KDGpu::FenceStatus::Signalled) {
        std::cout << "GPU work complete" << std::endl;
    } else {
        std::cout << "GPU still working" << std::endl;
    }

Filename: kdgpu_doc_snippets.cpp

CPU-GPU synchronization:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    // Example showing pattern for synchronizing CPU and GPU per frame
    KDGpu::Fence frameFence = device.createFence(KDGpu::FenceOptions{
            .createSignalled = true, // allowing us to wait initially
    });

    auto renderFrame = [&]() {
        // 1. Wait for previous frame to complete
        frameFence.wait();

        // 2. Update resources
        // ... upload new data ...

        // 3. Record and submit commands
        KDGpu::CommandRecorder recorder = device.createCommandRecorder();
        // ... record rendering ...
        KDGpu::CommandBuffer cmdBuffer = recorder.finish();

        frameFence.reset(); // Unsignal fence
        queue.submit(KDGpu::SubmitOptions{
                .commandBuffers = { cmdBuffer },
                .signalFence = frameFence,
        });
    };

    while (true) {
        renderFrame();
    }

Filename: kdgpu_doc_snippets.cpp

Resetting fences:

1
2
3
4
5
6
    fence.wait(); // Wait for signal
    fence.reset(); // Return to unsignaled state
    queue.submit(KDGpu::SubmitOptions{
            .commandBuffers = { commandBuffer },
            .signalFence = fence, // Reuse fence
    });

Filename: kdgpu_doc_snippets.cpp

Vulkan mapping:

See also:

FenceOptions, Queue, GpuSemaphore, Device

KDGpu API Overview

KDGpu to Vulkan API Mapping

Public Functions Documentation

function Fence

1
Fence()

function ~Fence

1
~Fence()

function Fence

1
2
3
Fence(
    Fence && other
)

function operator=

1
2
3
Fence & operator=(
    Fence && other
)

function Fence

1
2
3
Fence(
    const Fence & 
) =delete

function operator=

1
2
3
Fence & operator=(
    const Fence & 
) =delete

function handle

1
inline const Handle< Fence_t > & handle() const

function isValid

1
inline bool isValid() const

function operator Handle< Fence_t >

1
inline operator Handle< Fence_t >() const

function reset

1
void reset()

function wait

1
void wait()

function status

1
FenceStatus status() const

function externalFenceHandle

1
HandleOrFD externalFenceHandle() const

Friends

friend Device

1
2
3
friend class Device(
    Device 
);

friend Queue

1
2
3
friend class Queue(
    Queue 
);

friend operator==

1
2
3
4
5
friend KDGPU_EXPORT bool operator==(
    const Fence & a,

    const Fence & b
);

Updated on 2026-03-31 at 00:02:07 +0000