1 /* 2 * Copyright 2009 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24 #include <stdio.h> 25 #include "main/macros.h" 26 #include "compiler/glsl/glsl_parser_extras.h" 27 #include "glsl_types.h" 28 #include "util/hash_table.h" 29 30 31 mtx_t glsl_type::mutex = _MTX_INITIALIZER_NP; 32 hash_table *glsl_type::array_types = NULL; 33 hash_table *glsl_type::record_types = NULL; 34 hash_table *glsl_type::interface_types = NULL; 35 hash_table *glsl_type::function_types = NULL; 36 hash_table *glsl_type::subroutine_types = NULL; 37 void *glsl_type::mem_ctx = NULL; 38 39 void 40 glsl_type::init_ralloc_type_ctx(void) 41 { 42 if (glsl_type::mem_ctx == NULL) { 43 glsl_type::mem_ctx = ralloc_autofree_context(); 44 assert(glsl_type::mem_ctx != NULL); 45 } 46 } 47 48 glsl_type::glsl_type(GLenum gl_type, 49 glsl_base_type base_type, unsigned vector_elements, 50 unsigned matrix_columns, const char *name) : 51 gl_type(gl_type), 52 base_type(base_type), 53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), 54 sampled_type(0), interface_packing(0), interface_row_major(0), 55 vector_elements(vector_elements), matrix_columns(matrix_columns), 56 length(0) 57 { 58 /* Values of these types must fit in the two bits of 59 * glsl_type::sampled_type. 60 */ 61 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT) & 3) == unsigned(GLSL_TYPE_UINT)); 62 STATIC_ASSERT((unsigned(GLSL_TYPE_INT) & 3) == unsigned(GLSL_TYPE_INT)); 63 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT) & 3) == unsigned(GLSL_TYPE_FLOAT)); 64 65 mtx_lock(&glsl_type::mutex); 66 67 init_ralloc_type_ctx(); 68 assert(name != NULL); 69 this->name = ralloc_strdup(this->mem_ctx, name); 70 71 mtx_unlock(&glsl_type::mutex); 72 73 /* Neither dimension is zero or both dimensions are zero. 74 */ 75 assert((vector_elements == 0) == (matrix_columns == 0)); 76 memset(& fields, 0, sizeof(fields)); 77 } 78 79 glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type, 80 enum glsl_sampler_dim dim, bool shadow, bool array, 81 unsigned type, const char *name) : 82 gl_type(gl_type), 83 base_type(base_type), 84 sampler_dimensionality(dim), sampler_shadow(shadow), 85 sampler_array(array), sampled_type(type), interface_packing(0), 86 interface_row_major(0), length(0) 87 { 88 mtx_lock(&glsl_type::mutex); 89 90 init_ralloc_type_ctx(); 91 assert(name != NULL); 92 this->name = ralloc_strdup(this->mem_ctx, name); 93 94 mtx_unlock(&glsl_type::mutex); 95 96 memset(& fields, 0, sizeof(fields)); 97 98 if (base_type == GLSL_TYPE_SAMPLER) { 99 /* Samplers take no storage whatsoever. */ 100 matrix_columns = vector_elements = 0; 101 } else { 102 matrix_columns = vector_elements = 1; 103 } 104 } 105 106 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, 107 const char *name) : 108 gl_type(0), 109 base_type(GLSL_TYPE_STRUCT), 110 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), 111 sampled_type(0), interface_packing(0), interface_row_major(0), 112 vector_elements(0), matrix_columns(0), 113 length(num_fields) 114 { 115 unsigned int i; 116 117 mtx_lock(&glsl_type::mutex); 118 119 init_ralloc_type_ctx(); 120 assert(name != NULL); 121 this->name = ralloc_strdup(this->mem_ctx, name); 122 this->fields.structure = ralloc_array(this->mem_ctx, 123 glsl_struct_field, length); 124 125 for (i = 0; i < length; i++) { 126 this->fields.structure[i] = fields[i]; 127 this->fields.structure[i].name = ralloc_strdup(this->fields.structure, 128 fields[i].name); 129 } 130 131 mtx_unlock(&glsl_type::mutex); 132 } 133 134 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, 135 enum glsl_interface_packing packing, 136 bool row_major, const char *name) : 137 gl_type(0), 138 base_type(GLSL_TYPE_INTERFACE), 139 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), 140 sampled_type(0), interface_packing((unsigned) packing), 141 interface_row_major((unsigned) row_major), 142 vector_elements(0), matrix_columns(0), 143 length(num_fields) 144 { 145 unsigned int i; 146 147 mtx_lock(&glsl_type::mutex); 148 149 init_ralloc_type_ctx(); 150 assert(name != NULL); 151 this->name = ralloc_strdup(this->mem_ctx, name); 152 this->fields.structure = rzalloc_array(this->mem_ctx, 153 glsl_struct_field, length); 154 for (i = 0; i < length; i++) { 155 this->fields.structure[i] = fields[i]; 156 this->fields.structure[i].name = ralloc_strdup(this->fields.structure, 157 fields[i].name); 158 } 159 160 mtx_unlock(&glsl_type::mutex); 161 } 162 163 glsl_type::glsl_type(const glsl_type *return_type, 164 const glsl_function_param *params, unsigned num_params) : 165 gl_type(0), 166 base_type(GLSL_TYPE_FUNCTION), 167 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), 168 sampled_type(0), interface_packing(0), interface_row_major(0), 169 vector_elements(0), matrix_columns(0), 170 length(num_params) 171 { 172 unsigned int i; 173 174 mtx_lock(&glsl_type::mutex); 175 176 init_ralloc_type_ctx(); 177 178 this->fields.parameters = rzalloc_array(this->mem_ctx, 179 glsl_function_param, num_params + 1); 180 181 /* We store the return type as the first parameter */ 182 this->fields.parameters[0].type = return_type; 183 this->fields.parameters[0].in = false; 184 this->fields.parameters[0].out = true; 185 186 /* We store the i'th parameter in slot i+1 */ 187 for (i = 0; i < length; i++) { 188 this->fields.parameters[i + 1].type = params[i].type; 189 this->fields.parameters[i + 1].in = params[i].in; 190 this->fields.parameters[i + 1].out = params[i].out; 191 } 192 193 mtx_unlock(&glsl_type::mutex); 194 } 195 196 glsl_type::glsl_type(const char *subroutine_name) : 197 gl_type(0), 198 base_type(GLSL_TYPE_SUBROUTINE), 199 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), 200 sampled_type(0), interface_packing(0), interface_row_major(0), 201 vector_elements(1), matrix_columns(1), 202 length(0) 203 { 204 mtx_lock(&glsl_type::mutex); 205 206 init_ralloc_type_ctx(); 207 assert(subroutine_name != NULL); 208 this->name = ralloc_strdup(this->mem_ctx, subroutine_name); 209 mtx_unlock(&glsl_type::mutex); 210 } 211 212 bool 213 glsl_type::contains_sampler() const 214 { 215 if (this->is_array()) { 216 return this->fields.array->contains_sampler(); 217 } else if (this->is_record() || this->is_interface()) { 218 for (unsigned int i = 0; i < this->length; i++) { 219 if (this->fields.structure[i].type->contains_sampler()) 220 return true; 221 } 222 return false; 223 } else { 224 return this->is_sampler(); 225 } 226 } 227 228 229 bool 230 glsl_type::contains_integer() const 231 { 232 if (this->is_array()) { 233 return this->fields.array->contains_integer(); 234 } else if (this->is_record() || this->is_interface()) { 235 for (unsigned int i = 0; i < this->length; i++) { 236 if (this->fields.structure[i].type->contains_integer()) 237 return true; 238 } 239 return false; 240 } else { 241 return this->is_integer(); 242 } 243 } 244 245 bool 246 glsl_type::contains_double() const 247 { 248 if (this->is_array()) { 249 return this->fields.array->contains_double(); 250 } else if (this->is_record() || this->is_interface()) { 251 for (unsigned int i = 0; i < this->length; i++) { 252 if (this->fields.structure[i].type->contains_double()) 253 return true; 254 } 255 return false; 256 } else { 257 return this->is_double(); 258 } 259 } 260 261 bool 262 glsl_type::contains_opaque() const { 263 switch (base_type) { 264 case GLSL_TYPE_SAMPLER: 265 case GLSL_TYPE_IMAGE: 266 case GLSL_TYPE_ATOMIC_UINT: 267 return true; 268 case GLSL_TYPE_ARRAY: 269 return fields.array->contains_opaque(); 270 case GLSL_TYPE_STRUCT: 271 case GLSL_TYPE_INTERFACE: 272 for (unsigned int i = 0; i < length; i++) { 273 if (fields.structure[i].type->contains_opaque()) 274 return true; 275 } 276 return false; 277 default: 278 return false; 279 } 280 } 281 282 bool 283 glsl_type::contains_subroutine() const 284 { 285 if (this->is_array()) { 286 return this->fields.array->contains_subroutine(); 287 } else if (this->is_record() || this->is_interface()) { 288 for (unsigned int i = 0; i < this->length; i++) { 289 if (this->fields.structure[i].type->contains_subroutine()) 290 return true; 291 } 292 return false; 293 } else { 294 return this->is_subroutine(); 295 } 296 } 297 298 gl_texture_index 299 glsl_type::sampler_index() const 300 { 301 const glsl_type *const t = (this->is_array()) ? this->fields.array : this; 302 303 assert(t->is_sampler()); 304 305 switch (t->sampler_dimensionality) { 306 case GLSL_SAMPLER_DIM_1D: 307 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX; 308 case GLSL_SAMPLER_DIM_2D: 309 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX; 310 case GLSL_SAMPLER_DIM_3D: 311 return TEXTURE_3D_INDEX; 312 case GLSL_SAMPLER_DIM_CUBE: 313 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX; 314 case GLSL_SAMPLER_DIM_RECT: 315 return TEXTURE_RECT_INDEX; 316 case GLSL_SAMPLER_DIM_BUF: 317 return TEXTURE_BUFFER_INDEX; 318 case GLSL_SAMPLER_DIM_EXTERNAL: 319 return TEXTURE_EXTERNAL_INDEX; 320 case GLSL_SAMPLER_DIM_MS: 321 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX; 322 default: 323 assert(!"Should not get here."); 324 return TEXTURE_BUFFER_INDEX; 325 } 326 } 327 328 bool 329 glsl_type::contains_image() const 330 { 331 if (this->is_array()) { 332 return this->fields.array->contains_image(); 333 } else if (this->is_record() || this->is_interface()) { 334 for (unsigned int i = 0; i < this->length; i++) { 335 if (this->fields.structure[i].type->contains_image()) 336 return true; 337 } 338 return false; 339 } else { 340 return this->is_image(); 341 } 342 } 343 344 const glsl_type *glsl_type::get_base_type() const 345 { 346 switch (base_type) { 347 case GLSL_TYPE_UINT: 348 return uint_type; 349 case GLSL_TYPE_INT: 350 return int_type; 351 case GLSL_TYPE_FLOAT: 352 return float_type; 353 case GLSL_TYPE_DOUBLE: 354 return double_type; 355 case GLSL_TYPE_BOOL: 356 return bool_type; 357 default: 358 return error_type; 359 } 360 } 361 362 363 const glsl_type *glsl_type::get_scalar_type() const 364 { 365 const glsl_type *type = this; 366 367 /* Handle arrays */ 368 while (type->base_type == GLSL_TYPE_ARRAY) 369 type = type->fields.array; 370 371 /* Handle vectors and matrices */ 372 switch (type->base_type) { 373 case GLSL_TYPE_UINT: 374 return uint_type; 375 case GLSL_TYPE_INT: 376 return int_type; 377 case GLSL_TYPE_FLOAT: 378 return float_type; 379 case GLSL_TYPE_DOUBLE: 380 return double_type; 381 case GLSL_TYPE_BOOL: 382 return bool_type; 383 default: 384 /* Handle everything else */ 385 return type; 386 } 387 } 388 389 390 void 391 _mesa_glsl_release_types(void) 392 { 393 /* Should only be called during atexit (either when unloading shared 394 * object, or if process terminates), so no mutex-locking should be 395 * necessary. 396 */ 397 if (glsl_type::array_types != NULL) { 398 _mesa_hash_table_destroy(glsl_type::array_types, NULL); 399 glsl_type::array_types = NULL; 400 } 401 402 if (glsl_type::record_types != NULL) { 403 _mesa_hash_table_destroy(glsl_type::record_types, NULL); 404 glsl_type::record_types = NULL; 405 } 406 407 if (glsl_type::interface_types != NULL) { 408 _mesa_hash_table_destroy(glsl_type::interface_types, NULL); 409 glsl_type::interface_types = NULL; 410 } 411 } 412 413 414 glsl_type::glsl_type(const glsl_type *array, unsigned length) : 415 base_type(GLSL_TYPE_ARRAY), 416 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), 417 sampled_type(0), interface_packing(0), interface_row_major(0), 418 vector_elements(0), matrix_columns(0), 419 length(length), name(NULL) 420 { 421 this->fields.array = array; 422 /* Inherit the gl type of the base. The GL type is used for 423 * uniform/statevar handling in Mesa and the arrayness of the type 424 * is represented by the size rather than the type. 425 */ 426 this->gl_type = array->gl_type; 427 428 /* Allow a maximum of 10 characters for the array size. This is enough 429 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating 430 * NUL. 431 */ 432 const unsigned name_length = strlen(array->name) + 10 + 3; 433 434 mtx_lock(&glsl_type::mutex); 435 char *const n = (char *) ralloc_size(this->mem_ctx, name_length); 436 mtx_unlock(&glsl_type::mutex); 437 438 if (length == 0) 439 snprintf(n, name_length, "%s[]", array->name); 440 else { 441 /* insert outermost dimensions in the correct spot 442 * otherwise the dimension order will be backwards 443 */ 444 const char *pos = strchr(array->name, '['); 445 if (pos) { 446 int idx = pos - array->name; 447 snprintf(n, idx+1, "%s", array->name); 448 snprintf(n + idx, name_length - idx, "[%u]%s", 449 length, array->name + idx); 450 } else { 451 snprintf(n, name_length, "%s[%u]", array->name, length); 452 } 453 } 454 455 this->name = n; 456 } 457 458 459 const glsl_type * 460 glsl_type::vec(unsigned components) 461 { 462 if (components == 0 || components > 4) 463 return error_type; 464 465 static const glsl_type *const ts[] = { 466 float_type, vec2_type, vec3_type, vec4_type 467 }; 468 return ts[components - 1]; 469 } 470 471 const glsl_type * 472 glsl_type::dvec(unsigned components) 473 { 474 if (components == 0 || components > 4) 475 return error_type; 476 477 static const glsl_type *const ts[] = { 478 double_type, dvec2_type, dvec3_type, dvec4_type 479 }; 480 return ts[components - 1]; 481 } 482 483 const glsl_type * 484 glsl_type::ivec(unsigned components) 485 { 486 if (components == 0 || components > 4) 487 return error_type; 488 489 static const glsl_type *const ts[] = { 490 int_type, ivec2_type, ivec3_type, ivec4_type 491 }; 492 return ts[components - 1]; 493 } 494 495 496 const glsl_type * 497 glsl_type::uvec(unsigned components) 498 { 499 if (components == 0 || components > 4) 500 return error_type; 501 502 static const glsl_type *const ts[] = { 503 uint_type, uvec2_type, uvec3_type, uvec4_type 504 }; 505 return ts[components - 1]; 506 } 507 508 509 const glsl_type * 510 glsl_type::bvec(unsigned components) 511 { 512 if (components == 0 || components > 4) 513 return error_type; 514 515 static const glsl_type *const ts[] = { 516 bool_type, bvec2_type, bvec3_type, bvec4_type 517 }; 518 return ts[components - 1]; 519 } 520 521 522 const glsl_type * 523 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns) 524 { 525 if (base_type == GLSL_TYPE_VOID) 526 return void_type; 527 528 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4)) 529 return error_type; 530 531 /* Treat GLSL vectors as Nx1 matrices. 532 */ 533 if (columns == 1) { 534 switch (base_type) { 535 case GLSL_TYPE_UINT: 536 return uvec(rows); 537 case GLSL_TYPE_INT: 538 return ivec(rows); 539 case GLSL_TYPE_FLOAT: 540 return vec(rows); 541 case GLSL_TYPE_DOUBLE: 542 return dvec(rows); 543 case GLSL_TYPE_BOOL: 544 return bvec(rows); 545 default: 546 return error_type; 547 } 548 } else { 549 if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1)) 550 return error_type; 551 552 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following 553 * combinations are valid: 554 * 555 * 1 2 3 4 556 * 1 557 * 2 x x x 558 * 3 x x x 559 * 4 x x x 560 */ 561 #define IDX(c,r) (((c-1)*3) + (r-1)) 562 563 if (base_type == GLSL_TYPE_DOUBLE) { 564 switch (IDX(columns, rows)) { 565 case IDX(2,2): return dmat2_type; 566 case IDX(2,3): return dmat2x3_type; 567 case IDX(2,4): return dmat2x4_type; 568 case IDX(3,2): return dmat3x2_type; 569 case IDX(3,3): return dmat3_type; 570 case IDX(3,4): return dmat3x4_type; 571 case IDX(4,2): return dmat4x2_type; 572 case IDX(4,3): return dmat4x3_type; 573 case IDX(4,4): return dmat4_type; 574 default: return error_type; 575 } 576 } else { 577 switch (IDX(columns, rows)) { 578 case IDX(2,2): return mat2_type; 579 case IDX(2,3): return mat2x3_type; 580 case IDX(2,4): return mat2x4_type; 581 case IDX(3,2): return mat3x2_type; 582 case IDX(3,3): return mat3_type; 583 case IDX(3,4): return mat3x4_type; 584 case IDX(4,2): return mat4x2_type; 585 case IDX(4,3): return mat4x3_type; 586 case IDX(4,4): return mat4_type; 587 default: return error_type; 588 } 589 } 590 } 591 592 assert(!"Should not get here."); 593 return error_type; 594 } 595 596 const glsl_type * 597 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim, 598 bool shadow, 599 bool array, 600 glsl_base_type type) 601 { 602 switch (type) { 603 case GLSL_TYPE_FLOAT: 604 switch (dim) { 605 case GLSL_SAMPLER_DIM_1D: 606 if (shadow) 607 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type); 608 else 609 return (array ? sampler1DArray_type : sampler1D_type); 610 case GLSL_SAMPLER_DIM_2D: 611 if (shadow) 612 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type); 613 else 614 return (array ? sampler2DArray_type : sampler2D_type); 615 case GLSL_SAMPLER_DIM_3D: 616 if (shadow || array) 617 return error_type; 618 else 619 return sampler3D_type; 620 case GLSL_SAMPLER_DIM_CUBE: 621 if (shadow) 622 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type); 623 else 624 return (array ? samplerCubeArray_type : samplerCube_type); 625 case GLSL_SAMPLER_DIM_RECT: 626 if (array) 627 return error_type; 628 if (shadow) 629 return sampler2DRectShadow_type; 630 else 631 return sampler2DRect_type; 632 case GLSL_SAMPLER_DIM_BUF: 633 if (shadow || array) 634 return error_type; 635 else 636 return samplerBuffer_type; 637 case GLSL_SAMPLER_DIM_MS: 638 if (shadow) 639 return error_type; 640 return (array ? sampler2DMSArray_type : sampler2DMS_type); 641 case GLSL_SAMPLER_DIM_EXTERNAL: 642 if (shadow || array) 643 return error_type; 644 else 645 return samplerExternalOES_type; 646 case GLSL_SAMPLER_DIM_SUBPASS: 647 case GLSL_SAMPLER_DIM_SUBPASS_MS: 648 return error_type; 649 } 650 case GLSL_TYPE_INT: 651 if (shadow) 652 return error_type; 653 switch (dim) { 654 case GLSL_SAMPLER_DIM_1D: 655 return (array ? isampler1DArray_type : isampler1D_type); 656 case GLSL_SAMPLER_DIM_2D: 657 return (array ? isampler2DArray_type : isampler2D_type); 658 case GLSL_SAMPLER_DIM_3D: 659 if (array) 660 return error_type; 661 return isampler3D_type; 662 case GLSL_SAMPLER_DIM_CUBE: 663 return (array ? isamplerCubeArray_type : isamplerCube_type); 664 case GLSL_SAMPLER_DIM_RECT: 665 if (array) 666 return error_type; 667 return isampler2DRect_type; 668 case GLSL_SAMPLER_DIM_BUF: 669 if (array) 670 return error_type; 671 return isamplerBuffer_type; 672 case GLSL_SAMPLER_DIM_MS: 673 return (array ? isampler2DMSArray_type : isampler2DMS_type); 674 case GLSL_SAMPLER_DIM_EXTERNAL: 675 return error_type; 676 case GLSL_SAMPLER_DIM_SUBPASS: 677 case GLSL_SAMPLER_DIM_SUBPASS_MS: 678 return error_type; 679 } 680 case GLSL_TYPE_UINT: 681 if (shadow) 682 return error_type; 683 switch (dim) { 684 case GLSL_SAMPLER_DIM_1D: 685 return (array ? usampler1DArray_type : usampler1D_type); 686 case GLSL_SAMPLER_DIM_2D: 687 return (array ? usampler2DArray_type : usampler2D_type); 688 case GLSL_SAMPLER_DIM_3D: 689 if (array) 690 return error_type; 691 return usampler3D_type; 692 case GLSL_SAMPLER_DIM_CUBE: 693 return (array ? usamplerCubeArray_type : usamplerCube_type); 694 case GLSL_SAMPLER_DIM_RECT: 695 if (array) 696 return error_type; 697 return usampler2DRect_type; 698 case GLSL_SAMPLER_DIM_BUF: 699 if (array) 700 return error_type; 701 return usamplerBuffer_type; 702 case GLSL_SAMPLER_DIM_MS: 703 return (array ? usampler2DMSArray_type : usampler2DMS_type); 704 case GLSL_SAMPLER_DIM_EXTERNAL: 705 return error_type; 706 case GLSL_SAMPLER_DIM_SUBPASS: 707 case GLSL_SAMPLER_DIM_SUBPASS_MS: 708 return error_type; 709 } 710 default: 711 return error_type; 712 } 713 714 unreachable("switch statement above should be complete"); 715 } 716 717 const glsl_type * 718 glsl_type::get_image_instance(enum glsl_sampler_dim dim, 719 bool array, glsl_base_type type) 720 { 721 switch (type) { 722 case GLSL_TYPE_FLOAT: 723 switch (dim) { 724 case GLSL_SAMPLER_DIM_1D: 725 return (array ? image1DArray_type : image1D_type); 726 case GLSL_SAMPLER_DIM_2D: 727 return (array ? image2DArray_type : image2D_type); 728 case GLSL_SAMPLER_DIM_3D: 729 return image3D_type; 730 case GLSL_SAMPLER_DIM_CUBE: 731 return (array ? imageCubeArray_type : imageCube_type); 732 case GLSL_SAMPLER_DIM_RECT: 733 if (array) 734 return error_type; 735 else 736 return image2DRect_type; 737 case GLSL_SAMPLER_DIM_BUF: 738 if (array) 739 return error_type; 740 else 741 return imageBuffer_type; 742 case GLSL_SAMPLER_DIM_MS: 743 return (array ? image2DMSArray_type : image2DMS_type); 744 case GLSL_SAMPLER_DIM_SUBPASS: 745 return subpassInput_type; 746 case GLSL_SAMPLER_DIM_SUBPASS_MS: 747 return subpassInputMS_type; 748 case GLSL_SAMPLER_DIM_EXTERNAL: 749 return error_type; 750 } 751 case GLSL_TYPE_INT: 752 switch (dim) { 753 case GLSL_SAMPLER_DIM_1D: 754 return (array ? iimage1DArray_type : iimage1D_type); 755 case GLSL_SAMPLER_DIM_2D: 756 return (array ? iimage2DArray_type : iimage2D_type); 757 case GLSL_SAMPLER_DIM_3D: 758 if (array) 759 return error_type; 760 return iimage3D_type; 761 case GLSL_SAMPLER_DIM_CUBE: 762 return (array ? iimageCubeArray_type : iimageCube_type); 763 case GLSL_SAMPLER_DIM_RECT: 764 if (array) 765 return error_type; 766 return iimage2DRect_type; 767 case GLSL_SAMPLER_DIM_BUF: 768 if (array) 769 return error_type; 770 return iimageBuffer_type; 771 case GLSL_SAMPLER_DIM_MS: 772 return (array ? iimage2DMSArray_type : iimage2DMS_type); 773 case GLSL_SAMPLER_DIM_SUBPASS: 774 return isubpassInput_type; 775 case GLSL_SAMPLER_DIM_SUBPASS_MS: 776 return isubpassInputMS_type; 777 case GLSL_SAMPLER_DIM_EXTERNAL: 778 return error_type; 779 } 780 case GLSL_TYPE_UINT: 781 switch (dim) { 782 case GLSL_SAMPLER_DIM_1D: 783 return (array ? uimage1DArray_type : uimage1D_type); 784 case GLSL_SAMPLER_DIM_2D: 785 return (array ? uimage2DArray_type : uimage2D_type); 786 case GLSL_SAMPLER_DIM_3D: 787 if (array) 788 return error_type; 789 return uimage3D_type; 790 case GLSL_SAMPLER_DIM_CUBE: 791 return (array ? uimageCubeArray_type : uimageCube_type); 792 case GLSL_SAMPLER_DIM_RECT: 793 if (array) 794 return error_type; 795 return uimage2DRect_type; 796 case GLSL_SAMPLER_DIM_BUF: 797 if (array) 798 return error_type; 799 return uimageBuffer_type; 800 case GLSL_SAMPLER_DIM_MS: 801 return (array ? uimage2DMSArray_type : uimage2DMS_type); 802 case GLSL_SAMPLER_DIM_SUBPASS: 803 return usubpassInput_type; 804 case GLSL_SAMPLER_DIM_SUBPASS_MS: 805 return usubpassInputMS_type; 806 case GLSL_SAMPLER_DIM_EXTERNAL: 807 return error_type; 808 } 809 default: 810 return error_type; 811 } 812 813 unreachable("switch statement above should be complete"); 814 } 815 816 const glsl_type * 817 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size) 818 { 819 /* Generate a name using the base type pointer in the key. This is 820 * done because the name of the base type may not be unique across 821 * shaders. For example, two shaders may have different record types 822 * named 'foo'. 823 */ 824 char key[128]; 825 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size); 826 827 mtx_lock(&glsl_type::mutex); 828 829 if (array_types == NULL) { 830 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string, 831 _mesa_key_string_equal); 832 } 833 834 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key); 835 if (entry == NULL) { 836 mtx_unlock(&glsl_type::mutex); 837 const glsl_type *t = new glsl_type(base, array_size); 838 mtx_lock(&glsl_type::mutex); 839 840 entry = _mesa_hash_table_insert(array_types, 841 ralloc_strdup(mem_ctx, key), 842 (void *) t); 843 } 844 845 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY); 846 assert(((glsl_type *) entry->data)->length == array_size); 847 assert(((glsl_type *) entry->data)->fields.array == base); 848 849 mtx_unlock(&glsl_type::mutex); 850 851 return (glsl_type *) entry->data; 852 } 853 854 855 bool 856 glsl_type::record_compare(const glsl_type *b, bool match_locations) const 857 { 858 if (this->length != b->length) 859 return false; 860 861 if (this->interface_packing != b->interface_packing) 862 return false; 863 864 if (this->interface_row_major != b->interface_row_major) 865 return false; 866 867 /* From the GLSL 4.20 specification (Sec 4.2): 868 * 869 * "Structures must have the same name, sequence of type names, and 870 * type definitions, and field names to be considered the same type." 871 * 872 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5). 873 * 874 * Note that we cannot force type name check when comparing unnamed 875 * structure types, these have a unique name assigned during parsing. 876 */ 877 if (!this->is_anonymous() && !b->is_anonymous()) 878 if (strcmp(this->name, b->name) != 0) 879 return false; 880 881 for (unsigned i = 0; i < this->length; i++) { 882 if (this->fields.structure[i].type != b->fields.structure[i].type) 883 return false; 884 if (strcmp(this->fields.structure[i].name, 885 b->fields.structure[i].name) != 0) 886 return false; 887 if (this->fields.structure[i].matrix_layout 888 != b->fields.structure[i].matrix_layout) 889 return false; 890 if (match_locations && this->fields.structure[i].location 891 != b->fields.structure[i].location) 892 return false; 893 if (this->fields.structure[i].offset 894 != b->fields.structure[i].offset) 895 return false; 896 if (this->fields.structure[i].interpolation 897 != b->fields.structure[i].interpolation) 898 return false; 899 if (this->fields.structure[i].centroid 900 != b->fields.structure[i].centroid) 901 return false; 902 if (this->fields.structure[i].sample 903 != b->fields.structure[i].sample) 904 return false; 905 if (this->fields.structure[i].patch 906 != b->fields.structure[i].patch) 907 return false; 908 if (this->fields.structure[i].image_read_only 909 != b->fields.structure[i].image_read_only) 910 return false; 911 if (this->fields.structure[i].image_write_only 912 != b->fields.structure[i].image_write_only) 913 return false; 914 if (this->fields.structure[i].image_coherent 915 != b->fields.structure[i].image_coherent) 916 return false; 917 if (this->fields.structure[i].image_volatile 918 != b->fields.structure[i].image_volatile) 919 return false; 920 if (this->fields.structure[i].image_restrict 921 != b->fields.structure[i].image_restrict) 922 return false; 923 if (this->fields.structure[i].precision 924 != b->fields.structure[i].precision) 925 return false; 926 if (this->fields.structure[i].explicit_xfb_buffer 927 != b->fields.structure[i].explicit_xfb_buffer) 928 return false; 929 if (this->fields.structure[i].xfb_buffer 930 != b->fields.structure[i].xfb_buffer) 931 return false; 932 if (this->fields.structure[i].xfb_stride 933 != b->fields.structure[i].xfb_stride) 934 return false; 935 } 936 937 return true; 938 } 939 940 941 bool 942 glsl_type::record_key_compare(const void *a, const void *b) 943 { 944 const glsl_type *const key1 = (glsl_type *) a; 945 const glsl_type *const key2 = (glsl_type *) b; 946 947 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2); 948 } 949 950 951 /** 952 * Generate an integer hash value for a glsl_type structure type. 953 */ 954 unsigned 955 glsl_type::record_key_hash(const void *a) 956 { 957 const glsl_type *const key = (glsl_type *) a; 958 uintptr_t hash = key->length; 959 unsigned retval; 960 961 for (unsigned i = 0; i < key->length; i++) { 962 /* casting pointer to uintptr_t */ 963 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type; 964 } 965 966 if (sizeof(hash) == 8) 967 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32); 968 else 969 retval = hash; 970 971 return retval; 972 } 973 974 975 const glsl_type * 976 glsl_type::get_record_instance(const glsl_struct_field *fields, 977 unsigned num_fields, 978 const char *name) 979 { 980 const glsl_type key(fields, num_fields, name); 981 982 mtx_lock(&glsl_type::mutex); 983 984 if (record_types == NULL) { 985 record_types = _mesa_hash_table_create(NULL, record_key_hash, 986 record_key_compare); 987 } 988 989 const struct hash_entry *entry = _mesa_hash_table_search(record_types, 990 &key); 991 if (entry == NULL) { 992 mtx_unlock(&glsl_type::mutex); 993 const glsl_type *t = new glsl_type(fields, num_fields, name); 994 mtx_lock(&glsl_type::mutex); 995 996 entry = _mesa_hash_table_insert(record_types, t, (void *) t); 997 } 998 999 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT); 1000 assert(((glsl_type *) entry->data)->length == num_fields); 1001 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0); 1002 1003 mtx_unlock(&glsl_type::mutex); 1004 1005 return (glsl_type *) entry->data; 1006 } 1007 1008 1009 const glsl_type * 1010 glsl_type::get_interface_instance(const glsl_struct_field *fields, 1011 unsigned num_fields, 1012 enum glsl_interface_packing packing, 1013 bool row_major, 1014 const char *block_name) 1015 { 1016 const glsl_type key(fields, num_fields, packing, row_major, block_name); 1017 1018 mtx_lock(&glsl_type::mutex); 1019 1020 if (interface_types == NULL) { 1021 interface_types = _mesa_hash_table_create(NULL, record_key_hash, 1022 record_key_compare); 1023 } 1024 1025 const struct hash_entry *entry = _mesa_hash_table_search(interface_types, 1026 &key); 1027 if (entry == NULL) { 1028 mtx_unlock(&glsl_type::mutex); 1029 const glsl_type *t = new glsl_type(fields, num_fields, 1030 packing, row_major, block_name); 1031 mtx_lock(&glsl_type::mutex); 1032 1033 entry = _mesa_hash_table_insert(interface_types, t, (void *) t); 1034 } 1035 1036 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE); 1037 assert(((glsl_type *) entry->data)->length == num_fields); 1038 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0); 1039 1040 mtx_unlock(&glsl_type::mutex); 1041 1042 return (glsl_type *) entry->data; 1043 } 1044 1045 const glsl_type * 1046 glsl_type::get_subroutine_instance(const char *subroutine_name) 1047 { 1048 const glsl_type key(subroutine_name); 1049 1050 mtx_lock(&glsl_type::mutex); 1051 1052 if (subroutine_types == NULL) { 1053 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash, 1054 record_key_compare); 1055 } 1056 1057 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types, 1058 &key); 1059 if (entry == NULL) { 1060 mtx_unlock(&glsl_type::mutex); 1061 const glsl_type *t = new glsl_type(subroutine_name); 1062 mtx_lock(&glsl_type::mutex); 1063 1064 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t); 1065 } 1066 1067 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE); 1068 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0); 1069 1070 mtx_unlock(&glsl_type::mutex); 1071 1072 return (glsl_type *) entry->data; 1073 } 1074 1075 1076 static bool 1077 function_key_compare(const void *a, const void *b) 1078 { 1079 const glsl_type *const key1 = (glsl_type *) a; 1080 const glsl_type *const key2 = (glsl_type *) b; 1081 1082 if (key1->length != key2->length) 1083 return false; 1084 1085 return memcmp(key1->fields.parameters, key2->fields.parameters, 1086 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0; 1087 } 1088 1089 1090 static uint32_t 1091 function_key_hash(const void *a) 1092 { 1093 const glsl_type *const key = (glsl_type *) a; 1094 return _mesa_hash_data(key->fields.parameters, 1095 (key->length + 1) * sizeof(*key->fields.parameters)); 1096 } 1097 1098 const glsl_type * 1099 glsl_type::get_function_instance(const glsl_type *return_type, 1100 const glsl_function_param *params, 1101 unsigned num_params) 1102 { 1103 const glsl_type key(return_type, params, num_params); 1104 1105 mtx_lock(&glsl_type::mutex); 1106 1107 if (function_types == NULL) { 1108 function_types = _mesa_hash_table_create(NULL, function_key_hash, 1109 function_key_compare); 1110 } 1111 1112 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key); 1113 if (entry == NULL) { 1114 mtx_unlock(&glsl_type::mutex); 1115 const glsl_type *t = new glsl_type(return_type, params, num_params); 1116 mtx_lock(&glsl_type::mutex); 1117 1118 entry = _mesa_hash_table_insert(function_types, t, (void *) t); 1119 } 1120 1121 const glsl_type *t = (const glsl_type *)entry->data; 1122 1123 assert(t->base_type == GLSL_TYPE_FUNCTION); 1124 assert(t->length == num_params); 1125 1126 mtx_unlock(&glsl_type::mutex); 1127 1128 return t; 1129 } 1130 1131 1132 const glsl_type * 1133 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b) 1134 { 1135 if (type_a == type_b) { 1136 return type_a; 1137 } else if (type_a->is_matrix() && type_b->is_matrix()) { 1138 /* Matrix multiply. The columns of A must match the rows of B. Given 1139 * the other previously tested constraints, this means the vector type 1140 * of a row from A must be the same as the vector type of a column from 1141 * B. 1142 */ 1143 if (type_a->row_type() == type_b->column_type()) { 1144 /* The resulting matrix has the number of columns of matrix B and 1145 * the number of rows of matrix A. We get the row count of A by 1146 * looking at the size of a vector that makes up a column. The 1147 * transpose (size of a row) is done for B. 1148 */ 1149 const glsl_type *const type = 1150 get_instance(type_a->base_type, 1151 type_a->column_type()->vector_elements, 1152 type_b->row_type()->vector_elements); 1153 assert(type != error_type); 1154 1155 return type; 1156 } 1157 } else if (type_a->is_matrix()) { 1158 /* A is a matrix and B is a column vector. Columns of A must match 1159 * rows of B. Given the other previously tested constraints, this 1160 * means the vector type of a row from A must be the same as the 1161 * vector the type of B. 1162 */ 1163 if (type_a->row_type() == type_b) { 1164 /* The resulting vector has a number of elements equal to 1165 * the number of rows of matrix A. */ 1166 const glsl_type *const type = 1167 get_instance(type_a->base_type, 1168 type_a->column_type()->vector_elements, 1169 1); 1170 assert(type != error_type); 1171 1172 return type; 1173 } 1174 } else { 1175 assert(type_b->is_matrix()); 1176 1177 /* A is a row vector and B is a matrix. Columns of A must match rows 1178 * of B. Given the other previously tested constraints, this means 1179 * the type of A must be the same as the vector type of a column from 1180 * B. 1181 */ 1182 if (type_a == type_b->column_type()) { 1183 /* The resulting vector has a number of elements equal to 1184 * the number of columns of matrix B. */ 1185 const glsl_type *const type = 1186 get_instance(type_a->base_type, 1187 type_b->row_type()->vector_elements, 1188 1); 1189 assert(type != error_type); 1190 1191 return type; 1192 } 1193 } 1194 1195 return error_type; 1196 } 1197 1198 1199 const glsl_type * 1200 glsl_type::field_type(const char *name) const 1201 { 1202 if (this->base_type != GLSL_TYPE_STRUCT 1203 && this->base_type != GLSL_TYPE_INTERFACE) 1204 return error_type; 1205 1206 for (unsigned i = 0; i < this->length; i++) { 1207 if (strcmp(name, this->fields.structure[i].name) == 0) 1208 return this->fields.structure[i].type; 1209 } 1210 1211 return error_type; 1212 } 1213 1214 1215 int 1216 glsl_type::field_index(const char *name) const 1217 { 1218 if (this->base_type != GLSL_TYPE_STRUCT 1219 && this->base_type != GLSL_TYPE_INTERFACE) 1220 return -1; 1221 1222 for (unsigned i = 0; i < this->length; i++) { 1223 if (strcmp(name, this->fields.structure[i].name) == 0) 1224 return i; 1225 } 1226 1227 return -1; 1228 } 1229 1230 1231 unsigned 1232 glsl_type::component_slots() const 1233 { 1234 switch (this->base_type) { 1235 case GLSL_TYPE_UINT: 1236 case GLSL_TYPE_INT: 1237 case GLSL_TYPE_FLOAT: 1238 case GLSL_TYPE_BOOL: 1239 return this->components(); 1240 1241 case GLSL_TYPE_DOUBLE: 1242 return 2 * this->components(); 1243 1244 case GLSL_TYPE_STRUCT: 1245 case GLSL_TYPE_INTERFACE: { 1246 unsigned size = 0; 1247 1248 for (unsigned i = 0; i < this->length; i++) 1249 size += this->fields.structure[i].type->component_slots(); 1250 1251 return size; 1252 } 1253 1254 case GLSL_TYPE_ARRAY: 1255 return this->length * this->fields.array->component_slots(); 1256 1257 case GLSL_TYPE_IMAGE: 1258 return 1; 1259 case GLSL_TYPE_SUBROUTINE: 1260 return 1; 1261 1262 case GLSL_TYPE_FUNCTION: 1263 case GLSL_TYPE_SAMPLER: 1264 case GLSL_TYPE_ATOMIC_UINT: 1265 case GLSL_TYPE_VOID: 1266 case GLSL_TYPE_ERROR: 1267 break; 1268 } 1269 1270 return 0; 1271 } 1272 1273 unsigned 1274 glsl_type::record_location_offset(unsigned length) const 1275 { 1276 unsigned offset = 0; 1277 const glsl_type *t = this->without_array(); 1278 if (t->is_record()) { 1279 assert(length <= t->length); 1280 1281 for (unsigned i = 0; i < length; i++) { 1282 const glsl_type *st = t->fields.structure[i].type; 1283 const glsl_type *wa = st->without_array(); 1284 if (wa->is_record()) { 1285 unsigned r_offset = wa->record_location_offset(wa->length); 1286 offset += st->is_array() ? 1287 st->arrays_of_arrays_size() * r_offset : r_offset; 1288 } else if (st->is_array() && st->fields.array->is_array()) { 1289 unsigned outer_array_size = st->length; 1290 const glsl_type *base_type = st->fields.array; 1291 1292 /* For arrays of arrays the outer arrays take up a uniform 1293 * slot for each element. The innermost array elements share a 1294 * single slot so we ignore the innermost array when calculating 1295 * the offset. 1296 */ 1297 while (base_type->fields.array->is_array()) { 1298 outer_array_size = outer_array_size * base_type->length; 1299 base_type = base_type->fields.array; 1300 } 1301 offset += outer_array_size; 1302 } else { 1303 /* We dont worry about arrays here because unless the array 1304 * contains a structure or another array it only takes up a single 1305 * uniform slot. 1306 */ 1307 offset += 1; 1308 } 1309 } 1310 } 1311 return offset; 1312 } 1313 1314 unsigned 1315 glsl_type::uniform_locations() const 1316 { 1317 unsigned size = 0; 1318 1319 switch (this->base_type) { 1320 case GLSL_TYPE_UINT: 1321 case GLSL_TYPE_INT: 1322 case GLSL_TYPE_FLOAT: 1323 case GLSL_TYPE_DOUBLE: 1324 case GLSL_TYPE_BOOL: 1325 case GLSL_TYPE_SAMPLER: 1326 case GLSL_TYPE_IMAGE: 1327 case GLSL_TYPE_SUBROUTINE: 1328 return 1; 1329 1330 case GLSL_TYPE_STRUCT: 1331 case GLSL_TYPE_INTERFACE: 1332 for (unsigned i = 0; i < this->length; i++) 1333 size += this->fields.structure[i].type->uniform_locations(); 1334 return size; 1335 case GLSL_TYPE_ARRAY: 1336 return this->length * this->fields.array->uniform_locations(); 1337 default: 1338 return 0; 1339 } 1340 } 1341 1342 unsigned 1343 glsl_type::varying_count() const 1344 { 1345 unsigned size = 0; 1346 1347 switch (this->base_type) { 1348 case GLSL_TYPE_UINT: 1349 case GLSL_TYPE_INT: 1350 case GLSL_TYPE_FLOAT: 1351 case GLSL_TYPE_DOUBLE: 1352 case GLSL_TYPE_BOOL: 1353 return 1; 1354 1355 case GLSL_TYPE_STRUCT: 1356 case GLSL_TYPE_INTERFACE: 1357 for (unsigned i = 0; i < this->length; i++) 1358 size += this->fields.structure[i].type->varying_count(); 1359 return size; 1360 case GLSL_TYPE_ARRAY: 1361 /* Don't count innermost array elements */ 1362 if (this->without_array()->is_record() || 1363 this->without_array()->is_interface() || 1364 this->fields.array->is_array()) 1365 return this->length * this->fields.array->varying_count(); 1366 else 1367 return this->fields.array->varying_count(); 1368 default: 1369 assert(!"unsupported varying type"); 1370 return 0; 1371 } 1372 } 1373 1374 bool 1375 glsl_type::can_implicitly_convert_to(const glsl_type *desired, 1376 _mesa_glsl_parse_state *state) const 1377 { 1378 if (this == desired) 1379 return true; 1380 1381 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no 1382 * state, we're doing intra-stage function linking where these checks have 1383 * already been done. 1384 */ 1385 if (state && (state->es_shader || !state->is_version(120, 0))) 1386 return false; 1387 1388 /* There is no conversion among matrix types. */ 1389 if (this->matrix_columns > 1 || desired->matrix_columns > 1) 1390 return false; 1391 1392 /* Vector size must match. */ 1393 if (this->vector_elements != desired->vector_elements) 1394 return false; 1395 1396 /* int and uint can be converted to float. */ 1397 if (desired->is_float() && this->is_integer()) 1398 return true; 1399 1400 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int 1401 * can be converted to uint. Note that state may be NULL here, when 1402 * resolving function calls in the linker. By this time, all the 1403 * state-dependent checks have already happened though, so allow anything 1404 * that's allowed in any shader version. 1405 */ 1406 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable || 1407 state->MESA_shader_integer_functions_enable) && 1408 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT) 1409 return true; 1410 1411 /* No implicit conversions from double. */ 1412 if ((!state || state->has_double()) && this->is_double()) 1413 return false; 1414 1415 /* Conversions from different types to double. */ 1416 if ((!state || state->has_double()) && desired->is_double()) { 1417 if (this->is_float()) 1418 return true; 1419 if (this->is_integer()) 1420 return true; 1421 } 1422 1423 return false; 1424 } 1425 1426 unsigned 1427 glsl_type::std140_base_alignment(bool row_major) const 1428 { 1429 unsigned N = is_64bit() ? 8 : 4; 1430 1431 /* (1) If the member is a scalar consuming <N> basic machine units, the 1432 * base alignment is <N>. 1433 * 1434 * (2) If the member is a two- or four-component vector with components 1435 * consuming <N> basic machine units, the base alignment is 2<N> or 1436 * 4<N>, respectively. 1437 * 1438 * (3) If the member is a three-component vector with components consuming 1439 * <N> basic machine units, the base alignment is 4<N>. 1440 */ 1441 if (this->is_scalar() || this->is_vector()) { 1442 switch (this->vector_elements) { 1443 case 1: 1444 return N; 1445 case 2: 1446 return 2 * N; 1447 case 3: 1448 case 4: 1449 return 4 * N; 1450 } 1451 } 1452 1453 /* (4) If the member is an array of scalars or vectors, the base alignment 1454 * and array stride are set to match the base alignment of a single 1455 * array element, according to rules (1), (2), and (3), and rounded up 1456 * to the base alignment of a vec4. The array may have padding at the 1457 * end; the base offset of the member following the array is rounded up 1458 * to the next multiple of the base alignment. 1459 * 1460 * (6) If the member is an array of <S> column-major matrices with <C> 1461 * columns and <R> rows, the matrix is stored identically to a row of 1462 * <S>*<C> column vectors with <R> components each, according to rule 1463 * (4). 1464 * 1465 * (8) If the member is an array of <S> row-major matrices with <C> columns 1466 * and <R> rows, the matrix is stored identically to a row of <S>*<R> 1467 * row vectors with <C> components each, according to rule (4). 1468 * 1469 * (10) If the member is an array of <S> structures, the <S> elements of 1470 * the array are laid out in order, according to rule (9). 1471 */ 1472 if (this->is_array()) { 1473 if (this->fields.array->is_scalar() || 1474 this->fields.array->is_vector() || 1475 this->fields.array->is_matrix()) { 1476 return MAX2(this->fields.array->std140_base_alignment(row_major), 16); 1477 } else { 1478 assert(this->fields.array->is_record() || 1479 this->fields.array->is_array()); 1480 return this->fields.array->std140_base_alignment(row_major); 1481 } 1482 } 1483 1484 /* (5) If the member is a column-major matrix with <C> columns and 1485 * <R> rows, the matrix is stored identically to an array of 1486 * <C> column vectors with <R> components each, according to 1487 * rule (4). 1488 * 1489 * (7) If the member is a row-major matrix with <C> columns and <R> 1490 * rows, the matrix is stored identically to an array of <R> 1491 * row vectors with <C> components each, according to rule (4). 1492 */ 1493 if (this->is_matrix()) { 1494 const struct glsl_type *vec_type, *array_type; 1495 int c = this->matrix_columns; 1496 int r = this->vector_elements; 1497 1498 if (row_major) { 1499 vec_type = get_instance(base_type, c, 1); 1500 array_type = glsl_type::get_array_instance(vec_type, r); 1501 } else { 1502 vec_type = get_instance(base_type, r, 1); 1503 array_type = glsl_type::get_array_instance(vec_type, c); 1504 } 1505 1506 return array_type->std140_base_alignment(false); 1507 } 1508 1509 /* (9) If the member is a structure, the base alignment of the 1510 * structure is <N>, where <N> is the largest base alignment 1511 * value of any of its members, and rounded up to the base 1512 * alignment of a vec4. The individual members of this 1513 * sub-structure are then assigned offsets by applying this set 1514 * of rules recursively, where the base offset of the first 1515 * member of the sub-structure is equal to the aligned offset 1516 * of the structure. The structure may have padding at the end; 1517 * the base offset of the member following the sub-structure is 1518 * rounded up to the next multiple of the base alignment of the 1519 * structure. 1520 */ 1521 if (this->is_record()) { 1522 unsigned base_alignment = 16; 1523 for (unsigned i = 0; i < this->length; i++) { 1524 bool field_row_major = row_major; 1525 const enum glsl_matrix_layout matrix_layout = 1526 glsl_matrix_layout(this->fields.structure[i].matrix_layout); 1527 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) { 1528 field_row_major = true; 1529 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) { 1530 field_row_major = false; 1531 } 1532 1533 const struct glsl_type *field_type = this->fields.structure[i].type; 1534 base_alignment = MAX2(base_alignment, 1535 field_type->std140_base_alignment(field_row_major)); 1536 } 1537 return base_alignment; 1538 } 1539 1540 assert(!"not reached"); 1541 return -1; 1542 } 1543 1544 unsigned 1545 glsl_type::std140_size(bool row_major) const 1546 { 1547 unsigned N = is_64bit() ? 8 : 4; 1548 1549 /* (1) If the member is a scalar consuming <N> basic machine units, the 1550 * base alignment is <N>. 1551 * 1552 * (2) If the member is a two- or four-component vector with components 1553 * consuming <N> basic machine units, the base alignment is 2<N> or 1554 * 4<N>, respectively. 1555 * 1556 * (3) If the member is a three-component vector with components consuming 1557 * <N> basic machine units, the base alignment is 4<N>. 1558 */ 1559 if (this->is_scalar() || this->is_vector()) { 1560 return this->vector_elements * N; 1561 } 1562 1563 /* (5) If the member is a column-major matrix with <C> columns and 1564 * <R> rows, the matrix is stored identically to an array of 1565 * <C> column vectors with <R> components each, according to 1566 * rule (4). 1567 * 1568 * (6) If the member is an array of <S> column-major matrices with <C> 1569 * columns and <R> rows, the matrix is stored identically to a row of 1570 * <S>*<C> column vectors with <R> components each, according to rule 1571 * (4). 1572 * 1573 * (7) If the member is a row-major matrix with <C> columns and <R> 1574 * rows, the matrix is stored identically to an array of <R> 1575 * row vectors with <C> components each, according to rule (4). 1576 * 1577 * (8) If the member is an array of <S> row-major matrices with <C> columns 1578 * and <R> rows, the matrix is stored identically to a row of <S>*<R> 1579 * row vectors with <C> components each, according to rule (4). 1580 */ 1581 if (this->without_array()->is_matrix()) { 1582 const struct glsl_type *element_type; 1583 const struct glsl_type *vec_type; 1584 unsigned int array_len; 1585 1586 if (this->is_array()) { 1587 element_type = this->without_array(); 1588 array_len = this->arrays_of_arrays_size(); 1589 } else { 1590 element_type = this; 1591 array_len = 1; 1592 } 1593 1594 if (row_major) { 1595 vec_type = get_instance(element_type->base_type, 1596 element_type->matrix_columns, 1); 1597 1598 array_len *= element_type->vector_elements; 1599 } else { 1600 vec_type = get_instance(element_type->base_type, 1601 element_type->vector_elements, 1); 1602 array_len *= element_type->matrix_columns; 1603 } 1604 const glsl_type *array_type = glsl_type::get_array_instance(vec_type, 1605 array_len); 1606 1607 return array_type->std140_size(false); 1608 } 1609 1610 /* (4) If the member is an array of scalars or vectors, the base alignment 1611 * and array stride are set to match the base alignment of a single 1612 * array element, according to rules (1), (2), and (3), and rounded up 1613 * to the base alignment of a vec4. The array may have padding at the 1614 * end; the base offset of the member following the array is rounded up 1615 * to the next multiple of the base alignment. 1616 * 1617 * (10) If the member is an array of <S> structures, the <S> elements of 1618 * the array are laid out in order, according to rule (9). 1619 */ 1620 if (this->is_array()) { 1621 if (this->without_array()->is_record()) { 1622 return this->arrays_of_arrays_size() * 1623 this->without_array()->std140_size(row_major); 1624 } else { 1625 unsigned element_base_align = 1626 this->without_array()->std140_base_alignment(row_major); 1627 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16); 1628 } 1629 } 1630 1631 /* (9) If the member is a structure, the base alignment of the 1632 * structure is <N>, where <N> is the largest base alignment 1633 * value of any of its members, and rounded up to the base 1634 * alignment of a vec4. The individual members of this 1635 * sub-structure are then assigned offsets by applying this set 1636 * of rules recursively, where the base offset of the first 1637 * member of the sub-structure is equal to the aligned offset 1638 * of the structure. The structure may have padding at the end; 1639 * the base offset of the member following the sub-structure is 1640 * rounded up to the next multiple of the base alignment of the 1641 * structure. 1642 */ 1643 if (this->is_record() || this->is_interface()) { 1644 unsigned size = 0; 1645 unsigned max_align = 0; 1646 1647 for (unsigned i = 0; i < this->length; i++) { 1648 bool field_row_major = row_major; 1649 const enum glsl_matrix_layout matrix_layout = 1650 glsl_matrix_layout(this->fields.structure[i].matrix_layout); 1651 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) { 1652 field_row_major = true; 1653 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) { 1654 field_row_major = false; 1655 } 1656 1657 const struct glsl_type *field_type = this->fields.structure[i].type; 1658 unsigned align = field_type->std140_base_alignment(field_row_major); 1659 1660 /* Ignore unsized arrays when calculating size */ 1661 if (field_type->is_unsized_array()) 1662 continue; 1663 1664 size = glsl_align(size, align); 1665 size += field_type->std140_size(field_row_major); 1666 1667 max_align = MAX2(align, max_align); 1668 1669 if (field_type->is_record() && (i + 1 < this->length)) 1670 size = glsl_align(size, 16); 1671 } 1672 size = glsl_align(size, MAX2(max_align, 16)); 1673 return size; 1674 } 1675 1676 assert(!"not reached"); 1677 return -1; 1678 } 1679 1680 unsigned 1681 glsl_type::std430_base_alignment(bool row_major) const 1682 { 1683 1684 unsigned N = is_64bit() ? 8 : 4; 1685 1686 /* (1) If the member is a scalar consuming <N> basic machine units, the 1687 * base alignment is <N>. 1688 * 1689 * (2) If the member is a two- or four-component vector with components 1690 * consuming <N> basic machine units, the base alignment is 2<N> or 1691 * 4<N>, respectively. 1692 * 1693 * (3) If the member is a three-component vector with components consuming 1694 * <N> basic machine units, the base alignment is 4<N>. 1695 */ 1696 if (this->is_scalar() || this->is_vector()) { 1697 switch (this->vector_elements) { 1698 case 1: 1699 return N; 1700 case 2: 1701 return 2 * N; 1702 case 3: 1703 case 4: 1704 return 4 * N; 1705 } 1706 } 1707 1708 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout": 1709 * 1710 * "When using the std430 storage layout, shader storage blocks will be 1711 * laid out in buffer storage identically to uniform and shader storage 1712 * blocks using the std140 layout, except that the base alignment and 1713 * stride of arrays of scalars and vectors in rule 4 and of structures 1714 * in rule 9 are not rounded up a multiple of the base alignment of a vec4. 1715 */ 1716 1717 /* (1) If the member is a scalar consuming <N> basic machine units, the 1718 * base alignment is <N>. 1719 * 1720 * (2) If the member is a two- or four-component vector with components 1721 * consuming <N> basic machine units, the base alignment is 2<N> or 1722 * 4<N>, respectively. 1723 * 1724 * (3) If the member is a three-component vector with components consuming 1725 * <N> basic machine units, the base alignment is 4<N>. 1726 */ 1727 if (this->is_array()) 1728 return this->fields.array->std430_base_alignment(row_major); 1729 1730 /* (5) If the member is a column-major matrix with <C> columns and 1731 * <R> rows, the matrix is stored identically to an array of 1732 * <C> column vectors with <R> components each, according to 1733 * rule (4). 1734 * 1735 * (7) If the member is a row-major matrix with <C> columns and <R> 1736 * rows, the matrix is stored identically to an array of <R> 1737 * row vectors with <C> components each, according to rule (4). 1738 */ 1739 if (this->is_matrix()) { 1740 const struct glsl_type *vec_type, *array_type; 1741 int c = this->matrix_columns; 1742 int r = this->vector_elements; 1743 1744 if (row_major) { 1745 vec_type = get_instance(base_type, c, 1); 1746 array_type = glsl_type::get_array_instance(vec_type, r); 1747 } else { 1748 vec_type = get_instance(base_type, r, 1); 1749 array_type = glsl_type::get_array_instance(vec_type, c); 1750 } 1751 1752 return array_type->std430_base_alignment(false); 1753 } 1754 1755 /* (9) If the member is a structure, the base alignment of the 1756 * structure is <N>, where <N> is the largest base alignment 1757 * value of any of its members, and rounded up to the base 1758 * alignment of a vec4. The individual members of this 1759 * sub-structure are then assigned offsets by applying this set 1760 * of rules recursively, where the base offset of the first 1761 * member of the sub-structure is equal to the aligned offset 1762 * of the structure. The structure may have padding at the end; 1763 * the base offset of the member following the sub-structure is 1764 * rounded up to the next multiple of the base alignment of the 1765 * structure. 1766 */ 1767 if (this->is_record()) { 1768 unsigned base_alignment = 0; 1769 for (unsigned i = 0; i < this->length; i++) { 1770 bool field_row_major = row_major; 1771 const enum glsl_matrix_layout matrix_layout = 1772 glsl_matrix_layout(this->fields.structure[i].matrix_layout); 1773 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) { 1774 field_row_major = true; 1775 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) { 1776 field_row_major = false; 1777 } 1778 1779 const struct glsl_type *field_type = this->fields.structure[i].type; 1780 base_alignment = MAX2(base_alignment, 1781 field_type->std430_base_alignment(field_row_major)); 1782 } 1783 assert(base_alignment > 0); 1784 return base_alignment; 1785 } 1786 assert(!"not reached"); 1787 return -1; 1788 } 1789 1790 unsigned 1791 glsl_type::std430_array_stride(bool row_major) const 1792 { 1793 unsigned N = is_64bit() ? 8 : 4; 1794 1795 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N. 1796 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout" 1797 * 1798 * (3) If the member is a three-component vector with components consuming 1799 * <N> basic machine units, the base alignment is 4<N>. 1800 */ 1801 if (this->is_vector() && this->vector_elements == 3) 1802 return 4 * N; 1803 1804 /* By default use std430_size(row_major) */ 1805 return this->std430_size(row_major); 1806 } 1807 1808 unsigned 1809 glsl_type::std430_size(bool row_major) const 1810 { 1811 unsigned N = is_64bit() ? 8 : 4; 1812 1813 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout": 1814 * 1815 * "When using the std430 storage layout, shader storage blocks will be 1816 * laid out in buffer storage identically to uniform and shader storage 1817 * blocks using the std140 layout, except that the base alignment and 1818 * stride of arrays of scalars and vectors in rule 4 and of structures 1819 * in rule 9 are not rounded up a multiple of the base alignment of a vec4. 1820 */ 1821 if (this->is_scalar() || this->is_vector()) 1822 return this->vector_elements * N; 1823 1824 if (this->without_array()->is_matrix()) { 1825 const struct glsl_type *element_type; 1826 const struct glsl_type *vec_type; 1827 unsigned int array_len; 1828 1829 if (this->is_array()) { 1830 element_type = this->without_array(); 1831 array_len = this->arrays_of_arrays_size(); 1832 } else { 1833 element_type = this; 1834 array_len = 1; 1835 } 1836 1837 if (row_major) { 1838 vec_type = get_instance(element_type->base_type, 1839 element_type->matrix_columns, 1); 1840 1841 array_len *= element_type->vector_elements; 1842 } else { 1843 vec_type = get_instance(element_type->base_type, 1844 element_type->vector_elements, 1); 1845 array_len *= element_type->matrix_columns; 1846 } 1847 const glsl_type *array_type = glsl_type::get_array_instance(vec_type, 1848 array_len); 1849 1850 return array_type->std430_size(false); 1851 } 1852 1853 if (this->is_array()) { 1854 if (this->without_array()->is_record()) 1855 return this->arrays_of_arrays_size() * 1856 this->without_array()->std430_size(row_major); 1857 else 1858 return this->arrays_of_arrays_size() * 1859 this->without_array()->std430_base_alignment(row_major); 1860 } 1861 1862 if (this->is_record() || this->is_interface()) { 1863 unsigned size = 0; 1864 unsigned max_align = 0; 1865 1866 for (unsigned i = 0; i < this->length; i++) { 1867 bool field_row_major = row_major; 1868 const enum glsl_matrix_layout matrix_layout = 1869 glsl_matrix_layout(this->fields.structure[i].matrix_layout); 1870 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) { 1871 field_row_major = true; 1872 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) { 1873 field_row_major = false; 1874 } 1875 1876 const struct glsl_type *field_type = this->fields.structure[i].type; 1877 unsigned align = field_type->std430_base_alignment(field_row_major); 1878 size = glsl_align(size, align); 1879 size += field_type->std430_size(field_row_major); 1880 1881 max_align = MAX2(align, max_align); 1882 } 1883 size = glsl_align(size, max_align); 1884 return size; 1885 } 1886 1887 assert(!"not reached"); 1888 return -1; 1889 } 1890 1891 unsigned 1892 glsl_type::count_attribute_slots(bool is_vertex_input) const 1893 { 1894 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: 1895 * 1896 * "A scalar input counts the same amount against this limit as a vec4, 1897 * so applications may want to consider packing groups of four 1898 * unrelated float inputs together into a vector to better utilize the 1899 * capabilities of the underlying hardware. A matrix input will use up 1900 * multiple locations. The number of locations used will equal the 1901 * number of columns in the matrix." 1902 * 1903 * The spec does not explicitly say how arrays are counted. However, it 1904 * should be safe to assume the total number of slots consumed by an array 1905 * is the number of entries in the array multiplied by the number of slots 1906 * consumed by a single element of the array. 1907 * 1908 * The spec says nothing about how structs are counted, because vertex 1909 * attributes are not allowed to be (or contain) structs. However, Mesa 1910 * allows varying structs, the number of varying slots taken up by a 1911 * varying struct is simply equal to the sum of the number of slots taken 1912 * up by each element. 1913 * 1914 * Doubles are counted different depending on whether they are vertex 1915 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit 1916 * take one location no matter what size they are, otherwise dvec3/4 1917 * take two locations. 1918 */ 1919 switch (this->base_type) { 1920 case GLSL_TYPE_UINT: 1921 case GLSL_TYPE_INT: 1922 case GLSL_TYPE_FLOAT: 1923 case GLSL_TYPE_BOOL: 1924 return this->matrix_columns; 1925 case GLSL_TYPE_DOUBLE: 1926 if (this->vector_elements > 2 && !is_vertex_input) 1927 return this->matrix_columns * 2; 1928 else 1929 return this->matrix_columns; 1930 case GLSL_TYPE_STRUCT: 1931 case GLSL_TYPE_INTERFACE: { 1932 unsigned size = 0; 1933 1934 for (unsigned i = 0; i < this->length; i++) 1935 size += this->fields.structure[i].type->count_attribute_slots(is_vertex_input); 1936 1937 return size; 1938 } 1939 1940 case GLSL_TYPE_ARRAY: 1941 return this->length * this->fields.array->count_attribute_slots(is_vertex_input); 1942 1943 case GLSL_TYPE_FUNCTION: 1944 case GLSL_TYPE_SAMPLER: 1945 case GLSL_TYPE_IMAGE: 1946 case GLSL_TYPE_ATOMIC_UINT: 1947 case GLSL_TYPE_VOID: 1948 case GLSL_TYPE_SUBROUTINE: 1949 case GLSL_TYPE_ERROR: 1950 break; 1951 } 1952 1953 assert(!"Unexpected type in count_attribute_slots()"); 1954 1955 return 0; 1956 } 1957 1958 int 1959 glsl_type::coordinate_components() const 1960 { 1961 int size; 1962 1963 switch (sampler_dimensionality) { 1964 case GLSL_SAMPLER_DIM_1D: 1965 case GLSL_SAMPLER_DIM_BUF: 1966 size = 1; 1967 break; 1968 case GLSL_SAMPLER_DIM_2D: 1969 case GLSL_SAMPLER_DIM_RECT: 1970 case GLSL_SAMPLER_DIM_MS: 1971 case GLSL_SAMPLER_DIM_EXTERNAL: 1972 case GLSL_SAMPLER_DIM_SUBPASS: 1973 size = 2; 1974 break; 1975 case GLSL_SAMPLER_DIM_3D: 1976 case GLSL_SAMPLER_DIM_CUBE: 1977 size = 3; 1978 break; 1979 default: 1980 assert(!"Should not get here."); 1981 size = 1; 1982 break; 1983 } 1984 1985 /* Array textures need an additional component for the array index, except 1986 * for cubemap array images that behave like a 2D array of interleaved 1987 * cubemap faces. 1988 */ 1989 if (sampler_array && 1990 !(base_type == GLSL_TYPE_IMAGE && 1991 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE)) 1992 size += 1; 1993 1994 return size; 1995 } 1996 1997 /** 1998 * Declarations of type flyweights (glsl_type::_foo_type) and 1999 * convenience pointers (glsl_type::foo_type). 2000 * @{ 2001 */ 2002 #define DECL_TYPE(NAME, ...) \ 2003 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \ 2004 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type; 2005 2006 #define STRUCT_TYPE(NAME) 2007 2008 #include "compiler/builtin_type_macros.h" 2009 /** @} */ 2010