Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
EngineCore::VulkanBuffer Class Reference

RAII wrapper for Vulkan buffer and device memory. More...

#include <VulkanBuffer.h>

Collaboration diagram for EngineCore::VulkanBuffer:

Public Member Functions

 VulkanBuffer (VulkanBuffer &&other) noexcept
VulkanBufferoperator= (VulkanBuffer &&other) noexcept
 VulkanBuffer ()
 Default constructor - uses EngineManager singleton to get Vulkan handles.
 VulkanBuffer (VkDevice device, VkPhysicalDevice physicalDevice)
 Construct with direct Vulkan handles (decoupled from ApplicationContext)
 VulkanBuffer (ApplicationContext *context)
 Construct from ApplicationContext (for engine code)
 ~VulkanBuffer ()
void setDebugName (const std::string &name)
 Sets the name shown in vulkan validation layer.
std::string getDebugName () const
void create (size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
 Creates a buffer with a certain size, usage flags and memory properties.
void destroy ()
 Destroys all vulkan resources of the buffer.
void * map ()
 Maps the buffer to be writable by the cpu. The mapped memory range is also stored in the object.
void unmap ()
 Unmaps the memory if it is mapped.
bool isMapped () const
 Checks whether the buffer is already mapped to a memory range.
void flush (VkDeviceSize mappedOffset=0, VkDeviceSize mappedSize=VK_WHOLE_SIZE)
 Flushes a mapped memory range to the GPU to make CPU writes visible.
void uploadData (const void *data, VkDeviceSize dataSize)
 Helper function to map, copy the data and unmap the buffer. Best used for infrequent updates.
bool ensureSize (size_t requiredSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
 Ensures the buffer has at least the required size, recreating if necessary.
VkBuffer getBuffer () const
VkDeviceMemory getBufferMemory () const
 Gets the underlying VkDeviceMemory handle (for backward compatibility)
VkDeviceSize getBufferSize () const
void * getMappedMemory () const
bool isValid () const
void invalidate ()
void overrideSize (VkDeviceSize newSize)
 Overrides the size of a buffer in case the VulkanBuffer is created in place.

Private Attributes

friend AssetManager
std::string debugName = "Buffer"
VkDevice device_ = VK_NULL_HANDLE
VkPhysicalDevice physicalDevice_ = VK_NULL_HANDLE
VmaAllocator allocator_ = VK_NULL_HANDLE
 Borrowed reference to VMA allocator.
VkBuffer buffer = VK_NULL_HANDLE
VmaAllocation allocation_ = VK_NULL_HANDLE
 VMA allocation handle.
VkDeviceSize size = 0
void * mappedMemory = VK_NULL_HANDLE
bool isHostCoherent_ = false
 True if buffer uses HOST_COHERENT memory (no flush needed)
bool isPersistentlyMapped_ = false

Detailed Description

RAII wrapper for Vulkan buffer and device memory.

Supports three construction modes:

  1. Direct Vulkan handles (for tests and decoupled usage)
  2. ApplicationContext pointer (for engine code)
  3. Default (uses EngineManager singleton)
Author
Konstantin Passig
Date
2026-01-12

Definition at line 29 of file VulkanBuffer.h.

Constructor & Destructor Documentation

◆ VulkanBuffer() [1/4]

EngineCore::VulkanBuffer::VulkanBuffer ( VulkanBuffer && other)
inlinenoexcept

Definition at line 35 of file VulkanBuffer.h.

References VulkanBuffer().

Referenced by operator=(), and VulkanBuffer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VulkanBuffer() [2/4]

EngineCore::VulkanBuffer::VulkanBuffer ( )

Default constructor - uses EngineManager singleton to get Vulkan handles.

Definition at line 12 of file VulkanBuffer.cpp.

References allocator_, device_, EngineCore::Engine::getContext(), EngineCore::EngineManager::getEngineModule(), EngineCore::EngineManager::getInstance(), EngineCore::ApplicationContext::getVkDevice(), EngineCore::ApplicationContext::getVkPhysicalDevice(), EngineCore::ApplicationContext::getVmaAllocator(), and physicalDevice_.

Here is the call graph for this function:

◆ VulkanBuffer() [3/4]

EngineCore::VulkanBuffer::VulkanBuffer ( VkDevice device,
VkPhysicalDevice physicalDevice )

Construct with direct Vulkan handles (decoupled from ApplicationContext)

Use this constructor for tests or when ApplicationContext is not available.

Parameters
deviceThe Vulkan logical device
physicalDeviceThe Vulkan physical device (needed for memory type selection)
Author
Konstantin Passig
Date
2026-01-12

Definition at line 22 of file VulkanBuffer.cpp.

References allocator_, device_, and physicalDevice_.

◆ VulkanBuffer() [4/4]

EngineCore::VulkanBuffer::VulkanBuffer ( ApplicationContext * context)
explicit

Construct from ApplicationContext (for engine code)

Parameters
contextThe application context providing Vulkan handles

Definition at line 29 of file VulkanBuffer.cpp.

References allocator_, device_, and physicalDevice_.

◆ ~VulkanBuffer()

EngineCore::VulkanBuffer::~VulkanBuffer ( )

Definition at line 36 of file VulkanBuffer.cpp.

Member Function Documentation

◆ create()

void EngineCore::VulkanBuffer::create ( size_t size,
VkBufferUsageFlags usage,
VkMemoryPropertyFlags properties )

Creates a buffer with a certain size, usage flags and memory properties.

Parameters
sizesize of the buffer to create
usageVulkan usage flags of the buffer
propertiesMemory property flags for the buffers memory
Date
2025-06-27
Author
Konstantin Passig

Definition at line 59 of file VulkanBuffer.cpp.

References allocation_, allocator_, buffer, VulkanHelper::createBuffer(), debugName, device_, isHostCoherent_, isPersistentlyMapped_, mappedMemory, physicalDevice_, and size.

Referenced by EngineCore::Texture::createDataUploadCommand(), ensureSize(), and EngineCore::RenderingDataManager::uploadMaterialBuffers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroy()

void EngineCore::VulkanBuffer::destroy ( )

Destroys all vulkan resources of the buffer.

Date
2025-06-27
Author
Konstantin Passig

Definition at line 117 of file VulkanBuffer.cpp.

References allocation_, allocator_, buffer, device_, invalidate(), isMapped(), and unmap().

Referenced by EngineCore::Texture::createDataUploadCommand(), ensureSize(), operator=(), and EngineCore::RenderingDataManager::uploadMaterialBuffers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ensureSize()

bool EngineCore::VulkanBuffer::ensureSize ( size_t requiredSize,
VkBufferUsageFlags usage,
VkMemoryPropertyFlags properties )

Ensures the buffer has at least the required size, recreating if necessary.

If the current buffer is smaller than requiredSize, it will be destroyed and recreated with the new size. If the buffer is already large enough, no action is taken.

Parameters
requiredSizeThe minimum required size in bytes
usageVulkan buffer usage flags for recreation
propertiesMemory property flags for recreation
Returns
true if buffer was recreated, false if existing size was sufficient
Date
2026-01-12
Author
Konstantin Passig

Definition at line 212 of file VulkanBuffer.cpp.

References create(), debugName, destroy(), EngineCore::MIN_BUFFER_SIZE, setDebugName(), and size.

Here is the call graph for this function:

◆ flush()

void EngineCore::VulkanBuffer::flush ( VkDeviceSize mappedOffset = 0,
VkDeviceSize mappedSize = VK_WHOLE_SIZE )

Flushes a mapped memory range to the GPU to make CPU writes visible.

Parameters
mappedOffsetThe offset within the buffer which was mapped
mappedSizeThe size of the memory range to flush
Date
2025-06-14
Author
Konstantin Passig

Definition at line 175 of file VulkanBuffer.cpp.

References allocation_, allocator_, and isHostCoherent_.

Referenced by uploadData().

Here is the caller graph for this function:

◆ getBuffer()

◆ getBufferMemory()

VkDeviceMemory EngineCore::VulkanBuffer::getBufferMemory ( ) const
nodiscard

Gets the underlying VkDeviceMemory handle (for backward compatibility)

Note
Prefer using VMA directly when possible

Definition at line 201 of file VulkanBuffer.cpp.

References allocation_, and allocator_.

◆ getBufferSize()

VkDeviceSize EngineCore::VulkanBuffer::getBufferSize ( ) const
inlinenodiscard

Definition at line 222 of file VulkanBuffer.h.

References size.

Referenced by Vulkan::BarrierBundle::addComputeBufferBarrier(), and EngineCore::RenderingDataManager::uploadMaterialBuffers().

Here is the caller graph for this function:

◆ getDebugName()

std::string EngineCore::VulkanBuffer::getDebugName ( ) const
nodiscard

Definition at line 54 of file VulkanBuffer.cpp.

References debugName.

◆ getMappedMemory()

void * EngineCore::VulkanBuffer::getMappedMemory ( ) const
inlinenodiscard

Definition at line 224 of file VulkanBuffer.h.

References mappedMemory.

◆ invalidate()

void EngineCore::VulkanBuffer::invalidate ( )
inline

Definition at line 226 of file VulkanBuffer.h.

References allocation_, buffer, isHostCoherent_, isPersistentlyMapped_, mappedMemory, and size.

Referenced by destroy().

Here is the caller graph for this function:

◆ isMapped()

bool EngineCore::VulkanBuffer::isMapped ( ) const
inlinenodiscard

Checks whether the buffer is already mapped to a memory range.

Returns
whether the buffer is already mapepd
Date
2025-06-14
Author
Konstantin Passig

Definition at line 177 of file VulkanBuffer.h.

References mappedMemory.

Referenced by EngineCore::Texture::createDataUploadCommand(), destroy(), map(), and unmap().

Here is the caller graph for this function:

◆ isValid()

bool EngineCore::VulkanBuffer::isValid ( ) const
inlinenodiscard

Definition at line 225 of file VulkanBuffer.h.

References buffer.

◆ map()

void * EngineCore::VulkanBuffer::map ( )

Maps the buffer to be writable by the cpu. The mapped memory range is also stored in the object.

Returns
pointer to the mapped memory range
Date
2025-06-14
Author
Konstantin Passig

Definition at line 140 of file VulkanBuffer.cpp.

References allocation_, allocator_, debugName, isMapped(), and mappedMemory.

Referenced by EngineCore::Texture::createDataUploadCommand(), uploadData(), and EngineCore::RenderingDataManager::uploadMaterialBuffers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

VulkanBuffer & EngineCore::VulkanBuffer::operator= ( VulkanBuffer && other)
inlinenoexcept

Definition at line 59 of file VulkanBuffer.h.

References allocation_, allocator_, buffer, destroy(), device_, isHostCoherent_, isPersistentlyMapped_, mappedMemory, physicalDevice_, size, and VulkanBuffer().

Here is the call graph for this function:

◆ overrideSize()

void EngineCore::VulkanBuffer::overrideSize ( VkDeviceSize newSize)
inline

Overrides the size of a buffer in case the VulkanBuffer is created in place.

Parameters
newSizesize of the buffer
Date
2025-06-27
Author
Konstantin Passig

Definition at line 244 of file VulkanBuffer.h.

References size.

◆ setDebugName()

void EngineCore::VulkanBuffer::setDebugName ( const std::string & name)

Sets the name shown in vulkan validation layer.

Parameters
namethe name which should be shown in validation messages
Date
2025-06-27
Author
Konstantin Passig

Definition at line 40 of file VulkanBuffer.cpp.

References allocation_, allocator_, buffer, debugName, device_, and VulkanHelper::setObjectName().

Referenced by EngineCore::Texture::createDataUploadCommand(), ensureSize(), and EngineCore::RenderingDataManager::uploadMaterialBuffers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unmap()

void EngineCore::VulkanBuffer::unmap ( )

Unmaps the memory if it is mapped.

Date
2025-06-14
Author
Konstantin Passig

Definition at line 160 of file VulkanBuffer.cpp.

References allocation_, allocator_, isMapped(), isPersistentlyMapped_, and mappedMemory.

Referenced by EngineCore::Texture::createDataUploadCommand(), destroy(), uploadData(), and EngineCore::RenderingDataManager::uploadMaterialBuffers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uploadData()

void EngineCore::VulkanBuffer::uploadData ( const void * data,
VkDeviceSize dataSize )

Helper function to map, copy the data and unmap the buffer. Best used for infrequent updates.

Parameters
datathe data to upload
dataSizethe size of the data to upload
Date
2025-06-14
Author
Konstantin Passig

Definition at line 183 of file VulkanBuffer.cpp.

References flush(), isHostCoherent_, map(), size, TRACY_ZONE_SCOPED_NAMED, and unmap().

Here is the call graph for this function:

Member Data Documentation

◆ allocation_

VmaAllocation EngineCore::VulkanBuffer::allocation_ = VK_NULL_HANDLE
private

VMA allocation handle.

Definition at line 254 of file VulkanBuffer.h.

Referenced by create(), destroy(), flush(), getBufferMemory(), invalidate(), map(), operator=(), setDebugName(), and unmap().

◆ allocator_

VmaAllocator EngineCore::VulkanBuffer::allocator_ = VK_NULL_HANDLE
private

Borrowed reference to VMA allocator.

Definition at line 251 of file VulkanBuffer.h.

Referenced by create(), destroy(), flush(), getBufferMemory(), map(), operator=(), setDebugName(), unmap(), VulkanBuffer(), VulkanBuffer(), and VulkanBuffer().

◆ AssetManager

friend EngineCore::VulkanBuffer::AssetManager
private

Definition at line 30 of file VulkanBuffer.h.

◆ buffer

VkBuffer EngineCore::VulkanBuffer::buffer = VK_NULL_HANDLE
private

Definition at line 253 of file VulkanBuffer.h.

Referenced by create(), destroy(), getBuffer(), invalidate(), isValid(), operator=(), and setDebugName().

◆ debugName

std::string EngineCore::VulkanBuffer::debugName = "Buffer"
private

Definition at line 247 of file VulkanBuffer.h.

Referenced by create(), ensureSize(), getDebugName(), map(), and setDebugName().

◆ device_

VkDevice EngineCore::VulkanBuffer::device_ = VK_NULL_HANDLE
private

◆ isHostCoherent_

bool EngineCore::VulkanBuffer::isHostCoherent_ = false
private

True if buffer uses HOST_COHERENT memory (no flush needed)

Definition at line 260 of file VulkanBuffer.h.

Referenced by create(), flush(), invalidate(), operator=(), and uploadData().

◆ isPersistentlyMapped_

bool EngineCore::VulkanBuffer::isPersistentlyMapped_ = false
private

True if buffer was created with VMA_ALLOCATION_CREATE_MAPPED_BIT (persistent mapping) Persistent mappings should NOT call vmaUnmapMemory

Definition at line 264 of file VulkanBuffer.h.

Referenced by create(), invalidate(), operator=(), and unmap().

◆ mappedMemory

void* EngineCore::VulkanBuffer::mappedMemory = VK_NULL_HANDLE
private

Definition at line 257 of file VulkanBuffer.h.

Referenced by create(), getMappedMemory(), invalidate(), isMapped(), map(), operator=(), and unmap().

◆ physicalDevice_

VkPhysicalDevice EngineCore::VulkanBuffer::physicalDevice_ = VK_NULL_HANDLE
private

Definition at line 250 of file VulkanBuffer.h.

Referenced by create(), operator=(), VulkanBuffer(), VulkanBuffer(), and VulkanBuffer().

◆ size

VkDeviceSize EngineCore::VulkanBuffer::size = 0
private

The documentation for this class was generated from the following files:
  • /home/magerbeton/Documents/gl3-vulkan/Engine/include/Engine/Renderer/VulkanBuffer.h
  • /home/magerbeton/Documents/gl3-vulkan/Engine/src/Engine/Renderer/VulkanBuffer.cpp