15 VulkanBuffer(VkDevice device, VkPhysicalDevice physicalDevice,
17 VkCommandPool commandPool, VkQueue graphicsQueue);
24 void Update(
void* data,
size_t size)
override;
31 void Unmap()
override;
41 static void InitAllocator(VkInstance instance,
42 VkPhysicalDevice physicalDevice, VkDevice device);
44 static void DestroyAllocator();
49 static void DiscardPendingCopies();
78 void CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
79 VkMemoryPropertyFlags properties, VkBuffer& buffer,
80 VmaAllocation& memory, VkDeviceSize* outAllocSize =
nullptr,
81 uint32_t* outMemTypeIdx =
nullptr);
85 void CopyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
89 uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
91 VkDevice m_device = VK_NULL_HANDLE;
92 VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
93 VkCommandPool m_commandPool = VK_NULL_HANDLE;
94 VkQueue m_graphicsQueue = VK_NULL_HANDLE;
96 VkBuffer m_buffer = VK_NULL_HANDLE;
97 VmaAllocation m_memory = VK_NULL_HANDLE;
98 VkDeviceSize m_allocSize = 0;
99 VkDeviceSize m_bufferSize = 0;
100 VkBufferUsageFlags m_usage = 0;
101 uint32_t m_memoryTypeIndex = 0;
103 VkBuffer m_stagingBuffer = VK_NULL_HANDLE;
104 VmaAllocation m_stagingMemory = VK_NULL_HANDLE;
106 void* m_mappedData =
nullptr;
109 static bool s_batchingEnabled;
110 static bool s_batchActive;
111 static VkCommandBuffer s_batchCommandBuffer;
112 static VkDevice s_batchDevice;
113 static VkCommandPool s_batchCommandPool;
114 static VkQueue s_batchQueue;
115 static std::vector<PendingStagingCleanup> s_pendingCleanup;
118 static void EnsureBatchStarted(VkDevice device, VkCommandPool pool,
124 struct DeferredBufferDelete {
126 VmaAllocation memory;
128 VkDeviceSize allocSize;
129 VkBufferUsageFlags usage;
130 uint32_t memoryTypeIndex;
131 uint64_t frameNumber;
132 VkDeviceSize bufferSize;
134 static std::vector<DeferredBufferDelete> s_deferredDeletions;
135 static uint64_t s_frameNumber;
139 struct PooledBuffer {
141 VmaAllocation memory;
143 VkDeviceSize allocSize;
144 VkBufferUsageFlags usage;
145 uint32_t memoryTypeIndex;
146 VkDeviceSize bufferSize;
147 uint64_t insertFrame;
149 static std::vector<PooledBuffer> s_bufferPool;
150 static VkDeviceSize s_poolBytes;
153 static constexpr VkDeviceSize MAX_POOL_BYTES = 64 * 1024 * 1024;
154 static constexpr size_t MAX_POOL_SIZE = 128;
157 bool TryRecycleBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
158 VkMemoryPropertyFlags properties,
159 VkBuffer& buffer, VmaAllocation& memory,
160 VkDeviceSize* outAllocSize =
nullptr);
163 static void EvictPoolOverBudget();
168 static void ProcessDeferredDeletions(uint32_t maxFramesInFlight);
169 static void FlushAllDeferredDeletions();
173 static VkDeviceSize GetTotalAllocatedBytes();
174 static VkDeviceSize GetDeviceLocalHeapSize();
175 static VkDeviceSize GetDeviceLocalAllocatedBytes();
176 static void SetPhysicalDevice(VkPhysicalDevice device);
178 s_totalAllocatedBytes -= size;
181 s_totalAllocatedBytes -= size;
182 if (memTypeIdx < s_memTypeCount)
183 s_perTypeBytes[memTypeIdx] -= size;
185 static void DumpPerFrameAllocStats();
188 static VmaAllocator s_allocator;
189 static VkDeviceSize s_totalAllocatedBytes;
190 static VkPhysicalDevice s_physicalDeviceGlobal;
191 static VkDeviceSize s_perTypeBytes[VK_MAX_MEMORY_TYPES];
192 static bool s_memTypeIsDeviceLocal[VK_MAX_MEMORY_TYPES];
193 static uint32_t s_memTypeCount;
195 static void EvictPoolForMemType(uint32_t memTypeIdx);