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