blob: 7bff859d554519322e16de76b1fb8fd83942014b [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
Ian Romanick0ad22cd2010-06-21 17:18:31 -070075#include "main/mtypes.h"
Ian Romanick25f51d32010-07-16 15:51:50 -070076#include "main/macros.h"
Eric Anholtafe125e2010-07-26 17:47:59 -070077#include "main/shaderobj.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070078#include "glsl_symbol_table.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070079#include "ir.h"
80#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030081#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070082#include "linker.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070083#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070084
85/**
86 * Visitor that determines whether or not a variable is ever written.
87 */
88class find_assignment_visitor : public ir_hierarchical_visitor {
89public:
90 find_assignment_visitor(const char *name)
91 : name(name), found(false)
92 {
93 /* empty */
94 }
95
96 virtual ir_visitor_status visit_enter(ir_assignment *ir)
97 {
98 ir_variable *const var = ir->lhs->variable_referenced();
99
100 if (strcmp(name, var->name) == 0) {
101 found = true;
102 return visit_stop;
103 }
104
105 return visit_continue_with_parent;
106 }
107
108 bool variable_found()
109 {
110 return found;
111 }
112
113private:
114 const char *name; /**< Find writes to a variable with this name. */
115 bool found; /**< Was a write to the variable found? */
116};
117
Ian Romanickc93b8f12010-06-17 15:20:22 -0700118
Ian Romanickc33e78f2010-08-13 12:30:41 -0700119/**
120 * Visitor that determines whether or not a variable is ever read.
121 */
122class find_deref_visitor : public ir_hierarchical_visitor {
123public:
124 find_deref_visitor(const char *name)
125 : name(name), found(false)
126 {
127 /* empty */
128 }
129
130 virtual ir_visitor_status visit(ir_dereference_variable *ir)
131 {
132 if (strcmp(this->name, ir->var->name) == 0) {
133 this->found = true;
134 return visit_stop;
135 }
136
137 return visit_continue;
138 }
139
140 bool variable_found() const
141 {
142 return this->found;
143 }
144
145private:
146 const char *name; /**< Find writes to a variable with this name. */
147 bool found; /**< Was a write to the variable found? */
148};
149
150
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700151void
Eric Anholt849e1812010-06-30 11:49:17 -0700152linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700153{
154 va_list ap;
155
156 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
157 va_start(ap, fmt);
158 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
159 va_end(ap);
160}
161
162
163void
Eric Anholt16b68b12010-06-30 11:05:43 -0700164invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700165 int generic_base)
166{
Eric Anholt16b68b12010-06-30 11:05:43 -0700167 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700168 ir_variable *const var = ((ir_instruction *) node)->as_variable();
169
170 if ((var == NULL) || (var->mode != (unsigned) mode))
171 continue;
172
173 /* Only assign locations for generic attributes / varyings / etc.
174 */
175 if (var->location >= generic_base)
176 var->location = -1;
177 }
178}
179
180
Ian Romanickc93b8f12010-06-17 15:20:22 -0700181/**
Ian Romanick69846702010-06-22 17:29:19 -0700182 * Determine the number of attribute slots required for a particular type
183 *
184 * This code is here because it implements the language rules of a specific
185 * GLSL version. Since it's a property of the language and not a property of
186 * types in general, it doesn't really belong in glsl_type.
187 */
188unsigned
189count_attribute_slots(const glsl_type *t)
190{
191 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
192 *
193 * "A scalar input counts the same amount against this limit as a vec4,
194 * so applications may want to consider packing groups of four
195 * unrelated float inputs together into a vector to better utilize the
196 * capabilities of the underlying hardware. A matrix input will use up
197 * multiple locations. The number of locations used will equal the
198 * number of columns in the matrix."
199 *
200 * The spec does not explicitly say how arrays are counted. However, it
201 * should be safe to assume the total number of slots consumed by an array
202 * is the number of entries in the array multiplied by the number of slots
203 * consumed by a single element of the array.
204 */
205
206 if (t->is_array())
207 return t->array_size() * count_attribute_slots(t->element_type());
208
209 if (t->is_matrix())
210 return t->matrix_columns;
211
212 return 1;
213}
214
215
216/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700217 * Verify that a vertex shader executable meets all semantic requirements
218 *
219 * \param shader Vertex shader executable to be verified
220 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700221bool
Eric Anholt849e1812010-06-30 11:49:17 -0700222validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700223 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700224{
225 if (shader == NULL)
226 return true;
227
Ian Romanick832dfa52010-06-17 15:04:20 -0700228 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700229 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700230 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700231 linker_error_printf(prog,
232 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700233 return false;
234 }
235
236 return true;
237}
238
239
Ian Romanickc93b8f12010-06-17 15:20:22 -0700240/**
241 * Verify that a fragment shader executable meets all semantic requirements
242 *
243 * \param shader Fragment shader executable to be verified
244 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700245bool
Eric Anholt849e1812010-06-30 11:49:17 -0700246validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700247 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700248{
249 if (shader == NULL)
250 return true;
251
Ian Romanick832dfa52010-06-17 15:04:20 -0700252 find_assignment_visitor frag_color("gl_FragColor");
253 find_assignment_visitor frag_data("gl_FragData");
254
Eric Anholt16b68b12010-06-30 11:05:43 -0700255 frag_color.run(shader->ir);
256 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700257
Ian Romanick832dfa52010-06-17 15:04:20 -0700258 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700259 linker_error_printf(prog, "fragment shader writes to both "
260 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700261 return false;
262 }
263
264 return true;
265}
266
267
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700268/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700269 * Generate a string describing the mode of a variable
270 */
271static const char *
272mode_string(const ir_variable *var)
273{
274 switch (var->mode) {
275 case ir_var_auto:
276 return (var->read_only) ? "global constant" : "global variable";
277
278 case ir_var_uniform: return "uniform";
279 case ir_var_in: return "shader input";
280 case ir_var_out: return "shader output";
281 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700282
283 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700284 default:
285 assert(!"Should not get here.");
286 return "invalid variable";
287 }
288}
289
290
291/**
292 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700293 */
294bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700295cross_validate_globals(struct gl_shader_program *prog,
296 struct gl_shader **shader_list,
297 unsigned num_shaders,
298 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700299{
300 /* Examine all of the uniforms in all of the shaders and cross validate
301 * them.
302 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700303 glsl_symbol_table variables;
304 for (unsigned i = 0; i < num_shaders; i++) {
305 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700306 ir_variable *const var = ((ir_instruction *) node)->as_variable();
307
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700308 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700309 continue;
310
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700311 if (uniforms_only && (var->mode != ir_var_uniform))
312 continue;
313
Ian Romanick7e2aa912010-07-19 17:12:42 -0700314 /* Don't cross validate temporaries that are at global scope. These
315 * will eventually get pulled into the shaders 'main'.
316 */
317 if (var->mode == ir_var_temporary)
318 continue;
319
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700320 /* If a global with this name has already been seen, verify that the
321 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700322 * initializers, the values of the initializers must be the same.
323 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700324 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700325 if (existing != NULL) {
326 if (var->type != existing->type) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700327 linker_error_printf(prog, "%s `%s' declared as type "
Ian Romanickf36460e2010-06-23 12:07:22 -0700328 "`%s' and type `%s'\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700329 mode_string(var),
Ian Romanickf36460e2010-06-23 12:07:22 -0700330 var->name, var->type->name,
331 existing->type->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700332 return false;
333 }
334
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700335 /* FINISHME: Handle non-constant initializers.
336 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700337 if (var->constant_value != NULL) {
338 if (existing->constant_value != NULL) {
339 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700340 linker_error_printf(prog, "initializers for %s "
Ian Romanickf36460e2010-06-23 12:07:22 -0700341 "`%s' have differing values\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700342 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700343 return false;
344 }
345 } else
346 /* If the first-seen instance of a particular uniform did not
347 * have an initializer but a later instance does, copy the
348 * initializer to the version stored in the symbol table.
349 */
Ian Romanickde415b72010-07-14 13:22:12 -0700350 /* FINISHME: This is wrong. The constant_value field should
351 * FINISHME: not be modified! Imagine a case where a shader
352 * FINISHME: without an initializer is linked in two different
353 * FINISHME: programs with shaders that have differing
354 * FINISHME: initializers. Linking with the first will
355 * FINISHME: modify the shader, and linking with the second
356 * FINISHME: will fail.
357 */
Eric Anholt8273bd42010-08-04 12:34:56 -0700358 existing->constant_value =
359 var->constant_value->clone(talloc_parent(existing), NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700360 }
361 } else
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700362 variables.add_variable(var->name, var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700363 }
364 }
365
366 return true;
367}
368
369
Ian Romanick37101922010-06-18 19:02:10 -0700370/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700371 * Perform validation of uniforms used across multiple shader stages
372 */
373bool
374cross_validate_uniforms(struct gl_shader_program *prog)
375{
376 return cross_validate_globals(prog, prog->_LinkedShaders,
377 prog->_NumLinkedShaders, true);
378}
379
380
381/**
Ian Romanick37101922010-06-18 19:02:10 -0700382 * Validate that outputs from one stage match inputs of another
383 */
384bool
Eric Anholt849e1812010-06-30 11:49:17 -0700385cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700386 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700387{
388 glsl_symbol_table parameters;
389 /* FINISHME: Figure these out dynamically. */
390 const char *const producer_stage = "vertex";
391 const char *const consumer_stage = "fragment";
392
393 /* Find all shader outputs in the "producer" stage.
394 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700395 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700396 ir_variable *const var = ((ir_instruction *) node)->as_variable();
397
398 /* FINISHME: For geometry shaders, this should also look for inout
399 * FINISHME: variables.
400 */
401 if ((var == NULL) || (var->mode != ir_var_out))
402 continue;
403
404 parameters.add_variable(var->name, var);
405 }
406
407
408 /* Find all shader inputs in the "consumer" stage. Any variables that have
409 * matching outputs already in the symbol table must have the same type and
410 * qualifiers.
411 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700412 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700413 ir_variable *const input = ((ir_instruction *) node)->as_variable();
414
415 /* FINISHME: For geometry shaders, this should also look for inout
416 * FINISHME: variables.
417 */
418 if ((input == NULL) || (input->mode != ir_var_in))
419 continue;
420
421 ir_variable *const output = parameters.get_variable(input->name);
422 if (output != NULL) {
423 /* Check that the types match between stages.
424 */
425 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700426 linker_error_printf(prog,
427 "%s shader output `%s' delcared as "
428 "type `%s', but %s shader input declared "
429 "as type `%s'\n",
430 producer_stage, output->name,
431 output->type->name,
432 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700433 return false;
434 }
435
436 /* Check that all of the qualifiers match between stages.
437 */
438 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700439 linker_error_printf(prog,
440 "%s shader output `%s' %s centroid qualifier, "
441 "but %s shader input %s centroid qualifier\n",
442 producer_stage,
443 output->name,
444 (output->centroid) ? "has" : "lacks",
445 consumer_stage,
446 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700447 return false;
448 }
449
450 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700451 linker_error_printf(prog,
452 "%s shader output `%s' %s invariant qualifier, "
453 "but %s shader input %s invariant qualifier\n",
454 producer_stage,
455 output->name,
456 (output->invariant) ? "has" : "lacks",
457 consumer_stage,
458 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700459 return false;
460 }
461
462 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700463 linker_error_printf(prog,
464 "%s shader output `%s' specifies %s "
465 "interpolation qualifier, "
466 "but %s shader input specifies %s "
467 "interpolation qualifier\n",
468 producer_stage,
469 output->name,
470 output->interpolation_string(),
471 consumer_stage,
472 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700473 return false;
474 }
475 }
476 }
477
478 return true;
479}
480
481
Ian Romanick3fb87872010-07-09 14:09:34 -0700482/**
483 * Populates a shaders symbol table with all global declarations
484 */
485static void
486populate_symbol_table(gl_shader *sh)
487{
488 sh->symbols = new(sh) glsl_symbol_table;
489
490 foreach_list(node, sh->ir) {
491 ir_instruction *const inst = (ir_instruction *) node;
492 ir_variable *var;
493 ir_function *func;
494
495 if ((func = inst->as_function()) != NULL) {
496 sh->symbols->add_function(func->name, func);
497 } else if ((var = inst->as_variable()) != NULL) {
498 sh->symbols->add_variable(var->name, var);
499 }
500 }
501}
502
503
504/**
Ian Romanick31a97862010-07-12 18:48:50 -0700505 * Remap variables referenced in an instruction tree
506 *
507 * This is used when instruction trees are cloned from one shader and placed in
508 * another. These trees will contain references to \c ir_variable nodes that
509 * do not exist in the target shader. This function finds these \c ir_variable
510 * references and replaces the references with matching variables in the target
511 * shader.
512 *
513 * If there is no matching variable in the target shader, a clone of the
514 * \c ir_variable is made and added to the target shader. The new variable is
515 * added to \b both the instruction stream and the symbol table.
516 *
517 * \param inst IR tree that is to be processed.
518 * \param symbols Symbol table containing global scope symbols in the
519 * linked shader.
520 * \param instructions Instruction stream where new variable declarations
521 * should be added.
522 */
523void
Eric Anholt8273bd42010-08-04 12:34:56 -0700524remap_variables(ir_instruction *inst, struct gl_shader *target,
525 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700526{
527 class remap_visitor : public ir_hierarchical_visitor {
528 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700529 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700530 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700531 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700532 this->target = target;
533 this->symbols = target->symbols;
534 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700535 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700536 }
537
538 virtual ir_visitor_status visit(ir_dereference_variable *ir)
539 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700540 if (ir->var->mode == ir_var_temporary) {
541 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
542
543 assert(var != NULL);
544 ir->var = var;
545 return visit_continue;
546 }
547
Ian Romanick31a97862010-07-12 18:48:50 -0700548 ir_variable *const existing =
549 this->symbols->get_variable(ir->var->name);
550 if (existing != NULL)
551 ir->var = existing;
552 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700553 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700554
555 this->symbols->add_variable(copy->name, copy);
556 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700557 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700558 }
559
560 return visit_continue;
561 }
562
563 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700564 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700565 glsl_symbol_table *symbols;
566 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700567 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700568 };
569
Eric Anholt8273bd42010-08-04 12:34:56 -0700570 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700571
572 inst->accept(&v);
573}
574
575
576/**
577 * Move non-declarations from one instruction stream to another
578 *
579 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700580 * 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 -0700581 * pointer) for \c last and \c false for \c make_copies on the first
582 * call. Successive calls pass the return value of the previous call for
583 * \c last and \c true for \c make_copies.
584 *
585 * \param instructions Source instruction stream
586 * \param last Instruction after which new instructions should be
587 * inserted in the target instruction stream
588 * \param make_copies Flag selecting whether instructions in \c instructions
589 * should be copied (via \c ir_instruction::clone) into the
590 * target list or moved.
591 *
592 * \return
593 * The new "last" instruction in the target instruction stream. This pointer
594 * is suitable for use as the \c last parameter of a later call to this
595 * function.
596 */
597exec_node *
598move_non_declarations(exec_list *instructions, exec_node *last,
599 bool make_copies, gl_shader *target)
600{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700601 hash_table *temps = NULL;
602
603 if (make_copies)
604 temps = hash_table_ctor(0, hash_table_pointer_hash,
605 hash_table_pointer_compare);
606
Ian Romanick303c99f2010-07-19 12:34:56 -0700607 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700608 ir_instruction *inst = (ir_instruction *) node;
609
Ian Romanick7e2aa912010-07-19 17:12:42 -0700610 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700611 continue;
612
Ian Romanick7e2aa912010-07-19 17:12:42 -0700613 ir_variable *var = inst->as_variable();
614 if ((var != NULL) && (var->mode != ir_var_temporary))
615 continue;
616
617 assert(inst->as_assignment()
618 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700619
620 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700621 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700622
623 if (var != NULL)
624 hash_table_insert(temps, inst, var);
625 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700626 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700627 } else {
628 inst->remove();
629 }
630
631 last->insert_after(inst);
632 last = inst;
633 }
634
Ian Romanick7e2aa912010-07-19 17:12:42 -0700635 if (make_copies)
636 hash_table_dtor(temps);
637
Ian Romanick31a97862010-07-12 18:48:50 -0700638 return last;
639}
640
641/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700642 * Get the function signature for main from a shader
643 */
644static ir_function_signature *
645get_main_function_signature(gl_shader *sh)
646{
647 ir_function *const f = sh->symbols->get_function("main");
648 if (f != NULL) {
649 exec_list void_parameters;
650
651 /* Look for the 'void main()' signature and ensure that it's defined.
652 * This keeps the linker from accidentally pick a shader that just
653 * contains a prototype for main.
654 *
655 * We don't have to check for multiple definitions of main (in multiple
656 * shaders) because that would have already been caught above.
657 */
658 ir_function_signature *sig = f->matching_signature(&void_parameters);
659 if ((sig != NULL) && sig->is_defined) {
660 return sig;
661 }
662 }
663
664 return NULL;
665}
666
667
668/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700669 * Combine a group of shaders for a single stage to generate a linked shader
670 *
671 * \note
672 * If this function is supplied a single shader, it is cloned, and the new
673 * shader is returned.
674 */
675static struct gl_shader *
676link_intrastage_shaders(struct gl_shader_program *prog,
677 struct gl_shader **shader_list,
678 unsigned num_shaders)
679{
Ian Romanick13f782c2010-06-29 18:53:38 -0700680 /* Check that global variables defined in multiple shaders are consistent.
681 */
682 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
683 return NULL;
684
685 /* Check that there is only a single definition of each function signature
686 * across all shaders.
687 */
688 for (unsigned i = 0; i < (num_shaders - 1); i++) {
689 foreach_list(node, shader_list[i]->ir) {
690 ir_function *const f = ((ir_instruction *) node)->as_function();
691
692 if (f == NULL)
693 continue;
694
695 for (unsigned j = i + 1; j < num_shaders; j++) {
696 ir_function *const other =
697 shader_list[j]->symbols->get_function(f->name);
698
699 /* If the other shader has no function (and therefore no function
700 * signatures) with the same name, skip to the next shader.
701 */
702 if (other == NULL)
703 continue;
704
705 foreach_iter (exec_list_iterator, iter, *f) {
706 ir_function_signature *sig =
707 (ir_function_signature *) iter.get();
708
709 if (!sig->is_defined || sig->is_built_in)
710 continue;
711
712 ir_function_signature *other_sig =
713 other->exact_matching_signature(& sig->parameters);
714
715 if ((other_sig != NULL) && other_sig->is_defined
716 && !other_sig->is_built_in) {
717 linker_error_printf(prog,
718 "function `%s' is multiply defined",
719 f->name);
720 return NULL;
721 }
722 }
723 }
724 }
725 }
726
727 /* Find the shader that defines main, and make a clone of it.
728 *
729 * Starting with the clone, search for undefined references. If one is
730 * found, find the shader that defines it. Clone the reference and add
731 * it to the shader. Repeat until there are no undefined references or
732 * until a reference cannot be resolved.
733 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700734 gl_shader *main = NULL;
735 for (unsigned i = 0; i < num_shaders; i++) {
736 if (get_main_function_signature(shader_list[i]) != NULL) {
737 main = shader_list[i];
738 break;
739 }
740 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700741
Ian Romanick15ce87e2010-07-09 15:28:22 -0700742 if (main == NULL) {
743 linker_error_printf(prog, "%s shader lacks `main'\n",
744 (shader_list[0]->Type == GL_VERTEX_SHADER)
745 ? "vertex" : "fragment");
746 return NULL;
747 }
748
749 gl_shader *const linked = _mesa_new_shader(NULL, 0, main->Type);
750 linked->ir = new(linked) exec_list;
Eric Anholt8273bd42010-08-04 12:34:56 -0700751 clone_ir_list(linked, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700752
753 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700754
Ian Romanick31a97862010-07-12 18:48:50 -0700755 /* The a pointer to the main function in the final linked shader (i.e., the
756 * copy of the original shader that contained the main function).
757 */
758 ir_function_signature *const main_sig = get_main_function_signature(linked);
759
760 /* Move any instructions other than variable declarations or function
761 * declarations into main.
762 */
Ian Romanick9303e352010-07-19 12:33:54 -0700763 exec_node *insertion_point =
764 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
765 linked);
766
Ian Romanick31a97862010-07-12 18:48:50 -0700767 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700768 if (shader_list[i] == main)
769 continue;
770
Ian Romanick31a97862010-07-12 18:48:50 -0700771 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700772 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700773 }
774
Ian Romanick13f782c2010-06-29 18:53:38 -0700775 /* Resolve initializers for global variables in the linked shader.
776 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700777 unsigned num_linking_shaders = num_shaders;
778 for (unsigned i = 0; i < num_shaders; i++)
779 num_linking_shaders += shader_list[i]->num_builtins_to_link;
780
781 gl_shader **linking_shaders =
782 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
783
784 memcpy(linking_shaders, shader_list,
785 sizeof(linking_shaders[0]) * num_shaders);
786
787 unsigned idx = num_shaders;
788 for (unsigned i = 0; i < num_shaders; i++) {
789 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
790 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
791 idx += shader_list[i]->num_builtins_to_link;
792 }
793
794 assert(idx == num_linking_shaders);
795
796 link_function_calls(prog, linked, linking_shaders, num_linking_shaders);
797
798 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -0700799
Ian Romanick3fb87872010-07-09 14:09:34 -0700800 return linked;
801}
802
803
Ian Romanick019a59b2010-06-21 16:10:42 -0700804struct uniform_node {
805 exec_node link;
806 struct gl_uniform *u;
807 unsigned slots;
808};
809
Ian Romanickabee16e2010-06-21 16:16:05 -0700810void
Eric Anholt849e1812010-06-30 11:49:17 -0700811assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700812{
813 /* */
814 exec_list uniforms;
815 unsigned total_uniforms = 0;
816 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
817 hash_table_string_compare);
818
Ian Romanickabee16e2010-06-21 16:16:05 -0700819 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700820 unsigned next_position = 0;
821
Eric Anholt16b68b12010-06-30 11:05:43 -0700822 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700823 ir_variable *const var = ((ir_instruction *) node)->as_variable();
824
825 if ((var == NULL) || (var->mode != ir_var_uniform))
826 continue;
827
828 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
Eric Anholt8d61a232010-08-05 16:00:46 -0700829 if (vec4_slots == 0) {
830 /* If we've got a sampler or an aggregate of them, the size can
831 * end up zero. Don't allocate any space.
832 */
833 continue;
834 }
Ian Romanick019a59b2010-06-21 16:10:42 -0700835
836 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
837 if (n == NULL) {
838 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
839 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
840 n->slots = vec4_slots;
841
842 n->u[0].Name = strdup(var->name);
843 for (unsigned j = 1; j < vec4_slots; j++)
844 n->u[j].Name = n->u[0].Name;
845
846 hash_table_insert(ht, n, n->u[0].Name);
847 uniforms.push_tail(& n->link);
848 total_uniforms += vec4_slots;
849 }
850
851 if (var->constant_value != NULL)
852 for (unsigned j = 0; j < vec4_slots; j++)
853 n->u[j].Initialized = true;
854
855 var->location = next_position;
856
857 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700858 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700859 case GL_VERTEX_SHADER:
860 n->u[j].VertPos = next_position;
861 break;
862 case GL_FRAGMENT_SHADER:
863 n->u[j].FragPos = next_position;
864 break;
865 case GL_GEOMETRY_SHADER:
866 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700867 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700868 break;
869 }
870
871 next_position++;
872 }
873 }
874 }
875
876 gl_uniform_list *ul = (gl_uniform_list *)
877 calloc(1, sizeof(gl_uniform_list));
878
879 ul->Size = total_uniforms;
880 ul->NumUniforms = total_uniforms;
881 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
882
883 unsigned idx = 0;
884 uniform_node *next;
885 for (uniform_node *node = (uniform_node *) uniforms.head
886 ; node->link.next != NULL
887 ; node = next) {
888 next = (uniform_node *) node->link.next;
889
890 node->link.remove();
891 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
892 idx += node->slots;
893
894 free(node->u);
895 free(node);
896 }
897
898 hash_table_dtor(ht);
899
Ian Romanickabee16e2010-06-21 16:16:05 -0700900 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700901}
902
903
Ian Romanick69846702010-06-22 17:29:19 -0700904/**
905 * Find a contiguous set of available bits in a bitmask
906 *
907 * \param used_mask Bits representing used (1) and unused (0) locations
908 * \param needed_count Number of contiguous bits needed.
909 *
910 * \return
911 * Base location of the available bits on success or -1 on failure.
912 */
913int
914find_available_slots(unsigned used_mask, unsigned needed_count)
915{
916 unsigned needed_mask = (1 << needed_count) - 1;
917 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
918
919 /* The comparison to 32 is redundant, but without it GCC emits "warning:
920 * cannot optimize possibly infinite loops" for the loop below.
921 */
922 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
923 return -1;
924
925 for (int i = 0; i <= max_bit_to_test; i++) {
926 if ((needed_mask & ~used_mask) == needed_mask)
927 return i;
928
929 needed_mask <<= 1;
930 }
931
932 return -1;
933}
934
935
936bool
Eric Anholt849e1812010-06-30 11:49:17 -0700937assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700938{
Ian Romanick9342d262010-06-22 17:41:37 -0700939 /* Mark invalid attribute locations as being used.
940 */
941 unsigned used_locations = (max_attribute_index >= 32)
942 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700943
Eric Anholt16b68b12010-06-30 11:05:43 -0700944 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700945 assert(sh->Type == GL_VERTEX_SHADER);
946
Ian Romanick69846702010-06-22 17:29:19 -0700947 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700948 *
949 * 1. Invalidate the location assignments for all vertex shader inputs.
950 *
951 * 2. Assign locations for inputs that have user-defined (via
952 * glBindVertexAttribLocation) locatoins.
953 *
Ian Romanick69846702010-06-22 17:29:19 -0700954 * 3. Sort the attributes without assigned locations by number of slots
955 * required in decreasing order. Fragmentation caused by attribute
956 * locations assigned by the application may prevent large attributes
957 * from having enough contiguous space.
958 *
959 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700960 */
961
962 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
963
Ian Romanick553dcdc2010-06-23 12:14:02 -0700964 if (prog->Attributes != NULL) {
965 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700966 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700967 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700968
Ian Romanick69846702010-06-22 17:29:19 -0700969 /* Note: attributes that occupy multiple slots, such as arrays or
970 * matrices, may appear in the attrib array multiple times.
971 */
972 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700973 continue;
974
Ian Romanick69846702010-06-22 17:29:19 -0700975 /* From page 61 of the OpenGL 4.0 spec:
976 *
977 * "LinkProgram will fail if the attribute bindings assigned by
978 * BindAttribLocation do not leave not enough space to assign a
979 * location for an active matrix attribute or an active attribute
980 * array, both of which require multiple contiguous generic
981 * attributes."
982 *
983 * Previous versions of the spec contain similar language but omit the
984 * bit about attribute arrays.
985 *
986 * Page 61 of the OpenGL 4.0 spec also says:
987 *
988 * "It is possible for an application to bind more than one
989 * attribute name to the same location. This is referred to as
990 * aliasing. This will only work if only one of the aliased
991 * attributes is active in the executable program, or if no path
992 * through the shader consumes more than one attribute of a set
993 * of attributes aliased to the same location. A link error can
994 * occur if the linker determines that every path through the
995 * shader consumes multiple aliased attributes, but
996 * implementations are not required to generate an error in this
997 * case."
998 *
999 * These two paragraphs are either somewhat contradictory, or I don't
1000 * fully understand one or both of them.
1001 */
1002 /* FINISHME: The code as currently written does not support attribute
1003 * FINISHME: location aliasing (see comment above).
1004 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001005 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -07001006 const unsigned slots = count_attribute_slots(var->type);
1007
1008 /* Mask representing the contiguous slots that will be used by this
1009 * attribute.
1010 */
1011 const unsigned use_mask = (1 << slots) - 1;
1012
1013 /* Generate a link error if the set of bits requested for this
1014 * attribute overlaps any previously allocated bits.
1015 */
1016 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001017 linker_error_printf(prog,
1018 "insufficient contiguous attribute locations "
1019 "available for vertex shader input `%s'",
1020 var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001021 return false;
1022 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001023
1024 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -07001025 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001026 }
1027 }
1028
Ian Romanick69846702010-06-22 17:29:19 -07001029 /* Temporary storage for the set of attributes that need locations assigned.
1030 */
1031 struct temp_attr {
1032 unsigned slots;
1033 ir_variable *var;
1034
1035 /* Used below in the call to qsort. */
1036 static int compare(const void *a, const void *b)
1037 {
1038 const temp_attr *const l = (const temp_attr *) a;
1039 const temp_attr *const r = (const temp_attr *) b;
1040
1041 /* Reversed because we want a descending order sort below. */
1042 return r->slots - l->slots;
1043 }
1044 } to_assign[16];
1045
1046 unsigned num_attr = 0;
1047
Eric Anholt16b68b12010-06-30 11:05:43 -07001048 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001049 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1050
1051 if ((var == NULL) || (var->mode != ir_var_in))
1052 continue;
1053
1054 /* The location was explicitly assigned, nothing to do here.
1055 */
1056 if (var->location != -1)
1057 continue;
1058
Ian Romanick69846702010-06-22 17:29:19 -07001059 to_assign[num_attr].slots = count_attribute_slots(var->type);
1060 to_assign[num_attr].var = var;
1061 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001062 }
Ian Romanick69846702010-06-22 17:29:19 -07001063
1064 /* If all of the attributes were assigned locations by the application (or
1065 * are built-in attributes with fixed locations), return early. This should
1066 * be the common case.
1067 */
1068 if (num_attr == 0)
1069 return true;
1070
1071 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1072
Ian Romanick982e3792010-06-29 18:58:20 -07001073 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
1074 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
1075 * to prevent it from being automatically allocated below.
1076 */
Ian Romanickc33e78f2010-08-13 12:30:41 -07001077 find_deref_visitor find("gl_Vertex");
1078 find.run(sh->ir);
1079 if (find.variable_found())
1080 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -07001081
Ian Romanick69846702010-06-22 17:29:19 -07001082 for (unsigned i = 0; i < num_attr; i++) {
1083 /* Mask representing the contiguous slots that will be used by this
1084 * attribute.
1085 */
1086 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1087
1088 int location = find_available_slots(used_locations, to_assign[i].slots);
1089
1090 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001091 linker_error_printf(prog,
1092 "insufficient contiguous attribute locations "
1093 "available for vertex shader input `%s'",
1094 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001095 return false;
1096 }
1097
1098 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1099 used_locations |= (use_mask << location);
1100 }
1101
1102 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001103}
1104
1105
1106void
Eric Anholtb7062832010-07-28 13:52:23 -07001107assign_varying_locations(struct gl_shader_program *prog,
1108 gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001109{
1110 /* FINISHME: Set dynamically when geometry shader support is added. */
1111 unsigned output_index = VERT_RESULT_VAR0;
1112 unsigned input_index = FRAG_ATTRIB_VAR0;
1113
1114 /* Operate in a total of three passes.
1115 *
1116 * 1. Assign locations for any matching inputs and outputs.
1117 *
1118 * 2. Mark output variables in the producer that do not have locations as
1119 * not being outputs. This lets the optimizer eliminate them.
1120 *
1121 * 3. Mark input variables in the consumer that do not have locations as
1122 * not being inputs. This lets the optimizer eliminate them.
1123 */
1124
1125 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1126 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1127
Eric Anholt16b68b12010-06-30 11:05:43 -07001128 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001129 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1130
1131 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1132 || (output_var->location != -1))
1133 continue;
1134
1135 ir_variable *const input_var =
1136 consumer->symbols->get_variable(output_var->name);
1137
1138 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1139 continue;
1140
1141 assert(input_var->location == -1);
1142
1143 /* FINISHME: Location assignment will need some changes when arrays,
1144 * FINISHME: matrices, and structures are allowed as shader inputs /
1145 * FINISHME: outputs.
1146 */
1147 output_var->location = output_index;
1148 input_var->location = input_index;
1149
1150 output_index++;
1151 input_index++;
1152 }
1153
Eric Anholt16b68b12010-06-30 11:05:43 -07001154 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001155 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1156
1157 if ((var == NULL) || (var->mode != ir_var_out))
1158 continue;
1159
1160 /* An 'out' variable is only really a shader output if its value is read
1161 * by the following stage.
1162 */
Eric Anholtc10a6852010-07-13 11:07:16 -07001163 if (var->location == -1) {
Eric Anholtc10a6852010-07-13 11:07:16 -07001164 var->mode = ir_var_auto;
1165 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001166 }
1167
Eric Anholt16b68b12010-06-30 11:05:43 -07001168 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001169 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1170
1171 if ((var == NULL) || (var->mode != ir_var_in))
1172 continue;
1173
Eric Anholtb7062832010-07-28 13:52:23 -07001174 if (var->location == -1) {
1175 if (prog->Version <= 120) {
1176 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1177 *
1178 * Only those varying variables used (i.e. read) in
1179 * the fragment shader executable must be written to
1180 * by the vertex shader executable; declaring
1181 * superfluous varying variables in a vertex shader is
1182 * permissible.
1183 *
1184 * We interpret this text as meaning that the VS must
1185 * write the variable for the FS to read it. See
1186 * "glsl1-varying read but not written" in piglit.
1187 */
1188
1189 linker_error_printf(prog, "fragment shader varying %s not written "
1190 "by vertex shader\n.", var->name);
1191 prog->LinkStatus = false;
1192 }
1193
1194 /* An 'in' variable is only really a shader input if its
1195 * value is written by the previous stage.
1196 */
Eric Anholtb7062832010-07-28 13:52:23 -07001197 var->mode = ir_var_auto;
1198 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001199 }
1200}
1201
1202
1203void
Eric Anholt849e1812010-06-30 11:49:17 -07001204link_shaders(struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001205{
1206 prog->LinkStatus = false;
1207 prog->Validated = false;
1208 prog->_Used = false;
1209
Ian Romanickf36460e2010-06-23 12:07:22 -07001210 if (prog->InfoLog != NULL)
1211 talloc_free(prog->InfoLog);
1212
1213 prog->InfoLog = talloc_strdup(NULL, "");
1214
Ian Romanick832dfa52010-06-17 15:04:20 -07001215 /* Separate the shaders into groups based on their type.
1216 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001217 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001218 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001219 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001220 unsigned num_frag_shaders = 0;
1221
Eric Anholt16b68b12010-06-30 11:05:43 -07001222 vert_shader_list = (struct gl_shader **)
1223 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001224 frag_shader_list = &vert_shader_list[prog->NumShaders];
1225
Ian Romanick25f51d32010-07-16 15:51:50 -07001226 unsigned min_version = UINT_MAX;
1227 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001228 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001229 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1230 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1231
Ian Romanick832dfa52010-06-17 15:04:20 -07001232 switch (prog->Shaders[i]->Type) {
1233 case GL_VERTEX_SHADER:
1234 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1235 num_vert_shaders++;
1236 break;
1237 case GL_FRAGMENT_SHADER:
1238 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1239 num_frag_shaders++;
1240 break;
1241 case GL_GEOMETRY_SHADER:
1242 /* FINISHME: Support geometry shaders. */
1243 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1244 break;
1245 }
1246 }
1247
Ian Romanick25f51d32010-07-16 15:51:50 -07001248 /* Previous to GLSL version 1.30, different compilation units could mix and
1249 * match shading language versions. With GLSL 1.30 and later, the versions
1250 * of all shaders must match.
1251 */
1252 assert(min_version >= 110);
1253 assert(max_version <= 130);
1254 if ((max_version >= 130) && (min_version != max_version)) {
1255 linker_error_printf(prog, "all shaders must use same shading "
1256 "language version\n");
1257 goto done;
1258 }
1259
1260 prog->Version = max_version;
1261
Ian Romanickcd6764e2010-07-16 16:00:07 -07001262 /* Link all shaders for a particular stage and validate the result.
1263 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001264 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001265 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001266 gl_shader *const sh =
1267 link_intrastage_shaders(prog, vert_shader_list, num_vert_shaders);
1268
1269 if (sh == NULL)
1270 goto done;
1271
1272 if (!validate_vertex_shader_executable(prog, sh))
1273 goto done;
1274
1275 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001276 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001277 }
1278
1279 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001280 gl_shader *const sh =
1281 link_intrastage_shaders(prog, frag_shader_list, num_frag_shaders);
1282
1283 if (sh == NULL)
1284 goto done;
1285
1286 if (!validate_fragment_shader_executable(prog, sh))
1287 goto done;
1288
1289 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001290 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001291 }
1292
Ian Romanick3ed850e2010-06-23 12:18:21 -07001293 /* Here begins the inter-stage linking phase. Some initial validation is
1294 * performed, then locations are assigned for uniforms, attributes, and
1295 * varyings.
1296 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001297 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -07001298 /* Validate the inputs of each stage with the output of the preceeding
1299 * stage.
1300 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001301 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -07001302 if (!cross_validate_outputs_to_inputs(prog,
1303 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -07001304 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001305 goto done;
1306 }
1307
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001308 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001309 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001310
Ian Romanick13e10e42010-06-21 12:03:24 -07001311 /* FINISHME: Perform whole-program optimization here. */
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001312 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
1313 /* Optimization passes */
1314 bool progress;
1315 exec_list *ir = prog->_LinkedShaders[i]->ir;
1316
1317 /* Lowering */
1318 do_mat_op_to_vec(ir);
1319 do_mod_to_fract(ir);
1320 do_div_to_mul_rcp(ir);
Eric Anholtbc4034b2010-08-05 15:22:05 -07001321 do_explog_to_explog2(ir);
Eric Anholt5854d452010-08-09 21:22:17 -07001322 do_sub_to_add_neg(ir);
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001323
1324 do {
1325 progress = false;
1326
1327 progress = do_function_inlining(ir) || progress;
Eric Anholt2e853ca2010-08-05 10:09:12 -07001328 progress = do_dead_functions(ir) || progress;
Eric Anholt7f7eaf02010-08-05 11:01:09 -07001329 progress = do_structure_splitting(ir) || progress;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001330 progress = do_if_simplification(ir) || progress;
1331 progress = do_copy_propagation(ir) || progress;
1332 progress = do_dead_code_local(ir) || progress;
Eric Anholt59c45e92010-07-27 14:32:21 -07001333 progress = do_dead_code(ir) || progress;
Eric Anholt78469542010-07-30 17:04:49 -07001334 progress = do_tree_grafting(ir) || progress;
Eric Anholt8bebbeb2010-08-09 17:03:46 -07001335 progress = do_constant_propagation(ir) || progress;
Eric Anholte3a90b82010-08-04 17:12:14 -07001336 progress = do_constant_variable(ir) || progress;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001337 progress = do_constant_folding(ir) || progress;
Eric Anholtf6b03f32010-07-31 15:47:35 -07001338 progress = do_algebraic(ir) || progress;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001339 progress = do_if_return(ir) || progress;
1340#if 0
1341 if (ctx->Shader.EmitNoIfs)
1342 progress = do_if_to_cond_assign(ir) || progress;
1343#endif
1344
1345 progress = do_vec_index_to_swizzle(ir) || progress;
1346 /* Do this one after the previous to let the easier pass handle
1347 * constant vector indexing.
1348 */
1349 progress = do_vec_index_to_cond_assign(ir) || progress;
1350
1351 progress = do_swizzle_swizzle(ir) || progress;
1352 } while (progress);
1353 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001354
Ian Romanickabee16e2010-06-21 16:16:05 -07001355 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001356
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001357 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER)
Ian Romanick9342d262010-06-22 17:41:37 -07001358 /* FINISHME: The value of the max_attribute_index parameter is
1359 * FINISHME: implementation dependent based on the value of
1360 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1361 * FINISHME: at least 16, so hardcode 16 for now.
1362 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001363 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -07001364 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -07001365
Ian Romanick0e59b262010-06-23 11:23:01 -07001366 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
Eric Anholtb7062832010-07-28 13:52:23 -07001367 assign_varying_locations(prog,
1368 prog->_LinkedShaders[i - 1],
Ian Romanick0e59b262010-06-23 11:23:01 -07001369 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -07001370
1371 /* FINISHME: Assign fragment shader output locations. */
1372
Ian Romanick832dfa52010-06-17 15:04:20 -07001373done:
1374 free(vert_shader_list);
1375}