15 if (engine ==
nullptr)
throw std::runtime_error(
"engine is null");
30 :
device_( context->getVkDevice() )
64 VkBufferCreateInfo bufferCI{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
66 bufferCI.usage = usage;
67 bufferCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
69 VmaAllocationCreateInfo allocCI{};
70 allocCI.usage = VMA_MEMORY_USAGE_AUTO;
73 if (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
75 allocCI.flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
77 if (properties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
79 allocCI.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
82 if ((properties & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) &&
83 !(properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
85 allocCI.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
88 VmaAllocationInfo allocInfo{};
91 throw std::runtime_error(
"Failed to create VMA buffer: " +
debugName);
97 if (allocCI.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT)
103 isHostCoherent_ = (properties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0;
108 VkDeviceMemory tempMemory;
111 isHostCoherent_ = (properties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0;
126 else if (
device_ != VK_NULL_HANDLE &&
buffer != VK_NULL_HANDLE)
132 PLOGW <<
"VulkanBuffer::destroy() called without VMA - memory may be leaked";
136 PLOGW <<
"Called VulkanBuffer::destroy() with valid resources but no allocator handle. Resources could not be destroyed.";
148 PLOGE <<
"Failed to map VMA buffer: " <<
debugName;
154 PLOGW <<
"VulkanBuffer::map() called without VMA allocation";
191 void* memory =
map();
192 memcpy(memory, data, dataSize);
205 VmaAllocationInfo info{};
207 return info.deviceMemory;
209 return VK_NULL_HANDLE;
221 if (
size >= requiredSize) {
232 create(requiredSize, usage, properties);
235 if (!savedDebugName.empty()) {
#define TRACY_ZONE_SCOPED_NAMED(name)
The application context is the core class which stores the basic openxr and vulkan objects.
const VkDevice getVkDevice() const
Gets the vulkan device.
const VkPhysicalDevice & getVkPhysicalDevice() const
Gets vulkan physical device.
VmaAllocator getVmaAllocator() const
Gets the VMA allocator for memory management.
A buffer write exception is typically thrown when trying to write to a buffer which does not have eno...
EngineCore::Engine * getEngineModule()
gets the pointer to the engine object
static EngineManager & getInstance()
gets a reference to the engine manager
ApplicationContext * getContext() const
Gets the application context which stores the vulkan instance the device and queues.
bool isPersistentlyMapped_
void setDebugName(const std::string &name)
Sets the name shown in vulkan validation layer.
void flush(VkDeviceSize mappedOffset=0, VkDeviceSize mappedSize=VK_WHOLE_SIZE)
Flushes a mapped memory range to the GPU to make CPU writes visible.
void * map()
Maps the buffer to be writable by the cpu. The mapped memory range is also stored in the object.
VmaAllocator allocator_
Borrowed reference to VMA allocator.
void unmap()
Unmaps the memory if it is mapped.
void uploadData(const void *data, VkDeviceSize dataSize)
Helper function to map, copy the data and unmap the buffer. Best used for infrequent updates.
VulkanBuffer()
Default constructor - uses EngineManager singleton to get Vulkan handles.
VmaAllocation allocation_
VMA allocation handle.
bool ensureSize(size_t requiredSize, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Ensures the buffer has at least the required size, recreating if necessary.
bool isMapped() const
Checks whether the buffer is already mapped to a memory range.
std::string getDebugName() const
void destroy()
Destroys all vulkan resources of the buffer.
VkDeviceMemory getBufferMemory() const
Gets the underlying VkDeviceMemory handle (for backward compatibility)
bool isHostCoherent_
True if buffer uses HOST_COHERENT memory (no flush needed)
void create(size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
Creates a buffer with a certain size, usage flags and memory properties.
VkPhysicalDevice physicalDevice_
static void setObjectName(VkDevice device, VulkanObjectType objectHandle, const std::string &name)
static void createBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory)
Log category system implementation.
constexpr size_t MIN_BUFFER_SIZE