Diligent Engine  v.2.4.g
PipelineResourceAttribsD3D11.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 <array>
34 
35 #include "BasicTypes.h"
36 #include "DebugUtilities.hpp"
37 #include "HashUtils.hpp"
38 #include "GraphicsAccessories.hpp"
39 
40 namespace Diligent
41 {
42 
44 {
51 };
52 
53 
55 // sizeof(D3D11ResourceBindPoints) == 8, x64
57 {
59  static constexpr Uint32 NumShaderTypes = 6;
60 
62  {
63 #ifdef DILIGENT_DEBUG
64  for (auto BindPoint : Bindings)
65  VERIFY_EXPR(BindPoint == InvalidBindPoint);
66 #endif
67  }
68 
69  D3D11ResourceBindPoints(const D3D11ResourceBindPoints&) noexcept = default;
70 
72  {
73  return static_cast<SHADER_TYPE>(ActiveStages);
74  }
75 
76  bool IsEmpty() const
77  {
79  }
80 
81  bool IsStageActive(Uint32 ShaderInd) const
82  {
83  bool IsActive = (GetActiveStages() & GetShaderTypeFromIndex(ShaderInd)) != 0;
84  VERIFY_EXPR((IsActive && Bindings[ShaderInd] != InvalidBindPoint ||
85  !IsActive && Bindings[ShaderInd] == InvalidBindPoint));
86  return IsActive;
87  }
88 
89  Uint8 operator[](Uint32 ShaderInd) const
90  {
91  VERIFY(IsStageActive(ShaderInd), "Requesting bind point for inactive shader stage.");
92  return Bindings[ShaderInd];
93  }
94 
95  size_t GetHash() const
96  {
97  size_t Hash = 0;
98  for (auto Binding : Bindings)
99  HashCombine(Hash, Binding);
100  return Hash;
101  }
102 
103  bool operator==(const D3D11ResourceBindPoints& rhs) const
104  {
105  return Bindings == rhs.Bindings;
106  }
107 
109  {
110  D3D11ResourceBindPoints NewBindPoints{*this};
111  for (auto Stages = GetActiveStages(); Stages != SHADER_TYPE_UNKNOWN;)
112  {
113  auto ShaderInd = ExtractFirstShaderStageIndex(Stages);
114  VERIFY_EXPR(Uint32{Bindings[ShaderInd]} + value < InvalidBindPoint);
115  NewBindPoints.Bindings[ShaderInd] = Bindings[ShaderInd] + static_cast<Uint8>(value);
116  }
117  return NewBindPoints;
118  }
119 
120 private:
121  struct StageAccessor
122  {
123  StageAccessor(D3D11ResourceBindPoints& _BindPoints,
124  const Uint32 _ShaderInd) :
125  BindPoints{_BindPoints},
126  ShaderInd{_ShaderInd}
127  {}
128 
129  // clang-format off
130  StageAccessor (const StageAccessor&) = delete;
131  StageAccessor ( StageAccessor&&) = delete;
132  StageAccessor& operator=(const StageAccessor&) = delete;
133  StageAccessor& operator=( StageAccessor&&) = delete;
134  // clang-format on
135 
136  Uint8 operator=(Uint32 BindPoint)
137  {
138  return BindPoints.Set(ShaderInd, BindPoint);
139  }
140 
141  operator Uint8() const
142  {
143  return static_cast<const D3D11ResourceBindPoints&>(BindPoints)[ShaderInd];
144  }
145 
146  private:
147  D3D11ResourceBindPoints& BindPoints;
148  const Uint32 ShaderInd;
149  };
150 
151 public:
152  StageAccessor operator[](Uint32 ShaderInd)
153  {
154  return {*this, ShaderInd};
155  }
156 
157 private:
158  Uint8 Set(Uint32 ShaderInd, Uint32 BindPoint)
159  {
160  VERIFY_EXPR(ShaderInd < NumShaderTypes);
161  VERIFY(BindPoint < InvalidBindPoint, "Bind point (", BindPoint, ") is out of range.");
162 
163  Bindings[ShaderInd] = static_cast<Uint8>(BindPoint);
164  ActiveStages |= Uint32{1} << ShaderInd;
165  return static_cast<Uint8>(BindPoint);
166  }
167 
168  static constexpr Uint8 InvalidBindPoint = 0xFF;
169 
170  // 0 1 2 3 4 5
171  // | PS | VS | GS | HS | DS | CS |
172  std::array<Uint8, NumShaderTypes> Bindings{InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint};
173 
174  Uint8 ActiveStages = 0;
175 };
176 
177 
180 {
182 
183  Uint8 operator[](Uint32 Stage) const
184  {
185  VERIFY_EXPR(Stage < NumShaderTypes);
186  return (PackedCounters >> (NumBitsPerStage * Stage)) & StageMask;
187  }
188 
190  {
191 #ifdef DILIGENT_DEBUG
192  for (Uint32 s = 0; s < NumShaderTypes; ++s)
193  {
194  const Uint32 val0 = static_cast<const D3D11ResourceRangeCounters&>(*this)[s];
195  const Uint32 val1 = rhs[s];
196  VERIFY(val0 + val1 <= MaxCounter, "The resulting value (", val0 + val1, ") is out of range.");
197  }
198 #endif
199  PackedCounters += rhs.PackedCounters;
200  return *this;
201  }
202 
203  bool operator==(const D3D11ResourceRangeCounters& rhs) const
204  {
205  return PackedCounters == rhs.PackedCounters;
206  }
207 
208 private:
209  struct StageAccessor
210  {
211  StageAccessor(D3D11ResourceRangeCounters& _Counters,
212  const Uint32 _ShaderInd) :
213  Counters{_Counters},
214  ShaderInd{_ShaderInd}
215  {}
216 
217  // clang-format off
218  StageAccessor (const StageAccessor&) = delete;
219  StageAccessor ( StageAccessor&&) = delete;
220  StageAccessor& operator=(const StageAccessor&) = delete;
221  StageAccessor& operator=( StageAccessor&&) = delete;
222  // clang-format on
223 
224  Uint8 operator=(Uint32 Counter)
225  {
226  return Counters.Set(ShaderInd, Counter);
227  }
228 
229  Uint8 operator+=(Uint32 Val)
230  {
231  Uint32 CurrValue = static_cast<const D3D11ResourceRangeCounters&>(Counters)[ShaderInd];
232  return Counters.Set(ShaderInd, CurrValue + Val);
233  }
234 
235  operator Uint8() const
236  {
237  return static_cast<const D3D11ResourceRangeCounters&>(Counters)[ShaderInd];
238  }
239 
240  private:
241  D3D11ResourceRangeCounters& Counters;
242  const Uint32 ShaderInd;
243  };
244 
245 public:
246  StageAccessor operator[](Uint32 ShaderInd)
247  {
248  return {*this, ShaderInd};
249  }
250 
251 private:
252  Uint8 Set(Uint32 ShaderInd, Uint32 Counter)
253  {
254  VERIFY_EXPR(Counter <= MaxCounter);
255  const Uint64 BitOffset = NumBitsPerStage * ShaderInd;
256  PackedCounters &= ~(StageMask << BitOffset);
257  PackedCounters |= Uint64{Counter} << BitOffset;
258  return static_cast<Uint8>(Counter);
259  }
260 
261  static constexpr Uint64 NumBitsPerStage = 8;
262  static constexpr Uint64 StageMask = (Uint64{1} << NumBitsPerStage) - 1;
263  static constexpr Uint32 MaxCounter = (Uint32{1} << NumBitsPerStage) - 1;
264 
265  // 0 1 2 3 4 5 6 7 8
266  // | PS | VS | GS | HS | DS | CS |unused|unused|
267  Uint64 PackedCounters = 0;
268 };
269 
271 using D3D11ShaderResourceCounters = std::array<D3D11ResourceRangeCounters, D3D11_RESOURCE_RANGE_COUNT>;
272 
273 
274 // sizeof(PipelineResourceAttribsD3D11) == 12, x64
276 {
277 private:
278  static constexpr Uint32 _SamplerIndBits = 31;
279  static constexpr Uint32 _SamplerAssignedBits = 1;
280 
281 public:
282  static constexpr Uint32 InvalidSamplerInd = (1u << _SamplerIndBits) - 1;
283 
284  // clang-format off
285  const Uint32 SamplerInd : _SamplerIndBits; // Index of the assigned sampler in m_Desc.Resources.
286  const Uint32 ImtblSamplerAssigned : _SamplerAssignedBits; // Immutable sampler flag for Texture SRV or Sampler.
287  // clang-format on
289 
291  bool _ImtblSamplerAssigned) noexcept :
292  // clang-format off
293  SamplerInd {_SamplerInd },
294  ImtblSamplerAssigned{_ImtblSamplerAssigned ? 1u : 0u}
295  // clang-format on
296  {
297  VERIFY(SamplerInd == _SamplerInd, "Sampler index (", _SamplerInd, ") exceeds maximum representable value.");
298  }
299 
300  bool IsSamplerAssigned() const { return SamplerInd != InvalidSamplerInd; }
301  bool IsImmutableSamplerAssigned() const { return ImtblSamplerAssigned != 0; }
302 
304  {
305  // Ignore assigned sampler index.
306  // clang-format off
308  BindPoints == rhs.BindPoints;
309  // clang-format on
310  }
311 
312  size_t GetHash() const
313  {
315  }
316 };
317 
318 } // namespace Diligent
Diligent::PipelineResourceAttribsD3D11::SamplerInd
const Uint32 SamplerInd
Definition: PipelineResourceAttribsD3D11.hpp:285
Diligent::D3D11ResourceRangeCounters
Shader resource counters for one specific resource range.
Definition: PipelineResourceAttribsD3D11.hpp:179
Diligent::D3D11ResourceBindPoints::IsEmpty
bool IsEmpty() const
Definition: PipelineResourceAttribsD3D11.hpp:76
Diligent::ExtractFirstShaderStageIndex
Int32 ExtractFirstShaderStageIndex(SHADER_TYPE &Stages)
Definition: GraphicsAccessories.hpp:450
Diligent::PipelineResourceAttribsD3D11
Definition: PipelineResourceAttribsD3D11.hpp:275
GraphicsAccessories.hpp
Diligent::GetShaderTypeFromIndex
SHADER_TYPE GetShaderTypeFromIndex(Int32 Index)
Definition: GraphicsAccessories.hpp:501
Diligent::SHADER_TYPE
SHADER_TYPE
Describes the shader type.
Definition: GraphicsTypes.h:65
Diligent::D3D11_RESOURCE_RANGE_COUNT
@ D3D11_RESOURCE_RANGE_COUNT
Definition: PipelineResourceAttribsD3D11.hpp:49
Diligent::Uint64
uint64_t Uint64
64-bit unsigned integer
Definition: BasicTypes.h:50
Diligent::D3D11ResourceRangeCounters::operator==
bool operator==(const D3D11ResourceRangeCounters &rhs) const
Definition: PipelineResourceAttribsD3D11.hpp:203
Diligent::D3D11_RESOURCE_RANGE_SAMPLER
@ D3D11_RESOURCE_RANGE_SAMPLER
Definition: PipelineResourceAttribsD3D11.hpp:47
Diligent::D3D11_RESOURCE_RANGE_SRV
@ D3D11_RESOURCE_RANGE_SRV
Definition: PipelineResourceAttribsD3D11.hpp:46
Diligent::D3D11ResourceRangeCounters::operator+=
D3D11ResourceRangeCounters & operator+=(const D3D11ResourceRangeCounters &rhs)
Definition: PipelineResourceAttribsD3D11.hpp:189
DebugUtilities.hpp
Diligent::D3D11ResourceBindPoints::IsStageActive
bool IsStageActive(Uint32 ShaderInd) const
Definition: PipelineResourceAttribsD3D11.hpp:81
Diligent::D3D11_RESOURCE_RANGE_CBV
@ D3D11_RESOURCE_RANGE_CBV
Definition: PipelineResourceAttribsD3D11.hpp:45
Diligent::D3D11ResourceRangeCounters::operator[]
Uint8 operator[](Uint32 Stage) const
Definition: PipelineResourceAttribsD3D11.hpp:183
Diligent::PipelineResourceAttribsD3D11::ImtblSamplerAssigned
const Uint32 ImtblSamplerAssigned
Definition: PipelineResourceAttribsD3D11.hpp:286
Diligent::D3D11ResourceBindPoints::operator+
D3D11ResourceBindPoints operator+(Uint32 value) const
Definition: PipelineResourceAttribsD3D11.hpp:108
Diligent::PipelineResourceAttribsD3D11::BindPoints
D3D11ResourceBindPoints BindPoints
Definition: PipelineResourceAttribsD3D11.hpp:288
Diligent::D3D11ShaderResourceCounters
std::array< D3D11ResourceRangeCounters, D3D11_RESOURCE_RANGE_COUNT > D3D11ShaderResourceCounters
Resource counters for all shader stages and all resource types.
Definition: PipelineResourceAttribsD3D11.hpp:271
Diligent::D3D11ResourceRangeCounters::operator[]
StageAccessor operator[](Uint32 ShaderInd)
Definition: PipelineResourceAttribsD3D11.hpp:246
Diligent::PipelineResourceAttribsD3D11::IsCompatibleWith
bool IsCompatibleWith(const PipelineResourceAttribsD3D11 &rhs) const
Definition: PipelineResourceAttribsD3D11.hpp:303
BasicTypes.h
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::ComputeHash
std::size_t ComputeHash(const ArgsType &... Args)
Definition: HashUtils.hpp:57
Diligent::SHADER_TYPE_UNKNOWN
@ SHADER_TYPE_UNKNOWN
Unknown shader type.
Definition: GraphicsTypes.h:67
Diligent::D3D11ResourceBindPoints::GetHash
size_t GetHash() const
Definition: PipelineResourceAttribsD3D11.hpp:95
Diligent::PipelineResourceAttribsD3D11::IsImmutableSamplerAssigned
bool IsImmutableSamplerAssigned() const
Definition: PipelineResourceAttribsD3D11.hpp:301
Diligent::D3D11ResourceBindPoints
Resource binding points in all shader stages.
Definition: PipelineResourceAttribsD3D11.hpp:56
Diligent::D3D11ResourceBindPoints::GetActiveStages
SHADER_TYPE GetActiveStages() const
Definition: PipelineResourceAttribsD3D11.hpp:71
Diligent::D3D11ResourceRangeCounters::NumShaderTypes
static constexpr Uint32 NumShaderTypes
Definition: PipelineResourceAttribsD3D11.hpp:181
Diligent::D3D11ResourceBindPoints::operator[]
Uint8 operator[](Uint32 ShaderInd) const
Definition: PipelineResourceAttribsD3D11.hpp:89
Diligent::PipelineResourceAttribsD3D11::GetHash
size_t GetHash() const
Definition: PipelineResourceAttribsD3D11.hpp:312
HashUtils.hpp
Diligent::D3D11_RESOURCE_RANGE_UAV
@ D3D11_RESOURCE_RANGE_UAV
Definition: PipelineResourceAttribsD3D11.hpp:48
Diligent::Uint8
uint8_t Uint8
8-bit unsigned integer
Definition: BasicTypes.h:53
Diligent::D3D11ResourceBindPoints::operator[]
StageAccessor operator[](Uint32 ShaderInd)
Definition: PipelineResourceAttribsD3D11.hpp:152
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::D3D11ResourceBindPoints::NumShaderTypes
static constexpr Uint32 NumShaderTypes
The number of different shader types (Vertex, Pixel, Geometry, Hull, Domain, Compute)
Definition: PipelineResourceAttribsD3D11.hpp:59
Diligent::D3D11_RESOURCE_RANGE_UNKNOWN
@ D3D11_RESOURCE_RANGE_UNKNOWN
Definition: PipelineResourceAttribsD3D11.hpp:50
Diligent::PipelineResourceAttribsD3D11::IsSamplerAssigned
bool IsSamplerAssigned() const
Definition: PipelineResourceAttribsD3D11.hpp:300
Diligent::PipelineResourceAttribsD3D11::PipelineResourceAttribsD3D11
PipelineResourceAttribsD3D11(Uint32 _SamplerInd, bool _ImtblSamplerAssigned) noexcept
Definition: PipelineResourceAttribsD3D11.hpp:290
Diligent::D3D11ResourceBindPoints::operator==
bool operator==(const D3D11ResourceBindPoints &rhs) const
Definition: PipelineResourceAttribsD3D11.hpp:103
Diligent::D3D11ResourceBindPoints::D3D11ResourceBindPoints
D3D11ResourceBindPoints() noexcept
Definition: PipelineResourceAttribsD3D11.hpp:61
Diligent::PipelineResourceAttribsD3D11::InvalidSamplerInd
static constexpr Uint32 InvalidSamplerInd
Definition: PipelineResourceAttribsD3D11.hpp:282
Diligent::HashCombine
void HashCombine(std::size_t &Seed, const T &Val)
Definition: HashUtils.hpp:44
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Diligent::D3D11_RESOURCE_RANGE
D3D11_RESOURCE_RANGE
Definition: PipelineResourceAttribsD3D11.hpp:43
BindPoint
Uint32 BindPoint
Definition: DXBCUtils.cpp:83