Diligent Engine API Reference
DeviceContextD3D11Impl.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 "DeviceContextD3D11.h"
30 #include "DeviceContextBase.h"
31 #include "ShaderD3D11Impl.h"
32 
33 #ifdef _DEBUG
34 # define VERIFY_CONTEXT_BINDINGS
35 #endif
36 
37 namespace Diligent
38 {
39 
41 class DeviceContextD3D11Impl : public DeviceContextBase<IDeviceContextD3D11>
42 {
43 public:
45 
46  DeviceContextD3D11Impl(IReferenceCounters *pRefCounters, IMemoryAllocator &Allocator, IRenderDevice *pDevice, ID3D11DeviceContext *pd3d11DeviceContext, const struct EngineD3D11Attribs &EngineAttribs, bool bIsDeferred);
47  virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final;
48 
49  virtual void SetPipelineState(IPipelineState *pPipelineState)override final;
50 
51  virtual void TransitionShaderResources(IPipelineState *pPipelineState, IShaderResourceBinding *pShaderResourceBinding)override final;
52 
53  virtual void CommitShaderResources(IShaderResourceBinding *pShaderResourceBinding, Uint32 Flags)override final;
54 
55  virtual void SetStencilRef(Uint32 StencilRef)override final;
56 
57  virtual void SetBlendFactors(const float* pBlendFactors = nullptr)override final;
58 
59  virtual void SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags )override final;
60 
61  virtual void InvalidateState()override final;
62 
63  virtual void SetIndexBuffer( IBuffer *pIndexBuffer, Uint32 ByteOffset )override final;
64 
65  virtual void SetViewports( Uint32 NumViewports, const Viewport *pViewports, Uint32 RTWidth, Uint32 RTHeight )override final;
66 
67  virtual void SetScissorRects( Uint32 NumRects, const Rect *pRects, Uint32 RTWidth, Uint32 RTHeight )override final;
68 
69  virtual void SetRenderTargets( Uint32 NumRenderTargets, ITextureView *ppRenderTargets[], ITextureView *pDepthStencil )override final;
70 
71  virtual void Draw( DrawAttribs &DrawAttribs )override final;
72 
73  virtual void DispatchCompute( const DispatchComputeAttribs &DispatchAttrs )override final;
74 
75  virtual void ClearDepthStencil( ITextureView *pView, Uint32 ClearFlags, float fDepth, Uint8 Stencil)override final;
76 
77  virtual void ClearRenderTarget( ITextureView *pView, const float *RGBA )override final;
78 
79  virtual void Flush()override final;
80 
81  void FinishCommandList(class ICommandList **ppCommandList)override final;
82 
83  virtual void ExecuteCommandList(class ICommandList *pCommandList)override final;
84 
85  ID3D11DeviceContext* GetD3D11DeviceContext(){ return m_pd3d11DeviceContext; }
86 
87  void CommitRenderTargets();
88 
96 
98  static constexpr int NumShaderTypes = 6;
99 
100 private:
101 
103  void CommitD3D11IndexBuffer(VALUE_TYPE IndexType);
104 
106  void CommitD3D11VertexBuffers(class PipelineStateD3D11Impl *pPipelineStateD3D11);
107 
109  template<typename TD3D11ResourceViewType,
110  typename TSetD3D11View,
111  size_t NumSlots>
112  void UnbindResourceView(TD3D11ResourceViewType CommittedD3D11ViewsArr[][NumSlots],
113  ID3D11Resource* CommittedD3D11ResourcesArr[][NumSlots],
114  Uint8 NumCommittedResourcesArr[],
115  IDeviceObject *pResToUnbind,
116  ID3D11Resource *pd3d11ResToUndind,
117  TSetD3D11View SetD3D11ViewMethods[]);
118 
122  void UnbindTextureFromInput(TextureBaseD3D11 *pTexture, ID3D11Resource *pd3d11Resource);
123 
128  void UnbindBufferFromInput(BufferD3D11Impl *pBuffer, ID3D11Resource *pd3d11Buffer);
129 
133  void UnbindResourceFromUAV(IDeviceObject *pResource, ID3D11Resource *pd3d11Resource);
134 
136  void UnbindTextureFromRenderTarget(TextureBaseD3D11 *pResource);
137 
139  void UnbindTextureFromDepthStencil(TextureBaseD3D11 *pTexD3D11);
140 
141  template<bool TransitionResources,
142  bool CommitResources>
143  void TransitionAndCommitShaderResources(IPipelineState *pPSO, IShaderResourceBinding *pShaderResourceBinding);
144 
145  void ClearStateCache();
146 
147  CComPtr<ID3D11DeviceContext> m_pd3d11DeviceContext;
148 
152  ID3D11Buffer* m_CommittedD3D11CBs [NumShaderTypes][D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT];
153 
157  ID3D11ShaderResourceView* m_CommittedD3D11SRVs [NumShaderTypes][D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
158 
162  ID3D11SamplerState* m_CommittedD3D11Samplers[NumShaderTypes][D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
163 
167  ID3D11UnorderedAccessView* m_CommittedD3D11UAVs [NumShaderTypes][D3D11_PS_CS_UAV_REGISTER_COUNT];
168 
172  ID3D11Resource* m_CommittedD3D11SRVResources [NumShaderTypes][D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
173 
177  ID3D11Resource* m_CommittedD3D11UAVResources [NumShaderTypes][D3D11_PS_CS_UAV_REGISTER_COUNT];
178 
179  Uint8 m_NumCommittedCBs[NumShaderTypes] = {};
180  Uint8 m_NumCommittedSRVs[NumShaderTypes]= {};
181  Uint8 m_NumCommittedSamplers[NumShaderTypes]= {};
182  Uint8 m_NumCommittedUAVs[NumShaderTypes]= {};
183 
188  ID3D11Buffer* m_CommittedD3D11VertexBuffers[MaxBufferSlots] = {};
190  UINT m_CommittedD3D11VBStrides[MaxBufferSlots];
192  UINT m_CommittedD3D11VBOffsets[MaxBufferSlots];
194  UINT m_NumCommittedD3D11VBs;
196  bool m_bCommittedD3D11VBsUpToDate = false;
197 
201  ID3D11InputLayout *m_CommittedD3D11InputLayout = nullptr;
202 
205  CComPtr<ID3D11Buffer> m_CommittedD3D11IndexBuffer;
207  VALUE_TYPE m_CommittedIBFormat;
209  Uint32 m_CommittedD3D11IndexDataStartOffset;
211  bool m_bCommittedD3D11IBUpToDate = false;
212 
213  D3D11_PRIMITIVE_TOPOLOGY m_CommittedD3D11PrimTopology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
214  PRIMITIVE_TOPOLOGY m_CommittedPrimitiveTopology = PRIMITIVE_TOPOLOGY_UNDEFINED;
215 
217  CComPtr<ID3D11DeviceChild> m_CommittedD3DShaders[NumShaderTypes];
218 
219  Uint32 m_DebugFlags;
220 
221  FixedBlockMemoryAllocator m_CmdListAllocator;
222 
223 #ifdef VERIFY_CONTEXT_BINDINGS
224  void dbgVerifyRenderTargetFormats();
226 
228  template<UINT MaxResources, typename TD3D11ResourceType, typename TGetD3D11ResourcesType>
229  void dbgVerifyCommittedResources(TD3D11ResourceType CommittedD3D11ResourcesArr[][MaxResources],
230  Uint8 NumCommittedResourcesArr[],
231  TGetD3D11ResourcesType GetD3D11ResMethods[],
232  const Char *ResourceName,
233  SHADER_TYPE ShaderType);
234 
236  template<UINT MaxResources, typename TD3D11ViewType>
237  void dbgVerifyViewConsistency(TD3D11ViewType CommittedD3D11ViewArr[][MaxResources],
238  ID3D11Resource* CommittedD3D11ResourcesArr[][MaxResources],
239  Uint8 NumCommittedResourcesArr[],
240  const Char *ResourceName,
241  SHADER_TYPE ShaderType);
242 
245  void dbgVerifyCommittedSRVs(SHADER_TYPE ShaderType = SHADER_TYPE_UNKNOWN);
246 
249  void dbgVerifyCommittedUAVs(SHADER_TYPE ShaderType = SHADER_TYPE_UNKNOWN);
250 
253  void dbgVerifyCommittedSamplers(SHADER_TYPE ShaderType = SHADER_TYPE_UNKNOWN);
254 
257  void dbgVerifyCommittedCBs(SHADER_TYPE ShaderType = SHADER_TYPE_UNKNOWN);
258 
262  void dbgVerifyCommittedIndexBuffer();
263 
267  void dbgVerifyCommittedVertexBuffers();
268 
272  void dbgVerifyCommittedShaders();
273 
274 #else
275  #define dbgVerifyRenderTargetFormats(...)
276  #define dbgVerifyCommittedSRVs(...)
277  #define dbgVerifyCommittedUAVs(...)
278  #define dbgVerifyCommittedSamplers(...)
279  #define dbgVerifyCommittedCBs(...)
280  #define dbgVerifyCommittedIndexBuffer(...)
281  #define dbgVerifyCommittedVertexBuffers(...)
282  #define dbgVerifyCommittedShaders(...)
283 
284 #endif
285 };
286 
287 }
static constexpr int NumShaderTypes
Number of different shader types (Vertex, Pixel, Geometry, Domain, Hull, Compute) ...
Definition: DeviceContextD3D11Impl.h:98
static constexpr Uint32 MaxBufferSlots
Maximum number of input buffer slots. D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT == 32...
Definition: Constants.h:33
Render device interface.
Definition: RenderDevice.h:55
Base interface for all objects created by the render device Diligent::IRenderDevice.
Definition: DeviceObject.h:40
PRIMITIVE_TOPOLOGY
Input primitive topology.
Definition: DeviceContext.h:56
Base implementation of the Diligent::ITextureD3D11 interface.
Definition: TextureBaseD3D11.h:50
virtual void SetViewports(Uint32 NumViewports, const Viewport *pViewports, Uint32 RTWidth, Uint32 RTHeight) override final
Sets an array of viewports.
Definition: DeviceContextD3D11Impl.cpp:864
virtual void Flush() override final
Flushes the command buffer.
Definition: DeviceContextD3D11Impl.cpp:847
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: DeviceContextD3D11Impl.cpp:852
ID3D11DeviceContext * GetD3D11DeviceContext()
Returns a pointer to the ID3D11DeviceContext interface of the internal Direct3D11 object...
Definition: DeviceContextD3D11Impl.h:85
virtual void ClearDepthStencil(ITextureView *pView, Uint32 ClearFlags, float fDepth, Uint8 Stencil) override final
Clears a depth-stencil view.
Definition: DeviceContextD3D11Impl.cpp:779
SHADER_TYPE
Describes the shader type.
Definition: Shader.h:46
virtual void Draw(DrawAttribs &DrawAttribs) override final
Executes a draw command.
Definition: DeviceContextD3D11Impl.cpp:660
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Base implementation of the device context.
Definition: DeviceContextBase.h:68
Describes the viewport.
Definition: DeviceContext.h:376
virtual void SetBlendFactors(const float *pBlendFactors=nullptr) override final
Definition: DeviceContextD3D11Impl.cpp:548
Shader resource binding interface.
Definition: ShaderResourceBinding.h:40
void FinishCommandList(class ICommandList **ppCommandList) override final
Finishes recording commands and generates a command list.
Definition: DeviceContextD3D11Impl.cpp:1243
Undefined topology.
Definition: DeviceContext.h:59
virtual void DispatchCompute(const DispatchComputeAttribs &DispatchAttrs) override final
Executes a dispatch compute command.
Definition: DeviceContextD3D11Impl.cpp:746
Describes dispatch command arguments.
Definition: DeviceContext.h:312
virtual void SetIndexBuffer(IBuffer *pIndexBuffer, Uint32 ByteOffset) override final
Base implementation of IDeviceContext::SetIndexBuffer(); caches the strong reference to the index buf...
Definition: DeviceContextD3D11Impl.cpp:858
virtual void SetScissorRects(Uint32 NumRects, const Rect *pRects, Uint32 RTWidth, Uint32 RTHeight) override final
Sets active scissor rects.
Definition: DeviceContextD3D11Impl.cpp:888
Buffer interface.
Definition: Buffer.h:200
Implementation of the Diligent::IPipelineStateD3D11 interface.
Definition: PipelineStateD3D11Impl.h:40
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: DeviceContextD3D11Impl.cpp:1158
virtual void ExecuteCommandList(class ICommandList *pCommandList) override final
Executes recorded commands in a command list.
Definition: DeviceContextD3D11Impl.cpp:1275
Texture view interface.
Definition: TextureView.h:163
VALUE_TYPE
Value type.
Definition: GraphicsTypes.h:39
Command list interface.
Definition: CommandList.h:42
Unknown shader type.
Definition: Shader.h:48
void ReleaseCommittedShaderResources()
Clears the committed shader resource cache. This function is called once per frame (before present) t...
Definition: DeviceContextD3D11Impl.cpp:1214
Definition: PipelineState.h:209
Defines the draw command attributes.
Definition: DeviceContext.h:212
virtual void SetStencilRef(Uint32 StencilRef) override final
Sets the stencil reference value.
Definition: DeviceContextD3D11Impl.cpp:538
Describes the rectangle.
Definition: DeviceContext.h:420
virtual void InvalidateState() override final
Invalidates the cached context state.
Definition: DeviceContextD3D11Impl.cpp:1351
virtual void ClearRenderTarget(ITextureView *pView, const float *RGBA) override final
Clears a render target view.
Definition: DeviceContextD3D11Impl.cpp:812
Implementation of the Diligent::IBufferD3D11 interface.
Definition: BufferD3D11Impl.h:51
virtual void CommitShaderResources(IShaderResourceBinding *pShaderResourceBinding, Uint32 Flags) override final
Commits shader resources to the device context.
Definition: DeviceContextD3D11Impl.cpp:527
virtual void QueryInterface(const Diligent::INTERFACE_ID &IID, IObject **ppInterface) override final
Queries the specific interface, see IObject::QueryInterface() for details.
virtual void TransitionShaderResources(IPipelineState *pPipelineState, IShaderResourceBinding *pShaderResourceBinding) override final
Transitions shader resources to the require states.
Definition: DeviceContextD3D11Impl.cpp:522
Implementation of the Diligent::IDeviceContextD3D11 interface.
Definition: DeviceContextD3D11Impl.h:41
Attributes of the Direct3D11-based engine implementation.
Definition: EngineD3D11Attribs.h:51
virtual void SetPipelineState(IPipelineState *pPipelineState) override final
Base implementation of IDeviceContext::SetPipelineState(); caches references to the pipeline state ob...
Definition: DeviceContextD3D11Impl.cpp:73