Skip to content

Commit

Permalink
memory + buffer allocation update
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuncbilek95 committed Nov 7, 2024
1 parent dbb4487 commit 8c3d6bb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
21 changes: 20 additions & 1 deletion Example/01-General/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ using namespace MAGE;

#include <iostream>

struct Vertex
{
Math::Vec3f pos;
Math::Vec4f color;
};

Vector<Vertex> 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<ConsoleSink>();
Expand All @@ -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<VBuffer> bufferTest1 = MakeOwned<VBuffer>(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<VBuffer> bufferTest2 = MakeOwned<VBuffer>(bufferTestProp, &*device);
bufferTest2->Update({ square.data(), square.size() * sizeof(Vertex) });

window.Show();
while (!window.IsClosed())
Expand All @@ -47,6 +65,7 @@ int main()
}
window.Hide();

bufferTest2->Destroy();
bufferTest1->Destroy();
swapchain->Destroy();
device->Destroy();
Expand Down
14 changes: 2 additions & 12 deletions Source/Engine/Vulkan/RHI/Buffer/VBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 2 additions & 5 deletions Source/Engine/Vulkan/RHI/Buffer/VBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -52,6 +50,5 @@ namespace MAGE

vk::Buffer m_buffer;
u64 m_memoryOffset;
u8* m_mappedData;
};
}
4 changes: 4 additions & 0 deletions Source/Engine/Vulkan/RHI/Memory/VMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void**>(&m_mappedData)), "VMemory");

m_subMemories.emplace_back(desc.blockSize, false);
}

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions Source/Engine/Vulkan/RHI/Memory/VMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ namespace MAGE
u64 m_freeSize;

Vector<SubMemory> m_subMemories;

u8* m_mappedData;
};
}

0 comments on commit 8c3d6bb

Please sign in to comment.