blob: 155c9d40b32f631515ebb4c772c8e89e3b87efbd [file] [log] [blame]
Ian Romanick832dfa52010-06-17 15:04:20 -07001/*
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#include <cstdlib>
67#include <cstdio>
Ian Romanickf36460e2010-06-23 12:07:22 -070068#include <cstdarg>
69
70extern "C" {
71#include <talloc.h>
72}
Ian Romanick832dfa52010-06-17 15:04:20 -070073
Ian Romanick0ad22cd2010-06-21 17:18:31 -070074#include "main/mtypes.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070075#include "glsl_symbol_table.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070076#include "ir.h"
77#include "program.h"
Ian Romanick019a59b2010-06-21 16:10:42 -070078#include "hash_table.h"
Ian Romanick3fb87872010-07-09 14:09:34 -070079#include "shader_api.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070080
81/**
82 * Visitor that determines whether or not a variable is ever written.
83 */
84class find_assignment_visitor : public ir_hierarchical_visitor {
85public:
86 find_assignment_visitor(const char *name)
87 : name(name), found(false)
88 {
89 /* empty */
90 }
91
92 virtual ir_visitor_status visit_enter(ir_assignment *ir)
93 {
94 ir_variable *const var = ir->lhs->variable_referenced();
95
96 if (strcmp(name, var->name) == 0) {
97 found = true;
98 return visit_stop;
99 }
100
101 return visit_continue_with_parent;
102 }
103
104 bool variable_found()
105 {
106 return found;
107 }
108
109private:
110 const char *name; /**< Find writes to a variable with this name. */
111 bool found; /**< Was a write to the variable found? */
112};
113
Ian Romanickc93b8f12010-06-17 15:20:22 -0700114
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700115void
Eric Anholt849e1812010-06-30 11:49:17 -0700116linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700117{
118 va_list ap;
119
120 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
121 va_start(ap, fmt);
122 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
123 va_end(ap);
124}
125
126
127void
Eric Anholt16b68b12010-06-30 11:05:43 -0700128invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700129 int generic_base)
130{
Eric Anholt16b68b12010-06-30 11:05:43 -0700131 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700132 ir_variable *const var = ((ir_instruction *) node)->as_variable();
133
134 if ((var == NULL) || (var->mode != (unsigned) mode))
135 continue;
136
137 /* Only assign locations for generic attributes / varyings / etc.
138 */
139 if (var->location >= generic_base)
140 var->location = -1;
141 }
142}
143
144
Ian Romanickc93b8f12010-06-17 15:20:22 -0700145/**
Ian Romanick69846702010-06-22 17:29:19 -0700146 * Determine the number of attribute slots required for a particular type
147 *
148 * This code is here because it implements the language rules of a specific
149 * GLSL version. Since it's a property of the language and not a property of
150 * types in general, it doesn't really belong in glsl_type.
151 */
152unsigned
153count_attribute_slots(const glsl_type *t)
154{
155 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
156 *
157 * "A scalar input counts the same amount against this limit as a vec4,
158 * so applications may want to consider packing groups of four
159 * unrelated float inputs together into a vector to better utilize the
160 * capabilities of the underlying hardware. A matrix input will use up
161 * multiple locations. The number of locations used will equal the
162 * number of columns in the matrix."
163 *
164 * The spec does not explicitly say how arrays are counted. However, it
165 * should be safe to assume the total number of slots consumed by an array
166 * is the number of entries in the array multiplied by the number of slots
167 * consumed by a single element of the array.
168 */
169
170 if (t->is_array())
171 return t->array_size() * count_attribute_slots(t->element_type());
172
173 if (t->is_matrix())
174 return t->matrix_columns;
175
176 return 1;
177}
178
179
180/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700181 * Verify that a vertex shader executable meets all semantic requirements
182 *
183 * \param shader Vertex shader executable to be verified
184 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700185bool
Eric Anholt849e1812010-06-30 11:49:17 -0700186validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700187 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700188{
189 if (shader == NULL)
190 return true;
191
192 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700193 linker_error_printf(prog, "vertex shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700194 return false;
195 }
196
197 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700198 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700199 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700200 linker_error_printf(prog,
201 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700202 return false;
203 }
204
205 return true;
206}
207
208
Ian Romanickc93b8f12010-06-17 15:20:22 -0700209/**
210 * Verify that a fragment shader executable meets all semantic requirements
211 *
212 * \param shader Fragment shader executable to be verified
213 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700214bool
Eric Anholt849e1812010-06-30 11:49:17 -0700215validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700216 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700217{
218 if (shader == NULL)
219 return true;
220
221 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700222 linker_error_printf(prog, "fragment shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700223 return false;
224 }
225
226 find_assignment_visitor frag_color("gl_FragColor");
227 find_assignment_visitor frag_data("gl_FragData");
228
Eric Anholt16b68b12010-06-30 11:05:43 -0700229 frag_color.run(shader->ir);
230 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700231
Ian Romanick832dfa52010-06-17 15:04:20 -0700232 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700233 linker_error_printf(prog, "fragment shader writes to both "
234 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700235 return false;
236 }
237
238 return true;
239}
240
241
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700242/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700243 * Generate a string describing the mode of a variable
244 */
245static const char *
246mode_string(const ir_variable *var)
247{
248 switch (var->mode) {
249 case ir_var_auto:
250 return (var->read_only) ? "global constant" : "global variable";
251
252 case ir_var_uniform: return "uniform";
253 case ir_var_in: return "shader input";
254 case ir_var_out: return "shader output";
255 case ir_var_inout: return "shader inout";
256 default:
257 assert(!"Should not get here.");
258 return "invalid variable";
259 }
260}
261
262
263/**
264 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700265 */
266bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700267cross_validate_globals(struct gl_shader_program *prog,
268 struct gl_shader **shader_list,
269 unsigned num_shaders,
270 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700271{
272 /* Examine all of the uniforms in all of the shaders and cross validate
273 * them.
274 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700275 glsl_symbol_table variables;
276 for (unsigned i = 0; i < num_shaders; i++) {
277 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700278 ir_variable *const var = ((ir_instruction *) node)->as_variable();
279
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700280 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700281 continue;
282
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700283 if (uniforms_only && (var->mode != ir_var_uniform))
284 continue;
285
286 /* If a global with this name has already been seen, verify that the
287 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700288 * initializers, the values of the initializers must be the same.
289 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700290 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700291 if (existing != NULL) {
292 if (var->type != existing->type) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700293 linker_error_printf(prog, "%s `%s' declared as type "
Ian Romanickf36460e2010-06-23 12:07:22 -0700294 "`%s' and type `%s'\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700295 mode_string(var),
Ian Romanickf36460e2010-06-23 12:07:22 -0700296 var->name, var->type->name,
297 existing->type->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700298 return false;
299 }
300
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700301 /* FINISHME: Handle non-constant initializers.
302 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700303 if (var->constant_value != NULL) {
304 if (existing->constant_value != NULL) {
305 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700306 linker_error_printf(prog, "initializers for %s "
Ian Romanickf36460e2010-06-23 12:07:22 -0700307 "`%s' have differing values\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700308 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700309 return false;
310 }
311 } else
312 /* If the first-seen instance of a particular uniform did not
313 * have an initializer but a later instance does, copy the
314 * initializer to the version stored in the symbol table.
315 */
Ian Romanick4e6a3e02010-07-13 09:22:35 -0700316 existing->constant_value = var->constant_value->clone(NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700317 }
318 } else
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700319 variables.add_variable(var->name, var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700320 }
321 }
322
323 return true;
324}
325
326
Ian Romanick37101922010-06-18 19:02:10 -0700327/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700328 * Perform validation of uniforms used across multiple shader stages
329 */
330bool
331cross_validate_uniforms(struct gl_shader_program *prog)
332{
333 return cross_validate_globals(prog, prog->_LinkedShaders,
334 prog->_NumLinkedShaders, true);
335}
336
337
338/**
Ian Romanick37101922010-06-18 19:02:10 -0700339 * Validate that outputs from one stage match inputs of another
340 */
341bool
Eric Anholt849e1812010-06-30 11:49:17 -0700342cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700343 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700344{
345 glsl_symbol_table parameters;
346 /* FINISHME: Figure these out dynamically. */
347 const char *const producer_stage = "vertex";
348 const char *const consumer_stage = "fragment";
349
350 /* Find all shader outputs in the "producer" stage.
351 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700352 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700353 ir_variable *const var = ((ir_instruction *) node)->as_variable();
354
355 /* FINISHME: For geometry shaders, this should also look for inout
356 * FINISHME: variables.
357 */
358 if ((var == NULL) || (var->mode != ir_var_out))
359 continue;
360
361 parameters.add_variable(var->name, var);
362 }
363
364
365 /* Find all shader inputs in the "consumer" stage. Any variables that have
366 * matching outputs already in the symbol table must have the same type and
367 * qualifiers.
368 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700369 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700370 ir_variable *const input = ((ir_instruction *) node)->as_variable();
371
372 /* FINISHME: For geometry shaders, this should also look for inout
373 * FINISHME: variables.
374 */
375 if ((input == NULL) || (input->mode != ir_var_in))
376 continue;
377
378 ir_variable *const output = parameters.get_variable(input->name);
379 if (output != NULL) {
380 /* Check that the types match between stages.
381 */
382 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700383 linker_error_printf(prog,
384 "%s shader output `%s' delcared as "
385 "type `%s', but %s shader input declared "
386 "as type `%s'\n",
387 producer_stage, output->name,
388 output->type->name,
389 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700390 return false;
391 }
392
393 /* Check that all of the qualifiers match between stages.
394 */
395 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700396 linker_error_printf(prog,
397 "%s shader output `%s' %s centroid qualifier, "
398 "but %s shader input %s centroid qualifier\n",
399 producer_stage,
400 output->name,
401 (output->centroid) ? "has" : "lacks",
402 consumer_stage,
403 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700404 return false;
405 }
406
407 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700408 linker_error_printf(prog,
409 "%s shader output `%s' %s invariant qualifier, "
410 "but %s shader input %s invariant qualifier\n",
411 producer_stage,
412 output->name,
413 (output->invariant) ? "has" : "lacks",
414 consumer_stage,
415 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700416 return false;
417 }
418
419 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700420 linker_error_printf(prog,
421 "%s shader output `%s' specifies %s "
422 "interpolation qualifier, "
423 "but %s shader input specifies %s "
424 "interpolation qualifier\n",
425 producer_stage,
426 output->name,
427 output->interpolation_string(),
428 consumer_stage,
429 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700430 return false;
431 }
432 }
433 }
434
435 return true;
436}
437
438
Ian Romanick3fb87872010-07-09 14:09:34 -0700439/**
440 * Populates a shaders symbol table with all global declarations
441 */
442static void
443populate_symbol_table(gl_shader *sh)
444{
445 sh->symbols = new(sh) glsl_symbol_table;
446
447 foreach_list(node, sh->ir) {
448 ir_instruction *const inst = (ir_instruction *) node;
449 ir_variable *var;
450 ir_function *func;
451
452 if ((func = inst->as_function()) != NULL) {
453 sh->symbols->add_function(func->name, func);
454 } else if ((var = inst->as_variable()) != NULL) {
455 sh->symbols->add_variable(var->name, var);
456 }
457 }
458}
459
460
461/**
Ian Romanick31a97862010-07-12 18:48:50 -0700462 * Remap variables referenced in an instruction tree
463 *
464 * This is used when instruction trees are cloned from one shader and placed in
465 * another. These trees will contain references to \c ir_variable nodes that
466 * do not exist in the target shader. This function finds these \c ir_variable
467 * references and replaces the references with matching variables in the target
468 * shader.
469 *
470 * If there is no matching variable in the target shader, a clone of the
471 * \c ir_variable is made and added to the target shader. The new variable is
472 * added to \b both the instruction stream and the symbol table.
473 *
474 * \param inst IR tree that is to be processed.
475 * \param symbols Symbol table containing global scope symbols in the
476 * linked shader.
477 * \param instructions Instruction stream where new variable declarations
478 * should be added.
479 */
480void
481remap_variables(ir_instruction *inst, glsl_symbol_table *symbols,
482 exec_list *instructions)
483{
484 class remap_visitor : public ir_hierarchical_visitor {
485 public:
486 remap_visitor(glsl_symbol_table *symbols, exec_list *instructions)
487 {
488 this->symbols = symbols;
489 this->instructions = instructions;
490 }
491
492 virtual ir_visitor_status visit(ir_dereference_variable *ir)
493 {
494 ir_variable *const existing =
495 this->symbols->get_variable(ir->var->name);
496 if (existing != NULL)
497 ir->var = existing;
498 else {
499 ir_variable *copy = ir->var->clone(NULL);
500
501 this->symbols->add_variable(copy->name, copy);
502 this->instructions->push_head(copy);
503 }
504
505 return visit_continue;
506 }
507
508 private:
509 glsl_symbol_table *symbols;
510 exec_list *instructions;
511 };
512
513 remap_visitor v(symbols, instructions);
514
515 inst->accept(&v);
516}
517
518
519/**
520 * Move non-declarations from one instruction stream to another
521 *
522 * The intended usage pattern of this function is to pass the pointer to the
523 * head sentinal of a list (i.e., a pointer to the list cast to an \c exec_node
524 * pointer) for \c last and \c false for \c make_copies on the first
525 * call. Successive calls pass the return value of the previous call for
526 * \c last and \c true for \c make_copies.
527 *
528 * \param instructions Source instruction stream
529 * \param last Instruction after which new instructions should be
530 * inserted in the target instruction stream
531 * \param make_copies Flag selecting whether instructions in \c instructions
532 * should be copied (via \c ir_instruction::clone) into the
533 * target list or moved.
534 *
535 * \return
536 * The new "last" instruction in the target instruction stream. This pointer
537 * is suitable for use as the \c last parameter of a later call to this
538 * function.
539 */
540exec_node *
541move_non_declarations(exec_list *instructions, exec_node *last,
542 bool make_copies, gl_shader *target)
543{
Ian Romanick303c99f2010-07-19 12:34:56 -0700544 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700545 ir_instruction *inst = (ir_instruction *) node;
546
547 if (inst->as_variable() || inst->as_function())
548 continue;
549
550 assert(inst->as_assignment());
551
552 if (make_copies) {
553 inst = inst->clone(NULL);
554 remap_variables(inst, target->symbols, target->ir);
555 } else {
556 inst->remove();
557 }
558
559 last->insert_after(inst);
560 last = inst;
561 }
562
563 return last;
564}
565
566/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700567 * Get the function signature for main from a shader
568 */
569static ir_function_signature *
570get_main_function_signature(gl_shader *sh)
571{
572 ir_function *const f = sh->symbols->get_function("main");
573 if (f != NULL) {
574 exec_list void_parameters;
575
576 /* Look for the 'void main()' signature and ensure that it's defined.
577 * This keeps the linker from accidentally pick a shader that just
578 * contains a prototype for main.
579 *
580 * We don't have to check for multiple definitions of main (in multiple
581 * shaders) because that would have already been caught above.
582 */
583 ir_function_signature *sig = f->matching_signature(&void_parameters);
584 if ((sig != NULL) && sig->is_defined) {
585 return sig;
586 }
587 }
588
589 return NULL;
590}
591
592
593/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700594 * Combine a group of shaders for a single stage to generate a linked shader
595 *
596 * \note
597 * If this function is supplied a single shader, it is cloned, and the new
598 * shader is returned.
599 */
600static struct gl_shader *
601link_intrastage_shaders(struct gl_shader_program *prog,
602 struct gl_shader **shader_list,
603 unsigned num_shaders)
604{
Ian Romanick13f782c2010-06-29 18:53:38 -0700605 /* Check that global variables defined in multiple shaders are consistent.
606 */
607 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
608 return NULL;
609
610 /* Check that there is only a single definition of each function signature
611 * across all shaders.
612 */
613 for (unsigned i = 0; i < (num_shaders - 1); i++) {
614 foreach_list(node, shader_list[i]->ir) {
615 ir_function *const f = ((ir_instruction *) node)->as_function();
616
617 if (f == NULL)
618 continue;
619
620 for (unsigned j = i + 1; j < num_shaders; j++) {
621 ir_function *const other =
622 shader_list[j]->symbols->get_function(f->name);
623
624 /* If the other shader has no function (and therefore no function
625 * signatures) with the same name, skip to the next shader.
626 */
627 if (other == NULL)
628 continue;
629
630 foreach_iter (exec_list_iterator, iter, *f) {
631 ir_function_signature *sig =
632 (ir_function_signature *) iter.get();
633
634 if (!sig->is_defined || sig->is_built_in)
635 continue;
636
637 ir_function_signature *other_sig =
638 other->exact_matching_signature(& sig->parameters);
639
640 if ((other_sig != NULL) && other_sig->is_defined
641 && !other_sig->is_built_in) {
642 linker_error_printf(prog,
643 "function `%s' is multiply defined",
644 f->name);
645 return NULL;
646 }
647 }
648 }
649 }
650 }
651
652 /* Find the shader that defines main, and make a clone of it.
653 *
654 * Starting with the clone, search for undefined references. If one is
655 * found, find the shader that defines it. Clone the reference and add
656 * it to the shader. Repeat until there are no undefined references or
657 * until a reference cannot be resolved.
658 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700659 gl_shader *main = NULL;
660 for (unsigned i = 0; i < num_shaders; i++) {
661 if (get_main_function_signature(shader_list[i]) != NULL) {
662 main = shader_list[i];
663 break;
664 }
665 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700666
Ian Romanick15ce87e2010-07-09 15:28:22 -0700667 if (main == NULL) {
668 linker_error_printf(prog, "%s shader lacks `main'\n",
669 (shader_list[0]->Type == GL_VERTEX_SHADER)
670 ? "vertex" : "fragment");
671 return NULL;
672 }
673
674 gl_shader *const linked = _mesa_new_shader(NULL, 0, main->Type);
675 linked->ir = new(linked) exec_list;
676 clone_ir_list(linked->ir, main->ir);
677
678 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700679
Ian Romanick31a97862010-07-12 18:48:50 -0700680 /* The a pointer to the main function in the final linked shader (i.e., the
681 * copy of the original shader that contained the main function).
682 */
683 ir_function_signature *const main_sig = get_main_function_signature(linked);
684
685 /* Move any instructions other than variable declarations or function
686 * declarations into main.
687 */
Ian Romanick9303e352010-07-19 12:33:54 -0700688 exec_node *insertion_point =
689 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
690 linked);
691
Ian Romanick31a97862010-07-12 18:48:50 -0700692 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700693 if (shader_list[i] == main)
694 continue;
695
Ian Romanick31a97862010-07-12 18:48:50 -0700696 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700697 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700698 }
699
Ian Romanick13f782c2010-06-29 18:53:38 -0700700 /* Resolve initializers for global variables in the linked shader.
701 */
Ian Romanick3fb87872010-07-09 14:09:34 -0700702
Ian Romanick3fb87872010-07-09 14:09:34 -0700703 return linked;
704}
705
706
Ian Romanick019a59b2010-06-21 16:10:42 -0700707struct uniform_node {
708 exec_node link;
709 struct gl_uniform *u;
710 unsigned slots;
711};
712
Ian Romanickabee16e2010-06-21 16:16:05 -0700713void
Eric Anholt849e1812010-06-30 11:49:17 -0700714assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700715{
716 /* */
717 exec_list uniforms;
718 unsigned total_uniforms = 0;
719 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
720 hash_table_string_compare);
721
Ian Romanickabee16e2010-06-21 16:16:05 -0700722 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700723 unsigned next_position = 0;
724
Eric Anholt16b68b12010-06-30 11:05:43 -0700725 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700726 ir_variable *const var = ((ir_instruction *) node)->as_variable();
727
728 if ((var == NULL) || (var->mode != ir_var_uniform))
729 continue;
730
731 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
732 assert(vec4_slots != 0);
733
734 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
735 if (n == NULL) {
736 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
737 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
738 n->slots = vec4_slots;
739
740 n->u[0].Name = strdup(var->name);
741 for (unsigned j = 1; j < vec4_slots; j++)
742 n->u[j].Name = n->u[0].Name;
743
744 hash_table_insert(ht, n, n->u[0].Name);
745 uniforms.push_tail(& n->link);
746 total_uniforms += vec4_slots;
747 }
748
749 if (var->constant_value != NULL)
750 for (unsigned j = 0; j < vec4_slots; j++)
751 n->u[j].Initialized = true;
752
753 var->location = next_position;
754
755 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700756 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700757 case GL_VERTEX_SHADER:
758 n->u[j].VertPos = next_position;
759 break;
760 case GL_FRAGMENT_SHADER:
761 n->u[j].FragPos = next_position;
762 break;
763 case GL_GEOMETRY_SHADER:
764 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700765 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700766 break;
767 }
768
769 next_position++;
770 }
771 }
772 }
773
774 gl_uniform_list *ul = (gl_uniform_list *)
775 calloc(1, sizeof(gl_uniform_list));
776
777 ul->Size = total_uniforms;
778 ul->NumUniforms = total_uniforms;
779 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
780
781 unsigned idx = 0;
782 uniform_node *next;
783 for (uniform_node *node = (uniform_node *) uniforms.head
784 ; node->link.next != NULL
785 ; node = next) {
786 next = (uniform_node *) node->link.next;
787
788 node->link.remove();
789 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
790 idx += node->slots;
791
792 free(node->u);
793 free(node);
794 }
795
796 hash_table_dtor(ht);
797
Ian Romanickabee16e2010-06-21 16:16:05 -0700798 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700799}
800
801
Ian Romanick69846702010-06-22 17:29:19 -0700802/**
803 * Find a contiguous set of available bits in a bitmask
804 *
805 * \param used_mask Bits representing used (1) and unused (0) locations
806 * \param needed_count Number of contiguous bits needed.
807 *
808 * \return
809 * Base location of the available bits on success or -1 on failure.
810 */
811int
812find_available_slots(unsigned used_mask, unsigned needed_count)
813{
814 unsigned needed_mask = (1 << needed_count) - 1;
815 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
816
817 /* The comparison to 32 is redundant, but without it GCC emits "warning:
818 * cannot optimize possibly infinite loops" for the loop below.
819 */
820 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
821 return -1;
822
823 for (int i = 0; i <= max_bit_to_test; i++) {
824 if ((needed_mask & ~used_mask) == needed_mask)
825 return i;
826
827 needed_mask <<= 1;
828 }
829
830 return -1;
831}
832
833
834bool
Eric Anholt849e1812010-06-30 11:49:17 -0700835assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700836{
Ian Romanick9342d262010-06-22 17:41:37 -0700837 /* Mark invalid attribute locations as being used.
838 */
839 unsigned used_locations = (max_attribute_index >= 32)
840 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700841
Eric Anholt16b68b12010-06-30 11:05:43 -0700842 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700843 assert(sh->Type == GL_VERTEX_SHADER);
844
Ian Romanick69846702010-06-22 17:29:19 -0700845 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700846 *
847 * 1. Invalidate the location assignments for all vertex shader inputs.
848 *
849 * 2. Assign locations for inputs that have user-defined (via
850 * glBindVertexAttribLocation) locatoins.
851 *
Ian Romanick69846702010-06-22 17:29:19 -0700852 * 3. Sort the attributes without assigned locations by number of slots
853 * required in decreasing order. Fragmentation caused by attribute
854 * locations assigned by the application may prevent large attributes
855 * from having enough contiguous space.
856 *
857 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700858 */
859
860 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
861
Ian Romanick553dcdc2010-06-23 12:14:02 -0700862 if (prog->Attributes != NULL) {
863 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700864 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700865 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700866
Ian Romanick69846702010-06-22 17:29:19 -0700867 /* Note: attributes that occupy multiple slots, such as arrays or
868 * matrices, may appear in the attrib array multiple times.
869 */
870 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700871 continue;
872
Ian Romanick69846702010-06-22 17:29:19 -0700873 /* From page 61 of the OpenGL 4.0 spec:
874 *
875 * "LinkProgram will fail if the attribute bindings assigned by
876 * BindAttribLocation do not leave not enough space to assign a
877 * location for an active matrix attribute or an active attribute
878 * array, both of which require multiple contiguous generic
879 * attributes."
880 *
881 * Previous versions of the spec contain similar language but omit the
882 * bit about attribute arrays.
883 *
884 * Page 61 of the OpenGL 4.0 spec also says:
885 *
886 * "It is possible for an application to bind more than one
887 * attribute name to the same location. This is referred to as
888 * aliasing. This will only work if only one of the aliased
889 * attributes is active in the executable program, or if no path
890 * through the shader consumes more than one attribute of a set
891 * of attributes aliased to the same location. A link error can
892 * occur if the linker determines that every path through the
893 * shader consumes multiple aliased attributes, but
894 * implementations are not required to generate an error in this
895 * case."
896 *
897 * These two paragraphs are either somewhat contradictory, or I don't
898 * fully understand one or both of them.
899 */
900 /* FINISHME: The code as currently written does not support attribute
901 * FINISHME: location aliasing (see comment above).
902 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700903 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -0700904 const unsigned slots = count_attribute_slots(var->type);
905
906 /* Mask representing the contiguous slots that will be used by this
907 * attribute.
908 */
909 const unsigned use_mask = (1 << slots) - 1;
910
911 /* Generate a link error if the set of bits requested for this
912 * attribute overlaps any previously allocated bits.
913 */
914 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700915 linker_error_printf(prog,
916 "insufficient contiguous attribute locations "
917 "available for vertex shader input `%s'",
918 var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700919 return false;
920 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700921
922 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -0700923 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700924 }
925 }
926
Ian Romanick69846702010-06-22 17:29:19 -0700927 /* Temporary storage for the set of attributes that need locations assigned.
928 */
929 struct temp_attr {
930 unsigned slots;
931 ir_variable *var;
932
933 /* Used below in the call to qsort. */
934 static int compare(const void *a, const void *b)
935 {
936 const temp_attr *const l = (const temp_attr *) a;
937 const temp_attr *const r = (const temp_attr *) b;
938
939 /* Reversed because we want a descending order sort below. */
940 return r->slots - l->slots;
941 }
942 } to_assign[16];
943
944 unsigned num_attr = 0;
945
Eric Anholt16b68b12010-06-30 11:05:43 -0700946 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700947 ir_variable *const var = ((ir_instruction *) node)->as_variable();
948
949 if ((var == NULL) || (var->mode != ir_var_in))
950 continue;
951
952 /* The location was explicitly assigned, nothing to do here.
953 */
954 if (var->location != -1)
955 continue;
956
Ian Romanick69846702010-06-22 17:29:19 -0700957 to_assign[num_attr].slots = count_attribute_slots(var->type);
958 to_assign[num_attr].var = var;
959 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700960 }
Ian Romanick69846702010-06-22 17:29:19 -0700961
962 /* If all of the attributes were assigned locations by the application (or
963 * are built-in attributes with fixed locations), return early. This should
964 * be the common case.
965 */
966 if (num_attr == 0)
967 return true;
968
969 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
970
Ian Romanick982e3792010-06-29 18:58:20 -0700971 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
972 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
973 * to prevent it from being automatically allocated below.
974 */
Ian Romanick35c89202010-07-07 16:28:39 -0700975 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -0700976
Ian Romanick69846702010-06-22 17:29:19 -0700977 for (unsigned i = 0; i < num_attr; i++) {
978 /* Mask representing the contiguous slots that will be used by this
979 * attribute.
980 */
981 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
982
983 int location = find_available_slots(used_locations, to_assign[i].slots);
984
985 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700986 linker_error_printf(prog,
987 "insufficient contiguous attribute locations "
988 "available for vertex shader input `%s'",
989 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700990 return false;
991 }
992
993 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
994 used_locations |= (use_mask << location);
995 }
996
997 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700998}
999
1000
1001void
Eric Anholt16b68b12010-06-30 11:05:43 -07001002assign_varying_locations(gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001003{
1004 /* FINISHME: Set dynamically when geometry shader support is added. */
1005 unsigned output_index = VERT_RESULT_VAR0;
1006 unsigned input_index = FRAG_ATTRIB_VAR0;
1007
1008 /* Operate in a total of three passes.
1009 *
1010 * 1. Assign locations for any matching inputs and outputs.
1011 *
1012 * 2. Mark output variables in the producer that do not have locations as
1013 * not being outputs. This lets the optimizer eliminate them.
1014 *
1015 * 3. Mark input variables in the consumer that do not have locations as
1016 * not being inputs. This lets the optimizer eliminate them.
1017 */
1018
1019 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1020 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1021
Eric Anholt16b68b12010-06-30 11:05:43 -07001022 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001023 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1024
1025 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1026 || (output_var->location != -1))
1027 continue;
1028
1029 ir_variable *const input_var =
1030 consumer->symbols->get_variable(output_var->name);
1031
1032 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1033 continue;
1034
1035 assert(input_var->location == -1);
1036
1037 /* FINISHME: Location assignment will need some changes when arrays,
1038 * FINISHME: matrices, and structures are allowed as shader inputs /
1039 * FINISHME: outputs.
1040 */
1041 output_var->location = output_index;
1042 input_var->location = input_index;
1043
1044 output_index++;
1045 input_index++;
1046 }
1047
Eric Anholt16b68b12010-06-30 11:05:43 -07001048 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001049 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1050
1051 if ((var == NULL) || (var->mode != ir_var_out))
1052 continue;
1053
1054 /* An 'out' variable is only really a shader output if its value is read
1055 * by the following stage.
1056 */
Eric Anholtc10a6852010-07-13 11:07:16 -07001057 if (var->location == -1) {
1058 var->shader_out = false;
1059 var->mode = ir_var_auto;
1060 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001061 }
1062
Eric Anholt16b68b12010-06-30 11:05:43 -07001063 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001064 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1065
1066 if ((var == NULL) || (var->mode != ir_var_in))
1067 continue;
1068
1069 /* An 'in' variable is only really a shader input if its value is written
1070 * by the previous stage.
1071 */
1072 var->shader_in = (var->location != -1);
1073 }
1074}
1075
1076
1077void
Eric Anholt849e1812010-06-30 11:49:17 -07001078link_shaders(struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001079{
1080 prog->LinkStatus = false;
1081 prog->Validated = false;
1082 prog->_Used = false;
1083
Ian Romanickf36460e2010-06-23 12:07:22 -07001084 if (prog->InfoLog != NULL)
1085 talloc_free(prog->InfoLog);
1086
1087 prog->InfoLog = talloc_strdup(NULL, "");
1088
Ian Romanick832dfa52010-06-17 15:04:20 -07001089 /* Separate the shaders into groups based on their type.
1090 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001091 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001092 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001093 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001094 unsigned num_frag_shaders = 0;
1095
Eric Anholt16b68b12010-06-30 11:05:43 -07001096 vert_shader_list = (struct gl_shader **)
1097 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001098 frag_shader_list = &vert_shader_list[prog->NumShaders];
1099
1100 for (unsigned i = 0; i < prog->NumShaders; i++) {
1101 switch (prog->Shaders[i]->Type) {
1102 case GL_VERTEX_SHADER:
1103 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1104 num_vert_shaders++;
1105 break;
1106 case GL_FRAGMENT_SHADER:
1107 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1108 num_frag_shaders++;
1109 break;
1110 case GL_GEOMETRY_SHADER:
1111 /* FINISHME: Support geometry shaders. */
1112 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1113 break;
1114 }
1115 }
1116
1117 /* FINISHME: Implement intra-stage linking. */
Ian Romanickabee16e2010-06-21 16:16:05 -07001118 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001119 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001120 gl_shader *const sh =
1121 link_intrastage_shaders(prog, vert_shader_list, num_vert_shaders);
1122
1123 if (sh == NULL)
1124 goto done;
1125
1126 if (!validate_vertex_shader_executable(prog, sh))
1127 goto done;
1128
1129 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001130 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001131 }
1132
1133 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001134 gl_shader *const sh =
1135 link_intrastage_shaders(prog, frag_shader_list, num_frag_shaders);
1136
1137 if (sh == NULL)
1138 goto done;
1139
1140 if (!validate_fragment_shader_executable(prog, sh))
1141 goto done;
1142
1143 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001144 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001145 }
1146
Ian Romanick3ed850e2010-06-23 12:18:21 -07001147 /* Here begins the inter-stage linking phase. Some initial validation is
1148 * performed, then locations are assigned for uniforms, attributes, and
1149 * varyings.
1150 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001151 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -07001152 /* Validate the inputs of each stage with the output of the preceeding
1153 * stage.
1154 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001155 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -07001156 if (!cross_validate_outputs_to_inputs(prog,
1157 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -07001158 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001159 goto done;
1160 }
1161
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001162 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001163 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001164
Ian Romanick13e10e42010-06-21 12:03:24 -07001165 /* FINISHME: Perform whole-program optimization here. */
1166
Ian Romanickabee16e2010-06-21 16:16:05 -07001167 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001168
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001169 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER)
Ian Romanick9342d262010-06-22 17:41:37 -07001170 /* FINISHME: The value of the max_attribute_index parameter is
1171 * FINISHME: implementation dependent based on the value of
1172 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1173 * FINISHME: at least 16, so hardcode 16 for now.
1174 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001175 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -07001176 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -07001177
Ian Romanick0e59b262010-06-23 11:23:01 -07001178 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
1179 assign_varying_locations(prog->_LinkedShaders[i - 1],
1180 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -07001181
1182 /* FINISHME: Assign fragment shader output locations. */
1183
Ian Romanick832dfa52010-06-17 15:04:20 -07001184done:
1185 free(vert_shader_list);
1186}