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