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