82 #include "DescriptorHeap.h" 87 enum class CachedResourceType : Int32
99 class ShaderResourceCacheD3D12
103 enum DbgCacheContentType
105 StaticShaderResources,
109 ShaderResourceCacheD3D12(DbgCacheContentType dbgContentType)
111 : m_DbgContentType(dbgContentType)
116 ~ShaderResourceCacheD3D12();
118 void Initialize(IMemoryAllocator &MemAllocator, Uint32 NumTables, Uint32 TableSizes[]);
120 static constexpr Uint32 InvalidDescriptorOffset =
static_cast<Uint32
>(-1);
125 CachedResourceType Type = CachedResourceType::Unknown;
128 D3D12_CPU_DESCRIPTOR_HANDLE CPUDescriptorHandle = {0};
129 RefCntAutoPtr<IDeviceObject> pObject;
135 RootTable(Uint32 NumResources, Resource *pResources) :
136 m_NumResources(NumResources),
137 m_pResources(pResources)
140 inline Resource& GetResource(Uint32 OffsetFromTableStart,
141 const D3D12_DESCRIPTOR_HEAP_TYPE dbgDescriptorHeapType,
144 VERIFY(m_dbgHeapType == dbgDescriptorHeapType,
"Incosistent descriptor heap type" );
145 VERIFY(m_dbgShaderType == dbgRefShaderType,
"Incosistent shader type" );
147 VERIFY(OffsetFromTableStart < m_NumResources,
"Root table at index is not large enough to store descriptor at offset ", OffsetFromTableStart );
148 return m_pResources[OffsetFromTableStart];
151 inline Uint32 GetSize()
const{
return m_NumResources; }
154 Uint32 m_TableStartOffset = InvalidDescriptorOffset;
157 void SetDebugAttribs(Uint32 MaxOffset,
158 const D3D12_DESCRIPTOR_HEAP_TYPE dbgDescriptorHeapType,
161 VERIFY_EXPR(m_NumResources == MaxOffset);
162 m_dbgHeapType = dbgDescriptorHeapType;
163 m_dbgShaderType = dbgRefShaderType;
166 D3D12_DESCRIPTOR_HEAP_TYPE DbgGetHeapType()
const{
return m_dbgHeapType;}
169 const Uint32 m_NumResources = 0;
173 D3D12_DESCRIPTOR_HEAP_TYPE m_dbgHeapType = D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES;
177 Resource*
const m_pResources =
nullptr;
180 inline RootTable& GetRootTable(Uint32 RootIndex)
182 VERIFY_EXPR(RootIndex < m_NumTables);
183 return reinterpret_cast<RootTable*
>(m_pMemory)[RootIndex];
186 inline Uint32 GetNumRootTables()
const{
return m_NumTables; }
188 void SetDescriptorHeapSpace(DescriptorHeapAllocation &&CbcSrvUavHeapSpace, DescriptorHeapAllocation &&SamplerHeapSpace)
190 VERIFY(m_SamplerHeapSpace.GetCpuHandle().ptr == 0 && m_CbvSrvUavHeapSpace.GetCpuHandle().ptr == 0,
"Space has already been allocated in GPU descriptor heaps");
192 Uint32 NumSamplerDescriptors = 0, NumSrvCbvUavDescriptors = 0;
193 for (Uint32 rt = 0; rt < m_NumTables; ++rt)
195 auto &Tbl = GetRootTable(rt);
196 if(Tbl.m_TableStartOffset != InvalidDescriptorOffset)
198 if(Tbl.DbgGetHeapType() == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)
200 VERIFY(Tbl.m_TableStartOffset == NumSrvCbvUavDescriptors,
"Descriptor space allocation is not continuous");
201 NumSrvCbvUavDescriptors = std::max(NumSrvCbvUavDescriptors, Tbl.m_TableStartOffset + Tbl.GetSize());
205 VERIFY(Tbl.m_TableStartOffset == NumSamplerDescriptors,
"Descriptor space allocation is not continuous");
206 NumSamplerDescriptors = std::max(NumSamplerDescriptors, Tbl.m_TableStartOffset + Tbl.GetSize());
210 VERIFY(NumSrvCbvUavDescriptors == CbcSrvUavHeapSpace.GetNumHandles() || NumSrvCbvUavDescriptors == 0 && CbcSrvUavHeapSpace.GetCpuHandle(0).ptr == 0,
"Unexpected descriptor heap allocation size" );
211 VERIFY(NumSamplerDescriptors == SamplerHeapSpace.GetNumHandles() || NumSamplerDescriptors == 0 && SamplerHeapSpace.GetCpuHandle(0).ptr == 0,
"Unexpected descriptor heap allocation size" );
214 m_CbvSrvUavHeapSpace = std::move(CbcSrvUavHeapSpace);
215 m_SamplerHeapSpace = std::move(SamplerHeapSpace);
218 ID3D12DescriptorHeap* GetSrvCbvUavDescriptorHeap(){
return m_CbvSrvUavHeapSpace.GetDescriptorHeap();}
219 ID3D12DescriptorHeap* GetSamplerDescriptorHeap() {
return m_SamplerHeapSpace.GetDescriptorHeap();}
222 template<D3D12_DESCRIPTOR_HEAP_TYPE HeapType>
223 D3D12_CPU_DESCRIPTOR_HANDLE GetShaderVisibleTableCPUDescriptorHandle(Uint32 RootParamInd, Uint32 OffsetFromTableStart = 0)
225 auto &RootParam = GetRootTable(RootParamInd);
226 VERIFY(HeapType == RootParam.DbgGetHeapType(),
"Invalid descriptor heap type");
228 D3D12_CPU_DESCRIPTOR_HANDLE CPUDescriptorHandle = {0};
232 if( RootParam.m_TableStartOffset != InvalidDescriptorOffset )
234 VERIFY(RootParam.m_TableStartOffset + OffsetFromTableStart < RootParam.m_NumResources,
"Offset is out of range");
235 if( HeapType == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER )
237 VERIFY_EXPR(!m_SamplerHeapSpace.IsNull());
238 CPUDescriptorHandle = m_SamplerHeapSpace.GetCpuHandle(RootParam.m_TableStartOffset + OffsetFromTableStart);
240 else if( HeapType == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV )
242 VERIFY_EXPR(!m_CbvSrvUavHeapSpace.IsNull());
243 CPUDescriptorHandle = m_CbvSrvUavHeapSpace.GetCpuHandle(RootParam.m_TableStartOffset + OffsetFromTableStart);
247 UNEXPECTED(
"Unexpected descriptor heap type");
251 return CPUDescriptorHandle;
255 template<D3D12_DESCRIPTOR_HEAP_TYPE HeapType>
256 D3D12_GPU_DESCRIPTOR_HANDLE GetShaderVisibleTableGPUDescriptorHandle(Uint32 RootParamInd, Uint32 OffsetFromTableStart = 0)
258 auto &RootParam = GetRootTable(RootParamInd);
259 VERIFY(RootParam.m_TableStartOffset != InvalidDescriptorOffset,
"GPU descriptor handle must never be requested for dynamic resources");
260 VERIFY(RootParam.m_TableStartOffset + OffsetFromTableStart < RootParam.m_NumResources,
"Offset is out of range");
262 D3D12_GPU_DESCRIPTOR_HANDLE GPUDescriptorHandle = {0};
263 VERIFY( HeapType == RootParam.DbgGetHeapType(),
"Invalid descriptor heap type");
264 if( HeapType == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER )
266 VERIFY_EXPR(!m_SamplerHeapSpace.IsNull());
267 GPUDescriptorHandle = m_SamplerHeapSpace.GetGpuHandle(RootParam.m_TableStartOffset + OffsetFromTableStart);
269 else if( HeapType == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV )
271 VERIFY_EXPR(!m_CbvSrvUavHeapSpace.IsNull());
272 GPUDescriptorHandle = m_CbvSrvUavHeapSpace.GetGpuHandle(RootParam.m_TableStartOffset + OffsetFromTableStart);
276 UNEXPECTED(
"Unexpected descriptor heap type");
279 return GPUDescriptorHandle;
284 DbgCacheContentType DbgGetContentType()
const{
return m_DbgContentType;}
288 ShaderResourceCacheD3D12(
const ShaderResourceCacheD3D12&) =
delete;
289 ShaderResourceCacheD3D12(ShaderResourceCacheD3D12&&) =
delete;
290 ShaderResourceCacheD3D12& operator = (
const ShaderResourceCacheD3D12&) =
delete;
291 ShaderResourceCacheD3D12& operator = (ShaderResourceCacheD3D12&&) =
delete;
294 DescriptorHeapAllocation m_SamplerHeapSpace;
297 DescriptorHeapAllocation m_CbvSrvUavHeapSpace;
299 IMemoryAllocator *m_pAllocator=
nullptr;
300 void *m_pMemory =
nullptr;
301 Uint32 m_NumTables = 0;
305 const DbgCacheContentType m_DbgContentType;
SHADER_TYPE
Describes the shader type.
Definition: Shader.h:46
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Unknown shader type.
Definition: Shader.h:48