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