29 #include "D3D12ResourceBase.h" 30 #include "TextureViewD3D12.h" 31 #include "TextureD3D12.h" 32 #include "BufferD3D12.h" 33 #include "DescriptorHeap.h" 41 DWParam( FLOAT f ) : Float(f) {}
42 DWParam( UINT u ) : Uint(u) {}
43 DWParam( INT i ) : Int(i) {}
45 void operator= ( FLOAT f ) { Float = f; }
46 void operator= ( UINT u ) { Uint = u; }
47 void operator= ( INT i ) { Int = i; }
62 CommandContext( IMemoryAllocator &MemAllocator,
63 class CommandListManager& CmdListManager,
64 GPUDescriptorHeap GPUDescriptorHeaps[],
65 const Uint32 DynamicDescriptorAllocationChunkSize[]);
67 ~CommandContext(
void);
71 ID3D12GraphicsCommandList* Close(ID3D12CommandAllocator **ppAllocator);
72 void Reset( CommandListManager& CmdListManager );
74 class GraphicsContext& AsGraphicsContext();
75 class ComputeContext& AsComputeContext();
77 void ClearUAVFloat( ITextureViewD3D12 *pTexView,
const float* Color );
78 void ClearUAVUint( ITextureViewD3D12 *pTexView,
const UINT *Color );
80 void CopyResource(ID3D12Resource *pDstRes, ID3D12Resource *pSrcRes)
82 m_pCommandList->CopyResource(pDstRes, pSrcRes);
85 void TransitionResource(ITextureD3D12 *pTexture, D3D12_RESOURCE_STATES NewState,
bool FlushImmediate =
false);
86 void TransitionResource(IBufferD3D12 *pBuffer, D3D12_RESOURCE_STATES NewState,
bool FlushImmediate =
false);
88 void FlushResourceBarriers();
90 struct ShaderDescriptorHeaps
92 ID3D12DescriptorHeap* pSrvCbvUavHeap;
93 ID3D12DescriptorHeap* pSamplerHeap;
94 ShaderDescriptorHeaps(ID3D12DescriptorHeap* _pSrvCbvUavHeap =
nullptr, ID3D12DescriptorHeap* _pSamplerHeap =
nullptr) :
95 pSrvCbvUavHeap(_pSrvCbvUavHeap),
96 pSamplerHeap(_pSamplerHeap)
98 bool operator == (
const ShaderDescriptorHeaps& rhs)
const 100 return pSrvCbvUavHeap == rhs.pSrvCbvUavHeap && pSamplerHeap == rhs.pSamplerHeap;
104 return pSrvCbvUavHeap !=
nullptr || pSamplerHeap !=
nullptr;
107 void SetDescriptorHeaps( ShaderDescriptorHeaps& Heaps );
109 void ExecuteIndirect(ID3D12CommandSignature *pCmdSignature, ID3D12Resource *pBuff, Uint64 ArgsOffset)
111 FlushResourceBarriers();
112 m_pCommandList->ExecuteIndirect(pCmdSignature, 1, pBuff, ArgsOffset,
nullptr, 0);
115 void SetID(
const Char* ID) { m_ID = ID; }
116 ID3D12GraphicsCommandList *GetCommandList(){
return m_pCommandList;}
118 void DiscardDynamicDescriptors(Uint64 FenceValue);
119 DescriptorHeapAllocation AllocateDynamicGPUVisibleDescriptor( D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count = 1 );
121 void InsertUAVBarrier(D3D12ResourceBase& Resource, IDeviceObject &Object,
bool FlushImmediate =
false);
123 void SetPipelineState( ID3D12PipelineState* pPSO )
125 if (pPSO != m_pCurPipelineState)
127 m_pCommandList->SetPipelineState(m_pCurPipelineState = pPSO);
132 void TransitionResource(D3D12ResourceBase& Resource, IDeviceObject &Object, D3D12_RESOURCE_STATES NewState,
bool FlushImmediate);
133 void InsertAliasBarrier(D3D12ResourceBase& Before, D3D12ResourceBase& After, IDeviceObject &BeforeObj, IDeviceObject &AfterObj,
bool FlushImmediate =
false);
135 CComPtr<ID3D12GraphicsCommandList> m_pCommandList;
136 CComPtr<ID3D12CommandAllocator> m_pCurrentAllocator;
138 ID3D12PipelineState* m_pCurPipelineState =
nullptr;
139 ID3D12RootSignature* m_pCurGraphicsRootSignature =
nullptr;
140 ID3D12RootSignature* m_pCurComputeRootSignature =
nullptr;
142 static constexpr
int MaxPendingBarriers = 16;
143 std::vector<D3D12_RESOURCE_BARRIER, STDAllocatorRawMem<D3D12_RESOURCE_BARRIER> > m_PendingResourceBarriers;
148 std::vector< RefCntAutoPtr<IDeviceObject>, STDAllocatorRawMem<RefCntAutoPtr<IDeviceObject>> > m_PendingBarrierObjects;
150 ShaderDescriptorHeaps m_BoundDescriptorHeaps;
155 DynamicSuballocationsManager m_DynamicGPUDescriptorAllocator[2];
159 D3D12_PRIMITIVE_TOPOLOGY m_PrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
163 class GraphicsContext :
public CommandContext
166 void ClearRenderTarget( ITextureViewD3D12 *pRTV,
const float *Color );
167 void ClearDepthStencil( ITextureViewD3D12 *pDSV, D3D12_CLEAR_FLAGS ClearFlags,
float Depth, UINT8 Stencil );
169 void SetRootSignature( ID3D12RootSignature *pRootSig )
171 if (pRootSig != m_pCurGraphicsRootSignature)
173 m_pCommandList->SetGraphicsRootSignature(m_pCurGraphicsRootSignature = pRootSig);
177 void SetRenderTargets( UINT NumRTVs, ITextureViewD3D12** ppRTVs, ITextureViewD3D12* pDSV );
179 void SetViewports( UINT NumVPs,
const D3D12_VIEWPORT* pVPs )
181 m_pCommandList->RSSetViewports(NumVPs, pVPs);
184 void SetScissorRects( UINT NumRects,
const D3D12_RECT* pRects )
186 m_pCommandList->RSSetScissorRects(NumRects, pRects);
189 void SetStencilRef( UINT StencilRef )
191 m_pCommandList->OMSetStencilRef( StencilRef );
194 void SetBlendFactor(
const float* BlendFactor )
196 m_pCommandList->OMSetBlendFactor( BlendFactor );
199 void SetPrimitiveTopology( D3D12_PRIMITIVE_TOPOLOGY Topology )
201 if(m_PrimitiveTopology != Topology)
203 m_PrimitiveTopology = Topology;
204 m_pCommandList->IASetPrimitiveTopology(Topology);
208 void SetConstants( UINT RootIndex, UINT NumConstants,
const void* pConstants )
210 m_pCommandList->SetGraphicsRoot32BitConstants( RootIndex, NumConstants, pConstants, 0 );
213 void SetConstants( UINT RootIndex, DWParam X )
215 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, X.Uint, 0 );
218 void SetConstants( UINT RootIndex, DWParam X, DWParam Y )
220 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, X.Uint, 0 );
221 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, Y.Uint, 1 );
224 void SetConstants( UINT RootIndex, DWParam X, DWParam Y, DWParam Z )
226 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, X.Uint, 0 );
227 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, Y.Uint, 1 );
228 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, Z.Uint, 2 );
231 void SetConstants( UINT RootIndex, DWParam X, DWParam Y, DWParam Z, DWParam W )
233 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, X.Uint, 0 );
234 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, Y.Uint, 1 );
235 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, Z.Uint, 2 );
236 m_pCommandList->SetGraphicsRoot32BitConstant( RootIndex, W.Uint, 3 );
239 void SetConstantBuffer( UINT RootIndex, D3D12_GPU_VIRTUAL_ADDRESS CBV )
241 m_pCommandList->SetGraphicsRootConstantBufferView(RootIndex, CBV);
244 void SetDescriptorTable( UINT RootIndex, D3D12_GPU_DESCRIPTOR_HANDLE FirstHandle )
246 m_pCommandList->SetGraphicsRootDescriptorTable( RootIndex, FirstHandle );
249 void SetIndexBuffer(
const D3D12_INDEX_BUFFER_VIEW& IBView )
251 m_pCommandList->IASetIndexBuffer(&IBView);
254 void SetVertexBuffers( UINT StartSlot, UINT Count,
const D3D12_VERTEX_BUFFER_VIEW VBViews[] )
256 m_pCommandList->IASetVertexBuffers(StartSlot, Count, VBViews);
259 void Draw(UINT VertexCountPerInstance, UINT InstanceCount,
260 UINT StartVertexLocation, UINT StartInstanceLocation)
262 FlushResourceBarriers();
263 m_pCommandList->DrawInstanced(VertexCountPerInstance, InstanceCount, StartVertexLocation, StartInstanceLocation);
266 void DrawIndexed(UINT IndexCountPerInstance, UINT InstanceCount, UINT StartIndexLocation,
267 INT BaseVertexLocation, UINT StartInstanceLocation)
269 FlushResourceBarriers();
270 m_pCommandList->DrawIndexedInstanced(IndexCountPerInstance, InstanceCount, StartIndexLocation, BaseVertexLocation, StartInstanceLocation);
274 class ComputeContext :
public CommandContext
278 void SetRootSignature( ID3D12RootSignature *pRootSig )
280 if (pRootSig != m_pCurComputeRootSignature)
282 m_pCommandList->SetComputeRootSignature(m_pCurComputeRootSignature = pRootSig);
286 void SetConstants( UINT RootIndex, UINT NumConstants,
const void* pConstants )
288 m_pCommandList->SetComputeRoot32BitConstants( RootIndex, NumConstants, pConstants, 0 );
291 void SetConstants( UINT RootIndex, DWParam X )
293 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, X.Uint, 0 );
296 void SetConstants( UINT RootIndex, DWParam X, DWParam Y )
298 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, X.Uint, 0 );
299 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, Y.Uint, 1 );
302 void SetConstants( UINT RootIndex, DWParam X, DWParam Y, DWParam Z )
304 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, X.Uint, 0 );
305 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, Y.Uint, 1 );
306 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, Z.Uint, 2 );
309 void SetConstants( UINT RootIndex, DWParam X, DWParam Y, DWParam Z, DWParam W )
311 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, X.Uint, 0 );
312 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, Y.Uint, 1 );
313 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, Z.Uint, 2 );
314 m_pCommandList->SetComputeRoot32BitConstant( RootIndex, W.Uint, 3 );
318 void SetConstantBuffer( UINT RootIndex, D3D12_GPU_VIRTUAL_ADDRESS CBV )
320 m_pCommandList->SetComputeRootConstantBufferView(RootIndex, CBV);
323 void SetDescriptorTable( UINT RootIndex, D3D12_GPU_DESCRIPTOR_HANDLE FirstHandle )
325 m_pCommandList->SetComputeRootDescriptorTable( RootIndex, FirstHandle );
328 void Dispatch(
size_t GroupCountX = 1,
size_t GroupCountY = 1,
size_t GroupCountZ = 1 )
330 FlushResourceBarriers();
331 m_pCommandList->Dispatch((UINT)GroupCountX, (UINT)GroupCountY, (UINT)GroupCountZ);
335 inline GraphicsContext& CommandContext::AsGraphicsContext()
337 return static_cast<GraphicsContext&
>(*this);
340 inline ComputeContext& CommandContext::AsComputeContext()
342 return static_cast<ComputeContext&
>(*this);
345 inline void CommandContext::SetDescriptorHeaps( ShaderDescriptorHeaps& Heaps )
348 VERIFY(Heaps.pSrvCbvUavHeap !=
nullptr || Heaps.pSamplerHeap !=
nullptr,
"At least one heap is expected to be set");
349 VERIFY(Heaps.pSrvCbvUavHeap ==
nullptr || Heaps.pSrvCbvUavHeap->GetDesc().Type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
"Invalid heap type provided in pSrvCbvUavHeap");
350 VERIFY(Heaps.pSamplerHeap ==
nullptr || Heaps.pSamplerHeap->GetDesc().Type == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
"Invalid heap type provided in pSamplerHeap");
353 if (!(Heaps == m_BoundDescriptorHeaps))
355 m_BoundDescriptorHeaps = Heaps;
357 ID3D12DescriptorHeap **ppHeaps =
reinterpret_cast<ID3D12DescriptorHeap**
>(&Heaps);
358 UINT NumHeaps = (ppHeaps[0] !=
nullptr ? 1 : 0) + (ppHeaps[1] !=
nullptr ? 1 : 0);
359 if(ppHeaps[0] ==
nullptr)
362 m_pCommandList->SetDescriptorHeaps(NumHeaps, ppHeaps);
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34