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

#include <VulkanStagedBuffer.h>

Collaboration diagram for Vulkan::StagedBuffer:

Classes

struct  PendingDeletion
 Old buffers waiting to be deleted after all frames complete. More...

Public Member Functions

 StagedBuffer ()
void create (Engine::Core::ApplicationContext *context, VkDeviceSize size, VkBufferUsageFlags usage, bool persistentMapping=false)
 Creates the staged buffer with device and staging buffers.
void destroy ()
StagedBufferSyncObjects upload (const Engine::Core::ApplicationContext *context, const void *data, VkDeviceSize size)
StagedBufferSyncObjects uploadPartial (const Engine::Core::ApplicationContext *context, const void *data, VkDeviceSize size, VkDeviceSize offset)
 Upload data to a specific offset in the buffer (for incremental updates)
void advanceStagingBuffer ()
 Advance to next staging buffer for double-buffering. Call this at the start of each frame to ensure we don't overwrite staging data that a previous frame's transfer is still reading.
bool ensureSize (VkDeviceSize requiredSize, VkBufferUsageFlags usage)
 Ensures the buffer is at least the required size. If resizing is needed, the old buffers are moved to pending deletion rather than destroyed immediately.
void processPendingDeletions ()
 Destroys all buffers that were pending deletion. Call this after all frames that might reference old buffers have completed.
bool hasPendingDeletions () const
 Checks if there are buffers pending deletion.
const Vulkan::BuffergetBuffer () const
Vulkan::BuffergetBufferRef ()
const Vulkan::BuffergetStagingBuffer () const
Vulkan::BuffergetStagingBufferRef ()
VkDeviceSize getSize () const
 Gets the current size of the device buffer.
void setDebugName (const std::string &name, std::optional< uint32_t > index=std::nullopt)

Static Public Attributes

static constexpr uint32_t STAGING_BUFFER_COUNT = 2
 Number of staging buffers for multi-buffering (matches frames in flight)

Private Attributes

Engine::Core::ApplicationContextcontext_ = nullptr
std::optional< Bufferbuffer
std::array< std::optional< Vulkan::Buffer >, STAGING_BUFFER_COUNTstagingBuffers_
uint32_t currentStagingIndex_ = 0
std::string debugName_
bool usePersistentMapping_ = false
std::vector< PendingDeletionpendingDeletions

Static Private Attributes

static constexpr uint32_t DELETION_DELAY_FRAMES = 6

Detailed Description

Definition at line 21 of file VulkanStagedBuffer.h.

Constructor & Destructor Documentation

◆ StagedBuffer()

Vulkan::StagedBuffer::StagedBuffer ( )

Member Function Documentation

◆ advanceStagingBuffer()

void Vulkan::StagedBuffer::advanceStagingBuffer ( )
inline

Advance to next staging buffer for double-buffering. Call this at the start of each frame to ensure we don't overwrite staging data that a previous frame's transfer is still reading.

Definition at line 57 of file VulkanStagedBuffer.h.

References currentStagingIndex_, and STAGING_BUFFER_COUNT.

◆ create()

void Vulkan::StagedBuffer::create ( Engine::Core::ApplicationContext * context,
VkDeviceSize size,
VkBufferUsageFlags usage,
bool persistentMapping = false )

Creates the staged buffer with device and staging buffers.

Parameters
contextApplication context
sizeInitial size in bytes
usageVulkan buffer usage flags
persistentMappingIf true, staging buffer stays mapped for faster uploads. Use for frequently updated buffers. Default is false for backwards compatibility.

◆ destroy()

void Vulkan::StagedBuffer::destroy ( )

◆ ensureSize()

bool Vulkan::StagedBuffer::ensureSize ( VkDeviceSize requiredSize,
VkBufferUsageFlags usage )

Ensures the buffer is at least the required size. If resizing is needed, the old buffers are moved to pending deletion rather than destroyed immediately.

Parameters
requiredSizeMinimum required size in bytes
usageVulkan buffer usage flags
Returns
true if the buffer was recreated (descriptor sets need updating)

◆ getBuffer()

const Vulkan::Buffer & Vulkan::StagedBuffer::getBuffer ( ) const
inlinenodiscard

Definition at line 80 of file VulkanStagedBuffer.h.

References buffer.

◆ getBufferRef()

Vulkan::Buffer & Vulkan::StagedBuffer::getBufferRef ( )
inlinenodiscard

