git.s-ol.nu ~forks/DiligentCore / cbc82d7
Moved static variable cache and managers to PipelineResourceSignatureBase assiduous 6 months ago
17 changed file(s) with 196 addition(s) and 250 deletion(s). Raw diff Collapse all Expand all
6161 m_ReservedSize {Other.m_ReservedSize },
6262 m_CurrAlignment{Other.m_CurrAlignment},
6363 m_pAllocator {Other.m_pAllocator }
64 #if DILIGENT_DEBUG
65 , m_DbgCurrAllocation{Other.m_DbgCurrAllocation}
66 , m_DbgAllocations{std::move(Other.m_DbgAllocations)}
67 #endif
6468 // clang-format on
6569 {
6670 Other.Reset();
3232 #include <array>
3333 #include <limits>
3434 #include <algorithm>
35 #include <memory>
3536
3637 #include "PrivateConstants.h"
3738 #include "PipelineResourceSignature.h"
7879
7980 // Render device implementation type (RenderDeviceD3D12Impl, RenderDeviceVkImpl, etc.).
8081 using RenderDeviceImplType = typename EngineImplTraits::RenderDeviceImplType;
82
83 // Shader resource cache implementation type (ShaderResourceCacheD3D12, ShaderResourceCacheVk, etc.)
84 using ShaderResourceCacheImplType = typename EngineImplTraits::ShaderResourceCacheImplType;
85
86 // Shader variable manager implementation type (ShaderVariableManagerD3D12, ShaderVariableManagerVk, etc.)
87 using ShaderVariableManagerImplType = typename EngineImplTraits::ShaderVariableManagerImplType;
8188
8289 using TDeviceObjectBase = DeviceObjectBase<BaseInterface, RenderDeviceImplType, PipelineResourceSignatureDesc>;
8390
272279 }
273280
274281 protected:
275 void ReserveSpaceForDescription(FixedLinearAllocator& Allocator, const PipelineResourceSignatureDesc& Desc) const noexcept(false)
276 {
282 template <typename TReserveCustomData>
283 FixedLinearAllocator ReserveSpace(IMemoryAllocator& RawAllocator,
284 const PipelineResourceSignatureDesc& Desc,
285 TReserveCustomData ReserveCustomData) noexcept(false)
286 {
287 FixedLinearAllocator Allocator{RawAllocator};
288
277289 Allocator.AddSpace<PipelineResourceDesc>(Desc.NumResources);
278290 Allocator.AddSpace<ImmutableSamplerDesc>(Desc.NumImmutableSamplers);
279291
299311
300312 if (Desc.UseCombinedTextureSamplers)
301313 Allocator.AddSpaceForString(Desc.CombinedSamplerSuffix);
302 }
303
314
315 ReserveCustomData(Allocator);
316
317 const auto NumStaticResStages = GetNumStaticResStages();
318 if (NumStaticResStages > 0)
319 {
320 Allocator.AddSpace<ShaderResourceCacheImplType>(1);
321 Allocator.AddSpace<ShaderVariableManagerImplType>(NumStaticResStages);
322 }
323
324 Allocator.Reserve();
325 // The memory is now owned by PipelineResourceSignatureBase and will be freed by Destruct().
326 m_pRawMemory = decltype(m_pRawMemory){Allocator.ReleaseOwnership(), STDDeleterRawMem<void>{RawAllocator}};
327
328 CopyDescription(Allocator, Desc);
329
330 return Allocator;
331 }
332
333 private:
304334 void CopyDescription(FixedLinearAllocator& Allocator, const PipelineResourceSignatureDesc& Desc) noexcept(false)
305335 {
306336 PipelineResourceDesc* pResources = Allocator.ConstructArray<PipelineResourceDesc>(Desc.NumResources);
354384 this->m_Desc.CombinedSamplerSuffix = Allocator.CopyString(Desc.CombinedSamplerSuffix);
355385 }
356386
387 protected:
357388 void Destruct()
358389 {
359390 VERIFY(!m_IsDestructed, "This object has already been destructed");
362393 this->m_Desc.ImmutableSamplers = nullptr;
363394 this->m_Desc.CombinedSamplerSuffix = nullptr;
364395
396 auto& RawAllocator = GetRawAllocator();
397
398 if (m_StaticVarsMgrs != nullptr)
399 {
400 for (auto Idx : m_StaticResStageIndex)
401 {
402 if (Idx >= 0)
403 {
404 m_StaticVarsMgrs[Idx].Destroy(RawAllocator);
405 m_StaticVarsMgrs[Idx].~ShaderVariableManagerImplType();
406 }
407 }
408 m_StaticVarsMgrs = nullptr;
409 }
410
411 if (m_pStaticResCache != nullptr)
412 {
413 m_pStaticResCache->~ShaderResourceCacheImplType();
414 m_pStaticResCache = nullptr;
415 }
416
365417 m_StaticResStageIndex.fill(-1);
418
419 m_pRawMemory.reset();
366420
367421 #if DILIGENT_DEBUG
368422 m_IsDestructed = true;
369423 #endif
370424 }
371425
372 template <typename ShaderVarManagerType>
373 Uint32 GetStaticVariableCountImpl(SHADER_TYPE ShaderType, const ShaderVarManagerType StaticVarMgrs[]) const
426 Uint32 GetStaticVariableCountImpl(SHADER_TYPE ShaderType) const
374427 {
375428 if (!IsConsistentShaderType(ShaderType, m_PipelineType))
376429 {
385438 return 0;
386439
387440 VERIFY_EXPR(static_cast<Uint32>(VarMngrInd) < GetNumStaticResStages());
388 return StaticVarMgrs[VarMngrInd].GetVariableCount();
389 }
390
391 template <typename ShaderVarManagerType>
392 IShaderResourceVariable* GetStaticVariableByNameImpl(SHADER_TYPE ShaderType, const Char* Name, const ShaderVarManagerType StaticVarMgrs[]) const
441 return m_StaticVarsMgrs[VarMngrInd].GetVariableCount();
442 }
443
444 IShaderResourceVariable* GetStaticVariableByNameImpl(SHADER_TYPE ShaderType, const Char* Name) const
393445 {
394446 if (!IsConsistentShaderType(ShaderType, m_PipelineType))
395447 {
404456 return nullptr;
405457
406458 VERIFY_EXPR(static_cast<Uint32>(VarMngrInd) < GetNumStaticResStages());
407 return StaticVarMgrs[VarMngrInd].GetVariable(Name);
408 }
409
410 template <typename ShaderVarManagerType>
411 IShaderResourceVariable* GetStaticVariableByIndexImpl(SHADER_TYPE ShaderType, Uint32 Index, const ShaderVarManagerType StaticVarMgrs[]) const
459 return m_StaticVarsMgrs[VarMngrInd].GetVariable(Name);
460 }
461
462 IShaderResourceVariable* GetStaticVariableByIndexImpl(SHADER_TYPE ShaderType, Uint32 Index) const
412463 {
413464 if (!IsConsistentShaderType(ShaderType, m_PipelineType))
414465 {
423474 return nullptr;
424475
425476 VERIFY_EXPR(static_cast<Uint32>(VarMngrInd) < GetNumStaticResStages());
426 return StaticVarMgrs[VarMngrInd].GetVariable(Index);
427 }
428
429 template <typename ShaderVarManagerType>
430 void BindStaticResourcesImpl(Uint32 ShaderFlags,
431 IResourceMapping* pResMapping,
432 Uint32 Flags,
433 ShaderVarManagerType StaticVarMgrs[])
477 return m_StaticVarsMgrs[VarMngrInd].GetVariable(Index);
478 }
479
480 void BindStaticResourcesImpl(Uint32 ShaderFlags,
481 IResourceMapping* pResMapping,
482 Uint32 Flags)
434483 {
435484 const auto PipelineType = GetPipelineType();
436485 for (Uint32 ShaderInd = 0; ShaderInd < m_StaticResStageIndex.size(); ++ShaderInd)
443492 const auto ShaderType = GetShaderTypeFromPipelineIndex(ShaderInd, PipelineType);
444493 if (ShaderFlags & ShaderType)
445494 {
446 StaticVarMgrs[VarMngrInd].BindResources(pResMapping, Flags);
495 m_StaticVarsMgrs[VarMngrInd].BindResources(pResMapping, Flags);
447496 }
448497 }
449498 }
502551 }
503552
504553 protected:
554 std::unique_ptr<void, STDDeleterRawMem<void>> m_pRawMemory;
555
556 // Static resource cache for all static resources
557 ShaderResourceCacheImplType* m_pStaticResCache = nullptr;
558
559 // Static variables manager for every shader stage
560 ShaderVariableManagerImplType* m_StaticVarsMgrs = nullptr; // [GetNumStaticResStages()]
561
505562 size_t m_Hash = 0;
506563
507564 // Resource offsets (e.g. index of the first resource), for each variable type.
7373
7474 class FixedBlockMemoryAllocator;
7575
76 class ShaderResourceCacheD3D12;
77 class ShaderVariableManagerD3D12;
78
7679 struct EngineD3D12ImplTraits
7780 {
7881 using RenderDeviceInterface = IRenderDeviceD3D12;
116119
117120 using BuffViewObjAllocatorType = FixedBlockMemoryAllocator;
118121 using TexViewObjAllocatorType = FixedBlockMemoryAllocator;
122
123 using ShaderResourceCacheImplType = ShaderResourceCacheD3D12;
124 using ShaderVariableManagerImplType = ShaderVariableManagerD3D12;
119125 };
120126
121127 } // namespace Diligent
294294 void Destruct();
295295
296296 private:
297 ResourceAttribs* m_pResourceAttribs = nullptr; // [m_Desc.NumResources]
298 ShaderResourceCacheD3D12* m_pStaticResCache = nullptr;
299 ShaderVariableManagerD3D12* m_StaticVarsMgrs = nullptr; // [GetNumStaticResStages()]
300 ImmutableSamplerAttribs* m_ImmutableSamplers = nullptr; // [m_Desc.NumImmutableSamplers]
297 ResourceAttribs* m_pResourceAttribs = nullptr; // [m_Desc.NumResources]
298 ImmutableSamplerAttribs* m_ImmutableSamplers = nullptr; // [m_Desc.NumImmutableSamplers]
301299
302300 RootParamsManager m_RootParams;
303301
138138
139139 try
140140 {
141 FixedLinearAllocator MemPool{GetRawAllocator()};
142
143 // Reserve at least 1 element because m_pResourceAttribs must hold a pointer to memory
144 MemPool.AddSpace<ResourceAttribs>(std::max(1u, Desc.NumResources));
145 MemPool.AddSpace<ImmutableSamplerAttribs>(m_Desc.NumImmutableSamplers);
146
147 ReserveSpaceForDescription(MemPool, Desc);
148
149 const auto NumStaticResStages = GetNumStaticResStages();
150 if (NumStaticResStages > 0)
151 {
152 MemPool.AddSpace<ShaderResourceCacheD3D12>(1);
153 MemPool.AddSpace<ShaderVariableManagerD3D12>(NumStaticResStages);
154 }
155
156 MemPool.Reserve();
141 auto& RawAllocator{GetRawAllocator()};
142 auto MemPool = ReserveSpace(RawAllocator, Desc,
143 [&Desc](FixedLinearAllocator& MemPool) //
144 {
145 MemPool.AddSpace<ResourceAttribs>(Desc.NumResources);
146 MemPool.AddSpace<ImmutableSamplerAttribs>(Desc.NumImmutableSamplers);
147 });
157148
158149 static_assert(std::is_trivially_destructible<ResourceAttribs>::value,
159150 "ResourceAttribs objects must be constructed to be properly destructed in case an excpetion is thrown");
160 m_pResourceAttribs = MemPool.Allocate<ResourceAttribs>(std::max(1u, m_Desc.NumResources));
151 m_pResourceAttribs = MemPool.Allocate<ResourceAttribs>(m_Desc.NumResources);
161152 m_ImmutableSamplers = MemPool.ConstructArray<ImmutableSamplerAttribs>(m_Desc.NumImmutableSamplers);
162
163 // The memory is now owned by PipelineResourceSignatureD3D12Impl and will be freed by Destruct().
164 auto* Ptr = MemPool.ReleaseOwnership();
165 VERIFY_EXPR(Ptr == m_pResourceAttribs);
166 (void)Ptr;
167
168 CopyDescription(MemPool, Desc);
169153
170154 StaticResCacheTblSizesArrayType StaticResCacheTblSizes = {};
171155 AllocateRootParameters(StaticResCacheTblSizes);
172156
157 const auto NumStaticResStages = GetNumStaticResStages();
173158 if (NumStaticResStages > 0)
174159 {
175160 m_pStaticResCache = MemPool.Construct<ShaderResourceCacheD3D12>(CacheContentType::Signature);
177162 // Moreover, all objects must be constructed if an exception is thrown for Destruct() method to work properly.
178163 m_StaticVarsMgrs = MemPool.ConstructArray<ShaderVariableManagerD3D12>(NumStaticResStages, std::ref(*this), std::ref(*m_pStaticResCache));
179164
180 m_pStaticResCache->Initialize(GetRawAllocator(), static_cast<Uint32>(StaticResCacheTblSizes.size()), StaticResCacheTblSizes.data());
165 m_pStaticResCache->Initialize(RawAllocator, static_cast<Uint32>(StaticResCacheTblSizes.size()), StaticResCacheTblSizes.data());
181166
182167 constexpr SHADER_RESOURCE_VARIABLE_TYPE AllowedVarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_STATIC};
183168 for (Uint32 i = 0; i < m_StaticResStageIndex.size(); ++i)
187172 {
188173 VERIFY_EXPR(static_cast<Uint32>(Idx) < NumStaticResStages);
189174 const auto ShaderType = GetShaderTypeFromPipelineIndex(i, GetPipelineType());
190 m_StaticVarsMgrs[Idx].Initialize(*this, GetRawAllocator(), AllowedVarTypes, _countof(AllowedVarTypes), ShaderType);
175 m_StaticVarsMgrs[Idx].Initialize(*this, RawAllocator, AllowedVarTypes, _countof(AllowedVarTypes), ShaderType);
191176 }
192177 }
193178 }
397382
398383 void PipelineResourceSignatureD3D12Impl::Destruct()
399384 {
400 auto& RawAllocator = GetRawAllocator();
401
402 if (m_StaticVarsMgrs != nullptr)
403 {
404 for (auto Idx : m_StaticResStageIndex)
405 {
406 if (Idx >= 0)
407 {
408 m_StaticVarsMgrs[Idx].Destroy(RawAllocator);
409 m_StaticVarsMgrs[Idx].~ShaderVariableManagerD3D12();
410 }
411 }
412 m_StaticVarsMgrs = nullptr;
413 }
414
415 if (m_pStaticResCache != nullptr)
416 {
417 m_pStaticResCache->~ShaderResourceCacheD3D12();
418 m_pStaticResCache = nullptr;
419 }
420
421385 if (m_ImmutableSamplers != nullptr)
422386 {
423387 for (Uint32 i = 0; i < m_Desc.NumImmutableSamplers; ++i)
427391 m_ImmutableSamplers = nullptr;
428392 }
429393
430 if (void* pRawMem = m_pResourceAttribs)
431 {
432 RawAllocator.Free(pRawMem);
433 m_pResourceAttribs = nullptr;
434 }
394 m_pResourceAttribs = nullptr;
435395
436396 TPipelineResourceSignatureBase::Destruct();
437397 }
470430
471431 Uint32 PipelineResourceSignatureD3D12Impl::GetStaticVariableCount(SHADER_TYPE ShaderType) const
472432 {
473 return GetStaticVariableCountImpl(ShaderType, m_StaticVarsMgrs);
433 return GetStaticVariableCountImpl(ShaderType);
474434 }
475435
476436 IShaderResourceVariable* PipelineResourceSignatureD3D12Impl::GetStaticVariableByName(SHADER_TYPE ShaderType, const Char* Name)
477437 {
478 return GetStaticVariableByNameImpl(ShaderType, Name, m_StaticVarsMgrs);
438 return GetStaticVariableByNameImpl(ShaderType, Name);
479439 }
480440
481441 IShaderResourceVariable* PipelineResourceSignatureD3D12Impl::GetStaticVariableByIndex(SHADER_TYPE ShaderType, Uint32 Index)
482442 {
483 return GetStaticVariableByIndexImpl(ShaderType, Index, m_StaticVarsMgrs);
443 return GetStaticVariableByIndexImpl(ShaderType, Index);
484444 }
485445
486446 void PipelineResourceSignatureD3D12Impl::BindStaticResources(Uint32 ShaderFlags,
487447 IResourceMapping* pResMapping,
488448 Uint32 Flags)
489449 {
490 BindStaticResourcesImpl(ShaderFlags, pResMapping, Flags, m_StaticVarsMgrs);
450 BindStaticResourcesImpl(ShaderFlags, pResMapping, Flags);
491451 }
492452
493453 size_t PipelineResourceSignatureD3D12Impl::CalculateHash() const
7070
7171 class FixedBlockMemoryAllocator;
7272
73 class ShaderResourceCacheGL;
74 class ShaderVariableManagerGL;
75
7376 struct EngineGLImplTraits
7477 {
7578 using RenderDeviceInterface = IGLDeviceBaseInterface;
109112
110113 using BuffViewObjAllocatorType = FixedBlockMemoryAllocator;
111114 using TexViewObjAllocatorType = FixedBlockMemoryAllocator;
115
116 using ShaderResourceCacheImplType = ShaderResourceCacheGL;
117 using ShaderVariableManagerImplType = ShaderVariableManagerGL;
112118 };
113119
114120 } // namespace Diligent
213213
214214 ResourceAttribs* m_pResourceAttribs = nullptr; // [m_Desc.NumResources]
215215
216 // Resource cache for static resource variables only
217 ShaderResourceCacheGL* m_pStaticResCache = nullptr;
218
219 ShaderVariableManagerGL* m_StaticVarsMgrs = nullptr; // [GetNumStaticResStages()]
220
221216 using SamplerPtr = RefCntAutoPtr<ISampler>;
222217 SamplerPtr* m_ImmutableSamplers = nullptr; // [m_Desc.NumImmutableSamplers]
223218
8787
8888 ~ShaderVariableManagerGL();
8989
90 void DestroyVariables(IMemoryAllocator& Allocator);
90 void Destroy(IMemoryAllocator& Allocator);
9191
9292 // No copies, only moves are allowed
9393 // clang-format off
129129 {
130130 try
131131 {
132 FixedLinearAllocator MemPool{GetRawAllocator()};
133
134 // Reserve at least 1 element because m_pResourceAttribs must hold a pointer to memory
135 MemPool.AddSpace<ResourceAttribs>(std::max(1u, Desc.NumResources));
136 MemPool.AddSpace<SamplerPtr>(Desc.NumImmutableSamplers);
137
138 ReserveSpaceForDescription(MemPool, Desc);
132 auto& RawAllocator{GetRawAllocator()};
133 auto MemPool = ReserveSpace(RawAllocator, Desc,
134 [&](FixedLinearAllocator& MemPool) //
135 {
136 MemPool.AddSpace<ResourceAttribs>(Desc.NumResources);
137 MemPool.AddSpace<SamplerPtr>(Desc.NumImmutableSamplers);
138 });
139
140 static_assert(std::is_trivially_destructible<ResourceAttribs>::value,
141 "ResourceAttribs objects must be constructed to be properly destructed in case an excpetion is thrown");
142 m_pResourceAttribs = MemPool.Allocate<ResourceAttribs>(m_Desc.NumResources);
143 m_ImmutableSamplers = MemPool.ConstructArray<SamplerPtr>(m_Desc.NumImmutableSamplers);
139144
140145 const auto NumStaticResStages = GetNumStaticResStages();
141146 if (NumStaticResStages > 0)
142147 {
143 MemPool.AddSpace<ShaderResourceCacheGL>(1);
144 MemPool.AddSpace<ShaderVariableManagerGL>(NumStaticResStages);
145 }
146
147 MemPool.Reserve();
148
149 static_assert(std::is_trivially_destructible<ResourceAttribs>::value,
150 "ResourceAttribs objects must be constructed to be properly destructed in case an excpetion is thrown");
151 m_pResourceAttribs = MemPool.Allocate<ResourceAttribs>(std::max(1u, m_Desc.NumResources));
152 m_ImmutableSamplers = MemPool.ConstructArray<SamplerPtr>(m_Desc.NumImmutableSamplers);
153
154 // The memory is now owned by PipelineResourceSignatureGLImpl and will be freed by Destruct().
155 auto* Ptr = MemPool.ReleaseOwnership();
156 VERIFY_EXPR(Ptr == m_pResourceAttribs);
157 (void)Ptr;
158
159 CopyDescription(MemPool, Desc);
160
161 if (NumStaticResStages > 0)
162 {
163148 m_pStaticResCache = MemPool.Construct<ShaderResourceCacheGL>(ShaderResourceCacheGL::CacheContentType::Signature);
164149 m_StaticVarsMgrs = MemPool.ConstructArray<ShaderVariableManagerGL>(NumStaticResStages, std::ref(*this), std::ref(*m_pStaticResCache));
165150 }
176161 {
177162 VERIFY_EXPR(static_cast<Uint32>(Idx) < NumStaticResStages);
178163 const auto ShaderType = GetShaderTypeFromPipelineIndex(i, GetPipelineType());
179 m_StaticVarsMgrs[Idx].Initialize(*this, GetRawAllocator(), AllowedVarTypes, _countof(AllowedVarTypes), ShaderType);
164 m_StaticVarsMgrs[Idx].Initialize(*this, RawAllocator, AllowedVarTypes, _countof(AllowedVarTypes), ShaderType);
180165 }
181166 }
182167 }
324309
325310 void PipelineResourceSignatureGLImpl::Destruct()
326311 {
327 auto& RawAllocator = GetRawAllocator();
328
329312 if (m_ImmutableSamplers != nullptr)
330313 {
331314 for (Uint32 s = 0; s < m_Desc.NumImmutableSamplers; ++s)
334317 m_ImmutableSamplers = nullptr;
335318 }
336319
337 if (m_StaticVarsMgrs)
338 {
339 for (auto Idx : m_StaticResStageIndex)
340 {
341 if (Idx >= 0)
342 {
343 m_StaticVarsMgrs[Idx].DestroyVariables(RawAllocator);
344 m_StaticVarsMgrs[Idx].~ShaderVariableManagerGL();
345 }
346 }
347 m_StaticVarsMgrs = nullptr;
348 }
349
350 if (m_pStaticResCache)
351 {
352 m_pStaticResCache->~ShaderResourceCacheGL();
353 m_pStaticResCache = nullptr;
354 }
355
356 if (void* pRawMem = m_pResourceAttribs)
357 {
358 RawAllocator.Free(pRawMem);
359 m_pResourceAttribs = nullptr;
360 }
320 m_pResourceAttribs = nullptr;
361321
362322 TPipelineResourceSignatureBase::Destruct();
363323 }
518478
519479 Uint32 PipelineResourceSignatureGLImpl::GetStaticVariableCount(SHADER_TYPE ShaderType) const
520480 {
521 return GetStaticVariableCountImpl(ShaderType, m_StaticVarsMgrs);
481 return GetStaticVariableCountImpl(ShaderType);
522482 }
523483
524484 IShaderResourceVariable* PipelineResourceSignatureGLImpl::GetStaticVariableByName(SHADER_TYPE ShaderType, const Char* Name)
525485 {
526 return GetStaticVariableByNameImpl(ShaderType, Name, m_StaticVarsMgrs);
486 return GetStaticVariableByNameImpl(ShaderType, Name);
527487 }
528488
529489 IShaderResourceVariable* PipelineResourceSignatureGLImpl::GetStaticVariableByIndex(SHADER_TYPE ShaderType, Uint32 Index)
530490 {
531 return GetStaticVariableByIndexImpl(ShaderType, Index, m_StaticVarsMgrs);
491 return GetStaticVariableByIndexImpl(ShaderType, Index);
532492 }
533493
534494 void PipelineResourceSignatureGLImpl::BindStaticResources(Uint32 ShaderFlags,
535495 IResourceMapping* pResMapping,
536496 Uint32 Flags)
537497 {
538 BindStaticResourcesImpl(ShaderFlags, pResMapping, Flags, m_StaticVarsMgrs);
498 BindStaticResourcesImpl(ShaderFlags, pResMapping, Flags);
539499 }
540500
541501 void PipelineResourceSignatureGLImpl::CopyStaticResources(ShaderResourceCacheGL& DstResourceCache) const
103103 for (Uint32 s = 0; s < GetNumShaders(); ++s)
104104 {
105105 auto& VarDataAllocator = SRBMemAllocator.GetShaderVariableDataAllocator(s);
106 m_pShaderVarMgrs[s].DestroyVariables(VarDataAllocator);
106 m_pShaderVarMgrs[s].Destroy(VarDataAllocator);
107107 m_pShaderVarMgrs[s].~ShaderVariableManagerGL();
108108 }
109109
198198
199199 ShaderVariableManagerGL::~ShaderVariableManagerGL()
200200 {
201 VERIFY(m_ResourceBuffer == nullptr, "DestroyVariables() has not been called");
202 }
203
204 void ShaderVariableManagerGL::DestroyVariables(IMemoryAllocator& Allocator)
201 VERIFY(m_ResourceBuffer == nullptr, "Destroy() has not been called");
202 }
203
204 void ShaderVariableManagerGL::Destroy(IMemoryAllocator& Allocator)
205205 {
206206 if (m_ResourceBuffer == nullptr)
207207 return;
7373
7474 class FixedBlockMemoryAllocator;
7575
76 class ShaderResourceCacheVk;
77 class ShaderVariableManagerVk;
78
7679 struct EngineVkImplTraits
7780 {
7881 using RenderDeviceInterface = IRenderDeviceVk;
116119
117120 using BuffViewObjAllocatorType = FixedBlockMemoryAllocator;
118121 using TexViewObjAllocatorType = FixedBlockMemoryAllocator;
122
123 using ShaderResourceCacheImplType = ShaderResourceCacheVk;
124 using ShaderVariableManagerImplType = ShaderVariableManagerVk;
119125 };
120126
121127 } // namespace Diligent
303303
304304 void Destruct();
305305
306 void CreateSetLayouts(const CacheOffsetsType& CacheSizes,
307 const BindingCountType& BindingCount);
306 void CreateSetLayouts();
308307
309308 size_t CalculateHash() const;
310309
326325 // accounting for array size.
327326 Uint16 m_DynamicStorageBufferCount = 0;
328327
329 // Static resource cache for all static resources
330 ShaderResourceCacheVk* m_pStaticResCache = nullptr;
331 // Static variables manager for every shader stage
332 ShaderVariableManagerVk* m_StaticVarsMgrs = nullptr; // [GetNumStaticResStages()]
333
334328 ImmutableSamplerAttribs* m_ImmutableSamplers = nullptr; // [m_Desc.NumImmutableSamplers]
335329
336330 SRBMemoryAllocator m_SRBMemAllocator;
8787
8888 ~ShaderVariableManagerVk();
8989
90 void DestroyVariables(IMemoryAllocator& Allocator);
90 void Destroy(IMemoryAllocator& Allocator);
9191
9292 ShaderVariableVkImpl* GetVariable(const Char* Name) const;
9393 ShaderVariableVkImpl* GetVariable(Uint32 Index) const;
7575 case DescriptorType::StorageBufferDynamic_ReadOnly: return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
7676 case DescriptorType::InputAttachment: return VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
7777 case DescriptorType::AccelerationStructure: return VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
78 // clang-format on
78 // clang-format on
7979 default:
8080 UNEXPECTED("Unknown descriptor type");
8181 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
226226 {
227227 try
228228 {
229 FixedLinearAllocator MemPool{GetRawAllocator()};
230
231 // Reserve at least 1 element because m_pResourceAttribs must hold a pointer to memory
232 MemPool.AddSpace<ResourceAttribs>(std::max(1u, Desc.NumResources));
233 MemPool.AddSpace<ImmutableSamplerAttribs>(Desc.NumImmutableSamplers);
234
235 ReserveSpaceForDescription(MemPool, Desc);
236
237 Uint32 StaticResourceCount = 0; // The total number of static resources in all stages
238 // accounting for array sizes.
239
240 CacheOffsetsType CacheGroupSizes = {}; // Required cache size for each cache group
241 BindingCountType BindingCount = {}; // Binding count in each cache group
242
243 for (Uint32 i = 0; i < Desc.NumResources; ++i)
244 {
245 const auto& ResDesc = Desc.Resources[i];
246 const auto CacheGroup = GetResourceCacheGroup(ResDesc);
247
248 if (ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
249 StaticResourceCount += ResDesc.ArraySize;
250
251 BindingCount[CacheGroup] += 1;
252 // Note that we may reserve space for separate immutable samplers, which will never be used, but this is OK.
253 CacheGroupSizes[CacheGroup] += ResDesc.ArraySize;
254 }
229 auto& RawAllocator{GetRawAllocator()};
230 auto MemPool = ReserveSpace(RawAllocator, Desc,
231 [&Desc](FixedLinearAllocator& MemPool) //
232 {
233 MemPool.AddSpace<ResourceAttribs>(Desc.NumResources);
234 MemPool.AddSpace<ImmutableSamplerAttribs>(Desc.NumImmutableSamplers);
235 });
236
237 m_pResourceAttribs = MemPool.Allocate<ResourceAttribs>(m_Desc.NumResources);
238 m_ImmutableSamplers = MemPool.ConstructArray<ImmutableSamplerAttribs>(m_Desc.NumImmutableSamplers);
255239
256240 const auto NumStaticResStages = GetNumStaticResStages();
257241 if (NumStaticResStages > 0)
258242 {
259 MemPool.AddSpace<ShaderResourceCacheVk>(1);
260 MemPool.AddSpace<ShaderVariableManagerVk>(NumStaticResStages);
261 }
262
263 MemPool.Reserve();
264
265 m_pResourceAttribs = MemPool.Allocate<ResourceAttribs>(std::max(1u, m_Desc.NumResources));
266 m_ImmutableSamplers = MemPool.ConstructArray<ImmutableSamplerAttribs>(m_Desc.NumImmutableSamplers);
267
268 // The memory is now owned by PipelineResourceSignatureVkImpl and will be freed by Destruct().
269 auto* Ptr = MemPool.ReleaseOwnership();
270 VERIFY_EXPR(Ptr == m_pResourceAttribs);
271 (void)Ptr;
272
273 CopyDescription(MemPool, Desc);
274
275 if (NumStaticResStages > 0)
276 {
277243 m_pStaticResCache = MemPool.Construct<ShaderResourceCacheVk>(CacheContentType::Signature);
278244 m_StaticVarsMgrs = MemPool.ConstructArray<ShaderVariableManagerVk>(NumStaticResStages, std::ref(*this), std::ref(*m_pStaticResCache));
279245
280 m_pStaticResCache->InitializeSets(GetRawAllocator(), 1, &StaticResourceCount);
281 }
282
283 CreateSetLayouts(CacheGroupSizes, BindingCount);
246 Uint32 StaticResourceCount = 0; // The total number of static resources in all stages
247 // accounting for array sizes.
248 for (Uint32 i = 0; i < Desc.NumResources; ++i)
249 {
250 const auto& ResDesc = Desc.Resources[i];
251 if (ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
252 StaticResourceCount += ResDesc.ArraySize;
253 }
254 m_pStaticResCache->InitializeSets(RawAllocator, 1, &StaticResourceCount);
255 }
256
257 CreateSetLayouts();
284258
285259 if (NumStaticResStages > 0)
286260 {
292266 {
293267 VERIFY_EXPR(static_cast<Uint32>(Idx) < NumStaticResStages);
294268 const auto ShaderType = GetShaderTypeFromPipelineIndex(i, GetPipelineType());
295 m_StaticVarsMgrs[Idx].Initialize(*this, GetRawAllocator(), AllowedVarTypes, _countof(AllowedVarTypes), ShaderType);
269 m_StaticVarsMgrs[Idx].Initialize(*this, RawAllocator, AllowedVarTypes, _countof(AllowedVarTypes), ShaderType);
296270 }
297271 }
298272 }
306280 }
307281 }
308282
309 void PipelineResourceSignatureVkImpl::CreateSetLayouts(const CacheOffsetsType& CacheGroupSizes,
310 const BindingCountType& BindingCount)
311 {
283 void PipelineResourceSignatureVkImpl::CreateSetLayouts()
284 {
285 CacheOffsetsType CacheGroupSizes = {}; // Required cache size for each cache group
286 BindingCountType BindingCount = {}; // Binding count in each cache group
287 for (Uint32 i = 0; i < m_Desc.NumResources; ++i)
288 {
289 const auto& ResDesc = m_Desc.Resources[i];
290 const auto CacheGroup = GetResourceCacheGroup(ResDesc);
291
292 BindingCount[CacheGroup] += 1;
293 // Note that we may reserve space for separate immutable samplers, which will never be used, but this is OK.
294 CacheGroupSizes[CacheGroup] += ResDesc.ArraySize;
295 }
296
312297 // Descriptor set mapping (static/mutable (0) or dynamic (1) -> set index)
313298 std::array<Uint32, DESCRIPTOR_SET_ID_NUM_SETS> DSMapping = {};
314299 {
597582 m_pDevice->SafeReleaseDeviceObject(std::move(Layout), ~0ull);
598583 }
599584
600 auto& RawAllocator = GetRawAllocator();
601
602 if (m_StaticVarsMgrs != nullptr)
603 {
604 for (auto Idx : m_StaticResStageIndex)
605 {
606 if (Idx >= 0)
607 {
608 m_StaticVarsMgrs[Idx].DestroyVariables(RawAllocator);
609 m_StaticVarsMgrs[Idx].~ShaderVariableManagerVk();
610 }
611 }
612 m_StaticVarsMgrs = nullptr;
613 }
614
615 if (m_pStaticResCache != nullptr)
616 {
617 m_pStaticResCache->~ShaderResourceCacheVk();
618 m_pStaticResCache = nullptr;
619 }
620
621585 if (m_ImmutableSamplers != nullptr)
622586 {
623587 for (Uint32 i = 0; i < m_Desc.NumImmutableSamplers; ++i)
627591 m_ImmutableSamplers = nullptr;
628592 }
629593
630 if (void* pRawMem = m_pResourceAttribs)
631 {
632 RawAllocator.Free(pRawMem);
633 m_pResourceAttribs = nullptr;
634 }
594 m_pResourceAttribs = nullptr;
635595
636596 TPipelineResourceSignatureBase::Destruct();
637597 }
670630
671631 Uint32 PipelineResourceSignatureVkImpl::GetStaticVariableCount(SHADER_TYPE ShaderType) const
672632 {
673 return GetStaticVariableCountImpl(ShaderType, m_StaticVarsMgrs);
633 return GetStaticVariableCountImpl(ShaderType);
674634 }
675635
676636 IShaderResourceVariable* PipelineResourceSignatureVkImpl::GetStaticVariableByName(SHADER_TYPE ShaderType, const Char* Name)
677637 {
678 return GetStaticVariableByNameImpl(ShaderType, Name, m_StaticVarsMgrs);
638 return GetStaticVariableByNameImpl(ShaderType, Name);
679639 }
680640
681641 IShaderResourceVariable* PipelineResourceSignatureVkImpl::GetStaticVariableByIndex(SHADER_TYPE ShaderType, Uint32 Index)
682642 {
683 return GetStaticVariableByIndexImpl(ShaderType, Index, m_StaticVarsMgrs);
643 return GetStaticVariableByIndexImpl(ShaderType, Index);
684644 }
685645
686646 void PipelineResourceSignatureVkImpl::BindStaticResources(Uint32 ShaderFlags,
687647 IResourceMapping* pResMapping,
688648 Uint32 Flags)
689649 {
690 BindStaticResourcesImpl(ShaderFlags, pResMapping, Flags, m_StaticVarsMgrs);
650 BindStaticResourcesImpl(ShaderFlags, pResMapping, Flags);
691651 }
692652
693653 void PipelineResourceSignatureVkImpl::InitSRBResourceCache(ShaderResourceCacheVk& ResourceCache,
104104 for (Uint32 s = 0; s < GetNumShaders(); ++s)
105105 {
106106 auto& VarDataAllocator = SRBMemAllocator.GetShaderVariableDataAllocator(s);
107 m_pShaderVarMgrs[s].DestroyVariables(VarDataAllocator);
107 m_pShaderVarMgrs[s].Destroy(VarDataAllocator);
108108 m_pShaderVarMgrs[s].~ShaderVariableManagerVk();
109109 }
110110
123123
124124 ShaderVariableManagerVk::~ShaderVariableManagerVk()
125125 {
126 VERIFY(m_pVariables == nullptr, "DestroyVariables() has not been called");
127 }
128
129 void ShaderVariableManagerVk::DestroyVariables(IMemoryAllocator& Allocator)
126 VERIFY(m_pVariables == nullptr, "Destroy() has not been called");
127 }
128
129 void ShaderVariableManagerVk::Destroy(IMemoryAllocator& Allocator)
130130 {
131131 if (m_pVariables != nullptr)
132132 {