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