From 8c3d6bbc660b947759faf546bba9337a8723d872 Mon Sep 17 00:00:00 2001 From: Metehan Tuncbilek Date: Thu, 7 Nov 2024 19:01:01 +0300 Subject: [PATCH] memory + buffer allocation update --- Example/01-General/main.cpp | 21 ++++++++++++++++++++- Source/Engine/Vulkan/RHI/Buffer/VBuffer.cpp | 14 ++------------ Source/Engine/Vulkan/RHI/Buffer/VBuffer.h | 7 ++----- Source/Engine/Vulkan/RHI/Memory/VMemory.cpp | 4 ++++ Source/Engine/Vulkan/RHI/Memory/VMemory.h | 2 ++ 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Example/01-General/main.cpp b/Example/01-General/main.cpp index 439e152..8dd0a34 100644 --- a/Example/01-General/main.cpp +++ b/Example/01-General/main.cpp @@ -12,6 +12,20 @@ using namespace MAGE; #include +struct Vertex +{ + Math::Vec3f pos; + Math::Vec4f color; +}; + +Vector square = +{ + {{ -0.5f, -0.5f, 0.0f }, { 1.0f, 0.0f, 0.0f, 1.0f }}, + {{ -0.5f, 0.5f, 0.0f }, { 0.0f, 0.0f, 0.0f, 1.0f }}, + {{ 0.5f, 0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }}, + {{ 0.5f, -0.5f, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f }} +}; + int main() { SystemLog::Get().InitLogger(); @@ -36,9 +50,13 @@ int main() vk::PresentModeKHR::eFifoRelaxed, { 1280, 720 }, 2, &*gQueue), &*device); BufferProps bufferTestProp = BufferProps(); - bufferTestProp.sizeInBytes = MiBToByte(200); bufferTestProp.memory = device->GetAllocator()->GetAvailableMemory(AllocProps(bufferTestProp.sizeInBytes, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible)); Owned bufferTest1 = MakeOwned(bufferTestProp, &*device); + bufferTest1->Update({ square.data(), square.size() * sizeof(Vertex) }); + + bufferTestProp.memory = device->GetAllocator()->GetAvailableMemory(AllocProps(bufferTestProp.sizeInBytes, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible)); + Owned bufferTest2 = MakeOwned(bufferTestProp, &*device); + bufferTest2->Update({ square.data(), square.size() * sizeof(Vertex) }); window.Show(); while (!window.IsClosed()) @@ -47,6 +65,7 @@ int main() } window.Hide(); + bufferTest2->Destroy(); bufferTest1->Destroy(); swapchain->Destroy(); device->Destroy(); diff --git a/Source/Engine/Vulkan/RHI/Buffer/VBuffer.cpp b/Source/Engine/Vulkan/RHI/Buffer/VBuffer.cpp index 951a71c..360ec78 100644 --- a/Source/Engine/Vulkan/RHI/Buffer/VBuffer.cpp +++ b/Source/Engine/Vulkan/RHI/Buffer/VBuffer.cpp @@ -28,19 +28,9 @@ namespace MAGE Destroy(); } - void VBuffer::Map() + void VBuffer::Update(RawBuffer buffer) const { - //m_rootDevice->GetVkDevice().mapMemory(m_props.memory->m_memory, m_actualOffset, m_props.sizeInBytes, {}, m_mappedData); - } - - void VBuffer::Update(RawBuffer buffer) - { - memcpy(m_mappedData, buffer.Data(), buffer.Size()); - } - - void VBuffer::Unmap() const - { - m_rootDevice->GetVkDevice().unmapMemory(m_props.memory->m_memory); + memcpy(m_props.memory->m_mappedData + m_memoryOffset, buffer.Data(), buffer.Size()); } void VBuffer::Destroy() diff --git a/Source/Engine/Vulkan/RHI/Buffer/VBuffer.h b/Source/Engine/Vulkan/RHI/Buffer/VBuffer.h index 47e195e..661ccc3 100644 --- a/Source/Engine/Vulkan/RHI/Buffer/VBuffer.h +++ b/Source/Engine/Vulkan/RHI/Buffer/VBuffer.h @@ -37,13 +37,11 @@ namespace MAGE inline vk::Buffer GetVkBuffer() const { return m_buffer; } inline vk::DeviceMemory GetVkMemory() const { return m_props.memory->m_memory; } - inline u8* GetMappedData() const { return m_mappedData; } + inline u8* GetMappedData() const { return m_props.memory->m_mappedData; } inline u64 GetMemoryOffset() const { return m_memoryOffset; } - void Map(); - void Update(RawBuffer buffer); - void Unmap() const; + void Update(RawBuffer buffer) const; void Destroy() override final; @@ -52,6 +50,5 @@ namespace MAGE vk::Buffer m_buffer; u64 m_memoryOffset; - u8* m_mappedData; }; } \ No newline at end of file diff --git a/Source/Engine/Vulkan/RHI/Memory/VMemory.cpp b/Source/Engine/Vulkan/RHI/Memory/VMemory.cpp index aa6c4c4..587a5a5 100644 --- a/Source/Engine/Vulkan/RHI/Memory/VMemory.cpp +++ b/Source/Engine/Vulkan/RHI/Memory/VMemory.cpp @@ -17,6 +17,9 @@ namespace MAGE ErrorUtils::VkAssert(device->GetVkDevice().allocateMemory(&allocInfo, nullptr, &m_memory), "VMemory"); + if(desc.memoryType & vk::MemoryPropertyFlagBits::eHostVisible) + ErrorUtils::VkAssert(device->GetVkDevice().mapMemory(m_memory, 0, vk::WholeSize, {}, reinterpret_cast(&m_mappedData)), "VMemory"); + m_subMemories.emplace_back(desc.blockSize, false); } @@ -110,6 +113,7 @@ namespace MAGE { if (m_memory != VK_NULL_HANDLE) { + m_rootDevice->GetVkDevice().unmapMemory(m_memory); m_rootDevice->GetVkDevice().freeMemory(m_memory); m_memory = VK_NULL_HANDLE; } diff --git a/Source/Engine/Vulkan/RHI/Memory/VMemory.h b/Source/Engine/Vulkan/RHI/Memory/VMemory.h index 8099fc6..4be6bd0 100644 --- a/Source/Engine/Vulkan/RHI/Memory/VMemory.h +++ b/Source/Engine/Vulkan/RHI/Memory/VMemory.h @@ -54,5 +54,7 @@ namespace MAGE u64 m_freeSize; Vector m_subMemories; + + u8* m_mappedData; }; }