link_varyings.cpp revision b8e80941
1/* 2 * Copyright © 2012 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/** 25 * \file link_varyings.cpp 26 * 27 * Linker functions related specifically to linking varyings between shader 28 * stages. 29 */ 30 31 32#include "main/errors.h" 33#include "main/mtypes.h" 34#include "glsl_symbol_table.h" 35#include "glsl_parser_extras.h" 36#include "ir_optimization.h" 37#include "linker.h" 38#include "link_varyings.h" 39#include "main/macros.h" 40#include "util/hash_table.h" 41#include "util/u_math.h" 42#include "program.h" 43 44 45/** 46 * Get the varying type stripped of the outermost array if we're processing 47 * a stage whose varyings are arrays indexed by a vertex number (such as 48 * geometry shader inputs). 49 */ 50static const glsl_type * 51get_varying_type(const ir_variable *var, gl_shader_stage stage) 52{ 53 const glsl_type *type = var->type; 54 55 if (!var->data.patch && 56 ((var->data.mode == ir_var_shader_out && 57 stage == MESA_SHADER_TESS_CTRL) || 58 (var->data.mode == ir_var_shader_in && 59 (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL || 60 stage == MESA_SHADER_GEOMETRY)))) { 61 assert(type->is_array()); 62 type = type->fields.array; 63 } 64 65 return type; 66} 67 68static void 69create_xfb_varying_names(void *mem_ctx, const glsl_type *t, char **name, 70 size_t name_length, unsigned *count, 71 const char *ifc_member_name, 72 const glsl_type *ifc_member_t, char ***varying_names) 73{ 74 if (t->is_interface()) { 75 size_t new_length = name_length; 76 77 assert(ifc_member_name && ifc_member_t); 78 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", ifc_member_name); 79 80 create_xfb_varying_names(mem_ctx, ifc_member_t, name, new_length, count, 81 NULL, NULL, varying_names); 82 } else if (t->is_struct()) { 83 for (unsigned i = 0; i < t->length; i++) { 84 const char *field = t->fields.structure[i].name; 85 size_t new_length = name_length; 86 87 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field); 88 89 create_xfb_varying_names(mem_ctx, t->fields.structure[i].type, name, 90 new_length, count, NULL, NULL, 91 varying_names); 92 } 93 } else if (t->without_array()->is_struct() || 94 t->without_array()->is_interface() || 95 (t->is_array() && t->fields.array->is_array())) { 96 for (unsigned i = 0; i < t->length; i++) { 97 size_t new_length = name_length; 98 99 /* Append the subscript to the current variable name */ 100 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i); 101 102 create_xfb_varying_names(mem_ctx, t->fields.array, name, new_length, 103 count, ifc_member_name, ifc_member_t, 104 varying_names); 105 } 106 } else { 107 (*varying_names)[(*count)++] = ralloc_strdup(mem_ctx, *name); 108 } 109} 110 111static bool 112process_xfb_layout_qualifiers(void *mem_ctx, const gl_linked_shader *sh, 113 struct gl_shader_program *prog, 114 unsigned *num_tfeedback_decls, 115 char ***varying_names) 116{ 117 bool has_xfb_qualifiers = false; 118 119 /* We still need to enable transform feedback mode even if xfb_stride is 120 * only applied to a global out. Also we don't bother to propagate 121 * xfb_stride to interface block members so this will catch that case also. 122 */ 123 for (unsigned j = 0; j < MAX_FEEDBACK_BUFFERS; j++) { 124 if (prog->TransformFeedback.BufferStride[j]) { 125 has_xfb_qualifiers = true; 126 break; 127 } 128 } 129 130 foreach_in_list(ir_instruction, node, sh->ir) { 131 ir_variable *var = node->as_variable(); 132 if (!var || var->data.mode != ir_var_shader_out) 133 continue; 134 135 /* From the ARB_enhanced_layouts spec: 136 * 137 * "Any shader making any static use (after preprocessing) of any of 138 * these *xfb_* qualifiers will cause the shader to be in a 139 * transform feedback capturing mode and hence responsible for 140 * describing the transform feedback setup. This mode will capture 141 * any output selected by *xfb_offset*, directly or indirectly, to 142 * a transform feedback buffer." 143 */ 144 if (var->data.explicit_xfb_buffer || var->data.explicit_xfb_stride) { 145 has_xfb_qualifiers = true; 146 } 147 148 if (var->data.explicit_xfb_offset) { 149 *num_tfeedback_decls += var->type->varying_count(); 150 has_xfb_qualifiers = true; 151 } 152 } 153 154 if (*num_tfeedback_decls == 0) 155 return has_xfb_qualifiers; 156 157 unsigned i = 0; 158 *varying_names = ralloc_array(mem_ctx, char *, *num_tfeedback_decls); 159 foreach_in_list(ir_instruction, node, sh->ir) { 160 ir_variable *var = node->as_variable(); 161 if (!var || var->data.mode != ir_var_shader_out) 162 continue; 163 164 if (var->data.explicit_xfb_offset) { 165 char *name; 166 const glsl_type *type, *member_type; 167 168 if (var->data.from_named_ifc_block) { 169 type = var->get_interface_type(); 170 171 /* Find the member type before it was altered by lowering */ 172 const glsl_type *type_wa = type->without_array(); 173 member_type = 174 type_wa->fields.structure[type_wa->field_index(var->name)].type; 175 name = ralloc_strdup(NULL, type_wa->name); 176 } else { 177 type = var->type; 178 member_type = NULL; 179 name = ralloc_strdup(NULL, var->name); 180 } 181 create_xfb_varying_names(mem_ctx, type, &name, strlen(name), &i, 182 var->name, member_type, varying_names); 183 ralloc_free(name); 184 } 185 } 186 187 assert(i == *num_tfeedback_decls); 188 return has_xfb_qualifiers; 189} 190 191/** 192 * Validate the types and qualifiers of an output from one stage against the 193 * matching input to another stage. 194 */ 195static void 196cross_validate_types_and_qualifiers(struct gl_context *ctx, 197 struct gl_shader_program *prog, 198 const ir_variable *input, 199 const ir_variable *output, 200 gl_shader_stage consumer_stage, 201 gl_shader_stage producer_stage) 202{ 203 /* Check that the types match between stages. 204 */ 205 const glsl_type *type_to_match = input->type; 206 207 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */ 208 const bool extra_array_level = (producer_stage == MESA_SHADER_VERTEX && 209 consumer_stage != MESA_SHADER_FRAGMENT) || 210 consumer_stage == MESA_SHADER_GEOMETRY; 211 if (extra_array_level) { 212 assert(type_to_match->is_array()); 213 type_to_match = type_to_match->fields.array; 214 } 215 216 if (type_to_match != output->type) { 217 if (output->type->is_struct()) { 218 /* Structures across shader stages can have different name 219 * and considered to match in type if and only if structure 220 * members match in name, type, qualification, and declaration 221 * order. 222 */ 223 if (!output->type->record_compare(type_to_match, false, true)) { 224 linker_error(prog, 225 "%s shader output `%s' declared as struct `%s', " 226 "doesn't match in type with %s shader input " 227 "declared as struct `%s'\n", 228 _mesa_shader_stage_to_string(producer_stage), 229 output->name, 230 output->type->name, 231 _mesa_shader_stage_to_string(consumer_stage), 232 input->type->name); 233 } 234 } else if (!output->type->is_array() || !is_gl_identifier(output->name)) { 235 /* There is a bit of a special case for gl_TexCoord. This 236 * built-in is unsized by default. Applications that variable 237 * access it must redeclare it with a size. There is some 238 * language in the GLSL spec that implies the fragment shader 239 * and vertex shader do not have to agree on this size. Other 240 * driver behave this way, and one or two applications seem to 241 * rely on it. 242 * 243 * Neither declaration needs to be modified here because the array 244 * sizes are fixed later when update_array_sizes is called. 245 * 246 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec: 247 * 248 * "Unlike user-defined varying variables, the built-in 249 * varying variables don't have a strict one-to-one 250 * correspondence between the vertex language and the 251 * fragment language." 252 */ 253 linker_error(prog, 254 "%s shader output `%s' declared as type `%s', " 255 "but %s shader input declared as type `%s'\n", 256 _mesa_shader_stage_to_string(producer_stage), 257 output->name, 258 output->type->name, 259 _mesa_shader_stage_to_string(consumer_stage), 260 input->type->name); 261 return; 262 } 263 } 264 265 /* Check that all of the qualifiers match between stages. 266 */ 267 268 /* According to the OpenGL and OpenGLES GLSL specs, the centroid qualifier 269 * should match until OpenGL 4.3 and OpenGLES 3.1. The OpenGLES 3.0 270 * conformance test suite does not verify that the qualifiers must match. 271 * The deqp test suite expects the opposite (OpenGLES 3.1) behavior for 272 * OpenGLES 3.0 drivers, so we relax the checking in all cases. 273 */ 274 if (false /* always skip the centroid check */ && 275 prog->data->Version < (prog->IsES ? 310 : 430) && 276 input->data.centroid != output->data.centroid) { 277 linker_error(prog, 278 "%s shader output `%s' %s centroid qualifier, " 279 "but %s shader input %s centroid qualifier\n", 280 _mesa_shader_stage_to_string(producer_stage), 281 output->name, 282 (output->data.centroid) ? "has" : "lacks", 283 _mesa_shader_stage_to_string(consumer_stage), 284 (input->data.centroid) ? "has" : "lacks"); 285 return; 286 } 287 288 if (input->data.sample != output->data.sample) { 289 linker_error(prog, 290 "%s shader output `%s' %s sample qualifier, " 291 "but %s shader input %s sample qualifier\n", 292 _mesa_shader_stage_to_string(producer_stage), 293 output->name, 294 (output->data.sample) ? "has" : "lacks", 295 _mesa_shader_stage_to_string(consumer_stage), 296 (input->data.sample) ? "has" : "lacks"); 297 return; 298 } 299 300 if (input->data.patch != output->data.patch) { 301 linker_error(prog, 302 "%s shader output `%s' %s patch qualifier, " 303 "but %s shader input %s patch qualifier\n", 304 _mesa_shader_stage_to_string(producer_stage), 305 output->name, 306 (output->data.patch) ? "has" : "lacks", 307 _mesa_shader_stage_to_string(consumer_stage), 308 (input->data.patch) ? "has" : "lacks"); 309 return; 310 } 311 312 /* The GLSL 4.30 and GLSL ES 3.00 specifications say: 313 * 314 * "As only outputs need be declared with invariant, an output from 315 * one shader stage will still match an input of a subsequent stage 316 * without the input being declared as invariant." 317 * 318 * while GLSL 4.20 says: 319 * 320 * "For variables leaving one shader and coming into another shader, 321 * the invariant keyword has to be used in both shaders, or a link 322 * error will result." 323 * 324 * and GLSL ES 1.00 section 4.6.4 "Invariance and Linking" says: 325 * 326 * "The invariance of varyings that are declared in both the vertex 327 * and fragment shaders must match." 328 */ 329 if (input->data.explicit_invariant != output->data.explicit_invariant && 330 prog->data->Version < (prog->IsES ? 300 : 430)) { 331 linker_error(prog, 332 "%s shader output `%s' %s invariant qualifier, " 333 "but %s shader input %s invariant qualifier\n", 334 _mesa_shader_stage_to_string(producer_stage), 335 output->name, 336 (output->data.explicit_invariant) ? "has" : "lacks", 337 _mesa_shader_stage_to_string(consumer_stage), 338 (input->data.explicit_invariant) ? "has" : "lacks"); 339 return; 340 } 341 342 /* GLSL >= 4.40 removes text requiring interpolation qualifiers 343 * to match cross stage, they must only match within the same stage. 344 * 345 * From page 84 (page 90 of the PDF) of the GLSL 4.40 spec: 346 * 347 * "It is a link-time error if, within the same stage, the interpolation 348 * qualifiers of variables of the same name do not match. 349 * 350 * Section 4.3.9 (Interpolation) of the GLSL ES 3.00 spec says: 351 * 352 * "When no interpolation qualifier is present, smooth interpolation 353 * is used." 354 * 355 * So we match variables where one is smooth and the other has no explicit 356 * qualifier. 357 */ 358 unsigned input_interpolation = input->data.interpolation; 359 unsigned output_interpolation = output->data.interpolation; 360 if (prog->IsES) { 361 if (input_interpolation == INTERP_MODE_NONE) 362 input_interpolation = INTERP_MODE_SMOOTH; 363 if (output_interpolation == INTERP_MODE_NONE) 364 output_interpolation = INTERP_MODE_SMOOTH; 365 } 366 if (input_interpolation != output_interpolation && 367 prog->data->Version < 440) { 368 if (!ctx->Const.AllowGLSLCrossStageInterpolationMismatch) { 369 linker_error(prog, 370 "%s shader output `%s' specifies %s " 371 "interpolation qualifier, " 372 "but %s shader input specifies %s " 373 "interpolation qualifier\n", 374 _mesa_shader_stage_to_string(producer_stage), 375 output->name, 376 interpolation_string(output->data.interpolation), 377 _mesa_shader_stage_to_string(consumer_stage), 378 interpolation_string(input->data.interpolation)); 379 return; 380 } else { 381 linker_warning(prog, 382 "%s shader output `%s' specifies %s " 383 "interpolation qualifier, " 384 "but %s shader input specifies %s " 385 "interpolation qualifier\n", 386 _mesa_shader_stage_to_string(producer_stage), 387 output->name, 388 interpolation_string(output->data.interpolation), 389 _mesa_shader_stage_to_string(consumer_stage), 390 interpolation_string(input->data.interpolation)); 391 } 392 } 393} 394 395/** 396 * Validate front and back color outputs against single color input 397 */ 398static void 399cross_validate_front_and_back_color(struct gl_context *ctx, 400 struct gl_shader_program *prog, 401 const ir_variable *input, 402 const ir_variable *front_color, 403 const ir_variable *back_color, 404 gl_shader_stage consumer_stage, 405 gl_shader_stage producer_stage) 406{ 407 if (front_color != NULL && front_color->data.assigned) 408 cross_validate_types_and_qualifiers(ctx, prog, input, front_color, 409 consumer_stage, producer_stage); 410 411 if (back_color != NULL && back_color->data.assigned) 412 cross_validate_types_and_qualifiers(ctx, prog, input, back_color, 413 consumer_stage, producer_stage); 414} 415 416static unsigned 417compute_variable_location_slot(ir_variable *var, gl_shader_stage stage) 418{ 419 unsigned location_start = VARYING_SLOT_VAR0; 420 421 switch (stage) { 422 case MESA_SHADER_VERTEX: 423 if (var->data.mode == ir_var_shader_in) 424 location_start = VERT_ATTRIB_GENERIC0; 425 break; 426 case MESA_SHADER_TESS_CTRL: 427 case MESA_SHADER_TESS_EVAL: 428 if (var->data.patch) 429 location_start = VARYING_SLOT_PATCH0; 430 break; 431 case MESA_SHADER_FRAGMENT: 432 if (var->data.mode == ir_var_shader_out) 433 location_start = FRAG_RESULT_DATA0; 434 break; 435 default: 436 break; 437 } 438 439 return var->data.location - location_start; 440} 441 442struct explicit_location_info { 443 ir_variable *var; 444 bool base_type_is_integer; 445 unsigned base_type_bit_size; 446 unsigned interpolation; 447 bool centroid; 448 bool sample; 449 bool patch; 450}; 451 452static bool 453check_location_aliasing(struct explicit_location_info explicit_locations[][4], 454 ir_variable *var, 455 unsigned location, 456 unsigned component, 457 unsigned location_limit, 458 const glsl_type *type, 459 unsigned interpolation, 460 bool centroid, 461 bool sample, 462 bool patch, 463 gl_shader_program *prog, 464 gl_shader_stage stage) 465{ 466 unsigned last_comp; 467 unsigned base_type_bit_size; 468 const glsl_type *type_without_array = type->without_array(); 469 const bool base_type_is_integer = 470 glsl_base_type_is_integer(type_without_array->base_type); 471 const bool is_struct = type_without_array->is_struct(); 472 if (is_struct) { 473 /* structs don't have a defined underlying base type so just treat all 474 * component slots as used and set the bit size to 0. If there is 475 * location aliasing, we'll fail anyway later. 476 */ 477 last_comp = 4; 478 base_type_bit_size = 0; 479 } else { 480 unsigned dmul = type_without_array->is_64bit() ? 2 : 1; 481 last_comp = component + type_without_array->vector_elements * dmul; 482 base_type_bit_size = 483 glsl_base_type_get_bit_size(type_without_array->base_type); 484 } 485 486 while (location < location_limit) { 487 unsigned comp = 0; 488 while (comp < 4) { 489 struct explicit_location_info *info = 490 &explicit_locations[location][comp]; 491 492 if (info->var) { 493 if (info->var->type->without_array()->is_struct() || is_struct) { 494 /* Structs cannot share location since they are incompatible 495 * with any other underlying numerical type. 496 */ 497 linker_error(prog, 498 "%s shader has multiple %sputs sharing the " 499 "same location that don't have the same " 500 "underlying numerical type. Struct variable '%s', " 501 "location %u\n", 502 _mesa_shader_stage_to_string(stage), 503 var->data.mode == ir_var_shader_in ? "in" : "out", 504 is_struct ? var->name : info->var->name, 505 location); 506 return false; 507 } else if (comp >= component && comp < last_comp) { 508 /* Component aliasing is not allowed */ 509 linker_error(prog, 510 "%s shader has multiple %sputs explicitly " 511 "assigned to location %d and component %d\n", 512 _mesa_shader_stage_to_string(stage), 513 var->data.mode == ir_var_shader_in ? "in" : "out", 514 location, comp); 515 return false; 516 } else { 517 /* From the OpenGL 4.60.5 spec, section 4.4.1 Input Layout 518 * Qualifiers, Page 67, (Location aliasing): 519 * 520 * " Further, when location aliasing, the aliases sharing the 521 * location must have the same underlying numerical type 522 * and bit width (floating-point or integer, 32-bit versus 523 * 64-bit, etc.) and the same auxiliary storage and 524 * interpolation qualification." 525 */ 526 527 /* If the underlying numerical type isn't integer, implicitly 528 * it will be float or else we would have failed by now. 529 */ 530 if (info->base_type_is_integer != base_type_is_integer) { 531 linker_error(prog, 532 "%s shader has multiple %sputs sharing the " 533 "same location that don't have the same " 534 "underlying numerical type. Location %u " 535 "component %u.\n", 536 _mesa_shader_stage_to_string(stage), 537 var->data.mode == ir_var_shader_in ? 538 "in" : "out", location, comp); 539 return false; 540 } 541 542 if (info->base_type_bit_size != base_type_bit_size) { 543 linker_error(prog, 544 "%s shader has multiple %sputs sharing the " 545 "same location that don't have the same " 546 "underlying numerical bit size. Location %u " 547 "component %u.\n", 548 _mesa_shader_stage_to_string(stage), 549 var->data.mode == ir_var_shader_in ? 550 "in" : "out", location, comp); 551 return false; 552 } 553 554 if (info->interpolation != interpolation) { 555 linker_error(prog, 556 "%s shader has multiple %sputs sharing the " 557 "same location that don't have the same " 558 "interpolation qualification. Location %u " 559 "component %u.\n", 560 _mesa_shader_stage_to_string(stage), 561 var->data.mode == ir_var_shader_in ? 562 "in" : "out", location, comp); 563 return false; 564 } 565 566 if (info->centroid != centroid || 567 info->sample != sample || 568 info->patch != patch) { 569 linker_error(prog, 570 "%s shader has multiple %sputs sharing the " 571 "same location that don't have the same " 572 "auxiliary storage qualification. Location %u " 573 "component %u.\n", 574 _mesa_shader_stage_to_string(stage), 575 var->data.mode == ir_var_shader_in ? 576 "in" : "out", location, comp); 577 return false; 578 } 579 } 580 } else if (comp >= component && comp < last_comp) { 581 info->var = var; 582 info->base_type_is_integer = base_type_is_integer; 583 info->base_type_bit_size = base_type_bit_size; 584 info->interpolation = interpolation; 585 info->centroid = centroid; 586 info->sample = sample; 587 info->patch = patch; 588 } 589 590 comp++; 591 592 /* We need to do some special handling for doubles as dvec3 and 593 * dvec4 consume two consecutive locations. We don't need to 594 * worry about components beginning at anything other than 0 as 595 * the spec does not allow this for dvec3 and dvec4. 596 */ 597 if (comp == 4 && last_comp > 4) { 598 last_comp = last_comp - 4; 599 /* Bump location index and reset the component index */ 600 location++; 601 comp = 0; 602 component = 0; 603 } 604 } 605 606 location++; 607 } 608 609 return true; 610} 611 612static bool 613validate_explicit_variable_location(struct gl_context *ctx, 614 struct explicit_location_info explicit_locations[][4], 615 ir_variable *var, 616 gl_shader_program *prog, 617 gl_linked_shader *sh) 618{ 619 const glsl_type *type = get_varying_type(var, sh->Stage); 620 unsigned num_elements = type->count_attribute_slots(false); 621 unsigned idx = compute_variable_location_slot(var, sh->Stage); 622 unsigned slot_limit = idx + num_elements; 623 624 /* Vertex shader inputs and fragment shader outputs are validated in 625 * assign_attribute_or_color_locations() so we should not attempt to 626 * validate them again here. 627 */ 628 unsigned slot_max; 629 if (var->data.mode == ir_var_shader_out) { 630 assert(sh->Stage != MESA_SHADER_FRAGMENT); 631 slot_max = 632 ctx->Const.Program[sh->Stage].MaxOutputComponents / 4; 633 } else { 634 assert(var->data.mode == ir_var_shader_in); 635 assert(sh->Stage != MESA_SHADER_VERTEX); 636 slot_max = 637 ctx->Const.Program[sh->Stage].MaxInputComponents / 4; 638 } 639 640 if (slot_limit > slot_max) { 641 linker_error(prog, 642 "Invalid location %u in %s shader\n", 643 idx, _mesa_shader_stage_to_string(sh->Stage)); 644 return false; 645 } 646 647 const glsl_type *type_without_array = type->without_array(); 648 if (type_without_array->is_interface()) { 649 for (unsigned i = 0; i < type_without_array->length; i++) { 650 glsl_struct_field *field = &type_without_array->fields.structure[i]; 651 unsigned field_location = field->location - 652 (field->patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0); 653 if (!check_location_aliasing(explicit_locations, var, 654 field_location, 655 0, field_location + 1, 656 field->type, 657 field->interpolation, 658 field->centroid, 659 field->sample, 660 field->patch, 661 prog, sh->Stage)) { 662 return false; 663 } 664 } 665 } else if (!check_location_aliasing(explicit_locations, var, 666 idx, var->data.location_frac, 667 slot_limit, type, 668 var->data.interpolation, 669 var->data.centroid, 670 var->data.sample, 671 var->data.patch, 672 prog, sh->Stage)) { 673 return false; 674 } 675 676 return true; 677} 678 679/** 680 * Validate explicit locations for the inputs to the first stage and the 681 * outputs of the last stage in a program, if those are not the VS and FS 682 * shaders. 683 */ 684void 685validate_first_and_last_interface_explicit_locations(struct gl_context *ctx, 686 struct gl_shader_program *prog, 687 gl_shader_stage first_stage, 688 gl_shader_stage last_stage) 689{ 690 /* VS inputs and FS outputs are validated in 691 * assign_attribute_or_color_locations() 692 */ 693 bool validate_first_stage = first_stage != MESA_SHADER_VERTEX; 694 bool validate_last_stage = last_stage != MESA_SHADER_FRAGMENT; 695 if (!validate_first_stage && !validate_last_stage) 696 return; 697 698 struct explicit_location_info explicit_locations[MAX_VARYING][4]; 699 700 gl_shader_stage stages[2] = { first_stage, last_stage }; 701 bool validate_stage[2] = { validate_first_stage, validate_last_stage }; 702 ir_variable_mode var_direction[2] = { ir_var_shader_in, ir_var_shader_out }; 703 704 for (unsigned i = 0; i < 2; i++) { 705 if (!validate_stage[i]) 706 continue; 707 708 gl_shader_stage stage = stages[i]; 709 710 gl_linked_shader *sh = prog->_LinkedShaders[stage]; 711 assert(sh); 712 713 memset(explicit_locations, 0, sizeof(explicit_locations)); 714 715 foreach_in_list(ir_instruction, node, sh->ir) { 716 ir_variable *const var = node->as_variable(); 717 718 if (var == NULL || 719 !var->data.explicit_location || 720 var->data.location < VARYING_SLOT_VAR0 || 721 var->data.mode != var_direction[i]) 722 continue; 723 724 if (!validate_explicit_variable_location( 725 ctx, explicit_locations, var, prog, sh)) { 726 return; 727 } 728 } 729 } 730} 731 732/** 733 * Validate that outputs from one stage match inputs of another 734 */ 735void 736cross_validate_outputs_to_inputs(struct gl_context *ctx, 737 struct gl_shader_program *prog, 738 gl_linked_shader *producer, 739 gl_linked_shader *consumer) 740{ 741 glsl_symbol_table parameters; 742 struct explicit_location_info output_explicit_locations[MAX_VARYING][4] = {}; 743 struct explicit_location_info input_explicit_locations[MAX_VARYING][4] = {}; 744 745 /* Find all shader outputs in the "producer" stage. 746 */ 747 foreach_in_list(ir_instruction, node, producer->ir) { 748 ir_variable *const var = node->as_variable(); 749 750 if (var == NULL || var->data.mode != ir_var_shader_out) 751 continue; 752 753 if (!var->data.explicit_location 754 || var->data.location < VARYING_SLOT_VAR0) 755 parameters.add_variable(var); 756 else { 757 /* User-defined varyings with explicit locations are handled 758 * differently because they do not need to have matching names. 759 */ 760 if (!validate_explicit_variable_location(ctx, 761 output_explicit_locations, 762 var, prog, producer)) { 763 return; 764 } 765 } 766 } 767 768 769 /* Find all shader inputs in the "consumer" stage. Any variables that have 770 * matching outputs already in the symbol table must have the same type and 771 * qualifiers. 772 * 773 * Exception: if the consumer is the geometry shader, then the inputs 774 * should be arrays and the type of the array element should match the type 775 * of the corresponding producer output. 776 */ 777 foreach_in_list(ir_instruction, node, consumer->ir) { 778 ir_variable *const input = node->as_variable(); 779 780 if (input == NULL || input->data.mode != ir_var_shader_in) 781 continue; 782 783 if (strcmp(input->name, "gl_Color") == 0 && input->data.used) { 784 const ir_variable *const front_color = 785 parameters.get_variable("gl_FrontColor"); 786 787 const ir_variable *const back_color = 788 parameters.get_variable("gl_BackColor"); 789 790 cross_validate_front_and_back_color(ctx, prog, input, 791 front_color, back_color, 792 consumer->Stage, producer->Stage); 793 } else if (strcmp(input->name, "gl_SecondaryColor") == 0 && input->data.used) { 794 const ir_variable *const front_color = 795 parameters.get_variable("gl_FrontSecondaryColor"); 796 797 const ir_variable *const back_color = 798 parameters.get_variable("gl_BackSecondaryColor"); 799 800 cross_validate_front_and_back_color(ctx, prog, input, 801 front_color, back_color, 802 consumer->Stage, producer->Stage); 803 } else { 804 /* The rules for connecting inputs and outputs change in the presence 805 * of explicit locations. In this case, we no longer care about the 806 * names of the variables. Instead, we care only about the 807 * explicitly assigned location. 808 */ 809 ir_variable *output = NULL; 810 if (input->data.explicit_location 811 && input->data.location >= VARYING_SLOT_VAR0) { 812 813 const glsl_type *type = get_varying_type(input, consumer->Stage); 814 unsigned num_elements = type->count_attribute_slots(false); 815 unsigned idx = 816 compute_variable_location_slot(input, consumer->Stage); 817 unsigned slot_limit = idx + num_elements; 818 819 if (!validate_explicit_variable_location(ctx, 820 input_explicit_locations, 821 input, prog, consumer)) { 822 return; 823 } 824 825 while (idx < slot_limit) { 826 if (idx >= MAX_VARYING) { 827 linker_error(prog, 828 "Invalid location %u in %s shader\n", idx, 829 _mesa_shader_stage_to_string(consumer->Stage)); 830 return; 831 } 832 833 output = output_explicit_locations[idx][input->data.location_frac].var; 834 835 if (output == NULL) { 836 /* A linker failure should only happen when there is no 837 * output declaration and there is Static Use of the 838 * declared input. 839 */ 840 if (input->data.used) { 841 linker_error(prog, 842 "%s shader input `%s' with explicit location " 843 "has no matching output\n", 844 _mesa_shader_stage_to_string(consumer->Stage), 845 input->name); 846 break; 847 } 848 } else if (input->data.location != output->data.location) { 849 linker_error(prog, 850 "%s shader input `%s' with explicit location " 851 "has no matching output\n", 852 _mesa_shader_stage_to_string(consumer->Stage), 853 input->name); 854 break; 855 } 856 idx++; 857 } 858 } else { 859 output = parameters.get_variable(input->name); 860 } 861 862 if (output != NULL) { 863 /* Interface blocks have their own validation elsewhere so don't 864 * try validating them here. 865 */ 866 if (!(input->get_interface_type() && 867 output->get_interface_type())) 868 cross_validate_types_and_qualifiers(ctx, prog, input, output, 869 consumer->Stage, 870 producer->Stage); 871 } else { 872 /* Check for input vars with unmatched output vars in prev stage 873 * taking into account that interface blocks could have a matching 874 * output but with different name, so we ignore them. 875 */ 876 assert(!input->data.assigned); 877 if (input->data.used && !input->get_interface_type() && 878 !input->data.explicit_location) 879 linker_error(prog, 880 "%s shader input `%s' " 881 "has no matching output in the previous stage\n", 882 _mesa_shader_stage_to_string(consumer->Stage), 883 input->name); 884 } 885 } 886 } 887} 888 889/** 890 * Demote shader inputs and outputs that are not used in other stages, and 891 * remove them via dead code elimination. 892 */ 893static void 894remove_unused_shader_inputs_and_outputs(bool is_separate_shader_object, 895 gl_linked_shader *sh, 896 enum ir_variable_mode mode) 897{ 898 if (is_separate_shader_object) 899 return; 900 901 foreach_in_list(ir_instruction, node, sh->ir) { 902 ir_variable *const var = node->as_variable(); 903 904 if (var == NULL || var->data.mode != int(mode)) 905 continue; 906 907 /* A shader 'in' or 'out' variable is only really an input or output if 908 * its value is used by other shader stages. This will cause the 909 * variable to have a location assigned. 910 */ 911 if (var->data.is_unmatched_generic_inout && !var->data.is_xfb_only) { 912 assert(var->data.mode != ir_var_temporary); 913 914 /* Assign zeros to demoted inputs to allow more optimizations. */ 915 if (var->data.mode == ir_var_shader_in && !var->constant_value) 916 var->constant_value = ir_constant::zero(var, var->type); 917 918 var->data.mode = ir_var_auto; 919 } 920 } 921 922 /* Eliminate code that is now dead due to unused inputs/outputs being 923 * demoted. 924 */ 925 while (do_dead_code(sh->ir, false)) 926 ; 927 928} 929 930/** 931 * Initialize this object based on a string that was passed to 932 * glTransformFeedbackVaryings. 933 * 934 * If the input is mal-formed, this call still succeeds, but it sets 935 * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var() 936 * will fail to find any matching variable. 937 */ 938void 939tfeedback_decl::init(struct gl_context *ctx, const void *mem_ctx, 940 const char *input) 941{ 942 /* We don't have to be pedantic about what is a valid GLSL variable name, 943 * because any variable with an invalid name can't exist in the IR anyway. 944 */ 945 946 this->location = -1; 947 this->orig_name = input; 948 this->lowered_builtin_array_variable = none; 949 this->skip_components = 0; 950 this->next_buffer_separator = false; 951 this->matched_candidate = NULL; 952 this->stream_id = 0; 953 this->buffer = 0; 954 this->offset = 0; 955 956 if (ctx->Extensions.ARB_transform_feedback3) { 957 /* Parse gl_NextBuffer. */ 958 if (strcmp(input, "gl_NextBuffer") == 0) { 959 this->next_buffer_separator = true; 960 return; 961 } 962 963 /* Parse gl_SkipComponents. */ 964 if (strcmp(input, "gl_SkipComponents1") == 0) 965 this->skip_components = 1; 966 else if (strcmp(input, "gl_SkipComponents2") == 0) 967 this->skip_components = 2; 968 else if (strcmp(input, "gl_SkipComponents3") == 0) 969 this->skip_components = 3; 970 else if (strcmp(input, "gl_SkipComponents4") == 0) 971 this->skip_components = 4; 972 973 if (this->skip_components) 974 return; 975 } 976 977 /* Parse a declaration. */ 978 const char *base_name_end; 979 long subscript = parse_program_resource_name(input, &base_name_end); 980 this->var_name = ralloc_strndup(mem_ctx, input, base_name_end - input); 981 if (this->var_name == NULL) { 982 _mesa_error_no_memory(__func__); 983 return; 984 } 985 986 if (subscript >= 0) { 987 this->array_subscript = subscript; 988 this->is_subscripted = true; 989 } else { 990 this->is_subscripted = false; 991 } 992 993 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this 994 * class must behave specially to account for the fact that gl_ClipDistance 995 * is converted from a float[8] to a vec4[2]. 996 */ 997 if (ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerCombinedClipCullDistance && 998 strcmp(this->var_name, "gl_ClipDistance") == 0) { 999 this->lowered_builtin_array_variable = clip_distance; 1000 } 1001 if (ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerCombinedClipCullDistance && 1002 strcmp(this->var_name, "gl_CullDistance") == 0) { 1003 this->lowered_builtin_array_variable = cull_distance; 1004 } 1005 1006 if (ctx->Const.LowerTessLevel && 1007 (strcmp(this->var_name, "gl_TessLevelOuter") == 0)) 1008 this->lowered_builtin_array_variable = tess_level_outer; 1009 if (ctx->Const.LowerTessLevel && 1010 (strcmp(this->var_name, "gl_TessLevelInner") == 0)) 1011 this->lowered_builtin_array_variable = tess_level_inner; 1012} 1013 1014 1015/** 1016 * Determine whether two tfeedback_decl objects refer to the same variable and 1017 * array index (if applicable). 1018 */ 1019bool 1020tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y) 1021{ 1022 assert(x.is_varying() && y.is_varying()); 1023 1024 if (strcmp(x.var_name, y.var_name) != 0) 1025 return false; 1026 if (x.is_subscripted != y.is_subscripted) 1027 return false; 1028 if (x.is_subscripted && x.array_subscript != y.array_subscript) 1029 return false; 1030 return true; 1031} 1032 1033 1034/** 1035 * Assign a location and stream ID for this tfeedback_decl object based on the 1036 * transform feedback candidate found by find_candidate. 1037 * 1038 * If an error occurs, the error is reported through linker_error() and false 1039 * is returned. 1040 */ 1041bool 1042tfeedback_decl::assign_location(struct gl_context *ctx, 1043 struct gl_shader_program *prog) 1044{ 1045 assert(this->is_varying()); 1046 1047 unsigned fine_location 1048 = this->matched_candidate->toplevel_var->data.location * 4 1049 + this->matched_candidate->toplevel_var->data.location_frac 1050 + this->matched_candidate->offset; 1051 const unsigned dmul = 1052 this->matched_candidate->type->without_array()->is_64bit() ? 2 : 1; 1053 1054 if (this->matched_candidate->type->is_array()) { 1055 /* Array variable */ 1056 const unsigned matrix_cols = 1057 this->matched_candidate->type->fields.array->matrix_columns; 1058 const unsigned vector_elements = 1059 this->matched_candidate->type->fields.array->vector_elements; 1060 unsigned actual_array_size; 1061 switch (this->lowered_builtin_array_variable) { 1062 case clip_distance: 1063 actual_array_size = prog->last_vert_prog ? 1064 prog->last_vert_prog->info.clip_distance_array_size : 0; 1065 break; 1066 case cull_distance: 1067 actual_array_size = prog->last_vert_prog ? 1068 prog->last_vert_prog->info.cull_distance_array_size : 0; 1069 break; 1070 case tess_level_outer: 1071 actual_array_size = 4; 1072 break; 1073 case tess_level_inner: 1074 actual_array_size = 2; 1075 break; 1076 case none: 1077 default: 1078 actual_array_size = this->matched_candidate->type->array_size(); 1079 break; 1080 } 1081 1082 if (this->is_subscripted) { 1083 /* Check array bounds. */ 1084 if (this->array_subscript >= actual_array_size) { 1085 linker_error(prog, "Transform feedback varying %s has index " 1086 "%i, but the array size is %u.", 1087 this->orig_name, this->array_subscript, 1088 actual_array_size); 1089 return false; 1090 } 1091 unsigned array_elem_size = this->lowered_builtin_array_variable ? 1092 1 : vector_elements * matrix_cols * dmul; 1093 fine_location += array_elem_size * this->array_subscript; 1094 this->size = 1; 1095 } else { 1096 this->size = actual_array_size; 1097 } 1098 this->vector_elements = vector_elements; 1099 this->matrix_columns = matrix_cols; 1100 if (this->lowered_builtin_array_variable) 1101 this->type = GL_FLOAT; 1102 else 1103 this->type = this->matched_candidate->type->fields.array->gl_type; 1104 } else { 1105 /* Regular variable (scalar, vector, or matrix) */ 1106 if (this->is_subscripted) { 1107 linker_error(prog, "Transform feedback varying %s requested, " 1108 "but %s is not an array.", 1109 this->orig_name, this->var_name); 1110 return false; 1111 } 1112 this->size = 1; 1113 this->vector_elements = this->matched_candidate->type->vector_elements; 1114 this->matrix_columns = this->matched_candidate->type->matrix_columns; 1115 this->type = this->matched_candidate->type->gl_type; 1116 } 1117 this->location = fine_location / 4; 1118 this->location_frac = fine_location % 4; 1119 1120 /* From GL_EXT_transform_feedback: 1121 * A program will fail to link if: 1122 * 1123 * * the total number of components to capture in any varying 1124 * variable in <varyings> is greater than the constant 1125 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the 1126 * buffer mode is SEPARATE_ATTRIBS_EXT; 1127 */ 1128 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS && 1129 this->num_components() > 1130 ctx->Const.MaxTransformFeedbackSeparateComponents) { 1131 linker_error(prog, "Transform feedback varying %s exceeds " 1132 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.", 1133 this->orig_name); 1134 return false; 1135 } 1136 1137 /* Only transform feedback varyings can be assigned to non-zero streams, 1138 * so assign the stream id here. 1139 */ 1140 this->stream_id = this->matched_candidate->toplevel_var->data.stream; 1141 1142 unsigned array_offset = this->array_subscript * 4 * dmul; 1143 unsigned struct_offset = this->matched_candidate->offset * 4 * dmul; 1144 this->buffer = this->matched_candidate->toplevel_var->data.xfb_buffer; 1145 this->offset = this->matched_candidate->toplevel_var->data.offset + 1146 array_offset + struct_offset; 1147 1148 return true; 1149} 1150 1151 1152unsigned 1153tfeedback_decl::get_num_outputs() const 1154{ 1155 if (!this->is_varying()) { 1156 return 0; 1157 } 1158 return (this->num_components() + this->location_frac + 3)/4; 1159} 1160 1161 1162/** 1163 * Update gl_transform_feedback_info to reflect this tfeedback_decl. 1164 * 1165 * If an error occurs, the error is reported through linker_error() and false 1166 * is returned. 1167 */ 1168bool 1169tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog, 1170 struct gl_transform_feedback_info *info, 1171 unsigned buffer, unsigned buffer_index, 1172 const unsigned max_outputs, 1173 BITSET_WORD *used_components[MAX_FEEDBACK_BUFFERS], 1174 bool *explicit_stride, bool has_xfb_qualifiers, 1175 const void* mem_ctx) const 1176{ 1177 unsigned xfb_offset = 0; 1178 unsigned size = this->size; 1179 /* Handle gl_SkipComponents. */ 1180 if (this->skip_components) { 1181 info->Buffers[buffer].Stride += this->skip_components; 1182 size = this->skip_components; 1183 goto store_varying; 1184 } 1185 1186 if (this->next_buffer_separator) { 1187 size = 0; 1188 goto store_varying; 1189 } 1190 1191 if (has_xfb_qualifiers) { 1192 xfb_offset = this->offset / 4; 1193 } else { 1194 xfb_offset = info->Buffers[buffer].Stride; 1195 } 1196 info->Varyings[info->NumVarying].Offset = xfb_offset * 4; 1197 1198 { 1199 unsigned location = this->location; 1200 unsigned location_frac = this->location_frac; 1201 unsigned num_components = this->num_components(); 1202 1203 /* From GL_EXT_transform_feedback: 1204 * 1205 * " A program will fail to link if: 1206 * 1207 * * the total number of components to capture is greater than the 1208 * constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT 1209 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT." 1210 * 1211 * From GL_ARB_enhanced_layouts: 1212 * 1213 * " The resulting stride (implicit or explicit) must be less than or 1214 * equal to the implementation-dependent constant 1215 * gl_MaxTransformFeedbackInterleavedComponents." 1216 */ 1217 if ((prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS || 1218 has_xfb_qualifiers) && 1219 xfb_offset + num_components > 1220 ctx->Const.MaxTransformFeedbackInterleavedComponents) { 1221 linker_error(prog, 1222 "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS " 1223 "limit has been exceeded."); 1224 return false; 1225 } 1226 1227 /* From the OpenGL 4.60.5 spec, section 4.4.2. Output Layout Qualifiers, 1228 * Page 76, (Transform Feedback Layout Qualifiers): 1229 * 1230 * " No aliasing in output buffers is allowed: It is a compile-time or 1231 * link-time error to specify variables with overlapping transform 1232 * feedback offsets." 1233 */ 1234 const unsigned max_components = 1235 ctx->Const.MaxTransformFeedbackInterleavedComponents; 1236 const unsigned first_component = xfb_offset; 1237 const unsigned last_component = xfb_offset + num_components - 1; 1238 const unsigned start_word = BITSET_BITWORD(first_component); 1239 const unsigned end_word = BITSET_BITWORD(last_component); 1240 BITSET_WORD *used; 1241 assert(last_component < max_components); 1242 1243 if (!used_components[buffer]) { 1244 used_components[buffer] = 1245 rzalloc_array(mem_ctx, BITSET_WORD, BITSET_WORDS(max_components)); 1246 } 1247 used = used_components[buffer]; 1248 1249 for (unsigned word = start_word; word <= end_word; word++) { 1250 unsigned start_range = 0; 1251 unsigned end_range = BITSET_WORDBITS - 1; 1252 1253 if (word == start_word) 1254 start_range = first_component % BITSET_WORDBITS; 1255 1256 if (word == end_word) 1257 end_range = last_component % BITSET_WORDBITS; 1258 1259 if (used[word] & BITSET_RANGE(start_range, end_range)) { 1260 linker_error(prog, 1261 "variable '%s', xfb_offset (%d) is causing aliasing.", 1262 this->orig_name, xfb_offset * 4); 1263 return false; 1264 } 1265 used[word] |= BITSET_RANGE(start_range, end_range); 1266 } 1267 1268 while (num_components > 0) { 1269 unsigned output_size = MIN2(num_components, 4 - location_frac); 1270 assert((info->NumOutputs == 0 && max_outputs == 0) || 1271 info->NumOutputs < max_outputs); 1272 1273 /* From the ARB_enhanced_layouts spec: 1274 * 1275 * "If such a block member or variable is not written during a shader 1276 * invocation, the buffer contents at the assigned offset will be 1277 * undefined. Even if there are no static writes to a variable or 1278 * member that is assigned a transform feedback offset, the space is 1279 * still allocated in the buffer and still affects the stride." 1280 */ 1281 if (this->is_varying_written()) { 1282 info->Outputs[info->NumOutputs].ComponentOffset = location_frac; 1283 info->Outputs[info->NumOutputs].OutputRegister = location; 1284 info->Outputs[info->NumOutputs].NumComponents = output_size; 1285 info->Outputs[info->NumOutputs].StreamId = stream_id; 1286 info->Outputs[info->NumOutputs].OutputBuffer = buffer; 1287 info->Outputs[info->NumOutputs].DstOffset = xfb_offset; 1288 ++info->NumOutputs; 1289 } 1290 info->Buffers[buffer].Stream = this->stream_id; 1291 xfb_offset += output_size; 1292 1293 num_components -= output_size; 1294 location++; 1295 location_frac = 0; 1296 } 1297 } 1298 1299 if (explicit_stride && explicit_stride[buffer]) { 1300 if (this->is_64bit() && info->Buffers[buffer].Stride % 2) { 1301 linker_error(prog, "invalid qualifier xfb_stride=%d must be a " 1302 "multiple of 8 as its applied to a type that is or " 1303 "contains a double.", 1304 info->Buffers[buffer].Stride * 4); 1305 return false; 1306 } 1307 1308 if (xfb_offset > info->Buffers[buffer].Stride) { 1309 linker_error(prog, "xfb_offset (%d) overflows xfb_stride (%d) for " 1310 "buffer (%d)", xfb_offset * 4, 1311 info->Buffers[buffer].Stride * 4, buffer); 1312 return false; 1313 } 1314 } else { 1315 info->Buffers[buffer].Stride = xfb_offset; 1316 } 1317 1318 store_varying: 1319 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, 1320 this->orig_name); 1321 info->Varyings[info->NumVarying].Type = this->type; 1322 info->Varyings[info->NumVarying].Size = size; 1323 info->Varyings[info->NumVarying].BufferIndex = buffer_index; 1324 info->NumVarying++; 1325 info->Buffers[buffer].NumVaryings++; 1326 1327 return true; 1328} 1329 1330 1331const tfeedback_candidate * 1332tfeedback_decl::find_candidate(gl_shader_program *prog, 1333 hash_table *tfeedback_candidates) 1334{ 1335 const char *name = this->var_name; 1336 switch (this->lowered_builtin_array_variable) { 1337 case none: 1338 name = this->var_name; 1339 break; 1340 case clip_distance: 1341 name = "gl_ClipDistanceMESA"; 1342 break; 1343 case cull_distance: 1344 name = "gl_CullDistanceMESA"; 1345 break; 1346 case tess_level_outer: 1347 name = "gl_TessLevelOuterMESA"; 1348 break; 1349 case tess_level_inner: 1350 name = "gl_TessLevelInnerMESA"; 1351 break; 1352 } 1353 hash_entry *entry = _mesa_hash_table_search(tfeedback_candidates, name); 1354 1355 this->matched_candidate = entry ? 1356 (const tfeedback_candidate *) entry->data : NULL; 1357 1358 if (!this->matched_candidate) { 1359 /* From GL_EXT_transform_feedback: 1360 * A program will fail to link if: 1361 * 1362 * * any variable name specified in the <varyings> array is not 1363 * declared as an output in the geometry shader (if present) or 1364 * the vertex shader (if no geometry shader is present); 1365 */ 1366 linker_error(prog, "Transform feedback varying %s undeclared.", 1367 this->orig_name); 1368 } 1369 1370 return this->matched_candidate; 1371} 1372 1373 1374/** 1375 * Parse all the transform feedback declarations that were passed to 1376 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects. 1377 * 1378 * If an error occurs, the error is reported through linker_error() and false 1379 * is returned. 1380 */ 1381static bool 1382parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog, 1383 const void *mem_ctx, unsigned num_names, 1384 char **varying_names, tfeedback_decl *decls) 1385{ 1386 for (unsigned i = 0; i < num_names; ++i) { 1387 decls[i].init(ctx, mem_ctx, varying_names[i]); 1388 1389 if (!decls[i].is_varying()) 1390 continue; 1391 1392 /* From GL_EXT_transform_feedback: 1393 * A program will fail to link if: 1394 * 1395 * * any two entries in the <varyings> array specify the same varying 1396 * variable; 1397 * 1398 * We interpret this to mean "any two entries in the <varyings> array 1399 * specify the same varying variable and array index", since transform 1400 * feedback of arrays would be useless otherwise. 1401 */ 1402 for (unsigned j = 0; j < i; ++j) { 1403 if (decls[j].is_varying()) { 1404 if (tfeedback_decl::is_same(decls[i], decls[j])) { 1405 linker_error(prog, "Transform feedback varying %s specified " 1406 "more than once.", varying_names[i]); 1407 return false; 1408 } 1409 } 1410 } 1411 } 1412 return true; 1413} 1414 1415 1416static int 1417cmp_xfb_offset(const void * x_generic, const void * y_generic) 1418{ 1419 tfeedback_decl *x = (tfeedback_decl *) x_generic; 1420 tfeedback_decl *y = (tfeedback_decl *) y_generic; 1421 1422 if (x->get_buffer() != y->get_buffer()) 1423 return x->get_buffer() - y->get_buffer(); 1424 return x->get_offset() - y->get_offset(); 1425} 1426 1427/** 1428 * Store transform feedback location assignments into 1429 * prog->sh.LinkedTransformFeedback based on the data stored in 1430 * tfeedback_decls. 1431 * 1432 * If an error occurs, the error is reported through linker_error() and false 1433 * is returned. 1434 */ 1435static bool 1436store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog, 1437 unsigned num_tfeedback_decls, 1438 tfeedback_decl *tfeedback_decls, bool has_xfb_qualifiers, 1439 const void *mem_ctx) 1440{ 1441 if (!prog->last_vert_prog) 1442 return true; 1443 1444 /* Make sure MaxTransformFeedbackBuffers is less than 32 so the bitmask for 1445 * tracking the number of buffers doesn't overflow. 1446 */ 1447 assert(ctx->Const.MaxTransformFeedbackBuffers < 32); 1448 1449 bool separate_attribs_mode = 1450 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS; 1451 1452 struct gl_program *xfb_prog = prog->last_vert_prog; 1453 xfb_prog->sh.LinkedTransformFeedback = 1454 rzalloc(xfb_prog, struct gl_transform_feedback_info); 1455 1456 /* The xfb_offset qualifier does not have to be used in increasing order 1457 * however some drivers expect to receive the list of transform feedback 1458 * declarations in order so sort it now for convenience. 1459 */ 1460 if (has_xfb_qualifiers) { 1461 qsort(tfeedback_decls, num_tfeedback_decls, sizeof(*tfeedback_decls), 1462 cmp_xfb_offset); 1463 } 1464 1465 xfb_prog->sh.LinkedTransformFeedback->Varyings = 1466 rzalloc_array(xfb_prog, struct gl_transform_feedback_varying_info, 1467 num_tfeedback_decls); 1468 1469 unsigned num_outputs = 0; 1470 for (unsigned i = 0; i < num_tfeedback_decls; ++i) { 1471 if (tfeedback_decls[i].is_varying_written()) 1472 num_outputs += tfeedback_decls[i].get_num_outputs(); 1473 } 1474 1475 xfb_prog->sh.LinkedTransformFeedback->Outputs = 1476 rzalloc_array(xfb_prog, struct gl_transform_feedback_output, 1477 num_outputs); 1478 1479 unsigned num_buffers = 0; 1480 unsigned buffers = 0; 1481 BITSET_WORD *used_components[MAX_FEEDBACK_BUFFERS] = {}; 1482 1483 if (!has_xfb_qualifiers && separate_attribs_mode) { 1484 /* GL_SEPARATE_ATTRIBS */ 1485 for (unsigned i = 0; i < num_tfeedback_decls; ++i) { 1486 if (!tfeedback_decls[i].store(ctx, prog, 1487 xfb_prog->sh.LinkedTransformFeedback, 1488 num_buffers, num_buffers, num_outputs, 1489 used_components, NULL, 1490 has_xfb_qualifiers, mem_ctx)) 1491 return false; 1492 1493 buffers |= 1 << num_buffers; 1494 num_buffers++; 1495 } 1496 } 1497 else { 1498 /* GL_INVERLEAVED_ATTRIBS */ 1499 int buffer_stream_id = -1; 1500 unsigned buffer = 1501 num_tfeedback_decls ? tfeedback_decls[0].get_buffer() : 0; 1502 bool explicit_stride[MAX_FEEDBACK_BUFFERS] = { false }; 1503 1504 /* Apply any xfb_stride global qualifiers */ 1505 if (has_xfb_qualifiers) { 1506 for (unsigned j = 0; j < MAX_FEEDBACK_BUFFERS; j++) { 1507 if (prog->TransformFeedback.BufferStride[j]) { 1508 explicit_stride[j] = true; 1509 xfb_prog->sh.LinkedTransformFeedback->Buffers[j].Stride = 1510 prog->TransformFeedback.BufferStride[j] / 4; 1511 } 1512 } 1513 } 1514 1515 for (unsigned i = 0; i < num_tfeedback_decls; ++i) { 1516 if (has_xfb_qualifiers && 1517 buffer != tfeedback_decls[i].get_buffer()) { 1518 /* we have moved to the next buffer so reset stream id */ 1519 buffer_stream_id = -1; 1520 num_buffers++; 1521 } 1522 1523 if (tfeedback_decls[i].is_next_buffer_separator()) { 1524 if (!tfeedback_decls[i].store(ctx, prog, 1525 xfb_prog->sh.LinkedTransformFeedback, 1526 buffer, num_buffers, num_outputs, 1527 used_components, explicit_stride, 1528 has_xfb_qualifiers, mem_ctx)) 1529 return false; 1530 num_buffers++; 1531 buffer_stream_id = -1; 1532 continue; 1533 } 1534 1535 if (has_xfb_qualifiers) { 1536 buffer = tfeedback_decls[i].get_buffer(); 1537 } else { 1538 buffer = num_buffers; 1539 } 1540 1541 if (tfeedback_decls[i].is_varying()) { 1542 if (buffer_stream_id == -1) { 1543 /* First varying writing to this buffer: remember its stream */ 1544 buffer_stream_id = (int) tfeedback_decls[i].get_stream_id(); 1545 1546 /* Only mark a buffer as active when there is a varying 1547 * attached to it. This behaviour is based on a revised version 1548 * of section 13.2.2 of the GL 4.6 spec. 1549 */ 1550 buffers |= 1 << buffer; 1551 } else if (buffer_stream_id != 1552 (int) tfeedback_decls[i].get_stream_id()) { 1553 /* Varying writes to the same buffer from a different stream */ 1554 linker_error(prog, 1555 "Transform feedback can't capture varyings belonging " 1556 "to different vertex streams in a single buffer. " 1557 "Varying %s writes to buffer from stream %u, other " 1558 "varyings in the same buffer write from stream %u.", 1559 tfeedback_decls[i].name(), 1560 tfeedback_decls[i].get_stream_id(), 1561 buffer_stream_id); 1562 return false; 1563 } 1564 } 1565 1566 if (!tfeedback_decls[i].store(ctx, prog, 1567 xfb_prog->sh.LinkedTransformFeedback, 1568 buffer, num_buffers, num_outputs, 1569 used_components, explicit_stride, 1570 has_xfb_qualifiers, mem_ctx)) 1571 return false; 1572 } 1573 } 1574 1575 assert(xfb_prog->sh.LinkedTransformFeedback->NumOutputs == num_outputs); 1576 1577 xfb_prog->sh.LinkedTransformFeedback->ActiveBuffers = buffers; 1578 return true; 1579} 1580 1581namespace { 1582 1583/** 1584 * Data structure recording the relationship between outputs of one shader 1585 * stage (the "producer") and inputs of another (the "consumer"). 1586 */ 1587class varying_matches 1588{ 1589public: 1590 varying_matches(bool disable_varying_packing, bool xfb_enabled, 1591 bool enhanced_layouts_enabled, 1592 gl_shader_stage producer_stage, 1593 gl_shader_stage consumer_stage); 1594 ~varying_matches(); 1595 void record(ir_variable *producer_var, ir_variable *consumer_var); 1596 unsigned assign_locations(struct gl_shader_program *prog, 1597 uint8_t components[], 1598 uint64_t reserved_slots); 1599 void store_locations() const; 1600 1601private: 1602 bool is_varying_packing_safe(const glsl_type *type, 1603 const ir_variable *var) const; 1604 1605 /** 1606 * If true, this driver disables varying packing, so all varyings need to 1607 * be aligned on slot boundaries, and take up a number of slots equal to 1608 * their number of matrix columns times their array size. 1609 * 1610 * Packing may also be disabled because our current packing method is not 1611 * safe in SSO or versions of OpenGL where interpolation qualifiers are not 1612 * guaranteed to match across stages. 1613 */ 1614 const bool disable_varying_packing; 1615 1616 /** 1617 * If true, this driver has transform feedback enabled. The transform 1618 * feedback code requires at least some packing be done even when varying 1619 * packing is disabled, fortunately where transform feedback requires 1620 * packing it's safe to override the disabled setting. See 1621 * is_varying_packing_safe(). 1622 */ 1623 const bool xfb_enabled; 1624 1625 const bool enhanced_layouts_enabled; 1626 1627 /** 1628 * Enum representing the order in which varyings are packed within a 1629 * packing class. 1630 * 1631 * Currently we pack vec4's first, then vec2's, then scalar values, then 1632 * vec3's. This order ensures that the only vectors that are at risk of 1633 * having to be "double parked" (split between two adjacent varying slots) 1634 * are the vec3's. 1635 */ 1636 enum packing_order_enum { 1637 PACKING_ORDER_VEC4, 1638 PACKING_ORDER_VEC2, 1639 PACKING_ORDER_SCALAR, 1640 PACKING_ORDER_VEC3, 1641 }; 1642 1643 static unsigned compute_packing_class(const ir_variable *var); 1644 static packing_order_enum compute_packing_order(const ir_variable *var); 1645 static int match_comparator(const void *x_generic, const void *y_generic); 1646 static int xfb_comparator(const void *x_generic, const void *y_generic); 1647 1648 /** 1649 * Structure recording the relationship between a single producer output 1650 * and a single consumer input. 1651 */ 1652 struct match { 1653 /** 1654 * Packing class for this varying, computed by compute_packing_class(). 1655 */ 1656 unsigned packing_class; 1657 1658 /** 1659 * Packing order for this varying, computed by compute_packing_order(). 1660 */ 1661 packing_order_enum packing_order; 1662 unsigned num_components; 1663 1664 /** 1665 * The output variable in the producer stage. 1666 */ 1667 ir_variable *producer_var; 1668 1669 /** 1670 * The input variable in the consumer stage. 1671 */ 1672 ir_variable *consumer_var; 1673 1674 /** 1675 * The location which has been assigned for this varying. This is 1676 * expressed in multiples of a float, with the first generic varying 1677 * (i.e. the one referred to by VARYING_SLOT_VAR0) represented by the 1678 * value 0. 1679 */ 1680 unsigned generic_location; 1681 } *matches; 1682 1683 /** 1684 * The number of elements in the \c matches array that are currently in 1685 * use. 1686 */ 1687 unsigned num_matches; 1688 1689 /** 1690 * The number of elements that were set aside for the \c matches array when 1691 * it was allocated. 1692 */ 1693 unsigned matches_capacity; 1694 1695 gl_shader_stage producer_stage; 1696 gl_shader_stage consumer_stage; 1697}; 1698 1699} /* anonymous namespace */ 1700 1701varying_matches::varying_matches(bool disable_varying_packing, 1702 bool xfb_enabled, 1703 bool enhanced_layouts_enabled, 1704 gl_shader_stage producer_stage, 1705 gl_shader_stage consumer_stage) 1706 : disable_varying_packing(disable_varying_packing), 1707 xfb_enabled(xfb_enabled), 1708 enhanced_layouts_enabled(enhanced_layouts_enabled), 1709 producer_stage(producer_stage), 1710 consumer_stage(consumer_stage) 1711{ 1712 /* Note: this initial capacity is rather arbitrarily chosen to be large 1713 * enough for many cases without wasting an unreasonable amount of space. 1714 * varying_matches::record() will resize the array if there are more than 1715 * this number of varyings. 1716 */ 1717 this->matches_capacity = 8; 1718 this->matches = (match *) 1719 malloc(sizeof(*this->matches) * this->matches_capacity); 1720 this->num_matches = 0; 1721} 1722 1723 1724varying_matches::~varying_matches() 1725{ 1726 free(this->matches); 1727} 1728 1729 1730/** 1731 * Packing is always safe on individual arrays, structures, and matrices. It 1732 * is also safe if the varying is only used for transform feedback. 1733 */ 1734bool 1735varying_matches::is_varying_packing_safe(const glsl_type *type, 1736 const ir_variable *var) const 1737{ 1738 if (consumer_stage == MESA_SHADER_TESS_EVAL || 1739 consumer_stage == MESA_SHADER_TESS_CTRL || 1740 producer_stage == MESA_SHADER_TESS_CTRL) 1741 return false; 1742 1743 return xfb_enabled && (type->is_array() || type->is_struct() || 1744 type->is_matrix() || var->data.is_xfb_only); 1745} 1746 1747 1748/** 1749 * Record the given producer/consumer variable pair in the list of variables 1750 * that should later be assigned locations. 1751 * 1752 * It is permissible for \c consumer_var to be NULL (this happens if a 1753 * variable is output by the producer and consumed by transform feedback, but 1754 * not consumed by the consumer). 1755 * 1756 * If \c producer_var has already been paired up with a consumer_var, or 1757 * producer_var is part of fixed pipeline functionality (and hence already has 1758 * a location assigned), this function has no effect. 1759 * 1760 * Note: as a side effect this function may change the interpolation type of 1761 * \c producer_var, but only when the change couldn't possibly affect 1762 * rendering. 1763 */ 1764void 1765varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var) 1766{ 1767 assert(producer_var != NULL || consumer_var != NULL); 1768 1769 if ((producer_var && (!producer_var->data.is_unmatched_generic_inout || 1770 producer_var->data.explicit_location)) || 1771 (consumer_var && (!consumer_var->data.is_unmatched_generic_inout || 1772 consumer_var->data.explicit_location))) { 1773 /* Either a location already exists for this variable (since it is part 1774 * of fixed functionality), or it has already been recorded as part of a 1775 * previous match. 1776 */ 1777 return; 1778 } 1779 1780 bool needs_flat_qualifier = consumer_var == NULL && 1781 (producer_var->type->contains_integer() || 1782 producer_var->type->contains_double()); 1783 1784 if (!disable_varying_packing && 1785 (needs_flat_qualifier || 1786 (consumer_stage != MESA_SHADER_NONE && consumer_stage != MESA_SHADER_FRAGMENT))) { 1787 /* Since this varying is not being consumed by the fragment shader, its 1788 * interpolation type varying cannot possibly affect rendering. 1789 * Also, this variable is non-flat and is (or contains) an integer 1790 * or a double. 1791 * If the consumer stage is unknown, don't modify the interpolation 1792 * type as it could affect rendering later with separate shaders. 1793 * 1794 * lower_packed_varyings requires all integer varyings to flat, 1795 * regardless of where they appear. We can trivially satisfy that 1796 * requirement by changing the interpolation type to flat here. 1797 */ 1798 if (producer_var) { 1799 producer_var->data.centroid = false; 1800 producer_var->data.sample = false; 1801 producer_var->data.interpolation = INTERP_MODE_FLAT; 1802 } 1803 1804 if (consumer_var) { 1805 consumer_var->data.centroid = false; 1806 consumer_var->data.sample = false; 1807 consumer_var->data.interpolation = INTERP_MODE_FLAT; 1808 } 1809 } 1810 1811 if (this->num_matches == this->matches_capacity) { 1812 this->matches_capacity *= 2; 1813 this->matches = (match *) 1814 realloc(this->matches, 1815 sizeof(*this->matches) * this->matches_capacity); 1816 } 1817 1818 /* We must use the consumer to compute the packing class because in GL4.4+ 1819 * there is no guarantee interpolation qualifiers will match across stages. 1820 * 1821 * From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.30 spec: 1822 * 1823 * "The type and presence of interpolation qualifiers of variables with 1824 * the same name declared in all linked shaders for the same cross-stage 1825 * interface must match, otherwise the link command will fail. 1826 * 1827 * When comparing an output from one stage to an input of a subsequent 1828 * stage, the input and output don't match if their interpolation 1829 * qualifiers (or lack thereof) are not the same." 1830 * 1831 * This text was also in at least revison 7 of the 4.40 spec but is no 1832 * longer in revision 9 and not in the 4.50 spec. 1833 */ 1834 const ir_variable *const var = (consumer_var != NULL) 1835 ? consumer_var : producer_var; 1836 const gl_shader_stage stage = (consumer_var != NULL) 1837 ? consumer_stage : producer_stage; 1838 const glsl_type *type = get_varying_type(var, stage); 1839 1840 if (producer_var && consumer_var && 1841 consumer_var->data.must_be_shader_input) { 1842 producer_var->data.must_be_shader_input = 1; 1843 } 1844 1845 this->matches[this->num_matches].packing_class 1846 = this->compute_packing_class(var); 1847 this->matches[this->num_matches].packing_order 1848 = this->compute_packing_order(var); 1849 if ((this->disable_varying_packing && !is_varying_packing_safe(type, var)) || 1850 var->data.must_be_shader_input) { 1851 unsigned slots = type->count_attribute_slots(false); 1852 this->matches[this->num_matches].num_components = slots * 4; 1853 } else { 1854 this->matches[this->num_matches].num_components 1855 = type->component_slots(); 1856 } 1857 1858 this->matches[this->num_matches].producer_var = producer_var; 1859 this->matches[this->num_matches].consumer_var = consumer_var; 1860 this->num_matches++; 1861 if (producer_var) 1862 producer_var->data.is_unmatched_generic_inout = 0; 1863 if (consumer_var) 1864 consumer_var->data.is_unmatched_generic_inout = 0; 1865} 1866 1867 1868/** 1869 * Choose locations for all of the variable matches that were previously 1870 * passed to varying_matches::record(). 1871 * \param components returns array[slot] of number of components used 1872 * per slot (1, 2, 3 or 4) 1873 * \param reserved_slots bitmask indicating which varying slots are already 1874 * allocated 1875 * \return number of slots (4-element vectors) allocated 1876 */ 1877unsigned 1878varying_matches::assign_locations(struct gl_shader_program *prog, 1879 uint8_t components[], 1880 uint64_t reserved_slots) 1881{ 1882 /* If packing has been disabled then we cannot safely sort the varyings by 1883 * class as it may mean we are using a version of OpenGL where 1884 * interpolation qualifiers are not guaranteed to be matching across 1885 * shaders, sorting in this case could result in mismatching shader 1886 * interfaces. 1887 * When packing is disabled the sort orders varyings used by transform 1888 * feedback first, but also depends on *undefined behaviour* of qsort to 1889 * reverse the order of the varyings. See: xfb_comparator(). 1890 */ 1891 if (!this->disable_varying_packing) { 1892 /* Sort varying matches into an order that makes them easy to pack. */ 1893 qsort(this->matches, this->num_matches, sizeof(*this->matches), 1894 &varying_matches::match_comparator); 1895 } else { 1896 /* Only sort varyings that are only used by transform feedback. */ 1897 qsort(this->matches, this->num_matches, sizeof(*this->matches), 1898 &varying_matches::xfb_comparator); 1899 } 1900 1901 unsigned generic_location = 0; 1902 unsigned generic_patch_location = MAX_VARYING*4; 1903 bool previous_var_xfb_only = false; 1904 unsigned previous_packing_class = ~0u; 1905 1906 /* For tranform feedback separate mode, we know the number of attributes 1907 * is <= the number of buffers. So packing isn't critical. In fact, 1908 * packing vec3 attributes can cause trouble because splitting a vec3 1909 * effectively creates an additional transform feedback output. The 1910 * extra TFB output may exceed device driver limits. 1911 */ 1912 const bool dont_pack_vec3 = 1913 (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS && 1914 prog->TransformFeedback.NumVarying > 0); 1915 1916 for (unsigned i = 0; i < this->num_matches; i++) { 1917 unsigned *location = &generic_location; 1918 const ir_variable *var; 1919 const glsl_type *type; 1920 bool is_vertex_input = false; 1921 1922 if (matches[i].consumer_var) { 1923 var = matches[i].consumer_var; 1924 type = get_varying_type(var, consumer_stage); 1925 if (consumer_stage == MESA_SHADER_VERTEX) 1926 is_vertex_input = true; 1927 } else { 1928 var = matches[i].producer_var; 1929 type = get_varying_type(var, producer_stage); 1930 } 1931 1932 if (var->data.patch) 1933 location = &generic_patch_location; 1934 1935 /* Advance to the next slot if this varying has a different packing 1936 * class than the previous one, and we're not already on a slot 1937 * boundary. 1938 * 1939 * Also advance to the next slot if packing is disabled. This makes sure 1940 * we don't assign varyings the same locations which is possible 1941 * because we still pack individual arrays, records and matrices even 1942 * when packing is disabled. Note we don't advance to the next slot if 1943 * we can pack varyings together that are only used for transform 1944 * feedback. 1945 */ 1946 if (var->data.must_be_shader_input || 1947 (this->disable_varying_packing && 1948 !(previous_var_xfb_only && var->data.is_xfb_only)) || 1949 (previous_packing_class != this->matches[i].packing_class) || 1950 (this->matches[i].packing_order == PACKING_ORDER_VEC3 && 1951 dont_pack_vec3)) { 1952 *location = ALIGN(*location, 4); 1953 } 1954 1955 previous_var_xfb_only = var->data.is_xfb_only; 1956 previous_packing_class = this->matches[i].packing_class; 1957 1958 /* The number of components taken up by this variable. For vertex shader 1959 * inputs, we use the number of slots * 4, as they have different 1960 * counting rules. 1961 */ 1962 unsigned num_components = is_vertex_input ? 1963 type->count_attribute_slots(is_vertex_input) * 4 : 1964 this->matches[i].num_components; 1965 1966 /* The last slot for this variable, inclusive. */ 1967 unsigned slot_end = *location + num_components - 1; 1968 1969 /* FIXME: We could be smarter in the below code and loop back over 1970 * trying to fill any locations that we skipped because we couldn't pack 1971 * the varying between an explicit location. For now just let the user 1972 * hit the linking error if we run out of room and suggest they use 1973 * explicit locations. 1974 */ 1975 while (slot_end < MAX_VARYING * 4u) { 1976 const unsigned slots = (slot_end / 4u) - (*location / 4u) + 1; 1977 const uint64_t slot_mask = ((1ull << slots) - 1) << (*location / 4u); 1978 1979 assert(slots > 0); 1980 1981 if ((reserved_slots & slot_mask) == 0) { 1982 break; 1983 } 1984 1985 *location = ALIGN(*location + 1, 4); 1986 slot_end = *location + num_components - 1; 1987 } 1988 1989 if (!var->data.patch && slot_end >= MAX_VARYING * 4u) { 1990 linker_error(prog, "insufficient contiguous locations available for " 1991 "%s it is possible an array or struct could not be " 1992 "packed between varyings with explicit locations. Try " 1993 "using an explicit location for arrays and structs.", 1994 var->name); 1995 } 1996 1997 if (slot_end < MAX_VARYINGS_INCL_PATCH * 4u) { 1998 for (unsigned j = *location / 4u; j < slot_end / 4u; j++) 1999 components[j] = 4; 2000 components[slot_end / 4u] = (slot_end & 3) + 1; 2001 } 2002 2003 this->matches[i].generic_location = *location; 2004 2005 *location = slot_end + 1; 2006 } 2007 2008 return (generic_location + 3) / 4; 2009} 2010 2011 2012/** 2013 * Update the producer and consumer shaders to reflect the locations 2014 * assignments that were made by varying_matches::assign_locations(). 2015 */ 2016void 2017varying_matches::store_locations() const 2018{ 2019 /* Check is location needs to be packed with lower_packed_varyings() or if 2020 * we can just use ARB_enhanced_layouts packing. 2021 */ 2022 bool pack_loc[MAX_VARYINGS_INCL_PATCH] = { 0 }; 2023 const glsl_type *loc_type[MAX_VARYINGS_INCL_PATCH][4] = { {NULL, NULL} }; 2024 2025 for (unsigned i = 0; i < this->num_matches; i++) { 2026 ir_variable *producer_var = this->matches[i].producer_var; 2027 ir_variable *consumer_var = this->matches[i].consumer_var; 2028 unsigned generic_location = this->matches[i].generic_location; 2029 unsigned slot = generic_location / 4; 2030 unsigned offset = generic_location % 4; 2031 2032 if (producer_var) { 2033 producer_var->data.location = VARYING_SLOT_VAR0 + slot; 2034 producer_var->data.location_frac = offset; 2035 } 2036 2037 if (consumer_var) { 2038 assert(consumer_var->data.location == -1); 2039 consumer_var->data.location = VARYING_SLOT_VAR0 + slot; 2040 consumer_var->data.location_frac = offset; 2041 } 2042 2043 /* Find locations suitable for native packing via 2044 * ARB_enhanced_layouts. 2045 */ 2046 if (producer_var && consumer_var) { 2047 if (enhanced_layouts_enabled) { 2048 const glsl_type *type = 2049 get_varying_type(producer_var, producer_stage); 2050 if (type->is_array() || type->is_matrix() || type->is_struct() || 2051 type->is_double()) { 2052 unsigned comp_slots = type->component_slots() + offset; 2053 unsigned slots = comp_slots / 4; 2054 if (comp_slots % 4) 2055 slots += 1; 2056 2057 for (unsigned j = 0; j < slots; j++) { 2058 pack_loc[slot + j] = true; 2059 } 2060 } else if (offset + type->vector_elements > 4) { 2061 pack_loc[slot] = true; 2062 pack_loc[slot + 1] = true; 2063 } else { 2064 loc_type[slot][offset] = type; 2065 } 2066 } 2067 } 2068 } 2069 2070 /* Attempt to use ARB_enhanced_layouts for more efficient packing if 2071 * suitable. 2072 */ 2073 if (enhanced_layouts_enabled) { 2074 for (unsigned i = 0; i < this->num_matches; i++) { 2075 ir_variable *producer_var = this->matches[i].producer_var; 2076 ir_variable *consumer_var = this->matches[i].consumer_var; 2077 unsigned generic_location = this->matches[i].generic_location; 2078 unsigned slot = generic_location / 4; 2079 2080 if (pack_loc[slot] || !producer_var || !consumer_var) 2081 continue; 2082 2083 const glsl_type *type = 2084 get_varying_type(producer_var, producer_stage); 2085 bool type_match = true; 2086 for (unsigned j = 0; j < 4; j++) { 2087 if (loc_type[slot][j]) { 2088 if (type->base_type != loc_type[slot][j]->base_type) 2089 type_match = false; 2090 } 2091 } 2092 2093 if (type_match) { 2094 producer_var->data.explicit_location = 1; 2095 consumer_var->data.explicit_location = 1; 2096 producer_var->data.explicit_component = 1; 2097 consumer_var->data.explicit_component = 1; 2098 } 2099 } 2100 } 2101} 2102 2103 2104/** 2105 * Compute the "packing class" of the given varying. This is an unsigned 2106 * integer with the property that two variables in the same packing class can 2107 * be safely backed into the same vec4. 2108 */ 2109unsigned 2110varying_matches::compute_packing_class(const ir_variable *var) 2111{ 2112 /* Without help from the back-end, there is no way to pack together 2113 * variables with different interpolation types, because 2114 * lower_packed_varyings must choose exactly one interpolation type for 2115 * each packed varying it creates. 2116 * 2117 * However, we can safely pack together floats, ints, and uints, because: 2118 * 2119 * - varyings of base type "int" and "uint" must use the "flat" 2120 * interpolation type, which can only occur in GLSL 1.30 and above. 2121 * 2122 * - On platforms that support GLSL 1.30 and above, lower_packed_varyings 2123 * can store flat floats as ints without losing any information (using 2124 * the ir_unop_bitcast_* opcodes). 2125 * 2126 * Therefore, the packing class depends only on the interpolation type. 2127 */ 2128 const unsigned interp = var->is_interpolation_flat() 2129 ? unsigned(INTERP_MODE_FLAT) : var->data.interpolation; 2130 2131 assert(interp < (1 << 3)); 2132 2133 const unsigned packing_class = (interp << 0) | 2134 (var->data.centroid << 3) | 2135 (var->data.sample << 4) | 2136 (var->data.patch << 5) | 2137 (var->data.must_be_shader_input << 6); 2138 2139 return packing_class; 2140} 2141 2142 2143/** 2144 * Compute the "packing order" of the given varying. This is a sort key we 2145 * use to determine when to attempt to pack the given varying relative to 2146 * other varyings in the same packing class. 2147 */ 2148varying_matches::packing_order_enum 2149varying_matches::compute_packing_order(const ir_variable *var) 2150{ 2151 const glsl_type *element_type = var->type; 2152 2153 while (element_type->is_array()) { 2154 element_type = element_type->fields.array; 2155 } 2156 2157 switch (element_type->component_slots() % 4) { 2158 case 1: return PACKING_ORDER_SCALAR; 2159 case 2: return PACKING_ORDER_VEC2; 2160 case 3: return PACKING_ORDER_VEC3; 2161 case 0: return PACKING_ORDER_VEC4; 2162 default: 2163 assert(!"Unexpected value of vector_elements"); 2164 return PACKING_ORDER_VEC4; 2165 } 2166} 2167 2168 2169/** 2170 * Comparison function passed to qsort() to sort varyings by packing_class and 2171 * then by packing_order. 2172 */ 2173int 2174varying_matches::match_comparator(const void *x_generic, const void *y_generic) 2175{ 2176 const match *x = (const match *) x_generic; 2177 const match *y = (const match *) y_generic; 2178 2179 if (x->packing_class != y->packing_class) 2180 return x->packing_class - y->packing_class; 2181 return x->packing_order - y->packing_order; 2182} 2183 2184 2185/** 2186 * Comparison function passed to qsort() to sort varyings used only by 2187 * transform feedback when packing of other varyings is disabled. 2188 */ 2189int 2190varying_matches::xfb_comparator(const void *x_generic, const void *y_generic) 2191{ 2192 const match *x = (const match *) x_generic; 2193 2194 if (x->producer_var != NULL && x->producer_var->data.is_xfb_only) 2195 return match_comparator(x_generic, y_generic); 2196 2197 /* FIXME: When the comparator returns 0 it means the elements being 2198 * compared are equivalent. However the qsort documentation says: 2199 * 2200 * "The order of equivalent elements is undefined." 2201 * 2202 * In practice the sort ends up reversing the order of the varyings which 2203 * means locations are also assigned in this reversed order and happens to 2204 * be what we want. This is also whats happening in 2205 * varying_matches::match_comparator(). 2206 */ 2207 return 0; 2208} 2209 2210 2211/** 2212 * Is the given variable a varying variable to be counted against the 2213 * limit in ctx->Const.MaxVarying? 2214 * This includes variables such as texcoords, colors and generic 2215 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord. 2216 */ 2217static bool 2218var_counts_against_varying_limit(gl_shader_stage stage, const ir_variable *var) 2219{ 2220 /* Only fragment shaders will take a varying variable as an input */ 2221 if (stage == MESA_SHADER_FRAGMENT && 2222 var->data.mode == ir_var_shader_in) { 2223 switch (var->data.location) { 2224 case VARYING_SLOT_POS: 2225 case VARYING_SLOT_FACE: 2226 case VARYING_SLOT_PNTC: 2227 return false; 2228 default: 2229 return true; 2230 } 2231 } 2232 return false; 2233} 2234 2235 2236/** 2237 * Visitor class that generates tfeedback_candidate structs describing all 2238 * possible targets of transform feedback. 2239 * 2240 * tfeedback_candidate structs are stored in the hash table 2241 * tfeedback_candidates, which is passed to the constructor. This hash table 2242 * maps varying names to instances of the tfeedback_candidate struct. 2243 */ 2244class tfeedback_candidate_generator : public program_resource_visitor 2245{ 2246public: 2247 tfeedback_candidate_generator(void *mem_ctx, 2248 hash_table *tfeedback_candidates, 2249 gl_shader_stage stage) 2250 : mem_ctx(mem_ctx), 2251 tfeedback_candidates(tfeedback_candidates), 2252 stage(stage), 2253 toplevel_var(NULL), 2254 varying_floats(0) 2255 { 2256 } 2257 2258 void process(ir_variable *var) 2259 { 2260 /* All named varying interface blocks should be flattened by now */ 2261 assert(!var->is_interface_instance()); 2262 assert(var->data.mode == ir_var_shader_out); 2263 2264 this->toplevel_var = var; 2265 this->varying_floats = 0; 2266 const glsl_type *t = 2267 var->data.from_named_ifc_block ? var->get_interface_type() : var->type; 2268 if (!var->data.patch && stage == MESA_SHADER_TESS_CTRL) { 2269 assert(t->is_array()); 2270 t = t->fields.array; 2271 } 2272 program_resource_visitor::process(var, t, false); 2273 } 2274 2275private: 2276 virtual void visit_field(const glsl_type *type, const char *name, 2277 bool /* row_major */, 2278 const glsl_type * /* record_type */, 2279 const enum glsl_interface_packing, 2280 bool /* last_field */) 2281 { 2282 assert(!type->without_array()->is_struct()); 2283 assert(!type->without_array()->is_interface()); 2284 2285 tfeedback_candidate *candidate 2286 = rzalloc(this->mem_ctx, tfeedback_candidate); 2287 candidate->toplevel_var = this->toplevel_var; 2288 candidate->type = type; 2289 candidate->offset = this->varying_floats; 2290 _mesa_hash_table_insert(this->tfeedback_candidates, 2291 ralloc_strdup(this->mem_ctx, name), 2292 candidate); 2293 this->varying_floats += type->component_slots(); 2294 } 2295 2296 /** 2297 * Memory context used to allocate hash table keys and values. 2298 */ 2299 void * const mem_ctx; 2300 2301 /** 2302 * Hash table in which tfeedback_candidate objects should be stored. 2303 */ 2304 hash_table * const tfeedback_candidates; 2305 2306 gl_shader_stage stage; 2307 2308 /** 2309 * Pointer to the toplevel variable that is being traversed. 2310 */ 2311 ir_variable *toplevel_var; 2312 2313 /** 2314 * Total number of varying floats that have been visited so far. This is 2315 * used to determine the offset to each varying within the toplevel 2316 * variable. 2317 */ 2318 unsigned varying_floats; 2319}; 2320 2321 2322namespace linker { 2323 2324void 2325populate_consumer_input_sets(void *mem_ctx, exec_list *ir, 2326 hash_table *consumer_inputs, 2327 hash_table *consumer_interface_inputs, 2328 ir_variable *consumer_inputs_with_locations[VARYING_SLOT_TESS_MAX]) 2329{ 2330 memset(consumer_inputs_with_locations, 2331 0, 2332 sizeof(consumer_inputs_with_locations[0]) * VARYING_SLOT_TESS_MAX); 2333 2334 foreach_in_list(ir_instruction, node, ir) { 2335 ir_variable *const input_var = node->as_variable(); 2336 2337 if (input_var != NULL && input_var->data.mode == ir_var_shader_in) { 2338 /* All interface blocks should have been lowered by this point */ 2339 assert(!input_var->type->is_interface()); 2340 2341 if (input_var->data.explicit_location) { 2342 /* assign_varying_locations only cares about finding the 2343 * ir_variable at the start of a contiguous location block. 2344 * 2345 * - For !producer, consumer_inputs_with_locations isn't used. 2346 * 2347 * - For !consumer, consumer_inputs_with_locations is empty. 2348 * 2349 * For consumer && producer, if you were trying to set some 2350 * ir_variable to the middle of a location block on the other side 2351 * of producer/consumer, cross_validate_outputs_to_inputs() should 2352 * be link-erroring due to either type mismatch or location 2353 * overlaps. If the variables do match up, then they've got a 2354 * matching data.location and you only looked at 2355 * consumer_inputs_with_locations[var->data.location], not any 2356 * following entries for the array/structure. 2357 */ 2358 consumer_inputs_with_locations[input_var->data.location] = 2359 input_var; 2360 } else if (input_var->get_interface_type() != NULL) { 2361 char *const iface_field_name = 2362 ralloc_asprintf(mem_ctx, "%s.%s", 2363 input_var->get_interface_type()->without_array()->name, 2364 input_var->name); 2365 _mesa_hash_table_insert(consumer_interface_inputs, 2366 iface_field_name, input_var); 2367 } else { 2368 _mesa_hash_table_insert(consumer_inputs, 2369 ralloc_strdup(mem_ctx, input_var->name), 2370 input_var); 2371 } 2372 } 2373 } 2374} 2375 2376/** 2377 * Find a variable from the consumer that "matches" the specified variable 2378 * 2379 * This function only finds inputs with names that match. There is no 2380 * validation (here) that the types, etc. are compatible. 2381 */ 2382ir_variable * 2383get_matching_input(void *mem_ctx, 2384 const ir_variable *output_var, 2385 hash_table *consumer_inputs, 2386 hash_table *consumer_interface_inputs, 2387 ir_variable *consumer_inputs_with_locations[VARYING_SLOT_TESS_MAX]) 2388{ 2389 ir_variable *input_var; 2390 2391 if (output_var->data.explicit_location) { 2392 input_var = consumer_inputs_with_locations[output_var->data.location]; 2393 } else if (output_var->get_interface_type() != NULL) { 2394 char *const iface_field_name = 2395 ralloc_asprintf(mem_ctx, "%s.%s", 2396 output_var->get_interface_type()->without_array()->name, 2397 output_var->name); 2398 hash_entry *entry = _mesa_hash_table_search(consumer_interface_inputs, iface_field_name); 2399 input_var = entry ? (ir_variable *) entry->data : NULL; 2400 } else { 2401 hash_entry *entry = _mesa_hash_table_search(consumer_inputs, output_var->name); 2402 input_var = entry ? (ir_variable *) entry->data : NULL; 2403 } 2404 2405 return (input_var == NULL || input_var->data.mode != ir_var_shader_in) 2406 ? NULL : input_var; 2407} 2408 2409} 2410 2411static int 2412io_variable_cmp(const void *_a, const void *_b) 2413{ 2414 const ir_variable *const a = *(const ir_variable **) _a; 2415 const ir_variable *const b = *(const ir_variable **) _b; 2416 2417 if (a->data.explicit_location && b->data.explicit_location) 2418 return b->data.location - a->data.location; 2419 2420 if (a->data.explicit_location && !b->data.explicit_location) 2421 return 1; 2422 2423 if (!a->data.explicit_location && b->data.explicit_location) 2424 return -1; 2425 2426 return -strcmp(a->name, b->name); 2427} 2428 2429/** 2430 * Sort the shader IO variables into canonical order 2431 */ 2432static void 2433canonicalize_shader_io(exec_list *ir, enum ir_variable_mode io_mode) 2434{ 2435 ir_variable *var_table[MAX_PROGRAM_OUTPUTS * 4]; 2436 unsigned num_variables = 0; 2437 2438 foreach_in_list(ir_instruction, node, ir) { 2439 ir_variable *const var = node->as_variable(); 2440 2441 if (var == NULL || var->data.mode != io_mode) 2442 continue; 2443 2444 /* If we have already encountered more I/O variables that could 2445 * successfully link, bail. 2446 */ 2447 if (num_variables == ARRAY_SIZE(var_table)) 2448 return; 2449 2450 var_table[num_variables++] = var; 2451 } 2452 2453 if (num_variables == 0) 2454 return; 2455 2456 /* Sort the list in reverse order (io_variable_cmp handles this). Later 2457 * we're going to push the variables on to the IR list as a stack, so we 2458 * want the last variable (in canonical order) to be first in the list. 2459 */ 2460 qsort(var_table, num_variables, sizeof(var_table[0]), io_variable_cmp); 2461 2462 /* Remove the variable from it's current location in the IR, and put it at 2463 * the front. 2464 */ 2465 for (unsigned i = 0; i < num_variables; i++) { 2466 var_table[i]->remove(); 2467 ir->push_head(var_table[i]); 2468 } 2469} 2470 2471/** 2472 * Generate a bitfield map of the explicit locations for shader varyings. 2473 * 2474 * Note: For Tessellation shaders we are sitting right on the limits of the 2475 * 64 bit map. Per-vertex and per-patch both have separate location domains 2476 * with a max of MAX_VARYING. 2477 */ 2478static uint64_t 2479reserved_varying_slot(struct gl_linked_shader *stage, 2480 ir_variable_mode io_mode) 2481{ 2482 assert(io_mode == ir_var_shader_in || io_mode == ir_var_shader_out); 2483 /* Avoid an overflow of the returned value */ 2484 assert(MAX_VARYINGS_INCL_PATCH <= 64); 2485 2486 uint64_t slots = 0; 2487 int var_slot; 2488 2489 if (!stage) 2490 return slots; 2491 2492 foreach_in_list(ir_instruction, node, stage->ir) { 2493 ir_variable *const var = node->as_variable(); 2494 2495 if (var == NULL || var->data.mode != io_mode || 2496 !var->data.explicit_location || 2497 var->data.location < VARYING_SLOT_VAR0) 2498 continue; 2499 2500 var_slot = var->data.location - VARYING_SLOT_VAR0; 2501 2502 unsigned num_elements = get_varying_type(var, stage->Stage) 2503 ->count_attribute_slots(io_mode == ir_var_shader_in && 2504 stage->Stage == MESA_SHADER_VERTEX); 2505 for (unsigned i = 0; i < num_elements; i++) { 2506 if (var_slot >= 0 && var_slot < MAX_VARYINGS_INCL_PATCH) 2507 slots |= UINT64_C(1) << var_slot; 2508 var_slot += 1; 2509 } 2510 } 2511 2512 return slots; 2513} 2514 2515 2516/** 2517 * Assign locations for all variables that are produced in one pipeline stage 2518 * (the "producer") and consumed in the next stage (the "consumer"). 2519 * 2520 * Variables produced by the producer may also be consumed by transform 2521 * feedback. 2522 * 2523 * \param num_tfeedback_decls is the number of declarations indicating 2524 * variables that may be consumed by transform feedback. 2525 * 2526 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects 2527 * representing the result of parsing the strings passed to 2528 * glTransformFeedbackVaryings(). assign_location() will be called for 2529 * each of these objects that matches one of the outputs of the 2530 * producer. 2531 * 2532 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to 2533 * be NULL. In this case, varying locations are assigned solely based on the 2534 * requirements of transform feedback. 2535 */ 2536static bool 2537assign_varying_locations(struct gl_context *ctx, 2538 void *mem_ctx, 2539 struct gl_shader_program *prog, 2540 gl_linked_shader *producer, 2541 gl_linked_shader *consumer, 2542 unsigned num_tfeedback_decls, 2543 tfeedback_decl *tfeedback_decls, 2544 const uint64_t reserved_slots) 2545{ 2546 /* Tessellation shaders treat inputs and outputs as shared memory and can 2547 * access inputs and outputs of other invocations. 2548 * Therefore, they can't be lowered to temps easily (and definitely not 2549 * efficiently). 2550 */ 2551 bool unpackable_tess = 2552 (consumer && consumer->Stage == MESA_SHADER_TESS_EVAL) || 2553 (consumer && consumer->Stage == MESA_SHADER_TESS_CTRL) || 2554 (producer && producer->Stage == MESA_SHADER_TESS_CTRL); 2555 2556 /* Transform feedback code assumes varying arrays are packed, so if the 2557 * driver has disabled varying packing, make sure to at least enable 2558 * packing required by transform feedback. 2559 */ 2560 bool xfb_enabled = 2561 ctx->Extensions.EXT_transform_feedback && !unpackable_tess; 2562 2563 /* Disable packing on outward facing interfaces for SSO because in ES we 2564 * need to retain the unpacked varying information for draw time 2565 * validation. 2566 * 2567 * Packing is still enabled on individual arrays, structs, and matrices as 2568 * these are required by the transform feedback code and it is still safe 2569 * to do so. We also enable packing when a varying is only used for 2570 * transform feedback and its not a SSO. 2571 */ 2572 bool disable_varying_packing = 2573 ctx->Const.DisableVaryingPacking || unpackable_tess; 2574 if (prog->SeparateShader && (producer == NULL || consumer == NULL)) 2575 disable_varying_packing = true; 2576 2577 varying_matches matches(disable_varying_packing, xfb_enabled, 2578 ctx->Extensions.ARB_enhanced_layouts, 2579 producer ? producer->Stage : MESA_SHADER_NONE, 2580 consumer ? consumer->Stage : MESA_SHADER_NONE); 2581 hash_table *tfeedback_candidates = 2582 _mesa_hash_table_create(NULL, _mesa_key_hash_string, 2583 _mesa_key_string_equal); 2584 hash_table *consumer_inputs = 2585 _mesa_hash_table_create(NULL, _mesa_key_hash_string, 2586 _mesa_key_string_equal); 2587 hash_table *consumer_interface_inputs = 2588 _mesa_hash_table_create(NULL, _mesa_key_hash_string, 2589 _mesa_key_string_equal); 2590 ir_variable *consumer_inputs_with_locations[VARYING_SLOT_TESS_MAX] = { 2591 NULL, 2592 }; 2593 2594 unsigned consumer_vertices = 0; 2595 if (consumer && consumer->Stage == MESA_SHADER_GEOMETRY) 2596 consumer_vertices = prog->Geom.VerticesIn; 2597 2598 /* Operate in a total of four passes. 2599 * 2600 * 1. Sort inputs / outputs into a canonical order. This is necessary so 2601 * that inputs / outputs of separable shaders will be assigned 2602 * predictable locations regardless of the order in which declarations 2603 * appeared in the shader source. 2604 * 2605 * 2. Assign locations for any matching inputs and outputs. 2606 * 2607 * 3. Mark output variables in the producer that do not have locations as 2608 * not being outputs. This lets the optimizer eliminate them. 2609 * 2610 * 4. Mark input variables in the consumer that do not have locations as 2611 * not being inputs. This lets the optimizer eliminate them. 2612 */ 2613 if (consumer) 2614 canonicalize_shader_io(consumer->ir, ir_var_shader_in); 2615 2616 if (producer) 2617 canonicalize_shader_io(producer->ir, ir_var_shader_out); 2618 2619 if (consumer) 2620 linker::populate_consumer_input_sets(mem_ctx, consumer->ir, 2621 consumer_inputs, 2622 consumer_interface_inputs, 2623 consumer_inputs_with_locations); 2624 2625 if (producer) { 2626 foreach_in_list(ir_instruction, node, producer->ir) { 2627 ir_variable *const output_var = node->as_variable(); 2628 2629 if (output_var == NULL || output_var->data.mode != ir_var_shader_out) 2630 continue; 2631 2632 /* Only geometry shaders can use non-zero streams */ 2633 assert(output_var->data.stream == 0 || 2634 (output_var->data.stream < MAX_VERTEX_STREAMS && 2635 producer->Stage == MESA_SHADER_GEOMETRY)); 2636 2637 if (num_tfeedback_decls > 0) { 2638 tfeedback_candidate_generator g(mem_ctx, tfeedback_candidates, producer->Stage); 2639 /* From OpenGL 4.6 (Core Profile) spec, section 11.1.2.1 2640 * ("Vertex Shader Variables / Output Variables") 2641 * 2642 * "Each program object can specify a set of output variables from 2643 * one shader to be recorded in transform feedback mode (see 2644 * section 13.3). The variables that can be recorded are those 2645 * emitted by the first active shader, in order, from the 2646 * following list: 2647 * 2648 * * geometry shader 2649 * * tessellation evaluation shader 2650 * * tessellation control shader 2651 * * vertex shader" 2652 * 2653 * But on OpenGL ES 3.2, section 11.1.2.1 ("Vertex Shader 2654 * Variables / Output Variables") tessellation control shader is 2655 * not included in the stages list. 2656 */ 2657 if (!prog->IsES || producer->Stage != MESA_SHADER_TESS_CTRL) { 2658 g.process(output_var); 2659 } 2660 } 2661 2662 ir_variable *const input_var = 2663 linker::get_matching_input(mem_ctx, output_var, consumer_inputs, 2664 consumer_interface_inputs, 2665 consumer_inputs_with_locations); 2666 2667 /* If a matching input variable was found, add this output (and the 2668 * input) to the set. If this is a separable program and there is no 2669 * consumer stage, add the output. 2670 * 2671 * Always add TCS outputs. They are shared by all invocations 2672 * within a patch and can be used as shared memory. 2673 */ 2674 if (input_var || (prog->SeparateShader && consumer == NULL) || 2675 producer->Stage == MESA_SHADER_TESS_CTRL) { 2676 matches.record(output_var, input_var); 2677 } 2678 2679 /* Only stream 0 outputs can be consumed in the next stage */ 2680 if (input_var && output_var->data.stream != 0) { 2681 linker_error(prog, "output %s is assigned to stream=%d but " 2682 "is linked to an input, which requires stream=0", 2683 output_var->name, output_var->data.stream); 2684 return false; 2685 } 2686 } 2687 } else { 2688 /* If there's no producer stage, then this must be a separable program. 2689 * For example, we may have a program that has just a fragment shader. 2690 * Later this program will be used with some arbitrary vertex (or 2691 * geometry) shader program. This means that locations must be assigned 2692 * for all the inputs. 2693 */ 2694 foreach_in_list(ir_instruction, node, consumer->ir) { 2695 ir_variable *const input_var = node->as_variable(); 2696 if (input_var && input_var->data.mode == ir_var_shader_in) { 2697 matches.record(NULL, input_var); 2698 } 2699 } 2700 } 2701 2702 for (unsigned i = 0; i < num_tfeedback_decls; ++i) { 2703 if (!tfeedback_decls[i].is_varying()) 2704 continue; 2705 2706 const tfeedback_candidate *matched_candidate 2707 = tfeedback_decls[i].find_candidate(prog, tfeedback_candidates); 2708 2709 if (matched_candidate == NULL) { 2710 _mesa_hash_table_destroy(tfeedback_candidates, NULL); 2711 return false; 2712 } 2713 2714 /* Mark xfb varyings as always active */ 2715 matched_candidate->toplevel_var->data.always_active_io = 1; 2716 2717 /* Mark any corresponding inputs as always active also. We must do this 2718 * because we have a NIR pass that lowers vectors to scalars and another 2719 * that removes unused varyings. 2720 * We don't split varyings marked as always active because there is no 2721 * point in doing so. This means we need to mark both sides of the 2722 * interface as always active otherwise we will have a mismatch and 2723 * start removing things we shouldn't. 2724 */ 2725 ir_variable *const input_var = 2726 linker::get_matching_input(mem_ctx, matched_candidate->toplevel_var, 2727 consumer_inputs, 2728 consumer_interface_inputs, 2729 consumer_inputs_with_locations); 2730 if (input_var) 2731 input_var->data.always_active_io = 1; 2732 2733 if (matched_candidate->toplevel_var->data.is_unmatched_generic_inout) { 2734 matched_candidate->toplevel_var->data.is_xfb_only = 1; 2735 matches.record(matched_candidate->toplevel_var, NULL); 2736 } 2737 } 2738 2739 _mesa_hash_table_destroy(consumer_inputs, NULL); 2740 _mesa_hash_table_destroy(consumer_interface_inputs, NULL); 2741 2742 uint8_t components[MAX_VARYINGS_INCL_PATCH] = {0}; 2743 const unsigned slots_used = matches.assign_locations( 2744 prog, components, reserved_slots); 2745 matches.store_locations(); 2746 2747 for (unsigned i = 0; i < num_tfeedback_decls; ++i) { 2748 if (tfeedback_decls[i].is_varying()) { 2749 if (!tfeedback_decls[i].assign_location(ctx, prog)) { 2750 _mesa_hash_table_destroy(tfeedback_candidates, NULL); 2751 return false; 2752 } 2753 } 2754 } 2755 _mesa_hash_table_destroy(tfeedback_candidates, NULL); 2756 2757 if (consumer && producer) { 2758 foreach_in_list(ir_instruction, node, consumer->ir) { 2759 ir_variable *const var = node->as_variable(); 2760 2761 if (var && var->data.mode == ir_var_shader_in && 2762 var->data.is_unmatched_generic_inout) { 2763 if (!prog->IsES && prog->data->Version <= 120) { 2764 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec: 2765 * 2766 * Only those varying variables used (i.e. read) in 2767 * the fragment shader executable must be written to 2768 * by the vertex shader executable; declaring 2769 * superfluous varying variables in a vertex shader is 2770 * permissible. 2771 * 2772 * We interpret this text as meaning that the VS must 2773 * write the variable for the FS to read it. See 2774 * "glsl1-varying read but not written" in piglit. 2775 */ 2776 linker_error(prog, "%s shader varying %s not written " 2777 "by %s shader\n.", 2778 _mesa_shader_stage_to_string(consumer->Stage), 2779 var->name, 2780 _mesa_shader_stage_to_string(producer->Stage)); 2781 } else { 2782 linker_warning(prog, "%s shader varying %s not written " 2783 "by %s shader\n.", 2784 _mesa_shader_stage_to_string(consumer->Stage), 2785 var->name, 2786 _mesa_shader_stage_to_string(producer->Stage)); 2787 } 2788 } 2789 } 2790 2791 /* Now that validation is done its safe to remove unused varyings. As 2792 * we have both a producer and consumer its safe to remove unused 2793 * varyings even if the program is a SSO because the stages are being 2794 * linked together i.e. we have a multi-stage SSO. 2795 */ 2796 remove_unused_shader_inputs_and_outputs(false, producer, 2797 ir_var_shader_out); 2798 remove_unused_shader_inputs_and_outputs(false, consumer, 2799 ir_var_shader_in); 2800 } 2801 2802 if (producer) { 2803 lower_packed_varyings(mem_ctx, slots_used, components, ir_var_shader_out, 2804 0, producer, disable_varying_packing, 2805 xfb_enabled); 2806 } 2807 2808 if (consumer) { 2809 lower_packed_varyings(mem_ctx, slots_used, components, ir_var_shader_in, 2810 consumer_vertices, consumer, 2811 disable_varying_packing, xfb_enabled); 2812 } 2813 2814 return true; 2815} 2816 2817static bool 2818check_against_output_limit(struct gl_context *ctx, 2819 struct gl_shader_program *prog, 2820 gl_linked_shader *producer, 2821 unsigned num_explicit_locations) 2822{ 2823 unsigned output_vectors = num_explicit_locations; 2824 2825 foreach_in_list(ir_instruction, node, producer->ir) { 2826 ir_variable *const var = node->as_variable(); 2827 2828 if (var && !var->data.explicit_location && 2829 var->data.mode == ir_var_shader_out && 2830 var_counts_against_varying_limit(producer->Stage, var)) { 2831 /* outputs for fragment shader can't be doubles */ 2832 output_vectors += var->type->count_attribute_slots(false); 2833 } 2834 } 2835 2836 assert(producer->Stage != MESA_SHADER_FRAGMENT); 2837 unsigned max_output_components = 2838 ctx->Const.Program[producer->Stage].MaxOutputComponents; 2839 2840 const unsigned output_components = output_vectors * 4; 2841 if (output_components > max_output_components) { 2842 if (ctx->API == API_OPENGLES2 || prog->IsES) 2843 linker_error(prog, "%s shader uses too many output vectors " 2844 "(%u > %u)\n", 2845 _mesa_shader_stage_to_string(producer->Stage), 2846 output_vectors, 2847 max_output_components / 4); 2848 else 2849 linker_error(prog, "%s shader uses too many output components " 2850 "(%u > %u)\n", 2851 _mesa_shader_stage_to_string(producer->Stage), 2852 output_components, 2853 max_output_components); 2854 2855 return false; 2856 } 2857 2858 return true; 2859} 2860 2861static bool 2862check_against_input_limit(struct gl_context *ctx, 2863 struct gl_shader_program *prog, 2864 gl_linked_shader *consumer, 2865 unsigned num_explicit_locations) 2866{ 2867 unsigned input_vectors = num_explicit_locations; 2868 2869 foreach_in_list(ir_instruction, node, consumer->ir) { 2870 ir_variable *const var = node->as_variable(); 2871 2872 if (var && !var->data.explicit_location && 2873 var->data.mode == ir_var_shader_in && 2874 var_counts_against_varying_limit(consumer->Stage, var)) { 2875 /* vertex inputs aren't varying counted */ 2876 input_vectors += var->type->count_attribute_slots(false); 2877 } 2878 } 2879 2880 assert(consumer->Stage != MESA_SHADER_VERTEX); 2881 unsigned max_input_components = 2882 ctx->Const.Program[consumer->Stage].MaxInputComponents; 2883 2884 const unsigned input_components = input_vectors * 4; 2885 if (input_components > max_input_components) { 2886 if (ctx->API == API_OPENGLES2 || prog->IsES) 2887 linker_error(prog, "%s shader uses too many input vectors " 2888 "(%u > %u)\n", 2889 _mesa_shader_stage_to_string(consumer->Stage), 2890 input_vectors, 2891 max_input_components / 4); 2892 else 2893 linker_error(prog, "%s shader uses too many input components " 2894 "(%u > %u)\n", 2895 _mesa_shader_stage_to_string(consumer->Stage), 2896 input_components, 2897 max_input_components); 2898 2899 return false; 2900 } 2901 2902 return true; 2903} 2904 2905bool 2906link_varyings(struct gl_shader_program *prog, unsigned first, unsigned last, 2907 struct gl_context *ctx, void *mem_ctx) 2908{ 2909 bool has_xfb_qualifiers = false; 2910 unsigned num_tfeedback_decls = 0; 2911 char **varying_names = NULL; 2912 tfeedback_decl *tfeedback_decls = NULL; 2913 2914 /* From the ARB_enhanced_layouts spec: 2915 * 2916 * "If the shader used to record output variables for transform feedback 2917 * varyings uses the "xfb_buffer", "xfb_offset", or "xfb_stride" layout 2918 * qualifiers, the values specified by TransformFeedbackVaryings are 2919 * ignored, and the set of variables captured for transform feedback is 2920 * instead derived from the specified layout qualifiers." 2921 */ 2922 for (int i = MESA_SHADER_FRAGMENT - 1; i >= 0; i--) { 2923 /* Find last stage before fragment shader */ 2924 if (prog->_LinkedShaders[i]) { 2925 has_xfb_qualifiers = 2926 process_xfb_layout_qualifiers(mem_ctx, prog->_LinkedShaders[i], 2927 prog, &num_tfeedback_decls, 2928 &varying_names); 2929 break; 2930 } 2931 } 2932 2933 if (!has_xfb_qualifiers) { 2934 num_tfeedback_decls = prog->TransformFeedback.NumVarying; 2935 varying_names = prog->TransformFeedback.VaryingNames; 2936 } 2937 2938 if (num_tfeedback_decls != 0) { 2939 /* From GL_EXT_transform_feedback: 2940 * A program will fail to link if: 2941 * 2942 * * the <count> specified by TransformFeedbackVaryingsEXT is 2943 * non-zero, but the program object has no vertex or geometry 2944 * shader; 2945 */ 2946 if (first >= MESA_SHADER_FRAGMENT) { 2947 linker_error(prog, "Transform feedback varyings specified, but " 2948 "no vertex, tessellation, or geometry shader is " 2949 "present.\n"); 2950 return false; 2951 } 2952 2953 tfeedback_decls = rzalloc_array(mem_ctx, tfeedback_decl, 2954 num_tfeedback_decls); 2955 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls, 2956 varying_names, tfeedback_decls)) 2957 return false; 2958 } 2959 2960 /* If there is no fragment shader we need to set transform feedback. 2961 * 2962 * For SSO we also need to assign output locations. We assign them here 2963 * because we need to do it for both single stage programs and multi stage 2964 * programs. 2965 */ 2966 if (last < MESA_SHADER_FRAGMENT && 2967 (num_tfeedback_decls != 0 || prog->SeparateShader)) { 2968 const uint64_t reserved_out_slots = 2969 reserved_varying_slot(prog->_LinkedShaders[last], ir_var_shader_out); 2970 if (!assign_varying_locations(ctx, mem_ctx, prog, 2971 prog->_LinkedShaders[last], NULL, 2972 num_tfeedback_decls, tfeedback_decls, 2973 reserved_out_slots)) 2974 return false; 2975 } 2976 2977 if (last <= MESA_SHADER_FRAGMENT) { 2978 /* Remove unused varyings from the first/last stage unless SSO */ 2979 remove_unused_shader_inputs_and_outputs(prog->SeparateShader, 2980 prog->_LinkedShaders[first], 2981 ir_var_shader_in); 2982 remove_unused_shader_inputs_and_outputs(prog->SeparateShader, 2983 prog->_LinkedShaders[last], 2984 ir_var_shader_out); 2985 2986 /* If the program is made up of only a single stage */ 2987 if (first == last) { 2988 gl_linked_shader *const sh = prog->_LinkedShaders[last]; 2989 2990 do_dead_builtin_varyings(ctx, NULL, sh, 0, NULL); 2991 do_dead_builtin_varyings(ctx, sh, NULL, num_tfeedback_decls, 2992 tfeedback_decls); 2993 2994 if (prog->SeparateShader) { 2995 const uint64_t reserved_slots = 2996 reserved_varying_slot(sh, ir_var_shader_in); 2997 2998 /* Assign input locations for SSO, output locations are already 2999 * assigned. 3000 */ 3001 if (!assign_varying_locations(ctx, mem_ctx, prog, 3002 NULL /* producer */, 3003 sh /* consumer */, 3004 0 /* num_tfeedback_decls */, 3005 NULL /* tfeedback_decls */, 3006 reserved_slots)) 3007 return false; 3008 } 3009 } else { 3010 /* Linking the stages in the opposite order (from fragment to vertex) 3011 * ensures that inter-shader outputs written to in an earlier stage 3012 * are eliminated if they are (transitively) not used in a later 3013 * stage. 3014 */ 3015 int next = last; 3016 for (int i = next - 1; i >= 0; i--) { 3017 if (prog->_LinkedShaders[i] == NULL && i != 0) 3018 continue; 3019 3020 gl_linked_shader *const sh_i = prog->_LinkedShaders[i]; 3021 gl_linked_shader *const sh_next = prog->_LinkedShaders[next]; 3022 3023 const uint64_t reserved_out_slots = 3024 reserved_varying_slot(sh_i, ir_var_shader_out); 3025 const uint64_t reserved_in_slots = 3026 reserved_varying_slot(sh_next, ir_var_shader_in); 3027 3028 do_dead_builtin_varyings(ctx, sh_i, sh_next, 3029 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, 3030 tfeedback_decls); 3031 3032 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next, 3033 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, 3034 tfeedback_decls, 3035 reserved_out_slots | reserved_in_slots)) 3036 return false; 3037 3038 /* This must be done after all dead varyings are eliminated. */ 3039 if (sh_i != NULL) { 3040 unsigned slots_used = util_bitcount64(reserved_out_slots); 3041 if (!check_against_output_limit(ctx, prog, sh_i, slots_used)) { 3042 return false; 3043 } 3044 } 3045 3046 unsigned slots_used = util_bitcount64(reserved_in_slots); 3047 if (!check_against_input_limit(ctx, prog, sh_next, slots_used)) 3048 return false; 3049 3050 next = i; 3051 } 3052 } 3053 } 3054 3055 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls, 3056 has_xfb_qualifiers, mem_ctx)) 3057 return false; 3058 3059 return true; 3060} 3061