fixing allocator

This commit is contained in:
Kbz-8
2025-05-21 10:58:57 +02:00
parent b85dbd2524
commit 5c51e23508
8 changed files with 110 additions and 17 deletions

View File

@@ -12,13 +12,15 @@ namespace Scop
class MemoryChunk
{
public:
MemoryChunk(VkDevice device, VkPhysicalDevice physical, VkDeviceSize size, std::int32_t memory_type_index, bool is_dedicated);
MemoryChunk(VkDevice device, VkPhysicalDevice physical, VkDeviceSize size, std::int32_t memory_type_index, bool is_dedicated, std::uint32_t& vram_usage, std::uint32_t& vram_host_visible_usage);
[[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; }
[[nodiscard]] inline bool IsDedicated() const noexcept { return m_is_dedicated; }
[[nodiscard]] inline void* GetMap() const noexcept { return p_map; }
[[nodiscard]] inline VkDeviceSize GetSize() const noexcept { return m_size; }
~MemoryChunk();

View File

@@ -11,8 +11,9 @@
namespace Scop
{
constexpr std::size_t SMALL_HEAP_MAX_SIZE = (1024ULL * 1024 * 1024);
constexpr std::size_t DEFAULT_LARGE_HEAP_BLOCK_SIZE = (256ULL * 1024 * 1024);
constexpr std::size_t SMALL_HEAP_MAX_SIZE = (1024ULL * 1024 * 1024); // 1GB
constexpr std::size_t DEFAULT_LARGE_HEAP_BLOCK_SIZE = (256ULL * 1024 * 1024); // 256MiB
constexpr std::uint32_t NEW_BLOCK_SIZE_SHIFT_MAX = 3;
class DeviceAllocator
{
@@ -26,6 +27,9 @@ namespace Scop
[[nodiscard]] MemoryBlock Allocate(VkDeviceSize size, VkDeviceSize alignment, std::int32_t memory_type_index, bool dedicated_chunk = false);
void Deallocate(const MemoryBlock& block);
[[nodiscard]] inline std::uint32_t GetVramUsage() const noexcept { return m_vram_usage; }
[[nodiscard]] inline std::uint32_t GetVramHostVisibleUsage() const noexcept { return m_vram_host_visible_usage; }
~DeviceAllocator() = default;
private:
@@ -39,6 +43,9 @@ namespace Scop
std::size_t m_allocations_count = 0;
std::mutex m_alloc_mutex;
std::mutex m_dealloc_mutex;
std::uint32_t m_vram_usage = 0;
std::uint32_t m_vram_host_visible_usage = 0;
bool m_last_chunk_creation_failed = false;
};
}