Diligent Engine  v.2.4.g
RootParamsManager.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 #include <memory>
34 #include <vector>
35 #include <array>
36 
37 #include "Shader.h"
38 #include "ShaderResourceVariable.h"
39 
40 namespace Diligent
41 {
42 
44 {
48 };
49 
51 {
53 }
54 
56 {
57 private:
58  static constexpr Uint32 ParameterGroupBits = 1;
59  static constexpr Uint32 RootIndexBits = 32 - ParameterGroupBits;
60  static_assert((1 << ParameterGroupBits) >= ROOT_PARAMETER_GROUP_COUNT, "Not enough bits to represent ROOT_PARAMETER_GROUP");
61 
62 public:
63  const Uint32 RootIndex : RootIndexBits;
64 
65  const ROOT_PARAMETER_GROUP Group : ParameterGroupBits;
66 
67  // Each descriptor table is suballocated from one of the four descriptor heap allocations:
68  // {CBV_SRV_UAV, SAMPLER} x {STATIC_MUTABLE, DYNAMIC}.
69  // TableOffsetInGroupAllocation indicates starting offset from the beginning of the
70  // corresponding allocation.
72 
74 
75  const D3D12_ROOT_PARAMETER d3d12RootParam;
76 
77  RootParameter(Uint32 _RootIndex,
78  ROOT_PARAMETER_GROUP _Group,
79  const D3D12_ROOT_PARAMETER& _d3d12RootParam,
80  Uint32 _TableOffsetInGroupAllocation = InvalidTableOffsetInGroupAllocation) noexcept;
81 
82  // clang-format off
83  RootParameter (const RootParameter&) = delete;
84  RootParameter& operator=(const RootParameter&) = delete;
85  RootParameter (RootParameter&&) = default;
87  // clang-format on
88 
90  {
91  VERIFY(d3d12RootParam.ParameterType == D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE,
92  "Incorrect parameter type: descriptor table is expected");
93 
94  // All descriptors in the table are tightly packed, so the table size is given
95  // by the end of the last range
96  const auto& d3d12Tbl = d3d12RootParam.DescriptorTable;
97  VERIFY(d3d12Tbl.NumDescriptorRanges > 0, "Descriptor table must contain at least one range");
98  const auto& d3d12LastRange = d3d12Tbl.pDescriptorRanges[d3d12Tbl.NumDescriptorRanges - 1];
99  VERIFY(d3d12LastRange.NumDescriptors > 0, "The range must not be empty");
100  return d3d12LastRange.OffsetInDescriptorsFromTableStart + d3d12LastRange.NumDescriptors;
101  }
102 
103  bool operator==(const RootParameter& rhs) const;
104  bool operator!=(const RootParameter& rhs) const { return !(*this == rhs); }
105 
106  size_t GetHash() const;
107 };
108 static_assert(sizeof(RootParameter) == sizeof(D3D12_ROOT_PARAMETER) + sizeof(Uint32) * 2, "Unexpected sizeof(RootParameter) - did you pack the members properly?");
109 
110 
112 
116 
117 // Note that root index is NOT the same as the index of
118 // the root table or index of the root view, e.g.
119 //
120 // Root Index | Root Table Index | Root View Index
121 // 0 | 0 |
122 // 1 | | 0
123 // 2 | 1 |
124 // 3 | 2 |
125 // 4 | | 1
126 //
128 {
129 public:
130  RootParamsManager() noexcept {}
132 
133  // clang-format off
134  RootParamsManager (const RootParamsManager&) = delete;
138  // clang-format on
139 
140  Uint32 GetNumRootTables() const { return m_NumRootTables; }
141  Uint32 GetNumRootViews() const { return m_NumRootViews; }
142 
143  const RootParameter& GetRootTable(Uint32 TableInd) const
144  {
145  VERIFY_EXPR(TableInd < m_NumRootTables);
146  return m_pRootTables[TableInd];
147  }
148 
149  const RootParameter& GetRootView(Uint32 ViewInd) const
150  {
151  VERIFY_EXPR(ViewInd < m_NumRootViews);
152  return m_pRootViews[ViewInd];
153  }
154 
155  // Returns the total number of resources in a given parameter group and descriptor heap type
156  Uint32 GetParameterGroupSize(D3D12_DESCRIPTOR_HEAP_TYPE d3d12HeapType, ROOT_PARAMETER_GROUP Group) const
157  {
158  return m_ParameterGroupSizes[d3d12HeapType][Group];
159  }
160 
161  bool operator==(const RootParamsManager& RootParams) const;
162 
163 #ifdef DILIGENT_DEBUG
164  void Validate() const;
165 #endif
166 
167 private:
168  friend class RootParamsBuilder;
169 
170  std::unique_ptr<void, STDDeleter<void, IMemoryAllocator>> m_pMemory;
171 
172  Uint32 m_NumRootTables = 0;
173  Uint32 m_NumRootViews = 0;
174 
175  const RootParameter* m_pRootTables = nullptr;
176  const RootParameter* m_pRootViews = nullptr;
177 
178  // The total number of resources placed in descriptor tables for each heap type and parameter group type
179  std::array<std::array<Uint32, ROOT_PARAMETER_GROUP_COUNT>, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER + 1> m_ParameterGroupSizes{};
180 };
181 
183 {
184 public:
186 
187  // Allocates root parameter slot for the given resource attributes.
188  void AllocateResourceSlot(SHADER_TYPE ShaderStages,
189  SHADER_RESOURCE_VARIABLE_TYPE VariableType,
190  D3D12_ROOT_PARAMETER_TYPE RootParameterType,
191  D3D12_DESCRIPTOR_RANGE_TYPE RangeType,
192  Uint32 ArraySize,
193  Uint32 Register,
194  Uint32 Space,
195  Uint32& RootIndex,
196  Uint32& OffsetFromTableStart);
197 
198  void InitializeMgr(IMemoryAllocator& MemAllocator, RootParamsManager& ParamsMgr);
199 
200 private:
201  // Adds a new root view parameter and returns the reference to it.
202  RootParameter& AddRootView(D3D12_ROOT_PARAMETER_TYPE ParameterType,
203  Uint32 RootIndex,
204  UINT Register,
205  UINT RegisterSpace,
206  D3D12_SHADER_VISIBILITY Visibility,
207  ROOT_PARAMETER_GROUP RootType);
208 
209  struct RootTableData;
210  // Adds a new root table parameter and returns the reference to it.
211  RootTableData& AddRootTable(Uint32 RootIndex,
212  D3D12_SHADER_VISIBILITY Visibility,
213  ROOT_PARAMETER_GROUP RootType,
214  Uint32 NumRangesInNewTable = 1);
215 
216 
217 private:
218  struct RootTableData
219  {
220  RootTableData(Uint32 _RootIndex,
221  D3D12_SHADER_VISIBILITY _Visibility,
222  ROOT_PARAMETER_GROUP _Group,
223  Uint32 _NumRanges);
224  void Extend(Uint32 NumExtraRanges);
225 
226  const Uint32 RootIndex;
227  const ROOT_PARAMETER_GROUP Group;
228  D3D12_ROOT_PARAMETER d3d12RootParam{};
229 
230  std::vector<D3D12_DESCRIPTOR_RANGE> Ranges;
231  };
232  std::vector<RootTableData> m_RootTables;
233  std::vector<RootParameter> m_RootViews;
234 
235  static constexpr int InvalidRootTableIndex = -1;
236 
237  // The array below contains the index of a CBV/SRV/UAV root table in m_RootTables
238  // (NOT the Root Index!), for every root parameter group (static/mutable, dynamic)
239  // and every shader visbility, or -1, if the table is not yet assigned to the combination.
240  // Note: max(D3D12_SHADER_VISIBILITY) == D3D12_SHADER_VISIBILITY_MESH == 7
241  std::array<std::array<int, 8>, ROOT_PARAMETER_GROUP_COUNT> m_SrvCbvUavRootTablesMap = {};
242 
243  // This array contains the same data for Sampler root table
244  std::array<std::array<int, 8>, ROOT_PARAMETER_GROUP_COUNT> m_SamplerRootTablesMap = {};
245 };
246 
247 } // namespace Diligent
Diligent::ROOT_PARAMETER_GROUP_STATIC_MUTABLE
@ ROOT_PARAMETER_GROUP_STATIC_MUTABLE
Definition: RootParamsManager.hpp:45
Diligent::RootParameter::operator=
RootParameter & operator=(const RootParameter &)=delete
Diligent::RootParamsManager::RootParamsManager
RootParamsManager() noexcept
Definition: RootParamsManager.hpp:130
Diligent::SHADER_TYPE
SHADER_TYPE
Describes the shader type.
Definition: GraphicsTypes.h:65
Diligent::RootParameter::operator==
bool operator==(const RootParameter &rhs) const
Definition: RootParamsManager.cpp:94
Diligent::RootParameter::GetDescriptorTableSize
Uint32 GetDescriptorTableSize() const
Definition: RootParamsManager.hpp:89
Diligent::ROOT_PARAMETER_GROUP
ROOT_PARAMETER_GROUP
Definition: RootParamsManager.hpp:43
Shader.h
Diligent::SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC
@ SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC
Shader variable binding is dynamic. It can be set multiple times for every instance of shader resourc...
Definition: ShaderResourceVariable.h:62
Diligent::RootParameter::RootIndex
const Uint32 RootIndex
Definition: RootParamsManager.hpp:60
Diligent::RootParameter
Definition: RootParamsManager.hpp:55
Diligent::RootParamsBuilder::InitializeMgr
void InitializeMgr(IMemoryAllocator &MemAllocator, RootParamsManager &ParamsMgr)
Definition: RootParamsManager.cpp:405
Space
Uint32 Space
Definition: DXBCUtils.cpp:99
Diligent::RootParamsBuilder::AllocateResourceSlot
void AllocateResourceSlot(SHADER_TYPE ShaderStages, SHADER_RESOURCE_VARIABLE_TYPE VariableType, D3D12_ROOT_PARAMETER_TYPE RootParameterType, D3D12_DESCRIPTOR_RANGE_TYPE RangeType, Uint32 ArraySize, Uint32 Register, Uint32 Space, Uint32 &RootIndex, Uint32 &OffsetFromTableStart)
Definition: RootParamsManager.cpp:319
Diligent::RootParamsManager::GetParameterGroupSize
Uint32 GetParameterGroupSize(D3D12_DESCRIPTOR_HEAP_TYPE d3d12HeapType, ROOT_PARAMETER_GROUP Group) const
Definition: RootParamsManager.hpp:156
Diligent::RootParamsBuilder::RootParamsBuilder
RootParamsBuilder()
Definition: RootParamsManager.cpp:238
Diligent::RootParameter::TableOffsetInGroupAllocation
const Uint32 TableOffsetInGroupAllocation
Definition: RootParamsManager.hpp:71
Diligent::ROOT_PARAMETER_GROUP_DYNAMIC
@ ROOT_PARAMETER_GROUP_DYNAMIC
Definition: RootParamsManager.hpp:46
Diligent::RootParamsManager::GetRootView
const RootParameter & GetRootView(Uint32 ViewInd) const
Definition: RootParamsManager.hpp:149
Diligent::RootParameter::RootParameter
RootParameter(Uint32 _RootIndex, ROOT_PARAMETER_GROUP _Group, const D3D12_ROOT_PARAMETER &_d3d12RootParam, Uint32 _TableOffsetInGroupAllocation=InvalidTableOffsetInGroupAllocation) noexcept
Definition: RootParamsManager.cpp:74
Diligent::RootParameter::GetHash
size_t GetHash() const
Definition: RootParamsManager.cpp:104
Diligent::RootParamsManager::GetRootTable
const RootParameter & GetRootTable(Uint32 TableInd) const
Definition: RootParamsManager.hpp:143
Diligent::RootParamsManager::operator=
RootParamsManager & operator=(const RootParamsManager &)=delete
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::RootParameter::d3d12RootParam
const D3D12_ROOT_PARAMETER d3d12RootParam
Definition: RootParamsManager.hpp:75
Diligent::RootParameter::InvalidTableOffsetInGroupAllocation
static constexpr Uint32 InvalidTableOffsetInGroupAllocation
Definition: RootParamsManager.hpp:73
Diligent::IMemoryAllocator
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
Diligent::RootParamsManager::~RootParamsManager
~RootParamsManager()
Definition: RootParamsManager.cpp:145
Diligent::RootParamsManager::operator==
bool operator==(const RootParamsManager &RootParams) const
Definition: RootParamsManager.cpp:150
Diligent::ROOT_PARAMETER_GROUP_COUNT
@ ROOT_PARAMETER_GROUP_COUNT
Definition: RootParamsManager.hpp:47
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
ShaderResourceVariable.h
Diligent::RootParameter::Group
const ROOT_PARAMETER_GROUP Group
Definition: RootParamsManager.hpp:65
Diligent::RootParamsManager::GetNumRootViews
Uint32 GetNumRootViews() const
Definition: RootParamsManager.hpp:141
Diligent::VariableTypeToRootParameterGroup
ROOT_PARAMETER_GROUP VariableTypeToRootParameterGroup(SHADER_RESOURCE_VARIABLE_TYPE VarType)
Definition: RootParamsManager.hpp:50
Diligent::RootParameter::operator!=
bool operator!=(const RootParameter &rhs) const
Definition: RootParamsManager.hpp:104
Diligent::RootParamsManager::GetNumRootTables
Uint32 GetNumRootTables() const
Definition: RootParamsManager.hpp:140
Diligent::RootParamsManager
Container for root parameters.
Definition: RootParamsManager.hpp:127
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
Diligent::RootParamsBuilder
Definition: RootParamsManager.hpp:182