5#include <vulkan/vulkan.h>
29#include <vulkan/vulkan_xcb.h>
35struct QueueFamilyIndices
37 int32_t mGraphicsFamily = -1;
38 int32_t mPresentFamily = -1;
42 return mGraphicsFamily >= 0 &&
47struct SwapChainSupportDetails
49 VkSurfaceCapabilitiesKHR capabilities;
50 std::vector<VkSurfaceFormatKHR> formats;
51 std::vector<VkPresentModeKHR> presentModes;
57 int32_t mStartIndex = -1;
58 int32_t mEndIndex = -1;
75 void BeginVkRenderPass(
const RenderPassSetup& rpSetup,
bool insertBarrier, glm::vec4 clearColor = {});
77 void EndVkRenderPass();
78 void CommitPipeline();
79 void DrawLines(
const std::vector<Line>& lines);
81 const glm::vec3& cameraRight,
const glm::vec3& cameraUp);
82 void DrawFullscreen();
83 void BindFullscreenVertexBuffer(VkCommandBuffer cb);
86 void CreateSwapchain();
87 void RecreateSwapchain(
bool recreateSurface);
89 VkPhysicalDevice GetPhysicalDevice();
95 VkExtent2D& GetSwapchainExtent();
96 VkFormat GetSwapchainFormat();
97 VkFormat GetSceneColorFormat();
98 Image* GetSceneColorImage();
99 Image* GetSwapchainImage();
101 Pipeline* GetBoundPipeline();
103 void SavePipelineCacheToFile();
105 void SetViewport(int32_t x, int32_t y, int32_t width, int32_t height,
bool handlePrerotation,
bool useSceneRes);
106 void SetScissor(int32_t x, int32_t y, int32_t width, int32_t height,
bool handlePrerotation,
bool useSceneRes);
108 void CreateCommandBuffers();
113 void SetMultiBufferFrameOffset(uint32_t offset) { mMultiBufferFrameOffset = offset; }
114 uint32_t GetMultiBufferFrameOffset()
const {
return mMultiBufferFrameOffset; }
117 Image* GetShadowMapImage();
120 VkCommandPool GetCommandPool();
121 VkQueue GetGraphicsQueue();
127 VkRenderPass GetCurrentRenderPass()
const;
129 void UpdateGlobalDescriptorSet();
130 void UpdateGlobalUniformData();
132 void BindGlobalDescriptorSet();
136 bool IsValidationEnabled()
const;
137 bool IsRayTracingSupported()
const;
138 bool HasFeatureWideLines()
const;
139 bool HasFeatureFillModeNonSolid()
const;
141 bool AreMaterialsEnabled()
const;
142 void EnableMaterials(
bool enable);
144 void BeginGpuTimestamp(
const char* name);
145 void EndGpuTimestamp(
const char* name);
146 void ReadTimeQueryResults();
151 void RenderPostProcessChain();
153 VkSurfaceTransformFlagBitsKHR GetPreTransformFlag()
const;
155 uint32_t GetSceneWidth();
156 uint32_t GetSceneHeight();
158 const VkPhysicalDeviceProperties& GetDeviceProperties()
const;
159 UniformBuffer* GetFrameUniformBuffer();
161 Shader* GetGlobalShader(
const std::string& name);
166 void SetVertexShader(
Shader* shader);
167 void SetFragmentShader(
Shader* shader);
168 void SetComputeShader(
Shader* shader);
169 void SetVertexShader(
const std::string& globalName);
170 void SetFragmentShader(
const std::string& globalName);
171 void SetComputeShader(
const std::string& globalName);
172 void SetRenderPass(VkRenderPass renderPass);
174 void SetRasterizerDiscard(
bool discard);
175 void SetPrimitiveTopology(VkPrimitiveTopology primitiveToplogy);
176 void SetPolygonMode(VkPolygonMode polygonMode);
177 void SetLineWidth(
float lineWidth);
178 void SetDynamicLineWidth(
bool dynamicLineWidth);
179 void SetCullMode(VkCullModeFlags cullMode);
180 void SetFrontFace(VkFrontFace frontFace);
181 void SetDepthBias(
float depthBias);
182 void SetDepthTestEnabled(
bool enabled);
183 void SetDepthWriteEnabled(
bool enabled);
184 void SetDepthCompareOp(VkCompareOp compareOp);
185 void SetBlendState(VkPipelineColorBlendAttachmentState blendState, uint32_t index = 0);
186 void SetBlendState(
BasicBlendState basicBlendState, uint32_t index = 0);
187 void SetBlendEnable(
bool enable, uint32_t index = 0);
188 void SetBlendColorOp(VkBlendFactor src, VkBlendFactor dst, VkBlendOp op, uint32_t index = 0);
189 void SetBlendAlphaOp(VkBlendFactor src, VkBlendFactor dst, VkBlendOp op, uint32_t index = 0);
190 void SetColorWriteMask(VkColorComponentFlags writeMask, uint32_t index = 0);
195 static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(
196 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
197 VkDebugUtilsMessageTypeFlagsEXT messageType,
198 const VkDebugUtilsMessengerCallbackDataEXT* callbackData,
201 void CreateInstance();
202 void CreateDebugCallback();
203 void CreateSurface();
204 void CreateLogicalDevice();
205 void CreateFrameUniformBuffer();
206 void DestroyFrameUniformBuffer();
207 void CreateRenderPasses();
208 void DestroyRenderPasses();
209 void CreateCommandPool();
210 void CreateSemaphores();
212 void CreateDescriptorPools();
213 void DestroyDescriptorPools();
214 void CreateDepthImage();
215 void CreateSceneColorImage();
216 void CreateShadowMapImage();
217 void CreateQueryPools();
218 void DestroyQueryPools();
219 void RecreateSurface();
220 void CreateGlobalShaders();
221 void DestroyGlobalShaders();
225 void PickPhysicalDevice();
226 bool IsDeviceSuitable(VkPhysicalDevice device);
227 VkSurfaceFormatKHR ChooseSwapSurfaceFormat(
const std::vector<VkSurfaceFormatKHR>& availableFormats);
228 VkPresentModeKHR ChooseSwapPresentMode(
const std::vector<VkPresentModeKHR>& availableModes);
229 VkExtent2D ChooseSwapExtent(
const VkSurfaceCapabilitiesKHR& capabilities);
230 QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
231 SwapChainSupportDetails QuerySwapChainSupport(VkPhysicalDevice device);
233 bool CheckValidationLayerSupport(
236 bool CheckDeviceExtensionSupport(
237 VkPhysicalDevice device,
238 const char** extensions,
241 void DestroySwapchain();
242 void DestroyDebugCallback();
247 VkInstance mInstance = VK_NULL_HANDLE;
248 VkDebugReportCallbackEXT mCallback = VK_NULL_HANDLE;
249 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
250 VkDevice mDevice = VK_NULL_HANDLE;
251 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
252 VkQueue mPresentQueue = VK_NULL_HANDLE;
253 VkSurfaceKHR mSurface = VK_NULL_HANDLE;
254 uint32_t mGraphicsQueueFamily = 0;
255 uint32_t mPresentQueueFamily = 0;
260 VkDescriptorPool mImguiDescriptorPool = VK_NULL_HANDLE;
263 VkCommandPool mCommandPool = VK_NULL_HANDLE;
264 std::vector<VkCommandBuffer> mCommandBuffers;
267 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
268 std::vector<VkImage> mSwapchainImages;
269 std::vector<VkImageView> mSwapchainImageViews;
270 VkFormat mSwapchainImageFormat = VK_FORMAT_UNDEFINED;
271 VkExtent2D mSwapchainExtent = {};
272 std::vector<Image*> mExtSwapchainImages;
276 VkRenderPass mImguiRenderPass = VK_NULL_HANDLE;
279 Image* mShadowMapImage =
nullptr;
280 Image* mSceneColorImage =
nullptr;
281 VkFormat mSceneColorImageFormat;
282 Image* mDepthImage =
nullptr;
283 VkFormat mDepthImageFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
286 VkSemaphore mImageAvailableSemaphore[MAX_FRAMES] = {};
287 VkSemaphore mRenderFinishedSemaphore[MAX_FRAMES] = {};
288 VkFence mWaitFences[MAX_FRAMES] = {};
292 Pipeline* mBoundPipeline =
nullptr;
295 std::unordered_map<std::string, Shader*> mGlobalShaders;
296 DescriptorSet mGlobalDescriptorSet;
297 DescriptorSet mDebugDescriptorSet;
298 DescriptorSet mPostProcessDescriptorSet;
299 UniformBuffer* mFrameUniformBuffer =
nullptr;
303 DestroyQueue mDestroyQueue;
310 VkDebugUtilsMessengerEXT mDebugMessenger = VK_NULL_HANDLE;
311 uint32_t mEnabledExtensionCount = 0;
313 uint32_t mEnabledLayersCount = 0;
317 std::vector<GpuTimespan> mGpuTimespans[MAX_FRAMES];
318 VkQueryPool mTimeQueryPools[MAX_FRAMES] = { };
319 int32_t mNumTimestamps[MAX_FRAMES] = { };
320 float mTimestampPeriod = 0.0f;
321 bool mTimestampsSupported =
false;
330 int32_t mFrameIndex = 0;
331 int32_t mFrameNumber = 0;
332 uint32_t mSwapchainImageIndex = 0;
334 int32_t mNumLinesAllocated = 0;
335 Buffer* mLineVertexBuffer =
nullptr;
336 int32_t mNumSplatsAllocated = 0;
337 Buffer* mSplatVertexBuffer =
nullptr;
338 bool mInitialized =
false;
339 bool mEnableMaterials =
false;
340 bool mSupportsRayTracing =
false;
341 bool mFeatureWideLines =
false;
342 bool mFeatureFillModeNonSolid =
false;
344 VkSurfaceTransformFlagBitsKHR mPreTransformFlag = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
345 uint32_t mMultiBufferFrameOffset = 0;
346 float mResolutionScale = 1.0f;
347 uint32_t mSceneWidth = 0;
348 uint32_t mSceneHeight = 0;
349 VkPhysicalDeviceProperties mDeviceProperties;
350 Buffer* mFullScreenVertexBuffer =
nullptr;
354 class Node3D* ProcessHitCheck(
World* world, int32_t pixelX, int32_t pixelY, uint32_t* outInstance =
nullptr);
356 void CreateHitCheck();
357 void DestroyHitCheck();
359 Image* mHitCheckImage =
nullptr;
360 Buffer* mHitCheckBuffer =
nullptr;
365void CreateVulkanContext();
366void DestroyVulkanContext();
367VulkanContext* GetVulkanContext();
bool Initialize()
Definition Engine.cpp:491
RenderPassId
Definition GraphicsTypes.h:90
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 PipelineCache.h:9
Definition PostProcessChain.h:10
Definition RayTracer.h:15
Definition RenderPassCache.h:59
void BeginFrame()
Definition ImGuizmo.cpp:982
Definition EngineTypes.h:359
Definition VulkanTypes.h:30
Definition VulkanTypes.h:85