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