summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsTools
diff options
context:
space:
mode:
authorEgor Yusov <egor.yusov@gmail.com>2017-04-06 19:25:39 +0000
committerEgor Yusov <egor.yusov@gmail.com>2017-04-06 19:25:39 +0000
commit9fe08de5b5708dbfd455b0fccaa4f8a992b14eb2 (patch)
tree4ee2e9bef2cea7684e630ce91db40921a8379178 /Graphics/GraphicsTools
parentMerge branch 'master' of https://github.com/DiligentGraphics/DiligentCore.git (diff)
downloadDiligentCore-9fe08de5b5708dbfd455b0fccaa4f8a992b14eb2.tar.gz
DiligentCore-9fe08de5b5708dbfd455b0fccaa4f8a992b14eb2.zip
Fixed D3D12 warnings, updated copyright notice
Diffstat (limited to 'Graphics/GraphicsTools')
-rw-r--r--Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems4
-rw-r--r--Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems.filters4
-rw-r--r--Graphics/GraphicsTools/include/BasicShaderSourceStreamFactory.h2
-rw-r--r--Graphics/GraphicsTools/include/GraphicsUtilities.h2
-rw-r--r--Graphics/GraphicsTools/include/RingBuffer.h7
-rw-r--r--Graphics/GraphicsTools/include/ShaderMacroHelper.h2
-rw-r--r--Graphics/GraphicsTools/include/VariableSizeAllocationsManager.h329
-rw-r--r--Graphics/GraphicsTools/include/VariableSizeGPUAllocationsManager.h90
-rw-r--r--Graphics/GraphicsTools/include/pch.h2
-rw-r--r--Graphics/GraphicsTools/src/BasicShaderSourceStreamFactory.cpp2
-rw-r--r--Graphics/GraphicsTools/src/GraphicsUtilities.cpp2
-rw-r--r--Graphics/GraphicsTools/src/pch.cpp2
12 files changed, 436 insertions, 12 deletions
diff --git a/Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems b/Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems
index 4ca8db57..050502cc 100644
--- a/Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems
+++ b/Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems
@@ -18,8 +18,8 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\BasicShaderSourceStreamFactory.h" />
- <ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\FreeBlockListManager.h" />
- <ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\FreeBlockListManagerGPU.h" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\VariableSizeAllocationsManager.h" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\VariableSizeGPUAllocationsManager.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\GraphicsUtilities.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\pch.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\RingBuffer.h" />
diff --git a/Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems.filters b/Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems.filters
index 0c8d5aa8..8d2bdcc1 100644
--- a/Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems.filters
+++ b/Graphics/GraphicsTools/build/Windows.Shared/GraphicsTools.Shared.vcxitems.filters
@@ -24,10 +24,10 @@
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\RingBuffer.h">
<Filter>include</Filter>
</ClInclude>
- <ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\FreeBlockListManager.h">
+ <ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\VariableSizeAllocationsManager.h">
<Filter>include</Filter>
</ClInclude>
- <ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\FreeBlockListManagerGPU.h">
+ <ClInclude Include="$(MSBuildThisFileDirectory)..\..\include\VariableSizeGPUAllocationsManager.h">
<Filter>include</Filter>
</ClInclude>
</ItemGroup>
diff --git a/Graphics/GraphicsTools/include/BasicShaderSourceStreamFactory.h b/Graphics/GraphicsTools/include/BasicShaderSourceStreamFactory.h
index 86e2650b..24e87df5 100644
--- a/Graphics/GraphicsTools/include/BasicShaderSourceStreamFactory.h
+++ b/Graphics/GraphicsTools/include/BasicShaderSourceStreamFactory.h
@@ -1,4 +1,4 @@
-/* Copyright 2015-2016 Egor Yusov
+/* Copyright 2015-2017 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/Graphics/GraphicsTools/include/GraphicsUtilities.h b/Graphics/GraphicsTools/include/GraphicsUtilities.h
index 6e6b34f6..f13684fd 100644
--- a/Graphics/GraphicsTools/include/GraphicsUtilities.h
+++ b/Graphics/GraphicsTools/include/GraphicsUtilities.h
@@ -1,4 +1,4 @@
-/* Copyright 2015-2016 Egor Yusov
+/* Copyright 2015-2017 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/Graphics/GraphicsTools/include/RingBuffer.h b/Graphics/GraphicsTools/include/RingBuffer.h
index 043e884d..6ac15fc1 100644
--- a/Graphics/GraphicsTools/include/RingBuffer.h
+++ b/Graphics/GraphicsTools/include/RingBuffer.h
@@ -1,4 +1,4 @@
-/* Copyright 2015-2016 Egor Yusov
+/* Copyright 2015-2017 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,10 @@
#pragma once
+/// \file
+/// Implementation of Diligent::RingBuffer class
+
+
#include <deque>
#include "MemoryAllocator.h"
#include "STDAllocator.h"
@@ -30,6 +34,7 @@
namespace Diligent
{
+ /// Implementation of a ring buffer. The class is not thread-safe.
class RingBuffer
{
public:
diff --git a/Graphics/GraphicsTools/include/ShaderMacroHelper.h b/Graphics/GraphicsTools/include/ShaderMacroHelper.h
index f64c2349..1beec624 100644
--- a/Graphics/GraphicsTools/include/ShaderMacroHelper.h
+++ b/Graphics/GraphicsTools/include/ShaderMacroHelper.h
@@ -1,4 +1,4 @@
-/* Copyright 2015-2016 Egor Yusov
+/* Copyright 2015-2017 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/Graphics/GraphicsTools/include/VariableSizeAllocationsManager.h b/Graphics/GraphicsTools/include/VariableSizeAllocationsManager.h
new file mode 100644
index 00000000..86b8f6e1
--- /dev/null
+++ b/Graphics/GraphicsTools/include/VariableSizeAllocationsManager.h
@@ -0,0 +1,329 @@
+/* Copyright 2015-2017 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
+
+#include <map>
+#include "MemoryAllocator.h"
+#include "STDAllocator.h"
+#include "DebugUtilities.h"
+
+namespace Diligent
+{
+ // The class handles free memory block management to accommodate variable-size allocation requests.
+ // It keeps track of free blocks only and does not record allocation sizes. The class uses two ordered maps
+ // to facilitate operations. The first map keeps blocks sorted by their offsets. The second multimap keeps blocks
+ // sorted by their sizes. The elements of the two maps reference each other, which enables efficient block
+ // insertion, removal and merging.
+ //
+ // 8 32 64 104
+ // |<---16--->| |<-----24------>| |<---16--->| |<-----32----->|
+ //
+ //
+ // m_FreeBlocksBySize m_FreeBlocksByOffset
+ // size->offset offset->size
+ //
+ // 16 ------------------> 8 ----------> {size = 16, &m_FreeBlocksBySize[0]}
+ //
+ // 16 ------. .-------> 32 ----------> {size = 24, &m_FreeBlocksBySize[2]}
+ // '.'
+ // 24 -------' '--------> 64 ----------> {size = 16, &m_FreeBlocksBySize[1]}
+ //
+ // 32 ------------------> 104 ----------> {size = 32, &m_FreeBlocksBySize[3]}
+ //
+ class VariableSizeAllocationsManager
+ {
+ public:
+ typedef size_t OffsetType;
+ static const OffsetType InvalidOffset = static_cast<OffsetType>(-1);
+
+ private:
+ struct FreeBlockInfo;
+
+ // Type of the map that keeps memory blocks sorted by their offsets
+ using TFreeBlocksByOffsetMap =
+ std::map<OffsetType,
+ FreeBlockInfo,
+ std::less<OffsetType>, // Standard ordering
+ STDAllocatorRawMem<std::pair<OffsetType, FreeBlockInfo>> // Raw memory allocator
+ >;
+
+ // Type of the map that keeps memory blocks sorted by their sizes
+ using TFreeBlocksBySizeMap =
+ std::multimap<OffsetType,
+ TFreeBlocksByOffsetMap::iterator,
+ std::less<OffsetType>, // Standard ordering
+ STDAllocatorRawMem<std::pair<OffsetType, TFreeBlocksByOffsetMap::iterator>> // Raw memory allocator
+ >;
+
+ struct FreeBlockInfo
+ {
+ // Block size (no reserved space for the size of the allocation)
+ OffsetType Size;
+
+ // Iterator referencing this block in the multimap sorted by the block size
+ TFreeBlocksBySizeMap::iterator OrderBySizeIt;
+
+ FreeBlockInfo(OffsetType _Size) : Size(_Size){}
+ };
+
+ public:
+ VariableSizeAllocationsManager(OffsetType MaxSize, IMemoryAllocator &Allocator) :
+ m_MaxSize(MaxSize),
+ m_FreeSize(MaxSize),
+ m_FreeBlocksByOffset( STD_ALLOCATOR_RAW_MEM(TFreeBlocksByOffsetMap::value_type, Allocator, "Allocator for map<OffsetType, FreeBlockInfo>") ),
+ m_FreeBlocksBySize( STD_ALLOCATOR_RAW_MEM(TFreeBlocksBySizeMap::value_type, Allocator, "Allocator for multimap<OffsetType, TFreeBlocksByOffsetMap::iterator>") )
+ {
+ // Insert single maximum-size block
+ AddNewBlock(0, m_MaxSize);
+
+#ifdef _DEBUG
+ DbgVerifyList();
+#endif
+ }
+
+ ~VariableSizeAllocationsManager()
+ {
+#ifdef _DEBUG
+ if( !m_FreeBlocksByOffset.empty() || !m_FreeBlocksBySize.empty() )
+ {
+ VERIFY(m_FreeBlocksByOffset.size() == 1, "Single free block is expected");
+ VERIFY(m_FreeBlocksByOffset.begin()->first == 0, "Head chunk offset is expected to be 0");
+ VERIFY(m_FreeBlocksByOffset.begin()->second.Size == m_MaxSize, "Head chunk size is expected to be ", m_MaxSize);
+ VERIFY_EXPR(m_FreeBlocksByOffset.begin()->second.OrderBySizeIt == m_FreeBlocksBySize.begin());
+ VERIFY(m_FreeBlocksBySize.size() == m_FreeBlocksByOffset.size(), "Sizes of the two maps must be equal");
+
+ VERIFY(m_FreeBlocksBySize.size() == 1, "Single free block is expected");
+ VERIFY(m_FreeBlocksBySize.begin()->first == m_MaxSize, "Head chunk size is expected to be ", m_MaxSize);
+ VERIFY(m_FreeBlocksBySize.begin()->second == m_FreeBlocksByOffset.begin(), "Incorrect first block");
+ }
+#endif
+ }
+
+ VariableSizeAllocationsManager(VariableSizeAllocationsManager&& rhs) :
+ m_FreeBlocksByOffset(std::move(rhs.m_FreeBlocksByOffset)),
+ m_FreeBlocksBySize(std::move(rhs.m_FreeBlocksBySize)),
+ m_MaxSize(rhs.m_MaxSize),
+ m_FreeSize(rhs.m_FreeSize)
+ {
+ //rhs.m_MaxSize = 0; - const
+ rhs.m_FreeSize = 0;
+ }
+
+ VariableSizeAllocationsManager& operator = (VariableSizeAllocationsManager&& rhs) = default;
+ VariableSizeAllocationsManager(const VariableSizeAllocationsManager&) = delete;
+ VariableSizeAllocationsManager& operator = (const VariableSizeAllocationsManager&) = delete;
+
+ OffsetType Allocate(OffsetType Size)
+ {
+ VERIFY_EXPR(Size != 0);
+ if(m_FreeSize < Size)
+ return InvalidOffset;
+
+ // Get the first block that is large enough to encompass Size bytes
+ // lower_bound() returns an iterator pointing to the first element that
+ // is not less (i.e. >= ) than key
+ auto SmallestBlockItIt = m_FreeBlocksBySize.lower_bound(Size);
+ if(SmallestBlockItIt == m_FreeBlocksBySize.end())
+ return InvalidOffset;
+
+ auto SmallestBlockIt = SmallestBlockItIt->second;
+ VERIFY_EXPR(Size <= SmallestBlockIt->second.Size);
+ VERIFY_EXPR(SmallestBlockIt->second.Size == SmallestBlockItIt->first);
+
+ // SmallestBlockIt.Offset
+ // | |
+ // |<------SmallestBlockIt.Size------>|
+ // |<------Size------>|<---NewSize--->|
+ // | |
+ // Offset NewOffset
+ //
+ auto Offset = SmallestBlockIt->first;
+ auto NewOffset = Offset + Size;
+ auto NewSize = SmallestBlockIt->second.Size - Size;
+ VERIFY_EXPR(SmallestBlockItIt == SmallestBlockIt->second.OrderBySizeIt);
+ m_FreeBlocksBySize.erase(SmallestBlockItIt);
+ m_FreeBlocksByOffset.erase(SmallestBlockIt);
+ if (NewSize > 0)
+ {
+ AddNewBlock(NewOffset, NewSize);
+ }
+
+ m_FreeSize -= Size;
+
+#ifdef _DEBUG
+ DbgVerifyList();
+#endif
+ return Offset;
+ }
+
+ void Free(OffsetType Offset, OffsetType Size)
+ {
+ VERIFY_EXPR(Offset+Size <= m_MaxSize);
+
+ // Find the first element whose offset is greater than the specified offset.
+ // upper_bound() returns an iterator pointing to the first element in the
+ // container whose key is considered to go after k.
+ auto NextBlockIt = m_FreeBlocksByOffset.upper_bound(Offset);
+#ifdef _DEBUG
+ {
+ auto LowBnd = m_FreeBlocksByOffset.lower_bound(Offset); // First element whose offset is >=
+ // Since zero-size allocations are not allowed, lower bound must always be equal to the upper bound
+ VERIFY_EXPR(LowBnd == NextBlockIt);
+ }
+#endif
+ // Block being deallocated must not overlap with the next block
+ VERIFY_EXPR(NextBlockIt == m_FreeBlocksByOffset.end() || Offset+Size <= NextBlockIt->first);
+ auto PrevBlockIt = NextBlockIt;
+ if(PrevBlockIt != m_FreeBlocksByOffset.begin())
+ {
+ --PrevBlockIt;
+ // Block being deallocated must not overlap with the previous block
+ VERIFY_EXPR(Offset >= PrevBlockIt->first + PrevBlockIt->second.Size);
+ }
+ else
+ PrevBlockIt = m_FreeBlocksByOffset.end();
+
+ OffsetType NewSize, NewOffset;
+ if(PrevBlockIt != m_FreeBlocksByOffset.end() && Offset == PrevBlockIt->first + PrevBlockIt->second.Size)
+ {
+ // PrevBlock.Offset Offset
+ // | |
+ // |<-----PrevBlock.Size----->|<------Size-------->|
+ //
+ NewSize = PrevBlockIt->second.Size + Size;
+ NewOffset = PrevBlockIt->first;
+
+ if (NextBlockIt != m_FreeBlocksByOffset.end() && Offset + Size == NextBlockIt->first)
+ {
+ // PrevBlock.Offset Offset NextBlock.Offset
+ // | | |
+ // |<-----PrevBlock.Size----->|<------Size-------->|<-----NextBlock.Size----->|
+ //
+ NewSize += NextBlockIt->second.Size;
+ m_FreeBlocksBySize.erase(PrevBlockIt->second.OrderBySizeIt);
+ m_FreeBlocksBySize.erase(NextBlockIt->second.OrderBySizeIt);
+ // Delete the range of two blocks
+ ++NextBlockIt;
+ m_FreeBlocksByOffset.erase(PrevBlockIt, NextBlockIt);
+ }
+ else
+ {
+ // PrevBlock.Offset Offset NextBlock.Offset
+ // | | |
+ // |<-----PrevBlock.Size----->|<------Size-------->| ~ ~ ~ |<-----NextBlock.Size----->|
+ //
+ m_FreeBlocksBySize.erase(PrevBlockIt->second.OrderBySizeIt);
+ m_FreeBlocksByOffset.erase(PrevBlockIt);
+ }
+ }
+ else if (NextBlockIt != m_FreeBlocksByOffset.end() && Offset + Size == NextBlockIt->first)
+ {
+ // PrevBlock.Offset Offset NextBlock.Offset
+ // | | |
+ // |<-----PrevBlock.Size----->| ~ ~ ~ |<------Size-------->|<-----NextBlock.Size----->|
+ //
+ NewSize = Size + NextBlockIt->second.Size;
+ NewOffset = Offset;
+ m_FreeBlocksBySize.erase(NextBlockIt->second.OrderBySizeIt);
+ m_FreeBlocksByOffset.erase(NextBlockIt);
+ }
+ else
+ {
+ // PrevBlock.Offset Offset NextBlock.Offset
+ // | | |
+ // |<-----PrevBlock.Size----->| ~ ~ ~ |<------Size-------->| ~ ~ ~ |<-----NextBlock.Size----->|
+ //
+ NewSize = Size;
+ NewOffset = Offset;
+ }
+
+ AddNewBlock(NewOffset, NewSize);
+
+ m_FreeSize += Size;
+#ifdef _DEBUG
+ DbgVerifyList();
+#endif
+ }
+
+ OffsetType GetMaxSize()const{return m_MaxSize;}
+ bool IsFull()const{ return m_FreeSize==0; };
+ bool IsEmpty()const{ return m_FreeSize==m_MaxSize; };
+ OffsetType GetFreeSize()const{return m_FreeSize;}
+
+#ifdef _DEBUG
+ size_t DbgGetNumFreeBlocks()const{return m_FreeBlocksByOffset.size();}
+#endif
+
+ private:
+ void AddNewBlock(OffsetType Offset, OffsetType Size)
+ {
+ auto NewBlockIt = m_FreeBlocksByOffset.emplace(Offset, Size);
+ VERIFY_EXPR(NewBlockIt.second);
+ auto OrderIt = m_FreeBlocksBySize.emplace(Size, NewBlockIt.first);
+ NewBlockIt.first->second.OrderBySizeIt = OrderIt;
+ }
+
+
+#ifdef _DEBUG
+ void DbgVerifyList()
+ {
+ OffsetType TotalFreeSize = 0;
+
+ auto BlockIt = m_FreeBlocksByOffset.begin();
+ auto PrevBlockIt = m_FreeBlocksByOffset.end();
+ VERIFY_EXPR(m_FreeBlocksByOffset.size() == m_FreeBlocksBySize.size());
+ while (BlockIt != m_FreeBlocksByOffset.end())
+ {
+ VERIFY_EXPR(BlockIt->first >= 0 && BlockIt->first + BlockIt->second.Size <= m_MaxSize);
+ VERIFY_EXPR(BlockIt == BlockIt->second.OrderBySizeIt->second);
+ VERIFY_EXPR(BlockIt->second.Size == BlockIt->second.OrderBySizeIt->first);
+ // PrevBlock.Offset BlockIt.first
+ // | |
+ // ~ ~ |<-----PrevBlock.Size----->| ~ ~ ~ |<------Size-------->| ~ ~ ~
+ //
+ VERIFY(PrevBlockIt == m_FreeBlocksByOffset.end() || BlockIt->first > PrevBlockIt->first + PrevBlockIt->second.Size, "Unmerged adjacent or overlapping blocks detected" );
+ TotalFreeSize += BlockIt->second.Size;
+
+ PrevBlockIt = BlockIt;
+ ++BlockIt;
+ }
+
+ auto OrderIt = m_FreeBlocksBySize.begin();
+ while (OrderIt != m_FreeBlocksBySize.end())
+ {
+ VERIFY_EXPR(OrderIt->first == OrderIt->second->second.Size);
+ ++OrderIt;
+ }
+
+ VERIFY_EXPR(TotalFreeSize == m_FreeSize);
+ }
+#endif
+
+ TFreeBlocksByOffsetMap m_FreeBlocksByOffset;
+ TFreeBlocksBySizeMap m_FreeBlocksBySize;
+
+ const OffsetType m_MaxSize = 0;
+ OffsetType m_FreeSize = 0;
+ };
+}
diff --git a/Graphics/GraphicsTools/include/VariableSizeGPUAllocationsManager.h b/Graphics/GraphicsTools/include/VariableSizeGPUAllocationsManager.h
new file mode 100644
index 00000000..04a347d3
--- /dev/null
+++ b/Graphics/GraphicsTools/include/VariableSizeGPUAllocationsManager.h
@@ -0,0 +1,90 @@
+/* Copyright 2015-2017 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
+
+#include <deque>
+#include "VariableSizeAllocationsManager.h"
+#include "STDAllocator.h"
+
+namespace Diligent
+{
+ // Class extends basic variable-size memory block allocator by deferring deallocation
+ // of freed blocks untill the corresponding frame is completed
+ class VariableSizeGPUAllocationsManager : public VariableSizeAllocationsManager
+ {
+ private:
+ struct FreedAllocationInfo
+ {
+ OffsetType Offset;
+ OffsetType Size;
+ Uint64 FrameNumber;
+ FreedAllocationInfo(OffsetType _Offset, OffsetType _Size, Uint64 _FrameNumber) :
+ Offset(_Offset), Size(_Size), FrameNumber(_FrameNumber)
+ {}
+ };
+
+ public:
+ VariableSizeGPUAllocationsManager(OffsetType MaxSize, IMemoryAllocator &Allocator) :
+ VariableSizeAllocationsManager(MaxSize, Allocator),
+ m_StaleAllocations(0, FreedAllocationInfo(0,0,0), STD_ALLOCATOR_RAW_MEM(FreedAllocationInfo, Allocator, "Allocator for deque< FreedAllocationInfo>" ))
+ {}
+
+ ~VariableSizeGPUAllocationsManager()
+ {
+ VERIFY(m_StaleAllocations.empty(), "Not all stale allocations released");
+ }
+
+ // = default causes compiler error when instantiating std::vector::emplace_back() in Visual Studio 2015 (Version 14.0.23107.0 D14REL)
+ VariableSizeGPUAllocationsManager(VariableSizeGPUAllocationsManager&& rhs) :
+ VariableSizeAllocationsManager(std::move(rhs)),
+ m_StaleAllocations(std::move(rhs.m_StaleAllocations))
+ {
+ }
+
+ VariableSizeGPUAllocationsManager& operator = (VariableSizeGPUAllocationsManager&& rhs) = default;
+ VariableSizeGPUAllocationsManager(const VariableSizeGPUAllocationsManager&) = delete;
+ VariableSizeGPUAllocationsManager& operator = (const VariableSizeGPUAllocationsManager&) = delete;
+
+ void Free(OffsetType Offset, OffsetType Size, Uint64 FrameNumber)
+ {
+ // Do not release the block immediately, but add
+ // it to the queue instead
+ m_StaleAllocations.emplace_back(Offset, Size, FrameNumber);
+ }
+
+ void ReleaseCompletedFrames(Uint64 NumCompletedFrames)
+ {
+ // Free all allocations from the beginning of the queue that belong to completed frames
+ while(!m_StaleAllocations.empty() && m_StaleAllocations.front().FrameNumber < NumCompletedFrames)
+ {
+ auto &OldestAllocation = m_StaleAllocations.front();
+ VariableSizeAllocationsManager::Free(OldestAllocation.Offset, OldestAllocation.Size);
+ m_StaleAllocations.pop_front();
+ }
+ }
+
+ private:
+ std::deque< FreedAllocationInfo, STDAllocatorRawMem<FreedAllocationInfo> > m_StaleAllocations;
+ };
+}
diff --git a/Graphics/GraphicsTools/include/pch.h b/Graphics/GraphicsTools/include/pch.h
index 41fa3c3b..690233d1 100644
--- a/Graphics/GraphicsTools/include/pch.h
+++ b/Graphics/GraphicsTools/include/pch.h
@@ -1,4 +1,4 @@
-/* Copyright 2015-2016 Egor Yusov
+/* Copyright 2015-2017 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/Graphics/GraphicsTools/src/BasicShaderSourceStreamFactory.cpp b/Graphics/GraphicsTools/src/BasicShaderSourceStreamFactory.cpp
index e15635e8..6c1db669 100644
--- a/Graphics/GraphicsTools/src/BasicShaderSourceStreamFactory.cpp
+++ b/Graphics/GraphicsTools/src/BasicShaderSourceStreamFactory.cpp
@@ -1,4 +1,4 @@
-/* Copyright 2015-2016 Egor Yusov
+/* Copyright 2015-2017 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp
index f3a7d873..e0ae1dce 100644
--- a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp
+++ b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp
@@ -1,4 +1,4 @@
-/* Copyright 2015-2016 Egor Yusov
+/* Copyright 2015-2017 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/Graphics/GraphicsTools/src/pch.cpp b/Graphics/GraphicsTools/src/pch.cpp
index 997e0f4a..d5f45c6a 100644
--- a/Graphics/GraphicsTools/src/pch.cpp
+++ b/Graphics/GraphicsTools/src/pch.cpp
@@ -1,4 +1,4 @@
-/* Copyright 2015-2016 Egor Yusov
+/* Copyright 2015-2017 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.