glsl/types: Add a real is_integer helper
[mesa.git] / src / compiler / glsl_types.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef GLSL_TYPES_H
26 #define GLSL_TYPES_H
27
28 #include <string.h>
29 #include <assert.h>
30
31 #include "shader_enums.h"
32 #include "blob.h"
33 #include "c11/threads.h"
34 #include "util/macros.h"
35
36 #ifdef __cplusplus
37 #include "main/config.h"
38 #endif
39
40 struct glsl_type;
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 struct _mesa_glsl_parse_state;
47 struct glsl_symbol_table;
48
49 extern void
50 glsl_type_singleton_init_or_ref();
51
52 extern void
53 glsl_type_singleton_decref();
54
55 extern void
56 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
57
58 void encode_type_to_blob(struct blob *blob, const struct glsl_type *type);
59
60 const struct glsl_type *decode_type_from_blob(struct blob_reader *blob);
61
62 #ifdef __cplusplus
63 }
64 #endif
65
66 enum glsl_base_type {
67 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
68 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
69 */
70 GLSL_TYPE_UINT = 0,
71 GLSL_TYPE_INT,
72 GLSL_TYPE_FLOAT,
73 GLSL_TYPE_FLOAT16,
74 GLSL_TYPE_DOUBLE,
75 GLSL_TYPE_UINT8,
76 GLSL_TYPE_INT8,
77 GLSL_TYPE_UINT16,
78 GLSL_TYPE_INT16,
79 GLSL_TYPE_UINT64,
80 GLSL_TYPE_INT64,
81 GLSL_TYPE_BOOL,
82 GLSL_TYPE_SAMPLER,
83 GLSL_TYPE_IMAGE,
84 GLSL_TYPE_ATOMIC_UINT,
85 GLSL_TYPE_STRUCT,
86 GLSL_TYPE_INTERFACE,
87 GLSL_TYPE_ARRAY,
88 GLSL_TYPE_VOID,
89 GLSL_TYPE_SUBROUTINE,
90 GLSL_TYPE_FUNCTION,
91 GLSL_TYPE_ERROR
92 };
93
94 static inline bool glsl_base_type_is_16bit(enum glsl_base_type type)
95 {
96 return type == GLSL_TYPE_FLOAT16 ||
97 type == GLSL_TYPE_UINT16 ||
98 type == GLSL_TYPE_INT16;
99 }
100
101 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type)
102 {
103 return type == GLSL_TYPE_DOUBLE ||
104 type == GLSL_TYPE_UINT64 ||
105 type == GLSL_TYPE_INT64 ||
106 type == GLSL_TYPE_IMAGE ||
107 type == GLSL_TYPE_SAMPLER;
108 }
109
110 static inline bool glsl_base_type_is_integer(enum glsl_base_type type)
111 {
112 return type == GLSL_TYPE_UINT8 ||
113 type == GLSL_TYPE_INT8 ||
114 type == GLSL_TYPE_UINT16 ||
115 type == GLSL_TYPE_INT16 ||
116 type == GLSL_TYPE_UINT ||
117 type == GLSL_TYPE_INT ||
118 type == GLSL_TYPE_UINT64 ||
119 type == GLSL_TYPE_INT64 ||
120 type == GLSL_TYPE_BOOL ||
121 type == GLSL_TYPE_SAMPLER ||
122 type == GLSL_TYPE_IMAGE;
123 }
124
125 static inline unsigned int
126 glsl_base_type_get_bit_size(const enum glsl_base_type base_type)
127 {
128 switch (base_type) {
129 case GLSL_TYPE_BOOL:
130 return 1;
131
132 case GLSL_TYPE_INT:
133 case GLSL_TYPE_UINT:
134 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
135 case GLSL_TYPE_SUBROUTINE:
136 return 32;
137
138 case GLSL_TYPE_FLOAT16:
139 case GLSL_TYPE_UINT16:
140 case GLSL_TYPE_INT16:
141 return 16;
142
143 case GLSL_TYPE_UINT8:
144 case GLSL_TYPE_INT8:
145 return 8;
146
147 case GLSL_TYPE_DOUBLE:
148 case GLSL_TYPE_INT64:
149 case GLSL_TYPE_UINT64:
150 case GLSL_TYPE_IMAGE:
151 case GLSL_TYPE_SAMPLER:
152 return 64;
153
154 default:
155 unreachable("unknown base type");
156 }
157
158 return 0;
159 }
160
161 enum glsl_sampler_dim {
162 GLSL_SAMPLER_DIM_1D = 0,
163 GLSL_SAMPLER_DIM_2D,
164 GLSL_SAMPLER_DIM_3D,
165 GLSL_SAMPLER_DIM_CUBE,
166 GLSL_SAMPLER_DIM_RECT,
167 GLSL_SAMPLER_DIM_BUF,
168 GLSL_SAMPLER_DIM_EXTERNAL,
169 GLSL_SAMPLER_DIM_MS,
170 GLSL_SAMPLER_DIM_SUBPASS, /* for vulkan input attachments */
171 GLSL_SAMPLER_DIM_SUBPASS_MS, /* for multisampled vulkan input attachments */
172 };
173
174 enum glsl_matrix_layout {
175 /**
176 * The layout of the matrix is inherited from the object containing the
177 * matrix (the top level structure or the uniform block).
178 */
179 GLSL_MATRIX_LAYOUT_INHERITED,
180
181 /**
182 * Explicit column-major layout
183 *
184 * If a uniform block doesn't have an explicit layout set, it will default
185 * to this layout.
186 */
187 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
188
189 /**
190 * Row-major layout
191 */
192 GLSL_MATRIX_LAYOUT_ROW_MAJOR
193 };
194
195 enum {
196 GLSL_PRECISION_NONE = 0,
197 GLSL_PRECISION_HIGH,
198 GLSL_PRECISION_MEDIUM,
199 GLSL_PRECISION_LOW
200 };
201
202 #ifdef __cplusplus
203 #include "GL/gl.h"
204 #include "util/ralloc.h"
205 #include "main/menums.h" /* for gl_texture_index, C++'s enum rules are broken */
206
207 struct glsl_type {
208 GLenum gl_type;
209 glsl_base_type base_type:8;
210
211 glsl_base_type sampled_type:8; /**< Type of data returned using this
212 * sampler or image. Only \c
213 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
214 * and \c GLSL_TYPE_UINT are valid.
215 */
216
217 unsigned sampler_dimensionality:4; /**< \see glsl_sampler_dim */
218 unsigned sampler_shadow:1;
219 unsigned sampler_array:1;
220 unsigned interface_packing:2;
221 unsigned interface_row_major:1;
222
223 /**
224 * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
225 *
226 * Only used for Compute kernels
227 */
228 unsigned packed:1;
229
230 private:
231 glsl_type() : mem_ctx(NULL)
232 {
233 // Dummy constructor, just for the sake of ASSERT_BITFIELD_SIZE.
234 }
235
236 public:
237 /**
238 * \name Vector and matrix element counts
239 *
240 * For scalars, each of these values will be 1. For non-numeric types
241 * these will be 0.
242 */
243 /*@{*/
244 uint8_t vector_elements; /**< 1, 2, 3, or 4 vector elements. */
245 uint8_t matrix_columns; /**< 1, 2, 3, or 4 matrix columns. */
246 /*@}*/
247
248 /**
249 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
250 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
251 * elements in the structure and the number of values pointed to by
252 * \c fields.structure (below).
253 */
254 unsigned length;
255
256 /**
257 * Name of the data type
258 *
259 * Will never be \c NULL.
260 */
261 const char *name;
262
263 /**
264 * Explicit array, matrix, or vector stride. This is used to communicate
265 * explicit array layouts from SPIR-V. Should be 0 if the type has no
266 * explicit stride.
267 */
268 unsigned explicit_stride;
269
270 /**
271 * Subtype of composite data types.
272 */
273 union {
274 const struct glsl_type *array; /**< Type of array elements. */
275 struct glsl_function_param *parameters; /**< Parameters to function. */
276 struct glsl_struct_field *structure; /**< List of struct fields. */
277 } fields;
278
279 /**
280 * \name Pointers to various public type singletons
281 */
282 /*@{*/
283 #undef DECL_TYPE
284 #define DECL_TYPE(NAME, ...) \
285 static const glsl_type *const NAME##_type;
286 #undef STRUCT_TYPE
287 #define STRUCT_TYPE(NAME) \
288 static const glsl_type *const struct_##NAME##_type;
289 #include "compiler/builtin_type_macros.h"
290 /*@}*/
291
292 /**
293 * Convenience accessors for vector types (shorter than get_instance()).
294 * @{
295 */
296 static const glsl_type *vec(unsigned components, const glsl_type *const ts[]);
297 static const glsl_type *vec(unsigned components);
298 static const glsl_type *f16vec(unsigned components);
299 static const glsl_type *dvec(unsigned components);
300 static const glsl_type *ivec(unsigned components);
301 static const glsl_type *uvec(unsigned components);
302 static const glsl_type *bvec(unsigned components);
303 static const glsl_type *i64vec(unsigned components);
304 static const glsl_type *u64vec(unsigned components);
305 static const glsl_type *i16vec(unsigned components);
306 static const glsl_type *u16vec(unsigned components);
307 static const glsl_type *i8vec(unsigned components);
308 static const glsl_type *u8vec(unsigned components);
309 /**@}*/
310
311 /**
312 * For numeric and boolean derived types returns the basic scalar type
313 *
314 * If the type is a numeric or boolean scalar, vector, or matrix type,
315 * this function gets the scalar type of the individual components. For
316 * all other types, including arrays of numeric or boolean types, the
317 * error type is returned.
318 */
319 const glsl_type *get_base_type() const;
320
321 /**
322 * Get the basic scalar type which this type aggregates.
323 *
324 * If the type is a numeric or boolean scalar, vector, or matrix, or an
325 * array of any of those, this function gets the scalar type of the
326 * individual components. For structs and arrays of structs, this function
327 * returns the struct type. For samplers and arrays of samplers, this
328 * function returns the sampler type.
329 */
330 const glsl_type *get_scalar_type() const;
331
332 /**
333 * Gets the "bare" type without any decorations or layout information.
334 */
335 const glsl_type *get_bare_type() const;
336
337 /**
338 * Get the instance of a built-in scalar, vector, or matrix type
339 */
340 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
341 unsigned columns,
342 unsigned explicit_stride = 0,
343 bool row_major = false);
344
345 /**
346 * Get the instance of a sampler type
347 */
348 static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
349 bool shadow,
350 bool array,
351 glsl_base_type type);
352
353 static const glsl_type *get_image_instance(enum glsl_sampler_dim dim,
354 bool array, glsl_base_type type);
355
356 /**
357 * Get the instance of an array type
358 */
359 static const glsl_type *get_array_instance(const glsl_type *base,
360 unsigned elements,
361 unsigned explicit_stride = 0);
362
363 /**
364 * Get the instance of a record type
365 */
366 static const glsl_type *get_struct_instance(const glsl_struct_field *fields,
367 unsigned num_fields,
368 const char *name,
369 bool packed = false);
370
371 /**
372 * Get the instance of an interface block type
373 */
374 static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
375 unsigned num_fields,
376 enum glsl_interface_packing packing,
377 bool row_major,
378 const char *block_name);
379
380 /**
381 * Get the instance of an subroutine type
382 */
383 static const glsl_type *get_subroutine_instance(const char *subroutine_name);
384
385 /**
386 * Get the instance of a function type
387 */
388 static const glsl_type *get_function_instance(const struct glsl_type *return_type,
389 const glsl_function_param *parameters,
390 unsigned num_params);
391
392 /**
393 * Get the type resulting from a multiplication of \p type_a * \p type_b
394 */
395 static const glsl_type *get_mul_type(const glsl_type *type_a,
396 const glsl_type *type_b);
397
398 /**
399 * Query the total number of scalars that make up a scalar, vector or matrix
400 */
401 unsigned components() const
402 {
403 return vector_elements * matrix_columns;
404 }
405
406 /**
407 * Calculate the number of components slots required to hold this type
408 *
409 * This is used to determine how many uniform or varying locations a type
410 * might occupy.
411 */
412 unsigned component_slots() const;
413
414 /**
415 * Calculate offset between the base location of the struct in
416 * uniform storage and a struct member.
417 * For the initial call, length is the index of the member to find the
418 * offset for.
419 */
420 unsigned struct_location_offset(unsigned length) const;
421
422 /**
423 * Calculate the number of unique values from glGetUniformLocation for the
424 * elements of the type.
425 *
426 * This is used to allocate slots in the UniformRemapTable, the amount of
427 * locations may not match with actual used storage space by the driver.
428 */
429 unsigned uniform_locations() const;
430
431 /**
432 * Used to count the number of varyings contained in the type ignoring
433 * innermost array elements.
434 */
435 unsigned varying_count() const;
436
437 /**
438 * Calculate the number of attribute slots required to hold this type
439 *
440 * This implements the language rules of GLSL 1.50 for counting the number
441 * of slots used by a vertex attribute. It also determines the number of
442 * varying slots the type will use up in the absence of varying packing
443 * (and thus, it can be used to measure the number of varying slots used by
444 * the varyings that are generated by lower_packed_varyings).
445 *
446 * For vertex shader attributes - doubles only take one slot.
447 * For inter-shader varyings - dvec3/dvec4 take two slots.
448 *
449 * Vulkan doesn’t make this distinction so the argument should always be
450 * false.
451 */
452 unsigned count_attribute_slots(bool is_gl_vertex_input) const;
453
454 /**
455 * Alignment in bytes of the start of this type in a std140 uniform
456 * block.
457 */
458 unsigned std140_base_alignment(bool row_major) const;
459
460 /** Size in bytes of this type in a std140 uniform block.
461 *
462 * Note that this is not GL_UNIFORM_SIZE (which is the number of
463 * elements in the array)
464 */
465 unsigned std140_size(bool row_major) const;
466
467 /**
468 * Gets an explicitly laid out type with the std140 layout.
469 */
470 const glsl_type *get_explicit_std140_type(bool row_major) const;
471
472 /**
473 * Alignment in bytes of the start of this type in a std430 shader
474 * storage block.
475 */
476 unsigned std430_base_alignment(bool row_major) const;
477
478 /**
479 * Calculate array stride in bytes of this type in a std430 shader storage
480 * block.
481 */
482 unsigned std430_array_stride(bool row_major) const;
483
484 /**
485 * Size in bytes of this type in a std430 shader storage block.
486 *
487 * Note that this is not GL_BUFFER_SIZE
488 */
489 unsigned std430_size(bool row_major) const;
490
491 /**
492 * Gets an explicitly laid out type with the std430 layout.
493 */
494 const glsl_type *get_explicit_std430_type(bool row_major) const;
495
496 /**
497 * Gets an explicitly laid out interface type.
498 */
499 const glsl_type *get_explicit_interface_type(bool supports_std430) const;
500
501 /**
502 * Alignment in bytes of the start of this type in OpenCL memory.
503 */
504 unsigned cl_alignment() const;
505
506 /**
507 * Size in bytes of this type in OpenCL memory
508 */
509 unsigned cl_size() const;
510
511 /**
512 * \brief Can this type be implicitly converted to another?
513 *
514 * \return True if the types are identical or if this type can be converted
515 * to \c desired according to Section 4.1.10 of the GLSL spec.
516 *
517 * \verbatim
518 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
519 * Implicit Conversions:
520 *
521 * In some situations, an expression and its type will be implicitly
522 * converted to a different type. The following table shows all allowed
523 * implicit conversions:
524 *
525 * Type of expression | Can be implicitly converted to
526 * --------------------------------------------------
527 * int float
528 * uint
529 *
530 * ivec2 vec2
531 * uvec2
532 *
533 * ivec3 vec3
534 * uvec3
535 *
536 * ivec4 vec4
537 * uvec4
538 *
539 * There are no implicit array or structure conversions. For example,
540 * an array of int cannot be implicitly converted to an array of float.
541 * There are no implicit conversions between signed and unsigned
542 * integers.
543 * \endverbatim
544 */
545 bool can_implicitly_convert_to(const glsl_type *desired,
546 _mesa_glsl_parse_state *state) const;
547
548 /**
549 * Query whether or not a type is a scalar (non-vector and non-matrix).
550 */
551 bool is_scalar() const
552 {
553 return (vector_elements == 1)
554 && (base_type >= GLSL_TYPE_UINT)
555 && (base_type <= GLSL_TYPE_IMAGE);
556 }
557
558 /**
559 * Query whether or not a type is a vector
560 */
561 bool is_vector() const
562 {
563 return (vector_elements > 1)
564 && (matrix_columns == 1)
565 && (base_type >= GLSL_TYPE_UINT)
566 && (base_type <= GLSL_TYPE_BOOL);
567 }
568
569 /**
570 * Query whether or not a type is a matrix
571 */
572 bool is_matrix() const
573 {
574 /* GLSL only has float matrices. */
575 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT ||
576 base_type == GLSL_TYPE_DOUBLE ||
577 base_type == GLSL_TYPE_FLOAT16);
578 }
579
580 /**
581 * Query whether or not a type is a non-array numeric type
582 */
583 bool is_numeric() const
584 {
585 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_INT64);
586 }
587
588 /**
589 * Query whether or not a type is an integer.
590 */
591 bool is_integer() const
592 {
593 return glsl_base_type_is_integer(base_type);
594 }
595
596 /**
597 * Query whether or not a type is an 32-bit integer.
598 */
599 bool is_integer_32() const
600 {
601 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
602 }
603
604 /**
605 * Query whether or not a type is a 64-bit integer.
606 */
607 bool is_integer_64() const
608 {
609 return base_type == GLSL_TYPE_UINT64 || base_type == GLSL_TYPE_INT64;
610 }
611
612 /**
613 * Query whether or not a type is a 32-bit or 64-bit integer
614 */
615 bool is_integer_32_64() const
616 {
617 return is_integer_32() || is_integer_64();
618 }
619
620 /**
621 * Query whether or not type is an integral type, or for struct and array
622 * types, contains an integral type.
623 */
624 bool contains_integer() const;
625
626 /**
627 * Query whether or not type is a double type, or for struct, interface and
628 * array types, contains a double type.
629 */
630 bool contains_double() const;
631
632 /**
633 * Query whether or not type is a 64-bit type, or for struct, interface and
634 * array types, contains a double type.
635 */
636 bool contains_64bit() const;
637
638 /**
639 * Query whether or not a type is a float type
640 */
641 bool is_float() const
642 {
643 return base_type == GLSL_TYPE_FLOAT;
644 }
645
646 /**
647 * Query whether or not a type is a double type
648 */
649 bool is_double() const
650 {
651 return base_type == GLSL_TYPE_DOUBLE;
652 }
653
654 /**
655 * Query whether a 64-bit type takes two slots.
656 */
657 bool is_dual_slot() const
658 {
659 return is_64bit() && vector_elements > 2;
660 }
661
662 /**
663 * Query whether or not a type is 64-bit
664 */
665 bool is_64bit() const
666 {
667 return glsl_base_type_is_64bit(base_type);
668 }
669
670 /**
671 * Query whether or not a type is 16-bit
672 */
673 bool is_16bit() const
674 {
675 return glsl_base_type_is_16bit(base_type);
676 }
677
678 /**
679 * Query whether or not a type is 32-bit
680 */
681 bool is_32bit() const
682 {
683 return base_type == GLSL_TYPE_UINT ||
684 base_type == GLSL_TYPE_INT ||
685 base_type == GLSL_TYPE_FLOAT;
686 }
687
688 /**
689 * Query whether or not a type is a non-array boolean type
690 */
691 bool is_boolean() const
692 {
693 return base_type == GLSL_TYPE_BOOL;
694 }
695
696 /**
697 * Query whether or not a type is a sampler
698 */
699 bool is_sampler() const
700 {
701 return base_type == GLSL_TYPE_SAMPLER;
702 }
703
704 /**
705 * Query whether or not type is a sampler, or for struct, interface and
706 * array types, contains a sampler.
707 */
708 bool contains_sampler() const;
709
710 /**
711 * Query whether or not type is an array or for struct, interface and
712 * array types, contains an array.
713 */
714 bool contains_array() const;
715
716 /**
717 * Get the Mesa texture target index for a sampler type.
718 */
719 gl_texture_index sampler_index() const;
720
721 /**
722 * Query whether or not type is an image, or for struct, interface and
723 * array types, contains an image.
724 */
725 bool contains_image() const;
726
727 /**
728 * Query whether or not a type is an image
729 */
730 bool is_image() const
731 {
732 return base_type == GLSL_TYPE_IMAGE;
733 }
734
735 /**
736 * Query whether or not a type is an array
737 */
738 bool is_array() const
739 {
740 return base_type == GLSL_TYPE_ARRAY;
741 }
742
743 bool is_array_of_arrays() const
744 {
745 return is_array() && fields.array->is_array();
746 }
747
748 /**
749 * Query whether or not a type is a record
750 */
751 bool is_struct() const
752 {
753 return base_type == GLSL_TYPE_STRUCT;
754 }
755
756 /**
757 * Query whether or not a type is an interface
758 */
759 bool is_interface() const
760 {
761 return base_type == GLSL_TYPE_INTERFACE;
762 }
763
764 /**
765 * Query whether or not a type is the void type singleton.
766 */
767 bool is_void() const
768 {
769 return base_type == GLSL_TYPE_VOID;
770 }
771
772 /**
773 * Query whether or not a type is the error type singleton.
774 */
775 bool is_error() const
776 {
777 return base_type == GLSL_TYPE_ERROR;
778 }
779
780 /**
781 * Query if a type is unnamed/anonymous (named by the parser)
782 */
783
784 bool is_subroutine() const
785 {
786 return base_type == GLSL_TYPE_SUBROUTINE;
787 }
788 bool contains_subroutine() const;
789
790 bool is_anonymous() const
791 {
792 return !strncmp(name, "#anon", 5);
793 }
794
795 /**
796 * Get the type stripped of any arrays
797 *
798 * \return
799 * Pointer to the type of elements of the first non-array type for array
800 * types, or pointer to itself for non-array types.
801 */
802 const glsl_type *without_array() const
803 {
804 const glsl_type *t = this;
805
806 while (t->is_array())
807 t = t->fields.array;
808
809 return t;
810 }
811
812 /**
813 * Return the total number of elements in an array including the elements
814 * in arrays of arrays.
815 */
816 unsigned arrays_of_arrays_size() const
817 {
818 if (!is_array())
819 return 0;
820
821 unsigned size = length;
822 const glsl_type *base_type = fields.array;
823
824 while (base_type->is_array()) {
825 size = size * base_type->length;
826 base_type = base_type->fields.array;
827 }
828 return size;
829 }
830
831 /**
832 * Query whether or not a type is an atomic_uint.
833 */
834 bool is_atomic_uint() const
835 {
836 return base_type == GLSL_TYPE_ATOMIC_UINT;
837 }
838
839 /**
840 * Return the amount of atomic counter storage required for a type.
841 */
842 unsigned atomic_size() const
843 {
844 if (is_atomic_uint())
845 return ATOMIC_COUNTER_SIZE;
846 else if (is_array())
847 return length * fields.array->atomic_size();
848 else
849 return 0;
850 }
851
852 /**
853 * Return whether a type contains any atomic counters.
854 */
855 bool contains_atomic() const
856 {
857 return atomic_size() > 0;
858 }
859
860 /**
861 * Return whether a type contains any opaque types.
862 */
863 bool contains_opaque() const;
864
865 /**
866 * Query the full type of a matrix row
867 *
868 * \return
869 * If the type is not a matrix, \c glsl_type::error_type is returned.
870 * Otherwise a type matching the rows of the matrix is returned.
871 */
872 const glsl_type *row_type() const
873 {
874 if (!is_matrix())
875 return error_type;
876
877 if (explicit_stride && !interface_row_major)
878 return get_instance(base_type, matrix_columns, 1, explicit_stride);
879 else
880 return get_instance(base_type, matrix_columns, 1);
881 }
882
883 /**
884 * Query the full type of a matrix column
885 *
886 * \return
887 * If the type is not a matrix, \c glsl_type::error_type is returned.
888 * Otherwise a type matching the columns of the matrix is returned.
889 */
890 const glsl_type *column_type() const
891 {
892 if (!is_matrix())
893 return error_type;
894
895 if (explicit_stride && interface_row_major)
896 return get_instance(base_type, vector_elements, 1, explicit_stride);
897 else
898 return get_instance(base_type, vector_elements, 1);
899 }
900
901 /**
902 * Get the type of a structure field
903 *
904 * \return
905 * Pointer to the type of the named field. If the type is not a structure
906 * or the named field does not exist, \c glsl_type::error_type is returned.
907 */
908 const glsl_type *field_type(const char *name) const;
909
910 /**
911 * Get the location of a field within a record type
912 */
913 int field_index(const char *name) const;
914
915 /**
916 * Query the number of elements in an array type
917 *
918 * \return
919 * The number of elements in the array for array types or -1 for non-array
920 * types. If the number of elements in the array has not yet been declared,
921 * zero is returned.
922 */
923 int array_size() const
924 {
925 return is_array() ? length : -1;
926 }
927
928 /**
929 * Query whether the array size for all dimensions has been declared.
930 */
931 bool is_unsized_array() const
932 {
933 return is_array() && length == 0;
934 }
935
936 /**
937 * Return the number of coordinate components needed for this
938 * sampler or image type.
939 *
940 * This is based purely on the sampler's dimensionality. For example, this
941 * returns 1 for sampler1D, and 3 for sampler2DArray.
942 *
943 * Note that this is often different than actual coordinate type used in
944 * a texturing built-in function, since those pack additional values (such
945 * as the shadow comparator or projector) into the coordinate type.
946 */
947 int coordinate_components() const;
948
949 /**
950 * Compares whether this type matches another type without taking into
951 * account the precision in structures.
952 *
953 * This is applied recursively so that structures containing structure
954 * members can also ignore the precision.
955 */
956 bool compare_no_precision(const glsl_type *b) const;
957
958 /**
959 * Compare a record type against another record type.
960 *
961 * This is useful for matching record types declared on the same shader
962 * stage as well as across different shader stages.
963 * The option to not match name is needed for matching record types
964 * declared across different shader stages.
965 * The option to not match locations is to deal with places where the
966 * same struct is defined in a block which has a location set on it.
967 */
968 bool record_compare(const glsl_type *b, bool match_name,
969 bool match_locations = true,
970 bool match_precision = true) const;
971
972 /**
973 * Get the type interface packing.
974 */
975 enum glsl_interface_packing get_interface_packing() const
976 {
977 return (enum glsl_interface_packing)interface_packing;
978 }
979
980 /**
981 * Get the type interface packing used internally. For shared and packing
982 * layouts this is implementation defined.
983 */
984 enum glsl_interface_packing get_internal_ifc_packing(bool std430_supported) const
985 {
986 enum glsl_interface_packing packing = this->get_interface_packing();
987 if (packing == GLSL_INTERFACE_PACKING_STD140 ||
988 (!std430_supported &&
989 (packing == GLSL_INTERFACE_PACKING_SHARED ||
990 packing == GLSL_INTERFACE_PACKING_PACKED))) {
991 return GLSL_INTERFACE_PACKING_STD140;
992 } else {
993 assert(packing == GLSL_INTERFACE_PACKING_STD430 ||
994 (std430_supported &&
995 (packing == GLSL_INTERFACE_PACKING_SHARED ||
996 packing == GLSL_INTERFACE_PACKING_PACKED)));
997 return GLSL_INTERFACE_PACKING_STD430;
998 }
999 }
1000
1001 /**
1002 * Check if the type interface is row major
1003 */
1004 bool get_interface_row_major() const
1005 {
1006 return (bool) interface_row_major;
1007 }
1008
1009 ~glsl_type();
1010
1011 private:
1012
1013 static mtx_t hash_mutex;
1014
1015 /**
1016 * ralloc context for the type itself.
1017 */
1018 void *mem_ctx;
1019
1020 /** Constructor for vector and matrix types */
1021 glsl_type(GLenum gl_type,
1022 glsl_base_type base_type, unsigned vector_elements,
1023 unsigned matrix_columns, const char *name,
1024 unsigned explicit_stride = 0, bool row_major = false);
1025
1026 /** Constructor for sampler or image types */
1027 glsl_type(GLenum gl_type, glsl_base_type base_type,
1028 enum glsl_sampler_dim dim, bool shadow, bool array,
1029 glsl_base_type type, const char *name);
1030
1031 /** Constructor for record types */
1032 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
1033 const char *name, bool packed = false);
1034
1035 /** Constructor for interface types */
1036 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
1037 enum glsl_interface_packing packing,
1038 bool row_major, const char *name);
1039
1040 /** Constructor for interface types */
1041 glsl_type(const glsl_type *return_type,
1042 const glsl_function_param *params, unsigned num_params);
1043
1044 /** Constructors for array types */
1045 glsl_type(const glsl_type *array, unsigned length, unsigned explicit_stride);
1046
1047 /** Constructor for subroutine types */
1048 glsl_type(const char *name);
1049
1050 /** Hash table containing the known explicit matrix and vector types. */
1051 static struct hash_table *explicit_matrix_types;
1052
1053 /** Hash table containing the known array types. */
1054 static struct hash_table *array_types;
1055
1056 /** Hash table containing the known struct types. */
1057 static struct hash_table *struct_types;
1058
1059 /** Hash table containing the known interface types. */
1060 static struct hash_table *interface_types;
1061
1062 /** Hash table containing the known subroutine types. */
1063 static struct hash_table *subroutine_types;
1064
1065 /** Hash table containing the known function types. */
1066 static struct hash_table *function_types;
1067
1068 static bool record_key_compare(const void *a, const void *b);
1069 static unsigned record_key_hash(const void *key);
1070
1071 /**
1072 * \name Built-in type flyweights
1073 */
1074 /*@{*/
1075 #undef DECL_TYPE
1076 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
1077 #undef STRUCT_TYPE
1078 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
1079 #include "compiler/builtin_type_macros.h"
1080 /*@}*/
1081
1082 /**
1083 * \name Friend functions.
1084 *
1085 * These functions are friends because they must have C linkage and the
1086 * need to call various private methods or access various private static
1087 * data.
1088 */
1089 /*@{*/
1090 friend void glsl_type_singleton_init_or_ref(void);
1091 friend void glsl_type_singleton_decref(void);
1092 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
1093 /*@}*/
1094 };
1095
1096 #undef DECL_TYPE
1097 #undef STRUCT_TYPE
1098 #endif /* __cplusplus */
1099
1100 struct glsl_struct_field {
1101 const struct glsl_type *type;
1102 const char *name;
1103
1104 /**
1105 * For interface blocks, gl_varying_slot corresponding to the input/output
1106 * if this is a built-in input/output (i.e. a member of the built-in
1107 * gl_PerVertex interface block); -1 otherwise.
1108 *
1109 * Ignored for structs.
1110 */
1111 int location;
1112
1113 /**
1114 * For interface blocks, members may have an explicit byte offset
1115 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1116 *
1117 * Unless used for xfb_offset this field is ignored for structs.
1118 */
1119 int offset;
1120
1121 /**
1122 * For interface blocks, members may define a transform feedback buffer;
1123 * -1 otherwise.
1124 */
1125 int xfb_buffer;
1126
1127 /**
1128 * For interface blocks, members may define a transform feedback stride;
1129 * -1 otherwise.
1130 */
1131 int xfb_stride;
1132
1133 /**
1134 * For interface blocks, the interpolation mode (as in
1135 * ir_variable::interpolation). 0 otherwise.
1136 */
1137 unsigned interpolation:2;
1138
1139 /**
1140 * For interface blocks, 1 if this variable uses centroid interpolation (as
1141 * in ir_variable::centroid). 0 otherwise.
1142 */
1143 unsigned centroid:1;
1144
1145 /**
1146 * For interface blocks, 1 if this variable uses sample interpolation (as
1147 * in ir_variable::sample). 0 otherwise.
1148 */
1149 unsigned sample:1;
1150
1151 /**
1152 * Layout of the matrix. Uses glsl_matrix_layout values.
1153 */
1154 unsigned matrix_layout:2;
1155
1156 /**
1157 * For interface blocks, 1 if this variable is a per-patch input or output
1158 * (as in ir_variable::patch). 0 otherwise.
1159 */
1160 unsigned patch:1;
1161
1162 /**
1163 * Precision qualifier
1164 */
1165 unsigned precision:2;
1166
1167 /**
1168 * Memory qualifiers, applicable to buffer variables defined in shader
1169 * storage buffer objects (SSBOs)
1170 */
1171 unsigned memory_read_only:1;
1172 unsigned memory_write_only:1;
1173 unsigned memory_coherent:1;
1174 unsigned memory_volatile:1;
1175 unsigned memory_restrict:1;
1176
1177 /**
1178 * Layout format, applicable to image variables only.
1179 */
1180 unsigned image_format:16;
1181
1182 /**
1183 * Any of the xfb_* qualifiers trigger the shader to be in transform
1184 * feedback mode so we need to keep track of whether the buffer was
1185 * explicitly set or if its just been assigned the default global value.
1186 */
1187 unsigned explicit_xfb_buffer:1;
1188
1189 unsigned implicit_sized_array:1;
1190 #ifdef __cplusplus
1191 glsl_struct_field(const struct glsl_type *_type, const char *_name)
1192 : type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0),
1193 xfb_stride(0), interpolation(0), centroid(0),
1194 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0),
1195 precision(GLSL_PRECISION_NONE), memory_read_only(0),
1196 memory_write_only(0), memory_coherent(0), memory_volatile(0),
1197 memory_restrict(0), image_format(0), explicit_xfb_buffer(0),
1198 implicit_sized_array(0)
1199 {
1200 /* empty */
1201 }
1202
1203 glsl_struct_field()
1204 : type(NULL), name(NULL), location(-1), offset(-1), xfb_buffer(0),
1205 xfb_stride(0), interpolation(0), centroid(0),
1206 sample(0), matrix_layout(0), patch(0),
1207 precision(0), memory_read_only(0),
1208 memory_write_only(0), memory_coherent(0), memory_volatile(0),
1209 memory_restrict(0), image_format(0), explicit_xfb_buffer(0),
1210 implicit_sized_array(0)
1211 {
1212 /* empty */
1213 }
1214 #endif
1215 };
1216
1217 struct glsl_function_param {
1218 const struct glsl_type *type;
1219
1220 bool in;
1221 bool out;
1222 };
1223
1224 static inline unsigned int
1225 glsl_align(unsigned int a, unsigned int align)
1226 {
1227 return (a + align - 1) / align * align;
1228 }
1229
1230 #endif /* GLSL_TYPES_H */