Polyphase Game Engine
Loading...
Searching...
No Matches
VramAllocator.h
Go to the documentation of this file.
1#pragma once
2
3#if API_VULKAN
4
5#include <vulkan/vulkan.h>
6#include <vector>
7
8struct VramAllocation
9{
10 VkDeviceMemory mDeviceMemory;
11 uint32_t mType;
12 int64_t mID;
13 VkDeviceSize mSize;
14 VkDeviceSize mOffset;
15 VkDeviceSize mPaddedSize;
16
17 VramAllocation() :
18 mDeviceMemory(VK_NULL_HANDLE),
19 mType(0),
20 mID(-1),
21 mSize(0),
22 mOffset(0),
23 mPaddedSize(0)
24 {
25
26 }
27
28 bool IsValid() const
29 {
30 return mDeviceMemory != VK_NULL_HANDLE;
31 }
32};
33
34struct VramMemoryChunk
35{
36 int64_t mID;
37 uint64_t mOffset;
38 uint64_t mSize;
39 bool mFree;
40
41 VramMemoryChunk() :
42 mID(-1),
43 mOffset(0),
44 mSize(0),
45 mFree(true)
46 {
47
48 }
49};
50
51struct VramMemoryBlock
52{
53 VramMemoryChunk* AllocateChunk(uint64_t size);
54 bool FreeChunk(int64_t id);
55
56 VramMemoryBlock() :
57 mDeviceMemory(0),
58 mSize(0),
59 mAvailableMemory(0),
60 mLargestChunk(0),
61 mMemoryType(0)
62 {
63
64 }
65
66 std::vector<VramMemoryChunk> mChunks;
67 VkDeviceMemory mDeviceMemory;
68 uint64_t mSize;
69 uint64_t mAvailableMemory;
70 uint64_t mLargestChunk;
71 uint32_t mMemoryType;
72};
73
74class VramAllocator
75{
76public:
77
78 static void Alloc(uint64_t size, uint64_t alignment, uint32_t memoryType, VramAllocation& outAllocation);
79 static void Free(VramAllocation& allocation);
80
81 static uint64_t GetNumBlocksAllocated();
82 static uint64_t GetNumAllocations();
83 static uint64_t GetNumAllocatedBytes();
84
85 static const uint64_t sDefaultBlockSize;
86
87private:
88
89 static VramMemoryBlock* AllocateBlock(uint64_t newBlockSize, uint32_t memoryType);
90 static void FreeBlock(VramMemoryBlock& block);
91
92
93 static std::vector<VramMemoryBlock> sBlocks;
94 static uint64_t sNumAllocations;
95 static uint64_t sNumAllocatedBytes;
96};
97
98#endif