blob: cde70adff5674e8ab750f24532e984d7847cc449 [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>
Ian Romanick25f51d32010-07-16 15:51:50 -070069#include <climits>
Ian Romanickf36460e2010-06-23 12:07:22 -070070
71extern "C" {
72#include <talloc.h>
73}
Ian Romanick832dfa52010-06-17 15:04:20 -070074
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080075#include "main/core.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070076#include "glsl_symbol_table.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070077#include "ir.h"
78#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030079#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070080#include "linker.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070081#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070082
Ian Romanick3322fba2010-10-14 13:28:42 -070083extern "C" {
84#include "main/shaderobj.h"
85}
86
Ian Romanick832dfa52010-06-17 15:04:20 -070087/**
88 * Visitor that determines whether or not a variable is ever written.
89 */
90class find_assignment_visitor : public ir_hierarchical_visitor {
91public:
92 find_assignment_visitor(const char *name)
93 : name(name), found(false)
94 {
95 /* empty */
96 }
97
98 virtual ir_visitor_status visit_enter(ir_assignment *ir)
99 {
100 ir_variable *const var = ir->lhs->variable_referenced();
101
102 if (strcmp(name, var->name) == 0) {
103 found = true;
104 return visit_stop;
105 }
106
107 return visit_continue_with_parent;
108 }
109
Eric Anholt18a60232010-08-23 11:29:25 -0700110 virtual ir_visitor_status visit_enter(ir_call *ir)
111 {
112 exec_list_iterator sig_iter = ir->get_callee()->parameters.iterator();
113 foreach_iter(exec_list_iterator, iter, *ir) {
114 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
115 ir_variable *sig_param = (ir_variable *)sig_iter.get();
116
117 if (sig_param->mode == ir_var_out ||
118 sig_param->mode == ir_var_inout) {
119 ir_variable *var = param_rval->variable_referenced();
120 if (var && strcmp(name, var->name) == 0) {
121 found = true;
122 return visit_stop;
123 }
124 }
125 sig_iter.next();
126 }
127
128 return visit_continue_with_parent;
129 }
130
Ian Romanick832dfa52010-06-17 15:04:20 -0700131 bool variable_found()
132 {
133 return found;
134 }
135
136private:
137 const char *name; /**< Find writes to a variable with this name. */
138 bool found; /**< Was a write to the variable found? */
139};
140
Ian Romanickc93b8f12010-06-17 15:20:22 -0700141
Ian Romanickc33e78f2010-08-13 12:30:41 -0700142/**
143 * Visitor that determines whether or not a variable is ever read.
144 */
145class find_deref_visitor : public ir_hierarchical_visitor {
146public:
147 find_deref_visitor(const char *name)
148 : name(name), found(false)
149 {
150 /* empty */
151 }
152
153 virtual ir_visitor_status visit(ir_dereference_variable *ir)
154 {
155 if (strcmp(this->name, ir->var->name) == 0) {
156 this->found = true;
157 return visit_stop;
158 }
159
160 return visit_continue;
161 }
162
163 bool variable_found() const
164 {
165 return this->found;
166 }
167
168private:
169 const char *name; /**< Find writes to a variable with this name. */
170 bool found; /**< Was a write to the variable found? */
171};
172
173
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700174void
Eric Anholt849e1812010-06-30 11:49:17 -0700175linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700176{
177 va_list ap;
178
179 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
180 va_start(ap, fmt);
181 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
182 va_end(ap);
183}
184
185
186void
Eric Anholt16b68b12010-06-30 11:05:43 -0700187invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700188 int generic_base)
189{
Eric Anholt16b68b12010-06-30 11:05:43 -0700190 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700191 ir_variable *const var = ((ir_instruction *) node)->as_variable();
192
193 if ((var == NULL) || (var->mode != (unsigned) mode))
194 continue;
195
196 /* Only assign locations for generic attributes / varyings / etc.
197 */
Ian Romanick68a4fc92010-10-07 17:21:22 -0700198 if ((var->location >= generic_base) && !var->explicit_location)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700199 var->location = -1;
200 }
201}
202
203
Ian Romanickc93b8f12010-06-17 15:20:22 -0700204/**
Ian Romanick69846702010-06-22 17:29:19 -0700205 * Determine the number of attribute slots required for a particular type
206 *
207 * This code is here because it implements the language rules of a specific
208 * GLSL version. Since it's a property of the language and not a property of
209 * types in general, it doesn't really belong in glsl_type.
210 */
211unsigned
212count_attribute_slots(const glsl_type *t)
213{
214 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
215 *
216 * "A scalar input counts the same amount against this limit as a vec4,
217 * so applications may want to consider packing groups of four
218 * unrelated float inputs together into a vector to better utilize the
219 * capabilities of the underlying hardware. A matrix input will use up
220 * multiple locations. The number of locations used will equal the
221 * number of columns in the matrix."
222 *
223 * The spec does not explicitly say how arrays are counted. However, it
224 * should be safe to assume the total number of slots consumed by an array
225 * is the number of entries in the array multiplied by the number of slots
226 * consumed by a single element of the array.
227 */
228
229 if (t->is_array())
230 return t->array_size() * count_attribute_slots(t->element_type());
231
232 if (t->is_matrix())
233 return t->matrix_columns;
234
235 return 1;
236}
237
238
239/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700240 * Verify that a vertex shader executable meets all semantic requirements
241 *
242 * \param shader Vertex shader executable to be verified
243 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700244bool
Eric Anholt849e1812010-06-30 11:49:17 -0700245validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700246 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700247{
248 if (shader == NULL)
249 return true;
250
Ian Romanick832dfa52010-06-17 15:04:20 -0700251 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700252 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700253 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700254 linker_error_printf(prog,
255 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700256 return false;
257 }
258
259 return true;
260}
261
262
Ian Romanickc93b8f12010-06-17 15:20:22 -0700263/**
264 * Verify that a fragment shader executable meets all semantic requirements
265 *
266 * \param shader Fragment shader executable to be verified
267 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700268bool
Eric Anholt849e1812010-06-30 11:49:17 -0700269validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700270 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700271{
272 if (shader == NULL)
273 return true;
274
Ian Romanick832dfa52010-06-17 15:04:20 -0700275 find_assignment_visitor frag_color("gl_FragColor");
276 find_assignment_visitor frag_data("gl_FragData");
277
Eric Anholt16b68b12010-06-30 11:05:43 -0700278 frag_color.run(shader->ir);
279 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700280
Ian Romanick832dfa52010-06-17 15:04:20 -0700281 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700282 linker_error_printf(prog, "fragment shader writes to both "
283 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700284 return false;
285 }
286
287 return true;
288}
289
290
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700291/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700292 * Generate a string describing the mode of a variable
293 */
294static const char *
295mode_string(const ir_variable *var)
296{
297 switch (var->mode) {
298 case ir_var_auto:
299 return (var->read_only) ? "global constant" : "global variable";
300
301 case ir_var_uniform: return "uniform";
302 case ir_var_in: return "shader input";
303 case ir_var_out: return "shader output";
304 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700305
306 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700307 default:
308 assert(!"Should not get here.");
309 return "invalid variable";
310 }
311}
312
313
314/**
315 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700316 */
317bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700318cross_validate_globals(struct gl_shader_program *prog,
319 struct gl_shader **shader_list,
320 unsigned num_shaders,
321 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700322{
323 /* Examine all of the uniforms in all of the shaders and cross validate
324 * them.
325 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700326 glsl_symbol_table variables;
327 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700328 if (shader_list[i] == NULL)
329 continue;
330
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700331 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700332 ir_variable *const var = ((ir_instruction *) node)->as_variable();
333
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700334 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700335 continue;
336
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700337 if (uniforms_only && (var->mode != ir_var_uniform))
338 continue;
339
Ian Romanick7e2aa912010-07-19 17:12:42 -0700340 /* Don't cross validate temporaries that are at global scope. These
341 * will eventually get pulled into the shaders 'main'.
342 */
343 if (var->mode == ir_var_temporary)
344 continue;
345
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700346 /* If a global with this name has already been seen, verify that the
347 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700348 * initializers, the values of the initializers must be the same.
349 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700350 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700351 if (existing != NULL) {
352 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700353 /* Consider the types to be "the same" if both types are arrays
354 * of the same type and one of the arrays is implicitly sized.
355 * In addition, set the type of the linked variable to the
356 * explicitly sized array.
357 */
358 if (var->type->is_array()
359 && existing->type->is_array()
360 && (var->type->fields.array == existing->type->fields.array)
361 && ((var->type->length == 0)
362 || (existing->type->length == 0))) {
363 if (existing->type->length == 0)
364 existing->type = var->type;
365 } else {
366 linker_error_printf(prog, "%s `%s' declared as type "
367 "`%s' and type `%s'\n",
368 mode_string(var),
369 var->name, var->type->name,
370 existing->type->name);
371 return false;
372 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700373 }
374
Ian Romanick68a4fc92010-10-07 17:21:22 -0700375 if (var->explicit_location) {
376 if (existing->explicit_location
377 && (var->location != existing->location)) {
378 linker_error_printf(prog, "explicit locations for %s "
379 "`%s' have differing values\n",
380 mode_string(var), var->name);
381 return false;
382 }
383
384 existing->location = var->location;
385 existing->explicit_location = true;
386 }
387
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700388 /* FINISHME: Handle non-constant initializers.
389 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700390 if (var->constant_value != NULL) {
391 if (existing->constant_value != NULL) {
392 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700393 linker_error_printf(prog, "initializers for %s "
Ian Romanickf36460e2010-06-23 12:07:22 -0700394 "`%s' have differing values\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700395 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700396 return false;
397 }
398 } else
399 /* If the first-seen instance of a particular uniform did not
400 * have an initializer but a later instance does, copy the
401 * initializer to the version stored in the symbol table.
402 */
Ian Romanickde415b72010-07-14 13:22:12 -0700403 /* FINISHME: This is wrong. The constant_value field should
404 * FINISHME: not be modified! Imagine a case where a shader
405 * FINISHME: without an initializer is linked in two different
406 * FINISHME: programs with shaders that have differing
407 * FINISHME: initializers. Linking with the first will
408 * FINISHME: modify the shader, and linking with the second
409 * FINISHME: will fail.
410 */
Eric Anholt8273bd42010-08-04 12:34:56 -0700411 existing->constant_value =
412 var->constant_value->clone(talloc_parent(existing), NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700413 }
414 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700415 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700416 }
417 }
418
419 return true;
420}
421
422
Ian Romanick37101922010-06-18 19:02:10 -0700423/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700424 * Perform validation of uniforms used across multiple shader stages
425 */
426bool
427cross_validate_uniforms(struct gl_shader_program *prog)
428{
429 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700430 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700431}
432
433
434/**
Ian Romanick37101922010-06-18 19:02:10 -0700435 * Validate that outputs from one stage match inputs of another
436 */
437bool
Eric Anholt849e1812010-06-30 11:49:17 -0700438cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700439 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700440{
441 glsl_symbol_table parameters;
442 /* FINISHME: Figure these out dynamically. */
443 const char *const producer_stage = "vertex";
444 const char *const consumer_stage = "fragment";
445
446 /* Find all shader outputs in the "producer" stage.
447 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700448 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700449 ir_variable *const var = ((ir_instruction *) node)->as_variable();
450
451 /* FINISHME: For geometry shaders, this should also look for inout
452 * FINISHME: variables.
453 */
454 if ((var == NULL) || (var->mode != ir_var_out))
455 continue;
456
Eric Anholt001eee52010-11-05 06:11:24 -0700457 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700458 }
459
460
461 /* Find all shader inputs in the "consumer" stage. Any variables that have
462 * matching outputs already in the symbol table must have the same type and
463 * qualifiers.
464 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700465 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700466 ir_variable *const input = ((ir_instruction *) node)->as_variable();
467
468 /* FINISHME: For geometry shaders, this should also look for inout
469 * FINISHME: variables.
470 */
471 if ((input == NULL) || (input->mode != ir_var_in))
472 continue;
473
474 ir_variable *const output = parameters.get_variable(input->name);
475 if (output != NULL) {
476 /* Check that the types match between stages.
477 */
478 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700479 linker_error_printf(prog,
Brian Paul2b955252010-09-21 14:56:58 -0600480 "%s shader output `%s' declared as "
Ian Romanickf36460e2010-06-23 12:07:22 -0700481 "type `%s', but %s shader input declared "
482 "as type `%s'\n",
483 producer_stage, output->name,
484 output->type->name,
485 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700486 return false;
487 }
488
489 /* Check that all of the qualifiers match between stages.
490 */
491 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700492 linker_error_printf(prog,
493 "%s shader output `%s' %s centroid qualifier, "
494 "but %s shader input %s centroid qualifier\n",
495 producer_stage,
496 output->name,
497 (output->centroid) ? "has" : "lacks",
498 consumer_stage,
499 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700500 return false;
501 }
502
503 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700504 linker_error_printf(prog,
505 "%s shader output `%s' %s invariant qualifier, "
506 "but %s shader input %s invariant qualifier\n",
507 producer_stage,
508 output->name,
509 (output->invariant) ? "has" : "lacks",
510 consumer_stage,
511 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700512 return false;
513 }
514
515 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700516 linker_error_printf(prog,
517 "%s shader output `%s' specifies %s "
518 "interpolation qualifier, "
519 "but %s shader input specifies %s "
520 "interpolation qualifier\n",
521 producer_stage,
522 output->name,
523 output->interpolation_string(),
524 consumer_stage,
525 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700526 return false;
527 }
528 }
529 }
530
531 return true;
532}
533
534
Ian Romanick3fb87872010-07-09 14:09:34 -0700535/**
536 * Populates a shaders symbol table with all global declarations
537 */
538static void
539populate_symbol_table(gl_shader *sh)
540{
541 sh->symbols = new(sh) glsl_symbol_table;
542
543 foreach_list(node, sh->ir) {
544 ir_instruction *const inst = (ir_instruction *) node;
545 ir_variable *var;
546 ir_function *func;
547
548 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700549 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700550 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700551 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700552 }
553 }
554}
555
556
557/**
Ian Romanick31a97862010-07-12 18:48:50 -0700558 * Remap variables referenced in an instruction tree
559 *
560 * This is used when instruction trees are cloned from one shader and placed in
561 * another. These trees will contain references to \c ir_variable nodes that
562 * do not exist in the target shader. This function finds these \c ir_variable
563 * references and replaces the references with matching variables in the target
564 * shader.
565 *
566 * If there is no matching variable in the target shader, a clone of the
567 * \c ir_variable is made and added to the target shader. The new variable is
568 * added to \b both the instruction stream and the symbol table.
569 *
570 * \param inst IR tree that is to be processed.
571 * \param symbols Symbol table containing global scope symbols in the
572 * linked shader.
573 * \param instructions Instruction stream where new variable declarations
574 * should be added.
575 */
576void
Eric Anholt8273bd42010-08-04 12:34:56 -0700577remap_variables(ir_instruction *inst, struct gl_shader *target,
578 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700579{
580 class remap_visitor : public ir_hierarchical_visitor {
581 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700582 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700583 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700584 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700585 this->target = target;
586 this->symbols = target->symbols;
587 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700588 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700589 }
590
591 virtual ir_visitor_status visit(ir_dereference_variable *ir)
592 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700593 if (ir->var->mode == ir_var_temporary) {
594 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
595
596 assert(var != NULL);
597 ir->var = var;
598 return visit_continue;
599 }
600
Ian Romanick31a97862010-07-12 18:48:50 -0700601 ir_variable *const existing =
602 this->symbols->get_variable(ir->var->name);
603 if (existing != NULL)
604 ir->var = existing;
605 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700606 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700607
Eric Anholt001eee52010-11-05 06:11:24 -0700608 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700609 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700610 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700611 }
612
613 return visit_continue;
614 }
615
616 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700617 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700618 glsl_symbol_table *symbols;
619 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700620 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700621 };
622
Eric Anholt8273bd42010-08-04 12:34:56 -0700623 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700624
625 inst->accept(&v);
626}
627
628
629/**
630 * Move non-declarations from one instruction stream to another
631 *
632 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700633 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
Ian Romanick31a97862010-07-12 18:48:50 -0700634 * pointer) for \c last and \c false for \c make_copies on the first
635 * call. Successive calls pass the return value of the previous call for
636 * \c last and \c true for \c make_copies.
637 *
638 * \param instructions Source instruction stream
639 * \param last Instruction after which new instructions should be
640 * inserted in the target instruction stream
641 * \param make_copies Flag selecting whether instructions in \c instructions
642 * should be copied (via \c ir_instruction::clone) into the
643 * target list or moved.
644 *
645 * \return
646 * The new "last" instruction in the target instruction stream. This pointer
647 * is suitable for use as the \c last parameter of a later call to this
648 * function.
649 */
650exec_node *
651move_non_declarations(exec_list *instructions, exec_node *last,
652 bool make_copies, gl_shader *target)
653{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700654 hash_table *temps = NULL;
655
656 if (make_copies)
657 temps = hash_table_ctor(0, hash_table_pointer_hash,
658 hash_table_pointer_compare);
659
Ian Romanick303c99f2010-07-19 12:34:56 -0700660 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700661 ir_instruction *inst = (ir_instruction *) node;
662
Ian Romanick7e2aa912010-07-19 17:12:42 -0700663 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700664 continue;
665
Ian Romanick7e2aa912010-07-19 17:12:42 -0700666 ir_variable *var = inst->as_variable();
667 if ((var != NULL) && (var->mode != ir_var_temporary))
668 continue;
669
670 assert(inst->as_assignment()
671 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700672
673 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700674 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700675
676 if (var != NULL)
677 hash_table_insert(temps, inst, var);
678 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700679 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700680 } else {
681 inst->remove();
682 }
683
684 last->insert_after(inst);
685 last = inst;
686 }
687
Ian Romanick7e2aa912010-07-19 17:12:42 -0700688 if (make_copies)
689 hash_table_dtor(temps);
690
Ian Romanick31a97862010-07-12 18:48:50 -0700691 return last;
692}
693
694/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700695 * Get the function signature for main from a shader
696 */
697static ir_function_signature *
698get_main_function_signature(gl_shader *sh)
699{
700 ir_function *const f = sh->symbols->get_function("main");
701 if (f != NULL) {
702 exec_list void_parameters;
703
704 /* Look for the 'void main()' signature and ensure that it's defined.
705 * This keeps the linker from accidentally pick a shader that just
706 * contains a prototype for main.
707 *
708 * We don't have to check for multiple definitions of main (in multiple
709 * shaders) because that would have already been caught above.
710 */
711 ir_function_signature *sig = f->matching_signature(&void_parameters);
712 if ((sig != NULL) && sig->is_defined) {
713 return sig;
714 }
715 }
716
717 return NULL;
718}
719
720
721/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700722 * Combine a group of shaders for a single stage to generate a linked shader
723 *
724 * \note
725 * If this function is supplied a single shader, it is cloned, and the new
726 * shader is returned.
727 */
728static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800729link_intrastage_shaders(void *mem_ctx,
730 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700731 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700732 struct gl_shader **shader_list,
733 unsigned num_shaders)
734{
Ian Romanick13f782c2010-06-29 18:53:38 -0700735 /* Check that global variables defined in multiple shaders are consistent.
736 */
737 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
738 return NULL;
739
740 /* Check that there is only a single definition of each function signature
741 * across all shaders.
742 */
743 for (unsigned i = 0; i < (num_shaders - 1); i++) {
744 foreach_list(node, shader_list[i]->ir) {
745 ir_function *const f = ((ir_instruction *) node)->as_function();
746
747 if (f == NULL)
748 continue;
749
750 for (unsigned j = i + 1; j < num_shaders; j++) {
751 ir_function *const other =
752 shader_list[j]->symbols->get_function(f->name);
753
754 /* If the other shader has no function (and therefore no function
755 * signatures) with the same name, skip to the next shader.
756 */
757 if (other == NULL)
758 continue;
759
760 foreach_iter (exec_list_iterator, iter, *f) {
761 ir_function_signature *sig =
762 (ir_function_signature *) iter.get();
763
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700764 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700765 continue;
766
767 ir_function_signature *other_sig =
768 other->exact_matching_signature(& sig->parameters);
769
770 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700771 && !other_sig->is_builtin) {
Ian Romanick13f782c2010-06-29 18:53:38 -0700772 linker_error_printf(prog,
773 "function `%s' is multiply defined",
774 f->name);
775 return NULL;
776 }
777 }
778 }
779 }
780 }
781
782 /* Find the shader that defines main, and make a clone of it.
783 *
784 * Starting with the clone, search for undefined references. If one is
785 * found, find the shader that defines it. Clone the reference and add
786 * it to the shader. Repeat until there are no undefined references or
787 * until a reference cannot be resolved.
788 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700789 gl_shader *main = NULL;
790 for (unsigned i = 0; i < num_shaders; i++) {
791 if (get_main_function_signature(shader_list[i]) != NULL) {
792 main = shader_list[i];
793 break;
794 }
795 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700796
Ian Romanick15ce87e2010-07-09 15:28:22 -0700797 if (main == NULL) {
798 linker_error_printf(prog, "%s shader lacks `main'\n",
799 (shader_list[0]->Type == GL_VERTEX_SHADER)
800 ? "vertex" : "fragment");
801 return NULL;
802 }
803
Ian Romanick4a455952010-10-13 15:13:02 -0700804 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700805 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800806 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700807
808 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700809
Ian Romanick31a97862010-07-12 18:48:50 -0700810 /* The a pointer to the main function in the final linked shader (i.e., the
811 * copy of the original shader that contained the main function).
812 */
813 ir_function_signature *const main_sig = get_main_function_signature(linked);
814
815 /* Move any instructions other than variable declarations or function
816 * declarations into main.
817 */
Ian Romanick9303e352010-07-19 12:33:54 -0700818 exec_node *insertion_point =
819 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
820 linked);
821
Ian Romanick31a97862010-07-12 18:48:50 -0700822 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700823 if (shader_list[i] == main)
824 continue;
825
Ian Romanick31a97862010-07-12 18:48:50 -0700826 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700827 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700828 }
829
Ian Romanick13f782c2010-06-29 18:53:38 -0700830 /* Resolve initializers for global variables in the linked shader.
831 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700832 unsigned num_linking_shaders = num_shaders;
833 for (unsigned i = 0; i < num_shaders; i++)
834 num_linking_shaders += shader_list[i]->num_builtins_to_link;
835
836 gl_shader **linking_shaders =
837 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
838
839 memcpy(linking_shaders, shader_list,
840 sizeof(linking_shaders[0]) * num_shaders);
841
842 unsigned idx = num_shaders;
843 for (unsigned i = 0; i < num_shaders; i++) {
844 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
845 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
846 idx += shader_list[i]->num_builtins_to_link;
847 }
848
849 assert(idx == num_linking_shaders);
850
Ian Romanick4a455952010-10-13 15:13:02 -0700851 if (!link_function_calls(prog, linked, linking_shaders,
852 num_linking_shaders)) {
853 ctx->Driver.DeleteShader(ctx, linked);
854 linked = NULL;
855 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700856
857 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -0700858
Ian Romanick3fb87872010-07-09 14:09:34 -0700859 return linked;
860}
861
862
Ian Romanick019a59b2010-06-21 16:10:42 -0700863struct uniform_node {
864 exec_node link;
865 struct gl_uniform *u;
866 unsigned slots;
867};
868
Eric Anholta721abf2010-08-23 10:32:01 -0700869/**
870 * Update the sizes of linked shader uniform arrays to the maximum
871 * array index used.
872 *
873 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
874 *
875 * If one or more elements of an array are active,
876 * GetActiveUniform will return the name of the array in name,
877 * subject to the restrictions listed above. The type of the array
878 * is returned in type. The size parameter contains the highest
879 * array element index used, plus one. The compiler or linker
880 * determines the highest index used. There will be only one
881 * active uniform reported by the GL per uniform array.
882
883 */
884static void
Eric Anholt586b4b52010-09-28 14:32:16 -0700885update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -0700886{
Ian Romanick3322fba2010-10-14 13:28:42 -0700887 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
888 if (prog->_LinkedShaders[i] == NULL)
889 continue;
890
Eric Anholta721abf2010-08-23 10:32:01 -0700891 foreach_list(node, prog->_LinkedShaders[i]->ir) {
892 ir_variable *const var = ((ir_instruction *) node)->as_variable();
893
Eric Anholt586b4b52010-09-28 14:32:16 -0700894 if ((var == NULL) || (var->mode != ir_var_uniform &&
895 var->mode != ir_var_in &&
896 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -0700897 !var->type->is_array())
898 continue;
899
900 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -0700901 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
902 if (prog->_LinkedShaders[j] == NULL)
903 continue;
904
Eric Anholta721abf2010-08-23 10:32:01 -0700905 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
906 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
907 if (!other_var)
908 continue;
909
910 if (strcmp(var->name, other_var->name) == 0 &&
911 other_var->max_array_access > size) {
912 size = other_var->max_array_access;
913 }
914 }
915 }
Eric Anholt586b4b52010-09-28 14:32:16 -0700916
Eric Anholta721abf2010-08-23 10:32:01 -0700917 if (size + 1 != var->type->fields.array->length) {
918 var->type = glsl_type::get_array_instance(var->type->fields.array,
919 size + 1);
920 /* FINISHME: We should update the types of array
921 * dereferences of this variable now.
922 */
923 }
924 }
925 }
926}
927
Eric Anholt45388b52010-08-24 13:51:35 -0700928static void
929add_uniform(void *mem_ctx, exec_list *uniforms, struct hash_table *ht,
930 const char *name, const glsl_type *type, GLenum shader_type,
931 unsigned *next_shader_pos, unsigned *total_uniforms)
932{
933 if (type->is_record()) {
934 for (unsigned int i = 0; i < type->length; i++) {
935 const glsl_type *field_type = type->fields.structure[i].type;
936 char *field_name = talloc_asprintf(mem_ctx, "%s.%s", name,
937 type->fields.structure[i].name);
938
939 add_uniform(mem_ctx, uniforms, ht, field_name, field_type,
940 shader_type, next_shader_pos, total_uniforms);
941 }
942 } else {
943 uniform_node *n = (uniform_node *) hash_table_find(ht, name);
944 unsigned int vec4_slots;
945 const glsl_type *array_elem_type = NULL;
946
947 if (type->is_array()) {
948 array_elem_type = type->fields.array;
949 /* Array of structures. */
950 if (array_elem_type->is_record()) {
951 for (unsigned int i = 0; i < type->length; i++) {
952 char *elem_name = talloc_asprintf(mem_ctx, "%s[%d]", name, i);
953 add_uniform(mem_ctx, uniforms, ht, elem_name, array_elem_type,
954 shader_type, next_shader_pos, total_uniforms);
955 }
956 return;
957 }
958 }
959
960 /* Fix the storage size of samplers at 1 vec4 each. Be sure to pad out
961 * vectors to vec4 slots.
962 */
963 if (type->is_array()) {
964 if (array_elem_type->is_sampler())
965 vec4_slots = type->length;
966 else
967 vec4_slots = type->length * array_elem_type->matrix_columns;
968 } else if (type->is_sampler()) {
969 vec4_slots = 1;
970 } else {
971 vec4_slots = type->matrix_columns;
972 }
973
974 if (n == NULL) {
975 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
976 n->u = (gl_uniform *) calloc(1, sizeof(struct gl_uniform));
977 n->slots = vec4_slots;
978
979 n->u->Name = strdup(name);
Eric Anholt0924ba02010-08-24 14:27:27 -0700980 n->u->Type = type;
Eric Anholt45388b52010-08-24 13:51:35 -0700981 n->u->VertPos = -1;
982 n->u->FragPos = -1;
983 n->u->GeomPos = -1;
984 (*total_uniforms)++;
985
986 hash_table_insert(ht, n, name);
987 uniforms->push_tail(& n->link);
988 }
989
990 switch (shader_type) {
991 case GL_VERTEX_SHADER:
992 n->u->VertPos = *next_shader_pos;
993 break;
994 case GL_FRAGMENT_SHADER:
995 n->u->FragPos = *next_shader_pos;
996 break;
997 case GL_GEOMETRY_SHADER:
998 n->u->GeomPos = *next_shader_pos;
999 break;
1000 }
1001
1002 (*next_shader_pos) += vec4_slots;
1003 }
1004}
1005
Ian Romanickabee16e2010-06-21 16:16:05 -07001006void
Eric Anholt849e1812010-06-30 11:49:17 -07001007assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -07001008{
1009 /* */
1010 exec_list uniforms;
1011 unsigned total_uniforms = 0;
1012 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
1013 hash_table_string_compare);
Eric Anholt45388b52010-08-24 13:51:35 -07001014 void *mem_ctx = talloc_new(NULL);
Ian Romanick019a59b2010-06-21 16:10:42 -07001015
Ian Romanick3322fba2010-10-14 13:28:42 -07001016 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1017 if (prog->_LinkedShaders[i] == NULL)
1018 continue;
1019
Ian Romanick019a59b2010-06-21 16:10:42 -07001020 unsigned next_position = 0;
1021
Eric Anholt16b68b12010-06-30 11:05:43 -07001022 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -07001023 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1024
1025 if ((var == NULL) || (var->mode != ir_var_uniform))
1026 continue;
1027
Eric Anholt45388b52010-08-24 13:51:35 -07001028 if (strncmp(var->name, "gl_", 3) == 0) {
1029 /* At the moment, we don't allocate uniform locations for
1030 * builtin uniforms. It's permitted by spec, and we'll
1031 * likely switch to doing that at some point, but not yet.
Eric Anholt8d61a232010-08-05 16:00:46 -07001032 */
1033 continue;
1034 }
Ian Romanick019a59b2010-06-21 16:10:42 -07001035
Ian Romanick019a59b2010-06-21 16:10:42 -07001036 var->location = next_position;
Eric Anholt45388b52010-08-24 13:51:35 -07001037 add_uniform(mem_ctx, &uniforms, ht, var->name, var->type,
1038 prog->_LinkedShaders[i]->Type,
1039 &next_position, &total_uniforms);
Ian Romanick019a59b2010-06-21 16:10:42 -07001040 }
1041 }
1042
Eric Anholt45388b52010-08-24 13:51:35 -07001043 talloc_free(mem_ctx);
1044
Ian Romanick019a59b2010-06-21 16:10:42 -07001045 gl_uniform_list *ul = (gl_uniform_list *)
1046 calloc(1, sizeof(gl_uniform_list));
1047
1048 ul->Size = total_uniforms;
1049 ul->NumUniforms = total_uniforms;
1050 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
1051
1052 unsigned idx = 0;
1053 uniform_node *next;
1054 for (uniform_node *node = (uniform_node *) uniforms.head
1055 ; node->link.next != NULL
1056 ; node = next) {
1057 next = (uniform_node *) node->link.next;
1058
1059 node->link.remove();
Eric Anholt45388b52010-08-24 13:51:35 -07001060 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform));
1061 idx++;
Ian Romanick019a59b2010-06-21 16:10:42 -07001062
1063 free(node->u);
1064 free(node);
1065 }
1066
1067 hash_table_dtor(ht);
1068
Ian Romanickabee16e2010-06-21 16:16:05 -07001069 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -07001070}
1071
1072
Ian Romanick69846702010-06-22 17:29:19 -07001073/**
1074 * Find a contiguous set of available bits in a bitmask
1075 *
1076 * \param used_mask Bits representing used (1) and unused (0) locations
1077 * \param needed_count Number of contiguous bits needed.
1078 *
1079 * \return
1080 * Base location of the available bits on success or -1 on failure.
1081 */
1082int
1083find_available_slots(unsigned used_mask, unsigned needed_count)
1084{
1085 unsigned needed_mask = (1 << needed_count) - 1;
1086 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1087
1088 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1089 * cannot optimize possibly infinite loops" for the loop below.
1090 */
1091 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1092 return -1;
1093
1094 for (int i = 0; i <= max_bit_to_test; i++) {
1095 if ((needed_mask & ~used_mask) == needed_mask)
1096 return i;
1097
1098 needed_mask <<= 1;
1099 }
1100
1101 return -1;
1102}
1103
1104
1105bool
Eric Anholt849e1812010-06-30 11:49:17 -07001106assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001107{
Ian Romanick9342d262010-06-22 17:41:37 -07001108 /* Mark invalid attribute locations as being used.
1109 */
1110 unsigned used_locations = (max_attribute_index >= 32)
1111 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001112
Eric Anholt16b68b12010-06-30 11:05:43 -07001113 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001114 assert(sh->Type == GL_VERTEX_SHADER);
1115
Ian Romanick69846702010-06-22 17:29:19 -07001116 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001117 *
1118 * 1. Invalidate the location assignments for all vertex shader inputs.
1119 *
1120 * 2. Assign locations for inputs that have user-defined (via
1121 * glBindVertexAttribLocation) locatoins.
1122 *
Ian Romanick69846702010-06-22 17:29:19 -07001123 * 3. Sort the attributes without assigned locations by number of slots
1124 * required in decreasing order. Fragmentation caused by attribute
1125 * locations assigned by the application may prevent large attributes
1126 * from having enough contiguous space.
1127 *
1128 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001129 */
1130
1131 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
1132
Ian Romanick553dcdc2010-06-23 12:14:02 -07001133 if (prog->Attributes != NULL) {
1134 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001135 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -07001136 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001137
Ian Romanick69846702010-06-22 17:29:19 -07001138 /* Note: attributes that occupy multiple slots, such as arrays or
1139 * matrices, may appear in the attrib array multiple times.
1140 */
1141 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001142 continue;
1143
Ian Romanick69846702010-06-22 17:29:19 -07001144 /* From page 61 of the OpenGL 4.0 spec:
1145 *
1146 * "LinkProgram will fail if the attribute bindings assigned by
1147 * BindAttribLocation do not leave not enough space to assign a
1148 * location for an active matrix attribute or an active attribute
1149 * array, both of which require multiple contiguous generic
1150 * attributes."
1151 *
1152 * Previous versions of the spec contain similar language but omit the
1153 * bit about attribute arrays.
1154 *
1155 * Page 61 of the OpenGL 4.0 spec also says:
1156 *
1157 * "It is possible for an application to bind more than one
1158 * attribute name to the same location. This is referred to as
1159 * aliasing. This will only work if only one of the aliased
1160 * attributes is active in the executable program, or if no path
1161 * through the shader consumes more than one attribute of a set
1162 * of attributes aliased to the same location. A link error can
1163 * occur if the linker determines that every path through the
1164 * shader consumes multiple aliased attributes, but
1165 * implementations are not required to generate an error in this
1166 * case."
1167 *
1168 * These two paragraphs are either somewhat contradictory, or I don't
1169 * fully understand one or both of them.
1170 */
1171 /* FINISHME: The code as currently written does not support attribute
1172 * FINISHME: location aliasing (see comment above).
1173 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001174 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -07001175 const unsigned slots = count_attribute_slots(var->type);
1176
1177 /* Mask representing the contiguous slots that will be used by this
1178 * attribute.
1179 */
1180 const unsigned use_mask = (1 << slots) - 1;
1181
1182 /* Generate a link error if the set of bits requested for this
1183 * attribute overlaps any previously allocated bits.
1184 */
1185 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001186 linker_error_printf(prog,
1187 "insufficient contiguous attribute locations "
1188 "available for vertex shader input `%s'",
1189 var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001190 return false;
1191 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001192
1193 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -07001194 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001195 }
1196 }
1197
Ian Romanick69846702010-06-22 17:29:19 -07001198 /* Temporary storage for the set of attributes that need locations assigned.
1199 */
1200 struct temp_attr {
1201 unsigned slots;
1202 ir_variable *var;
1203
1204 /* Used below in the call to qsort. */
1205 static int compare(const void *a, const void *b)
1206 {
1207 const temp_attr *const l = (const temp_attr *) a;
1208 const temp_attr *const r = (const temp_attr *) b;
1209
1210 /* Reversed because we want a descending order sort below. */
1211 return r->slots - l->slots;
1212 }
1213 } to_assign[16];
1214
1215 unsigned num_attr = 0;
1216
Eric Anholt16b68b12010-06-30 11:05:43 -07001217 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001218 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1219
1220 if ((var == NULL) || (var->mode != ir_var_in))
1221 continue;
1222
Ian Romanick68a4fc92010-10-07 17:21:22 -07001223 if (var->explicit_location) {
1224 const unsigned slots = count_attribute_slots(var->type);
1225 const unsigned use_mask = (1 << slots) - 1;
1226 const int attr = var->location - VERT_ATTRIB_GENERIC0;
1227
1228 if ((var->location >= (int)(max_attribute_index + VERT_ATTRIB_GENERIC0))
1229 || (var->location < 0)) {
1230 linker_error_printf(prog,
1231 "invalid explicit location %d specified for "
1232 "`%s'\n",
1233 (var->location < 0) ? var->location : attr,
1234 var->name);
1235 return false;
1236 } else if (var->location >= VERT_ATTRIB_GENERIC0) {
1237 used_locations |= (use_mask << attr);
1238 }
1239 }
1240
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001241 /* The location was explicitly assigned, nothing to do here.
1242 */
1243 if (var->location != -1)
1244 continue;
1245
Ian Romanick69846702010-06-22 17:29:19 -07001246 to_assign[num_attr].slots = count_attribute_slots(var->type);
1247 to_assign[num_attr].var = var;
1248 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001249 }
Ian Romanick69846702010-06-22 17:29:19 -07001250
1251 /* If all of the attributes were assigned locations by the application (or
1252 * are built-in attributes with fixed locations), return early. This should
1253 * be the common case.
1254 */
1255 if (num_attr == 0)
1256 return true;
1257
1258 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1259
Ian Romanick982e3792010-06-29 18:58:20 -07001260 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
1261 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
1262 * to prevent it from being automatically allocated below.
1263 */
Ian Romanickc33e78f2010-08-13 12:30:41 -07001264 find_deref_visitor find("gl_Vertex");
1265 find.run(sh->ir);
1266 if (find.variable_found())
1267 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -07001268
Ian Romanick69846702010-06-22 17:29:19 -07001269 for (unsigned i = 0; i < num_attr; i++) {
1270 /* Mask representing the contiguous slots that will be used by this
1271 * attribute.
1272 */
1273 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1274
1275 int location = find_available_slots(used_locations, to_assign[i].slots);
1276
1277 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001278 linker_error_printf(prog,
1279 "insufficient contiguous attribute locations "
1280 "available for vertex shader input `%s'",
1281 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001282 return false;
1283 }
1284
1285 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1286 used_locations |= (use_mask << location);
1287 }
1288
1289 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001290}
1291
1292
Ian Romanick40e114b2010-08-17 14:55:50 -07001293/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001294 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001295 */
1296void
Ian Romanickcc90e622010-10-19 17:59:10 -07001297demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001298{
1299 foreach_list(node, sh->ir) {
1300 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1301
Ian Romanickcc90e622010-10-19 17:59:10 -07001302 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001303 continue;
1304
Ian Romanickcc90e622010-10-19 17:59:10 -07001305 /* A shader 'in' or 'out' variable is only really an input or output if
1306 * its value is used by other shader stages. This will cause the variable
1307 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001308 */
1309 if (var->location == -1) {
1310 var->mode = ir_var_auto;
1311 }
1312 }
1313}
1314
1315
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001316void
Eric Anholtb7062832010-07-28 13:52:23 -07001317assign_varying_locations(struct gl_shader_program *prog,
1318 gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001319{
1320 /* FINISHME: Set dynamically when geometry shader support is added. */
1321 unsigned output_index = VERT_RESULT_VAR0;
1322 unsigned input_index = FRAG_ATTRIB_VAR0;
1323
1324 /* Operate in a total of three passes.
1325 *
1326 * 1. Assign locations for any matching inputs and outputs.
1327 *
1328 * 2. Mark output variables in the producer that do not have locations as
1329 * not being outputs. This lets the optimizer eliminate them.
1330 *
1331 * 3. Mark input variables in the consumer that do not have locations as
1332 * not being inputs. This lets the optimizer eliminate them.
1333 */
1334
1335 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1336 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1337
Eric Anholt16b68b12010-06-30 11:05:43 -07001338 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001339 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1340
1341 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1342 || (output_var->location != -1))
1343 continue;
1344
1345 ir_variable *const input_var =
1346 consumer->symbols->get_variable(output_var->name);
1347
1348 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1349 continue;
1350
1351 assert(input_var->location == -1);
1352
Ian Romanick0e59b262010-06-23 11:23:01 -07001353 output_var->location = output_index;
1354 input_var->location = input_index;
1355
Ian Romanickdf869d92010-08-30 15:37:44 -07001356 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1357 assert(!output_var->type->is_record());
1358
1359 if (output_var->type->is_array()) {
1360 const unsigned slots = output_var->type->length
1361 * output_var->type->fields.array->matrix_columns;
1362
1363 output_index += slots;
1364 input_index += slots;
1365 } else {
1366 const unsigned slots = output_var->type->matrix_columns;
1367
1368 output_index += slots;
1369 input_index += slots;
1370 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001371 }
1372
Eric Anholt16b68b12010-06-30 11:05:43 -07001373 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001374 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1375
1376 if ((var == NULL) || (var->mode != ir_var_in))
1377 continue;
1378
Eric Anholtb7062832010-07-28 13:52:23 -07001379 if (var->location == -1) {
1380 if (prog->Version <= 120) {
1381 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1382 *
1383 * Only those varying variables used (i.e. read) in
1384 * the fragment shader executable must be written to
1385 * by the vertex shader executable; declaring
1386 * superfluous varying variables in a vertex shader is
1387 * permissible.
1388 *
1389 * We interpret this text as meaning that the VS must
1390 * write the variable for the FS to read it. See
1391 * "glsl1-varying read but not written" in piglit.
1392 */
1393
1394 linker_error_printf(prog, "fragment shader varying %s not written "
1395 "by vertex shader\n.", var->name);
1396 prog->LinkStatus = false;
1397 }
1398
1399 /* An 'in' variable is only really a shader input if its
1400 * value is written by the previous stage.
1401 */
Eric Anholtb7062832010-07-28 13:52:23 -07001402 var->mode = ir_var_auto;
1403 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001404 }
1405}
1406
1407
1408void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001409link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001410{
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001411 void *mem_ctx = talloc_init("temporary linker context");
1412
Ian Romanick832dfa52010-06-17 15:04:20 -07001413 prog->LinkStatus = false;
1414 prog->Validated = false;
1415 prog->_Used = false;
1416
Ian Romanickf36460e2010-06-23 12:07:22 -07001417 if (prog->InfoLog != NULL)
1418 talloc_free(prog->InfoLog);
1419
1420 prog->InfoLog = talloc_strdup(NULL, "");
1421
Ian Romanick832dfa52010-06-17 15:04:20 -07001422 /* Separate the shaders into groups based on their type.
1423 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001424 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001425 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001426 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001427 unsigned num_frag_shaders = 0;
1428
Eric Anholt16b68b12010-06-30 11:05:43 -07001429 vert_shader_list = (struct gl_shader **)
1430 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001431 frag_shader_list = &vert_shader_list[prog->NumShaders];
1432
Ian Romanick25f51d32010-07-16 15:51:50 -07001433 unsigned min_version = UINT_MAX;
1434 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001435 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001436 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1437 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1438
Ian Romanick832dfa52010-06-17 15:04:20 -07001439 switch (prog->Shaders[i]->Type) {
1440 case GL_VERTEX_SHADER:
1441 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1442 num_vert_shaders++;
1443 break;
1444 case GL_FRAGMENT_SHADER:
1445 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1446 num_frag_shaders++;
1447 break;
1448 case GL_GEOMETRY_SHADER:
1449 /* FINISHME: Support geometry shaders. */
1450 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1451 break;
1452 }
1453 }
1454
Ian Romanick25f51d32010-07-16 15:51:50 -07001455 /* Previous to GLSL version 1.30, different compilation units could mix and
1456 * match shading language versions. With GLSL 1.30 and later, the versions
1457 * of all shaders must match.
1458 */
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001459 assert(min_version >= 100);
Ian Romanick25f51d32010-07-16 15:51:50 -07001460 assert(max_version <= 130);
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001461 if ((max_version >= 130 || min_version == 100)
1462 && min_version != max_version) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001463 linker_error_printf(prog, "all shaders must use same shading "
1464 "language version\n");
1465 goto done;
1466 }
1467
1468 prog->Version = max_version;
1469
Ian Romanick3322fba2010-10-14 13:28:42 -07001470 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1471 if (prog->_LinkedShaders[i] != NULL)
1472 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1473
1474 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07001475 }
1476
Ian Romanickcd6764e2010-07-16 16:00:07 -07001477 /* Link all shaders for a particular stage and validate the result.
1478 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001479 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001480 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001481 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1482 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001483
1484 if (sh == NULL)
1485 goto done;
1486
1487 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001488 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001489
Ian Romanick3322fba2010-10-14 13:28:42 -07001490 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1491 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001492 }
1493
1494 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001495 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001496 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1497 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001498
1499 if (sh == NULL)
1500 goto done;
1501
1502 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001503 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001504
Ian Romanick3322fba2010-10-14 13:28:42 -07001505 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1506 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001507 }
1508
Ian Romanick3ed850e2010-06-23 12:18:21 -07001509 /* Here begins the inter-stage linking phase. Some initial validation is
1510 * performed, then locations are assigned for uniforms, attributes, and
1511 * varyings.
1512 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001513 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07001514 unsigned prev;
1515
1516 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1517 if (prog->_LinkedShaders[prev] != NULL)
1518 break;
1519 }
1520
Ian Romanick37101922010-06-18 19:02:10 -07001521 /* Validate the inputs of each stage with the output of the preceeding
1522 * stage.
1523 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001524 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1525 if (prog->_LinkedShaders[i] == NULL)
1526 continue;
1527
Ian Romanickf36460e2010-06-23 12:07:22 -07001528 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07001529 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07001530 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001531 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001532
1533 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07001534 }
1535
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001536 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001537 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001538
Eric Anholt2f4fe152010-08-10 13:06:49 -07001539 /* Do common optimization before assigning storage for attributes,
1540 * uniforms, and varyings. Later optimization could possibly make
1541 * some of that unused.
1542 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001543 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1544 if (prog->_LinkedShaders[i] == NULL)
1545 continue;
1546
Luca Barbierie591c462010-09-05 22:29:58 +02001547 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
Eric Anholt2f4fe152010-08-10 13:06:49 -07001548 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001549 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001550
Eric Anholt586b4b52010-09-28 14:32:16 -07001551 update_array_sizes(prog);
1552
Ian Romanickabee16e2010-06-21 16:16:05 -07001553 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001554
Ian Romanick3322fba2010-10-14 13:28:42 -07001555 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
Ian Romanick9342d262010-06-22 17:41:37 -07001556 /* FINISHME: The value of the max_attribute_index parameter is
1557 * FINISHME: implementation dependent based on the value of
1558 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1559 * FINISHME: at least 16, so hardcode 16 for now.
1560 */
Ian Romanick4b5489d2010-10-07 17:20:15 -07001561 if (!assign_attribute_locations(prog, 16)) {
1562 prog->LinkStatus = false;
Ian Romanick69846702010-06-22 17:29:19 -07001563 goto done;
Ian Romanick4b5489d2010-10-07 17:20:15 -07001564 }
Ian Romanick40e114b2010-08-17 14:55:50 -07001565 }
1566
Ian Romanick3322fba2010-10-14 13:28:42 -07001567 unsigned prev;
1568 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1569 if (prog->_LinkedShaders[prev] != NULL)
1570 break;
1571 }
1572
1573 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1574 if (prog->_LinkedShaders[i] == NULL)
1575 continue;
1576
Eric Anholtb7062832010-07-28 13:52:23 -07001577 assign_varying_locations(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07001578 prog->_LinkedShaders[prev],
Ian Romanick0e59b262010-06-23 11:23:01 -07001579 prog->_LinkedShaders[i]);
Ian Romanick3322fba2010-10-14 13:28:42 -07001580 prev = i;
1581 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001582
Ian Romanickcc90e622010-10-19 17:59:10 -07001583 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1584 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
1585 ir_var_out);
1586 }
1587
1588 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
1589 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
1590
1591 demote_shader_inputs_and_outputs(sh, ir_var_in);
1592 demote_shader_inputs_and_outputs(sh, ir_var_inout);
1593 demote_shader_inputs_and_outputs(sh, ir_var_out);
1594 }
1595
1596 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
1597 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1598
1599 demote_shader_inputs_and_outputs(sh, ir_var_in);
1600 }
1601
Ian Romanick13e10e42010-06-21 12:03:24 -07001602 /* FINISHME: Assign fragment shader output locations. */
1603
Ian Romanick832dfa52010-06-17 15:04:20 -07001604done:
1605 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001606
1607 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1608 if (prog->_LinkedShaders[i] == NULL)
1609 continue;
1610
1611 /* Retain any live IR, but trash the rest. */
1612 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
1613 }
1614
1615 talloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07001616}