From 3784d0fdeccccb0ab2cc5e29fe66971f98abbb39 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Wed, 7 Oct 2026 11:51:01 -0700 Subject: [PATCH] engine backend updates to create buffer to include the vma info --- engine/src/vulkan/buffer.cpp | 7 +++++-- engine/src/vulkan/buffer.hpp | 10 ++++++---- engine/src/vulkan/device.cpp | 5 +++-- engine/src/vulkan/device.hpp | 2 +- engine/src/vulkan/image.cpp | 11 ++++++----- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/engine/src/vulkan/buffer.cpp b/engine/src/vulkan/buffer.cpp index 774eb15..7b34288 100644 --- a/engine/src/vulkan/buffer.cpp +++ b/engine/src/vulkan/buffer.cpp @@ -27,8 +27,8 @@ Buffer::Buffer(Device& device, vk::DeviceSize instanceSize, u32 instanceCount, memoryPropertyFlags{memoryPropertyFlags} { alignmentSize = getAlignment(instanceSize, minOffsetAlignment); bufferSize = alignmentSize * instanceCount; - device.createBuffer(bufferSize, usageFlags, memoryPropertyFlags, buffer, - allocation); + device.createBuffer(bufferSize, this->usageFlags, this->memoryPropertyFlags, + this->buffer, this->allocation, this->info); } Buffer::~Buffer() { @@ -62,6 +62,9 @@ void Buffer::writeToBuffer(void* data, vk::DeviceSize size, if (size == VK_WHOLE_SIZE) { memcpy(mapped, data, bufferSize); } else { + SOPHIA_ASSERT(offset + size <= bufferSize, + "write of {} bytes at offset {} overflows buffer of {} bytes", + size, offset, bufferSize); char* memOffset = (char*)mapped; memOffset += offset; memcpy(memOffset, data, size); diff --git a/engine/src/vulkan/buffer.hpp b/engine/src/vulkan/buffer.hpp index 605a402..2915824 100644 --- a/engine/src/vulkan/buffer.hpp +++ b/engine/src/vulkan/buffer.hpp @@ -36,9 +36,10 @@ class Buffer { vk::DescriptorBufferInfo descriptorInfoForIndex(int index); vk::Result invalidateIndex(int index); - vk::Buffer getBuffer() const { return buffer; } - void* getMappedMemory() const { return mapped; } - uint32_t getInstanceCount() const { return instanceCount; } + void* getMappedMemory() const { return this->mapped; } + vk::Buffer getBuffer() const { return this->buffer; } + const VmaAllocationInfo& getAllocationInfo() const { return this->info; } + u32 getInstanceCount() const { return instanceCount; } vk::DeviceSize getInstanceSize() const { return instanceSize; } vk::DeviceSize getAlignmentSize() const { return alignmentSize; } vk::BufferUsageFlags getUsageFlags() const { return usageFlags; } @@ -55,9 +56,10 @@ class Buffer { void* mapped = nullptr; vk::Buffer buffer = VK_NULL_HANDLE; VmaAllocation allocation = VK_NULL_HANDLE; + VmaAllocationInfo info; vk::DeviceSize bufferSize; - uint32_t instanceCount; + u32 instanceCount; vk::DeviceSize instanceSize; vk::DeviceSize alignmentSize; vk::BufferUsageFlags usageFlags; diff --git a/engine/src/vulkan/device.cpp b/engine/src/vulkan/device.cpp index 7509a02..d001f3d 100644 --- a/engine/src/vulkan/device.cpp +++ b/engine/src/vulkan/device.cpp @@ -184,7 +184,8 @@ vk::Format Device::findSupportedFormat( void Device::createBuffer(vk::DeviceSize size, vk::BufferUsageFlags usage, vk::MemoryPropertyFlags properties, - vk::Buffer& buffer, VmaAllocation& allocation) { + vk::Buffer& buffer, VmaAllocation& allocation, + VmaAllocationInfo& info) { vk::BufferCreateInfo bufferInfo = {}; bufferInfo.size = size; bufferInfo.usage = usage; @@ -196,7 +197,7 @@ void Device::createBuffer(vk::DeviceSize size, vk::BufferUsageFlags usage, VkBuffer rawBuffer = VK_NULL_HANDLE; VkResult result = vmaCreateBuffer( this->allocator, &static_cast(bufferInfo), - &allocInfo, &rawBuffer, &allocation, nullptr); + &allocInfo, &rawBuffer, &allocation, &info); if (result != VK_SUCCESS) { log::fatal("failed to create buffer. Error: {}", vk::to_string(static_cast(result))); diff --git a/engine/src/vulkan/device.hpp b/engine/src/vulkan/device.hpp index e8e0d08..f7389a4 100644 --- a/engine/src/vulkan/device.hpp +++ b/engine/src/vulkan/device.hpp @@ -69,7 +69,7 @@ class Device { void createBuffer(vk::DeviceSize size, vk::BufferUsageFlags usage, vk::MemoryPropertyFlags properties, vk::Buffer& buffer, - VmaAllocation& allocation); + VmaAllocation& allocation, VmaAllocationInfo& info); vk::CommandBuffer beginSingleTimeCommands(); diff --git a/engine/src/vulkan/image.cpp b/engine/src/vulkan/image.cpp index d1cd9a5..cb3ff73 100644 --- a/engine/src/vulkan/image.cpp +++ b/engine/src/vulkan/image.cpp @@ -226,11 +226,12 @@ std::unique_ptr Image::Builder::build() { vk::Buffer stagingBuffer; VmaAllocation stagingAllocation; - this->device.createBuffer(this->initialDataSize, - vk::BufferUsageFlagBits::eTransferSrc, - vk::MemoryPropertyFlagBits::eHostVisible | - vk::MemoryPropertyFlagBits::eHostCoherent, - stagingBuffer, stagingAllocation); + VmaAllocationInfo stagingAllocationInfo; + this->device.createBuffer( + this->initialDataSize, vk::BufferUsageFlagBits::eTransferSrc, + vk::MemoryPropertyFlagBits::eHostVisible | + vk::MemoryPropertyFlagBits::eHostCoherent, + stagingBuffer, stagingAllocation, stagingAllocationInfo); void* mapped = nullptr; vmaMapMemory(allocator, stagingAllocation, &mapped);