blob: b8a7126e3f6c6fe2d389448af713598866b5b9ea [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 */
Ian Romanickf36460e2010-06-23 12:07:22 -070066
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080067#include "main/core.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070068#include "glsl_symbol_table.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070069#include "ir.h"
70#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030071#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070072#include "linker.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070073#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070074
Ian Romanick3322fba2010-10-14 13:28:42 -070075extern "C" {
76#include "main/shaderobj.h"
77}
78
Ian Romanick832dfa52010-06-17 15:04:20 -070079/**
80 * Visitor that determines whether or not a variable is ever written.
81 */
82class find_assignment_visitor : public ir_hierarchical_visitor {
83public:
84 find_assignment_visitor(const char *name)
85 : name(name), found(false)
86 {
87 /* empty */
88 }
89
90 virtual ir_visitor_status visit_enter(ir_assignment *ir)
91 {
92 ir_variable *const var = ir->lhs->variable_referenced();
93
94 if (strcmp(name, var->name) == 0) {
95 found = true;
96 return visit_stop;
97 }
98
99 return visit_continue_with_parent;
100 }
101
Eric Anholt18a60232010-08-23 11:29:25 -0700102 virtual ir_visitor_status visit_enter(ir_call *ir)
103 {
104 exec_list_iterator sig_iter = ir->get_callee()->parameters.iterator();
105 foreach_iter(exec_list_iterator, iter, *ir) {
106 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
107 ir_variable *sig_param = (ir_variable *)sig_iter.get();
108
109 if (sig_param->mode == ir_var_out ||
110 sig_param->mode == ir_var_inout) {
111 ir_variable *var = param_rval->variable_referenced();
112 if (var && strcmp(name, var->name) == 0) {
113 found = true;
114 return visit_stop;
115 }
116 }
117 sig_iter.next();
118 }
119
120 return visit_continue_with_parent;
121 }
122
Ian Romanick832dfa52010-06-17 15:04:20 -0700123 bool variable_found()
124 {
125 return found;
126 }
127
128private:
129 const char *name; /**< Find writes to a variable with this name. */
130 bool found; /**< Was a write to the variable found? */
131};
132
Ian Romanickc93b8f12010-06-17 15:20:22 -0700133
Ian Romanickc33e78f2010-08-13 12:30:41 -0700134/**
135 * Visitor that determines whether or not a variable is ever read.
136 */
137class find_deref_visitor : public ir_hierarchical_visitor {
138public:
139 find_deref_visitor(const char *name)
140 : name(name), found(false)
141 {
142 /* empty */
143 }
144
145 virtual ir_visitor_status visit(ir_dereference_variable *ir)
146 {
147 if (strcmp(this->name, ir->var->name) == 0) {
148 this->found = true;
149 return visit_stop;
150 }
151
152 return visit_continue;
153 }
154
155 bool variable_found() const
156 {
157 return this->found;
158 }
159
160private:
161 const char *name; /**< Find writes to a variable with this name. */
162 bool found; /**< Was a write to the variable found? */
163};
164
165
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700166void
Ian Romanick586e7412011-07-28 14:04:09 -0700167linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700168{
169 va_list ap;
170
Kenneth Graunked3073f52011-01-21 14:32:31 -0800171 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700172 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800173 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700174 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700175
176 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700177}
178
179
180void
Ian Romanick379a32f2011-07-28 14:09:06 -0700181linker_warning(gl_shader_program *prog, const char *fmt, ...)
182{
183 va_list ap;
184
185 ralloc_strcat(&prog->InfoLog, "error: ");
186 va_start(ap, fmt);
187 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
188 va_end(ap);
189
190}
191
192
193void
Ian Romanickf6ee7bc2011-10-11 16:15:47 -0700194link_invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
195 int generic_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700196{
Eric Anholt16b68b12010-06-30 11:05:43 -0700197 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700198 ir_variable *const var = ((ir_instruction *) node)->as_variable();
199
200 if ((var == NULL) || (var->mode != (unsigned) mode))
201 continue;
202
203 /* Only assign locations for generic attributes / varyings / etc.
204 */
Ian Romanick68a4fc92010-10-07 17:21:22 -0700205 if ((var->location >= generic_base) && !var->explicit_location)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700206 var->location = -1;
207 }
208}
209
210
Ian Romanickc93b8f12010-06-17 15:20:22 -0700211/**
Ian Romanick69846702010-06-22 17:29:19 -0700212 * Determine the number of attribute slots required for a particular type
213 *
214 * This code is here because it implements the language rules of a specific
215 * GLSL version. Since it's a property of the language and not a property of
216 * types in general, it doesn't really belong in glsl_type.
217 */
218unsigned
219count_attribute_slots(const glsl_type *t)
220{
221 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
222 *
223 * "A scalar input counts the same amount against this limit as a vec4,
224 * so applications may want to consider packing groups of four
225 * unrelated float inputs together into a vector to better utilize the
226 * capabilities of the underlying hardware. A matrix input will use up
227 * multiple locations. The number of locations used will equal the
228 * number of columns in the matrix."
229 *
230 * The spec does not explicitly say how arrays are counted. However, it
231 * should be safe to assume the total number of slots consumed by an array
232 * is the number of entries in the array multiplied by the number of slots
233 * consumed by a single element of the array.
234 */
235
236 if (t->is_array())
237 return t->array_size() * count_attribute_slots(t->element_type());
238
239 if (t->is_matrix())
240 return t->matrix_columns;
241
242 return 1;
243}
244
245
246/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700247 * Verify that a vertex shader executable meets all semantic requirements.
248 *
249 * Also sets prog->Vert.UsesClipDistance as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700250 *
251 * \param shader Vertex shader executable to be verified
252 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700253bool
Eric Anholt849e1812010-06-30 11:49:17 -0700254validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700255 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700256{
257 if (shader == NULL)
258 return true;
259
Ian Romanick832dfa52010-06-17 15:04:20 -0700260 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700261 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700262 if (!find.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700263 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700264 return false;
265 }
266
Paul Berryb453ba22011-08-11 18:10:22 -0700267 if (prog->Version >= 130) {
268 /* From section 7.1 (Vertex Shader Special Variables) of the
269 * GLSL 1.30 spec:
270 *
271 * "It is an error for a shader to statically write both
272 * gl_ClipVertex and gl_ClipDistance."
273 */
274 find_assignment_visitor clip_vertex("gl_ClipVertex");
275 find_assignment_visitor clip_distance("gl_ClipDistance");
276
277 clip_vertex.run(shader->ir);
278 clip_distance.run(shader->ir);
279 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
280 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
281 "and `gl_ClipDistance'\n");
282 return false;
283 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700284 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berryb453ba22011-08-11 18:10:22 -0700285 }
286
Ian Romanick832dfa52010-06-17 15:04:20 -0700287 return true;
288}
289
290
Ian Romanickc93b8f12010-06-17 15:20:22 -0700291/**
292 * Verify that a fragment shader executable meets all semantic requirements
293 *
294 * \param shader Fragment shader executable to be verified
295 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700296bool
Eric Anholt849e1812010-06-30 11:49:17 -0700297validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700298 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700299{
300 if (shader == NULL)
301 return true;
302
Ian Romanick832dfa52010-06-17 15:04:20 -0700303 find_assignment_visitor frag_color("gl_FragColor");
304 find_assignment_visitor frag_data("gl_FragData");
305
Eric Anholt16b68b12010-06-30 11:05:43 -0700306 frag_color.run(shader->ir);
307 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700308
Ian Romanick832dfa52010-06-17 15:04:20 -0700309 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700310 linker_error(prog, "fragment shader writes to both "
311 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700312 return false;
313 }
314
315 return true;
316}
317
318
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700319/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700320 * Generate a string describing the mode of a variable
321 */
322static const char *
323mode_string(const ir_variable *var)
324{
325 switch (var->mode) {
326 case ir_var_auto:
327 return (var->read_only) ? "global constant" : "global variable";
328
329 case ir_var_uniform: return "uniform";
330 case ir_var_in: return "shader input";
331 case ir_var_out: return "shader output";
332 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700333
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800334 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700335 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700336 default:
337 assert(!"Should not get here.");
338 return "invalid variable";
339 }
340}
341
342
343/**
344 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700345 */
346bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700347cross_validate_globals(struct gl_shader_program *prog,
348 struct gl_shader **shader_list,
349 unsigned num_shaders,
350 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700351{
352 /* Examine all of the uniforms in all of the shaders and cross validate
353 * them.
354 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700355 glsl_symbol_table variables;
356 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700357 if (shader_list[i] == NULL)
358 continue;
359
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700360 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700361 ir_variable *const var = ((ir_instruction *) node)->as_variable();
362
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700363 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700364 continue;
365
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700366 if (uniforms_only && (var->mode != ir_var_uniform))
367 continue;
368
Ian Romanick7e2aa912010-07-19 17:12:42 -0700369 /* Don't cross validate temporaries that are at global scope. These
370 * will eventually get pulled into the shaders 'main'.
371 */
372 if (var->mode == ir_var_temporary)
373 continue;
374
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700375 /* If a global with this name has already been seen, verify that the
376 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700377 * initializers, the values of the initializers must be the same.
378 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700379 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700380 if (existing != NULL) {
381 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700382 /* Consider the types to be "the same" if both types are arrays
383 * of the same type and one of the arrays is implicitly sized.
384 * In addition, set the type of the linked variable to the
385 * explicitly sized array.
386 */
387 if (var->type->is_array()
388 && existing->type->is_array()
389 && (var->type->fields.array == existing->type->fields.array)
390 && ((var->type->length == 0)
391 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800392 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700393 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800394 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700395 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700396 linker_error(prog, "%s `%s' declared as type "
397 "`%s' and type `%s'\n",
398 mode_string(var),
399 var->name, var->type->name,
400 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700401 return false;
402 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700403 }
404
Ian Romanick68a4fc92010-10-07 17:21:22 -0700405 if (var->explicit_location) {
406 if (existing->explicit_location
407 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700408 linker_error(prog, "explicit locations for %s "
409 "`%s' have differing values\n",
410 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700411 return false;
412 }
413
414 existing->location = var->location;
415 existing->explicit_location = true;
416 }
417
Ian Romanick46173f92011-10-31 13:07:06 -0700418 /* Validate layout qualifiers for gl_FragDepth.
419 *
420 * From the AMD/ARB_conservative_depth specs:
421 *
422 * "If gl_FragDepth is redeclared in any fragment shader in a
423 * program, it must be redeclared in all fragment shaders in
424 * that program that have static assignments to
425 * gl_FragDepth. All redeclarations of gl_FragDepth in all
426 * fragment shaders in a single program must have the same set
427 * of qualifiers."
428 */
429 if (strcmp(var->name, "gl_FragDepth") == 0) {
430 bool layout_declared = var->depth_layout != ir_depth_layout_none;
431 bool layout_differs =
432 var->depth_layout != existing->depth_layout;
433
434 if (layout_declared && layout_differs) {
435 linker_error(prog,
436 "All redeclarations of gl_FragDepth in all "
437 "fragment shaders in a single program must have "
438 "the same set of qualifiers.");
439 }
440
441 if (var->used && layout_differs) {
442 linker_error(prog,
443 "If gl_FragDepth is redeclared with a layout "
444 "qualifier in any fragment shader, it must be "
445 "redeclared with the same layout qualifier in "
446 "all fragment shaders that have assignments to "
447 "gl_FragDepth");
448 }
449 }
Chad Versaceaddae332011-01-27 01:40:31 -0800450
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700451 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
452 *
453 * "If a shared global has multiple initializers, the
454 * initializers must all be constant expressions, and they
455 * must all have the same value. Otherwise, a link error will
456 * result. (A shared global having only one initializer does
457 * not require that initializer to be a constant expression.)"
458 *
459 * Previous to 4.20 the GLSL spec simply said that initializers
460 * must have the same value. In this case of non-constant
461 * initializers, this was impossible to determine. As a result,
462 * no vendor actually implemented that behavior. The 4.20
463 * behavior matches the implemented behavior of at least one other
464 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700465 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700466 if (var->constant_initializer != NULL) {
467 if (existing->constant_initializer != NULL) {
468 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700469 linker_error(prog, "initializers for %s "
470 "`%s' have differing values\n",
471 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700472 return false;
473 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700474 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700475 /* If the first-seen instance of a particular uniform did not
476 * have an initializer but a later instance does, copy the
477 * initializer to the version stored in the symbol table.
478 */
Ian Romanickde415b72010-07-14 13:22:12 -0700479 /* FINISHME: This is wrong. The constant_value field should
480 * FINISHME: not be modified! Imagine a case where a shader
481 * FINISHME: without an initializer is linked in two different
482 * FINISHME: programs with shaders that have differing
483 * FINISHME: initializers. Linking with the first will
484 * FINISHME: modify the shader, and linking with the second
485 * FINISHME: will fail.
486 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700487 existing->constant_initializer =
488 var->constant_initializer->clone(ralloc_parent(existing),
489 NULL);
490 }
491 }
492
493 if (var->has_initializer) {
494 if (existing->has_initializer
495 && (var->constant_initializer == NULL
496 || existing->constant_initializer == NULL)) {
497 linker_error(prog,
498 "shared global variable `%s' has multiple "
499 "non-constant initializers.\n",
500 var->name);
501 return false;
502 }
503
504 /* Some instance had an initializer, so keep track of that. In
505 * this location, all sorts of initializers (constant or
506 * otherwise) will propagate the existence to the variable
507 * stored in the symbol table.
508 */
509 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700510 }
Chad Versace7528f142010-11-17 14:34:38 -0800511
512 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700513 linker_error(prog, "declarations for %s `%s' have "
514 "mismatching invariant qualifiers\n",
515 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800516 return false;
517 }
Chad Versace61428dd2011-01-10 15:29:30 -0800518 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700519 linker_error(prog, "declarations for %s `%s' have "
520 "mismatching centroid qualifiers\n",
521 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800522 return false;
523 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700524 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700525 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700526 }
527 }
528
529 return true;
530}
531
532
Ian Romanick37101922010-06-18 19:02:10 -0700533/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700534 * Perform validation of uniforms used across multiple shader stages
535 */
536bool
537cross_validate_uniforms(struct gl_shader_program *prog)
538{
539 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700540 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700541}
542
543
544/**
Ian Romanick37101922010-06-18 19:02:10 -0700545 * Validate that outputs from one stage match inputs of another
546 */
547bool
Eric Anholt849e1812010-06-30 11:49:17 -0700548cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700549 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700550{
551 glsl_symbol_table parameters;
552 /* FINISHME: Figure these out dynamically. */
553 const char *const producer_stage = "vertex";
554 const char *const consumer_stage = "fragment";
555
556 /* Find all shader outputs in the "producer" stage.
557 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700558 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700559 ir_variable *const var = ((ir_instruction *) node)->as_variable();
560
561 /* FINISHME: For geometry shaders, this should also look for inout
562 * FINISHME: variables.
563 */
564 if ((var == NULL) || (var->mode != ir_var_out))
565 continue;
566
Eric Anholt001eee52010-11-05 06:11:24 -0700567 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700568 }
569
570
571 /* Find all shader inputs in the "consumer" stage. Any variables that have
572 * matching outputs already in the symbol table must have the same type and
573 * qualifiers.
574 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700575 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700576 ir_variable *const input = ((ir_instruction *) node)->as_variable();
577
578 /* FINISHME: For geometry shaders, this should also look for inout
579 * FINISHME: variables.
580 */
581 if ((input == NULL) || (input->mode != ir_var_in))
582 continue;
583
584 ir_variable *const output = parameters.get_variable(input->name);
585 if (output != NULL) {
586 /* Check that the types match between stages.
587 */
588 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800589 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500590 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800591 * access it must redeclare it with a size. There is some
592 * language in the GLSL spec that implies the fragment shader
593 * and vertex shader do not have to agree on this size. Other
594 * driver behave this way, and one or two applications seem to
595 * rely on it.
596 *
597 * Neither declaration needs to be modified here because the array
598 * sizes are fixed later when update_array_sizes is called.
599 *
600 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
601 *
602 * "Unlike user-defined varying variables, the built-in
603 * varying variables don't have a strict one-to-one
604 * correspondence between the vertex language and the
605 * fragment language."
606 */
607 if (!output->type->is_array()
608 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700609 linker_error(prog,
610 "%s shader output `%s' declared as type `%s', "
611 "but %s shader input declared as type `%s'\n",
612 producer_stage, output->name,
613 output->type->name,
614 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800615 return false;
616 }
Ian Romanick37101922010-06-18 19:02:10 -0700617 }
618
619 /* Check that all of the qualifiers match between stages.
620 */
621 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700622 linker_error(prog,
623 "%s shader output `%s' %s centroid qualifier, "
624 "but %s shader input %s centroid qualifier\n",
625 producer_stage,
626 output->name,
627 (output->centroid) ? "has" : "lacks",
628 consumer_stage,
629 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700630 return false;
631 }
632
633 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700634 linker_error(prog,
635 "%s shader output `%s' %s invariant qualifier, "
636 "but %s shader input %s invariant qualifier\n",
637 producer_stage,
638 output->name,
639 (output->invariant) ? "has" : "lacks",
640 consumer_stage,
641 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700642 return false;
643 }
644
645 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700646 linker_error(prog,
647 "%s shader output `%s' specifies %s "
648 "interpolation qualifier, "
649 "but %s shader input specifies %s "
650 "interpolation qualifier\n",
651 producer_stage,
652 output->name,
653 output->interpolation_string(),
654 consumer_stage,
655 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700656 return false;
657 }
658 }
659 }
660
661 return true;
662}
663
664
Ian Romanick3fb87872010-07-09 14:09:34 -0700665/**
666 * Populates a shaders symbol table with all global declarations
667 */
668static void
669populate_symbol_table(gl_shader *sh)
670{
671 sh->symbols = new(sh) glsl_symbol_table;
672
673 foreach_list(node, sh->ir) {
674 ir_instruction *const inst = (ir_instruction *) node;
675 ir_variable *var;
676 ir_function *func;
677
678 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700679 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700680 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700681 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700682 }
683 }
684}
685
686
687/**
Ian Romanick31a97862010-07-12 18:48:50 -0700688 * Remap variables referenced in an instruction tree
689 *
690 * This is used when instruction trees are cloned from one shader and placed in
691 * another. These trees will contain references to \c ir_variable nodes that
692 * do not exist in the target shader. This function finds these \c ir_variable
693 * references and replaces the references with matching variables in the target
694 * shader.
695 *
696 * If there is no matching variable in the target shader, a clone of the
697 * \c ir_variable is made and added to the target shader. The new variable is
698 * added to \b both the instruction stream and the symbol table.
699 *
700 * \param inst IR tree that is to be processed.
701 * \param symbols Symbol table containing global scope symbols in the
702 * linked shader.
703 * \param instructions Instruction stream where new variable declarations
704 * should be added.
705 */
706void
Eric Anholt8273bd42010-08-04 12:34:56 -0700707remap_variables(ir_instruction *inst, struct gl_shader *target,
708 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700709{
710 class remap_visitor : public ir_hierarchical_visitor {
711 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700712 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700713 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700714 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700715 this->target = target;
716 this->symbols = target->symbols;
717 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700718 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700719 }
720
721 virtual ir_visitor_status visit(ir_dereference_variable *ir)
722 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700723 if (ir->var->mode == ir_var_temporary) {
724 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
725
726 assert(var != NULL);
727 ir->var = var;
728 return visit_continue;
729 }
730
Ian Romanick31a97862010-07-12 18:48:50 -0700731 ir_variable *const existing =
732 this->symbols->get_variable(ir->var->name);
733 if (existing != NULL)
734 ir->var = existing;
735 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700736 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700737
Eric Anholt001eee52010-11-05 06:11:24 -0700738 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700739 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700740 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700741 }
742
743 return visit_continue;
744 }
745
746 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700747 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700748 glsl_symbol_table *symbols;
749 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700750 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700751 };
752
Eric Anholt8273bd42010-08-04 12:34:56 -0700753 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700754
755 inst->accept(&v);
756}
757
758
759/**
760 * Move non-declarations from one instruction stream to another
761 *
762 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700763 * 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 -0700764 * pointer) for \c last and \c false for \c make_copies on the first
765 * call. Successive calls pass the return value of the previous call for
766 * \c last and \c true for \c make_copies.
767 *
768 * \param instructions Source instruction stream
769 * \param last Instruction after which new instructions should be
770 * inserted in the target instruction stream
771 * \param make_copies Flag selecting whether instructions in \c instructions
772 * should be copied (via \c ir_instruction::clone) into the
773 * target list or moved.
774 *
775 * \return
776 * The new "last" instruction in the target instruction stream. This pointer
777 * is suitable for use as the \c last parameter of a later call to this
778 * function.
779 */
780exec_node *
781move_non_declarations(exec_list *instructions, exec_node *last,
782 bool make_copies, gl_shader *target)
783{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700784 hash_table *temps = NULL;
785
786 if (make_copies)
787 temps = hash_table_ctor(0, hash_table_pointer_hash,
788 hash_table_pointer_compare);
789
Ian Romanick303c99f2010-07-19 12:34:56 -0700790 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700791 ir_instruction *inst = (ir_instruction *) node;
792
Ian Romanick7e2aa912010-07-19 17:12:42 -0700793 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700794 continue;
795
Ian Romanick7e2aa912010-07-19 17:12:42 -0700796 ir_variable *var = inst->as_variable();
797 if ((var != NULL) && (var->mode != ir_var_temporary))
798 continue;
799
800 assert(inst->as_assignment()
801 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700802
803 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700804 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700805
806 if (var != NULL)
807 hash_table_insert(temps, inst, var);
808 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700809 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700810 } else {
811 inst->remove();
812 }
813
814 last->insert_after(inst);
815 last = inst;
816 }
817
Ian Romanick7e2aa912010-07-19 17:12:42 -0700818 if (make_copies)
819 hash_table_dtor(temps);
820
Ian Romanick31a97862010-07-12 18:48:50 -0700821 return last;
822}
823
824/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700825 * Get the function signature for main from a shader
826 */
827static ir_function_signature *
828get_main_function_signature(gl_shader *sh)
829{
830 ir_function *const f = sh->symbols->get_function("main");
831 if (f != NULL) {
832 exec_list void_parameters;
833
834 /* Look for the 'void main()' signature and ensure that it's defined.
835 * This keeps the linker from accidentally pick a shader that just
836 * contains a prototype for main.
837 *
838 * We don't have to check for multiple definitions of main (in multiple
839 * shaders) because that would have already been caught above.
840 */
841 ir_function_signature *sig = f->matching_signature(&void_parameters);
842 if ((sig != NULL) && sig->is_defined) {
843 return sig;
844 }
845 }
846
847 return NULL;
848}
849
850
851/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700852 * Combine a group of shaders for a single stage to generate a linked shader
853 *
854 * \note
855 * If this function is supplied a single shader, it is cloned, and the new
856 * shader is returned.
857 */
858static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800859link_intrastage_shaders(void *mem_ctx,
860 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700861 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700862 struct gl_shader **shader_list,
863 unsigned num_shaders)
864{
Ian Romanick13f782c2010-06-29 18:53:38 -0700865 /* Check that global variables defined in multiple shaders are consistent.
866 */
867 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
868 return NULL;
869
870 /* Check that there is only a single definition of each function signature
871 * across all shaders.
872 */
873 for (unsigned i = 0; i < (num_shaders - 1); i++) {
874 foreach_list(node, shader_list[i]->ir) {
875 ir_function *const f = ((ir_instruction *) node)->as_function();
876
877 if (f == NULL)
878 continue;
879
880 for (unsigned j = i + 1; j < num_shaders; j++) {
881 ir_function *const other =
882 shader_list[j]->symbols->get_function(f->name);
883
884 /* If the other shader has no function (and therefore no function
885 * signatures) with the same name, skip to the next shader.
886 */
887 if (other == NULL)
888 continue;
889
890 foreach_iter (exec_list_iterator, iter, *f) {
891 ir_function_signature *sig =
892 (ir_function_signature *) iter.get();
893
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700894 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700895 continue;
896
897 ir_function_signature *other_sig =
898 other->exact_matching_signature(& sig->parameters);
899
900 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700901 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -0700902 linker_error(prog, "function `%s' is multiply defined",
903 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -0700904 return NULL;
905 }
906 }
907 }
908 }
909 }
910
911 /* Find the shader that defines main, and make a clone of it.
912 *
913 * Starting with the clone, search for undefined references. If one is
914 * found, find the shader that defines it. Clone the reference and add
915 * it to the shader. Repeat until there are no undefined references or
916 * until a reference cannot be resolved.
917 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700918 gl_shader *main = NULL;
919 for (unsigned i = 0; i < num_shaders; i++) {
920 if (get_main_function_signature(shader_list[i]) != NULL) {
921 main = shader_list[i];
922 break;
923 }
924 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700925
Ian Romanick15ce87e2010-07-09 15:28:22 -0700926 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -0700927 linker_error(prog, "%s shader lacks `main'\n",
928 (shader_list[0]->Type == GL_VERTEX_SHADER)
929 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -0700930 return NULL;
931 }
932
Ian Romanick4a455952010-10-13 15:13:02 -0700933 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700934 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800935 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700936
937 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700938
Ian Romanick31a97862010-07-12 18:48:50 -0700939 /* The a pointer to the main function in the final linked shader (i.e., the
940 * copy of the original shader that contained the main function).
941 */
942 ir_function_signature *const main_sig = get_main_function_signature(linked);
943
944 /* Move any instructions other than variable declarations or function
945 * declarations into main.
946 */
Ian Romanick9303e352010-07-19 12:33:54 -0700947 exec_node *insertion_point =
948 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
949 linked);
950
Ian Romanick31a97862010-07-12 18:48:50 -0700951 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700952 if (shader_list[i] == main)
953 continue;
954
Ian Romanick31a97862010-07-12 18:48:50 -0700955 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700956 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700957 }
958
Ian Romanick13f782c2010-06-29 18:53:38 -0700959 /* Resolve initializers for global variables in the linked shader.
960 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700961 unsigned num_linking_shaders = num_shaders;
962 for (unsigned i = 0; i < num_shaders; i++)
963 num_linking_shaders += shader_list[i]->num_builtins_to_link;
964
965 gl_shader **linking_shaders =
966 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
967
968 memcpy(linking_shaders, shader_list,
969 sizeof(linking_shaders[0]) * num_shaders);
970
971 unsigned idx = num_shaders;
972 for (unsigned i = 0; i < num_shaders; i++) {
973 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
974 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
975 idx += shader_list[i]->num_builtins_to_link;
976 }
977
978 assert(idx == num_linking_shaders);
979
Ian Romanick4a455952010-10-13 15:13:02 -0700980 if (!link_function_calls(prog, linked, linking_shaders,
981 num_linking_shaders)) {
982 ctx->Driver.DeleteShader(ctx, linked);
983 linked = NULL;
984 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700985
986 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -0700987
Paul Berryc148ef62011-08-03 15:37:01 -0700988#ifdef DEBUG
989 /* At this point linked should contain all of the linked IR, so
990 * validate it to make sure nothing went wrong.
991 */
992 if (linked)
993 validate_ir_tree(linked->ir);
994#endif
995
Ian Romanickc87e9ef2011-01-25 12:04:08 -0800996 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -0800997 * unspecified sizes have a size specified. The size is inferred from the
998 * max_array_access field.
999 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001000 if (linked != NULL) {
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001001 class array_sizing_visitor : public ir_hierarchical_visitor {
1002 public:
1003 virtual ir_visitor_status visit(ir_variable *var)
1004 {
1005 if (var->type->is_array() && (var->type->length == 0)) {
1006 const glsl_type *type =
1007 glsl_type::get_array_instance(var->type->fields.array,
Ian Romanick25b36e82011-02-15 18:17:53 -08001008 var->max_array_access + 1);
Ian Romanick6f539212010-12-07 18:30:33 -08001009
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001010 assert(type != NULL);
1011 var->type = type;
1012 }
Ian Romanick6f539212010-12-07 18:30:33 -08001013
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001014 return visit_continue;
1015 }
1016 } v;
Ian Romanick6f539212010-12-07 18:30:33 -08001017
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001018 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001019 }
1020
Ian Romanick3fb87872010-07-09 14:09:34 -07001021 return linked;
1022}
1023
Eric Anholta721abf2010-08-23 10:32:01 -07001024/**
1025 * Update the sizes of linked shader uniform arrays to the maximum
1026 * array index used.
1027 *
1028 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1029 *
1030 * If one or more elements of an array are active,
1031 * GetActiveUniform will return the name of the array in name,
1032 * subject to the restrictions listed above. The type of the array
1033 * is returned in type. The size parameter contains the highest
1034 * array element index used, plus one. The compiler or linker
1035 * determines the highest index used. There will be only one
1036 * active uniform reported by the GL per uniform array.
1037
1038 */
1039static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001040update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001041{
Ian Romanick3322fba2010-10-14 13:28:42 -07001042 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1043 if (prog->_LinkedShaders[i] == NULL)
1044 continue;
1045
Eric Anholta721abf2010-08-23 10:32:01 -07001046 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1047 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1048
Eric Anholt586b4b52010-09-28 14:32:16 -07001049 if ((var == NULL) || (var->mode != ir_var_uniform &&
1050 var->mode != ir_var_in &&
1051 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001052 !var->type->is_array())
1053 continue;
1054
1055 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001056 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1057 if (prog->_LinkedShaders[j] == NULL)
1058 continue;
1059
Eric Anholta721abf2010-08-23 10:32:01 -07001060 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1061 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1062 if (!other_var)
1063 continue;
1064
1065 if (strcmp(var->name, other_var->name) == 0 &&
1066 other_var->max_array_access > size) {
1067 size = other_var->max_array_access;
1068 }
1069 }
1070 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001071
Eric Anholta721abf2010-08-23 10:32:01 -07001072 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001073 /* If this is a built-in uniform (i.e., it's backed by some
1074 * fixed-function state), adjust the number of state slots to
1075 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001076 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001077 * slots is an integer multiple of the number of array elements.
1078 * Determine the number of slots per array element by dividing by
1079 * the old (total) size.
1080 */
1081 if (var->num_state_slots > 0) {
1082 var->num_state_slots = (size + 1)
1083 * (var->num_state_slots / var->type->length);
1084 }
1085
Eric Anholta721abf2010-08-23 10:32:01 -07001086 var->type = glsl_type::get_array_instance(var->type->fields.array,
1087 size + 1);
1088 /* FINISHME: We should update the types of array
1089 * dereferences of this variable now.
1090 */
1091 }
1092 }
1093 }
1094}
1095
Ian Romanick69846702010-06-22 17:29:19 -07001096/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001097 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001098 *
1099 * \param used_mask Bits representing used (1) and unused (0) locations
1100 * \param needed_count Number of contiguous bits needed.
1101 *
1102 * \return
1103 * Base location of the available bits on success or -1 on failure.
1104 */
1105int
1106find_available_slots(unsigned used_mask, unsigned needed_count)
1107{
1108 unsigned needed_mask = (1 << needed_count) - 1;
1109 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1110
1111 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1112 * cannot optimize possibly infinite loops" for the loop below.
1113 */
1114 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1115 return -1;
1116
1117 for (int i = 0; i <= max_bit_to_test; i++) {
1118 if ((needed_mask & ~used_mask) == needed_mask)
1119 return i;
1120
1121 needed_mask <<= 1;
1122 }
1123
1124 return -1;
1125}
1126
1127
Ian Romanickd32d4f72011-06-27 17:59:58 -07001128/**
1129 * Assign locations for either VS inputs for FS outputs
1130 *
1131 * \param prog Shader program whose variables need locations assigned
1132 * \param target_index Selector for the program target to receive location
1133 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1134 * \c MESA_SHADER_FRAGMENT.
1135 * \param max_index Maximum number of generic locations. This corresponds
1136 * to either the maximum number of draw buffers or the
1137 * maximum number of generic attributes.
1138 *
1139 * \return
1140 * If locations are successfully assigned, true is returned. Otherwise an
1141 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001142 */
Ian Romanick69846702010-06-22 17:29:19 -07001143bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001144assign_attribute_or_color_locations(gl_shader_program *prog,
1145 unsigned target_index,
1146 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001147{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001148 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001149 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001150 unsigned used_locations = (max_index >= 32)
1151 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001152
Ian Romanickd32d4f72011-06-27 17:59:58 -07001153 assert((target_index == MESA_SHADER_VERTEX)
1154 || (target_index == MESA_SHADER_FRAGMENT));
1155
1156 gl_shader *const sh = prog->_LinkedShaders[target_index];
1157 if (sh == NULL)
1158 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001159
Ian Romanick69846702010-06-22 17:29:19 -07001160 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001161 *
1162 * 1. Invalidate the location assignments for all vertex shader inputs.
1163 *
1164 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001165 * glBindVertexAttribLocation) locations and outputs that have
1166 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001167 *
Ian Romanick69846702010-06-22 17:29:19 -07001168 * 3. Sort the attributes without assigned locations by number of slots
1169 * required in decreasing order. Fragmentation caused by attribute
1170 * locations assigned by the application may prevent large attributes
1171 * from having enough contiguous space.
1172 *
1173 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001174 */
1175
Ian Romanickd32d4f72011-06-27 17:59:58 -07001176 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001177 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001178
Ian Romanickd32d4f72011-06-27 17:59:58 -07001179 const enum ir_variable_mode direction =
1180 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1181
1182
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07001183 link_invalidate_variable_locations(sh, direction, generic_base);
Ian Romanickd32d4f72011-06-27 17:59:58 -07001184
Ian Romanick69846702010-06-22 17:29:19 -07001185 /* Temporary storage for the set of attributes that need locations assigned.
1186 */
1187 struct temp_attr {
1188 unsigned slots;
1189 ir_variable *var;
1190
1191 /* Used below in the call to qsort. */
1192 static int compare(const void *a, const void *b)
1193 {
1194 const temp_attr *const l = (const temp_attr *) a;
1195 const temp_attr *const r = (const temp_attr *) b;
1196
1197 /* Reversed because we want a descending order sort below. */
1198 return r->slots - l->slots;
1199 }
1200 } to_assign[16];
1201
1202 unsigned num_attr = 0;
1203
Eric Anholt16b68b12010-06-30 11:05:43 -07001204 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001205 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1206
Brian Paul4470ff22011-07-19 21:10:25 -06001207 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001208 continue;
1209
Ian Romanick68a4fc92010-10-07 17:21:22 -07001210 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001211 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001212 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001213 linker_error(prog,
1214 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001215 (var->location < 0)
1216 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001217 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001218 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001219 }
1220 } else if (target_index == MESA_SHADER_VERTEX) {
1221 unsigned binding;
1222
1223 if (prog->AttributeBindings->get(binding, var->name)) {
1224 assert(binding >= VERT_ATTRIB_GENERIC0);
1225 var->location = binding;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001226 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001227 } else if (target_index == MESA_SHADER_FRAGMENT) {
1228 unsigned binding;
1229
1230 if (prog->FragDataBindings->get(binding, var->name)) {
1231 assert(binding >= FRAG_RESULT_DATA0);
1232 var->location = binding;
1233 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001234 }
1235
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001236 /* If the variable is not a built-in and has a location statically
1237 * assigned in the shader (presumably via a layout qualifier), make sure
1238 * that it doesn't collide with other assigned locations. Otherwise,
1239 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001240 */
Ian Romanick523b6112011-08-17 15:40:03 -07001241 const unsigned slots = count_attribute_slots(var->type);
1242 if (var->location != -1) {
1243 if (var->location >= generic_base) {
1244 /* From page 61 of the OpenGL 4.0 spec:
1245 *
1246 * "LinkProgram will fail if the attribute bindings assigned
1247 * by BindAttribLocation do not leave not enough space to
1248 * assign a location for an active matrix attribute or an
1249 * active attribute array, both of which require multiple
1250 * contiguous generic attributes."
1251 *
1252 * Previous versions of the spec contain similar language but omit
1253 * the bit about attribute arrays.
1254 *
1255 * Page 61 of the OpenGL 4.0 spec also says:
1256 *
1257 * "It is possible for an application to bind more than one
1258 * attribute name to the same location. This is referred to as
1259 * aliasing. This will only work if only one of the aliased
1260 * attributes is active in the executable program, or if no
1261 * path through the shader consumes more than one attribute of
1262 * a set of attributes aliased to the same location. A link
1263 * error can occur if the linker determines that every path
1264 * through the shader consumes multiple aliased attributes,
1265 * but implementations are not required to generate an error
1266 * in this case."
1267 *
1268 * These two paragraphs are either somewhat contradictory, or I
1269 * don't fully understand one or both of them.
1270 */
1271 /* FINISHME: The code as currently written does not support
1272 * FINISHME: attribute location aliasing (see comment above).
1273 */
1274 /* Mask representing the contiguous slots that will be used by
1275 * this attribute.
1276 */
1277 const unsigned attr = var->location - generic_base;
1278 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001279
Ian Romanick523b6112011-08-17 15:40:03 -07001280 /* Generate a link error if the set of bits requested for this
1281 * attribute overlaps any previously allocated bits.
1282 */
1283 if ((~(use_mask << attr) & used_locations) != used_locations) {
1284 linker_error(prog,
1285 "insufficient contiguous attribute locations "
1286 "available for vertex shader input `%s'",
1287 var->name);
1288 return false;
1289 }
1290
1291 used_locations |= (use_mask << attr);
1292 }
1293
1294 continue;
1295 }
1296
1297 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001298 to_assign[num_attr].var = var;
1299 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001300 }
Ian Romanick69846702010-06-22 17:29:19 -07001301
1302 /* If all of the attributes were assigned locations by the application (or
1303 * are built-in attributes with fixed locations), return early. This should
1304 * be the common case.
1305 */
1306 if (num_attr == 0)
1307 return true;
1308
1309 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1310
Ian Romanickd32d4f72011-06-27 17:59:58 -07001311 if (target_index == MESA_SHADER_VERTEX) {
1312 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1313 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1314 * reserved to prevent it from being automatically allocated below.
1315 */
1316 find_deref_visitor find("gl_Vertex");
1317 find.run(sh->ir);
1318 if (find.variable_found())
1319 used_locations |= (1 << 0);
1320 }
Ian Romanick982e3792010-06-29 18:58:20 -07001321
Ian Romanick69846702010-06-22 17:29:19 -07001322 for (unsigned i = 0; i < num_attr; i++) {
1323 /* Mask representing the contiguous slots that will be used by this
1324 * attribute.
1325 */
1326 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1327
1328 int location = find_available_slots(used_locations, to_assign[i].slots);
1329
1330 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001331 const char *const string = (target_index == MESA_SHADER_VERTEX)
1332 ? "vertex shader input" : "fragment shader output";
1333
Ian Romanick586e7412011-07-28 14:04:09 -07001334 linker_error(prog,
1335 "insufficient contiguous attribute locations "
1336 "available for %s `%s'",
1337 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001338 return false;
1339 }
1340
Ian Romanickd32d4f72011-06-27 17:59:58 -07001341 to_assign[i].var->location = generic_base + location;
Ian Romanick69846702010-06-22 17:29:19 -07001342 used_locations |= (use_mask << location);
1343 }
1344
1345 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001346}
1347
1348
Ian Romanick40e114b2010-08-17 14:55:50 -07001349/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001350 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001351 */
1352void
Ian Romanickcc90e622010-10-19 17:59:10 -07001353demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001354{
1355 foreach_list(node, sh->ir) {
1356 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1357
Ian Romanickcc90e622010-10-19 17:59:10 -07001358 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001359 continue;
1360
Ian Romanickcc90e622010-10-19 17:59:10 -07001361 /* A shader 'in' or 'out' variable is only really an input or output if
1362 * its value is used by other shader stages. This will cause the variable
1363 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001364 */
1365 if (var->location == -1) {
1366 var->mode = ir_var_auto;
1367 }
1368 }
1369}
1370
1371
Paul Berry871ddb92011-11-05 11:17:32 -07001372/**
1373 * Data structure tracking information about a transform feedback declaration
1374 * during linking.
1375 */
1376class tfeedback_decl
1377{
1378public:
1379 bool init(struct gl_shader_program *prog, const void *mem_ctx,
1380 const char *input);
1381 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1382 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1383 ir_variable *output_var);
1384 bool store(struct gl_shader_program *prog,
1385 struct gl_transform_feedback_info *info, unsigned buffer) const;
1386
1387
1388 /**
1389 * True if assign_location() has been called for this object.
1390 */
1391 bool is_assigned() const
1392 {
1393 return this->location != -1;
1394 }
1395
1396 /**
1397 * Determine whether this object refers to the variable var.
1398 */
1399 bool matches_var(ir_variable *var) const
1400 {
1401 return strcmp(var->name, this->var_name) == 0;
1402 }
1403
1404 /**
1405 * The total number of varying components taken up by this variable. Only
1406 * valid if is_assigned() is true.
1407 */
1408 unsigned num_components() const
1409 {
1410 return this->vector_elements * this->matrix_columns;
1411 }
1412
1413private:
1414 /**
1415 * The name that was supplied to glTransformFeedbackVaryings. Used for
1416 * error reporting.
1417 */
1418 const char *orig_name;
1419
1420 /**
1421 * The name of the variable, parsed from orig_name.
1422 */
1423 char *var_name;
1424
1425 /**
1426 * True if the declaration in orig_name represents an array.
1427 */
1428 bool is_array;
1429
1430 /**
1431 * If is_array is true, the array index that was specified in orig_name.
1432 */
1433 unsigned array_index;
1434
1435 /**
1436 * The vertex shader output location that the linker assigned for this
1437 * variable. -1 if a location hasn't been assigned yet.
1438 */
1439 int location;
1440
1441 /**
1442 * If location != -1, the number of vector elements in this variable, or 1
1443 * if this variable is a scalar.
1444 */
1445 unsigned vector_elements;
1446
1447 /**
1448 * If location != -1, the number of matrix columns in this variable, or 1
1449 * if this variable is not a matrix.
1450 */
1451 unsigned matrix_columns;
1452};
1453
1454
1455/**
1456 * Initialize this object based on a string that was passed to
1457 * glTransformFeedbackVaryings. If there is a parse error, the error is
1458 * reported using linker_error(), and false is returned.
1459 */
1460bool
1461tfeedback_decl::init(struct gl_shader_program *prog, const void *mem_ctx,
1462 const char *input)
1463{
1464 /* We don't have to be pedantic about what is a valid GLSL variable name,
1465 * because any variable with an invalid name can't exist in the IR anyway.
1466 */
1467
1468 this->location = -1;
1469 this->orig_name = input;
1470
1471 const char *bracket = strrchr(input, '[');
1472
1473 if (bracket) {
1474 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
1475 if (sscanf(bracket, "[%u]", &this->array_index) == 1) {
1476 this->is_array = true;
1477 return true;
1478 }
1479 } else {
1480 this->var_name = ralloc_strdup(mem_ctx, input);
1481 this->is_array = false;
1482 return true;
1483 }
1484
1485 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1486 return false;
1487}
1488
1489
1490/**
1491 * Determine whether two tfeedback_decl objects refer to the same variable and
1492 * array index (if applicable).
1493 */
1494bool
1495tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1496{
1497 if (strcmp(x.var_name, y.var_name) != 0)
1498 return false;
1499 if (x.is_array != y.is_array)
1500 return false;
1501 if (x.is_array && x.array_index != y.array_index)
1502 return false;
1503 return true;
1504}
1505
1506
1507/**
1508 * Assign a location for this tfeedback_decl object based on the location
1509 * assignment in output_var.
1510 *
1511 * If an error occurs, the error is reported through linker_error() and false
1512 * is returned.
1513 */
1514bool
1515tfeedback_decl::assign_location(struct gl_context *ctx,
1516 struct gl_shader_program *prog,
1517 ir_variable *output_var)
1518{
1519 if (output_var->type->is_array()) {
1520 /* Array variable */
1521 if (!this->is_array) {
1522 linker_error(prog, "Transform feedback varying %s found, "
1523 "but it's not an array ([] not expected).",
1524 this->orig_name);
1525 return false;
1526 }
1527 /* Check array bounds. */
1528 if (this->array_index >=
1529 (unsigned) output_var->type->array_size()) {
1530 linker_error(prog, "Transform feedback varying %s has index "
1531 "%i, but the array size is %i.",
1532 this->orig_name, this->array_index,
1533 output_var->type->array_size());
1534 return false;
1535 }
1536 const unsigned matrix_cols =
1537 output_var->type->fields.array->matrix_columns;
1538 this->location = output_var->location + this->array_index * matrix_cols;
1539 this->vector_elements = output_var->type->fields.array->vector_elements;
1540 this->matrix_columns = matrix_cols;
1541 } else {
1542 /* Regular variable (scalar, vector, or matrix) */
1543 if (this->is_array) {
1544 linker_error(prog, "Transform feedback varying %s found, "
1545 "but it's an array ([] expected).",
1546 this->orig_name);
1547 return false;
1548 }
1549 this->location = output_var->location;
1550 this->vector_elements = output_var->type->vector_elements;
1551 this->matrix_columns = output_var->type->matrix_columns;
1552 }
1553 /* From GL_EXT_transform_feedback:
1554 * A program will fail to link if:
1555 *
1556 * * the total number of components to capture in any varying
1557 * variable in <varyings> is greater than the constant
1558 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1559 * buffer mode is SEPARATE_ATTRIBS_EXT;
1560 */
1561 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1562 this->num_components() >
1563 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1564 linker_error(prog, "Transform feedback varying %s exceeds "
1565 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1566 this->orig_name);
1567 return false;
1568 }
1569
1570 return true;
1571}
1572
1573
1574/**
1575 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1576 *
1577 * If an error occurs, the error is reported through linker_error() and false
1578 * is returned.
1579 */
1580bool
1581tfeedback_decl::store(struct gl_shader_program *prog,
1582 struct gl_transform_feedback_info *info,
1583 unsigned buffer) const
1584{
1585 if (!this->is_assigned()) {
1586 /* From GL_EXT_transform_feedback:
1587 * A program will fail to link if:
1588 *
1589 * * any variable name specified in the <varyings> array is not
1590 * declared as an output in the geometry shader (if present) or
1591 * the vertex shader (if no geometry shader is present);
1592 */
1593 linker_error(prog, "Transform feedback varying %s undeclared.",
1594 this->orig_name);
1595 return false;
1596 }
1597 for (unsigned v = 0; v < this->matrix_columns; ++v) {
1598 info->Outputs[info->NumOutputs].OutputRegister = this->location + v;
1599 info->Outputs[info->NumOutputs].NumComponents = this->vector_elements;
1600 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1601 ++info->NumOutputs;
1602 }
1603 return true;
1604}
1605
1606
1607/**
1608 * Parse all the transform feedback declarations that were passed to
1609 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1610 *
1611 * If an error occurs, the error is reported through linker_error() and false
1612 * is returned.
1613 */
1614static bool
1615parse_tfeedback_decls(struct gl_shader_program *prog, const void *mem_ctx,
1616 unsigned num_names, char **varying_names,
1617 tfeedback_decl *decls)
1618{
1619 for (unsigned i = 0; i < num_names; ++i) {
1620 if (!decls[i].init(prog, mem_ctx, varying_names[i]))
1621 return false;
1622 /* From GL_EXT_transform_feedback:
1623 * A program will fail to link if:
1624 *
1625 * * any two entries in the <varyings> array specify the same varying
1626 * variable;
1627 *
1628 * We interpret this to mean "any two entries in the <varyings> array
1629 * specify the same varying variable and array index", since transform
1630 * feedback of arrays would be useless otherwise.
1631 */
1632 for (unsigned j = 0; j < i; ++j) {
1633 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1634 linker_error(prog, "Transform feedback varying %s specified "
1635 "more than once.", varying_names[i]);
1636 return false;
1637 }
1638 }
1639 }
1640 return true;
1641}
1642
1643
1644/**
1645 * Assign a location for a variable that is produced in one pipeline stage
1646 * (the "producer") and consumed in the next stage (the "consumer").
1647 *
1648 * \param input_var is the input variable declaration in the consumer.
1649 *
1650 * \param output_var is the output variable declaration in the producer.
1651 *
1652 * \param input_index is the counter that keeps track of assigned input
1653 * locations in the consumer.
1654 *
1655 * \param output_index is the counter that keeps track of assigned output
1656 * locations in the producer.
1657 *
1658 * It is permissible for \c input_var to be NULL (this happens if a variable
1659 * is output by the producer and consumed by transform feedback, but not
1660 * consumed by the consumer).
1661 *
1662 * If the variable has already been assigned a location, this function has no
1663 * effect.
1664 */
1665void
1666assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1667 unsigned *input_index, unsigned *output_index)
1668{
1669 if (output_var->location != -1) {
1670 /* Location already assigned. */
1671 return;
1672 }
1673
1674 if (input_var) {
1675 assert(input_var->location == -1);
1676 input_var->location = *input_index;
1677 }
1678
1679 output_var->location = *output_index;
1680
1681 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1682 assert(!output_var->type->is_record());
1683
1684 if (output_var->type->is_array()) {
1685 const unsigned slots = output_var->type->length
1686 * output_var->type->fields.array->matrix_columns;
1687
1688 *output_index += slots;
1689 *input_index += slots;
1690 } else {
1691 const unsigned slots = output_var->type->matrix_columns;
1692
1693 *output_index += slots;
1694 *input_index += slots;
1695 }
1696}
1697
1698
1699/**
1700 * Assign locations for all variables that are produced in one pipeline stage
1701 * (the "producer") and consumed in the next stage (the "consumer").
1702 *
1703 * Variables produced by the producer may also be consumed by transform
1704 * feedback.
1705 *
1706 * \param num_tfeedback_decls is the number of declarations indicating
1707 * variables that may be consumed by transform feedback.
1708 *
1709 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
1710 * representing the result of parsing the strings passed to
1711 * glTransformFeedbackVaryings(). assign_location() will be called for
1712 * each of these objects that matches one of the outputs of the
1713 * producer.
1714 *
1715 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
1716 * be NULL. In this case, varying locations are assigned solely based on the
1717 * requirements of transform feedback.
1718 */
Ian Romanickde773242011-06-09 13:31:32 -07001719bool
1720assign_varying_locations(struct gl_context *ctx,
1721 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07001722 gl_shader *producer, gl_shader *consumer,
1723 unsigned num_tfeedback_decls,
1724 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07001725{
1726 /* FINISHME: Set dynamically when geometry shader support is added. */
1727 unsigned output_index = VERT_RESULT_VAR0;
1728 unsigned input_index = FRAG_ATTRIB_VAR0;
1729
1730 /* Operate in a total of three passes.
1731 *
1732 * 1. Assign locations for any matching inputs and outputs.
1733 *
1734 * 2. Mark output variables in the producer that do not have locations as
1735 * not being outputs. This lets the optimizer eliminate them.
1736 *
1737 * 3. Mark input variables in the consumer that do not have locations as
1738 * not being inputs. This lets the optimizer eliminate them.
1739 */
1740
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07001741 link_invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
Paul Berry871ddb92011-11-05 11:17:32 -07001742 if (consumer)
1743 link_invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
Ian Romanick0e59b262010-06-23 11:23:01 -07001744
Eric Anholt16b68b12010-06-30 11:05:43 -07001745 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001746 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1747
Paul Berry871ddb92011-11-05 11:17:32 -07001748 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07001749 continue;
1750
Paul Berry871ddb92011-11-05 11:17:32 -07001751 ir_variable *input_var =
1752 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07001753
Paul Berry871ddb92011-11-05 11:17:32 -07001754 if (input_var && input_var->mode != ir_var_in)
1755 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07001756
Paul Berry871ddb92011-11-05 11:17:32 -07001757 if (input_var) {
1758 assign_varying_location(input_var, output_var, &input_index,
1759 &output_index);
1760 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001761
Paul Berry871ddb92011-11-05 11:17:32 -07001762 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
1763 if (!tfeedback_decls[i].is_assigned() &&
1764 tfeedback_decls[i].matches_var(output_var)) {
1765 if (output_var->location == -1) {
1766 assign_varying_location(input_var, output_var, &input_index,
1767 &output_index);
1768 }
1769 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
1770 return false;
1771 }
Ian Romanickdf869d92010-08-30 15:37:44 -07001772 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001773 }
1774
Ian Romanickde773242011-06-09 13:31:32 -07001775 unsigned varying_vectors = 0;
1776
Paul Berry871ddb92011-11-05 11:17:32 -07001777 if (consumer) {
1778 foreach_list(node, consumer->ir) {
1779 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07001780
Paul Berry871ddb92011-11-05 11:17:32 -07001781 if ((var == NULL) || (var->mode != ir_var_in))
1782 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07001783
Paul Berry871ddb92011-11-05 11:17:32 -07001784 if (var->location == -1) {
1785 if (prog->Version <= 120) {
1786 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1787 *
1788 * Only those varying variables used (i.e. read) in
1789 * the fragment shader executable must be written to
1790 * by the vertex shader executable; declaring
1791 * superfluous varying variables in a vertex shader is
1792 * permissible.
1793 *
1794 * We interpret this text as meaning that the VS must
1795 * write the variable for the FS to read it. See
1796 * "glsl1-varying read but not written" in piglit.
1797 */
Eric Anholtb7062832010-07-28 13:52:23 -07001798
Paul Berry871ddb92011-11-05 11:17:32 -07001799 linker_error(prog, "fragment shader varying %s not written "
1800 "by vertex shader\n.", var->name);
1801 }
Eric Anholtb7062832010-07-28 13:52:23 -07001802
Paul Berry871ddb92011-11-05 11:17:32 -07001803 /* An 'in' variable is only really a shader input if its
1804 * value is written by the previous stage.
1805 */
1806 var->mode = ir_var_auto;
1807 } else {
1808 /* The packing rules are used for vertex shader inputs are also
1809 * used for fragment shader inputs.
1810 */
1811 varying_vectors += count_attribute_slots(var->type);
1812 }
Eric Anholtb7062832010-07-28 13:52:23 -07001813 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001814 }
Ian Romanickde773242011-06-09 13:31:32 -07001815
1816 if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
1817 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001818 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
1819 linker_warning(prog, "shader uses too many varying vectors "
1820 "(%u > %u), but the driver will try to optimize "
1821 "them out; this is non-portable out-of-spec "
1822 "behavior\n",
1823 varying_vectors, ctx->Const.MaxVarying);
1824 } else {
1825 linker_error(prog, "shader uses too many varying vectors "
1826 "(%u > %u)\n",
1827 varying_vectors, ctx->Const.MaxVarying);
1828 return false;
1829 }
Ian Romanickde773242011-06-09 13:31:32 -07001830 }
1831 } else {
1832 const unsigned float_components = varying_vectors * 4;
1833 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001834 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
1835 linker_warning(prog, "shader uses too many varying components "
1836 "(%u > %u), but the driver will try to optimize "
1837 "them out; this is non-portable out-of-spec "
1838 "behavior\n",
1839 float_components, ctx->Const.MaxVarying * 4);
1840 } else {
1841 linker_error(prog, "shader uses too many varying components "
1842 "(%u > %u)\n",
1843 float_components, ctx->Const.MaxVarying * 4);
1844 return false;
1845 }
Ian Romanickde773242011-06-09 13:31:32 -07001846 }
1847 }
1848
1849 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07001850}
1851
1852
Paul Berry871ddb92011-11-05 11:17:32 -07001853/**
1854 * Store transform feedback location assignments into
1855 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
1856 *
1857 * If an error occurs, the error is reported through linker_error() and false
1858 * is returned.
1859 */
1860static bool
1861store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
1862 unsigned num_tfeedback_decls,
1863 tfeedback_decl *tfeedback_decls)
1864{
1865 unsigned total_tfeedback_components = 0;
1866 prog->LinkedTransformFeedback.NumOutputs = 0;
1867 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
1868 unsigned buffer =
1869 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS ? i : 0;
1870 if (!tfeedback_decls[i].store(prog, &prog->LinkedTransformFeedback,
1871 buffer))
1872 return false;
1873 total_tfeedback_components += tfeedback_decls[i].num_components();
1874 }
1875
1876 /* From GL_EXT_transform_feedback:
1877 * A program will fail to link if:
1878 *
1879 * * the total number of components to capture is greater than
1880 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1881 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1882 */
1883 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1884 total_tfeedback_components >
1885 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1886 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1887 "limit has been exceeded.");
1888 return false;
1889 }
1890
1891 return true;
1892}
1893
Ian Romanick92f81592011-11-08 12:37:19 -08001894/**
Marek Olšákec174a42011-11-18 15:00:10 +01001895 * Store the gl_FragDepth layout in the gl_shader_program struct.
1896 */
1897static void
1898store_fragdepth_layout(struct gl_shader_program *prog)
1899{
1900 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1901 return;
1902 }
1903
1904 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1905
1906 /* We don't look up the gl_FragDepth symbol directly because if
1907 * gl_FragDepth is not used in the shader, it's removed from the IR.
1908 * However, the symbol won't be removed from the symbol table.
1909 *
1910 * We're only interested in the cases where the variable is NOT removed
1911 * from the IR.
1912 */
1913 foreach_list(node, ir) {
1914 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1915
1916 if (var == NULL || var->mode != ir_var_out) {
1917 continue;
1918 }
1919
1920 if (strcmp(var->name, "gl_FragDepth") == 0) {
1921 switch (var->depth_layout) {
1922 case ir_depth_layout_none:
1923 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1924 return;
1925 case ir_depth_layout_any:
1926 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1927 return;
1928 case ir_depth_layout_greater:
1929 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1930 return;
1931 case ir_depth_layout_less:
1932 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1933 return;
1934 case ir_depth_layout_unchanged:
1935 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1936 return;
1937 default:
1938 assert(0);
1939 return;
1940 }
1941 }
1942 }
1943}
1944
1945/**
Ian Romanick92f81592011-11-08 12:37:19 -08001946 * Validate the resources used by a program versus the implementation limits
1947 */
1948static bool
1949check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1950{
1951 static const char *const shader_names[MESA_SHADER_TYPES] = {
1952 "vertex", "fragment", "geometry"
1953 };
1954
1955 const unsigned max_samplers[MESA_SHADER_TYPES] = {
1956 ctx->Const.MaxVertexTextureImageUnits,
1957 ctx->Const.MaxTextureImageUnits,
1958 ctx->Const.MaxGeometryTextureImageUnits
1959 };
1960
1961 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
1962 ctx->Const.VertexProgram.MaxUniformComponents,
1963 ctx->Const.FragmentProgram.MaxUniformComponents,
1964 0 /* FINISHME: Geometry shaders. */
1965 };
1966
1967 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1968 struct gl_shader *sh = prog->_LinkedShaders[i];
1969
1970 if (sh == NULL)
1971 continue;
1972
1973 if (sh->num_samplers > max_samplers[i]) {
1974 linker_error(prog, "Too many %s shader texture samplers",
1975 shader_names[i]);
1976 }
1977
1978 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001979 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1980 linker_warning(prog, "Too many %s shader uniform components, "
1981 "but the driver will try to optimize them out; "
1982 "this is non-portable out-of-spec behavior\n",
1983 shader_names[i]);
1984 } else {
1985 linker_error(prog, "Too many %s shader uniform components",
1986 shader_names[i]);
1987 }
Ian Romanick92f81592011-11-08 12:37:19 -08001988 }
1989 }
1990
1991 return prog->LinkStatus;
1992}
Paul Berry871ddb92011-11-05 11:17:32 -07001993
Ian Romanick0e59b262010-06-23 11:23:01 -07001994void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001995link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001996{
Paul Berry871ddb92011-11-05 11:17:32 -07001997 tfeedback_decl *tfeedback_decls = NULL;
1998 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1999
Kenneth Graunked3073f52011-01-21 14:32:31 -08002000 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002001
Ian Romanick832dfa52010-06-17 15:04:20 -07002002 prog->LinkStatus = false;
2003 prog->Validated = false;
2004 prog->_Used = false;
2005
Ian Romanickf36460e2010-06-23 12:07:22 -07002006 if (prog->InfoLog != NULL)
Kenneth Graunked3073f52011-01-21 14:32:31 -08002007 ralloc_free(prog->InfoLog);
Ian Romanickf36460e2010-06-23 12:07:22 -07002008
Kenneth Graunked3073f52011-01-21 14:32:31 -08002009 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002010
Ian Romanick832dfa52010-06-17 15:04:20 -07002011 /* Separate the shaders into groups based on their type.
2012 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002013 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002014 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002015 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002016 unsigned num_frag_shaders = 0;
2017
Eric Anholt16b68b12010-06-30 11:05:43 -07002018 vert_shader_list = (struct gl_shader **)
2019 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002020 frag_shader_list = &vert_shader_list[prog->NumShaders];
2021
Ian Romanick25f51d32010-07-16 15:51:50 -07002022 unsigned min_version = UINT_MAX;
2023 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07002024 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002025 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2026 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2027
Ian Romanick832dfa52010-06-17 15:04:20 -07002028 switch (prog->Shaders[i]->Type) {
2029 case GL_VERTEX_SHADER:
2030 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2031 num_vert_shaders++;
2032 break;
2033 case GL_FRAGMENT_SHADER:
2034 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2035 num_frag_shaders++;
2036 break;
2037 case GL_GEOMETRY_SHADER:
2038 /* FINISHME: Support geometry shaders. */
2039 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2040 break;
2041 }
2042 }
2043
Ian Romanick25f51d32010-07-16 15:51:50 -07002044 /* Previous to GLSL version 1.30, different compilation units could mix and
2045 * match shading language versions. With GLSL 1.30 and later, the versions
2046 * of all shaders must match.
2047 */
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002048 assert(min_version >= 100);
Ian Romanick25f51d32010-07-16 15:51:50 -07002049 assert(max_version <= 130);
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002050 if ((max_version >= 130 || min_version == 100)
2051 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002052 linker_error(prog, "all shaders must use same shading "
2053 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002054 goto done;
2055 }
2056
2057 prog->Version = max_version;
2058
Ian Romanick3322fba2010-10-14 13:28:42 -07002059 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2060 if (prog->_LinkedShaders[i] != NULL)
2061 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2062
2063 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002064 }
2065
Ian Romanickcd6764e2010-07-16 16:00:07 -07002066 /* Link all shaders for a particular stage and validate the result.
2067 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002068 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002069 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002070 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2071 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002072
2073 if (sh == NULL)
2074 goto done;
2075
2076 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002077 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002078
Ian Romanick3322fba2010-10-14 13:28:42 -07002079 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2080 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002081 }
2082
2083 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002084 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002085 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2086 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002087
2088 if (sh == NULL)
2089 goto done;
2090
2091 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002092 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002093
Ian Romanick3322fba2010-10-14 13:28:42 -07002094 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2095 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002096 }
2097
Ian Romanick3ed850e2010-06-23 12:18:21 -07002098 /* Here begins the inter-stage linking phase. Some initial validation is
2099 * performed, then locations are assigned for uniforms, attributes, and
2100 * varyings.
2101 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002102 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002103 unsigned prev;
2104
2105 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2106 if (prog->_LinkedShaders[prev] != NULL)
2107 break;
2108 }
2109
Bryan Cainf18a0862011-04-23 19:29:15 -05002110 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002111 * stage.
2112 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002113 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2114 if (prog->_LinkedShaders[i] == NULL)
2115 continue;
2116
Ian Romanickf36460e2010-06-23 12:07:22 -07002117 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002118 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002119 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002120 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002121
2122 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002123 }
2124
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002125 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002126 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002127
Eric Anholt2f4fe152010-08-10 13:06:49 -07002128 /* Do common optimization before assigning storage for attributes,
2129 * uniforms, and varyings. Later optimization could possibly make
2130 * some of that unused.
2131 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002132 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2133 if (prog->_LinkedShaders[i] == NULL)
2134 continue;
2135
Ian Romanick02c5ae12011-07-11 10:46:01 -07002136 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2137 if (!prog->LinkStatus)
2138 goto done;
2139
Paul Berryc06e3252011-08-11 20:58:21 -07002140 if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
2141 lower_clip_distance(prog->_LinkedShaders[i]->ir);
2142
Ian Romanick1d5d67f2011-10-21 11:17:39 -07002143 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, 32))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002144 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002145 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002146
Ian Romanickd32d4f72011-06-27 17:59:58 -07002147 /* FINISHME: The value of the max_attribute_index parameter is
2148 * FINISHME: implementation dependent based on the value of
2149 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2150 * FINISHME: at least 16, so hardcode 16 for now.
2151 */
2152 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002153 goto done;
2154 }
2155
2156 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, ctx->Const.MaxDrawBuffers)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002157 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002158 }
2159
Ian Romanick3322fba2010-10-14 13:28:42 -07002160 unsigned prev;
2161 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2162 if (prog->_LinkedShaders[prev] != NULL)
2163 break;
2164 }
2165
Paul Berry871ddb92011-11-05 11:17:32 -07002166 if (num_tfeedback_decls != 0) {
2167 /* From GL_EXT_transform_feedback:
2168 * A program will fail to link if:
2169 *
2170 * * the <count> specified by TransformFeedbackVaryingsEXT is
2171 * non-zero, but the program object has no vertex or geometry
2172 * shader;
2173 */
2174 if (prev >= MESA_SHADER_FRAGMENT) {
2175 linker_error(prog, "Transform feedback varyings specified, but "
2176 "no vertex or geometry shader is present.");
2177 goto done;
2178 }
2179
2180 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2181 prog->TransformFeedback.NumVarying);
2182 if (!parse_tfeedback_decls(prog, mem_ctx, num_tfeedback_decls,
2183 prog->TransformFeedback.VaryingNames,
2184 tfeedback_decls))
2185 goto done;
2186 }
2187
Ian Romanick3322fba2010-10-14 13:28:42 -07002188 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2189 if (prog->_LinkedShaders[i] == NULL)
2190 continue;
2191
Paul Berry871ddb92011-11-05 11:17:32 -07002192 if (!assign_varying_locations(
2193 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2194 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2195 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002196 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002197
Ian Romanick3322fba2010-10-14 13:28:42 -07002198 prev = i;
2199 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002200
Paul Berry871ddb92011-11-05 11:17:32 -07002201 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2202 /* There was no fragment shader, but we still have to assign varying
2203 * locations for use by transform feedback.
2204 */
2205 if (!assign_varying_locations(
2206 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2207 tfeedback_decls))
2208 goto done;
2209 }
2210
2211 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2212 goto done;
2213
Ian Romanickcc90e622010-10-19 17:59:10 -07002214 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2215 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2216 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002217
2218 /* Eliminate code that is now dead due to unused vertex outputs being
2219 * demoted.
2220 */
2221 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2222 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002223 }
2224
2225 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2226 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2227
2228 demote_shader_inputs_and_outputs(sh, ir_var_in);
2229 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2230 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002231
2232 /* Eliminate code that is now dead due to unused geometry outputs being
2233 * demoted.
2234 */
2235 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2236 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002237 }
2238
2239 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2240 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2241
2242 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002243
2244 /* Eliminate code that is now dead due to unused fragment inputs being
2245 * demoted. This shouldn't actually do anything other than remove
2246 * declarations of the (now unused) global variables.
2247 */
2248 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2249 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002250 }
2251
Ian Romanick960d7222011-10-21 11:21:02 -07002252 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002253 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002254 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002255
Ian Romanick92f81592011-11-08 12:37:19 -08002256 if (!check_resources(ctx, prog))
2257 goto done;
2258
Ian Romanickce9171f2011-02-03 17:10:14 -08002259 /* OpenGL ES requires that a vertex shader and a fragment shader both be
2260 * present in a linked program. By checking for use of shading language
2261 * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
2262 */
Eric Anholt57f79782011-07-22 12:57:47 -07002263 if (!prog->InternalSeparateShader &&
2264 (ctx->API == API_OPENGLES2 || prog->Version == 100)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002265 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002266 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002267 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002268 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002269 }
2270 }
2271
Ian Romanick13e10e42010-06-21 12:03:24 -07002272 /* FINISHME: Assign fragment shader output locations. */
2273
Ian Romanick832dfa52010-06-17 15:04:20 -07002274done:
2275 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002276
2277 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2278 if (prog->_LinkedShaders[i] == NULL)
2279 continue;
2280
2281 /* Retain any live IR, but trash the rest. */
2282 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002283
2284 /* The symbol table in the linked shaders may contain references to
2285 * variables that were removed (e.g., unused uniforms). Since it may
2286 * contain junk, there is no possible valid use. Delete it and set the
2287 * pointer to NULL.
2288 */
2289 delete prog->_LinkedShaders[i]->symbols;
2290 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002291 }
2292
Kenneth Graunked3073f52011-01-21 14:32:31 -08002293 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002294}