KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
VulkanContext.h
Go to the documentation of this file.
1
10#pragma once
11
12#define GLFW_INCLUDE_VULKAN
13
15#include "GLFW/glfw3.h"
16#include "vulkan/vulkan_core.h"
19
20namespace Karma
21{
22 class VulkanShader;
23
24 class Texture;
25
26 class Scene;
27
31 class RendererAPI;
32
37
39 {
40 VkDescriptorSet viewSet; // Set 0: Camera UBO
41 std::vector<VkDescriptorSet> textureSet; // Set 1: Per-mesh Texture
42 std::vector<VkDescriptorSet> objectsSet; // Set 2: Per-mesh UBO
43 };
44
59 {
66 std::optional<uint32_t> graphicsFamily;
67
78 std::optional<uint32_t> presentFamily;
79
87 {
88 return graphicsFamily.has_value() && presentFamily.has_value();
89 }
90 };
91
103 {
110 VkSurfaceCapabilitiesKHR capabilities;
111
117 std::vector<VkSurfaceFormatKHR> formats;
118
124 std::vector<VkPresentModeKHR> presentModes;
125 };
126
140 {
141 public:
150 VulkanContext(GLFWwindow* windowHandle);
151
172 virtual ~VulkanContext() override;
173
196 virtual void Init() override;
197
207 virtual void SwapBuffers() override;
208
209 virtual bool OnWindowResize(WindowResizeEvent& event) override {/*No need for Vulkan for now.*/ return true; }
210
216 void CreateInstance();
217
226 void PrintAvailableExtensions();
227
234 void PrintAvailablePhysicalDevices(const std::vector<VkPhysicalDevice>& physicalDevices);
235
236 // Validation layers
237 bool CheckValidationLayerSupport();
238 std::vector<const char*> GetRequiredExtensions(VkInstanceCreateFlags& flagsToBeSet);
239
240 static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
241 VkDebugUtilsMessageTypeFlagsEXT messageType,
242 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
243 void* pUserData);
244
245 void SetupDebugMessenger();
246 void PopulateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
247 VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
248 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
249 const VkAllocationCallbacks* pAllocator,
250 VkDebugUtilsMessengerEXT* pDebugMessenger);
251
252 void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator);
253
259 void CreateSurface();
260
268 void PickPhysicalDevice();
269
279 bool IsDeviceSuitable(VkPhysicalDevice device);
280
287 QueueFamilyIndicesDepricated FindQueueFamilies(VkPhysicalDevice device);
288
300 void CreateLogicalDevice();
301
314 void CreateSwapChain();
315
324 bool CheckDeviceExtensionSupport(VkPhysicalDevice device);
325
332 SwapChainSupportDetailsDepricated QuerySwapChainSupport(VkPhysicalDevice device);
333
349 VkSurfaceFormatKHR ChooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
350
361 VkPresentModeKHR ChooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
362
371 VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
372
382 uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
383
390 void CreateImageViews();
391
397 void CreateRenderPass();
398
404 void CreateFrameBuffers();
405
411 void CreateCommandPool();
412
419 void CreateDepthResources();
420
428 VkFormat FindSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
429
436 VkFormat FindDepthFormat();
437
448 bool HasStencilComponent(VkFormat format);
449
463 void TransitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout);
464
478 void CopyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
479
488 void RecreateSwapChain();
489
498 void CleanupSwapChain();
499
510 void SetVSync(bool bEnable);
511
517 void Initializeglslang();
518
525 void RegisterUBO(VulkanUniformBuffer* ubo);
526
533 void ClearUBO();
534
541 void RecreateUBO();
542
562 VkShaderModule CreateShaderModule(const std::vector<uint32_t>& code);
563
564 void CreateVulkanResourcesForScene(std::shared_ptr<Scene> scene3D );
565
571 void CreateGeneralShader();
572
578 void CreateGeneralTexture();
579
587 void CreateGeneralDescriptorSetLayouts();
588
598 void CreateKarmaGuiGeneralGraphicsPipeline(VkRenderPass renderPassKG, float windowKGWidth, float windowKGHeight);
599
607 void CleanUpKarmaGuiGeneralGraphicsPipeline();
608
620 void CreateGeneralDescriptorPool(uint32_t smElementsNumber);
621
629 void CreateGeneralDescriptorSets(std::shared_ptr<Scene> scene3D, uint32_t smElementsNumber, uint32_t maxFramesInFlight);
630
641 void UpdateGeneralDescriptorSets(std::shared_ptr<Scene> scene3D, uint32_t frameIndex);
642
671 void UploadUBO(size_t frameIndex);
672
673 // Getters
674 VkDevice GetLogicalDevice() const { return m_device; }
675 VkPhysicalDevice GetPhysicalDevice() const { return m_physicalDevice; }
676 VkExtent2D GetSwapChainExtent() const { return m_swapChainExtent; }
677 VkRenderPass GetRenderPass() const { return m_renderPass; }
678 const std::vector<VkFramebuffer>& GetSwapChainFrameBuffer() const { return m_swapChainFrameBuffers; }
679 VkSwapchainKHR GetSwapChain() const { return m_swapChain; }
680 inline const std::vector<VkImage>& GetSwapChainImages() const { return m_swapChainImages; }
681 VkFormat GetSwapChainImageFormat() const { return m_swapChainImageFormat; }
682 const std::vector<VkImageView>& GetSwapChainImageViews() const { return m_swapChainImageViews; }
683 VkSurfaceFormatKHR GetSurfaceFormat() const { return m_surfaceFormat; }
684 VkQueue GetGraphicsQueue() const { return m_graphicsQueue; }
685 VkQueue GetPresentQueue() const { return m_presentQueue; }
686 VkCommandPool GetCommandPool() const { return m_commandPool; }
687 VkImageView GetDepthImageView() const { return m_DepthImageView; }
688 const VkPhysicalDeviceFeatures& GetSupportedDeviceFeatures() const { return m_SupportedDeviceFeatures; }
689 VkInstance GetInstance() const { return m_Instance; }
690 uint32_t GetImageCount() const { return uint32_t(m_swapChainImages.size()); }
691 uint32_t GetMinImageCount() const { return m_MinImageCount; }
692 VkSurfaceKHR GetSurface() const { return m_surface; }
693 VkPresentModeKHR GetPresentMode() const { return m_presentMode; }
694 std::shared_ptr<VulkanShader> GetGeneralShader() const { return m_GeneralShader; }
695 VkPipeline GetKarmaGuiGeneralGraphicsPipeline() const { return m_KarmaGuiGeneralGraphicsPipeline; }
696 VkPipelineLayout GetKarmaGuiGeneralPipelineLayout() const { return m_KarmaGuiGeneralPipelineLayout; }
697 const std::vector<FrameDescriptorSets>& GetGeneralDescriptorSets() const { return m_GeneralDescriptorSets; }
698
699 private:
700 // Apologies for little out-of-sync naming convention, was dealing with flood of lines when
701 // learning Vulkan!
702 GLFWwindow* m_windowHandle;
703 VulkanRendererAPI* m_vulkanRendererAPI;
704
705 VkInstance m_Instance;
706 VkDebugUtilsMessengerEXT debugMessenger;
707
708 VkPhysicalDeviceFeatures m_SupportedDeviceFeatures;
709
710 static bool bEnableValidationLayers;
711
712 VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
713 VkDevice m_device;
714 VkQueue m_graphicsQueue;
715
716 VkSurfaceKHR m_surface;
717 VkQueue m_presentQueue;
718 VkPresentModeKHR m_presentMode;
719
720 VkSurfaceFormatKHR m_surfaceFormat;
721
722 VkSwapchainKHR m_swapChain;
723 std::vector<VkImage> m_swapChainImages;
724 VkFormat m_swapChainImageFormat;
725 VkExtent2D m_swapChainExtent;
726 std::vector<VkImageView> m_swapChainImageViews;
727
728 VkRenderPass m_renderPass;
729
730 std::vector<VkFramebuffer> m_swapChainFrameBuffers;
731 VkCommandPool m_commandPool;
732
733 std::set<VulkanUniformBuffer*> m_VulkanUBO;
734
735 // General vulkan resources
736 std::shared_ptr<VulkanShader> m_GeneralShader;
737 std::shared_ptr<Texture> m_GeneralTexture;
738 VkPipelineLayout m_KarmaGuiGeneralPipelineLayout;
739 VkPipeline m_KarmaGuiGeneralGraphicsPipeline;
740 VkDescriptorSetLayout m_ViewLayout; // Camera UBO
741 VkDescriptorSetLayout m_TextureLayout; // Material UBO + texture sampler
742 VkDescriptorSetLayout m_ObjectLayout; // Object UBO
743 VkDescriptorPool m_GeneralDescriptorPool;
744 std::vector<FrameDescriptorSets> m_GeneralDescriptorSets;
745
746 bool bVSync = true;
747
748 VkImage m_DepthImage;
749 VkDeviceMemory m_DepthImageMemory;
750 VkImageView m_DepthImageView;
751
752 uint32_t m_MinImageCount = 0;
753 };
754}
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
This file contains base class GraphicsContext.
This file contains VulkanVertex/Index/ImageBuffer classes.
This file contains VulkanRendererAPI class with Vulkan specific implementation of RendererAPI pure vi...
An abstract class for creating a context for Renderer and provide graphics API.
Definition GraphicsContext.h:20
An abstract class for a renderer.
Definition RendererAPI.h:21
Class for organizing and containing Scene relevant data.
Definition Scene.h:24
Texture class.
Definition Texture.h:45
VulkanContext(GLFWwindow *windowHandle)
A constructor to set the m_vulkanRendererAPI (using static_cast, or compilet time cast)....
Definition VulkanContext.cpp:56
virtual bool OnWindowResize(WindowResizeEvent &event) override
Calls glViewport function which specifies the affine transformation of x and y from normalized device...
Definition VulkanContext.h:209
virtual void SwapBuffers() override
In Vulkan there is no default framebuffer, hence we need to explicitly swap the buffers in the swapch...
Definition VulkanContext.cpp:654
virtual void Init() override
Initializes VulkanContext by creating appropriate Vulkan and glslang specific instruments and allocat...
Definition VulkanContext.cpp:144
Vulkan specific implementation of Shader class.
Definition VulkanShader.h:28
Event triggered when Window is resized.
Definition ApplicationEvent.h:20
Definition VulkanContext.h:39
A structure for graphics and present queuefamilies.
Definition VulkanContext.h:59
bool IsComplete()
Routine for querying if appropriate queue families (graphicsFamily and presentFamily) are available.
Definition VulkanContext.h:86
std::optional< uint32_t > graphicsFamily
The queues in this queue family support graphics operations.
Definition VulkanContext.h:66
std::optional< uint32_t > presentFamily
The queues in this queue family support image presentation.
Definition VulkanContext.h:78
Structure with data required for appropriate creation and working of swapchain.
Definition VulkanContext.h:103
VkSurfaceCapabilitiesKHR capabilities
Basic surface capabilities (min/max number of images in swap chain, min/max width and height of image...
Definition VulkanContext.h:110
std::vector< VkSurfaceFormatKHR > formats
Surface formats (pixel format, color space).
Definition VulkanContext.h:117
std::vector< VkPresentModeKHR > presentModes
Available presentation modes.
Definition VulkanContext.h:124
The actual uniform buffer class with implemented function using Vulkan API.
Definition VulkanBuffer.h:266