Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
SubmitScheduler.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_RENDERER_BACKEND_VULKAN_SUBMITSCHEDULER
19#define INCLUDED_RENDERER_BACKEND_VULKAN_SUBMITSCHEDULER
20
22
23#include <array>
24#include <cstdint>
25#include <glad/vulkan.h>
26#include <memory>
27#include <queue>
28#include <vector>
29
30namespace Renderer::Backend::Vulkan { class CRingCommandContext; }
31
32namespace Renderer
33{
34
35namespace Backend
36{
37
38namespace Vulkan
39{
40
41/**
42 * A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages
43 * properly.
44 */
46{
47public:
48 using SubmitHandle = uint32_t;
49 static constexpr SubmitHandle INVALID_SUBMIT_HANDLE = 0;
50
51 static std::unique_ptr<CSubmitScheduler> Create(
52 CDevice* device, VkQueue queue);
54
55 SubmitHandle Submit(VkCommandBuffer commandBuffer);
56
57 void WaitUntilFree(const SubmitHandle handle);
58
59 uint32_t GetFrameID() const { return m_FrameID; }
60
62
63 /**
64 * It's a caller responsibility to guarantee a semaphore lifespan.
65 */
66 void EnqueueWaitOnNextSubmit(VkSemaphore semaphore, const VkPipelineStageFlags stageMask);
67 void EnqueueSignalOnNextSubmit(VkSemaphore semaphore);
68
69private:
70 CSubmitScheduler(CDevice* device, VkQueue queue);
71
72 CDevice* m_Device = nullptr;
73 VkQueue m_Queue = VK_NULL_HANDLE;
74
75 struct Fence
76 {
77 VkFence value = VK_NULL_HANDLE;
79 bool inUse = false;
80 };
81 std::vector<Fence> m_Fences;
82 uint32_t m_FenceIndex = 0;
83
84 // We assume that we won't run so long that the frame ID will overflow.
85 uint32_t m_FrameID = 0;
88 {
90 uint32_t fenceIndex;
91 };
92 std::queue<SubmittedHandle> m_SubmittedHandles;
93
94 std::vector<VkSemaphore> m_NextWaitSemaphores;
95 std::vector<VkPipelineStageFlags> m_NextWaitDstStageMasks;
96 std::vector<VkSemaphore> m_NextSubmitSignalSemaphores;
97
98 std::vector<VkCommandBuffer> m_SubmittedCommandBuffers;
99};
100
101} // namespace Vulkan
102
103} // namespace Backend
104
105} // namespace Renderer
106
107#endif // INCLUDED_RENDERER_BACKEND_VULKAN_SUBMITSCHEDULER
Definition: Device.h:67
A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages properly.
Definition: SubmitScheduler.h:46
void EnqueueWaitOnNextSubmit(VkSemaphore semaphore, const VkPipelineStageFlags stageMask)
It's a caller responsibility to guarantee a semaphore lifespan.
Definition: SubmitScheduler.cpp:142
void EnqueueSignalOnNextSubmit(VkSemaphore semaphore)
Definition: SubmitScheduler.cpp:149
std::vector< VkCommandBuffer > m_SubmittedCommandBuffers
Definition: SubmitScheduler.h:98
SubmitHandle Flush()
Definition: SubmitScheduler.cpp:101
uint32_t GetFrameID() const
Definition: SubmitScheduler.h:59
VkQueue m_Queue
Definition: SubmitScheduler.h:73
uint32_t m_FenceIndex
Definition: SubmitScheduler.h:82
SubmitHandle m_CurrentHandle
Definition: SubmitScheduler.h:86
SubmitHandle Submit(VkCommandBuffer commandBuffer)
Definition: SubmitScheduler.cpp:76
std::vector< VkSemaphore > m_NextSubmitSignalSemaphores
Definition: SubmitScheduler.h:96
static constexpr SubmitHandle INVALID_SUBMIT_HANDLE
Definition: SubmitScheduler.h:49
std::vector< VkSemaphore > m_NextWaitSemaphores
Definition: SubmitScheduler.h:94
void WaitUntilFree(const SubmitHandle handle)
Definition: SubmitScheduler.cpp:82
CDevice * m_Device
Definition: SubmitScheduler.h:72
uint32_t m_FrameID
Definition: SubmitScheduler.h:85
~CSubmitScheduler()
Definition: SubmitScheduler.cpp:67
CSubmitScheduler(CDevice *device, VkQueue queue)
Definition: SubmitScheduler.cpp:62
std::vector< Fence > m_Fences
Definition: SubmitScheduler.h:81
static std::unique_ptr< CSubmitScheduler > Create(CDevice *device, VkQueue queue)
Definition: SubmitScheduler.cpp:41
uint32_t SubmitHandle
Definition: SubmitScheduler.h:48
std::queue< SubmittedHandle > m_SubmittedHandles
Definition: SubmitScheduler.h:92
std::vector< VkPipelineStageFlags > m_NextWaitDstStageMasks
Definition: SubmitScheduler.h:95
Definition: Buffer.cpp:37
Backend
Definition: Backend.h:28
Definition: Canvas2D.h:30
VkFence value
Definition: SubmitScheduler.h:77
SubmitHandle lastUsedHandle
Definition: SubmitScheduler.h:78
bool inUse
Definition: SubmitScheduler.h:79
SubmitHandle value
Definition: SubmitScheduler.h:89
uint32_t fenceIndex
Definition: SubmitScheduler.h:90