|
Vulkan Schnee 0.0.1
High-performance rendering engine
|
RAII wrapper for Vulkan buffer and device memory. More...
#include <VulkanBuffer.h>
Public Member Functions | |
| VulkanBuffer (VulkanBuffer &&other) noexcept | |
| VulkanBuffer & | operator= (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 |
RAII wrapper for Vulkan buffer and device memory.
Supports three construction modes:
Definition at line 29 of file VulkanBuffer.h.
|
inlinenoexcept |
Definition at line 35 of file VulkanBuffer.h.
References VulkanBuffer().
Referenced by operator=(), and VulkanBuffer().
| 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_.
| 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.
| device | The Vulkan logical device |
| physicalDevice | The Vulkan physical device (needed for memory type selection) |
Definition at line 22 of file VulkanBuffer.cpp.
References allocator_, device_, and physicalDevice_.
|
explicit |
Construct from ApplicationContext (for engine code)
| context | The application context providing Vulkan handles |
Definition at line 29 of file VulkanBuffer.cpp.
References allocator_, device_, and physicalDevice_.
| EngineCore::VulkanBuffer::~VulkanBuffer | ( | ) |
Definition at line 36 of file VulkanBuffer.cpp.
| void EngineCore::VulkanBuffer::create | ( | size_t | size, |
| VkBufferUsageFlags | usage, | ||
| VkMemoryPropertyFlags | properties ) |
Creates a buffer with a certain size, usage flags and memory properties.
| size | size of the buffer to create |
| usage | Vulkan usage flags of the buffer |
| properties | Memory property flags for the buffers memory |
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().
| void EngineCore::VulkanBuffer::destroy | ( | ) |
Destroys all vulkan resources of the buffer.
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().
| 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.
| requiredSize | The minimum required size in bytes |
| usage | Vulkan buffer usage flags for recreation |
| properties | Memory property flags for recreation |
Definition at line 212 of file VulkanBuffer.cpp.
References create(), debugName, destroy(), EngineCore::MIN_BUFFER_SIZE, setDebugName(), and size.
| 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.
| mappedOffset | The offset within the buffer which was mapped |
| mappedSize | The size of the memory range to flush |
Definition at line 175 of file VulkanBuffer.cpp.
References allocation_, allocator_, and isHostCoherent_.
Referenced by uploadData().
|
inlinenodiscard |
Definition at line 216 of file VulkanBuffer.h.
References buffer.
Referenced by Vulkan::BarrierBundle::addComputeBufferBarrier(), EngineCore::Texture::createDataUploadCommand(), EngineCore::Renderer::recordPass2Culling(), EngineCore::Renderer::recordPass2RenderPass(), EngineCore::Renderer::recordRenderPass(), EngineCore::Renderer::recordVertexShaderDraws(), EngineCore::Renderer::recordVertexShaderDrawsDepthOnly(), EngineCore::Renderer::recordVSInstancedDrawingPipeline(), EngineCore::Renderer::renderToXr(), EngineCore::Renderer::resetMeshletCullingDispatchBuffers(), and EngineCore::RenderProcess::updateComputeVSPrepareDrawDescriptorSets().
|
nodiscard |
Gets the underlying VkDeviceMemory handle (for backward compatibility)
Definition at line 201 of file VulkanBuffer.cpp.
References allocation_, and allocator_.
|
inlinenodiscard |
Definition at line 222 of file VulkanBuffer.h.
References size.
Referenced by Vulkan::BarrierBundle::addComputeBufferBarrier(), and EngineCore::RenderingDataManager::uploadMaterialBuffers().
|
nodiscard |
Definition at line 54 of file VulkanBuffer.cpp.
References debugName.
|
inlinenodiscard |
Definition at line 224 of file VulkanBuffer.h.
References mappedMemory.
|
inline |
Definition at line 226 of file VulkanBuffer.h.
References allocation_, buffer, isHostCoherent_, isPersistentlyMapped_, mappedMemory, and size.
Referenced by destroy().
|
inlinenodiscard |
Checks whether the buffer is already mapped to a memory range.
Definition at line 177 of file VulkanBuffer.h.
References mappedMemory.
Referenced by EngineCore::Texture::createDataUploadCommand(), destroy(), map(), and unmap().
|
inlinenodiscard |
Definition at line 225 of file VulkanBuffer.h.
References buffer.
| void * EngineCore::VulkanBuffer::map | ( | ) |
Maps the buffer to be writable by the cpu. The mapped memory range is also stored in the object.
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().
|
inlinenoexcept |
Definition at line 59 of file VulkanBuffer.h.
References allocation_, allocator_, buffer, destroy(), device_, isHostCoherent_, isPersistentlyMapped_, mappedMemory, physicalDevice_, size, and VulkanBuffer().
|
inline |
Overrides the size of a buffer in case the VulkanBuffer is created in place.
| newSize | size of the buffer |
Definition at line 244 of file VulkanBuffer.h.
References size.
| void EngineCore::VulkanBuffer::setDebugName | ( | const std::string & | name | ) |
Sets the name shown in vulkan validation layer.
| name | the name which should be shown in validation messages |
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().
| void EngineCore::VulkanBuffer::unmap | ( | ) |
Unmaps the memory if it is mapped.
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().
| 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.
| data | the data to upload |
| dataSize | the size of the data to upload |
Definition at line 183 of file VulkanBuffer.cpp.
References flush(), isHostCoherent_, map(), size, TRACY_ZONE_SCOPED_NAMED, and unmap().
|
private |
VMA allocation handle.
Definition at line 254 of file VulkanBuffer.h.
Referenced by create(), destroy(), flush(), getBufferMemory(), invalidate(), map(), operator=(), setDebugName(), and unmap().
|
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().
|
private |
Definition at line 30 of file VulkanBuffer.h.
|
private |
Definition at line 253 of file VulkanBuffer.h.
Referenced by create(), destroy(), getBuffer(), invalidate(), isValid(), operator=(), and setDebugName().
|
private |
Definition at line 247 of file VulkanBuffer.h.
Referenced by create(), ensureSize(), getDebugName(), map(), and setDebugName().
|
private |
Definition at line 249 of file VulkanBuffer.h.
Referenced by create(), destroy(), operator=(), setDebugName(), VulkanBuffer(), VulkanBuffer(), and VulkanBuffer().
|
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().
|
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().
|
private |
Definition at line 257 of file VulkanBuffer.h.
Referenced by create(), getMappedMemory(), invalidate(), isMapped(), map(), operator=(), and unmap().
|
private |
Definition at line 250 of file VulkanBuffer.h.
Referenced by create(), operator=(), VulkanBuffer(), VulkanBuffer(), and VulkanBuffer().
|
private |
Definition at line 255 of file VulkanBuffer.h.
Referenced by create(), ensureSize(), getBufferSize(), invalidate(), operator=(), overrideSize(), and uploadData().