blob: 481fcab16fe6377e6e6a792b9bd0f10f62f4b495 [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"
76#include "glsl_parser_extras.h"
77#include "ir.h"
Ian Romanick019a59b2010-06-21 16:10:42 -070078#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070079#include "program.h"
Ian Romanick019a59b2010-06-21 16:10:42 -070080#include "hash_table.h"
Ian Romanick3fb87872010-07-09 14:09:34 -070081#include "shader_api.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070082
83/**
84 * Visitor that determines whether or not a variable is ever written.
85 */
86class find_assignment_visitor : public ir_hierarchical_visitor {
87public:
88 find_assignment_visitor(const char *name)
89 : name(name), found(false)
90 {
91 /* empty */
92 }
93
94 virtual ir_visitor_status visit_enter(ir_assignment *ir)
95 {
96 ir_variable *const var = ir->lhs->variable_referenced();
97
98 if (strcmp(name, var->name) == 0) {
99 found = true;
100 return visit_stop;
101 }
102
103 return visit_continue_with_parent;
104 }
105
106 bool variable_found()
107 {
108 return found;
109 }
110
111private:
112 const char *name; /**< Find writes to a variable with this name. */
113 bool found; /**< Was a write to the variable found? */
114};
115
Ian Romanickc93b8f12010-06-17 15:20:22 -0700116
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700117void
Eric Anholt849e1812010-06-30 11:49:17 -0700118linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700119{
120 va_list ap;
121
122 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
123 va_start(ap, fmt);
124 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
125 va_end(ap);
126}
127
128
129void
Eric Anholt16b68b12010-06-30 11:05:43 -0700130invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700131 int generic_base)
132{
Eric Anholt16b68b12010-06-30 11:05:43 -0700133 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700134 ir_variable *const var = ((ir_instruction *) node)->as_variable();
135
136 if ((var == NULL) || (var->mode != (unsigned) mode))
137 continue;
138
139 /* Only assign locations for generic attributes / varyings / etc.
140 */
141 if (var->location >= generic_base)
142 var->location = -1;
143 }
144}
145
146
Ian Romanickc93b8f12010-06-17 15:20:22 -0700147/**
Ian Romanick69846702010-06-22 17:29:19 -0700148 * Determine the number of attribute slots required for a particular type
149 *
150 * This code is here because it implements the language rules of a specific
151 * GLSL version. Since it's a property of the language and not a property of
152 * types in general, it doesn't really belong in glsl_type.
153 */
154unsigned
155count_attribute_slots(const glsl_type *t)
156{
157 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
158 *
159 * "A scalar input counts the same amount against this limit as a vec4,
160 * so applications may want to consider packing groups of four
161 * unrelated float inputs together into a vector to better utilize the
162 * capabilities of the underlying hardware. A matrix input will use up
163 * multiple locations. The number of locations used will equal the
164 * number of columns in the matrix."
165 *
166 * The spec does not explicitly say how arrays are counted. However, it
167 * should be safe to assume the total number of slots consumed by an array
168 * is the number of entries in the array multiplied by the number of slots
169 * consumed by a single element of the array.
170 */
171
172 if (t->is_array())
173 return t->array_size() * count_attribute_slots(t->element_type());
174
175 if (t->is_matrix())
176 return t->matrix_columns;
177
178 return 1;
179}
180
181
182/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700183 * Verify that a vertex shader executable meets all semantic requirements
184 *
185 * \param shader Vertex shader executable to be verified
186 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700187bool
Eric Anholt849e1812010-06-30 11:49:17 -0700188validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700189 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700190{
191 if (shader == NULL)
192 return true;
193
194 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700195 linker_error_printf(prog, "vertex shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700196 return false;
197 }
198
199 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700200 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700201 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700202 linker_error_printf(prog,
203 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700204 return false;
205 }
206
207 return true;
208}
209
210
Ian Romanickc93b8f12010-06-17 15:20:22 -0700211/**
212 * Verify that a fragment shader executable meets all semantic requirements
213 *
214 * \param shader Fragment shader executable to be verified
215 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700216bool
Eric Anholt849e1812010-06-30 11:49:17 -0700217validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700218 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700219{
220 if (shader == NULL)
221 return true;
222
223 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700224 linker_error_printf(prog, "fragment shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700225 return false;
226 }
227
228 find_assignment_visitor frag_color("gl_FragColor");
229 find_assignment_visitor frag_data("gl_FragData");
230
Eric Anholt16b68b12010-06-30 11:05:43 -0700231 frag_color.run(shader->ir);
232 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700233
Ian Romanick832dfa52010-06-17 15:04:20 -0700234 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700235 linker_error_printf(prog, "fragment shader writes to both "
236 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700237 return false;
238 }
239
240 return true;
241}
242
243
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700244/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700245 * Generate a string describing the mode of a variable
246 */
247static const char *
248mode_string(const ir_variable *var)
249{
250 switch (var->mode) {
251 case ir_var_auto:
252 return (var->read_only) ? "global constant" : "global variable";
253
254 case ir_var_uniform: return "uniform";
255 case ir_var_in: return "shader input";
256 case ir_var_out: return "shader output";
257 case ir_var_inout: return "shader inout";
258 default:
259 assert(!"Should not get here.");
260 return "invalid variable";
261 }
262}
263
264
265/**
266 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700267 */
268bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700269cross_validate_globals(struct gl_shader_program *prog,
270 struct gl_shader **shader_list,
271 unsigned num_shaders,
272 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700273{
274 /* Examine all of the uniforms in all of the shaders and cross validate
275 * them.
276 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700277 glsl_symbol_table variables;
278 for (unsigned i = 0; i < num_shaders; i++) {
279 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700280 ir_variable *const var = ((ir_instruction *) node)->as_variable();
281
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700282 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700283 continue;
284
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700285 if (uniforms_only && (var->mode != ir_var_uniform))
286 continue;
287
288 /* If a global with this name has already been seen, verify that the
289 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700290 * initializers, the values of the initializers must be the same.
291 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700292 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700293 if (existing != NULL) {
294 if (var->type != existing->type) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700295 linker_error_printf(prog, "%s `%s' declared as type "
Ian Romanickf36460e2010-06-23 12:07:22 -0700296 "`%s' and type `%s'\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700297 mode_string(var),
Ian Romanickf36460e2010-06-23 12:07:22 -0700298 var->name, var->type->name,
299 existing->type->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700300 return false;
301 }
302
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700303 /* FINISHME: Handle non-constant initializers.
304 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700305 if (var->constant_value != NULL) {
306 if (existing->constant_value != NULL) {
307 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700308 linker_error_printf(prog, "initializers for %s "
Ian Romanickf36460e2010-06-23 12:07:22 -0700309 "`%s' have differing values\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700310 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700311 return false;
312 }
313 } else
314 /* If the first-seen instance of a particular uniform did not
315 * have an initializer but a later instance does, copy the
316 * initializer to the version stored in the symbol table.
317 */
Eric Anholt4b6fd392010-06-23 11:37:12 -0700318 existing->constant_value =
319 (ir_constant *)var->constant_value->clone(NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700320 }
321 } else
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700322 variables.add_variable(var->name, var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700323 }
324 }
325
326 return true;
327}
328
329
Ian Romanick37101922010-06-18 19:02:10 -0700330/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700331 * Perform validation of uniforms used across multiple shader stages
332 */
333bool
334cross_validate_uniforms(struct gl_shader_program *prog)
335{
336 return cross_validate_globals(prog, prog->_LinkedShaders,
337 prog->_NumLinkedShaders, true);
338}
339
340
341/**
Ian Romanick37101922010-06-18 19:02:10 -0700342 * Validate that outputs from one stage match inputs of another
343 */
344bool
Eric Anholt849e1812010-06-30 11:49:17 -0700345cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700346 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700347{
348 glsl_symbol_table parameters;
349 /* FINISHME: Figure these out dynamically. */
350 const char *const producer_stage = "vertex";
351 const char *const consumer_stage = "fragment";
352
353 /* Find all shader outputs in the "producer" stage.
354 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700355 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700356 ir_variable *const var = ((ir_instruction *) node)->as_variable();
357
358 /* FINISHME: For geometry shaders, this should also look for inout
359 * FINISHME: variables.
360 */
361 if ((var == NULL) || (var->mode != ir_var_out))
362 continue;
363
364 parameters.add_variable(var->name, var);
365 }
366
367
368 /* Find all shader inputs in the "consumer" stage. Any variables that have
369 * matching outputs already in the symbol table must have the same type and
370 * qualifiers.
371 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700372 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700373 ir_variable *const input = ((ir_instruction *) node)->as_variable();
374
375 /* FINISHME: For geometry shaders, this should also look for inout
376 * FINISHME: variables.
377 */
378 if ((input == NULL) || (input->mode != ir_var_in))
379 continue;
380
381 ir_variable *const output = parameters.get_variable(input->name);
382 if (output != NULL) {
383 /* Check that the types match between stages.
384 */
385 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700386 linker_error_printf(prog,
387 "%s shader output `%s' delcared as "
388 "type `%s', but %s shader input declared "
389 "as type `%s'\n",
390 producer_stage, output->name,
391 output->type->name,
392 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700393 return false;
394 }
395
396 /* Check that all of the qualifiers match between stages.
397 */
398 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700399 linker_error_printf(prog,
400 "%s shader output `%s' %s centroid qualifier, "
401 "but %s shader input %s centroid qualifier\n",
402 producer_stage,
403 output->name,
404 (output->centroid) ? "has" : "lacks",
405 consumer_stage,
406 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700407 return false;
408 }
409
410 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700411 linker_error_printf(prog,
412 "%s shader output `%s' %s invariant qualifier, "
413 "but %s shader input %s invariant qualifier\n",
414 producer_stage,
415 output->name,
416 (output->invariant) ? "has" : "lacks",
417 consumer_stage,
418 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700419 return false;
420 }
421
422 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700423 linker_error_printf(prog,
424 "%s shader output `%s' specifies %s "
425 "interpolation qualifier, "
426 "but %s shader input specifies %s "
427 "interpolation qualifier\n",
428 producer_stage,
429 output->name,
430 output->interpolation_string(),
431 consumer_stage,
432 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700433 return false;
434 }
435 }
436 }
437
438 return true;
439}
440
441
Ian Romanick3fb87872010-07-09 14:09:34 -0700442/**
443 * Populates a shaders symbol table with all global declarations
444 */
445static void
446populate_symbol_table(gl_shader *sh)
447{
448 sh->symbols = new(sh) glsl_symbol_table;
449
450 foreach_list(node, sh->ir) {
451 ir_instruction *const inst = (ir_instruction *) node;
452 ir_variable *var;
453 ir_function *func;
454
455 if ((func = inst->as_function()) != NULL) {
456 sh->symbols->add_function(func->name, func);
457 } else if ((var = inst->as_variable()) != NULL) {
458 sh->symbols->add_variable(var->name, var);
459 }
460 }
461}
462
463
464/**
Ian Romanick31a97862010-07-12 18:48:50 -0700465 * Remap variables referenced in an instruction tree
466 *
467 * This is used when instruction trees are cloned from one shader and placed in
468 * another. These trees will contain references to \c ir_variable nodes that
469 * do not exist in the target shader. This function finds these \c ir_variable
470 * references and replaces the references with matching variables in the target
471 * shader.
472 *
473 * If there is no matching variable in the target shader, a clone of the
474 * \c ir_variable is made and added to the target shader. The new variable is
475 * added to \b both the instruction stream and the symbol table.
476 *
477 * \param inst IR tree that is to be processed.
478 * \param symbols Symbol table containing global scope symbols in the
479 * linked shader.
480 * \param instructions Instruction stream where new variable declarations
481 * should be added.
482 */
483void
484remap_variables(ir_instruction *inst, glsl_symbol_table *symbols,
485 exec_list *instructions)
486{
487 class remap_visitor : public ir_hierarchical_visitor {
488 public:
489 remap_visitor(glsl_symbol_table *symbols, exec_list *instructions)
490 {
491 this->symbols = symbols;
492 this->instructions = instructions;
493 }
494
495 virtual ir_visitor_status visit(ir_dereference_variable *ir)
496 {
497 ir_variable *const existing =
498 this->symbols->get_variable(ir->var->name);
499 if (existing != NULL)
500 ir->var = existing;
501 else {
502 ir_variable *copy = ir->var->clone(NULL);
503
504 this->symbols->add_variable(copy->name, copy);
505 this->instructions->push_head(copy);
506 }
507
508 return visit_continue;
509 }
510
511 private:
512 glsl_symbol_table *symbols;
513 exec_list *instructions;
514 };
515
516 remap_visitor v(symbols, instructions);
517
518 inst->accept(&v);
519}
520
521
522/**
523 * Move non-declarations from one instruction stream to another
524 *
525 * The intended usage pattern of this function is to pass the pointer to the
526 * head sentinal of a list (i.e., a pointer to the list cast to an \c exec_node
527 * pointer) for \c last and \c false for \c make_copies on the first
528 * call. Successive calls pass the return value of the previous call for
529 * \c last and \c true for \c make_copies.
530 *
531 * \param instructions Source instruction stream
532 * \param last Instruction after which new instructions should be
533 * inserted in the target instruction stream
534 * \param make_copies Flag selecting whether instructions in \c instructions
535 * should be copied (via \c ir_instruction::clone) into the
536 * target list or moved.
537 *
538 * \return
539 * The new "last" instruction in the target instruction stream. This pointer
540 * is suitable for use as the \c last parameter of a later call to this
541 * function.
542 */
543exec_node *
544move_non_declarations(exec_list *instructions, exec_node *last,
545 bool make_copies, gl_shader *target)
546{
547 foreach_list(node, instructions) {
548 ir_instruction *inst = (ir_instruction *) node;
549
550 if (inst->as_variable() || inst->as_function())
551 continue;
552
553 assert(inst->as_assignment());
554
555 if (make_copies) {
556 inst = inst->clone(NULL);
557 remap_variables(inst, target->symbols, target->ir);
558 } else {
559 inst->remove();
560 }
561
562 last->insert_after(inst);
563 last = inst;
564 }
565
566 return last;
567}
568
569/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700570 * Get the function signature for main from a shader
571 */
572static ir_function_signature *
573get_main_function_signature(gl_shader *sh)
574{
575 ir_function *const f = sh->symbols->get_function("main");
576 if (f != NULL) {
577 exec_list void_parameters;
578
579 /* Look for the 'void main()' signature and ensure that it's defined.
580 * This keeps the linker from accidentally pick a shader that just
581 * contains a prototype for main.
582 *
583 * We don't have to check for multiple definitions of main (in multiple
584 * shaders) because that would have already been caught above.
585 */
586 ir_function_signature *sig = f->matching_signature(&void_parameters);
587 if ((sig != NULL) && sig->is_defined) {
588 return sig;
589 }
590 }
591
592 return NULL;
593}
594
595
596/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700597 * Combine a group of shaders for a single stage to generate a linked shader
598 *
599 * \note
600 * If this function is supplied a single shader, it is cloned, and the new
601 * shader is returned.
602 */
603static struct gl_shader *
604link_intrastage_shaders(struct gl_shader_program *prog,
605 struct gl_shader **shader_list,
606 unsigned num_shaders)
607{
Ian Romanick13f782c2010-06-29 18:53:38 -0700608 /* Check that global variables defined in multiple shaders are consistent.
609 */
610 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
611 return NULL;
612
613 /* Check that there is only a single definition of each function signature
614 * across all shaders.
615 */
616 for (unsigned i = 0; i < (num_shaders - 1); i++) {
617 foreach_list(node, shader_list[i]->ir) {
618 ir_function *const f = ((ir_instruction *) node)->as_function();
619
620 if (f == NULL)
621 continue;
622
623 for (unsigned j = i + 1; j < num_shaders; j++) {
624 ir_function *const other =
625 shader_list[j]->symbols->get_function(f->name);
626
627 /* If the other shader has no function (and therefore no function
628 * signatures) with the same name, skip to the next shader.
629 */
630 if (other == NULL)
631 continue;
632
633 foreach_iter (exec_list_iterator, iter, *f) {
634 ir_function_signature *sig =
635 (ir_function_signature *) iter.get();
636
637 if (!sig->is_defined || sig->is_built_in)
638 continue;
639
640 ir_function_signature *other_sig =
641 other->exact_matching_signature(& sig->parameters);
642
643 if ((other_sig != NULL) && other_sig->is_defined
644 && !other_sig->is_built_in) {
645 linker_error_printf(prog,
646 "function `%s' is multiply defined",
647 f->name);
648 return NULL;
649 }
650 }
651 }
652 }
653 }
654
655 /* Find the shader that defines main, and make a clone of it.
656 *
657 * Starting with the clone, search for undefined references. If one is
658 * found, find the shader that defines it. Clone the reference and add
659 * it to the shader. Repeat until there are no undefined references or
660 * until a reference cannot be resolved.
661 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700662 gl_shader *main = NULL;
663 for (unsigned i = 0; i < num_shaders; i++) {
664 if (get_main_function_signature(shader_list[i]) != NULL) {
665 main = shader_list[i];
666 break;
667 }
668 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700669
Ian Romanick15ce87e2010-07-09 15:28:22 -0700670 if (main == NULL) {
671 linker_error_printf(prog, "%s shader lacks `main'\n",
672 (shader_list[0]->Type == GL_VERTEX_SHADER)
673 ? "vertex" : "fragment");
674 return NULL;
675 }
676
677 gl_shader *const linked = _mesa_new_shader(NULL, 0, main->Type);
678 linked->ir = new(linked) exec_list;
679 clone_ir_list(linked->ir, main->ir);
680
681 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700682
Ian Romanick31a97862010-07-12 18:48:50 -0700683 /* The a pointer to the main function in the final linked shader (i.e., the
684 * copy of the original shader that contained the main function).
685 */
686 ir_function_signature *const main_sig = get_main_function_signature(linked);
687
688 /* Move any instructions other than variable declarations or function
689 * declarations into main.
690 */
691 exec_node *insertion_point = (exec_node *) &main_sig->body;
692 for (unsigned i = 0; i < num_shaders; i++) {
693 insertion_point = move_non_declarations(shader_list[i]->ir,
694 insertion_point,
695 (shader_list[i] != main),
696 linked);
697 }
698
Ian Romanick13f782c2010-06-29 18:53:38 -0700699 /* Resolve initializers for global variables in the linked shader.
700 */
Ian Romanick3fb87872010-07-09 14:09:34 -0700701
Ian Romanick3fb87872010-07-09 14:09:34 -0700702 return linked;
703}
704
705
Ian Romanick019a59b2010-06-21 16:10:42 -0700706struct uniform_node {
707 exec_node link;
708 struct gl_uniform *u;
709 unsigned slots;
710};
711
Ian Romanickabee16e2010-06-21 16:16:05 -0700712void
Eric Anholt849e1812010-06-30 11:49:17 -0700713assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700714{
715 /* */
716 exec_list uniforms;
717 unsigned total_uniforms = 0;
718 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
719 hash_table_string_compare);
720
Ian Romanickabee16e2010-06-21 16:16:05 -0700721 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700722 unsigned next_position = 0;
723
Eric Anholt16b68b12010-06-30 11:05:43 -0700724 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700725 ir_variable *const var = ((ir_instruction *) node)->as_variable();
726
727 if ((var == NULL) || (var->mode != ir_var_uniform))
728 continue;
729
730 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
731 assert(vec4_slots != 0);
732
733 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
734 if (n == NULL) {
735 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
736 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
737 n->slots = vec4_slots;
738
739 n->u[0].Name = strdup(var->name);
740 for (unsigned j = 1; j < vec4_slots; j++)
741 n->u[j].Name = n->u[0].Name;
742
743 hash_table_insert(ht, n, n->u[0].Name);
744 uniforms.push_tail(& n->link);
745 total_uniforms += vec4_slots;
746 }
747
748 if (var->constant_value != NULL)
749 for (unsigned j = 0; j < vec4_slots; j++)
750 n->u[j].Initialized = true;
751
752 var->location = next_position;
753
754 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700755 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700756 case GL_VERTEX_SHADER:
757 n->u[j].VertPos = next_position;
758 break;
759 case GL_FRAGMENT_SHADER:
760 n->u[j].FragPos = next_position;
761 break;
762 case GL_GEOMETRY_SHADER:
763 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700764 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700765 break;
766 }
767
768 next_position++;
769 }
770 }
771 }
772
773 gl_uniform_list *ul = (gl_uniform_list *)
774 calloc(1, sizeof(gl_uniform_list));
775
776 ul->Size = total_uniforms;
777 ul->NumUniforms = total_uniforms;
778 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
779
780 unsigned idx = 0;
781 uniform_node *next;
782 for (uniform_node *node = (uniform_node *) uniforms.head
783 ; node->link.next != NULL
784 ; node = next) {
785 next = (uniform_node *) node->link.next;
786
787 node->link.remove();
788 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
789 idx += node->slots;
790
791 free(node->u);
792 free(node);
793 }
794
795 hash_table_dtor(ht);
796
Ian Romanickabee16e2010-06-21 16:16:05 -0700797 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700798}
799
800
Ian Romanick69846702010-06-22 17:29:19 -0700801/**
802 * Find a contiguous set of available bits in a bitmask
803 *
804 * \param used_mask Bits representing used (1) and unused (0) locations
805 * \param needed_count Number of contiguous bits needed.
806 *
807 * \return
808 * Base location of the available bits on success or -1 on failure.
809 */
810int
811find_available_slots(unsigned used_mask, unsigned needed_count)
812{
813 unsigned needed_mask = (1 << needed_count) - 1;
814 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
815
816 /* The comparison to 32 is redundant, but without it GCC emits "warning:
817 * cannot optimize possibly infinite loops" for the loop below.
818 */
819 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
820 return -1;
821
822 for (int i = 0; i <= max_bit_to_test; i++) {
823 if ((needed_mask & ~used_mask) == needed_mask)
824 return i;
825
826 needed_mask <<= 1;
827 }
828
829 return -1;
830}
831
832
833bool
Eric Anholt849e1812010-06-30 11:49:17 -0700834assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700835{
Ian Romanick9342d262010-06-22 17:41:37 -0700836 /* Mark invalid attribute locations as being used.
837 */
838 unsigned used_locations = (max_attribute_index >= 32)
839 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700840
Eric Anholt16b68b12010-06-30 11:05:43 -0700841 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700842 assert(sh->Type == GL_VERTEX_SHADER);
843
Ian Romanick69846702010-06-22 17:29:19 -0700844 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700845 *
846 * 1. Invalidate the location assignments for all vertex shader inputs.
847 *
848 * 2. Assign locations for inputs that have user-defined (via
849 * glBindVertexAttribLocation) locatoins.
850 *
Ian Romanick69846702010-06-22 17:29:19 -0700851 * 3. Sort the attributes without assigned locations by number of slots
852 * required in decreasing order. Fragmentation caused by attribute
853 * locations assigned by the application may prevent large attributes
854 * from having enough contiguous space.
855 *
856 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700857 */
858
859 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
860
Ian Romanick553dcdc2010-06-23 12:14:02 -0700861 if (prog->Attributes != NULL) {
862 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700863 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700864 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700865
Ian Romanick69846702010-06-22 17:29:19 -0700866 /* Note: attributes that occupy multiple slots, such as arrays or
867 * matrices, may appear in the attrib array multiple times.
868 */
869 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700870 continue;
871
Ian Romanick69846702010-06-22 17:29:19 -0700872 /* From page 61 of the OpenGL 4.0 spec:
873 *
874 * "LinkProgram will fail if the attribute bindings assigned by
875 * BindAttribLocation do not leave not enough space to assign a
876 * location for an active matrix attribute or an active attribute
877 * array, both of which require multiple contiguous generic
878 * attributes."
879 *
880 * Previous versions of the spec contain similar language but omit the
881 * bit about attribute arrays.
882 *
883 * Page 61 of the OpenGL 4.0 spec also says:
884 *
885 * "It is possible for an application to bind more than one
886 * attribute name to the same location. This is referred to as
887 * aliasing. This will only work if only one of the aliased
888 * attributes is active in the executable program, or if no path
889 * through the shader consumes more than one attribute of a set
890 * of attributes aliased to the same location. A link error can
891 * occur if the linker determines that every path through the
892 * shader consumes multiple aliased attributes, but
893 * implementations are not required to generate an error in this
894 * case."
895 *
896 * These two paragraphs are either somewhat contradictory, or I don't
897 * fully understand one or both of them.
898 */
899 /* FINISHME: The code as currently written does not support attribute
900 * FINISHME: location aliasing (see comment above).
901 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700902 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -0700903 const unsigned slots = count_attribute_slots(var->type);
904
905 /* Mask representing the contiguous slots that will be used by this
906 * attribute.
907 */
908 const unsigned use_mask = (1 << slots) - 1;
909
910 /* Generate a link error if the set of bits requested for this
911 * attribute overlaps any previously allocated bits.
912 */
913 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700914 linker_error_printf(prog,
915 "insufficient contiguous attribute locations "
916 "available for vertex shader input `%s'",
917 var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700918 return false;
919 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700920
921 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -0700922 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700923 }
924 }
925
Ian Romanick69846702010-06-22 17:29:19 -0700926 /* Temporary storage for the set of attributes that need locations assigned.
927 */
928 struct temp_attr {
929 unsigned slots;
930 ir_variable *var;
931
932 /* Used below in the call to qsort. */
933 static int compare(const void *a, const void *b)
934 {
935 const temp_attr *const l = (const temp_attr *) a;
936 const temp_attr *const r = (const temp_attr *) b;
937
938 /* Reversed because we want a descending order sort below. */
939 return r->slots - l->slots;
940 }
941 } to_assign[16];
942
943 unsigned num_attr = 0;
944
Eric Anholt16b68b12010-06-30 11:05:43 -0700945 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700946 ir_variable *const var = ((ir_instruction *) node)->as_variable();
947
948 if ((var == NULL) || (var->mode != ir_var_in))
949 continue;
950
951 /* The location was explicitly assigned, nothing to do here.
952 */
953 if (var->location != -1)
954 continue;
955
Ian Romanick69846702010-06-22 17:29:19 -0700956 to_assign[num_attr].slots = count_attribute_slots(var->type);
957 to_assign[num_attr].var = var;
958 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700959 }
Ian Romanick69846702010-06-22 17:29:19 -0700960
961 /* If all of the attributes were assigned locations by the application (or
962 * are built-in attributes with fixed locations), return early. This should
963 * be the common case.
964 */
965 if (num_attr == 0)
966 return true;
967
968 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
969
Ian Romanick982e3792010-06-29 18:58:20 -0700970 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
971 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
972 * to prevent it from being automatically allocated below.
973 */
Ian Romanick35c89202010-07-07 16:28:39 -0700974 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -0700975
Ian Romanick69846702010-06-22 17:29:19 -0700976 for (unsigned i = 0; i < num_attr; i++) {
977 /* Mask representing the contiguous slots that will be used by this
978 * attribute.
979 */
980 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
981
982 int location = find_available_slots(used_locations, to_assign[i].slots);
983
984 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700985 linker_error_printf(prog,
986 "insufficient contiguous attribute locations "
987 "available for vertex shader input `%s'",
988 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700989 return false;
990 }
991
992 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
993 used_locations |= (use_mask << location);
994 }
995
996 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700997}
998
999
1000void
Eric Anholt16b68b12010-06-30 11:05:43 -07001001assign_varying_locations(gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001002{
1003 /* FINISHME: Set dynamically when geometry shader support is added. */
1004 unsigned output_index = VERT_RESULT_VAR0;
1005 unsigned input_index = FRAG_ATTRIB_VAR0;
1006
1007 /* Operate in a total of three passes.
1008 *
1009 * 1. Assign locations for any matching inputs and outputs.
1010 *
1011 * 2. Mark output variables in the producer that do not have locations as
1012 * not being outputs. This lets the optimizer eliminate them.
1013 *
1014 * 3. Mark input variables in the consumer that do not have locations as
1015 * not being inputs. This lets the optimizer eliminate them.
1016 */
1017
1018 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1019 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1020
Eric Anholt16b68b12010-06-30 11:05:43 -07001021 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001022 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1023
1024 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1025 || (output_var->location != -1))
1026 continue;
1027
1028 ir_variable *const input_var =
1029 consumer->symbols->get_variable(output_var->name);
1030
1031 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1032 continue;
1033
1034 assert(input_var->location == -1);
1035
1036 /* FINISHME: Location assignment will need some changes when arrays,
1037 * FINISHME: matrices, and structures are allowed as shader inputs /
1038 * FINISHME: outputs.
1039 */
1040 output_var->location = output_index;
1041 input_var->location = input_index;
1042
1043 output_index++;
1044 input_index++;
1045 }
1046
Eric Anholt16b68b12010-06-30 11:05:43 -07001047 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001048 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1049
1050 if ((var == NULL) || (var->mode != ir_var_out))
1051 continue;
1052
1053 /* An 'out' variable is only really a shader output if its value is read
1054 * by the following stage.
1055 */
1056 var->shader_out = (var->location != -1);
1057 }
1058
Eric Anholt16b68b12010-06-30 11:05:43 -07001059 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001060 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1061
1062 if ((var == NULL) || (var->mode != ir_var_in))
1063 continue;
1064
1065 /* An 'in' variable is only really a shader input if its value is written
1066 * by the previous stage.
1067 */
1068 var->shader_in = (var->location != -1);
1069 }
1070}
1071
1072
1073void
Eric Anholt849e1812010-06-30 11:49:17 -07001074link_shaders(struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001075{
1076 prog->LinkStatus = false;
1077 prog->Validated = false;
1078 prog->_Used = false;
1079
Ian Romanickf36460e2010-06-23 12:07:22 -07001080 if (prog->InfoLog != NULL)
1081 talloc_free(prog->InfoLog);
1082
1083 prog->InfoLog = talloc_strdup(NULL, "");
1084
Ian Romanick832dfa52010-06-17 15:04:20 -07001085 /* Separate the shaders into groups based on their type.
1086 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001087 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001088 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001089 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001090 unsigned num_frag_shaders = 0;
1091
Eric Anholt16b68b12010-06-30 11:05:43 -07001092 vert_shader_list = (struct gl_shader **)
1093 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001094 frag_shader_list = &vert_shader_list[prog->NumShaders];
1095
1096 for (unsigned i = 0; i < prog->NumShaders; i++) {
1097 switch (prog->Shaders[i]->Type) {
1098 case GL_VERTEX_SHADER:
1099 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1100 num_vert_shaders++;
1101 break;
1102 case GL_FRAGMENT_SHADER:
1103 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1104 num_frag_shaders++;
1105 break;
1106 case GL_GEOMETRY_SHADER:
1107 /* FINISHME: Support geometry shaders. */
1108 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1109 break;
1110 }
1111 }
1112
1113 /* FINISHME: Implement intra-stage linking. */
Ian Romanickabee16e2010-06-21 16:16:05 -07001114 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001115 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001116 gl_shader *const sh =
1117 link_intrastage_shaders(prog, vert_shader_list, num_vert_shaders);
1118
1119 if (sh == NULL)
1120 goto done;
1121
1122 if (!validate_vertex_shader_executable(prog, sh))
1123 goto done;
1124
1125 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001126 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001127 }
1128
1129 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001130 gl_shader *const sh =
1131 link_intrastage_shaders(prog, frag_shader_list, num_frag_shaders);
1132
1133 if (sh == NULL)
1134 goto done;
1135
1136 if (!validate_fragment_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
Ian Romanick3ed850e2010-06-23 12:18:21 -07001143 /* Here begins the inter-stage linking phase. Some initial validation is
1144 * performed, then locations are assigned for uniforms, attributes, and
1145 * varyings.
1146 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001147 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -07001148 /* Validate the inputs of each stage with the output of the preceeding
1149 * stage.
1150 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001151 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -07001152 if (!cross_validate_outputs_to_inputs(prog,
1153 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -07001154 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001155 goto done;
1156 }
1157
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001158 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001159 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001160
Ian Romanick13e10e42010-06-21 12:03:24 -07001161 /* FINISHME: Perform whole-program optimization here. */
1162
Ian Romanickabee16e2010-06-21 16:16:05 -07001163 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001164
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001165 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER)
Ian Romanick9342d262010-06-22 17:41:37 -07001166 /* FINISHME: The value of the max_attribute_index parameter is
1167 * FINISHME: implementation dependent based on the value of
1168 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1169 * FINISHME: at least 16, so hardcode 16 for now.
1170 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001171 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -07001172 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -07001173
Ian Romanick0e59b262010-06-23 11:23:01 -07001174 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
1175 assign_varying_locations(prog->_LinkedShaders[i - 1],
1176 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -07001177
1178 /* FINISHME: Assign fragment shader output locations. */
1179
Ian Romanick832dfa52010-06-17 15:04:20 -07001180done:
1181 free(vert_shader_list);
1182}