1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* Copyright 2019-2021 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#pragma once
/// \file
/// Declaration of Diligent::PipelineResourceAttribsD3D11 struct
#include <array>
#include "BasicTypes.h"
#include "DebugUtilities.hpp"
#include "HashUtils.hpp"
#include "GraphicsAccessories.hpp"
namespace Diligent
{
enum D3D11_RESOURCE_RANGE : Uint32
{
D3D11_RESOURCE_RANGE_CBV = 0,
D3D11_RESOURCE_RANGE_SRV,
D3D11_RESOURCE_RANGE_SAMPLER,
D3D11_RESOURCE_RANGE_UAV,
D3D11_RESOURCE_RANGE_COUNT,
D3D11_RESOURCE_RANGE_UNKNOWN = ~0u
};
/// Resource binding points in all shader stages.
// sizeof(D3D11ResourceBindPoints) == 8, x64
struct D3D11ResourceBindPoints
{
/// The number of different shader types (Vertex, Pixel, Geometry, Hull, Domain, Compute)
static constexpr Uint32 NumShaderTypes = 6;
D3D11ResourceBindPoints() noexcept
{
#ifdef DILIGENT_DEBUG
for (auto BindPoint : Bindings)
VERIFY_EXPR(BindPoint == InvalidBindPoint);
#endif
}
D3D11ResourceBindPoints(const D3D11ResourceBindPoints&) noexcept = default;
SHADER_TYPE GetActiveStages() const
{
return static_cast<SHADER_TYPE>(ActiveStages);
}
bool IsEmpty() const
{
return GetActiveStages() == SHADER_TYPE_UNKNOWN;
}
bool IsStageActive(Uint32 ShaderInd) const
{
bool IsActive = (GetActiveStages() & GetShaderTypeFromIndex(ShaderInd)) != 0;
VERIFY_EXPR((IsActive && Bindings[ShaderInd] != InvalidBindPoint ||
!IsActive && Bindings[ShaderInd] == InvalidBindPoint));
return IsActive;
}
Uint8 operator[](Uint32 ShaderInd) const
{
VERIFY(IsStageActive(ShaderInd), "Requesting bind point for inactive shader stage");
return Bindings[ShaderInd];
}
size_t GetHash() const
{
size_t Hash = 0;
for (auto Binding : Bindings)
HashCombine(Hash, Binding);
return Hash;
}
bool operator==(const D3D11ResourceBindPoints& rhs) const
{
return Bindings == rhs.Bindings;
}
D3D11ResourceBindPoints operator+(Uint32 value) const
{
D3D11ResourceBindPoints NewBindPoints{*this};
for (auto Stages = GetActiveStages(); Stages != SHADER_TYPE_UNKNOWN;)
{
auto ShaderInd = ExtractFirstShaderStageIndex(Stages);
VERIFY_EXPR(Uint32{Bindings[ShaderInd]} + value < InvalidBindPoint);
NewBindPoints.Bindings[ShaderInd] = Bindings[ShaderInd] + static_cast<Uint8>(value);
}
return NewBindPoints;
}
private:
struct StageAccessor
{
StageAccessor(D3D11ResourceBindPoints& _BindPoints,
const Uint32 _ShaderInd) :
BindPoints{_BindPoints},
ShaderInd{_ShaderInd}
{}
// clang-format off
StageAccessor (const StageAccessor&) = delete;
StageAccessor ( StageAccessor&&) = delete;
StageAccessor& operator=(const StageAccessor&) = delete;
StageAccessor& operator=( StageAccessor&&) = delete;
// clang-format on
Uint8 operator=(Uint32 BindPoint)
{
return BindPoints.Set(ShaderInd, BindPoint);
}
operator Uint8() const
{
return static_cast<const D3D11ResourceBindPoints&>(BindPoints)[ShaderInd];
}
private:
D3D11ResourceBindPoints& BindPoints;
const Uint32 ShaderInd;
};
public:
StageAccessor operator[](Uint32 ShaderInd)
{
return {*this, ShaderInd};
}
private:
Uint8 Set(Uint32 ShaderInd, Uint32 BindPoint)
{
VERIFY_EXPR(ShaderInd < NumShaderTypes);
VERIFY(BindPoint < InvalidBindPoint, "Bind point (", BindPoint, ") is out of range");
Bindings[ShaderInd] = static_cast<Uint8>(BindPoint);
ActiveStages |= Uint32{1} << ShaderInd;
return static_cast<Uint8>(BindPoint);
}
static constexpr Uint8 InvalidBindPoint = 0xFF;
// 0 1 2 3 4 5
// | PS | VS | GS | HS | DS | CS |
std::array<Uint8, NumShaderTypes> Bindings{InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint};
Uint8 ActiveStages = 0;
};
/// Shader resource counters for one specific resource range
struct D3D11ResourceRangeCounters
{
static constexpr Uint32 NumShaderTypes = D3D11ResourceBindPoints::NumShaderTypes;
Uint8 operator[](Uint32 Stage) const
{
VERIFY_EXPR(Stage < NumShaderTypes);
return (PackedCounters >> (NumBitsPerStage * Stage)) & StageMask;
}
D3D11ResourceRangeCounters& operator+=(const D3D11ResourceRangeCounters& rhs)
{
#ifdef DILIGENT_DEBUG
for (Uint32 s = 0; s < NumShaderTypes; ++s)
{
const Uint32 val0 = static_cast<const D3D11ResourceRangeCounters&>(*this)[s];
const Uint32 val1 = rhs[s];
VERIFY(val0 + val1 <= MaxCounter, "The resulting value is out of range");
}
#endif
PackedCounters += rhs.PackedCounters;
return *this;
}
bool operator==(const D3D11ResourceRangeCounters& rhs) const
{
return PackedCounters == rhs.PackedCounters;
}
private:
struct StageAccessor
{
StageAccessor(D3D11ResourceRangeCounters& _Counters,
const Uint32 _ShaderInd) :
Counters{_Counters},
ShaderInd{_ShaderInd}
{}
// clang-format off
StageAccessor (const StageAccessor&) = delete;
StageAccessor ( StageAccessor&&) = delete;
StageAccessor& operator=(const StageAccessor&) = delete;
StageAccessor& operator=( StageAccessor&&) = delete;
// clang-format on
Uint8 operator=(Uint32 Counter)
{
return Counters.Set(ShaderInd, Counter);
}
Uint8 operator+=(Uint32 Val)
{
Uint32 CurrValue = static_cast<const D3D11ResourceRangeCounters&>(Counters)[ShaderInd];
return Counters.Set(ShaderInd, CurrValue + Val);
}
operator Uint8() const
{
return static_cast<const D3D11ResourceRangeCounters&>(Counters)[ShaderInd];
}
private:
D3D11ResourceRangeCounters& Counters;
const Uint32 ShaderInd;
};
public:
StageAccessor operator[](Uint32 ShaderInd)
{
return {*this, ShaderInd};
}
private:
Uint8 Set(Uint32 ShaderInd, Uint32 Counter)
{
VERIFY_EXPR(Counter <= MaxCounter);
const Uint64 BitOffset = NumBitsPerStage * ShaderInd;
PackedCounters &= ~(StageMask << BitOffset);
PackedCounters |= Uint64{Counter} << BitOffset;
return static_cast<Uint8>(Counter);
}
static constexpr Uint64 NumBitsPerStage = 8;
static constexpr Uint64 StageMask = (Uint64{1} << NumBitsPerStage) - 1;
static constexpr Uint32 MaxCounter = (Uint32{1} << NumBitsPerStage) - 1;
// 0 1 2 3 4 5 6 7 8
// | PS | VS | GS | HS | DS | CS |unused|unused|
Uint64 PackedCounters = 0;
};
/// Resource counters for all shader stages and all resource types
using D3D11ShaderResourceCounters = std::array<D3D11ResourceRangeCounters, D3D11_RESOURCE_RANGE_COUNT>;
// sizeof(PipelineResourceAttribsD3D11) == 12, x64
struct PipelineResourceAttribsD3D11
{
private:
static constexpr Uint32 _SamplerIndBits = 31;
static constexpr Uint32 _SamplerAssignedBits = 1;
public:
static constexpr Uint32 InvalidSamplerInd = (1u << _SamplerIndBits) - 1;
// clang-format off
const Uint32 SamplerInd : _SamplerIndBits; // Index of the assigned sampler in m_Desc.Resources.
const Uint32 ImtblSamplerAssigned : _SamplerAssignedBits; // Immutable sampler flag.
// clang-format on
D3D11ResourceBindPoints BindPoints;
PipelineResourceAttribsD3D11(Uint32 _SamplerInd,
bool _ImtblSamplerAssigned) noexcept :
// clang-format off
SamplerInd {_SamplerInd },
ImtblSamplerAssigned{_ImtblSamplerAssigned ? 1u : 0u}
// clang-format on
{
VERIFY(SamplerInd == _SamplerInd, "Sampler index (", _SamplerInd, ") exceeds maximum representable value");
}
bool IsSamplerAssigned() const { return SamplerInd != InvalidSamplerInd; }
bool IsImmutableSamplerAssigned() const { return ImtblSamplerAssigned != 0; }
bool IsCompatibleWith(const PipelineResourceAttribsD3D11& rhs) const
{
// Ignore cache offset and sampler index.
// clang-format off
return IsImmutableSamplerAssigned() == rhs.IsImmutableSamplerAssigned() &&
BindPoints == rhs.BindPoints;
// clang-format on
}
size_t GetHash() const
{
return ComputeHash(IsImmutableSamplerAssigned(), BindPoints.GetHash());
}
};
} // namespace Diligent
|