Pyrogenesis  trunk
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 
29 namespace Renderer
30 {
31 
32 namespace Backend
33 {
34 
35 namespace Vulkan
36 {
37 
38 class CDevice;
39 class CRingCommandContext;
40 class CSwapChain;
41 
42 /**
43  * A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages
44  * properly.
45  */
47 {
48 public:
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 private:
66  void Flush();
67 
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.
82  SubmitHandle m_CurrentHandle = INVALID_SUBMIT_HANDLE + 1;
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.
92  struct FrameObject
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 };
110 
111 } // namespace Vulkan
112 
113 } // namespace Backend
114 
115 } // namespace Renderer
116 
117 #endif // INCLUDED_RENDERER_BACKEND_VULKAN_SUBMITSCHEDULER
void Flush()
Definition: SubmitScheduler.cpp:135
bool AcquireNextImage(CSwapChain &swapChain)
Definition: SubmitScheduler.cpp:87
std::unique_ptr< CRingCommandContext > m_AcquireCommandContext
Definition: SubmitScheduler.h:107
VkPipelineStageFlags m_NextWaitDstStageMask
Definition: SubmitScheduler.h:102
A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages properly.
Definition: SubmitScheduler.h:46
VkQueue m_Queue
Definition: SubmitScheduler.h:69
Definition: SwapChain.h:43
Definition: Device.h:59
std::queue< SubmittedHandle > m_SubmittedHandles
Definition: SubmitScheduler.h:88
~CSubmitScheduler()
Definition: SubmitScheduler.cpp:69
void Present(CSwapChain &swapChain)
Definition: SubmitScheduler.cpp:100
uint32_t fenceIndex
Definition: SubmitScheduler.h:86
VkSemaphore m_NextWaitSemaphore
Definition: SubmitScheduler.h:101
VkFence value
Definition: SubmitScheduler.h:73
void WaitUntilFree(const SubmitHandle handle)
Definition: SubmitScheduler.cpp:116
VkSemaphore m_NextSubmitSignalSemaphore
Definition: SubmitScheduler.h:103
uint32_t GetFrameID() const
Definition: SubmitScheduler.h:63
SubmitHandle m_CurrentHandle
Definition: SubmitScheduler.h:82
Backend
Definition: Backend.h:27
SubmitHandle value
Definition: SubmitScheduler.h:85
uint32_t m_FenceIndex
Definition: SubmitScheduler.h:78
bool inUse
Definition: SubmitScheduler.h:75
unsigned int uint32_t
Definition: wposix_types.h:53
Definition: VideoMode.h:28
std::array< FrameObject, NUMBER_OF_FRAMES_IN_FLIGHT > m_FrameObjects
Definition: SubmitScheduler.h:99
uint32_t m_FrameID
Definition: SubmitScheduler.h:81
SubmitHandle lastUsedHandle
Definition: SubmitScheduler.h:74
CSubmitScheduler(CDevice *device, const uint32_t queueFamilyIndex, VkQueue queue)
Definition: SubmitScheduler.cpp:36
CDevice * m_Device
Definition: SubmitScheduler.h:68
std::vector< Fence > m_Fences
Definition: SubmitScheduler.h:77
std::vector< VkCommandBuffer > m_SubmittedCommandBuffers
Definition: SubmitScheduler.h:105
uint32_t SubmitHandle
Definition: SubmitScheduler.h:49
SubmitHandle Submit(VkCommandBuffer commandBuffer)
Definition: SubmitScheduler.cpp:110
std::unique_ptr< CRingCommandContext > m_PresentCommandContext
Definition: SubmitScheduler.h:108
static constexpr SubmitHandle INVALID_SUBMIT_HANDLE
Definition: SubmitScheduler.h:50