initial commit

This commit is contained in:
Kbz-8
2025-05-01 23:03:47 +02:00
commit 5eb5821c2a
200 changed files with 434542 additions and 0 deletions

View 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

View 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

View 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