dri: Use DRM_FORMAT_* instead of defining our own copy.
[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 #include "c11/threads.h"
34 #include "util/macros.h"
35
36 #ifdef __cplusplus
37 #include "main/config.h"
38 #endif
39
40 struct glsl_type;
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 struct _mesa_glsl_parse_state;
47 struct glsl_symbol_table;
48
49 extern void
50 glsl_type_singleton_init_or_ref();
51
52 extern void
53 glsl_type_singleton_decref();
54
55 extern void
56 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
57
58 void encode_type_to_blob(struct blob *blob, const struct glsl_type *type);
59
60 const struct glsl_type *decode_type_from_blob(struct blob_reader *blob);
61
62 #ifdef __cplusplus
63 }
64 #endif
65
66 typedef void (*glsl_type_size_align_func)(const struct glsl_type *type,
67 unsigned *size, unsigned *align);
68
69 enum glsl_base_type {
70 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
71 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
72 */
73 GLSL_TYPE_UINT = 0,
74 GLSL_TYPE_INT,
75 GLSL_TYPE_FLOAT,
76 GLSL_TYPE_FLOAT16,
77 GLSL_TYPE_DOUBLE,
78 GLSL_TYPE_UINT8,
79 GLSL_TYPE_INT8,
80 GLSL_TYPE_UINT16,
81 GLSL_TYPE_INT16,
82 GLSL_TYPE_UINT64,
83 GLSL_TYPE_INT64,
84 GLSL_TYPE_BOOL,
85 GLSL_TYPE_SAMPLER,
86 GLSL_TYPE_IMAGE,
87 GLSL_TYPE_ATOMIC_UINT,
88 GLSL_TYPE_STRUCT,
89 GLSL_TYPE_INTERFACE,
90 GLSL_TYPE_ARRAY,
91 GLSL_TYPE_VOID,
92 GLSL_TYPE_SUBROUTINE,
93 GLSL_TYPE_FUNCTION,
94 GLSL_TYPE_ERROR
95 };
96
97 /* Return the bit size of a type. Note that this differs from
98 * glsl_get_bit_size in that it returns 32 bits for bools, whereas at
99 * the NIR level we would want to return 1 bit for bools.
100 */
101 static unsigned glsl_base_type_bit_size(enum glsl_base_type type)
102 {
103 switch (type) {
104 case GLSL_TYPE_BOOL:
105 case GLSL_TYPE_INT:
106 case GLSL_TYPE_UINT:
107 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
108 case GLSL_TYPE_SUBROUTINE:
109 return 32;
110
111 case GLSL_TYPE_FLOAT16:
112 case GLSL_TYPE_UINT16:
113 case GLSL_TYPE_INT16:
114 return 16;
115
116 case GLSL_TYPE_UINT8:
117 case GLSL_TYPE_INT8:
118 return 8;
119
120 case GLSL_TYPE_DOUBLE:
121 case GLSL_TYPE_INT64:
122 case GLSL_TYPE_UINT64:
123 case GLSL_TYPE_IMAGE:
124 case GLSL_TYPE_SAMPLER:
125 return 64;
126
127 default:
128 /* For GLSL_TYPE_STRUCT etc, it should be ok to return 0. This usually
129 * happens when calling this method through is_64bit and is_16bit
130 * methods
131 */
132 return 0;
133 }
134
135 return 0;
136 }
137
138 static inline bool glsl_base_type_is_16bit(enum glsl_base_type type)
139 {
140 return glsl_base_type_bit_size(type) == 16;
141 }
142
143 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type)
144 {
145 return glsl_base_type_bit_size(type) == 64;
146 }
147
148 static inline bool glsl_base_type_is_integer(enum glsl_base_type type)
149 {
150 return type == GLSL_TYPE_UINT8 ||
151 type == GLSL_TYPE_INT8 ||
152 type == GLSL_TYPE_UINT16 ||
153 type == GLSL_TYPE_INT16 ||
154 type == GLSL_TYPE_UINT ||
155 type == GLSL_TYPE_INT ||
156 type == GLSL_TYPE_UINT64 ||
157 type == GLSL_TYPE_INT64 ||
158 type == GLSL_TYPE_BOOL ||
159 type == GLSL_TYPE_SAMPLER ||
160 type == GLSL_TYPE_IMAGE;
161 }
162
163 static inline unsigned int
164 glsl_base_type_get_bit_size(const enum glsl_base_type base_type)
165 {
166 switch (base_type) {
167 case GLSL_TYPE_BOOL:
168 return 1;
169
170 case GLSL_TYPE_INT:
171 case GLSL_TYPE_UINT:
172 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
173 case GLSL_TYPE_SUBROUTINE:
174 return 32;
175
176 case GLSL_TYPE_FLOAT16:
177 case GLSL_TYPE_UINT16:
178 case GLSL_TYPE_INT16:
179 return 16;
180
181 case GLSL_TYPE_UINT8:
182 case GLSL_TYPE_INT8:
183 return 8;
184
185 case GLSL_TYPE_DOUBLE:
186 case GLSL_TYPE_INT64:
187 case GLSL_TYPE_UINT64:
188 case GLSL_TYPE_IMAGE:
189 case GLSL_TYPE_SAMPLER:
190 return 64;
191
192 default:
193 unreachable("unknown base type");
194 }
195
196 return 0;
197 }
198
199 enum glsl_sampler_dim {
200 GLSL_SAMPLER_DIM_1D = 0,
201 GLSL_SAMPLER_DIM_2D,
202 GLSL_SAMPLER_DIM_3D,
203 GLSL_SAMPLER_DIM_CUBE,
204 GLSL_SAMPLER_DIM_RECT,
205 GLSL_SAMPLER_DIM_BUF,
206 GLSL_SAMPLER_DIM_EXTERNAL,
207 GLSL_SAMPLER_DIM_MS,
208 GLSL_SAMPLER_DIM_SUBPASS, /* for vulkan input attachments */
209 GLSL_SAMPLER_DIM_SUBPASS_MS, /* for multisampled vulkan input attachments */
210 };
211
212 enum glsl_matrix_layout {
213 /**
214 * The layout of the matrix is inherited from the object containing the
215 * matrix (the top level structure or the uniform block).
216 */
217 GLSL_MATRIX_LAYOUT_INHERITED,
218
219 /**
220 * Explicit column-major layout
221 *
222 * If a uniform block doesn't have an explicit layout set, it will default
223 * to this layout.
224 */
225 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
226
227 /**
228 * Row-major layout
229 */
230 GLSL_MATRIX_LAYOUT_ROW_MAJOR
231 };
232
233 enum {
234 GLSL_PRECISION_NONE = 0,
235 GLSL_PRECISION_HIGH,
236 GLSL_PRECISION_MEDIUM,
237 GLSL_PRECISION_LOW
238 };
239
240 #ifdef __cplusplus
241 #include "GL/gl.h"
242 #include "util/ralloc.h"
243 #include "main/menums.h" /* for gl_texture_index, C++'s enum rules are broken */
244
245 struct glsl_type {
246 GLenum gl_type;
247 glsl_base_type base_type:8;
248
249 glsl_base_type sampled_type:8; /**< Type of data returned using this
250 * sampler or image. Only \c
251 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
252 * and \c GLSL_TYPE_UINT are valid.
253 */
254
255 unsigned sampler_dimensionality:4; /**< \see glsl_sampler_dim */
256 unsigned sampler_shadow:1;
257 unsigned sampler_array:1;
258 unsigned interface_packing:2;
259 unsigned interface_row_major:1;
260
261 /**
262 * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
263 *
264 * Only used for Compute kernels
265 */
266 unsigned packed:1;
267
268 private:
269 glsl_type() : mem_ctx(NULL)
270 {
271 // Dummy constructor, just for the sake of ASSERT_BITFIELD_SIZE.
272 }
273
274 public:
275 /**
276 * \name Vector and matrix element counts
277 *
278 * For scalars, each of these values will be 1. For non-numeric types
279 * these will be 0.
280 */
281 /*@{*/
282 uint8_t vector_elements; /**< 1, 2, 3, or 4 vector elements. */
283 uint8_t matrix_columns; /**< 1, 2, 3, or 4 matrix columns. */
284 /*@}*/
285
286 /**
287 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
288 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
289 * elements in the structure and the number of values pointed to by
290 * \c fields.structure (below).
291 */
292 unsigned length;
293
294 /**
295 * Name of the data type
296 *
297 * Will never be \c NULL.
298 */
299 const char *name;
300
301 /**
302 * Explicit array, matrix, or vector stride. This is used to communicate
303 * explicit array layouts from SPIR-V. Should be 0 if the type has no
304 * explicit stride.
305 */
306 unsigned explicit_stride;
307
308 /**
309 * Subtype of composite data types.
310 */
311 union {
312 const struct glsl_type *array; /**< Type of array elements. */
313 struct glsl_function_param *parameters; /**< Parameters to function. */
314 struct glsl_struct_field *structure; /**< List of struct fields. */
315 } fields;
316
317 /**
318 * \name Pointers to various public type singletons
319 */
320 /*@{*/
321 #undef DECL_TYPE
322 #define DECL_TYPE(NAME, ...) \
323 static const glsl_type *const NAME##_type;
324 #undef STRUCT_TYPE
325 #define STRUCT_TYPE(NAME) \
326 static const glsl_type *const struct_##NAME##_type;
327 #include "compiler/builtin_type_macros.h"
328 /*@}*/
329
330 /**
331 * Convenience accessors for vector types (shorter than get_instance()).
332 * @{
333 */
334 static const glsl_type *vec(unsigned components, const glsl_type *const ts[]);
335 static const glsl_type *vec(unsigned components);
336 static const glsl_type *f16vec(unsigned components);
337 static const glsl_type *dvec(unsigned components);
338 static const glsl_type *ivec(unsigned components);
339 static const glsl_type *uvec(unsigned components);
340 static const glsl_type *bvec(unsigned components);
341 static const glsl_type *i64vec(unsigned components);
342 static const glsl_type *u64vec(unsigned components);
343 static const glsl_type *i16vec(unsigned components);
344 static const glsl_type *u16vec(unsigned components);
345 static const glsl_type *i8vec(unsigned components);
346 static const glsl_type *u8vec(unsigned components);
347 /**@}*/
348
349 /**
350 * For numeric and boolean derived types returns the basic scalar type
351 *
352 * If the type is a numeric or boolean scalar, vector, or matrix type,
353 * this function gets the scalar type of the individual components. For
354 * all other types, including arrays of numeric or boolean types, the
355 * error type is returned.
356 */
357 const glsl_type *get_base_type() const;
358
359 /**
360 * Get the basic scalar type which this type aggregates.
361 *
362 * If the type is a numeric or boolean scalar, vector, or matrix, or an
363 * array of any of those, this function gets the scalar type of the
364 * individual components. For structs and arrays of structs, this function
365 * returns the struct type. For samplers and arrays of samplers, this
366 * function returns the sampler type.
367 */
368 const glsl_type *get_scalar_type() const;
369
370 /**
371 * Gets the "bare" type without any decorations or layout information.
372 */
373 const glsl_type *get_bare_type() const;
374
375 /**
376 * Get the instance of a built-in scalar, vector, or matrix type
377 */
378 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
379 unsigned columns,
380 unsigned explicit_stride = 0,
381 bool row_major = false);
382
383 /**
384 * Get the instance of a sampler type
385 */
386 static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
387 bool shadow,
388 bool array,
389 glsl_base_type type);
390
391 static const glsl_type *get_image_instance(enum glsl_sampler_dim dim,
392 bool array, glsl_base_type type);
393
394 /**
395 * Get the instance of an array type
396 */
397 static const glsl_type *get_array_instance(const glsl_type *base,
398 unsigned elements,
399 unsigned explicit_stride = 0);
400
401 /**
402 * Get the instance of a record type
403 */
404 static const glsl_type *get_struct_instance(const glsl_struct_field *fields,
405 unsigned num_fields,
406 const char *name,
407 bool packed = false);
408
409 /**
410 * Get the instance of an interface block type
411 */
412 static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
413 unsigned num_fields,
414 enum glsl_interface_packing packing,
415 bool row_major,
416 const char *block_name);
417
418 /**
419 * Get the instance of an subroutine type
420 */
421 static const glsl_type *get_subroutine_instance(const char *subroutine_name);
422
423 /**
424 * Get the instance of a function type
425 */
426 static const glsl_type *get_function_instance(const struct glsl_type *return_type,
427 const glsl_function_param *parameters,
428 unsigned num_params);
429
430 /**
431 * Get the type resulting from a multiplication of \p type_a * \p type_b
432 */
433 static const glsl_type *get_mul_type(const glsl_type *type_a,
434 const glsl_type *type_b);
435
436 /**
437 * Query the total number of scalars that make up a scalar, vector or matrix
438 */
439 unsigned components() const
440 {
441 return vector_elements * matrix_columns;
442 }
443
444 /**
445 * Calculate the number of components slots required to hold this type
446 *
447 * This is used to determine how many uniform or varying locations a type
448 * might occupy.
449 */
450 unsigned component_slots() const;
451
452 /**
453 * Calculate offset between the base location of the struct in
454 * uniform storage and a struct member.
455 * For the initial call, length is the index of the member to find the
456 * offset for.
457 */
458 unsigned struct_location_offset(unsigned length) const;
459
460 /**
461 * Calculate the number of unique values from glGetUniformLocation for the
462 * elements of the type.
463 *
464 * This is used to allocate slots in the UniformRemapTable, the amount of
465 * locations may not match with actual used storage space by the driver.
466 */
467 unsigned uniform_locations() const;
468
469 /**
470 * Used to count the number of varyings contained in the type ignoring
471 * innermost array elements.
472 */
473 unsigned varying_count() const;
474
475 /**
476 * Calculate the number of attribute slots required to hold this type
477 *
478 * This implements the language rules of GLSL 1.50 for counting the number
479 * of slots used by a vertex attribute. It also determines the number of
480 * varying slots the type will use up in the absence of varying packing
481 * (and thus, it can be used to measure the number of varying slots used by
482 * the varyings that are generated by lower_packed_varyings).
483 *
484 * For vertex shader attributes - doubles only take one slot.
485 * For inter-shader varyings - dvec3/dvec4 take two slots.
486 *
487 * Vulkan doesn’t make this distinction so the argument should always be
488 * false.
489 */
490 unsigned count_attribute_slots(bool is_gl_vertex_input) const;
491
492 /**
493 * Alignment in bytes of the start of this type in a std140 uniform
494 * block.
495 */
496 unsigned std140_base_alignment(bool row_major) const;
497
498 /** Size in bytes of this type in a std140 uniform block.
499 *
500 * Note that this is not GL_UNIFORM_SIZE (which is the number of
501 * elements in the array)
502 */
503 unsigned std140_size(bool row_major) const;
504
505 /**
506 * Gets an explicitly laid out type with the std140 layout.
507 */
508 const glsl_type *get_explicit_std140_type(bool row_major) const;
509
510 /**
511 * Alignment in bytes of the start of this type in a std430 shader
512 * storage block.
513 */
514 unsigned std430_base_alignment(bool row_major) const;
515
516 /**
517 * Calculate array stride in bytes of this type in a std430 shader storage
518 * block.
519 */
520 unsigned std430_array_stride(bool row_major) const;
521
522 /**
523 * Size in bytes of this type in a std430 shader storage block.
524 *
525 * Note that this is not GL_BUFFER_SIZE
526 */
527 unsigned std430_size(bool row_major) const;
528
529 /**
530 * Gets an explicitly laid out type with the std430 layout.
531 */
532 const glsl_type *get_explicit_std430_type(bool row_major) const;
533
534 /**
535 * Gets an explicitly laid out interface type.
536 */
537 const glsl_type *get_explicit_interface_type(bool supports_std430) const;
538
539 /** Returns an explicitly laid out type given a type and size/align func
540 *
541 * The size/align func is only called for scalar and vector types and the
542 * returned type is otherwise laid out in the natural way as follows:
543 *
544 * - Arrays and matrices have a stride of ALIGN(elem_size, elem_align).
545 *
546 * - Structure types have their elements in-order and as tightly packed as
547 * possible following the alignment required by the size/align func.
548 *
549 * - All composite types (structures, matrices, and arrays) have an
550 * alignment equal to the highest alighment of any member of the composite.
551 *
552 * The types returned by this function are likely not suitable for most UBO
553 * or SSBO layout because they do not add the extra array and substructure
554 * alignment that is required by std140 and std430.
555 */
556 const glsl_type *get_explicit_type_for_size_align(glsl_type_size_align_func type_info,
557 unsigned *size, unsigned *align) const;
558
559 /**
560 * Alignment in bytes of the start of this type in OpenCL memory.
561 */
562 unsigned cl_alignment() const;
563
564 /**
565 * Size in bytes of this type in OpenCL memory
566 */
567 unsigned cl_size() const;
568
569 /**
570 * Size in bytes of this type based on its explicit data.
571 *
572 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
573 * through explicit offset, stride and matrix layout, so the size
574 * can/should be computed used those values.
575 *
576 * Note that the value returned by this method is only correct if such
577 * values are set, so only with SPIR-V shaders. Should not be used with
578 * GLSL shaders.
579 */
580 unsigned explicit_size(bool align_to_stride=false) const;
581
582 /**
583 * \brief Can this type be implicitly converted to another?
584 *
585 * \return True if the types are identical or if this type can be converted
586 * to \c desired according to Section 4.1.10 of the GLSL spec.
587 *
588 * \verbatim
589 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
590 * Implicit Conversions:
591 *
592 * In some situations, an expression and its type will be implicitly
593 * converted to a different type. The following table shows all allowed
594 * implicit conversions:
595 *
596 * Type of expression | Can be implicitly converted to
597 * --------------------------------------------------
598 * int float
599 * uint
600 *
601 * ivec2 vec2
602 * uvec2
603 *
604 * ivec3 vec3
605 * uvec3
606 *
607 * ivec4 vec4
608 * uvec4
609 *
610 * There are no implicit array or structure conversions. For example,
611 * an array of int cannot be implicitly converted to an array of float.
612 * There are no implicit conversions between signed and unsigned
613 * integers.
614 * \endverbatim
615 */
616 bool can_implicitly_convert_to(const glsl_type *desired,
617 _mesa_glsl_parse_state *state) const;
618
619 /**
620 * Query whether or not a type is a scalar (non-vector and non-matrix).
621 */
622 bool is_scalar() const
623 {
624 return (vector_elements == 1)
625 && (base_type >= GLSL_TYPE_UINT)
626 && (base_type <= GLSL_TYPE_IMAGE);
627 }
628
629 /**
630 * Query whether or not a type is a vector
631 */
632 bool is_vector() const
633 {
634 return (vector_elements > 1)
635 && (matrix_columns == 1)
636 && (base_type >= GLSL_TYPE_UINT)
637 && (base_type <= GLSL_TYPE_BOOL);
638 }
639
640 /**
641 * Query whether or not a type is a matrix
642 */
643 bool is_matrix() const
644 {
645 /* GLSL only has float matrices. */
646 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT ||
647 base_type == GLSL_TYPE_DOUBLE ||
648 base_type == GLSL_TYPE_FLOAT16);
649 }
650
651 /**
652 * Query whether or not a type is a non-array numeric type
653 */
654 bool is_numeric() const
655 {
656 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_INT64);
657 }
658
659 /**
660 * Query whether or not a type is an integer.
661 */
662 bool is_integer() const
663 {
664 return glsl_base_type_is_integer(base_type);
665 }
666
667 /**
668 * Query whether or not a type is an 32-bit integer.
669 */
670 bool is_integer_32() const
671 {
672 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
673 }
674
675 /**
676 * Query whether or not a type is a 64-bit integer.
677 */
678 bool is_integer_64() const
679 {
680 return base_type == GLSL_TYPE_UINT64 || base_type == GLSL_TYPE_INT64;
681 }
682
683 /**
684 * Query whether or not a type is a 32-bit or 64-bit integer
685 */
686 bool is_integer_32_64() const
687 {
688 return is_integer_32() || is_integer_64();
689 }
690
691 /**
692 * Query whether or not type is an integral type, or for struct and array
693 * types, contains an integral type.
694 */
695 bool contains_integer() const;
696
697 /**
698 * Query whether or not type is a double type, or for struct, interface and
699 * array types, contains a double type.
700 */
701 bool contains_double() const;
702
703 /**
704 * Query whether or not type is a 64-bit type, or for struct, interface and
705 * array types, contains a double type.
706 */
707 bool contains_64bit() const;
708
709 /**
710 * Query whether or not a type is a float type
711 */
712 bool is_float() const
713 {
714 return base_type == GLSL_TYPE_FLOAT;
715 }
716
717 /**
718 * Query whether or not a type is a double type
719 */
720 bool is_double() const
721 {
722 return base_type == GLSL_TYPE_DOUBLE;
723 }
724
725 /**
726 * Query whether a 64-bit type takes two slots.
727 */
728 bool is_dual_slot() const
729 {
730 return is_64bit() && vector_elements > 2;
731 }
732
733 /**
734 * Query whether or not a type is 64-bit
735 */
736 bool is_64bit() const
737 {
738 return glsl_base_type_is_64bit(base_type);
739 }
740
741 /**
742 * Query whether or not a type is 16-bit
743 */
744 bool is_16bit() const
745 {
746 return glsl_base_type_is_16bit(base_type);
747 }
748
749 /**
750 * Query whether or not a type is 32-bit
751 */
752 bool is_32bit() const
753 {
754 return base_type == GLSL_TYPE_UINT ||
755 base_type == GLSL_TYPE_INT ||
756 base_type == GLSL_TYPE_FLOAT;
757 }
758
759 /**
760 * Query whether or not a type is a non-array boolean type
761 */
762 bool is_boolean() const
763 {
764 return base_type == GLSL_TYPE_BOOL;
765 }
766
767 /**
768 * Query whether or not a type is a sampler
769 */
770 bool is_sampler() const
771 {
772 return base_type == GLSL_TYPE_SAMPLER;
773 }
774
775 /**
776 * Query whether or not type is a sampler, or for struct, interface and
777 * array types, contains a sampler.
778 */
779 bool contains_sampler() const;
780
781 /**
782 * Query whether or not type is an array or for struct, interface and
783 * array types, contains an array.
784 */
785 bool contains_array() const;
786
787 /**
788 * Get the Mesa texture target index for a sampler type.
789 */
790 gl_texture_index sampler_index() const;
791
792 /**
793 * Query whether or not type is an image, or for struct, interface and
794 * array types, contains an image.
795 */
796 bool contains_image() const;
797
798 /**
799 * Query whether or not a type is an image
800 */
801 bool is_image() const
802 {
803 return base_type == GLSL_TYPE_IMAGE;
804 }
805
806 /**
807 * Query whether or not a type is an array
808 */
809 bool is_array() const
810 {
811 return base_type == GLSL_TYPE_ARRAY;
812 }
813
814 bool is_array_of_arrays() const
815 {
816 return is_array() && fields.array->is_array();
817 }
818
819 /**
820 * Query whether or not a type is a record
821 */
822 bool is_struct() const
823 {
824 return base_type == GLSL_TYPE_STRUCT;
825 }
826
827 /**
828 * Query whether or not a type is an interface
829 */
830 bool is_interface() const
831 {
832 return base_type == GLSL_TYPE_INTERFACE;
833 }
834
835 /**
836 * Query whether or not a type is the void type singleton.
837 */
838 bool is_void() const
839 {
840 return base_type == GLSL_TYPE_VOID;
841 }
842
843 /**
844 * Query whether or not a type is the error type singleton.
845 */
846 bool is_error() const
847 {
848 return base_type == GLSL_TYPE_ERROR;
849 }
850
851 /**
852 * Query if a type is unnamed/anonymous (named by the parser)
853 */
854
855 bool is_subroutine() const
856 {
857 return base_type == GLSL_TYPE_SUBROUTINE;
858 }
859 bool contains_subroutine() const;
860
861 bool is_anonymous() const
862 {
863 return !strncmp(name, "#anon", 5);
864 }
865
866 /**
867 * Get the type stripped of any arrays
868 *
869 * \return
870 * Pointer to the type of elements of the first non-array type for array
871 * types, or pointer to itself for non-array types.
872 */
873 const glsl_type *without_array() const
874 {
875 const glsl_type *t = this;
876
877 while (t->is_array())
878 t = t->fields.array;
879
880 return t;
881 }
882
883 /**
884 * Return the total number of elements in an array including the elements
885 * in arrays of arrays.
886 */
887 unsigned arrays_of_arrays_size() const
888 {
889 if (!is_array())
890 return 0;
891
892 unsigned size = length;
893 const glsl_type *base_type = fields.array;
894
895 while (base_type->is_array()) {
896 size = size * base_type->length;
897 base_type = base_type->fields.array;
898 }
899 return size;
900 }
901
902 /**
903 * Return bit size for this type.
904 */
905 unsigned bit_size() const
906 {
907 return glsl_base_type_bit_size(this->base_type);
908 }
909
910
911 /**
912 * Query whether or not a type is an atomic_uint.
913 */
914 bool is_atomic_uint() const
915 {
916 return base_type == GLSL_TYPE_ATOMIC_UINT;
917 }
918
919 /**
920 * Return the amount of atomic counter storage required for a type.
921 */
922 unsigned atomic_size() const
923 {
924 if (is_atomic_uint())
925 return ATOMIC_COUNTER_SIZE;
926 else if (is_array())
927 return length * fields.array->atomic_size();
928 else
929 return 0;
930 }
931
932 /**
933 * Return whether a type contains any atomic counters.
934 */
935 bool contains_atomic() const
936 {
937 return atomic_size() > 0;
938 }
939
940 /**
941 * Return whether a type contains any opaque types.
942 */
943 bool contains_opaque() const;
944
945 /**
946 * Query the full type of a matrix row
947 *
948 * \return
949 * If the type is not a matrix, \c glsl_type::error_type is returned.
950 * Otherwise a type matching the rows of the matrix is returned.
951 */
952 const glsl_type *row_type() const
953 {
954 if (!is_matrix())
955 return error_type;
956
957 if (explicit_stride && !interface_row_major)
958 return get_instance(base_type, matrix_columns, 1, explicit_stride);
959 else
960 return get_instance(base_type, matrix_columns, 1);
961 }
962
963 /**
964 * Query the full type of a matrix column
965 *
966 * \return
967 * If the type is not a matrix, \c glsl_type::error_type is returned.
968 * Otherwise a type matching the columns of the matrix is returned.
969 */
970 const glsl_type *column_type() const
971 {
972 if (!is_matrix())
973 return error_type;
974
975 if (explicit_stride && interface_row_major)
976 return get_instance(base_type, vector_elements, 1, explicit_stride);
977 else
978 return get_instance(base_type, vector_elements, 1);
979 }
980
981 /**
982 * Get the type of a structure field
983 *
984 * \return
985 * Pointer to the type of the named field. If the type is not a structure
986 * or the named field does not exist, \c glsl_type::error_type is returned.
987 */
988 const glsl_type *field_type(const char *name) const;
989
990 /**
991 * Get the location of a field within a record type
992 */
993 int field_index(const char *name) const;
994
995 /**
996 * Query the number of elements in an array type
997 *
998 * \return
999 * The number of elements in the array for array types or -1 for non-array
1000 * types. If the number of elements in the array has not yet been declared,
1001 * zero is returned.
1002 */
1003 int array_size() const
1004 {
1005 return is_array() ? length : -1;
1006 }
1007
1008 /**
1009 * Query whether the array size for all dimensions has been declared.
1010 */
1011 bool is_unsized_array() const
1012 {
1013 return is_array() && length == 0;
1014 }
1015
1016 /**
1017 * Return the number of coordinate components needed for this
1018 * sampler or image type.
1019 *
1020 * This is based purely on the sampler's dimensionality. For example, this
1021 * returns 1 for sampler1D, and 3 for sampler2DArray.
1022 *
1023 * Note that this is often different than actual coordinate type used in
1024 * a texturing built-in function, since those pack additional values (such
1025 * as the shadow comparator or projector) into the coordinate type.
1026 */
1027 int coordinate_components() const;
1028
1029 /**
1030 * Compares whether this type matches another type without taking into
1031 * account the precision in structures.
1032 *
1033 * This is applied recursively so that structures containing structure
1034 * members can also ignore the precision.
1035 */
1036 bool compare_no_precision(const glsl_type *b) const;
1037
1038 /**
1039 * Compare a record type against another record type.
1040 *
1041 * This is useful for matching record types declared on the same shader
1042 * stage as well as across different shader stages.
1043 * The option to not match name is needed for matching record types
1044 * declared across different shader stages.
1045 * The option to not match locations is to deal with places where the
1046 * same struct is defined in a block which has a location set on it.
1047 */
1048 bool record_compare(const glsl_type *b, bool match_name,
1049 bool match_locations = true,
1050 bool match_precision = true) const;
1051
1052 /**
1053 * Get the type interface packing.
1054 */
1055 enum glsl_interface_packing get_interface_packing() const
1056 {
1057 return (enum glsl_interface_packing)interface_packing;
1058 }
1059
1060 /**
1061 * Get the type interface packing used internally. For shared and packing
1062 * layouts this is implementation defined.
1063 */
1064 enum glsl_interface_packing get_internal_ifc_packing(bool std430_supported) const
1065 {
1066 enum glsl_interface_packing packing = this->get_interface_packing();
1067 if (packing == GLSL_INTERFACE_PACKING_STD140 ||
1068 (!std430_supported &&
1069 (packing == GLSL_INTERFACE_PACKING_SHARED ||
1070 packing == GLSL_INTERFACE_PACKING_PACKED))) {
1071 return GLSL_INTERFACE_PACKING_STD140;
1072 } else {
1073 assert(packing == GLSL_INTERFACE_PACKING_STD430 ||
1074 (std430_supported &&
1075 (packing == GLSL_INTERFACE_PACKING_SHARED ||
1076 packing == GLSL_INTERFACE_PACKING_PACKED)));
1077 return GLSL_INTERFACE_PACKING_STD430;
1078 }
1079 }
1080
1081 /**
1082 * Check if the type interface is row major
1083 */
1084 bool get_interface_row_major() const
1085 {
1086 return (bool) interface_row_major;
1087 }
1088
1089 ~glsl_type();
1090
1091 private:
1092
1093 static mtx_t hash_mutex;
1094
1095 /**
1096 * ralloc context for the type itself.
1097 */
1098 void *mem_ctx;
1099
1100 /** Constructor for vector and matrix types */
1101 glsl_type(GLenum gl_type,
1102 glsl_base_type base_type, unsigned vector_elements,
1103 unsigned matrix_columns, const char *name,
1104 unsigned explicit_stride = 0, bool row_major = false);
1105
1106 /** Constructor for sampler or image types */
1107 glsl_type(GLenum gl_type, glsl_base_type base_type,
1108 enum glsl_sampler_dim dim, bool shadow, bool array,
1109 glsl_base_type type, const char *name);
1110
1111 /** Constructor for record types */
1112 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
1113 const char *name, bool packed = false);
1114
1115 /** Constructor for interface types */
1116 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
1117 enum glsl_interface_packing packing,
1118 bool row_major, const char *name);
1119
1120 /** Constructor for interface types */
1121 glsl_type(const glsl_type *return_type,
1122 const glsl_function_param *params, unsigned num_params);
1123
1124 /** Constructors for array types */
1125 glsl_type(const glsl_type *array, unsigned length, unsigned explicit_stride);
1126
1127 /** Constructor for subroutine types */
1128 glsl_type(const char *name);
1129
1130 /** Hash table containing the known explicit matrix and vector types. */
1131 static struct hash_table *explicit_matrix_types;
1132
1133 /** Hash table containing the known array types. */
1134 static struct hash_table *array_types;
1135
1136 /** Hash table containing the known struct types. */
1137 static struct hash_table *struct_types;
1138
1139 /** Hash table containing the known interface types. */
1140 static struct hash_table *interface_types;
1141
1142 /** Hash table containing the known subroutine types. */
1143 static struct hash_table *subroutine_types;
1144
1145 /** Hash table containing the known function types. */
1146 static struct hash_table *function_types;
1147
1148 static bool record_key_compare(const void *a, const void *b);
1149 static unsigned record_key_hash(const void *key);
1150
1151 /**
1152 * \name Built-in type flyweights
1153 */
1154 /*@{*/
1155 #undef DECL_TYPE
1156 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
1157 #undef STRUCT_TYPE
1158 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
1159 #include "compiler/builtin_type_macros.h"
1160 /*@}*/
1161
1162 /**
1163 * \name Friend functions.
1164 *
1165 * These functions are friends because they must have C linkage and the
1166 * need to call various private methods or access various private static
1167 * data.
1168 */
1169 /*@{*/
1170 friend void glsl_type_singleton_init_or_ref(void);
1171 friend void glsl_type_singleton_decref(void);
1172 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
1173 /*@}*/
1174 };
1175
1176 #undef DECL_TYPE
1177 #undef STRUCT_TYPE
1178 #endif /* __cplusplus */
1179
1180 struct glsl_struct_field {
1181 const struct glsl_type *type;
1182 const char *name;
1183
1184 /**
1185 * For interface blocks, gl_varying_slot corresponding to the input/output
1186 * if this is a built-in input/output (i.e. a member of the built-in
1187 * gl_PerVertex interface block); -1 otherwise.
1188 *
1189 * Ignored for structs.
1190 */
1191 int location;
1192
1193 /**
1194 * For interface blocks, members may have an explicit byte offset
1195 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1196 *
1197 * Unless used for xfb_offset this field is ignored for structs.
1198 */
1199 int offset;
1200
1201 /**
1202 * For interface blocks, members may define a transform feedback buffer;
1203 * -1 otherwise.
1204 */
1205 int xfb_buffer;
1206
1207 /**
1208 * For interface blocks, members may define a transform feedback stride;
1209 * -1 otherwise.
1210 */
1211 int xfb_stride;
1212
1213 /**
1214 * For interface blocks, the interpolation mode (as in
1215 * ir_variable::interpolation). 0 otherwise.
1216 */
1217 unsigned interpolation:2;
1218
1219 /**
1220 * For interface blocks, 1 if this variable uses centroid interpolation (as
1221 * in ir_variable::centroid). 0 otherwise.
1222 */
1223 unsigned centroid:1;
1224
1225 /**
1226 * For interface blocks, 1 if this variable uses sample interpolation (as
1227 * in ir_variable::sample). 0 otherwise.
1228 */
1229 unsigned sample:1;
1230
1231 /**
1232 * Layout of the matrix. Uses glsl_matrix_layout values.
1233 */
1234 unsigned matrix_layout:2;
1235
1236 /**
1237 * For interface blocks, 1 if this variable is a per-patch input or output
1238 * (as in ir_variable::patch). 0 otherwise.
1239 */
1240 unsigned patch:1;
1241
1242 /**
1243 * Precision qualifier
1244 */
1245 unsigned precision:2;
1246
1247 /**
1248 * Memory qualifiers, applicable to buffer variables defined in shader
1249 * storage buffer objects (SSBOs)
1250 */
1251 unsigned memory_read_only:1;
1252 unsigned memory_write_only:1;
1253 unsigned memory_coherent:1;
1254 unsigned memory_volatile:1;
1255 unsigned memory_restrict:1;
1256
1257 /**
1258 * Layout format, applicable to image variables only.
1259 */
1260 unsigned image_format:16;
1261
1262 /**
1263 * Any of the xfb_* qualifiers trigger the shader to be in transform
1264 * feedback mode so we need to keep track of whether the buffer was
1265 * explicitly set or if its just been assigned the default global value.
1266 */
1267 unsigned explicit_xfb_buffer:1;
1268
1269 unsigned implicit_sized_array:1;
1270 #ifdef __cplusplus
1271 #define DEFAULT_CONSTRUCTORS(_type, _precision, _name) \
1272 type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0), \
1273 xfb_stride(0), interpolation(0), centroid(0), \
1274 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0), \
1275 precision(_precision), memory_read_only(0), \
1276 memory_write_only(0), memory_coherent(0), memory_volatile(0), \
1277 memory_restrict(0), image_format(0), explicit_xfb_buffer(0), \
1278 implicit_sized_array(0)
1279
1280 glsl_struct_field(const struct glsl_type *_type,
1281 int _precision,
1282 const char *_name)
1283 : DEFAULT_CONSTRUCTORS(_type, _precision, _name)
1284 {
1285 /* empty */
1286 }
1287
1288 glsl_struct_field(const struct glsl_type *_type, const char *_name)
1289 : DEFAULT_CONSTRUCTORS(_type, GLSL_PRECISION_NONE, _name)
1290 {
1291 /* empty */
1292 }
1293
1294 glsl_struct_field()
1295 : DEFAULT_CONSTRUCTORS(NULL, GLSL_PRECISION_NONE, NULL)
1296 {
1297 /* empty */
1298 }
1299 #undef DEFAULT_CONSTRUCTORS
1300 #endif
1301 };
1302
1303 struct glsl_function_param {
1304 const struct glsl_type *type;
1305
1306 bool in;
1307 bool out;
1308 };
1309
1310 static inline unsigned int
1311 glsl_align(unsigned int a, unsigned int align)
1312 {
1313 return (a + align - 1) / align * align;
1314 }
1315
1316 #endif /* GLSL_TYPES_H */