Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
VulkanRenderer Class Reference

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.

Detailed Description

Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.

Constructor & Destructor Documentation

◆ VulkanRenderer()

VulkanRenderer::VulkanRenderer ( GLFWwindow * win,
const std::string & exeDirectory = "",
bool enableValidation = true )

Construct a new Vulkan Renderer:: Vulkan Renderer object.

Parameters
winReference window.
enableValidationTrue to configure validation layers, false otherwise.

◆ ~VulkanRenderer()

VulkanRenderer::~VulkanRenderer ( )

Destroy the Vulkan Renderer object and free resources.

Destroy the Vulkan Renderer:: Vulkan Renderer object.

Member Function Documentation

◆ beginFrame()

void VulkanRenderer::beginFrame ( )

Starts render pass recording for the current frame.

Prepares Vulkan contexts for command recording.

◆ beginSingleUseCommands()

VkCommandBuffer VulkanRenderer::beginSingleUseCommands ( )
inline

Begins command recording on a temporary command buffer.

Returns
VkCommandBuffer handle.

◆ cleanup()

void VulkanRenderer::cleanup ( )

Deallocates all Vulkan rendering state and resource bindings.

releases allocated Vulkan resources.

◆ createBuffersAndPipelines()

void VulkanRenderer::createBuffersAndPipelines ( )
private

Instantiates buffers and pipelines.

Creates uniform buffers and compiles base pipeline.

◆ createCameraUBO()

void VulkanRenderer::createCameraUBO ( )

Instantiates uniform buffers for camera view and projection matrices.

Allocates memory for Camera Uniform Buffer.

◆ createCommandsAndSync()

void VulkanRenderer::createCommandsAndSync ( )
private

Sets up command buffers, pools, and sync fences.

Allocates command pools and sync structures.

◆ createDeviceAndQueues()

void VulkanRenderer::createDeviceAndQueues ( )
private

Creates physical/logical devices and queue interfaces.

Selects physical GPU and configures device queues.

◆ createInstanceAndDebug()

void VulkanRenderer::createInstanceAndDebug ( )
private

Instantiates the root Vulkan API instance.

Creates Vulkan instance and hooks validation callback logs.

◆ createInstanceBuffer()

void VulkanRenderer::createInstanceBuffer ( size_t maxInstances)

Creates the GPU buffers for batch instancing.

Allocates memory for model transformation instance buffer.

Parameters
maxInstancesAllocated size bound.
maxInstancesNumber of maximum instances.

◆ createPipeline()

void VulkanRenderer::createPipeline ( )
private

Prepares and compiles graphics pipeline configurations.

Builds the default graphics pipeline configuration.

◆ createPipelineForShaders()

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.

Parameters
vertPathFile path to compiled vertex shader.
fragPathFile path to compiled fragment shader.
Returns
PipelineHandle wrapping VkPipeline and layout.
Parameters
vertpath to vertex bytecode.
fragpath to fragment bytecode.
Returns
Pipeline handle.

◆ createSwapchain()

void VulkanRenderer::createSwapchain ( )
private

Configures swapchain render targets.

Initializes swapchain presentation structures.

◆ createWindowSurface()

void VulkanRenderer::createWindowSurface ( )
private

Couples window handle with Vulkan instance context.

Creates the presentation surface coupling Vulkan and window context.

◆ destroyDebugMessenger()

void VulkanRenderer::destroyDebugMessenger ( )
private

Cleans up messenger validation logs.

Deallocates debug messenger log handle.

◆ drawInstances()

void VulkanRenderer::drawInstances ( )

Performs drawing of instanced mesh batches.

◆ drawMesh()

void VulkanRenderer::drawMesh ( const Mesh & mesh,
const Material & mat,
const Transform & transform )

Submits draw commands for a single mesh.

Parameters
meshThe target mesh.
matThe applied material.
transformThe transformation components.

◆ endFrame()

void VulkanRenderer::endFrame ( )

Submits command buffers and presents the rendered swapchain image.

Stops active render pass recording.

◆ endSingleUseCommands()

void VulkanRenderer::endSingleUseCommands ( VkCommandBuffer commandBuffer)
inline

Finalizes and submits a temporary command buffer.

Parameters
commandBufferThe temporary command buffer.

◆ getActiveCameraPosition()

const glm::vec3 & VulkanRenderer::getActiveCameraPosition ( ) const
inline

Gets active camera position.

Returns
Vec3.

◆ getActiveCameraProjection()

const glm::mat4 & VulkanRenderer::getActiveCameraProjection ( ) const
inline

Gets active camera projection matrix.

Returns
Mat4.

◆ getActiveCameraView()

const glm::mat4 & VulkanRenderer::getActiveCameraView ( ) const
inline

Gets active camera view matrix.

Returns
Mat4.

◆ getActiveCameraViewProj()

const glm::mat4 & VulkanRenderer::getActiveCameraViewProj ( ) const
inline

Gets active camera view projection matrix.

Returns
Mat4.

◆ getCameraDescriptorSet()

VkDescriptorSet VulkanRenderer::getCameraDescriptorSet ( ) const
inline

