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