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