Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.63 KB | None | 0 0
  1. #ifndef _GFX_D3D12_COMMAND_LIST_HPP_
  2. #define _GFX_D3D12_COMMAND_LIST_HPP_
  3.  
  4. #include "d3d12_command_allocator.hpp"
  5. #include "d3d12_pipeline_state_object.hpp"
  6. #include "d3d12_index_buffer.hpp"
  7. #include "d3d12_vertex_buffer.hpp"
  8. #include "d3d12_root_signature.hpp"
  9. #include "d3d12_descriptor_heap.hpp"
  10.  
  11. class GFXCommandListInterfaceD3D12 {
  12. public:
  13.   ID3D12GraphicsCommandList* GetInterface() { return pCommandList.Raw(); }
  14. protected:
  15.   UniqueCOM<ID3D12GraphicsCommandList> pCommandList;
  16. };
  17.  
  18. using GFXCommandListInterface = GFXCommandListInterfaceD3D12;
  19.  
  20. template<GFXCommandListType type>
  21. class GFXCommandListCommonCommandsD3D12 : public virtual GFXCommandListInterfaceD3D12 {
  22. public:
  23.     bool Init  (GFXCommandAllocatorD3D12<type>& cmdAllocator, GFXPipelineStateObjectD3D12* pso = nullptr)
  24.   {
  25.     ID3D12PipelineState* d3dPso = pso != nullptr ? pso->GetInterface() : nullptr;
  26.  
  27.     HRESULT hr = S_OK;
  28.     if (FAILED(hr = pGfxD3DDevice->GetInterface()->CreateCommandList(0, static_cast<D3D12_COMMAND_LIST_TYPE>(type), cmdAllocator.GetInterface(), d3dPso, IID_PPV_ARGS(&pCommandList)))) {
  29.       AppLogError(D3D12_SUBSYSTEM, SPrintf("Failed to create command list. HRESULT: %d", hr).c_str());
  30.     }
  31.  
  32.     return true;
  33.   }
  34.  
  35.   void Term  (void)
  36.   {
  37.     pCommandList.Reset();
  38.   }
  39.  
  40.   void Close (void)
  41.   {
  42.     if (HRESULT hr; FAILED(hr = pCommandList->Close())) {
  43.         AppLogError(D3D12_SUBSYSTEM, SPrintf("Failed to close command list. HRESULT: %d", hr).c_str());
  44.     }
  45.   }
  46.  
  47.   void Reset (GFXCommandAllocatorD3D12<type>& cmdAllocator, GFXPipelineStateObjectD3D12* pso = nullptr)
  48.   {
  49.     ID3D12PipelineState* d3dPso = pso != nullptr ? static_cast<GFXPipelineStateObjectD3D12*>(pso)->GetInterface() : nullptr;
  50.  
  51.     if (HRESULT hr; FAILED(hr = pCommandList->Reset(cmdAllocator.GetInterface(), d3dPso))) {
  52.       AppLogError(D3D12_SUBSYSTEM, "Failed to reset command list");
  53.     }
  54.   }
  55. };
  56.  
  57. class GFXCommandListCopyComputeGraphicsCommonD3D12 : public virtual GFXCommandListInterfaceD3D12 {
  58. public:
  59.   void ResourceBarrier(U32 numBarriers, const GFXResourceBarrier* pBarriers)
  60.   {
  61.     static_assert(SizeOf<D3D12_RESOURCE_BARRIER>() == SizeOf<GFXResourceBarrier>(), "GFXResourceBarrier don't match d312 interface");
  62.     const D3D12_RESOURCE_BARRIER* pD3DBarriers = reinterpret_cast<const D3D12_RESOURCE_BARRIER*>(pBarriers);
  63.     pCommandList->ResourceBarrier(numBarriers, pD3DBarriers);
  64.   }
  65. };
  66.  
  67.  
  68. class GFXCommandListCopyCommandsD3D12 : public virtual GFXCommandListCopyComputeGraphicsCommonD3D12 {
  69. public:
  70.   void CopyBufferRegion();
  71.   void CopyResource();
  72.   void CopyTextureRegion();
  73.   void CopyTiles();
  74. };
  75.  
  76.  
  77. class GFXCommandListComputeGraphicsCommonD3D12 : public virtual GFXCommandListInterfaceD3D12 {
  78. public:
  79.   void ClearState(void);
  80.   void DiscardResource(void);
  81.  
  82.   void SetDescriptorHeaps(GFXDescriptorHeapD3D12* pDescriptorHeaps, U32 numHeaps)
  83.   {
  84.     ID3D12DescriptorHeap* pD3DDescHeaps[8];
  85.     ASSERT(numHeaps <= 8);
  86.  
  87.     for (U32 i = 0; i < numHeaps; ++i) {
  88.       pD3DDescHeaps[i] = pDescriptorHeaps[i].GetInterface();
  89.     }
  90.  
  91.     pCommandList->SetDescriptorHeaps(numHeaps, pD3DDescHeaps);
  92.   }
  93.  
  94.   void SetPipelineState(void);
  95.   void SetPredication(void);
  96. };
  97.  
  98.  
  99. class GFXCommandListComputeCommandsD3D12 : public virtual GFXCommandListComputeGraphicsCommonD3D12 {
  100. public:
  101.   void ClearUnorderedAccessViewFloat();
  102.   void ClearUnorderedAccessViewUint();
  103.   void Dispatch();
  104.   void ExecuteIndirect();
  105.   void SetComputeRoot32BitConstant();
  106.   void SetComputeRoot32BitConstants();
  107.   void SetComputeRootConstantBufferView();
  108.   void SetComputeRootDescriptorTable();
  109.   void SetComputeRootShaderResourceView();
  110.   void SetComputeRootSignature();
  111.   void SetComputeRootUnorderedAccessView();
  112. };
  113.  
  114. using GFXCommandListComputeCommands = GFXCommandListComputeCommandsD3D12;
  115.  
  116.  
  117. class GFXCommandListGraphicsCommandsD3D12 : public virtual GFXCommandListComputeGraphicsCommonD3D12 {
  118. public:
  119.   void BeginEvent(void);
  120.   void BeginQuery(void);
  121.  
  122.   void ClearDepthStencilView(const GFXDescriptorCPUHandle& descriptor, Float depth, U8 stencil, U32 numRects, const GFXRect* pRects)
  123.   {
  124.     const D3D12_CPU_DESCRIPTOR_HANDLE& pD3DDescriptor = reinterpret_cast<const D3D12_CPU_DESCRIPTOR_HANDLE&>(descriptor);
  125.     const D3D12_RECT*                  pD3DRects = reinterpret_cast<const D3D12_RECT*>(pRects);
  126.     pCommandList->ClearDepthStencilView(pD3DDescriptor, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, depth, stencil, numRects, pD3DRects);
  127.   }
  128.  
  129.   void ClearRenderTargetView(const GFXDescriptorCPUHandle& descriptor, const Float4& color, U32 numRects, const GFXRect* pRects)
  130.   {
  131.     const D3D12_CPU_DESCRIPTOR_HANDLE& pD3DDescriptor = reinterpret_cast<const D3D12_CPU_DESCRIPTOR_HANDLE&>(descriptor);
  132.     const D3D12_RECT*                  pD3DRects = reinterpret_cast<const D3D12_RECT*>(pRects);
  133.     pCommandList->ClearRenderTargetView(pD3DDescriptor, color.elements, numRects, pD3DRects);
  134.   }
  135.  
  136.   void DrawIndexedInstanced(U32 indexCountPerInstance, U32 instanceCount, U32 startIndexLocation, I32 baseVertexOffset, U32 startInstanceLocation)
  137.   {
  138.     pCommandList->DrawIndexedInstanced(indexCountPerInstance, instanceCount, startIndexLocation, baseVertexOffset, startInstanceLocation);
  139.   }
  140.  
  141.   void DrawInstanced(U32 vertexCountPerInstance, U32 instanceCount, U32 startVertexLocation, U32 startInstanceLocation)
  142.   {
  143.     pCommandList->DrawInstanced(vertexCountPerInstance, instanceCount, startVertexLocation, startInstanceLocation);
  144.   }
  145.  
  146.   void EndEvent(void);
  147.   void EndQuery(void);
  148.   void ExecuteBundle(void);
  149.  
  150.   void IASetIndexBuffer(const GFXIndexBufferD3D12& indexBuffer)
  151.   {
  152.     auto view = indexBuffer.GetView();
  153.     pCommandList->IASetIndexBuffer(&view);
  154.   }
  155.  
  156.   void IASetPrimitiveTopology(GFXPrimitiveTopology topology)
  157.   {
  158.     pCommandList->IASetPrimitiveTopology(static_cast<D3D12_PRIMITIVE_TOPOLOGY>(topology));
  159.   }
  160.  
  161.   void IASetVertexBuffer(const GFXVertexBufferD3D12& vertexBuffer)
  162.   {
  163.     auto view = vertexBuffer.GetView();
  164.     pCommandList->IASetVertexBuffers(0, 1, &view);
  165.   }
  166.  
  167.   void OMSetBlendFactor(void);
  168.  
  169.   void OMSetRenderTargets(U32 numRTs, const GFXDescriptorCPUHandle* pRTDescriptors, bool signleHandleToDescriptorRange, const GFXDescriptorCPUHandle* pDSDescriptor)
  170.   {
  171.     const D3D12_CPU_DESCRIPTOR_HANDLE* pD3DDescriptorsRT = reinterpret_cast<const D3D12_CPU_DESCRIPTOR_HANDLE*>(pRTDescriptors);
  172.     const D3D12_CPU_DESCRIPTOR_HANDLE* pD3DDectiptorDS = reinterpret_cast<const D3D12_CPU_DESCRIPTOR_HANDLE*>(pDSDescriptor);
  173.     pCommandList->OMSetRenderTargets(numRTs, pD3DDescriptorsRT, signleHandleToDescriptorRange, pD3DDectiptorDS);
  174.   }
  175.  
  176.   void OMSetStencilRef(void);
  177.   void ResolveQueryData(void);
  178.   void ResolveSubresource(void);
  179.  
  180.   void RSSetScissorRects(U32 numRects, const GFXRect* pRects)
  181.   {
  182.     static_assert(SizeOf<D3D12_RECT>() == SizeOf<GFXRect>(), "GFXRect don't match d3d12 interface");
  183.     const D3D12_RECT* pD3D12Rects = reinterpret_cast<const D3D12_RECT*>(pRects);
  184.     pCommandList->RSSetScissorRects(numRects, pD3D12Rects);
  185.   }
  186.  
  187.   void RSSetViewports(U32 numViewports, const GFXViewport* pViewports)
  188.   {
  189.     static_assert(SizeOf<D3D12_VIEWPORT>() == SizeOf<GFXViewport>(), "GFXViewport don't match d3d12 interface");
  190.     const D3D12_VIEWPORT* pD3DViewports = reinterpret_cast<const D3D12_VIEWPORT*>(pViewports);
  191.     pCommandList->RSSetViewports(numViewports, pD3DViewports);
  192.   }
  193.  
  194.   void SetGraphicsRoot32BitConstant(void);
  195.   void SetGraphicsRoot32BitConstants(void);
  196.  
  197.   void SetGraphicsRootConstantBufferView(U32 rootParameterIndex, GFXGPUVirutalAddress gpuVirtualAddress)
  198.   {
  199.     pCommandList->SetGraphicsRootConstantBufferView(rootParameterIndex, gpuVirtualAddress);
  200.   }
  201.  
  202.   void SetGraphicsRootDescriptorTable(U32 rootParameterIndex, GFXDescriptorGPUHandle handle)
  203.   {
  204.     D3D12_GPU_DESCRIPTOR_HANDLE d3dHandle;
  205.     d3dHandle.ptr = handle.ptr;
  206.     pCommandList->SetGraphicsRootDescriptorTable(rootParameterIndex, d3dHandle);
  207.   }
  208.  
  209.   void SetGraphicsRootShaderResourceView(void);
  210.  
  211.   void SetGraphicsRootSignature(GFXRootSignatureD3D12& rootSignature)
  212.   {
  213.     pCommandList->SetGraphicsRootSignature(rootSignature.GetInterface());
  214.   }
  215.   void SetGraphicsRootUnorderedAccessView(void);
  216.   void SetMarker(void);
  217.   void SOSetTargets(void);
  218. };
  219.  
  220. using GFXCommandListGraphicsCommands = GFXCommandListGraphicsCommandsD3D12;
  221.  
  222. template<GFXCommandListType type>
  223. class GFXCommandListD3D12;
  224.  
  225. template<>
  226. class GFXCommandListD3D12<GFXCommandListType::Direct> : public virtual GFXCommandListCommonCommandsD3D12<GFXCommandListType::Direct>,
  227.                                                         public virtual GFXCommandListComputeCommandsD3D12,
  228.                                                         public virtual GFXCommandListGraphicsCommandsD3D12,
  229.                                                         public virtual GFXCommandListCopyCommandsD3D12
  230. {};
  231.  
  232. using GFXDirectCommandList = GFXCommandListD3D12<GFXCommandListType::Direct>;
  233.  
  234. template<>
  235. class GFXCommandListD3D12<GFXCommandListType::Compute> : public virtual GFXCommandListCommonCommandsD3D12<GFXCommandListType::Compute>,
  236.                                                          public virtual GFXCommandListComputeCommandsD3D12,
  237.                                                          public virtual GFXCommandListCopyCommandsD3D12
  238. {};
  239.  
  240. using GFXComputeCommandList = GFXCommandListD3D12<GFXCommandListType::Compute>;
  241.  
  242. template<>
  243. class GFXCommandListD3D12<GFXCommandListType::Copy> : public virtual GFXCommandListCommonCommandsD3D12<GFXCommandListType::Copy>,
  244.                                                       public virtual GFXCommandListCopyCommandsD3D12
  245. {};
  246.  
  247. using GFXCopyCommandList = GFXCommandListD3D12<GFXCommandListType::Copy>;
  248.  
  249. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement