KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
Karma::VulkanUniformBuffer Struct Reference

The actual uniform buffer class with implemented function using Vulkan API. More...

#include <VulkanBuffer.h>

Inheritance diagram for Karma::VulkanUniformBuffer:
Karma::UniformBufferObject

Public Member Functions

 VulkanUniformBuffer (std::vector< ShaderDataType > dataTypes, uint32_t bindingPointIndex)
 Constructor for Vulkan buffer. Calls VulkanUniformBuffer::BufferCreation().
virtual ~VulkanUniformBuffer ()
 Destructor. For freeing up UBO resources, use VulkanUniformBuffer::ClearBuffer().
void CreateBuffer (VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory)
 Creates Vulkan buffer (VKbuffer) for ubo use and allocates device memory appropriately.
const std::vector< VkBuffer > & GetUniformBuffers () const
 Getter for the uniform buffers.
void ClearBuffer ()
 Frees up the resources by destroying the buffer object(s) and clearing memory.
void BufferCreation ()
 Based on MAX_FRAMES_IN_FLIGHT (number of images (to work upon (CPU side) whilst an image is being rendered (GPU side processing)) + 1), uniform buffers are created with appropriate buffer size.
virtual void UpdateCameraUniform () override
void UploadUniformBuffer (size_t frameIndex) override
 Uploads (copies) the data to GPU buffer memory (m_UniformBuffersMemory) from host-accessible (CPU) pointer, m_UniformList, to the beginning of the mapped range.
Public Member Functions inherited from Karma::UniformBufferObject
 UniformBufferObject (std::vector< ShaderDataType > dataTypes, uint32_t bindingPointIndex)
 An agnostic constructor provided for precomputation of offset and buffer size.
virtual ~UniformBufferObject ()=default
 Pure virtual destructor and sub class should have appropriate implementation.
template<typename... T>
void UpdateUniforms (T &&... uniforms)
 Set the m_UniformList with latest supplied uniforms.
uint32_t GetBufferSize () const
const std::vector< UBODataPointer > & GetUniformList () const
const std::vector< ShaderDataType > & GetUniformDataType () const
const std::vector< uint32_t > & GetAlignedOffsets () const
const std::vector< uint32_t > & GetUniformSize () const
uint32_t GetBindingPointIndex () const

Additional Inherited Members

Static Public Member Functions inherited from Karma::UniformBufferObject
static UniformBufferObjectCreate (std::vector< ShaderDataType > dataTypes, uint32_t bindingPointIndex)
 A function for appropriate initialization of UBO based on programmer selected rendered (Vulkan or OpenGL).
Protected Member Functions inherited from Karma::UniformBufferObject
void CalculateOffsetsAndBufferSize ()
 Calculate offsets and buffer size of the uniform buffer object(s).
Protected Attributes inherited from Karma::UniformBufferObject
uint32_t m_BufferSize
 Total size of the buffer in bytes.
uint32_t m_BindingPoint
 The binding point prescribed in vertex shader for instance https://github.com/ravimohan1991/KarmaEngine/blob/5ff57f5747c19efcea050646bc2d217c99d74015/Resources/Shaders/shader.vert#L14-L18.
std::vector< UBODataPointerm_UniformList
 List of uniforms to be uploaded.
std::vector< ShaderDataTypem_UniformDataType
 List of data types (ShaderDataType) for the m_UniformList.
std::vector< uint32_t > m_UniformAlignedOffsets
 List of individual uniform boundary aligned (with multiple of individual uniform size) offsets.
std::vector< uint32_t > m_UniformSizes
 List of individual uniform sizes.

Detailed Description

The actual uniform buffer class with implemented function using Vulkan API.

Since
Karma 1.0.0

Constructor & Destructor Documentation

◆ VulkanUniformBuffer()

Karma::VulkanUniformBuffer::VulkanUniformBuffer ( std::vector< ShaderDataType > dataTypes,
uint32_t bindingPointIndex )

Constructor for Vulkan buffer. Calls VulkanUniformBuffer::BufferCreation().

Also registers the UBO with VulkanContext so that it can be updated appropriately during rendering.

