15 VulkanBuffer(VkDevice device, VkPhysicalDevice physicalDevice,
17 VkCommandPool commandPool, VkQueue graphicsQueue);
24 void Update(
void* data,
size_t size)
override;
29 void Unmap()
override;
37 static void InitAllocator(VkInstance instance,
38 VkPhysicalDevice physicalDevice, VkDevice device);
39 static void DestroyAllocator();
43 static void FlushPendingCopies();
70 void CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
71 VkMemoryPropertyFlags properties,
72 VkBuffer& buffer, VmaAllocation& memory,
73 VkDeviceSize* outAllocSize =
nullptr,
74 uint32_t* outMemTypeIdx =
nullptr);
77 void CopyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer,
81 uint32_t FindMemoryType(uint32_t typeFilter,
82 VkMemoryPropertyFlags properties);
84 VkDevice m_device = VK_NULL_HANDLE;
85 VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
86 VkCommandPool m_commandPool = VK_NULL_HANDLE;
87 VkQueue m_graphicsQueue = VK_NULL_HANDLE;
89 VkBuffer m_buffer = VK_NULL_HANDLE;
90 VmaAllocation m_memory = VK_NULL_HANDLE;
91 VkDeviceSize m_allocSize = 0;
92 VkDeviceSize m_bufferSize = 0;
93 VkBufferUsageFlags m_usage = 0;
94 uint32_t m_memoryTypeIndex = 0;
96 VkBuffer m_stagingBuffer = VK_NULL_HANDLE;
97 VmaAllocation m_stagingMemory = VK_NULL_HANDLE;
99 void* m_mappedData =
nullptr;
100 bool m_pendingInBatch =
false;
103 static bool s_batchingEnabled;
104 static bool s_batchActive;
105 static VkCommandBuffer s_batchCommandBuffer;
106 static VkDevice s_batchDevice;
107 static VkCommandPool s_batchCommandPool;
108 static VkQueue s_batchQueue;
109 static std::vector<PendingStagingCleanup> s_pendingCleanup;
112 static void EnsureBatchStarted(VkDevice device, VkCommandPool pool,
117 struct DeferredBufferDelete {
119 VmaAllocation memory;
121 VkDeviceSize allocSize;
122 VkBufferUsageFlags usage;
123 uint32_t memoryTypeIndex;
124 uint64_t frameNumber;
125 VkDeviceSize bufferSize;
127 static std::vector<DeferredBufferDelete> s_deferredDeletions;
128 static uint64_t s_frameNumber;
132 struct PooledBuffer {
134 VmaAllocation memory;
136 VkDeviceSize allocSize;
137 VkBufferUsageFlags usage;
138 uint32_t memoryTypeIndex;
139 VkDeviceSize bufferSize;
140 uint64_t insertFrame;
142 static std::vector<PooledBuffer> s_bufferPool;
143 static VkDeviceSize s_poolBytes;
146 static constexpr VkDeviceSize MAX_POOL_BYTES = 64 * 1024 * 1024;
147 static constexpr size_t MAX_POOL_SIZE = 128;
150 bool TryRecycleBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
151 VkMemoryPropertyFlags properties,
152 VkBuffer& buffer, VmaAllocation& memory,
153 VkDeviceSize* outAllocSize =
nullptr);
156 static void EvictPoolOverBudget();
161 static void ProcessDeferredDeletions(uint32_t maxFramesInFlight);
162 static void FlushAllDeferredDeletions();
166 static VkDeviceSize GetTotalAllocatedBytes();
167 static VkDeviceSize GetDeviceLocalHeapSize();
168 static VkDeviceSize GetDeviceLocalAllocatedBytes();
169 static void SetPhysicalDevice(VkPhysicalDevice device);
172 s_totalAllocatedBytes -= size;
173 if (memTypeIdx < s_memTypeCount)
174 s_perTypeBytes[memTypeIdx] -= size;
176 static void DumpPerFrameAllocStats();
179 static VmaAllocator s_allocator;
180 static VkDeviceSize s_totalAllocatedBytes;
181 static VkPhysicalDevice s_physicalDeviceGlobal;
182 static VkDeviceSize s_perTypeBytes[VK_MAX_MEMORY_TYPES];
183 static bool s_memTypeIsDeviceLocal[VK_MAX_MEMORY_TYPES];
184 static uint32_t s_memTypeCount;
186 static void EvictPoolForMemType(uint32_t memTypeIdx);