2 // Copyright (c) Microsoft. All rights reserved.
3 // This code is licensed under the MIT License (MIT).
4 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 // Developed by Minigraph
11 // Author: James Stanard
15 #include "CommandAllocatorPool.h"
17 CommandAllocatorPool::CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE Type
) :
18 m_cCommandListType(Type
),
23 CommandAllocatorPool::~CommandAllocatorPool()
28 void CommandAllocatorPool::Create(ID3D12Device
* pDevice
)
33 void CommandAllocatorPool::Shutdown()
35 for (size_t i
= 0; i
< m_AllocatorPool
.size(); ++i
)
36 m_AllocatorPool
[i
]->Release();
38 m_AllocatorPool
.clear();
41 ID3D12CommandAllocator
* CommandAllocatorPool::RequestAllocator(uint64_t CompletedFenceValue
)
43 std::lock_guard
<std::mutex
> LockGuard(m_AllocatorMutex
);
45 ID3D12CommandAllocator
* pAllocator
= nullptr;
47 if (!m_ReadyAllocators
.empty())
49 std::pair
<uint64_t, ID3D12CommandAllocator
*>& AllocatorPair
= m_ReadyAllocators
.front();
51 if (AllocatorPair
.first
<= CompletedFenceValue
)
53 pAllocator
= AllocatorPair
.second
;
54 ASSERT_SUCCEEDED(pAllocator
->Reset());
55 m_ReadyAllocators
.pop();
59 // If no allocator's were ready to be reused, create a new one
60 if (pAllocator
== nullptr)
62 ASSERT_SUCCEEDED(m_Device
->CreateCommandAllocator(m_cCommandListType
, MY_IID_PPV_ARGS(&pAllocator
)));
63 wchar_t AllocatorName
[32];
64 swprintf(AllocatorName
, 32, L
"CommandAllocator %zu", m_AllocatorPool
.size());
65 pAllocator
->SetName(AllocatorName
);
66 m_AllocatorPool
.push_back(pAllocator
);
72 void CommandAllocatorPool::DiscardAllocator(uint64_t FenceValue
, ID3D12CommandAllocator
* Allocator
)
74 std::lock_guard
<std::mutex
> LockGuard(m_AllocatorMutex
);
76 // That fence value indicates we are free to reset the allocator
77 m_ReadyAllocators
.push(std::make_pair(FenceValue
, Allocator
));