105 #define USE_VARIABLE_HASH_MAP 0 107 #include "unordered_map" 109 #include "ShaderD3DBase.h" 110 #include "ShaderBase.h" 111 #include "HashUtils.h" 112 #include "ShaderResourcesD3D12.h" 113 #include "ShaderResourceCacheD3D12.h" 114 #include "ShaderVariableD3DBase.h" 117 # define VERIFY_SHADER_BINDINGS 134 IMemoryAllocator &ResourceLayoutDataAllocator,
136 Uint32 NumAllowedTypes,
137 ShaderResourceCacheD3D12 &ResourceCache);
152 void Initialize(ID3D12Device *pd3d12Device,
153 const std::shared_ptr<const ShaderResourcesD3D12>& pSrcResources,
154 IMemoryAllocator &LayoutDataAllocator,
156 Uint32 NumAllowedTypes,
157 ShaderResourceCacheD3D12 *pResourceCache,
161 struct SRV_CBV_UAV : ShaderVariableD3DBase<ShaderResourceLayoutD3D12>
163 SRV_CBV_UAV(
const SRV_CBV_UAV&) =
delete;
164 SRV_CBV_UAV(SRV_CBV_UAV&&) =
delete;
165 SRV_CBV_UAV& operator = (
const SRV_CBV_UAV&) =
delete;
166 SRV_CBV_UAV& operator = (SRV_CBV_UAV&&) =
delete;
168 static constexpr Uint32 ResTypeBits = 3;
169 static constexpr Uint32 RootIndBits = 16-ResTypeBits;
170 static constexpr Uint32 RootIndMask = (1 << RootIndBits)-1;
171 static constexpr Uint32 ResTypeMask = (1 << ResTypeBits)-1;
173 static constexpr Uint16 InvalidRootIndex = RootIndMask;
174 static constexpr Uint16 MaxRootIndex = RootIndMask-1;
176 static constexpr Uint32 InvalidSamplerId = 0xFFFF;
177 static constexpr Uint32 MaxSamplerId = InvalidSamplerId-1;
178 static constexpr Uint32 InvalidOffset =
static_cast<Uint32
>(-1);
180 static_assert( static_cast<int>(CachedResourceType::NumTypes) <= ResTypeMask,
"3 bits is not enough to store CachedResourceType");
182 const Uint32 OffsetFromTableStart;
187 ResType_RootIndex(rhs.ResType_RootIndex),
188 SamplerId(static_cast<Uint16>(SamId)),
189 OffsetFromTableStart(rhs.OffsetFromTableStart),
190 ShaderVariableD3DBase<ShaderResourceLayoutD3D12>(ParentLayout, rhs.Attribs)
192 VERIFY(SamId == InvalidSamplerId || SamId <= MaxSamplerId,
"Sampler id exceeds max allowed value (", MaxSamplerId,
")" );
193 VERIFY(rhs.m_ParentResLayout.m_pResources == m_ParentResLayout.m_pResources,
"Incosistent resource references");
194 VERIFY(IsValidOffset(),
"Offset must be valid" );
195 VERIFY(IsValidRootIndex(),
"Root index must be valid" );
199 const D3DShaderResourceAttribs &_Attribs,
200 CachedResourceType ResType,
202 Uint32 _OffsetFromTableStart,
204 ResType_RootIndex( (static_cast<Uint16>(ResType) << RootIndBits) | (RootIndex & RootIndMask)),
205 SamplerId( static_cast<Uint16>(_SamplerId) ),
206 OffsetFromTableStart(_OffsetFromTableStart),
207 ShaderVariableD3DBase<ShaderResourceLayoutD3D12>(ParentLayout, _Attribs)
209 VERIFY(RootIndex == InvalidRootIndex || RootIndex <= MaxRootIndex,
"Root index exceeds max allowed value (", MaxRootIndex,
")" );
210 VERIFY(IsValidOffset(),
"Offset must be valid" );
211 VERIFY(SamplerId == InvalidSamplerId || SamplerId <= MaxSamplerId,
"Sampler id exceeds max allowed value (", MaxSamplerId,
")" );
214 bool IsBound(Uint32 ArrayIndex);
218 virtual void Set(
IDeviceObject *pObject)
override final{ BindResource(pObject, 0,
nullptr); }
220 virtual void SetArray(
IDeviceObject*
const* ppObjects, Uint32 FirstElement, Uint32 NumElements)
override final 222 for(Uint32 Elem = 0; Elem < NumElements; ++Elem)
223 BindResource(ppObjects[Elem], FirstElement+Elem,
nullptr);
226 bool IsValidSampler()
const{
return GetSamplerId() != InvalidSamplerId;}
227 bool IsValidRootIndex()
const{
return GetRootIndex() != InvalidRootIndex;}
228 bool IsValidOffset()
const{
return OffsetFromTableStart != InvalidOffset; }
230 CachedResourceType GetResType()
const 232 return static_cast<CachedResourceType
>( (ResType_RootIndex >> RootIndBits) & ResTypeMask );
234 Uint32 GetRootIndex()
const 236 return ResType_RootIndex & RootIndMask;
239 Uint32 GetSamplerId()
const 245 const Uint16 ResType_RootIndex;
249 const Uint16 SamplerId;
252 ShaderResourceCacheD3D12::Resource& DstRes,
254 D3D12_CPU_DESCRIPTOR_HANDLE ShdrVisibleHeapCPUDescriptorHandle);
256 template<
typename TResourceViewType,
257 typename TViewTypeEnum,
258 typename TBindSamplerProcType>
260 ShaderResourceCacheD3D12::Resource& DstRes,
262 D3D12_CPU_DESCRIPTOR_HANDLE ShdrVisibleHeapCPUDescriptorHandle,
263 TViewTypeEnum dbgExpectedViewType,
264 TBindSamplerProcType BindSamplerProc);
270 Sampler(
const Sampler&) =
delete;
271 Sampler(Sampler&&) =
delete;
272 Sampler& operator = (
const Sampler&) =
delete;
273 Sampler& operator = (Sampler&&) =
delete;
275 const D3DShaderResourceAttribs &Attribs;
278 static constexpr Uint32 InvalidRootIndex =
static_cast<Uint32
>(-1);
279 static constexpr Uint32 InvalidOffset =
static_cast<Uint32
>(-1);
281 const Uint32 RootIndex;
282 const Uint32 OffsetFromTableStart;
285 Attribs(Sam.Attribs),
286 m_ParentResLayout(ParentLayout),
287 RootIndex(Sam.RootIndex),
288 OffsetFromTableStart(Sam.OffsetFromTableStart)
290 VERIFY(Sam.m_ParentResLayout.m_pResources == m_ParentResLayout.m_pResources,
"Incosistent resource references");
291 VERIFY(IsValidRootIndex(),
"Root index must be valid" );
292 VERIFY(IsValidOffset(),
"Offset must be valid" );
295 Sampler(
ShaderResourceLayoutD3D12 &ParentResLayout,
const D3DShaderResourceAttribs &_Attribs, Uint32 _RootIndex, Uint32 _OffsetFromTableStart) :
296 RootIndex(_RootIndex),
297 OffsetFromTableStart(_OffsetFromTableStart),
299 m_ParentResLayout(ParentResLayout)
301 VERIFY(IsValidRootIndex(),
"Root index must be valid" );
302 VERIFY(IsValidOffset(),
"Offset must be valid" );
305 bool IsValidRootIndex()
const{
return RootIndex != InvalidRootIndex;}
306 bool IsValidOffset()
const{
return OffsetFromTableStart != InvalidOffset; }
308 void CacheSampler(
class ITextureViewD3D12 *pTexViewD3D12, Uint32 ArrayIndex, D3D12_CPU_DESCRIPTOR_HANDLE ShdrVisibleHeapCPUDescriptorHandle);
316 void BindResources(
IResourceMapping* pResourceMapping, Uint32 Flags,
const ShaderResourceCacheD3D12 *dbgResourceCache );
320 #ifdef VERIFY_SHADER_BINDINGS 321 void dbgVerifyBindings()
const;
324 IObject& GetOwner(){
return m_Owner;}
327 void InitVariablesHashMap();
329 Sampler &GetAssignedSampler(
const SRV_CBV_UAV &TexSrv);
331 const Char* GetShaderName()
const;
335 ShaderResourceCacheD3D12 *m_pResourceCache;
337 std::unique_ptr<void, STDDeleterRawMem<void> > m_ResourceBuffer;
338 Sampler* m_Samplers =
nullptr;
344 VERIFY_EXPR( r < m_NumCbvSrvUav[VarType] );
353 VERIFY_EXPR( r < m_NumCbvSrvUav[VarType] );
354 auto* CbvSrvUav =
reinterpret_cast<SRV_CBV_UAV*
>(m_ResourceBuffer.get());
355 return CbvSrvUav[GetSrvCbvUavOffset(VarType,r)];
359 VERIFY_EXPR( r < m_NumCbvSrvUav[VarType] );
360 auto* CbvSrvUav =
reinterpret_cast<SRV_CBV_UAV*
>(m_ResourceBuffer.get());
361 return CbvSrvUav[GetSrvCbvUavOffset(VarType,r)];
363 SRV_CBV_UAV& GetSrvCbvUav(Uint32 r)
365 VERIFY_EXPR( r < GetTotalSrvCbvUavCount() );
366 auto* CbvSrvUav =
reinterpret_cast<SRV_CBV_UAV*
>(m_ResourceBuffer.get());
372 VERIFY_EXPR( s < m_NumSamplers[VarType] );
381 VERIFY_EXPR( s < m_NumSamplers[VarType] );
382 return m_Samplers[GetSamplerOffset(VarType,s)];
386 VERIFY_EXPR( s < m_NumSamplers[VarType] );
387 return m_Samplers[GetSamplerOffset(VarType,s)];
389 Uint32 GetTotalSrvCbvUavCount()
const 394 Uint32 GetTotalSamplerCount()
const 400 void AllocateMemory(IMemoryAllocator &Allocator);
402 #if USE_VARIABLE_HASH_MAP 405 typedef std::pair<HashMapStringKey, IShaderVariable*> VariableHashElemType;
406 std::unordered_map<HashMapStringKey, IShaderVariable*, std::hash<HashMapStringKey>, std::equal_to<HashMapStringKey>, STDAllocatorRawMem<VariableHashElemType> > m_VariableHash;
409 CComPtr<ID3D12Device> m_pd3d12Device;
413 std::shared_ptr<const ShaderResourcesD3D12> m_pResources;
Base interface for all objects created by the render device Diligent::IRenderDevice.
Definition: DeviceObject.h:40
Shader resource variable.
Definition: Shader.h:265
Implementation of the Diligent::RootSignature class.
Definition: RootSignature.h:161
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Interface to the texture view object implemented in D3D11.
Definition: TextureViewD3D12.h:39
Resouce mapping.
Definition: ResourceMapping.h:80
SHADER_VARIABLE_TYPE
Describes shader variable type that is used by ShaderVariableDesc.
Definition: Shader.h:100
Shader variable is constant across all shader instances. It must be set once directly through IShader...
Definition: Shader.h:105
Shader variable is dynamic. It can be set multiple times for every instance of shader resource bindin...
Definition: Shader.h:114
Diligent::ShaderResourceLayoutD3D12 class.
Definition: ShaderResourceLayoutD3D12.h:125
Shader variable is constant across shader resource bindings instance (see IShaderResourceBinding). It must be set once through IShaderResourceBinding::BindResources() or through the shader variable. It cannot be set through IShader interface.
Definition: Shader.h:110
Total number of shader variable types.
Definition: Shader.h:117