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

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

#include <Buffer.h>

Collaboration diagram for Vulkan::Buffer:

Public Member Functions

 Buffer (Buffer &&other) noexcept
Bufferoperator= (Buffer &&other) noexcept
 Buffer ()
 Default constructor - uses EngineManager singleton to get Vulkan handles.
 Buffer (VkDevice device, VkPhysicalDevice physicalDevice)
 Construct with direct Vulkan handles (decoupled from ApplicationContext)
 Buffer (Engine::Core::ApplicationContext *context)
 Construct from ApplicationContext (for engine code)
 ~Buffer ()
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.
void uploadDataAtOffset (const void *data, VkDeviceSize dataSize, VkDeviceSize offset)
 Helper function to map, copy data at an offset and unmap the buffer.
bool ensureSize (size_t requiredSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
 Ensures the buffer has at least the required size, recreating if necessary.
VkBuffer getBuffer () const
VkDeviceAddress getDeviceAddress () 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

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)

Definition at line 26 of file Buffer.h.

Constructor & Destructor Documentation

◆ Buffer() [1/4]

Vulkan::Buffer::Buffer ( Buffer && other)
inlinenoexcept

Definition at line 32 of file Buffer.h.

References Buffer().

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

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

◆ Buffer() [2/4]

Vulkan::Buffer::Buffer ( )

Default constructor - uses EngineManager singleton to get Vulkan handles.

◆ Buffer() [3/4]

Vulkan::Buffer::Buffer ( 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)

◆ Buffer() [4/4]

Vulkan::Buffer::Buffer ( Engine::Core::ApplicationContext * context)
explicit

Construct from ApplicationContext (for engine code)

Parameters
contextThe application context providing Vulkan handles

◆ ~Buffer()

Vulkan::Buffer::~Buffer ( )

Member Function Documentation

◆ create()

void Vulkan::Buffer::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

◆ destroy()

void Vulkan::Buffer::destroy ( )

Destroys all vulkan resources of the buffer.

Referenced by operator=().

Here is the caller graph for this function:

◆ ensureSize()

bool Vulkan::Buffer::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

◆ flush()

void Vulkan::Buffer::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

◆ getBuffer()

VkBuffer Vulkan::Buffer::getBuffer ( ) const
inlinenodiscard

Definition at line 192 of file Buffer.h.

References buffer.

◆ getBufferMemory()

VkDeviceMemory Vulkan::Buffer::getBufferMemory ( ) const
nodiscard

Gets the underlying VkDeviceMemory handle (for backward compatibility)

Note
Prefer using VMA directly when possible

◆ getBufferSize()

VkDeviceSize Vulkan::Buffer::getBufferSize ( ) const
inlinenodiscard

Definition at line 199 of file Buffer.h.

References size_.

◆ getDebugName()

std::string Vulkan::Buffer::getDebugName ( ) const
nodiscard

◆ getDeviceAddress()

VkDeviceAddress Vulkan::Buffer::getDeviceAddress ( ) const
nodiscard

◆ getMappedMemory()

void * Vulkan::Buffer::getMappedMemory ( ) const
inlinenodiscard

Definition at line 201 of file Buffer.h.

References mappedMemory.

◆ invalidate()

void Vulkan::Buffer::invalidate ( )
inline

Definition at line 203 of file Buffer.h.

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

◆ isMapped()

bool Vulkan::Buffer::isMapped ( ) const
inlinenodiscard

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

Returns
whether the buffer is already mapepd

Definition at line 153 of file Buffer.h.

References mappedMemory.

◆ isValid()

bool Vulkan::Buffer::isValid ( ) const
inlinenodiscard

Definition at line 202 of file Buffer.h.

References buffer.

◆ map()

void * Vulkan::Buffer::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

◆ operator=()

Buffer & Vulkan::Buffer::operator= ( Buffer && other)
inlinenoexcept

Definition at line 56 of file Buffer.h.

References allocation_, allocator_, Buffer(), buffer, destroy(), device_, isHostCoherent_, isPersistentlyMapped_, mappedMemory, physicalDevice_, and size_.

Here is the call graph for this function:

◆ overrideSize()

void Vulkan::Buffer::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 221 of file Buffer.h.

References size_.

◆ setDebugName()

void Vulkan::Buffer::setDebugName ( const std::string & name)

Sets the name shown in vulkan validation layer.

Parameters
namethe name which should be shown in validation messages

◆ unmap()

void Vulkan::Buffer::unmap ( )

Unmaps the memory if it is mapped.

◆ uploadData()

void Vulkan::Buffer::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

◆ uploadDataAtOffset()

void Vulkan::Buffer::uploadDataAtOffset ( const void * data,
VkDeviceSize dataSize,
VkDeviceSize offset )

Helper function to map, copy data at an offset and unmap the buffer.

Parameters
datathe data to upload
dataSizethe size of the data to upload
offsetthe offset within the buffer to write at

Member Data Documentation

◆ allocation_

VmaAllocation Vulkan::Buffer::allocation_ = VK_NULL_HANDLE
private

VMA allocation handle.

Definition at line 231 of file Buffer.h.

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

◆ allocator_

VmaAllocator Vulkan::Buffer::allocator_ = VK_NULL_HANDLE
private

Borrowed reference to VMA allocator.

Definition at line 228 of file Buffer.h.

Referenced by operator=().

◆ buffer

VkBuffer Vulkan::Buffer::buffer = VK_NULL_HANDLE
private

Definition at line 230 of file Buffer.h.

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

◆ debugName_

std::string Vulkan::Buffer::debugName_ = "Buffer"
private

Definition at line 224 of file Buffer.h.

◆ device_

VkDevice Vulkan::Buffer::device_ = VK_NULL_HANDLE
private

Definition at line 226 of file Buffer.h.

Referenced by operator=().

◆ isHostCoherent_

bool Vulkan::Buffer::isHostCoherent_ = false
private

True if buffer uses HOST_COHERENT memory (no flush needed)

Definition at line 237 of file Buffer.h.

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

◆ isPersistentlyMapped_

bool Vulkan::Buffer::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 241 of file Buffer.h.

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

◆ mappedMemory

void* Vulkan::Buffer::mappedMemory = VK_NULL_HANDLE
private

Definition at line 234 of file Buffer.h.

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

◆ physicalDevice_

VkPhysicalDevice Vulkan::Buffer::physicalDevice_ = VK_NULL_HANDLE
private

Definition at line 227 of file Buffer.h.

Referenced by operator=().

◆ size_

VkDeviceSize Vulkan::Buffer::size_ = 0
private

Definition at line 232 of file Buffer.h.

Referenced by getBufferSize(), invalidate(), operator=(), and overrideSize().


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