blob: 58b029460e975ceba188bcc8563e20ec96912dee [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
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080071#include "main/core.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070072#include "glsl_symbol_table.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070073#include "ir.h"
74#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030075#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070076#include "linker.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070077#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070078
Ian Romanick3322fba2010-10-14 13:28:42 -070079extern "C" {
80#include "main/shaderobj.h"
81}
82
Ian Romanick832dfa52010-06-17 15:04:20 -070083/**
84 * Visitor that determines whether or not a variable is ever written.
85 */
86class find_assignment_visitor : public ir_hierarchical_visitor {
87public:
88 find_assignment_visitor(const char *name)
89 : name(name), found(false)
90 {
91 /* empty */
92 }
93
94 virtual ir_visitor_status visit_enter(ir_assignment *ir)
95 {
96 ir_variable *const var = ir->lhs->variable_referenced();
97
98 if (strcmp(name, var->name) == 0) {
99 found = true;
100 return visit_stop;
101 }
102
103 return visit_continue_with_parent;
104 }
105
Eric Anholt18a60232010-08-23 11:29:25 -0700106 virtual ir_visitor_status visit_enter(ir_call *ir)
107 {
108 exec_list_iterator sig_iter = ir->get_callee()->parameters.iterator();
109 foreach_iter(exec_list_iterator, iter, *ir) {
110 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
111 ir_variable *sig_param = (ir_variable *)sig_iter.get();
112
113 if (sig_param->mode == ir_var_out ||
114 sig_param->mode == ir_var_inout) {
115 ir_variable *var = param_rval->variable_referenced();
116 if (var && strcmp(name, var->name) == 0) {
117 found = true;
118 return visit_stop;
119 }
120 }
121 sig_iter.next();
122 }
123
124 return visit_continue_with_parent;
125 }
126
Ian Romanick832dfa52010-06-17 15:04:20 -0700127 bool variable_found()
128 {
129 return found;
130 }
131
132private:
133 const char *name; /**< Find writes to a variable with this name. */
134 bool found; /**< Was a write to the variable found? */
135};
136
Ian Romanickc93b8f12010-06-17 15:20:22 -0700137
Ian Romanickc33e78f2010-08-13 12:30:41 -0700138/**
139 * Visitor that determines whether or not a variable is ever read.
140 */
141class find_deref_visitor : public ir_hierarchical_visitor {
142public:
143 find_deref_visitor(const char *name)
144 : name(name), found(false)
145 {
146 /* empty */
147 }
148
149 virtual ir_visitor_status visit(ir_dereference_variable *ir)
150 {
151 if (strcmp(this->name, ir->var->name) == 0) {
152 this->found = true;
153 return visit_stop;
154 }
155
156 return visit_continue;
157 }
158
159 bool variable_found() const
160 {
161 return this->found;
162 }
163
164private:
165 const char *name; /**< Find writes to a variable with this name. */
166 bool found; /**< Was a write to the variable found? */
167};
168
169
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700170void
Eric Anholt849e1812010-06-30 11:49:17 -0700171linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700172{
173 va_list ap;
174
175 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
176 va_start(ap, fmt);
177 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
178 va_end(ap);
179}
180
181
182void
Eric Anholt16b68b12010-06-30 11:05:43 -0700183invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700184 int generic_base)
185{
Eric Anholt16b68b12010-06-30 11:05:43 -0700186 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700187 ir_variable *const var = ((ir_instruction *) node)->as_variable();
188
189 if ((var == NULL) || (var->mode != (unsigned) mode))
190 continue;
191
192 /* Only assign locations for generic attributes / varyings / etc.
193 */
Ian Romanick68a4fc92010-10-07 17:21:22 -0700194 if ((var->location >= generic_base) && !var->explicit_location)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700195 var->location = -1;
196 }
197}
198
199
Ian Romanickc93b8f12010-06-17 15:20:22 -0700200/**
Ian Romanick69846702010-06-22 17:29:19 -0700201 * Determine the number of attribute slots required for a particular type
202 *
203 * This code is here because it implements the language rules of a specific
204 * GLSL version. Since it's a property of the language and not a property of
205 * types in general, it doesn't really belong in glsl_type.
206 */
207unsigned
208count_attribute_slots(const glsl_type *t)
209{
210 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
211 *
212 * "A scalar input counts the same amount against this limit as a vec4,
213 * so applications may want to consider packing groups of four
214 * unrelated float inputs together into a vector to better utilize the
215 * capabilities of the underlying hardware. A matrix input will use up
216 * multiple locations. The number of locations used will equal the
217 * number of columns in the matrix."
218 *
219 * The spec does not explicitly say how arrays are counted. However, it
220 * should be safe to assume the total number of slots consumed by an array
221 * is the number of entries in the array multiplied by the number of slots
222 * consumed by a single element of the array.
223 */
224
225 if (t->is_array())
226 return t->array_size() * count_attribute_slots(t->element_type());
227
228 if (t->is_matrix())
229 return t->matrix_columns;
230
231 return 1;
232}
233
234
235/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700236 * Verify that a vertex shader executable meets all semantic requirements
237 *
238 * \param shader Vertex shader executable to be verified
239 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700240bool
Eric Anholt849e1812010-06-30 11:49:17 -0700241validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700242 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700243{
244 if (shader == NULL)
245 return true;
246
Ian Romanick832dfa52010-06-17 15:04:20 -0700247 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700248 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700249 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700250 linker_error_printf(prog,
251 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700252 return false;
253 }
254
255 return true;
256}
257
258
Ian Romanickc93b8f12010-06-17 15:20:22 -0700259/**
260 * Verify that a fragment shader executable meets all semantic requirements
261 *
262 * \param shader Fragment shader executable to be verified
263 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700264bool
Eric Anholt849e1812010-06-30 11:49:17 -0700265validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700266 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700267{
268 if (shader == NULL)
269 return true;
270
Ian Romanick832dfa52010-06-17 15:04:20 -0700271 find_assignment_visitor frag_color("gl_FragColor");
272 find_assignment_visitor frag_data("gl_FragData");
273
Eric Anholt16b68b12010-06-30 11:05:43 -0700274 frag_color.run(shader->ir);
275 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700276
Ian Romanick832dfa52010-06-17 15:04:20 -0700277 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700278 linker_error_printf(prog, "fragment shader writes to both "
279 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700280 return false;
281 }
282
283 return true;
284}
285
286
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700287/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700288 * Generate a string describing the mode of a variable
289 */
290static const char *
291mode_string(const ir_variable *var)
292{
293 switch (var->mode) {
294 case ir_var_auto:
295 return (var->read_only) ? "global constant" : "global variable";
296
297 case ir_var_uniform: return "uniform";
298 case ir_var_in: return "shader input";
299 case ir_var_out: return "shader output";
300 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700301
302 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700303 default:
304 assert(!"Should not get here.");
305 return "invalid variable";
306 }
307}
308
309
310/**
311 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700312 */
313bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700314cross_validate_globals(struct gl_shader_program *prog,
315 struct gl_shader **shader_list,
316 unsigned num_shaders,
317 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700318{
319 /* Examine all of the uniforms in all of the shaders and cross validate
320 * them.
321 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700322 glsl_symbol_table variables;
323 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700324 if (shader_list[i] == NULL)
325 continue;
326
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700327 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700328 ir_variable *const var = ((ir_instruction *) node)->as_variable();
329
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700330 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700331 continue;
332
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700333 if (uniforms_only && (var->mode != ir_var_uniform))
334 continue;
335
Ian Romanick7e2aa912010-07-19 17:12:42 -0700336 /* Don't cross validate temporaries that are at global scope. These
337 * will eventually get pulled into the shaders 'main'.
338 */
339 if (var->mode == ir_var_temporary)
340 continue;
341
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700342 /* If a global with this name has already been seen, verify that the
343 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700344 * initializers, the values of the initializers must be the same.
345 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700346 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700347 if (existing != NULL) {
348 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700349 /* Consider the types to be "the same" if both types are arrays
350 * of the same type and one of the arrays is implicitly sized.
351 * In addition, set the type of the linked variable to the
352 * explicitly sized array.
353 */
354 if (var->type->is_array()
355 && existing->type->is_array()
356 && (var->type->fields.array == existing->type->fields.array)
357 && ((var->type->length == 0)
358 || (existing->type->length == 0))) {
Ian Romanick6f539212010-12-07 18:30:33 -0800359 if (existing->type->length == 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700360 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800361 existing->max_array_access =
362 MAX2(existing->max_array_access,
363 var->max_array_access);
364 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700365 } 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 }
Chad Versace7528f142010-11-17 14:34:38 -0800414
415 if (existing->invariant != var->invariant) {
416 linker_error_printf(prog, "declarations for %s `%s' have "
417 "mismatching invariant qualifiers\n",
418 mode_string(var), var->name);
419 return false;
420 }
Chad Versace61428dd2011-01-10 15:29:30 -0800421 if (existing->centroid != var->centroid) {
422 linker_error_printf(prog, "declarations for %s `%s' have "
423 "mismatching centroid qualifiers\n",
424 mode_string(var), var->name);
425 return false;
426 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700427 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700428 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700429 }
430 }
431
432 return true;
433}
434
435
Ian Romanick37101922010-06-18 19:02:10 -0700436/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700437 * Perform validation of uniforms used across multiple shader stages
438 */
439bool
440cross_validate_uniforms(struct gl_shader_program *prog)
441{
442 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700443 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700444}
445
446
447/**
Ian Romanick37101922010-06-18 19:02:10 -0700448 * Validate that outputs from one stage match inputs of another
449 */
450bool
Eric Anholt849e1812010-06-30 11:49:17 -0700451cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700452 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700453{
454 glsl_symbol_table parameters;
455 /* FINISHME: Figure these out dynamically. */
456 const char *const producer_stage = "vertex";
457 const char *const consumer_stage = "fragment";
458
459 /* Find all shader outputs in the "producer" stage.
460 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700461 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700462 ir_variable *const var = ((ir_instruction *) node)->as_variable();
463
464 /* FINISHME: For geometry shaders, this should also look for inout
465 * FINISHME: variables.
466 */
467 if ((var == NULL) || (var->mode != ir_var_out))
468 continue;
469
Eric Anholt001eee52010-11-05 06:11:24 -0700470 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700471 }
472
473
474 /* Find all shader inputs in the "consumer" stage. Any variables that have
475 * matching outputs already in the symbol table must have the same type and
476 * qualifiers.
477 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700478 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700479 ir_variable *const input = ((ir_instruction *) node)->as_variable();
480
481 /* FINISHME: For geometry shaders, this should also look for inout
482 * FINISHME: variables.
483 */
484 if ((input == NULL) || (input->mode != ir_var_in))
485 continue;
486
487 ir_variable *const output = parameters.get_variable(input->name);
488 if (output != NULL) {
489 /* Check that the types match between stages.
490 */
491 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800492 /* There is a bit of a special case for gl_TexCoord. This
493 * built-in is unsized by default. Appliations that variable
494 * access it must redeclare it with a size. There is some
495 * language in the GLSL spec that implies the fragment shader
496 * and vertex shader do not have to agree on this size. Other
497 * driver behave this way, and one or two applications seem to
498 * rely on it.
499 *
500 * Neither declaration needs to be modified here because the array
501 * sizes are fixed later when update_array_sizes is called.
502 *
503 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
504 *
505 * "Unlike user-defined varying variables, the built-in
506 * varying variables don't have a strict one-to-one
507 * correspondence between the vertex language and the
508 * fragment language."
509 */
510 if (!output->type->is_array()
511 || (strncmp("gl_", output->name, 3) != 0)) {
512 linker_error_printf(prog,
513 "%s shader output `%s' declared as "
514 "type `%s', but %s shader input declared "
515 "as type `%s'\n",
516 producer_stage, output->name,
517 output->type->name,
518 consumer_stage, input->type->name);
519 return false;
520 }
Ian Romanick37101922010-06-18 19:02:10 -0700521 }
522
523 /* Check that all of the qualifiers match between stages.
524 */
525 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700526 linker_error_printf(prog,
527 "%s shader output `%s' %s centroid qualifier, "
528 "but %s shader input %s centroid qualifier\n",
529 producer_stage,
530 output->name,
531 (output->centroid) ? "has" : "lacks",
532 consumer_stage,
533 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700534 return false;
535 }
536
537 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700538 linker_error_printf(prog,
539 "%s shader output `%s' %s invariant qualifier, "
540 "but %s shader input %s invariant qualifier\n",
541 producer_stage,
542 output->name,
543 (output->invariant) ? "has" : "lacks",
544 consumer_stage,
545 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700546 return false;
547 }
548
549 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700550 linker_error_printf(prog,
551 "%s shader output `%s' specifies %s "
552 "interpolation qualifier, "
553 "but %s shader input specifies %s "
554 "interpolation qualifier\n",
555 producer_stage,
556 output->name,
557 output->interpolation_string(),
558 consumer_stage,
559 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700560 return false;
561 }
562 }
563 }
564
565 return true;
566}
567
568
Ian Romanick3fb87872010-07-09 14:09:34 -0700569/**
570 * Populates a shaders symbol table with all global declarations
571 */
572static void
573populate_symbol_table(gl_shader *sh)
574{
575 sh->symbols = new(sh) glsl_symbol_table;
576
577 foreach_list(node, sh->ir) {
578 ir_instruction *const inst = (ir_instruction *) node;
579 ir_variable *var;
580 ir_function *func;
581
582 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700583 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700584 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700585 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700586 }
587 }
588}
589
590
591/**
Ian Romanick31a97862010-07-12 18:48:50 -0700592 * Remap variables referenced in an instruction tree
593 *
594 * This is used when instruction trees are cloned from one shader and placed in
595 * another. These trees will contain references to \c ir_variable nodes that
596 * do not exist in the target shader. This function finds these \c ir_variable
597 * references and replaces the references with matching variables in the target
598 * shader.
599 *
600 * If there is no matching variable in the target shader, a clone of the
601 * \c ir_variable is made and added to the target shader. The new variable is
602 * added to \b both the instruction stream and the symbol table.
603 *
604 * \param inst IR tree that is to be processed.
605 * \param symbols Symbol table containing global scope symbols in the
606 * linked shader.
607 * \param instructions Instruction stream where new variable declarations
608 * should be added.
609 */
610void
Eric Anholt8273bd42010-08-04 12:34:56 -0700611remap_variables(ir_instruction *inst, struct gl_shader *target,
612 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700613{
614 class remap_visitor : public ir_hierarchical_visitor {
615 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700616 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700617 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700618 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700619 this->target = target;
620 this->symbols = target->symbols;
621 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700622 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700623 }
624
625 virtual ir_visitor_status visit(ir_dereference_variable *ir)
626 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700627 if (ir->var->mode == ir_var_temporary) {
628 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
629
630 assert(var != NULL);
631 ir->var = var;
632 return visit_continue;
633 }
634
Ian Romanick31a97862010-07-12 18:48:50 -0700635 ir_variable *const existing =
636 this->symbols->get_variable(ir->var->name);
637 if (existing != NULL)
638 ir->var = existing;
639 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700640 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700641
Eric Anholt001eee52010-11-05 06:11:24 -0700642 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700643 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700644 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700645 }
646
647 return visit_continue;
648 }
649
650 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700651 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700652 glsl_symbol_table *symbols;
653 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700654 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700655 };
656
Eric Anholt8273bd42010-08-04 12:34:56 -0700657 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700658
659 inst->accept(&v);
660}
661
662
663/**
664 * Move non-declarations from one instruction stream to another
665 *
666 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700667 * 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 -0700668 * pointer) for \c last and \c false for \c make_copies on the first
669 * call. Successive calls pass the return value of the previous call for
670 * \c last and \c true for \c make_copies.
671 *
672 * \param instructions Source instruction stream
673 * \param last Instruction after which new instructions should be
674 * inserted in the target instruction stream
675 * \param make_copies Flag selecting whether instructions in \c instructions
676 * should be copied (via \c ir_instruction::clone) into the
677 * target list or moved.
678 *
679 * \return
680 * The new "last" instruction in the target instruction stream. This pointer
681 * is suitable for use as the \c last parameter of a later call to this
682 * function.
683 */
684exec_node *
685move_non_declarations(exec_list *instructions, exec_node *last,
686 bool make_copies, gl_shader *target)
687{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700688 hash_table *temps = NULL;
689
690 if (make_copies)
691 temps = hash_table_ctor(0, hash_table_pointer_hash,
692 hash_table_pointer_compare);
693
Ian Romanick303c99f2010-07-19 12:34:56 -0700694 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700695 ir_instruction *inst = (ir_instruction *) node;
696
Ian Romanick7e2aa912010-07-19 17:12:42 -0700697 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700698 continue;
699
Ian Romanick7e2aa912010-07-19 17:12:42 -0700700 ir_variable *var = inst->as_variable();
701 if ((var != NULL) && (var->mode != ir_var_temporary))
702 continue;
703
704 assert(inst->as_assignment()
705 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700706
707 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700708 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700709
710 if (var != NULL)
711 hash_table_insert(temps, inst, var);
712 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700713 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700714 } else {
715 inst->remove();
716 }
717
718 last->insert_after(inst);
719 last = inst;
720 }
721
Ian Romanick7e2aa912010-07-19 17:12:42 -0700722 if (make_copies)
723 hash_table_dtor(temps);
724
Ian Romanick31a97862010-07-12 18:48:50 -0700725 return last;
726}
727
728/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700729 * Get the function signature for main from a shader
730 */
731static ir_function_signature *
732get_main_function_signature(gl_shader *sh)
733{
734 ir_function *const f = sh->symbols->get_function("main");
735 if (f != NULL) {
736 exec_list void_parameters;
737
738 /* Look for the 'void main()' signature and ensure that it's defined.
739 * This keeps the linker from accidentally pick a shader that just
740 * contains a prototype for main.
741 *
742 * We don't have to check for multiple definitions of main (in multiple
743 * shaders) because that would have already been caught above.
744 */
745 ir_function_signature *sig = f->matching_signature(&void_parameters);
746 if ((sig != NULL) && sig->is_defined) {
747 return sig;
748 }
749 }
750
751 return NULL;
752}
753
754
755/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700756 * Combine a group of shaders for a single stage to generate a linked shader
757 *
758 * \note
759 * If this function is supplied a single shader, it is cloned, and the new
760 * shader is returned.
761 */
762static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800763link_intrastage_shaders(void *mem_ctx,
764 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700765 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700766 struct gl_shader **shader_list,
767 unsigned num_shaders)
768{
Ian Romanick13f782c2010-06-29 18:53:38 -0700769 /* Check that global variables defined in multiple shaders are consistent.
770 */
771 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
772 return NULL;
773
774 /* Check that there is only a single definition of each function signature
775 * across all shaders.
776 */
777 for (unsigned i = 0; i < (num_shaders - 1); i++) {
778 foreach_list(node, shader_list[i]->ir) {
779 ir_function *const f = ((ir_instruction *) node)->as_function();
780
781 if (f == NULL)
782 continue;
783
784 for (unsigned j = i + 1; j < num_shaders; j++) {
785 ir_function *const other =
786 shader_list[j]->symbols->get_function(f->name);
787
788 /* If the other shader has no function (and therefore no function
789 * signatures) with the same name, skip to the next shader.
790 */
791 if (other == NULL)
792 continue;
793
794 foreach_iter (exec_list_iterator, iter, *f) {
795 ir_function_signature *sig =
796 (ir_function_signature *) iter.get();
797
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700798 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700799 continue;
800
801 ir_function_signature *other_sig =
802 other->exact_matching_signature(& sig->parameters);
803
804 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700805 && !other_sig->is_builtin) {
Ian Romanick13f782c2010-06-29 18:53:38 -0700806 linker_error_printf(prog,
807 "function `%s' is multiply defined",
808 f->name);
809 return NULL;
810 }
811 }
812 }
813 }
814 }
815
816 /* Find the shader that defines main, and make a clone of it.
817 *
818 * Starting with the clone, search for undefined references. If one is
819 * found, find the shader that defines it. Clone the reference and add
820 * it to the shader. Repeat until there are no undefined references or
821 * until a reference cannot be resolved.
822 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700823 gl_shader *main = NULL;
824 for (unsigned i = 0; i < num_shaders; i++) {
825 if (get_main_function_signature(shader_list[i]) != NULL) {
826 main = shader_list[i];
827 break;
828 }
829 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700830
Ian Romanick15ce87e2010-07-09 15:28:22 -0700831 if (main == NULL) {
832 linker_error_printf(prog, "%s shader lacks `main'\n",
833 (shader_list[0]->Type == GL_VERTEX_SHADER)
834 ? "vertex" : "fragment");
835 return NULL;
836 }
837
Ian Romanick4a455952010-10-13 15:13:02 -0700838 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700839 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800840 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700841
842 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700843
Ian Romanick31a97862010-07-12 18:48:50 -0700844 /* The a pointer to the main function in the final linked shader (i.e., the
845 * copy of the original shader that contained the main function).
846 */
847 ir_function_signature *const main_sig = get_main_function_signature(linked);
848
849 /* Move any instructions other than variable declarations or function
850 * declarations into main.
851 */
Ian Romanick9303e352010-07-19 12:33:54 -0700852 exec_node *insertion_point =
853 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
854 linked);
855
Ian Romanick31a97862010-07-12 18:48:50 -0700856 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700857 if (shader_list[i] == main)
858 continue;
859
Ian Romanick31a97862010-07-12 18:48:50 -0700860 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700861 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700862 }
863
Ian Romanick13f782c2010-06-29 18:53:38 -0700864 /* Resolve initializers for global variables in the linked shader.
865 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700866 unsigned num_linking_shaders = num_shaders;
867 for (unsigned i = 0; i < num_shaders; i++)
868 num_linking_shaders += shader_list[i]->num_builtins_to_link;
869
870 gl_shader **linking_shaders =
871 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
872
873 memcpy(linking_shaders, shader_list,
874 sizeof(linking_shaders[0]) * num_shaders);
875
876 unsigned idx = num_shaders;
877 for (unsigned i = 0; i < num_shaders; i++) {
878 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
879 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
880 idx += shader_list[i]->num_builtins_to_link;
881 }
882
883 assert(idx == num_linking_shaders);
884
Ian Romanick4a455952010-10-13 15:13:02 -0700885 if (!link_function_calls(prog, linked, linking_shaders,
886 num_linking_shaders)) {
887 ctx->Driver.DeleteShader(ctx, linked);
888 linked = NULL;
889 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700890
891 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -0700892
Ian Romanickc87e9ef2011-01-25 12:04:08 -0800893 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -0800894 * unspecified sizes have a size specified. The size is inferred from the
895 * max_array_access field.
896 */
Ian Romanick002cd2c2010-12-07 19:00:44 -0800897 if (linked != NULL) {
Ian Romanickc87e9ef2011-01-25 12:04:08 -0800898 class array_sizing_visitor : public ir_hierarchical_visitor {
899 public:
900 virtual ir_visitor_status visit(ir_variable *var)
901 {
902 if (var->type->is_array() && (var->type->length == 0)) {
903 const glsl_type *type =
904 glsl_type::get_array_instance(var->type->fields.array,
905 var->max_array_access);
Ian Romanick6f539212010-12-07 18:30:33 -0800906
Ian Romanickc87e9ef2011-01-25 12:04:08 -0800907 assert(type != NULL);
908 var->type = type;
909 }
Ian Romanick6f539212010-12-07 18:30:33 -0800910
Ian Romanickc87e9ef2011-01-25 12:04:08 -0800911 return visit_continue;
912 }
913 } v;
Ian Romanick6f539212010-12-07 18:30:33 -0800914
Ian Romanickc87e9ef2011-01-25 12:04:08 -0800915 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -0800916 }
917
Ian Romanick3fb87872010-07-09 14:09:34 -0700918 return linked;
919}
920
921
Ian Romanick019a59b2010-06-21 16:10:42 -0700922struct uniform_node {
923 exec_node link;
924 struct gl_uniform *u;
925 unsigned slots;
926};
927
Eric Anholta721abf2010-08-23 10:32:01 -0700928/**
929 * Update the sizes of linked shader uniform arrays to the maximum
930 * array index used.
931 *
932 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
933 *
934 * If one or more elements of an array are active,
935 * GetActiveUniform will return the name of the array in name,
936 * subject to the restrictions listed above. The type of the array
937 * is returned in type. The size parameter contains the highest
938 * array element index used, plus one. The compiler or linker
939 * determines the highest index used. There will be only one
940 * active uniform reported by the GL per uniform array.
941
942 */
943static void
Eric Anholt586b4b52010-09-28 14:32:16 -0700944update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -0700945{
Ian Romanick3322fba2010-10-14 13:28:42 -0700946 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
947 if (prog->_LinkedShaders[i] == NULL)
948 continue;
949
Eric Anholta721abf2010-08-23 10:32:01 -0700950 foreach_list(node, prog->_LinkedShaders[i]->ir) {
951 ir_variable *const var = ((ir_instruction *) node)->as_variable();
952
Eric Anholt586b4b52010-09-28 14:32:16 -0700953 if ((var == NULL) || (var->mode != ir_var_uniform &&
954 var->mode != ir_var_in &&
955 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -0700956 !var->type->is_array())
957 continue;
958
959 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -0700960 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
961 if (prog->_LinkedShaders[j] == NULL)
962 continue;
963
Eric Anholta721abf2010-08-23 10:32:01 -0700964 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
965 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
966 if (!other_var)
967 continue;
968
969 if (strcmp(var->name, other_var->name) == 0 &&
970 other_var->max_array_access > size) {
971 size = other_var->max_array_access;
972 }
973 }
974 }
Eric Anholt586b4b52010-09-28 14:32:16 -0700975
Eric Anholta721abf2010-08-23 10:32:01 -0700976 if (size + 1 != var->type->fields.array->length) {
977 var->type = glsl_type::get_array_instance(var->type->fields.array,
978 size + 1);
979 /* FINISHME: We should update the types of array
980 * dereferences of this variable now.
981 */
982 }
983 }
984 }
985}
986
Eric Anholt45388b52010-08-24 13:51:35 -0700987static void
988add_uniform(void *mem_ctx, exec_list *uniforms, struct hash_table *ht,
989 const char *name, const glsl_type *type, GLenum shader_type,
990 unsigned *next_shader_pos, unsigned *total_uniforms)
991{
992 if (type->is_record()) {
993 for (unsigned int i = 0; i < type->length; i++) {
994 const glsl_type *field_type = type->fields.structure[i].type;
995 char *field_name = talloc_asprintf(mem_ctx, "%s.%s", name,
996 type->fields.structure[i].name);
997
998 add_uniform(mem_ctx, uniforms, ht, field_name, field_type,
999 shader_type, next_shader_pos, total_uniforms);
1000 }
1001 } else {
1002 uniform_node *n = (uniform_node *) hash_table_find(ht, name);
1003 unsigned int vec4_slots;
1004 const glsl_type *array_elem_type = NULL;
1005
1006 if (type->is_array()) {
1007 array_elem_type = type->fields.array;
1008 /* Array of structures. */
1009 if (array_elem_type->is_record()) {
1010 for (unsigned int i = 0; i < type->length; i++) {
1011 char *elem_name = talloc_asprintf(mem_ctx, "%s[%d]", name, i);
1012 add_uniform(mem_ctx, uniforms, ht, elem_name, array_elem_type,
1013 shader_type, next_shader_pos, total_uniforms);
1014 }
1015 return;
1016 }
1017 }
1018
1019 /* Fix the storage size of samplers at 1 vec4 each. Be sure to pad out
1020 * vectors to vec4 slots.
1021 */
1022 if (type->is_array()) {
1023 if (array_elem_type->is_sampler())
1024 vec4_slots = type->length;
1025 else
1026 vec4_slots = type->length * array_elem_type->matrix_columns;
1027 } else if (type->is_sampler()) {
1028 vec4_slots = 1;
1029 } else {
1030 vec4_slots = type->matrix_columns;
1031 }
1032
1033 if (n == NULL) {
1034 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
1035 n->u = (gl_uniform *) calloc(1, sizeof(struct gl_uniform));
1036 n->slots = vec4_slots;
1037
1038 n->u->Name = strdup(name);
Eric Anholt0924ba02010-08-24 14:27:27 -07001039 n->u->Type = type;
Eric Anholt45388b52010-08-24 13:51:35 -07001040 n->u->VertPos = -1;
1041 n->u->FragPos = -1;
1042 n->u->GeomPos = -1;
1043 (*total_uniforms)++;
1044
1045 hash_table_insert(ht, n, name);
1046 uniforms->push_tail(& n->link);
1047 }
1048
1049 switch (shader_type) {
1050 case GL_VERTEX_SHADER:
1051 n->u->VertPos = *next_shader_pos;
1052 break;
1053 case GL_FRAGMENT_SHADER:
1054 n->u->FragPos = *next_shader_pos;
1055 break;
1056 case GL_GEOMETRY_SHADER:
1057 n->u->GeomPos = *next_shader_pos;
1058 break;
1059 }
1060
1061 (*next_shader_pos) += vec4_slots;
1062 }
1063}
1064
Ian Romanickabee16e2010-06-21 16:16:05 -07001065void
Eric Anholt849e1812010-06-30 11:49:17 -07001066assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -07001067{
1068 /* */
1069 exec_list uniforms;
1070 unsigned total_uniforms = 0;
1071 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
1072 hash_table_string_compare);
Eric Anholt45388b52010-08-24 13:51:35 -07001073 void *mem_ctx = talloc_new(NULL);
Ian Romanick019a59b2010-06-21 16:10:42 -07001074
Ian Romanick3322fba2010-10-14 13:28:42 -07001075 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1076 if (prog->_LinkedShaders[i] == NULL)
1077 continue;
1078
Ian Romanick019a59b2010-06-21 16:10:42 -07001079 unsigned next_position = 0;
1080
Eric Anholt16b68b12010-06-30 11:05:43 -07001081 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -07001082 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1083
1084 if ((var == NULL) || (var->mode != ir_var_uniform))
1085 continue;
1086
Eric Anholt45388b52010-08-24 13:51:35 -07001087 if (strncmp(var->name, "gl_", 3) == 0) {
1088 /* At the moment, we don't allocate uniform locations for
1089 * builtin uniforms. It's permitted by spec, and we'll
1090 * likely switch to doing that at some point, but not yet.
Eric Anholt8d61a232010-08-05 16:00:46 -07001091 */
1092 continue;
1093 }
Ian Romanick019a59b2010-06-21 16:10:42 -07001094
Ian Romanick019a59b2010-06-21 16:10:42 -07001095 var->location = next_position;
Eric Anholt45388b52010-08-24 13:51:35 -07001096 add_uniform(mem_ctx, &uniforms, ht, var->name, var->type,
1097 prog->_LinkedShaders[i]->Type,
1098 &next_position, &total_uniforms);
Ian Romanick019a59b2010-06-21 16:10:42 -07001099 }
1100 }
1101
Eric Anholt45388b52010-08-24 13:51:35 -07001102 talloc_free(mem_ctx);
1103
Ian Romanick019a59b2010-06-21 16:10:42 -07001104 gl_uniform_list *ul = (gl_uniform_list *)
1105 calloc(1, sizeof(gl_uniform_list));
1106
1107 ul->Size = total_uniforms;
1108 ul->NumUniforms = total_uniforms;
1109 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
1110
1111 unsigned idx = 0;
1112 uniform_node *next;
1113 for (uniform_node *node = (uniform_node *) uniforms.head
1114 ; node->link.next != NULL
1115 ; node = next) {
1116 next = (uniform_node *) node->link.next;
1117
1118 node->link.remove();
Eric Anholt45388b52010-08-24 13:51:35 -07001119 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform));
1120 idx++;
Ian Romanick019a59b2010-06-21 16:10:42 -07001121
1122 free(node->u);
1123 free(node);
1124 }
1125
1126 hash_table_dtor(ht);
1127
Ian Romanickabee16e2010-06-21 16:16:05 -07001128 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -07001129}
1130
1131
Ian Romanick69846702010-06-22 17:29:19 -07001132/**
1133 * Find a contiguous set of available bits in a bitmask
1134 *
1135 * \param used_mask Bits representing used (1) and unused (0) locations
1136 * \param needed_count Number of contiguous bits needed.
1137 *
1138 * \return
1139 * Base location of the available bits on success or -1 on failure.
1140 */
1141int
1142find_available_slots(unsigned used_mask, unsigned needed_count)
1143{
1144 unsigned needed_mask = (1 << needed_count) - 1;
1145 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1146
1147 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1148 * cannot optimize possibly infinite loops" for the loop below.
1149 */
1150 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1151 return -1;
1152
1153 for (int i = 0; i <= max_bit_to_test; i++) {
1154 if ((needed_mask & ~used_mask) == needed_mask)
1155 return i;
1156
1157 needed_mask <<= 1;
1158 }
1159
1160 return -1;
1161}
1162
1163
1164bool
Eric Anholt849e1812010-06-30 11:49:17 -07001165assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001166{
Ian Romanick9342d262010-06-22 17:41:37 -07001167 /* Mark invalid attribute locations as being used.
1168 */
1169 unsigned used_locations = (max_attribute_index >= 32)
1170 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001171
Eric Anholt16b68b12010-06-30 11:05:43 -07001172 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001173 assert(sh->Type == GL_VERTEX_SHADER);
1174
Ian Romanick69846702010-06-22 17:29:19 -07001175 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001176 *
1177 * 1. Invalidate the location assignments for all vertex shader inputs.
1178 *
1179 * 2. Assign locations for inputs that have user-defined (via
1180 * glBindVertexAttribLocation) locatoins.
1181 *
Ian Romanick69846702010-06-22 17:29:19 -07001182 * 3. Sort the attributes without assigned locations by number of slots
1183 * required in decreasing order. Fragmentation caused by attribute
1184 * locations assigned by the application may prevent large attributes
1185 * from having enough contiguous space.
1186 *
1187 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001188 */
1189
1190 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
1191
Ian Romanick553dcdc2010-06-23 12:14:02 -07001192 if (prog->Attributes != NULL) {
1193 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001194 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -07001195 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001196
Ian Romanick69846702010-06-22 17:29:19 -07001197 /* Note: attributes that occupy multiple slots, such as arrays or
1198 * matrices, may appear in the attrib array multiple times.
1199 */
1200 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001201 continue;
1202
Ian Romanick69846702010-06-22 17:29:19 -07001203 /* From page 61 of the OpenGL 4.0 spec:
1204 *
1205 * "LinkProgram will fail if the attribute bindings assigned by
1206 * BindAttribLocation do not leave not enough space to assign a
1207 * location for an active matrix attribute or an active attribute
1208 * array, both of which require multiple contiguous generic
1209 * attributes."
1210 *
1211 * Previous versions of the spec contain similar language but omit the
1212 * bit about attribute arrays.
1213 *
1214 * Page 61 of the OpenGL 4.0 spec also says:
1215 *
1216 * "It is possible for an application to bind more than one
1217 * attribute name to the same location. This is referred to as
1218 * aliasing. This will only work if only one of the aliased
1219 * attributes is active in the executable program, or if no path
1220 * through the shader consumes more than one attribute of a set
1221 * of attributes aliased to the same location. A link error can
1222 * occur if the linker determines that every path through the
1223 * shader consumes multiple aliased attributes, but
1224 * implementations are not required to generate an error in this
1225 * case."
1226 *
1227 * These two paragraphs are either somewhat contradictory, or I don't
1228 * fully understand one or both of them.
1229 */
1230 /* FINISHME: The code as currently written does not support attribute
1231 * FINISHME: location aliasing (see comment above).
1232 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001233 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -07001234 const unsigned slots = count_attribute_slots(var->type);
1235
1236 /* Mask representing the contiguous slots that will be used by this
1237 * attribute.
1238 */
1239 const unsigned use_mask = (1 << slots) - 1;
1240
1241 /* Generate a link error if the set of bits requested for this
1242 * attribute overlaps any previously allocated bits.
1243 */
1244 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001245 linker_error_printf(prog,
1246 "insufficient contiguous attribute locations "
1247 "available for vertex shader input `%s'",
1248 var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001249 return false;
1250 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001251
1252 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -07001253 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001254 }
1255 }
1256
Ian Romanick69846702010-06-22 17:29:19 -07001257 /* Temporary storage for the set of attributes that need locations assigned.
1258 */
1259 struct temp_attr {
1260 unsigned slots;
1261 ir_variable *var;
1262
1263 /* Used below in the call to qsort. */
1264 static int compare(const void *a, const void *b)
1265 {
1266 const temp_attr *const l = (const temp_attr *) a;
1267 const temp_attr *const r = (const temp_attr *) b;
1268
1269 /* Reversed because we want a descending order sort below. */
1270 return r->slots - l->slots;
1271 }
1272 } to_assign[16];
1273
1274 unsigned num_attr = 0;
1275
Eric Anholt16b68b12010-06-30 11:05:43 -07001276 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001277 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1278
1279 if ((var == NULL) || (var->mode != ir_var_in))
1280 continue;
1281
Ian Romanick68a4fc92010-10-07 17:21:22 -07001282 if (var->explicit_location) {
1283 const unsigned slots = count_attribute_slots(var->type);
1284 const unsigned use_mask = (1 << slots) - 1;
1285 const int attr = var->location - VERT_ATTRIB_GENERIC0;
1286
1287 if ((var->location >= (int)(max_attribute_index + VERT_ATTRIB_GENERIC0))
1288 || (var->location < 0)) {
1289 linker_error_printf(prog,
1290 "invalid explicit location %d specified for "
1291 "`%s'\n",
1292 (var->location < 0) ? var->location : attr,
1293 var->name);
1294 return false;
1295 } else if (var->location >= VERT_ATTRIB_GENERIC0) {
1296 used_locations |= (use_mask << attr);
1297 }
1298 }
1299
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001300 /* The location was explicitly assigned, nothing to do here.
1301 */
1302 if (var->location != -1)
1303 continue;
1304
Ian Romanick69846702010-06-22 17:29:19 -07001305 to_assign[num_attr].slots = count_attribute_slots(var->type);
1306 to_assign[num_attr].var = var;
1307 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001308 }
Ian Romanick69846702010-06-22 17:29:19 -07001309
1310 /* If all of the attributes were assigned locations by the application (or
1311 * are built-in attributes with fixed locations), return early. This should
1312 * be the common case.
1313 */
1314 if (num_attr == 0)
1315 return true;
1316
1317 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1318
Ian Romanick982e3792010-06-29 18:58:20 -07001319 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
1320 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
1321 * to prevent it from being automatically allocated below.
1322 */
Ian Romanickc33e78f2010-08-13 12:30:41 -07001323 find_deref_visitor find("gl_Vertex");
1324 find.run(sh->ir);
1325 if (find.variable_found())
1326 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -07001327
Ian Romanick69846702010-06-22 17:29:19 -07001328 for (unsigned i = 0; i < num_attr; i++) {
1329 /* Mask representing the contiguous slots that will be used by this
1330 * attribute.
1331 */
1332 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1333
1334 int location = find_available_slots(used_locations, to_assign[i].slots);
1335
1336 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001337 linker_error_printf(prog,
1338 "insufficient contiguous attribute locations "
1339 "available for vertex shader input `%s'",
1340 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001341 return false;
1342 }
1343
1344 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1345 used_locations |= (use_mask << location);
1346 }
1347
1348 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001349}
1350
1351
Ian Romanick40e114b2010-08-17 14:55:50 -07001352/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001353 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001354 */
1355void
Ian Romanickcc90e622010-10-19 17:59:10 -07001356demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001357{
1358 foreach_list(node, sh->ir) {
1359 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1360
Ian Romanickcc90e622010-10-19 17:59:10 -07001361 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001362 continue;
1363
Ian Romanickcc90e622010-10-19 17:59:10 -07001364 /* A shader 'in' or 'out' variable is only really an input or output if
1365 * its value is used by other shader stages. This will cause the variable
1366 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001367 */
1368 if (var->location == -1) {
1369 var->mode = ir_var_auto;
1370 }
1371 }
1372}
1373
1374
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001375void
Eric Anholtb7062832010-07-28 13:52:23 -07001376assign_varying_locations(struct gl_shader_program *prog,
1377 gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001378{
1379 /* FINISHME: Set dynamically when geometry shader support is added. */
1380 unsigned output_index = VERT_RESULT_VAR0;
1381 unsigned input_index = FRAG_ATTRIB_VAR0;
1382
1383 /* Operate in a total of three passes.
1384 *
1385 * 1. Assign locations for any matching inputs and outputs.
1386 *
1387 * 2. Mark output variables in the producer that do not have locations as
1388 * not being outputs. This lets the optimizer eliminate them.
1389 *
1390 * 3. Mark input variables in the consumer that do not have locations as
1391 * not being inputs. This lets the optimizer eliminate them.
1392 */
1393
1394 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1395 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1396
Eric Anholt16b68b12010-06-30 11:05:43 -07001397 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001398 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1399
1400 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1401 || (output_var->location != -1))
1402 continue;
1403
1404 ir_variable *const input_var =
1405 consumer->symbols->get_variable(output_var->name);
1406
1407 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1408 continue;
1409
1410 assert(input_var->location == -1);
1411
Ian Romanick0e59b262010-06-23 11:23:01 -07001412 output_var->location = output_index;
1413 input_var->location = input_index;
1414
Ian Romanickdf869d92010-08-30 15:37:44 -07001415 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1416 assert(!output_var->type->is_record());
1417
1418 if (output_var->type->is_array()) {
1419 const unsigned slots = output_var->type->length
1420 * output_var->type->fields.array->matrix_columns;
1421
1422 output_index += slots;
1423 input_index += slots;
1424 } else {
1425 const unsigned slots = output_var->type->matrix_columns;
1426
1427 output_index += slots;
1428 input_index += slots;
1429 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001430 }
1431
Eric Anholt16b68b12010-06-30 11:05:43 -07001432 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001433 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1434
1435 if ((var == NULL) || (var->mode != ir_var_in))
1436 continue;
1437
Eric Anholtb7062832010-07-28 13:52:23 -07001438 if (var->location == -1) {
1439 if (prog->Version <= 120) {
1440 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1441 *
1442 * Only those varying variables used (i.e. read) in
1443 * the fragment shader executable must be written to
1444 * by the vertex shader executable; declaring
1445 * superfluous varying variables in a vertex shader is
1446 * permissible.
1447 *
1448 * We interpret this text as meaning that the VS must
1449 * write the variable for the FS to read it. See
1450 * "glsl1-varying read but not written" in piglit.
1451 */
1452
1453 linker_error_printf(prog, "fragment shader varying %s not written "
1454 "by vertex shader\n.", var->name);
1455 prog->LinkStatus = false;
1456 }
1457
1458 /* An 'in' variable is only really a shader input if its
1459 * value is written by the previous stage.
1460 */
Eric Anholtb7062832010-07-28 13:52:23 -07001461 var->mode = ir_var_auto;
1462 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001463 }
1464}
1465
1466
1467void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001468link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001469{
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001470 void *mem_ctx = talloc_init("temporary linker context");
1471
Ian Romanick832dfa52010-06-17 15:04:20 -07001472 prog->LinkStatus = false;
1473 prog->Validated = false;
1474 prog->_Used = false;
1475
Ian Romanickf36460e2010-06-23 12:07:22 -07001476 if (prog->InfoLog != NULL)
1477 talloc_free(prog->InfoLog);
1478
1479 prog->InfoLog = talloc_strdup(NULL, "");
1480
Ian Romanick832dfa52010-06-17 15:04:20 -07001481 /* Separate the shaders into groups based on their type.
1482 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001483 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001484 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001485 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001486 unsigned num_frag_shaders = 0;
1487
Eric Anholt16b68b12010-06-30 11:05:43 -07001488 vert_shader_list = (struct gl_shader **)
1489 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001490 frag_shader_list = &vert_shader_list[prog->NumShaders];
1491
Ian Romanick25f51d32010-07-16 15:51:50 -07001492 unsigned min_version = UINT_MAX;
1493 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001494 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001495 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1496 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1497
Ian Romanick832dfa52010-06-17 15:04:20 -07001498 switch (prog->Shaders[i]->Type) {
1499 case GL_VERTEX_SHADER:
1500 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1501 num_vert_shaders++;
1502 break;
1503 case GL_FRAGMENT_SHADER:
1504 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1505 num_frag_shaders++;
1506 break;
1507 case GL_GEOMETRY_SHADER:
1508 /* FINISHME: Support geometry shaders. */
1509 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1510 break;
1511 }
1512 }
1513
Ian Romanick25f51d32010-07-16 15:51:50 -07001514 /* Previous to GLSL version 1.30, different compilation units could mix and
1515 * match shading language versions. With GLSL 1.30 and later, the versions
1516 * of all shaders must match.
1517 */
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001518 assert(min_version >= 100);
Ian Romanick25f51d32010-07-16 15:51:50 -07001519 assert(max_version <= 130);
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001520 if ((max_version >= 130 || min_version == 100)
1521 && min_version != max_version) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001522 linker_error_printf(prog, "all shaders must use same shading "
1523 "language version\n");
1524 goto done;
1525 }
1526
1527 prog->Version = max_version;
1528
Ian Romanick3322fba2010-10-14 13:28:42 -07001529 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1530 if (prog->_LinkedShaders[i] != NULL)
1531 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1532
1533 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07001534 }
1535
Ian Romanickcd6764e2010-07-16 16:00:07 -07001536 /* Link all shaders for a particular stage and validate the result.
1537 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001538 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001539 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001540 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1541 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001542
1543 if (sh == NULL)
1544 goto done;
1545
1546 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001547 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001548
Ian Romanick3322fba2010-10-14 13:28:42 -07001549 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1550 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001551 }
1552
1553 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001554 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001555 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1556 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001557
1558 if (sh == NULL)
1559 goto done;
1560
1561 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001562 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001563
Ian Romanick3322fba2010-10-14 13:28:42 -07001564 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1565 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001566 }
1567
Ian Romanick3ed850e2010-06-23 12:18:21 -07001568 /* Here begins the inter-stage linking phase. Some initial validation is
1569 * performed, then locations are assigned for uniforms, attributes, and
1570 * varyings.
1571 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001572 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07001573 unsigned prev;
1574
1575 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1576 if (prog->_LinkedShaders[prev] != NULL)
1577 break;
1578 }
1579
Ian Romanick37101922010-06-18 19:02:10 -07001580 /* Validate the inputs of each stage with the output of the preceeding
1581 * stage.
1582 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001583 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1584 if (prog->_LinkedShaders[i] == NULL)
1585 continue;
1586
Ian Romanickf36460e2010-06-23 12:07:22 -07001587 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07001588 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07001589 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001590 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001591
1592 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07001593 }
1594
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001595 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001596 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001597
Eric Anholt2f4fe152010-08-10 13:06:49 -07001598 /* Do common optimization before assigning storage for attributes,
1599 * uniforms, and varyings. Later optimization could possibly make
1600 * some of that unused.
1601 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001602 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1603 if (prog->_LinkedShaders[i] == NULL)
1604 continue;
1605
Luca Barbierie591c462010-09-05 22:29:58 +02001606 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
Eric Anholt2f4fe152010-08-10 13:06:49 -07001607 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001608 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001609
Eric Anholt586b4b52010-09-28 14:32:16 -07001610 update_array_sizes(prog);
1611
Ian Romanickabee16e2010-06-21 16:16:05 -07001612 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001613
Ian Romanick3322fba2010-10-14 13:28:42 -07001614 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
Ian Romanick9342d262010-06-22 17:41:37 -07001615 /* FINISHME: The value of the max_attribute_index parameter is
1616 * FINISHME: implementation dependent based on the value of
1617 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1618 * FINISHME: at least 16, so hardcode 16 for now.
1619 */
Ian Romanick4b5489d2010-10-07 17:20:15 -07001620 if (!assign_attribute_locations(prog, 16)) {
1621 prog->LinkStatus = false;
Ian Romanick69846702010-06-22 17:29:19 -07001622 goto done;
Ian Romanick4b5489d2010-10-07 17:20:15 -07001623 }
Ian Romanick40e114b2010-08-17 14:55:50 -07001624 }
1625
Ian Romanick3322fba2010-10-14 13:28:42 -07001626 unsigned prev;
1627 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1628 if (prog->_LinkedShaders[prev] != NULL)
1629 break;
1630 }
1631
1632 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1633 if (prog->_LinkedShaders[i] == NULL)
1634 continue;
1635
Eric Anholtb7062832010-07-28 13:52:23 -07001636 assign_varying_locations(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07001637 prog->_LinkedShaders[prev],
Ian Romanick0e59b262010-06-23 11:23:01 -07001638 prog->_LinkedShaders[i]);
Ian Romanick3322fba2010-10-14 13:28:42 -07001639 prev = i;
1640 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001641
Ian Romanickcc90e622010-10-19 17:59:10 -07001642 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1643 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
1644 ir_var_out);
1645 }
1646
1647 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
1648 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
1649
1650 demote_shader_inputs_and_outputs(sh, ir_var_in);
1651 demote_shader_inputs_and_outputs(sh, ir_var_inout);
1652 demote_shader_inputs_and_outputs(sh, ir_var_out);
1653 }
1654
1655 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
1656 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1657
1658 demote_shader_inputs_and_outputs(sh, ir_var_in);
1659 }
1660
Ian Romanick13e10e42010-06-21 12:03:24 -07001661 /* FINISHME: Assign fragment shader output locations. */
1662
Ian Romanick832dfa52010-06-17 15:04:20 -07001663done:
1664 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001665
1666 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1667 if (prog->_LinkedShaders[i] == NULL)
1668 continue;
1669
1670 /* Retain any live IR, but trash the rest. */
1671 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
1672 }
1673
1674 talloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07001675}