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