Diligent Engine  v.2.4.g
ShaderVariableManagerD3D12.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 //
34 // * ShaderVariableManagerD3D12 keeps the list of variables of specific types
35 // * Every ShaderVariableD3D12Impl references ResourceAttribs by index from PipelineResourceSignatureD3D12Impl
36 // * ShaderVariableManagerD3D12 keeps reference to ShaderResourceCacheD3D12
37 // * ShaderVariableManagerD3D12 is used by PipelineResourceSignatureD3D12Impl to manage static resources and by
38 // ShaderResourceBindingD3D12Impl to manage mutable and dynamic resources
39 //
40 // _____________________________ ________________________________________________________________________________
41 // | | | | | |
42 // .----| ShaderVariableManagerD3D12 |---------------->| ShaderVariableD3D12Impl[0] | ShaderVariableD3D12Impl[1] | ... |
43 // | |_____________________________| |______________________________|_______________________________|_________________|
44 // | | | |
45 // | m_pSignature m_ResIndex m_ResIndex
46 // | | | |
47 // | _____________V____________________ __________V_______________________________V_________________________________
48 // | | | m_pResourceAttribs | | | | |
49 // | |PipelineResourceSignatureD3D12Impl|------------------->| Resource[0] | Resource[1] | ... | Resource[s+m+d-1] |
50 // | |__________________________________| |__________________|__________________|_____________|________________________|
51 // | | |
52 // m_ResourceCache | |
53 // | | (RootTable, Offset) / (RootTable, Offset)
54 // | \ /
55 // | __________________________ _______________V________________________________________________V_______
56 // | | | | |
57 // '--->| ShaderResourceCacheD3D12 |---------------->| Resources |
58 // |__________________________| |________________________________________________________________________|
59 //
60 
64 
65 namespace Diligent
66 {
67 
68 class ShaderVariableD3D12Impl;
69 class ShaderResourceCacheD3D12;
70 class PipelineResourceSignatureD3D12Impl;
71 
72 // sizeof(ShaderVariableManagerD3D12) == 40 (x64, msvc, Release)
74 {
75 public:
77  ShaderResourceCacheD3D12& ResourceCache) noexcept :
78  m_Owner{Owner},
79  m_ResourceCache{ResourceCache}
80  {}
81 
82  // clang-format off
87  // clang-format on
88 
89  void Initialize(const PipelineResourceSignatureD3D12Impl& Signature,
90  IMemoryAllocator& Allocator,
91  const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
92  Uint32 NumAllowedTypes,
93  SHADER_TYPE ShaderStages);
95 
96  void Destroy(IMemoryAllocator& Allocator);
97 
98  ShaderVariableD3D12Impl* GetVariable(const Char* Name) const;
99  ShaderVariableD3D12Impl* GetVariable(Uint32 Index) const;
100 
101  // Binds object pObj to resource with index ResIndex and array index ArrayIndex.
102  void BindResource(IDeviceObject* pObj,
103  Uint32 ArrayIndex,
104  Uint32 ResIndex);
105 
106  bool IsBound(Uint32 ArrayIndex,
107  Uint32 ResIndex) const;
108 
109  void BindResources(IResourceMapping* pResourceMapping, Uint32 Flags);
110 
111  static size_t GetRequiredMemorySize(const PipelineResourceSignatureD3D12Impl& Signature,
112  const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
113  Uint32 NumAllowedTypes,
114  SHADER_TYPE ShaderStages,
115  Uint32& NumVariables);
116 
117  Uint32 GetVariableCount() const { return m_NumVariables; }
118 
119  IObject& GetOwner() { return m_Owner; }
120 
121 private:
124 
125  using ResourceAttribs = PipelineResourceAttribsD3D12;
126 
127  Uint32 GetVariableIndex(const ShaderVariableD3D12Impl& Variable);
128 
129  // These methods can't be defined in the header due to dependency on PipelineResourceSignatureD3D12Impl
130  const PipelineResourceDesc& GetResourceDesc(Uint32 Index) const;
131  const ResourceAttribs& GetResourceAttribs(Uint32 Index) const;
132 
133 private:
134  PipelineResourceSignatureD3D12Impl const* m_pSignature = nullptr;
135 
136  IObject& m_Owner;
137 
138  // Variable manager is owned by either Pipeline Resource Signature (in which case m_ResourceCache references
139  // static resource cache owned by the same signature object), or by SRB object (in which case
140  // m_ResourceCache references the cache in the SRB). Thus the cache and the signature
141  // (which the variables reference) are guaranteed to be alive while the manager is alive.
142  ShaderResourceCacheD3D12& m_ResourceCache;
143 
144  // Memory is allocated through the allocator provided by the pipeline resource signature. If allocation
145  // granularity > 1, fixed block memory allocator is used. This ensures that all resources from different
146  // shader resource bindings reside in continuous memory. If allocation granularity == 1, raw allocator is used.
147  ShaderVariableD3D12Impl* m_pVariables = nullptr;
148  Uint32 m_NumVariables = 0;
149 
150 #ifdef DILIGENT_DEBUG
151  IMemoryAllocator* m_pDbgAllocator = nullptr;
152 #endif
153 };
154 
155 // sizeof(ShaderVariableD3D12Impl) == 24 (x64)
156 class ShaderVariableD3D12Impl final : public ShaderVariableBase<ShaderVariableD3D12Impl, ShaderVariableManagerD3D12, IShaderResourceVariableD3D>
157 {
158 public:
161  Uint32 ResIndex) :
162  TBase{ParentManager, ResIndex}
163  {}
164 
165  // clang-format off
170  // clang-format on
171 
172  virtual void DILIGENT_CALL_TYPE QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface) override final
173  {
174  if (ppInterface == nullptr)
175  return;
176 
177  *ppInterface = nullptr;
178  if (IID == IID_ShaderResourceVariableD3D || IID == IID_ShaderResourceVariable || IID == IID_Unknown)
179  {
180  *ppInterface = this;
181  (*ppInterface)->AddRef();
182  }
183  }
184 
185  virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final
186  {
187  return m_ParentManager.IsBound(ArrayIndex, m_ResIndex);
188  }
189 
190  virtual void DILIGENT_CALL_TYPE GetHLSLResourceDesc(HLSLShaderResourceDesc& HLSLResDesc) const override final
191  {
192  GetResourceDesc(HLSLResDesc);
193  HLSLResDesc.ShaderRegister = GetAttribs().Register;
194  }
195 
197  {
198  return m_ParentManager.GetResourceDesc(m_ResIndex);
199  }
200 
201  void BindResource(IDeviceObject* pObj, Uint32 ArrayIndex) const
202  {
203  m_ParentManager.BindResource(pObj, ArrayIndex, m_ResIndex);
204  }
205 
206 private:
207  using ResourceAttribs = PipelineResourceAttribsD3D12;
208  const ResourceAttribs& GetAttribs() const
209  {
210  return m_ParentManager.GetResourceAttribs(m_ResIndex);
211  }
212 };
213 
214 } // namespace Diligent
Diligent::ShaderVariableBase< ShaderVariableD3D12Impl, ShaderVariableManagerD3D12, IShaderResourceVariableD3D >
Diligent::Char
char Char
Definition: BasicTypes.h:64
Diligent::ShaderVariableD3D12Impl::QueryInterface
virtual void QueryInterface(const INTERFACE_ID &IID, IObject **ppInterface) override final
Definition: ShaderVariableManagerD3D12.hpp:172
Diligent::ShaderVariableManagerD3D12::~ShaderVariableManagerD3D12
~ShaderVariableManagerD3D12()
Definition: ShaderVariableManagerD3D12.cpp:117
Diligent::SHADER_TYPE
SHADER_TYPE
Describes the shader type.
Definition: GraphicsTypes.h:65
Flags
Uint32 Flags
Definition: DXBCUtils.cpp:71
Diligent::PipelineResourceSignatureD3D12Impl
Implementation of the Diligent::PipelineResourceSignatureD3D12Impl class.
Definition: PipelineResourceSignatureD3D12Impl.hpp:55
Diligent::IObject
Base interface for all dynamic objects in the engine.
Definition: Object.h:41
Diligent::HLSLShaderResourceDesc
HLSL resource description.
Definition: ShaderD3D.h:46
Diligent::ShaderVariableD3D12Impl
Definition: ShaderVariableManagerD3D12.hpp:156
Diligent::ShaderVariableD3D12Impl::IsBound
virtual bool IsBound(Uint32 ArrayIndex) const override final
Definition: ShaderVariableManagerD3D12.hpp:185
ShaderResourceVariableBase.hpp
Diligent::ShaderVariableD3D12Impl::ShaderVariableD3D12Impl
ShaderVariableD3D12Impl(ShaderVariableManagerD3D12 &ParentManager, Uint32 ResIndex)
Definition: ShaderVariableManagerD3D12.hpp:160
Diligent::PipelineResourceAttribsD3D12
Definition: PipelineResourceAttribsD3D12.hpp:43
Diligent::IDeviceObject
Base interface for all objects created by the render device Diligent::IRenderDevice.
Definition: DeviceObject.h:52
Diligent::ShaderVariableD3D12Impl::GetHLSLResourceDesc
virtual void GetHLSLResourceDesc(HLSLShaderResourceDesc &HLSLResDesc) const override final
Definition: ShaderVariableManagerD3D12.hpp:190
Diligent::INTERFACE_ID
Unique interface identifier.
Definition: InterfaceID.h:37
Diligent::ShaderVariableManagerD3D12::operator=
ShaderVariableManagerD3D12 & operator=(const ShaderVariableManagerD3D12 &)=delete
Diligent::ShaderVariableManagerD3D12::Initialize
void Initialize(const PipelineResourceSignatureD3D12Impl &Signature, IMemoryAllocator &Allocator, const SHADER_RESOURCE_VARIABLE_TYPE *AllowedVarTypes, Uint32 NumAllowedTypes, SHADER_TYPE ShaderStages)
Definition: ShaderVariableManagerD3D12.cpp:86
Diligent::ShaderResourceCacheD3D12
Definition: ShaderResourceCacheD3D12.hpp:119
Diligent::ShaderVariableD3D12Impl::BindResource
void BindResource(IDeviceObject *pObj, Uint32 ArrayIndex) const
Definition: ShaderVariableManagerD3D12.hpp:201
Diligent::ShaderVariableD3D12Impl::GetDesc
const PipelineResourceDesc & GetDesc() const
Definition: ShaderVariableManagerD3D12.hpp:196
PipelineResourceAttribsD3D12.hpp
DILIGENT_CALL_TYPE
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
Diligent::PipelineResourceDesc
Pipeline resource description.
Definition: PipelineResourceSignature.h:120
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::ShaderVariableBase< ShaderVariableD3D12Impl, ShaderVariableManagerD3D12, IShaderResourceVariableD3D >::GetResourceDesc
virtual void GetResourceDesc(ShaderResourceDesc &ResourceDesc) const override final
Definition: ShaderResourceVariableBase.hpp:580
Diligent::ShaderVariableManagerD3D12::BindResource
void BindResource(IDeviceObject *pObj, Uint32 ArrayIndex, Uint32 ResIndex)
Definition: ShaderVariableManagerD3D12.cpp:613
Diligent::ShaderVariableManagerD3D12::ShaderVariableManagerD3D12
ShaderVariableManagerD3D12(IObject &Owner, ShaderResourceCacheD3D12 &ResourceCache) noexcept
Definition: ShaderVariableManagerD3D12.hpp:76
Diligent::ShaderVariableManagerD3D12::Destroy
void Destroy(IMemoryAllocator &Allocator)
Definition: ShaderVariableManagerD3D12.cpp:122
ShaderResourceVariableD3D.h
Diligent::ShaderVariableManagerD3D12::GetRequiredMemorySize
static size_t GetRequiredMemorySize(const PipelineResourceSignatureD3D12Impl &Signature, const SHADER_RESOURCE_VARIABLE_TYPE *AllowedVarTypes, Uint32 NumAllowedTypes, SHADER_TYPE ShaderStages, Uint32 &NumVariables)
Definition: ShaderVariableManagerD3D12.cpp:69
Diligent::IMemoryAllocator
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
Diligent::ShaderVariableManagerD3D12::GetVariableCount
Uint32 GetVariableCount() const
Definition: ShaderVariableManagerD3D12.hpp:117
Diligent::ShaderVariableManagerD3D12
Definition: ShaderVariableManagerD3D12.hpp:73
Diligent::PipelineResourceAttribsD3D12::Register
const Uint32 Register
Definition: PipelineResourceAttribsD3D12.hpp:68
Diligent::ShaderVariableManagerD3D12::GetOwner
IObject & GetOwner()
Definition: ShaderVariableManagerD3D12.hpp:119
Diligent::ShaderVariableManagerD3D12::IsBound
bool IsBound(Uint32 ArrayIndex, Uint32 ResIndex) const
Definition: ShaderVariableManagerD3D12.cpp:623
Diligent::ShaderVariableManagerD3D12::GetVariable
ShaderVariableD3D12Impl * GetVariable(const Char *Name) const
Definition: ShaderVariableManagerD3D12.cpp:148
Diligent::ShaderVariableBase< ShaderVariableD3D12Impl, ShaderVariableManagerD3D12, IShaderResourceVariableD3D >::m_ResIndex
const Uint32 m_ResIndex
Definition: ShaderResourceVariableBase.hpp:633
Diligent::ShaderVariableD3D12Impl::operator=
ShaderVariableD3D12Impl & operator=(const ShaderVariableD3D12Impl &)=delete
Diligent::ShaderVariableManagerD3D12::BindResources
void BindResources(IResourceMapping *pResourceMapping, Uint32 Flags)
Definition: ShaderVariableManagerD3D12.cpp:191
Diligent::ShaderVariableBase< ShaderVariableD3D12Impl, ShaderVariableManagerD3D12, IShaderResourceVariableD3D >::m_ParentManager
ShaderVariableManagerD3D12 & m_ParentManager
Definition: ShaderResourceVariableBase.hpp:630
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Diligent::SHADER_RESOURCE_VARIABLE_TYPE
SHADER_RESOURCE_VARIABLE_TYPE
Describes the type of the shader resource variable.
Definition: ShaderResourceVariable.h:48