mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-13 15:43:34 +00:00
initial commit
This commit is contained in:
49
ScopEngine/Runtime/Includes/Renderer/Memory/Block.h
git.filemode.normal_file
49
ScopEngine/Runtime/Includes/Renderer/Memory/Block.h
git.filemode.normal_file
@@ -0,0 +1,49 @@
|
||||
#ifndef __SCOP_VULKAN_MEMORY_BLOCK__
|
||||
#define __SCOP_VULKAN_MEMORY_BLOCK__
|
||||
|
||||
#include <kvf.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
class MemoryBlock
|
||||
{
|
||||
friend class MemoryChunk;
|
||||
|
||||
public:
|
||||
MemoryBlock() = default;
|
||||
|
||||
[[nodiscard]] inline bool operator==(const MemoryBlock& rhs) const noexcept
|
||||
{
|
||||
return memory == rhs.memory &&
|
||||
offset == rhs.offset &&
|
||||
size == rhs.size &&
|
||||
free == rhs.free &&
|
||||
map == rhs.map;
|
||||
}
|
||||
|
||||
inline void Swap(MemoryBlock& rhs) noexcept
|
||||
{
|
||||
std::swap(memory, rhs.memory);
|
||||
std::swap(offset, rhs.offset);
|
||||
std::swap(size, rhs.size);
|
||||
std::swap(map, rhs.map);
|
||||
std::swap(free, rhs.free);
|
||||
}
|
||||
|
||||
~MemoryBlock() = default;
|
||||
|
||||
public:
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
VkDeviceSize offset = 0;
|
||||
VkDeviceSize size = 0;
|
||||
void* map = nullptr; // useless if it's a GPU allocation
|
||||
|
||||
private:
|
||||
bool free = false;
|
||||
};
|
||||
|
||||
constexpr MemoryBlock NULL_MEMORY_BLOCK{};
|
||||
}
|
||||
|
||||
#endif
|
||||
35
ScopEngine/Runtime/Includes/Renderer/Memory/Chunk.h
git.filemode.normal_file
35
ScopEngine/Runtime/Includes/Renderer/Memory/Chunk.h
git.filemode.normal_file
@@ -0,0 +1,35 @@
|
||||
#ifndef __SCOP_VULKAN_MEMORY_CHUNK__
|
||||
#define __SCOP_VULKAN_MEMORY_CHUNK__
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
|
||||
#include <Renderer/Memory/Block.h>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
class MemoryChunk
|
||||
{
|
||||
public:
|
||||
MemoryChunk(VkDevice device, VkPhysicalDevice physical, VkDeviceSize size, std::int32_t memory_type_index);
|
||||
|
||||
[[nodiscard]] std::optional<MemoryBlock> Allocate(VkDeviceSize size, VkDeviceSize alignment);
|
||||
void Deallocate(const MemoryBlock& block);
|
||||
[[nodiscard]] inline bool Has(const MemoryBlock& block) const noexcept { return block.memory == m_memory; }
|
||||
[[nodiscard]] inline std::int32_t GetMemoryTypeIndex() const noexcept { return m_memory_type_index; }
|
||||
|
||||
~MemoryChunk();
|
||||
|
||||
protected:
|
||||
std::vector<MemoryBlock> m_blocks;
|
||||
VkDevice m_device = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice m_physical = VK_NULL_HANDLE;
|
||||
VkDeviceMemory m_memory = VK_NULL_HANDLE;
|
||||
void* p_map = nullptr;
|
||||
VkDeviceSize m_size = 0;
|
||||
std::int32_t m_memory_type_index;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
35
ScopEngine/Runtime/Includes/Renderer/Memory/DeviceAllocator.h
git.filemode.normal_file
35
ScopEngine/Runtime/Includes/Renderer/Memory/DeviceAllocator.h
git.filemode.normal_file
@@ -0,0 +1,35 @@
|
||||
#ifndef __SCOP_VULKAN_MEMORY_DEVICE_ALLOCATOR__
|
||||
#define __SCOP_VULKAN_MEMORY_DEVICE_ALLOCATOR__
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
|
||||
#include <Renderer/Memory/Block.h>
|
||||
#include <Renderer/Memory/Chunk.h>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
class DeviceAllocator
|
||||
{
|
||||
public:
|
||||
DeviceAllocator() = default;
|
||||
|
||||
inline void AttachToDevice(VkDevice device, VkPhysicalDevice physical) noexcept { m_device = device; m_physical = physical; }
|
||||
inline void DetachFromDevice() noexcept { m_chunks.clear(); m_device = VK_NULL_HANDLE; m_physical = VK_NULL_HANDLE; }
|
||||
[[nodiscard]] inline std::size_t GetAllocationsCount() const noexcept { return m_allocations_count; }
|
||||
|
||||
[[nodiscard]] MemoryBlock Allocate(VkDeviceSize size, VkDeviceSize alignment, std::int32_t memory_type_index, bool dedicated_chunk = false);
|
||||
void Deallocate(const MemoryBlock& block);
|
||||
|
||||
~DeviceAllocator() = default;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<MemoryChunk>> m_chunks;
|
||||
VkDevice m_device = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice m_physical = VK_NULL_HANDLE;
|
||||
std::size_t m_allocations_count = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user