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