Diligent Engine  v.2.4.g
ShaderVariableManagerVk.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 // * ShaderVariableManagerVk keeps the list of variables of specific types
35 // * Every ShaderVariableVkImpl references ResourceAttribs by index from PipelineResourceSignatureVkImpl
36 // * ShaderVariableManagerVk keeps reference to ShaderResourceCacheVk
37 // * ShaderVariableManagerVk is used by PipelineResourceSignatureVkImpl to manage static resources and by
38 // ShaderResourceBindingVkImpl to manage mutable and dynamic resources
39 //
40 // __________________________ __________________________________________________________________________
41 // | | | | | |
42 // .----| ShaderVariableManagerVk |-------------------------->| ShaderVariableVkImpl[0] | ShaderVariableVkImpl[1] | ... |
43 // | |__________________________| |___________________________|____________________________|_________________|
44 // | | \ |
45 // | m_pSignature m_ResIndex m_ResIndex
46 // | | \ |
47 // | ___________V_____________________ ______________________V_______________________V____________________________
48 // | | | m_pResourceAttribs | | | | |
49 // | | PipelineResourceSignatureVkImpl |------------------->| Resource[0] | Resource[1] | ... | Resource[s+m+d-1] |
50 // | |_________________________________| |__________________|________________|_______________|_____________________|
51 // | | |
52 // | | |
53 // | | (DescriptorSet, CacheOffset) / (DescriptorSet, CacheOffset)
54 // | \ /
55 // | __________________________ ________V___________________________________________________V_______
56 // | | | | |
57 // '--->| ShaderResourceCacheVk |-------------------------->| Resources |
58 // |__________________________| |____________________________________________________________________|
59 //
60 //
61 
62 #include <memory>
63 
67 
68 namespace Diligent
69 {
70 
71 class ShaderVariableVkImpl;
72 
73 // sizeof(ShaderVariableManagerVk) == 40 (x64, msvc, Release)
75 {
76 public:
78  ShaderResourceCacheVk& ResourceCache) noexcept :
79  m_Owner{Owner},
80  m_ResourceCache{ResourceCache}
81  {}
82 
83  void Initialize(const PipelineResourceSignatureVkImpl& Signature,
84  IMemoryAllocator& Allocator,
85  const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
86  Uint32 NumAllowedTypes,
88 
90 
91  void Destroy(IMemoryAllocator& Allocator);
92 
93  ShaderVariableVkImpl* GetVariable(const Char* Name) const;
94  ShaderVariableVkImpl* GetVariable(Uint32 Index) const;
95 
96  // Binds object pObj to resource with index ResIndex and array index ArrayIndex.
97  void BindResource(IDeviceObject* pObj,
98  Uint32 ArrayIndex,
99  Uint32 ResIndex);
100 
101  bool IsBound(Uint32 ArrayIndex,
102  Uint32 ResIndex) const;
103 
104  void BindResources(IResourceMapping* pResourceMapping, Uint32 Flags) const;
105 
106  static size_t GetRequiredMemorySize(const PipelineResourceSignatureVkImpl& Signature,
107  const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
108  Uint32 NumAllowedTypes,
109  SHADER_TYPE ShaderStages,
110  Uint32& NumVariables);
111 
112  Uint32 GetVariableCount() const { return m_NumVariables; }
113 
114  IObject& GetOwner() { return m_Owner; }
115 
116 private:
117  friend ShaderVariableVkImpl;
119 
120  using ResourceAttribs = PipelineResourceAttribsVk;
121 
122  Uint32 GetVariableIndex(const ShaderVariableVkImpl& Variable);
123 
124  // These two methods can't be implemented in the header because they depend on PipelineResourceSignatureVkImpl
125  const PipelineResourceDesc& GetResourceDesc(Uint32 Index) const;
126  const ResourceAttribs& GetAttribs(Uint32 Index) const;
127 
128 private:
129  PipelineResourceSignatureVkImpl const* m_pSignature = nullptr;
130 
131  IObject& m_Owner;
132 
133  // Variable mgr is owned by either Pipeline Resource Signature (in which case m_ResourceCache references
134  // static resource cache owned by the same PRS object), or by SRB object (in which case
135  // m_ResourceCache references the cache in the SRB). Thus the cache and the signature
136  // (which the variables reference) are guaranteed to be alive while the manager is alive.
137  ShaderResourceCacheVk& m_ResourceCache;
138 
139  // Memory is allocated through the allocator provided by the resource signature. If allocation granularity > 1, fixed block
140  // memory allocator is used. This ensures that all resources from different shader resource bindings reside in
141  // continuous memory. If allocation granularity == 1, raw allocator is used.
142  ShaderVariableVkImpl* m_pVariables = nullptr;
143  Uint32 m_NumVariables = 0;
144 
145 #ifdef DILIGENT_DEBUG
146  IMemoryAllocator* m_pDbgAllocator = nullptr;
147 #endif
148 };
149 
150 // sizeof(ShaderVariableVkImpl) == 24 (x64)
151 class ShaderVariableVkImpl final : public ShaderVariableBase<ShaderVariableVkImpl, ShaderVariableManagerVk, IShaderResourceVariable>
152 {
153 public:
155 
157  Uint32 ResIndex) :
158  TBase{ParentManager, ResIndex}
159  {}
160 
161  // clang-format off
162  ShaderVariableVkImpl (const ShaderVariableVkImpl&) = delete;
166  // clang-format on
167 
168  virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final
169  {
170  return m_ParentManager.IsBound(ArrayIndex, m_ResIndex);
171  }
172 
173  void BindResource(IDeviceObject* pObj, Uint32 ArrayIndex) const
174  {
175  return m_ParentManager.BindResource(pObj, ArrayIndex, m_ResIndex);
176  }
177 };
178 
179 } // namespace Diligent
Diligent::ShaderVariableManagerVk::ShaderVariableManagerVk
ShaderVariableManagerVk(IObject &Owner, ShaderResourceCacheVk &ResourceCache) noexcept
Definition: ShaderVariableManagerVk.hpp:77
Diligent::ShaderVariableBase< ShaderVariableVkImpl, ShaderVariableManagerVk, IShaderResourceVariable >
Diligent::Char
char Char
Definition: BasicTypes.h:64
Diligent::SHADER_TYPE
SHADER_TYPE
Describes the shader type.
Definition: GraphicsTypes.h:65
Diligent::ShaderVariableManagerVk::GetVariable
ShaderVariableVkImpl * GetVariable(const Char *Name) const
Definition: ShaderVariableManagerVk.cpp:131
Flags
Uint32 Flags
Definition: DXBCUtils.cpp:71
Diligent::ShaderVariableManagerVk::BindResources
void BindResources(IResourceMapping *pResourceMapping, Uint32 Flags) const
Definition: ShaderVariableManagerVk.cpp:187
Diligent::ShaderVariableVkImpl::operator=
ShaderVariableVkImpl & operator=(const ShaderVariableVkImpl &)=delete
Diligent::IObject
Base interface for all dynamic objects in the engine.
Definition: Object.h:41
ShaderResourceVariableBase.hpp
Diligent::ShaderVariableVkImpl
Definition: ShaderVariableManagerVk.hpp:151
Diligent::IDeviceObject
Base interface for all objects created by the render device Diligent::IRenderDevice.
Definition: DeviceObject.h:52
Diligent::ShaderResourceCacheVk
Definition: ShaderResourceCacheVk.hpp:72
Diligent::ShaderVariableManagerVk::IsBound
bool IsBound(Uint32 ArrayIndex, Uint32 ResIndex) const
Definition: ShaderVariableManagerVk.cpp:653
Diligent::ShaderVariableManagerVk
Definition: ShaderVariableManagerVk.hpp:74
Diligent::ShaderVariableManagerVk::Initialize
void Initialize(const PipelineResourceSignatureVkImpl &Signature, IMemoryAllocator &Allocator, const SHADER_RESOURCE_VARIABLE_TYPE *AllowedVarTypes, Uint32 NumAllowedTypes, SHADER_TYPE ShaderType)
Definition: ShaderVariableManagerVk.cpp:80
Diligent::ShaderVariableManagerVk::~ShaderVariableManagerVk
~ShaderVariableManagerVk()
Definition: ShaderVariableManagerVk.cpp:113
Diligent::ShaderVariableManagerVk::GetRequiredMemorySize
static size_t GetRequiredMemorySize(const PipelineResourceSignatureVkImpl &Signature, const SHADER_RESOURCE_VARIABLE_TYPE *AllowedVarTypes, Uint32 NumAllowedTypes, SHADER_TYPE ShaderStages, Uint32 &NumVariables)
Definition: ShaderVariableManagerVk.cpp:63
DILIGENT_CALL_TYPE
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
Diligent::ShaderVariableManagerVk::GetOwner
IObject & GetOwner()
Definition: ShaderVariableManagerVk.hpp:114
Diligent::PipelineResourceDesc
Pipeline resource description.
Definition: PipelineResourceSignature.h:120
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::ShaderVariableVkImpl::BindResource
void BindResource(IDeviceObject *pObj, Uint32 ArrayIndex) const
Definition: ShaderVariableManagerVk.hpp:173
ShaderResourceCacheVk.hpp
Diligent::ShaderVariableManagerVk::GetVariableCount
Uint32 GetVariableCount() const
Definition: ShaderVariableManagerVk.hpp:112
Diligent::ShaderVariableManagerVk::BindResource
void BindResource(IDeviceObject *pObj, Uint32 ArrayIndex, Uint32 ResIndex)
Definition: ShaderVariableManagerVk.cpp:642
Diligent::IMemoryAllocator
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
Diligent::ShaderVariableVkImpl::ShaderVariableVkImpl
ShaderVariableVkImpl(ShaderVariableManagerVk &ParentManager, Uint32 ResIndex)
Definition: ShaderVariableManagerVk.hpp:156
Diligent::ShaderVariableBase< ShaderVariableVkImpl, ShaderVariableManagerVk, IShaderResourceVariable >::m_ResIndex
const Uint32 m_ResIndex
Definition: ShaderResourceVariableBase.hpp:633
Diligent::ShaderVariableManagerVk::Destroy
void Destroy(IMemoryAllocator &Allocator)
Definition: ShaderVariableManagerVk.cpp:118
ShaderType
Uint16 ShaderType
Definition: DXBCUtils.cpp:70
Diligent::PipelineResourceAttribsVk
Definition: PipelineResourceAttribsVk.hpp:65
Diligent::PipelineResourceSignatureVkImpl
Implementation of the Diligent::PipelineResourceSignatureVkImpl class.
Definition: PipelineResourceSignatureVkImpl.hpp:54
Diligent::ShaderVariableBase< ShaderVariableVkImpl, ShaderVariableManagerVk, IShaderResourceVariable >::m_ParentManager
ShaderVariableManagerVk & m_ParentManager
Definition: ShaderResourceVariableBase.hpp:630
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Diligent::ShaderVariableVkImpl::IsBound
virtual bool IsBound(Uint32 ArrayIndex) const override final
Definition: ShaderVariableManagerVk.hpp:168
Diligent::SHADER_RESOURCE_VARIABLE_TYPE
SHADER_RESOURCE_VARIABLE_TYPE
Describes the type of the shader resource variable.
Definition: ShaderResourceVariable.h:48
PipelineResourceAttribsVk.hpp