blob: 2dc569777e773281b0e3d6f10d242e327eeb0089 [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
Ian Romanickabee16e2010-06-21 16:16:05 -0700812void
Eric Anholt849e1812010-06-30 11:49:17 -0700813assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700814{
815 /* */
816 exec_list uniforms;
817 unsigned total_uniforms = 0;
818 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
819 hash_table_string_compare);
820
Ian Romanickabee16e2010-06-21 16:16:05 -0700821 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700822 unsigned next_position = 0;
823
Eric Anholt16b68b12010-06-30 11:05:43 -0700824 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700825 ir_variable *const var = ((ir_instruction *) node)->as_variable();
826
827 if ((var == NULL) || (var->mode != ir_var_uniform))
828 continue;
829
830 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
Eric Anholt8d61a232010-08-05 16:00:46 -0700831 if (vec4_slots == 0) {
832 /* If we've got a sampler or an aggregate of them, the size can
833 * end up zero. Don't allocate any space.
834 */
835 continue;
836 }
Ian Romanick019a59b2010-06-21 16:10:42 -0700837
838 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
839 if (n == NULL) {
840 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
841 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
842 n->slots = vec4_slots;
843
844 n->u[0].Name = strdup(var->name);
845 for (unsigned j = 1; j < vec4_slots; j++)
Eric Anholtf1d5a942010-08-18 17:39:57 -0700846 n->u[j].Name = strdup(var->name);
Ian Romanick019a59b2010-06-21 16:10:42 -0700847
848 hash_table_insert(ht, n, n->u[0].Name);
849 uniforms.push_tail(& n->link);
850 total_uniforms += vec4_slots;
851 }
852
853 if (var->constant_value != NULL)
854 for (unsigned j = 0; j < vec4_slots; j++)
855 n->u[j].Initialized = true;
856
857 var->location = next_position;
858
859 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700860 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700861 case GL_VERTEX_SHADER:
862 n->u[j].VertPos = next_position;
863 break;
864 case GL_FRAGMENT_SHADER:
865 n->u[j].FragPos = next_position;
866 break;
867 case GL_GEOMETRY_SHADER:
868 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700869 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700870 break;
871 }
872
873 next_position++;
874 }
875 }
876 }
877
878 gl_uniform_list *ul = (gl_uniform_list *)
879 calloc(1, sizeof(gl_uniform_list));
880
881 ul->Size = total_uniforms;
882 ul->NumUniforms = total_uniforms;
883 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
884
885 unsigned idx = 0;
886 uniform_node *next;
887 for (uniform_node *node = (uniform_node *) uniforms.head
888 ; node->link.next != NULL
889 ; node = next) {
890 next = (uniform_node *) node->link.next;
891
892 node->link.remove();
893 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
894 idx += node->slots;
895
896 free(node->u);
897 free(node);
898 }
899
900 hash_table_dtor(ht);
901
Ian Romanickabee16e2010-06-21 16:16:05 -0700902 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700903}
904
905
Ian Romanick69846702010-06-22 17:29:19 -0700906/**
907 * Find a contiguous set of available bits in a bitmask
908 *
909 * \param used_mask Bits representing used (1) and unused (0) locations
910 * \param needed_count Number of contiguous bits needed.
911 *
912 * \return
913 * Base location of the available bits on success or -1 on failure.
914 */
915int
916find_available_slots(unsigned used_mask, unsigned needed_count)
917{
918 unsigned needed_mask = (1 << needed_count) - 1;
919 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
920
921 /* The comparison to 32 is redundant, but without it GCC emits "warning:
922 * cannot optimize possibly infinite loops" for the loop below.
923 */
924 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
925 return -1;
926
927 for (int i = 0; i <= max_bit_to_test; i++) {
928 if ((needed_mask & ~used_mask) == needed_mask)
929 return i;
930
931 needed_mask <<= 1;
932 }
933
934 return -1;
935}
936
937
938bool
Eric Anholt849e1812010-06-30 11:49:17 -0700939assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700940{
Ian Romanick9342d262010-06-22 17:41:37 -0700941 /* Mark invalid attribute locations as being used.
942 */
943 unsigned used_locations = (max_attribute_index >= 32)
944 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700945
Eric Anholt16b68b12010-06-30 11:05:43 -0700946 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700947 assert(sh->Type == GL_VERTEX_SHADER);
948
Ian Romanick69846702010-06-22 17:29:19 -0700949 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700950 *
951 * 1. Invalidate the location assignments for all vertex shader inputs.
952 *
953 * 2. Assign locations for inputs that have user-defined (via
954 * glBindVertexAttribLocation) locatoins.
955 *
Ian Romanick69846702010-06-22 17:29:19 -0700956 * 3. Sort the attributes without assigned locations by number of slots
957 * required in decreasing order. Fragmentation caused by attribute
958 * locations assigned by the application may prevent large attributes
959 * from having enough contiguous space.
960 *
961 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700962 */
963
964 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
965
Ian Romanick553dcdc2010-06-23 12:14:02 -0700966 if (prog->Attributes != NULL) {
967 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700968 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700969 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700970
Ian Romanick69846702010-06-22 17:29:19 -0700971 /* Note: attributes that occupy multiple slots, such as arrays or
972 * matrices, may appear in the attrib array multiple times.
973 */
974 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700975 continue;
976
Ian Romanick69846702010-06-22 17:29:19 -0700977 /* From page 61 of the OpenGL 4.0 spec:
978 *
979 * "LinkProgram will fail if the attribute bindings assigned by
980 * BindAttribLocation do not leave not enough space to assign a
981 * location for an active matrix attribute or an active attribute
982 * array, both of which require multiple contiguous generic
983 * attributes."
984 *
985 * Previous versions of the spec contain similar language but omit the
986 * bit about attribute arrays.
987 *
988 * Page 61 of the OpenGL 4.0 spec also says:
989 *
990 * "It is possible for an application to bind more than one
991 * attribute name to the same location. This is referred to as
992 * aliasing. This will only work if only one of the aliased
993 * attributes is active in the executable program, or if no path
994 * through the shader consumes more than one attribute of a set
995 * of attributes aliased to the same location. A link error can
996 * occur if the linker determines that every path through the
997 * shader consumes multiple aliased attributes, but
998 * implementations are not required to generate an error in this
999 * case."
1000 *
1001 * These two paragraphs are either somewhat contradictory, or I don't
1002 * fully understand one or both of them.
1003 */
1004 /* FINISHME: The code as currently written does not support attribute
1005 * FINISHME: location aliasing (see comment above).
1006 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001007 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -07001008 const unsigned slots = count_attribute_slots(var->type);
1009
1010 /* Mask representing the contiguous slots that will be used by this
1011 * attribute.
1012 */
1013 const unsigned use_mask = (1 << slots) - 1;
1014
1015 /* Generate a link error if the set of bits requested for this
1016 * attribute overlaps any previously allocated bits.
1017 */
1018 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001019 linker_error_printf(prog,
1020 "insufficient contiguous attribute locations "
1021 "available for vertex shader input `%s'",
1022 var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001023 return false;
1024 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001025
1026 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -07001027 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001028 }
1029 }
1030
Ian Romanick69846702010-06-22 17:29:19 -07001031 /* Temporary storage for the set of attributes that need locations assigned.
1032 */
1033 struct temp_attr {
1034 unsigned slots;
1035 ir_variable *var;
1036
1037 /* Used below in the call to qsort. */
1038 static int compare(const void *a, const void *b)
1039 {
1040 const temp_attr *const l = (const temp_attr *) a;
1041 const temp_attr *const r = (const temp_attr *) b;
1042
1043 /* Reversed because we want a descending order sort below. */
1044 return r->slots - l->slots;
1045 }
1046 } to_assign[16];
1047
1048 unsigned num_attr = 0;
1049
Eric Anholt16b68b12010-06-30 11:05:43 -07001050 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001051 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1052
1053 if ((var == NULL) || (var->mode != ir_var_in))
1054 continue;
1055
1056 /* The location was explicitly assigned, nothing to do here.
1057 */
1058 if (var->location != -1)
1059 continue;
1060
Ian Romanick69846702010-06-22 17:29:19 -07001061 to_assign[num_attr].slots = count_attribute_slots(var->type);
1062 to_assign[num_attr].var = var;
1063 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001064 }
Ian Romanick69846702010-06-22 17:29:19 -07001065
1066 /* If all of the attributes were assigned locations by the application (or
1067 * are built-in attributes with fixed locations), return early. This should
1068 * be the common case.
1069 */
1070 if (num_attr == 0)
1071 return true;
1072
1073 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1074
Ian Romanick982e3792010-06-29 18:58:20 -07001075 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
1076 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
1077 * to prevent it from being automatically allocated below.
1078 */
Ian Romanickc33e78f2010-08-13 12:30:41 -07001079 find_deref_visitor find("gl_Vertex");
1080 find.run(sh->ir);
1081 if (find.variable_found())
1082 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -07001083
Ian Romanick69846702010-06-22 17:29:19 -07001084 for (unsigned i = 0; i < num_attr; i++) {
1085 /* Mask representing the contiguous slots that will be used by this
1086 * attribute.
1087 */
1088 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1089
1090 int location = find_available_slots(used_locations, to_assign[i].slots);
1091
1092 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -07001093 linker_error_printf(prog,
1094 "insufficient contiguous attribute locations "
1095 "available for vertex shader input `%s'",
1096 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001097 return false;
1098 }
1099
1100 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1101 used_locations |= (use_mask << location);
1102 }
1103
1104 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001105}
1106
1107
Ian Romanick40e114b2010-08-17 14:55:50 -07001108/**
1109 * Demote shader outputs that are not read to being just plain global variables
1110 */
1111void
1112demote_unread_shader_outputs(gl_shader *sh)
1113{
1114 foreach_list(node, sh->ir) {
1115 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1116
1117 if ((var == NULL) || (var->mode != ir_var_out))
1118 continue;
1119
1120 /* An 'out' variable is only really a shader output if its value is read
1121 * by the following stage.
1122 */
1123 if (var->location == -1) {
1124 var->mode = ir_var_auto;
1125 }
1126 }
1127}
1128
1129
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001130void
Eric Anholtb7062832010-07-28 13:52:23 -07001131assign_varying_locations(struct gl_shader_program *prog,
1132 gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -07001133{
1134 /* FINISHME: Set dynamically when geometry shader support is added. */
1135 unsigned output_index = VERT_RESULT_VAR0;
1136 unsigned input_index = FRAG_ATTRIB_VAR0;
1137
1138 /* Operate in a total of three passes.
1139 *
1140 * 1. Assign locations for any matching inputs and outputs.
1141 *
1142 * 2. Mark output variables in the producer that do not have locations as
1143 * not being outputs. This lets the optimizer eliminate them.
1144 *
1145 * 3. Mark input variables in the consumer that do not have locations as
1146 * not being inputs. This lets the optimizer eliminate them.
1147 */
1148
1149 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1150 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1151
Eric Anholt16b68b12010-06-30 11:05:43 -07001152 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001153 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1154
1155 if ((output_var == NULL) || (output_var->mode != ir_var_out)
1156 || (output_var->location != -1))
1157 continue;
1158
1159 ir_variable *const input_var =
1160 consumer->symbols->get_variable(output_var->name);
1161
1162 if ((input_var == NULL) || (input_var->mode != ir_var_in))
1163 continue;
1164
1165 assert(input_var->location == -1);
1166
1167 /* FINISHME: Location assignment will need some changes when arrays,
1168 * FINISHME: matrices, and structures are allowed as shader inputs /
1169 * FINISHME: outputs.
1170 */
1171 output_var->location = output_index;
1172 input_var->location = input_index;
1173
1174 output_index++;
1175 input_index++;
1176 }
1177
Ian Romanick40e114b2010-08-17 14:55:50 -07001178 demote_unread_shader_outputs(producer);
Ian Romanick0e59b262010-06-23 11:23:01 -07001179
Eric Anholt16b68b12010-06-30 11:05:43 -07001180 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001181 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1182
1183 if ((var == NULL) || (var->mode != ir_var_in))
1184 continue;
1185
Eric Anholtb7062832010-07-28 13:52:23 -07001186 if (var->location == -1) {
1187 if (prog->Version <= 120) {
1188 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1189 *
1190 * Only those varying variables used (i.e. read) in
1191 * the fragment shader executable must be written to
1192 * by the vertex shader executable; declaring
1193 * superfluous varying variables in a vertex shader is
1194 * permissible.
1195 *
1196 * We interpret this text as meaning that the VS must
1197 * write the variable for the FS to read it. See
1198 * "glsl1-varying read but not written" in piglit.
1199 */
1200
1201 linker_error_printf(prog, "fragment shader varying %s not written "
1202 "by vertex shader\n.", var->name);
1203 prog->LinkStatus = false;
1204 }
1205
1206 /* An 'in' variable is only really a shader input if its
1207 * value is written by the previous stage.
1208 */
Eric Anholtb7062832010-07-28 13:52:23 -07001209 var->mode = ir_var_auto;
1210 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001211 }
1212}
1213
1214
1215void
Eric Anholt5d0f4302010-08-18 12:02:35 -07001216link_shaders(GLcontext *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001217{
1218 prog->LinkStatus = false;
1219 prog->Validated = false;
1220 prog->_Used = false;
1221
Ian Romanickf36460e2010-06-23 12:07:22 -07001222 if (prog->InfoLog != NULL)
1223 talloc_free(prog->InfoLog);
1224
1225 prog->InfoLog = talloc_strdup(NULL, "");
1226
Ian Romanick832dfa52010-06-17 15:04:20 -07001227 /* Separate the shaders into groups based on their type.
1228 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001229 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001230 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001231 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001232 unsigned num_frag_shaders = 0;
1233
Eric Anholt16b68b12010-06-30 11:05:43 -07001234 vert_shader_list = (struct gl_shader **)
1235 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001236 frag_shader_list = &vert_shader_list[prog->NumShaders];
1237
Ian Romanick25f51d32010-07-16 15:51:50 -07001238 unsigned min_version = UINT_MAX;
1239 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001240 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001241 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1242 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1243
Ian Romanick832dfa52010-06-17 15:04:20 -07001244 switch (prog->Shaders[i]->Type) {
1245 case GL_VERTEX_SHADER:
1246 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1247 num_vert_shaders++;
1248 break;
1249 case GL_FRAGMENT_SHADER:
1250 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1251 num_frag_shaders++;
1252 break;
1253 case GL_GEOMETRY_SHADER:
1254 /* FINISHME: Support geometry shaders. */
1255 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1256 break;
1257 }
1258 }
1259
Ian Romanick25f51d32010-07-16 15:51:50 -07001260 /* Previous to GLSL version 1.30, different compilation units could mix and
1261 * match shading language versions. With GLSL 1.30 and later, the versions
1262 * of all shaders must match.
1263 */
1264 assert(min_version >= 110);
1265 assert(max_version <= 130);
1266 if ((max_version >= 130) && (min_version != max_version)) {
1267 linker_error_printf(prog, "all shaders must use same shading "
1268 "language version\n");
1269 goto done;
1270 }
1271
1272 prog->Version = max_version;
1273
Eric Anholt5d0f4302010-08-18 12:02:35 -07001274 for (unsigned int i = 0; i < prog->_NumLinkedShaders; i++) {
1275 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1276 }
1277
Ian Romanickcd6764e2010-07-16 16:00:07 -07001278 /* Link all shaders for a particular stage and validate the result.
1279 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001280 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001281 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001282 gl_shader *const sh =
Eric Anholt5d0f4302010-08-18 12:02:35 -07001283 link_intrastage_shaders(ctx, prog, vert_shader_list, num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001284
1285 if (sh == NULL)
1286 goto done;
1287
1288 if (!validate_vertex_shader_executable(prog, sh))
1289 goto done;
1290
1291 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001292 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001293 }
1294
1295 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001296 gl_shader *const sh =
Eric Anholt5d0f4302010-08-18 12:02:35 -07001297 link_intrastage_shaders(ctx, prog, frag_shader_list, num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001298
1299 if (sh == NULL)
1300 goto done;
1301
1302 if (!validate_fragment_shader_executable(prog, sh))
1303 goto done;
1304
1305 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -07001306 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001307 }
1308
Ian Romanick3ed850e2010-06-23 12:18:21 -07001309 /* Here begins the inter-stage linking phase. Some initial validation is
1310 * performed, then locations are assigned for uniforms, attributes, and
1311 * varyings.
1312 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001313 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -07001314 /* Validate the inputs of each stage with the output of the preceeding
1315 * stage.
1316 */
Ian Romanickabee16e2010-06-21 16:16:05 -07001317 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -07001318 if (!cross_validate_outputs_to_inputs(prog,
1319 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -07001320 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001321 goto done;
1322 }
1323
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001324 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001325 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001326
Eric Anholt2f4fe152010-08-10 13:06:49 -07001327 /* Do common optimization before assigning storage for attributes,
1328 * uniforms, and varyings. Later optimization could possibly make
1329 * some of that unused.
1330 */
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001331 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Eric Anholt2f4fe152010-08-10 13:06:49 -07001332 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true))
1333 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001334 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001335
Ian Romanickabee16e2010-06-21 16:16:05 -07001336 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001337
Ian Romanick40e114b2010-08-17 14:55:50 -07001338 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER) {
Ian Romanick9342d262010-06-22 17:41:37 -07001339 /* FINISHME: The value of the max_attribute_index parameter is
1340 * FINISHME: implementation dependent based on the value of
1341 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1342 * FINISHME: at least 16, so hardcode 16 for now.
1343 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001344 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -07001345 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -07001346
Ian Romanick40e114b2010-08-17 14:55:50 -07001347 if (prog->_NumLinkedShaders == 1)
1348 demote_unread_shader_outputs(prog->_LinkedShaders[0]);
1349 }
1350
Ian Romanick0e59b262010-06-23 11:23:01 -07001351 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
Eric Anholtb7062832010-07-28 13:52:23 -07001352 assign_varying_locations(prog,
1353 prog->_LinkedShaders[i - 1],
Ian Romanick0e59b262010-06-23 11:23:01 -07001354 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -07001355
1356 /* FINISHME: Assign fragment shader output locations. */
1357
Ian Romanick832dfa52010-06-17 15:04:20 -07001358done:
1359 free(vert_shader_list);
1360}