meta: Refactor configuration of renderbuffer sampling
[mesa.git] / src / 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/mtypes.h"
33 #include "glsl_symbol_table.h"
34 #include "glsl_parser_extras.h"
35 #include "ir_optimization.h"
36 #include "linker.h"
37 #include "link_varyings.h"
38 #include "main/macros.h"
39 #include "program/hash_table.h"
40 #include "program.h"
41
42
43 /**
44 * Validate the types and qualifiers of an output from one stage against the
45 * matching input to another stage.
46 */
47 static void
48 cross_validate_types_and_qualifiers(struct gl_shader_program *prog,
49 const ir_variable *input,
50 const ir_variable *output,
51 gl_shader_stage consumer_stage,
52 gl_shader_stage producer_stage)
53 {
54 /* Check that the types match between stages.
55 */
56 const glsl_type *type_to_match = input->type;
57 if (consumer_stage == MESA_SHADER_GEOMETRY) {
58 assert(type_to_match->is_array()); /* Enforced by ast_to_hir */
59 type_to_match = type_to_match->element_type();
60 }
61 if (type_to_match != output->type) {
62 /* There is a bit of a special case for gl_TexCoord. This
63 * built-in is unsized by default. Applications that variable
64 * access it must redeclare it with a size. There is some
65 * language in the GLSL spec that implies the fragment shader
66 * and vertex shader do not have to agree on this size. Other
67 * driver behave this way, and one or two applications seem to
68 * rely on it.
69 *
70 * Neither declaration needs to be modified here because the array
71 * sizes are fixed later when update_array_sizes is called.
72 *
73 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
74 *
75 * "Unlike user-defined varying variables, the built-in
76 * varying variables don't have a strict one-to-one
77 * correspondence between the vertex language and the
78 * fragment language."
79 */
80 if (!output->type->is_array()
81 || (strncmp("gl_", output->name, 3) != 0)) {
82 linker_error(prog,
83 "%s shader output `%s' declared as type `%s', "
84 "but %s shader input declared as type `%s'\n",
85 _mesa_shader_stage_to_string(producer_stage),
86 output->name,
87 output->type->name,
88 _mesa_shader_stage_to_string(consumer_stage),
89 input->type->name);
90 return;
91 }
92 }
93
94 /* Check that all of the qualifiers match between stages.
95 */
96 if (input->data.centroid != output->data.centroid) {
97 linker_error(prog,
98 "%s shader output `%s' %s centroid qualifier, "
99 "but %s shader input %s centroid qualifier\n",
100 _mesa_shader_stage_to_string(producer_stage),
101 output->name,
102 (output->data.centroid) ? "has" : "lacks",
103 _mesa_shader_stage_to_string(consumer_stage),
104 (input->data.centroid) ? "has" : "lacks");
105 return;
106 }
107
108 if (input->data.sample != output->data.sample) {
109 linker_error(prog,
110 "%s shader output `%s' %s sample qualifier, "
111 "but %s shader input %s sample qualifier\n",
112 _mesa_shader_stage_to_string(producer_stage),
113 output->name,
114 (output->data.sample) ? "has" : "lacks",
115 _mesa_shader_stage_to_string(consumer_stage),
116 (input->data.sample) ? "has" : "lacks");
117 return;
118 }
119
120 if (input->data.invariant != output->data.invariant) {
121 linker_error(prog,
122 "%s shader output `%s' %s invariant qualifier, "
123 "but %s shader input %s invariant qualifier\n",
124 _mesa_shader_stage_to_string(producer_stage),
125 output->name,
126 (output->data.invariant) ? "has" : "lacks",
127 _mesa_shader_stage_to_string(consumer_stage),
128 (input->data.invariant) ? "has" : "lacks");
129 return;
130 }
131
132 if (input->data.interpolation != output->data.interpolation) {
133 linker_error(prog,
134 "%s shader output `%s' specifies %s "
135 "interpolation qualifier, "
136 "but %s shader input specifies %s "
137 "interpolation qualifier\n",
138 _mesa_shader_stage_to_string(producer_stage),
139 output->name,
140 interpolation_string(output->data.interpolation),
141 _mesa_shader_stage_to_string(consumer_stage),
142 interpolation_string(input->data.interpolation));
143 return;
144 }
145 }
146
147 /**
148 * Validate front and back color outputs against single color input
149 */
150 static void
151 cross_validate_front_and_back_color(struct gl_shader_program *prog,
152 const ir_variable *input,
153 const ir_variable *front_color,
154 const ir_variable *back_color,
155 gl_shader_stage consumer_stage,
156 gl_shader_stage producer_stage)
157 {
158 if (front_color != NULL && front_color->data.assigned)
159 cross_validate_types_and_qualifiers(prog, input, front_color,
160 consumer_stage, producer_stage);
161
162 if (back_color != NULL && back_color->data.assigned)
163 cross_validate_types_and_qualifiers(prog, input, back_color,
164 consumer_stage, producer_stage);
165 }
166
167 /**
168 * Validate that outputs from one stage match inputs of another
169 */
170 void
171 cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
172 gl_shader *producer, gl_shader *consumer)
173 {
174 glsl_symbol_table parameters;
175 ir_variable *explicit_locations[MAX_VARYING] = { NULL, };
176
177 /* Find all shader outputs in the "producer" stage.
178 */
179 foreach_list(node, producer->ir) {
180 ir_variable *const var = ((ir_instruction *) node)->as_variable();
181
182 if ((var == NULL) || (var->data.mode != ir_var_shader_out))
183 continue;
184
185 if (!var->data.explicit_location
186 || var->data.location < VARYING_SLOT_VAR0)
187 parameters.add_variable(var);
188 else {
189 /* User-defined varyings with explicit locations are handled
190 * differently because they do not need to have matching names.
191 */
192 const unsigned idx = var->data.location - VARYING_SLOT_VAR0;
193
194 if (explicit_locations[idx] != NULL) {
195 linker_error(prog,
196 "%s shader has multiple outputs explicitly "
197 "assigned to location %d\n",
198 _mesa_shader_stage_to_string(producer->Stage),
199 idx);
200 return;
201 }
202
203 explicit_locations[idx] = var;
204 }
205 }
206
207
208 /* Find all shader inputs in the "consumer" stage. Any variables that have
209 * matching outputs already in the symbol table must have the same type and
210 * qualifiers.
211 *
212 * Exception: if the consumer is the geometry shader, then the inputs
213 * should be arrays and the type of the array element should match the type
214 * of the corresponding producer output.
215 */
216 foreach_list(node, consumer->ir) {
217 ir_variable *const input = ((ir_instruction *) node)->as_variable();
218
219 if ((input == NULL) || (input->data.mode != ir_var_shader_in))
220 continue;
221
222 if (strcmp(input->name, "gl_Color") == 0 && input->data.used) {
223 const ir_variable *const front_color =
224 parameters.get_variable("gl_FrontColor");
225
226 const ir_variable *const back_color =
227 parameters.get_variable("gl_BackColor");
228
229 cross_validate_front_and_back_color(prog, input,
230 front_color, back_color,
231 consumer->Stage, producer->Stage);
232 } else if (strcmp(input->name, "gl_SecondaryColor") == 0 && input->data.used) {
233 const ir_variable *const front_color =
234 parameters.get_variable("gl_FrontSecondaryColor");
235
236 const ir_variable *const back_color =
237 parameters.get_variable("gl_BackSecondaryColor");
238
239 cross_validate_front_and_back_color(prog, input,
240 front_color, back_color,
241 consumer->Stage, producer->Stage);
242 } else {
243 /* The rules for connecting inputs and outputs change in the presence
244 * of explicit locations. In this case, we no longer care about the
245 * names of the variables. Instead, we care only about the
246 * explicitly assigned location.
247 */
248 ir_variable *output = NULL;
249 if (input->data.explicit_location
250 && input->data.location >= VARYING_SLOT_VAR0) {
251 output = explicit_locations[input->data.location - VARYING_SLOT_VAR0];
252
253 if (output == NULL) {
254 linker_error(prog,
255 "%s shader input `%s' with explicit location "
256 "has no matching output\n",
257 _mesa_shader_stage_to_string(consumer->Stage),
258 input->name);
259 }
260 } else {
261 output = parameters.get_variable(input->name);
262 }
263
264 if (output != NULL) {
265 cross_validate_types_and_qualifiers(prog, input, output,
266 consumer->Stage, producer->Stage);
267 }
268 }
269 }
270 }
271
272
273 /**
274 * Initialize this object based on a string that was passed to
275 * glTransformFeedbackVaryings.
276 *
277 * If the input is mal-formed, this call still succeeds, but it sets
278 * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var()
279 * will fail to find any matching variable.
280 */
281 void
282 tfeedback_decl::init(struct gl_context *ctx, const void *mem_ctx,
283 const char *input)
284 {
285 /* We don't have to be pedantic about what is a valid GLSL variable name,
286 * because any variable with an invalid name can't exist in the IR anyway.
287 */
288
289 this->location = -1;
290 this->orig_name = input;
291 this->is_clip_distance_mesa = false;
292 this->skip_components = 0;
293 this->next_buffer_separator = false;
294 this->matched_candidate = NULL;
295
296 if (ctx->Extensions.ARB_transform_feedback3) {
297 /* Parse gl_NextBuffer. */
298 if (strcmp(input, "gl_NextBuffer") == 0) {
299 this->next_buffer_separator = true;
300 return;
301 }
302
303 /* Parse gl_SkipComponents. */
304 if (strcmp(input, "gl_SkipComponents1") == 0)
305 this->skip_components = 1;
306 else if (strcmp(input, "gl_SkipComponents2") == 0)
307 this->skip_components = 2;
308 else if (strcmp(input, "gl_SkipComponents3") == 0)
309 this->skip_components = 3;
310 else if (strcmp(input, "gl_SkipComponents4") == 0)
311 this->skip_components = 4;
312
313 if (this->skip_components)
314 return;
315 }
316
317 /* Parse a declaration. */
318 const char *base_name_end;
319 long subscript = parse_program_resource_name(input, &base_name_end);
320 this->var_name = ralloc_strndup(mem_ctx, input, base_name_end - input);
321 if (subscript >= 0) {
322 this->array_subscript = subscript;
323 this->is_subscripted = true;
324 } else {
325 this->is_subscripted = false;
326 }
327
328 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
329 * class must behave specially to account for the fact that gl_ClipDistance
330 * is converted from a float[8] to a vec4[2].
331 */
332 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
333 strcmp(this->var_name, "gl_ClipDistance") == 0) {
334 this->is_clip_distance_mesa = true;
335 }
336 }
337
338
339 /**
340 * Determine whether two tfeedback_decl objects refer to the same variable and
341 * array index (if applicable).
342 */
343 bool
344 tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
345 {
346 assert(x.is_varying() && y.is_varying());
347
348 if (strcmp(x.var_name, y.var_name) != 0)
349 return false;
350 if (x.is_subscripted != y.is_subscripted)
351 return false;
352 if (x.is_subscripted && x.array_subscript != y.array_subscript)
353 return false;
354 return true;
355 }
356
357
358 /**
359 * Assign a location for this tfeedback_decl object based on the transform
360 * feedback candidate found by find_candidate.
361 *
362 * If an error occurs, the error is reported through linker_error() and false
363 * is returned.
364 */
365 bool
366 tfeedback_decl::assign_location(struct gl_context *ctx,
367 struct gl_shader_program *prog)
368 {
369 assert(this->is_varying());
370
371 unsigned fine_location
372 = this->matched_candidate->toplevel_var->data.location * 4
373 + this->matched_candidate->toplevel_var->data.location_frac
374 + this->matched_candidate->offset;
375
376 if (this->matched_candidate->type->is_array()) {
377 /* Array variable */
378 const unsigned matrix_cols =
379 this->matched_candidate->type->fields.array->matrix_columns;
380 const unsigned vector_elements =
381 this->matched_candidate->type->fields.array->vector_elements;
382 unsigned actual_array_size = this->is_clip_distance_mesa ?
383 prog->LastClipDistanceArraySize :
384 this->matched_candidate->type->array_size();
385
386 if (this->is_subscripted) {
387 /* Check array bounds. */
388 if (this->array_subscript >= actual_array_size) {
389 linker_error(prog, "Transform feedback varying %s has index "
390 "%i, but the array size is %u.",
391 this->orig_name, this->array_subscript,
392 actual_array_size);
393 return false;
394 }
395 unsigned array_elem_size = this->is_clip_distance_mesa ?
396 1 : vector_elements * matrix_cols;
397 fine_location += array_elem_size * this->array_subscript;
398 this->size = 1;
399 } else {
400 this->size = actual_array_size;
401 }
402 this->vector_elements = vector_elements;
403 this->matrix_columns = matrix_cols;
404 if (this->is_clip_distance_mesa)
405 this->type = GL_FLOAT;
406 else
407 this->type = this->matched_candidate->type->fields.array->gl_type;
408 } else {
409 /* Regular variable (scalar, vector, or matrix) */
410 if (this->is_subscripted) {
411 linker_error(prog, "Transform feedback varying %s requested, "
412 "but %s is not an array.",
413 this->orig_name, this->var_name);
414 return false;
415 }
416 this->size = 1;
417 this->vector_elements = this->matched_candidate->type->vector_elements;
418 this->matrix_columns = this->matched_candidate->type->matrix_columns;
419 this->type = this->matched_candidate->type->gl_type;
420 }
421 this->location = fine_location / 4;
422 this->location_frac = fine_location % 4;
423
424 /* From GL_EXT_transform_feedback:
425 * A program will fail to link if:
426 *
427 * * the total number of components to capture in any varying
428 * variable in <varyings> is greater than the constant
429 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
430 * buffer mode is SEPARATE_ATTRIBS_EXT;
431 */
432 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
433 this->num_components() >
434 ctx->Const.MaxTransformFeedbackSeparateComponents) {
435 linker_error(prog, "Transform feedback varying %s exceeds "
436 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
437 this->orig_name);
438 return false;
439 }
440
441 return true;
442 }
443
444
445 unsigned
446 tfeedback_decl::get_num_outputs() const
447 {
448 if (!this->is_varying()) {
449 return 0;
450 }
451
452 return (this->num_components() + this->location_frac + 3)/4;
453 }
454
455
456 /**
457 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
458 *
459 * If an error occurs, the error is reported through linker_error() and false
460 * is returned.
461 */
462 bool
463 tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
464 struct gl_transform_feedback_info *info,
465 unsigned buffer, const unsigned max_outputs) const
466 {
467 assert(!this->next_buffer_separator);
468
469 /* Handle gl_SkipComponents. */
470 if (this->skip_components) {
471 info->BufferStride[buffer] += this->skip_components;
472 return true;
473 }
474
475 /* From GL_EXT_transform_feedback:
476 * A program will fail to link if:
477 *
478 * * the total number of components to capture is greater than
479 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
480 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
481 */
482 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
483 info->BufferStride[buffer] + this->num_components() >
484 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
485 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
486 "limit has been exceeded.");
487 return false;
488 }
489
490 unsigned location = this->location;
491 unsigned location_frac = this->location_frac;
492 unsigned num_components = this->num_components();
493 while (num_components > 0) {
494 unsigned output_size = MIN2(num_components, 4 - location_frac);
495 assert(info->NumOutputs < max_outputs);
496 info->Outputs[info->NumOutputs].ComponentOffset = location_frac;
497 info->Outputs[info->NumOutputs].OutputRegister = location;
498 info->Outputs[info->NumOutputs].NumComponents = output_size;
499 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
500 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
501 ++info->NumOutputs;
502 info->BufferStride[buffer] += output_size;
503 num_components -= output_size;
504 location++;
505 location_frac = 0;
506 }
507
508 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
509 info->Varyings[info->NumVarying].Type = this->type;
510 info->Varyings[info->NumVarying].Size = this->size;
511 info->NumVarying++;
512
513 return true;
514 }
515
516
517 const tfeedback_candidate *
518 tfeedback_decl::find_candidate(gl_shader_program *prog,
519 hash_table *tfeedback_candidates)
520 {
521 const char *name = this->is_clip_distance_mesa
522 ? "gl_ClipDistanceMESA" : this->var_name;
523 this->matched_candidate = (const tfeedback_candidate *)
524 hash_table_find(tfeedback_candidates, name);
525 if (!this->matched_candidate) {
526 /* From GL_EXT_transform_feedback:
527 * A program will fail to link if:
528 *
529 * * any variable name specified in the <varyings> array is not
530 * declared as an output in the geometry shader (if present) or
531 * the vertex shader (if no geometry shader is present);
532 */
533 linker_error(prog, "Transform feedback varying %s undeclared.",
534 this->orig_name);
535 }
536 return this->matched_candidate;
537 }
538
539
540 /**
541 * Parse all the transform feedback declarations that were passed to
542 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
543 *
544 * If an error occurs, the error is reported through linker_error() and false
545 * is returned.
546 */
547 bool
548 parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
549 const void *mem_ctx, unsigned num_names,
550 char **varying_names, tfeedback_decl *decls)
551 {
552 for (unsigned i = 0; i < num_names; ++i) {
553 decls[i].init(ctx, mem_ctx, varying_names[i]);
554
555 if (!decls[i].is_varying())
556 continue;
557
558 /* From GL_EXT_transform_feedback:
559 * A program will fail to link if:
560 *
561 * * any two entries in the <varyings> array specify the same varying
562 * variable;
563 *
564 * We interpret this to mean "any two entries in the <varyings> array
565 * specify the same varying variable and array index", since transform
566 * feedback of arrays would be useless otherwise.
567 */
568 for (unsigned j = 0; j < i; ++j) {
569 if (!decls[j].is_varying())
570 continue;
571
572 if (tfeedback_decl::is_same(decls[i], decls[j])) {
573 linker_error(prog, "Transform feedback varying %s specified "
574 "more than once.", varying_names[i]);
575 return false;
576 }
577 }
578 }
579 return true;
580 }
581
582
583 /**
584 * Store transform feedback location assignments into
585 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
586 *
587 * If an error occurs, the error is reported through linker_error() and false
588 * is returned.
589 */
590 bool
591 store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
592 unsigned num_tfeedback_decls,
593 tfeedback_decl *tfeedback_decls)
594 {
595 bool separate_attribs_mode =
596 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
597
598 ralloc_free(prog->LinkedTransformFeedback.Varyings);
599 ralloc_free(prog->LinkedTransformFeedback.Outputs);
600
601 memset(&prog->LinkedTransformFeedback, 0,
602 sizeof(prog->LinkedTransformFeedback));
603
604 prog->LinkedTransformFeedback.Varyings =
605 rzalloc_array(prog,
606 struct gl_transform_feedback_varying_info,
607 num_tfeedback_decls);
608
609 unsigned num_outputs = 0;
610 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
611 num_outputs += tfeedback_decls[i].get_num_outputs();
612
613 prog->LinkedTransformFeedback.Outputs =
614 rzalloc_array(prog,
615 struct gl_transform_feedback_output,
616 num_outputs);
617
618 unsigned num_buffers = 0;
619
620 if (separate_attribs_mode) {
621 /* GL_SEPARATE_ATTRIBS */
622 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
623 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
624 num_buffers, num_outputs))
625 return false;
626
627 num_buffers++;
628 }
629 }
630 else {
631 /* GL_INVERLEAVED_ATTRIBS */
632 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
633 if (tfeedback_decls[i].is_next_buffer_separator()) {
634 num_buffers++;
635 continue;
636 }
637
638 if (!tfeedback_decls[i].store(ctx, prog,
639 &prog->LinkedTransformFeedback,
640 num_buffers, num_outputs))
641 return false;
642 }
643 num_buffers++;
644 }
645
646 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
647
648 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
649 return true;
650 }
651
652 namespace {
653
654 /**
655 * Data structure recording the relationship between outputs of one shader
656 * stage (the "producer") and inputs of another (the "consumer").
657 */
658 class varying_matches
659 {
660 public:
661 varying_matches(bool disable_varying_packing, bool consumer_is_fs);
662 ~varying_matches();
663 void record(ir_variable *producer_var, ir_variable *consumer_var);
664 unsigned assign_locations();
665 void store_locations() const;
666
667 private:
668 /**
669 * If true, this driver disables varying packing, so all varyings need to
670 * be aligned on slot boundaries, and take up a number of slots equal to
671 * their number of matrix columns times their array size.
672 */
673 const bool disable_varying_packing;
674
675 /**
676 * Enum representing the order in which varyings are packed within a
677 * packing class.
678 *
679 * Currently we pack vec4's first, then vec2's, then scalar values, then
680 * vec3's. This order ensures that the only vectors that are at risk of
681 * having to be "double parked" (split between two adjacent varying slots)
682 * are the vec3's.
683 */
684 enum packing_order_enum {
685 PACKING_ORDER_VEC4,
686 PACKING_ORDER_VEC2,
687 PACKING_ORDER_SCALAR,
688 PACKING_ORDER_VEC3,
689 };
690
691 static unsigned compute_packing_class(const ir_variable *var);
692 static packing_order_enum compute_packing_order(const ir_variable *var);
693 static int match_comparator(const void *x_generic, const void *y_generic);
694
695 /**
696 * Structure recording the relationship between a single producer output
697 * and a single consumer input.
698 */
699 struct match {
700 /**
701 * Packing class for this varying, computed by compute_packing_class().
702 */
703 unsigned packing_class;
704
705 /**
706 * Packing order for this varying, computed by compute_packing_order().
707 */
708 packing_order_enum packing_order;
709 unsigned num_components;
710
711 /**
712 * The output variable in the producer stage.
713 */
714 ir_variable *producer_var;
715
716 /**
717 * The input variable in the consumer stage.
718 */
719 ir_variable *consumer_var;
720
721 /**
722 * The location which has been assigned for this varying. This is
723 * expressed in multiples of a float, with the first generic varying
724 * (i.e. the one referred to by VARYING_SLOT_VAR0) represented by the
725 * value 0.
726 */
727 unsigned generic_location;
728 } *matches;
729
730 /**
731 * The number of elements in the \c matches array that are currently in
732 * use.
733 */
734 unsigned num_matches;
735
736 /**
737 * The number of elements that were set aside for the \c matches array when
738 * it was allocated.
739 */
740 unsigned matches_capacity;
741
742 const bool consumer_is_fs;
743 };
744
745 } /* anonymous namespace */
746
747 varying_matches::varying_matches(bool disable_varying_packing,
748 bool consumer_is_fs)
749 : disable_varying_packing(disable_varying_packing),
750 consumer_is_fs(consumer_is_fs)
751 {
752 /* Note: this initial capacity is rather arbitrarily chosen to be large
753 * enough for many cases without wasting an unreasonable amount of space.
754 * varying_matches::record() will resize the array if there are more than
755 * this number of varyings.
756 */
757 this->matches_capacity = 8;
758 this->matches = (match *)
759 malloc(sizeof(*this->matches) * this->matches_capacity);
760 this->num_matches = 0;
761 }
762
763
764 varying_matches::~varying_matches()
765 {
766 free(this->matches);
767 }
768
769
770 /**
771 * Record the given producer/consumer variable pair in the list of variables
772 * that should later be assigned locations.
773 *
774 * It is permissible for \c consumer_var to be NULL (this happens if a
775 * variable is output by the producer and consumed by transform feedback, but
776 * not consumed by the consumer).
777 *
778 * If \c producer_var has already been paired up with a consumer_var, or
779 * producer_var is part of fixed pipeline functionality (and hence already has
780 * a location assigned), this function has no effect.
781 *
782 * Note: as a side effect this function may change the interpolation type of
783 * \c producer_var, but only when the change couldn't possibly affect
784 * rendering.
785 */
786 void
787 varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
788 {
789 assert(producer_var != NULL || consumer_var != NULL);
790
791 if ((producer_var && !producer_var->data.is_unmatched_generic_inout)
792 || (consumer_var && !consumer_var->data.is_unmatched_generic_inout)) {
793 /* Either a location already exists for this variable (since it is part
794 * of fixed functionality), or it has already been recorded as part of a
795 * previous match.
796 */
797 return;
798 }
799
800 if ((consumer_var == NULL && producer_var->type->contains_integer()) ||
801 !consumer_is_fs) {
802 /* Since this varying is not being consumed by the fragment shader, its
803 * interpolation type varying cannot possibly affect rendering. Also,
804 * this variable is non-flat and is (or contains) an integer.
805 *
806 * lower_packed_varyings requires all integer varyings to flat,
807 * regardless of where they appear. We can trivially satisfy that
808 * requirement by changing the interpolation type to flat here.
809 */
810 producer_var->data.centroid = false;
811 producer_var->data.sample = false;
812 producer_var->data.interpolation = INTERP_QUALIFIER_FLAT;
813
814 if (consumer_var) {
815 consumer_var->data.centroid = false;
816 consumer_var->data.sample = false;
817 consumer_var->data.interpolation = INTERP_QUALIFIER_FLAT;
818 }
819 }
820
821 if (this->num_matches == this->matches_capacity) {
822 this->matches_capacity *= 2;
823 this->matches = (match *)
824 realloc(this->matches,
825 sizeof(*this->matches) * this->matches_capacity);
826 }
827
828 const ir_variable *const var = (producer_var != NULL)
829 ? producer_var : consumer_var;
830
831 this->matches[this->num_matches].packing_class
832 = this->compute_packing_class(var);
833 this->matches[this->num_matches].packing_order
834 = this->compute_packing_order(var);
835 if (this->disable_varying_packing) {
836 unsigned slots = var->type->is_array()
837 ? (var->type->length * var->type->fields.array->matrix_columns)
838 : var->type->matrix_columns;
839 this->matches[this->num_matches].num_components = 4 * slots;
840 } else {
841 this->matches[this->num_matches].num_components
842 = var->type->component_slots();
843 }
844 this->matches[this->num_matches].producer_var = producer_var;
845 this->matches[this->num_matches].consumer_var = consumer_var;
846 this->num_matches++;
847 if (producer_var)
848 producer_var->data.is_unmatched_generic_inout = 0;
849 if (consumer_var)
850 consumer_var->data.is_unmatched_generic_inout = 0;
851 }
852
853
854 /**
855 * Choose locations for all of the variable matches that were previously
856 * passed to varying_matches::record().
857 */
858 unsigned
859 varying_matches::assign_locations()
860 {
861 /* Sort varying matches into an order that makes them easy to pack. */
862 qsort(this->matches, this->num_matches, sizeof(*this->matches),
863 &varying_matches::match_comparator);
864
865 unsigned generic_location = 0;
866
867 for (unsigned i = 0; i < this->num_matches; i++) {
868 /* Advance to the next slot if this varying has a different packing
869 * class than the previous one, and we're not already on a slot
870 * boundary.
871 */
872 if (i > 0 &&
873 this->matches[i - 1].packing_class
874 != this->matches[i].packing_class) {
875 generic_location = ALIGN(generic_location, 4);
876 }
877
878 this->matches[i].generic_location = generic_location;
879
880 generic_location += this->matches[i].num_components;
881 }
882
883 return (generic_location + 3) / 4;
884 }
885
886
887 /**
888 * Update the producer and consumer shaders to reflect the locations
889 * assignments that were made by varying_matches::assign_locations().
890 */
891 void
892 varying_matches::store_locations() const
893 {
894 for (unsigned i = 0; i < this->num_matches; i++) {
895 ir_variable *producer_var = this->matches[i].producer_var;
896 ir_variable *consumer_var = this->matches[i].consumer_var;
897 unsigned generic_location = this->matches[i].generic_location;
898 unsigned slot = generic_location / 4;
899 unsigned offset = generic_location % 4;
900
901 if (producer_var) {
902 producer_var->data.location = VARYING_SLOT_VAR0 + slot;
903 producer_var->data.location_frac = offset;
904 }
905
906 if (consumer_var) {
907 assert(consumer_var->data.location == -1);
908 consumer_var->data.location = VARYING_SLOT_VAR0 + slot;
909 consumer_var->data.location_frac = offset;
910 }
911 }
912 }
913
914
915 /**
916 * Compute the "packing class" of the given varying. This is an unsigned
917 * integer with the property that two variables in the same packing class can
918 * be safely backed into the same vec4.
919 */
920 unsigned
921 varying_matches::compute_packing_class(const ir_variable *var)
922 {
923 /* Without help from the back-end, there is no way to pack together
924 * variables with different interpolation types, because
925 * lower_packed_varyings must choose exactly one interpolation type for
926 * each packed varying it creates.
927 *
928 * However, we can safely pack together floats, ints, and uints, because:
929 *
930 * - varyings of base type "int" and "uint" must use the "flat"
931 * interpolation type, which can only occur in GLSL 1.30 and above.
932 *
933 * - On platforms that support GLSL 1.30 and above, lower_packed_varyings
934 * can store flat floats as ints without losing any information (using
935 * the ir_unop_bitcast_* opcodes).
936 *
937 * Therefore, the packing class depends only on the interpolation type.
938 */
939 unsigned packing_class = var->data.centroid | (var->data.sample << 1);
940 packing_class *= 4;
941 packing_class += var->data.interpolation;
942 return packing_class;
943 }
944
945
946 /**
947 * Compute the "packing order" of the given varying. This is a sort key we
948 * use to determine when to attempt to pack the given varying relative to
949 * other varyings in the same packing class.
950 */
951 varying_matches::packing_order_enum
952 varying_matches::compute_packing_order(const ir_variable *var)
953 {
954 const glsl_type *element_type = var->type;
955
956 while (element_type->base_type == GLSL_TYPE_ARRAY) {
957 element_type = element_type->fields.array;
958 }
959
960 switch (element_type->component_slots() % 4) {
961 case 1: return PACKING_ORDER_SCALAR;
962 case 2: return PACKING_ORDER_VEC2;
963 case 3: return PACKING_ORDER_VEC3;
964 case 0: return PACKING_ORDER_VEC4;
965 default:
966 assert(!"Unexpected value of vector_elements");
967 return PACKING_ORDER_VEC4;
968 }
969 }
970
971
972 /**
973 * Comparison function passed to qsort() to sort varyings by packing_class and
974 * then by packing_order.
975 */
976 int
977 varying_matches::match_comparator(const void *x_generic, const void *y_generic)
978 {
979 const match *x = (const match *) x_generic;
980 const match *y = (const match *) y_generic;
981
982 if (x->packing_class != y->packing_class)
983 return x->packing_class - y->packing_class;
984 return x->packing_order - y->packing_order;
985 }
986
987
988 /**
989 * Is the given variable a varying variable to be counted against the
990 * limit in ctx->Const.MaxVarying?
991 * This includes variables such as texcoords, colors and generic
992 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
993 */
994 static bool
995 var_counts_against_varying_limit(gl_shader_stage stage, const ir_variable *var)
996 {
997 /* Only fragment shaders will take a varying variable as an input */
998 if (stage == MESA_SHADER_FRAGMENT &&
999 var->data.mode == ir_var_shader_in) {
1000 switch (var->data.location) {
1001 case VARYING_SLOT_POS:
1002 case VARYING_SLOT_FACE:
1003 case VARYING_SLOT_PNTC:
1004 return false;
1005 default:
1006 return true;
1007 }
1008 }
1009 return false;
1010 }
1011
1012
1013 /**
1014 * Visitor class that generates tfeedback_candidate structs describing all
1015 * possible targets of transform feedback.
1016 *
1017 * tfeedback_candidate structs are stored in the hash table
1018 * tfeedback_candidates, which is passed to the constructor. This hash table
1019 * maps varying names to instances of the tfeedback_candidate struct.
1020 */
1021 class tfeedback_candidate_generator : public program_resource_visitor
1022 {
1023 public:
1024 tfeedback_candidate_generator(void *mem_ctx,
1025 hash_table *tfeedback_candidates)
1026 : mem_ctx(mem_ctx),
1027 tfeedback_candidates(tfeedback_candidates),
1028 toplevel_var(NULL),
1029 varying_floats(0)
1030 {
1031 }
1032
1033 void process(ir_variable *var)
1034 {
1035 this->toplevel_var = var;
1036 this->varying_floats = 0;
1037 if (var->is_interface_instance())
1038 program_resource_visitor::process(var->get_interface_type(),
1039 var->get_interface_type()->name);
1040 else
1041 program_resource_visitor::process(var);
1042 }
1043
1044 private:
1045 virtual void visit_field(const glsl_type *type, const char *name,
1046 bool row_major)
1047 {
1048 assert(!type->is_record());
1049 assert(!(type->is_array() && type->fields.array->is_record()));
1050 assert(!type->is_interface());
1051 assert(!(type->is_array() && type->fields.array->is_interface()));
1052
1053 (void) row_major;
1054
1055 tfeedback_candidate *candidate
1056 = rzalloc(this->mem_ctx, tfeedback_candidate);
1057 candidate->toplevel_var = this->toplevel_var;
1058 candidate->type = type;
1059 candidate->offset = this->varying_floats;
1060 hash_table_insert(this->tfeedback_candidates, candidate,
1061 ralloc_strdup(this->mem_ctx, name));
1062 this->varying_floats += type->component_slots();
1063 }
1064
1065 /**
1066 * Memory context used to allocate hash table keys and values.
1067 */
1068 void * const mem_ctx;
1069
1070 /**
1071 * Hash table in which tfeedback_candidate objects should be stored.
1072 */
1073 hash_table * const tfeedback_candidates;
1074
1075 /**
1076 * Pointer to the toplevel variable that is being traversed.
1077 */
1078 ir_variable *toplevel_var;
1079
1080 /**
1081 * Total number of varying floats that have been visited so far. This is
1082 * used to determine the offset to each varying within the toplevel
1083 * variable.
1084 */
1085 unsigned varying_floats;
1086 };
1087
1088
1089 namespace linker {
1090
1091 bool
1092 populate_consumer_input_sets(void *mem_ctx, exec_list *ir,
1093 hash_table *consumer_inputs,
1094 hash_table *consumer_interface_inputs,
1095 ir_variable *consumer_inputs_with_locations[VARYING_SLOT_MAX])
1096 {
1097 memset(consumer_inputs_with_locations,
1098 0,
1099 sizeof(consumer_inputs_with_locations[0]) * VARYING_SLOT_MAX);
1100
1101 foreach_list(node, ir) {
1102 ir_variable *const input_var = ((ir_instruction *) node)->as_variable();
1103
1104 if ((input_var != NULL) && (input_var->data.mode == ir_var_shader_in)) {
1105 if (input_var->type->is_interface())
1106 return false;
1107
1108 if (input_var->data.explicit_location) {
1109 /* assign_varying_locations only cares about finding the
1110 * ir_variable at the start of a contiguous location block.
1111 *
1112 * - For !producer, consumer_inputs_with_locations isn't used.
1113 *
1114 * - For !consumer, consumer_inputs_with_locations is empty.
1115 *
1116 * For consumer && producer, if you were trying to set some
1117 * ir_variable to the middle of a location block on the other side
1118 * of producer/consumer, cross_validate_outputs_to_inputs() should
1119 * be link-erroring due to either type mismatch or location
1120 * overlaps. If the variables do match up, then they've got a
1121 * matching data.location and you only looked at
1122 * consumer_inputs_with_locations[var->data.location], not any
1123 * following entries for the array/structure.
1124 */
1125 consumer_inputs_with_locations[input_var->data.location] =
1126 input_var;
1127 } else if (input_var->get_interface_type() != NULL) {
1128 char *const iface_field_name =
1129 ralloc_asprintf(mem_ctx, "%s.%s",
1130 input_var->get_interface_type()->name,
1131 input_var->name);
1132 hash_table_insert(consumer_interface_inputs, input_var,
1133 iface_field_name);
1134 } else {
1135 hash_table_insert(consumer_inputs, input_var,
1136 ralloc_strdup(mem_ctx, input_var->name));
1137 }
1138 }
1139 }
1140
1141 return true;
1142 }
1143
1144 /**
1145 * Find a variable from the consumer that "matches" the specified variable
1146 *
1147 * This function only finds inputs with names that match. There is no
1148 * validation (here) that the types, etc. are compatible.
1149 */
1150 ir_variable *
1151 get_matching_input(void *mem_ctx,
1152 const ir_variable *output_var,
1153 hash_table *consumer_inputs,
1154 hash_table *consumer_interface_inputs,
1155 ir_variable *consumer_inputs_with_locations[VARYING_SLOT_MAX])
1156 {
1157 ir_variable *input_var;
1158
1159 if (output_var->data.explicit_location) {
1160 input_var = consumer_inputs_with_locations[output_var->data.location];
1161 } else if (output_var->get_interface_type() != NULL) {
1162 char *const iface_field_name =
1163 ralloc_asprintf(mem_ctx, "%s.%s",
1164 output_var->get_interface_type()->name,
1165 output_var->name);
1166 input_var =
1167 (ir_variable *) hash_table_find(consumer_interface_inputs,
1168 iface_field_name);
1169 } else {
1170 input_var =
1171 (ir_variable *) hash_table_find(consumer_inputs, output_var->name);
1172 }
1173
1174 return (input_var == NULL || input_var->data.mode != ir_var_shader_in)
1175 ? NULL : input_var;
1176 }
1177
1178 }
1179
1180 static int
1181 io_variable_cmp(const void *_a, const void *_b)
1182 {
1183 const ir_variable *const a = *(const ir_variable **) _a;
1184 const ir_variable *const b = *(const ir_variable **) _b;
1185
1186 if (a->data.explicit_location && b->data.explicit_location)
1187 return b->data.location - a->data.location;
1188
1189 if (a->data.explicit_location && !b->data.explicit_location)
1190 return 1;
1191
1192 if (!a->data.explicit_location && b->data.explicit_location)
1193 return -1;
1194
1195 return -strcmp(a->name, b->name);
1196 }
1197
1198 /**
1199 * Sort the shader IO variables into canonical order
1200 */
1201 static void
1202 canonicalize_shader_io(exec_list *ir, enum ir_variable_mode io_mode)
1203 {
1204 ir_variable *var_table[MAX_PROGRAM_OUTPUTS * 4];
1205 unsigned num_variables = 0;
1206
1207 foreach_list(node, ir) {
1208 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1209
1210 if (var == NULL || var->data.mode != io_mode)
1211 continue;
1212
1213 /* If we have already encountered more I/O variables that could
1214 * successfully link, bail.
1215 */
1216 if (num_variables == ARRAY_SIZE(var_table))
1217 return;
1218
1219 var_table[num_variables++] = var;
1220 }
1221
1222 if (num_variables == 0)
1223 return;
1224
1225 /* Sort the list in reverse order (io_variable_cmp handles this). Later
1226 * we're going to push the variables on to the IR list as a stack, so we
1227 * want the last variable (in canonical order) to be first in the list.
1228 */
1229 qsort(var_table, num_variables, sizeof(var_table[0]), io_variable_cmp);
1230
1231 /* Remove the variable from it's current location in the IR, and put it at
1232 * the front.
1233 */
1234 for (unsigned i = 0; i < num_variables; i++) {
1235 var_table[i]->remove();
1236 ir->push_head(var_table[i]);
1237 }
1238 }
1239
1240 /**
1241 * Assign locations for all variables that are produced in one pipeline stage
1242 * (the "producer") and consumed in the next stage (the "consumer").
1243 *
1244 * Variables produced by the producer may also be consumed by transform
1245 * feedback.
1246 *
1247 * \param num_tfeedback_decls is the number of declarations indicating
1248 * variables that may be consumed by transform feedback.
1249 *
1250 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
1251 * representing the result of parsing the strings passed to
1252 * glTransformFeedbackVaryings(). assign_location() will be called for
1253 * each of these objects that matches one of the outputs of the
1254 * producer.
1255 *
1256 * \param gs_input_vertices: if \c consumer is a geometry shader, this is the
1257 * number of input vertices it accepts. Otherwise zero.
1258 *
1259 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
1260 * be NULL. In this case, varying locations are assigned solely based on the
1261 * requirements of transform feedback.
1262 */
1263 bool
1264 assign_varying_locations(struct gl_context *ctx,
1265 void *mem_ctx,
1266 struct gl_shader_program *prog,
1267 gl_shader *producer, gl_shader *consumer,
1268 unsigned num_tfeedback_decls,
1269 tfeedback_decl *tfeedback_decls,
1270 unsigned gs_input_vertices)
1271 {
1272 varying_matches matches(ctx->Const.DisableVaryingPacking,
1273 consumer && consumer->Stage == MESA_SHADER_FRAGMENT);
1274 hash_table *tfeedback_candidates
1275 = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
1276 hash_table *consumer_inputs
1277 = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
1278 hash_table *consumer_interface_inputs
1279 = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
1280 ir_variable *consumer_inputs_with_locations[VARYING_SLOT_MAX] = {
1281 NULL,
1282 };
1283
1284 /* Operate in a total of four passes.
1285 *
1286 * 1. Sort inputs / outputs into a canonical order. This is necessary so
1287 * that inputs / outputs of separable shaders will be assigned
1288 * predictable locations regardless of the order in which declarations
1289 * appeared in the shader source.
1290 *
1291 * 2. Assign locations for any matching inputs and outputs.
1292 *
1293 * 3. Mark output variables in the producer that do not have locations as
1294 * not being outputs. This lets the optimizer eliminate them.
1295 *
1296 * 4. Mark input variables in the consumer that do not have locations as
1297 * not being inputs. This lets the optimizer eliminate them.
1298 */
1299 if (consumer)
1300 canonicalize_shader_io(consumer->ir, ir_var_shader_in);
1301
1302 if (producer)
1303 canonicalize_shader_io(producer->ir, ir_var_shader_out);
1304
1305 if (consumer
1306 && !linker::populate_consumer_input_sets(mem_ctx,
1307 consumer->ir,
1308 consumer_inputs,
1309 consumer_interface_inputs,
1310 consumer_inputs_with_locations)) {
1311 assert(!"populate_consumer_input_sets failed");
1312 hash_table_dtor(tfeedback_candidates);
1313 hash_table_dtor(consumer_inputs);
1314 hash_table_dtor(consumer_interface_inputs);
1315 return false;
1316 }
1317
1318 if (producer) {
1319 foreach_list(node, producer->ir) {
1320 ir_variable *const output_var =
1321 ((ir_instruction *) node)->as_variable();
1322
1323 if ((output_var == NULL) ||
1324 (output_var->data.mode != ir_var_shader_out))
1325 continue;
1326
1327 tfeedback_candidate_generator g(mem_ctx, tfeedback_candidates);
1328 g.process(output_var);
1329
1330 ir_variable *const input_var =
1331 linker::get_matching_input(mem_ctx, output_var, consumer_inputs,
1332 consumer_interface_inputs,
1333 consumer_inputs_with_locations);
1334
1335 /* If a matching input variable was found, add this ouptut (and the
1336 * input) to the set. If this is a separable program and there is no
1337 * consumer stage, add the output.
1338 */
1339 if (input_var || (prog->SeparateShader && consumer == NULL)) {
1340 matches.record(output_var, input_var);
1341 }
1342 }
1343 } else {
1344 /* If there's no producer stage, then this must be a separable program.
1345 * For example, we may have a program that has just a fragment shader.
1346 * Later this program will be used with some arbitrary vertex (or
1347 * geometry) shader program. This means that locations must be assigned
1348 * for all the inputs.
1349 */
1350 foreach_list(node, consumer->ir) {
1351 ir_variable *const input_var =
1352 ((ir_instruction *) node)->as_variable();
1353
1354 if ((input_var == NULL) ||
1355 (input_var->data.mode != ir_var_shader_in))
1356 continue;
1357
1358 matches.record(NULL, input_var);
1359 }
1360 }
1361
1362 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
1363 if (!tfeedback_decls[i].is_varying())
1364 continue;
1365
1366 const tfeedback_candidate *matched_candidate
1367 = tfeedback_decls[i].find_candidate(prog, tfeedback_candidates);
1368
1369 if (matched_candidate == NULL) {
1370 hash_table_dtor(tfeedback_candidates);
1371 hash_table_dtor(consumer_inputs);
1372 hash_table_dtor(consumer_interface_inputs);
1373 return false;
1374 }
1375
1376 if (matched_candidate->toplevel_var->data.is_unmatched_generic_inout)
1377 matches.record(matched_candidate->toplevel_var, NULL);
1378 }
1379
1380 const unsigned slots_used = matches.assign_locations();
1381 matches.store_locations();
1382
1383 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
1384 if (!tfeedback_decls[i].is_varying())
1385 continue;
1386
1387 if (!tfeedback_decls[i].assign_location(ctx, prog)) {
1388 hash_table_dtor(tfeedback_candidates);
1389 hash_table_dtor(consumer_inputs);
1390 hash_table_dtor(consumer_interface_inputs);
1391 return false;
1392 }
1393 }
1394
1395 hash_table_dtor(tfeedback_candidates);
1396 hash_table_dtor(consumer_inputs);
1397 hash_table_dtor(consumer_interface_inputs);
1398
1399 if (ctx->Const.DisableVaryingPacking) {
1400 /* Transform feedback code assumes varyings are packed, so if the driver
1401 * has disabled varying packing, make sure it does not support transform
1402 * feedback.
1403 */
1404 assert(!ctx->Extensions.EXT_transform_feedback);
1405 } else {
1406 if (producer) {
1407 lower_packed_varyings(mem_ctx, slots_used, ir_var_shader_out,
1408 0, producer);
1409 }
1410 if (consumer) {
1411 lower_packed_varyings(mem_ctx, slots_used, ir_var_shader_in,
1412 gs_input_vertices, consumer);
1413 }
1414 }
1415
1416 if (consumer && producer) {
1417 foreach_list(node, consumer->ir) {
1418 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1419
1420 if (var && var->data.mode == ir_var_shader_in &&
1421 var->data.is_unmatched_generic_inout) {
1422 if (prog->Version <= 120) {
1423 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1424 *
1425 * Only those varying variables used (i.e. read) in
1426 * the fragment shader executable must be written to
1427 * by the vertex shader executable; declaring
1428 * superfluous varying variables in a vertex shader is
1429 * permissible.
1430 *
1431 * We interpret this text as meaning that the VS must
1432 * write the variable for the FS to read it. See
1433 * "glsl1-varying read but not written" in piglit.
1434 */
1435
1436 linker_error(prog, "%s shader varying %s not written "
1437 "by %s shader\n.",
1438 _mesa_shader_stage_to_string(consumer->Stage),
1439 var->name,
1440 _mesa_shader_stage_to_string(producer->Stage));
1441 }
1442
1443 /* An 'in' variable is only really a shader input if its
1444 * value is written by the previous stage.
1445 */
1446 var->data.mode = ir_var_auto;
1447 }
1448 }
1449 }
1450
1451 return true;
1452 }
1453
1454 bool
1455 check_against_output_limit(struct gl_context *ctx,
1456 struct gl_shader_program *prog,
1457 gl_shader *producer)
1458 {
1459 unsigned output_vectors = 0;
1460
1461 foreach_list(node, producer->ir) {
1462 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1463
1464 if (var && var->data.mode == ir_var_shader_out &&
1465 var_counts_against_varying_limit(producer->Stage, var)) {
1466 output_vectors += var->type->count_attribute_slots();
1467 }
1468 }
1469
1470 assert(producer->Stage != MESA_SHADER_FRAGMENT);
1471 unsigned max_output_components =
1472 ctx->Const.Program[producer->Stage].MaxOutputComponents;
1473
1474 const unsigned output_components = output_vectors * 4;
1475 if (output_components > max_output_components) {
1476 if (ctx->API == API_OPENGLES2 || prog->IsES)
1477 linker_error(prog, "shader uses too many output vectors "
1478 "(%u > %u)\n",
1479 output_vectors,
1480 max_output_components / 4);
1481 else
1482 linker_error(prog, "shader uses too many output components "
1483 "(%u > %u)\n",
1484 output_components,
1485 max_output_components);
1486
1487 return false;
1488 }
1489
1490 return true;
1491 }
1492
1493 bool
1494 check_against_input_limit(struct gl_context *ctx,
1495 struct gl_shader_program *prog,
1496 gl_shader *consumer)
1497 {
1498 unsigned input_vectors = 0;
1499
1500 foreach_list(node, consumer->ir) {
1501 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1502
1503 if (var && var->data.mode == ir_var_shader_in &&
1504 var_counts_against_varying_limit(consumer->Stage, var)) {
1505 input_vectors += var->type->count_attribute_slots();
1506 }
1507 }
1508
1509 assert(consumer->Stage != MESA_SHADER_VERTEX);
1510 unsigned max_input_components =
1511 ctx->Const.Program[consumer->Stage].MaxInputComponents;
1512
1513 const unsigned input_components = input_vectors * 4;
1514 if (input_components > max_input_components) {
1515 if (ctx->API == API_OPENGLES2 || prog->IsES)
1516 linker_error(prog, "shader uses too many input vectors "
1517 "(%u > %u)\n",
1518 input_vectors,
1519 max_input_components / 4);
1520 else
1521 linker_error(prog, "shader uses too many input components "
1522 "(%u > %u)\n",
1523 input_components,
1524 max_input_components);
1525
1526 return false;
1527 }
1528
1529 return true;
1530 }