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