83d0e8710054167596956408b80fdb9e35a2b18a
[mesa.git] / src / glsl / linker.cpp
1 /*
2 * Copyright © 2010 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 linker.cpp
26 * GLSL linker implementation
27 *
28 * Given a set of shaders that are to be linked to generate a final program,
29 * there are three distinct stages.
30 *
31 * In the first stage shaders are partitioned into groups based on the shader
32 * type. All shaders of a particular type (e.g., vertex shaders) are linked
33 * together.
34 *
35 * - Undefined references in each shader are resolve to definitions in
36 * another shader.
37 * - Types and qualifiers of uniforms, outputs, and global variables defined
38 * in multiple shaders with the same name are verified to be the same.
39 * - Initializers for uniforms and global variables defined
40 * in multiple shaders with the same name are verified to be the same.
41 *
42 * The result, in the terminology of the GLSL spec, is a set of shader
43 * executables for each processing unit.
44 *
45 * After the first stage is complete, a series of semantic checks are performed
46 * on each of the shader executables.
47 *
48 * - Each shader executable must define a \c main function.
49 * - Each vertex shader executable must write to \c gl_Position.
50 * - Each fragment shader executable must write to either \c gl_FragData or
51 * \c gl_FragColor.
52 *
53 * In the final stage individual shader executables are linked to create a
54 * complete exectuable.
55 *
56 * - Types of uniforms defined in multiple shader stages with the same name
57 * are verified to be the same.
58 * - Initializers for uniforms defined in multiple shader stages with the
59 * same name are verified to be the same.
60 * - Types and qualifiers of outputs defined in one stage are verified to
61 * be the same as the types and qualifiers of inputs defined with the same
62 * name in a later stage.
63 *
64 * \author Ian Romanick <ian.d.romanick@intel.com>
65 */
66
67 #include "main/core.h"
68 #include "glsl_symbol_table.h"
69 #include "ir.h"
70 #include "program.h"
71 #include "program/hash_table.h"
72 #include "linker.h"
73 #include "ir_optimization.h"
74
75 extern "C" {
76 #include "main/shaderobj.h"
77 }
78
79 #define ALIGN(value, alignment) (((value) + alignment - 1) & ~(alignment - 1))
80
81 /**
82 * Visitor that determines whether or not a variable is ever written.
83 */
84 class find_assignment_visitor : public ir_hierarchical_visitor {
85 public:
86 find_assignment_visitor(const char *name)
87 : name(name), found(false)
88 {
89 /* empty */
90 }
91
92 virtual ir_visitor_status visit_enter(ir_assignment *ir)
93 {
94 ir_variable *const var = ir->lhs->variable_referenced();
95
96 if (strcmp(name, var->name) == 0) {
97 found = true;
98 return visit_stop;
99 }
100
101 return visit_continue_with_parent;
102 }
103
104 virtual ir_visitor_status visit_enter(ir_call *ir)
105 {
106 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
107 foreach_iter(exec_list_iterator, iter, *ir) {
108 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
109 ir_variable *sig_param = (ir_variable *)sig_iter.get();
110
111 if (sig_param->mode == ir_var_out ||
112 sig_param->mode == ir_var_inout) {
113 ir_variable *var = param_rval->variable_referenced();
114 if (var && strcmp(name, var->name) == 0) {
115 found = true;
116 return visit_stop;
117 }
118 }
119 sig_iter.next();
120 }
121
122 if (ir->return_deref != NULL) {
123 ir_variable *const var = ir->return_deref->variable_referenced();
124
125 if (strcmp(name, var->name) == 0) {
126 found = true;
127 return visit_stop;
128 }
129 }
130
131 return visit_continue_with_parent;
132 }
133
134 bool variable_found()
135 {
136 return found;
137 }
138
139 private:
140 const char *name; /**< Find writes to a variable with this name. */
141 bool found; /**< Was a write to the variable found? */
142 };
143
144
145 /**
146 * Visitor that determines whether or not a variable is ever read.
147 */
148 class find_deref_visitor : public ir_hierarchical_visitor {
149 public:
150 find_deref_visitor(const char *name)
151 : name(name), found(false)
152 {
153 /* empty */
154 }
155
156 virtual ir_visitor_status visit(ir_dereference_variable *ir)
157 {
158 if (strcmp(this->name, ir->var->name) == 0) {
159 this->found = true;
160 return visit_stop;
161 }
162
163 return visit_continue;
164 }
165
166 bool variable_found() const
167 {
168 return this->found;
169 }
170
171 private:
172 const char *name; /**< Find writes to a variable with this name. */
173 bool found; /**< Was a write to the variable found? */
174 };
175
176
177 void
178 linker_error(gl_shader_program *prog, const char *fmt, ...)
179 {
180 va_list ap;
181
182 ralloc_strcat(&prog->InfoLog, "error: ");
183 va_start(ap, fmt);
184 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
185 va_end(ap);
186
187 prog->LinkStatus = false;
188 }
189
190
191 void
192 linker_warning(gl_shader_program *prog, const char *fmt, ...)
193 {
194 va_list ap;
195
196 ralloc_strcat(&prog->InfoLog, "error: ");
197 va_start(ap, fmt);
198 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
199 va_end(ap);
200
201 }
202
203
204 void
205 link_invalidate_variable_locations(gl_shader *sh, int input_base,
206 int output_base)
207 {
208 foreach_list(node, sh->ir) {
209 ir_variable *const var = ((ir_instruction *) node)->as_variable();
210
211 if (var == NULL)
212 continue;
213
214 int base;
215 switch (var->mode) {
216 case ir_var_in:
217 base = input_base;
218 break;
219 case ir_var_out:
220 base = output_base;
221 break;
222 default:
223 continue;
224 }
225
226 /* Only assign locations for generic attributes / varyings / etc.
227 */
228 if ((var->location >= base) && !var->explicit_location)
229 var->location = -1;
230
231 if ((var->location == -1) && !var->explicit_location) {
232 var->is_unmatched_generic_inout = 1;
233 var->location_frac = 0;
234 } else {
235 var->is_unmatched_generic_inout = 0;
236 }
237 }
238 }
239
240
241 /**
242 * Determine the number of attribute slots required for a particular type
243 *
244 * This code is here because it implements the language rules of a specific
245 * GLSL version. Since it's a property of the language and not a property of
246 * types in general, it doesn't really belong in glsl_type.
247 */
248 unsigned
249 count_attribute_slots(const glsl_type *t)
250 {
251 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
252 *
253 * "A scalar input counts the same amount against this limit as a vec4,
254 * so applications may want to consider packing groups of four
255 * unrelated float inputs together into a vector to better utilize the
256 * capabilities of the underlying hardware. A matrix input will use up
257 * multiple locations. The number of locations used will equal the
258 * number of columns in the matrix."
259 *
260 * The spec does not explicitly say how arrays are counted. However, it
261 * should be safe to assume the total number of slots consumed by an array
262 * is the number of entries in the array multiplied by the number of slots
263 * consumed by a single element of the array.
264 */
265
266 if (t->is_array())
267 return t->array_size() * count_attribute_slots(t->element_type());
268
269 if (t->is_matrix())
270 return t->matrix_columns;
271
272 return 1;
273 }
274
275
276 /**
277 * Verify that a vertex shader executable meets all semantic requirements.
278 *
279 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
280 * as a side effect.
281 *
282 * \param shader Vertex shader executable to be verified
283 */
284 bool
285 validate_vertex_shader_executable(struct gl_shader_program *prog,
286 struct gl_shader *shader)
287 {
288 if (shader == NULL)
289 return true;
290
291 /* From the GLSL 1.10 spec, page 48:
292 *
293 * "The variable gl_Position is available only in the vertex
294 * language and is intended for writing the homogeneous vertex
295 * position. All executions of a well-formed vertex shader
296 * executable must write a value into this variable. [...] The
297 * variable gl_Position is available only in the vertex
298 * language and is intended for writing the homogeneous vertex
299 * position. All executions of a well-formed vertex shader
300 * executable must write a value into this variable."
301 *
302 * while in GLSL 1.40 this text is changed to:
303 *
304 * "The variable gl_Position is available only in the vertex
305 * language and is intended for writing the homogeneous vertex
306 * position. It can be written at any time during shader
307 * execution. It may also be read back by a vertex shader
308 * after being written. This value will be used by primitive
309 * assembly, clipping, culling, and other fixed functionality
310 * operations, if present, that operate on primitives after
311 * vertex processing has occurred. Its value is undefined if
312 * the vertex shader executable does not write gl_Position."
313 *
314 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
315 * not an error.
316 */
317 if (prog->Version < (prog->IsES ? 300 : 140)) {
318 find_assignment_visitor find("gl_Position");
319 find.run(shader->ir);
320 if (!find.variable_found()) {
321 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
322 return false;
323 }
324 }
325
326 prog->Vert.ClipDistanceArraySize = 0;
327
328 if (!prog->IsES && prog->Version >= 130) {
329 /* From section 7.1 (Vertex Shader Special Variables) of the
330 * GLSL 1.30 spec:
331 *
332 * "It is an error for a shader to statically write both
333 * gl_ClipVertex and gl_ClipDistance."
334 *
335 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
336 * gl_ClipVertex nor gl_ClipDistance.
337 */
338 find_assignment_visitor clip_vertex("gl_ClipVertex");
339 find_assignment_visitor clip_distance("gl_ClipDistance");
340
341 clip_vertex.run(shader->ir);
342 clip_distance.run(shader->ir);
343 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
344 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
345 "and `gl_ClipDistance'\n");
346 return false;
347 }
348 prog->Vert.UsesClipDistance = clip_distance.variable_found();
349 ir_variable *clip_distance_var =
350 shader->symbols->get_variable("gl_ClipDistance");
351 if (clip_distance_var)
352 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
353 }
354
355 return true;
356 }
357
358
359 /**
360 * Verify that a fragment shader executable meets all semantic requirements
361 *
362 * \param shader Fragment shader executable to be verified
363 */
364 bool
365 validate_fragment_shader_executable(struct gl_shader_program *prog,
366 struct gl_shader *shader)
367 {
368 if (shader == NULL)
369 return true;
370
371 find_assignment_visitor frag_color("gl_FragColor");
372 find_assignment_visitor frag_data("gl_FragData");
373
374 frag_color.run(shader->ir);
375 frag_data.run(shader->ir);
376
377 if (frag_color.variable_found() && frag_data.variable_found()) {
378 linker_error(prog, "fragment shader writes to both "
379 "`gl_FragColor' and `gl_FragData'\n");
380 return false;
381 }
382
383 return true;
384 }
385
386
387 /**
388 * Generate a string describing the mode of a variable
389 */
390 static const char *
391 mode_string(const ir_variable *var)
392 {
393 switch (var->mode) {
394 case ir_var_auto:
395 return (var->read_only) ? "global constant" : "global variable";
396
397 case ir_var_uniform: return "uniform";
398 case ir_var_in: return "shader input";
399 case ir_var_out: return "shader output";
400 case ir_var_inout: return "shader inout";
401
402 case ir_var_const_in:
403 case ir_var_temporary:
404 default:
405 assert(!"Should not get here.");
406 return "invalid variable";
407 }
408 }
409
410
411 /**
412 * Perform validation of global variables used across multiple shaders
413 */
414 bool
415 cross_validate_globals(struct gl_shader_program *prog,
416 struct gl_shader **shader_list,
417 unsigned num_shaders,
418 bool uniforms_only)
419 {
420 /* Examine all of the uniforms in all of the shaders and cross validate
421 * them.
422 */
423 glsl_symbol_table variables;
424 for (unsigned i = 0; i < num_shaders; i++) {
425 if (shader_list[i] == NULL)
426 continue;
427
428 foreach_list(node, shader_list[i]->ir) {
429 ir_variable *const var = ((ir_instruction *) node)->as_variable();
430
431 if (var == NULL)
432 continue;
433
434 if (uniforms_only && (var->mode != ir_var_uniform))
435 continue;
436
437 /* Don't cross validate temporaries that are at global scope. These
438 * will eventually get pulled into the shaders 'main'.
439 */
440 if (var->mode == ir_var_temporary)
441 continue;
442
443 /* If a global with this name has already been seen, verify that the
444 * new instance has the same type. In addition, if the globals have
445 * initializers, the values of the initializers must be the same.
446 */
447 ir_variable *const existing = variables.get_variable(var->name);
448 if (existing != NULL) {
449 if (var->type != existing->type) {
450 /* Consider the types to be "the same" if both types are arrays
451 * of the same type and one of the arrays is implicitly sized.
452 * In addition, set the type of the linked variable to the
453 * explicitly sized array.
454 */
455 if (var->type->is_array()
456 && existing->type->is_array()
457 && (var->type->fields.array == existing->type->fields.array)
458 && ((var->type->length == 0)
459 || (existing->type->length == 0))) {
460 if (var->type->length != 0) {
461 existing->type = var->type;
462 }
463 } else {
464 linker_error(prog, "%s `%s' declared as type "
465 "`%s' and type `%s'\n",
466 mode_string(var),
467 var->name, var->type->name,
468 existing->type->name);
469 return false;
470 }
471 }
472
473 if (var->explicit_location) {
474 if (existing->explicit_location
475 && (var->location != existing->location)) {
476 linker_error(prog, "explicit locations for %s "
477 "`%s' have differing values\n",
478 mode_string(var), var->name);
479 return false;
480 }
481
482 existing->location = var->location;
483 existing->explicit_location = true;
484 }
485
486 /* Validate layout qualifiers for gl_FragDepth.
487 *
488 * From the AMD/ARB_conservative_depth specs:
489 *
490 * "If gl_FragDepth is redeclared in any fragment shader in a
491 * program, it must be redeclared in all fragment shaders in
492 * that program that have static assignments to
493 * gl_FragDepth. All redeclarations of gl_FragDepth in all
494 * fragment shaders in a single program must have the same set
495 * of qualifiers."
496 */
497 if (strcmp(var->name, "gl_FragDepth") == 0) {
498 bool layout_declared = var->depth_layout != ir_depth_layout_none;
499 bool layout_differs =
500 var->depth_layout != existing->depth_layout;
501
502 if (layout_declared && layout_differs) {
503 linker_error(prog,
504 "All redeclarations of gl_FragDepth in all "
505 "fragment shaders in a single program must have "
506 "the same set of qualifiers.");
507 }
508
509 if (var->used && layout_differs) {
510 linker_error(prog,
511 "If gl_FragDepth is redeclared with a layout "
512 "qualifier in any fragment shader, it must be "
513 "redeclared with the same layout qualifier in "
514 "all fragment shaders that have assignments to "
515 "gl_FragDepth");
516 }
517 }
518
519 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
520 *
521 * "If a shared global has multiple initializers, the
522 * initializers must all be constant expressions, and they
523 * must all have the same value. Otherwise, a link error will
524 * result. (A shared global having only one initializer does
525 * not require that initializer to be a constant expression.)"
526 *
527 * Previous to 4.20 the GLSL spec simply said that initializers
528 * must have the same value. In this case of non-constant
529 * initializers, this was impossible to determine. As a result,
530 * no vendor actually implemented that behavior. The 4.20
531 * behavior matches the implemented behavior of at least one other
532 * vendor, so we'll implement that for all GLSL versions.
533 */
534 if (var->constant_initializer != NULL) {
535 if (existing->constant_initializer != NULL) {
536 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
537 linker_error(prog, "initializers for %s "
538 "`%s' have differing values\n",
539 mode_string(var), var->name);
540 return false;
541 }
542 } else {
543 /* If the first-seen instance of a particular uniform did not
544 * have an initializer but a later instance does, copy the
545 * initializer to the version stored in the symbol table.
546 */
547 /* FINISHME: This is wrong. The constant_value field should
548 * FINISHME: not be modified! Imagine a case where a shader
549 * FINISHME: without an initializer is linked in two different
550 * FINISHME: programs with shaders that have differing
551 * FINISHME: initializers. Linking with the first will
552 * FINISHME: modify the shader, and linking with the second
553 * FINISHME: will fail.
554 */
555 existing->constant_initializer =
556 var->constant_initializer->clone(ralloc_parent(existing),
557 NULL);
558 }
559 }
560
561 if (var->has_initializer) {
562 if (existing->has_initializer
563 && (var->constant_initializer == NULL
564 || existing->constant_initializer == NULL)) {
565 linker_error(prog,
566 "shared global variable `%s' has multiple "
567 "non-constant initializers.\n",
568 var->name);
569 return false;
570 }
571
572 /* Some instance had an initializer, so keep track of that. In
573 * this location, all sorts of initializers (constant or
574 * otherwise) will propagate the existence to the variable
575 * stored in the symbol table.
576 */
577 existing->has_initializer = true;
578 }
579
580 if (existing->invariant != var->invariant) {
581 linker_error(prog, "declarations for %s `%s' have "
582 "mismatching invariant qualifiers\n",
583 mode_string(var), var->name);
584 return false;
585 }
586 if (existing->centroid != var->centroid) {
587 linker_error(prog, "declarations for %s `%s' have "
588 "mismatching centroid qualifiers\n",
589 mode_string(var), var->name);
590 return false;
591 }
592 } else
593 variables.add_variable(var);
594 }
595 }
596
597 return true;
598 }
599
600
601 /**
602 * Perform validation of uniforms used across multiple shader stages
603 */
604 bool
605 cross_validate_uniforms(struct gl_shader_program *prog)
606 {
607 return cross_validate_globals(prog, prog->_LinkedShaders,
608 MESA_SHADER_TYPES, true);
609 }
610
611 /**
612 * Accumulates the array of prog->UniformBlocks and checks that all
613 * definitons of blocks agree on their contents.
614 */
615 static bool
616 interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
617 {
618 unsigned max_num_uniform_blocks = 0;
619 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
620 if (prog->_LinkedShaders[i])
621 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
622 }
623
624 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
625 struct gl_shader *sh = prog->_LinkedShaders[i];
626
627 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
628 max_num_uniform_blocks);
629 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
630 prog->UniformBlockStageIndex[i][j] = -1;
631
632 if (sh == NULL)
633 continue;
634
635 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
636 int index = link_cross_validate_uniform_block(prog,
637 &prog->UniformBlocks,
638 &prog->NumUniformBlocks,
639 &sh->UniformBlocks[j]);
640
641 if (index == -1) {
642 linker_error(prog, "uniform block `%s' has mismatching definitions",
643 sh->UniformBlocks[j].Name);
644 return false;
645 }
646
647 prog->UniformBlockStageIndex[i][index] = j;
648 }
649 }
650
651 return true;
652 }
653
654 /**
655 * Validate that outputs from one stage match inputs of another
656 */
657 bool
658 cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
659 gl_shader *producer, gl_shader *consumer)
660 {
661 glsl_symbol_table parameters;
662 /* FINISHME: Figure these out dynamically. */
663 const char *const producer_stage = "vertex";
664 const char *const consumer_stage = "fragment";
665
666 /* Find all shader outputs in the "producer" stage.
667 */
668 foreach_list(node, producer->ir) {
669 ir_variable *const var = ((ir_instruction *) node)->as_variable();
670
671 /* FINISHME: For geometry shaders, this should also look for inout
672 * FINISHME: variables.
673 */
674 if ((var == NULL) || (var->mode != ir_var_out))
675 continue;
676
677 parameters.add_variable(var);
678 }
679
680
681 /* Find all shader inputs in the "consumer" stage. Any variables that have
682 * matching outputs already in the symbol table must have the same type and
683 * qualifiers.
684 */
685 foreach_list(node, consumer->ir) {
686 ir_variable *const input = ((ir_instruction *) node)->as_variable();
687
688 /* FINISHME: For geometry shaders, this should also look for inout
689 * FINISHME: variables.
690 */
691 if ((input == NULL) || (input->mode != ir_var_in))
692 continue;
693
694 ir_variable *const output = parameters.get_variable(input->name);
695 if (output != NULL) {
696 /* Check that the types match between stages.
697 */
698 if (input->type != output->type) {
699 /* There is a bit of a special case for gl_TexCoord. This
700 * built-in is unsized by default. Applications that variable
701 * access it must redeclare it with a size. There is some
702 * language in the GLSL spec that implies the fragment shader
703 * and vertex shader do not have to agree on this size. Other
704 * driver behave this way, and one or two applications seem to
705 * rely on it.
706 *
707 * Neither declaration needs to be modified here because the array
708 * sizes are fixed later when update_array_sizes is called.
709 *
710 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
711 *
712 * "Unlike user-defined varying variables, the built-in
713 * varying variables don't have a strict one-to-one
714 * correspondence between the vertex language and the
715 * fragment language."
716 */
717 if (!output->type->is_array()
718 || (strncmp("gl_", output->name, 3) != 0)) {
719 linker_error(prog,
720 "%s shader output `%s' declared as type `%s', "
721 "but %s shader input declared as type `%s'\n",
722 producer_stage, output->name,
723 output->type->name,
724 consumer_stage, input->type->name);
725 return false;
726 }
727 }
728
729 /* Check that all of the qualifiers match between stages.
730 */
731 if (input->centroid != output->centroid) {
732 linker_error(prog,
733 "%s shader output `%s' %s centroid qualifier, "
734 "but %s shader input %s centroid qualifier\n",
735 producer_stage,
736 output->name,
737 (output->centroid) ? "has" : "lacks",
738 consumer_stage,
739 (input->centroid) ? "has" : "lacks");
740 return false;
741 }
742
743 if (input->invariant != output->invariant) {
744 linker_error(prog,
745 "%s shader output `%s' %s invariant qualifier, "
746 "but %s shader input %s invariant qualifier\n",
747 producer_stage,
748 output->name,
749 (output->invariant) ? "has" : "lacks",
750 consumer_stage,
751 (input->invariant) ? "has" : "lacks");
752 return false;
753 }
754
755 if (input->interpolation != output->interpolation) {
756 linker_error(prog,
757 "%s shader output `%s' specifies %s "
758 "interpolation qualifier, "
759 "but %s shader input specifies %s "
760 "interpolation qualifier\n",
761 producer_stage,
762 output->name,
763 output->interpolation_string(),
764 consumer_stage,
765 input->interpolation_string());
766 return false;
767 }
768 }
769 }
770
771 return true;
772 }
773
774
775 /**
776 * Populates a shaders symbol table with all global declarations
777 */
778 static void
779 populate_symbol_table(gl_shader *sh)
780 {
781 sh->symbols = new(sh) glsl_symbol_table;
782
783 foreach_list(node, sh->ir) {
784 ir_instruction *const inst = (ir_instruction *) node;
785 ir_variable *var;
786 ir_function *func;
787
788 if ((func = inst->as_function()) != NULL) {
789 sh->symbols->add_function(func);
790 } else if ((var = inst->as_variable()) != NULL) {
791 sh->symbols->add_variable(var);
792 }
793 }
794 }
795
796
797 /**
798 * Remap variables referenced in an instruction tree
799 *
800 * This is used when instruction trees are cloned from one shader and placed in
801 * another. These trees will contain references to \c ir_variable nodes that
802 * do not exist in the target shader. This function finds these \c ir_variable
803 * references and replaces the references with matching variables in the target
804 * shader.
805 *
806 * If there is no matching variable in the target shader, a clone of the
807 * \c ir_variable is made and added to the target shader. The new variable is
808 * added to \b both the instruction stream and the symbol table.
809 *
810 * \param inst IR tree that is to be processed.
811 * \param symbols Symbol table containing global scope symbols in the
812 * linked shader.
813 * \param instructions Instruction stream where new variable declarations
814 * should be added.
815 */
816 void
817 remap_variables(ir_instruction *inst, struct gl_shader *target,
818 hash_table *temps)
819 {
820 class remap_visitor : public ir_hierarchical_visitor {
821 public:
822 remap_visitor(struct gl_shader *target,
823 hash_table *temps)
824 {
825 this->target = target;
826 this->symbols = target->symbols;
827 this->instructions = target->ir;
828 this->temps = temps;
829 }
830
831 virtual ir_visitor_status visit(ir_dereference_variable *ir)
832 {
833 if (ir->var->mode == ir_var_temporary) {
834 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
835
836 assert(var != NULL);
837 ir->var = var;
838 return visit_continue;
839 }
840
841 ir_variable *const existing =
842 this->symbols->get_variable(ir->var->name);
843 if (existing != NULL)
844 ir->var = existing;
845 else {
846 ir_variable *copy = ir->var->clone(this->target, NULL);
847
848 this->symbols->add_variable(copy);
849 this->instructions->push_head(copy);
850 ir->var = copy;
851 }
852
853 return visit_continue;
854 }
855
856 private:
857 struct gl_shader *target;
858 glsl_symbol_table *symbols;
859 exec_list *instructions;
860 hash_table *temps;
861 };
862
863 remap_visitor v(target, temps);
864
865 inst->accept(&v);
866 }
867
868
869 /**
870 * Move non-declarations from one instruction stream to another
871 *
872 * The intended usage pattern of this function is to pass the pointer to the
873 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
874 * pointer) for \c last and \c false for \c make_copies on the first
875 * call. Successive calls pass the return value of the previous call for
876 * \c last and \c true for \c make_copies.
877 *
878 * \param instructions Source instruction stream
879 * \param last Instruction after which new instructions should be
880 * inserted in the target instruction stream
881 * \param make_copies Flag selecting whether instructions in \c instructions
882 * should be copied (via \c ir_instruction::clone) into the
883 * target list or moved.
884 *
885 * \return
886 * The new "last" instruction in the target instruction stream. This pointer
887 * is suitable for use as the \c last parameter of a later call to this
888 * function.
889 */
890 exec_node *
891 move_non_declarations(exec_list *instructions, exec_node *last,
892 bool make_copies, gl_shader *target)
893 {
894 hash_table *temps = NULL;
895
896 if (make_copies)
897 temps = hash_table_ctor(0, hash_table_pointer_hash,
898 hash_table_pointer_compare);
899
900 foreach_list_safe(node, instructions) {
901 ir_instruction *inst = (ir_instruction *) node;
902
903 if (inst->as_function())
904 continue;
905
906 ir_variable *var = inst->as_variable();
907 if ((var != NULL) && (var->mode != ir_var_temporary))
908 continue;
909
910 assert(inst->as_assignment()
911 || inst->as_call()
912 || inst->as_if() /* for initializers with the ?: operator */
913 || ((var != NULL) && (var->mode == ir_var_temporary)));
914
915 if (make_copies) {
916 inst = inst->clone(target, NULL);
917
918 if (var != NULL)
919 hash_table_insert(temps, inst, var);
920 else
921 remap_variables(inst, target, temps);
922 } else {
923 inst->remove();
924 }
925
926 last->insert_after(inst);
927 last = inst;
928 }
929
930 if (make_copies)
931 hash_table_dtor(temps);
932
933 return last;
934 }
935
936 /**
937 * Get the function signature for main from a shader
938 */
939 static ir_function_signature *
940 get_main_function_signature(gl_shader *sh)
941 {
942 ir_function *const f = sh->symbols->get_function("main");
943 if (f != NULL) {
944 exec_list void_parameters;
945
946 /* Look for the 'void main()' signature and ensure that it's defined.
947 * This keeps the linker from accidentally pick a shader that just
948 * contains a prototype for main.
949 *
950 * We don't have to check for multiple definitions of main (in multiple
951 * shaders) because that would have already been caught above.
952 */
953 ir_function_signature *sig = f->matching_signature(&void_parameters);
954 if ((sig != NULL) && sig->is_defined) {
955 return sig;
956 }
957 }
958
959 return NULL;
960 }
961
962
963 /**
964 * This class is only used in link_intrastage_shaders() below but declaring
965 * it inside that function leads to compiler warnings with some versions of
966 * gcc.
967 */
968 class array_sizing_visitor : public ir_hierarchical_visitor {
969 public:
970 virtual ir_visitor_status visit(ir_variable *var)
971 {
972 if (var->type->is_array() && (var->type->length == 0)) {
973 const glsl_type *type =
974 glsl_type::get_array_instance(var->type->fields.array,
975 var->max_array_access + 1);
976 assert(type != NULL);
977 var->type = type;
978 }
979 return visit_continue;
980 }
981 };
982
983 /**
984 * Combine a group of shaders for a single stage to generate a linked shader
985 *
986 * \note
987 * If this function is supplied a single shader, it is cloned, and the new
988 * shader is returned.
989 */
990 static struct gl_shader *
991 link_intrastage_shaders(void *mem_ctx,
992 struct gl_context *ctx,
993 struct gl_shader_program *prog,
994 struct gl_shader **shader_list,
995 unsigned num_shaders)
996 {
997 struct gl_uniform_block *uniform_blocks = NULL;
998 unsigned num_uniform_blocks = 0;
999
1000 /* Check that global variables defined in multiple shaders are consistent.
1001 */
1002 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
1003 return NULL;
1004
1005 /* Check that uniform blocks between shaders for a stage agree. */
1006 for (unsigned i = 0; i < num_shaders; i++) {
1007 struct gl_shader *sh = shader_list[i];
1008
1009 for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) {
1010 link_assign_uniform_block_offsets(shader_list[i]);
1011
1012 int index = link_cross_validate_uniform_block(mem_ctx,
1013 &uniform_blocks,
1014 &num_uniform_blocks,
1015 &sh->UniformBlocks[j]);
1016 if (index == -1) {
1017 linker_error(prog, "uniform block `%s' has mismatching definitions",
1018 sh->UniformBlocks[j].Name);
1019 return NULL;
1020 }
1021 }
1022 }
1023
1024 /* Check that there is only a single definition of each function signature
1025 * across all shaders.
1026 */
1027 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1028 foreach_list(node, shader_list[i]->ir) {
1029 ir_function *const f = ((ir_instruction *) node)->as_function();
1030
1031 if (f == NULL)
1032 continue;
1033
1034 for (unsigned j = i + 1; j < num_shaders; j++) {
1035 ir_function *const other =
1036 shader_list[j]->symbols->get_function(f->name);
1037
1038 /* If the other shader has no function (and therefore no function
1039 * signatures) with the same name, skip to the next shader.
1040 */
1041 if (other == NULL)
1042 continue;
1043
1044 foreach_iter (exec_list_iterator, iter, *f) {
1045 ir_function_signature *sig =
1046 (ir_function_signature *) iter.get();
1047
1048 if (!sig->is_defined || sig->is_builtin)
1049 continue;
1050
1051 ir_function_signature *other_sig =
1052 other->exact_matching_signature(& sig->parameters);
1053
1054 if ((other_sig != NULL) && other_sig->is_defined
1055 && !other_sig->is_builtin) {
1056 linker_error(prog, "function `%s' is multiply defined",
1057 f->name);
1058 return NULL;
1059 }
1060 }
1061 }
1062 }
1063 }
1064
1065 /* Find the shader that defines main, and make a clone of it.
1066 *
1067 * Starting with the clone, search for undefined references. If one is
1068 * found, find the shader that defines it. Clone the reference and add
1069 * it to the shader. Repeat until there are no undefined references or
1070 * until a reference cannot be resolved.
1071 */
1072 gl_shader *main = NULL;
1073 for (unsigned i = 0; i < num_shaders; i++) {
1074 if (get_main_function_signature(shader_list[i]) != NULL) {
1075 main = shader_list[i];
1076 break;
1077 }
1078 }
1079
1080 if (main == NULL) {
1081 linker_error(prog, "%s shader lacks `main'\n",
1082 (shader_list[0]->Type == GL_VERTEX_SHADER)
1083 ? "vertex" : "fragment");
1084 return NULL;
1085 }
1086
1087 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
1088 linked->ir = new(linked) exec_list;
1089 clone_ir_list(mem_ctx, linked->ir, main->ir);
1090
1091 linked->UniformBlocks = uniform_blocks;
1092 linked->NumUniformBlocks = num_uniform_blocks;
1093 ralloc_steal(linked, linked->UniformBlocks);
1094
1095 populate_symbol_table(linked);
1096
1097 /* The a pointer to the main function in the final linked shader (i.e., the
1098 * copy of the original shader that contained the main function).
1099 */
1100 ir_function_signature *const main_sig = get_main_function_signature(linked);
1101
1102 /* Move any instructions other than variable declarations or function
1103 * declarations into main.
1104 */
1105 exec_node *insertion_point =
1106 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1107 linked);
1108
1109 for (unsigned i = 0; i < num_shaders; i++) {
1110 if (shader_list[i] == main)
1111 continue;
1112
1113 insertion_point = move_non_declarations(shader_list[i]->ir,
1114 insertion_point, true, linked);
1115 }
1116
1117 /* Resolve initializers for global variables in the linked shader.
1118 */
1119 unsigned num_linking_shaders = num_shaders;
1120 for (unsigned i = 0; i < num_shaders; i++)
1121 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1122
1123 gl_shader **linking_shaders =
1124 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1125
1126 memcpy(linking_shaders, shader_list,
1127 sizeof(linking_shaders[0]) * num_shaders);
1128
1129 unsigned idx = num_shaders;
1130 for (unsigned i = 0; i < num_shaders; i++) {
1131 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1132 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1133 idx += shader_list[i]->num_builtins_to_link;
1134 }
1135
1136 assert(idx == num_linking_shaders);
1137
1138 if (!link_function_calls(prog, linked, linking_shaders,
1139 num_linking_shaders)) {
1140 ctx->Driver.DeleteShader(ctx, linked);
1141 linked = NULL;
1142 }
1143
1144 free(linking_shaders);
1145
1146 #ifdef DEBUG
1147 /* At this point linked should contain all of the linked IR, so
1148 * validate it to make sure nothing went wrong.
1149 */
1150 if (linked)
1151 validate_ir_tree(linked->ir);
1152 #endif
1153
1154 /* Make a pass over all variable declarations to ensure that arrays with
1155 * unspecified sizes have a size specified. The size is inferred from the
1156 * max_array_access field.
1157 */
1158 if (linked != NULL) {
1159 array_sizing_visitor v;
1160
1161 v.run(linked->ir);
1162 }
1163
1164 return linked;
1165 }
1166
1167 /**
1168 * Update the sizes of linked shader uniform arrays to the maximum
1169 * array index used.
1170 *
1171 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1172 *
1173 * If one or more elements of an array are active,
1174 * GetActiveUniform will return the name of the array in name,
1175 * subject to the restrictions listed above. The type of the array
1176 * is returned in type. The size parameter contains the highest
1177 * array element index used, plus one. The compiler or linker
1178 * determines the highest index used. There will be only one
1179 * active uniform reported by the GL per uniform array.
1180
1181 */
1182 static void
1183 update_array_sizes(struct gl_shader_program *prog)
1184 {
1185 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1186 if (prog->_LinkedShaders[i] == NULL)
1187 continue;
1188
1189 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1190 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1191
1192 if ((var == NULL) || (var->mode != ir_var_uniform &&
1193 var->mode != ir_var_in &&
1194 var->mode != ir_var_out) ||
1195 !var->type->is_array())
1196 continue;
1197
1198 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1199 * will not be eliminated. Since we always do std140, just
1200 * don't resize arrays in UBOs.
1201 */
1202 if (var->uniform_block != -1)
1203 continue;
1204
1205 unsigned int size = var->max_array_access;
1206 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1207 if (prog->_LinkedShaders[j] == NULL)
1208 continue;
1209
1210 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1211 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1212 if (!other_var)
1213 continue;
1214
1215 if (strcmp(var->name, other_var->name) == 0 &&
1216 other_var->max_array_access > size) {
1217 size = other_var->max_array_access;
1218 }
1219 }
1220 }
1221
1222 if (size + 1 != var->type->fields.array->length) {
1223 /* If this is a built-in uniform (i.e., it's backed by some
1224 * fixed-function state), adjust the number of state slots to
1225 * match the new array size. The number of slots per array entry
1226 * is not known. It seems safe to assume that the total number of
1227 * slots is an integer multiple of the number of array elements.
1228 * Determine the number of slots per array element by dividing by
1229 * the old (total) size.
1230 */
1231 if (var->num_state_slots > 0) {
1232 var->num_state_slots = (size + 1)
1233 * (var->num_state_slots / var->type->length);
1234 }
1235
1236 var->type = glsl_type::get_array_instance(var->type->fields.array,
1237 size + 1);
1238 /* FINISHME: We should update the types of array
1239 * dereferences of this variable now.
1240 */
1241 }
1242 }
1243 }
1244 }
1245
1246 /**
1247 * Find a contiguous set of available bits in a bitmask.
1248 *
1249 * \param used_mask Bits representing used (1) and unused (0) locations
1250 * \param needed_count Number of contiguous bits needed.
1251 *
1252 * \return
1253 * Base location of the available bits on success or -1 on failure.
1254 */
1255 int
1256 find_available_slots(unsigned used_mask, unsigned needed_count)
1257 {
1258 unsigned needed_mask = (1 << needed_count) - 1;
1259 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1260
1261 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1262 * cannot optimize possibly infinite loops" for the loop below.
1263 */
1264 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1265 return -1;
1266
1267 for (int i = 0; i <= max_bit_to_test; i++) {
1268 if ((needed_mask & ~used_mask) == needed_mask)
1269 return i;
1270
1271 needed_mask <<= 1;
1272 }
1273
1274 return -1;
1275 }
1276
1277
1278 /**
1279 * Assign locations for either VS inputs for FS outputs
1280 *
1281 * \param prog Shader program whose variables need locations assigned
1282 * \param target_index Selector for the program target to receive location
1283 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1284 * \c MESA_SHADER_FRAGMENT.
1285 * \param max_index Maximum number of generic locations. This corresponds
1286 * to either the maximum number of draw buffers or the
1287 * maximum number of generic attributes.
1288 *
1289 * \return
1290 * If locations are successfully assigned, true is returned. Otherwise an
1291 * error is emitted to the shader link log and false is returned.
1292 */
1293 bool
1294 assign_attribute_or_color_locations(gl_shader_program *prog,
1295 unsigned target_index,
1296 unsigned max_index)
1297 {
1298 /* Mark invalid locations as being used.
1299 */
1300 unsigned used_locations = (max_index >= 32)
1301 ? ~0 : ~((1 << max_index) - 1);
1302
1303 assert((target_index == MESA_SHADER_VERTEX)
1304 || (target_index == MESA_SHADER_FRAGMENT));
1305
1306 gl_shader *const sh = prog->_LinkedShaders[target_index];
1307 if (sh == NULL)
1308 return true;
1309
1310 /* Operate in a total of four passes.
1311 *
1312 * 1. Invalidate the location assignments for all vertex shader inputs.
1313 *
1314 * 2. Assign locations for inputs that have user-defined (via
1315 * glBindVertexAttribLocation) locations and outputs that have
1316 * user-defined locations (via glBindFragDataLocation).
1317 *
1318 * 3. Sort the attributes without assigned locations by number of slots
1319 * required in decreasing order. Fragmentation caused by attribute
1320 * locations assigned by the application may prevent large attributes
1321 * from having enough contiguous space.
1322 *
1323 * 4. Assign locations to any inputs without assigned locations.
1324 */
1325
1326 const int generic_base = (target_index == MESA_SHADER_VERTEX)
1327 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
1328
1329 const enum ir_variable_mode direction =
1330 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1331
1332
1333 /* Temporary storage for the set of attributes that need locations assigned.
1334 */
1335 struct temp_attr {
1336 unsigned slots;
1337 ir_variable *var;
1338
1339 /* Used below in the call to qsort. */
1340 static int compare(const void *a, const void *b)
1341 {
1342 const temp_attr *const l = (const temp_attr *) a;
1343 const temp_attr *const r = (const temp_attr *) b;
1344
1345 /* Reversed because we want a descending order sort below. */
1346 return r->slots - l->slots;
1347 }
1348 } to_assign[16];
1349
1350 unsigned num_attr = 0;
1351
1352 foreach_list(node, sh->ir) {
1353 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1354
1355 if ((var == NULL) || (var->mode != (unsigned) direction))
1356 continue;
1357
1358 if (var->explicit_location) {
1359 if ((var->location >= (int)(max_index + generic_base))
1360 || (var->location < 0)) {
1361 linker_error(prog,
1362 "invalid explicit location %d specified for `%s'\n",
1363 (var->location < 0)
1364 ? var->location : var->location - generic_base,
1365 var->name);
1366 return false;
1367 }
1368 } else if (target_index == MESA_SHADER_VERTEX) {
1369 unsigned binding;
1370
1371 if (prog->AttributeBindings->get(binding, var->name)) {
1372 assert(binding >= VERT_ATTRIB_GENERIC0);
1373 var->location = binding;
1374 var->is_unmatched_generic_inout = 0;
1375 }
1376 } else if (target_index == MESA_SHADER_FRAGMENT) {
1377 unsigned binding;
1378 unsigned index;
1379
1380 if (prog->FragDataBindings->get(binding, var->name)) {
1381 assert(binding >= FRAG_RESULT_DATA0);
1382 var->location = binding;
1383 var->is_unmatched_generic_inout = 0;
1384
1385 if (prog->FragDataIndexBindings->get(index, var->name)) {
1386 var->index = index;
1387 }
1388 }
1389 }
1390
1391 /* If the variable is not a built-in and has a location statically
1392 * assigned in the shader (presumably via a layout qualifier), make sure
1393 * that it doesn't collide with other assigned locations. Otherwise,
1394 * add it to the list of variables that need linker-assigned locations.
1395 */
1396 const unsigned slots = count_attribute_slots(var->type);
1397 if (var->location != -1) {
1398 if (var->location >= generic_base && var->index < 1) {
1399 /* From page 61 of the OpenGL 4.0 spec:
1400 *
1401 * "LinkProgram will fail if the attribute bindings assigned
1402 * by BindAttribLocation do not leave not enough space to
1403 * assign a location for an active matrix attribute or an
1404 * active attribute array, both of which require multiple
1405 * contiguous generic attributes."
1406 *
1407 * Previous versions of the spec contain similar language but omit
1408 * the bit about attribute arrays.
1409 *
1410 * Page 61 of the OpenGL 4.0 spec also says:
1411 *
1412 * "It is possible for an application to bind more than one
1413 * attribute name to the same location. This is referred to as
1414 * aliasing. This will only work if only one of the aliased
1415 * attributes is active in the executable program, or if no
1416 * path through the shader consumes more than one attribute of
1417 * a set of attributes aliased to the same location. A link
1418 * error can occur if the linker determines that every path
1419 * through the shader consumes multiple aliased attributes,
1420 * but implementations are not required to generate an error
1421 * in this case."
1422 *
1423 * These two paragraphs are either somewhat contradictory, or I
1424 * don't fully understand one or both of them.
1425 */
1426 /* FINISHME: The code as currently written does not support
1427 * FINISHME: attribute location aliasing (see comment above).
1428 */
1429 /* Mask representing the contiguous slots that will be used by
1430 * this attribute.
1431 */
1432 const unsigned attr = var->location - generic_base;
1433 const unsigned use_mask = (1 << slots) - 1;
1434
1435 /* Generate a link error if the set of bits requested for this
1436 * attribute overlaps any previously allocated bits.
1437 */
1438 if ((~(use_mask << attr) & used_locations) != used_locations) {
1439 const char *const string = (target_index == MESA_SHADER_VERTEX)
1440 ? "vertex shader input" : "fragment shader output";
1441 linker_error(prog,
1442 "insufficient contiguous locations "
1443 "available for %s `%s' %d %d %d", string,
1444 var->name, used_locations, use_mask, attr);
1445 return false;
1446 }
1447
1448 used_locations |= (use_mask << attr);
1449 }
1450
1451 continue;
1452 }
1453
1454 to_assign[num_attr].slots = slots;
1455 to_assign[num_attr].var = var;
1456 num_attr++;
1457 }
1458
1459 /* If all of the attributes were assigned locations by the application (or
1460 * are built-in attributes with fixed locations), return early. This should
1461 * be the common case.
1462 */
1463 if (num_attr == 0)
1464 return true;
1465
1466 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1467
1468 if (target_index == MESA_SHADER_VERTEX) {
1469 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1470 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1471 * reserved to prevent it from being automatically allocated below.
1472 */
1473 find_deref_visitor find("gl_Vertex");
1474 find.run(sh->ir);
1475 if (find.variable_found())
1476 used_locations |= (1 << 0);
1477 }
1478
1479 for (unsigned i = 0; i < num_attr; i++) {
1480 /* Mask representing the contiguous slots that will be used by this
1481 * attribute.
1482 */
1483 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1484
1485 int location = find_available_slots(used_locations, to_assign[i].slots);
1486
1487 if (location < 0) {
1488 const char *const string = (target_index == MESA_SHADER_VERTEX)
1489 ? "vertex shader input" : "fragment shader output";
1490
1491 linker_error(prog,
1492 "insufficient contiguous locations "
1493 "available for %s `%s'",
1494 string, to_assign[i].var->name);
1495 return false;
1496 }
1497
1498 to_assign[i].var->location = generic_base + location;
1499 to_assign[i].var->is_unmatched_generic_inout = 0;
1500 used_locations |= (use_mask << location);
1501 }
1502
1503 return true;
1504 }
1505
1506
1507 /**
1508 * Demote shader inputs and outputs that are not used in other stages
1509 */
1510 void
1511 demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
1512 {
1513 foreach_list(node, sh->ir) {
1514 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1515
1516 if ((var == NULL) || (var->mode != int(mode)))
1517 continue;
1518
1519 /* A shader 'in' or 'out' variable is only really an input or output if
1520 * its value is used by other shader stages. This will cause the variable
1521 * to have a location assigned.
1522 */
1523 if (var->is_unmatched_generic_inout) {
1524 var->mode = ir_var_auto;
1525 }
1526 }
1527 }
1528
1529
1530 /**
1531 * Data structure tracking information about a transform feedback declaration
1532 * during linking.
1533 */
1534 class tfeedback_decl
1535 {
1536 public:
1537 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1538 const void *mem_ctx, const char *input);
1539 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1540 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1541 ir_variable *output_var);
1542 unsigned get_num_outputs() const;
1543 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
1544 struct gl_transform_feedback_info *info, unsigned buffer,
1545 const unsigned max_outputs) const;
1546 ir_variable *find_output_var(gl_shader_program *prog,
1547 gl_shader *producer) const;
1548
1549 bool is_next_buffer_separator() const
1550 {
1551 return this->next_buffer_separator;
1552 }
1553
1554 bool is_varying() const
1555 {
1556 return !this->next_buffer_separator && !this->skip_components;
1557 }
1558
1559 /**
1560 * The total number of varying components taken up by this variable. Only
1561 * valid if assign_location() has been called.
1562 */
1563 unsigned num_components() const
1564 {
1565 if (this->is_clip_distance_mesa)
1566 return this->size;
1567 else
1568 return this->vector_elements * this->matrix_columns * this->size;
1569 }
1570
1571 private:
1572 /**
1573 * The name that was supplied to glTransformFeedbackVaryings. Used for
1574 * error reporting and glGetTransformFeedbackVarying().
1575 */
1576 const char *orig_name;
1577
1578 /**
1579 * The name of the variable, parsed from orig_name.
1580 */
1581 const char *var_name;
1582
1583 /**
1584 * True if the declaration in orig_name represents an array.
1585 */
1586 bool is_subscripted;
1587
1588 /**
1589 * If is_subscripted is true, the subscript that was specified in orig_name.
1590 */
1591 unsigned array_subscript;
1592
1593 /**
1594 * True if the variable is gl_ClipDistance and the driver lowers
1595 * gl_ClipDistance to gl_ClipDistanceMESA.
1596 */
1597 bool is_clip_distance_mesa;
1598
1599 /**
1600 * The vertex shader output location that the linker assigned for this
1601 * variable. -1 if a location hasn't been assigned yet.
1602 */
1603 int location;
1604
1605 /**
1606 * If non-zero, then this variable may be packed along with other variables
1607 * into a single varying slot, so this offset should be applied when
1608 * accessing components. For example, an offset of 1 means that the x
1609 * component of this variable is actually stored in component y of the
1610 * location specified by \c location.
1611 *
1612 * Only valid if location != -1.
1613 */
1614 unsigned location_frac;
1615
1616 /**
1617 * If location != -1, the number of vector elements in this variable, or 1
1618 * if this variable is a scalar.
1619 */
1620 unsigned vector_elements;
1621
1622 /**
1623 * If location != -1, the number of matrix columns in this variable, or 1
1624 * if this variable is not a matrix.
1625 */
1626 unsigned matrix_columns;
1627
1628 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1629 GLenum type;
1630
1631 /**
1632 * If location != -1, the size that should be returned by
1633 * glGetTransformFeedbackVarying().
1634 */
1635 unsigned size;
1636
1637 /**
1638 * How many components to skip. If non-zero, this is
1639 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1640 */
1641 unsigned skip_components;
1642
1643 /**
1644 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1645 */
1646 bool next_buffer_separator;
1647 };
1648
1649
1650 /**
1651 * Initialize this object based on a string that was passed to
1652 * glTransformFeedbackVaryings. If there is a parse error, the error is
1653 * reported using linker_error(), and false is returned.
1654 */
1655 bool
1656 tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1657 const void *mem_ctx, const char *input)
1658 {
1659 /* We don't have to be pedantic about what is a valid GLSL variable name,
1660 * because any variable with an invalid name can't exist in the IR anyway.
1661 */
1662
1663 this->location = -1;
1664 this->orig_name = input;
1665 this->is_clip_distance_mesa = false;
1666 this->skip_components = 0;
1667 this->next_buffer_separator = false;
1668
1669 if (ctx->Extensions.ARB_transform_feedback3) {
1670 /* Parse gl_NextBuffer. */
1671 if (strcmp(input, "gl_NextBuffer") == 0) {
1672 this->next_buffer_separator = true;
1673 return true;
1674 }
1675
1676 /* Parse gl_SkipComponents. */
1677 if (strcmp(input, "gl_SkipComponents1") == 0)
1678 this->skip_components = 1;
1679 else if (strcmp(input, "gl_SkipComponents2") == 0)
1680 this->skip_components = 2;
1681 else if (strcmp(input, "gl_SkipComponents3") == 0)
1682 this->skip_components = 3;
1683 else if (strcmp(input, "gl_SkipComponents4") == 0)
1684 this->skip_components = 4;
1685
1686 if (this->skip_components)
1687 return true;
1688 }
1689
1690 /* Parse a declaration. */
1691 const char *bracket = strrchr(input, '[');
1692
1693 if (bracket) {
1694 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
1695 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
1696 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1697 return false;
1698 }
1699 this->is_subscripted = true;
1700 } else {
1701 this->var_name = ralloc_strdup(mem_ctx, input);
1702 this->is_subscripted = false;
1703 }
1704
1705 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1706 * class must behave specially to account for the fact that gl_ClipDistance
1707 * is converted from a float[8] to a vec4[2].
1708 */
1709 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1710 strcmp(this->var_name, "gl_ClipDistance") == 0) {
1711 this->is_clip_distance_mesa = true;
1712 }
1713
1714 return true;
1715 }
1716
1717
1718 /**
1719 * Determine whether two tfeedback_decl objects refer to the same variable and
1720 * array index (if applicable).
1721 */
1722 bool
1723 tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1724 {
1725 assert(x.is_varying() && y.is_varying());
1726
1727 if (strcmp(x.var_name, y.var_name) != 0)
1728 return false;
1729 if (x.is_subscripted != y.is_subscripted)
1730 return false;
1731 if (x.is_subscripted && x.array_subscript != y.array_subscript)
1732 return false;
1733 return true;
1734 }
1735
1736
1737 /**
1738 * Assign a location for this tfeedback_decl object based on the location
1739 * assignment in output_var.
1740 *
1741 * If an error occurs, the error is reported through linker_error() and false
1742 * is returned.
1743 */
1744 bool
1745 tfeedback_decl::assign_location(struct gl_context *ctx,
1746 struct gl_shader_program *prog,
1747 ir_variable *output_var)
1748 {
1749 assert(this->is_varying());
1750
1751 if (output_var->type->is_array()) {
1752 /* Array variable */
1753 const unsigned matrix_cols =
1754 output_var->type->fields.array->matrix_columns;
1755 const unsigned vector_elements =
1756 output_var->type->fields.array->vector_elements;
1757 unsigned actual_array_size = this->is_clip_distance_mesa ?
1758 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
1759
1760 if (this->is_subscripted) {
1761 /* Check array bounds. */
1762 if (this->array_subscript >= actual_array_size) {
1763 linker_error(prog, "Transform feedback varying %s has index "
1764 "%i, but the array size is %u.",
1765 this->orig_name, this->array_subscript,
1766 actual_array_size);
1767 return false;
1768 }
1769 if (this->is_clip_distance_mesa) {
1770 this->location =
1771 output_var->location + this->array_subscript / 4;
1772 this->location_frac = this->array_subscript % 4;
1773 } else {
1774 unsigned fine_location
1775 = output_var->location * 4 + output_var->location_frac;
1776 unsigned array_elem_size = vector_elements * matrix_cols;
1777 fine_location += array_elem_size * this->array_subscript;
1778 this->location = fine_location / 4;
1779 this->location_frac = fine_location % 4;
1780 }
1781 this->size = 1;
1782 } else {
1783 this->location = output_var->location;
1784 this->location_frac = output_var->location_frac;
1785 this->size = actual_array_size;
1786 }
1787 this->vector_elements = vector_elements;
1788 this->matrix_columns = matrix_cols;
1789 if (this->is_clip_distance_mesa)
1790 this->type = GL_FLOAT;
1791 else
1792 this->type = output_var->type->fields.array->gl_type;
1793 } else {
1794 /* Regular variable (scalar, vector, or matrix) */
1795 if (this->is_subscripted) {
1796 linker_error(prog, "Transform feedback varying %s requested, "
1797 "but %s is not an array.",
1798 this->orig_name, this->var_name);
1799 return false;
1800 }
1801 this->location = output_var->location;
1802 this->location_frac = output_var->location_frac;
1803 this->size = 1;
1804 this->vector_elements = output_var->type->vector_elements;
1805 this->matrix_columns = output_var->type->matrix_columns;
1806 this->type = output_var->type->gl_type;
1807 }
1808
1809 /* From GL_EXT_transform_feedback:
1810 * A program will fail to link if:
1811 *
1812 * * the total number of components to capture in any varying
1813 * variable in <varyings> is greater than the constant
1814 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1815 * buffer mode is SEPARATE_ATTRIBS_EXT;
1816 */
1817 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1818 this->num_components() >
1819 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1820 linker_error(prog, "Transform feedback varying %s exceeds "
1821 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1822 this->orig_name);
1823 return false;
1824 }
1825
1826 return true;
1827 }
1828
1829
1830 unsigned
1831 tfeedback_decl::get_num_outputs() const
1832 {
1833 if (!this->is_varying()) {
1834 return 0;
1835 }
1836
1837 return (this->num_components() + this->location_frac + 3)/4;
1838 }
1839
1840
1841 /**
1842 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1843 *
1844 * If an error occurs, the error is reported through linker_error() and false
1845 * is returned.
1846 */
1847 bool
1848 tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1849 struct gl_transform_feedback_info *info,
1850 unsigned buffer, const unsigned max_outputs) const
1851 {
1852 assert(!this->next_buffer_separator);
1853
1854 /* Handle gl_SkipComponents. */
1855 if (this->skip_components) {
1856 info->BufferStride[buffer] += this->skip_components;
1857 return true;
1858 }
1859
1860 /* From GL_EXT_transform_feedback:
1861 * A program will fail to link if:
1862 *
1863 * * the total number of components to capture is greater than
1864 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1865 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1866 */
1867 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1868 info->BufferStride[buffer] + this->num_components() >
1869 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1870 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1871 "limit has been exceeded.");
1872 return false;
1873 }
1874
1875 unsigned location = this->location;
1876 unsigned location_frac = this->location_frac;
1877 unsigned num_components = this->num_components();
1878 while (num_components > 0) {
1879 unsigned output_size = MIN2(num_components, 4 - location_frac);
1880 assert(info->NumOutputs < max_outputs);
1881 info->Outputs[info->NumOutputs].ComponentOffset = location_frac;
1882 info->Outputs[info->NumOutputs].OutputRegister = location;
1883 info->Outputs[info->NumOutputs].NumComponents = output_size;
1884 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1885 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
1886 ++info->NumOutputs;
1887 info->BufferStride[buffer] += output_size;
1888 num_components -= output_size;
1889 location++;
1890 location_frac = 0;
1891 }
1892
1893 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1894 info->Varyings[info->NumVarying].Type = this->type;
1895 info->Varyings[info->NumVarying].Size = this->size;
1896 info->NumVarying++;
1897
1898 return true;
1899 }
1900
1901
1902 ir_variable *
1903 tfeedback_decl::find_output_var(gl_shader_program *prog,
1904 gl_shader *producer) const
1905 {
1906 const char *name = this->is_clip_distance_mesa
1907 ? "gl_ClipDistanceMESA" : this->var_name;
1908 ir_variable *var = producer->symbols->get_variable(name);
1909 if (var && var->mode == ir_var_out)
1910 return var;
1911
1912 /* From GL_EXT_transform_feedback:
1913 * A program will fail to link if:
1914 *
1915 * * any variable name specified in the <varyings> array is not
1916 * declared as an output in the geometry shader (if present) or
1917 * the vertex shader (if no geometry shader is present);
1918 */
1919 linker_error(prog, "Transform feedback varying %s undeclared.",
1920 this->orig_name);
1921 return NULL;
1922 }
1923
1924
1925 /**
1926 * Parse all the transform feedback declarations that were passed to
1927 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1928 *
1929 * If an error occurs, the error is reported through linker_error() and false
1930 * is returned.
1931 */
1932 static bool
1933 parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1934 const void *mem_ctx, unsigned num_names,
1935 char **varying_names, tfeedback_decl *decls)
1936 {
1937 for (unsigned i = 0; i < num_names; ++i) {
1938 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
1939 return false;
1940
1941 if (!decls[i].is_varying())
1942 continue;
1943
1944 /* From GL_EXT_transform_feedback:
1945 * A program will fail to link if:
1946 *
1947 * * any two entries in the <varyings> array specify the same varying
1948 * variable;
1949 *
1950 * We interpret this to mean "any two entries in the <varyings> array
1951 * specify the same varying variable and array index", since transform
1952 * feedback of arrays would be useless otherwise.
1953 */
1954 for (unsigned j = 0; j < i; ++j) {
1955 if (!decls[j].is_varying())
1956 continue;
1957
1958 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1959 linker_error(prog, "Transform feedback varying %s specified "
1960 "more than once.", varying_names[i]);
1961 return false;
1962 }
1963 }
1964 }
1965 return true;
1966 }
1967
1968
1969 /**
1970 * Data structure recording the relationship between outputs of one shader
1971 * stage (the "producer") and inputs of another (the "consumer").
1972 */
1973 class varying_matches
1974 {
1975 public:
1976 varying_matches(bool disable_varying_packing);
1977 ~varying_matches();
1978 void record(ir_variable *producer_var, ir_variable *consumer_var);
1979 unsigned assign_locations();
1980 void store_locations(unsigned producer_base, unsigned consumer_base) const;
1981
1982 private:
1983 /**
1984 * If true, this driver disables varying packing, so all varyings need to
1985 * be aligned on slot boundaries, and take up a number of slots equal to
1986 * their number of matrix columns times their array size.
1987 */
1988 const bool disable_varying_packing;
1989
1990 /**
1991 * Enum representing the order in which varyings are packed within a
1992 * packing class.
1993 *
1994 * Currently we pack vec4's first, then vec2's, then scalar values, then
1995 * vec3's. This order ensures that the only vectors that are at risk of
1996 * having to be "double parked" (split between two adjacent varying slots)
1997 * are the vec3's.
1998 */
1999 enum packing_order_enum {
2000 PACKING_ORDER_VEC4,
2001 PACKING_ORDER_VEC2,
2002 PACKING_ORDER_SCALAR,
2003 PACKING_ORDER_VEC3,
2004 };
2005
2006 static unsigned compute_packing_class(ir_variable *var);
2007 static packing_order_enum compute_packing_order(ir_variable *var);
2008 static int match_comparator(const void *x_generic, const void *y_generic);
2009
2010 /**
2011 * Structure recording the relationship between a single producer output
2012 * and a single consumer input.
2013 */
2014 struct match {
2015 /**
2016 * Packing class for this varying, computed by compute_packing_class().
2017 */
2018 unsigned packing_class;
2019
2020 /**
2021 * Packing order for this varying, computed by compute_packing_order().
2022 */
2023 packing_order_enum packing_order;
2024 unsigned num_components;
2025
2026 /**
2027 * The output variable in the producer stage.
2028 */
2029 ir_variable *producer_var;
2030
2031 /**
2032 * The input variable in the consumer stage.
2033 */
2034 ir_variable *consumer_var;
2035
2036 /**
2037 * The location which has been assigned for this varying. This is
2038 * expressed in multiples of a float, with the first generic varying
2039 * (i.e. the one referred to by VERT_RESULT_VAR0 or FRAG_ATTRIB_VAR0)
2040 * represented by the value 0.
2041 */
2042 unsigned generic_location;
2043 } *matches;
2044
2045 /**
2046 * The number of elements in the \c matches array that are currently in
2047 * use.
2048 */
2049 unsigned num_matches;
2050
2051 /**
2052 * The number of elements that were set aside for the \c matches array when
2053 * it was allocated.
2054 */
2055 unsigned matches_capacity;
2056 };
2057
2058
2059 varying_matches::varying_matches(bool disable_varying_packing)
2060 : disable_varying_packing(disable_varying_packing)
2061 {
2062 /* Note: this initial capacity is rather arbitrarily chosen to be large
2063 * enough for many cases without wasting an unreasonable amount of space.
2064 * varying_matches::record() will resize the array if there are more than
2065 * this number of varyings.
2066 */
2067 this->matches_capacity = 8;
2068 this->matches = (match *)
2069 malloc(sizeof(*this->matches) * this->matches_capacity);
2070 this->num_matches = 0;
2071 }
2072
2073
2074 varying_matches::~varying_matches()
2075 {
2076 free(this->matches);
2077 }
2078
2079
2080 /**
2081 * Record the given producer/consumer variable pair in the list of variables
2082 * that should later be assigned locations.
2083 *
2084 * It is permissible for \c consumer_var to be NULL (this happens if a
2085 * variable is output by the producer and consumed by transform feedback, but
2086 * not consumed by the consumer).
2087 *
2088 * If \c producer_var has already been paired up with a consumer_var, or
2089 * producer_var is part of fixed pipeline functionality (and hence already has
2090 * a location assigned), this function has no effect.
2091 */
2092 void
2093 varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
2094 {
2095 if (!producer_var->is_unmatched_generic_inout) {
2096 /* Either a location already exists for this variable (since it is part
2097 * of fixed functionality), or it has already been recorded as part of a
2098 * previous match.
2099 */
2100 return;
2101 }
2102
2103 if (this->num_matches == this->matches_capacity) {
2104 this->matches_capacity *= 2;
2105 this->matches = (match *)
2106 realloc(this->matches,
2107 sizeof(*this->matches) * this->matches_capacity);
2108 }
2109 this->matches[this->num_matches].packing_class
2110 = this->compute_packing_class(producer_var);
2111 this->matches[this->num_matches].packing_order
2112 = this->compute_packing_order(producer_var);
2113 if (this->disable_varying_packing) {
2114 unsigned slots = producer_var->type->is_array()
2115 ? (producer_var->type->length
2116 * producer_var->type->fields.array->matrix_columns)
2117 : producer_var->type->matrix_columns;
2118 this->matches[this->num_matches].num_components = 4 * slots;
2119 } else {
2120 this->matches[this->num_matches].num_components
2121 = producer_var->type->component_slots();
2122 }
2123 this->matches[this->num_matches].producer_var = producer_var;
2124 this->matches[this->num_matches].consumer_var = consumer_var;
2125 this->num_matches++;
2126 producer_var->is_unmatched_generic_inout = 0;
2127 if (consumer_var)
2128 consumer_var->is_unmatched_generic_inout = 0;
2129 }
2130
2131
2132 /**
2133 * Choose locations for all of the variable matches that were previously
2134 * passed to varying_matches::record().
2135 */
2136 unsigned
2137 varying_matches::assign_locations()
2138 {
2139 /* Sort varying matches into an order that makes them easy to pack. */
2140 qsort(this->matches, this->num_matches, sizeof(*this->matches),
2141 &varying_matches::match_comparator);
2142
2143 unsigned generic_location = 0;
2144
2145 for (unsigned i = 0; i < this->num_matches; i++) {
2146 /* Advance to the next slot if this varying has a different packing
2147 * class than the previous one, and we're not already on a slot
2148 * boundary.
2149 */
2150 if (i > 0 &&
2151 this->matches[i - 1].packing_class
2152 != this->matches[i].packing_class) {
2153 generic_location = ALIGN(generic_location, 4);
2154 }
2155
2156 this->matches[i].generic_location = generic_location;
2157
2158 generic_location += this->matches[i].num_components;
2159 }
2160
2161 return (generic_location + 3) / 4;
2162 }
2163
2164
2165 /**
2166 * Update the producer and consumer shaders to reflect the locations
2167 * assignments that were made by varying_matches::assign_locations().
2168 */
2169 void
2170 varying_matches::store_locations(unsigned producer_base,
2171 unsigned consumer_base) const
2172 {
2173 for (unsigned i = 0; i < this->num_matches; i++) {
2174 ir_variable *producer_var = this->matches[i].producer_var;
2175 ir_variable *consumer_var = this->matches[i].consumer_var;
2176 unsigned generic_location = this->matches[i].generic_location;
2177 unsigned slot = generic_location / 4;
2178 unsigned offset = generic_location % 4;
2179
2180 producer_var->location = producer_base + slot;
2181 producer_var->location_frac = offset;
2182 if (consumer_var) {
2183 assert(consumer_var->location == -1);
2184 consumer_var->location = consumer_base + slot;
2185 consumer_var->location_frac = offset;
2186 }
2187 }
2188 }
2189
2190
2191 /**
2192 * Compute the "packing class" of the given varying. This is an unsigned
2193 * integer with the property that two variables in the same packing class can
2194 * be safely backed into the same vec4.
2195 */
2196 unsigned
2197 varying_matches::compute_packing_class(ir_variable *var)
2198 {
2199 /* In this initial implementation we conservatively assume that variables
2200 * can only be packed if their base type (float/int/uint/bool) matches and
2201 * their interpolation and centroid qualifiers match.
2202 *
2203 * TODO: relax these restrictions when the driver back-end permits.
2204 */
2205 unsigned packing_class = var->centroid ? 1 : 0;
2206 packing_class *= 4;
2207 packing_class += var->interpolation;
2208 packing_class *= GLSL_TYPE_ERROR;
2209 packing_class += var->type->get_scalar_type()->base_type;
2210 return packing_class;
2211 }
2212
2213
2214 /**
2215 * Compute the "packing order" of the given varying. This is a sort key we
2216 * use to determine when to attempt to pack the given varying relative to
2217 * other varyings in the same packing class.
2218 */
2219 varying_matches::packing_order_enum
2220 varying_matches::compute_packing_order(ir_variable *var)
2221 {
2222 const glsl_type *element_type = var->type;
2223
2224 /* FINISHME: Support for "varying" records in GLSL 1.50. */
2225 while (element_type->base_type == GLSL_TYPE_ARRAY) {
2226 element_type = element_type->fields.array;
2227 }
2228
2229 switch (element_type->vector_elements) {
2230 case 1: return PACKING_ORDER_SCALAR;
2231 case 2: return PACKING_ORDER_VEC2;
2232 case 3: return PACKING_ORDER_VEC3;
2233 case 4: return PACKING_ORDER_VEC4;
2234 default:
2235 assert(!"Unexpected value of vector_elements");
2236 return PACKING_ORDER_VEC4;
2237 }
2238 }
2239
2240
2241 /**
2242 * Comparison function passed to qsort() to sort varyings by packing_class and
2243 * then by packing_order.
2244 */
2245 int
2246 varying_matches::match_comparator(const void *x_generic, const void *y_generic)
2247 {
2248 const match *x = (const match *) x_generic;
2249 const match *y = (const match *) y_generic;
2250
2251 if (x->packing_class != y->packing_class)
2252 return x->packing_class - y->packing_class;
2253 return x->packing_order - y->packing_order;
2254 }
2255
2256
2257 /**
2258 * Is the given variable a varying variable to be counted against the
2259 * limit in ctx->Const.MaxVarying?
2260 * This includes variables such as texcoords, colors and generic
2261 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2262 */
2263 static bool
2264 is_varying_var(GLenum shaderType, const ir_variable *var)
2265 {
2266 /* Only fragment shaders will take a varying variable as an input */
2267 if (shaderType == GL_FRAGMENT_SHADER &&
2268 var->mode == ir_var_in) {
2269 switch (var->location) {
2270 case FRAG_ATTRIB_WPOS:
2271 case FRAG_ATTRIB_FACE:
2272 case FRAG_ATTRIB_PNTC:
2273 return false;
2274 default:
2275 return true;
2276 }
2277 }
2278 return false;
2279 }
2280
2281
2282 /**
2283 * Assign locations for all variables that are produced in one pipeline stage
2284 * (the "producer") and consumed in the next stage (the "consumer").
2285 *
2286 * Variables produced by the producer may also be consumed by transform
2287 * feedback.
2288 *
2289 * \param num_tfeedback_decls is the number of declarations indicating
2290 * variables that may be consumed by transform feedback.
2291 *
2292 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2293 * representing the result of parsing the strings passed to
2294 * glTransformFeedbackVaryings(). assign_location() will be called for
2295 * each of these objects that matches one of the outputs of the
2296 * producer.
2297 *
2298 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2299 * be NULL. In this case, varying locations are assigned solely based on the
2300 * requirements of transform feedback.
2301 */
2302 bool
2303 assign_varying_locations(struct gl_context *ctx,
2304 struct gl_shader_program *prog,
2305 gl_shader *producer, gl_shader *consumer,
2306 unsigned num_tfeedback_decls,
2307 tfeedback_decl *tfeedback_decls)
2308 {
2309 /* FINISHME: Set dynamically when geometry shader support is added. */
2310 const unsigned producer_base = VERT_RESULT_VAR0;
2311 const unsigned consumer_base = FRAG_ATTRIB_VAR0;
2312 varying_matches matches(ctx->Const.DisableVaryingPacking);
2313
2314 /* Operate in a total of three passes.
2315 *
2316 * 1. Assign locations for any matching inputs and outputs.
2317 *
2318 * 2. Mark output variables in the producer that do not have locations as
2319 * not being outputs. This lets the optimizer eliminate them.
2320 *
2321 * 3. Mark input variables in the consumer that do not have locations as
2322 * not being inputs. This lets the optimizer eliminate them.
2323 */
2324
2325 foreach_list(node, producer->ir) {
2326 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2327
2328 if ((output_var == NULL) || (output_var->mode != ir_var_out))
2329 continue;
2330
2331 ir_variable *input_var =
2332 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
2333
2334 if (input_var && input_var->mode != ir_var_in)
2335 input_var = NULL;
2336
2337 if (input_var) {
2338 matches.record(output_var, input_var);
2339 }
2340 }
2341
2342 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2343 if (!tfeedback_decls[i].is_varying())
2344 continue;
2345
2346 ir_variable *output_var
2347 = tfeedback_decls[i].find_output_var(prog, producer);
2348
2349 if (output_var == NULL)
2350 return false;
2351
2352 if (output_var->is_unmatched_generic_inout) {
2353 matches.record(output_var, NULL);
2354 }
2355 }
2356
2357 const unsigned slots_used = matches.assign_locations();
2358 matches.store_locations(producer_base, consumer_base);
2359
2360 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2361 if (!tfeedback_decls[i].is_varying())
2362 continue;
2363
2364 ir_variable *output_var
2365 = tfeedback_decls[i].find_output_var(prog, producer);
2366
2367 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2368 return false;
2369 }
2370
2371 if (ctx->Const.DisableVaryingPacking) {
2372 /* Transform feedback code assumes varyings are packed, so if the driver
2373 * has disabled varying packing, make sure it does not support transform
2374 * feedback.
2375 */
2376 assert(!ctx->Extensions.EXT_transform_feedback);
2377 } else {
2378 lower_packed_varyings(ctx, producer_base, slots_used, ir_var_out,
2379 producer);
2380 if (consumer) {
2381 lower_packed_varyings(ctx, consumer_base, slots_used, ir_var_in,
2382 consumer);
2383 }
2384 }
2385
2386 unsigned varying_vectors = 0;
2387
2388 if (consumer) {
2389 foreach_list(node, consumer->ir) {
2390 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2391
2392 if ((var == NULL) || (var->mode != ir_var_in))
2393 continue;
2394
2395 if (var->is_unmatched_generic_inout) {
2396 if (prog->Version <= 120) {
2397 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2398 *
2399 * Only those varying variables used (i.e. read) in
2400 * the fragment shader executable must be written to
2401 * by the vertex shader executable; declaring
2402 * superfluous varying variables in a vertex shader is
2403 * permissible.
2404 *
2405 * We interpret this text as meaning that the VS must
2406 * write the variable for the FS to read it. See
2407 * "glsl1-varying read but not written" in piglit.
2408 */
2409
2410 linker_error(prog, "fragment shader varying %s not written "
2411 "by vertex shader\n.", var->name);
2412 }
2413
2414 /* An 'in' variable is only really a shader input if its
2415 * value is written by the previous stage.
2416 */
2417 var->mode = ir_var_auto;
2418 } else if (is_varying_var(consumer->Type, var)) {
2419 /* The packing rules are used for vertex shader inputs are also
2420 * used for fragment shader inputs.
2421 */
2422 varying_vectors += count_attribute_slots(var->type);
2423 }
2424 }
2425 }
2426
2427 if (ctx->API == API_OPENGLES2 || prog->IsES) {
2428 if (varying_vectors > ctx->Const.MaxVarying) {
2429 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2430 linker_warning(prog, "shader uses too many varying vectors "
2431 "(%u > %u), but the driver will try to optimize "
2432 "them out; this is non-portable out-of-spec "
2433 "behavior\n",
2434 varying_vectors, ctx->Const.MaxVarying);
2435 } else {
2436 linker_error(prog, "shader uses too many varying vectors "
2437 "(%u > %u)\n",
2438 varying_vectors, ctx->Const.MaxVarying);
2439 return false;
2440 }
2441 }
2442 } else {
2443 const unsigned float_components = varying_vectors * 4;
2444 if (float_components > ctx->Const.MaxVarying * 4) {
2445 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2446 linker_warning(prog, "shader uses too many varying components "
2447 "(%u > %u), but the driver will try to optimize "
2448 "them out; this is non-portable out-of-spec "
2449 "behavior\n",
2450 float_components, ctx->Const.MaxVarying * 4);
2451 } else {
2452 linker_error(prog, "shader uses too many varying components "
2453 "(%u > %u)\n",
2454 float_components, ctx->Const.MaxVarying * 4);
2455 return false;
2456 }
2457 }
2458 }
2459
2460 return true;
2461 }
2462
2463
2464 /**
2465 * Store transform feedback location assignments into
2466 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2467 *
2468 * If an error occurs, the error is reported through linker_error() and false
2469 * is returned.
2470 */
2471 static bool
2472 store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2473 unsigned num_tfeedback_decls,
2474 tfeedback_decl *tfeedback_decls)
2475 {
2476 bool separate_attribs_mode =
2477 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
2478
2479 ralloc_free(prog->LinkedTransformFeedback.Varyings);
2480 ralloc_free(prog->LinkedTransformFeedback.Outputs);
2481
2482 memset(&prog->LinkedTransformFeedback, 0,
2483 sizeof(prog->LinkedTransformFeedback));
2484
2485 prog->LinkedTransformFeedback.Varyings =
2486 rzalloc_array(prog,
2487 struct gl_transform_feedback_varying_info,
2488 num_tfeedback_decls);
2489
2490 unsigned num_outputs = 0;
2491 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
2492 num_outputs += tfeedback_decls[i].get_num_outputs();
2493
2494 prog->LinkedTransformFeedback.Outputs =
2495 rzalloc_array(prog,
2496 struct gl_transform_feedback_output,
2497 num_outputs);
2498
2499 unsigned num_buffers = 0;
2500
2501 if (separate_attribs_mode) {
2502 /* GL_SEPARATE_ATTRIBS */
2503 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2504 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2505 num_buffers, num_outputs))
2506 return false;
2507
2508 num_buffers++;
2509 }
2510 }
2511 else {
2512 /* GL_INVERLEAVED_ATTRIBS */
2513 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2514 if (tfeedback_decls[i].is_next_buffer_separator()) {
2515 num_buffers++;
2516 continue;
2517 }
2518
2519 if (!tfeedback_decls[i].store(ctx, prog,
2520 &prog->LinkedTransformFeedback,
2521 num_buffers, num_outputs))
2522 return false;
2523 }
2524 num_buffers++;
2525 }
2526
2527 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
2528
2529 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
2530 return true;
2531 }
2532
2533 /**
2534 * Store the gl_FragDepth layout in the gl_shader_program struct.
2535 */
2536 static void
2537 store_fragdepth_layout(struct gl_shader_program *prog)
2538 {
2539 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2540 return;
2541 }
2542
2543 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2544
2545 /* We don't look up the gl_FragDepth symbol directly because if
2546 * gl_FragDepth is not used in the shader, it's removed from the IR.
2547 * However, the symbol won't be removed from the symbol table.
2548 *
2549 * We're only interested in the cases where the variable is NOT removed
2550 * from the IR.
2551 */
2552 foreach_list(node, ir) {
2553 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2554
2555 if (var == NULL || var->mode != ir_var_out) {
2556 continue;
2557 }
2558
2559 if (strcmp(var->name, "gl_FragDepth") == 0) {
2560 switch (var->depth_layout) {
2561 case ir_depth_layout_none:
2562 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2563 return;
2564 case ir_depth_layout_any:
2565 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2566 return;
2567 case ir_depth_layout_greater:
2568 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2569 return;
2570 case ir_depth_layout_less:
2571 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2572 return;
2573 case ir_depth_layout_unchanged:
2574 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2575 return;
2576 default:
2577 assert(0);
2578 return;
2579 }
2580 }
2581 }
2582 }
2583
2584 /**
2585 * Validate the resources used by a program versus the implementation limits
2586 */
2587 static bool
2588 check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2589 {
2590 static const char *const shader_names[MESA_SHADER_TYPES] = {
2591 "vertex", "fragment", "geometry"
2592 };
2593
2594 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2595 ctx->Const.MaxVertexTextureImageUnits,
2596 ctx->Const.MaxTextureImageUnits,
2597 ctx->Const.MaxGeometryTextureImageUnits
2598 };
2599
2600 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2601 ctx->Const.VertexProgram.MaxUniformComponents,
2602 ctx->Const.FragmentProgram.MaxUniformComponents,
2603 0 /* FINISHME: Geometry shaders. */
2604 };
2605
2606 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
2607 ctx->Const.VertexProgram.MaxUniformBlocks,
2608 ctx->Const.FragmentProgram.MaxUniformBlocks,
2609 ctx->Const.GeometryProgram.MaxUniformBlocks,
2610 };
2611
2612 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2613 struct gl_shader *sh = prog->_LinkedShaders[i];
2614
2615 if (sh == NULL)
2616 continue;
2617
2618 if (sh->num_samplers > max_samplers[i]) {
2619 linker_error(prog, "Too many %s shader texture samplers",
2620 shader_names[i]);
2621 }
2622
2623 if (sh->num_uniform_components > max_uniform_components[i]) {
2624 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2625 linker_warning(prog, "Too many %s shader uniform components, "
2626 "but the driver will try to optimize them out; "
2627 "this is non-portable out-of-spec behavior\n",
2628 shader_names[i]);
2629 } else {
2630 linker_error(prog, "Too many %s shader uniform components",
2631 shader_names[i]);
2632 }
2633 }
2634 }
2635
2636 unsigned blocks[MESA_SHADER_TYPES] = {0};
2637 unsigned total_uniform_blocks = 0;
2638
2639 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
2640 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
2641 if (prog->UniformBlockStageIndex[j][i] != -1) {
2642 blocks[j]++;
2643 total_uniform_blocks++;
2644 }
2645 }
2646
2647 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
2648 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
2649 prog->NumUniformBlocks,
2650 ctx->Const.MaxCombinedUniformBlocks);
2651 } else {
2652 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2653 if (blocks[i] > max_uniform_blocks[i]) {
2654 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
2655 shader_names[i],
2656 blocks[i],
2657 max_uniform_blocks[i]);
2658 break;
2659 }
2660 }
2661 }
2662 }
2663
2664 return prog->LinkStatus;
2665 }
2666
2667 void
2668 link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
2669 {
2670 tfeedback_decl *tfeedback_decls = NULL;
2671 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2672
2673 void *mem_ctx = ralloc_context(NULL); // temporary linker context
2674
2675 prog->LinkStatus = false;
2676 prog->Validated = false;
2677 prog->_Used = false;
2678
2679 ralloc_free(prog->InfoLog);
2680 prog->InfoLog = ralloc_strdup(NULL, "");
2681
2682 ralloc_free(prog->UniformBlocks);
2683 prog->UniformBlocks = NULL;
2684 prog->NumUniformBlocks = 0;
2685 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2686 ralloc_free(prog->UniformBlockStageIndex[i]);
2687 prog->UniformBlockStageIndex[i] = NULL;
2688 }
2689
2690 /* Separate the shaders into groups based on their type.
2691 */
2692 struct gl_shader **vert_shader_list;
2693 unsigned num_vert_shaders = 0;
2694 struct gl_shader **frag_shader_list;
2695 unsigned num_frag_shaders = 0;
2696
2697 vert_shader_list = (struct gl_shader **)
2698 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
2699 frag_shader_list = &vert_shader_list[prog->NumShaders];
2700
2701 unsigned min_version = UINT_MAX;
2702 unsigned max_version = 0;
2703 const bool is_es_prog =
2704 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
2705 for (unsigned i = 0; i < prog->NumShaders; i++) {
2706 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2707 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2708
2709 if (prog->Shaders[i]->IsES != is_es_prog) {
2710 linker_error(prog, "all shaders must use same shading "
2711 "language version\n");
2712 goto done;
2713 }
2714
2715 switch (prog->Shaders[i]->Type) {
2716 case GL_VERTEX_SHADER:
2717 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2718 num_vert_shaders++;
2719 break;
2720 case GL_FRAGMENT_SHADER:
2721 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2722 num_frag_shaders++;
2723 break;
2724 case GL_GEOMETRY_SHADER:
2725 /* FINISHME: Support geometry shaders. */
2726 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2727 break;
2728 }
2729 }
2730
2731 /* Previous to GLSL version 1.30, different compilation units could mix and
2732 * match shading language versions. With GLSL 1.30 and later, the versions
2733 * of all shaders must match.
2734 *
2735 * GLSL ES has never allowed mixing of shading language versions.
2736 */
2737 if ((is_es_prog || max_version >= 130)
2738 && min_version != max_version) {
2739 linker_error(prog, "all shaders must use same shading "
2740 "language version\n");
2741 goto done;
2742 }
2743
2744 prog->Version = max_version;
2745 prog->IsES = is_es_prog;
2746
2747 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2748 if (prog->_LinkedShaders[i] != NULL)
2749 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2750
2751 prog->_LinkedShaders[i] = NULL;
2752 }
2753
2754 /* Link all shaders for a particular stage and validate the result.
2755 */
2756 if (num_vert_shaders > 0) {
2757 gl_shader *const sh =
2758 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2759 num_vert_shaders);
2760
2761 if (sh == NULL)
2762 goto done;
2763
2764 if (!validate_vertex_shader_executable(prog, sh))
2765 goto done;
2766
2767 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2768 sh);
2769 }
2770
2771 if (num_frag_shaders > 0) {
2772 gl_shader *const sh =
2773 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2774 num_frag_shaders);
2775
2776 if (sh == NULL)
2777 goto done;
2778
2779 if (!validate_fragment_shader_executable(prog, sh))
2780 goto done;
2781
2782 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2783 sh);
2784 }
2785
2786 /* Here begins the inter-stage linking phase. Some initial validation is
2787 * performed, then locations are assigned for uniforms, attributes, and
2788 * varyings.
2789 */
2790 if (cross_validate_uniforms(prog)) {
2791 unsigned prev;
2792
2793 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2794 if (prog->_LinkedShaders[prev] != NULL)
2795 break;
2796 }
2797
2798 /* Validate the inputs of each stage with the output of the preceding
2799 * stage.
2800 */
2801 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2802 if (prog->_LinkedShaders[i] == NULL)
2803 continue;
2804
2805 if (!cross_validate_outputs_to_inputs(prog,
2806 prog->_LinkedShaders[prev],
2807 prog->_LinkedShaders[i]))
2808 goto done;
2809
2810 prev = i;
2811 }
2812
2813 prog->LinkStatus = true;
2814 }
2815
2816 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2817 * it before optimization because we want most of the checks to get
2818 * dropped thanks to constant propagation.
2819 *
2820 * This rule also applies to GLSL ES 3.00.
2821 */
2822 if (max_version >= (is_es_prog ? 300 : 130)) {
2823 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2824 if (sh) {
2825 lower_discard_flow(sh->ir);
2826 }
2827 }
2828
2829 if (!interstage_cross_validate_uniform_blocks(prog))
2830 goto done;
2831
2832 /* Do common optimization before assigning storage for attributes,
2833 * uniforms, and varyings. Later optimization could possibly make
2834 * some of that unused.
2835 */
2836 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2837 if (prog->_LinkedShaders[i] == NULL)
2838 continue;
2839
2840 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2841 if (!prog->LinkStatus)
2842 goto done;
2843
2844 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2845 lower_clip_distance(prog->_LinkedShaders[i]);
2846 }
2847
2848 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2849
2850 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
2851 ;
2852 }
2853
2854 /* Mark all generic shader inputs and outputs as unpaired. */
2855 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2856 link_invalidate_variable_locations(
2857 prog->_LinkedShaders[MESA_SHADER_VERTEX],
2858 VERT_ATTRIB_GENERIC0, VERT_RESULT_VAR0);
2859 }
2860 /* FINISHME: Geometry shaders not implemented yet */
2861 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2862 link_invalidate_variable_locations(
2863 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2864 FRAG_ATTRIB_VAR0, FRAG_RESULT_DATA0);
2865 }
2866
2867 /* FINISHME: The value of the max_attribute_index parameter is
2868 * FINISHME: implementation dependent based on the value of
2869 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2870 * FINISHME: at least 16, so hardcode 16 for now.
2871 */
2872 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
2873 goto done;
2874 }
2875
2876 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
2877 goto done;
2878 }
2879
2880 unsigned prev;
2881 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2882 if (prog->_LinkedShaders[prev] != NULL)
2883 break;
2884 }
2885
2886 if (num_tfeedback_decls != 0) {
2887 /* From GL_EXT_transform_feedback:
2888 * A program will fail to link if:
2889 *
2890 * * the <count> specified by TransformFeedbackVaryingsEXT is
2891 * non-zero, but the program object has no vertex or geometry
2892 * shader;
2893 */
2894 if (prev >= MESA_SHADER_FRAGMENT) {
2895 linker_error(prog, "Transform feedback varyings specified, but "
2896 "no vertex or geometry shader is present.");
2897 goto done;
2898 }
2899
2900 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2901 prog->TransformFeedback.NumVarying);
2902 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
2903 prog->TransformFeedback.VaryingNames,
2904 tfeedback_decls))
2905 goto done;
2906 }
2907
2908 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2909 if (prog->_LinkedShaders[i] == NULL)
2910 continue;
2911
2912 if (!assign_varying_locations(
2913 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2914 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2915 tfeedback_decls))
2916 goto done;
2917
2918 prev = i;
2919 }
2920
2921 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2922 /* There was no fragment shader, but we still have to assign varying
2923 * locations for use by transform feedback.
2924 */
2925 if (!assign_varying_locations(
2926 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2927 tfeedback_decls))
2928 goto done;
2929 }
2930
2931 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2932 goto done;
2933
2934 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2935 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2936 ir_var_out);
2937
2938 /* Eliminate code that is now dead due to unused vertex outputs being
2939 * demoted.
2940 */
2941 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2942 ;
2943 }
2944
2945 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2946 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2947
2948 demote_shader_inputs_and_outputs(sh, ir_var_in);
2949 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2950 demote_shader_inputs_and_outputs(sh, ir_var_out);
2951
2952 /* Eliminate code that is now dead due to unused geometry outputs being
2953 * demoted.
2954 */
2955 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2956 ;
2957 }
2958
2959 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2960 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2961
2962 demote_shader_inputs_and_outputs(sh, ir_var_in);
2963
2964 /* Eliminate code that is now dead due to unused fragment inputs being
2965 * demoted. This shouldn't actually do anything other than remove
2966 * declarations of the (now unused) global variables.
2967 */
2968 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2969 ;
2970 }
2971
2972 update_array_sizes(prog);
2973 link_assign_uniform_locations(prog);
2974 store_fragdepth_layout(prog);
2975
2976 if (!check_resources(ctx, prog))
2977 goto done;
2978
2979 /* OpenGL ES requires that a vertex shader and a fragment shader both be
2980 * present in a linked program. By checking prog->IsES, we also
2981 * catch the GL_ARB_ES2_compatibility case.
2982 */
2983 if (!prog->InternalSeparateShader &&
2984 (ctx->API == API_OPENGLES2 || prog->IsES)) {
2985 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
2986 linker_error(prog, "program lacks a vertex shader\n");
2987 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2988 linker_error(prog, "program lacks a fragment shader\n");
2989 }
2990 }
2991
2992 /* FINISHME: Assign fragment shader output locations. */
2993
2994 done:
2995 free(vert_shader_list);
2996
2997 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2998 if (prog->_LinkedShaders[i] == NULL)
2999 continue;
3000
3001 /* Retain any live IR, but trash the rest. */
3002 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
3003
3004 /* The symbol table in the linked shaders may contain references to
3005 * variables that were removed (e.g., unused uniforms). Since it may
3006 * contain junk, there is no possible valid use. Delete it and set the
3007 * pointer to NULL.
3008 */
3009 delete prog->_LinkedShaders[i]->symbols;
3010 prog->_LinkedShaders[i]->symbols = NULL;
3011 }
3012
3013 ralloc_free(mem_ctx);
3014 }