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