Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Device.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_DEVICE
19#define INCLUDED_RENDERER_BACKEND_VULKAN_DEVICE
20
21#include "ps/CStr.h"
30
31#include <cstddef>
32#include <cstdint>
33#include <glad/vulkan.h>
34#include <js/TypeDecls.h>
35#include <limits>
36#include <memory>
37#include <queue>
38#include <span>
39#include <string>
40#include <utility>
41#include <vector>
42
43namespace Renderer::Backend { enum class Format; }
44namespace Renderer::Backend::Vulkan { class CBuffer; }
45namespace Renderer::Backend::Vulkan { class CDescriptorManager; }
46namespace Renderer::Backend::Vulkan { class CRenderPassManager; }
47namespace Renderer::Backend::Vulkan { class CRingCommandContext; }
48namespace Renderer::Backend::Vulkan { class CSamplerManager; }
49namespace Renderer::Backend::Vulkan { class CSubmitScheduler; }
50namespace Renderer::Backend::Vulkan { class CSwapChain; }
51namespace Renderer::Backend::Vulkan { class CTexture; }
52
53typedef struct SDL_Window SDL_Window;
54
55namespace Renderer
56{
57
58namespace Backend
59{
60
61namespace Vulkan
62{
63
64static constexpr size_t NUMBER_OF_FRAMES_IN_FLIGHT = 3;
65
66class CDevice final : public IDevice
67{
68public:
69 /**
70 * Creates the Vulkan device.
71 */
72 static std::unique_ptr<CDevice> Create(SDL_Window* window);
73
74 ~CDevice() override;
75
76 Backend GetBackend() const override { return Backend::VULKAN; }
77
78 const std::string& GetName() const override { return m_Name; }
79 const std::string& GetVersion() const override { return m_Version; }
80 const std::string& GetDriverInformation() const override { return m_DriverInformation; }
81 const std::vector<std::string>& GetExtensions() const override { return m_Extensions; }
82
83 void Report(const Script::Request& rq, JS::HandleValue settings) override;
84
85 std::unique_ptr<IDeviceCommandContext> CreateCommandContext() override;
86
87 std::unique_ptr<ISwapChain> CreateSwapChain(
88 const char* name, SDL_Window* window,
89 int surfaceDrawableWidth, int surfaceDrawableHeight,
90 const bool vsync, std::unique_ptr<ISwapChain> oldSwapChain) override;
91
92 void WaitUntilIdle() override;
93
94 std::unique_ptr<IGraphicsPipelineState> CreateGraphicsPipelineState(
95 const SGraphicsPipelineStateDesc& pipelineStateDesc) override;
96
97 std::unique_ptr<IComputePipelineState> CreateComputePipelineState(
98 const SComputePipelineStateDesc& pipelineStateDesc) override;
99
100 std::unique_ptr<IVertexInputLayout> CreateVertexInputLayout(
101 const std::span<const SVertexAttributeFormat> attributes) override;
102
103 std::unique_ptr<ITexture> CreateTexture(
104 const char* name, const ITexture::Type type, const uint32_t usage,
105 const Format format, const uint32_t width, const uint32_t height,
106 const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount) override;
107
108 std::unique_ptr<ITexture> CreateTexture2D(
109 const char* name, const uint32_t usage,
110 const Format format, const uint32_t width, const uint32_t height,
111 const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount = 1, const uint32_t sampleCount = 1) override;
112
113 std::unique_ptr<IFramebuffer> CreateFramebuffer(
114 const char* name, SColorAttachment* colorAttachment,
115 SDepthStencilAttachment* depthStencilAttachment) override;
116
117 std::unique_ptr<IBuffer> CreateBuffer(
118 const char* name, const IBuffer::Type type, const uint32_t size, const uint32_t usage) override;
119
120 std::unique_ptr<CBuffer> CreateCBuffer(
121 const char* name, const IBuffer::Type type, const uint32_t size, const uint32_t usage);
122
123 std::unique_ptr<IShaderProgram> CreateShaderProgram(
124 const CStr& name, const CShaderDefines& defines) override;
125
126 bool IsTextureFormatSupported(const Format format) const override;
127
128 bool IsFramebufferFormatSupported(const Format format) const override;
129
131 const uint32_t usage, const bool depth, const bool stencil) const override;
132
133 bool IsQueryResultAvailable(const uint32_t handle) const override;
134
135 uint32_t AllocateQuery() override;
136
137 void FreeQuery(const uint32_t handle) override;
138
139 uint64_t GetQueryResult(const uint32_t handle) override;
140
141 const Capabilities& GetCapabilities() const override { return m_Capabilities; }
142
143 void CollectStatistics(StatisticsVector& statistics) const override;
144
145 VkDevice GetVkDevice() const { return m_Device; }
146
147 VmaAllocator GetVMAAllocator() { return m_VMAAllocator; }
148
150 VkCommandBuffer commandBuffer, const uint32_t handle, const bool isScopeBegin);
151
153 VkObjectType type, const void* handle, const VmaAllocation allocation)
154 {
155 ScheduleObjectToDestroy(type, reinterpret_cast<uint64_t>(handle), allocation);
156 }
157
159 VkObjectType type, const uint64_t handle, const VmaAllocation allocation);
160
162
164
165 void OnPresent();
166
168
169 void SetObjectName(VkObjectType type, const void* handle, const char* name)
170 {
171 SetObjectName(type, reinterpret_cast<uint64_t>(handle), name);
172 }
173
174 void SetObjectName(VkObjectType type, const uint64_t handle, const char* name);
175
176 std::unique_ptr<CRingCommandContext> CreateRingCommandContext(const size_t size);
177
179
181
183
185
187
188 uint32_t GetFrameID() const { return m_FrameID; }
189
190private:
192
193 void ProcessObjectToDestroyQueue(const bool ignoreFrameID = false);
194 void ProcessDeviceObjectToDestroyQueue(const bool ignoreFrameID = false);
195
196 bool IsFormatSupportedForUsage(const Format format, const uint32_t usage) const;
197
198 std::string m_Name;
199 std::string m_Version;
200 std::string m_VendorID;
202 std::vector<std::string> m_Extensions;
203 std::vector<std::string> m_InstanceExtensions;
204 std::vector<std::string> m_ValidationLayers;
205
207 std::vector<SAvailablePhysicalDevice> m_AvailablePhysicalDevices;
208
210
211 VkInstance m_Instance = VK_NULL_HANDLE;
212
213 VkDebugUtilsMessengerEXT m_DebugMessenger = VK_NULL_HANDLE;
214
216 VkSurfaceKHR m_Surface = VK_NULL_HANDLE;
217 VkDevice m_Device = VK_NULL_HANDLE;
218 VmaAllocator m_VMAAllocator = VK_NULL_HANDLE;
219 VkQueue m_GraphicsQueue = VK_NULL_HANDLE;
220 uint32_t m_GraphicsQueueFamilyIndex = std::numeric_limits<uint32_t>::max();
221
222 VkQueryPool m_QueryPool{VK_NULL_HANDLE};
223 struct Query
224 {
226 bool occupied{};
227 bool submitted{};
228 };
229 std::vector<Query> m_Queries;
230
231 uint32_t m_FrameID = 0;
232
234 {
235 uint32_t frameID;
236 VkObjectType type;
237 uint64_t handle;
238 VmaAllocation allocation;
239 };
240 std::queue<ObjectToDestroy> m_ObjectToDestroyQueue;
241 std::queue<std::pair<uint32_t, DeviceObjectUID>> m_TextureToDestroyQueue;
242 std::queue<std::pair<uint32_t, DeviceObjectUID>> m_BufferToDestroyQueue;
243
244 std::unique_ptr<CRenderPassManager> m_RenderPassManager;
245 std::unique_ptr<CSamplerManager> m_SamplerManager;
246 std::unique_ptr<CDescriptorManager> m_DescriptorManager;
247 std::unique_ptr<CSubmitScheduler> m_SubmitScheduler;
248
250};
251
252} // namespace Vulkan
253
254} // namespace Backend
255
256} // namespace Renderer
257
258#endif // INCLUDED_RENDERER_BACKEND_VULKAN_DEVICE
struct SDL_Window SDL_Window
Definition: VideoMode.h:31
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:153
Represents a texture object.
Definition: TextureManager.h:271
Type
Definition: IBuffer.h:35
Definition: IDevice.h:64
std::vector< StatisticsItem > StatisticsVector
Definition: IDevice.h:228
Type
Definition: ITexture.h:36
Definition: DescriptorManager.h:51
Definition: Device.h:67
void SetObjectName(VkObjectType type, const void *handle, const char *name)
Definition: Device.h:169
void OnPresent()
Definition: Device.cpp:831
VmaAllocator m_VMAAllocator
Definition: Device.h:218
bool IsFramebufferFormatSupported(const Format format) const override
Definition: Device.cpp:880
std::unique_ptr< IShaderProgram > CreateShaderProgram(const CStr &name, const CShaderDefines &defines) override
Definition: Device.cpp:820
bool IsQueryResultAvailable(const uint32_t handle) const override
Definition: Device.cpp:944
std::unique_ptr< IVertexInputLayout > CreateVertexInputLayout(const std::span< const SVertexAttributeFormat > attributes) override
Creates a vertex input layout.
Definition: Device.cpp:774
VkDevice GetVkDevice() const
Definition: Device.h:145
uint32_t AllocateQuery() override
Definition: Device.cpp:924
void ScheduleBufferToDestroy(const DeviceObjectUID uid)
Definition: Device.cpp:1035
bool IsFormatSupportedForUsage(const Format format, const uint32_t usage) const
Definition: Device.cpp:994
std::unique_ptr< ISwapChain > CreateSwapChain(const char *name, SDL_Window *window, int surfaceDrawableWidth, int surfaceDrawableHeight, const bool vsync, std::unique_ptr< ISwapChain > oldSwapChain) override
To be able to present something on a window it needs to create swapchain which provides framebuffer t...
Definition: Device.cpp:735
const std::string & GetVersion() const override
Definition: Device.h:79
std::string m_DriverInformation
Definition: Device.h:201
Backend GetBackend() const override
Definition: Device.h:76
std::vector< std::string > m_InstanceExtensions
Definition: Device.h:203
CRenderPassManager & GetRenderPassManager()
Definition: Device.h:180
std::string m_Version
Definition: Device.h:199
VkQueue m_GraphicsQueue
Definition: Device.h:219
std::queue< std::pair< uint32_t, DeviceObjectUID > > m_TextureToDestroyQueue
Definition: Device.h:241
SDL_Window * m_Window
Definition: Device.h:215
uint64_t GetQueryResult(const uint32_t handle) override
After a call of the function the query result becomes invalid.
Definition: Device.cpp:957
const std::string & GetDriverInformation() const override
Definition: Device.h:80
CSamplerManager & GetSamplerManager()
Definition: Device.h:182
uint32_t m_GraphicsQueueFamilyIndex
Definition: Device.h:220
VkInstance m_Instance
Definition: Device.h:211
std::unique_ptr< IBuffer > CreateBuffer(const char *name, const IBuffer::Type type, const uint32_t size, const uint32_t usage) override
Definition: Device.cpp:808
std::unique_ptr< CSubmitScheduler > m_SubmitScheduler
Definition: Device.h:247
std::unique_ptr< ITexture > CreateTexture(const char *name, const ITexture::Type type, const uint32_t usage, const Format format, const uint32_t width, const uint32_t height, const Sampler::Desc &defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount) override
Definition: Device.cpp:780
std::string m_VendorID
Definition: Device.h:200
void OnSwapChainRecreation()
Definition: Device.cpp:839
void Report(const Script::Request &rq, JS::HandleValue settings) override
Definition: Device.cpp:706
std::unique_ptr< CRenderPassManager > m_RenderPassManager
Definition: Device.h:244
std::string m_Name
Definition: Device.h:198
SAvailablePhysicalDevice m_ChoosenDevice
Definition: Device.h:206
VkSurfaceKHR m_Surface
Definition: Device.h:216
const std::vector< std::string > & GetExtensions() const override
Definition: Device.h:81
void ScheduleTextureToDestroy(const DeviceObjectUID uid)
Definition: Device.cpp:1030
void InsertTimestampQuery(VkCommandBuffer commandBuffer, const uint32_t handle, const bool isScopeBegin)
Definition: Device.cpp:1013
std::unique_ptr< IFramebuffer > CreateFramebuffer(const char *name, SColorAttachment *colorAttachment, SDepthStencilAttachment *depthStencilAttachment) override
Definition: Device.cpp:800
std::queue< std::pair< uint32_t, DeviceObjectUID > > m_BufferToDestroyQueue
Definition: Device.h:242
~CDevice() override
Definition: Device.cpp:672
void FreeQuery(const uint32_t handle) override
Definition: Device.cpp:937
VmaAllocator GetVMAAllocator()
Definition: Device.h:147
static std::unique_ptr< CDevice > Create(SDL_Window *window)
Creates the Vulkan device.
Definition: Device.cpp:196
const SAvailablePhysicalDevice & GetChoosenPhysicalDevice() const
Definition: Device.h:178
void WaitUntilIdle() override
Waits until all submitted work (via DeviceCommandContext::Flush) to backend is completed and it's saf...
Definition: Device.cpp:752
std::vector< std::string > m_ValidationLayers
Definition: Device.h:204
std::vector< std::string > m_Extensions
Definition: Device.h:202
std::unique_ptr< IGraphicsPipelineState > CreateGraphicsPipelineState(const SGraphicsPipelineStateDesc &pipelineStateDesc) override
Creates a graphics pipeline state.
Definition: Device.cpp:762
DeviceObjectUID GenerateNextDeviceObjectUID()
Definition: Device.cpp:1128
uint32_t GetFrameID() const
Definition: Device.h:188
const std::string & GetName() const override
Definition: Device.h:78
void CollectStatistics(StatisticsVector &statistics) const override
Definition: Device.cpp:970
void ProcessDeviceObjectToDestroyQueue(const bool ignoreFrameID=false)
Definition: Device.cpp:1111
VkDebugUtilsMessengerEXT m_DebugMessenger
Definition: Device.h:213
DeviceObjectUID m_LastAvailableUID
Definition: Device.h:249
CDescriptorManager & GetDescriptorManager()
Definition: Device.h:184
std::vector< Query > m_Queries
Definition: Device.h:229
Format GetPreferredDepthStencilFormat(const uint32_t usage, const bool depth, const bool stencil) const override
Returns the most suitable format for the usage.
Definition: Device.cpp:890
std::unique_ptr< CDescriptorManager > m_DescriptorManager
Definition: Device.h:246
uint32_t m_FrameID
Definition: Device.h:231
VkQueryPool m_QueryPool
Definition: Device.h:222
void ProcessObjectToDestroyQueue(const bool ignoreFrameID=false)
Definition: Device.cpp:1058
std::unique_ptr< CSamplerManager > m_SamplerManager
Definition: Device.h:245
void ScheduleObjectToDestroy(VkObjectType type, const void *handle, const VmaAllocation allocation)
Definition: Device.h:152
Capabilities m_Capabilities
Definition: Device.h:209
std::queue< ObjectToDestroy > m_ObjectToDestroyQueue
Definition: Device.h:240
std::unique_ptr< IDeviceCommandContext > CreateCommandContext() override
Definition: Device.cpp:826
std::unique_ptr< CRingCommandContext > CreateRingCommandContext(const size_t size)
Definition: Device.cpp:1052
const Capabilities & GetCapabilities() const override
Definition: Device.h:141
VkDevice m_Device
Definition: Device.h:217
std::unique_ptr< CBuffer > CreateCBuffer(const char *name, const IBuffer::Type type, const uint32_t size, const uint32_t usage)
Definition: Device.cpp:814
bool IsTextureFormatSupported(const Format format) const override
Definition: Device.cpp:847
std::unique_ptr< ITexture > CreateTexture2D(const char *name, const uint32_t usage, const Format format, const uint32_t width, const uint32_t height, const Sampler::Desc &defaultSamplerDesc, const uint32_t MIPLevelCount=1, const uint32_t sampleCount=1) override
Definition: Device.cpp:790
std::vector< SAvailablePhysicalDevice > m_AvailablePhysicalDevices
Definition: Device.h:207
std::unique_ptr< IComputePipelineState > CreateComputePipelineState(const SComputePipelineStateDesc &pipelineStateDesc) override
Creates a compute pipeline state.
Definition: Device.cpp:768
A helper class to store unique render passes.
Definition: RenderPassManager.h:48
A helper class to store unique samplers.
Definition: SamplerManager.h:45
Definition: Request.h:51
Definition: Buffer.cpp:37
uint32_t DeviceObjectUID
Unique identifier for a device object.
Definition: DeviceObjectUID.h:40
static constexpr size_t NUMBER_OF_FRAMES_IN_FLIGHT
Definition: Device.h:64
Definition: Canvas2D.h:30
Format
Definition: Format.h:28
Backend
Definition: Backend.h:28
Definition: Canvas2D.h:30
Definition: IFramebuffer.h:62
Definition: PipelineState.h:177
Definition: IFramebuffer.h:70
Definition: PipelineState.h:167
Definition: Sampler.h:54
uint32_t frameID
Definition: Device.h:235
VkObjectType type
Definition: Device.h:236
VmaAllocation allocation
Definition: Device.h:238
bool occupied
Definition: Device.h:226
uint32_t lastUsageFrameID
Definition: Device.h:225
bool submitted
Definition: Device.h:227
Structure to store all information that might be useful on device selection.
Definition: DeviceSelection.h:44