Diligent Engine API Reference
RenderDeviceD3D12Impl.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 "RenderDeviceD3D12.h"
30 #include "RenderDeviceD3DBase.h"
31 #include "DescriptorHeap.h"
32 #include "CommandListManager.h"
33 #include "CommandContext.h"
34 #include "DynamicUploadHeap.h"
35 #include "Atomics.h"
36 #include "CommandQueueD3D12.h"
37 
39 namespace Diligent
40 {
41 
43 class RenderDeviceD3D12Impl : public RenderDeviceD3DBase<IRenderDeviceD3D12>
44 {
45 public:
46  typedef RenderDeviceD3DBase<IRenderDeviceD3D12> TRenderDeviceBase;
47 
48  RenderDeviceD3D12Impl( IReferenceCounters *pRefCounters, IMemoryAllocator &RawMemAllocator, const EngineD3D12Attribs &CreationAttribs, ID3D12Device *pD3D12Device, ICommandQueueD3D12 *pCmdQueue, Uint32 NumDeferredContexts );
50 
51  virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final;
52 
53  virtual void CreatePipelineState( const PipelineStateDesc &PipelineDesc, IPipelineState **ppPipelineState )override final;
54 
55  virtual void CreateBuffer(const BufferDesc& BuffDesc, const BufferData &BuffData, IBuffer **ppBuffer)override final;
56 
57  virtual void CreateShader(const ShaderCreationAttribs &ShaderCreationAttribs, IShader **ppShader)override final;
58 
59  virtual void CreateTexture(const TextureDesc& TexDesc, const TextureData &Data, ITexture **ppTexture)override final;
60 
61  void CreateTexture(const TextureDesc& TexDesc, ID3D12Resource *pd3d12Texture, class TextureD3D12Impl **ppTexture);
62 
63  virtual void CreateSampler(const SamplerDesc& SamplerDesc, ISampler **ppSampler)override final;
64 
65  virtual ID3D12Device* GetD3D12Device()override final{return m_pd3d12Device;}
66 
67  virtual void CreateTextureFromD3DResource(ID3D12Resource *pd3d12Texture, ITexture **ppTexture)override final;
68 
69  virtual void CreateBufferFromD3DResource(ID3D12Resource *pd3d12Buffer, const BufferDesc& BuffDesc, IBuffer **ppBuffer)override final;
70 
71  DescriptorHeapAllocation AllocateDescriptor( D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count = 1 );
72  DescriptorHeapAllocation AllocateGPUDescriptors( D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count = 1 );
73 
74  Uint64 GetCompletedFenceValue();
75  virtual Uint64 GetNextFenceValue() override final
76  {
77  return m_pCommandQueue->GetNextFenceValue();
78  }
79 
80  Uint64 GetCurrentFrameNumber()const {return static_cast<Uint64>(m_FrameNumber);}
81  virtual Bool IsFenceSignaled(Uint64 FenceValue) override final;
82 
83  ICommandQueueD3D12 *GetCmdQueue(){return m_pCommandQueue;}
84 
85  void IdleGPU(bool ReleaseStaleObjects);
86  CommandContext* AllocateCommandContext(const Char *ID = "");
87  void CloseAndExecuteCommandContext(CommandContext *pCtx, bool DiscardStaleObjects);
88  void DisposeCommandContext(CommandContext*);
89 
90  void SafeReleaseD3D12Object(ID3D12Object* pObj);
91  void FinishFrame(bool ReleaseAllResources);
92  virtual void FinishFrame()override final { FinishFrame(false); }
93 
94  DynamicUploadHeap* RequestUploadHeap();
95  void ReleaseUploadHeap(DynamicUploadHeap* pUploadHeap);
96 
97 private:
98  virtual void TestTextureFormat( TEXTURE_FORMAT TexFormat );
99  void ProcessReleaseQueue(Uint64 CompletedFenceValue);
100  void DiscardStaleD3D12Objects(Uint64 CmdListNumber, Uint64 FenceValue);
101 
103  CComPtr<ID3D12Device> m_pd3d12Device;
104  RefCntAutoPtr<ICommandQueueD3D12> m_pCommandQueue;
105 
106  EngineD3D12Attribs m_EngineAttribs;
107 
108  CPUDescriptorHeap m_CPUDescriptorHeaps[D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES];
109  GPUDescriptorHeap m_GPUDescriptorHeaps[2]; // D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV == 0
110  // D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER == 1
111 
112  const Uint32 m_DynamicDescriptorAllocationChunkSize[2];
113 
114  std::mutex m_CmdQueueMutex;
115 
116  Atomics::AtomicInt64 m_FrameNumber;
117  Atomics::AtomicInt64 m_NextCmdListNumber;
118 
119  // The following basic requirement guarantees correctness of resource deallocation:
120  //
121  // A resource is never released before the last draw command referencing it is invoked on the immediate context
122  //
123  // See http://diligentgraphics.com/diligent-engine/architecture/d3d12/managing-resource-lifetimes/
124 
125  //
126  // CPU
127  // Last Reference
128  // of resource X
129  // |
130  // | Submit Cmd Submit Cmd Submit Cmd
131  // | List N List N+1 List N+2
132  // V | | |
133  // NextFenceValue | * N | N+1 | N+2 |
134  //
135  //
136  // CompletedFenceValue | N-3 | N-2 | N-1 | N |
137  // . . . . .
138  // -----------------------------.--------------.---------------.-------------------.----------------.-------------
139  // . . . . .
140  //
141  // GPU | Cmd List N-2 | Cmd List N-1 | Cmd List N | Cmd List N+1 |
142  // |
143  // |
144  // Resource X can
145  // be released
146 
147  CommandListManager m_CmdListManager;
148 
149  typedef std::unique_ptr<CommandContext, STDDeleterRawMem<CommandContext> > ContextPoolElemType;
150  std::vector< ContextPoolElemType, STDAllocatorRawMem<ContextPoolElemType> > m_ContextPool;
151 
152  std::deque<CommandContext*, STDAllocatorRawMem<CommandContext*> > m_AvailableContexts;
153  std::mutex m_ContextAllocationMutex;
154 
155  std::mutex m_ReleaseQueueMutex;
156  typedef std::pair<Uint64, CComPtr<ID3D12Object> > ReleaseQueueElemType;
157  std::deque< ReleaseQueueElemType, STDAllocatorRawMem<ReleaseQueueElemType> > m_D3D12ObjReleaseQueue;
158 
159  std::mutex m_StaleObjectsMutex;
160  std::deque< ReleaseQueueElemType, STDAllocatorRawMem<ReleaseQueueElemType> > m_StaleD3D12Objects;
161 
162  std::mutex m_UploadHeapMutex;
163  typedef std::unique_ptr<DynamicUploadHeap, STDDeleterRawMem<DynamicUploadHeap> > UploadHeapPoolElemType;
164  std::vector< UploadHeapPoolElemType, STDAllocatorRawMem<UploadHeapPoolElemType> > m_UploadHeaps;
165 };
166 
167 }
TEXTURE_FORMAT
Texture formats.
Definition: GraphicsTypes.h:244
Texture description.
Definition: Texture.h:82
Shader interface.
Definition: Shader.h:288
Describes the buffer initial data.
Definition: Buffer.h:176
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Describes the initial data to store in the texture.
Definition: Texture.h:243
Sampler description.
Definition: Sampler.h:52
Buffer interface.
Definition: Buffer.h:200
Buffer description.
Definition: Buffer.h:57
Command queue interface.
Definition: CommandQueueD3D12.h:37
Definition: PipelineState.h:209
Template class that implements reference counting.
Definition: RefCntAutoPtr.h:71
Texture sampler interface.
Definition: Sampler.h:180
Base implementation of the Diligent::ITextureD3D12 interface.
Definition: TextureD3D12Impl.h:42
Attributes specific to D3D12 engine.
Definition: GraphicsTypes.h:965
Pipeline state description.
Definition: PipelineState.h:179
Shader creation attributes.
Definition: Shader.h:204
Texture inteface.
Definition: Texture.h:276
Implementation of the Diligent::IRenderDeviceD3D12 interface.
Definition: RenderDeviceD3D12Impl.h:43