blob: d8c42ac462427397648a7707a16721222658aade [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))) {
Ian Romanick6f539212010-12-07 18:30:33 -0800363 if (existing->type->length == 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700364 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800365 existing->max_array_access =
366 MAX2(existing->max_array_access,
367 var->max_array_access);
368 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700369 } else {
370 linker_error_printf(prog, "%s `%s' declared as type "
371 "`%s' and type `%s'\n",
372 mode_string(var),
373 var->name, var->type->name,
374 existing->type->name);
375 return false;
376 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700377 }
378
Ian Romanick68a4fc92010-10-07 17:21:22 -0700379 if (var->explicit_location) {
380 if (existing->explicit_location
381 && (var->location != existing->location)) {
382 linker_error_printf(prog, "explicit locations for %s "
383 "`%s' have differing values\n",
384 mode_string(var), var->name);
385 return false;
386 }
387
388 existing->location = var->location;
389 existing->explicit_location = true;
390 }
391
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700392 /* FINISHME: Handle non-constant initializers.
393 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700394 if (var->constant_value != NULL) {
395 if (existing->constant_value != NULL) {
396 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700397 linker_error_printf(prog, "initializers for %s "
Ian Romanickf36460e2010-06-23 12:07:22 -0700398 "`%s' have differing values\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700399 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700400 return false;
401 }
402 } else
403 /* If the first-seen instance of a particular uniform did not
404 * have an initializer but a later instance does, copy the
405 * initializer to the version stored in the symbol table.
406 */
Ian Romanickde415b72010-07-14 13:22:12 -0700407 /* FINISHME: This is wrong. The constant_value field should
408 * FINISHME: not be modified! Imagine a case where a shader
409 * FINISHME: without an initializer is linked in two different
410 * FINISHME: programs with shaders that have differing
411 * FINISHME: initializers. Linking with the first will
412 * FINISHME: modify the shader, and linking with the second
413 * FINISHME: will fail.
414 */
Eric Anholt8273bd42010-08-04 12:34:56 -0700415 existing->constant_value =
416 var->constant_value->clone(talloc_parent(existing), NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700417 }
Chad Versace7528f142010-11-17 14:34:38 -0800418
419 if (existing->invariant != var->invariant) {
420 linker_error_printf(prog, "declarations for %s `%s' have "
421 "mismatching invariant qualifiers\n",
422 mode_string(var), var->name);
423 return false;
424 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700425 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700426 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700427 }
428 }
429
430 return true;
431}
432
433
Ian Romanick37101922010-06-18 19:02:10 -0700434/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700435 * Perform validation of uniforms used across multiple shader stages
436 */
437bool
438cross_validate_uniforms(struct gl_shader_program *prog)
439{
440 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700441 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700442}
443
444
445/**
Ian Romanick37101922010-06-18 19:02:10 -0700446 * Validate that outputs from one stage match inputs of another
447 */
448bool
Eric Anholt849e1812010-06-30 11:49:17 -0700449cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700450 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700451{
452 glsl_symbol_table parameters;
453 /* FINISHME: Figure these out dynamically. */
454 const char *const producer_stage = "vertex";
455 const char *const consumer_stage = "fragment";
456
457 /* Find all shader outputs in the "producer" stage.
458 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700459 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700460 ir_variable *const var = ((ir_instruction *) node)->as_variable();
461
462 /* FINISHME: For geometry shaders, this should also look for inout
463 * FINISHME: variables.
464 */
465 if ((var == NULL) || (var->mode != ir_var_out))
466 continue;
467
Eric Anholt001eee52010-11-05 06:11:24 -0700468 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700469 }
470
471
472 /* Find all shader inputs in the "consumer" stage. Any variables that have
473 * matching outputs already in the symbol table must have the same type and
474 * qualifiers.
475 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700476 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700477 ir_variable *const input = ((ir_instruction *) node)->as_variable();
478
479 /* FINISHME: For geometry shaders, this should also look for inout
480 * FINISHME: variables.
481 */
482 if ((input == NULL) || (input->mode != ir_var_in))
483 continue;
484
485 ir_variable *const output = parameters.get_variable(input->name);
486 if (output != NULL) {
487 /* Check that the types match between stages.
488 */
489 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800490 /* There is a bit of a special case for gl_TexCoord. This
491 * built-in is unsized by default. Appliations that variable
492 * access it must redeclare it with a size. There is some
493 * language in the GLSL spec that implies the fragment shader
494 * and vertex shader do not have to agree on this size. Other
495 * driver behave this way, and one or two applications seem to
496 * rely on it.
497 *
498 * Neither declaration needs to be modified here because the array
499 * sizes are fixed later when update_array_sizes is called.
500 *
501 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
502 *
503 * "Unlike user-defined varying variables, the built-in
504 * varying variables don't have a strict one-to-one
505 * correspondence between the vertex language and the
506 * fragment language."
507 */
508 if (!output->type->is_array()
509 || (strncmp("gl_", output->name, 3) != 0)) {
510 linker_error_printf(prog,
511 "%s shader output `%s' declared as "
512 "type `%s', but %s shader input declared "
513 "as type `%s'\n",
514 producer_stage, output->name,
515 output->type->name,
516 consumer_stage, input->type->name);
517 return false;
518 }
Ian Romanick37101922010-06-18 19:02:10 -0700519 }
520
521 /* Check that all of the qualifiers match between stages.
522 */
523 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700524 linker_error_printf(prog,
525 "%s shader output `%s' %s centroid qualifier, "
526 "but %s shader input %s centroid qualifier\n",
527 producer_stage,
528 output->name,
529 (output->centroid) ? "has" : "lacks",
530 consumer_stage,
531 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700532 return false;
533 }
534
535 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700536 linker_error_printf(prog,
537 "%s shader output `%s' %s invariant qualifier, "
538 "but %s shader input %s invariant qualifier\n",
539 producer_stage,
540 output->name,
541 (output->invariant) ? "has" : "lacks",
542 consumer_stage,
543 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700544 return false;
545 }
546
547 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700548 linker_error_printf(prog,
549 "%s shader output `%s' specifies %s "
550 "interpolation qualifier, "
551 "but %s shader input specifies %s "
552 "interpolation qualifier\n",
553 producer_stage,
554 output->name,
555 output->interpolation_string(),
556 consumer_stage,
557 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700558 return false;
559 }
560 }
561 }
562
563 return true;
564}
565
566
Ian Romanick3fb87872010-07-09 14:09:34 -0700567/**
568 * Populates a shaders symbol table with all global declarations
569 */
570static void
571populate_symbol_table(gl_shader *sh)
572{
573 sh->symbols = new(sh) glsl_symbol_table;
574
575 foreach_list(node, sh->ir) {
576 ir_instruction *const inst = (ir_instruction *) node;
577 ir_variable *var;
578 ir_function *func;
579
580 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700581 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700582 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700583 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700584 }
585 }
586}
587
588
589/**
Ian Romanick31a97862010-07-12 18:48:50 -0700590 * Remap variables referenced in an instruction tree
591 *
592 * This is used when instruction trees are cloned from one shader and placed in
593 * another. These trees will contain references to \c ir_variable nodes that
594 * do not exist in the target shader. This function finds these \c ir_variable
595 * references and replaces the references with matching variables in the target
596 * shader.
597 *
598 * If there is no matching variable in the target shader, a clone of the
599 * \c ir_variable is made and added to the target shader. The new variable is
600 * added to \b both the instruction stream and the symbol table.
601 *
602 * \param inst IR tree that is to be processed.
603 * \param symbols Symbol table containing global scope symbols in the
604 * linked shader.
605 * \param instructions Instruction stream where new variable declarations
606 * should be added.
607 */
608void
Eric Anholt8273bd42010-08-04 12:34:56 -0700609remap_variables(ir_instruction *inst, struct gl_shader *target,
610 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700611{
612 class remap_visitor : public ir_hierarchical_visitor {
613 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700614 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700615 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700616 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700617 this->target = target;
618 this->symbols = target->symbols;
619 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700620 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700621 }
622
623 virtual ir_visitor_status visit(ir_dereference_variable *ir)
624 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700625 if (ir->var->mode == ir_var_temporary) {
626 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
627
628 assert(var != NULL);
629 ir->var = var;
630 return visit_continue;
631 }
632
Ian Romanick31a97862010-07-12 18:48:50 -0700633 ir_variable *const existing =
634 this->symbols->get_variable(ir->var->name);
635 if (existing != NULL)
636 ir->var = existing;
637 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700638 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700639
Eric Anholt001eee52010-11-05 06:11:24 -0700640 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700641 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700642 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700643 }
644
645 return visit_continue;
646 }
647
648 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700649 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700650 glsl_symbol_table *symbols;
651 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700652 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700653 };
654
Eric Anholt8273bd42010-08-04 12:34:56 -0700655 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700656
657 inst->accept(&v);
658}
659
660
661/**
662 * Move non-declarations from one instruction stream to another
663 *
664 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700665 * 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 -0700666 * pointer) for \c last and \c false for \c make_copies on the first
667 * call. Successive calls pass the return value of the previous call for
668 * \c last and \c true for \c make_copies.
669 *
670 * \param instructions Source instruction stream
671 * \param last Instruction after which new instructions should be
672 * inserted in the target instruction stream
673 * \param make_copies Flag selecting whether instructions in \c instructions
674 * should be copied (via \c ir_instruction::clone) into the
675 * target list or moved.
676 *
677 * \return
678 * The new "last" instruction in the target instruction stream. This pointer
679 * is suitable for use as the \c last parameter of a later call to this
680 * function.
681 */
682exec_node *
683move_non_declarations(exec_list *instructions, exec_node *last,
684 bool make_copies, gl_shader *target)
685{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700686 hash_table *temps = NULL;
687
688 if (make_copies)
689 temps = hash_table_ctor(0, hash_table_pointer_hash,
690 hash_table_pointer_compare);
691
Ian Romanick303c99f2010-07-19 12:34:56 -0700692 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700693 ir_instruction *inst = (ir_instruction *) node;
694
Ian Romanick7e2aa912010-07-19 17:12:42 -0700695 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700696 continue;
697
Ian Romanick7e2aa912010-07-19 17:12:42 -0700698 ir_variable *var = inst->as_variable();
699 if ((var != NULL) && (var->mode != ir_var_temporary))
700 continue;
701
702 assert(inst->as_assignment()
703 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700704
705 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700706 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700707
708 if (var != NULL)
709 hash_table_insert(temps, inst, var);
710 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700711 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700712 } else {
713 inst->remove();
714 }
715
716 last->insert_after(inst);
717 last = inst;
718 }
719
Ian Romanick7e2aa912010-07-19 17:12:42 -0700720 if (make_copies)
721 hash_table_dtor(temps);
722
Ian Romanick31a97862010-07-12 18:48:50 -0700723 return last;
724}
725
726/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700727 * Get the function signature for main from a shader
728 */
729static ir_function_signature *
730get_main_function_signature(gl_shader *sh)
731{
732 ir_function *const f = sh->symbols->get_function("main");
733 if (f != NULL) {
734 exec_list void_parameters;
735
736 /* Look for the 'void main()' signature and ensure that it's defined.
737 * This keeps the linker from accidentally pick a shader that just
738 * contains a prototype for main.
739 *
740 * We don't have to check for multiple definitions of main (in multiple
741 * shaders) because that would have already been caught above.
742 */
743 ir_function_signature *sig = f->matching_signature(&void_parameters);
744 if ((sig != NULL) && sig->is_defined) {
745 return sig;
746 }
747 }
748
749 return NULL;
750}
751
752
753/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700754 * Combine a group of shaders for a single stage to generate a linked shader
755 *
756 * \note
757 * If this function is supplied a single shader, it is cloned, and the new
758 * shader is returned.
759 */
760static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800761link_intrastage_shaders(void *mem_ctx,
762 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700763 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700764 struct gl_shader **shader_list,
765 unsigned num_shaders)
766{
Ian Romanick13f782c2010-06-29 18:53:38 -0700767 /* Check that global variables defined in multiple shaders are consistent.
768 */
769 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
770 return NULL;
771
772 /* Check that there is only a single definition of each function signature
773 * across all shaders.
774 */
775 for (unsigned i = 0; i < (num_shaders - 1); i++) {
776 foreach_list(node, shader_list[i]->ir) {
777 ir_function *const f = ((ir_instruction *) node)->as_function();
778
779 if (f == NULL)
780 continue;
781
782 for (unsigned j = i + 1; j < num_shaders; j++) {
783 ir_function *const other =
784 shader_list[j]->symbols->get_function(f->name);
785
786 /* If the other shader has no function (and therefore no function
787 * signatures) with the same name, skip to the next shader.
788 */
789 if (other == NULL)
790 continue;
791
792 foreach_iter (exec_list_iterator, iter, *f) {
793 ir_function_signature *sig =
794 (ir_function_signature *) iter.get();
795
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700796 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700797 continue;
798
799 ir_function_signature *other_sig =
800 other->exact_matching_signature(& sig->parameters);
801
802 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700803 && !other_sig->is_builtin) {
Ian Romanick13f782c2010-06-29 18:53:38 -0700804 linker_error_printf(prog,
805 "function `%s' is multiply defined",
806 f->name);
807 return NULL;
808 }
809 }
810 }
811 }
812 }
813
814 /* Find the shader that defines main, and make a clone of it.
815 *
816 * Starting with the clone, search for undefined references. If one is
817 * found, find the shader that defines it. Clone the reference and add
818 * it to the shader. Repeat until there are no undefined references or
819 * until a reference cannot be resolved.
820 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700821 gl_shader *main = NULL;
822 for (unsigned i = 0; i < num_shaders; i++) {
823 if (get_main_function_signature(shader_list[i]) != NULL) {
824 main = shader_list[i];
825 break;
826 }
827 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700828
Ian Romanick15ce87e2010-07-09 15:28:22 -0700829 if (main == NULL) {
830 linker_error_printf(prog, "%s shader lacks `main'\n",
831 (shader_list[0]->Type == GL_VERTEX_SHADER)
832 ? "vertex" : "fragment");
833 return NULL;
834 }
835
Ian Romanick4a455952010-10-13 15:13:02 -0700836 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700837 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800838 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700839
840 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700841
Ian Romanick31a97862010-07-12 18:48:50 -0700842 /* The a pointer to the main function in the final linked shader (i.e., the
843 * copy of the original shader that contained the main function).
844 */
845 ir_function_signature *const main_sig = get_main_function_signature(linked);
846
847 /* Move any instructions other than variable declarations or function
848 * declarations into main.
849 */
Ian Romanick9303e352010-07-19 12:33:54 -0700850 exec_node *insertion_point =
851 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
852 linked);
853
Ian Romanick31a97862010-07-12 18:48:50 -0700854 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700855 if (shader_list[i] == main)
856 continue;
857
Ian Romanick31a97862010-07-12 18:48:50 -0700858 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700859 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700860 }
861
Ian Romanick13f782c2010-06-29 18:53:38 -0700862 /* Resolve initializers for global variables in the linked shader.
863 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700864 unsigned num_linking_shaders = num_shaders;
865 for (unsigned i = 0; i < num_shaders; i++)
866 num_linking_shaders += shader_list[i]->num_builtins_to_link;
867
868 gl_shader **linking_shaders =
869 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
870
871 memcpy(linking_shaders, shader_list,
872 sizeof(linking_shaders[0]) * num_shaders);
873
874 unsigned idx = num_shaders;
875 for (unsigned i = 0; i < num_shaders; i++) {
876 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
877 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
878 idx += shader_list[i]->num_builtins_to_link;
879 }
880
881 assert(idx == num_linking_shaders);
882
Ian Romanick4a455952010-10-13 15:13:02 -0700883 if (!link_function_calls(prog, linked, linking_shaders,
884 num_linking_shaders)) {
885 ctx->Driver.DeleteShader(ctx, linked);
886 linked = NULL;
887 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700888
889 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -0700890
Ian Romanick6f539212010-12-07 18:30:33 -0800891 /* Make a pass over all global variables to ensure that arrays with
892 * unspecified sizes have a size specified. The size is inferred from the
893 * max_array_access field.
894 */
Ian Romanick002cd2c2010-12-07 19:00:44 -0800895 if (linked != NULL) {
896 foreach_list(node, linked->ir) {
897 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick6f539212010-12-07 18:30:33 -0800898
Ian Romanick002cd2c2010-12-07 19:00:44 -0800899 if (var == NULL)
900 continue;
Ian Romanick6f539212010-12-07 18:30:33 -0800901
Ian Romanick002cd2c2010-12-07 19:00:44 -0800902 if ((var->mode != ir_var_auto) && (var->mode != ir_var_temporary))
903 continue;
Ian Romanick6f539212010-12-07 18:30:33 -0800904
Ian Romanick002cd2c2010-12-07 19:00:44 -0800905 if (!var->type->is_array() || (var->type->length != 0))
906 continue;
Ian Romanick6f539212010-12-07 18:30:33 -0800907
Ian Romanick002cd2c2010-12-07 19:00:44 -0800908 const glsl_type *type =
909 glsl_type::get_array_instance(var->type->fields.array,
910 var->max_array_access);
911
912 assert(type != NULL);
913 var->type = type;
914 }
Ian Romanick6f539212010-12-07 18:30:33 -0800915 }
916
Ian Romanick3fb87872010-07-09 14:09:34 -0700917 return linked;
918}
919
920
Ian Romanick019a59b2010-06-21 16:10:42 -0700921struct uniform_node {
922 exec_node link;
923 struct gl_uniform *u;
924 unsigned slots;
925};
926
Eric Anholta721abf2010-08-23 10:32:01 -0700927/**
928 * Update the sizes of linked shader uniform arrays to the maximum
929 * array index used.
930 *
931 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
932 *
933 * If one or more elements of an array are active,
934 * GetActiveUniform will return the name of the array in name,
935 * subject to the restrictions listed above. The type of the array
936 * is returned in type. The size parameter contains the highest
937 * array element index used, plus one. The compiler or linker
938 * determines the highest index used. There will be only one
939 * active uniform reported by the GL per uniform array.
940
941 */
942static void
Eric Anholt586b4b52010-09-28 14:32:16 -0700943update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -0700944{
Ian Romanick3322fba2010-10-14 13:28:42 -0700945 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
946 if (prog->_LinkedShaders[i] == NULL)
947 continue;
948
Eric Anholta721abf2010-08-23 10:32:01 -0700949 foreach_list(node, prog->_LinkedShaders[i]->ir) {
950 ir_variable *const var = ((ir_instruction *) node)->as_variable();
951
Eric Anholt586b4b52010-09-28 14:32:16 -0700952 if ((var == NULL) || (var->mode != ir_var_uniform &&
953 var->mode != ir_var_in &&
954 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -0700955 !var->type->is_array())
956 continue;
957
958 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -0700959 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
960 if (prog->_LinkedShaders[j] == NULL)
961 continue;
962
Eric Anholta721abf2010-08-23 10:32:01 -0700963 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
964 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
965 if (!other_var)
966 continue;
967
968 if (strcmp(var->name, other_var->name) == 0 &&
969 other_var->max_array_access > size) {
970 size = other_var->max_array_access;
971 }
972 }
973 }
Eric Anholt586b4b52010-09-28 14:32:16 -0700974
Eric Anholta721abf2010-08-23 10:32:01 -0700975 if (size + 1 != var->type->fields.array->length) {
976 var->type = glsl_type::get_array_instance(var->type->fields.array,
977 size + 1);
978 /* FINISHME: We should update the types of array
979 * dereferences of this variable now.
980 */
981 }
982 }
983 }
984}
985
Eric Anholt45388b52010-08-24 13:51:35 -0700986static void
987add_uniform(void *mem_ctx, exec_list *uniforms, struct hash_table *ht,
988 const char *name, const glsl_type *type, GLenum shader_type,
989 unsigned *next_shader_pos, unsigned *total_uniforms)
990{
991 if (type->is_record()) {
992 for (unsigned int i = 0; i < type->length; i++) {
993 const glsl_type *field_type = type->fields.structure[i].type;
994 char *field_name = talloc_asprintf(mem_ctx, "%s.%s", name,
995 type->fields.structure[i].name);
996
997 add_uniform(mem_ctx, uniforms, ht, field_name, field_type,
998 shader_type, next_shader_pos, total_uniforms);
999 }
1000 } else {
1001 uniform_node *n = (uniform_node *) hash_table_find(ht, name);
1002 unsigned int vec4_slots;
1003 const glsl_type *array_elem_type = NULL;
1004
1005 if (type->is_array()) {
1006 array_elem_type = type->fields.array;
1007 /* Array of structures. */
1008 if (array_elem_type->is_record()) {
1009 for (unsigned int i = 0; i < type->length; i++) {
1010 char *elem_name = talloc_asprintf(mem_ctx, "%s[%d]", name, i);
1011 add_uniform(mem_ctx, uniforms, ht, elem_name, array_elem_type,
1012 shader_type, next_shader_pos, total_uniforms);
1013 }
1014 return;
1015 }
1016 }
1017
1018 /* Fix the storage size of samplers at 1 vec4 each. Be sure to pad out
1019 * vectors to vec4 slots.
1020 */
1021 if (type->is_array()) {
1022 if (array_elem_type->is_sampler())
1023 vec4_slots = type->length;
1024 else
1025 vec4_slots = type->length * array_elem_type->matrix_columns;
1026 } else if (type->is_sampler()) {
1027 vec4_slots = 1;
1028 } else {
1029 vec4_slots = type->matrix_columns;
1030 }
1031
1032 if (n == NULL) {
1033 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
1034 n->u = (gl_uniform *) calloc(1, sizeof(struct gl_uniform));
1035 n->slots = vec4_slots;
1036
1037 n->u->Name = strdup(name);
Eric Anholt0924ba02010-08-24 14:27:27 -07001038 n->u->Type = type;
Eric Anholt45388b52010-08-24 13:51:35 -07001039 n->u->VertPos = -1;
1040 n->u->FragPos = -1;
1041 n->u->GeomPos = -1;
1042 (*total_uniforms)++;
1043
1044 hash_table_insert(ht, n, name);
1045 uniforms->push_tail(& n->link);
1046 }
1047
1048 switch (shader_type) {
1049 case GL_VERTEX_SHADER:
1050 n->u->VertPos = *next_shader_pos;
1051 break;
1052 case GL_FRAGMENT_SHADER:
1053 n->u->FragPos = *next_shader_pos;
1054 break;
1055 case GL_GEOMETRY_SHADER:
1056 n->u->GeomPos = *next_shader_pos;
1057 break;
1058 }
1059
1060 (*next_shader_pos) += vec4_slots;
1061 }
1062}
1063
Ian Romanickabee16e2010-06-21 16:16:05 -07001064void
Eric Anholt849e1812010-06-30 11:49:17 -07001065assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -07001066{
1067 /* */
1068 exec_list uniforms;
1069 unsigned total_uniforms = 0;
1070 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
1071 hash_table_string_compare);
Eric Anholt45388b52010-08-24 13:51:35 -07001072 void *mem_ctx = talloc_new(NULL);
Ian Romanick019a59b2010-06-21 16:10:42 -07001073
Ian Romanick3322fba2010-10-14 13:28:42 -07001074 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1075 if (prog->_LinkedShaders[i] == NULL)
1076 continue;
1077
Ian Romanick019a59b2010-06-21 16:10:42 -07001078 unsigned next_position = 0;
1079
Eric Anholt16b68b12010-06-30 11:05:43 -07001080 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -07001081 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1082
1083 if ((var == NULL) || (var->mode != ir_var_uniform))
1084 continue;
1085
Eric Anholt45388b52010-08-24 13:51:35 -07001086 if (strncmp(var->name, "gl_", 3) == 0) {
1087 /* At the moment, we don't allocate uniform locations for
1088 * builtin uniforms. It's permitted by spec, and we'll
1089 * likely switch to doing that at some point, but not yet.
Eric Anholt8d61a232010-08-05 16:00:46 -07001090 */
1091 continue;
1092 }
Ian Romanick019a59b2010-06-21 16:10:42 -07001093
Ian Romanick019a59b2010-06-21 16:10:42 -07001094 var->location = next_position;
Eric Anholt45388b52010-08-24 13:51:35 -07001095 add_uniform(mem_ctx, &uniforms, ht, var->name, var->type,
1096 prog->_LinkedShaders[i]->Type,
1097 &next_position, &total_uniforms);
Ian Romanick019a59b2010-06-21 16:10:42 -07001098 }
1099 }
1100
Eric Anholt45388b52010-08-24 13:51:35 -07001101 talloc_free(mem_ctx);
1102
Ian Romanick019a59b2010-06-21 16:10:42 -07001103 gl_uniform_list *ul = (gl_uniform_list *)
1104 calloc(1, sizeof(gl_uniform_list));
1105
1106 ul->Size = total_uniforms;
1107 ul->NumUniforms = total_uniforms;
1108 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
1109
1110 unsigned idx = 0;
1111 uniform_node *next;
1112 for (uniform_node *node = (uniform_node *) uniforms.head
1113 ; node->link.next != NULL
1114 ; node = next) {
1115 next = (uniform_node *) node->link.next;
1116
1117 node->link.remove();
Eric Anholt45388b52010-08-24 13:51:35 -07001118 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform));
1119 idx++;
Ian Romanick019a59b2010-06-21 16:10:42 -07001120
1121 free(node->u);
1122 free(node);
1123 }
1124
1125 hash_table_dtor(ht);
1126
Ian Romanickabee16e2010-06-21 16:16:05 -07001127 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -07001128}
1129
1130
Ian Romanick69846702010-06-22 17:29:19 -07001131/**
1132 * Find a contiguous set of available bits in a bitmask
1133 *
1134 * \param used_mask Bits representing used (1) and unused (0) locations
1135 * \param needed_count Number of contiguous bits needed.
1136 *
1137 * \return
1138 * Base location of the available bits on success or -1 on failure.
1139 */
1140int
1141find_available_slots(unsigned used_mask, unsigned needed_count)
1142{
1143 unsigned needed_mask = (1 << needed_count) - 1;
1144 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1145
1146 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1147 * cannot optimize possibly infinite loops" for the loop below.
1148 */
1149 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1150 return -1;
1151
1152 for (int i = 0; i <= max_bit_to_test; i++) {
1153 if ((needed_mask & ~used_mask) == needed_mask)
1154 return i;
1155
1156 needed_mask <<= 1;
1157 }
1158
1159 return -1;
1160}
1161
1162
1163bool
Eric Anholt849e1812010-06-30 11:49:17 -07001164assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001165{
Ian Romanick9342d262010-06-22 17:41:37 -07001166 /* Mark invalid attribute locations as being used.
1167 */
1168 unsigned used_locations = (max_attribute_index >= 32)
1169 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001170
Eric Anholt16b68b12010-06-30 11:05:43 -07001171 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001172 assert(sh->Type == GL_VERTEX_SHADER);
1173
Ian Romanick69846702010-06-22 17:29:19 -07001174 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001175 *
1176 * 1. Invalidate the location assignments for all vertex shader inputs.
1177 *
1178 * 2. Assign locations for inputs that have user-defined (via
1179 * glBindVertexAttribLocation) locatoins.
1180 *
Ian Romanick69846702010-06-22 17:29:19 -07001181 * 3. Sort the attributes without assigned locations by number of slots
1182 * required in decreasing order. Fragmentation caused by attribute
1183 * locations assigned by the application may prevent large attributes
1184 * from having enough contiguous space.
1185 *
1186 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001187 */
1188
1189 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
1190
Ian Romanick553dcdc2010-06-23 12:14:02 -07001191 if (prog->Attributes != NULL) {
1192 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001193 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -07001194 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001195
Ian Romanick69846702010-06-22 17:29:19 -07001196 /* Note: attributes that occupy multiple slots, such as arrays or
1197 * matrices, may appear in the attrib array multiple times.
1198 */
1199 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001200 continue;
1201
Ian Romanick69846702010-06-22 17:29:19 -07001202 /* From page 61 of the OpenGL 4.0 spec:
1203 *
1204 * "LinkProgram will fail if the attribute bindings assigned by
1205 * BindAttribLocation do not leave not enough space to assign a
1206 * location for an active matrix attribute or an active attribute
1207 * array, both of which require multiple contiguous generic
1208 * attributes."
1209 *
1210 * Previous versions of the spec contain similar language but omit the
1211 * bit about attribute arrays.
1212 *
1213 * Page 61 of the OpenGL 4.0 spec also says:
1214 *
1215 * "It is possible for an application to bind more than one
1216 * attribute name to the same location. This is referred to as
1217 * aliasing. This will only work if only one of the aliased
1218 * attributes is active in the executable program, or if no path
1219 * through the shader consumes more than one attribute of a set
1220 * of attributes aliased to the same location. A link error can
1221 * occur if the linker determines that every path through the
1222 * shader consumes multiple aliased attributes, but
1223 * implementations are not required to generate an error in this
1224 * case."
1225 *
1226 * These two paragraphs are either somewhat contradictory, or I don't
1227 * fully understand one or both of them.
1228 */
1229 /* FINISHME: The code as currently written does not support attribute
1230 * FINISHME: location aliasing (see comment above).
1231 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001232 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -07001233 const unsigned slots = count_attribute_slots(var->type);
1234
1235 /* Mask representing the contiguous slots that will be used by this
1236 * attribute.
1237 */
1238 const unsigned use_mask = (1 << slots) - 1;
1239
1240 /* Generate a link error if the set of bits requested for this
1241 * attribute overlaps any previously allocated bits.
1242 */
1243 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001244 linker_error_printf(prog,
1245 "insufficient contiguous attribute locations "
1246 "available for vertex shader input `%s'",
1247 var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001248 return false;
1249 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001250
1251 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -07001252 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001253 }
1254 }
1255
Ian Romanick69846702010-06-22 17:29:19 -07001256 /* Temporary storage for the set of attributes that need locations assigned.
1257 */
1258 struct temp_attr {
1259 unsigned slots;
1260 ir_variable *var;
1261
1262 /* Used below in the call to qsort. */
1263 static int compare(const void *a, const void *b)
1264 {
1265 const temp_attr *const l = (const temp_attr *) a;
1266 const temp_attr *const r = (const temp_attr *) b;
1267
1268 /* Reversed because we want a descending order sort below. */
1269 return r->slots - l->slots;
1270 }
1271 } to_assign[16];
1272
1273 unsigned num_attr = 0;
1274
Eric Anholt16b68b12010-06-30 11:05:43 -07001275 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001276 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1277
1278 if ((var == NULL) || (var->mode != ir_var_in))
1279 continue;
1280
Ian Romanick68a4fc92010-10-07 17:21:22 -07001281 if (var->explicit_location) {
1282 const unsigned slots = count_attribute_slots(var->type);
1283 const unsigned use_mask = (1 << slots) - 1;
1284 const int attr = var->location - VERT_ATTRIB_GENERIC0;
1285
1286 if ((var->location >= (int)(max_attribute_index + VERT_ATTRIB_GENERIC0))
1287 || (var->location < 0)) {
1288 linker_error_printf(prog,
1289 "invalid explicit location %d specified for "
1290 "`%s'\n",
1291 (var->location < 0) ? var->location : attr,
1292 var->name);
1293 return false;
1294 } else if (var->location >= VERT_ATTRIB_GENERIC0) {
1295 used_locations |= (use_mask << attr);
1296 }
1297 }
1298
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001299 /* The location was explicitly assigned, nothing to do here.
1300 */
1301 if (var->location != -1)
1302 continue;
1303
Ian Romanick69846702010-06-22 17:29:19 -07001304 to_assign[num_attr].slots = count_attribute_slots(var->type);
1305 to_assign[num_attr].var = var;
1306 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001307 }
Ian Romanick69846702010-06-22 17:29:19 -07001308
1309 /* If all of the attributes were assigned locations by the application (or
1310 * are built-in attributes with fixed locations), return early. This should
1311 * be the common case.
1312 */
1313 if (num_attr == 0)
1314 return true;
1315
1316 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1317
Ian Romanick982e3792010-06-29 18:58:20 -07001318 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
1319 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
1320 * to prevent it from being automatically allocated below.
1321 */
Ian Romanickc33e78f2010-08-13 12:30:41 -07001322 find_deref_visitor find("gl_Vertex");
1323 find.run(sh->ir);
1324 if (find.variable_found())
1325 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -07001326
Ian Romanick69846702010-06-22 17:29:19 -07001327 for (unsigned i = 0; i < num_attr; i++) {
1328 /* Mask representing the contiguous slots that will be used by this
1329 * attribute.
1330 */
1331 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1332
1333 int location = find_available_slots(used_locations, to_assign[i].slots);
1334
1335 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001336 linker_error_printf(prog,
1337 "insufficient contiguous attribute locations "
1338 "available for vertex shader input `%s'",
1339 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001340 return false;
1341 }
1342
1343 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1344 used_locations |= (use_mask << location);
1345 }
1346
1347 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001348}
1349
1350
Ian Romanick40e114b2010-08-17 14:55:50 -07001351/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001352 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001353 */
1354void
Ian Romanickcc90e622010-10-19 17:59:10 -07001355demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001356{
1357 foreach_list(node, sh->ir) {
1358 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1359
Ian Romanickcc90e622010-10-19 17:59:10 -07001360 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001361 continue;
1362
Ian Romanickcc90e622010-10-19 17:59:10 -07001363 /* A shader 'in' or 'out' variable is only really an input or output if
1364 * its value is used by other shader stages. This will cause the variable
1365 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001366 */
1367 if (var->location == -1) {
1368 var->mode = ir_var_auto;
1369 }
1370 }
1371}
1372
1373
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001374void
Eric Anholtb7062832010-07-28 13:52:23 -07001375assign_varying_locations(struct gl_shader_program *prog,
1376 gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001377{
1378 /* FINISHME: Set dynamically when geometry shader support is added. */
1379 unsigned output_index = VERT_RESULT_VAR0;
1380 unsigned input_index = FRAG_ATTRIB_VAR0;
1381
1382 /* Operate in a total of three passes.
1383 *
1384 * 1. Assign locations for any matching inputs and outputs.
1385 *
1386 * 2. Mark output variables in the producer that do not have locations as
1387 * not being outputs. This lets the optimizer eliminate them.
1388 *
1389 * 3. Mark input variables in the consumer that do not have locations as
1390 * not being inputs. This lets the optimizer eliminate them.
1391 */
1392
1393 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1394 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1395
Eric Anholt16b68b12010-06-30 11:05:43 -07001396 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001397 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1398
1399 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1400 || (output_var->location != -1))
1401 continue;
1402
1403 ir_variable *const input_var =
1404 consumer->symbols->get_variable(output_var->name);
1405
1406 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1407 continue;
1408
1409 assert(input_var->location == -1);
1410
Ian Romanick0e59b262010-06-23 11:23:01 -07001411 output_var->location = output_index;
1412 input_var->location = input_index;
1413
Ian Romanickdf869d92010-08-30 15:37:44 -07001414 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1415 assert(!output_var->type->is_record());
1416
1417 if (output_var->type->is_array()) {
1418 const unsigned slots = output_var->type->length
1419 * output_var->type->fields.array->matrix_columns;
1420
1421 output_index += slots;
1422 input_index += slots;
1423 } else {
1424 const unsigned slots = output_var->type->matrix_columns;
1425
1426 output_index += slots;
1427 input_index += slots;
1428 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001429 }
1430
Eric Anholt16b68b12010-06-30 11:05:43 -07001431 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001432 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1433
1434 if ((var == NULL) || (var->mode != ir_var_in))
1435 continue;
1436
Eric Anholtb7062832010-07-28 13:52:23 -07001437 if (var->location == -1) {
1438 if (prog->Version <= 120) {
1439 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1440 *
1441 * Only those varying variables used (i.e. read) in
1442 * the fragment shader executable must be written to
1443 * by the vertex shader executable; declaring
1444 * superfluous varying variables in a vertex shader is
1445 * permissible.
1446 *
1447 * We interpret this text as meaning that the VS must
1448 * write the variable for the FS to read it. See
1449 * "glsl1-varying read but not written" in piglit.
1450 */
1451
1452 linker_error_printf(prog, "fragment shader varying %s not written "
1453 "by vertex shader\n.", var->name);
1454 prog->LinkStatus = false;
1455 }
1456
1457 /* An 'in' variable is only really a shader input if its
1458 * value is written by the previous stage.
1459 */
Eric Anholtb7062832010-07-28 13:52:23 -07001460 var->mode = ir_var_auto;
1461 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001462 }
1463}
1464
1465
1466void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001467link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001468{
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001469 void *mem_ctx = talloc_init("temporary linker context");
1470
Ian Romanick832dfa52010-06-17 15:04:20 -07001471 prog->LinkStatus = false;
1472 prog->Validated = false;
1473 prog->_Used = false;
1474
Ian Romanickf36460e2010-06-23 12:07:22 -07001475 if (prog->InfoLog != NULL)
1476 talloc_free(prog->InfoLog);
1477
1478 prog->InfoLog = talloc_strdup(NULL, "");
1479
Ian Romanick832dfa52010-06-17 15:04:20 -07001480 /* Separate the shaders into groups based on their type.
1481 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001482 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001483 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001484 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001485 unsigned num_frag_shaders = 0;
1486
Eric Anholt16b68b12010-06-30 11:05:43 -07001487 vert_shader_list = (struct gl_shader **)
1488 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001489 frag_shader_list = &vert_shader_list[prog->NumShaders];
1490
Ian Romanick25f51d32010-07-16 15:51:50 -07001491 unsigned min_version = UINT_MAX;
1492 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001493 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001494 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1495 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1496
Ian Romanick832dfa52010-06-17 15:04:20 -07001497 switch (prog->Shaders[i]->Type) {
1498 case GL_VERTEX_SHADER:
1499 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1500 num_vert_shaders++;
1501 break;
1502 case GL_FRAGMENT_SHADER:
1503 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1504 num_frag_shaders++;
1505 break;
1506 case GL_GEOMETRY_SHADER:
1507 /* FINISHME: Support geometry shaders. */
1508 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1509 break;
1510 }
1511 }
1512
Ian Romanick25f51d32010-07-16 15:51:50 -07001513 /* Previous to GLSL version 1.30, different compilation units could mix and
1514 * match shading language versions. With GLSL 1.30 and later, the versions
1515 * of all shaders must match.
1516 */
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001517 assert(min_version >= 100);
Ian Romanick25f51d32010-07-16 15:51:50 -07001518 assert(max_version <= 130);
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001519 if ((max_version >= 130 || min_version == 100)
1520 && min_version != max_version) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001521 linker_error_printf(prog, "all shaders must use same shading "
1522 "language version\n");
1523 goto done;
1524 }
1525
1526 prog->Version = max_version;
1527
Ian Romanick3322fba2010-10-14 13:28:42 -07001528 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1529 if (prog->_LinkedShaders[i] != NULL)
1530 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1531
1532 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07001533 }
1534
Ian Romanickcd6764e2010-07-16 16:00:07 -07001535 /* Link all shaders for a particular stage and validate the result.
1536 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001537 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001538 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001539 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1540 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001541
1542 if (sh == NULL)
1543 goto done;
1544
1545 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001546 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001547
Ian Romanick3322fba2010-10-14 13:28:42 -07001548 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1549 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001550 }
1551
1552 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001553 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001554 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1555 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001556
1557 if (sh == NULL)
1558 goto done;
1559
1560 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001561 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001562
Ian Romanick3322fba2010-10-14 13:28:42 -07001563 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1564 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001565 }
1566
Ian Romanick3ed850e2010-06-23 12:18:21 -07001567 /* Here begins the inter-stage linking phase. Some initial validation is
1568 * performed, then locations are assigned for uniforms, attributes, and
1569 * varyings.
1570 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001571 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07001572 unsigned prev;
1573
1574 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1575 if (prog->_LinkedShaders[prev] != NULL)
1576 break;
1577 }
1578
Ian Romanick37101922010-06-18 19:02:10 -07001579 /* Validate the inputs of each stage with the output of the preceeding
1580 * stage.
1581 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001582 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1583 if (prog->_LinkedShaders[i] == NULL)
1584 continue;
1585
Ian Romanickf36460e2010-06-23 12:07:22 -07001586 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07001587 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07001588 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001589 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001590
1591 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07001592 }
1593
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001594 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001595 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001596
Eric Anholt2f4fe152010-08-10 13:06:49 -07001597 /* Do common optimization before assigning storage for attributes,
1598 * uniforms, and varyings. Later optimization could possibly make
1599 * some of that unused.
1600 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001601 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1602 if (prog->_LinkedShaders[i] == NULL)
1603 continue;
1604
Luca Barbierie591c462010-09-05 22:29:58 +02001605 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
Eric Anholt2f4fe152010-08-10 13:06:49 -07001606 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001607 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001608
Eric Anholt586b4b52010-09-28 14:32:16 -07001609 update_array_sizes(prog);
1610
Ian Romanickabee16e2010-06-21 16:16:05 -07001611 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001612
Ian Romanick3322fba2010-10-14 13:28:42 -07001613 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
Ian Romanick9342d262010-06-22 17:41:37 -07001614 /* FINISHME: The value of the max_attribute_index parameter is
1615 * FINISHME: implementation dependent based on the value of
1616 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1617 * FINISHME: at least 16, so hardcode 16 for now.
1618 */
Ian Romanick4b5489d2010-10-07 17:20:15 -07001619 if (!assign_attribute_locations(prog, 16)) {
1620 prog->LinkStatus = false;
Ian Romanick69846702010-06-22 17:29:19 -07001621 goto done;
Ian Romanick4b5489d2010-10-07 17:20:15 -07001622 }
Ian Romanick40e114b2010-08-17 14:55:50 -07001623 }
1624
Ian Romanick3322fba2010-10-14 13:28:42 -07001625 unsigned prev;
1626 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1627 if (prog->_LinkedShaders[prev] != NULL)
1628 break;
1629 }
1630
1631 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1632 if (prog->_LinkedShaders[i] == NULL)
1633 continue;
1634
Eric Anholtb7062832010-07-28 13:52:23 -07001635 assign_varying_locations(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07001636 prog->_LinkedShaders[prev],
Ian Romanick0e59b262010-06-23 11:23:01 -07001637 prog->_LinkedShaders[i]);
Ian Romanick3322fba2010-10-14 13:28:42 -07001638 prev = i;
1639 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001640
Ian Romanickcc90e622010-10-19 17:59:10 -07001641 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1642 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
1643 ir_var_out);
1644 }
1645
1646 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
1647 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
1648
1649 demote_shader_inputs_and_outputs(sh, ir_var_in);
1650 demote_shader_inputs_and_outputs(sh, ir_var_inout);
1651 demote_shader_inputs_and_outputs(sh, ir_var_out);
1652 }
1653
1654 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
1655 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1656
1657 demote_shader_inputs_and_outputs(sh, ir_var_in);
1658 }
1659
Ian Romanick13e10e42010-06-21 12:03:24 -07001660 /* FINISHME: Assign fragment shader output locations. */
1661
Ian Romanick832dfa52010-06-17 15:04:20 -07001662done:
1663 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001664
1665 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1666 if (prog->_LinkedShaders[i] == NULL)
1667 continue;
1668
1669 /* Retain any live IR, but trash the rest. */
1670 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
1671 }
1672
1673 talloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07001674}