-
Notifications
You must be signed in to change notification settings - Fork 79
/
DescriptorAllocator.cpp
76 lines (57 loc) · 1.96 KB
/
DescriptorAllocator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <DX12LibPCH.h>
#include <DescriptorAllocator.h>
#include <DescriptorAllocatorPage.h>
DescriptorAllocator::DescriptorAllocator(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t numDescriptorsPerHeap)
: m_HeapType(type)
, m_NumDescriptorsPerHeap(numDescriptorsPerHeap)
{
}
DescriptorAllocator::~DescriptorAllocator()
{}
std::shared_ptr<DescriptorAllocatorPage> DescriptorAllocator::CreateAllocatorPage()
{
auto newPage = std::make_shared<DescriptorAllocatorPage>( m_HeapType, m_NumDescriptorsPerHeap );
m_HeapPool.emplace_back( newPage );
m_AvailableHeaps.insert( m_HeapPool.size() - 1 );
return newPage;
}
DescriptorAllocation DescriptorAllocator::Allocate(uint32_t numDescriptors)
{
std::lock_guard<std::mutex> lock( m_AllocationMutex );
DescriptorAllocation allocation;
for ( auto iter = m_AvailableHeaps.begin(); iter != m_AvailableHeaps.end(); ++iter )
{
auto allocatorPage = m_HeapPool[*iter];
allocation = allocatorPage->Allocate( numDescriptors );
if ( allocatorPage->NumFreeHandles() == 0 )
{
iter = m_AvailableHeaps.erase( iter );
}
// A valid allocation has been found.
if ( !allocation.IsNull() )
{
break;
}
}
// No available heap could satisfy the requested number of descriptors.
if ( allocation.IsNull() )
{
m_NumDescriptorsPerHeap = std::max( m_NumDescriptorsPerHeap, numDescriptors );
auto newPage = CreateAllocatorPage();
allocation = newPage->Allocate( numDescriptors );
}
return allocation;
}
void DescriptorAllocator::ReleaseStaleDescriptors( uint64_t frameNumber )
{
std::lock_guard<std::mutex> lock( m_AllocationMutex );
for ( size_t i = 0; i < m_HeapPool.size(); ++i )
{
auto page = m_HeapPool[i];
page->ReleaseStaleDescriptors( frameNumber );
if ( page->NumFreeHandles() > 0 )
{
m_AvailableHeaps.insert( i );
}
}
}