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
20 #include "CommandAllocatorPool.h"
24 friend class CommandListManager
;
25 friend class CommandContext
;
28 CommandQueue(D3D12_COMMAND_LIST_TYPE Type
);
31 void Create(ID3D12Device
* pDevice
);
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
; }
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
;
78 ~CommandListManager();
80 void Create(ID3D12Device
* pDevice
);
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
)
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)
119 m_GraphicsQueue
.WaitForIdle();
120 m_ComputeQueue
.WaitForIdle();
121 m_CopyQueue
.WaitForIdle();
126 ID3D12Device
* m_Device
;
128 CommandQueue m_GraphicsQueue
;
129 CommandQueue m_ComputeQueue
;
130 CommandQueue m_CopyQueue
;