Repository navigation
Expand file tree
/
Copy pathUtils.cpp
More file actions
401 lines (366 loc) · 13.8 KB
/
Copy pathUtils.cpp
File metadata and controls
401 lines (366 loc) · 13.8 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// Copyright 2024 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "compiler/translator/wgsl/Utils.h"
#include "common/log_utils.h"
#include "compiler/translator/BaseTypes.h"
#include "compiler/translator/Common.h"
#include "compiler/translator/ImmutableString.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/Types.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
const char kWrappedPrefix[] = "ANGLE_wrapped_";
}
template <typename StringStreamType>
void WriteWgslBareTypeName(StringStreamType &output,
const TType &type,
const EmitTypeConfig &config)
{
const TBasicType basicType = type.getBasicType();
switch (basicType)
{
case TBasicType::EbtVoid:
output << type.getBasicString();
break;
case TBasicType::EbtBool:
if (config.addressSpace == WgslAddressSpace::Uniform)
{
// The uniform address space does not support bools in WGSL, so they are emulated
// with u32 (which matches gles because bools are 4-bytes long in std140).
output << "u32";
}
else
{
output << "bool";
}
break;
// TODO(anglebug.com/42267100): is there double precision (f64) in GLSL? It doesn't really
// exist in WGSL (i.e. f64 does not exist but AbstractFloat can handle 64 bits???) Metal
// does not have 64 bit double precision types. It's being implemented in WGPU:
// https://github.com/gpuweb/gpuweb/issues/2805
case TBasicType::EbtFloat:
output << "f32";
break;
case TBasicType::EbtInt:
output << "i32";
break;
case TBasicType::EbtUInt:
output << "u32";
break;
case TBasicType::EbtStruct:
WriteNameOf(output, *type.getStruct(),
type.getStruct()->isImplementingInterfaceBlock() ? kUserBlockNamePrefix
: kUserVariableNamePrefix);
break;
case TBasicType::EbtInterfaceBlock:
WriteNameOf(output, *type.getInterfaceBlock(), kUserBlockNamePrefix);
break;
default:
if (IsSampler(basicType))
{
// Variables of sampler type should be written elsewhere since they require special
// handling; they are split into two different variables in WGSL.
// TODO(anglebug.com/389145696): this is reachable if a sampler is passed as a
// function parameter. They should be monomorphized.
UNIMPLEMENTED();
}
else if (IsImage(basicType))
{
// GLSL's image types are not implemented in this backend.
UNIMPLEMENTED();
output << "texture_storage_2d<";
switch (type.getBasicType())
{
case EbtImage2D:
output << "f32";
break;
case EbtIImage2D:
output << "i32";
break;
case EbtUImage2D:
output << "u32";
break;
default:
UNIMPLEMENTED();
break;
}
if (type.getMemoryQualifier().readonly || type.getMemoryQualifier().writeonly)
{
UNIMPLEMENTED();
}
output << ">";
}
else
{
UNREACHABLE();
}
break;
}
}
template <typename StringStreamType>
void WriteNameOf(StringStreamType &output,
SymbolType symbolType,
const ImmutableString &name,
char userSymbolPrefix)
{
switch (symbolType)
{
case SymbolType::BuiltIn:
output << name;
break;
case SymbolType::UserDefined:
output << '_' << userSymbolPrefix << name;
break;
case SymbolType::AngleInternal:
output << name;
break;
case SymbolType::Empty:
// TODO(anglebug.com/42267100): support this if necessary
UNREACHABLE();
}
}
template <typename StringStreamType>
void WriteWgslType(StringStreamType &output, const TType &type, const EmitTypeConfig &config)
{
if (type.isArray())
{
// WGSL does not support samplers anywhere inside structs or arrays.
ASSERT(!type.isSampler() && !type.isStructureContainingSamplers());
// Examples:
// array<f32, 5>
// array<array<u32, 5>, 10>
output << "array<";
TType innerType = type;
innerType.toArrayElementType();
if (ElementTypeNeedsUniformWrapperStruct(config.addressSpace == WgslAddressSpace::Uniform,
&type))
{
// Multidimensional arrays not currently supported in uniforms in the WebGPU backend
ASSERT(!innerType.isArray());
// Due to uniform address space layout constraints, certain array element types must
// be wrapped in a wrapper struct.
// Example: array<ANGLE_wrapped_f32, 5>
output << MakeUniformWrapperStructName(&innerType);
}
else
{
WriteWgslType(output, innerType, config);
}
output << ", " << type.getOutermostArraySize() << ">";
}
else if (type.isVector())
{
output << "vec" << static_cast<uint32_t>(type.getNominalSize()) << "<";
WriteWgslBareTypeName(output, type, config);
output << ">";
}
else if (type.isMatrix())
{
if (config.addressSpace == WgslAddressSpace::Uniform && type.getRows() == 2)
{
// matCx2 in the uniform address space is too packed for std140, and so they will be
// represented by an array<ANGLE_wrapped_vec2, C>.
output << "array<" << kWrappedPrefix << "vec2, "
<< static_cast<uint32_t>(type.getCols()) << ">";
}
else
{
output << "mat" << static_cast<uint32_t>(type.getCols()) << "x"
<< static_cast<uint32_t>(type.getRows()) << "<";
WriteWgslBareTypeName(output, type, config);
output << ">";
}
}
else
{
// This type has no dimensions and is equivalent to its bare type.
WriteWgslBareTypeName(output, type, config);
}
}
template void WriteWgslBareTypeName<TInfoSinkBase>(TInfoSinkBase &output,
const TType &type,
const EmitTypeConfig &config);
template void WriteNameOf<TInfoSinkBase>(TInfoSinkBase &output,
SymbolType symbolType,
const ImmutableString &name,
char userSymbolPrefix);
template void WriteWgslType<TInfoSinkBase>(TInfoSinkBase &output,
const TType &type,
const EmitTypeConfig &config);
template void WriteWgslBareTypeName<TStringStream>(TStringStream &output,
const TType &type,
const EmitTypeConfig &config);
template void WriteNameOf<TStringStream>(TStringStream &output,
SymbolType symbolType,
const ImmutableString &name,
char userSymbolPrefix);
template void WriteWgslType<TStringStream>(TStringStream &output,
const TType &type,
const EmitTypeConfig &config);
template <typename StringStreamType>
void WriteWgslSamplerType(StringStreamType &output,
const TType &type,
WgslSamplerTypeConfig samplerType)
{
ASSERT(type.isSampler());
if (samplerType == WgslSamplerTypeConfig::Texture)
{
output << "texture";
if (IsShadowSampler(type.getBasicType()))
{
output << "_depth";
}
if (IsSamplerMS(type.getBasicType()))
{
output << "_multisampled";
ASSERT(IsSampler2D(type.getBasicType()));
// Unsupported in wGSL, it seems.
ASSERT(!IsSampler2DMSArray(type.getBasicType()));
}
if (IsSampler2D(type.getBasicType()) || IsSampler2DArray(type.getBasicType()))
{
output << "_2d";
}
else if (IsSampler3D(type.getBasicType()))
{
output << "_3d";
}
else if (IsSamplerCube(type.getBasicType()))
{
output << "_cube";
}
if (IsSamplerArray(type.getBasicType()))
{
ASSERT(!IsSampler3D(type.getBasicType()));
output << "_array";
}
// Shadow samplers are always floating point in both GLSL and WGSL and don't need to be
// parameterized.
if (!IsShadowSampler(type.getBasicType()))
{
output << "<";
if (!IsIntegerSampler(type.getBasicType()))
{
output << "f32";
}
else if (!IsIntegerSamplerUnsigned(type.getBasicType()))
{
output << "i32";
}
else
{
output << "u32";
}
output << ">";
}
if (type.getMemoryQualifier().readonly || type.getMemoryQualifier().writeonly)
{
// TODO(anglebug.com/42267100): implement memory qualifiers.
UNIMPLEMENTED();
}
}
else
{
ASSERT(samplerType == WgslSamplerTypeConfig::Sampler);
// sampler or sampler_comparison.
if (IsShadowSampler(type.getBasicType()))
{
output << "sampler_comparison";
}
else
{
output << "sampler";
}
}
}
template void WriteWgslSamplerType<TInfoSinkBase>(TInfoSinkBase &output,
const TType &type,
WgslSamplerTypeConfig samplerType);
template void WriteWgslSamplerType<TStringStream>(TStringStream &output,
const TType &type,
WgslSamplerTypeConfig samplerType);
ImmutableString MakeUniformWrapperStructName(const TType *type)
{
TType typeToOutput(*type);
const char *typeStr;
// Bools are represented as u32s in the uniform address space (and bvecs as uvecs).
// TODO(anglebug.com/376553328): simplify by using WriteWgslType({WgslAddressSpace::Uniform})
// here.
if (typeToOutput.getBasicType() == EbtBool)
{
typeToOutput.setBasicType(TBasicType::EbtUInt);
}
typeStr = typeToOutput.getBuiltInTypeNameString();
return BuildConcatenatedImmutableString(kWrappedPrefix, typeStr);
}
bool ElementTypeNeedsUniformWrapperStruct(bool inUniformAddressSpace, const TType *type)
{
// Only types that are used as array element types in the uniform address space need wrapper
// structs. If the array element type is a struct it does not need to be wrapped in another
// layer of struct.
if (!inUniformAddressSpace || !type->isArray() || type->getStruct())
{
return false;
}
TType elementType = *type;
elementType.toArrayElementType();
// If the array element type's stride is already a multiple of 16, it does not need a wrapper
// struct.
//
// The remaining possible element types are scalars, vectors, matrices, and other arrays.
// - Scalars need to be aligned to 16.
// - vec3 and vec4 are already aligned to 16, but vec2 needs to be aligned.
// - Matrices are aligned to 16 automatically, except matCx2 which already needs to be handled
// by specialized code anyway.
// - WebGL2 doesn't support nested arrays so this won't either.
ASSERT(!elementType.isArray());
return elementType.isScalar() || (elementType.isVector() && elementType.getNominalSize() == 2);
}
GlobalVars FindGlobalVars(TIntermBlock *root)
{
GlobalVars globals;
for (TIntermNode *node : *root->getSequence())
{
if (TIntermDeclaration *declNode = node->getAsDeclarationNode())
{
Declaration decl = ViewDeclaration(*declNode);
globals.insert({decl.symbol.variable().name(), declNode});
}
}
return globals;
}
WgslPointerAddressSpace GetWgslAddressSpaceForPointer(const TType &type)
{
switch (type.getQualifier())
{
case EvqTemporary:
// NOTE: As of Sept 2025, parameters are immutable in WGSL (and are handled by an AST pass
// that copies parameters to temporaries). Include these here in case parameters become
// mutable in the future.
case EvqParamIn:
case EvqParamOut:
case EvqParamInOut:
return WgslPointerAddressSpace::Function;
default:
// EvqGlobal and various other shader outputs/builtins are all globals.
return WgslPointerAddressSpace::Private;
}
}
ImmutableString StringForWgslPointerAddressSpace(WgslPointerAddressSpace as)
{
switch (as)
{
case WgslPointerAddressSpace::Function:
return ImmutableString("function");
case WgslPointerAddressSpace::Private:
return ImmutableString("private");
}
}
} // namespace sh