Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
VulkanCore.hpp
1#pragma once
2
3#include <functional>
4
5#include <vma/vk_mem_alloc.h>
6
7#include <Common/Logging.hpp>
8#include <Common/Graphics/Core.hpp>
9#include <Common/Graphics/DLLDefs.hpp>
10#include <vulkan/vulkan.h>
11
12namespace Grindstone::GraphicsAPI::Vulkan {
14 VkSurfaceCapabilitiesKHR capabilities;
15 std::vector<VkSurfaceFormatKHR> formats;
16 std::vector<VkPresentModeKHR> presentModes;
17 };
18
20 uint32_t graphicsFamily = 0;
21 uint32_t presentFamily = 0;
22
23 bool hasGraphicsFamily = false;
24 bool hasPresentFamily = false;
25
26 bool IsComplete() const {
27 return hasGraphicsFamily && hasPresentFamily;
28 }
29 };
30
32 public:
33 virtual bool Initialize(const Grindstone::GraphicsAPI::Core::CreateInfo& ci) override;
34 ~Core();
35
36 static Vulkan::Core* graphicsWrapper;
37 static Vulkan::Core& Get();
38 virtual void RegisterWindow(Window* window) override;
39 public:
40 QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
41 uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
42 virtual VkInstance GetInstance();
43 virtual VkDevice GetDevice();
44 virtual VkPhysicalDevice GetPhysicalDevice();
45 virtual VkCommandBuffer BeginSingleTimeCommands();
46 virtual uint32_t GetGraphicsFamily();
47 virtual void EndSingleTimeCommands(VkCommandBuffer commandBuffer);
48 VkCommandPool GetGraphicsCommandPool() const;
49 private:
50
51 VkInstance instance = nullptr;
52 VkDevice device = nullptr;
53 VkPhysicalDevice physicalDevice = nullptr;
54 VkDebugUtilsMessengerEXT debugMessenger = nullptr;
55 std::vector<VkSemaphore> imageAvailableSemaphores;
56 std::vector<VkSemaphore> renderFinishedSemaphores;
57 std::vector<VkFence> inFlightFences;
58 std::vector<VkFence> imagesInFlight;
59 size_t currentFrame = 0;
60 PFN_vkSetDebugUtilsObjectNameEXT pfnDebugUtilsSetObjectName = nullptr;
61 public:
62 VkQueue graphicsQueue = nullptr;
63 VkQueue presentQueue = nullptr;
64 uint32_t graphicsFamily = 0;
65 uint32_t presentFamily = 0;
66 VkCommandPool commandPoolGraphics = nullptr;
67 VkDescriptorPool descriptorPool = nullptr;
68 private:
69 void CreateInstance();
70 void SetupDebugMessenger();
71 void PickPhysicalDevice();
72 void CreateLogicalDevice();
73 void CreateCommandPool();
74 void CreateDescriptorPool();
75 private:
76 void CreateAllocator();
77 uint16_t ScoreDevice(VkPhysicalDevice device);
78 bool CheckDeviceExtensionSupport(VkPhysicalDevice device);
79 bool CheckValidationLayerSupport();
80 std::vector<const char*> GetRequiredExtensions();
81 void PopulateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
82 public:
83 void NameObject(VkObjectType objectType, void* object, const char* objectName);
84
85 virtual const char* GetVendorName() const override;
86 virtual const char* GetAdapterName() const override;
87 virtual const char* GetAPIName() const override;
88 virtual const char* GetAPIVersion() const override;
89
90 virtual void AdjustPerspective(float *perspective) override;
91
92 virtual void DeleteRenderTarget(GraphicsAPI::RenderTarget * ptr) override;
93 virtual void DeleteDepthStencilTarget(GraphicsAPI::DepthStencilTarget * ptr) override;
94 virtual void DeleteFramebuffer(GraphicsAPI::Framebuffer *ptr) override;
95 virtual void DeleteVertexBuffer(GraphicsAPI::VertexBuffer *ptr) override;
96 virtual void DeleteIndexBuffer(GraphicsAPI::IndexBuffer *ptr) override;
97 virtual void DeleteUniformBuffer(GraphicsAPI::UniformBuffer * ptr) override;
98 virtual void DeleteGraphicsPipeline(GraphicsAPI::GraphicsPipeline* ptr) override;
99 virtual void DeleteComputePipeline(GraphicsAPI::ComputePipeline* ptr) override;
100 virtual void DeleteRenderPass(GraphicsAPI::RenderPass *ptr) override;
101 virtual void DeleteTexture(GraphicsAPI::Texture* ptr) override;
102 virtual void DeleteDescriptorSet(GraphicsAPI::DescriptorSet* ptr) override;
103 virtual void DeleteDescriptorSetLayout(GraphicsAPI::DescriptorSetLayout* ptr) override;
104 virtual void DeleteCommandBuffer(GraphicsAPI::CommandBuffer *ptr) override;
105 virtual void DeleteVertexArrayObject(GraphicsAPI::VertexArrayObject *ptr) override;
106
107 virtual GraphicsAPI::Framebuffer *CreateFramebuffer(const GraphicsAPI::Framebuffer::CreateInfo& ci) override;
108 virtual GraphicsAPI::RenderPass *CreateRenderPass(const GraphicsAPI::RenderPass::CreateInfo& ci) override;
109 virtual GraphicsAPI::GraphicsPipeline* CreateGraphicsPipeline(const GraphicsAPI::GraphicsPipeline::CreateInfo& ci) override;
110 virtual GraphicsAPI::ComputePipeline* CreateComputePipeline(const GraphicsAPI::ComputePipeline::CreateInfo& ci) override;
111 virtual GraphicsAPI::CommandBuffer *CreateCommandBuffer(const GraphicsAPI::CommandBuffer::CreateInfo& ci) override;
112 virtual GraphicsAPI::VertexArrayObject *CreateVertexArrayObject(const GraphicsAPI::VertexArrayObject::CreateInfo& ci) override;
113 virtual GraphicsAPI::VertexBuffer *CreateVertexBuffer(const GraphicsAPI::VertexBuffer::CreateInfo& ci) override;
114 virtual GraphicsAPI::IndexBuffer *CreateIndexBuffer(const GraphicsAPI::IndexBuffer::CreateInfo& ci) override;
115 virtual GraphicsAPI::UniformBuffer *CreateUniformBuffer(const GraphicsAPI::UniformBuffer::CreateInfo& ci) override;
116 virtual GraphicsAPI::Texture * CreateCubemap(const GraphicsAPI::Texture::CubemapCreateInfo& ci) override;
117 virtual GraphicsAPI::Texture* CreateTexture(const GraphicsAPI::Texture::CreateInfo& ci) override;
118 virtual GraphicsAPI::DescriptorSet* CreateDescriptorSet(const GraphicsAPI::DescriptorSet::CreateInfo& ci) override;
119 virtual GraphicsAPI::DescriptorSetLayout* CreateDescriptorSetLayout(const GraphicsAPI::DescriptorSetLayout::CreateInfo& ci) override;
120 virtual GraphicsAPI::RenderTarget* CreateRenderTarget(const GraphicsAPI::RenderTarget::CreateInfo& rt) override;
121 virtual GraphicsAPI::RenderTarget *CreateRenderTarget(const GraphicsAPI::RenderTarget::CreateInfo* rt, uint32_t rc, bool cube = false) override;
122 virtual GraphicsAPI::DepthStencilTarget *CreateDepthStencilTarget(const GraphicsAPI::DepthStencilTarget::CreateInfo& rt) override;
123
124 virtual inline bool ShouldUseImmediateMode() const override;
125 virtual inline bool SupportsCommandBuffers() const override;
126 virtual inline bool SupportsTesselation() const override;
127 virtual inline bool SupportsGeometryShader() const override;
128 virtual inline bool SupportsComputeShader() const override;
129 virtual inline bool SupportsMultiDrawIndirect() const override;
130
131 virtual void WaitUntilIdle() override;
132
133 // Unused
134 virtual void Clear(ClearMode mask, float clear_color[4], float clear_depth, uint32_t clear_stencil) override;
135 virtual void BindGraphicsPipeline(GraphicsAPI::GraphicsPipeline*) override;
136 virtual void BindVertexArrayObject(GraphicsAPI::VertexArrayObject *) override;
137 virtual void DrawImmediateIndexed(GeometryType geom_type, bool largeBuffer, int32_t baseVertex, uint32_t indexOffsetPtr, uint32_t indexCount) override;
138 virtual void DrawImmediateVertices(GeometryType geom_type, uint32_t base, uint32_t count) override;
139 virtual void SetImmediateBlending(
140 BlendOperation colorOp, BlendFactor colorSrc, BlendFactor colorDst,
141 BlendOperation alphaOp, BlendFactor alphaSrc, BlendFactor alphaDst
142 ) override;
143 virtual void EnableDepthWrite(bool state) override;
144 virtual void SetColorMask(ColorMask mask) override;
145 private:
146 std::string vendorName;
147 std::string adapterName;
148 std::string apiVersion;
149 VmaAllocator allocator;
150
151 Window* primaryWindow = nullptr;
152
153 // Inherited via Core
154 virtual const char* GetDefaultShaderExtension() const override;
155 virtual void CopyDepthBufferFromReadToWrite(uint32_t srcWidth, uint32_t srcHeight, uint32_t dstWidth, uint32_t dstHeight) override;
156 virtual void BindDefaultFramebuffer() override;
157 virtual void BindDefaultFramebufferWrite() override;
158 virtual void BindDefaultFramebufferRead() override;
159 virtual void ResizeViewport(uint32_t w, uint32_t h) override;
160 };
161}
Definition CommandBuffer.hpp:21
Definition ComputePipeline.hpp:18
Definition Core.hpp:31
Definition DepthStencilTarget.hpp:15
Definition DescriptorSetLayout.hpp:11
Definition DescriptorSet.hpp:11
Definition Framebuffer.hpp:19
Definition GraphicsPipeline.hpp:18
Definition IndexBuffer.hpp:11
Definition RenderPass.hpp:27
Definition RenderTarget.hpp:12
Definition Texture.hpp:40
Definition UniformBuffer.hpp:10
Definition VertexArrayObject.hpp:10
Definition VertexBuffer.hpp:130
Definition VulkanCore.hpp:31
Definition Window.cs:7