| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Gr/Vulkan/GpuMemoryManager.h>
- #include <AnKi/Util/List.h>
- namespace anki {
- class ClassInf
- {
- public:
- PtrSize m_slotSize;
- PtrSize m_chunkSize;
- };
- static constexpr Array<ClassInf, 7> CLASSES{{{256_B, 16_KB},
- {4_KB, 256_KB},
- {128_KB, 8_MB},
- {1_MB, 64_MB},
- {16_MB, 128_MB},
- {64_MB, 256_MB},
- {128_MB, 256_MB}}};
- /// Special classes for the ReBAR memory. Have that as a special case because it's so limited and needs special care.
- static constexpr Array<ClassInf, 3> REBAR_CLASSES{{{1_MB, 1_MB}, {12_MB, 12_MB}, {24_MB, 24_MB}}};
- class GpuMemoryManager::Memory final :
- public ClassGpuAllocatorMemory,
- public IntrusiveListEnabled<GpuMemoryManager::Memory>
- {
- public:
- VkDeviceMemory m_handle = VK_NULL_HANDLE;
- void* m_mappedAddress = nullptr;
- SpinLock m_mtx;
- U8 m_classIdx = MAX_U8;
- };
- class GpuMemoryManager::Interface final : public ClassGpuAllocatorInterface
- {
- public:
- GrAllocator<U8> m_alloc;
- Array<IntrusiveList<Memory>, CLASSES.getSize()> m_vacantMemory;
- Array<ClassInf, CLASSES.getSize()> m_classes = {};
- U8 m_classCount = 0;
- Mutex m_mtx;
- VkDevice m_dev = VK_NULL_HANDLE;
- U8 m_memTypeIdx = MAX_U8;
- Bool m_exposesBufferGpuAddress = false;
- Error allocate(U32 classIdx, ClassGpuAllocatorMemory*& cmem) override
- {
- ANKI_ASSERT(classIdx < m_classCount);
- Memory* mem;
- LockGuard<Mutex> lock(m_mtx);
- if(!m_vacantMemory[classIdx].isEmpty())
- {
- // Recycle
- mem = &m_vacantMemory[classIdx].getFront();
- m_vacantMemory[classIdx].popFront();
- }
- else
- {
- // Create new
- VkMemoryAllocateInfo ci = {};
- ci.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
- ci.allocationSize = m_classes[classIdx].m_chunkSize;
- ci.memoryTypeIndex = m_memTypeIdx;
- VkMemoryAllocateFlagsInfo flags = {};
- flags.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO;
- flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
- if(m_exposesBufferGpuAddress)
- {
- ci.pNext = &flags;
- }
- VkDeviceMemory memHandle;
- if(ANKI_UNLIKELY(vkAllocateMemory(m_dev, &ci, nullptr, &memHandle) < 0))
- {
- ANKI_VK_LOGF("Out of GPU memory. Mem type index %u, size %zu", m_memTypeIdx,
- m_classes[classIdx].m_chunkSize);
- }
- mem = m_alloc.newInstance<Memory>();
- mem->m_handle = memHandle;
- mem->m_classIdx = U8(classIdx);
- }
- ANKI_ASSERT(mem);
- ANKI_ASSERT(mem->m_handle);
- ANKI_ASSERT(mem->m_classIdx == classIdx);
- ANKI_ASSERT(mem->m_mappedAddress == nullptr);
- cmem = mem;
- return Error::NONE;
- }
- void free(ClassGpuAllocatorMemory* cmem) override
- {
- ANKI_ASSERT(cmem);
- Memory* mem = static_cast<Memory*>(cmem);
- ANKI_ASSERT(mem->m_handle);
- LockGuard<Mutex> lock(m_mtx);
- m_vacantMemory[mem->m_classIdx].pushBack(mem);
- // Unmap
- if(mem->m_mappedAddress)
- {
- vkUnmapMemory(m_dev, mem->m_handle);
- mem->m_mappedAddress = nullptr;
- }
- }
- U32 getClassCount() const override
- {
- return m_classCount;
- }
- void getClassInfo(U32 classIdx, PtrSize& slotSize, PtrSize& chunkSize) const override
- {
- ANKI_ASSERT(classIdx < m_classCount);
- slotSize = m_classes[classIdx].m_slotSize;
- chunkSize = m_classes[classIdx].m_chunkSize;
- }
- void collectGarbage()
- {
- LockGuard<Mutex> lock(m_mtx);
- for(U classIdx = 0; classIdx < m_classCount; ++classIdx)
- {
- while(!m_vacantMemory[classIdx].isEmpty())
- {
- Memory* mem = &m_vacantMemory[classIdx].getFront();
- m_vacantMemory[classIdx].popFront();
- if(mem->m_mappedAddress)
- {
- vkUnmapMemory(m_dev, mem->m_handle);
- }
- vkFreeMemory(m_dev, mem->m_handle, nullptr);
- m_alloc.deleteInstance(mem);
- }
- }
- }
- // Map memory
- void* mapMemory(ClassGpuAllocatorMemory* cmem)
- {
- ANKI_ASSERT(cmem);
- Memory* mem = static_cast<Memory*>(cmem);
- void* out;
- LockGuard<SpinLock> lock(mem->m_mtx);
- if(mem->m_mappedAddress)
- {
- out = mem->m_mappedAddress;
- }
- else
- {
- ANKI_VK_CHECKF(vkMapMemory(m_dev, mem->m_handle, 0, m_classes[mem->m_classIdx].m_chunkSize, 0, &out));
- mem->m_mappedAddress = out;
- }
- ANKI_ASSERT(out);
- return out;
- }
- };
- class GpuMemoryManager::ClassAllocator : public ClassGpuAllocator
- {
- public:
- Bool m_isDeviceMemory;
- };
- GpuMemoryManager::~GpuMemoryManager()
- {
- }
- void GpuMemoryManager::destroy()
- {
- for(U32 i = 0; i < m_ifaces.getSize(); ++i)
- {
- for(U32 j = 0; j < 2; j++)
- {
- m_ifaces[i][j].collectGarbage();
- }
- }
- m_ifaces.destroy(m_alloc);
- m_callocs.destroy(m_alloc);
- }
- void GpuMemoryManager::init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8> alloc, Bool exposeBufferGpuAddress)
- {
- ANKI_ASSERT(pdev);
- ANKI_ASSERT(dev);
- // Print some info
- ANKI_VK_LOGI("Initializing memory manager");
- for(const ClassInf& c : CLASSES)
- {
- ANKI_VK_LOGI("\tGPU mem class. Chunk size: %lu, slotSize: %lu, allocsPerChunk %lu", c.m_chunkSize, c.m_slotSize,
- c.m_chunkSize / c.m_slotSize);
- }
- vkGetPhysicalDeviceMemoryProperties(pdev, &m_memoryProperties);
- m_alloc = alloc;
- m_ifaces.create(alloc, m_memoryProperties.memoryTypeCount);
- for(U32 memTypeIdx = 0; memTypeIdx < m_ifaces.getSize(); ++memTypeIdx)
- {
- for(U32 linear = 0; linear < 2; ++linear)
- {
- Interface& iface = m_ifaces[memTypeIdx][linear];
- iface.m_alloc = alloc;
- iface.m_dev = dev;
- iface.m_memTypeIdx = U8(memTypeIdx);
- iface.m_exposesBufferGpuAddress = (linear == 1) && exposeBufferGpuAddress;
- // Find if it's ReBAR
- const VkMemoryPropertyFlags props = m_memoryProperties.memoryTypes[memTypeIdx].propertyFlags;
- const VkMemoryPropertyFlags reBarProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
- | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
- | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
- const PtrSize heapSize =
- m_memoryProperties.memoryHeaps[m_memoryProperties.memoryTypes[memTypeIdx].heapIndex].size;
- const Bool isReBar = props == reBarProps && heapSize <= 256_MB;
- if(isReBar)
- {
- ANKI_VK_LOGI("Memory type %u is ReBAR", memTypeIdx);
- }
- // Choose different classes
- if(!isReBar)
- {
- iface.m_classCount = CLASSES.getSize();
- iface.m_classes = CLASSES;
- }
- else
- {
- iface.m_classCount = REBAR_CLASSES.getSize();
- memcpy(&iface.m_classes[0], &REBAR_CLASSES[0], REBAR_CLASSES.getSizeInBytes());
- }
- }
- }
- // One allocator per linear/non-linear resources
- m_callocs.create(alloc, m_memoryProperties.memoryTypeCount);
- for(U32 memTypeIdx = 0; memTypeIdx < m_callocs.getSize(); ++memTypeIdx)
- {
- for(U32 linear = 0; linear < 2; ++linear)
- {
- m_callocs[memTypeIdx][linear].init(m_alloc, &m_ifaces[memTypeIdx][linear]);
- const U32 heapIdx = m_memoryProperties.memoryTypes[memTypeIdx].heapIndex;
- m_callocs[memTypeIdx][linear].m_isDeviceMemory =
- !!(m_memoryProperties.memoryHeaps[heapIdx].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT);
- }
- }
- }
- void GpuMemoryManager::allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, Bool linearResource,
- GpuMemoryHandle& handle)
- {
- ClassGpuAllocator& calloc = m_callocs[memTypeIdx][linearResource];
- const Error err = calloc.allocate(size, alignment, handle.m_classHandle);
- (void)err;
- handle.m_memory = static_cast<Memory*>(handle.m_classHandle.m_memory)->m_handle;
- handle.m_offset = handle.m_classHandle.m_offset;
- handle.m_linear = linearResource;
- handle.m_memTypeIdx = U8(memTypeIdx);
- }
- void GpuMemoryManager::freeMemory(GpuMemoryHandle& handle)
- {
- ANKI_ASSERT(handle);
- ClassGpuAllocator& calloc = m_callocs[handle.m_memTypeIdx][handle.m_linear];
- calloc.free(handle.m_classHandle);
- handle = {};
- }
- void* GpuMemoryManager::getMappedAddress(GpuMemoryHandle& handle)
- {
- ANKI_ASSERT(handle);
- Interface& iface = m_ifaces[handle.m_memTypeIdx][handle.m_linear];
- U8* out = static_cast<U8*>(iface.mapMemory(handle.m_classHandle.m_memory));
- return static_cast<void*>(out + handle.m_offset);
- }
- U32 GpuMemoryManager::findMemoryType(U32 resourceMemTypeBits, VkMemoryPropertyFlags preferFlags,
- VkMemoryPropertyFlags avoidFlags) const
- {
- U32 prefered = MAX_U32;
- // Iterate all mem types
- for(U32 i = 0; i < m_memoryProperties.memoryTypeCount; i++)
- {
- if(resourceMemTypeBits & (1u << i))
- {
- const VkMemoryPropertyFlags flags = m_memoryProperties.memoryTypes[i].propertyFlags;
- if((flags & preferFlags) == preferFlags && (flags & avoidFlags) == 0)
- {
- // It's the candidate we want
- if(prefered == MAX_U32)
- {
- prefered = i;
- }
- else
- {
- // On some Intel drivers there are identical memory types pointing to different heaps. Chose the
- // biggest heap
- const PtrSize crntHeapSize =
- m_memoryProperties.memoryHeaps[m_memoryProperties.memoryTypes[i].heapIndex].size;
- const PtrSize prevHeapSize =
- m_memoryProperties.memoryHeaps[m_memoryProperties.memoryTypes[prefered].heapIndex].size;
- if(crntHeapSize > prevHeapSize)
- {
- prefered = i;
- }
- }
- }
- }
- }
- return prefered;
- }
- void GpuMemoryManager::getAllocatedMemory(PtrSize& gpuMemory, PtrSize& cpuMemory) const
- {
- gpuMemory = 0;
- cpuMemory = 0;
- for(U32 memTypeIdx = 0; memTypeIdx < m_callocs.getSize(); ++memTypeIdx)
- {
- for(U32 linear = 0; linear < 2; ++linear)
- {
- if(m_callocs[memTypeIdx][linear].m_isDeviceMemory)
- {
- gpuMemory += m_callocs[memTypeIdx][linear].getAllocatedMemory();
- }
- else
- {
- cpuMemory += m_callocs[memTypeIdx][linear].getAllocatedMemory();
- }
- }
- }
- }
- } // end namespace anki
|