|
Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
|
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions. More...
#include <VulkanRenderer.hpp>
Public Member Functions | |
| VulkanRenderer (GLFWwindow *win, const std::string &exeDir="", bool enableValidationLayers=true) | |
| Construct a new Vulkan Renderer:: Vulkan Renderer object. | |
| ~VulkanRenderer () | |
| Destroy the Vulkan Renderer object and free resources. | |
| std::string | resolveShaderPath (const std::string &originalPath) const |
| Resolves a shader path relative to the executable or CWD to support packaged/SDK builds. | |
| const std::string & | getExeDir () const |
| Gets the directory where the active executable resides. | |
| void | beginFrame () |
| Starts render pass recording for the current frame. | |
| void | endFrame () |
| Submits command buffers and presents the rendered swapchain image. | |
| void | drawMesh (const Mesh &mesh, const Material &mat, const Transform &transform) |
| Submits draw commands for a single mesh. | |
| void | drawInstances () |
| Performs drawing of instanced mesh batches. | |
| void | createInstanceBuffer (size_t maxInstances) |
| Creates the GPU buffers for batch instancing. | |
| void | updateInstanceBuffer () |
| Transfers CPU instances down to the GPU instance buffer. | |
| void | createCameraUBO () |
| Instantiates uniform buffers for camera view and projection matrices. | |
| void | updateCameraUBO (const CameraUBO &ubo) |
| Refreshes active camera matrix attributes in UBO. | |
| void | recreateSwapchain () |
| Recreates the swapchain when window resizing occurs. | |
| void | cleanup () |
| Deallocates all Vulkan rendering state and resource bindings. | |
| PipelineHandle | createPipelineForShaders (const std::string &vertPath, const std::string &fragPath) |
| Creates a graphic pipeline custom-configured with specified shader source files. | |
| void | uploadMesh (size_t id) |
| Allocates and uploads GPU buffers for a specific mesh loaded on the CPU. | |
| bool | shouldClose () const |
| Returns window close request status. | |
| float | getDeltaTime () |
| Calculates delta time since the previous frame. | |
| GLFWwindow * | getWindow () const |
| Gets GLFW window pointer. | |
| bool | getKey (int key) const |
| Checks if a key is currently pressed (executes within engine.dll context). | |
| bool | getMouseButton (int button) const |
| Checks if a mouse button is currently pressed (executes within engine.dll context). | |
| void | getMousePosition (double *xpos, double *ypos) const |
| Gets the mouse cursor position (executes within engine.dll context). | |
| void | getWindowSize (int *width, int *height) const |
| Gets the window size (executes within engine.dll context). | |
| VkRenderPass | getRenderPass () const |
| Gets Vulkan RenderPass. | |
| VkPipeline | getPipeline () const |
| Gets Vulkan Pipeline. | |
| VkPipelineLayout | getPipelineLayout () const |
| Gets Vulkan Pipeline Layout. | |
| VkExtent2D | getSwapchainExtent () const |
| Gets Swapchain Extent. | |
| VkCommandBuffer | getCurrentCommandBuffer () |
| Retrieves active Vulkan command buffer for current frame. | |
| VkCommandBuffer | beginSingleUseCommands () |
| Begins command recording on a temporary command buffer. | |
| void | endSingleUseCommands (VkCommandBuffer commandBuffer) |
| Finalizes and submits a temporary command buffer. | |
| VkDescriptorSet | getCameraDescriptorSet () const |
| Gets Camera Descriptor Set. | |
| VkDescriptorSetLayout | getTextureDescriptorSetLayout () const |
| Gets Descriptor Set Layout for Textures. | |
| VkDescriptorSet | getDefaultTextureSet () const |
| Gets Default White Texture Descriptor Set fallback. | |
| void | setActiveCamera (const glm::mat4 &viewProj, const glm::vec3 &position) |
| Configures active camera matrices. | |
| void | setActiveCamera (const glm::mat4 &projection, const glm::vec3 &position, const glm::mat4 &view) |
| Configures active camera view, projection, and position. | |
| bool | hasActiveCamera () const |
| Checks if a camera is currently active. | |
| const glm::mat4 & | getActiveCameraViewProj () const |
| Gets active camera view projection matrix. | |
| const glm::vec3 & | getActiveCameraPosition () const |
| Gets active camera position. | |
| const glm::mat4 & | getActiveCameraView () const |
| Gets active camera view matrix. | |
| const glm::mat4 & | getActiveCameraProjection () const |
| Gets active camera projection matrix. | |
| void | setGameplayCamera (const glm::mat4 &viewProj, const glm::vec3 &position) |
| const glm::mat4 & | getGameplayCameraViewProj () const |
| const glm::vec3 & | getGameplayCameraPosition () const |
Public Attributes | |
| glm::mat4 | gameplayCameraViewProj = glm::mat4(1.0f) |
| glm::vec3 | gameplayCameraPosition = glm::vec3(0.0f) |
| std::vector< std::unique_ptr< VulkanPipeline > > | pipelines |
| Storage vector of created custom Vulkan pipelines. | |
| InstanceDataSoA | instanceDataCPU |
| CPU-side instancing data in Structure-of-Arrays format. | |
| MeshSoA | meshSoA |
| Mesh database components in SoA layout. | |
| VulkanBuffer | instanceBuffer |
| Vulkan GPU buffer for instance matrices. | |
| VulkanDevice | device { true } |
| Core logical and physical Vulkan device interface. | |
| VulkanSwapchain | swapchain |
| Swapchain manager module. | |
| VulkanDescriptors | descriptors |
| Global Vulkan descriptor set management module. | |
| std::unique_ptr< ResourceManager > | resourceManager |
| Global Resource Manager instance. | |
Private Member Functions | |
| void | initVulkan () |
| Initializes main Vulkan instance, debuggers, devices, surfaces, and pipelines. | |
| void | createPipeline () |
| Prepares and compiles graphics pipeline configurations. | |
| void | createWindowSurface () |
| Couples window handle with Vulkan instance context. | |
| VkDebugUtilsMessengerCreateInfoEXT | populateDebugMessengerCreateInfo () |
| Populates the Vulkan debug messenger structure parameters. | |
| void | setupDebugMessenger () |
| Attaches validation layer logger callbacks. | |
| void | destroyDebugMessenger () |
| Cleans up messenger validation logs. | |
| void | createInstanceAndDebug () |
| Instantiates the root Vulkan API instance. | |
| void | createDeviceAndQueues () |
| Creates physical/logical devices and queue interfaces. | |
| void | setupDescriptors () |
| Initializes descriptor pools and configurations. | |
| void | createSwapchain () |
| Configures swapchain render targets. | |
| void | createBuffersAndPipelines () |
| Instantiates buffers and pipelines. | |
| void | createCommandsAndSync () |
| Sets up command buffers, pools, and sync fences. | |
| void | submitAndPresent () |
| Submits command buffers to graphic queue and schedules screen presentation. | |
Private Attributes | |
| GLFWwindow * | window = nullptr |
| Pointer to GLFW window. | |
| std::string | exeDir |
| Directory where the executable is located. | |
| VkDebugUtilsMessengerEXT | debugMessenger = VK_NULL_HANDLE |
| Debug messenger handle for validation layer callbacks. | |
| bool | enableValidationLayers = false |
| Flag denoting validation checks state. | |
| VulkanPipeline | pipeline |
| Default graphics pipeline state manager. | |
| VulkanCommandManager | cmdManager |
| Command pool and command buffer allocator. | |
| VulkanFrameSync | frameSync |
| Synchronization fences and semaphores management. | |
| VkSurfaceKHR | surface = VK_NULL_HANDLE |
| Presentation surface binding window and Vulkan instance. | |
| uint32_t | currentImageIndex = 0 |
| Swapchain index of next image to render. | |
| VulkanBuffer | cameraBuffer |
| GPU buffer for the camera UBO. | |
| VkDescriptorSet | cameraDescriptorSet = VK_NULL_HANDLE |
| descriptor set representing active camera. | |
| bool | framebufferResized = false |
| Flag checking if window resized. | |
| double | lastTime = 0.0 |
| Previous frame time stamp. | |
| glm::mat4 | activeCameraViewProj { 1.0f } |
| Active camera view proj matrix cache. | |
| glm::vec3 | activeCameraPosition { 0.0f } |
| Active camera world space position cache. | |
| glm::mat4 | activeCameraView {} |
| Active camera view matrix cache. | |
| glm::mat4 | activeCameraProjection {} |
| Active camera projection matrix cache. | |
| bool | hasActiveCameraData = false |
| Flag checking if active camera is initialized. | |
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.
| VulkanRenderer::VulkanRenderer | ( | GLFWwindow * | win, |
| const std::string & | exeDirectory = "", | ||
| bool | enableValidation = true ) |
Construct a new Vulkan Renderer:: Vulkan Renderer object.
| win | Reference window. |
| enableValidation | True to configure validation layers, false otherwise. |
| VulkanRenderer::~VulkanRenderer | ( | ) |
Destroy the Vulkan Renderer object and free resources.
Destroy the Vulkan Renderer:: Vulkan Renderer object.
| void VulkanRenderer::beginFrame | ( | ) |
Starts render pass recording for the current frame.
Prepares Vulkan contexts for command recording.
|
inline |
Begins command recording on a temporary command buffer.
| void VulkanRenderer::cleanup | ( | ) |
Deallocates all Vulkan rendering state and resource bindings.
releases allocated Vulkan resources.
|
private |
Instantiates buffers and pipelines.
Creates uniform buffers and compiles base pipeline.
| void VulkanRenderer::createCameraUBO | ( | ) |
Instantiates uniform buffers for camera view and projection matrices.
Allocates memory for Camera Uniform Buffer.
|
private |
Sets up command buffers, pools, and sync fences.
Allocates command pools and sync structures.
|
private |
Creates physical/logical devices and queue interfaces.
Selects physical GPU and configures device queues.
|
private |
Instantiates the root Vulkan API instance.
Creates Vulkan instance and hooks validation callback logs.
| void VulkanRenderer::createInstanceBuffer | ( | size_t | maxInstances | ) |
Creates the GPU buffers for batch instancing.
Allocates memory for model transformation instance buffer.
| maxInstances | Allocated size bound. |
| maxInstances | Number of maximum instances. |
|
private |
Prepares and compiles graphics pipeline configurations.
Builds the default graphics pipeline configuration.
| PipelineHandle VulkanRenderer::createPipelineForShaders | ( | const std::string & | vert, |
| const std::string & | frag ) |
Creates a graphic pipeline custom-configured with specified shader source files.
Helper utility creating a separate graphics pipeline using target shaders.
| vertPath | File path to compiled vertex shader. |
| fragPath | File path to compiled fragment shader. |
| vert | path to vertex bytecode. |
| frag | path to fragment bytecode. |
|
private |
Configures swapchain render targets.
Initializes swapchain presentation structures.
|
private |
Couples window handle with Vulkan instance context.
Creates the presentation surface coupling Vulkan and window context.
|
private |
Cleans up messenger validation logs.
Deallocates debug messenger log handle.
| void VulkanRenderer::drawInstances | ( | ) |
Performs drawing of instanced mesh batches.
| void VulkanRenderer::drawMesh | ( | const Mesh & | mesh, |
| const Material & | mat, | ||
| const Transform & | transform ) |
Submits draw commands for a single mesh.
| mesh | The target mesh. |
| mat | The applied material. |
| transform | The transformation components. |
| void VulkanRenderer::endFrame | ( | ) |
Submits command buffers and presents the rendered swapchain image.
Stops active render pass recording.
|
inline |
Finalizes and submits a temporary command buffer.
| commandBuffer | The temporary command buffer. |
|
inline |
Gets active camera position.
|
inline |
Gets active camera projection matrix.
|
inline |
Gets active camera view matrix.
|
inline |
Gets active camera view projection matrix.
|
inline |
Gets Camera Descriptor Set.
|
inline |
Retrieves active Vulkan command buffer for current frame.
| VkDescriptorSet VulkanRenderer::getDefaultTextureSet | ( | ) | const |
Gets Default White Texture Descriptor Set fallback.
Retrieves the default white texture descriptor set fallback.
| float VulkanRenderer::getDeltaTime | ( | ) |
Calculates delta time since the previous frame.
Computes frame delta time since last invocation.
|
inline |
Gets the directory where the active executable resides.
|
inline |
|
inline |
| bool VulkanRenderer::getKey | ( | int | key | ) | const |
Checks if a key is currently pressed (executes within engine.dll context).
| key | GLFW key code. |
| bool VulkanRenderer::getMouseButton | ( | int | button | ) | const |
Checks if a mouse button is currently pressed (executes within engine.dll context).
| button | GLFW mouse button code. |
| void VulkanRenderer::getMousePosition | ( | double * | xpos, |
| double * | ypos ) const |
Gets the mouse cursor position (executes within engine.dll context).
| xpos | Output X position. |
| ypos | Output Y position. |
|
inline |
Gets Vulkan Pipeline.
|
inline |
Gets Vulkan Pipeline Layout.
|
inline |
Gets Vulkan RenderPass.
|
inline |
Gets Swapchain Extent.
|
inline |
Gets Descriptor Set Layout for Textures.
|
inline |
Gets GLFW window pointer.
| void VulkanRenderer::getWindowSize | ( | int * | width, |
| int * | height ) const |
Gets the window size (executes within engine.dll context).
| width | Output width. |
| height | Output height. |
|
inline |
Checks if a camera is currently active.
|
private |
Initializes main Vulkan instance, debuggers, devices, surfaces, and pipelines.
Initializes the Vulkan API contexts.
|
private |
Populates the Vulkan debug messenger structure parameters.
Populates validation debug create info parameters.
| void VulkanRenderer::recreateSwapchain | ( | ) |
Recreates the swapchain when window resizing occurs.
Deallocates current swapchain and rebuilds it matching window dimensions.
| std::string VulkanRenderer::resolveShaderPath | ( | const std::string & | originalPath | ) | const |
Resolves a shader path relative to the executable or CWD to support packaged/SDK builds.
| originalPath | The hardcoded shader path to resolve. |
|
inline |
Configures active camera view, projection, and position.
| projection | Projection matrix. |
| position | 3D position vector. |
| view | View matrix. |
|
inline |
Configures active camera matrices.
| viewProj | View-Projection matrix. |
| position | 3D position vector. |
|
inline |
|
private |
Attaches validation layer logger callbacks.
Attaches validation logger callbacks.
|
private |
Initializes descriptor pools and configurations.
Setup descriptor layouts and allocations.
| bool VulkanRenderer::shouldClose | ( | ) | const |
Returns window close request status.
Verification if window close signals are active.
|
private |
Submits command buffers to graphic queue and schedules screen presentation.
Submits command buffers to graphics queue.
| void VulkanRenderer::updateCameraUBO | ( | const CameraUBO & | ubo | ) |
Refreshes active camera matrix attributes in UBO.
Maps and uploads camera projection matrices to GPU.
| void VulkanRenderer::updateInstanceBuffer | ( | ) |
Transfers CPU instances down to the GPU instance buffer.
Uploads instanced structures to GPU buffers.
| void VulkanRenderer::uploadMesh | ( | size_t | meshID | ) |
Allocates and uploads GPU buffers for a specific mesh loaded on the CPU.
Uploads mesh data to vertex and index buffers.
| id | Mesh ID. |
| meshID | ID of target mesh. |
|
private |
Active camera world space position cache.
|
private |
Active camera projection matrix cache.
|
private |
Active camera view matrix cache.
|
private |
Active camera view proj matrix cache.
|
private |
GPU buffer for the camera UBO.
|
private |
descriptor set representing active camera.
|
private |
Command pool and command buffer allocator.
|
private |
Swapchain index of next image to render.
|
private |
Debug messenger handle for validation layer callbacks.
| VulkanDescriptors VulkanRenderer::descriptors |
Global Vulkan descriptor set management module.
| VulkanDevice VulkanRenderer::device { true } |
Core logical and physical Vulkan device interface.
|
private |
Flag denoting validation checks state.
|
private |
Directory where the executable is located.
|
private |
Flag checking if window resized.
|
private |
Synchronization fences and semaphores management.
| glm::vec3 VulkanRenderer::gameplayCameraPosition = glm::vec3(0.0f) |
| glm::mat4 VulkanRenderer::gameplayCameraViewProj = glm::mat4(1.0f) |
|
private |
Flag checking if active camera is initialized.
| VulkanBuffer VulkanRenderer::instanceBuffer |
Vulkan GPU buffer for instance matrices.
| InstanceDataSoA VulkanRenderer::instanceDataCPU |
CPU-side instancing data in Structure-of-Arrays format.
|
private |
Previous frame time stamp.
|
private |
Default graphics pipeline state manager.
| std::vector<std::unique_ptr<VulkanPipeline> > VulkanRenderer::pipelines |
Storage vector of created custom Vulkan pipelines.
| std::unique_ptr<ResourceManager> VulkanRenderer::resourceManager |
Global Resource Manager instance.
|
private |
Presentation surface binding window and Vulkan instance.
| VulkanSwapchain VulkanRenderer::swapchain |
Swapchain manager module.
|
private |
Pointer to GLFW window.