diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2018-09-23 02:03:07 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2018-09-23 02:03:07 +0000 |
| commit | 396c5eddcf80630a79b5174c070321080f2fa27c (patch) | |
| tree | 852eec4f1be727f8479ec75e77c42fac6d506206 /Graphics/GraphicsEngineNextGenBase | |
| parent | Reworked upload allocation release process in Vk backend (diff) | |
| download | DiligentCore-396c5eddcf80630a79b5174c070321080f2fa27c.tar.gz DiligentCore-396c5eddcf80630a79b5174c070321080f2fa27c.zip | |
Reworked vulkan dynamic heap deallocation to use main release queue
Diffstat (limited to 'Graphics/GraphicsEngineNextGenBase')
3 files changed, 174 insertions, 1 deletions
diff --git a/Graphics/GraphicsEngineNextGenBase/CMakeLists.txt b/Graphics/GraphicsEngineNextGenBase/CMakeLists.txt index 68be7ff7..a86156bb 100644 --- a/Graphics/GraphicsEngineNextGenBase/CMakeLists.txt +++ b/Graphics/GraphicsEngineNextGenBase/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required (VERSION 3.6) project(GraphicsEngineNextGenBase CXX) set(INCLUDE + include/DynamicHeap.h include/RenderDeviceNextGenBase.h ) diff --git a/Graphics/GraphicsEngineNextGenBase/include/DynamicHeap.h b/Graphics/GraphicsEngineNextGenBase/include/DynamicHeap.h new file mode 100644 index 00000000..ddd33d09 --- /dev/null +++ b/Graphics/GraphicsEngineNextGenBase/include/DynamicHeap.h @@ -0,0 +1,167 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +/// \file +/// Defines dynamic heap utilities + +#include <mutex> +#include <deque> +#include <vector> +#include "VariableSizeAllocationsManager.h" +#include "RingBuffer.h" + +namespace Diligent +{ + +namespace DynamicHeap +{ + + +// Having global ring buffer shared between all contexts is inconvinient because all contexts +// must share the same frame. Having individual ring bufer per context may result in a lot of unused +// memory. As a result, ring buffer is not currently used for dynamic memory management. +// Instead, every dynamic heap allocates pages from the global dynamic memory manager. +class MasterBlockRingBufferBasedManager +{ +public: + using OffsetType = RingBuffer::OffsetType; + using MasterBlock = RingBuffer::OffsetType; + static constexpr const OffsetType InvalidOffset = RingBuffer::InvalidOffset; + + MasterBlockRingBufferBasedManager(IMemoryAllocator& Allocator, + Uint32 Size) : + m_RingBuffer(Size, Allocator) + {} + + MasterBlockRingBufferBasedManager (const MasterBlockRingBufferBasedManager&) = delete; + MasterBlockRingBufferBasedManager ( MasterBlockRingBufferBasedManager&&) = delete; + MasterBlockRingBufferBasedManager& operator= (const MasterBlockRingBufferBasedManager&) = delete; + MasterBlockRingBufferBasedManager& operator= ( MasterBlockRingBufferBasedManager&&) = delete; + + void DiscardMasterBlocks(std::vector<MasterBlock>& /*Blocks*/, Uint64 FenceValue) + { + std::lock_guard<std::mutex> Lock(m_RingBufferMtx); + m_RingBuffer.FinishCurrentFrame(FenceValue); + } + + void ReleaseStaleBlocks(Uint64 LastCompletedFenceValue) + { + std::lock_guard<std::mutex> Lock(m_RingBufferMtx); + m_RingBuffer.ReleaseCompletedFrames(LastCompletedFenceValue); + } + + OffsetType GetSize() const { return m_RingBuffer.GetMaxSize(); } + OffsetType GetUsedSize()const { return m_RingBuffer.GetUsedSize(); } + +protected: + MasterBlock AllocateMasterBlock(OffsetType SizeInBytes, OffsetType Alignment) + { + std::lock_guard<std::mutex> Lock(m_RingBufferMtx); + return m_RingBuffer.Allocate(SizeInBytes, Alignment); + } + +private: + std::mutex m_RingBufferMtx; + RingBuffer m_RingBuffer; +}; + + +class MasterBlockListBasedManager +{ +public: + using OffsetType = VariableSizeAllocationsManager::OffsetType; + using MasterBlock = VariableSizeAllocationsManager::Allocation; + static constexpr const OffsetType InvalidOffset = VariableSizeAllocationsManager::InvalidOffset; + + MasterBlockListBasedManager(IMemoryAllocator& Allocator, + Uint32 Size) : + m_AllocationsMgr(Size, Allocator) + {} + + MasterBlockListBasedManager (const MasterBlockListBasedManager&) = delete; + MasterBlockListBasedManager ( MasterBlockListBasedManager&&) = delete; + MasterBlockListBasedManager& operator= (const MasterBlockListBasedManager&) = delete; + MasterBlockListBasedManager& operator= ( MasterBlockListBasedManager&&) = delete; + + template<typename RenderDeviceImplType> + void ReleaseMasterBlocks(std::vector<MasterBlock>& Blocks, RenderDeviceImplType& Device, Uint64 CmdQueueMask) + { + struct StaleMasterBlock + { + + MasterBlock Block; + MasterBlockListBasedManager* Mgr; + + StaleMasterBlock(MasterBlock&& _Block, MasterBlockListBasedManager* _Mgr) : + Block (std::move(_Block)), + Mgr (_Mgr) + { + } + + StaleMasterBlock (const StaleMasterBlock&) = delete; + StaleMasterBlock& operator= (const StaleMasterBlock&) = delete; + StaleMasterBlock& operator= ( StaleMasterBlock&&) = delete; + + StaleMasterBlock(StaleMasterBlock&& rhs)noexcept : + Block (std::move(rhs.Block)), + Mgr (rhs.Mgr) + { + rhs.Block = MasterBlock{}; + rhs.Mgr = nullptr; + } + + ~StaleMasterBlock() + { + if (Mgr != nullptr) + { + std::lock_guard<std::mutex> Lock(Mgr->m_AllocationsMgrMtx); + Mgr->m_AllocationsMgr.Free(std::move(Block)); + } + } + }; + for(auto& Block : Blocks) + { + Device.SafeReleaseDeviceObject(StaleMasterBlock{std::move(Block), this}, CmdQueueMask); + } + } + + OffsetType GetSize() const { return m_AllocationsMgr.GetMaxSize(); } + OffsetType GetUsedSize()const { return m_AllocationsMgr.GetUsedSize();} + +protected: + MasterBlock AllocateMasterBlock(OffsetType SizeInBytes, OffsetType Alignment) + { + std::lock_guard<std::mutex> Lock(m_AllocationsMgrMtx); + return m_AllocationsMgr.Allocate(SizeInBytes, Alignment); + } + +private: + std::mutex m_AllocationsMgrMtx; + VariableSizeAllocationsManager m_AllocationsMgr; +}; + +} // namespace DynamicHeap + +} // namespace Diligent diff --git a/Graphics/GraphicsEngineNextGenBase/include/RenderDeviceNextGenBase.h b/Graphics/GraphicsEngineNextGenBase/include/RenderDeviceNextGenBase.h index 157c0b96..c198d7a0 100644 --- a/Graphics/GraphicsEngineNextGenBase/include/RenderDeviceNextGenBase.h +++ b/Graphics/GraphicsEngineNextGenBase/include/RenderDeviceNextGenBase.h @@ -271,7 +271,12 @@ protected: if (m_CommandQueues != nullptr) { for(size_t q=0; q < m_CmdQueueCount; ++q) - m_CommandQueues[q].~CommandQueue(); + { + auto& Queue = m_CommandQueues[q]; + DEV_CHECK_ERR(Queue.ReleaseQueue.GetStaleResourceCount() == 0, "All stale resources must be released before destroying a command queue"); + DEV_CHECK_ERR(Queue.ReleaseQueue.GetPendingReleaseResourceCount() == 0, "All resources must be released before destroying a command queue"); + Queue.~CommandQueue(); + } m_RawMemAllocator.Free(m_CommandQueues); m_CommandQueues = nullptr; } |
