Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
VulkanRenderer.hpp
1/*
2Copyright (C) 2016-2021,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef AEONGAMES_VULKANRENDERER_HPP
18#define AEONGAMES_VULKANRENDERER_HPP
19
20#include <vulkan/vulkan.h>
21#include <unordered_map>
22#include <memory>
23#include <tuple>
25#include "aeongames/Renderer.hpp"
28#include "VulkanWindow.hpp"
29#include "VulkanBuffer.hpp"
30#include "VulkanMesh.hpp"
31#include "VulkanPipeline.hpp"
32#include "VulkanMaterial.hpp"
33#include "VulkanTexture.hpp"
34#include "VulkanMemoryPoolBuffer.hpp"
35
36namespace AeonGames
37{
38 class VulkanTexture;
39 class VulkanWindow;
41 class VulkanRenderer final : public Renderer
42 {
43 public:
45 VulkanRenderer ( void* aWindow );
46 ~VulkanRenderer() final;
48 const VkInstance& GetInstance() const;
50 const VkPhysicalDevice& GetPhysicalDevice() const;
52 const VkDevice& GetDevice() const;
54 const VkQueue& GetQueue() const;
56 const VkPhysicalDeviceProperties& GetPhysicalDeviceProperties() const;
58 const VkPhysicalDeviceMemoryProperties& GetPhysicalDeviceMemoryProperties() const;
60 const VkDescriptorSetLayout& GetDescriptorSetLayout ( const VkDescriptorSetLayoutCreateInfo& aDescriptorSetLayoutCreateInfo ) const;
61 //const VkDescriptorSetLayout& GetSamplerDescriptorSetLayout ( size_t aSamplerCount ) const;
63 uint32_t GetQueueFamilyIndex() const;
65 uint32_t GetMemoryTypeIndex ( VkMemoryPropertyFlags aVkMemoryPropertyFlags ) const;
67 uint32_t FindMemoryTypeIndex ( uint32_t typeFilter, VkMemoryPropertyFlags properties ) const;
69 VkCommandBuffer BeginSingleTimeCommands() const;
71 void EndSingleTimeCommands ( VkCommandBuffer commandBuffer ) const;
72 void LoadMesh ( const Mesh& aMesh ) final;
73 void UnloadMesh ( const Mesh& aMesh ) final;
74 void LoadPipeline ( const Pipeline& aPipeline ) final;
75 void UnloadPipeline ( const Pipeline& aPipeline ) final;
76 void LoadMaterial ( const Material& aMaterial ) final;
77 void UnloadMaterial ( const Material& aMaterial ) final;
78 void LoadTexture ( const Texture& aTexture ) final;
79 void UnloadTexture ( const Texture& aTexture ) final;
81 const VkDescriptorImageInfo* GetTextureDescriptorImageInfo ( const Texture& aTexture ) const;
82
83 void AttachWindow ( void* aWindowId ) final;
84 void DetachWindow ( void* aWindowId ) final;
85 void SetClearColor ( void* aWindowId, float R, float G, float B, float A ) final;
86 void SetProjectionMatrix ( void* aWindowId, const Matrix4x4& aMatrix ) final;
87 void SetViewMatrix ( void* aWindowId, const Matrix4x4& aMatrix ) final;
88 void ResizeViewport ( void* aWindowId, int32_t aX, int32_t aY, uint32_t aWidth, uint32_t aHeight ) final;
89 void BeginRender ( void* aWindowId ) final;
90 void EndRender ( void* aWindowId ) final;
91 void Render ( void* aWindowId,
92 const Matrix4x4& aModelMatrix,
93 const Mesh& aMesh,
94 const Pipeline& aPipeline,
95 const Material* aMaterial = nullptr,
96 const BufferAccessor* aSkeleton = nullptr,
98 uint32_t aVertexStart = 0,
99 uint32_t aVertexCount = 0xffffffff,
100 uint32_t aInstanceCount = 1,
101 uint32_t aFirstInstance = 0 ) const final;
102 const Frustum& GetFrustum ( void* aWindowId ) const final;
103 BufferAccessor AllocateSingleFrameUniformMemory ( void* aWindowId, size_t aSize ) final;
105 VkRenderPass GetRenderPass() const;
107 const VulkanPipeline* GetVulkanPipeline ( const Pipeline& aPipeline );
109 const VulkanMaterial* GetVulkanMaterial ( const Material& aMaterial );
111 const VulkanMesh* GetVulkanMesh ( const Mesh& aMesh );
112#if defined (VK_USE_PLATFORM_XLIB_KHR)
113 Display* GetDisplay() const;
114#endif
115 private:
116 void InitializeInstance();
117 void InitializeDevice();
118 void InitializeCommandPools();
119 void InitializeDebug();
120 void SetupLayersAndExtensions();
121 void SetupDebug();
122 void LoadFunctions();
123 void FinalizeInstance();
124 void FinalizeDevice();
125 void FinalizeCommandPools();
126 void FinalizeDebug();
127 void InitializeDescriptorSetLayout ( VkDescriptorSetLayout& aVkDescriptorSetLayout, VkDescriptorType aVkDescriptorType );
128 void FinalizeDescriptorSetLayout ( VkDescriptorSetLayout& aVkDescriptorSetLayout );
129#if defined (VK_USE_PLATFORM_XLIB_KHR)
130 Display* mDisplay {XOpenDisplay ( nullptr ) };
131#endif
132 bool mValidate { true };
133 VkInstance mVkInstance{ VK_NULL_HANDLE };
134 VkDevice mVkDevice { VK_NULL_HANDLE};
135 VkPhysicalDevice mVkPhysicalDevice{ VK_NULL_HANDLE };
136 VkPhysicalDeviceProperties mVkPhysicalDeviceProperties{};
137 VkPhysicalDeviceMemoryProperties mVkPhysicalDeviceMemoryProperties{};
138 PFN_vkCreateDebugUtilsMessengerEXT vkCreateDebugUtilsMessengerEXT{nullptr};
139 PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessengerEXT{nullptr};
140 VkDebugUtilsMessengerEXT mVkDebugUtilsMessengerEXT{VK_NULL_HANDLE};
141 VkCommandPool mVkSingleTimeCommandPool{ VK_NULL_HANDLE };
142 VkQueue mVkQueue{ VK_NULL_HANDLE };
143 mutable std::vector<std::tuple<size_t, VkDescriptorSetLayout >> mVkDescriptorSetLayouts{};
144 mutable const VulkanPipeline* mBoundPipeline{nullptr};
145 uint32_t mQueueFamilyIndex{};
146 std::vector<const char*> mInstanceLayerNames{};
147 std::vector<const char*> mInstanceExtensionNames{};
148 std::vector<const char*> mDeviceLayerNames{};
149 std::vector<const char*> mDeviceExtensionNames{};
150 // Instance Functions
151 bool mFunctionsLoaded = false;
152 std::unordered_map<size_t, VulkanMesh> mMeshStore{};
153 std::unordered_map<size_t, VulkanPipeline> mPipelineStore{};
154 std::unordered_map<size_t, VulkanMaterial> mMaterialStore{};
155 std::unordered_map<size_t, VulkanTexture> mTextureStore{};
156 std::unordered_map<void*, VulkanWindow> mWindowStore{};
157 };
158}
159#endif
Header for 4x4 matrix class.
Platform-specific macros, includes, and DLL export/import definitions.
Header for Transform component class.
Provides access to a region within a memory pool buffer.
View frustum defined by six clipping planes, extracted from a projection (or view-projection) matrix.
Definition Frustum.hpp:35
Represents a surface material with uniform properties and texture samplers.
Definition Material.hpp:38
4 by 4 matrix in colum mayor order.
Definition Matrix4x4.hpp:35
Represents a polygon mesh with vertex attributes and index data.
Definition Mesh.hpp:31
Rendering pipeline resource.
Definition Pipeline.hpp:122
Abstract base class for rendering backends.
Definition Renderer.hpp:44
Represents a 2D texture image resource.
Definition Texture.hpp:33
Vulkan material binding handler for descriptor sets and uniforms.
Vulkan mesh wrapper managing vertex and index buffer resources.
Vulkan graphics pipeline with descriptor set and push constant reflection.
uint32_t GetQueueFamilyIndex() const
Get the queue family index used for graphics operations.
void UnloadMesh(const Mesh &aMesh) final
Unloads mesh data from GPU memory.
const VkDescriptorSetLayout & GetDescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo &aDescriptorSetLayoutCreateInfo) const
Get or create a descriptor set layout matching the given create info.
void UnloadPipeline(const Pipeline &aPipeline) final
Unloads a rendering pipeline from the renderer.
const VkPhysicalDevice & GetPhysicalDevice() const
Get the physical device handle.
void LoadMesh(const Mesh &aMesh) final
Loads mesh data into GPU memory.
const VkInstance & GetInstance() const
Get the Vulkan instance handle.
void BeginRender(void *aWindowId) final
Begins a render pass for the given window surface.
const VulkanMesh * GetVulkanMesh(const Mesh &aMesh)
Get the cached VulkanMesh for a Mesh resource.
BufferAccessor AllocateSingleFrameUniformMemory(void *aWindowId, size_t aSize) final
Allocates uniform buffer memory that is valid for a single frame.
const VulkanMaterial * GetVulkanMaterial(const Material &aMaterial)
Get the cached VulkanMaterial for a Material resource.
const VkPhysicalDeviceMemoryProperties & GetPhysicalDeviceMemoryProperties() const
Get the physical device memory properties.
VkCommandBuffer BeginSingleTimeCommands() const
Begin recording a single-use command buffer.
VulkanRenderer(void *aWindow)
Construct from a native window handle.
void LoadPipeline(const Pipeline &aPipeline) final
Loads a rendering pipeline (shaders and state) into the renderer.
void SetClearColor(void *aWindowId, float R, float G, float B, float A) final
Sets the color to be used to clear the window background.
void AttachWindow(void *aWindowId) final
Attach a Window as a rendering surface.
const VkQueue & GetQueue() const
Get the graphics queue handle.
void ResizeViewport(void *aWindowId, int32_t aX, int32_t aY, uint32_t aWidth, uint32_t aHeight) final
Resizes the specific window surface's viewport.
void LoadTexture(const Texture &aTexture) final
Loads a texture into GPU memory.
void LoadMaterial(const Material &aMaterial) final
Loads material data into the renderer.
const VulkanPipeline * GetVulkanPipeline(const Pipeline &aPipeline)
Get the cached VulkanPipeline for a Pipeline resource.
uint32_t FindMemoryTypeIndex(uint32_t typeFilter, VkMemoryPropertyFlags properties) const
Find a memory type index matching both a type filter and property flags.
void SetViewMatrix(void *aWindowId, const Matrix4x4 &aMatrix) final
Sets the view matrix for a specific window surface.
void EndSingleTimeCommands(VkCommandBuffer commandBuffer) const
Submit and free a single-use command buffer.
void Render(void *aWindowId, const Matrix4x4 &aModelMatrix, const Mesh &aMesh, const Pipeline &aPipeline, const Material *aMaterial=nullptr, const BufferAccessor *aSkeleton=nullptr, Topology aTopology=Topology::TRIANGLE_LIST, uint32_t aVertexStart=0, uint32_t aVertexCount=0xffffffff, uint32_t aInstanceCount=1, uint32_t aFirstInstance=0) const final
Issues a draw call for a mesh with the given pipeline and optional material.
const VkPhysicalDeviceProperties & GetPhysicalDeviceProperties() const
Get the physical device properties.
const VkDescriptorImageInfo * GetTextureDescriptorImageInfo(const Texture &aTexture) const
Get the descriptor image info for a loaded texture.
void DetachWindow(void *aWindowId) final
Detach a Window as a rendering surface.
VkRenderPass GetRenderPass() const
Get the common Vulkan render pass.
void EndRender(void *aWindowId) final
Ends the current render pass for the given window surface.
const Frustum & GetFrustum(void *aWindowId) const final
Returns the view frustum for the given window surface.
void SetProjectionMatrix(void *aWindowId, const Matrix4x4 &aMatrix) final
Sets the projection matrix for a specific window surface.
uint32_t GetMemoryTypeIndex(VkMemoryPropertyFlags aVkMemoryPropertyFlags) const
Get a memory type index matching the requested property flags.
void UnloadTexture(const Texture &aTexture) final
Unloads a texture from GPU memory.
const VkDevice & GetDevice() const
Get the logical device handle.
void UnloadMaterial(const Material &aMaterial) final
Unloads material data from the renderer.
Vulkan texture resource wrapper with image and descriptor management.
Vulkan per-window swapchain, render pass, and rendering state.
<- This is here just for the literals
Definition AABB.hpp:31
Topology
Primitive topology types for rendering.
Definition Pipeline.hpp:38
@ TRIANGLE_LIST
Independent triangles.
Definition Pipeline.hpp:45