Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
TimelineSynchronizer.h
Go to the documentation of this file.
1#pragma once
2
3#include "PipelineStages.h"
4
5#include <optional>
6#include <vulkan/vulkan_core.h>
7
8namespace EngineCore
9{
10
12
29{
30public:
37 explicit TimelineSynchronizer(ApplicationContext* context, uint32_t framesInFlight);
38
40
41 // Non-copyable
44
45 // Movable
48
56 [[nodiscard]] uint64_t getStageValue(uint64_t frameNumber, PipelineStage stage) const
57 {
58 // +1 offset ensures frame 0's first signal (value 1) is greater than
59 // the timeline semaphore's initial value (0), as required by Vulkan
60 return frameNumber * PIPELINE_STAGE_COUNT + static_cast<uint32_t>(stage) + 1;
61 }
62
71 [[nodiscard]] std::optional<uint64_t> getWaitValue(
72 uint64_t currentFrame,
74 uint32_t framesBack = 1) const
75 {
76 if (currentFrame < framesBack)
77 {
78 return std::nullopt;
79 }
80 return getStageValue(currentFrame - framesBack, waitForStage);
81 }
82
93 [[nodiscard]] std::optional<uint64_t> getResourceReuseWaitValue(
94 uint64_t currentFrame,
96 {
97 if (currentFrame < framesInFlight_)
98 {
99 return std::nullopt;
100 }
101 return getStageValue(currentFrame - framesInFlight_, waitForStage);
102 }
103
108 [[nodiscard]] uint64_t getCurrentValue() const;
109
116 void waitForValue(uint64_t value, uint64_t timeout = UINT64_MAX) const;
117
125 void waitForStage(uint64_t frameNumber, PipelineStage stage,
126 uint64_t timeout = UINT64_MAX) const
127 {
128 waitForValue(getStageValue(frameNumber, stage), timeout);
129 }
130
140 void signalValue(uint64_t value) const;
141
146 [[nodiscard]] VkSemaphore getSemaphore() const { return timelineSemaphore_; }
147
152 [[nodiscard]] uint32_t getFramesInFlight() const { return framesInFlight_; }
153
154private:
156 VkSemaphore timelineSemaphore_ = VK_NULL_HANDLE;
157 uint32_t framesInFlight_ = 0;
158};
159
160} // namespace EngineCore
The application context is the core class which stores the basic openxr and vulkan objects.
uint64_t getStageValue(uint64_t frameNumber, PipelineStage stage) const
Gets the timeline value for a specific stage at a specific frame.
void signalValue(uint64_t value) const
CPU-side signal of the timeline semaphore.
uint32_t getFramesInFlight() const
Gets the number of frames in flight.
TimelineSynchronizer(ApplicationContext *context, uint32_t framesInFlight)
Constructs a timeline synchronizer.
VkSemaphore getSemaphore() const
Gets the underlying Vulkan timeline semaphore.
TimelineSynchronizer & operator=(const TimelineSynchronizer &)=delete
TimelineSynchronizer(const TimelineSynchronizer &)=delete
uint64_t getCurrentValue() const
Gets the current GPU timeline semaphore value.
void waitForStage(uint64_t frameNumber, PipelineStage stage, uint64_t timeout=UINT64_MAX) const
CPU-side wait for a specific stage to complete.
std::optional< uint64_t > getWaitValue(uint64_t currentFrame, PipelineStage waitForStage, uint32_t framesBack=1) const
Gets the value to wait for from a previous frame's stage.
std::optional< uint64_t > getResourceReuseWaitValue(uint64_t currentFrame, PipelineStage waitForStage) const
Gets the wait value for resource reuse (frames-in-flight synchronization)
void waitForValue(uint64_t value, uint64_t timeout=UINT64_MAX) const
CPU-side wait for a specific timeline value.
Log category system implementation.
constexpr uint32_t PIPELINE_STAGE_COUNT
PipelineStage
Extensible pipeline stages for timeline semaphore synchronization.