Definition at line 81 of file VulkanStagedBuffer.h.

References buffer.

◆ getSize()

VkDeviceSize Vulkan::StagedBuffer::getSize ( ) const
nodiscard

Gets the current size of the device buffer.

Returns
Buffer size in bytes, or 0 if not created

◆ getStagingBuffer()

const Vulkan::Buffer & Vulkan::StagedBuffer::getStagingBuffer ( ) const
inlinenodiscard

Definition at line 82 of file VulkanStagedBuffer.h.

References currentStagingIndex_, and stagingBuffers_.

◆ getStagingBufferRef()

Vulkan::Buffer & Vulkan::StagedBuffer::getStagingBufferRef ( )
inlinenodiscard

Definition at line 83 of file VulkanStagedBuffer.h.

References currentStagingIndex_, and stagingBuffers_.

◆ hasPendingDeletions()

bool Vulkan::StagedBuffer::hasPendingDeletions ( ) const
inlinenodiscard

Checks if there are buffers pending deletion.

Returns
true if there are old buffers waiting to be deleted

Definition at line 78 of file VulkanStagedBuffer.h.

References pendingDeletions.

◆ processPendingDeletions()

void Vulkan::StagedBuffer::processPendingDeletions ( )

Destroys all buffers that were pending deletion. Call this after all frames that might reference old buffers have completed.

◆ setDebugName()

void Vulkan::StagedBuffer::setDebugName ( const std::string & name,
std::optional< uint32_t > index = std::nullopt )

◆ upload()

StagedBufferSyncObjects Vulkan::StagedBuffer::upload ( const Engine::Core::ApplicationContext * context,
const void * data,
VkDeviceSize size )

◆ uploadPartial()

StagedBufferSyncObjects Vulkan::StagedBuffer::uploadPartial ( const Engine::Core::ApplicationContext * context,
const void * data,
VkDeviceSize size,
VkDeviceSize offset )

Upload data to a specific offset in the buffer (for incremental updates)

Parameters
contextApplication context
dataPointer to data to upload
sizeSize of data in bytes
offsetOffset into the device buffer where data should be written
Returns
Sync objects for the transfer

Member Data Documentation

◆ buffer

std::optional<Buffer> Vulkan::StagedBuffer::buffer
private

Definition at line 95 of file VulkanStagedBuffer.h.

Referenced by getBuffer(), and getBufferRef().

◆ context_

Engine::Core::ApplicationContext* Vulkan::StagedBuffer::context_ = nullptr
private

Definition at line 94 of file VulkanStagedBuffer.h.

◆ currentStagingIndex_

uint32_t Vulkan::StagedBuffer::currentStagingIndex_ = 0
private

◆ debugName_

std::string Vulkan::StagedBuffer::debugName_
private

Definition at line 100 of file VulkanStagedBuffer.h.

◆ DELETION_DELAY_FRAMES

uint32_t Vulkan::StagedBuffer::DELETION_DELAY_FRAMES = 6
staticconstexprprivate

Number of frames to keep old buffers alive Must be > MAX_FRAMES_IN_FLIGHT to ensure GPU work completes before deletion. Using 6 to account for: 2 frames in flight + transfer/graphics queue depth + safety margin.

Definition at line 114 of file VulkanStagedBuffer.h.

◆ pendingDeletions

std::vector<PendingDeletion> Vulkan::StagedBuffer::pendingDeletions
private

Definition at line 109 of file VulkanStagedBuffer.h.

Referenced by hasPendingDeletions().

◆ STAGING_BUFFER_COUNT

uint32_t Vulkan::StagedBuffer::STAGING_BUFFER_COUNT = 2
staticconstexpr

Number of staging buffers for multi-buffering (matches frames in flight)

Definition at line 24 of file VulkanStagedBuffer.h.

Referenced by advanceStagingBuffer().

◆ stagingBuffers_

std::array<std::optional<Vulkan::Buffer>, STAGING_BUFFER_COUNT> Vulkan::StagedBuffer::stagingBuffers_
private

Double-buffered staging buffers to prevent race conditions when previous frame's transfer is still in flight

Definition at line 98 of file VulkanStagedBuffer.h.

Referenced by getStagingBuffer(), and getStagingBufferRef().

◆ usePersistentMapping_

bool Vulkan::StagedBuffer::usePersistentMapping_ = false
private

Definition at line 101 of file VulkanStagedBuffer.h.


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