core: Define VK_USE_PLATFORM_XCB_KHR before including vkd3d_utils.h.
[vkmodelviewer.git] / Core / CommandAllocatorPool.cpp
blob0a9a188b70fcd57025df9ba5de9988563862dc17
1 //
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.
8 //
9 // Developed by Minigraph
11 // Author: James Stanard
14 #include "pch.h"
15 #include "CommandAllocatorPool.h"
17 CommandAllocatorPool::CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE Type) :
18 m_cCommandListType(Type),
19 m_Device(nullptr)
23 CommandAllocatorPool::~CommandAllocatorPool()
25 Shutdown();
28 void CommandAllocatorPool::Create(ID3D12Device * pDevice)
30 m_Device = 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);
69 return 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));