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