blob: 39169aa10ff46891e7a84c05eda1ff7688835e70 [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 *
Paul Berry642e5b412012-01-04 13:57:52 -0800249 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
250 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700251 *
252 * \param shader Vertex shader executable to be verified
253 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700254bool
Eric Anholt849e1812010-06-30 11:49:17 -0700255validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700256 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700257{
258 if (shader == NULL)
259 return true;
260
Ian Romanick832dfa52010-06-17 15:04:20 -0700261 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700262 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700263 if (!find.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700264 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700265 return false;
266 }
267
Paul Berry642e5b412012-01-04 13:57:52 -0800268 prog->Vert.ClipDistanceArraySize = 0;
269
Paul Berryb453ba22011-08-11 18:10:22 -0700270 if (prog->Version >= 130) {
271 /* From section 7.1 (Vertex Shader Special Variables) of the
272 * GLSL 1.30 spec:
273 *
274 * "It is an error for a shader to statically write both
275 * gl_ClipVertex and gl_ClipDistance."
276 */
277 find_assignment_visitor clip_vertex("gl_ClipVertex");
278 find_assignment_visitor clip_distance("gl_ClipDistance");
279
280 clip_vertex.run(shader->ir);
281 clip_distance.run(shader->ir);
282 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
283 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
284 "and `gl_ClipDistance'\n");
285 return false;
286 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700287 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800288 ir_variable *clip_distance_var =
289 shader->symbols->get_variable("gl_ClipDistance");
290 if (clip_distance_var)
291 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700292 }
293
Ian Romanick832dfa52010-06-17 15:04:20 -0700294 return true;
295}
296
297
Ian Romanickc93b8f12010-06-17 15:20:22 -0700298/**
299 * Verify that a fragment shader executable meets all semantic requirements
300 *
301 * \param shader Fragment shader executable to be verified
302 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700303bool
Eric Anholt849e1812010-06-30 11:49:17 -0700304validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700305 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700306{
307 if (shader == NULL)
308 return true;
309
Ian Romanick832dfa52010-06-17 15:04:20 -0700310 find_assignment_visitor frag_color("gl_FragColor");
311 find_assignment_visitor frag_data("gl_FragData");
312
Eric Anholt16b68b12010-06-30 11:05:43 -0700313 frag_color.run(shader->ir);
314 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700315
Ian Romanick832dfa52010-06-17 15:04:20 -0700316 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700317 linker_error(prog, "fragment shader writes to both "
318 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700319 return false;
320 }
321
322 return true;
323}
324
325
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700326/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700327 * Generate a string describing the mode of a variable
328 */
329static const char *
330mode_string(const ir_variable *var)
331{
332 switch (var->mode) {
333 case ir_var_auto:
334 return (var->read_only) ? "global constant" : "global variable";
335
336 case ir_var_uniform: return "uniform";
337 case ir_var_in: return "shader input";
338 case ir_var_out: return "shader output";
339 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700340
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800341 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700342 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700343 default:
344 assert(!"Should not get here.");
345 return "invalid variable";
346 }
347}
348
349
350/**
351 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700352 */
353bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700354cross_validate_globals(struct gl_shader_program *prog,
355 struct gl_shader **shader_list,
356 unsigned num_shaders,
357 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700358{
359 /* Examine all of the uniforms in all of the shaders and cross validate
360 * them.
361 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700362 glsl_symbol_table variables;
363 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700364 if (shader_list[i] == NULL)
365 continue;
366
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700367 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700368 ir_variable *const var = ((ir_instruction *) node)->as_variable();
369
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700370 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700371 continue;
372
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700373 if (uniforms_only && (var->mode != ir_var_uniform))
374 continue;
375
Ian Romanick7e2aa912010-07-19 17:12:42 -0700376 /* Don't cross validate temporaries that are at global scope. These
377 * will eventually get pulled into the shaders 'main'.
378 */
379 if (var->mode == ir_var_temporary)
380 continue;
381
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700382 /* If a global with this name has already been seen, verify that the
383 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700384 * initializers, the values of the initializers must be the same.
385 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700386 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700387 if (existing != NULL) {
388 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700389 /* Consider the types to be "the same" if both types are arrays
390 * of the same type and one of the arrays is implicitly sized.
391 * In addition, set the type of the linked variable to the
392 * explicitly sized array.
393 */
394 if (var->type->is_array()
395 && existing->type->is_array()
396 && (var->type->fields.array == existing->type->fields.array)
397 && ((var->type->length == 0)
398 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800399 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700400 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800401 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700402 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700403 linker_error(prog, "%s `%s' declared as type "
404 "`%s' and type `%s'\n",
405 mode_string(var),
406 var->name, var->type->name,
407 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700408 return false;
409 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700410 }
411
Ian Romanick68a4fc92010-10-07 17:21:22 -0700412 if (var->explicit_location) {
413 if (existing->explicit_location
414 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700415 linker_error(prog, "explicit locations for %s "
416 "`%s' have differing values\n",
417 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700418 return false;
419 }
420
421 existing->location = var->location;
422 existing->explicit_location = true;
423 }
424
Ian Romanick46173f92011-10-31 13:07:06 -0700425 /* Validate layout qualifiers for gl_FragDepth.
426 *
427 * From the AMD/ARB_conservative_depth specs:
428 *
429 * "If gl_FragDepth is redeclared in any fragment shader in a
430 * program, it must be redeclared in all fragment shaders in
431 * that program that have static assignments to
432 * gl_FragDepth. All redeclarations of gl_FragDepth in all
433 * fragment shaders in a single program must have the same set
434 * of qualifiers."
435 */
436 if (strcmp(var->name, "gl_FragDepth") == 0) {
437 bool layout_declared = var->depth_layout != ir_depth_layout_none;
438 bool layout_differs =
439 var->depth_layout != existing->depth_layout;
440
441 if (layout_declared && layout_differs) {
442 linker_error(prog,
443 "All redeclarations of gl_FragDepth in all "
444 "fragment shaders in a single program must have "
445 "the same set of qualifiers.");
446 }
447
448 if (var->used && layout_differs) {
449 linker_error(prog,
450 "If gl_FragDepth is redeclared with a layout "
451 "qualifier in any fragment shader, it must be "
452 "redeclared with the same layout qualifier in "
453 "all fragment shaders that have assignments to "
454 "gl_FragDepth");
455 }
456 }
Chad Versaceaddae332011-01-27 01:40:31 -0800457
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700458 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
459 *
460 * "If a shared global has multiple initializers, the
461 * initializers must all be constant expressions, and they
462 * must all have the same value. Otherwise, a link error will
463 * result. (A shared global having only one initializer does
464 * not require that initializer to be a constant expression.)"
465 *
466 * Previous to 4.20 the GLSL spec simply said that initializers
467 * must have the same value. In this case of non-constant
468 * initializers, this was impossible to determine. As a result,
469 * no vendor actually implemented that behavior. The 4.20
470 * behavior matches the implemented behavior of at least one other
471 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700472 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700473 if (var->constant_initializer != NULL) {
474 if (existing->constant_initializer != NULL) {
475 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700476 linker_error(prog, "initializers for %s "
477 "`%s' have differing values\n",
478 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700479 return false;
480 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700481 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700482 /* If the first-seen instance of a particular uniform did not
483 * have an initializer but a later instance does, copy the
484 * initializer to the version stored in the symbol table.
485 */
Ian Romanickde415b72010-07-14 13:22:12 -0700486 /* FINISHME: This is wrong. The constant_value field should
487 * FINISHME: not be modified! Imagine a case where a shader
488 * FINISHME: without an initializer is linked in two different
489 * FINISHME: programs with shaders that have differing
490 * FINISHME: initializers. Linking with the first will
491 * FINISHME: modify the shader, and linking with the second
492 * FINISHME: will fail.
493 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700494 existing->constant_initializer =
495 var->constant_initializer->clone(ralloc_parent(existing),
496 NULL);
497 }
498 }
499
500 if (var->has_initializer) {
501 if (existing->has_initializer
502 && (var->constant_initializer == NULL
503 || existing->constant_initializer == NULL)) {
504 linker_error(prog,
505 "shared global variable `%s' has multiple "
506 "non-constant initializers.\n",
507 var->name);
508 return false;
509 }
510
511 /* Some instance had an initializer, so keep track of that. In
512 * this location, all sorts of initializers (constant or
513 * otherwise) will propagate the existence to the variable
514 * stored in the symbol table.
515 */
516 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700517 }
Chad Versace7528f142010-11-17 14:34:38 -0800518
519 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700520 linker_error(prog, "declarations for %s `%s' have "
521 "mismatching invariant qualifiers\n",
522 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800523 return false;
524 }
Chad Versace61428dd2011-01-10 15:29:30 -0800525 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700526 linker_error(prog, "declarations for %s `%s' have "
527 "mismatching centroid qualifiers\n",
528 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800529 return false;
530 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700531 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700532 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700533 }
534 }
535
536 return true;
537}
538
539
Ian Romanick37101922010-06-18 19:02:10 -0700540/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700541 * Perform validation of uniforms used across multiple shader stages
542 */
543bool
544cross_validate_uniforms(struct gl_shader_program *prog)
545{
546 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700547 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700548}
549
550
551/**
Ian Romanick37101922010-06-18 19:02:10 -0700552 * Validate that outputs from one stage match inputs of another
553 */
554bool
Eric Anholt849e1812010-06-30 11:49:17 -0700555cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700556 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700557{
558 glsl_symbol_table parameters;
559 /* FINISHME: Figure these out dynamically. */
560 const char *const producer_stage = "vertex";
561 const char *const consumer_stage = "fragment";
562
563 /* Find all shader outputs in the "producer" stage.
564 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700565 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700566 ir_variable *const var = ((ir_instruction *) node)->as_variable();
567
568 /* FINISHME: For geometry shaders, this should also look for inout
569 * FINISHME: variables.
570 */
571 if ((var == NULL) || (var->mode != ir_var_out))
572 continue;
573
Eric Anholt001eee52010-11-05 06:11:24 -0700574 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700575 }
576
577
578 /* Find all shader inputs in the "consumer" stage. Any variables that have
579 * matching outputs already in the symbol table must have the same type and
580 * qualifiers.
581 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700582 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700583 ir_variable *const input = ((ir_instruction *) node)->as_variable();
584
585 /* FINISHME: For geometry shaders, this should also look for inout
586 * FINISHME: variables.
587 */
588 if ((input == NULL) || (input->mode != ir_var_in))
589 continue;
590
591 ir_variable *const output = parameters.get_variable(input->name);
592 if (output != NULL) {
593 /* Check that the types match between stages.
594 */
595 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800596 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500597 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800598 * access it must redeclare it with a size. There is some
599 * language in the GLSL spec that implies the fragment shader
600 * and vertex shader do not have to agree on this size. Other
601 * driver behave this way, and one or two applications seem to
602 * rely on it.
603 *
604 * Neither declaration needs to be modified here because the array
605 * sizes are fixed later when update_array_sizes is called.
606 *
607 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
608 *
609 * "Unlike user-defined varying variables, the built-in
610 * varying variables don't have a strict one-to-one
611 * correspondence between the vertex language and the
612 * fragment language."
613 */
614 if (!output->type->is_array()
615 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700616 linker_error(prog,
617 "%s shader output `%s' declared as type `%s', "
618 "but %s shader input declared as type `%s'\n",
619 producer_stage, output->name,
620 output->type->name,
621 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800622 return false;
623 }
Ian Romanick37101922010-06-18 19:02:10 -0700624 }
625
626 /* Check that all of the qualifiers match between stages.
627 */
628 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700629 linker_error(prog,
630 "%s shader output `%s' %s centroid qualifier, "
631 "but %s shader input %s centroid qualifier\n",
632 producer_stage,
633 output->name,
634 (output->centroid) ? "has" : "lacks",
635 consumer_stage,
636 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700637 return false;
638 }
639
640 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700641 linker_error(prog,
642 "%s shader output `%s' %s invariant qualifier, "
643 "but %s shader input %s invariant qualifier\n",
644 producer_stage,
645 output->name,
646 (output->invariant) ? "has" : "lacks",
647 consumer_stage,
648 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700649 return false;
650 }
651
652 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700653 linker_error(prog,
654 "%s shader output `%s' specifies %s "
655 "interpolation qualifier, "
656 "but %s shader input specifies %s "
657 "interpolation qualifier\n",
658 producer_stage,
659 output->name,
660 output->interpolation_string(),
661 consumer_stage,
662 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700663 return false;
664 }
665 }
666 }
667
668 return true;
669}
670
671
Ian Romanick3fb87872010-07-09 14:09:34 -0700672/**
673 * Populates a shaders symbol table with all global declarations
674 */
675static void
676populate_symbol_table(gl_shader *sh)
677{
678 sh->symbols = new(sh) glsl_symbol_table;
679
680 foreach_list(node, sh->ir) {
681 ir_instruction *const inst = (ir_instruction *) node;
682 ir_variable *var;
683 ir_function *func;
684
685 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700686 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700687 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700688 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700689 }
690 }
691}
692
693
694/**
Ian Romanick31a97862010-07-12 18:48:50 -0700695 * Remap variables referenced in an instruction tree
696 *
697 * This is used when instruction trees are cloned from one shader and placed in
698 * another. These trees will contain references to \c ir_variable nodes that
699 * do not exist in the target shader. This function finds these \c ir_variable
700 * references and replaces the references with matching variables in the target
701 * shader.
702 *
703 * If there is no matching variable in the target shader, a clone of the
704 * \c ir_variable is made and added to the target shader. The new variable is
705 * added to \b both the instruction stream and the symbol table.
706 *
707 * \param inst IR tree that is to be processed.
708 * \param symbols Symbol table containing global scope symbols in the
709 * linked shader.
710 * \param instructions Instruction stream where new variable declarations
711 * should be added.
712 */
713void
Eric Anholt8273bd42010-08-04 12:34:56 -0700714remap_variables(ir_instruction *inst, struct gl_shader *target,
715 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700716{
717 class remap_visitor : public ir_hierarchical_visitor {
718 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700719 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700720 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700721 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700722 this->target = target;
723 this->symbols = target->symbols;
724 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700725 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700726 }
727
728 virtual ir_visitor_status visit(ir_dereference_variable *ir)
729 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700730 if (ir->var->mode == ir_var_temporary) {
731 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
732
733 assert(var != NULL);
734 ir->var = var;
735 return visit_continue;
736 }
737
Ian Romanick31a97862010-07-12 18:48:50 -0700738 ir_variable *const existing =
739 this->symbols->get_variable(ir->var->name);
740 if (existing != NULL)
741 ir->var = existing;
742 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700743 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700744
Eric Anholt001eee52010-11-05 06:11:24 -0700745 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700746 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700747 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700748 }
749
750 return visit_continue;
751 }
752
753 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700754 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700755 glsl_symbol_table *symbols;
756 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700757 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700758 };
759
Eric Anholt8273bd42010-08-04 12:34:56 -0700760 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700761
762 inst->accept(&v);
763}
764
765
766/**
767 * Move non-declarations from one instruction stream to another
768 *
769 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700770 * 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 -0700771 * pointer) for \c last and \c false for \c make_copies on the first
772 * call. Successive calls pass the return value of the previous call for
773 * \c last and \c true for \c make_copies.
774 *
775 * \param instructions Source instruction stream
776 * \param last Instruction after which new instructions should be
777 * inserted in the target instruction stream
778 * \param make_copies Flag selecting whether instructions in \c instructions
779 * should be copied (via \c ir_instruction::clone) into the
780 * target list or moved.
781 *
782 * \return
783 * The new "last" instruction in the target instruction stream. This pointer
784 * is suitable for use as the \c last parameter of a later call to this
785 * function.
786 */
787exec_node *
788move_non_declarations(exec_list *instructions, exec_node *last,
789 bool make_copies, gl_shader *target)
790{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700791 hash_table *temps = NULL;
792
793 if (make_copies)
794 temps = hash_table_ctor(0, hash_table_pointer_hash,
795 hash_table_pointer_compare);
796
Ian Romanick303c99f2010-07-19 12:34:56 -0700797 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700798 ir_instruction *inst = (ir_instruction *) node;
799
Ian Romanick7e2aa912010-07-19 17:12:42 -0700800 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700801 continue;
802
Ian Romanick7e2aa912010-07-19 17:12:42 -0700803 ir_variable *var = inst->as_variable();
804 if ((var != NULL) && (var->mode != ir_var_temporary))
805 continue;
806
807 assert(inst->as_assignment()
808 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700809
810 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700811 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700812
813 if (var != NULL)
814 hash_table_insert(temps, inst, var);
815 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700816 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700817 } else {
818 inst->remove();
819 }
820
821 last->insert_after(inst);
822 last = inst;
823 }
824
Ian Romanick7e2aa912010-07-19 17:12:42 -0700825 if (make_copies)
826 hash_table_dtor(temps);
827
Ian Romanick31a97862010-07-12 18:48:50 -0700828 return last;
829}
830
831/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700832 * Get the function signature for main from a shader
833 */
834static ir_function_signature *
835get_main_function_signature(gl_shader *sh)
836{
837 ir_function *const f = sh->symbols->get_function("main");
838 if (f != NULL) {
839 exec_list void_parameters;
840
841 /* Look for the 'void main()' signature and ensure that it's defined.
842 * This keeps the linker from accidentally pick a shader that just
843 * contains a prototype for main.
844 *
845 * We don't have to check for multiple definitions of main (in multiple
846 * shaders) because that would have already been caught above.
847 */
848 ir_function_signature *sig = f->matching_signature(&void_parameters);
849 if ((sig != NULL) && sig->is_defined) {
850 return sig;
851 }
852 }
853
854 return NULL;
855}
856
857
858/**
Brian Paul84a12732012-02-02 20:10:40 -0700859 * This class is only used in link_intrastage_shaders() below but declaring
860 * it inside that function leads to compiler warnings with some versions of
861 * gcc.
862 */
863class array_sizing_visitor : public ir_hierarchical_visitor {
864public:
865 virtual ir_visitor_status visit(ir_variable *var)
866 {
867 if (var->type->is_array() && (var->type->length == 0)) {
868 const glsl_type *type =
869 glsl_type::get_array_instance(var->type->fields.array,
870 var->max_array_access + 1);
871 assert(type != NULL);
872 var->type = type;
873 }
874 return visit_continue;
875 }
876};
877
878
879/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700880 * Combine a group of shaders for a single stage to generate a linked shader
881 *
882 * \note
883 * If this function is supplied a single shader, it is cloned, and the new
884 * shader is returned.
885 */
886static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800887link_intrastage_shaders(void *mem_ctx,
888 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700889 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700890 struct gl_shader **shader_list,
891 unsigned num_shaders)
892{
Ian Romanick13f782c2010-06-29 18:53:38 -0700893 /* Check that global variables defined in multiple shaders are consistent.
894 */
895 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
896 return NULL;
897
898 /* Check that there is only a single definition of each function signature
899 * across all shaders.
900 */
901 for (unsigned i = 0; i < (num_shaders - 1); i++) {
902 foreach_list(node, shader_list[i]->ir) {
903 ir_function *const f = ((ir_instruction *) node)->as_function();
904
905 if (f == NULL)
906 continue;
907
908 for (unsigned j = i + 1; j < num_shaders; j++) {
909 ir_function *const other =
910 shader_list[j]->symbols->get_function(f->name);
911
912 /* If the other shader has no function (and therefore no function
913 * signatures) with the same name, skip to the next shader.
914 */
915 if (other == NULL)
916 continue;
917
918 foreach_iter (exec_list_iterator, iter, *f) {
919 ir_function_signature *sig =
920 (ir_function_signature *) iter.get();
921
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700922 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700923 continue;
924
925 ir_function_signature *other_sig =
926 other->exact_matching_signature(& sig->parameters);
927
928 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700929 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -0700930 linker_error(prog, "function `%s' is multiply defined",
931 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -0700932 return NULL;
933 }
934 }
935 }
936 }
937 }
938
939 /* Find the shader that defines main, and make a clone of it.
940 *
941 * Starting with the clone, search for undefined references. If one is
942 * found, find the shader that defines it. Clone the reference and add
943 * it to the shader. Repeat until there are no undefined references or
944 * until a reference cannot be resolved.
945 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700946 gl_shader *main = NULL;
947 for (unsigned i = 0; i < num_shaders; i++) {
948 if (get_main_function_signature(shader_list[i]) != NULL) {
949 main = shader_list[i];
950 break;
951 }
952 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700953
Ian Romanick15ce87e2010-07-09 15:28:22 -0700954 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -0700955 linker_error(prog, "%s shader lacks `main'\n",
956 (shader_list[0]->Type == GL_VERTEX_SHADER)
957 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -0700958 return NULL;
959 }
960
Ian Romanick4a455952010-10-13 15:13:02 -0700961 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700962 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800963 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700964
965 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -0700966
Ian Romanick31a97862010-07-12 18:48:50 -0700967 /* The a pointer to the main function in the final linked shader (i.e., the
968 * copy of the original shader that contained the main function).
969 */
970 ir_function_signature *const main_sig = get_main_function_signature(linked);
971
972 /* Move any instructions other than variable declarations or function
973 * declarations into main.
974 */
Ian Romanick9303e352010-07-19 12:33:54 -0700975 exec_node *insertion_point =
976 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
977 linked);
978
Ian Romanick31a97862010-07-12 18:48:50 -0700979 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -0700980 if (shader_list[i] == main)
981 continue;
982
Ian Romanick31a97862010-07-12 18:48:50 -0700983 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -0700984 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -0700985 }
986
Ian Romanick13f782c2010-06-29 18:53:38 -0700987 /* Resolve initializers for global variables in the linked shader.
988 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700989 unsigned num_linking_shaders = num_shaders;
990 for (unsigned i = 0; i < num_shaders; i++)
991 num_linking_shaders += shader_list[i]->num_builtins_to_link;
992
993 gl_shader **linking_shaders =
994 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
995
996 memcpy(linking_shaders, shader_list,
997 sizeof(linking_shaders[0]) * num_shaders);
998
999 unsigned idx = num_shaders;
1000 for (unsigned i = 0; i < num_shaders; i++) {
1001 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1002 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1003 idx += shader_list[i]->num_builtins_to_link;
1004 }
1005
1006 assert(idx == num_linking_shaders);
1007
Ian Romanick4a455952010-10-13 15:13:02 -07001008 if (!link_function_calls(prog, linked, linking_shaders,
1009 num_linking_shaders)) {
1010 ctx->Driver.DeleteShader(ctx, linked);
1011 linked = NULL;
1012 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001013
1014 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001015
Paul Berryc148ef62011-08-03 15:37:01 -07001016#ifdef DEBUG
1017 /* At this point linked should contain all of the linked IR, so
1018 * validate it to make sure nothing went wrong.
1019 */
1020 if (linked)
1021 validate_ir_tree(linked->ir);
1022#endif
1023
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001024 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001025 * unspecified sizes have a size specified. The size is inferred from the
1026 * max_array_access field.
1027 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001028 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001029 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001030
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001031 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001032 }
1033
Ian Romanick3fb87872010-07-09 14:09:34 -07001034 return linked;
1035}
1036
Eric Anholta721abf2010-08-23 10:32:01 -07001037/**
1038 * Update the sizes of linked shader uniform arrays to the maximum
1039 * array index used.
1040 *
1041 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1042 *
1043 * If one or more elements of an array are active,
1044 * GetActiveUniform will return the name of the array in name,
1045 * subject to the restrictions listed above. The type of the array
1046 * is returned in type. The size parameter contains the highest
1047 * array element index used, plus one. The compiler or linker
1048 * determines the highest index used. There will be only one
1049 * active uniform reported by the GL per uniform array.
1050
1051 */
1052static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001053update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001054{
Ian Romanick3322fba2010-10-14 13:28:42 -07001055 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1056 if (prog->_LinkedShaders[i] == NULL)
1057 continue;
1058
Eric Anholta721abf2010-08-23 10:32:01 -07001059 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1060 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1061
Eric Anholt586b4b52010-09-28 14:32:16 -07001062 if ((var == NULL) || (var->mode != ir_var_uniform &&
1063 var->mode != ir_var_in &&
1064 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001065 !var->type->is_array())
1066 continue;
1067
1068 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001069 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1070 if (prog->_LinkedShaders[j] == NULL)
1071 continue;
1072
Eric Anholta721abf2010-08-23 10:32:01 -07001073 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1074 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1075 if (!other_var)
1076 continue;
1077
1078 if (strcmp(var->name, other_var->name) == 0 &&
1079 other_var->max_array_access > size) {
1080 size = other_var->max_array_access;
1081 }
1082 }
1083 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001084
Eric Anholta721abf2010-08-23 10:32:01 -07001085 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001086 /* If this is a built-in uniform (i.e., it's backed by some
1087 * fixed-function state), adjust the number of state slots to
1088 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001089 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001090 * slots is an integer multiple of the number of array elements.
1091 * Determine the number of slots per array element by dividing by
1092 * the old (total) size.
1093 */
1094 if (var->num_state_slots > 0) {
1095 var->num_state_slots = (size + 1)
1096 * (var->num_state_slots / var->type->length);
1097 }
1098
Eric Anholta721abf2010-08-23 10:32:01 -07001099 var->type = glsl_type::get_array_instance(var->type->fields.array,
1100 size + 1);
1101 /* FINISHME: We should update the types of array
1102 * dereferences of this variable now.
1103 */
1104 }
1105 }
1106 }
1107}
1108
Ian Romanick69846702010-06-22 17:29:19 -07001109/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001110 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001111 *
1112 * \param used_mask Bits representing used (1) and unused (0) locations
1113 * \param needed_count Number of contiguous bits needed.
1114 *
1115 * \return
1116 * Base location of the available bits on success or -1 on failure.
1117 */
1118int
1119find_available_slots(unsigned used_mask, unsigned needed_count)
1120{
1121 unsigned needed_mask = (1 << needed_count) - 1;
1122 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1123
1124 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1125 * cannot optimize possibly infinite loops" for the loop below.
1126 */
1127 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1128 return -1;
1129
1130 for (int i = 0; i <= max_bit_to_test; i++) {
1131 if ((needed_mask & ~used_mask) == needed_mask)
1132 return i;
1133
1134 needed_mask <<= 1;
1135 }
1136
1137 return -1;
1138}
1139
1140
Ian Romanickd32d4f72011-06-27 17:59:58 -07001141/**
1142 * Assign locations for either VS inputs for FS outputs
1143 *
1144 * \param prog Shader program whose variables need locations assigned
1145 * \param target_index Selector for the program target to receive location
1146 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1147 * \c MESA_SHADER_FRAGMENT.
1148 * \param max_index Maximum number of generic locations. This corresponds
1149 * to either the maximum number of draw buffers or the
1150 * maximum number of generic attributes.
1151 *
1152 * \return
1153 * If locations are successfully assigned, true is returned. Otherwise an
1154 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001155 */
Ian Romanick69846702010-06-22 17:29:19 -07001156bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001157assign_attribute_or_color_locations(gl_shader_program *prog,
1158 unsigned target_index,
1159 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001160{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001161 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001162 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001163 unsigned used_locations = (max_index >= 32)
1164 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001165
Ian Romanickd32d4f72011-06-27 17:59:58 -07001166 assert((target_index == MESA_SHADER_VERTEX)
1167 || (target_index == MESA_SHADER_FRAGMENT));
1168
1169 gl_shader *const sh = prog->_LinkedShaders[target_index];
1170 if (sh == NULL)
1171 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001172
Ian Romanick69846702010-06-22 17:29:19 -07001173 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001174 *
1175 * 1. Invalidate the location assignments for all vertex shader inputs.
1176 *
1177 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001178 * glBindVertexAttribLocation) locations and outputs that have
1179 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001180 *
Ian Romanick69846702010-06-22 17:29:19 -07001181 * 3. Sort the attributes without assigned locations by number of slots
1182 * required in decreasing order. Fragmentation caused by attribute
1183 * locations assigned by the application may prevent large attributes
1184 * from having enough contiguous space.
1185 *
1186 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001187 */
1188
Ian Romanickd32d4f72011-06-27 17:59:58 -07001189 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001190 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001191
Ian Romanickd32d4f72011-06-27 17:59:58 -07001192 const enum ir_variable_mode direction =
1193 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1194
1195
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07001196 link_invalidate_variable_locations(sh, direction, generic_base);
Ian Romanickd32d4f72011-06-27 17:59:58 -07001197
Ian Romanick69846702010-06-22 17:29:19 -07001198 /* Temporary storage for the set of attributes that need locations assigned.
1199 */
1200 struct temp_attr {
1201 unsigned slots;
1202 ir_variable *var;
1203
1204 /* Used below in the call to qsort. */
1205 static int compare(const void *a, const void *b)
1206 {
1207 const temp_attr *const l = (const temp_attr *) a;
1208 const temp_attr *const r = (const temp_attr *) b;
1209
1210 /* Reversed because we want a descending order sort below. */
1211 return r->slots - l->slots;
1212 }
1213 } to_assign[16];
1214
1215 unsigned num_attr = 0;
1216
Eric Anholt16b68b12010-06-30 11:05:43 -07001217 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001218 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1219
Brian Paul4470ff22011-07-19 21:10:25 -06001220 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001221 continue;
1222
Ian Romanick68a4fc92010-10-07 17:21:22 -07001223 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001224 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001225 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001226 linker_error(prog,
1227 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001228 (var->location < 0)
1229 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001230 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001231 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001232 }
1233 } else if (target_index == MESA_SHADER_VERTEX) {
1234 unsigned binding;
1235
1236 if (prog->AttributeBindings->get(binding, var->name)) {
1237 assert(binding >= VERT_ATTRIB_GENERIC0);
1238 var->location = binding;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001239 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001240 } else if (target_index == MESA_SHADER_FRAGMENT) {
1241 unsigned binding;
1242
1243 if (prog->FragDataBindings->get(binding, var->name)) {
1244 assert(binding >= FRAG_RESULT_DATA0);
1245 var->location = binding;
1246 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001247 }
1248
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001249 /* If the variable is not a built-in and has a location statically
1250 * assigned in the shader (presumably via a layout qualifier), make sure
1251 * that it doesn't collide with other assigned locations. Otherwise,
1252 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001253 */
Ian Romanick523b6112011-08-17 15:40:03 -07001254 const unsigned slots = count_attribute_slots(var->type);
1255 if (var->location != -1) {
1256 if (var->location >= generic_base) {
1257 /* From page 61 of the OpenGL 4.0 spec:
1258 *
1259 * "LinkProgram will fail if the attribute bindings assigned
1260 * by BindAttribLocation do not leave not enough space to
1261 * assign a location for an active matrix attribute or an
1262 * active attribute array, both of which require multiple
1263 * contiguous generic attributes."
1264 *
1265 * Previous versions of the spec contain similar language but omit
1266 * the bit about attribute arrays.
1267 *
1268 * Page 61 of the OpenGL 4.0 spec also says:
1269 *
1270 * "It is possible for an application to bind more than one
1271 * attribute name to the same location. This is referred to as
1272 * aliasing. This will only work if only one of the aliased
1273 * attributes is active in the executable program, or if no
1274 * path through the shader consumes more than one attribute of
1275 * a set of attributes aliased to the same location. A link
1276 * error can occur if the linker determines that every path
1277 * through the shader consumes multiple aliased attributes,
1278 * but implementations are not required to generate an error
1279 * in this case."
1280 *
1281 * These two paragraphs are either somewhat contradictory, or I
1282 * don't fully understand one or both of them.
1283 */
1284 /* FINISHME: The code as currently written does not support
1285 * FINISHME: attribute location aliasing (see comment above).
1286 */
1287 /* Mask representing the contiguous slots that will be used by
1288 * this attribute.
1289 */
1290 const unsigned attr = var->location - generic_base;
1291 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001292
Ian Romanick523b6112011-08-17 15:40:03 -07001293 /* Generate a link error if the set of bits requested for this
1294 * attribute overlaps any previously allocated bits.
1295 */
1296 if ((~(use_mask << attr) & used_locations) != used_locations) {
1297 linker_error(prog,
1298 "insufficient contiguous attribute locations "
1299 "available for vertex shader input `%s'",
1300 var->name);
1301 return false;
1302 }
1303
1304 used_locations |= (use_mask << attr);
1305 }
1306
1307 continue;
1308 }
1309
1310 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001311 to_assign[num_attr].var = var;
1312 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001313 }
Ian Romanick69846702010-06-22 17:29:19 -07001314
1315 /* If all of the attributes were assigned locations by the application (or
1316 * are built-in attributes with fixed locations), return early. This should
1317 * be the common case.
1318 */
1319 if (num_attr == 0)
1320 return true;
1321
1322 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1323
Ian Romanickd32d4f72011-06-27 17:59:58 -07001324 if (target_index == MESA_SHADER_VERTEX) {
1325 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1326 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1327 * reserved to prevent it from being automatically allocated below.
1328 */
1329 find_deref_visitor find("gl_Vertex");
1330 find.run(sh->ir);
1331 if (find.variable_found())
1332 used_locations |= (1 << 0);
1333 }
Ian Romanick982e3792010-06-29 18:58:20 -07001334
Ian Romanick69846702010-06-22 17:29:19 -07001335 for (unsigned i = 0; i < num_attr; i++) {
1336 /* Mask representing the contiguous slots that will be used by this
1337 * attribute.
1338 */
1339 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1340
1341 int location = find_available_slots(used_locations, to_assign[i].slots);
1342
1343 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001344 const char *const string = (target_index == MESA_SHADER_VERTEX)
1345 ? "vertex shader input" : "fragment shader output";
1346
Ian Romanick586e7412011-07-28 14:04:09 -07001347 linker_error(prog,
1348 "insufficient contiguous attribute locations "
1349 "available for %s `%s'",
1350 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001351 return false;
1352 }
1353
Ian Romanickd32d4f72011-06-27 17:59:58 -07001354 to_assign[i].var->location = generic_base + location;
Ian Romanick69846702010-06-22 17:29:19 -07001355 used_locations |= (use_mask << location);
1356 }
1357
1358 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001359}
1360
1361
Ian Romanick40e114b2010-08-17 14:55:50 -07001362/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001363 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001364 */
1365void
Ian Romanickcc90e622010-10-19 17:59:10 -07001366demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001367{
1368 foreach_list(node, sh->ir) {
1369 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1370
Ian Romanickcc90e622010-10-19 17:59:10 -07001371 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001372 continue;
1373
Ian Romanickcc90e622010-10-19 17:59:10 -07001374 /* A shader 'in' or 'out' variable is only really an input or output if
1375 * its value is used by other shader stages. This will cause the variable
1376 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001377 */
1378 if (var->location == -1) {
1379 var->mode = ir_var_auto;
1380 }
1381 }
1382}
1383
1384
Paul Berry871ddb92011-11-05 11:17:32 -07001385/**
1386 * Data structure tracking information about a transform feedback declaration
1387 * during linking.
1388 */
1389class tfeedback_decl
1390{
1391public:
Paul Berry456279b2011-12-26 19:39:25 -08001392 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1393 const void *mem_ctx, const char *input);
Paul Berry871ddb92011-11-05 11:17:32 -07001394 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1395 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1396 ir_variable *output_var);
Christoph Bumillerd540af52012-01-20 13:24:46 +01001397 bool accumulate_num_outputs(struct gl_shader_program *prog, unsigned *count);
Paul Berryd3150eb2012-01-09 11:25:14 -08001398 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001399 struct gl_transform_feedback_info *info, unsigned buffer,
Christoph Bumillerd540af52012-01-20 13:24:46 +01001400 unsigned varying, const unsigned max_outputs) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001401
1402
1403 /**
1404 * True if assign_location() has been called for this object.
1405 */
1406 bool is_assigned() const
1407 {
1408 return this->location != -1;
1409 }
1410
1411 /**
1412 * Determine whether this object refers to the variable var.
1413 */
1414 bool matches_var(ir_variable *var) const
1415 {
Paul Berry642e5b412012-01-04 13:57:52 -08001416 if (this->is_clip_distance_mesa)
1417 return strcmp(var->name, "gl_ClipDistanceMESA") == 0;
1418 else
1419 return strcmp(var->name, this->var_name) == 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001420 }
1421
1422 /**
1423 * The total number of varying components taken up by this variable. Only
1424 * valid if is_assigned() is true.
1425 */
1426 unsigned num_components() const
1427 {
Paul Berry642e5b412012-01-04 13:57:52 -08001428 if (this->is_clip_distance_mesa)
1429 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001430 else
Paul Berry642e5b412012-01-04 13:57:52 -08001431 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001432 }
1433
1434private:
1435 /**
1436 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001437 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001438 */
1439 const char *orig_name;
1440
1441 /**
1442 * The name of the variable, parsed from orig_name.
1443 */
Paul Berry913a5c22011-12-27 08:24:57 -08001444 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001445
1446 /**
1447 * True if the declaration in orig_name represents an array.
1448 */
Paul Berry33fe0212012-01-03 20:41:34 -08001449 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001450
1451 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001452 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001453 */
Paul Berry33fe0212012-01-03 20:41:34 -08001454 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001455
1456 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001457 * True if the variable is gl_ClipDistance and the driver lowers
1458 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001459 */
Paul Berry642e5b412012-01-04 13:57:52 -08001460 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001461
1462 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001463 * The vertex shader output location that the linker assigned for this
1464 * variable. -1 if a location hasn't been assigned yet.
1465 */
1466 int location;
1467
1468 /**
1469 * If location != -1, the number of vector elements in this variable, or 1
1470 * if this variable is a scalar.
1471 */
1472 unsigned vector_elements;
1473
1474 /**
1475 * If location != -1, the number of matrix columns in this variable, or 1
1476 * if this variable is not a matrix.
1477 */
1478 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001479
1480 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1481 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001482
1483 /**
1484 * If location != -1, the size that should be returned by
1485 * glGetTransformFeedbackVarying().
1486 */
1487 unsigned size;
Paul Berry871ddb92011-11-05 11:17:32 -07001488};
1489
1490
1491/**
1492 * Initialize this object based on a string that was passed to
1493 * glTransformFeedbackVaryings. If there is a parse error, the error is
1494 * reported using linker_error(), and false is returned.
1495 */
1496bool
Paul Berry456279b2011-12-26 19:39:25 -08001497tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1498 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001499{
1500 /* We don't have to be pedantic about what is a valid GLSL variable name,
1501 * because any variable with an invalid name can't exist in the IR anyway.
1502 */
1503
1504 this->location = -1;
1505 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001506 this->is_clip_distance_mesa = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001507
1508 const char *bracket = strrchr(input, '[');
1509
1510 if (bracket) {
1511 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001512 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001513 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1514 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001515 }
Paul Berry33fe0212012-01-03 20:41:34 -08001516 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001517 } else {
1518 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001519 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001520 }
1521
Paul Berry642e5b412012-01-04 13:57:52 -08001522 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1523 * class must behave specially to account for the fact that gl_ClipDistance
1524 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001525 */
1526 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1527 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001528 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001529 }
1530
1531 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001532}
1533
1534
1535/**
1536 * Determine whether two tfeedback_decl objects refer to the same variable and
1537 * array index (if applicable).
1538 */
1539bool
1540tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1541{
1542 if (strcmp(x.var_name, y.var_name) != 0)
1543 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001544 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001545 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001546 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001547 return false;
1548 return true;
1549}
1550
1551
1552/**
1553 * Assign a location for this tfeedback_decl object based on the location
1554 * assignment in output_var.
1555 *
1556 * If an error occurs, the error is reported through linker_error() and false
1557 * is returned.
1558 */
1559bool
1560tfeedback_decl::assign_location(struct gl_context *ctx,
1561 struct gl_shader_program *prog,
1562 ir_variable *output_var)
1563{
1564 if (output_var->type->is_array()) {
1565 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001566 const unsigned matrix_cols =
1567 output_var->type->fields.array->matrix_columns;
Paul Berry642e5b412012-01-04 13:57:52 -08001568 unsigned actual_array_size = this->is_clip_distance_mesa ?
1569 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001570
1571 if (this->is_subscripted) {
1572 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001573 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001574 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001575 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001576 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001577 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001578 return false;
1579 }
Paul Berry642e5b412012-01-04 13:57:52 -08001580 if (this->is_clip_distance_mesa) {
1581 this->location =
1582 output_var->location + this->array_subscript / 4;
1583 } else {
1584 this->location =
1585 output_var->location + this->array_subscript * matrix_cols;
1586 }
Paul Berry33fe0212012-01-03 20:41:34 -08001587 this->size = 1;
1588 } else {
1589 this->location = output_var->location;
Paul Berry642e5b412012-01-04 13:57:52 -08001590 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001591 }
Paul Berry871ddb92011-11-05 11:17:32 -07001592 this->vector_elements = output_var->type->fields.array->vector_elements;
1593 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001594 if (this->is_clip_distance_mesa)
1595 this->type = GL_FLOAT;
1596 else
1597 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001598 } else {
1599 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001600 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001601 linker_error(prog, "Transform feedback varying %s requested, "
1602 "but %s is not an array.",
1603 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001604 return false;
1605 }
1606 this->location = output_var->location;
Paul Berry33fe0212012-01-03 20:41:34 -08001607 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001608 this->vector_elements = output_var->type->vector_elements;
1609 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001610 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001611 }
Paul Berry642e5b412012-01-04 13:57:52 -08001612
Paul Berry871ddb92011-11-05 11:17:32 -07001613 /* From GL_EXT_transform_feedback:
1614 * A program will fail to link if:
1615 *
1616 * * the total number of components to capture in any varying
1617 * variable in <varyings> is greater than the constant
1618 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1619 * buffer mode is SEPARATE_ATTRIBS_EXT;
1620 */
1621 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1622 this->num_components() >
1623 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1624 linker_error(prog, "Transform feedback varying %s exceeds "
1625 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1626 this->orig_name);
1627 return false;
1628 }
1629
1630 return true;
1631}
1632
1633
Paul Berry871ddb92011-11-05 11:17:32 -07001634bool
Christoph Bumillerd540af52012-01-20 13:24:46 +01001635tfeedback_decl::accumulate_num_outputs(struct gl_shader_program *prog,
1636 unsigned *count)
Paul Berry871ddb92011-11-05 11:17:32 -07001637{
1638 if (!this->is_assigned()) {
1639 /* From GL_EXT_transform_feedback:
1640 * A program will fail to link if:
1641 *
1642 * * any variable name specified in the <varyings> array is not
1643 * declared as an output in the geometry shader (if present) or
1644 * the vertex shader (if no geometry shader is present);
1645 */
1646 linker_error(prog, "Transform feedback varying %s undeclared.",
1647 this->orig_name);
1648 return false;
1649 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001650
Christoph Bumillerd540af52012-01-20 13:24:46 +01001651 unsigned translated_size = this->size;
1652 if (this->is_clip_distance_mesa)
1653 translated_size = (translated_size + 3) / 4;
1654
1655 *count += translated_size * this->matrix_columns;
1656
1657 return true;
1658}
1659
1660
1661/**
1662 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1663 *
1664 * If an error occurs, the error is reported through linker_error() and false
1665 * is returned.
1666 */
1667bool
1668tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1669 struct gl_transform_feedback_info *info,
1670 unsigned buffer,
1671 unsigned varying, const unsigned max_outputs) const
1672{
Paul Berryd3150eb2012-01-09 11:25:14 -08001673 /* From GL_EXT_transform_feedback:
1674 * A program will fail to link if:
1675 *
1676 * * the total number of components to capture is greater than
1677 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1678 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1679 */
1680 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1681 info->BufferStride[buffer] + this->num_components() >
1682 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1683 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1684 "limit has been exceeded.");
1685 return false;
1686 }
1687
Paul Berry642e5b412012-01-04 13:57:52 -08001688 unsigned translated_size = this->size;
1689 if (this->is_clip_distance_mesa)
1690 translated_size = (translated_size + 3) / 4;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001691 unsigned components_so_far = 0;
Paul Berry642e5b412012-01-04 13:57:52 -08001692 for (unsigned index = 0; index < translated_size; ++index) {
Paul Berry33fe0212012-01-03 20:41:34 -08001693 for (unsigned v = 0; v < this->matrix_columns; ++v) {
Paul Berry642e5b412012-01-04 13:57:52 -08001694 unsigned num_components = this->vector_elements;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001695 assert(info->NumOutputs < max_outputs);
Paul Berry642e5b412012-01-04 13:57:52 -08001696 info->Outputs[info->NumOutputs].ComponentOffset = 0;
1697 if (this->is_clip_distance_mesa) {
1698 if (this->is_subscripted) {
1699 num_components = 1;
1700 info->Outputs[info->NumOutputs].ComponentOffset =
1701 this->array_subscript % 4;
1702 } else {
1703 num_components = MIN2(4, this->size - components_so_far);
1704 }
1705 }
Paul Berry33fe0212012-01-03 20:41:34 -08001706 info->Outputs[info->NumOutputs].OutputRegister =
1707 this->location + v + index * this->matrix_columns;
1708 info->Outputs[info->NumOutputs].NumComponents = num_components;
1709 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1710 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
Paul Berry33fe0212012-01-03 20:41:34 -08001711 ++info->NumOutputs;
1712 info->BufferStride[buffer] += num_components;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001713 components_so_far += num_components;
Paul Berry33fe0212012-01-03 20:41:34 -08001714 }
Paul Berry871ddb92011-11-05 11:17:32 -07001715 }
Paul Berrybe4e9f72012-01-04 12:21:55 -08001716 assert(components_so_far == this->num_components());
Eric Anholt9d36c962012-01-02 17:08:13 -08001717
1718 info->Varyings[varying].Name = ralloc_strdup(prog, this->orig_name);
1719 info->Varyings[varying].Type = this->type;
Paul Berry33fe0212012-01-03 20:41:34 -08001720 info->Varyings[varying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001721 info->NumVarying++;
1722
Paul Berry871ddb92011-11-05 11:17:32 -07001723 return true;
1724}
1725
1726
1727/**
1728 * Parse all the transform feedback declarations that were passed to
1729 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1730 *
1731 * If an error occurs, the error is reported through linker_error() and false
1732 * is returned.
1733 */
1734static bool
Paul Berry456279b2011-12-26 19:39:25 -08001735parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1736 const void *mem_ctx, unsigned num_names,
1737 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001738{
1739 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001740 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001741 return false;
1742 /* From GL_EXT_transform_feedback:
1743 * A program will fail to link if:
1744 *
1745 * * any two entries in the <varyings> array specify the same varying
1746 * variable;
1747 *
1748 * We interpret this to mean "any two entries in the <varyings> array
1749 * specify the same varying variable and array index", since transform
1750 * feedback of arrays would be useless otherwise.
1751 */
1752 for (unsigned j = 0; j < i; ++j) {
1753 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1754 linker_error(prog, "Transform feedback varying %s specified "
1755 "more than once.", varying_names[i]);
1756 return false;
1757 }
1758 }
1759 }
1760 return true;
1761}
1762
1763
1764/**
1765 * Assign a location for a variable that is produced in one pipeline stage
1766 * (the "producer") and consumed in the next stage (the "consumer").
1767 *
1768 * \param input_var is the input variable declaration in the consumer.
1769 *
1770 * \param output_var is the output variable declaration in the producer.
1771 *
1772 * \param input_index is the counter that keeps track of assigned input
1773 * locations in the consumer.
1774 *
1775 * \param output_index is the counter that keeps track of assigned output
1776 * locations in the producer.
1777 *
1778 * It is permissible for \c input_var to be NULL (this happens if a variable
1779 * is output by the producer and consumed by transform feedback, but not
1780 * consumed by the consumer).
1781 *
1782 * If the variable has already been assigned a location, this function has no
1783 * effect.
1784 */
1785void
1786assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1787 unsigned *input_index, unsigned *output_index)
1788{
1789 if (output_var->location != -1) {
1790 /* Location already assigned. */
1791 return;
1792 }
1793
1794 if (input_var) {
1795 assert(input_var->location == -1);
1796 input_var->location = *input_index;
1797 }
1798
1799 output_var->location = *output_index;
1800
1801 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1802 assert(!output_var->type->is_record());
1803
1804 if (output_var->type->is_array()) {
1805 const unsigned slots = output_var->type->length
1806 * output_var->type->fields.array->matrix_columns;
1807
1808 *output_index += slots;
1809 *input_index += slots;
1810 } else {
1811 const unsigned slots = output_var->type->matrix_columns;
1812
1813 *output_index += slots;
1814 *input_index += slots;
1815 }
1816}
1817
1818
1819/**
1820 * Assign locations for all variables that are produced in one pipeline stage
1821 * (the "producer") and consumed in the next stage (the "consumer").
1822 *
1823 * Variables produced by the producer may also be consumed by transform
1824 * feedback.
1825 *
1826 * \param num_tfeedback_decls is the number of declarations indicating
1827 * variables that may be consumed by transform feedback.
1828 *
1829 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
1830 * representing the result of parsing the strings passed to
1831 * glTransformFeedbackVaryings(). assign_location() will be called for
1832 * each of these objects that matches one of the outputs of the
1833 * producer.
1834 *
1835 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
1836 * be NULL. In this case, varying locations are assigned solely based on the
1837 * requirements of transform feedback.
1838 */
Ian Romanickde773242011-06-09 13:31:32 -07001839bool
1840assign_varying_locations(struct gl_context *ctx,
1841 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07001842 gl_shader *producer, gl_shader *consumer,
1843 unsigned num_tfeedback_decls,
1844 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07001845{
1846 /* FINISHME: Set dynamically when geometry shader support is added. */
1847 unsigned output_index = VERT_RESULT_VAR0;
1848 unsigned input_index = FRAG_ATTRIB_VAR0;
1849
1850 /* Operate in a total of three passes.
1851 *
1852 * 1. Assign locations for any matching inputs and outputs.
1853 *
1854 * 2. Mark output variables in the producer that do not have locations as
1855 * not being outputs. This lets the optimizer eliminate them.
1856 *
1857 * 3. Mark input variables in the consumer that do not have locations as
1858 * not being inputs. This lets the optimizer eliminate them.
1859 */
1860
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07001861 link_invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
Paul Berry871ddb92011-11-05 11:17:32 -07001862 if (consumer)
1863 link_invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
Ian Romanick0e59b262010-06-23 11:23:01 -07001864
Eric Anholt16b68b12010-06-30 11:05:43 -07001865 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001866 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1867
Paul Berry871ddb92011-11-05 11:17:32 -07001868 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07001869 continue;
1870
Paul Berry871ddb92011-11-05 11:17:32 -07001871 ir_variable *input_var =
1872 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07001873
Paul Berry871ddb92011-11-05 11:17:32 -07001874 if (input_var && input_var->mode != ir_var_in)
1875 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07001876
Paul Berry871ddb92011-11-05 11:17:32 -07001877 if (input_var) {
1878 assign_varying_location(input_var, output_var, &input_index,
1879 &output_index);
1880 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001881
Paul Berry871ddb92011-11-05 11:17:32 -07001882 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
1883 if (!tfeedback_decls[i].is_assigned() &&
1884 tfeedback_decls[i].matches_var(output_var)) {
1885 if (output_var->location == -1) {
1886 assign_varying_location(input_var, output_var, &input_index,
1887 &output_index);
1888 }
1889 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
1890 return false;
1891 }
Ian Romanickdf869d92010-08-30 15:37:44 -07001892 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001893 }
1894
Ian Romanickde773242011-06-09 13:31:32 -07001895 unsigned varying_vectors = 0;
1896
Paul Berry871ddb92011-11-05 11:17:32 -07001897 if (consumer) {
1898 foreach_list(node, consumer->ir) {
1899 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07001900
Paul Berry871ddb92011-11-05 11:17:32 -07001901 if ((var == NULL) || (var->mode != ir_var_in))
1902 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07001903
Paul Berry871ddb92011-11-05 11:17:32 -07001904 if (var->location == -1) {
1905 if (prog->Version <= 120) {
1906 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1907 *
1908 * Only those varying variables used (i.e. read) in
1909 * the fragment shader executable must be written to
1910 * by the vertex shader executable; declaring
1911 * superfluous varying variables in a vertex shader is
1912 * permissible.
1913 *
1914 * We interpret this text as meaning that the VS must
1915 * write the variable for the FS to read it. See
1916 * "glsl1-varying read but not written" in piglit.
1917 */
Eric Anholtb7062832010-07-28 13:52:23 -07001918
Paul Berry871ddb92011-11-05 11:17:32 -07001919 linker_error(prog, "fragment shader varying %s not written "
1920 "by vertex shader\n.", var->name);
1921 }
Eric Anholtb7062832010-07-28 13:52:23 -07001922
Paul Berry871ddb92011-11-05 11:17:32 -07001923 /* An 'in' variable is only really a shader input if its
1924 * value is written by the previous stage.
1925 */
1926 var->mode = ir_var_auto;
1927 } else {
1928 /* The packing rules are used for vertex shader inputs are also
1929 * used for fragment shader inputs.
1930 */
1931 varying_vectors += count_attribute_slots(var->type);
1932 }
Eric Anholtb7062832010-07-28 13:52:23 -07001933 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001934 }
Ian Romanickde773242011-06-09 13:31:32 -07001935
1936 if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
1937 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001938 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
1939 linker_warning(prog, "shader uses too many varying vectors "
1940 "(%u > %u), but the driver will try to optimize "
1941 "them out; this is non-portable out-of-spec "
1942 "behavior\n",
1943 varying_vectors, ctx->Const.MaxVarying);
1944 } else {
1945 linker_error(prog, "shader uses too many varying vectors "
1946 "(%u > %u)\n",
1947 varying_vectors, ctx->Const.MaxVarying);
1948 return false;
1949 }
Ian Romanickde773242011-06-09 13:31:32 -07001950 }
1951 } else {
1952 const unsigned float_components = varying_vectors * 4;
1953 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001954 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
1955 linker_warning(prog, "shader uses too many varying components "
1956 "(%u > %u), but the driver will try to optimize "
1957 "them out; this is non-portable out-of-spec "
1958 "behavior\n",
1959 float_components, ctx->Const.MaxVarying * 4);
1960 } else {
1961 linker_error(prog, "shader uses too many varying components "
1962 "(%u > %u)\n",
1963 float_components, ctx->Const.MaxVarying * 4);
1964 return false;
1965 }
Ian Romanickde773242011-06-09 13:31:32 -07001966 }
1967 }
1968
1969 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07001970}
1971
1972
Paul Berry871ddb92011-11-05 11:17:32 -07001973/**
1974 * Store transform feedback location assignments into
1975 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
1976 *
1977 * If an error occurs, the error is reported through linker_error() and false
1978 * is returned.
1979 */
1980static bool
1981store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
1982 unsigned num_tfeedback_decls,
1983 tfeedback_decl *tfeedback_decls)
1984{
Paul Berryebfad9f2011-12-29 15:55:01 -08001985 bool separate_attribs_mode =
1986 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08001987
1988 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01001989 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08001990
1991 memset(&prog->LinkedTransformFeedback, 0,
1992 sizeof(prog->LinkedTransformFeedback));
1993
Paul Berry1be0fd82012-01-05 13:06:36 -08001994 prog->LinkedTransformFeedback.NumBuffers =
1995 separate_attribs_mode ? num_tfeedback_decls : 1;
1996
Eric Anholt9d36c962012-01-02 17:08:13 -08001997 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08001998 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001999 struct gl_transform_feedback_varying_info,
2000 num_tfeedback_decls);
2001
Christoph Bumillerd540af52012-01-20 13:24:46 +01002002 unsigned num_outputs = 0;
2003 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
2004 if (!tfeedback_decls[i].accumulate_num_outputs(prog, &num_outputs))
2005 return false;
2006
2007 prog->LinkedTransformFeedback.Outputs =
2008 rzalloc_array(prog,
2009 struct gl_transform_feedback_output,
2010 num_outputs);
2011
Paul Berry871ddb92011-11-05 11:17:32 -07002012 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
Paul Berryebfad9f2011-12-29 15:55:01 -08002013 unsigned buffer = separate_attribs_mode ? i : 0;
Paul Berryd3150eb2012-01-09 11:25:14 -08002014 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
Christoph Bumillerd540af52012-01-20 13:24:46 +01002015 buffer, i, num_outputs))
Paul Berry871ddb92011-11-05 11:17:32 -07002016 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07002017 }
Christoph Bumillerd540af52012-01-20 13:24:46 +01002018 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002019
2020 return true;
2021}
2022
Ian Romanick92f81592011-11-08 12:37:19 -08002023/**
Marek Olšákec174a42011-11-18 15:00:10 +01002024 * Store the gl_FragDepth layout in the gl_shader_program struct.
2025 */
2026static void
2027store_fragdepth_layout(struct gl_shader_program *prog)
2028{
2029 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2030 return;
2031 }
2032
2033 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2034
2035 /* We don't look up the gl_FragDepth symbol directly because if
2036 * gl_FragDepth is not used in the shader, it's removed from the IR.
2037 * However, the symbol won't be removed from the symbol table.
2038 *
2039 * We're only interested in the cases where the variable is NOT removed
2040 * from the IR.
2041 */
2042 foreach_list(node, ir) {
2043 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2044
2045 if (var == NULL || var->mode != ir_var_out) {
2046 continue;
2047 }
2048
2049 if (strcmp(var->name, "gl_FragDepth") == 0) {
2050 switch (var->depth_layout) {
2051 case ir_depth_layout_none:
2052 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2053 return;
2054 case ir_depth_layout_any:
2055 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2056 return;
2057 case ir_depth_layout_greater:
2058 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2059 return;
2060 case ir_depth_layout_less:
2061 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2062 return;
2063 case ir_depth_layout_unchanged:
2064 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2065 return;
2066 default:
2067 assert(0);
2068 return;
2069 }
2070 }
2071 }
2072}
2073
2074/**
Ian Romanick92f81592011-11-08 12:37:19 -08002075 * Validate the resources used by a program versus the implementation limits
2076 */
2077static bool
2078check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2079{
2080 static const char *const shader_names[MESA_SHADER_TYPES] = {
2081 "vertex", "fragment", "geometry"
2082 };
2083
2084 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2085 ctx->Const.MaxVertexTextureImageUnits,
2086 ctx->Const.MaxTextureImageUnits,
2087 ctx->Const.MaxGeometryTextureImageUnits
2088 };
2089
2090 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2091 ctx->Const.VertexProgram.MaxUniformComponents,
2092 ctx->Const.FragmentProgram.MaxUniformComponents,
2093 0 /* FINISHME: Geometry shaders. */
2094 };
2095
2096 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2097 struct gl_shader *sh = prog->_LinkedShaders[i];
2098
2099 if (sh == NULL)
2100 continue;
2101
2102 if (sh->num_samplers > max_samplers[i]) {
2103 linker_error(prog, "Too many %s shader texture samplers",
2104 shader_names[i]);
2105 }
2106
2107 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002108 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2109 linker_warning(prog, "Too many %s shader uniform components, "
2110 "but the driver will try to optimize them out; "
2111 "this is non-portable out-of-spec behavior\n",
2112 shader_names[i]);
2113 } else {
2114 linker_error(prog, "Too many %s shader uniform components",
2115 shader_names[i]);
2116 }
Ian Romanick92f81592011-11-08 12:37:19 -08002117 }
2118 }
2119
2120 return prog->LinkStatus;
2121}
Paul Berry871ddb92011-11-05 11:17:32 -07002122
Ian Romanick0e59b262010-06-23 11:23:01 -07002123void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002124link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002125{
Paul Berry871ddb92011-11-05 11:17:32 -07002126 tfeedback_decl *tfeedback_decls = NULL;
2127 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2128
Kenneth Graunked3073f52011-01-21 14:32:31 -08002129 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002130
Ian Romanick832dfa52010-06-17 15:04:20 -07002131 prog->LinkStatus = false;
2132 prog->Validated = false;
2133 prog->_Used = false;
2134
Ian Romanickf36460e2010-06-23 12:07:22 -07002135 if (prog->InfoLog != NULL)
Kenneth Graunked3073f52011-01-21 14:32:31 -08002136 ralloc_free(prog->InfoLog);
Ian Romanickf36460e2010-06-23 12:07:22 -07002137
Kenneth Graunked3073f52011-01-21 14:32:31 -08002138 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002139
Ian Romanick832dfa52010-06-17 15:04:20 -07002140 /* Separate the shaders into groups based on their type.
2141 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002142 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002143 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002144 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002145 unsigned num_frag_shaders = 0;
2146
Eric Anholt16b68b12010-06-30 11:05:43 -07002147 vert_shader_list = (struct gl_shader **)
2148 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002149 frag_shader_list = &vert_shader_list[prog->NumShaders];
2150
Ian Romanick25f51d32010-07-16 15:51:50 -07002151 unsigned min_version = UINT_MAX;
2152 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07002153 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002154 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2155 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2156
Ian Romanick832dfa52010-06-17 15:04:20 -07002157 switch (prog->Shaders[i]->Type) {
2158 case GL_VERTEX_SHADER:
2159 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2160 num_vert_shaders++;
2161 break;
2162 case GL_FRAGMENT_SHADER:
2163 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2164 num_frag_shaders++;
2165 break;
2166 case GL_GEOMETRY_SHADER:
2167 /* FINISHME: Support geometry shaders. */
2168 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2169 break;
2170 }
2171 }
2172
Ian Romanick25f51d32010-07-16 15:51:50 -07002173 /* Previous to GLSL version 1.30, different compilation units could mix and
2174 * match shading language versions. With GLSL 1.30 and later, the versions
2175 * of all shaders must match.
2176 */
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002177 assert(min_version >= 100);
Eric Anholtc5ff9a82012-03-08 13:49:15 -08002178 assert(max_version <= 140);
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002179 if ((max_version >= 130 || min_version == 100)
2180 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002181 linker_error(prog, "all shaders must use same shading "
2182 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002183 goto done;
2184 }
2185
2186 prog->Version = max_version;
2187
Ian Romanick3322fba2010-10-14 13:28:42 -07002188 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2189 if (prog->_LinkedShaders[i] != NULL)
2190 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2191
2192 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002193 }
2194
Ian Romanickcd6764e2010-07-16 16:00:07 -07002195 /* Link all shaders for a particular stage and validate the result.
2196 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002197 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002198 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002199 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2200 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002201
2202 if (sh == NULL)
2203 goto done;
2204
2205 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002206 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002207
Ian Romanick3322fba2010-10-14 13:28:42 -07002208 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2209 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002210 }
2211
2212 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002213 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002214 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2215 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002216
2217 if (sh == NULL)
2218 goto done;
2219
2220 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002221 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002222
Ian Romanick3322fba2010-10-14 13:28:42 -07002223 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2224 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002225 }
2226
Ian Romanick3ed850e2010-06-23 12:18:21 -07002227 /* Here begins the inter-stage linking phase. Some initial validation is
2228 * performed, then locations are assigned for uniforms, attributes, and
2229 * varyings.
2230 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002231 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002232 unsigned prev;
2233
2234 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2235 if (prog->_LinkedShaders[prev] != NULL)
2236 break;
2237 }
2238
Bryan Cainf18a0862011-04-23 19:29:15 -05002239 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002240 * stage.
2241 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002242 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2243 if (prog->_LinkedShaders[i] == NULL)
2244 continue;
2245
Ian Romanickf36460e2010-06-23 12:07:22 -07002246 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002247 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002248 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002249 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002250
2251 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002252 }
2253
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002254 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002255 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002256
Eric Anholt2f4fe152010-08-10 13:06:49 -07002257 /* Do common optimization before assigning storage for attributes,
2258 * uniforms, and varyings. Later optimization could possibly make
2259 * some of that unused.
2260 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002261 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2262 if (prog->_LinkedShaders[i] == NULL)
2263 continue;
2264
Ian Romanick02c5ae12011-07-11 10:46:01 -07002265 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2266 if (!prog->LinkStatus)
2267 goto done;
2268
Paul Berryc06e3252011-08-11 20:58:21 -07002269 if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
2270 lower_clip_distance(prog->_LinkedShaders[i]->ir);
2271
Ian Romanick1d5d67f2011-10-21 11:17:39 -07002272 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, 32))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002273 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002274 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002275
Ian Romanickd32d4f72011-06-27 17:59:58 -07002276 /* FINISHME: The value of the max_attribute_index parameter is
2277 * FINISHME: implementation dependent based on the value of
2278 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2279 * FINISHME: at least 16, so hardcode 16 for now.
2280 */
2281 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002282 goto done;
2283 }
2284
2285 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, ctx->Const.MaxDrawBuffers)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002286 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002287 }
2288
Ian Romanick3322fba2010-10-14 13:28:42 -07002289 unsigned prev;
2290 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2291 if (prog->_LinkedShaders[prev] != NULL)
2292 break;
2293 }
2294
Paul Berry871ddb92011-11-05 11:17:32 -07002295 if (num_tfeedback_decls != 0) {
2296 /* From GL_EXT_transform_feedback:
2297 * A program will fail to link if:
2298 *
2299 * * the <count> specified by TransformFeedbackVaryingsEXT is
2300 * non-zero, but the program object has no vertex or geometry
2301 * shader;
2302 */
2303 if (prev >= MESA_SHADER_FRAGMENT) {
2304 linker_error(prog, "Transform feedback varyings specified, but "
2305 "no vertex or geometry shader is present.");
2306 goto done;
2307 }
2308
2309 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2310 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002311 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002312 prog->TransformFeedback.VaryingNames,
2313 tfeedback_decls))
2314 goto done;
2315 }
2316
Ian Romanick3322fba2010-10-14 13:28:42 -07002317 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2318 if (prog->_LinkedShaders[i] == NULL)
2319 continue;
2320
Paul Berry871ddb92011-11-05 11:17:32 -07002321 if (!assign_varying_locations(
2322 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2323 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2324 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002325 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002326
Ian Romanick3322fba2010-10-14 13:28:42 -07002327 prev = i;
2328 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002329
Paul Berry871ddb92011-11-05 11:17:32 -07002330 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2331 /* There was no fragment shader, but we still have to assign varying
2332 * locations for use by transform feedback.
2333 */
2334 if (!assign_varying_locations(
2335 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2336 tfeedback_decls))
2337 goto done;
2338 }
2339
2340 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2341 goto done;
2342
Ian Romanickcc90e622010-10-19 17:59:10 -07002343 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2344 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2345 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002346
2347 /* Eliminate code that is now dead due to unused vertex outputs being
2348 * demoted.
2349 */
2350 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2351 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002352 }
2353
2354 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2355 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2356
2357 demote_shader_inputs_and_outputs(sh, ir_var_in);
2358 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2359 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002360
2361 /* Eliminate code that is now dead due to unused geometry outputs being
2362 * demoted.
2363 */
2364 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2365 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002366 }
2367
2368 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2369 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2370
2371 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002372
2373 /* Eliminate code that is now dead due to unused fragment inputs being
2374 * demoted. This shouldn't actually do anything other than remove
2375 * declarations of the (now unused) global variables.
2376 */
2377 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2378 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002379 }
2380
Ian Romanick960d7222011-10-21 11:21:02 -07002381 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002382 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002383 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002384
Ian Romanick92f81592011-11-08 12:37:19 -08002385 if (!check_resources(ctx, prog))
2386 goto done;
2387
Ian Romanickce9171f2011-02-03 17:10:14 -08002388 /* OpenGL ES requires that a vertex shader and a fragment shader both be
2389 * present in a linked program. By checking for use of shading language
2390 * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
2391 */
Eric Anholt57f79782011-07-22 12:57:47 -07002392 if (!prog->InternalSeparateShader &&
2393 (ctx->API == API_OPENGLES2 || prog->Version == 100)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002394 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002395 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002396 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002397 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002398 }
2399 }
2400
Ian Romanick13e10e42010-06-21 12:03:24 -07002401 /* FINISHME: Assign fragment shader output locations. */
2402
Ian Romanick832dfa52010-06-17 15:04:20 -07002403done:
2404 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002405
2406 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2407 if (prog->_LinkedShaders[i] == NULL)
2408 continue;
2409
2410 /* Retain any live IR, but trash the rest. */
2411 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002412
2413 /* The symbol table in the linked shaders may contain references to
2414 * variables that were removed (e.g., unused uniforms). Since it may
2415 * contain junk, there is no possible valid use. Delete it and set the
2416 * pointer to NULL.
2417 */
2418 delete prog->_LinkedShaders[i]->symbols;
2419 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002420 }
2421
Kenneth Graunked3073f52011-01-21 14:32:31 -08002422 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002423}