blob: deb30d7fecf9534a787983e0e48801e777bb3307 [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 *
Eric Anholt5d0f4302010-08-18 12:02:35 -0700677link_intrastage_shaders(GLcontext *ctx,
678 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700679 struct gl_shader **shader_list,
680 unsigned num_shaders)
681{
Ian Romanick13f782c2010-06-29 18:53:38 -0700682 /* Check that global variables defined in multiple shaders are consistent.
683 */
684 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
685 return NULL;
686
687 /* Check that there is only a single definition of each function signature
688 * across all shaders.
689 */
690 for (unsigned i = 0; i < (num_shaders - 1); i++) {
691 foreach_list(node, shader_list[i]->ir) {
692 ir_function *const f = ((ir_instruction *) node)->as_function();
693
694 if (f == NULL)
695 continue;
696
697 for (unsigned j = i + 1; j < num_shaders; j++) {
698 ir_function *const other =
699 shader_list[j]->symbols->get_function(f->name);
700
701 /* If the other shader has no function (and therefore no function
702 * signatures) with the same name, skip to the next shader.
703 */
704 if (other == NULL)
705 continue;
706
707 foreach_iter (exec_list_iterator, iter, *f) {
708 ir_function_signature *sig =
709 (ir_function_signature *) iter.get();
710
711 if (!sig->is_defined || sig->is_built_in)
712 continue;
713
714 ir_function_signature *other_sig =
715 other->exact_matching_signature(& sig->parameters);
716
717 if ((other_sig != NULL) && other_sig->is_defined
718 && !other_sig->is_built_in) {
719 linker_error_printf(prog,
720 "function `%s' is multiply defined",
721 f->name);
722 return NULL;
723 }
724 }
725 }
726 }
727 }
728
729 /* Find the shader that defines main, and make a clone of it.
730 *
731 * Starting with the clone, search for undefined references. If one is
732 * found, find the shader that defines it. Clone the reference and add
733 * it to the shader. Repeat until there are no undefined references or
734 * until a reference cannot be resolved.
735 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700736 gl_shader *main = NULL;
737 for (unsigned i = 0; i < num_shaders; i++) {
738 if (get_main_function_signature(shader_list[i]) != NULL) {
739 main = shader_list[i];
740 break;
741 }
742 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700743
Ian Romanick15ce87e2010-07-09 15:28:22 -0700744 if (main == NULL) {
745 linker_error_printf(prog, "%s shader lacks `main'\n",
746 (shader_list[0]->Type == GL_VERTEX_SHADER)
747 ? "vertex" : "fragment");
748 return NULL;
749 }
750
Eric Anholt5d0f4302010-08-18 12:02:35 -0700751 gl_shader *const linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700752 linked->ir = new(linked) exec_list;
Eric Anholt8273bd42010-08-04 12:34:56 -0700753 clone_ir_list(linked, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700754
755 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700756
Ian Romanick31a97862010-07-12 18:48:50 -0700757 /* The a pointer to the main function in the final linked shader (i.e., the
758 * copy of the original shader that contained the main function).
759 */
760 ir_function_signature *const main_sig = get_main_function_signature(linked);
761
762 /* Move any instructions other than variable declarations or function
763 * declarations into main.
764 */
Ian Romanick9303e352010-07-19 12:33:54 -0700765 exec_node *insertion_point =
766 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
767 linked);
768
Ian Romanick31a97862010-07-12 18:48:50 -0700769 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700770 if (shader_list[i] == main)
771 continue;
772
Ian Romanick31a97862010-07-12 18:48:50 -0700773 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700774 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700775 }
776
Ian Romanick13f782c2010-06-29 18:53:38 -0700777 /* Resolve initializers for global variables in the linked shader.
778 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700779 unsigned num_linking_shaders = num_shaders;
780 for (unsigned i = 0; i < num_shaders; i++)
781 num_linking_shaders += shader_list[i]->num_builtins_to_link;
782
783 gl_shader **linking_shaders =
784 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
785
786 memcpy(linking_shaders, shader_list,
787 sizeof(linking_shaders[0]) * num_shaders);
788
789 unsigned idx = num_shaders;
790 for (unsigned i = 0; i < num_shaders; i++) {
791 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
792 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
793 idx += shader_list[i]->num_builtins_to_link;
794 }
795
796 assert(idx == num_linking_shaders);
797
798 link_function_calls(prog, linked, linking_shaders, num_linking_shaders);
799
800 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -0700801
Ian Romanick3fb87872010-07-09 14:09:34 -0700802 return linked;
803}
804
805
Ian Romanick019a59b2010-06-21 16:10:42 -0700806struct uniform_node {
807 exec_node link;
808 struct gl_uniform *u;
809 unsigned slots;
810};
811
Eric Anholta721abf2010-08-23 10:32:01 -0700812/**
813 * Update the sizes of linked shader uniform arrays to the maximum
814 * array index used.
815 *
816 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
817 *
818 * If one or more elements of an array are active,
819 * GetActiveUniform will return the name of the array in name,
820 * subject to the restrictions listed above. The type of the array
821 * is returned in type. The size parameter contains the highest
822 * array element index used, plus one. The compiler or linker
823 * determines the highest index used. There will be only one
824 * active uniform reported by the GL per uniform array.
825
826 */
827static void
828update_uniform_array_sizes(struct gl_shader_program *prog)
829{
830 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
831 foreach_list(node, prog->_LinkedShaders[i]->ir) {
832 ir_variable *const var = ((ir_instruction *) node)->as_variable();
833
834 if ((var == NULL) || (var->mode != ir_var_uniform) ||
835 !var->type->is_array())
836 continue;
837
838 unsigned int size = var->max_array_access;
839 for (unsigned j = 0; j < prog->_NumLinkedShaders; j++) {
840 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
841 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
842 if (!other_var)
843 continue;
844
845 if (strcmp(var->name, other_var->name) == 0 &&
846 other_var->max_array_access > size) {
847 size = other_var->max_array_access;
848 }
849 }
850 }
851 if (size + 1 != var->type->fields.array->length) {
852 var->type = glsl_type::get_array_instance(var->type->fields.array,
853 size + 1);
854 /* FINISHME: We should update the types of array
855 * dereferences of this variable now.
856 */
857 }
858 }
859 }
860}
861
Ian Romanickabee16e2010-06-21 16:16:05 -0700862void
Eric Anholt849e1812010-06-30 11:49:17 -0700863assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700864{
865 /* */
866 exec_list uniforms;
867 unsigned total_uniforms = 0;
868 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
869 hash_table_string_compare);
870
Eric Anholta721abf2010-08-23 10:32:01 -0700871 update_uniform_array_sizes(prog);
872
Ian Romanickabee16e2010-06-21 16:16:05 -0700873 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700874 unsigned next_position = 0;
875
Eric Anholt16b68b12010-06-30 11:05:43 -0700876 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700877 ir_variable *const var = ((ir_instruction *) node)->as_variable();
878
879 if ((var == NULL) || (var->mode != ir_var_uniform))
880 continue;
881
882 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
Eric Anholt8d61a232010-08-05 16:00:46 -0700883 if (vec4_slots == 0) {
884 /* If we've got a sampler or an aggregate of them, the size can
885 * end up zero. Don't allocate any space.
886 */
887 continue;
888 }
Ian Romanick019a59b2010-06-21 16:10:42 -0700889
890 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
891 if (n == NULL) {
892 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
893 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
894 n->slots = vec4_slots;
895
896 n->u[0].Name = strdup(var->name);
897 for (unsigned j = 1; j < vec4_slots; j++)
Eric Anholtf1d5a942010-08-18 17:39:57 -0700898 n->u[j].Name = strdup(var->name);
Ian Romanick019a59b2010-06-21 16:10:42 -0700899
900 hash_table_insert(ht, n, n->u[0].Name);
901 uniforms.push_tail(& n->link);
902 total_uniforms += vec4_slots;
903 }
904
905 if (var->constant_value != NULL)
906 for (unsigned j = 0; j < vec4_slots; j++)
907 n->u[j].Initialized = true;
908
909 var->location = next_position;
910
911 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700912 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700913 case GL_VERTEX_SHADER:
914 n->u[j].VertPos = next_position;
915 break;
916 case GL_FRAGMENT_SHADER:
917 n->u[j].FragPos = next_position;
918 break;
919 case GL_GEOMETRY_SHADER:
920 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700921 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700922 break;
923 }
924
925 next_position++;
926 }
927 }
928 }
929
930 gl_uniform_list *ul = (gl_uniform_list *)
931 calloc(1, sizeof(gl_uniform_list));
932
933 ul->Size = total_uniforms;
934 ul->NumUniforms = total_uniforms;
935 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
936
937 unsigned idx = 0;
938 uniform_node *next;
939 for (uniform_node *node = (uniform_node *) uniforms.head
940 ; node->link.next != NULL
941 ; node = next) {
942 next = (uniform_node *) node->link.next;
943
944 node->link.remove();
945 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
946 idx += node->slots;
947
948 free(node->u);
949 free(node);
950 }
951
952 hash_table_dtor(ht);
953
Ian Romanickabee16e2010-06-21 16:16:05 -0700954 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700955}
956
957
Ian Romanick69846702010-06-22 17:29:19 -0700958/**
959 * Find a contiguous set of available bits in a bitmask
960 *
961 * \param used_mask Bits representing used (1) and unused (0) locations
962 * \param needed_count Number of contiguous bits needed.
963 *
964 * \return
965 * Base location of the available bits on success or -1 on failure.
966 */
967int
968find_available_slots(unsigned used_mask, unsigned needed_count)
969{
970 unsigned needed_mask = (1 << needed_count) - 1;
971 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
972
973 /* The comparison to 32 is redundant, but without it GCC emits "warning:
974 * cannot optimize possibly infinite loops" for the loop below.
975 */
976 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
977 return -1;
978
979 for (int i = 0; i <= max_bit_to_test; i++) {
980 if ((needed_mask & ~used_mask) == needed_mask)
981 return i;
982
983 needed_mask <<= 1;
984 }
985
986 return -1;
987}
988
989
990bool
Eric Anholt849e1812010-06-30 11:49:17 -0700991assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700992{
Ian Romanick9342d262010-06-22 17:41:37 -0700993 /* Mark invalid attribute locations as being used.
994 */
995 unsigned used_locations = (max_attribute_index >= 32)
996 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700997
Eric Anholt16b68b12010-06-30 11:05:43 -0700998 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700999 assert(sh->Type == GL_VERTEX_SHADER);
1000
Ian Romanick69846702010-06-22 17:29:19 -07001001 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001002 *
1003 * 1. Invalidate the location assignments for all vertex shader inputs.
1004 *
1005 * 2. Assign locations for inputs that have user-defined (via
1006 * glBindVertexAttribLocation) locatoins.
1007 *
Ian Romanick69846702010-06-22 17:29:19 -07001008 * 3. Sort the attributes without assigned locations by number of slots
1009 * required in decreasing order. Fragmentation caused by attribute
1010 * locations assigned by the application may prevent large attributes
1011 * from having enough contiguous space.
1012 *
1013 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001014 */
1015
1016 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
1017
Ian Romanick553dcdc2010-06-23 12:14:02 -07001018 if (prog->Attributes != NULL) {
1019 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001020 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -07001021 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001022
Ian Romanick69846702010-06-22 17:29:19 -07001023 /* Note: attributes that occupy multiple slots, such as arrays or
1024 * matrices, may appear in the attrib array multiple times.
1025 */
1026 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001027 continue;
1028
Ian Romanick69846702010-06-22 17:29:19 -07001029 /* From page 61 of the OpenGL 4.0 spec:
1030 *
1031 * "LinkProgram will fail if the attribute bindings assigned by
1032 * BindAttribLocation do not leave not enough space to assign a
1033 * location for an active matrix attribute or an active attribute
1034 * array, both of which require multiple contiguous generic
1035 * attributes."
1036 *
1037 * Previous versions of the spec contain similar language but omit the
1038 * bit about attribute arrays.
1039 *
1040 * Page 61 of the OpenGL 4.0 spec also says:
1041 *
1042 * "It is possible for an application to bind more than one
1043 * attribute name to the same location. This is referred to as
1044 * aliasing. This will only work if only one of the aliased
1045 * attributes is active in the executable program, or if no path
1046 * through the shader consumes more than one attribute of a set
1047 * of attributes aliased to the same location. A link error can
1048 * occur if the linker determines that every path through the
1049 * shader consumes multiple aliased attributes, but
1050 * implementations are not required to generate an error in this
1051 * case."
1052 *
1053 * These two paragraphs are either somewhat contradictory, or I don't
1054 * fully understand one or both of them.
1055 */
1056 /* FINISHME: The code as currently written does not support attribute
1057 * FINISHME: location aliasing (see comment above).
1058 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001059 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -07001060 const unsigned slots = count_attribute_slots(var->type);
1061
1062 /* Mask representing the contiguous slots that will be used by this
1063 * attribute.
1064 */
1065 const unsigned use_mask = (1 << slots) - 1;
1066
1067 /* Generate a link error if the set of bits requested for this
1068 * attribute overlaps any previously allocated bits.
1069 */
1070 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001071 linker_error_printf(prog,
1072 "insufficient contiguous attribute locations "
1073 "available for vertex shader input `%s'",
1074 var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001075 return false;
1076 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001077
1078 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -07001079 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001080 }
1081 }
1082
Ian Romanick69846702010-06-22 17:29:19 -07001083 /* Temporary storage for the set of attributes that need locations assigned.
1084 */
1085 struct temp_attr {
1086 unsigned slots;
1087 ir_variable *var;
1088
1089 /* Used below in the call to qsort. */
1090 static int compare(const void *a, const void *b)
1091 {
1092 const temp_attr *const l = (const temp_attr *) a;
1093 const temp_attr *const r = (const temp_attr *) b;
1094
1095 /* Reversed because we want a descending order sort below. */
1096 return r->slots - l->slots;
1097 }
1098 } to_assign[16];
1099
1100 unsigned num_attr = 0;
1101
Eric Anholt16b68b12010-06-30 11:05:43 -07001102 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001103 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1104
1105 if ((var == NULL) || (var->mode != ir_var_in))
1106 continue;
1107
1108 /* The location was explicitly assigned, nothing to do here.
1109 */
1110 if (var->location != -1)
1111 continue;
1112
Ian Romanick69846702010-06-22 17:29:19 -07001113 to_assign[num_attr].slots = count_attribute_slots(var->type);
1114 to_assign[num_attr].var = var;
1115 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001116 }
Ian Romanick69846702010-06-22 17:29:19 -07001117
1118 /* If all of the attributes were assigned locations by the application (or
1119 * are built-in attributes with fixed locations), return early. This should
1120 * be the common case.
1121 */
1122 if (num_attr == 0)
1123 return true;
1124
1125 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1126
Ian Romanick982e3792010-06-29 18:58:20 -07001127 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
1128 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
1129 * to prevent it from being automatically allocated below.
1130 */
Ian Romanickc33e78f2010-08-13 12:30:41 -07001131 find_deref_visitor find("gl_Vertex");
1132 find.run(sh->ir);
1133 if (find.variable_found())
1134 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -07001135
Ian Romanick69846702010-06-22 17:29:19 -07001136 for (unsigned i = 0; i < num_attr; i++) {
1137 /* Mask representing the contiguous slots that will be used by this
1138 * attribute.
1139 */
1140 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1141
1142 int location = find_available_slots(used_locations, to_assign[i].slots);
1143
1144 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001145 linker_error_printf(prog,
1146 "insufficient contiguous attribute locations "
1147 "available for vertex shader input `%s'",
1148 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001149 return false;
1150 }
1151
1152 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1153 used_locations |= (use_mask << location);
1154 }
1155
1156 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001157}
1158
1159
Ian Romanick40e114b2010-08-17 14:55:50 -07001160/**
1161 * Demote shader outputs that are not read to being just plain global variables
1162 */
1163void
1164demote_unread_shader_outputs(gl_shader *sh)
1165{
1166 foreach_list(node, sh->ir) {
1167 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1168
1169 if ((var == NULL) || (var->mode != ir_var_out))
1170 continue;
1171
1172 /* An 'out' variable is only really a shader output if its value is read
1173 * by the following stage.
1174 */
1175 if (var->location == -1) {
1176 var->mode = ir_var_auto;
1177 }
1178 }
1179}
1180
1181
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001182void
Eric Anholtb7062832010-07-28 13:52:23 -07001183assign_varying_locations(struct gl_shader_program *prog,
1184 gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001185{
1186 /* FINISHME: Set dynamically when geometry shader support is added. */
1187 unsigned output_index = VERT_RESULT_VAR0;
1188 unsigned input_index = FRAG_ATTRIB_VAR0;
1189
1190 /* Operate in a total of three passes.
1191 *
1192 * 1. Assign locations for any matching inputs and outputs.
1193 *
1194 * 2. Mark output variables in the producer that do not have locations as
1195 * not being outputs. This lets the optimizer eliminate them.
1196 *
1197 * 3. Mark input variables in the consumer that do not have locations as
1198 * not being inputs. This lets the optimizer eliminate them.
1199 */
1200
1201 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1202 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1203
Eric Anholt16b68b12010-06-30 11:05:43 -07001204 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001205 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1206
1207 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1208 || (output_var->location != -1))
1209 continue;
1210
1211 ir_variable *const input_var =
1212 consumer->symbols->get_variable(output_var->name);
1213
1214 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1215 continue;
1216
1217 assert(input_var->location == -1);
1218
1219 /* FINISHME: Location assignment will need some changes when arrays,
1220 * FINISHME: matrices, and structures are allowed as shader inputs /
1221 * FINISHME: outputs.
1222 */
1223 output_var->location = output_index;
1224 input_var->location = input_index;
1225
1226 output_index++;
1227 input_index++;
1228 }
1229
Ian Romanick40e114b2010-08-17 14:55:50 -07001230 demote_unread_shader_outputs(producer);
Ian Romanick0e59b262010-06-23 11:23:01 -07001231
Eric Anholt16b68b12010-06-30 11:05:43 -07001232 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001233 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1234
1235 if ((var == NULL) || (var->mode != ir_var_in))
1236 continue;
1237
Eric Anholtb7062832010-07-28 13:52:23 -07001238 if (var->location == -1) {
1239 if (prog->Version <= 120) {
1240 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1241 *
1242 * Only those varying variables used (i.e. read) in
1243 * the fragment shader executable must be written to
1244 * by the vertex shader executable; declaring
1245 * superfluous varying variables in a vertex shader is
1246 * permissible.
1247 *
1248 * We interpret this text as meaning that the VS must
1249 * write the variable for the FS to read it. See
1250 * "glsl1-varying read but not written" in piglit.
1251 */
1252
1253 linker_error_printf(prog, "fragment shader varying %s not written "
1254 "by vertex shader\n.", var->name);
1255 prog->LinkStatus = false;
1256 }
1257
1258 /* An 'in' variable is only really a shader input if its
1259 * value is written by the previous stage.
1260 */
Eric Anholtb7062832010-07-28 13:52:23 -07001261 var->mode = ir_var_auto;
1262 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001263 }
1264}
1265
1266
1267void
Eric Anholt5d0f4302010-08-18 12:02:35 -07001268link_shaders(GLcontext *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001269{
1270 prog->LinkStatus = false;
1271 prog->Validated = false;
1272 prog->_Used = false;
1273
Ian Romanickf36460e2010-06-23 12:07:22 -07001274 if (prog->InfoLog != NULL)
1275 talloc_free(prog->InfoLog);
1276
1277 prog->InfoLog = talloc_strdup(NULL, "");
1278
Ian Romanick832dfa52010-06-17 15:04:20 -07001279 /* Separate the shaders into groups based on their type.
1280 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001281 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001282 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001283 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001284 unsigned num_frag_shaders = 0;
1285
Eric Anholt16b68b12010-06-30 11:05:43 -07001286 vert_shader_list = (struct gl_shader **)
1287 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001288 frag_shader_list = &vert_shader_list[prog->NumShaders];
1289
Ian Romanick25f51d32010-07-16 15:51:50 -07001290 unsigned min_version = UINT_MAX;
1291 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001292 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001293 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1294 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1295
Ian Romanick832dfa52010-06-17 15:04:20 -07001296 switch (prog->Shaders[i]->Type) {
1297 case GL_VERTEX_SHADER:
1298 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1299 num_vert_shaders++;
1300 break;
1301 case GL_FRAGMENT_SHADER:
1302 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1303 num_frag_shaders++;
1304 break;
1305 case GL_GEOMETRY_SHADER:
1306 /* FINISHME: Support geometry shaders. */
1307 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1308 break;
1309 }
1310 }
1311
Ian Romanick25f51d32010-07-16 15:51:50 -07001312 /* Previous to GLSL version 1.30, different compilation units could mix and
1313 * match shading language versions. With GLSL 1.30 and later, the versions
1314 * of all shaders must match.
1315 */
1316 assert(min_version >= 110);
1317 assert(max_version <= 130);
1318 if ((max_version >= 130) && (min_version != max_version)) {
1319 linker_error_printf(prog, "all shaders must use same shading "
1320 "language version\n");
1321 goto done;
1322 }
1323
1324 prog->Version = max_version;
1325
Eric Anholt5d0f4302010-08-18 12:02:35 -07001326 for (unsigned int i = 0; i < prog->_NumLinkedShaders; i++) {
1327 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1328 }
1329
Ian Romanickcd6764e2010-07-16 16:00:07 -07001330 /* Link all shaders for a particular stage and validate the result.
1331 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001332 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001333 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001334 gl_shader *const sh =
Eric Anholt5d0f4302010-08-18 12:02:35 -07001335 link_intrastage_shaders(ctx, prog, vert_shader_list, num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001336
1337 if (sh == NULL)
1338 goto done;
1339
1340 if (!validate_vertex_shader_executable(prog, sh))
1341 goto done;
1342
1343 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001344 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001345 }
1346
1347 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001348 gl_shader *const sh =
Eric Anholt5d0f4302010-08-18 12:02:35 -07001349 link_intrastage_shaders(ctx, prog, frag_shader_list, num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001350
1351 if (sh == NULL)
1352 goto done;
1353
1354 if (!validate_fragment_shader_executable(prog, sh))
1355 goto done;
1356
1357 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001358 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001359 }
1360
Ian Romanick3ed850e2010-06-23 12:18:21 -07001361 /* Here begins the inter-stage linking phase. Some initial validation is
1362 * performed, then locations are assigned for uniforms, attributes, and
1363 * varyings.
1364 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001365 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -07001366 /* Validate the inputs of each stage with the output of the preceeding
1367 * stage.
1368 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001369 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -07001370 if (!cross_validate_outputs_to_inputs(prog,
1371 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -07001372 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001373 goto done;
1374 }
1375
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001376 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001377 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001378
Eric Anholt2f4fe152010-08-10 13:06:49 -07001379 /* Do common optimization before assigning storage for attributes,
1380 * uniforms, and varyings. Later optimization could possibly make
1381 * some of that unused.
1382 */
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001383 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Eric Anholt2f4fe152010-08-10 13:06:49 -07001384 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true))
1385 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001386 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001387
Ian Romanickabee16e2010-06-21 16:16:05 -07001388 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001389
Ian Romanick40e114b2010-08-17 14:55:50 -07001390 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER) {
Ian Romanick9342d262010-06-22 17:41:37 -07001391 /* FINISHME: The value of the max_attribute_index parameter is
1392 * FINISHME: implementation dependent based on the value of
1393 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1394 * FINISHME: at least 16, so hardcode 16 for now.
1395 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001396 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -07001397 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -07001398
Ian Romanick40e114b2010-08-17 14:55:50 -07001399 if (prog->_NumLinkedShaders == 1)
1400 demote_unread_shader_outputs(prog->_LinkedShaders[0]);
1401 }
1402
Ian Romanick0e59b262010-06-23 11:23:01 -07001403 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
Eric Anholtb7062832010-07-28 13:52:23 -07001404 assign_varying_locations(prog,
1405 prog->_LinkedShaders[i - 1],
Ian Romanick0e59b262010-06-23 11:23:01 -07001406 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -07001407
1408 /* FINISHME: Assign fragment shader output locations. */
1409
Ian Romanick832dfa52010-06-17 15:04:20 -07001410done:
1411 free(vert_shader_list);
1412}