blob: 06aa24e66f5b742aaf08226ea2022faf2fb30bf2 [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 Romanick8fe8a812010-07-13 17:36:13 -070080#include "linker.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070081
82/**
83 * Visitor that determines whether or not a variable is ever written.
84 */
85class find_assignment_visitor : public ir_hierarchical_visitor {
86public:
87 find_assignment_visitor(const char *name)
88 : name(name), found(false)
89 {
90 /* empty */
91 }
92
93 virtual ir_visitor_status visit_enter(ir_assignment *ir)
94 {
95 ir_variable *const var = ir->lhs->variable_referenced();
96
97 if (strcmp(name, var->name) == 0) {
98 found = true;
99 return visit_stop;
100 }
101
102 return visit_continue_with_parent;
103 }
104
105 bool variable_found()
106 {
107 return found;
108 }
109
110private:
111 const char *name; /**< Find writes to a variable with this name. */
112 bool found; /**< Was a write to the variable found? */
113};
114
Ian Romanickc93b8f12010-06-17 15:20:22 -0700115
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700116void
Eric Anholt849e1812010-06-30 11:49:17 -0700117linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700118{
119 va_list ap;
120
121 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
122 va_start(ap, fmt);
123 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
124 va_end(ap);
125}
126
127
128void
Eric Anholt16b68b12010-06-30 11:05:43 -0700129invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700130 int generic_base)
131{
Eric Anholt16b68b12010-06-30 11:05:43 -0700132 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700133 ir_variable *const var = ((ir_instruction *) node)->as_variable();
134
135 if ((var == NULL) || (var->mode != (unsigned) mode))
136 continue;
137
138 /* Only assign locations for generic attributes / varyings / etc.
139 */
140 if (var->location >= generic_base)
141 var->location = -1;
142 }
143}
144
145
Ian Romanickc93b8f12010-06-17 15:20:22 -0700146/**
Ian Romanick69846702010-06-22 17:29:19 -0700147 * Determine the number of attribute slots required for a particular type
148 *
149 * This code is here because it implements the language rules of a specific
150 * GLSL version. Since it's a property of the language and not a property of
151 * types in general, it doesn't really belong in glsl_type.
152 */
153unsigned
154count_attribute_slots(const glsl_type *t)
155{
156 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
157 *
158 * "A scalar input counts the same amount against this limit as a vec4,
159 * so applications may want to consider packing groups of four
160 * unrelated float inputs together into a vector to better utilize the
161 * capabilities of the underlying hardware. A matrix input will use up
162 * multiple locations. The number of locations used will equal the
163 * number of columns in the matrix."
164 *
165 * The spec does not explicitly say how arrays are counted. However, it
166 * should be safe to assume the total number of slots consumed by an array
167 * is the number of entries in the array multiplied by the number of slots
168 * consumed by a single element of the array.
169 */
170
171 if (t->is_array())
172 return t->array_size() * count_attribute_slots(t->element_type());
173
174 if (t->is_matrix())
175 return t->matrix_columns;
176
177 return 1;
178}
179
180
181/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700182 * Verify that a vertex shader executable meets all semantic requirements
183 *
184 * \param shader Vertex shader executable to be verified
185 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700186bool
Eric Anholt849e1812010-06-30 11:49:17 -0700187validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700188 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700189{
190 if (shader == NULL)
191 return true;
192
193 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700194 linker_error_printf(prog, "vertex shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700195 return false;
196 }
197
198 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700199 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700200 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700201 linker_error_printf(prog,
202 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700203 return false;
204 }
205
206 return true;
207}
208
209
Ian Romanickc93b8f12010-06-17 15:20:22 -0700210/**
211 * Verify that a fragment shader executable meets all semantic requirements
212 *
213 * \param shader Fragment shader executable to be verified
214 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700215bool
Eric Anholt849e1812010-06-30 11:49:17 -0700216validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700217 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700218{
219 if (shader == NULL)
220 return true;
221
222 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700223 linker_error_printf(prog, "fragment shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700224 return false;
225 }
226
227 find_assignment_visitor frag_color("gl_FragColor");
228 find_assignment_visitor frag_data("gl_FragData");
229
Eric Anholt16b68b12010-06-30 11:05:43 -0700230 frag_color.run(shader->ir);
231 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700232
Ian Romanick832dfa52010-06-17 15:04:20 -0700233 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700234 linker_error_printf(prog, "fragment shader writes to both "
235 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700236 return false;
237 }
238
239 return true;
240}
241
242
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700243/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700244 * Generate a string describing the mode of a variable
245 */
246static const char *
247mode_string(const ir_variable *var)
248{
249 switch (var->mode) {
250 case ir_var_auto:
251 return (var->read_only) ? "global constant" : "global variable";
252
253 case ir_var_uniform: return "uniform";
254 case ir_var_in: return "shader input";
255 case ir_var_out: return "shader output";
256 case ir_var_inout: return "shader inout";
257 default:
258 assert(!"Should not get here.");
259 return "invalid variable";
260 }
261}
262
263
264/**
265 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700266 */
267bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700268cross_validate_globals(struct gl_shader_program *prog,
269 struct gl_shader **shader_list,
270 unsigned num_shaders,
271 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700272{
273 /* Examine all of the uniforms in all of the shaders and cross validate
274 * them.
275 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700276 glsl_symbol_table variables;
277 for (unsigned i = 0; i < num_shaders; i++) {
278 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700279 ir_variable *const var = ((ir_instruction *) node)->as_variable();
280
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700281 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700282 continue;
283
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700284 if (uniforms_only && (var->mode != ir_var_uniform))
285 continue;
286
287 /* If a global with this name has already been seen, verify that the
288 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700289 * initializers, the values of the initializers must be the same.
290 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700291 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700292 if (existing != NULL) {
293 if (var->type != existing->type) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700294 linker_error_printf(prog, "%s `%s' declared as type "
Ian Romanickf36460e2010-06-23 12:07:22 -0700295 "`%s' and type `%s'\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700296 mode_string(var),
Ian Romanickf36460e2010-06-23 12:07:22 -0700297 var->name, var->type->name,
298 existing->type->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700299 return false;
300 }
301
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700302 /* FINISHME: Handle non-constant initializers.
303 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700304 if (var->constant_value != NULL) {
305 if (existing->constant_value != NULL) {
306 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700307 linker_error_printf(prog, "initializers for %s "
Ian Romanickf36460e2010-06-23 12:07:22 -0700308 "`%s' have differing values\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700309 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700310 return false;
311 }
312 } else
313 /* If the first-seen instance of a particular uniform did not
314 * have an initializer but a later instance does, copy the
315 * initializer to the version stored in the symbol table.
316 */
Ian Romanickde415b72010-07-14 13:22:12 -0700317 /* FINISHME: This is wrong. The constant_value field should
318 * FINISHME: not be modified! Imagine a case where a shader
319 * FINISHME: without an initializer is linked in two different
320 * FINISHME: programs with shaders that have differing
321 * FINISHME: initializers. Linking with the first will
322 * FINISHME: modify the shader, and linking with the second
323 * FINISHME: will fail.
324 */
Ian Romanick4e6a3e02010-07-13 09:22:35 -0700325 existing->constant_value = var->constant_value->clone(NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700326 }
327 } else
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700328 variables.add_variable(var->name, var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700329 }
330 }
331
332 return true;
333}
334
335
Ian Romanick37101922010-06-18 19:02:10 -0700336/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700337 * Perform validation of uniforms used across multiple shader stages
338 */
339bool
340cross_validate_uniforms(struct gl_shader_program *prog)
341{
342 return cross_validate_globals(prog, prog->_LinkedShaders,
343 prog->_NumLinkedShaders, true);
344}
345
346
347/**
Ian Romanick37101922010-06-18 19:02:10 -0700348 * Validate that outputs from one stage match inputs of another
349 */
350bool
Eric Anholt849e1812010-06-30 11:49:17 -0700351cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700352 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700353{
354 glsl_symbol_table parameters;
355 /* FINISHME: Figure these out dynamically. */
356 const char *const producer_stage = "vertex";
357 const char *const consumer_stage = "fragment";
358
359 /* Find all shader outputs in the "producer" stage.
360 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700361 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700362 ir_variable *const var = ((ir_instruction *) node)->as_variable();
363
364 /* FINISHME: For geometry shaders, this should also look for inout
365 * FINISHME: variables.
366 */
367 if ((var == NULL) || (var->mode != ir_var_out))
368 continue;
369
370 parameters.add_variable(var->name, var);
371 }
372
373
374 /* Find all shader inputs in the "consumer" stage. Any variables that have
375 * matching outputs already in the symbol table must have the same type and
376 * qualifiers.
377 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700378 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700379 ir_variable *const input = ((ir_instruction *) node)->as_variable();
380
381 /* FINISHME: For geometry shaders, this should also look for inout
382 * FINISHME: variables.
383 */
384 if ((input == NULL) || (input->mode != ir_var_in))
385 continue;
386
387 ir_variable *const output = parameters.get_variable(input->name);
388 if (output != NULL) {
389 /* Check that the types match between stages.
390 */
391 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700392 linker_error_printf(prog,
393 "%s shader output `%s' delcared as "
394 "type `%s', but %s shader input declared "
395 "as type `%s'\n",
396 producer_stage, output->name,
397 output->type->name,
398 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700399 return false;
400 }
401
402 /* Check that all of the qualifiers match between stages.
403 */
404 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700405 linker_error_printf(prog,
406 "%s shader output `%s' %s centroid qualifier, "
407 "but %s shader input %s centroid qualifier\n",
408 producer_stage,
409 output->name,
410 (output->centroid) ? "has" : "lacks",
411 consumer_stage,
412 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700413 return false;
414 }
415
416 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700417 linker_error_printf(prog,
418 "%s shader output `%s' %s invariant qualifier, "
419 "but %s shader input %s invariant qualifier\n",
420 producer_stage,
421 output->name,
422 (output->invariant) ? "has" : "lacks",
423 consumer_stage,
424 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700425 return false;
426 }
427
428 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700429 linker_error_printf(prog,
430 "%s shader output `%s' specifies %s "
431 "interpolation qualifier, "
432 "but %s shader input specifies %s "
433 "interpolation qualifier\n",
434 producer_stage,
435 output->name,
436 output->interpolation_string(),
437 consumer_stage,
438 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700439 return false;
440 }
441 }
442 }
443
444 return true;
445}
446
447
Ian Romanick3fb87872010-07-09 14:09:34 -0700448/**
449 * Populates a shaders symbol table with all global declarations
450 */
451static void
452populate_symbol_table(gl_shader *sh)
453{
454 sh->symbols = new(sh) glsl_symbol_table;
455
456 foreach_list(node, sh->ir) {
457 ir_instruction *const inst = (ir_instruction *) node;
458 ir_variable *var;
459 ir_function *func;
460
461 if ((func = inst->as_function()) != NULL) {
462 sh->symbols->add_function(func->name, func);
463 } else if ((var = inst->as_variable()) != NULL) {
464 sh->symbols->add_variable(var->name, var);
465 }
466 }
467}
468
469
470/**
Ian Romanick31a97862010-07-12 18:48:50 -0700471 * Remap variables referenced in an instruction tree
472 *
473 * This is used when instruction trees are cloned from one shader and placed in
474 * another. These trees will contain references to \c ir_variable nodes that
475 * do not exist in the target shader. This function finds these \c ir_variable
476 * references and replaces the references with matching variables in the target
477 * shader.
478 *
479 * If there is no matching variable in the target shader, a clone of the
480 * \c ir_variable is made and added to the target shader. The new variable is
481 * added to \b both the instruction stream and the symbol table.
482 *
483 * \param inst IR tree that is to be processed.
484 * \param symbols Symbol table containing global scope symbols in the
485 * linked shader.
486 * \param instructions Instruction stream where new variable declarations
487 * should be added.
488 */
489void
490remap_variables(ir_instruction *inst, glsl_symbol_table *symbols,
491 exec_list *instructions)
492{
493 class remap_visitor : public ir_hierarchical_visitor {
494 public:
495 remap_visitor(glsl_symbol_table *symbols, exec_list *instructions)
496 {
497 this->symbols = symbols;
498 this->instructions = instructions;
499 }
500
501 virtual ir_visitor_status visit(ir_dereference_variable *ir)
502 {
503 ir_variable *const existing =
504 this->symbols->get_variable(ir->var->name);
505 if (existing != NULL)
506 ir->var = existing;
507 else {
508 ir_variable *copy = ir->var->clone(NULL);
509
510 this->symbols->add_variable(copy->name, copy);
511 this->instructions->push_head(copy);
512 }
513
514 return visit_continue;
515 }
516
517 private:
518 glsl_symbol_table *symbols;
519 exec_list *instructions;
520 };
521
522 remap_visitor v(symbols, instructions);
523
524 inst->accept(&v);
525}
526
527
528/**
529 * Move non-declarations from one instruction stream to another
530 *
531 * The intended usage pattern of this function is to pass the pointer to the
532 * head sentinal of a list (i.e., a pointer to the list cast to an \c exec_node
533 * pointer) for \c last and \c false for \c make_copies on the first
534 * call. Successive calls pass the return value of the previous call for
535 * \c last and \c true for \c make_copies.
536 *
537 * \param instructions Source instruction stream
538 * \param last Instruction after which new instructions should be
539 * inserted in the target instruction stream
540 * \param make_copies Flag selecting whether instructions in \c instructions
541 * should be copied (via \c ir_instruction::clone) into the
542 * target list or moved.
543 *
544 * \return
545 * The new "last" instruction in the target instruction stream. This pointer
546 * is suitable for use as the \c last parameter of a later call to this
547 * function.
548 */
549exec_node *
550move_non_declarations(exec_list *instructions, exec_node *last,
551 bool make_copies, gl_shader *target)
552{
Ian Romanick303c99f2010-07-19 12:34:56 -0700553 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700554 ir_instruction *inst = (ir_instruction *) node;
555
556 if (inst->as_variable() || inst->as_function())
557 continue;
558
559 assert(inst->as_assignment());
560
561 if (make_copies) {
562 inst = inst->clone(NULL);
563 remap_variables(inst, target->symbols, target->ir);
564 } else {
565 inst->remove();
566 }
567
568 last->insert_after(inst);
569 last = inst;
570 }
571
572 return last;
573}
574
575/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700576 * Get the function signature for main from a shader
577 */
578static ir_function_signature *
579get_main_function_signature(gl_shader *sh)
580{
581 ir_function *const f = sh->symbols->get_function("main");
582 if (f != NULL) {
583 exec_list void_parameters;
584
585 /* Look for the 'void main()' signature and ensure that it's defined.
586 * This keeps the linker from accidentally pick a shader that just
587 * contains a prototype for main.
588 *
589 * We don't have to check for multiple definitions of main (in multiple
590 * shaders) because that would have already been caught above.
591 */
592 ir_function_signature *sig = f->matching_signature(&void_parameters);
593 if ((sig != NULL) && sig->is_defined) {
594 return sig;
595 }
596 }
597
598 return NULL;
599}
600
601
602/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700603 * Combine a group of shaders for a single stage to generate a linked shader
604 *
605 * \note
606 * If this function is supplied a single shader, it is cloned, and the new
607 * shader is returned.
608 */
609static struct gl_shader *
610link_intrastage_shaders(struct gl_shader_program *prog,
611 struct gl_shader **shader_list,
612 unsigned num_shaders)
613{
Ian Romanick13f782c2010-06-29 18:53:38 -0700614 /* Check that global variables defined in multiple shaders are consistent.
615 */
616 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
617 return NULL;
618
619 /* Check that there is only a single definition of each function signature
620 * across all shaders.
621 */
622 for (unsigned i = 0; i < (num_shaders - 1); i++) {
623 foreach_list(node, shader_list[i]->ir) {
624 ir_function *const f = ((ir_instruction *) node)->as_function();
625
626 if (f == NULL)
627 continue;
628
629 for (unsigned j = i + 1; j < num_shaders; j++) {
630 ir_function *const other =
631 shader_list[j]->symbols->get_function(f->name);
632
633 /* If the other shader has no function (and therefore no function
634 * signatures) with the same name, skip to the next shader.
635 */
636 if (other == NULL)
637 continue;
638
639 foreach_iter (exec_list_iterator, iter, *f) {
640 ir_function_signature *sig =
641 (ir_function_signature *) iter.get();
642
643 if (!sig->is_defined || sig->is_built_in)
644 continue;
645
646 ir_function_signature *other_sig =
647 other->exact_matching_signature(& sig->parameters);
648
649 if ((other_sig != NULL) && other_sig->is_defined
650 && !other_sig->is_built_in) {
651 linker_error_printf(prog,
652 "function `%s' is multiply defined",
653 f->name);
654 return NULL;
655 }
656 }
657 }
658 }
659 }
660
661 /* Find the shader that defines main, and make a clone of it.
662 *
663 * Starting with the clone, search for undefined references. If one is
664 * found, find the shader that defines it. Clone the reference and add
665 * it to the shader. Repeat until there are no undefined references or
666 * until a reference cannot be resolved.
667 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700668 gl_shader *main = NULL;
669 for (unsigned i = 0; i < num_shaders; i++) {
670 if (get_main_function_signature(shader_list[i]) != NULL) {
671 main = shader_list[i];
672 break;
673 }
674 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700675
Ian Romanick15ce87e2010-07-09 15:28:22 -0700676 if (main == NULL) {
677 linker_error_printf(prog, "%s shader lacks `main'\n",
678 (shader_list[0]->Type == GL_VERTEX_SHADER)
679 ? "vertex" : "fragment");
680 return NULL;
681 }
682
683 gl_shader *const linked = _mesa_new_shader(NULL, 0, main->Type);
684 linked->ir = new(linked) exec_list;
685 clone_ir_list(linked->ir, main->ir);
686
687 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700688
Ian Romanick31a97862010-07-12 18:48:50 -0700689 /* The a pointer to the main function in the final linked shader (i.e., the
690 * copy of the original shader that contained the main function).
691 */
692 ir_function_signature *const main_sig = get_main_function_signature(linked);
693
694 /* Move any instructions other than variable declarations or function
695 * declarations into main.
696 */
Ian Romanick9303e352010-07-19 12:33:54 -0700697 exec_node *insertion_point =
698 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
699 linked);
700
Ian Romanick31a97862010-07-12 18:48:50 -0700701 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700702 if (shader_list[i] == main)
703 continue;
704
Ian Romanick31a97862010-07-12 18:48:50 -0700705 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700706 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700707 }
708
Ian Romanick13f782c2010-06-29 18:53:38 -0700709 /* Resolve initializers for global variables in the linked shader.
710 */
Ian Romanick8fe8a812010-07-13 17:36:13 -0700711 link_function_calls(prog, linked, shader_list, num_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -0700712
Ian Romanick3fb87872010-07-09 14:09:34 -0700713 return linked;
714}
715
716
Ian Romanick019a59b2010-06-21 16:10:42 -0700717struct uniform_node {
718 exec_node link;
719 struct gl_uniform *u;
720 unsigned slots;
721};
722
Ian Romanickabee16e2010-06-21 16:16:05 -0700723void
Eric Anholt849e1812010-06-30 11:49:17 -0700724assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700725{
726 /* */
727 exec_list uniforms;
728 unsigned total_uniforms = 0;
729 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
730 hash_table_string_compare);
731
Ian Romanickabee16e2010-06-21 16:16:05 -0700732 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700733 unsigned next_position = 0;
734
Eric Anholt16b68b12010-06-30 11:05:43 -0700735 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700736 ir_variable *const var = ((ir_instruction *) node)->as_variable();
737
738 if ((var == NULL) || (var->mode != ir_var_uniform))
739 continue;
740
741 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
742 assert(vec4_slots != 0);
743
744 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
745 if (n == NULL) {
746 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
747 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
748 n->slots = vec4_slots;
749
750 n->u[0].Name = strdup(var->name);
751 for (unsigned j = 1; j < vec4_slots; j++)
752 n->u[j].Name = n->u[0].Name;
753
754 hash_table_insert(ht, n, n->u[0].Name);
755 uniforms.push_tail(& n->link);
756 total_uniforms += vec4_slots;
757 }
758
759 if (var->constant_value != NULL)
760 for (unsigned j = 0; j < vec4_slots; j++)
761 n->u[j].Initialized = true;
762
763 var->location = next_position;
764
765 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700766 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700767 case GL_VERTEX_SHADER:
768 n->u[j].VertPos = next_position;
769 break;
770 case GL_FRAGMENT_SHADER:
771 n->u[j].FragPos = next_position;
772 break;
773 case GL_GEOMETRY_SHADER:
774 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700775 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700776 break;
777 }
778
779 next_position++;
780 }
781 }
782 }
783
784 gl_uniform_list *ul = (gl_uniform_list *)
785 calloc(1, sizeof(gl_uniform_list));
786
787 ul->Size = total_uniforms;
788 ul->NumUniforms = total_uniforms;
789 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
790
791 unsigned idx = 0;
792 uniform_node *next;
793 for (uniform_node *node = (uniform_node *) uniforms.head
794 ; node->link.next != NULL
795 ; node = next) {
796 next = (uniform_node *) node->link.next;
797
798 node->link.remove();
799 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
800 idx += node->slots;
801
802 free(node->u);
803 free(node);
804 }
805
806 hash_table_dtor(ht);
807
Ian Romanickabee16e2010-06-21 16:16:05 -0700808 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700809}
810
811
Ian Romanick69846702010-06-22 17:29:19 -0700812/**
813 * Find a contiguous set of available bits in a bitmask
814 *
815 * \param used_mask Bits representing used (1) and unused (0) locations
816 * \param needed_count Number of contiguous bits needed.
817 *
818 * \return
819 * Base location of the available bits on success or -1 on failure.
820 */
821int
822find_available_slots(unsigned used_mask, unsigned needed_count)
823{
824 unsigned needed_mask = (1 << needed_count) - 1;
825 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
826
827 /* The comparison to 32 is redundant, but without it GCC emits "warning:
828 * cannot optimize possibly infinite loops" for the loop below.
829 */
830 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
831 return -1;
832
833 for (int i = 0; i <= max_bit_to_test; i++) {
834 if ((needed_mask & ~used_mask) == needed_mask)
835 return i;
836
837 needed_mask <<= 1;
838 }
839
840 return -1;
841}
842
843
844bool
Eric Anholt849e1812010-06-30 11:49:17 -0700845assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700846{
Ian Romanick9342d262010-06-22 17:41:37 -0700847 /* Mark invalid attribute locations as being used.
848 */
849 unsigned used_locations = (max_attribute_index >= 32)
850 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700851
Eric Anholt16b68b12010-06-30 11:05:43 -0700852 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700853 assert(sh->Type == GL_VERTEX_SHADER);
854
Ian Romanick69846702010-06-22 17:29:19 -0700855 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700856 *
857 * 1. Invalidate the location assignments for all vertex shader inputs.
858 *
859 * 2. Assign locations for inputs that have user-defined (via
860 * glBindVertexAttribLocation) locatoins.
861 *
Ian Romanick69846702010-06-22 17:29:19 -0700862 * 3. Sort the attributes without assigned locations by number of slots
863 * required in decreasing order. Fragmentation caused by attribute
864 * locations assigned by the application may prevent large attributes
865 * from having enough contiguous space.
866 *
867 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700868 */
869
870 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
871
Ian Romanick553dcdc2010-06-23 12:14:02 -0700872 if (prog->Attributes != NULL) {
873 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700874 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700875 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700876
Ian Romanick69846702010-06-22 17:29:19 -0700877 /* Note: attributes that occupy multiple slots, such as arrays or
878 * matrices, may appear in the attrib array multiple times.
879 */
880 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700881 continue;
882
Ian Romanick69846702010-06-22 17:29:19 -0700883 /* From page 61 of the OpenGL 4.0 spec:
884 *
885 * "LinkProgram will fail if the attribute bindings assigned by
886 * BindAttribLocation do not leave not enough space to assign a
887 * location for an active matrix attribute or an active attribute
888 * array, both of which require multiple contiguous generic
889 * attributes."
890 *
891 * Previous versions of the spec contain similar language but omit the
892 * bit about attribute arrays.
893 *
894 * Page 61 of the OpenGL 4.0 spec also says:
895 *
896 * "It is possible for an application to bind more than one
897 * attribute name to the same location. This is referred to as
898 * aliasing. This will only work if only one of the aliased
899 * attributes is active in the executable program, or if no path
900 * through the shader consumes more than one attribute of a set
901 * of attributes aliased to the same location. A link error can
902 * occur if the linker determines that every path through the
903 * shader consumes multiple aliased attributes, but
904 * implementations are not required to generate an error in this
905 * case."
906 *
907 * These two paragraphs are either somewhat contradictory, or I don't
908 * fully understand one or both of them.
909 */
910 /* FINISHME: The code as currently written does not support attribute
911 * FINISHME: location aliasing (see comment above).
912 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700913 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -0700914 const unsigned slots = count_attribute_slots(var->type);
915
916 /* Mask representing the contiguous slots that will be used by this
917 * attribute.
918 */
919 const unsigned use_mask = (1 << slots) - 1;
920
921 /* Generate a link error if the set of bits requested for this
922 * attribute overlaps any previously allocated bits.
923 */
924 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700925 linker_error_printf(prog,
926 "insufficient contiguous attribute locations "
927 "available for vertex shader input `%s'",
928 var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700929 return false;
930 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700931
932 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -0700933 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700934 }
935 }
936
Ian Romanick69846702010-06-22 17:29:19 -0700937 /* Temporary storage for the set of attributes that need locations assigned.
938 */
939 struct temp_attr {
940 unsigned slots;
941 ir_variable *var;
942
943 /* Used below in the call to qsort. */
944 static int compare(const void *a, const void *b)
945 {
946 const temp_attr *const l = (const temp_attr *) a;
947 const temp_attr *const r = (const temp_attr *) b;
948
949 /* Reversed because we want a descending order sort below. */
950 return r->slots - l->slots;
951 }
952 } to_assign[16];
953
954 unsigned num_attr = 0;
955
Eric Anholt16b68b12010-06-30 11:05:43 -0700956 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700957 ir_variable *const var = ((ir_instruction *) node)->as_variable();
958
959 if ((var == NULL) || (var->mode != ir_var_in))
960 continue;
961
962 /* The location was explicitly assigned, nothing to do here.
963 */
964 if (var->location != -1)
965 continue;
966
Ian Romanick69846702010-06-22 17:29:19 -0700967 to_assign[num_attr].slots = count_attribute_slots(var->type);
968 to_assign[num_attr].var = var;
969 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700970 }
Ian Romanick69846702010-06-22 17:29:19 -0700971
972 /* If all of the attributes were assigned locations by the application (or
973 * are built-in attributes with fixed locations), return early. This should
974 * be the common case.
975 */
976 if (num_attr == 0)
977 return true;
978
979 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
980
Ian Romanick982e3792010-06-29 18:58:20 -0700981 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
982 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
983 * to prevent it from being automatically allocated below.
984 */
Ian Romanick35c89202010-07-07 16:28:39 -0700985 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -0700986
Ian Romanick69846702010-06-22 17:29:19 -0700987 for (unsigned i = 0; i < num_attr; i++) {
988 /* Mask representing the contiguous slots that will be used by this
989 * attribute.
990 */
991 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
992
993 int location = find_available_slots(used_locations, to_assign[i].slots);
994
995 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700996 linker_error_printf(prog,
997 "insufficient contiguous attribute locations "
998 "available for vertex shader input `%s'",
999 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001000 return false;
1001 }
1002
1003 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1004 used_locations |= (use_mask << location);
1005 }
1006
1007 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001008}
1009
1010
1011void
Eric Anholt16b68b12010-06-30 11:05:43 -07001012assign_varying_locations(gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001013{
1014 /* FINISHME: Set dynamically when geometry shader support is added. */
1015 unsigned output_index = VERT_RESULT_VAR0;
1016 unsigned input_index = FRAG_ATTRIB_VAR0;
1017
1018 /* Operate in a total of three passes.
1019 *
1020 * 1. Assign locations for any matching inputs and outputs.
1021 *
1022 * 2. Mark output variables in the producer that do not have locations as
1023 * not being outputs. This lets the optimizer eliminate them.
1024 *
1025 * 3. Mark input variables in the consumer that do not have locations as
1026 * not being inputs. This lets the optimizer eliminate them.
1027 */
1028
1029 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1030 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1031
Eric Anholt16b68b12010-06-30 11:05:43 -07001032 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001033 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1034
1035 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1036 || (output_var->location != -1))
1037 continue;
1038
1039 ir_variable *const input_var =
1040 consumer->symbols->get_variable(output_var->name);
1041
1042 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1043 continue;
1044
1045 assert(input_var->location == -1);
1046
1047 /* FINISHME: Location assignment will need some changes when arrays,
1048 * FINISHME: matrices, and structures are allowed as shader inputs /
1049 * FINISHME: outputs.
1050 */
1051 output_var->location = output_index;
1052 input_var->location = input_index;
1053
1054 output_index++;
1055 input_index++;
1056 }
1057
Eric Anholt16b68b12010-06-30 11:05:43 -07001058 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001059 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1060
1061 if ((var == NULL) || (var->mode != ir_var_out))
1062 continue;
1063
1064 /* An 'out' variable is only really a shader output if its value is read
1065 * by the following stage.
1066 */
Eric Anholtc10a6852010-07-13 11:07:16 -07001067 if (var->location == -1) {
1068 var->shader_out = false;
1069 var->mode = ir_var_auto;
1070 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001071 }
1072
Eric Anholt16b68b12010-06-30 11:05:43 -07001073 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001074 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1075
1076 if ((var == NULL) || (var->mode != ir_var_in))
1077 continue;
1078
1079 /* An 'in' variable is only really a shader input if its value is written
1080 * by the previous stage.
1081 */
1082 var->shader_in = (var->location != -1);
1083 }
1084}
1085
1086
1087void
Eric Anholt849e1812010-06-30 11:49:17 -07001088link_shaders(struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001089{
1090 prog->LinkStatus = false;
1091 prog->Validated = false;
1092 prog->_Used = false;
1093
Ian Romanickf36460e2010-06-23 12:07:22 -07001094 if (prog->InfoLog != NULL)
1095 talloc_free(prog->InfoLog);
1096
1097 prog->InfoLog = talloc_strdup(NULL, "");
1098
Ian Romanick832dfa52010-06-17 15:04:20 -07001099 /* Separate the shaders into groups based on their type.
1100 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001101 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001102 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001103 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001104 unsigned num_frag_shaders = 0;
1105
Eric Anholt16b68b12010-06-30 11:05:43 -07001106 vert_shader_list = (struct gl_shader **)
1107 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001108 frag_shader_list = &vert_shader_list[prog->NumShaders];
1109
1110 for (unsigned i = 0; i < prog->NumShaders; i++) {
1111 switch (prog->Shaders[i]->Type) {
1112 case GL_VERTEX_SHADER:
1113 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1114 num_vert_shaders++;
1115 break;
1116 case GL_FRAGMENT_SHADER:
1117 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1118 num_frag_shaders++;
1119 break;
1120 case GL_GEOMETRY_SHADER:
1121 /* FINISHME: Support geometry shaders. */
1122 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1123 break;
1124 }
1125 }
1126
1127 /* FINISHME: Implement intra-stage linking. */
Ian Romanickabee16e2010-06-21 16:16:05 -07001128 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001129 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001130 gl_shader *const sh =
1131 link_intrastage_shaders(prog, vert_shader_list, num_vert_shaders);
1132
1133 if (sh == NULL)
1134 goto done;
1135
1136 if (!validate_vertex_shader_executable(prog, sh))
1137 goto done;
1138
1139 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001140 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001141 }
1142
1143 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001144 gl_shader *const sh =
1145 link_intrastage_shaders(prog, frag_shader_list, num_frag_shaders);
1146
1147 if (sh == NULL)
1148 goto done;
1149
1150 if (!validate_fragment_shader_executable(prog, sh))
1151 goto done;
1152
1153 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001154 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001155 }
1156
Ian Romanick3ed850e2010-06-23 12:18:21 -07001157 /* Here begins the inter-stage linking phase. Some initial validation is
1158 * performed, then locations are assigned for uniforms, attributes, and
1159 * varyings.
1160 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001161 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -07001162 /* Validate the inputs of each stage with the output of the preceeding
1163 * stage.
1164 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001165 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -07001166 if (!cross_validate_outputs_to_inputs(prog,
1167 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -07001168 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001169 goto done;
1170 }
1171
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001172 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001173 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001174
Ian Romanick13e10e42010-06-21 12:03:24 -07001175 /* FINISHME: Perform whole-program optimization here. */
1176
Ian Romanickabee16e2010-06-21 16:16:05 -07001177 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001178
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001179 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER)
Ian Romanick9342d262010-06-22 17:41:37 -07001180 /* FINISHME: The value of the max_attribute_index parameter is
1181 * FINISHME: implementation dependent based on the value of
1182 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1183 * FINISHME: at least 16, so hardcode 16 for now.
1184 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001185 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -07001186 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -07001187
Ian Romanick0e59b262010-06-23 11:23:01 -07001188 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
1189 assign_varying_locations(prog->_LinkedShaders[i - 1],
1190 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -07001191
1192 /* FINISHME: Assign fragment shader output locations. */
1193
Ian Romanick832dfa52010-06-17 15:04:20 -07001194done:
1195 free(vert_shader_list);
1196}