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