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