Karma Engine
Loading...
Searching...
No Matches
VulkanContext.h
1#pragma once
2
3#define GLFW_INCLUDE_VULKAN
4#include "krpch.h"
5
6#include "Karma/Renderer/GraphicsContext.h"
7#include "GLFW/glfw3.h"
8#include "vulkan/vulkan_core.h"
9#include "Platform/Vulkan/VulkanBuffer.h"
10#include "Platform/Vulkan/VulkanRendererAPI.h"
11
12namespace Karma
13{
14 class RendererAPI;
15 //class VulkanRendererAPI;
16 class VulkanVertexArray;
17 struct VulkanUniformBuffer;
18
20 {
21 std::optional<uint32_t> graphicsFamily;
22 std::optional<uint32_t> presentFamily;
23
24 bool IsComplete()
25 {
26 return graphicsFamily.has_value() && presentFamily.has_value();
27 }
28 };
29
31 {
32 VkSurfaceCapabilitiesKHR capabilities;
33 std::vector<VkSurfaceFormatKHR> formats;
34 std::vector<VkPresentModeKHR> presentModes;
35 };
36
37 class KARMA_API VulkanContext : public GraphicsContext
38 {
39 public:
40 VulkanContext(GLFWwindow* windowHandle);
41 virtual ~VulkanContext() override;
42
43 virtual void Init() override;
44 virtual void SwapBuffers() override;
45 virtual bool OnWindowResize(WindowResizeEvent& event) override {/*No need for Vulkan for now.*/ return true; }
46
47 void CreateInstance();
48
49 void PrintAvailableExtensions();
50 void PrintAvailablePhysicalDevices(const std::vector<VkPhysicalDevice>& physicalDevices);
51
52 // Validation layers
53 bool CheckValidationLayerSupport();
54 std::vector<const char*> GetRequiredExtensions(VkInstanceCreateFlags& flagsToBeSet);
55
56 static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
57 VkDebugUtilsMessageTypeFlagsEXT messageType,
58 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
59 void* pUserData);
60
61 void SetupDebugMessenger();
62 void PopulateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
63 VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
64 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
65 const VkAllocationCallbacks* pAllocator,
66 VkDebugUtilsMessengerEXT* pDebugMessenger);
67
68 void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator);
69
70 // Surface
71 void CreateSurface();
72
73 // Physical device
74 void PickPhysicalDevice();
75 bool IsDeviceSuitable(VkPhysicalDevice device);
76 QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
77
78 // Logical device
79 void CreateLogicalDevice();
80
81 // Swapchain
82 void CreateSwapChain();
83 bool CheckDeviceExtensionSupport(VkPhysicalDevice device);
84 SwapChainSupportDetails QuerySwapChainSupport(VkPhysicalDevice device);
85 VkSurfaceFormatKHR ChooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
86 VkPresentModeKHR ChooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
87 VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
88 uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
89
90 // Image views
91 void CreateImageViews();
92
93 // Renderer pass
94 void CreateRenderPass();
95
96 // Framebuffers
97 void CreateFrameBuffers();
98
99 // CommandPool
100 void CreateCommandPool();
101
102 // DepthImage
103 void CreateDepthResources();
104 VkFormat FindSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
105 VkFormat FindDepthFormat();
106 bool HasStencilComponent(VkFormat format);
107
108 // Texture relevant
109 //void CreateTextureImage(VulkanImageBuffer* vImageBuffer);
110 void TransitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout);
111 void CopyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
112 //void CreateTextureImageView();
113 //void CreateTextureSampler();
114
115 void RecreateSwapChain();
116 void CleanupSwapChain();
117
118 void SetVSync(bool bEnable);
119
120 void Initializeglslang();
121 void RegisterUBO(const std::shared_ptr<VulkanUniformBuffer>& ubo);
122 void ClearUBO();
123 void RecreateUBO();
124 void UploadUBO(size_t frameIndex);
125
126 // Getters
127 VkDevice GetLogicalDevice() const { return m_device; }
128 VkPhysicalDevice GetPhysicalDevice() const { return m_physicalDevice; }
129 VkExtent2D GetSwapChainExtent() const { return m_swapChainExtent; }
130 VkRenderPass GetRenderPass() const { return m_renderPass; }
131 const std::vector<VkFramebuffer>& GetSwapChainFrameBuffer() const { return m_swapChainFrameBuffers; }
132 VkSwapchainKHR GetSwapChain() const { return m_swapChain; }
133 inline const std::vector<VkImage>& GetSwapChainImages() const { return m_swapChainImages; }
134 VkFormat GetSwapChainImageFormat() const { return m_swapChainImageFormat; }
135 const std::vector<VkImageView>& GetSwapChainImageViews() const { return m_swapChainImageViews; }
136 VkSurfaceFormatKHR GetSurfaceFormat() const { return m_surfaceFormat; }
137 VkQueue GetGraphicsQueue() const { return m_graphicsQueue; }
138 VkQueue GetPresentQueue() const { return m_presentQueue; }
139 VkCommandPool GetCommandPool() const { return m_commandPool; }
140 //VkImageView GetTextureImageView() const { return m_TextureImageView; }
141 //VkSampler GetTextureSampler() const { return m_TextureSampler; }
142 const VkPhysicalDeviceFeatures& GetSupportedDeviceFeatures() const { return m_SupportedDeviceFeatures; }
143 VkInstance GetInstance() const { return m_Instance; }
144 uint32_t GetImageCount() const { return uint32_t(m_swapChainImages.size()); }
145 uint32_t GetMinImageCount() const { return m_MinImageCount; }
146 VkSurfaceKHR GetSurface() const { return m_surface; }
147 VkPresentModeKHR GetPresentMode() const { return m_presentMode; }
148
149 private:
150 // Apologies for little out-of-sync naming convention, was dealing with flood of lines when
151 // learning Vulkan!
152 GLFWwindow* m_windowHandle;
153 VulkanRendererAPI* m_vulkanRendererAPI;
154
155 VkInstance m_Instance;
156 VkDebugUtilsMessengerEXT debugMessenger;
157
158 VkPhysicalDeviceFeatures m_SupportedDeviceFeatures;
159
160 static bool bEnableValidationLayers;
161
162 VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
163 VkDevice m_device;
164 VkQueue m_graphicsQueue;
165
166 VkSurfaceKHR m_surface;
167 VkQueue m_presentQueue;
168 VkPresentModeKHR m_presentMode;
169
170 VkSurfaceFormatKHR m_surfaceFormat;
171
172 VkSwapchainKHR m_swapChain;
173 std::vector<VkImage> m_swapChainImages;
174 VkFormat m_swapChainImageFormat;
175 VkExtent2D m_swapChainExtent;
176 std::vector<VkImageView> m_swapChainImageViews;
177
178 VkRenderPass m_renderPass;
179
180 std::vector<VkFramebuffer> m_swapChainFrameBuffers;
181 VkCommandPool m_commandPool;
182
183 std::set<std::shared_ptr<VulkanUniformBuffer>> m_VulkanUBO;
184
185 bool bVSync = false;
186
187 VkImage m_DepthImage;
188 VkDeviceMemory m_DepthImageMemory;
189 VkImageView m_DepthImageView;
190
191 uint32_t m_MinImageCount = 0;
192
193 //VkImage m_TextureImage;
194 /*
195 VkDeviceMemory m_TextureImageMemory;
196 VkImageView m_TextureImageView;
197 VkSampler m_TextureSampler;
198 */
199 };
200}
Definition GraphicsContext.h:8
Definition VulkanContext.h:38
Definition VulkanRendererAPI.h:12
Definition ApplicationEvent.h:10
Definition VulkanContext.h:20
Definition VulkanContext.h:31