From 41c249a537f8f793ec7193c0d70c5e58599d63d7 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 6 Oct 2018 16:03:25 -0700 Subject: Added VariableSizeAllocationsManager::Allocation::IsValid() method for clearer implementation --- .../GraphicsEngineVulkan/include/VulkanDynamicHeap.h | 5 ++--- Graphics/GraphicsEngineVulkan/src/VulkanDynamicHeap.cpp | 16 ++++++++-------- .../src/VulkanUtilities/VulkanMemoryManager.cpp | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) (limited to 'Graphics/GraphicsEngineVulkan') diff --git a/Graphics/GraphicsEngineVulkan/include/VulkanDynamicHeap.h b/Graphics/GraphicsEngineVulkan/include/VulkanDynamicHeap.h index 9182c599..4c162aea 100644 --- a/Graphics/GraphicsEngineVulkan/include/VulkanDynamicHeap.h +++ b/Graphics/GraphicsEngineVulkan/include/VulkanDynamicHeap.h @@ -112,8 +112,7 @@ class VulkanDynamicMemoryManager : public DynamicHeap::MasterBlockListBasedManag { public: using TBase = DynamicHeap::MasterBlockListBasedManager; - using OffsetType = TBase::OffsetType; - using TBase::InvalidOffset; + using OffsetType = TBase::OffsetType; using MasterBlock = TBase::MasterBlock; VulkanDynamicMemoryManager(IMemoryAllocator& Allocator, @@ -200,7 +199,7 @@ public: using OffsetType = VulkanDynamicMemoryManager::OffsetType; using MasterBlock = VulkanDynamicMemoryManager::MasterBlock; - static constexpr OffsetType InvalidOffset = VulkanDynamicMemoryManager::InvalidOffset; + static constexpr OffsetType InvalidOffset = static_cast(-1); size_t GetAllocatedMasterBlockCount()const {return m_MasterBlocks.size();} diff --git a/Graphics/GraphicsEngineVulkan/src/VulkanDynamicHeap.cpp b/Graphics/GraphicsEngineVulkan/src/VulkanDynamicHeap.cpp index 83a36b34..0e5f4957 100644 --- a/Graphics/GraphicsEngineVulkan/src/VulkanDynamicHeap.cpp +++ b/Graphics/GraphicsEngineVulkan/src/VulkanDynamicHeap.cpp @@ -136,7 +136,7 @@ VulkanDynamicMemoryManager::MasterBlock VulkanDynamicMemoryManager::AllocateMast } auto Block = TBase::AllocateMasterBlock(SizeInBytes, Alignment); - if (Block.UnalignedOffset == InvalidOffset) + if (!Block.IsValid()) { // Allocation failed. Try to wait for GPU to finish pending frames to release some space auto StartIdleTime = std::chrono::high_resolution_clock::now(); @@ -144,11 +144,11 @@ VulkanDynamicMemoryManager::MasterBlock VulkanDynamicMemoryManager::AllocateMast static constexpr const auto MaxIdleDuration = std::chrono::duration{60.0 / 1000.0}; // 60 ms std::chrono::duration IdleDuration; Uint32 SleepIterations = 0; - while (Block.UnalignedOffset == InvalidOffset && IdleDuration < MaxIdleDuration) + while (!Block.IsValid() && IdleDuration < MaxIdleDuration) { m_DeviceVk.PurgeReleaseQueues(); Block = TBase::AllocateMasterBlock(SizeInBytes, Alignment); - if (Block.UnalignedOffset == InvalidOffset) + if (!Block.IsValid()) { std::this_thread::sleep_for(SleepPeriod); ++SleepIterations; @@ -158,14 +158,14 @@ VulkanDynamicMemoryManager::MasterBlock VulkanDynamicMemoryManager::AllocateMast IdleDuration = std::chrono::duration_cast>(CurrTime - StartIdleTime); } - if (Block.UnalignedOffset == InvalidOffset) + if (!Block.IsValid()) { // Last resort - idle GPU (there seems to be a driver bug: vkQueueWaitIdle() deadlocks and never returns) //m_DeviceVk.IdleGPU(true); //auto LastCompletedFenceValue = m_DeviceVk.GetCompletedFenceValue(); //m_AllocationStrategy.ReleaseStaleAllocations(LastCompletedFenceValue); //Offset = m_AllocationStrategy.Allocate(SizeInBytes); - if (Block.UnalignedOffset == InvalidOffset) + if (!Block.IsValid()) { LOG_ERROR_MESSAGE("Space in dynamic heap is exausted! After idling for ", std::fixed, std::setprecision(1), IdleDuration.count()*1000.0, " ms still no space is available. Increase the size of the heap by setting EngineVkAttribs::DynamicHeapSize to a greater value or optimize dynamic resource usage"); } @@ -187,7 +187,7 @@ VulkanDynamicMemoryManager::MasterBlock VulkanDynamicMemoryManager::AllocateMast } } - if (Block.UnalignedOffset != InvalidOffset) + if (Block.IsValid()) { m_TotalPeakSize = std::max(m_TotalPeakSize, GetUsedSize()); } @@ -207,7 +207,7 @@ VulkanDynamicAllocation VulkanDynamicHeap::Allocate(Uint32 SizeInBytes, Uint32 A { // Allocate directly from the memory manager auto MasterBlock = m_GlobalDynamicMemMgr.AllocateMasterBlock(SizeInBytes, Alignment); - if (MasterBlock.UnalignedOffset != InvalidOffset) + if (MasterBlock.IsValid()) { AlignedOffset = Align(MasterBlock.UnalignedOffset, size_t{Alignment}); AlignedSize = MasterBlock.Size; @@ -220,7 +220,7 @@ VulkanDynamicAllocation VulkanDynamicHeap::Allocate(Uint32 SizeInBytes, Uint32 A if (m_CurrOffset == InvalidOffset || SizeInBytes + (Align(m_CurrOffset, size_t{Alignment}) - m_CurrOffset) > m_AvailableSize) { auto MasterBlock = m_GlobalDynamicMemMgr.AllocateMasterBlock(m_MasterBlockSize, 0); - if (MasterBlock.UnalignedOffset != InvalidOffset) + if (MasterBlock.IsValid()) { m_CurrOffset = MasterBlock.UnalignedOffset; m_MasterBlocks.emplace_back(MasterBlock); diff --git a/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanMemoryManager.cpp b/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanMemoryManager.cpp index 65e138b6..5f531c9a 100644 --- a/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanMemoryManager.cpp +++ b/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanMemoryManager.cpp @@ -78,7 +78,7 @@ VulkanMemoryAllocation VulkanMemoryPage::Allocate(VkDeviceSize size, VkDeviceSiz { std::lock_guard Lock(m_Mutex); auto Allocation = m_AllocationMgr.Allocate(size, alignment); - if (Allocation.UnalignedOffset != Diligent::VariableSizeAllocationsManager::InvalidOffset) + if (Allocation.IsValid()) { // Offset may not necessarily be aligned, but the allocation is guaranteed to be large enough // to accomodate requested alignment -- cgit v1.2.3