42
42
*/
43
43
public final class TyperHelper {
44
44
45
- // Maps a ( shading language version, shader kind) pair to a mapping from builtin names to builtin
46
- // function prototypes.
47
- private static ConcurrentMap <ShadingLanguageVersion ,
48
- ConcurrentMap < ShaderKind , Map < String , List <FunctionPrototype > >>> builtins =
45
+ // Maps a shading language version + shader kind (+ WGSL compatibility) to a mapping from
46
+ // builtin names to builtin function prototypes.
47
+ private static ConcurrentMap <ShadingLanguageVersionAndKind , Map < String ,
48
+ List <FunctionPrototype >>> builtins =
49
49
new ConcurrentHashMap <>();
50
50
51
51
private TyperHelper () {
@@ -283,44 +283,48 @@ public static Type resolveTypeOfMul(Type lhsType, Type rhsType) {
283
283
* Yield the builtins available for the given shading language version and kind of shader.
284
284
*
285
285
* @param shadingLanguageVersion version of GLSL for which relevant builtins should be returned.
286
+ * @param isWgslCompatible determines whether only builtins that work when targeting WGSL
287
+ * should be included.
286
288
* @param shaderKind kind of shader (e.g. fragment or compute) for which relevant builtins
287
289
* should be returned.
288
290
* @return a mapping from name of builtin to sequence of function prototypes.
289
291
*/
290
- public static Map <String , List <FunctionPrototype >> getBuiltins (ShadingLanguageVersion
291
- shadingLanguageVersion , ShaderKind shaderKind ) {
292
+ public static Map <String , List <FunctionPrototype >> getBuiltins (
293
+ ShadingLanguageVersion shadingLanguageVersion ,
294
+ boolean isWgslCompatible ,
295
+ ShaderKind shaderKind ) {
292
296
293
297
assert shadingLanguageVersion != null ;
294
298
assert shaderKind != null ;
299
+ ShadingLanguageVersionAndKind key = new ShadingLanguageVersionAndKind (shadingLanguageVersion ,
300
+ isWgslCompatible , shaderKind );
295
301
296
- if (!builtins .containsKey (shadingLanguageVersion )) {
297
- builtins .putIfAbsent (shadingLanguageVersion , new ConcurrentHashMap <>());
298
- }
299
- if (!builtins .get (shadingLanguageVersion ).containsKey (shaderKind )) {
300
- builtins .get (shadingLanguageVersion ).putIfAbsent (shaderKind ,
301
- getBuiltinsForGlslVersion (shadingLanguageVersion , shaderKind ));
302
+ if (!builtins .containsKey (key )) {
303
+ builtins .putIfAbsent (key ,
304
+ getBuiltinsForGlslVersion (shadingLanguageVersion , isWgslCompatible , shaderKind ));
302
305
}
303
- return Collections .unmodifiableMap (builtins .get (shadingLanguageVersion ). get ( shaderKind ));
306
+ return Collections .unmodifiableMap (builtins .get (key ));
304
307
}
305
308
306
309
private static Map <String , List <FunctionPrototype >> getBuiltinsForGlslVersion (
307
- ShadingLanguageVersion shadingLanguageVersion , ShaderKind shaderKind ) {
310
+ ShadingLanguageVersion shadingLanguageVersion , boolean isWgslCompatible ,
311
+ ShaderKind shaderKind ) {
308
312
Map <String , List <FunctionPrototype >> builtinsForVersion = new HashMap <>();
309
313
310
314
// Section numbers refer to the ESSL 3.2 specification
311
315
312
316
// 8.1: Angle and Trigonometric Functions
313
317
314
- getBuiltinsForGlslVersionAngleAndTrigonometric (shadingLanguageVersion ,
315
- builtinsForVersion );
318
+ getBuiltinsForGlslVersionAngleAndTrigonometric (builtinsForVersion , shadingLanguageVersion ,
319
+ isWgslCompatible );
316
320
317
321
// 8.2: Exponential Functions
318
322
319
323
getBuiltinsForGlslVersionExponential (builtinsForVersion );
320
324
321
325
// 8.3: Common Functions
322
326
323
- getBuiltinsForGlslVersionCommon (shadingLanguageVersion , builtinsForVersion );
327
+ getBuiltinsForGlslVersionCommon (builtinsForVersion , shadingLanguageVersion , isWgslCompatible );
324
328
325
329
// 8.4: Floating-Point Pack and Unpack Functions
326
330
@@ -332,15 +336,15 @@ private static Map<String, List<FunctionPrototype>> getBuiltinsForGlslVersion(
332
336
333
337
// 8.6: Matrix Functions
334
338
335
- getBuiltinsForGlslVersionMatrix (builtinsForVersion , shadingLanguageVersion );
339
+ getBuiltinsForGlslVersionMatrix (builtinsForVersion , shadingLanguageVersion , isWgslCompatible );
336
340
337
341
// 8.7: Vector Relational Functions
338
342
339
343
getBuiltinsForGlslVersionVectorRelational (builtinsForVersion , shadingLanguageVersion );
340
344
341
345
// 8.8: Integer Functions
342
346
343
- getBuiltinsForGlslVersionInteger (builtinsForVersion , shadingLanguageVersion );
347
+ getBuiltinsForGlslVersionInteger (builtinsForVersion , shadingLanguageVersion , isWgslCompatible );
344
348
345
349
// 8.9. Texture Functions
346
350
getBuiltinsForGlslVersionTexture (builtinsForVersion , shadingLanguageVersion , shaderKind );
@@ -376,10 +380,12 @@ private static Map<String, List<FunctionPrototype>> getBuiltinsForGlslVersion(
376
380
*
377
381
* @param builtinsForVersion the list of builtins to add prototypes to
378
382
* @param shadingLanguageVersion the version of GLSL in use
383
+ * @param isWgslCompatible determines whether to restrict to builtins that WGSL also supports
379
384
*/
380
385
private static void getBuiltinsForGlslVersionAngleAndTrigonometric (
386
+ Map <String , List <FunctionPrototype >> builtinsForVersion ,
381
387
ShadingLanguageVersion shadingLanguageVersion ,
382
- Map < String , List < FunctionPrototype >> builtinsForVersion ) {
388
+ boolean isWgslCompatible ) {
383
389
if (shadingLanguageVersion .supportedAngleAndTrigonometricFunctions ()) {
384
390
{
385
391
final String name = "radians" ;
@@ -461,21 +467,21 @@ private static void getBuiltinsForGlslVersionAngleAndTrigonometric(
461
467
}
462
468
}
463
469
464
- {
470
+ if (! isWgslCompatible ) {
465
471
final String name = "asinh" ;
466
472
for (Type t : genType ()) {
467
473
addBuiltin (builtinsForVersion , name , t , t );
468
474
}
469
475
}
470
476
471
- {
477
+ if (! isWgslCompatible ) {
472
478
final String name = "acosh" ;
473
479
for (Type t : genType ()) {
474
480
addBuiltin (builtinsForVersion , name , t , t );
475
481
}
476
482
}
477
483
478
- {
484
+ if (! isWgslCompatible ) {
479
485
final String name = "atanh" ;
480
486
for (Type t : genType ()) {
481
487
addBuiltin (builtinsForVersion , name , t , t );
@@ -674,10 +680,12 @@ private static void getBuiltinsForGlslVersionVectorRelational(
674
680
*
675
681
* @param builtinsForVersion the list of builtins to add prototypes to
676
682
* @param shadingLanguageVersion the version of GLSL in use
683
+ * @param isWgslCompatible determines whether to restrict to builtins that WGSL also supports
677
684
*/
678
685
private static void getBuiltinsForGlslVersionInteger (
679
686
Map <String , List <FunctionPrototype >> builtinsForVersion ,
680
- ShadingLanguageVersion shadingLanguageVersion ) {
687
+ ShadingLanguageVersion shadingLanguageVersion ,
688
+ boolean isWgslCompatible ) {
681
689
if (shadingLanguageVersion .supportedIntegerFunctions ()) {
682
690
{
683
691
final String name = "uaddCarry" ;
@@ -713,7 +721,7 @@ private static void getBuiltinsForGlslVersionInteger(
713
721
}
714
722
}
715
723
716
- {
724
+ if (! isWgslCompatible ) {
717
725
final String name = "bitfieldExtract" ;
718
726
for (Type t : igenType ()) {
719
727
addBuiltin (builtinsForVersion , name , t , t , BasicType .INT , BasicType .INT );
@@ -723,7 +731,7 @@ private static void getBuiltinsForGlslVersionInteger(
723
731
}
724
732
}
725
733
726
- {
734
+ if (! isWgslCompatible ) {
727
735
final String name = "bitfieldInsert" ;
728
736
for (Type t : igenType ()) {
729
737
addBuiltin (builtinsForVersion , name , t , t , t , BasicType .INT , BasicType .INT );
@@ -753,15 +761,15 @@ private static void getBuiltinsForGlslVersionInteger(
753
761
}
754
762
}
755
763
756
- {
764
+ if (! isWgslCompatible ) {
757
765
final String name = "findLSB" ;
758
766
for (int i = 0 ; i < igenType ().size (); i ++) {
759
767
addBuiltin (builtinsForVersion , name , igenType ().get (i ), igenType ().get (i ));
760
768
addBuiltin (builtinsForVersion , name , igenType ().get (i ), ugenType ().get (i ));
761
769
}
762
770
}
763
771
764
- {
772
+ if (! isWgslCompatible ) {
765
773
final String name = "findMSB" ;
766
774
for (int i = 0 ; i < igenType ().size (); i ++) {
767
775
addBuiltin (builtinsForVersion , name , igenType ().get (i ), igenType ().get (i ));
@@ -1028,10 +1036,12 @@ private static void getBuiltinsForGlslVersionShaderMemoryControl(
1028
1036
*
1029
1037
* @param builtinsForVersion the list of builtins to add prototypes to
1030
1038
* @param shadingLanguageVersion the version of GLSL in use
1039
+ * @param isWgslCompatible determines whether to restrict to builtins that WGSL also supports
1031
1040
*/
1032
1041
private static void getBuiltinsForGlslVersionMatrix (
1033
1042
Map <String , List <FunctionPrototype >> builtinsForVersion ,
1034
- ShadingLanguageVersion shadingLanguageVersion ) {
1043
+ ShadingLanguageVersion shadingLanguageVersion ,
1044
+ boolean isWgslCompatible ) {
1035
1045
{
1036
1046
final String name = "matrixCompMult" ;
1037
1047
for (Type t : BasicType .allMatrixTypes ()) {
@@ -1068,14 +1078,14 @@ private static void getBuiltinsForGlslVersionMatrix(
1068
1078
addBuiltin (builtinsForVersion , name , BasicType .MAT4X4 , BasicType .MAT4X4 );
1069
1079
}
1070
1080
1071
- if (shadingLanguageVersion .supportedDeterminant ()) {
1081
+ if (shadingLanguageVersion .supportedDeterminant () && ! isWgslCompatible ) {
1072
1082
final String name = "determinant" ;
1073
1083
addBuiltin (builtinsForVersion , name , BasicType .FLOAT , BasicType .MAT2X2 );
1074
1084
addBuiltin (builtinsForVersion , name , BasicType .FLOAT , BasicType .MAT3X3 );
1075
1085
addBuiltin (builtinsForVersion , name , BasicType .FLOAT , BasicType .MAT4X4 );
1076
1086
}
1077
1087
1078
- if (shadingLanguageVersion .supportedInverse ()) {
1088
+ if (shadingLanguageVersion .supportedInverse () && ! isWgslCompatible ) {
1079
1089
final String name = "inverse" ;
1080
1090
addBuiltin (builtinsForVersion , name , BasicType .MAT2X2 , BasicType .MAT2X2 );
1081
1091
addBuiltin (builtinsForVersion , name , BasicType .MAT3X3 , BasicType .MAT3X3 );
@@ -1214,10 +1224,12 @@ private static void getBuiltinsForGlslVersionFloatingPointPackAndUnpack(
1214
1224
*
1215
1225
* @param builtinsForVersion the list of builtins to add prototypes to
1216
1226
* @param shadingLanguageVersion the version of GLSL in use
1227
+ * @param isWgslCompatible determines whether to restrict to builtins that WGSL also supports
1217
1228
*/
1218
1229
private static void getBuiltinsForGlslVersionCommon (
1230
+ Map <String , List <FunctionPrototype >> builtinsForVersion ,
1219
1231
ShadingLanguageVersion shadingLanguageVersion ,
1220
- Map < String , List < FunctionPrototype >> builtinsForVersion ) {
1232
+ boolean isWgslCompatible ) {
1221
1233
{
1222
1234
final String name = "abs" ;
1223
1235
for (Type t : genType ()) {
@@ -1230,7 +1242,7 @@ private static void getBuiltinsForGlslVersionCommon(
1230
1242
}
1231
1243
}
1232
1244
1233
- {
1245
+ if (! isWgslCompatible ) {
1234
1246
final String name = "sign" ;
1235
1247
for (Type t : genType ()) {
1236
1248
addBuiltin (builtinsForVersion , name , t , t );
@@ -1294,7 +1306,7 @@ private static void getBuiltinsForGlslVersionCommon(
1294
1306
}
1295
1307
}
1296
1308
1297
- if (shadingLanguageVersion .supportedModf ()) {
1309
+ if (shadingLanguageVersion .supportedModf () && ! isWgslCompatible ) {
1298
1310
{
1299
1311
final String name = "modf" ;
1300
1312
for (Type t : genType ()) {
@@ -1506,7 +1518,7 @@ private static void getBuiltinsForGlslVersionCommon(
1506
1518
}
1507
1519
}
1508
1520
1509
- if (shadingLanguageVersion .supportedFrexp ()) {
1521
+ if (shadingLanguageVersion .supportedFrexp () && ! isWgslCompatible ) {
1510
1522
{
1511
1523
final String name = "frexp" ;
1512
1524
addBuiltin (builtinsForVersion , name , BasicType .FLOAT , BasicType .FLOAT ,
0 commit comments