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