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 "mesa/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 "mesa/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 unsigned component_slots_aligned(unsigned offset) const; 525 526 /** 527 * Calculate offset between the base location of the struct in 528 * uniform storage and a struct member. 529 * For the initial call, length is the index of the member to find the 530 * offset for. 531 */ 532 unsigned struct_location_offset(unsigned length) const; 533 534 /** 535 * Calculate the number of unique values from glGetUniformLocation for the 536 * elements of the type. 537 * 538 * This is used to allocate slots in the UniformRemapTable, the amount of 539 * locations may not match with actual used storage space by the driver. 540 */ 541 unsigned uniform_locations() const; 542 543 /** 544 * Used to count the number of varyings contained in the type ignoring 545 * innermost array elements. 546 */ 547 unsigned varying_count() const; 548 549 /** 550 * Calculate the number of vec4 slots required to hold this type. 551 * 552 * This is the underlying recursive type_size function for 553 * count_attribute_slots() (vertex inputs and varyings) but also for 554 * gallium's !PIPE_CAP_PACKED_UNIFORMS case. 555 */ 556 unsigned count_vec4_slots(bool is_gl_vertex_input, bool bindless) const; 557 558 /** 559 * Calculate the number of vec4 slots required to hold this type. 560 * 561 * This is the underlying recursive type_size function for 562 * gallium's PIPE_CAP_PACKED_UNIFORMS case. 563 */ 564 unsigned count_dword_slots(bool bindless) const; 565 566 /** 567 * Calculate the number of attribute slots required to hold this type 568 * 569 * This implements the language rules of GLSL 1.50 for counting the number 570 * of slots used by a vertex attribute. It also determines the number of 571 * varying slots the type will use up in the absence of varying packing 572 * (and thus, it can be used to measure the number of varying slots used by 573 * the varyings that are generated by lower_packed_varyings). 574 * 575 * For vertex shader attributes - doubles only take one slot. 576 * For inter-shader varyings - dvec3/dvec4 take two slots. 577 * 578 * Vulkan doesn’t make this distinction so the argument should always be 579 * false. 580 */ count_attribute_slotsglsl_type581 unsigned count_attribute_slots(bool is_gl_vertex_input) const { 582 return count_vec4_slots(is_gl_vertex_input, true); 583 } 584 585 /** 586 * Alignment in bytes of the start of this type in a std140 uniform 587 * block. 588 */ 589 unsigned std140_base_alignment(bool row_major) const; 590 591 /** Size in bytes of this type in a std140 uniform block. 592 * 593 * Note that this is not GL_UNIFORM_SIZE (which is the number of 594 * elements in the array) 595 */ 596 unsigned std140_size(bool row_major) const; 597 598 /** 599 * Gets an explicitly laid out type with the std140 layout. 600 */ 601 const glsl_type *get_explicit_std140_type(bool row_major) const; 602 603 /** 604 * Alignment in bytes of the start of this type in a std430 shader 605 * storage block. 606 */ 607 unsigned std430_base_alignment(bool row_major) const; 608 609 /** 610 * Calculate array stride in bytes of this type in a std430 shader storage 611 * block. 612 */ 613 unsigned std430_array_stride(bool row_major) const; 614 615 /** 616 * Size in bytes of this type in a std430 shader storage block. 617 * 618 * Note that this is not GL_BUFFER_SIZE 619 */ 620 unsigned std430_size(bool row_major) const; 621 622 /** 623 * Gets an explicitly laid out type with the std430 layout. 624 */ 625 const glsl_type *get_explicit_std430_type(bool row_major) const; 626 627 /** 628 * Gets an explicitly laid out interface type. 629 */ 630 const glsl_type *get_explicit_interface_type(bool supports_std430) const; 631 632 /** Returns an explicitly laid out type given a type and size/align func 633 * 634 * The size/align func is only called for scalar and vector types and the 635 * returned type is otherwise laid out in the natural way as follows: 636 * 637 * - Arrays and matrices have a stride of ALIGN(elem_size, elem_align). 638 * 639 * - Structure types have their elements in-order and as tightly packed as 640 * possible following the alignment required by the size/align func. 641 * 642 * - All composite types (structures, matrices, and arrays) have an 643 * alignment equal to the highest alignment of any member of the composite. 644 * 645 * The types returned by this function are likely not suitable for most UBO 646 * or SSBO layout because they do not add the extra array and substructure 647 * alignment that is required by std140 and std430. 648 */ 649 const glsl_type *get_explicit_type_for_size_align(glsl_type_size_align_func type_info, 650 unsigned *size, unsigned *align) const; 651 652 const glsl_type *replace_vec3_with_vec4() const; 653 654 /** 655 * Alignment in bytes of the start of this type in OpenCL memory. 656 */ 657 unsigned cl_alignment() const; 658 659 /** 660 * Size in bytes of this type in OpenCL memory 661 */ 662 unsigned cl_size() const; 663 664 /** 665 * Size in bytes of this type based on its explicit data. 666 * 667 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed 668 * through explicit offset, stride and matrix layout, so the size 669 * can/should be computed used those values. 670 * 671 * Note that the value returned by this method is only correct if such 672 * values are set, so only with SPIR-V shaders. Should not be used with 673 * GLSL shaders. 674 */ 675 unsigned explicit_size(bool align_to_stride=false) const; 676 677 /** 678 * \brief Can this type be implicitly converted to another? 679 * 680 * \return True if the types are identical or if this type can be converted 681 * to \c desired according to Section 4.1.10 of the GLSL spec. 682 * 683 * \verbatim 684 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10 685 * Implicit Conversions: 686 * 687 * In some situations, an expression and its type will be implicitly 688 * converted to a different type. The following table shows all allowed 689 * implicit conversions: 690 * 691 * Type of expression | Can be implicitly converted to 692 * -------------------------------------------------- 693 * int float 694 * uint 695 * 696 * ivec2 vec2 697 * uvec2 698 * 699 * ivec3 vec3 700 * uvec3 701 * 702 * ivec4 vec4 703 * uvec4 704 * 705 * There are no implicit array or structure conversions. For example, 706 * an array of int cannot be implicitly converted to an array of float. 707 * There are no implicit conversions between signed and unsigned 708 * integers. 709 * \endverbatim 710 */ 711 bool can_implicitly_convert_to(const glsl_type *desired, 712 _mesa_glsl_parse_state *state) const; 713 714 /** 715 * Query whether or not a type is a scalar (non-vector and non-matrix). 716 */ is_scalarglsl_type717 bool is_scalar() const 718 { 719 return (vector_elements == 1) 720 && (base_type >= GLSL_TYPE_UINT) 721 && (base_type <= GLSL_TYPE_IMAGE); 722 } 723 724 /** 725 * Query whether or not a type is a vector 726 */ is_vectorglsl_type727 bool is_vector() const 728 { 729 return (vector_elements > 1) 730 && (matrix_columns == 1) 731 && (base_type >= GLSL_TYPE_UINT) 732 && (base_type <= GLSL_TYPE_BOOL); 733 } 734 735 /** 736 * Query whether or not a type is a matrix 737 */ is_matrixglsl_type738 bool is_matrix() const 739 { 740 /* GLSL only has float matrices. */ 741 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT || 742 base_type == GLSL_TYPE_DOUBLE || 743 base_type == GLSL_TYPE_FLOAT16); 744 } 745 746 /** 747 * Query whether or not a type is a non-array numeric type 748 */ is_numericglsl_type749 bool is_numeric() const 750 { 751 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_INT64); 752 } 753 754 /** 755 * Query whether or not a type is an integer. 756 */ is_integerglsl_type757 bool is_integer() const 758 { 759 return glsl_base_type_is_integer(base_type); 760 } 761 762 /** 763 * Query whether or not a type is a 16-bit integer. 764 */ is_integer_16glsl_type765 bool is_integer_16() const 766 { 767 return base_type == GLSL_TYPE_UINT16 || base_type == GLSL_TYPE_INT16; 768 } 769 770 /** 771 * Query whether or not a type is an 32-bit integer. 772 */ is_integer_32glsl_type773 bool is_integer_32() const 774 { 775 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT); 776 } 777 778 /** 779 * Query whether or not a type is a 64-bit integer. 780 */ is_integer_64glsl_type781 bool is_integer_64() const 782 { 783 return base_type == GLSL_TYPE_UINT64 || base_type == GLSL_TYPE_INT64; 784 } 785 786 /** 787 * Query whether or not a type is a 32-bit or 64-bit integer 788 */ is_integer_32_64glsl_type789 bool is_integer_32_64() const 790 { 791 return is_integer_32() || is_integer_64(); 792 } 793 794 /** 795 * Query whether or not a type is a 16-bit or 32-bit integer 796 */ is_integer_16_32glsl_type797 bool is_integer_16_32() const 798 { 799 return is_integer_16() || is_integer_32(); 800 } 801 802 /** 803 * Query whether or not a type is a 16-bit, 32-bit or 64-bit integer 804 */ is_integer_16_32_64glsl_type805 bool is_integer_16_32_64() const 806 { 807 return is_integer_16() || is_integer_32() || is_integer_64(); 808 } 809 810 /** 811 * Query whether or not type is an integral type, or for struct and array 812 * types, contains an integral type. 813 */ 814 bool contains_integer() const; 815 816 /** 817 * Query whether or not type is a double type, or for struct, interface and 818 * array types, contains a double type. 819 */ 820 bool contains_double() const; 821 822 /** 823 * Query whether or not type is a 64-bit type, or for struct, interface and 824 * array types, contains a double type. 825 */ 826 bool contains_64bit() const; 827 828 /** 829 * Query whether or not a type is a float type 830 */ is_floatglsl_type831 bool is_float() const 832 { 833 return base_type == GLSL_TYPE_FLOAT; 834 } 835 836 /** 837 * Query whether or not a type is a half-float or float type 838 */ is_float_16_32glsl_type839 bool is_float_16_32() const 840 { 841 return base_type == GLSL_TYPE_FLOAT16 || is_float(); 842 } 843 844 /** 845 * Query whether or not a type is a half-float, float or double 846 */ is_float_16_32_64glsl_type847 bool is_float_16_32_64() const 848 { 849 return base_type == GLSL_TYPE_FLOAT16 || is_float() || is_double(); 850 } 851 852 /** 853 * Query whether or not a type is a float or double 854 */ is_float_32_64glsl_type855 bool is_float_32_64() const 856 { 857 return is_float() || is_double(); 858 } 859 is_int_16_32_64glsl_type860 bool is_int_16_32_64() const 861 { 862 return base_type == GLSL_TYPE_INT16 || 863 base_type == GLSL_TYPE_INT || 864 base_type == GLSL_TYPE_INT64; 865 } 866 is_uint_16_32_64glsl_type867 bool is_uint_16_32_64() const 868 { 869 return base_type == GLSL_TYPE_UINT16 || 870 base_type == GLSL_TYPE_UINT || 871 base_type == GLSL_TYPE_UINT64; 872 } 873 is_int_16_32glsl_type874 bool is_int_16_32() const 875 { 876 return base_type == GLSL_TYPE_INT || 877 base_type == GLSL_TYPE_INT16; 878 } 879 is_uint_16_32glsl_type880 bool is_uint_16_32() const 881 { 882 return base_type == GLSL_TYPE_UINT || 883 base_type == GLSL_TYPE_UINT16; 884 } 885 886 /** 887 * Query whether or not a type is a double type 888 */ is_doubleglsl_type889 bool is_double() const 890 { 891 return base_type == GLSL_TYPE_DOUBLE; 892 } 893 894 /** 895 * Query whether a 64-bit type takes two slots. 896 */ is_dual_slotglsl_type897 bool is_dual_slot() const 898 { 899 return is_64bit() && vector_elements > 2; 900 } 901 902 /** 903 * Query whether or not a type is 64-bit 904 */ is_64bitglsl_type905 bool is_64bit() const 906 { 907 return glsl_base_type_is_64bit(base_type); 908 } 909 910 /** 911 * Query whether or not a type is 16-bit 912 */ is_16bitglsl_type913 bool is_16bit() const 914 { 915 return glsl_base_type_is_16bit(base_type); 916 } 917 918 /** 919 * Query whether or not a type is 32-bit 920 */ is_32bitglsl_type921 bool is_32bit() const 922 { 923 return base_type == GLSL_TYPE_UINT || 924 base_type == GLSL_TYPE_INT || 925 base_type == GLSL_TYPE_FLOAT; 926 } 927 928 /** 929 * Query whether or not a type is a non-array boolean type 930 */ is_booleanglsl_type931 bool is_boolean() const 932 { 933 return base_type == GLSL_TYPE_BOOL; 934 } 935 936 /** 937 * Query whether or not a type is a sampler 938 */ is_samplerglsl_type939 bool is_sampler() const 940 { 941 return base_type == GLSL_TYPE_SAMPLER; 942 } 943 944 /** 945 * Query whether or not type is a sampler, or for struct, interface and 946 * array types, contains a sampler. 947 */ 948 bool contains_sampler() const; 949 950 /** 951 * Query whether or not type is an array or for struct, interface and 952 * array types, contains an array. 953 */ 954 bool contains_array() const; 955 956 /** 957 * Get the Mesa texture target index for a sampler type. 958 */ 959 gl_texture_index sampler_index() const; 960 961 /** 962 * Query whether or not type is an image, or for struct, interface and 963 * array types, contains an image. 964 */ 965 bool contains_image() const; 966 967 /** 968 * Query whether or not a type is an image 969 */ is_imageglsl_type970 bool is_image() const 971 { 972 return base_type == GLSL_TYPE_IMAGE; 973 } 974 975 /** 976 * Query whether or not a type is an array 977 */ is_arrayglsl_type978 bool is_array() const 979 { 980 return base_type == GLSL_TYPE_ARRAY; 981 } 982 is_array_of_arraysglsl_type983 bool is_array_of_arrays() const 984 { 985 return is_array() && fields.array->is_array(); 986 } 987 988 /** 989 * Query whether or not a type is a record 990 */ is_structglsl_type991 bool is_struct() const 992 { 993 return base_type == GLSL_TYPE_STRUCT; 994 } 995 996 /** 997 * Query whether or not a type is an interface 998 */ is_interfaceglsl_type999 bool is_interface() const 1000 { 1001 return base_type == GLSL_TYPE_INTERFACE; 1002 } 1003 1004 /** 1005 * Query whether or not a type is the void type singleton. 1006 */ is_voidglsl_type1007 bool is_void() const 1008 { 1009 return base_type == GLSL_TYPE_VOID; 1010 } 1011 1012 /** 1013 * Query whether or not a type is the error type singleton. 1014 */ is_errorglsl_type1015 bool is_error() const 1016 { 1017 return base_type == GLSL_TYPE_ERROR; 1018 } 1019 1020 /** 1021 * Query if a type is unnamed/anonymous (named by the parser) 1022 */ 1023 is_subroutineglsl_type1024 bool is_subroutine() const 1025 { 1026 return base_type == GLSL_TYPE_SUBROUTINE; 1027 } 1028 bool contains_subroutine() const; 1029 is_anonymousglsl_type1030 bool is_anonymous() const 1031 { 1032 return !strncmp(name, "#anon", 5); 1033 } 1034 1035 /** 1036 * Get the type stripped of any arrays 1037 * 1038 * \return 1039 * Pointer to the type of elements of the first non-array type for array 1040 * types, or pointer to itself for non-array types. 1041 */ without_arrayglsl_type1042 const glsl_type *without_array() const 1043 { 1044 const glsl_type *t = this; 1045 1046 while (t->is_array()) 1047 t = t->fields.array; 1048 1049 return t; 1050 } 1051 1052 /** 1053 * Return the total number of elements in an array including the elements 1054 * in arrays of arrays. 1055 */ arrays_of_arrays_sizeglsl_type1056 unsigned arrays_of_arrays_size() const 1057 { 1058 if (!is_array()) 1059 return 0; 1060 1061 unsigned size = length; 1062 const glsl_type *array_base_type = fields.array; 1063 1064 while (array_base_type->is_array()) { 1065 size = size * array_base_type->length; 1066 array_base_type = array_base_type->fields.array; 1067 } 1068 return size; 1069 } 1070 1071 /** 1072 * Return bit size for this type. 1073 */ bit_sizeglsl_type1074 unsigned bit_size() const 1075 { 1076 return glsl_base_type_bit_size(this->base_type); 1077 } 1078 1079 1080 /** 1081 * Query whether or not a type is an atomic_uint. 1082 */ is_atomic_uintglsl_type1083 bool is_atomic_uint() const 1084 { 1085 return base_type == GLSL_TYPE_ATOMIC_UINT; 1086 } 1087 1088 /** 1089 * Return the amount of atomic counter storage required for a type. 1090 */ atomic_sizeglsl_type1091 unsigned atomic_size() const 1092 { 1093 if (is_atomic_uint()) 1094 return ATOMIC_COUNTER_SIZE; 1095 else if (is_array()) 1096 return length * fields.array->atomic_size(); 1097 else 1098 return 0; 1099 } 1100 1101 /** 1102 * Return whether a type contains any atomic counters. 1103 */ contains_atomicglsl_type1104 bool contains_atomic() const 1105 { 1106 return atomic_size() > 0; 1107 } 1108 1109 /** 1110 * Return whether a type contains any opaque types. 1111 */ 1112 bool contains_opaque() const; 1113 1114 /** 1115 * Query the full type of a matrix row 1116 * 1117 * \return 1118 * If the type is not a matrix, \c glsl_type::error_type is returned. 1119 * Otherwise a type matching the rows of the matrix is returned. 1120 */ row_typeglsl_type1121 const glsl_type *row_type() const 1122 { 1123 if (!is_matrix()) 1124 return error_type; 1125 1126 if (explicit_stride && !interface_row_major) 1127 return get_instance(base_type, matrix_columns, 1, explicit_stride); 1128 else 1129 return get_instance(base_type, matrix_columns, 1); 1130 } 1131 1132 /** 1133 * Query the full type of a matrix column 1134 * 1135 * \return 1136 * If the type is not a matrix, \c glsl_type::error_type is returned. 1137 * Otherwise a type matching the columns of the matrix is returned. 1138 */ column_typeglsl_type1139 const glsl_type *column_type() const 1140 { 1141 if (!is_matrix()) 1142 return error_type; 1143 1144 if (interface_row_major) { 1145 /* If we're row-major, the vector element stride is the same as the 1146 * matrix stride and we have no alignment (i.e. component-aligned). 1147 */ 1148 return get_instance(base_type, vector_elements, 1, 1149 explicit_stride, false, 0); 1150 } else { 1151 /* Otherwise, the vector is tightly packed (stride=0). For 1152 * alignment, we treat a matrix as an array of columns make the same 1153 * assumption that the alignment of the column is the same as the 1154 * alignment of the whole matrix. 1155 */ 1156 return get_instance(base_type, vector_elements, 1, 1157 0, false, explicit_alignment); 1158 } 1159 } 1160 1161 /** 1162 * Get the type of a structure field 1163 * 1164 * \return 1165 * Pointer to the type of the named field. If the type is not a structure 1166 * or the named field does not exist, \c glsl_type::error_type is returned. 1167 */ 1168 const glsl_type *field_type(const char *name) const; 1169 1170 /** 1171 * Get the location of a field within a record type 1172 */ 1173 int field_index(const char *name) const; 1174 1175 /** 1176 * Query the number of elements in an array type 1177 * 1178 * \return 1179 * The number of elements in the array for array types or -1 for non-array 1180 * types. If the number of elements in the array has not yet been declared, 1181 * zero is returned. 1182 */ array_sizeglsl_type1183 int array_size() const 1184 { 1185 return is_array() ? length : -1; 1186 } 1187 1188 /** 1189 * Query whether the array size for all dimensions has been declared. 1190 */ is_unsized_arrayglsl_type1191 bool is_unsized_array() const 1192 { 1193 return is_array() && length == 0; 1194 } 1195 1196 /** 1197 * Return the number of coordinate components needed for this 1198 * sampler or image type. 1199 * 1200 * This is based purely on the sampler's dimensionality. For example, this 1201 * returns 1 for sampler1D, and 3 for sampler2DArray. 1202 * 1203 * Note that this is often different than actual coordinate type used in 1204 * a texturing built-in function, since those pack additional values (such 1205 * as the shadow comparator or projector) into the coordinate type. 1206 */ 1207 int coordinate_components() const; 1208 1209 /** 1210 * Compares whether this type matches another type without taking into 1211 * account the precision in structures. 1212 * 1213 * This is applied recursively so that structures containing structure 1214 * members can also ignore the precision. 1215 */ 1216 bool compare_no_precision(const glsl_type *b) const; 1217 1218 /** 1219 * Compare a record type against another record type. 1220 * 1221 * This is useful for matching record types declared on the same shader 1222 * stage as well as across different shader stages. 1223 * The option to not match name is needed for matching record types 1224 * declared across different shader stages. 1225 * The option to not match locations is to deal with places where the 1226 * same struct is defined in a block which has a location set on it. 1227 */ 1228 bool record_compare(const glsl_type *b, bool match_name, 1229 bool match_locations = true, 1230 bool match_precision = true) const; 1231 1232 /** 1233 * Get the type interface packing. 1234 */ get_interface_packingglsl_type1235 enum glsl_interface_packing get_interface_packing() const 1236 { 1237 return (enum glsl_interface_packing)interface_packing; 1238 } 1239 1240 /** 1241 * Get the type interface packing used internally. For shared and packing 1242 * layouts this is implementation defined. 1243 */ get_internal_ifc_packingglsl_type1244 enum glsl_interface_packing get_internal_ifc_packing(bool std430_supported) const 1245 { 1246 enum glsl_interface_packing packing = this->get_interface_packing(); 1247 if (packing == GLSL_INTERFACE_PACKING_STD140 || 1248 (!std430_supported && 1249 (packing == GLSL_INTERFACE_PACKING_SHARED || 1250 packing == GLSL_INTERFACE_PACKING_PACKED))) { 1251 return GLSL_INTERFACE_PACKING_STD140; 1252 } else { 1253 assert(packing == GLSL_INTERFACE_PACKING_STD430 || 1254 (std430_supported && 1255 (packing == GLSL_INTERFACE_PACKING_SHARED || 1256 packing == GLSL_INTERFACE_PACKING_PACKED))); 1257 return GLSL_INTERFACE_PACKING_STD430; 1258 } 1259 } 1260 1261 /** 1262 * Check if the type interface is row major 1263 */ get_interface_row_majorglsl_type1264 bool get_interface_row_major() const 1265 { 1266 return (bool) interface_row_major; 1267 } 1268 1269 ~glsl_type(); 1270 1271 private: 1272 1273 static mtx_t hash_mutex; 1274 1275 /** 1276 * ralloc context for the type itself. 1277 */ 1278 void *mem_ctx; 1279 1280 /** Constructor for vector and matrix types */ 1281 glsl_type(GLenum gl_type, 1282 glsl_base_type base_type, unsigned vector_elements, 1283 unsigned matrix_columns, const char *name, 1284 unsigned explicit_stride = 0, bool row_major = false, 1285 unsigned explicit_alignment = 0); 1286 1287 /** Constructor for sampler or image types */ 1288 glsl_type(GLenum gl_type, glsl_base_type base_type, 1289 enum glsl_sampler_dim dim, bool shadow, bool array, 1290 glsl_base_type type, const char *name); 1291 1292 /** Constructor for record types */ 1293 glsl_type(const glsl_struct_field *fields, unsigned num_fields, 1294 const char *name, bool packed = false, 1295 unsigned explicit_alignment = 0); 1296 1297 /** Constructor for interface types */ 1298 glsl_type(const glsl_struct_field *fields, unsigned num_fields, 1299 enum glsl_interface_packing packing, 1300 bool row_major, const char *name); 1301 1302 /** Constructor for interface types */ 1303 glsl_type(const glsl_type *return_type, 1304 const glsl_function_param *params, unsigned num_params); 1305 1306 /** Constructors for array types */ 1307 glsl_type(const glsl_type *array, unsigned length, unsigned explicit_stride); 1308 1309 /** Constructor for subroutine types */ 1310 glsl_type(const char *name); 1311 1312 /** Hash table containing the known explicit matrix and vector types. */ 1313 static struct hash_table *explicit_matrix_types; 1314 1315 /** Hash table containing the known array types. */ 1316 static struct hash_table *array_types; 1317 1318 /** Hash table containing the known struct types. */ 1319 static struct hash_table *struct_types; 1320 1321 /** Hash table containing the known interface types. */ 1322 static struct hash_table *interface_types; 1323 1324 /** Hash table containing the known subroutine types. */ 1325 static struct hash_table *subroutine_types; 1326 1327 /** Hash table containing the known function types. */ 1328 static struct hash_table *function_types; 1329 1330 static bool record_key_compare(const void *a, const void *b); 1331 static unsigned record_key_hash(const void *key); 1332 1333 /** 1334 * \name Built-in type flyweights 1335 */ 1336 /*@{*/ 1337 #undef DECL_TYPE 1338 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type; 1339 #undef STRUCT_TYPE 1340 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type; 1341 #include "compiler/builtin_type_macros.h" 1342 /*@}*/ 1343 1344 /** 1345 * \name Friend functions. 1346 * 1347 * These functions are friends because they must have C linkage and the 1348 * need to call various private methods or access various private static 1349 * data. 1350 */ 1351 /*@{*/ 1352 friend void glsl_type_singleton_init_or_ref(void); 1353 friend void glsl_type_singleton_decref(void); 1354 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *); 1355 /*@}*/ 1356 }; 1357 1358 #undef DECL_TYPE 1359 #undef STRUCT_TYPE 1360 #endif /* __cplusplus */ 1361 1362 struct glsl_struct_field { 1363 const struct glsl_type *type; 1364 const char *name; 1365 1366 /** 1367 * For interface blocks, gl_varying_slot corresponding to the input/output 1368 * if this is a built-in input/output (i.e. a member of the built-in 1369 * gl_PerVertex interface block); -1 otherwise. 1370 * 1371 * Ignored for structs. 1372 */ 1373 int location; 1374 1375 /** 1376 * For interface blocks, members may explicitly assign the component used 1377 * by a varying. Ignored for structs. 1378 */ 1379 int component; 1380 1381 /** 1382 * For interface blocks, members may have an explicit byte offset 1383 * specified; -1 otherwise. Also used for xfb_offset layout qualifier. 1384 * 1385 * Unless used for xfb_offset this field is ignored for structs. 1386 */ 1387 int offset; 1388 1389 /** 1390 * For interface blocks, members may define a transform feedback buffer; 1391 * -1 otherwise. 1392 */ 1393 int xfb_buffer; 1394 1395 /** 1396 * For interface blocks, members may define a transform feedback stride; 1397 * -1 otherwise. 1398 */ 1399 int xfb_stride; 1400 1401 /** 1402 * Layout format, applicable to image variables only. 1403 */ 1404 enum pipe_format image_format; 1405 1406 union { 1407 struct { 1408 /** 1409 * For interface blocks, the interpolation mode (as in 1410 * ir_variable::interpolation). 0 otherwise. 1411 */ 1412 unsigned interpolation:3; 1413 1414 /** 1415 * For interface blocks, 1 if this variable uses centroid interpolation (as 1416 * in ir_variable::centroid). 0 otherwise. 1417 */ 1418 unsigned centroid:1; 1419 1420 /** 1421 * For interface blocks, 1 if this variable uses sample interpolation (as 1422 * in ir_variable::sample). 0 otherwise. 1423 */ 1424 unsigned sample:1; 1425 1426 /** 1427 * Layout of the matrix. Uses glsl_matrix_layout values. 1428 */ 1429 unsigned matrix_layout:2; 1430 1431 /** 1432 * For interface blocks, 1 if this variable is a per-patch input or output 1433 * (as in ir_variable::patch). 0 otherwise. 1434 */ 1435 unsigned patch:1; 1436 1437 /** 1438 * Precision qualifier 1439 */ 1440 unsigned precision:2; 1441 1442 /** 1443 * Memory qualifiers, applicable to buffer variables defined in shader 1444 * storage buffer objects (SSBOs) 1445 */ 1446 unsigned memory_read_only:1; 1447 unsigned memory_write_only:1; 1448 unsigned memory_coherent:1; 1449 unsigned memory_volatile:1; 1450 unsigned memory_restrict:1; 1451 1452 /** 1453 * Any of the xfb_* qualifiers trigger the shader to be in transform 1454 * feedback mode so we need to keep track of whether the buffer was 1455 * explicitly set or if its just been assigned the default global value. 1456 */ 1457 unsigned explicit_xfb_buffer:1; 1458 1459 unsigned implicit_sized_array:1; 1460 }; 1461 unsigned flags; 1462 }; 1463 #ifdef __cplusplus 1464 #define DEFAULT_CONSTRUCTORS(_type, _name) \ 1465 type(_type), name(_name), location(-1), component(-1), offset(-1), \ 1466 xfb_buffer(0), xfb_stride(0), image_format(PIPE_FORMAT_NONE), flags(0) \ 1467 glsl_struct_fieldglsl_struct_field1468 glsl_struct_field(const struct glsl_type *_type, 1469 int _precision, 1470 const char *_name) 1471 : DEFAULT_CONSTRUCTORS(_type, _name) 1472 { 1473 matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED; 1474 precision = _precision; 1475 } 1476 glsl_struct_fieldglsl_struct_field1477 glsl_struct_field(const struct glsl_type *_type, const char *_name) 1478 : DEFAULT_CONSTRUCTORS(_type, _name) 1479 { 1480 matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED; 1481 precision = GLSL_PRECISION_NONE; 1482 } 1483 glsl_struct_fieldglsl_struct_field1484 glsl_struct_field() 1485 : DEFAULT_CONSTRUCTORS(NULL, NULL) 1486 { 1487 matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED; 1488 precision = GLSL_PRECISION_NONE; 1489 } 1490 #undef DEFAULT_CONSTRUCTORS 1491 #endif 1492 }; 1493 1494 struct glsl_function_param { 1495 const struct glsl_type *type; 1496 1497 bool in; 1498 bool out; 1499 }; 1500 1501 static inline unsigned int glsl_align(unsigned int a,unsigned int align)1502 glsl_align(unsigned int a, unsigned int align) 1503 { 1504 return (a + align - 1) / align * align; 1505 } 1506 1507 #endif /* GLSL_TYPES_H */ 1508