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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/*
* 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::SPIRVShaderResources class
// SPIRVShaderResources class uses continuous chunk of memory to store all resources, as follows:
//
// m_MemoryBuffer m_TotalResources
// | | |
// | Uniform Buffers | Storage Buffers | Storage Images | Sampled Images | Atomic Counters | Separate Samplers | Separate Images | Stage Inputs | Resource Names |
#include <memory>
#include <vector>
#include <sstream>
#include "Shader.h"
#include "RenderDevice.h"
#include "STDAllocator.hpp"
#include "RefCntAutoPtr.hpp"
#include "StringPool.hpp"
namespace diligent_spirv_cross
{
class Compiler;
struct Resource;
} // namespace diligent_spirv_cross
namespace Diligent
{
// sizeof(SPIRVShaderResourceAttribs) == 32, msvc x64
struct SPIRVShaderResourceAttribs
{
enum ResourceType : Uint8
{
UniformBuffer = 0,
ROStorageBuffer,
RWStorageBuffer,
UniformTexelBuffer,
StorageTexelBuffer,
StorageImage,
SampledImage,
AtomicCounter,
SeparateImage,
SeparateSampler,
InputAttachment,
AccelerationStructure,
NumResourceTypes
};
static SHADER_RESOURCE_TYPE GetShaderResourceType(ResourceType Type);
// clang-format off
/* 0 */const char* const Name;
/* 8 */const Uint16 ArraySize;
/* 10 */const ResourceType Type;
/* 11.0*/const Uint8 ResourceDim : 7;
/* 11.7*/const Uint8 IsMS : 1;
// Offset in SPIRV words (uint32_t) of binding & descriptor set decorations in SPIRV binary
/* 12 */const uint32_t BindingDecorationOffset;
/* 16 */const uint32_t DescriptorSetDecorationOffset;
/* 20 */const Uint32 BufferStaticSize;
/* 24 */const Uint32 BufferStride;
/* 28 */
/* 32 */ // End of structure
// clang-format on
SPIRVShaderResourceAttribs(const diligent_spirv_cross::Compiler& Compiler,
const diligent_spirv_cross::Resource& Res,
const char* _Name,
ResourceType _Type,
Uint32 _BufferStaticSize = 0,
Uint32 _BufferStride = 0) noexcept;
ShaderResourceDesc GetResourceDesc() const
{
return ShaderResourceDesc{Name, GetShaderResourceType(Type), ArraySize};
}
RESOURCE_DIMENSION GetResourceDimension() const
{
return static_cast<RESOURCE_DIMENSION>(ResourceDim);
}
bool IsMultisample() const
{
return IsMS != 0;
}
};
static_assert(sizeof(SPIRVShaderResourceAttribs) % sizeof(void*) == 0, "Size of SPIRVShaderResourceAttribs struct must be multiple of sizeof(void*)");
// sizeof(SPIRVShaderResourceAttribs) == 16, msvc x64
struct SPIRVShaderStageInputAttribs
{
// clang-format off
SPIRVShaderStageInputAttribs(const char* _Semantic, uint32_t _LocationDecorationOffset) :
Semantic {_Semantic},
LocationDecorationOffset {_LocationDecorationOffset}
{}
// clang-format on
const char* const Semantic;
const uint32_t LocationDecorationOffset;
};
static_assert(sizeof(SPIRVShaderStageInputAttribs) % sizeof(void*) == 0, "Size of SPIRVShaderStageInputAttribs struct must be multiple of sizeof(void*)");
/// Diligent::SPIRVShaderResources class
class SPIRVShaderResources
{
public:
SPIRVShaderResources(IMemoryAllocator& Allocator,
IRenderDevice* pRenderDevice,
std::vector<uint32_t> spirv_binary,
const ShaderDesc& shaderDesc,
const char* CombinedSamplerSuffix,
bool LoadShaderStageInputs,
std::string& EntryPoint);
// clang-format off
SPIRVShaderResources (const SPIRVShaderResources&) = delete;
SPIRVShaderResources ( SPIRVShaderResources&&) = delete;
SPIRVShaderResources& operator = (const SPIRVShaderResources&) = delete;
SPIRVShaderResources& operator = ( SPIRVShaderResources&&) = delete;
// clang-format on
~SPIRVShaderResources();
// clang-format off
Uint32 GetNumUBs ()const noexcept{ return (m_StorageBufferOffset - 0); }
Uint32 GetNumSBs ()const noexcept{ return (m_StorageImageOffset - m_StorageBufferOffset); }
Uint32 GetNumImgs ()const noexcept{ return (m_SampledImageOffset - m_StorageImageOffset); }
Uint32 GetNumSmpldImgs ()const noexcept{ return (m_AtomicCounterOffset - m_SampledImageOffset); }
Uint32 GetNumACs ()const noexcept{ return (m_SeparateSamplerOffset - m_AtomicCounterOffset); }
Uint32 GetNumSepSmplrs ()const noexcept{ return (m_SeparateImageOffset - m_SeparateSamplerOffset);}
Uint32 GetNumSepImgs ()const noexcept{ return (m_InputAttachmentOffset - m_SeparateImageOffset); }
Uint32 GetNumInptAtts ()const noexcept{ return (m_AccelStructOffset - m_InputAttachmentOffset);}
Uint32 GetNumAccelStructs()const noexcept{ return (m_TotalResources - m_AccelStructOffset); }
Uint32 GetTotalResources () const noexcept { return m_TotalResources; }
Uint32 GetNumShaderStageInputs()const noexcept { return m_NumShaderStageInputs; }
const SPIRVShaderResourceAttribs& GetUB (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumUBs(), 0 ); }
const SPIRVShaderResourceAttribs& GetSB (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumSBs(), m_StorageBufferOffset ); }
const SPIRVShaderResourceAttribs& GetImg (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumImgs(), m_StorageImageOffset ); }
const SPIRVShaderResourceAttribs& GetSmpldImg (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumSmpldImgs(), m_SampledImageOffset ); }
const SPIRVShaderResourceAttribs& GetAC (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumACs(), m_AtomicCounterOffset ); }
const SPIRVShaderResourceAttribs& GetSepSmplr (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumSepSmplrs(), m_SeparateSamplerOffset); }
const SPIRVShaderResourceAttribs& GetSepImg (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumSepImgs(), m_SeparateImageOffset ); }
const SPIRVShaderResourceAttribs& GetInptAtt (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumInptAtts(), m_InputAttachmentOffset); }
const SPIRVShaderResourceAttribs& GetAccelStruct(Uint32 n)const noexcept{ return GetResAttribs(n, GetNumAccelStructs(), m_AccelStructOffset ); }
const SPIRVShaderResourceAttribs& GetResource (Uint32 n)const noexcept{ return GetResAttribs(n, GetTotalResources(), 0 ); }
// clang-format on
const SPIRVShaderStageInputAttribs& GetShaderStageInputAttribs(Uint32 n) const noexcept
{
VERIFY(n < m_NumShaderStageInputs, "Shader stage input index (", n, ") is out of range. Total input count: ", m_NumShaderStageInputs);
auto* ResourceMemoryEnd = reinterpret_cast<const SPIRVShaderResourceAttribs*>(m_MemoryBuffer.get()) + m_TotalResources;
return reinterpret_cast<const SPIRVShaderStageInputAttribs*>(ResourceMemoryEnd)[n];
}
struct ResourceCounters
{
Uint32 NumUBs = 0;
Uint32 NumSBs = 0;
Uint32 NumImgs = 0;
Uint32 NumSmpldImgs = 0;
Uint32 NumACs = 0;
Uint32 NumSepSmplrs = 0;
Uint32 NumSepImgs = 0;
Uint32 NumInptAtts = 0;
Uint32 NumAccelStructs = 0;
};
SHADER_TYPE GetShaderType() const noexcept { return m_ShaderType; }
// Process only resources listed in AllowedVarTypes
template <typename THandleUB,
typename THandleSB,
typename THandleImg,
typename THandleSmplImg,
typename THandleAC,
typename THandleSepSmpl,
typename THandleSepImg,
typename THandleInptAtt,
typename THandleAccelStruct>
void ProcessResources(THandleUB HandleUB,
THandleSB HandleSB,
THandleImg HandleImg,
THandleSmplImg HandleSmplImg,
THandleAC HandleAC,
THandleSepSmpl HandleSepSmpl,
THandleSepImg HandleSepImg,
THandleInptAtt HandleInptAtt,
THandleAccelStruct HandleAccelStruct) const
{
for (Uint32 n = 0; n < GetNumUBs(); ++n)
{
const auto& UB = GetUB(n);
HandleUB(UB, n);
}
for (Uint32 n = 0; n < GetNumSBs(); ++n)
{
const auto& SB = GetSB(n);
HandleSB(SB, n);
}
for (Uint32 n = 0; n < GetNumImgs(); ++n)
{
const auto& Img = GetImg(n);
HandleImg(Img, n);
}
for (Uint32 n = 0; n < GetNumSmpldImgs(); ++n)
{
const auto& SmplImg = GetSmpldImg(n);
HandleSmplImg(SmplImg, n);
}
for (Uint32 n = 0; n < GetNumACs(); ++n)
{
const auto& AC = GetAC(n);
HandleAC(AC, n);
}
for (Uint32 n = 0; n < GetNumSepSmplrs(); ++n)
{
const auto& SepSmpl = GetSepSmplr(n);
HandleSepSmpl(SepSmpl, n);
}
for (Uint32 n = 0; n < GetNumSepImgs(); ++n)
{
const auto& SepImg = GetSepImg(n);
HandleSepImg(SepImg, n);
}
for (Uint32 n = 0; n < GetNumInptAtts(); ++n)
{
const auto& InptAtt = GetInptAtt(n);
HandleInptAtt(InptAtt, n);
}
for (Uint32 n = 0; n < GetNumAccelStructs(); ++n)
{
const auto& AccelStruct = GetAccelStruct(n);
HandleAccelStruct(AccelStruct, n);
}
static_assert(Uint32{SPIRVShaderResourceAttribs::ResourceType::NumResourceTypes} == 12, "Please handle the new resource type here, if needed");
}
template <typename THandler>
void ProcessResources(THandler Handler) const
{
for (Uint32 n = 0; n < GetTotalResources(); ++n)
{
const auto& Res = GetResource(n);
Handler(Res, n);
}
}
std::string DumpResources();
// clang-format off
const char* GetCombinedSamplerSuffix() const { return m_CombinedSamplerSuffix; }
const char* GetShaderName() const { return m_ShaderName; }
bool IsUsingCombinedSamplers() const { return m_CombinedSamplerSuffix != nullptr; }
// clang-format on
bool IsHLSLSource() const { return m_IsHLSLSource; }
private:
void Initialize(IMemoryAllocator& Allocator,
const ResourceCounters& Counters,
Uint32 NumShaderStageInputs,
size_t ResourceNamesPoolSize,
StringPool& ResourceNamesPool);
SPIRVShaderResourceAttribs& GetResAttribs(Uint32 n, Uint32 NumResources, Uint32 Offset) noexcept
{
VERIFY(n < NumResources, "Resource index (", n, ") is out of range. Total resource count: ", NumResources);
VERIFY_EXPR(Offset + n < m_TotalResources);
return reinterpret_cast<SPIRVShaderResourceAttribs*>(m_MemoryBuffer.get())[Offset + n];
}
const SPIRVShaderResourceAttribs& GetResAttribs(Uint32 n, Uint32 NumResources, Uint32 Offset) const noexcept
{
VERIFY(n < NumResources, "Resource index (", n, ") is out of range. Total resource count: ", NumResources);
VERIFY_EXPR(Offset + n < m_TotalResources);
return reinterpret_cast<SPIRVShaderResourceAttribs*>(m_MemoryBuffer.get())[Offset + n];
}
// clang-format off
SPIRVShaderResourceAttribs& GetUB (Uint32 n)noexcept{ return GetResAttribs(n, GetNumUBs(), 0 ); }
SPIRVShaderResourceAttribs& GetSB (Uint32 n)noexcept{ return GetResAttribs(n, GetNumSBs(), m_StorageBufferOffset ); }
SPIRVShaderResourceAttribs& GetImg (Uint32 n)noexcept{ return GetResAttribs(n, GetNumImgs(), m_StorageImageOffset ); }
SPIRVShaderResourceAttribs& GetSmpldImg (Uint32 n)noexcept{ return GetResAttribs(n, GetNumSmpldImgs(), m_SampledImageOffset ); }
SPIRVShaderResourceAttribs& GetAC (Uint32 n)noexcept{ return GetResAttribs(n, GetNumACs(), m_AtomicCounterOffset ); }
SPIRVShaderResourceAttribs& GetSepSmplr (Uint32 n)noexcept{ return GetResAttribs(n, GetNumSepSmplrs(), m_SeparateSamplerOffset); }
SPIRVShaderResourceAttribs& GetSepImg (Uint32 n)noexcept{ return GetResAttribs(n, GetNumSepImgs(), m_SeparateImageOffset ); }
SPIRVShaderResourceAttribs& GetInptAtt (Uint32 n)noexcept{ return GetResAttribs(n, GetNumInptAtts(), m_InputAttachmentOffset); }
SPIRVShaderResourceAttribs& GetAccelStruct(Uint32 n)noexcept{ return GetResAttribs(n, GetNumAccelStructs(), m_AccelStructOffset ); }
SPIRVShaderResourceAttribs& GetResource (Uint32 n)noexcept{ return GetResAttribs(n, GetTotalResources(), 0 ); }
// clang-format on
SPIRVShaderStageInputAttribs& GetShaderStageInputAttribs(Uint32 n) noexcept
{
return const_cast<SPIRVShaderStageInputAttribs&>(const_cast<const SPIRVShaderResources*>(this)->GetShaderStageInputAttribs(n));
}
// Memory buffer that holds all resources as continuous chunk of memory:
// | UBs | SBs | StrgImgs | SmplImgs | ACs | SepSamplers | SepImgs | Stage Inputs | Resource Names |
std::unique_ptr<void, STDDeleterRawMem<void>> m_MemoryBuffer;
const char* m_CombinedSamplerSuffix = nullptr;
const char* m_ShaderName = nullptr;
using OffsetType = Uint16;
OffsetType m_StorageBufferOffset = 0;
OffsetType m_StorageImageOffset = 0;
OffsetType m_SampledImageOffset = 0;
OffsetType m_AtomicCounterOffset = 0;
OffsetType m_SeparateSamplerOffset = 0;
OffsetType m_SeparateImageOffset = 0;
OffsetType m_InputAttachmentOffset = 0;
OffsetType m_AccelStructOffset = 0;
OffsetType m_TotalResources = 0;
OffsetType m_NumShaderStageInputs = 0;
SHADER_TYPE m_ShaderType = SHADER_TYPE_UNKNOWN;
// Inidicates if the shader was compiled from HLSL source.
bool m_IsHLSLSource = false;
};
} // namespace Diligent
|