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