Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
SubmitScheduler.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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 <glad/vulkan.h>
24#include <memory>
25#include <queue>
26#include <utility>
27#include <vector>
28
29namespace Renderer
30{
31
32namespace Backend
33{
34
35namespace Vulkan
36{
37
38class CDevice;
39class CRingCommandContext;
40class CSwapChain;
41
42/**
43 * A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages
44 * properly.
45 */
47{
48public:
50 static constexpr SubmitHandle INVALID_SUBMIT_HANDLE = 0;
51
52 CSubmitScheduler(CDevice* device, const uint32_t queueFamilyIndex, VkQueue queue);
54
55 bool AcquireNextImage(CSwapChain& swapChain);
56
57 void Present(CSwapChain& swapChain);
58
59 SubmitHandle Submit(VkCommandBuffer commandBuffer);
60
61 void WaitUntilFree(const SubmitHandle handle);
62
63 uint32_t GetFrameID() const { return m_FrameID; }
64
65 void Flush();
66
67private:
68 CDevice* m_Device = nullptr;
69 VkQueue m_Queue = VK_NULL_HANDLE;
70
71 struct Fence
72 {
73 VkFence value = VK_NULL_HANDLE;
75 bool inUse = false;
76 };
77 std::vector<Fence> m_Fences;
79
80 // We assume that we won't run so long that the frame ID will overflow.
84 {
87 };
88 std::queue<SubmittedHandle> m_SubmittedHandles;
89
90 // We can't reuse frame data immediately after present because it might
91 // still be processing on GPU.
93 {
94 // We need to wait for the image on GPU to draw to it.
95 VkSemaphore acquireImageSemaphore = VK_NULL_HANDLE;
96 // We need to present only after all submit work is done.
97 VkSemaphore submitDone = VK_NULL_HANDLE;
98 };
99 std::array<FrameObject, NUMBER_OF_FRAMES_IN_FLIGHT> m_FrameObjects;
100
101 VkSemaphore m_NextWaitSemaphore = VK_NULL_HANDLE;
102 VkPipelineStageFlags m_NextWaitDstStageMask = 0;
103 VkSemaphore m_NextSubmitSignalSemaphore = VK_NULL_HANDLE;
104
105 std::vector<VkCommandBuffer> m_SubmittedCommandBuffers;
106
107 std::unique_ptr<CRingCommandContext> m_AcquireCommandContext;
108 std::unique_ptr<CRingCommandContext> m_PresentCommandContext;
109
113};
114
115} // namespace Vulkan
116
117} // namespace Backend
118
119} // namespace Renderer
120
121#endif // INCLUDED_RENDERER_BACKEND_VULKAN_SUBMITSCHEDULER
Definition: Device.h:60
A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages properly.
Definition: SubmitScheduler.h:47
bool AcquireNextImage(CSwapChain &swapChain)
Definition: SubmitScheduler.cpp:94
std::vector< VkCommandBuffer > m_SubmittedCommandBuffers
Definition: SubmitScheduler.h:105
VkSemaphore m_NextSubmitSignalSemaphore
Definition: SubmitScheduler.h:103
CSubmitScheduler(CDevice *device, const uint32_t queueFamilyIndex, VkQueue queue)
Definition: SubmitScheduler.cpp:37
bool m_DebugWaitIdleBeforePresent
Definition: SubmitScheduler.h:111
uint32_t GetFrameID() const
Definition: SubmitScheduler.h:63
VkSemaphore m_NextWaitSemaphore
Definition: SubmitScheduler.h:101
VkQueue m_Queue
Definition: SubmitScheduler.h:69
void Present(CSwapChain &swapChain)
Definition: SubmitScheduler.cpp:110
std::unique_ptr< CRingCommandContext > m_AcquireCommandContext
Definition: SubmitScheduler.h:107
uint32_t m_FenceIndex
Definition: SubmitScheduler.h:78
SubmitHandle m_CurrentHandle
Definition: SubmitScheduler.h:82
SubmitHandle Submit(VkCommandBuffer commandBuffer)
Definition: SubmitScheduler.cpp:127
VkPipelineStageFlags m_NextWaitDstStageMask
Definition: SubmitScheduler.h:102
void Flush()
Definition: SubmitScheduler.cpp:152
static constexpr SubmitHandle INVALID_SUBMIT_HANDLE
Definition: SubmitScheduler.h:50
std::array< FrameObject, NUMBER_OF_FRAMES_IN_FLIGHT > m_FrameObjects
Definition: SubmitScheduler.h:99
void WaitUntilFree(const SubmitHandle handle)
Definition: SubmitScheduler.cpp:133
CDevice * m_Device
Definition: SubmitScheduler.h:68
uint32_t m_FrameID
Definition: SubmitScheduler.h:81
bool m_DebugWaitIdleBeforeAcquire
Definition: SubmitScheduler.h:110
~CSubmitScheduler()
Definition: SubmitScheduler.cpp:76
bool m_DebugWaitIdleAfterPresent
Definition: SubmitScheduler.h:112
std::vector< Fence > m_Fences
Definition: SubmitScheduler.h:77
uint32_t SubmitHandle
Definition: SubmitScheduler.h:49
std::queue< SubmittedHandle > m_SubmittedHandles
Definition: SubmitScheduler.h:88
std::unique_ptr< CRingCommandContext > m_PresentCommandContext
Definition: SubmitScheduler.h:108
Definition: SwapChain.h:44
Backend
Definition: Backend.h:28
Definition: VideoMode.h:29
VkFence value
Definition: SubmitScheduler.h:73
SubmitHandle lastUsedHandle
Definition: SubmitScheduler.h:74
bool inUse
Definition: SubmitScheduler.h:75
VkSemaphore submitDone
Definition: SubmitScheduler.h:97
VkSemaphore acquireImageSemaphore
Definition: SubmitScheduler.h:95
SubmitHandle value
Definition: SubmitScheduler.h:85
uint32_t fenceIndex
Definition: SubmitScheduler.h:86
unsigned int uint32_t
Definition: wposix_types.h:53