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