Gets Camera Descriptor Set.

Returns
VkDescriptorSet handle.

◆ getCurrentCommandBuffer()

VkCommandBuffer VulkanRenderer::getCurrentCommandBuffer ( )
inline

Retrieves active Vulkan command buffer for current frame.

Returns
VkCommandBuffer handle.

◆ getDefaultTextureSet()

VkDescriptorSet VulkanRenderer::getDefaultTextureSet ( ) const

Gets Default White Texture Descriptor Set fallback.

Retrieves the default white texture descriptor set fallback.

Returns
VkDescriptorSet handle.

◆ getDeltaTime()

float VulkanRenderer::getDeltaTime ( )

Calculates delta time since the previous frame.

Computes frame delta time since last invocation.

Returns
Delta time in seconds.
Delta time value.

◆ getExeDir()

const std::string & VulkanRenderer::getExeDir ( ) const
inline

Gets the directory where the active executable resides.

◆ getGameplayCameraPosition()

const glm::vec3 & VulkanRenderer::getGameplayCameraPosition ( ) const
inline

◆ getGameplayCameraViewProj()

const glm::mat4 & VulkanRenderer::getGameplayCameraViewProj ( ) const
inline

◆ getKey()

bool VulkanRenderer::getKey ( int key) const

Checks if a key is currently pressed (executes within engine.dll context).

Parameters
keyGLFW key code.
Returns
True if pressed.

◆ getMouseButton()

bool VulkanRenderer::getMouseButton ( int button) const

Checks if a mouse button is currently pressed (executes within engine.dll context).

Parameters
buttonGLFW mouse button code.
Returns
True if pressed.

◆ getMousePosition()

void VulkanRenderer::getMousePosition ( double * xpos,
double * ypos ) const

Gets the mouse cursor position (executes within engine.dll context).

Parameters
xposOutput X position.
yposOutput Y position.

◆ getPipeline()

VkPipeline VulkanRenderer::getPipeline ( ) const
inline

Gets Vulkan Pipeline.

Returns
Pipeline handle.

◆ getPipelineLayout()

VkPipelineLayout VulkanRenderer::getPipelineLayout ( ) const
inline

Gets Vulkan Pipeline Layout.

Returns
PipelineLayout handle.

◆ getRenderPass()

VkRenderPass VulkanRenderer::getRenderPass ( ) const
inline

Gets Vulkan RenderPass.

Returns
RenderPass handle.

◆ getSwapchainExtent()

VkExtent2D VulkanRenderer::getSwapchainExtent ( ) const
inline

Gets Swapchain Extent.

Returns
Extent representation.

◆ getTextureDescriptorSetLayout()

VkDescriptorSetLayout VulkanRenderer::getTextureDescriptorSetLayout ( ) const
inline

Gets Descriptor Set Layout for Textures.

Returns
VkDescriptorSetLayout handle.

◆ getWindow()

GLFWwindow * VulkanRenderer::getWindow ( ) const
inline

Gets GLFW window pointer.

Returns
Pointer to GLFW window.

◆ getWindowSize()

void VulkanRenderer::getWindowSize ( int * width,
int * height ) const

Gets the window size (executes within engine.dll context).

Parameters
widthOutput width.
heightOutput height.

◆ hasActiveCamera()

bool VulkanRenderer::hasActiveCamera ( ) const
inline

Checks if a camera is currently active.

Returns
True if active camera data is loaded, false otherwise.

◆ initVulkan()

void VulkanRenderer::initVulkan ( )
private

Initializes main Vulkan instance, debuggers, devices, surfaces, and pipelines.

Initializes the Vulkan API contexts.

◆ populateDebugMessengerCreateInfo()

VkDebugUtilsMessengerCreateInfoEXT VulkanRenderer::populateDebugMessengerCreateInfo ( )
private

Populates the Vulkan debug messenger structure parameters.

Populates validation debug create info parameters.

Returns
debug create info.

◆ recreateSwapchain()

void VulkanRenderer::recreateSwapchain ( )

Recreates the swapchain when window resizing occurs.

Deallocates current swapchain and rebuilds it matching window dimensions.

◆ resolveShaderPath()

std::string VulkanRenderer::resolveShaderPath ( const std::string & originalPath) const

Resolves a shader path relative to the executable or CWD to support packaged/SDK builds.

Parameters
originalPathThe hardcoded shader path to resolve.
Returns
The resolved absolute or relative path to the shader file.

◆ setActiveCamera() [1/2]

void VulkanRenderer::setActiveCamera ( const glm::mat4 & projection,
const glm::vec3 & position,
const glm::mat4 & view )
inline

Configures active camera view, projection, and position.

Parameters
projectionProjection matrix.
position3D position vector.
viewView matrix.

◆ setActiveCamera() [2/2]

void VulkanRenderer::setActiveCamera ( const glm::mat4 & viewProj,
const glm::vec3 & position )
inline

Configures active camera matrices.

Parameters
viewProjView-Projection matrix.
position3D position vector.

◆ setGameplayCamera()

