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