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