Parameters
dataTypesList of data types for uniforms to be uploaded to GPU (like used in shaders), for instance https://github.com/ravimohan1991/KarmaEngine/blob/138c172ccedf31acfab982af51ae130f9a37d3bb/Application/src/KarmaApp.cpp#L39 where Mat4 are for https://github.com/ravimohan1991/KarmaEngine/blob/138c172ccedf31acfab982af51ae130f9a37d3bb/Resources/Shaders/shader.vert#L9-L13
bindingPointIndexThe binding of shader specified index
Since
Karma 1.0.0

◆ ~VulkanUniformBuffer()

Karma::VulkanUniformBuffer::~VulkanUniformBuffer ( )
virtual

Destructor. For freeing up UBO resources, use VulkanUniformBuffer::ClearBuffer().

See also
VulkanContext::ClearUBO
Since
Karma 1.0.0

Member Function Documentation

◆ BufferCreation()

void Karma::VulkanUniformBuffer::BufferCreation ( )

Based on MAX_FRAMES_IN_FLIGHT (number of images (to work upon (CPU side) whilst an image is being rendered (GPU side processing)) + 1), uniform buffers are created with appropriate buffer size.

Since
Karma 1.0.0.

◆ ClearBuffer()

void Karma::VulkanUniformBuffer::ClearBuffer ( )

Frees up the resources by destroying the buffer object(s) and clearing memory.

Since
Karma 1.0.0

◆ CreateBuffer()

void Karma::VulkanUniformBuffer::CreateBuffer ( VkDeviceSize size,
VkBufferUsageFlags usage,
VkMemoryPropertyFlags properties,
VkBuffer & buffer,
VkDeviceMemory & bufferMemory )

Creates Vulkan buffer (VKbuffer) for ubo use and allocates device memory appropriately.

Parameters
sizeThe size, in bytes, of the buffer to be created
propertiesThe demanded properties (https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkBufferUsageFlagBits.html). For instance (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
See also
VulkanUniformBuffer::BufferCreation()
Since
Karma 1.0.0

◆ GetUniformBuffers()

const std::vector< VkBuffer > & Karma::VulkanUniformBuffer::GetUniformBuffers ( ) const
inline

Getter for the uniform buffers.

Since
Karma 1.0.0

◆ UpdateCameraUniform()

void Karma::VulkanUniformBuffer::UpdateCameraUniform ( )
overridevirtual

◆ UploadUniformBuffer()

void Karma::VulkanUniformBuffer::UploadUniformBuffer ( size_t frameIndex)
overridevirtual

Uploads (copies) the data to GPU buffer memory (m_UniformBuffersMemory) from host-accessible (CPU) pointer, m_UniformList, to the beginning of the mapped range.

Basically copies the data from CPU side (uniforms) to GPU side (m_UniformBuffersMemory).

In summary, the process of updating uniform buffers in Vulkan involves the following steps:

  • Uniform data is first stored in a CPU-side structure representing the uniform buffer object.
  • The memory of the uniform buffer (a VkBuffer) is created with CPU-visible properties and mapped to a CPU pointer.
  • The updateUniformBuffer function copies the CPU-side data (like transformation matrices) into the mapped GPU buffer memory via a memcpy call.
  • This allows the GPU to read the fresh uniform data every frame during rendering without needing to re-record command buffers.

Mechanics: vkMapMemory returns a CPU-accessible pointer (data) to a specific range within m_UniformBuffersMemory[frameIndex], which is VkDeviceMemory allocated for GPU use but with VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT (see VulkanUniformBuffer::BufferCreation()). This pointer points directly into GPU memory, so memcpy(data, it.GetDataPointer(), uniformSize) overlays CPU data bytes onto that GPU buffer region

Parameters
frameIndexThe m_CurrentFrame index representing index of MAX_FRAMES_IN_FLIGHT (number of images (to work upon (CPU side) whilst an image is being rendered (GPU side processing)) + 1)
See also
VulkanRendererAPI::SubmitCommandBuffers()
Since
Karma 1.0.0

Implements Karma::UniformBufferObject.


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