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