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