Diligent Engine API Reference
DeviceContextD3D12Impl.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
28 
29 #include "DeviceContextD3D12.h"
30 #include "DeviceContextBase.h"
31 #include "GenerateMips.h"
32 
33 #ifdef _DEBUG
34 # define VERIFY_CONTEXT_BINDINGS
35 #endif
36 
37 namespace Diligent
38 {
39 
41 class DeviceContextD3D12Impl : public DeviceContextBase<IDeviceContextD3D12>
42 {
43 public:
45 
46  DeviceContextD3D12Impl(IReferenceCounters *pRefCounters, class RenderDeviceD3D12Impl *pDevice, bool bIsDeferred, const EngineD3D12Attribs &Attribs, Uint32 ContextId);
48 
49  virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final;
50 
51  virtual void SetPipelineState(IPipelineState *pPipelineState)override final;
52 
53  virtual void TransitionShaderResources(IPipelineState *pPipelineState, IShaderResourceBinding *pShaderResourceBinding)override final;
54 
55  virtual void CommitShaderResources(IShaderResourceBinding *pShaderResourceBinding, Uint32 Flags)override final;
56 
57  virtual void SetStencilRef(Uint32 StencilRef)override final;
58 
59  virtual void SetBlendFactors(const float* pBlendFactors = nullptr)override final;
60 
61  virtual void SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags )override final;
62 
63  virtual void InvalidateState()override final;
64 
65  virtual void SetIndexBuffer( IBuffer *pIndexBuffer, Uint32 ByteOffset )override final;
66 
67  virtual void SetViewports( Uint32 NumViewports, const Viewport *pViewports, Uint32 RTWidth, Uint32 RTHeight )override final;
68 
69  virtual void SetScissorRects( Uint32 NumRects, const Rect *pRects, Uint32 RTWidth, Uint32 RTHeight )override final;
70 
71  virtual void SetRenderTargets( Uint32 NumRenderTargets, ITextureView *ppRenderTargets[], ITextureView *pDepthStencil )override final;
72 
73  virtual void Draw( DrawAttribs &DrawAttribs )override final;
74 
75  virtual void DispatchCompute( const DispatchComputeAttribs &DispatchAttrs )override final;
76 
77  virtual void ClearDepthStencil( ITextureView *pView, Uint32 ClearFlags, float fDepth, Uint8 Stencil)override final;
78 
79  virtual void ClearRenderTarget( ITextureView *pView, const float *RGBA )override final;
80 
81  virtual void Flush()override final;
82 
83  virtual void FinishCommandList(class ICommandList **ppCommandList)override final;
84 
85  virtual void ExecuteCommandList(class ICommandList *pCommandList)override final;
86 
87  virtual void TransitionTextureState(ITexture *pTexture, D3D12_RESOURCE_STATES State)override final;
88 
89  virtual void TransitionBufferState(IBuffer *pBuffer, D3D12_RESOURCE_STATES State)override final;
90 
94  //void ClearShaderStateCache();
95 
97  //static constexpr int NumShaderTypes = 6;
98 
99  void UpdateBufferRegion(class BufferD3D12Impl *pBuffD3D12, struct DynamicAllocation& Allocation, Uint64 DstOffset, Uint64 NumBytes);
100  void UpdateBufferRegion(class BufferD3D12Impl *pBuffD3D12, const void *pData, Uint64 DstOffset, Uint64 NumBytes);
101  void CopyBufferRegion(class BufferD3D12Impl *pSrcBuffD3D12, class BufferD3D12Impl *pDstBuffD3D12, Uint64 SrcOffset, Uint64 DstOffset, Uint64 NumBytes);
102  void CopyTextureRegion(class TextureD3D12Impl *pSrcTexture, Uint32 SrcSubResIndex, const D3D12_BOX *pD3D12SrcBox,
103  class TextureD3D12Impl *pDstTexture, Uint32 DstSubResIndex, Uint32 DstX, Uint32 DstY, Uint32 DstZ);
104  void CopyTextureRegion(IBuffer *pSrcBuffer, Uint32 SrcStride, Uint32 SrcDepthStride, class TextureD3D12Impl *pTextureD3D12, Uint32 DstSubResIndex, const Box &DstBox);
105 
106  void GenerateMips(class TextureViewD3D12Impl *pTexView);
107 
108  struct DynamicAllocation AllocateDynamicSpace(size_t NumBytes);
109 
110  Uint32 GetContextId()const{return m_ContextId;}
111 
112  size_t GetNumCommandsInCtx()const { return m_NumCommandsInCurCtx; }
113 
114 private:
115  void CommitD3D12IndexBuffer(VALUE_TYPE IndexType);
116  void CommitD3D12VertexBuffers(class GraphicsContext &GraphCtx);
117  void TransitionD3D12VertexBuffers(class GraphicsContext &GraphCtx);
118  void CommitRenderTargets();
119  void CommitViewports();
120  void CommitScissorRects();
121  void Flush(bool RequestNewCmdCtx);
122 
123  friend class SwapChainD3D12Impl;
124  inline class CommandContext* RequestCmdContext()
125  {
126  // Make sure that the number of commands in the context is at least one,
127  // so that the context cannot be disposed by Flush()
128  m_NumCommandsInCurCtx = m_NumCommandsInCurCtx != 0 ? m_NumCommandsInCurCtx : 1;
129  return m_pCurrCmdCtx;
130  }
131  size_t m_NumCommandsInCurCtx = 0;
132  const Uint32 m_NumCommandsToFlush = 192;
133  CommandContext* m_pCurrCmdCtx = nullptr;
134 
135  CComPtr<ID3D12Resource> m_CommittedD3D12IndexBuffer;
136  VALUE_TYPE m_CommittedIBFormat = VT_UNDEFINED;
137  Uint32 m_CommittedD3D12IndexDataStartOffset = 0;
138 
139  CComPtr<ID3D12CommandSignature> m_pDrawIndirectSignature;
140  CComPtr<ID3D12CommandSignature> m_pDrawIndexedIndirectSignature;
141  CComPtr<ID3D12CommandSignature> m_pDispatchIndirectSignature;
142 
143  GenerateMipsHelper m_MipsGenerator;
144  class DynamicUploadHeap* m_pUploadHeap = nullptr;
145 
147  bool m_bCommittedD3D12VBsUpToDate = false;
148 
150  bool m_bCommittedD3D12IBUpToDate = false;
151 
152  class ShaderResourceCacheD3D12 *m_pCommittedResourceCache = nullptr;
153 
154  FixedBlockMemoryAllocator m_CmdListAllocator;
155  const Uint32 m_ContextId;
156 };
157 
158 }
virtual void ClearDepthStencil(ITextureView *pView, Uint32 ClearFlags, float fDepth, Uint8 Stencil) override final
Clears a depth-stencil view.
Definition: DeviceContextD3D12Impl.cpp:459
Implementation of the Diligent::IBufferD3D12 interface.
Definition: BufferD3D12Impl.h:42
virtual void SetVertexBuffers(Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags) override final
Base implementation of IDeviceContext::SetVertexBuffers(); validates parameters and caches references...
Definition: DeviceContextD3D12Impl.cpp:552
Undefined type.
Definition: GraphicsTypes.h:41
virtual void FinishCommandList(class ICommandList **ppCommandList) override final
Finishes recording commands and generates a command list.
Definition: DeviceContextD3D12Impl.cpp:809
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
virtual void DispatchCompute(const DispatchComputeAttribs &DispatchAttrs) override final
Executes a dispatch compute command.
Definition: DeviceContextD3D12Impl.cpp:403
Base implementation of the device context.
Definition: DeviceContextBase.h:68
Implementation of the Diligent::IDeviceContext interface.
Definition: DeviceContextD3D12Impl.h:41
Implementation of the Diligent::ISwapChainD3D12 interface.
Definition: SwapChainD3D12Impl.h:39
Describes the viewport.
Definition: DeviceContext.h:376
virtual void CommitShaderResources(IShaderResourceBinding *pShaderResourceBinding, Uint32 Flags) override final
Commits shader resources to the device context.
Definition: DeviceContextD3D12Impl.cpp:182
virtual void TransitionTextureState(ITexture *pTexture, D3D12_RESOURCE_STATES State) override final
Transitions internal D3D12 texture object to a specified state.
Definition: DeviceContextD3D12Impl.cpp:836
virtual void ClearRenderTarget(ITextureView *pView, const float *RGBA) override final
Clears a render target view.
Definition: DeviceContextD3D12Impl.cpp:491
Shader resource binding interface.
Definition: ShaderResourceBinding.h:40
Describes dispatch command arguments.
Definition: DeviceContext.h:312
virtual void SetRenderTargets(Uint32 NumRenderTargets, ITextureView *ppRenderTargets[], ITextureView *pDepthStencil) override final
Binds one or more render targets and the depth-stencil buffer to the pipeline. It also sets the viewp...
Definition: DeviceContextD3D12Impl.cpp:666
Buffer interface.
Definition: Buffer.h:200
Box.
Definition: GraphicsTypes.h:1005
Texture view interface.
Definition: TextureView.h:163
VALUE_TYPE
Value type.
Definition: GraphicsTypes.h:39
Command list interface.
Definition: CommandList.h:42
virtual void TransitionShaderResources(IPipelineState *pPipelineState, IShaderResourceBinding *pShaderResourceBinding) override final
Transitions shader resources to the require states.
Definition: DeviceContextD3D12Impl.cpp:173
Memory allocator that allocates memory in a fixed-size chunks.
Definition: FixedBlockMemoryAllocator.h:50
virtual void SetPipelineState(IPipelineState *pPipelineState) override final
Base implementation of IDeviceContext::SetPipelineState(); caches references to the pipeline state ob...
Definition: DeviceContextD3D12Impl.cpp:117
virtual void ExecuteCommandList(class ICommandList *pCommandList) override final
Executes recorded commands in a command list.
Definition: DeviceContextD3D12Impl.cpp:820
Definition: PipelineState.h:209
virtual void SetViewports(Uint32 NumViewports, const Viewport *pViewports, Uint32 RTWidth, Uint32 RTHeight) override final
Sets an array of viewports.
Definition: DeviceContextD3D12Impl.cpp:596
Defines the draw command attributes.
Definition: DeviceContext.h:212
virtual void SetIndexBuffer(IBuffer *pIndexBuffer, Uint32 ByteOffset) override final
Base implementation of IDeviceContext::SetIndexBuffer(); caches the strong reference to the index buf...
Definition: DeviceContextD3D12Impl.cpp:571
virtual void SetBlendFactors(const float *pBlendFactors=nullptr) override final
Definition: DeviceContextD3D12Impl.cpp:206
virtual void Flush() override final
Flushes the command buffer.
Definition: DeviceContextD3D12Impl.cpp:546
Describes the rectangle.
Definition: DeviceContext.h:420
Implementation of the Diligent::ITextureViewD3D12 interface.
Definition: TextureViewD3D12Impl.h:39
Base implementation of the Diligent::ITextureD3D12 interface.
Definition: TextureD3D12Impl.h:42
virtual void Draw(DrawAttribs &DrawAttribs) override final
Executes a draw command.
Definition: DeviceContextD3D12Impl.cpp:318
virtual void TransitionBufferState(IBuffer *pBuffer, D3D12_RESOURCE_STATES State) override final
Transitions internal D3D12 buffer object to a specified state.
Definition: DeviceContextD3D12Impl.cpp:843
Attributes specific to D3D12 engine.
Definition: GraphicsTypes.h:965
virtual void InvalidateState() override final
Invalidates the cached context state.
Definition: DeviceContextD3D12Impl.cpp:558
virtual void QueryInterface(const Diligent::INTERFACE_ID &IID, IObject **ppInterface) override final
Queries the specific interface, see IObject::QueryInterface() for details.
virtual void SetScissorRects(Uint32 NumRects, const Rect *pRects, Uint32 RTWidth, Uint32 RTHeight) override final
Sets active scissor rects.
Definition: DeviceContextD3D12Impl.cpp:608
virtual void SetStencilRef(Uint32 StencilRef) override final
Sets the stencil reference value.
Definition: DeviceContextD3D12Impl.cpp:198
Texture inteface.
Definition: Texture.h:276
Implementation of the Diligent::IRenderDeviceD3D12 interface.
Definition: RenderDeviceD3D12Impl.h:43