Line data Source code
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"
22 : #include "renderer/backend/Backend.h"
23 : #include "renderer/backend/IBuffer.h"
24 : #include "renderer/backend/IDevice.h"
25 : #include "renderer/backend/ITexture.h"
26 : #include "renderer/backend/vulkan/DeviceForward.h"
27 : #include "renderer/backend/vulkan/DeviceObjectUID.h"
28 : #include "renderer/backend/vulkan/DeviceSelection.h"
29 : #include "renderer/backend/vulkan/VMA.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 :
43 : namespace Renderer::Backend { enum class Format; }
44 : namespace Renderer::Backend::Vulkan { class CBuffer; }
45 : namespace Renderer::Backend::Vulkan { class CDescriptorManager; }
46 : namespace Renderer::Backend::Vulkan { class CRenderPassManager; }
47 : namespace Renderer::Backend::Vulkan { class CRingCommandContext; }
48 : namespace Renderer::Backend::Vulkan { class CSamplerManager; }
49 : namespace Renderer::Backend::Vulkan { class CSubmitScheduler; }
50 : namespace Renderer::Backend::Vulkan { class CSwapChain; }
51 : namespace Renderer::Backend::Vulkan { class CTexture; }
52 :
53 : typedef struct SDL_Window SDL_Window;
54 :
55 : namespace Renderer
56 : {
57 :
58 : namespace Backend
59 : {
60 :
61 : namespace Vulkan
62 : {
63 :
64 : static constexpr size_t NUMBER_OF_FRAMES_IN_FLIGHT = 3;
65 :
66 : class CDevice final : public IDevice
67 : {
68 : public:
69 : /**
70 : * Creates the Vulkan device.
71 : */
72 : static std::unique_ptr<CDevice> Create(SDL_Window* window);
73 :
74 : ~CDevice() override;
75 :
76 0 : Backend GetBackend() const override { return Backend::VULKAN; }
77 :
78 0 : const std::string& GetName() const override { return m_Name; }
79 0 : const std::string& GetVersion() const override { return m_Version; }
80 0 : const std::string& GetDriverInformation() const override { return m_DriverInformation; }
81 0 : 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 :
130 : Format GetPreferredDepthStencilFormat(
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 0 : const Capabilities& GetCapabilities() const override { return m_Capabilities; }
142 :
143 : void CollectStatistics(StatisticsVector& statistics) const override;
144 :
145 0 : VkDevice GetVkDevice() const { return m_Device; }
146 :
147 0 : VmaAllocator GetVMAAllocator() { return m_VMAAllocator; }
148 :
149 : void InsertTimestampQuery(
150 : VkCommandBuffer commandBuffer, const uint32_t handle, const bool isScopeBegin);
151 :
152 0 : void ScheduleObjectToDestroy(
153 : VkObjectType type, const void* handle, const VmaAllocation allocation)
154 : {
155 0 : ScheduleObjectToDestroy(type, reinterpret_cast<uint64_t>(handle), allocation);
156 0 : }
157 :
158 : void ScheduleObjectToDestroy(
159 : VkObjectType type, const uint64_t handle, const VmaAllocation allocation);
160 :
161 : void ScheduleTextureToDestroy(const DeviceObjectUID uid);
162 :
163 : void ScheduleBufferToDestroy(const DeviceObjectUID uid);
164 :
165 : void OnPresent();
166 :
167 : void OnSwapChainRecreation();
168 :
169 0 : void SetObjectName(VkObjectType type, const void* handle, const char* name)
170 : {
171 0 : SetObjectName(type, reinterpret_cast<uint64_t>(handle), name);
172 0 : }
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 :
178 0 : const SAvailablePhysicalDevice& GetChoosenPhysicalDevice() const { return m_ChoosenDevice; }
179 :
180 0 : CRenderPassManager& GetRenderPassManager() { return *m_RenderPassManager; }
181 :
182 0 : CSamplerManager& GetSamplerManager() { return *m_SamplerManager; }
183 :
184 0 : CDescriptorManager& GetDescriptorManager() { return *m_DescriptorManager; }
185 :
186 : DeviceObjectUID GenerateNextDeviceObjectUID();
187 :
188 : uint32_t GetFrameID() const { return m_FrameID; }
189 :
190 : private:
191 : CDevice();
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;
201 : std::string m_DriverInformation;
202 : std::vector<std::string> m_Extensions;
203 : std::vector<std::string> m_InstanceExtensions;
204 : std::vector<std::string> m_ValidationLayers;
205 :
206 : SAvailablePhysicalDevice m_ChoosenDevice{};
207 : std::vector<SAvailablePhysicalDevice> m_AvailablePhysicalDevices;
208 :
209 : Capabilities m_Capabilities{};
210 :
211 : VkInstance m_Instance = VK_NULL_HANDLE;
212 :
213 : VkDebugUtilsMessengerEXT m_DebugMessenger = VK_NULL_HANDLE;
214 :
215 : SDL_Window* m_Window = nullptr;
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 0 : struct Query
224 : {
225 : uint32_t lastUsageFrameID{};
226 : bool occupied{};
227 : bool submitted{};
228 : };
229 : std::vector<Query> m_Queries;
230 :
231 : uint32_t m_FrameID = 0;
232 :
233 : struct ObjectToDestroy
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 :
249 : DeviceObjectUID m_LastAvailableUID{1};
250 : };
251 :
252 : } // namespace Vulkan
253 :
254 : } // namespace Backend
255 :
256 : } // namespace Renderer
257 :
258 : #endif // INCLUDED_RENDERER_BACKEND_VULKAN_DEVICE
|