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