core: Define VK_USE_PLATFORM_XCB_KHR before including vkd3d_utils.h.
[vkmodelviewer.git] / Core / CommandListManager.h
blob5e4f01ef8ae010d34c97dfce68fe675e0f0a96e6
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 #pragma once
16 #include <vector>
17 #include <queue>
18 #include <mutex>
19 #include <stdint.h>
20 #include "CommandAllocatorPool.h"
22 class CommandQueue
24 friend class CommandListManager;
25 friend class CommandContext;
27 public:
28 CommandQueue(D3D12_COMMAND_LIST_TYPE Type);
29 ~CommandQueue();
31 void Create(ID3D12Device* pDevice);
32 void Shutdown();
34 inline bool IsReady()
36 return m_CommandQueue != nullptr;
39 uint64_t IncrementFence(void);
40 bool IsFenceComplete(uint64_t FenceValue);
41 void StallForFence(uint64_t FenceValue);
42 void StallForProducer(CommandQueue& Producer);
43 void WaitForFence(uint64_t FenceValue);
44 void WaitForIdle(void) { WaitForFence(m_NextFenceValue - 1); }
46 ID3D12CommandQueue* GetCommandQueue() { return m_CommandQueue; }
48 uint64_t GetNextFenceValue() { return m_NextFenceValue; }
50 private:
52 uint64_t ExecuteCommandList(ID3D12CommandList* List);
53 ID3D12CommandAllocator* RequestAllocator(void);
54 void DiscardAllocator(uint64_t FenceValueForReset, ID3D12CommandAllocator* Allocator);
56 ID3D12CommandQueue* m_CommandQueue;
58 const D3D12_COMMAND_LIST_TYPE m_Type;
60 CommandAllocatorPool m_AllocatorPool;
61 std::mutex m_FenceMutex;
62 std::mutex m_EventMutex;
64 // Lifetime of these objects is managed by the descriptor cache
65 ID3D12Fence* m_pFence;
66 uint64_t m_NextFenceValue;
67 uint64_t m_LastCompletedFenceValue;
68 HANDLE m_FenceEventHandle;
72 class CommandListManager
74 friend class CommandContext;
76 public:
77 CommandListManager();
78 ~CommandListManager();
80 void Create(ID3D12Device* pDevice);
81 void Shutdown();
83 CommandQueue& GetGraphicsQueue(void) { return m_GraphicsQueue; }
84 CommandQueue& GetComputeQueue(void) { return m_ComputeQueue; }
85 CommandQueue& GetCopyQueue(void) { return m_CopyQueue; }
87 CommandQueue& GetQueue(D3D12_COMMAND_LIST_TYPE Type = D3D12_COMMAND_LIST_TYPE_DIRECT)
89 switch (Type)
91 case D3D12_COMMAND_LIST_TYPE_COMPUTE: return m_ComputeQueue;
92 case D3D12_COMMAND_LIST_TYPE_COPY: return m_CopyQueue;
93 default: return m_GraphicsQueue;
97 ID3D12CommandQueue* GetCommandQueue()
99 return m_GraphicsQueue.GetCommandQueue();
102 void CreateNewCommandList(
103 D3D12_COMMAND_LIST_TYPE Type,
104 ID3D12GraphicsCommandList** List,
105 ID3D12CommandAllocator** Allocator);
107 // Test to see if a fence has already been reached
108 bool IsFenceComplete(uint64_t FenceValue)
110 return GetQueue(D3D12_COMMAND_LIST_TYPE(FenceValue >> 56)).IsFenceComplete(FenceValue);
113 // The CPU will wait for a fence to reach a specified value
114 void WaitForFence(uint64_t FenceValue);
116 // The CPU will wait for all command queues to empty (so that the GPU is idle)
117 void IdleGPU(void)
119 m_GraphicsQueue.WaitForIdle();
120 m_ComputeQueue.WaitForIdle();
121 m_CopyQueue.WaitForIdle();
124 private:
126 ID3D12Device* m_Device;
128 CommandQueue m_GraphicsQueue;
129 CommandQueue m_ComputeQueue;
130 CommandQueue m_CopyQueue;