1 //
2 // Copyright 2002 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #ifndef COMPILER_TRANSLATOR_BASETYPES_H_
8 #define COMPILER_TRANSLATOR_BASETYPES_H_
9
10 #include <algorithm>
11 #include <array>
12
13 #include "GLSLANG/ShaderLang.h"
14 #include "common/debug.h"
15 #include "compiler/translator/ImmutableString.h"
16
17 namespace sh
18 {
19
20 //
21 // Precision qualifiers
22 //
23 enum TPrecision
24 {
25 // These need to be kept sorted
26 EbpUndefined,
27 EbpLow,
28 EbpMedium,
29 EbpHigh,
30
31 // end of list
32 EbpLast
33 };
34
getPrecisionString(TPrecision p)35 inline const char *getPrecisionString(TPrecision p)
36 {
37 switch (p)
38 {
39 case EbpHigh:
40 return "highp";
41 case EbpMedium:
42 return "mediump";
43 case EbpLow:
44 return "lowp";
45 default:
46 return "mediump"; // Safest fallback
47 }
48 }
49
50 //
51 // Basic type. Arrays, vectors, etc., are orthogonal to this.
52 //
53 enum TBasicType
54 {
55 EbtVoid,
56 EbtFloat,
57 EbtDouble,
58 EbtInt,
59 EbtUInt,
60 EbtBool,
61
62 EbtAtomicCounter,
63 EbtYuvCscStandardEXT, // Only valid if EXT_YUV_target exists.
64
65 EbtGuardSamplerBegin, // non type: see implementation of IsSampler()
66 EbtSampler2D = EbtGuardSamplerBegin,
67 EbtSampler3D,
68 EbtSamplerCube,
69 EbtSampler2DArray,
70 EbtSamplerExternalOES, // Only valid if OES_EGL_image_external exists.
71 EbtSamplerExternal2DY2YEXT, // Only valid if GL_EXT_YUV_target exists.
72 EbtSampler2DRect, // Only valid if GL_ARB_texture_rectangle exists.
73 EbtSampler2DMS,
74 EbtSampler2DMSArray,
75 EbtISampler2D,
76 EbtISampler3D,
77 EbtISamplerCube,
78 EbtISampler2DArray,
79 EbtISampler2DMS,
80 EbtISampler2DMSArray,
81 EbtUSampler2D,
82 EbtUSampler3D,
83 EbtUSamplerCube,
84 EbtUSampler2DArray,
85 EbtUSampler2DMS,
86 EbtUSampler2DMSArray,
87 EbtSampler2DShadow,
88 EbtSamplerCubeShadow,
89 EbtSampler2DArrayShadow,
90 EbtSampler1D, // Desktop GLSL sampler types
91 EbtSampler1DArray,
92 EbtSampler1DArrayShadow,
93 EbtSamplerBuffer,
94 EbtSamplerCubeArray,
95 EbtSamplerCubeArrayShadow,
96 EbtSampler1DShadow,
97 EbtSampler2DRectShadow,
98 EbtISampler1D,
99 EbtISampler1DArray,
100 EbtISampler2DRect,
101 EbtISamplerBuffer,
102 EbtISamplerCubeArray,
103 EbtUSampler1D,
104 EbtUSampler1DArray,
105 EbtUSampler2DRect,
106 EbtUSamplerBuffer,
107 EbtUSamplerCubeArray,
108 EbtSamplerVideoWEBGL,
109 EbtGuardSamplerEnd = EbtSamplerVideoWEBGL, // non type: see implementation of IsSampler()
110
111 // images
112 EbtGuardImageBegin,
113 EbtImage2D = EbtGuardImageBegin,
114 EbtIImage2D,
115 EbtUImage2D,
116 EbtImage3D,
117 EbtIImage3D,
118 EbtUImage3D,
119 EbtImage2DArray,
120 EbtIImage2DArray,
121 EbtUImage2DArray,
122 EbtImageCube,
123 EbtIImageCube,
124 EbtUImageCube,
125 EbtImage1D, // Desktop GLSL image types
126 EbtIImage1D,
127 EbtUImage1D,
128 EbtImage1DArray,
129 EbtIImage1DArray,
130 EbtUImage1DArray,
131 EbtImage2DMS,
132 EbtIImage2DMS,
133 EbtUImage2DMS,
134 EbtImage2DMSArray,
135 EbtIImage2DMSArray,
136 EbtUImage2DMSArray,
137 EbtImage2DRect,
138 EbtIImage2DRect,
139 EbtUImage2DRect,
140 EbtImageCubeArray,
141 EbtIImageCubeArray,
142 EbtUImageCubeArray,
143 EbtImageRect,
144 EbtIImageRect,
145 EbtUImageRect,
146 EbtImageBuffer,
147 EbtIImageBuffer,
148 EbtUImageBuffer,
149 EbtGuardImageEnd = EbtUImageBuffer,
150
151 EbtSubpassInput,
152 EbtISubpassInput,
153 EbtUSubpassInput,
154 EbtSubpassInputMS,
155 EbtISubpassInputMS,
156 EbtUSubpassInputMS,
157
158 EbtLastSimpleType = EbtUSubpassInputMS,
159
160 EbtStruct,
161 EbtInterfaceBlock,
162
163 // end of list
164 EbtLast = EbtInterfaceBlock
165 };
166
167 class TBasicMangledName
168 {
169 public:
TBasicMangledName(TBasicType t)170 constexpr TBasicMangledName(TBasicType t) : mName{'\0', '\0'}
171 {
172 if (t > EbtLastSimpleType)
173 {
174 mName[0] = '{';
175 mName[1] = '\0';
176 }
177 else if (t < 26)
178 {
179 mName[0] = '0';
180 mName[1] = static_cast<char>('A' + t);
181 }
182 else if (t < 52)
183 {
184 mName[0] = '0';
185 mName[1] = static_cast<char>('a' - 26 + t);
186 }
187 else if (t < 78)
188 {
189 mName[0] = '1';
190 mName[1] = static_cast<char>('A' - 52 + t);
191 }
192 else if (t < 104)
193 {
194 mName[0] = '1';
195 mName[1] = static_cast<char>('a' - 78 + t);
196 }
197 }
198
getName()199 constexpr char *getName() { return mName; }
200
201 static constexpr int mangledNameSize = 2;
202
203 private:
204 char mName[mangledNameSize];
205 };
206
207 const char *getBasicString(TBasicType t);
208
IsSampler(TBasicType type)209 inline bool IsSampler(TBasicType type)
210 {
211 return type >= EbtGuardSamplerBegin && type <= EbtGuardSamplerEnd;
212 }
213
IsImage(TBasicType type)214 inline bool IsImage(TBasicType type)
215 {
216 return type >= EbtGuardImageBegin && type <= EbtGuardImageEnd;
217 }
218
IsAtomicCounter(TBasicType type)219 inline bool IsAtomicCounter(TBasicType type)
220 {
221 return type == EbtAtomicCounter;
222 }
223
IsOpaqueType(TBasicType type)224 inline bool IsOpaqueType(TBasicType type)
225 {
226 return IsSampler(type) || IsImage(type) || IsAtomicCounter(type);
227 }
228
IsIntegerSampler(TBasicType type)229 inline bool IsIntegerSampler(TBasicType type)
230 {
231 switch (type)
232 {
233 case EbtISampler2D:
234 case EbtISampler3D:
235 case EbtISamplerCube:
236 case EbtISampler2DArray:
237 case EbtISampler2DMS:
238 case EbtISampler2DMSArray:
239 case EbtUSampler2D:
240 case EbtUSampler3D:
241 case EbtUSamplerCube:
242 case EbtUSampler2DArray:
243 case EbtUSampler2DMS:
244 case EbtUSampler2DMSArray:
245 case EbtISampler1D:
246 case EbtISampler1DArray:
247 case EbtISampler2DRect:
248 case EbtISamplerBuffer:
249 case EbtISamplerCubeArray:
250 case EbtUSampler1D:
251 case EbtUSampler1DArray:
252 case EbtUSampler2DRect:
253 case EbtUSamplerBuffer:
254 case EbtUSamplerCubeArray:
255 return true;
256 case EbtSampler2D:
257 case EbtSampler3D:
258 case EbtSamplerCube:
259 case EbtSamplerExternalOES:
260 case EbtSamplerExternal2DY2YEXT:
261 case EbtSampler2DRect:
262 case EbtSampler2DArray:
263 case EbtSampler2DShadow:
264 case EbtSamplerCubeShadow:
265 case EbtSampler2DArrayShadow:
266 case EbtSampler2DMS:
267 case EbtSampler2DMSArray:
268 case EbtSampler1D:
269 case EbtSampler1DArray:
270 case EbtSampler1DArrayShadow:
271 case EbtSamplerBuffer:
272 case EbtSamplerCubeArray:
273 case EbtSamplerCubeArrayShadow:
274 case EbtSampler1DShadow:
275 case EbtSampler2DRectShadow:
276 case EbtSamplerVideoWEBGL:
277 return false;
278 default:
279 ASSERT(!IsSampler(type));
280 }
281
282 return false;
283 }
284
IsIntegerSamplerUnsigned(TBasicType type)285 inline bool IsIntegerSamplerUnsigned(TBasicType type)
286 {
287 switch (type)
288 {
289 case EbtISampler2D:
290 case EbtISampler3D:
291 case EbtISamplerCube:
292 case EbtISampler2DArray:
293 case EbtISampler2DMS:
294 case EbtISampler2DMSArray:
295 case EbtISampler1D:
296 case EbtISampler1DArray:
297 case EbtISampler2DRect:
298 case EbtISamplerBuffer:
299 case EbtISamplerCubeArray:
300 return false;
301 case EbtUSampler2D:
302 case EbtUSampler3D:
303 case EbtUSamplerCube:
304 case EbtUSampler2DArray:
305 case EbtUSampler2DMS:
306 case EbtUSampler2DMSArray:
307 case EbtUSampler1D:
308 case EbtUSampler1DArray:
309 case EbtUSampler2DRect:
310 case EbtUSamplerBuffer:
311 case EbtUSamplerCubeArray:
312 return true;
313 default:
314 ASSERT(!IsIntegerSampler(type));
315 }
316
317 return false;
318 }
319
IsSampler2DMS(TBasicType type)320 inline bool IsSampler2DMS(TBasicType type)
321 {
322 switch (type)
323 {
324 case EbtSampler2DMS:
325 case EbtISampler2DMS:
326 case EbtUSampler2DMS:
327 return true;
328 default:
329 return false;
330 }
331 }
332
IsSampler2DMSArray(TBasicType type)333 inline bool IsSampler2DMSArray(TBasicType type)
334 {
335 switch (type)
336 {
337 case EbtSampler2DMSArray:
338 case EbtISampler2DMSArray:
339 case EbtUSampler2DMSArray:
340 return true;
341 default:
342 return false;
343 }
344 }
345
IsFloatImage(TBasicType type)346 inline bool IsFloatImage(TBasicType type)
347 {
348 switch (type)
349 {
350 case EbtImage1D:
351 case EbtImage2D:
352 case EbtImage3D:
353 case EbtImage1DArray:
354 case EbtImage2DArray:
355 case EbtImageCube:
356 case EbtImage2DMS:
357 case EbtImage2DMSArray:
358 case EbtImage2DRect:
359 case EbtImageCubeArray:
360 case EbtImageRect:
361 case EbtImageBuffer:
362 return true;
363 default:
364 break;
365 }
366
367 return false;
368 }
369
IsIntegerImage(TBasicType type)370 inline bool IsIntegerImage(TBasicType type)
371 {
372
373 switch (type)
374 {
375 case EbtIImage1D:
376 case EbtIImage2D:
377 case EbtIImage3D:
378 case EbtIImage1DArray:
379 case EbtIImage2DArray:
380 case EbtIImageCube:
381 case EbtIImage2DMS:
382 case EbtIImage2DMSArray:
383 case EbtIImage2DRect:
384 case EbtIImageCubeArray:
385 case EbtIImageRect:
386 case EbtIImageBuffer:
387 return true;
388 default:
389 break;
390 }
391
392 return false;
393 }
394
IsUnsignedImage(TBasicType type)395 inline bool IsUnsignedImage(TBasicType type)
396 {
397
398 switch (type)
399 {
400 case EbtUImage1D:
401 case EbtUImage2D:
402 case EbtUImage3D:
403 case EbtUImage1DArray:
404 case EbtUImage2DArray:
405 case EbtUImageCube:
406 case EbtUImage2DMS:
407 case EbtUImage2DMSArray:
408 case EbtUImage2DRect:
409 case EbtUImageCubeArray:
410 case EbtUImageRect:
411 case EbtUImageBuffer:
412 return true;
413 default:
414 break;
415 }
416
417 return false;
418 }
419
420 // Samplers are divided into 4 disjoint categories: 2D, cube, 3D, and array.
421 // Array samplers are not 2D samplers.
IsSampler2D(TBasicType type)422 inline bool IsSampler2D(TBasicType type)
423 {
424 switch (type)
425 {
426 case EbtSampler2D:
427 case EbtISampler2D:
428 case EbtUSampler2D:
429 case EbtSampler2DRect:
430 case EbtISampler2DRect:
431 case EbtUSampler2DRect:
432 case EbtSampler2DRectShadow:
433 case EbtSamplerExternalOES:
434 case EbtSamplerExternal2DY2YEXT:
435 case EbtSampler2DShadow:
436 case EbtSampler2DMS:
437 case EbtISampler2DMS:
438 case EbtUSampler2DMS:
439 case EbtSamplerVideoWEBGL:
440 return true;
441 case EbtSampler2DArray:
442 case EbtISampler2DArray:
443 case EbtUSampler2DArray:
444 case EbtSampler2DMSArray:
445 case EbtISampler2DMSArray:
446 case EbtUSampler2DMSArray:
447 case EbtSampler2DArrayShadow:
448 case EbtSampler3D:
449 case EbtISampler3D:
450 case EbtUSampler3D:
451 case EbtISamplerCube:
452 case EbtUSamplerCube:
453 case EbtSamplerCube:
454 case EbtSamplerCubeShadow:
455 case EbtSampler1D:
456 case EbtSampler1DArray:
457 case EbtSampler1DArrayShadow:
458 case EbtSamplerBuffer:
459 case EbtSamplerCubeArray:
460 case EbtSamplerCubeArrayShadow:
461 case EbtSampler1DShadow:
462 case EbtISampler1D:
463 case EbtISampler1DArray:
464 case EbtISamplerBuffer:
465 case EbtISamplerCubeArray:
466 case EbtUSampler1D:
467 case EbtUSampler1DArray:
468 case EbtUSamplerBuffer:
469 case EbtUSamplerCubeArray:
470 return false;
471 default:
472 ASSERT(!IsSampler(type));
473 }
474
475 return false;
476 }
477
IsSamplerCube(TBasicType type)478 inline bool IsSamplerCube(TBasicType type)
479 {
480 switch (type)
481 {
482 case EbtSamplerCube:
483 case EbtISamplerCube:
484 case EbtUSamplerCube:
485 case EbtSamplerCubeShadow:
486 return true;
487 case EbtSampler2D:
488 case EbtSampler3D:
489 case EbtSamplerExternalOES:
490 case EbtSamplerExternal2DY2YEXT:
491 case EbtSampler2DRect:
492 case EbtSampler2DArray:
493 case EbtSampler2DMS:
494 case EbtSampler2DMSArray:
495 case EbtISampler2D:
496 case EbtISampler3D:
497 case EbtISampler2DArray:
498 case EbtISampler2DMS:
499 case EbtISampler2DMSArray:
500 case EbtUSampler2D:
501 case EbtUSampler3D:
502 case EbtUSampler2DArray:
503 case EbtUSampler2DMS:
504 case EbtUSampler2DMSArray:
505 case EbtSampler2DShadow:
506 case EbtSampler2DArrayShadow:
507 case EbtSampler1D:
508 case EbtSampler1DArray:
509 case EbtSampler1DArrayShadow:
510 case EbtSamplerBuffer:
511 case EbtSamplerCubeArray:
512 case EbtSamplerCubeArrayShadow:
513 case EbtSampler1DShadow:
514 case EbtSampler2DRectShadow:
515 case EbtISampler1D:
516 case EbtISampler1DArray:
517 case EbtISampler2DRect:
518 case EbtISamplerBuffer:
519 case EbtISamplerCubeArray:
520 case EbtUSampler1D:
521 case EbtUSampler1DArray:
522 case EbtUSampler2DRect:
523 case EbtUSamplerBuffer:
524 case EbtUSamplerCubeArray:
525 case EbtSamplerVideoWEBGL:
526 return false;
527 default:
528 ASSERT(!IsSampler(type));
529 }
530
531 return false;
532 }
533
IsSampler3D(TBasicType type)534 inline bool IsSampler3D(TBasicType type)
535 {
536 switch (type)
537 {
538 case EbtSampler3D:
539 case EbtISampler3D:
540 case EbtUSampler3D:
541 return true;
542 case EbtSampler2D:
543 case EbtSamplerCube:
544 case EbtSamplerExternalOES:
545 case EbtSamplerExternal2DY2YEXT:
546 case EbtSampler2DRect:
547 case EbtSampler2DArray:
548 case EbtSampler2DMS:
549 case EbtSampler2DMSArray:
550 case EbtISampler2D:
551 case EbtISamplerCube:
552 case EbtISampler2DArray:
553 case EbtISampler2DMS:
554 case EbtISampler2DMSArray:
555 case EbtUSampler2D:
556 case EbtUSamplerCube:
557 case EbtUSampler2DArray:
558 case EbtUSampler2DMS:
559 case EbtUSampler2DMSArray:
560 case EbtSampler2DShadow:
561 case EbtSamplerCubeShadow:
562 case EbtSampler2DArrayShadow:
563 case EbtSampler1D:
564 case EbtSampler1DArray:
565 case EbtSampler1DArrayShadow:
566 case EbtSamplerBuffer:
567 case EbtSamplerCubeArray:
568 case EbtSamplerCubeArrayShadow:
569 case EbtSampler1DShadow:
570 case EbtSampler2DRectShadow:
571 case EbtISampler1D:
572 case EbtISampler1DArray:
573 case EbtISampler2DRect:
574 case EbtISamplerBuffer:
575 case EbtISamplerCubeArray:
576 case EbtUSampler1D:
577 case EbtUSampler1DArray:
578 case EbtUSampler2DRect:
579 case EbtUSamplerBuffer:
580 case EbtUSamplerCubeArray:
581 case EbtSamplerVideoWEBGL:
582 return false;
583 default:
584 ASSERT(!IsSampler(type));
585 }
586
587 return false;
588 }
589
IsSamplerArray(TBasicType type)590 inline bool IsSamplerArray(TBasicType type)
591 {
592 switch (type)
593 {
594 case EbtSampler1DArray:
595 case EbtISampler1DArray:
596 case EbtUSampler1DArray:
597 case EbtSampler1DArrayShadow:
598 case EbtSampler2DArray:
599 case EbtISampler2DArray:
600 case EbtUSampler2DArray:
601 case EbtSampler2DMSArray:
602 case EbtISampler2DMSArray:
603 case EbtUSampler2DMSArray:
604 case EbtSampler2DArrayShadow:
605 case EbtSamplerCubeArray:
606 case EbtISamplerCubeArray:
607 case EbtUSamplerCubeArray:
608 case EbtSamplerCubeArrayShadow:
609 return true;
610 case EbtSampler2D:
611 case EbtISampler2D:
612 case EbtUSampler2D:
613 case EbtSampler2DRect:
614 case EbtSamplerExternalOES:
615 case EbtSamplerExternal2DY2YEXT:
616 case EbtSampler3D:
617 case EbtISampler3D:
618 case EbtUSampler3D:
619 case EbtISamplerCube:
620 case EbtUSamplerCube:
621 case EbtSamplerCube:
622 case EbtSampler2DShadow:
623 case EbtSamplerCubeShadow:
624 case EbtSampler2DMS:
625 case EbtISampler2DMS:
626 case EbtUSampler2DMS:
627 case EbtSampler1D:
628 case EbtSamplerBuffer:
629 case EbtSampler1DShadow:
630 case EbtSampler2DRectShadow:
631 case EbtISampler1D:
632 case EbtISampler2DRect:
633 case EbtISamplerBuffer:
634 case EbtUSampler1D:
635 case EbtUSampler2DRect:
636 case EbtUSamplerBuffer:
637 case EbtSamplerVideoWEBGL:
638 return false;
639 default:
640 ASSERT(!IsSampler(type));
641 }
642
643 return false;
644 }
645
IsShadowSampler(TBasicType type)646 inline bool IsShadowSampler(TBasicType type)
647 {
648 switch (type)
649 {
650 case EbtSampler1DShadow:
651 case EbtSampler2DShadow:
652 case EbtSamplerCubeShadow:
653 case EbtSampler1DArrayShadow:
654 case EbtSampler2DArrayShadow:
655 case EbtSamplerCubeArrayShadow:
656 case EbtSampler2DRectShadow:
657 return true;
658 case EbtISampler2D:
659 case EbtISampler3D:
660 case EbtISamplerCube:
661 case EbtISampler2DArray:
662 case EbtISampler2DMS:
663 case EbtISampler2DMSArray:
664 case EbtUSampler2D:
665 case EbtUSampler3D:
666 case EbtUSamplerCube:
667 case EbtUSampler2DArray:
668 case EbtUSampler2DMS:
669 case EbtUSampler2DMSArray:
670 case EbtSampler2D:
671 case EbtSampler3D:
672 case EbtSamplerCube:
673 case EbtSamplerExternalOES:
674 case EbtSamplerExternal2DY2YEXT:
675 case EbtSampler2DRect:
676 case EbtSampler2DArray:
677 case EbtSampler2DMS:
678 case EbtSampler2DMSArray:
679 case EbtSampler1D:
680 case EbtSampler1DArray:
681 case EbtSamplerBuffer:
682 case EbtSamplerCubeArray:
683 case EbtISampler1D:
684 case EbtISampler1DArray:
685 case EbtISampler2DRect:
686 case EbtISamplerBuffer:
687 case EbtISamplerCubeArray:
688 case EbtUSampler1D:
689 case EbtUSampler1DArray:
690 case EbtUSampler2DRect:
691 case EbtUSamplerBuffer:
692 case EbtUSamplerCubeArray:
693 case EbtSamplerVideoWEBGL:
694 return false;
695 default:
696 ASSERT(!IsSampler(type));
697 }
698
699 return false;
700 }
701
IsImage2D(TBasicType type)702 inline bool IsImage2D(TBasicType type)
703 {
704 switch (type)
705 {
706 case EbtImage2D:
707 case EbtIImage2D:
708 case EbtUImage2D:
709 case EbtImage2DRect:
710 case EbtIImage2DRect:
711 case EbtUImage2DRect:
712 case EbtImage2DMS:
713 case EbtIImage2DMS:
714 case EbtUImage2DMS:
715 return true;
716 case EbtImage3D:
717 case EbtIImage3D:
718 case EbtUImage3D:
719 case EbtImage2DArray:
720 case EbtIImage2DArray:
721 case EbtUImage2DArray:
722 case EbtImageCube:
723 case EbtIImageCube:
724 case EbtUImageCube:
725 case EbtImage1D:
726 case EbtIImage1D:
727 case EbtUImage1D:
728 case EbtImage1DArray:
729 case EbtIImage1DArray:
730 case EbtUImage1DArray:
731 case EbtImage2DMSArray:
732 case EbtIImage2DMSArray:
733 case EbtUImage2DMSArray:
734 case EbtImageCubeArray:
735 case EbtIImageCubeArray:
736 case EbtUImageCubeArray:
737 case EbtImageRect:
738 case EbtIImageRect:
739 case EbtUImageRect:
740 case EbtImageBuffer:
741 case EbtIImageBuffer:
742 case EbtUImageBuffer:
743 return false;
744 default:
745 ASSERT(!IsImage(type));
746 }
747
748 return false;
749 }
750
IsImage3D(TBasicType type)751 inline bool IsImage3D(TBasicType type)
752 {
753 switch (type)
754 {
755 case EbtImage3D:
756 case EbtIImage3D:
757 case EbtUImage3D:
758 return true;
759 case EbtImage2D:
760 case EbtIImage2D:
761 case EbtUImage2D:
762 case EbtImage2DArray:
763 case EbtIImage2DArray:
764 case EbtUImage2DArray:
765 case EbtImageCube:
766 case EbtIImageCube:
767 case EbtUImageCube:
768 case EbtImage1D:
769 case EbtIImage1D:
770 case EbtUImage1D:
771 case EbtImage1DArray:
772 case EbtIImage1DArray:
773 case EbtUImage1DArray:
774 case EbtImage2DMS:
775 case EbtIImage2DMS:
776 case EbtUImage2DMS:
777 case EbtImage2DMSArray:
778 case EbtIImage2DMSArray:
779 case EbtUImage2DMSArray:
780 case EbtImage2DRect:
781 case EbtIImage2DRect:
782 case EbtUImage2DRect:
783 case EbtImageCubeArray:
784 case EbtIImageCubeArray:
785 case EbtUImageCubeArray:
786 case EbtImageRect:
787 case EbtIImageRect:
788 case EbtUImageRect:
789 case EbtImageBuffer:
790 case EbtIImageBuffer:
791 case EbtUImageBuffer:
792 return false;
793 default:
794 ASSERT(!IsImage(type));
795 }
796
797 return false;
798 }
799
IsImage2DArray(TBasicType type)800 inline bool IsImage2DArray(TBasicType type)
801 {
802 switch (type)
803 {
804 case EbtImage2DArray:
805 case EbtIImage2DArray:
806 case EbtUImage2DArray:
807 case EbtImage2DMSArray:
808 case EbtIImage2DMSArray:
809 case EbtUImage2DMSArray:
810 return true;
811 case EbtImage2D:
812 case EbtIImage2D:
813 case EbtUImage2D:
814 case EbtImage3D:
815 case EbtIImage3D:
816 case EbtUImage3D:
817 case EbtImageCube:
818 case EbtIImageCube:
819 case EbtUImageCube:
820 case EbtImage1D:
821 case EbtIImage1D:
822 case EbtUImage1D:
823 case EbtImage1DArray:
824 case EbtIImage1DArray:
825 case EbtUImage1DArray:
826 case EbtImage2DMS:
827 case EbtIImage2DMS:
828 case EbtUImage2DMS:
829 case EbtImage2DRect:
830 case EbtIImage2DRect:
831 case EbtUImage2DRect:
832 case EbtImageCubeArray:
833 case EbtIImageCubeArray:
834 case EbtUImageCubeArray:
835 case EbtImageRect:
836 case EbtIImageRect:
837 case EbtUImageRect:
838 case EbtImageBuffer:
839 case EbtIImageBuffer:
840 case EbtUImageBuffer:
841 return false;
842 default:
843 ASSERT(!IsImage(type));
844 }
845
846 return false;
847 }
848
IsImageCube(TBasicType type)849 inline bool IsImageCube(TBasicType type)
850 {
851 switch (type)
852 {
853 case EbtImageCube:
854 case EbtIImageCube:
855 case EbtUImageCube:
856 return true;
857 case EbtImage2D:
858 case EbtIImage2D:
859 case EbtUImage2D:
860 case EbtImage3D:
861 case EbtIImage3D:
862 case EbtUImage3D:
863 case EbtImage2DArray:
864 case EbtIImage2DArray:
865 case EbtUImage2DArray:
866 case EbtImage1D:
867 case EbtIImage1D:
868 case EbtUImage1D:
869 case EbtImage1DArray:
870 case EbtIImage1DArray:
871 case EbtUImage1DArray:
872 case EbtImage2DMS:
873 case EbtIImage2DMS:
874 case EbtUImage2DMS:
875 case EbtImage2DMSArray:
876 case EbtIImage2DMSArray:
877 case EbtUImage2DMSArray:
878 case EbtImage2DRect:
879 case EbtIImage2DRect:
880 case EbtUImage2DRect:
881 case EbtImageCubeArray:
882 case EbtIImageCubeArray:
883 case EbtUImageCubeArray:
884 case EbtImageRect:
885 case EbtIImageRect:
886 case EbtUImageRect:
887 case EbtImageBuffer:
888 case EbtIImageBuffer:
889 case EbtUImageBuffer:
890 return false;
891 default:
892 ASSERT(!IsImage(type));
893 }
894
895 return false;
896 }
897
IsInteger(TBasicType type)898 inline bool IsInteger(TBasicType type)
899 {
900 return type == EbtInt || type == EbtUInt;
901 }
902
SupportsPrecision(TBasicType type)903 inline bool SupportsPrecision(TBasicType type)
904 {
905 return type == EbtFloat || type == EbtInt || type == EbtUInt || IsOpaqueType(type);
906 }
907
908 //
909 // Qualifiers and built-ins. These are mainly used to see what can be read
910 // or written, and by the machine dependent translator to know which registers
911 // to allocate variables in. Since built-ins tend to go to different registers
912 // than varying or uniform, it makes sense they are peers, not sub-classes.
913 //
914 enum TQualifier
915 {
916 EvqTemporary, // For temporaries (within a function), read/write
917 EvqGlobal, // For globals read/write
918 EvqConst, // User defined constants and non-output parameters in functions
919 EvqAttribute, // Readonly
920 EvqVaryingIn, // readonly, fragment shaders only
921 EvqVaryingOut, // vertex shaders only read/write
922 EvqUniform, // Readonly, vertex and fragment
923 EvqBuffer, // read/write, vertex, fragment and compute shader
924
925 EvqVertexIn, // Vertex shader input
926 EvqFragmentOut, // Fragment shader output
927 EvqVertexOut, // Vertex shader output
928 EvqFragmentIn, // Fragment shader input
929
930 // parameters
931 EvqIn,
932 EvqOut,
933 EvqInOut,
934 EvqConstReadOnly,
935
936 // built-ins read by vertex shader
937 EvqInstanceID,
938 EvqVertexID,
939
940 // built-ins written by vertex shader
941 EvqPosition,
942 EvqPointSize,
943
944 EvqDrawID, // ANGLE_multi_draw
945
946 EvqBaseVertex, // ANGLE_base_vertex_base_instance
947 EvqBaseInstance, // ANGLE_base_vertex_base_instance
948
949 // built-ins read by fragment shader
950 EvqFragCoord,
951 EvqFrontFacing,
952 EvqPointCoord,
953 EvqHelperInvocation,
954
955 // built-ins written by fragment shader
956 EvqFragColor,
957 EvqFragData,
958
959 EvqFragDepth, // gl_FragDepth for ESSL300.
960 EvqFragDepthEXT, // gl_FragDepthEXT for ESSL100, EXT_frag_depth.
961
962 EvqSecondaryFragColorEXT, // EXT_blend_func_extended
963 EvqSecondaryFragDataEXT, // EXT_blend_func_extended
964
965 EvqViewIDOVR, // OVR_multiview
966 EvqViewportIndex, // gl_ViewportIndex
967
968 // built-ins written by the shader_framebuffer_fetch extension(s)
969 EvqLastFragColor,
970 EvqLastFragData,
971
972 // GLSL ES 3.0 vertex output and fragment input
973 EvqSmooth, // Incomplete qualifier, smooth is the default
974 EvqFlat, // Incomplete qualifier
975 EvqNoPerspective, // Incomplete qualifier
976 EvqCentroid, // Incomplete qualifier
977 EvqSmoothOut,
978 EvqFlatOut,
979 EvqNoPerspectiveOut,
980 EvqCentroidOut, // Implies smooth
981 EvqSmoothIn,
982 EvqFlatIn,
983 EvqNoPerspectiveIn,
984 EvqCentroidIn, // Implies smooth
985
986 // GLSL ES 3.1 compute shader special variables
987 EvqShared,
988 EvqComputeIn,
989 EvqNumWorkGroups,
990 EvqWorkGroupSize,
991 EvqWorkGroupID,
992 EvqLocalInvocationID,
993 EvqGlobalInvocationID,
994 EvqLocalInvocationIndex,
995
996 // GLSL ES 3.1 memory qualifiers
997 EvqReadOnly,
998 EvqWriteOnly,
999 EvqCoherent,
1000 EvqRestrict,
1001 EvqVolatile,
1002
1003 // GLSL ES 3.1 extension EXT_geometry_shader qualifiers
1004 EvqGeometryIn,
1005 EvqGeometryOut,
1006 EvqPerVertexIn, // gl_in
1007 EvqPrimitiveIDIn, // gl_PrimitiveIDIn
1008 EvqInvocationID, // gl_InvocationID
1009 EvqPrimitiveID, // gl_PrimitiveID
1010 EvqLayer, // gl_Layer
1011
1012 // GLSL ES 3.1 extension EXT_gpu_shader5 qualifiers
1013 EvqPrecise,
1014
1015 // end of list
1016 EvqLast
1017 };
1018
IsQualifierUnspecified(TQualifier qualifier)1019 inline bool IsQualifierUnspecified(TQualifier qualifier)
1020 {
1021 return (qualifier == EvqTemporary || qualifier == EvqGlobal);
1022 }
1023
IsStorageBuffer(TQualifier qualifier)1024 inline bool IsStorageBuffer(TQualifier qualifier)
1025 {
1026 return qualifier == EvqBuffer;
1027 }
1028
IsShaderIn(TQualifier qualifier)1029 inline bool IsShaderIn(TQualifier qualifier)
1030 {
1031 switch (qualifier)
1032 {
1033 case EvqVertexIn:
1034 case EvqGeometryIn:
1035 case EvqFragmentIn:
1036 case EvqAttribute:
1037 case EvqVaryingIn:
1038 case EvqSmoothIn:
1039 case EvqFlatIn:
1040 case EvqNoPerspectiveIn:
1041 case EvqCentroidIn:
1042 return true;
1043 default:
1044 return false;
1045 }
1046 }
1047
IsShaderOut(TQualifier qualifier)1048 inline bool IsShaderOut(TQualifier qualifier)
1049 {
1050 switch (qualifier)
1051 {
1052 case EvqVertexOut:
1053 case EvqGeometryOut:
1054 case EvqFragmentOut:
1055 case EvqVaryingOut:
1056 case EvqSmoothOut:
1057 case EvqFlatOut:
1058 case EvqNoPerspectiveOut:
1059 case EvqCentroidOut:
1060 return true;
1061 default:
1062 return false;
1063 }
1064 }
1065
1066 enum TLayoutImageInternalFormat
1067 {
1068 EiifUnspecified,
1069 EiifRGBA32F,
1070 EiifRGBA16F,
1071 EiifR32F,
1072 EiifRGBA32UI,
1073 EiifRGBA16UI,
1074 EiifRGBA8UI,
1075 EiifR32UI,
1076 EiifRGBA32I,
1077 EiifRGBA16I,
1078 EiifRGBA8I,
1079 EiifR32I,
1080 EiifRGBA8,
1081 EiifRGBA8_SNORM
1082 };
1083
1084 enum TLayoutMatrixPacking
1085 {
1086 EmpUnspecified,
1087 EmpRowMajor,
1088 EmpColumnMajor
1089 };
1090
1091 enum TLayoutBlockStorage
1092 {
1093 EbsUnspecified,
1094 EbsShared,
1095 EbsPacked,
1096 EbsStd140,
1097 EbsStd430
1098 };
1099
1100 enum TYuvCscStandardEXT
1101 {
1102 EycsUndefined,
1103 EycsItu601,
1104 EycsItu601FullRange,
1105 EycsItu709
1106 };
1107
1108 enum TLayoutPrimitiveType
1109 {
1110 EptUndefined,
1111 EptPoints,
1112 EptLines,
1113 EptLinesAdjacency,
1114 EptTriangles,
1115 EptTrianglesAdjacency,
1116 EptLineStrip,
1117 EptTriangleStrip
1118 };
1119
1120 struct TLayoutQualifier
1121 {
1122 // Must have a trivial default constructor since it is used in YYSTYPE.
1123 TLayoutQualifier() = default;
1124
CreateTLayoutQualifier1125 constexpr static TLayoutQualifier Create() { return TLayoutQualifier(0); }
1126
isEmptyTLayoutQualifier1127 bool isEmpty() const
1128 {
1129 return location == -1 && binding == -1 && offset == -1 && numViews == -1 && yuv == false &&
1130 earlyFragmentTests == false && matrixPacking == EmpUnspecified &&
1131 blockStorage == EbsUnspecified && !localSize.isAnyValueSet() &&
1132 imageInternalFormat == EiifUnspecified && primitiveType == EptUndefined &&
1133 invocations == 0 && maxVertices == -1 && index == -1;
1134 }
1135
isCombinationValidTLayoutQualifier1136 bool isCombinationValid() const
1137 {
1138 bool workGroupSizeSpecified = localSize.isAnyValueSet();
1139 bool numViewsSet = (numViews != -1);
1140 bool geometryShaderSpecified =
1141 (primitiveType != EptUndefined) || (invocations != 0) || (maxVertices != -1);
1142 bool otherLayoutQualifiersSpecified =
1143 (location != -1 || binding != -1 || index != -1 || matrixPacking != EmpUnspecified ||
1144 blockStorage != EbsUnspecified || imageInternalFormat != EiifUnspecified);
1145
1146 // we can have either the work group size specified, or number of views,
1147 // or yuv layout qualifier, or early_fragment_tests layout qualifier, or the other layout
1148 // qualifiers.
1149 return (workGroupSizeSpecified ? 1 : 0) + (numViewsSet ? 1 : 0) + (yuv ? 1 : 0) +
1150 (earlyFragmentTests ? 1 : 0) + (otherLayoutQualifiersSpecified ? 1 : 0) +
1151 (geometryShaderSpecified ? 1 : 0) <=
1152 1;
1153 }
1154
isLocalSizeEqualTLayoutQualifier1155 bool isLocalSizeEqual(const WorkGroupSize &localSizeIn) const
1156 {
1157 return localSize.isWorkGroupSizeMatching(localSizeIn);
1158 }
1159
1160 int location;
1161 unsigned int locationsSpecified;
1162 TLayoutMatrixPacking matrixPacking;
1163 TLayoutBlockStorage blockStorage;
1164
1165 // Compute shader layout qualifiers.
1166 WorkGroupSize localSize;
1167
1168 int binding;
1169 int offset;
1170
1171 // Image format layout qualifier
1172 TLayoutImageInternalFormat imageInternalFormat;
1173
1174 // OVR_multiview num_views.
1175 int numViews;
1176
1177 // EXT_YUV_target yuv layout qualifier.
1178 bool yuv;
1179
1180 // early_fragment_tests qualifier.
1181 bool earlyFragmentTests;
1182
1183 // OES_geometry_shader layout qualifiers.
1184 TLayoutPrimitiveType primitiveType;
1185 int invocations;
1186 int maxVertices;
1187
1188 // EXT_blend_func_extended fragment output layout qualifier
1189 int index;
1190
1191 private:
TLayoutQualifierTLayoutQualifier1192 explicit constexpr TLayoutQualifier(int /*placeholder*/)
1193 : location(-1),
1194 locationsSpecified(0),
1195 matrixPacking(EmpUnspecified),
1196 blockStorage(EbsUnspecified),
1197 localSize(-1),
1198 binding(-1),
1199 offset(-1),
1200 imageInternalFormat(EiifUnspecified),
1201 numViews(-1),
1202 yuv(false),
1203 earlyFragmentTests(false),
1204 primitiveType(EptUndefined),
1205 invocations(0),
1206 maxVertices(-1),
1207 index(-1)
1208 {}
1209 };
1210
1211 struct TMemoryQualifier
1212 {
1213 // Must have a trivial default constructor since it is used in YYSTYPE.
1214 TMemoryQualifier() = default;
1215
isEmptyTMemoryQualifier1216 bool isEmpty() const
1217 {
1218 return !readonly && !writeonly && !coherent && !restrictQualifier && !volatileQualifier;
1219 }
1220
CreateTMemoryQualifier1221 constexpr static TMemoryQualifier Create() { return TMemoryQualifier(0); }
1222
1223 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1224 // An image can be qualified as both readonly and writeonly. It still can be can be used with
1225 // imageSize().
1226 bool readonly;
1227 bool writeonly;
1228 bool coherent;
1229
1230 // restrict and volatile are reserved keywords in C/C++
1231 bool restrictQualifier;
1232 bool volatileQualifier;
1233
1234 private:
TMemoryQualifierTMemoryQualifier1235 explicit constexpr TMemoryQualifier(int /*placeholder*/)
1236 : readonly(false),
1237 writeonly(false),
1238 coherent(false),
1239 restrictQualifier(false),
1240 volatileQualifier(false)
1241 {}
1242 };
1243
getWorkGroupSizeString(size_t dimension)1244 inline const char *getWorkGroupSizeString(size_t dimension)
1245 {
1246 switch (dimension)
1247 {
1248 case 0u:
1249 return "local_size_x";
1250 case 1u:
1251 return "local_size_y";
1252 case 2u:
1253 return "local_size_z";
1254 default:
1255 UNREACHABLE();
1256 return "dimension out of bounds";
1257 }
1258 }
1259
1260 //
1261 // This is just for debug and error message print out, carried along with the definitions above.
1262 //
getQualifierString(TQualifier q)1263 inline const char *getQualifierString(TQualifier q)
1264 {
1265 // clang-format off
1266 switch(q)
1267 {
1268 case EvqTemporary: return "Temporary";
1269 case EvqGlobal: return "Global";
1270 case EvqConst: return "const";
1271 case EvqAttribute: return "attribute";
1272 case EvqVaryingIn: return "varying";
1273 case EvqVaryingOut: return "varying";
1274 case EvqUniform: return "uniform";
1275 case EvqBuffer: return "buffer";
1276 case EvqVertexIn: return "in";
1277 case EvqFragmentOut: return "out";
1278 case EvqVertexOut: return "out";
1279 case EvqFragmentIn: return "in";
1280 case EvqIn: return "in";
1281 case EvqOut: return "out";
1282 case EvqInOut: return "inout";
1283 case EvqConstReadOnly: return "const";
1284 case EvqInstanceID: return "InstanceID";
1285 case EvqVertexID: return "VertexID";
1286 case EvqPosition: return "Position";
1287 case EvqPointSize: return "PointSize";
1288 case EvqDrawID: return "DrawID";
1289 case EvqBaseVertex: return "BaseVertex";
1290 case EvqBaseInstance: return "BaseInstance";
1291 case EvqFragCoord: return "FragCoord";
1292 case EvqFrontFacing: return "FrontFacing";
1293 case EvqHelperInvocation: return "HelperInvocation";
1294 case EvqPointCoord: return "PointCoord";
1295 case EvqFragColor: return "FragColor";
1296 case EvqFragData: return "FragData";
1297 case EvqFragDepthEXT: return "FragDepth";
1298 case EvqFragDepth: return "FragDepth";
1299 case EvqSecondaryFragColorEXT: return "SecondaryFragColorEXT";
1300 case EvqSecondaryFragDataEXT: return "SecondaryFragDataEXT";
1301 case EvqViewIDOVR: return "ViewIDOVR";
1302 case EvqViewportIndex: return "ViewportIndex";
1303 case EvqLayer: return "Layer";
1304 case EvqLastFragColor: return "LastFragColor";
1305 case EvqLastFragData: return "LastFragData";
1306 case EvqSmoothOut: return "smooth out";
1307 case EvqCentroidOut: return "smooth centroid out";
1308 case EvqFlatOut: return "flat out";
1309 case EvqNoPerspectiveOut: return "noperspective out";
1310 case EvqSmoothIn: return "smooth in";
1311 case EvqFlatIn: return "flat in";
1312 case EvqNoPerspectiveIn: return "noperspective in";
1313 case EvqCentroidIn: return "smooth centroid in";
1314 case EvqCentroid: return "centroid";
1315 case EvqFlat: return "flat";
1316 case EvqNoPerspective: return "noperspective";
1317 case EvqSmooth: return "smooth";
1318 case EvqShared: return "shared";
1319 case EvqComputeIn: return "in";
1320 case EvqNumWorkGroups: return "NumWorkGroups";
1321 case EvqWorkGroupSize: return "WorkGroupSize";
1322 case EvqWorkGroupID: return "WorkGroupID";
1323 case EvqLocalInvocationID: return "LocalInvocationID";
1324 case EvqGlobalInvocationID: return "GlobalInvocationID";
1325 case EvqLocalInvocationIndex: return "LocalInvocationIndex";
1326 case EvqReadOnly: return "readonly";
1327 case EvqWriteOnly: return "writeonly";
1328 case EvqGeometryIn: return "in";
1329 case EvqGeometryOut: return "out";
1330 case EvqPerVertexIn: return "gl_in";
1331 case EvqPrecise: return "precise";
1332 default: UNREACHABLE(); return "unknown qualifier";
1333 }
1334 // clang-format on
1335 }
1336
getMatrixPackingString(TLayoutMatrixPacking mpq)1337 inline const char *getMatrixPackingString(TLayoutMatrixPacking mpq)
1338 {
1339 switch (mpq)
1340 {
1341 case EmpUnspecified:
1342 return "mp_unspecified";
1343 case EmpRowMajor:
1344 return "row_major";
1345 case EmpColumnMajor:
1346 return "column_major";
1347 default:
1348 UNREACHABLE();
1349 return "unknown matrix packing";
1350 }
1351 }
1352
getBlockStorageString(TLayoutBlockStorage bsq)1353 inline const char *getBlockStorageString(TLayoutBlockStorage bsq)
1354 {
1355 switch (bsq)
1356 {
1357 case EbsUnspecified:
1358 return "bs_unspecified";
1359 case EbsShared:
1360 return "shared";
1361 case EbsPacked:
1362 return "packed";
1363 case EbsStd140:
1364 return "std140";
1365 case EbsStd430:
1366 return "std430";
1367 default:
1368 UNREACHABLE();
1369 return "unknown block storage";
1370 }
1371 }
1372
getImageInternalFormatString(TLayoutImageInternalFormat iifq)1373 inline const char *getImageInternalFormatString(TLayoutImageInternalFormat iifq)
1374 {
1375 switch (iifq)
1376 {
1377 case EiifRGBA32F:
1378 return "rgba32f";
1379 case EiifRGBA16F:
1380 return "rgba16f";
1381 case EiifR32F:
1382 return "r32f";
1383 case EiifRGBA32UI:
1384 return "rgba32ui";
1385 case EiifRGBA16UI:
1386 return "rgba16ui";
1387 case EiifRGBA8UI:
1388 return "rgba8ui";
1389 case EiifR32UI:
1390 return "r32ui";
1391 case EiifRGBA32I:
1392 return "rgba32i";
1393 case EiifRGBA16I:
1394 return "rgba16i";
1395 case EiifRGBA8I:
1396 return "rgba8i";
1397 case EiifR32I:
1398 return "r32i";
1399 case EiifRGBA8:
1400 return "rgba8";
1401 case EiifRGBA8_SNORM:
1402 return "rgba8_snorm";
1403 default:
1404 UNREACHABLE();
1405 return "unknown internal image format";
1406 }
1407 }
1408
getYuvCscStandardEXT(const ImmutableString & str)1409 inline TYuvCscStandardEXT getYuvCscStandardEXT(const ImmutableString &str)
1410 {
1411 if (str == "itu_601")
1412 return EycsItu601;
1413 else if (str == "itu_601_full_range")
1414 return EycsItu601FullRange;
1415 else if (str == "itu_709")
1416 return EycsItu709;
1417 return EycsUndefined;
1418 }
1419
getYuvCscStandardEXTString(TYuvCscStandardEXT ycsq)1420 inline const char *getYuvCscStandardEXTString(TYuvCscStandardEXT ycsq)
1421 {
1422 switch (ycsq)
1423 {
1424 case EycsItu601:
1425 return "itu_601";
1426 case EycsItu601FullRange:
1427 return "itu_601_full_range";
1428 case EycsItu709:
1429 return "itu_709";
1430 default:
1431 UNREACHABLE();
1432 return "unknown color space conversion standard";
1433 }
1434 }
1435
getGeometryShaderPrimitiveTypeString(TLayoutPrimitiveType primitiveType)1436 inline const char *getGeometryShaderPrimitiveTypeString(TLayoutPrimitiveType primitiveType)
1437 {
1438 switch (primitiveType)
1439 {
1440 case EptPoints:
1441 return "points";
1442 case EptLines:
1443 return "lines";
1444 case EptTriangles:
1445 return "triangles";
1446 case EptLinesAdjacency:
1447 return "lines_adjacency";
1448 case EptTrianglesAdjacency:
1449 return "triangles_adjacency";
1450 case EptLineStrip:
1451 return "line_strip";
1452 case EptTriangleStrip:
1453 return "triangle_strip";
1454 default:
1455 UNREACHABLE();
1456 return "unknown geometry shader primitive type";
1457 }
1458 }
1459
1460 } // namespace sh
1461
1462 #endif // COMPILER_TRANSLATOR_BASETYPES_H_
1463