Polyphase Game Engine
Loading...
Searching...
No Matches
VulkanContext.h
Go to the documentation of this file.
1#pragma once
2
3#if API_VULKAN
4
5#include <vulkan/vulkan.h>
6#include <stb_image.h>
7
8#include "VulkanUtils.h"
9#include "MultiBuffer.h"
10#include "DescriptorSet.h"
11#include "Pipeline.h"
12#include "PipelineConfigs.h"
13#include "VramAllocator.h"
14#include "DestroyQueue.h"
15#include "Buffer.h"
16#include "Image.h"
17#include "Line.h"
18#include "SmartPointer.h"
19#include "RayTracer.h"
20#include "Profiler.h"
21#include "DescriptorPool.h"
23#include "PipelineCache.h"
24#include "RenderPassCache.h"
25#include "PostProcessChain.h"
26
27#if PLATFORM_LINUX
28#include <xcb/xcb.h>
29#include <vulkan/vulkan_xcb.h>
30#endif
31
32struct EngineState;
33class World;
34
35struct QueueFamilyIndices
36{
37 int32_t mGraphicsFamily = -1;
38 int32_t mPresentFamily = -1;
39
40 bool IsComplete()
41 {
42 return mGraphicsFamily >= 0 &&
43 mPresentFamily >= 0;
44 }
45};
46
47struct SwapChainSupportDetails
48{
49 VkSurfaceCapabilitiesKHR capabilities;
50 std::vector<VkSurfaceFormatKHR> formats;
51 std::vector<VkPresentModeKHR> presentModes;
52};
53
54struct GpuTimespan
55{
56 std::string mName;
57 int32_t mStartIndex = -1;
58 int32_t mEndIndex = -1;
59};
60
61class VulkanContext
62{
63
64public:
65
66 VulkanContext();
67 ~VulkanContext();
68
69 void Initialize();
70 void Destroy();
71
72 void BeginFrame();
73 void EndFrame();
74 void BeginRenderPass(RenderPassId id);
75 void BeginVkRenderPass(const RenderPassSetup& rpSetup, bool insertBarrier, glm::vec4 clearColor = {});
76 void EndRenderPass();
77 void EndVkRenderPass();
78 void CommitPipeline();
79 void DrawLines(const std::vector<Line>& lines);
80 void DrawFullscreen();
81 void BindFullscreenVertexBuffer(VkCommandBuffer cb);
82
83 VkDevice GetDevice();
84 void CreateSwapchain();
85 void RecreateSwapchain(bool recreateSurface);
86
87 VkPhysicalDevice GetPhysicalDevice();
88 DescriptorPool& GetDescriptorPool();
89 DescriptorLayoutCache& GetDescriptorLayoutCache();
90
91 DestroyQueue* GetDestroyQueue();
92
93 VkExtent2D& GetSwapchainExtent();
94 VkFormat GetSwapchainFormat();
95 VkFormat GetSceneColorFormat();
96 Image* GetSceneColorImage();
97 Image* GetSwapchainImage();
98
99 Pipeline* GetBoundPipeline();
100 PipelineCache& GetPipelineCache();
101 void SavePipelineCacheToFile();
102
103 void SetViewport(int32_t x, int32_t y, int32_t width, int32_t height, bool handlePrerotation, bool useSceneRes);
104 void SetScissor(int32_t x, int32_t y, int32_t width, int32_t height, bool handlePrerotation, bool useSceneRes);
105
106 void CreateCommandBuffers();
107
108 uint32_t GetFrameIndex() const;
109 uint32_t GetFrameNumber() const;
110
111 void SetMultiBufferFrameOffset(uint32_t offset) { mMultiBufferFrameOffset = offset; }
112 uint32_t GetMultiBufferFrameOffset() const { return mMultiBufferFrameOffset; }
113 RenderPassId GetCurrentRenderPassId() const;
114
115 Image* GetShadowMapImage();
116
117 VkCommandBuffer GetCommandBuffer();
118 VkCommandPool GetCommandPool();
119 VkQueue GetGraphicsQueue();
120
121 void UpdateGlobalDescriptorSet();
122 void UpdateGlobalUniformData();
123
124 void BindGlobalDescriptorSet();
125
126 GlobalUniformData& GetGlobalUniformData();
127
128 bool IsValidationEnabled() const;
129 bool IsRayTracingSupported() const;
130 bool HasFeatureWideLines() const;
131 bool HasFeatureFillModeNonSolid() const;
132
133 bool AreMaterialsEnabled() const;
134 void EnableMaterials(bool enable);
135
136 void BeginGpuTimestamp(const char* name);
137 void EndGpuTimestamp(const char* name);
138 void ReadTimeQueryResults();
139
140 RayTracer* GetRayTracer();
141 PostProcessChain* GetPostProcessChain();
142
143 void RenderPostProcessChain();
144
145 VkSurfaceTransformFlagBitsKHR GetPreTransformFlag() const;
146
147 uint32_t GetSceneWidth();
148 uint32_t GetSceneHeight();
149
150 const VkPhysicalDeviceProperties& GetDeviceProperties() const;
151 UniformBuffer* GetFrameUniformBuffer();
152
153 Shader* GetGlobalShader(const std::string& name);
154
155 // Pipeline State
156 const PipelineState& GetPipelineState() const;
157 void SetPipelineState(const PipelineState& state);
158 void SetVertexShader(Shader* shader);
159 void SetFragmentShader(Shader* shader);
160 void SetComputeShader(Shader* shader);
161 void SetVertexShader(const std::string& globalName);
162 void SetFragmentShader(const std::string& globalName);
163 void SetComputeShader(const std::string& globalName);
164 void SetRenderPass(VkRenderPass renderPass);
165 void SetVertexType(VertexType vertexType);
166 void SetRasterizerDiscard(bool discard);
167 void SetPrimitiveTopology(VkPrimitiveTopology primitiveToplogy);
168 void SetPolygonMode(VkPolygonMode polygonMode);
169 void SetLineWidth(float lineWidth);
170 void SetDynamicLineWidth(bool dynamicLineWidth);
171 void SetCullMode(VkCullModeFlags cullMode);
172 void SetFrontFace(VkFrontFace frontFace);
173 void SetDepthBias(float depthBias);
174 void SetDepthTestEnabled(bool enabled);
175 void SetDepthWriteEnabled(bool enabled);
176 void SetDepthCompareOp(VkCompareOp compareOp);
177 void SetBlendState(VkPipelineColorBlendAttachmentState blendState, uint32_t index = 0);
178 void SetBlendState(BasicBlendState basicBlendState, uint32_t index = 0);
179 void SetBlendEnable(bool enable, uint32_t index = 0);
180 void SetBlendColorOp(VkBlendFactor src, VkBlendFactor dst, VkBlendOp op, uint32_t index = 0);
181 void SetBlendAlphaOp(VkBlendFactor src, VkBlendFactor dst, VkBlendOp op, uint32_t index = 0);
182 void SetColorWriteMask(VkColorComponentFlags writeMask, uint32_t index = 0);
183
184
185private:
186
187 static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(
188 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
189 VkDebugUtilsMessageTypeFlagsEXT messageType,
190 const VkDebugUtilsMessengerCallbackDataEXT* callbackData,
191 void* userData);
192
193 void CreateInstance();
194 void CreateDebugCallback();
195 void CreateSurface();
196 void CreateLogicalDevice();
197 void CreateFrameUniformBuffer();
198 void DestroyFrameUniformBuffer();
199 void CreateRenderPasses();
200 void DestroyRenderPasses();
201 void CreateCommandPool();
202 void CreateSemaphores();
203 void CreateFences();
204 void CreateDescriptorPools();
205 void DestroyDescriptorPools();
206 void CreateDepthImage();
207 void CreateSceneColorImage();
208 void CreateShadowMapImage();
209 void CreateQueryPools();
210 void DestroyQueryPools();
211 void RecreateSurface();
212 void CreateGlobalShaders();
213 void DestroyGlobalShaders();
214 void CreateMisc();
215 void DestroyMisc();
216
217 void PickPhysicalDevice();
218 bool IsDeviceSuitable(VkPhysicalDevice device);
219 VkSurfaceFormatKHR ChooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
220 VkPresentModeKHR ChooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availableModes);
221 VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
222 QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
223 SwapChainSupportDetails QuerySwapChainSupport(VkPhysicalDevice device);
224
225 bool CheckValidationLayerSupport(
226 const char** layers,
227 uint32_t count);
228 bool CheckDeviceExtensionSupport(
229 VkPhysicalDevice device,
230 const char** extensions,
231 uint32_t count);
232
233 void DestroySwapchain();
234 void DestroyDebugCallback();
235
236private:
237
238 // Core
239 VkInstance mInstance = VK_NULL_HANDLE;
240 VkDebugReportCallbackEXT mCallback = VK_NULL_HANDLE;
241 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
242 VkDevice mDevice = VK_NULL_HANDLE;
243 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
244 VkQueue mPresentQueue = VK_NULL_HANDLE;
245 VkSurfaceKHR mSurface = VK_NULL_HANDLE;
246 uint32_t mGraphicsQueueFamily = 0;
247 uint32_t mPresentQueueFamily = 0;
248
249 // Descriptors
250 DescriptorPool mDescriptorPools[MAX_FRAMES];
251 DescriptorLayoutCache mDescriptorLayoutCache;
252 VkDescriptorPool mImguiDescriptorPool = VK_NULL_HANDLE;
253
254 // Command Buffers
255 VkCommandPool mCommandPool = VK_NULL_HANDLE;
256 std::vector<VkCommandBuffer> mCommandBuffers;
257
258 // Swapchain
259 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
260 std::vector<VkImage> mSwapchainImages;
261 std::vector<VkImageView> mSwapchainImageViews;
262 VkFormat mSwapchainImageFormat = VK_FORMAT_UNDEFINED;
263 VkExtent2D mSwapchainExtent = {};
264 std::vector<Image*> mExtSwapchainImages;
265
266 // RenderPasses
267 RenderPassCache mRenderPassCache;
268 VkRenderPass mImguiRenderPass = VK_NULL_HANDLE;
269
270 // Images
271 Image* mShadowMapImage = nullptr;
272 Image* mSceneColorImage = nullptr;
273 VkFormat mSceneColorImageFormat;
274 Image* mDepthImage = nullptr;
275 VkFormat mDepthImageFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
276
277 // Synchronization
278 VkSemaphore mImageAvailableSemaphore[MAX_FRAMES] = {};
279 VkSemaphore mRenderFinishedSemaphore[MAX_FRAMES] = {};
280 VkFence mWaitFences[MAX_FRAMES] = {};
281
282 // Pipelines
283 PipelineCache mPipelineCache;
284 Pipeline* mBoundPipeline = nullptr;
285
286 // Shader Data
287 std::unordered_map<std::string, Shader*> mGlobalShaders;
288 DescriptorSet mGlobalDescriptorSet;
289 DescriptorSet mDebugDescriptorSet;
290 DescriptorSet mPostProcessDescriptorSet;
291 UniformBuffer* mFrameUniformBuffer = nullptr;
292 GlobalUniformData mGlobalUniformData;
293
294 // Destroy Queue
295 DestroyQueue mDestroyQueue;
296
297 // Ray Tracer
298 RayTracer mRayTracer;
299
300 // Debug
301 bool mValidate;
302 VkDebugUtilsMessengerEXT mDebugMessenger = VK_NULL_HANDLE;
303 uint32_t mEnabledExtensionCount = 0;
304 const char* mEnabledExtensions[MAX_ENABLED_EXTENSIONS] = { };
305 uint32_t mEnabledLayersCount = 0;
306 const char* mEnabledLayers[MAX_ENABLED_LAYERS] = { };
307
308 // Timestamp Queries
309 std::vector<GpuTimespan> mGpuTimespans[MAX_FRAMES];
310 VkQueryPool mTimeQueryPools[MAX_FRAMES] = { };
311 int32_t mNumTimestamps[MAX_FRAMES] = { };
312 float mTimestampPeriod = 0.0f;
313 bool mTimestampsSupported = false;
314
315 //Pipeline State
316 PipelineState mPipelineState;
317
318 // PostProcess
319 PostProcessChain mPostProcessChain;
320
321 // Misc
322 int32_t mFrameIndex = 0;
323 int32_t mFrameNumber = 0;
324 uint32_t mSwapchainImageIndex = 0;
325 RenderPassId mCurrentRenderPassId = RenderPassId::Count;
326 int32_t mNumLinesAllocated = 0;
327 Buffer* mLineVertexBuffer = nullptr;
328 bool mInitialized = false;
329 bool mEnableMaterials = false;
330 bool mSupportsRayTracing = false;
331 bool mFeatureWideLines = false;
332 bool mFeatureFillModeNonSolid = false;
333 EngineState* mEngineState = nullptr;
334 VkSurfaceTransformFlagBitsKHR mPreTransformFlag = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
335 uint32_t mMultiBufferFrameOffset = 0;
336 float mResolutionScale = 1.0f;
337 uint32_t mSceneWidth = 0;
338 uint32_t mSceneHeight = 0;
339 VkPhysicalDeviceProperties mDeviceProperties;
340 Buffer* mFullScreenVertexBuffer = nullptr;
341
342#if EDITOR
343public:
344 class Node3D* ProcessHitCheck(World* world, int32_t pixelX, int32_t pixelY, uint32_t* outInstance = nullptr);
345private:
346 void CreateHitCheck();
347 void DestroyHitCheck();
348
349 Image* mHitCheckImage = nullptr;
350 Buffer* mHitCheckBuffer = nullptr;
351#endif
352
353};
354
355void CreateVulkanContext();
356void DestroyVulkanContext();
357VulkanContext* GetVulkanContext();
358
359#endif
bool Initialize()
Definition Engine.cpp:417
RenderPassId
Definition GraphicsTypes.h:89
VertexType
Definition Vertex.h:7
#define MAX_ENABLED_LAYERS
Definition VulkanConstants.h:4
#define MAX_ENABLED_EXTENSIONS
Definition VulkanConstants.h:3
BasicBlendState
Definition VulkanTypes.h:21
uint32_t GetFrameNumber()
Definition VulkanUtils.cpp:326
VkCommandBuffer GetCommandBuffer()
Definition VulkanUtils.cpp:341
uint32_t GetFrameIndex()
Definition VulkanUtils.cpp:321
DestroyQueue * GetDestroyQueue()
Definition VulkanUtils.cpp:331
Definition DescriptorLayoutCache.h:8
Definition DescriptorPool.h:7
Definition Node3d.h:14
Definition PipelineCache.h:9
Definition PostProcessChain.h:10
Definition RayTracer.h:15
Definition RenderPassCache.h:59
Definition Shader.h:18
Definition World.h:24
void BeginFrame()
Definition ImGuizmo.cpp:982
Definition EngineTypes.h:343
Definition VulkanTypes.h:120
Definition VulkanTypes.h:30
Definition VulkanTypes.h:85