void VulkanRenderer::setGameplayCamera ( const glm::mat4 & viewProj,
const glm::vec3 & position )
inline

◆ setupDebugMessenger()

void VulkanRenderer::setupDebugMessenger ( )
private

Attaches validation layer logger callbacks.

Attaches validation logger callbacks.

◆ setupDescriptors()

void VulkanRenderer::setupDescriptors ( )
private

Initializes descriptor pools and configurations.

Setup descriptor layouts and allocations.

◆ shouldClose()

bool VulkanRenderer::shouldClose ( ) const

Returns window close request status.

Verification if window close signals are active.

Returns
True if close requested.

◆ submitAndPresent()

void VulkanRenderer::submitAndPresent ( )
private

Submits command buffers to graphic queue and schedules screen presentation.

Submits command buffers to graphics queue.

◆ updateCameraUBO()

void VulkanRenderer::updateCameraUBO ( const CameraUBO & ubo)

Refreshes active camera matrix attributes in UBO.

Maps and uploads camera projection matrices to GPU.

◆ updateInstanceBuffer()

void VulkanRenderer::updateInstanceBuffer ( )

Transfers CPU instances down to the GPU instance buffer.

Uploads instanced structures to GPU buffers.

◆ uploadMesh()

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.

Parameters
idMesh ID.
meshIDID of target mesh.

Member Data Documentation

◆ activeCameraPosition

glm::vec3 VulkanRenderer::activeCameraPosition { 0.0f }
private

Active camera world space position cache.

◆ activeCameraProjection

glm::mat4 VulkanRenderer::activeCameraProjection {}
private

Active camera projection matrix cache.

◆ activeCameraView

glm::mat4 VulkanRenderer::activeCameraView {}
private

Active camera view matrix cache.

◆ activeCameraViewProj

glm::mat4 VulkanRenderer::activeCameraViewProj { 1.0f }
private

Active camera view proj matrix cache.

◆ cameraBuffer

VulkanBuffer VulkanRenderer::cameraBuffer
private

GPU buffer for the camera UBO.

◆ cameraDescriptorSet

VkDescriptorSet VulkanRenderer::cameraDescriptorSet = VK_NULL_HANDLE
private

descriptor set representing active camera.

◆ cmdManager

VulkanCommandManager VulkanRenderer::cmdManager
private

Command pool and command buffer allocator.

◆ currentImageIndex

uint32_t VulkanRenderer::currentImageIndex = 0
private

Swapchain index of next image to render.

◆ debugMessenger

VkDebugUtilsMessengerEXT VulkanRenderer::debugMessenger = VK_NULL_HANDLE
private

Debug messenger handle for validation layer callbacks.

◆ descriptors

VulkanDescriptors VulkanRenderer::descriptors

Global Vulkan descriptor set management module.

◆ device

VulkanDevice VulkanRenderer::device { true }

Core logical and physical Vulkan device interface.

◆ enableValidationLayers

bool VulkanRenderer::enableValidationLayers = false
private

Flag denoting validation checks state.

◆ exeDir

std::string VulkanRenderer::exeDir
private

Directory where the executable is located.

◆ framebufferResized

bool VulkanRenderer::framebufferResized = false
private

Flag checking if window resized.

◆ frameSync

VulkanFrameSync VulkanRenderer::frameSync
private

Synchronization fences and semaphores management.

◆ gameplayCameraPosition

glm::vec3 VulkanRenderer::gameplayCameraPosition = glm::vec3(0.0f)

◆ gameplayCameraViewProj

glm::mat4 VulkanRenderer::gameplayCameraViewProj = glm::mat4(1.0f)

◆ hasActiveCameraData

bool VulkanRenderer::hasActiveCameraData = false
private

Flag checking if active camera is initialized.

◆ instanceBuffer

VulkanBuffer VulkanRenderer::instanceBuffer

Vulkan GPU buffer for instance matrices.

◆ instanceDataCPU

InstanceDataSoA VulkanRenderer::instanceDataCPU

CPU-side instancing data in Structure-of-Arrays format.

◆ lastTime

double VulkanRenderer::lastTime = 0.0
private

Previous frame time stamp.

◆ meshSoA

MeshSoA VulkanRenderer::meshSoA

Mesh database components in SoA layout.

◆ pipeline

VulkanPipeline VulkanRenderer::pipeline
private

Default graphics pipeline state manager.

◆ pipelines

std::vector<std::unique_ptr<VulkanPipeline> > VulkanRenderer::pipelines

Storage vector of created custom Vulkan pipelines.

◆ resourceManager

std::unique_ptr<ResourceManager> VulkanRenderer::resourceManager

Global Resource Manager instance.

◆ surface

VkSurfaceKHR VulkanRenderer::surface = VK_NULL_HANDLE
private

Presentation surface binding window and Vulkan instance.

◆ swapchain

VulkanSwapchain VulkanRenderer::swapchain

Swapchain manager module.

◆ window

GLFWwindow* VulkanRenderer::window = nullptr
private

Pointer to GLFW window.


The documentation for this class was generated from the following files: