blob: 4172e419105e18cb9334c0c9bed77ab694e9741b [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 Romanick45d97dd2010-08-16 13:59:34 -070075#include "main/compiler.h"
Ian Romanick0ad22cd2010-06-21 17:18:31 -070076#include "main/mtypes.h"
Ian Romanick25f51d32010-07-16 15:51:50 -070077#include "main/macros.h"
Eric Anholtafe125e2010-07-26 17:47:59 -070078#include "main/shaderobj.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070079#include "glsl_symbol_table.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070080#include "ir.h"
81#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030082#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070083#include "linker.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070084#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070085
86/**
87 * Visitor that determines whether or not a variable is ever written.
88 */
89class find_assignment_visitor : public ir_hierarchical_visitor {
90public:
91 find_assignment_visitor(const char *name)
92 : name(name), found(false)
93 {
94 /* empty */
95 }
96
97 virtual ir_visitor_status visit_enter(ir_assignment *ir)
98 {
99 ir_variable *const var = ir->lhs->variable_referenced();
100
101 if (strcmp(name, var->name) == 0) {
102 found = true;
103 return visit_stop;
104 }
105
106 return visit_continue_with_parent;
107 }
108
109 bool variable_found()
110 {
111 return found;
112 }
113
114private:
115 const char *name; /**< Find writes to a variable with this name. */
116 bool found; /**< Was a write to the variable found? */
117};
118
Ian Romanickc93b8f12010-06-17 15:20:22 -0700119
Ian Romanickc33e78f2010-08-13 12:30:41 -0700120/**
121 * Visitor that determines whether or not a variable is ever read.
122 */
123class find_deref_visitor : public ir_hierarchical_visitor {
124public:
125 find_deref_visitor(const char *name)
126 : name(name), found(false)
127 {
128 /* empty */
129 }
130
131 virtual ir_visitor_status visit(ir_dereference_variable *ir)
132 {
133 if (strcmp(this->name, ir->var->name) == 0) {
134 this->found = true;
135 return visit_stop;
136 }
137
138 return visit_continue;
139 }
140
141 bool variable_found() const
142 {
143 return this->found;
144 }
145
146private:
147 const char *name; /**< Find writes to a variable with this name. */
148 bool found; /**< Was a write to the variable found? */
149};
150
151
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700152void
Eric Anholt849e1812010-06-30 11:49:17 -0700153linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700154{
155 va_list ap;
156
157 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
158 va_start(ap, fmt);
159 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
160 va_end(ap);
161}
162
163
164void
Eric Anholt16b68b12010-06-30 11:05:43 -0700165invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700166 int generic_base)
167{
Eric Anholt16b68b12010-06-30 11:05:43 -0700168 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700169 ir_variable *const var = ((ir_instruction *) node)->as_variable();
170
171 if ((var == NULL) || (var->mode != (unsigned) mode))
172 continue;
173
174 /* Only assign locations for generic attributes / varyings / etc.
175 */
176 if (var->location >= generic_base)
177 var->location = -1;
178 }
179}
180
181
Ian Romanickc93b8f12010-06-17 15:20:22 -0700182/**
Ian Romanick69846702010-06-22 17:29:19 -0700183 * Determine the number of attribute slots required for a particular type
184 *
185 * This code is here because it implements the language rules of a specific
186 * GLSL version. Since it's a property of the language and not a property of
187 * types in general, it doesn't really belong in glsl_type.
188 */
189unsigned
190count_attribute_slots(const glsl_type *t)
191{
192 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
193 *
194 * "A scalar input counts the same amount against this limit as a vec4,
195 * so applications may want to consider packing groups of four
196 * unrelated float inputs together into a vector to better utilize the
197 * capabilities of the underlying hardware. A matrix input will use up
198 * multiple locations. The number of locations used will equal the
199 * number of columns in the matrix."
200 *
201 * The spec does not explicitly say how arrays are counted. However, it
202 * should be safe to assume the total number of slots consumed by an array
203 * is the number of entries in the array multiplied by the number of slots
204 * consumed by a single element of the array.
205 */
206
207 if (t->is_array())
208 return t->array_size() * count_attribute_slots(t->element_type());
209
210 if (t->is_matrix())
211 return t->matrix_columns;
212
213 return 1;
214}
215
216
217/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700218 * Verify that a vertex shader executable meets all semantic requirements
219 *
220 * \param shader Vertex shader executable to be verified
221 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700222bool
Eric Anholt849e1812010-06-30 11:49:17 -0700223validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700224 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700225{
226 if (shader == NULL)
227 return true;
228
Ian Romanick832dfa52010-06-17 15:04:20 -0700229 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700230 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700231 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700232 linker_error_printf(prog,
233 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700234 return false;
235 }
236
237 return true;
238}
239
240
Ian Romanickc93b8f12010-06-17 15:20:22 -0700241/**
242 * Verify that a fragment shader executable meets all semantic requirements
243 *
244 * \param shader Fragment shader executable to be verified
245 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700246bool
Eric Anholt849e1812010-06-30 11:49:17 -0700247validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700248 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700249{
250 if (shader == NULL)
251 return true;
252
Ian Romanick832dfa52010-06-17 15:04:20 -0700253 find_assignment_visitor frag_color("gl_FragColor");
254 find_assignment_visitor frag_data("gl_FragData");
255
Eric Anholt16b68b12010-06-30 11:05:43 -0700256 frag_color.run(shader->ir);
257 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700258
Ian Romanick832dfa52010-06-17 15:04:20 -0700259 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700260 linker_error_printf(prog, "fragment shader writes to both "
261 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700262 return false;
263 }
264
265 return true;
266}
267
268
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700269/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700270 * Generate a string describing the mode of a variable
271 */
272static const char *
273mode_string(const ir_variable *var)
274{
275 switch (var->mode) {
276 case ir_var_auto:
277 return (var->read_only) ? "global constant" : "global variable";
278
279 case ir_var_uniform: return "uniform";
280 case ir_var_in: return "shader input";
281 case ir_var_out: return "shader output";
282 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700283
284 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700285 default:
286 assert(!"Should not get here.");
287 return "invalid variable";
288 }
289}
290
291
292/**
293 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700294 */
295bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700296cross_validate_globals(struct gl_shader_program *prog,
297 struct gl_shader **shader_list,
298 unsigned num_shaders,
299 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700300{
301 /* Examine all of the uniforms in all of the shaders and cross validate
302 * them.
303 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700304 glsl_symbol_table variables;
305 for (unsigned i = 0; i < num_shaders; i++) {
306 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700307 ir_variable *const var = ((ir_instruction *) node)->as_variable();
308
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700309 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700310 continue;
311
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700312 if (uniforms_only && (var->mode != ir_var_uniform))
313 continue;
314
Ian Romanick7e2aa912010-07-19 17:12:42 -0700315 /* Don't cross validate temporaries that are at global scope. These
316 * will eventually get pulled into the shaders 'main'.
317 */
318 if (var->mode == ir_var_temporary)
319 continue;
320
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700321 /* If a global with this name has already been seen, verify that the
322 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700323 * initializers, the values of the initializers must be the same.
324 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700325 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700326 if (existing != NULL) {
327 if (var->type != existing->type) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700328 linker_error_printf(prog, "%s `%s' declared as type "
Ian Romanickf36460e2010-06-23 12:07:22 -0700329 "`%s' and type `%s'\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700330 mode_string(var),
Ian Romanickf36460e2010-06-23 12:07:22 -0700331 var->name, var->type->name,
332 existing->type->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700333 return false;
334 }
335
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700336 /* FINISHME: Handle non-constant initializers.
337 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700338 if (var->constant_value != NULL) {
339 if (existing->constant_value != NULL) {
340 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700341 linker_error_printf(prog, "initializers for %s "
Ian Romanickf36460e2010-06-23 12:07:22 -0700342 "`%s' have differing values\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700343 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700344 return false;
345 }
346 } else
347 /* If the first-seen instance of a particular uniform did not
348 * have an initializer but a later instance does, copy the
349 * initializer to the version stored in the symbol table.
350 */
Ian Romanickde415b72010-07-14 13:22:12 -0700351 /* FINISHME: This is wrong. The constant_value field should
352 * FINISHME: not be modified! Imagine a case where a shader
353 * FINISHME: without an initializer is linked in two different
354 * FINISHME: programs with shaders that have differing
355 * FINISHME: initializers. Linking with the first will
356 * FINISHME: modify the shader, and linking with the second
357 * FINISHME: will fail.
358 */
Eric Anholt8273bd42010-08-04 12:34:56 -0700359 existing->constant_value =
360 var->constant_value->clone(talloc_parent(existing), NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700361 }
362 } else
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700363 variables.add_variable(var->name, var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700364 }
365 }
366
367 return true;
368}
369
370
Ian Romanick37101922010-06-18 19:02:10 -0700371/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700372 * Perform validation of uniforms used across multiple shader stages
373 */
374bool
375cross_validate_uniforms(struct gl_shader_program *prog)
376{
377 return cross_validate_globals(prog, prog->_LinkedShaders,
378 prog->_NumLinkedShaders, true);
379}
380
381
382/**
Ian Romanick37101922010-06-18 19:02:10 -0700383 * Validate that outputs from one stage match inputs of another
384 */
385bool
Eric Anholt849e1812010-06-30 11:49:17 -0700386cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700387 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700388{
389 glsl_symbol_table parameters;
390 /* FINISHME: Figure these out dynamically. */
391 const char *const producer_stage = "vertex";
392 const char *const consumer_stage = "fragment";
393
394 /* Find all shader outputs in the "producer" stage.
395 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700396 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700397 ir_variable *const var = ((ir_instruction *) node)->as_variable();
398
399 /* FINISHME: For geometry shaders, this should also look for inout
400 * FINISHME: variables.
401 */
402 if ((var == NULL) || (var->mode != ir_var_out))
403 continue;
404
405 parameters.add_variable(var->name, var);
406 }
407
408
409 /* Find all shader inputs in the "consumer" stage. Any variables that have
410 * matching outputs already in the symbol table must have the same type and
411 * qualifiers.
412 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700413 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700414 ir_variable *const input = ((ir_instruction *) node)->as_variable();
415
416 /* FINISHME: For geometry shaders, this should also look for inout
417 * FINISHME: variables.
418 */
419 if ((input == NULL) || (input->mode != ir_var_in))
420 continue;
421
422 ir_variable *const output = parameters.get_variable(input->name);
423 if (output != NULL) {
424 /* Check that the types match between stages.
425 */
426 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700427 linker_error_printf(prog,
428 "%s shader output `%s' delcared as "
429 "type `%s', but %s shader input declared "
430 "as type `%s'\n",
431 producer_stage, output->name,
432 output->type->name,
433 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700434 return false;
435 }
436
437 /* Check that all of the qualifiers match between stages.
438 */
439 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700440 linker_error_printf(prog,
441 "%s shader output `%s' %s centroid qualifier, "
442 "but %s shader input %s centroid qualifier\n",
443 producer_stage,
444 output->name,
445 (output->centroid) ? "has" : "lacks",
446 consumer_stage,
447 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700448 return false;
449 }
450
451 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700452 linker_error_printf(prog,
453 "%s shader output `%s' %s invariant qualifier, "
454 "but %s shader input %s invariant qualifier\n",
455 producer_stage,
456 output->name,
457 (output->invariant) ? "has" : "lacks",
458 consumer_stage,
459 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700460 return false;
461 }
462
463 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700464 linker_error_printf(prog,
465 "%s shader output `%s' specifies %s "
466 "interpolation qualifier, "
467 "but %s shader input specifies %s "
468 "interpolation qualifier\n",
469 producer_stage,
470 output->name,
471 output->interpolation_string(),
472 consumer_stage,
473 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700474 return false;
475 }
476 }
477 }
478
479 return true;
480}
481
482
Ian Romanick3fb87872010-07-09 14:09:34 -0700483/**
484 * Populates a shaders symbol table with all global declarations
485 */
486static void
487populate_symbol_table(gl_shader *sh)
488{
489 sh->symbols = new(sh) glsl_symbol_table;
490
491 foreach_list(node, sh->ir) {
492 ir_instruction *const inst = (ir_instruction *) node;
493 ir_variable *var;
494 ir_function *func;
495
496 if ((func = inst->as_function()) != NULL) {
497 sh->symbols->add_function(func->name, func);
498 } else if ((var = inst->as_variable()) != NULL) {
499 sh->symbols->add_variable(var->name, var);
500 }
501 }
502}
503
504
505/**
Ian Romanick31a97862010-07-12 18:48:50 -0700506 * Remap variables referenced in an instruction tree
507 *
508 * This is used when instruction trees are cloned from one shader and placed in
509 * another. These trees will contain references to \c ir_variable nodes that
510 * do not exist in the target shader. This function finds these \c ir_variable
511 * references and replaces the references with matching variables in the target
512 * shader.
513 *
514 * If there is no matching variable in the target shader, a clone of the
515 * \c ir_variable is made and added to the target shader. The new variable is
516 * added to \b both the instruction stream and the symbol table.
517 *
518 * \param inst IR tree that is to be processed.
519 * \param symbols Symbol table containing global scope symbols in the
520 * linked shader.
521 * \param instructions Instruction stream where new variable declarations
522 * should be added.
523 */
524void
Eric Anholt8273bd42010-08-04 12:34:56 -0700525remap_variables(ir_instruction *inst, struct gl_shader *target,
526 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700527{
528 class remap_visitor : public ir_hierarchical_visitor {
529 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700530 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700531 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700532 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700533 this->target = target;
534 this->symbols = target->symbols;
535 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700536 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700537 }
538
539 virtual ir_visitor_status visit(ir_dereference_variable *ir)
540 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700541 if (ir->var->mode == ir_var_temporary) {
542 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
543
544 assert(var != NULL);
545 ir->var = var;
546 return visit_continue;
547 }
548
Ian Romanick31a97862010-07-12 18:48:50 -0700549 ir_variable *const existing =
550 this->symbols->get_variable(ir->var->name);
551 if (existing != NULL)
552 ir->var = existing;
553 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700554 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700555
556 this->symbols->add_variable(copy->name, copy);
557 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700558 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700559 }
560
561 return visit_continue;
562 }
563
564 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700565 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700566 glsl_symbol_table *symbols;
567 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700568 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700569 };
570
Eric Anholt8273bd42010-08-04 12:34:56 -0700571 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700572
573 inst->accept(&v);
574}
575
576
577/**
578 * Move non-declarations from one instruction stream to another
579 *
580 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700581 * 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 -0700582 * pointer) for \c last and \c false for \c make_copies on the first
583 * call. Successive calls pass the return value of the previous call for
584 * \c last and \c true for \c make_copies.
585 *
586 * \param instructions Source instruction stream
587 * \param last Instruction after which new instructions should be
588 * inserted in the target instruction stream
589 * \param make_copies Flag selecting whether instructions in \c instructions
590 * should be copied (via \c ir_instruction::clone) into the
591 * target list or moved.
592 *
593 * \return
594 * The new "last" instruction in the target instruction stream. This pointer
595 * is suitable for use as the \c last parameter of a later call to this
596 * function.
597 */
598exec_node *
599move_non_declarations(exec_list *instructions, exec_node *last,
600 bool make_copies, gl_shader *target)
601{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700602 hash_table *temps = NULL;
603
604 if (make_copies)
605 temps = hash_table_ctor(0, hash_table_pointer_hash,
606 hash_table_pointer_compare);
607
Ian Romanick303c99f2010-07-19 12:34:56 -0700608 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700609 ir_instruction *inst = (ir_instruction *) node;
610
Ian Romanick7e2aa912010-07-19 17:12:42 -0700611 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700612 continue;
613
Ian Romanick7e2aa912010-07-19 17:12:42 -0700614 ir_variable *var = inst->as_variable();
615 if ((var != NULL) && (var->mode != ir_var_temporary))
616 continue;
617
618 assert(inst->as_assignment()
619 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700620
621 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700622 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700623
624 if (var != NULL)
625 hash_table_insert(temps, inst, var);
626 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700627 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700628 } else {
629 inst->remove();
630 }
631
632 last->insert_after(inst);
633 last = inst;
634 }
635
Ian Romanick7e2aa912010-07-19 17:12:42 -0700636 if (make_copies)
637 hash_table_dtor(temps);
638
Ian Romanick31a97862010-07-12 18:48:50 -0700639 return last;
640}
641
642/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700643 * Get the function signature for main from a shader
644 */
645static ir_function_signature *
646get_main_function_signature(gl_shader *sh)
647{
648 ir_function *const f = sh->symbols->get_function("main");
649 if (f != NULL) {
650 exec_list void_parameters;
651
652 /* Look for the 'void main()' signature and ensure that it's defined.
653 * This keeps the linker from accidentally pick a shader that just
654 * contains a prototype for main.
655 *
656 * We don't have to check for multiple definitions of main (in multiple
657 * shaders) because that would have already been caught above.
658 */
659 ir_function_signature *sig = f->matching_signature(&void_parameters);
660 if ((sig != NULL) && sig->is_defined) {
661 return sig;
662 }
663 }
664
665 return NULL;
666}
667
668
669/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700670 * Combine a group of shaders for a single stage to generate a linked shader
671 *
672 * \note
673 * If this function is supplied a single shader, it is cloned, and the new
674 * shader is returned.
675 */
676static struct gl_shader *
677link_intrastage_shaders(struct gl_shader_program *prog,
678 struct gl_shader **shader_list,
679 unsigned num_shaders)
680{
Ian Romanick13f782c2010-06-29 18:53:38 -0700681 /* Check that global variables defined in multiple shaders are consistent.
682 */
683 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
684 return NULL;
685
686 /* Check that there is only a single definition of each function signature
687 * across all shaders.
688 */
689 for (unsigned i = 0; i < (num_shaders - 1); i++) {
690 foreach_list(node, shader_list[i]->ir) {
691 ir_function *const f = ((ir_instruction *) node)->as_function();
692
693 if (f == NULL)
694 continue;
695
696 for (unsigned j = i + 1; j < num_shaders; j++) {
697 ir_function *const other =
698 shader_list[j]->symbols->get_function(f->name);
699
700 /* If the other shader has no function (and therefore no function
701 * signatures) with the same name, skip to the next shader.
702 */
703 if (other == NULL)
704 continue;
705
706 foreach_iter (exec_list_iterator, iter, *f) {
707 ir_function_signature *sig =
708 (ir_function_signature *) iter.get();
709
710 if (!sig->is_defined || sig->is_built_in)
711 continue;
712
713 ir_function_signature *other_sig =
714 other->exact_matching_signature(& sig->parameters);
715
716 if ((other_sig != NULL) && other_sig->is_defined
717 && !other_sig->is_built_in) {
718 linker_error_printf(prog,
719 "function `%s' is multiply defined",
720 f->name);
721 return NULL;
722 }
723 }
724 }
725 }
726 }
727
728 /* Find the shader that defines main, and make a clone of it.
729 *
730 * Starting with the clone, search for undefined references. If one is
731 * found, find the shader that defines it. Clone the reference and add
732 * it to the shader. Repeat until there are no undefined references or
733 * until a reference cannot be resolved.
734 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700735 gl_shader *main = NULL;
736 for (unsigned i = 0; i < num_shaders; i++) {
737 if (get_main_function_signature(shader_list[i]) != NULL) {
738 main = shader_list[i];
739 break;
740 }
741 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700742
Ian Romanick15ce87e2010-07-09 15:28:22 -0700743 if (main == NULL) {
744 linker_error_printf(prog, "%s shader lacks `main'\n",
745 (shader_list[0]->Type == GL_VERTEX_SHADER)
746 ? "vertex" : "fragment");
747 return NULL;
748 }
749
750 gl_shader *const linked = _mesa_new_shader(NULL, 0, main->Type);
751 linked->ir = new(linked) exec_list;
Eric Anholt8273bd42010-08-04 12:34:56 -0700752 clone_ir_list(linked, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700753
754 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700755
Ian Romanick31a97862010-07-12 18:48:50 -0700756 /* The a pointer to the main function in the final linked shader (i.e., the
757 * copy of the original shader that contained the main function).
758 */
759 ir_function_signature *const main_sig = get_main_function_signature(linked);
760
761 /* Move any instructions other than variable declarations or function
762 * declarations into main.
763 */
Ian Romanick9303e352010-07-19 12:33:54 -0700764 exec_node *insertion_point =
765 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
766 linked);
767
Ian Romanick31a97862010-07-12 18:48:50 -0700768 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700769 if (shader_list[i] == main)
770 continue;
771
Ian Romanick31a97862010-07-12 18:48:50 -0700772 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700773 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700774 }
775
Ian Romanick13f782c2010-06-29 18:53:38 -0700776 /* Resolve initializers for global variables in the linked shader.
777 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700778 unsigned num_linking_shaders = num_shaders;
779 for (unsigned i = 0; i < num_shaders; i++)
780 num_linking_shaders += shader_list[i]->num_builtins_to_link;
781
782 gl_shader **linking_shaders =
783 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
784
785 memcpy(linking_shaders, shader_list,
786 sizeof(linking_shaders[0]) * num_shaders);
787
788 unsigned idx = num_shaders;
789 for (unsigned i = 0; i < num_shaders; i++) {
790 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
791 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
792 idx += shader_list[i]->num_builtins_to_link;
793 }
794
795 assert(idx == num_linking_shaders);
796
797 link_function_calls(prog, linked, linking_shaders, num_linking_shaders);
798
799 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -0700800
Ian Romanick3fb87872010-07-09 14:09:34 -0700801 return linked;
802}
803
804
Ian Romanick019a59b2010-06-21 16:10:42 -0700805struct uniform_node {
806 exec_node link;
807 struct gl_uniform *u;
808 unsigned slots;
809};
810
Ian Romanickabee16e2010-06-21 16:16:05 -0700811void
Eric Anholt849e1812010-06-30 11:49:17 -0700812assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700813{
814 /* */
815 exec_list uniforms;
816 unsigned total_uniforms = 0;
817 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
818 hash_table_string_compare);
819
Ian Romanickabee16e2010-06-21 16:16:05 -0700820 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700821 unsigned next_position = 0;
822
Eric Anholt16b68b12010-06-30 11:05:43 -0700823 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700824 ir_variable *const var = ((ir_instruction *) node)->as_variable();
825
826 if ((var == NULL) || (var->mode != ir_var_uniform))
827 continue;
828
829 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
Eric Anholt8d61a232010-08-05 16:00:46 -0700830 if (vec4_slots == 0) {
831 /* If we've got a sampler or an aggregate of them, the size can
832 * end up zero. Don't allocate any space.
833 */
834 continue;
835 }
Ian Romanick019a59b2010-06-21 16:10:42 -0700836
837 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
838 if (n == NULL) {
839 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
840 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
841 n->slots = vec4_slots;
842
843 n->u[0].Name = strdup(var->name);
844 for (unsigned j = 1; j < vec4_slots; j++)
845 n->u[j].Name = n->u[0].Name;
846
847 hash_table_insert(ht, n, n->u[0].Name);
848 uniforms.push_tail(& n->link);
849 total_uniforms += vec4_slots;
850 }
851
852 if (var->constant_value != NULL)
853 for (unsigned j = 0; j < vec4_slots; j++)
854 n->u[j].Initialized = true;
855
856 var->location = next_position;
857
858 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700859 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700860 case GL_VERTEX_SHADER:
861 n->u[j].VertPos = next_position;
862 break;
863 case GL_FRAGMENT_SHADER:
864 n->u[j].FragPos = next_position;
865 break;
866 case GL_GEOMETRY_SHADER:
867 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700868 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700869 break;
870 }
871
872 next_position++;
873 }
874 }
875 }
876
877 gl_uniform_list *ul = (gl_uniform_list *)
878 calloc(1, sizeof(gl_uniform_list));
879
880 ul->Size = total_uniforms;
881 ul->NumUniforms = total_uniforms;
882 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
883
884 unsigned idx = 0;
885 uniform_node *next;
886 for (uniform_node *node = (uniform_node *) uniforms.head
887 ; node->link.next != NULL
888 ; node = next) {
889 next = (uniform_node *) node->link.next;
890
891 node->link.remove();
892 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
893 idx += node->slots;
894
895 free(node->u);
896 free(node);
897 }
898
899 hash_table_dtor(ht);
900
Ian Romanickabee16e2010-06-21 16:16:05 -0700901 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700902}
903
904
Ian Romanick69846702010-06-22 17:29:19 -0700905/**
906 * Find a contiguous set of available bits in a bitmask
907 *
908 * \param used_mask Bits representing used (1) and unused (0) locations
909 * \param needed_count Number of contiguous bits needed.
910 *
911 * \return
912 * Base location of the available bits on success or -1 on failure.
913 */
914int
915find_available_slots(unsigned used_mask, unsigned needed_count)
916{
917 unsigned needed_mask = (1 << needed_count) - 1;
918 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
919
920 /* The comparison to 32 is redundant, but without it GCC emits "warning:
921 * cannot optimize possibly infinite loops" for the loop below.
922 */
923 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
924 return -1;
925
926 for (int i = 0; i <= max_bit_to_test; i++) {
927 if ((needed_mask & ~used_mask) == needed_mask)
928 return i;
929
930 needed_mask <<= 1;
931 }
932
933 return -1;
934}
935
936
937bool
Eric Anholt849e1812010-06-30 11:49:17 -0700938assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700939{
Ian Romanick9342d262010-06-22 17:41:37 -0700940 /* Mark invalid attribute locations as being used.
941 */
942 unsigned used_locations = (max_attribute_index >= 32)
943 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700944
Eric Anholt16b68b12010-06-30 11:05:43 -0700945 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700946 assert(sh->Type == GL_VERTEX_SHADER);
947
Ian Romanick69846702010-06-22 17:29:19 -0700948 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700949 *
950 * 1. Invalidate the location assignments for all vertex shader inputs.
951 *
952 * 2. Assign locations for inputs that have user-defined (via
953 * glBindVertexAttribLocation) locatoins.
954 *
Ian Romanick69846702010-06-22 17:29:19 -0700955 * 3. Sort the attributes without assigned locations by number of slots
956 * required in decreasing order. Fragmentation caused by attribute
957 * locations assigned by the application may prevent large attributes
958 * from having enough contiguous space.
959 *
960 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700961 */
962
963 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
964
Ian Romanick553dcdc2010-06-23 12:14:02 -0700965 if (prog->Attributes != NULL) {
966 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700967 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700968 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700969
Ian Romanick69846702010-06-22 17:29:19 -0700970 /* Note: attributes that occupy multiple slots, such as arrays or
971 * matrices, may appear in the attrib array multiple times.
972 */
973 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700974 continue;
975
Ian Romanick69846702010-06-22 17:29:19 -0700976 /* From page 61 of the OpenGL 4.0 spec:
977 *
978 * "LinkProgram will fail if the attribute bindings assigned by
979 * BindAttribLocation do not leave not enough space to assign a
980 * location for an active matrix attribute or an active attribute
981 * array, both of which require multiple contiguous generic
982 * attributes."
983 *
984 * Previous versions of the spec contain similar language but omit the
985 * bit about attribute arrays.
986 *
987 * Page 61 of the OpenGL 4.0 spec also says:
988 *
989 * "It is possible for an application to bind more than one
990 * attribute name to the same location. This is referred to as
991 * aliasing. This will only work if only one of the aliased
992 * attributes is active in the executable program, or if no path
993 * through the shader consumes more than one attribute of a set
994 * of attributes aliased to the same location. A link error can
995 * occur if the linker determines that every path through the
996 * shader consumes multiple aliased attributes, but
997 * implementations are not required to generate an error in this
998 * case."
999 *
1000 * These two paragraphs are either somewhat contradictory, or I don't
1001 * fully understand one or both of them.
1002 */
1003 /* FINISHME: The code as currently written does not support attribute
1004 * FINISHME: location aliasing (see comment above).
1005 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001006 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -07001007 const unsigned slots = count_attribute_slots(var->type);
1008
1009 /* Mask representing the contiguous slots that will be used by this
1010 * attribute.
1011 */
1012 const unsigned use_mask = (1 << slots) - 1;
1013
1014 /* Generate a link error if the set of bits requested for this
1015 * attribute overlaps any previously allocated bits.
1016 */
1017 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001018 linker_error_printf(prog,
1019 "insufficient contiguous attribute locations "
1020 "available for vertex shader input `%s'",
1021 var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001022 return false;
1023 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001024
1025 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -07001026 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001027 }
1028 }
1029
Ian Romanick69846702010-06-22 17:29:19 -07001030 /* Temporary storage for the set of attributes that need locations assigned.
1031 */
1032 struct temp_attr {
1033 unsigned slots;
1034 ir_variable *var;
1035
1036 /* Used below in the call to qsort. */
1037 static int compare(const void *a, const void *b)
1038 {
1039 const temp_attr *const l = (const temp_attr *) a;
1040 const temp_attr *const r = (const temp_attr *) b;
1041
1042 /* Reversed because we want a descending order sort below. */
1043 return r->slots - l->slots;
1044 }
1045 } to_assign[16];
1046
1047 unsigned num_attr = 0;
1048
Eric Anholt16b68b12010-06-30 11:05:43 -07001049 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001050 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1051
1052 if ((var == NULL) || (var->mode != ir_var_in))
1053 continue;
1054
1055 /* The location was explicitly assigned, nothing to do here.
1056 */
1057 if (var->location != -1)
1058 continue;
1059
Ian Romanick69846702010-06-22 17:29:19 -07001060 to_assign[num_attr].slots = count_attribute_slots(var->type);
1061 to_assign[num_attr].var = var;
1062 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001063 }
Ian Romanick69846702010-06-22 17:29:19 -07001064
1065 /* If all of the attributes were assigned locations by the application (or
1066 * are built-in attributes with fixed locations), return early. This should
1067 * be the common case.
1068 */
1069 if (num_attr == 0)
1070 return true;
1071
1072 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1073
Ian Romanick982e3792010-06-29 18:58:20 -07001074 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
1075 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
1076 * to prevent it from being automatically allocated below.
1077 */
Ian Romanickc33e78f2010-08-13 12:30:41 -07001078 find_deref_visitor find("gl_Vertex");
1079 find.run(sh->ir);
1080 if (find.variable_found())
1081 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -07001082
Ian Romanick69846702010-06-22 17:29:19 -07001083 for (unsigned i = 0; i < num_attr; i++) {
1084 /* Mask representing the contiguous slots that will be used by this
1085 * attribute.
1086 */
1087 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1088
1089 int location = find_available_slots(used_locations, to_assign[i].slots);
1090
1091 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001092 linker_error_printf(prog,
1093 "insufficient contiguous attribute locations "
1094 "available for vertex shader input `%s'",
1095 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001096 return false;
1097 }
1098
1099 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1100 used_locations |= (use_mask << location);
1101 }
1102
1103 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001104}
1105
1106
Ian Romanick40e114b2010-08-17 14:55:50 -07001107/**
1108 * Demote shader outputs that are not read to being just plain global variables
1109 */
1110void
1111demote_unread_shader_outputs(gl_shader *sh)
1112{
1113 foreach_list(node, sh->ir) {
1114 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1115
1116 if ((var == NULL) || (var->mode != ir_var_out))
1117 continue;
1118
1119 /* An 'out' variable is only really a shader output if its value is read
1120 * by the following stage.
1121 */
1122 if (var->location == -1) {
1123 var->mode = ir_var_auto;
1124 }
1125 }
1126}
1127
1128
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001129void
Eric Anholtb7062832010-07-28 13:52:23 -07001130assign_varying_locations(struct gl_shader_program *prog,
1131 gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001132{
1133 /* FINISHME: Set dynamically when geometry shader support is added. */
1134 unsigned output_index = VERT_RESULT_VAR0;
1135 unsigned input_index = FRAG_ATTRIB_VAR0;
1136
1137 /* Operate in a total of three passes.
1138 *
1139 * 1. Assign locations for any matching inputs and outputs.
1140 *
1141 * 2. Mark output variables in the producer that do not have locations as
1142 * not being outputs. This lets the optimizer eliminate them.
1143 *
1144 * 3. Mark input variables in the consumer that do not have locations as
1145 * not being inputs. This lets the optimizer eliminate them.
1146 */
1147
1148 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1149 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1150
Eric Anholt16b68b12010-06-30 11:05:43 -07001151 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001152 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1153
1154 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1155 || (output_var->location != -1))
1156 continue;
1157
1158 ir_variable *const input_var =
1159 consumer->symbols->get_variable(output_var->name);
1160
1161 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1162 continue;
1163
1164 assert(input_var->location == -1);
1165
1166 /* FINISHME: Location assignment will need some changes when arrays,
1167 * FINISHME: matrices, and structures are allowed as shader inputs /
1168 * FINISHME: outputs.
1169 */
1170 output_var->location = output_index;
1171 input_var->location = input_index;
1172
1173 output_index++;
1174 input_index++;
1175 }
1176
Ian Romanick40e114b2010-08-17 14:55:50 -07001177 demote_unread_shader_outputs(producer);
Ian Romanick0e59b262010-06-23 11:23:01 -07001178
Eric Anholt16b68b12010-06-30 11:05:43 -07001179 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001180 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1181
1182 if ((var == NULL) || (var->mode != ir_var_in))
1183 continue;
1184
Eric Anholtb7062832010-07-28 13:52:23 -07001185 if (var->location == -1) {
1186 if (prog->Version <= 120) {
1187 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1188 *
1189 * Only those varying variables used (i.e. read) in
1190 * the fragment shader executable must be written to
1191 * by the vertex shader executable; declaring
1192 * superfluous varying variables in a vertex shader is
1193 * permissible.
1194 *
1195 * We interpret this text as meaning that the VS must
1196 * write the variable for the FS to read it. See
1197 * "glsl1-varying read but not written" in piglit.
1198 */
1199
1200 linker_error_printf(prog, "fragment shader varying %s not written "
1201 "by vertex shader\n.", var->name);
1202 prog->LinkStatus = false;
1203 }
1204
1205 /* An 'in' variable is only really a shader input if its
1206 * value is written by the previous stage.
1207 */
Eric Anholtb7062832010-07-28 13:52:23 -07001208 var->mode = ir_var_auto;
1209 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001210 }
1211}
1212
1213
1214void
Eric Anholt849e1812010-06-30 11:49:17 -07001215link_shaders(struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001216{
1217 prog->LinkStatus = false;
1218 prog->Validated = false;
1219 prog->_Used = false;
1220
Ian Romanickf36460e2010-06-23 12:07:22 -07001221 if (prog->InfoLog != NULL)
1222 talloc_free(prog->InfoLog);
1223
1224 prog->InfoLog = talloc_strdup(NULL, "");
1225
Ian Romanick832dfa52010-06-17 15:04:20 -07001226 /* Separate the shaders into groups based on their type.
1227 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001228 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001229 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001230 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001231 unsigned num_frag_shaders = 0;
1232
Eric Anholt16b68b12010-06-30 11:05:43 -07001233 vert_shader_list = (struct gl_shader **)
1234 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001235 frag_shader_list = &vert_shader_list[prog->NumShaders];
1236
Ian Romanick25f51d32010-07-16 15:51:50 -07001237 unsigned min_version = UINT_MAX;
1238 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001239 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001240 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1241 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1242
Ian Romanick832dfa52010-06-17 15:04:20 -07001243 switch (prog->Shaders[i]->Type) {
1244 case GL_VERTEX_SHADER:
1245 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1246 num_vert_shaders++;
1247 break;
1248 case GL_FRAGMENT_SHADER:
1249 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1250 num_frag_shaders++;
1251 break;
1252 case GL_GEOMETRY_SHADER:
1253 /* FINISHME: Support geometry shaders. */
1254 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1255 break;
1256 }
1257 }
1258
Ian Romanick25f51d32010-07-16 15:51:50 -07001259 /* Previous to GLSL version 1.30, different compilation units could mix and
1260 * match shading language versions. With GLSL 1.30 and later, the versions
1261 * of all shaders must match.
1262 */
1263 assert(min_version >= 110);
1264 assert(max_version <= 130);
1265 if ((max_version >= 130) && (min_version != max_version)) {
1266 linker_error_printf(prog, "all shaders must use same shading "
1267 "language version\n");
1268 goto done;
1269 }
1270
1271 prog->Version = max_version;
1272
Ian Romanickcd6764e2010-07-16 16:00:07 -07001273 /* Link all shaders for a particular stage and validate the result.
1274 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001275 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001276 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001277 gl_shader *const sh =
1278 link_intrastage_shaders(prog, vert_shader_list, num_vert_shaders);
1279
1280 if (sh == NULL)
1281 goto done;
1282
1283 if (!validate_vertex_shader_executable(prog, sh))
1284 goto done;
1285
1286 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001287 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001288 }
1289
1290 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001291 gl_shader *const sh =
1292 link_intrastage_shaders(prog, frag_shader_list, num_frag_shaders);
1293
1294 if (sh == NULL)
1295 goto done;
1296
1297 if (!validate_fragment_shader_executable(prog, sh))
1298 goto done;
1299
1300 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001301 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001302 }
1303
Ian Romanick3ed850e2010-06-23 12:18:21 -07001304 /* Here begins the inter-stage linking phase. Some initial validation is
1305 * performed, then locations are assigned for uniforms, attributes, and
1306 * varyings.
1307 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001308 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -07001309 /* Validate the inputs of each stage with the output of the preceeding
1310 * stage.
1311 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001312 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -07001313 if (!cross_validate_outputs_to_inputs(prog,
1314 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -07001315 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001316 goto done;
1317 }
1318
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001319 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001320 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001321
Eric Anholt2f4fe152010-08-10 13:06:49 -07001322 /* Do common optimization before assigning storage for attributes,
1323 * uniforms, and varyings. Later optimization could possibly make
1324 * some of that unused.
1325 */
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001326 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Eric Anholt2f4fe152010-08-10 13:06:49 -07001327 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true))
1328 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001329 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001330
Ian Romanickabee16e2010-06-21 16:16:05 -07001331 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001332
Ian Romanick40e114b2010-08-17 14:55:50 -07001333 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER) {
Ian Romanick9342d262010-06-22 17:41:37 -07001334 /* FINISHME: The value of the max_attribute_index parameter is
1335 * FINISHME: implementation dependent based on the value of
1336 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1337 * FINISHME: at least 16, so hardcode 16 for now.
1338 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001339 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -07001340 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -07001341
Ian Romanick40e114b2010-08-17 14:55:50 -07001342 if (prog->_NumLinkedShaders == 1)
1343 demote_unread_shader_outputs(prog->_LinkedShaders[0]);
1344 }
1345
Ian Romanick0e59b262010-06-23 11:23:01 -07001346 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
Eric Anholtb7062832010-07-28 13:52:23 -07001347 assign_varying_locations(prog,
1348 prog->_LinkedShaders[i - 1],
Ian Romanick0e59b262010-06-23 11:23:01 -07001349 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -07001350
1351 /* FINISHME: Assign fragment shader output locations. */
1352
Ian Romanick832dfa52010-06-17 15:04:20 -07001353done:
1354 free(vert_shader_list);
1355}