blob: dd2278545d83329a5048732f16aea5f1866ba672 [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 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700104 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700105 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
Kenneth Graunked884f602012-03-20 15:56:37 -0700120 if (ir->return_deref != NULL) {
121 ir_variable *const var = ir->return_deref->variable_referenced();
122
123 if (strcmp(name, var->name) == 0) {
124 found = true;
125 return visit_stop;
126 }
127 }
128
Eric Anholt18a60232010-08-23 11:29:25 -0700129 return visit_continue_with_parent;
130 }
131
Ian Romanick832dfa52010-06-17 15:04:20 -0700132 bool variable_found()
133 {
134 return found;
135 }
136
137private:
138 const char *name; /**< Find writes to a variable with this name. */
139 bool found; /**< Was a write to the variable found? */
140};
141
Ian Romanickc93b8f12010-06-17 15:20:22 -0700142
Ian Romanickc33e78f2010-08-13 12:30:41 -0700143/**
144 * Visitor that determines whether or not a variable is ever read.
145 */
146class find_deref_visitor : public ir_hierarchical_visitor {
147public:
148 find_deref_visitor(const char *name)
149 : name(name), found(false)
150 {
151 /* empty */
152 }
153
154 virtual ir_visitor_status visit(ir_dereference_variable *ir)
155 {
156 if (strcmp(this->name, ir->var->name) == 0) {
157 this->found = true;
158 return visit_stop;
159 }
160
161 return visit_continue;
162 }
163
164 bool variable_found() const
165 {
166 return this->found;
167 }
168
169private:
170 const char *name; /**< Find writes to a variable with this name. */
171 bool found; /**< Was a write to the variable found? */
172};
173
174
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700175void
Ian Romanick586e7412011-07-28 14:04:09 -0700176linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700177{
178 va_list ap;
179
Kenneth Graunked3073f52011-01-21 14:32:31 -0800180 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700181 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800182 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700183 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700184
185 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700186}
187
188
189void
Ian Romanick379a32f2011-07-28 14:09:06 -0700190linker_warning(gl_shader_program *prog, const char *fmt, ...)
191{
192 va_list ap;
193
194 ralloc_strcat(&prog->InfoLog, "error: ");
195 va_start(ap, fmt);
196 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
197 va_end(ap);
198
199}
200
201
202void
Ian Romanickf6ee7bc2011-10-11 16:15:47 -0700203link_invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
204 int generic_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700205{
Eric Anholt16b68b12010-06-30 11:05:43 -0700206 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700207 ir_variable *const var = ((ir_instruction *) node)->as_variable();
208
209 if ((var == NULL) || (var->mode != (unsigned) mode))
210 continue;
211
212 /* Only assign locations for generic attributes / varyings / etc.
213 */
Ian Romanick68a4fc92010-10-07 17:21:22 -0700214 if ((var->location >= generic_base) && !var->explicit_location)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700215 var->location = -1;
216 }
217}
218
219
Ian Romanickc93b8f12010-06-17 15:20:22 -0700220/**
Ian Romanick69846702010-06-22 17:29:19 -0700221 * Determine the number of attribute slots required for a particular type
222 *
223 * This code is here because it implements the language rules of a specific
224 * GLSL version. Since it's a property of the language and not a property of
225 * types in general, it doesn't really belong in glsl_type.
226 */
227unsigned
228count_attribute_slots(const glsl_type *t)
229{
230 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
231 *
232 * "A scalar input counts the same amount against this limit as a vec4,
233 * so applications may want to consider packing groups of four
234 * unrelated float inputs together into a vector to better utilize the
235 * capabilities of the underlying hardware. A matrix input will use up
236 * multiple locations. The number of locations used will equal the
237 * number of columns in the matrix."
238 *
239 * The spec does not explicitly say how arrays are counted. However, it
240 * should be safe to assume the total number of slots consumed by an array
241 * is the number of entries in the array multiplied by the number of slots
242 * consumed by a single element of the array.
243 */
244
245 if (t->is_array())
246 return t->array_size() * count_attribute_slots(t->element_type());
247
248 if (t->is_matrix())
249 return t->matrix_columns;
250
251 return 1;
252}
253
254
255/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700256 * Verify that a vertex shader executable meets all semantic requirements.
257 *
Paul Berry642e5b412012-01-04 13:57:52 -0800258 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
259 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700260 *
261 * \param shader Vertex shader executable to be verified
262 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700263bool
Eric Anholt849e1812010-06-30 11:49:17 -0700264validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700265 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700266{
267 if (shader == NULL)
268 return true;
269
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700270 /* From the GLSL 1.10 spec, page 48:
271 *
272 * "The variable gl_Position is available only in the vertex
273 * language and is intended for writing the homogeneous vertex
274 * position. All executions of a well-formed vertex shader
275 * executable must write a value into this variable. [...] The
276 * variable gl_Position is available only in the vertex
277 * language and is intended for writing the homogeneous vertex
278 * position. All executions of a well-formed vertex shader
279 * executable must write a value into this variable."
280 *
281 * while in GLSL 1.40 this text is changed to:
282 *
283 * "The variable gl_Position is available only in the vertex
284 * language and is intended for writing the homogeneous vertex
285 * position. It can be written at any time during shader
286 * execution. It may also be read back by a vertex shader
287 * after being written. This value will be used by primitive
288 * assembly, clipping, culling, and other fixed functionality
289 * operations, if present, that operate on primitives after
290 * vertex processing has occurred. Its value is undefined if
291 * the vertex shader executable does not write gl_Position."
292 */
293 if (prog->Version < 140) {
294 find_assignment_visitor find("gl_Position");
295 find.run(shader->ir);
296 if (!find.variable_found()) {
297 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
298 return false;
299 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700300 }
301
Paul Berry642e5b412012-01-04 13:57:52 -0800302 prog->Vert.ClipDistanceArraySize = 0;
303
Paul Berryb453ba22011-08-11 18:10:22 -0700304 if (prog->Version >= 130) {
305 /* From section 7.1 (Vertex Shader Special Variables) of the
306 * GLSL 1.30 spec:
307 *
308 * "It is an error for a shader to statically write both
309 * gl_ClipVertex and gl_ClipDistance."
310 */
311 find_assignment_visitor clip_vertex("gl_ClipVertex");
312 find_assignment_visitor clip_distance("gl_ClipDistance");
313
314 clip_vertex.run(shader->ir);
315 clip_distance.run(shader->ir);
316 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
317 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
318 "and `gl_ClipDistance'\n");
319 return false;
320 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700321 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800322 ir_variable *clip_distance_var =
323 shader->symbols->get_variable("gl_ClipDistance");
324 if (clip_distance_var)
325 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700326 }
327
Ian Romanick832dfa52010-06-17 15:04:20 -0700328 return true;
329}
330
331
Ian Romanickc93b8f12010-06-17 15:20:22 -0700332/**
333 * Verify that a fragment shader executable meets all semantic requirements
334 *
335 * \param shader Fragment shader executable to be verified
336 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700337bool
Eric Anholt849e1812010-06-30 11:49:17 -0700338validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700339 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700340{
341 if (shader == NULL)
342 return true;
343
Ian Romanick832dfa52010-06-17 15:04:20 -0700344 find_assignment_visitor frag_color("gl_FragColor");
345 find_assignment_visitor frag_data("gl_FragData");
346
Eric Anholt16b68b12010-06-30 11:05:43 -0700347 frag_color.run(shader->ir);
348 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700349
Ian Romanick832dfa52010-06-17 15:04:20 -0700350 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700351 linker_error(prog, "fragment shader writes to both "
352 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700353 return false;
354 }
355
356 return true;
357}
358
359
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700360/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700361 * Generate a string describing the mode of a variable
362 */
363static const char *
364mode_string(const ir_variable *var)
365{
366 switch (var->mode) {
367 case ir_var_auto:
368 return (var->read_only) ? "global constant" : "global variable";
369
370 case ir_var_uniform: return "uniform";
371 case ir_var_in: return "shader input";
372 case ir_var_out: return "shader output";
373 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700374
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800375 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700376 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700377 default:
378 assert(!"Should not get here.");
379 return "invalid variable";
380 }
381}
382
383
384/**
385 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700386 */
387bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700388cross_validate_globals(struct gl_shader_program *prog,
389 struct gl_shader **shader_list,
390 unsigned num_shaders,
391 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700392{
393 /* Examine all of the uniforms in all of the shaders and cross validate
394 * them.
395 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700396 glsl_symbol_table variables;
397 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700398 if (shader_list[i] == NULL)
399 continue;
400
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700401 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700402 ir_variable *const var = ((ir_instruction *) node)->as_variable();
403
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700404 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700405 continue;
406
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700407 if (uniforms_only && (var->mode != ir_var_uniform))
408 continue;
409
Ian Romanick7e2aa912010-07-19 17:12:42 -0700410 /* Don't cross validate temporaries that are at global scope. These
411 * will eventually get pulled into the shaders 'main'.
412 */
413 if (var->mode == ir_var_temporary)
414 continue;
415
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700416 /* If a global with this name has already been seen, verify that the
417 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700418 * initializers, the values of the initializers must be the same.
419 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700420 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700421 if (existing != NULL) {
422 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700423 /* Consider the types to be "the same" if both types are arrays
424 * of the same type and one of the arrays is implicitly sized.
425 * In addition, set the type of the linked variable to the
426 * explicitly sized array.
427 */
428 if (var->type->is_array()
429 && existing->type->is_array()
430 && (var->type->fields.array == existing->type->fields.array)
431 && ((var->type->length == 0)
432 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800433 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700434 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800435 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700436 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700437 linker_error(prog, "%s `%s' declared as type "
438 "`%s' and type `%s'\n",
439 mode_string(var),
440 var->name, var->type->name,
441 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700442 return false;
443 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700444 }
445
Ian Romanick68a4fc92010-10-07 17:21:22 -0700446 if (var->explicit_location) {
447 if (existing->explicit_location
448 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700449 linker_error(prog, "explicit locations for %s "
450 "`%s' have differing values\n",
451 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700452 return false;
453 }
454
455 existing->location = var->location;
456 existing->explicit_location = true;
457 }
458
Ian Romanick46173f92011-10-31 13:07:06 -0700459 /* Validate layout qualifiers for gl_FragDepth.
460 *
461 * From the AMD/ARB_conservative_depth specs:
462 *
463 * "If gl_FragDepth is redeclared in any fragment shader in a
464 * program, it must be redeclared in all fragment shaders in
465 * that program that have static assignments to
466 * gl_FragDepth. All redeclarations of gl_FragDepth in all
467 * fragment shaders in a single program must have the same set
468 * of qualifiers."
469 */
470 if (strcmp(var->name, "gl_FragDepth") == 0) {
471 bool layout_declared = var->depth_layout != ir_depth_layout_none;
472 bool layout_differs =
473 var->depth_layout != existing->depth_layout;
474
475 if (layout_declared && layout_differs) {
476 linker_error(prog,
477 "All redeclarations of gl_FragDepth in all "
478 "fragment shaders in a single program must have "
479 "the same set of qualifiers.");
480 }
481
482 if (var->used && layout_differs) {
483 linker_error(prog,
484 "If gl_FragDepth is redeclared with a layout "
485 "qualifier in any fragment shader, it must be "
486 "redeclared with the same layout qualifier in "
487 "all fragment shaders that have assignments to "
488 "gl_FragDepth");
489 }
490 }
Chad Versaceaddae332011-01-27 01:40:31 -0800491
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700492 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
493 *
494 * "If a shared global has multiple initializers, the
495 * initializers must all be constant expressions, and they
496 * must all have the same value. Otherwise, a link error will
497 * result. (A shared global having only one initializer does
498 * not require that initializer to be a constant expression.)"
499 *
500 * Previous to 4.20 the GLSL spec simply said that initializers
501 * must have the same value. In this case of non-constant
502 * initializers, this was impossible to determine. As a result,
503 * no vendor actually implemented that behavior. The 4.20
504 * behavior matches the implemented behavior of at least one other
505 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700506 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700507 if (var->constant_initializer != NULL) {
508 if (existing->constant_initializer != NULL) {
509 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700510 linker_error(prog, "initializers for %s "
511 "`%s' have differing values\n",
512 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700513 return false;
514 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700515 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700516 /* If the first-seen instance of a particular uniform did not
517 * have an initializer but a later instance does, copy the
518 * initializer to the version stored in the symbol table.
519 */
Ian Romanickde415b72010-07-14 13:22:12 -0700520 /* FINISHME: This is wrong. The constant_value field should
521 * FINISHME: not be modified! Imagine a case where a shader
522 * FINISHME: without an initializer is linked in two different
523 * FINISHME: programs with shaders that have differing
524 * FINISHME: initializers. Linking with the first will
525 * FINISHME: modify the shader, and linking with the second
526 * FINISHME: will fail.
527 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700528 existing->constant_initializer =
529 var->constant_initializer->clone(ralloc_parent(existing),
530 NULL);
531 }
532 }
533
534 if (var->has_initializer) {
535 if (existing->has_initializer
536 && (var->constant_initializer == NULL
537 || existing->constant_initializer == NULL)) {
538 linker_error(prog,
539 "shared global variable `%s' has multiple "
540 "non-constant initializers.\n",
541 var->name);
542 return false;
543 }
544
545 /* Some instance had an initializer, so keep track of that. In
546 * this location, all sorts of initializers (constant or
547 * otherwise) will propagate the existence to the variable
548 * stored in the symbol table.
549 */
550 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700551 }
Chad Versace7528f142010-11-17 14:34:38 -0800552
553 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700554 linker_error(prog, "declarations for %s `%s' have "
555 "mismatching invariant qualifiers\n",
556 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800557 return false;
558 }
Chad Versace61428dd2011-01-10 15:29:30 -0800559 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700560 linker_error(prog, "declarations for %s `%s' have "
561 "mismatching centroid qualifiers\n",
562 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800563 return false;
564 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700565 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700566 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700567 }
568 }
569
570 return true;
571}
572
573
Ian Romanick37101922010-06-18 19:02:10 -0700574/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700575 * Perform validation of uniforms used across multiple shader stages
576 */
577bool
578cross_validate_uniforms(struct gl_shader_program *prog)
579{
580 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700581 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700582}
583
Eric Anholtf609cf72012-04-27 13:52:56 -0700584/**
585 * Accumulates the array of prog->UniformBlocks and checks that all
586 * definitons of blocks agree on their contents.
587 */
588static bool
589interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
590{
591 unsigned max_num_uniform_blocks = 0;
592 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
593 if (prog->_LinkedShaders[i])
594 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
595 }
596
597 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
598 struct gl_shader *sh = prog->_LinkedShaders[i];
599
600 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
601 max_num_uniform_blocks);
602 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
603 prog->UniformBlockStageIndex[i][j] = -1;
604
605 if (sh == NULL)
606 continue;
607
608 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
609 int index = link_cross_validate_uniform_block(prog,
610 &prog->UniformBlocks,
611 &prog->NumUniformBlocks,
612 &sh->UniformBlocks[j]);
613
614 if (index == -1) {
615 linker_error(prog, "uniform block `%s' has mismatching definitions",
616 sh->UniformBlocks[j].Name);
617 return false;
618 }
619
620 prog->UniformBlockStageIndex[i][index] = j;
621 }
622 }
623
624 return true;
625}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700626
627/**
Ian Romanick37101922010-06-18 19:02:10 -0700628 * Validate that outputs from one stage match inputs of another
629 */
630bool
Eric Anholt849e1812010-06-30 11:49:17 -0700631cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700632 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700633{
634 glsl_symbol_table parameters;
635 /* FINISHME: Figure these out dynamically. */
636 const char *const producer_stage = "vertex";
637 const char *const consumer_stage = "fragment";
638
639 /* Find all shader outputs in the "producer" stage.
640 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700641 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700642 ir_variable *const var = ((ir_instruction *) node)->as_variable();
643
644 /* FINISHME: For geometry shaders, this should also look for inout
645 * FINISHME: variables.
646 */
647 if ((var == NULL) || (var->mode != ir_var_out))
648 continue;
649
Eric Anholt001eee52010-11-05 06:11:24 -0700650 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700651 }
652
653
654 /* Find all shader inputs in the "consumer" stage. Any variables that have
655 * matching outputs already in the symbol table must have the same type and
656 * qualifiers.
657 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700658 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700659 ir_variable *const input = ((ir_instruction *) node)->as_variable();
660
661 /* FINISHME: For geometry shaders, this should also look for inout
662 * FINISHME: variables.
663 */
664 if ((input == NULL) || (input->mode != ir_var_in))
665 continue;
666
667 ir_variable *const output = parameters.get_variable(input->name);
668 if (output != NULL) {
669 /* Check that the types match between stages.
670 */
671 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800672 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500673 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800674 * access it must redeclare it with a size. There is some
675 * language in the GLSL spec that implies the fragment shader
676 * and vertex shader do not have to agree on this size. Other
677 * driver behave this way, and one or two applications seem to
678 * rely on it.
679 *
680 * Neither declaration needs to be modified here because the array
681 * sizes are fixed later when update_array_sizes is called.
682 *
683 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
684 *
685 * "Unlike user-defined varying variables, the built-in
686 * varying variables don't have a strict one-to-one
687 * correspondence between the vertex language and the
688 * fragment language."
689 */
690 if (!output->type->is_array()
691 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700692 linker_error(prog,
693 "%s shader output `%s' declared as type `%s', "
694 "but %s shader input declared as type `%s'\n",
695 producer_stage, output->name,
696 output->type->name,
697 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800698 return false;
699 }
Ian Romanick37101922010-06-18 19:02:10 -0700700 }
701
702 /* Check that all of the qualifiers match between stages.
703 */
704 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700705 linker_error(prog,
706 "%s shader output `%s' %s centroid qualifier, "
707 "but %s shader input %s centroid qualifier\n",
708 producer_stage,
709 output->name,
710 (output->centroid) ? "has" : "lacks",
711 consumer_stage,
712 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700713 return false;
714 }
715
716 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700717 linker_error(prog,
718 "%s shader output `%s' %s invariant qualifier, "
719 "but %s shader input %s invariant qualifier\n",
720 producer_stage,
721 output->name,
722 (output->invariant) ? "has" : "lacks",
723 consumer_stage,
724 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700725 return false;
726 }
727
728 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700729 linker_error(prog,
730 "%s shader output `%s' specifies %s "
731 "interpolation qualifier, "
732 "but %s shader input specifies %s "
733 "interpolation qualifier\n",
734 producer_stage,
735 output->name,
736 output->interpolation_string(),
737 consumer_stage,
738 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700739 return false;
740 }
741 }
742 }
743
744 return true;
745}
746
747
Ian Romanick3fb87872010-07-09 14:09:34 -0700748/**
749 * Populates a shaders symbol table with all global declarations
750 */
751static void
752populate_symbol_table(gl_shader *sh)
753{
754 sh->symbols = new(sh) glsl_symbol_table;
755
756 foreach_list(node, sh->ir) {
757 ir_instruction *const inst = (ir_instruction *) node;
758 ir_variable *var;
759 ir_function *func;
760
761 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700762 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700763 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700764 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700765 }
766 }
767}
768
769
770/**
Ian Romanick31a97862010-07-12 18:48:50 -0700771 * Remap variables referenced in an instruction tree
772 *
773 * This is used when instruction trees are cloned from one shader and placed in
774 * another. These trees will contain references to \c ir_variable nodes that
775 * do not exist in the target shader. This function finds these \c ir_variable
776 * references and replaces the references with matching variables in the target
777 * shader.
778 *
779 * If there is no matching variable in the target shader, a clone of the
780 * \c ir_variable is made and added to the target shader. The new variable is
781 * added to \b both the instruction stream and the symbol table.
782 *
783 * \param inst IR tree that is to be processed.
784 * \param symbols Symbol table containing global scope symbols in the
785 * linked shader.
786 * \param instructions Instruction stream where new variable declarations
787 * should be added.
788 */
789void
Eric Anholt8273bd42010-08-04 12:34:56 -0700790remap_variables(ir_instruction *inst, struct gl_shader *target,
791 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700792{
793 class remap_visitor : public ir_hierarchical_visitor {
794 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700795 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700796 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700797 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700798 this->target = target;
799 this->symbols = target->symbols;
800 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700801 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700802 }
803
804 virtual ir_visitor_status visit(ir_dereference_variable *ir)
805 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700806 if (ir->var->mode == ir_var_temporary) {
807 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
808
809 assert(var != NULL);
810 ir->var = var;
811 return visit_continue;
812 }
813
Ian Romanick31a97862010-07-12 18:48:50 -0700814 ir_variable *const existing =
815 this->symbols->get_variable(ir->var->name);
816 if (existing != NULL)
817 ir->var = existing;
818 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700819 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700820
Eric Anholt001eee52010-11-05 06:11:24 -0700821 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700822 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700823 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700824 }
825
826 return visit_continue;
827 }
828
829 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700830 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700831 glsl_symbol_table *symbols;
832 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700833 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700834 };
835
Eric Anholt8273bd42010-08-04 12:34:56 -0700836 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700837
838 inst->accept(&v);
839}
840
841
842/**
843 * Move non-declarations from one instruction stream to another
844 *
845 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700846 * 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 -0700847 * pointer) for \c last and \c false for \c make_copies on the first
848 * call. Successive calls pass the return value of the previous call for
849 * \c last and \c true for \c make_copies.
850 *
851 * \param instructions Source instruction stream
852 * \param last Instruction after which new instructions should be
853 * inserted in the target instruction stream
854 * \param make_copies Flag selecting whether instructions in \c instructions
855 * should be copied (via \c ir_instruction::clone) into the
856 * target list or moved.
857 *
858 * \return
859 * The new "last" instruction in the target instruction stream. This pointer
860 * is suitable for use as the \c last parameter of a later call to this
861 * function.
862 */
863exec_node *
864move_non_declarations(exec_list *instructions, exec_node *last,
865 bool make_copies, gl_shader *target)
866{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700867 hash_table *temps = NULL;
868
869 if (make_copies)
870 temps = hash_table_ctor(0, hash_table_pointer_hash,
871 hash_table_pointer_compare);
872
Ian Romanick303c99f2010-07-19 12:34:56 -0700873 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700874 ir_instruction *inst = (ir_instruction *) node;
875
Ian Romanick7e2aa912010-07-19 17:12:42 -0700876 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700877 continue;
878
Ian Romanick7e2aa912010-07-19 17:12:42 -0700879 ir_variable *var = inst->as_variable();
880 if ((var != NULL) && (var->mode != ir_var_temporary))
881 continue;
882
883 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700884 || inst->as_call()
Ian Romanick7e2aa912010-07-19 17:12:42 -0700885 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700886
887 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700888 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700889
890 if (var != NULL)
891 hash_table_insert(temps, inst, var);
892 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700893 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700894 } else {
895 inst->remove();
896 }
897
898 last->insert_after(inst);
899 last = inst;
900 }
901
Ian Romanick7e2aa912010-07-19 17:12:42 -0700902 if (make_copies)
903 hash_table_dtor(temps);
904
Ian Romanick31a97862010-07-12 18:48:50 -0700905 return last;
906}
907
908/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700909 * Get the function signature for main from a shader
910 */
911static ir_function_signature *
912get_main_function_signature(gl_shader *sh)
913{
914 ir_function *const f = sh->symbols->get_function("main");
915 if (f != NULL) {
916 exec_list void_parameters;
917
918 /* Look for the 'void main()' signature and ensure that it's defined.
919 * This keeps the linker from accidentally pick a shader that just
920 * contains a prototype for main.
921 *
922 * We don't have to check for multiple definitions of main (in multiple
923 * shaders) because that would have already been caught above.
924 */
925 ir_function_signature *sig = f->matching_signature(&void_parameters);
926 if ((sig != NULL) && sig->is_defined) {
927 return sig;
928 }
929 }
930
931 return NULL;
932}
933
934
935/**
Brian Paul84a12732012-02-02 20:10:40 -0700936 * This class is only used in link_intrastage_shaders() below but declaring
937 * it inside that function leads to compiler warnings with some versions of
938 * gcc.
939 */
940class array_sizing_visitor : public ir_hierarchical_visitor {
941public:
942 virtual ir_visitor_status visit(ir_variable *var)
943 {
944 if (var->type->is_array() && (var->type->length == 0)) {
945 const glsl_type *type =
946 glsl_type::get_array_instance(var->type->fields.array,
947 var->max_array_access + 1);
948 assert(type != NULL);
949 var->type = type;
950 }
951 return visit_continue;
952 }
953};
954
Brian Paul84a12732012-02-02 20:10:40 -0700955/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700956 * Combine a group of shaders for a single stage to generate a linked shader
957 *
958 * \note
959 * If this function is supplied a single shader, it is cloned, and the new
960 * shader is returned.
961 */
962static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800963link_intrastage_shaders(void *mem_ctx,
964 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700965 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700966 struct gl_shader **shader_list,
967 unsigned num_shaders)
968{
Eric Anholtf609cf72012-04-27 13:52:56 -0700969 struct gl_uniform_block *uniform_blocks = NULL;
970 unsigned num_uniform_blocks = 0;
971
Ian Romanick13f782c2010-06-29 18:53:38 -0700972 /* Check that global variables defined in multiple shaders are consistent.
973 */
974 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
975 return NULL;
976
Eric Anholtf609cf72012-04-27 13:52:56 -0700977 /* Check that uniform blocks between shaders for a stage agree. */
978 for (unsigned i = 0; i < num_shaders; i++) {
979 struct gl_shader *sh = shader_list[i];
980
981 for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) {
982 int index = link_cross_validate_uniform_block(mem_ctx,
983 &uniform_blocks,
984 &num_uniform_blocks,
985 &sh->UniformBlocks[j]);
986 if (index == -1) {
987 linker_error(prog, "uniform block `%s' has mismatching definitions",
988 sh->UniformBlocks[j].Name);
989 return NULL;
990 }
991 }
992 }
993
Ian Romanick13f782c2010-06-29 18:53:38 -0700994 /* Check that there is only a single definition of each function signature
995 * across all shaders.
996 */
997 for (unsigned i = 0; i < (num_shaders - 1); i++) {
998 foreach_list(node, shader_list[i]->ir) {
999 ir_function *const f = ((ir_instruction *) node)->as_function();
1000
1001 if (f == NULL)
1002 continue;
1003
1004 for (unsigned j = i + 1; j < num_shaders; j++) {
1005 ir_function *const other =
1006 shader_list[j]->symbols->get_function(f->name);
1007
1008 /* If the other shader has no function (and therefore no function
1009 * signatures) with the same name, skip to the next shader.
1010 */
1011 if (other == NULL)
1012 continue;
1013
1014 foreach_iter (exec_list_iterator, iter, *f) {
1015 ir_function_signature *sig =
1016 (ir_function_signature *) iter.get();
1017
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001018 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -07001019 continue;
1020
1021 ir_function_signature *other_sig =
1022 other->exact_matching_signature(& sig->parameters);
1023
1024 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001025 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -07001026 linker_error(prog, "function `%s' is multiply defined",
1027 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001028 return NULL;
1029 }
1030 }
1031 }
1032 }
1033 }
1034
1035 /* Find the shader that defines main, and make a clone of it.
1036 *
1037 * Starting with the clone, search for undefined references. If one is
1038 * found, find the shader that defines it. Clone the reference and add
1039 * it to the shader. Repeat until there are no undefined references or
1040 * until a reference cannot be resolved.
1041 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001042 gl_shader *main = NULL;
1043 for (unsigned i = 0; i < num_shaders; i++) {
1044 if (get_main_function_signature(shader_list[i]) != NULL) {
1045 main = shader_list[i];
1046 break;
1047 }
1048 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001049
Ian Romanick15ce87e2010-07-09 15:28:22 -07001050 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001051 linker_error(prog, "%s shader lacks `main'\n",
1052 (shader_list[0]->Type == GL_VERTEX_SHADER)
1053 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -07001054 return NULL;
1055 }
1056
Ian Romanick4a455952010-10-13 15:13:02 -07001057 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001058 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001059 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001060
Eric Anholtf609cf72012-04-27 13:52:56 -07001061 linked->UniformBlocks = uniform_blocks;
1062 linked->NumUniformBlocks = num_uniform_blocks;
1063 ralloc_steal(linked, linked->UniformBlocks);
1064
Ian Romanick15ce87e2010-07-09 15:28:22 -07001065 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001066
Ian Romanick31a97862010-07-12 18:48:50 -07001067 /* The a pointer to the main function in the final linked shader (i.e., the
1068 * copy of the original shader that contained the main function).
1069 */
1070 ir_function_signature *const main_sig = get_main_function_signature(linked);
1071
1072 /* Move any instructions other than variable declarations or function
1073 * declarations into main.
1074 */
Ian Romanick9303e352010-07-19 12:33:54 -07001075 exec_node *insertion_point =
1076 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1077 linked);
1078
Ian Romanick31a97862010-07-12 18:48:50 -07001079 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001080 if (shader_list[i] == main)
1081 continue;
1082
Ian Romanick31a97862010-07-12 18:48:50 -07001083 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001084 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001085 }
1086
Ian Romanick13f782c2010-06-29 18:53:38 -07001087 /* Resolve initializers for global variables in the linked shader.
1088 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001089 unsigned num_linking_shaders = num_shaders;
1090 for (unsigned i = 0; i < num_shaders; i++)
1091 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1092
1093 gl_shader **linking_shaders =
1094 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1095
1096 memcpy(linking_shaders, shader_list,
1097 sizeof(linking_shaders[0]) * num_shaders);
1098
1099 unsigned idx = num_shaders;
1100 for (unsigned i = 0; i < num_shaders; i++) {
1101 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1102 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1103 idx += shader_list[i]->num_builtins_to_link;
1104 }
1105
1106 assert(idx == num_linking_shaders);
1107
Ian Romanick4a455952010-10-13 15:13:02 -07001108 if (!link_function_calls(prog, linked, linking_shaders,
1109 num_linking_shaders)) {
1110 ctx->Driver.DeleteShader(ctx, linked);
1111 linked = NULL;
1112 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001113
1114 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001115
Paul Berryc148ef62011-08-03 15:37:01 -07001116#ifdef DEBUG
1117 /* At this point linked should contain all of the linked IR, so
1118 * validate it to make sure nothing went wrong.
1119 */
1120 if (linked)
1121 validate_ir_tree(linked->ir);
1122#endif
1123
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001124 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001125 * unspecified sizes have a size specified. The size is inferred from the
1126 * max_array_access field.
1127 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001128 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001129 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001130
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001131 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001132 }
1133
Ian Romanick3fb87872010-07-09 14:09:34 -07001134 return linked;
1135}
1136
Eric Anholta721abf2010-08-23 10:32:01 -07001137/**
1138 * Update the sizes of linked shader uniform arrays to the maximum
1139 * array index used.
1140 *
1141 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1142 *
1143 * If one or more elements of an array are active,
1144 * GetActiveUniform will return the name of the array in name,
1145 * subject to the restrictions listed above. The type of the array
1146 * is returned in type. The size parameter contains the highest
1147 * array element index used, plus one. The compiler or linker
1148 * determines the highest index used. There will be only one
1149 * active uniform reported by the GL per uniform array.
1150
1151 */
1152static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001153update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001154{
Ian Romanick3322fba2010-10-14 13:28:42 -07001155 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1156 if (prog->_LinkedShaders[i] == NULL)
1157 continue;
1158
Eric Anholta721abf2010-08-23 10:32:01 -07001159 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1160 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1161
Eric Anholt586b4b52010-09-28 14:32:16 -07001162 if ((var == NULL) || (var->mode != ir_var_uniform &&
1163 var->mode != ir_var_in &&
1164 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001165 !var->type->is_array())
1166 continue;
1167
Eric Anholt9feb4032012-05-01 14:43:31 -07001168 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1169 * will not be eliminated. Since we always do std140, just
1170 * don't resize arrays in UBOs.
1171 */
1172 if (var->uniform_block != -1)
1173 continue;
1174
Eric Anholta721abf2010-08-23 10:32:01 -07001175 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001176 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1177 if (prog->_LinkedShaders[j] == NULL)
1178 continue;
1179
Eric Anholta721abf2010-08-23 10:32:01 -07001180 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1181 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1182 if (!other_var)
1183 continue;
1184
1185 if (strcmp(var->name, other_var->name) == 0 &&
1186 other_var->max_array_access > size) {
1187 size = other_var->max_array_access;
1188 }
1189 }
1190 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001191
Eric Anholta721abf2010-08-23 10:32:01 -07001192 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001193 /* If this is a built-in uniform (i.e., it's backed by some
1194 * fixed-function state), adjust the number of state slots to
1195 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001196 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001197 * slots is an integer multiple of the number of array elements.
1198 * Determine the number of slots per array element by dividing by
1199 * the old (total) size.
1200 */
1201 if (var->num_state_slots > 0) {
1202 var->num_state_slots = (size + 1)
1203 * (var->num_state_slots / var->type->length);
1204 }
1205
Eric Anholta721abf2010-08-23 10:32:01 -07001206 var->type = glsl_type::get_array_instance(var->type->fields.array,
1207 size + 1);
1208 /* FINISHME: We should update the types of array
1209 * dereferences of this variable now.
1210 */
1211 }
1212 }
1213 }
1214}
1215
Ian Romanick69846702010-06-22 17:29:19 -07001216/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001217 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001218 *
1219 * \param used_mask Bits representing used (1) and unused (0) locations
1220 * \param needed_count Number of contiguous bits needed.
1221 *
1222 * \return
1223 * Base location of the available bits on success or -1 on failure.
1224 */
1225int
1226find_available_slots(unsigned used_mask, unsigned needed_count)
1227{
1228 unsigned needed_mask = (1 << needed_count) - 1;
1229 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1230
1231 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1232 * cannot optimize possibly infinite loops" for the loop below.
1233 */
1234 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1235 return -1;
1236
1237 for (int i = 0; i <= max_bit_to_test; i++) {
1238 if ((needed_mask & ~used_mask) == needed_mask)
1239 return i;
1240
1241 needed_mask <<= 1;
1242 }
1243
1244 return -1;
1245}
1246
1247
Ian Romanickd32d4f72011-06-27 17:59:58 -07001248/**
1249 * Assign locations for either VS inputs for FS outputs
1250 *
1251 * \param prog Shader program whose variables need locations assigned
1252 * \param target_index Selector for the program target to receive location
1253 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1254 * \c MESA_SHADER_FRAGMENT.
1255 * \param max_index Maximum number of generic locations. This corresponds
1256 * to either the maximum number of draw buffers or the
1257 * maximum number of generic attributes.
1258 *
1259 * \return
1260 * If locations are successfully assigned, true is returned. Otherwise an
1261 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001262 */
Ian Romanick69846702010-06-22 17:29:19 -07001263bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001264assign_attribute_or_color_locations(gl_shader_program *prog,
1265 unsigned target_index,
1266 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001267{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001268 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001269 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001270 unsigned used_locations = (max_index >= 32)
1271 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001272
Ian Romanickd32d4f72011-06-27 17:59:58 -07001273 assert((target_index == MESA_SHADER_VERTEX)
1274 || (target_index == MESA_SHADER_FRAGMENT));
1275
1276 gl_shader *const sh = prog->_LinkedShaders[target_index];
1277 if (sh == NULL)
1278 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001279
Ian Romanick69846702010-06-22 17:29:19 -07001280 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001281 *
1282 * 1. Invalidate the location assignments for all vertex shader inputs.
1283 *
1284 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001285 * glBindVertexAttribLocation) locations and outputs that have
1286 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001287 *
Ian Romanick69846702010-06-22 17:29:19 -07001288 * 3. Sort the attributes without assigned locations by number of slots
1289 * required in decreasing order. Fragmentation caused by attribute
1290 * locations assigned by the application may prevent large attributes
1291 * from having enough contiguous space.
1292 *
1293 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001294 */
1295
Ian Romanickd32d4f72011-06-27 17:59:58 -07001296 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001297 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001298
Ian Romanickd32d4f72011-06-27 17:59:58 -07001299 const enum ir_variable_mode direction =
1300 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1301
1302
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07001303 link_invalidate_variable_locations(sh, direction, generic_base);
Ian Romanickd32d4f72011-06-27 17:59:58 -07001304
Ian Romanick69846702010-06-22 17:29:19 -07001305 /* Temporary storage for the set of attributes that need locations assigned.
1306 */
1307 struct temp_attr {
1308 unsigned slots;
1309 ir_variable *var;
1310
1311 /* Used below in the call to qsort. */
1312 static int compare(const void *a, const void *b)
1313 {
1314 const temp_attr *const l = (const temp_attr *) a;
1315 const temp_attr *const r = (const temp_attr *) b;
1316
1317 /* Reversed because we want a descending order sort below. */
1318 return r->slots - l->slots;
1319 }
1320 } to_assign[16];
1321
1322 unsigned num_attr = 0;
1323
Eric Anholt16b68b12010-06-30 11:05:43 -07001324 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001325 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1326
Brian Paul4470ff22011-07-19 21:10:25 -06001327 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001328 continue;
1329
Ian Romanick68a4fc92010-10-07 17:21:22 -07001330 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001331 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001332 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001333 linker_error(prog,
1334 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001335 (var->location < 0)
1336 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001337 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001338 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001339 }
1340 } else if (target_index == MESA_SHADER_VERTEX) {
1341 unsigned binding;
1342
1343 if (prog->AttributeBindings->get(binding, var->name)) {
1344 assert(binding >= VERT_ATTRIB_GENERIC0);
1345 var->location = binding;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001346 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001347 } else if (target_index == MESA_SHADER_FRAGMENT) {
1348 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001349 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001350
1351 if (prog->FragDataBindings->get(binding, var->name)) {
1352 assert(binding >= FRAG_RESULT_DATA0);
1353 var->location = binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001354
1355 if (prog->FragDataIndexBindings->get(index, var->name)) {
1356 var->index = index;
1357 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001358 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001359 }
1360
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001361 /* If the variable is not a built-in and has a location statically
1362 * assigned in the shader (presumably via a layout qualifier), make sure
1363 * that it doesn't collide with other assigned locations. Otherwise,
1364 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001365 */
Ian Romanick523b6112011-08-17 15:40:03 -07001366 const unsigned slots = count_attribute_slots(var->type);
1367 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001368 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001369 /* From page 61 of the OpenGL 4.0 spec:
1370 *
1371 * "LinkProgram will fail if the attribute bindings assigned
1372 * by BindAttribLocation do not leave not enough space to
1373 * assign a location for an active matrix attribute or an
1374 * active attribute array, both of which require multiple
1375 * contiguous generic attributes."
1376 *
1377 * Previous versions of the spec contain similar language but omit
1378 * the bit about attribute arrays.
1379 *
1380 * Page 61 of the OpenGL 4.0 spec also says:
1381 *
1382 * "It is possible for an application to bind more than one
1383 * attribute name to the same location. This is referred to as
1384 * aliasing. This will only work if only one of the aliased
1385 * attributes is active in the executable program, or if no
1386 * path through the shader consumes more than one attribute of
1387 * a set of attributes aliased to the same location. A link
1388 * error can occur if the linker determines that every path
1389 * through the shader consumes multiple aliased attributes,
1390 * but implementations are not required to generate an error
1391 * in this case."
1392 *
1393 * These two paragraphs are either somewhat contradictory, or I
1394 * don't fully understand one or both of them.
1395 */
1396 /* FINISHME: The code as currently written does not support
1397 * FINISHME: attribute location aliasing (see comment above).
1398 */
1399 /* Mask representing the contiguous slots that will be used by
1400 * this attribute.
1401 */
1402 const unsigned attr = var->location - generic_base;
1403 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001404
Ian Romanick523b6112011-08-17 15:40:03 -07001405 /* Generate a link error if the set of bits requested for this
1406 * attribute overlaps any previously allocated bits.
1407 */
1408 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001409 const char *const string = (target_index == MESA_SHADER_VERTEX)
1410 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001411 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001412 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001413 "available for %s `%s' %d %d %d", string,
1414 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001415 return false;
1416 }
1417
1418 used_locations |= (use_mask << attr);
1419 }
1420
1421 continue;
1422 }
1423
1424 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001425 to_assign[num_attr].var = var;
1426 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001427 }
Ian Romanick69846702010-06-22 17:29:19 -07001428
1429 /* If all of the attributes were assigned locations by the application (or
1430 * are built-in attributes with fixed locations), return early. This should
1431 * be the common case.
1432 */
1433 if (num_attr == 0)
1434 return true;
1435
1436 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1437
Ian Romanickd32d4f72011-06-27 17:59:58 -07001438 if (target_index == MESA_SHADER_VERTEX) {
1439 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1440 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1441 * reserved to prevent it from being automatically allocated below.
1442 */
1443 find_deref_visitor find("gl_Vertex");
1444 find.run(sh->ir);
1445 if (find.variable_found())
1446 used_locations |= (1 << 0);
1447 }
Ian Romanick982e3792010-06-29 18:58:20 -07001448
Ian Romanick69846702010-06-22 17:29:19 -07001449 for (unsigned i = 0; i < num_attr; i++) {
1450 /* Mask representing the contiguous slots that will be used by this
1451 * attribute.
1452 */
1453 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1454
1455 int location = find_available_slots(used_locations, to_assign[i].slots);
1456
1457 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001458 const char *const string = (target_index == MESA_SHADER_VERTEX)
1459 ? "vertex shader input" : "fragment shader output";
1460
Ian Romanick586e7412011-07-28 14:04:09 -07001461 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001462 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001463 "available for %s `%s'",
1464 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001465 return false;
1466 }
1467
Ian Romanickd32d4f72011-06-27 17:59:58 -07001468 to_assign[i].var->location = generic_base + location;
Ian Romanick69846702010-06-22 17:29:19 -07001469 used_locations |= (use_mask << location);
1470 }
1471
1472 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001473}
1474
1475
Ian Romanick40e114b2010-08-17 14:55:50 -07001476/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001477 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001478 */
1479void
Ian Romanickcc90e622010-10-19 17:59:10 -07001480demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001481{
1482 foreach_list(node, sh->ir) {
1483 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1484
Ian Romanickcc90e622010-10-19 17:59:10 -07001485 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001486 continue;
1487
Ian Romanickcc90e622010-10-19 17:59:10 -07001488 /* A shader 'in' or 'out' variable is only really an input or output if
1489 * its value is used by other shader stages. This will cause the variable
1490 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001491 */
1492 if (var->location == -1) {
1493 var->mode = ir_var_auto;
1494 }
1495 }
1496}
1497
1498
Paul Berry871ddb92011-11-05 11:17:32 -07001499/**
1500 * Data structure tracking information about a transform feedback declaration
1501 * during linking.
1502 */
1503class tfeedback_decl
1504{
1505public:
Paul Berry456279b2011-12-26 19:39:25 -08001506 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1507 const void *mem_ctx, const char *input);
Paul Berry871ddb92011-11-05 11:17:32 -07001508 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1509 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1510 ir_variable *output_var);
Christoph Bumillerd540af52012-01-20 13:24:46 +01001511 bool accumulate_num_outputs(struct gl_shader_program *prog, unsigned *count);
Paul Berryd3150eb2012-01-09 11:25:14 -08001512 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001513 struct gl_transform_feedback_info *info, unsigned buffer,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001514 const unsigned max_outputs) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001515
1516 /**
1517 * True if assign_location() has been called for this object.
1518 */
1519 bool is_assigned() const
1520 {
1521 return this->location != -1;
1522 }
1523
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001524 bool is_next_buffer_separator() const
1525 {
1526 return this->next_buffer_separator;
1527 }
1528
1529 bool is_varying() const
1530 {
1531 return !this->next_buffer_separator && !this->skip_components;
1532 }
1533
Paul Berry871ddb92011-11-05 11:17:32 -07001534 /**
1535 * Determine whether this object refers to the variable var.
1536 */
1537 bool matches_var(ir_variable *var) const
1538 {
Paul Berry642e5b412012-01-04 13:57:52 -08001539 if (this->is_clip_distance_mesa)
1540 return strcmp(var->name, "gl_ClipDistanceMESA") == 0;
1541 else
1542 return strcmp(var->name, this->var_name) == 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001543 }
1544
1545 /**
1546 * The total number of varying components taken up by this variable. Only
1547 * valid if is_assigned() is true.
1548 */
1549 unsigned num_components() const
1550 {
Paul Berry642e5b412012-01-04 13:57:52 -08001551 if (this->is_clip_distance_mesa)
1552 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001553 else
Paul Berry642e5b412012-01-04 13:57:52 -08001554 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001555 }
1556
1557private:
1558 /**
1559 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001560 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001561 */
1562 const char *orig_name;
1563
1564 /**
1565 * The name of the variable, parsed from orig_name.
1566 */
Paul Berry913a5c22011-12-27 08:24:57 -08001567 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001568
1569 /**
1570 * True if the declaration in orig_name represents an array.
1571 */
Paul Berry33fe0212012-01-03 20:41:34 -08001572 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001573
1574 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001575 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001576 */
Paul Berry33fe0212012-01-03 20:41:34 -08001577 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001578
1579 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001580 * True if the variable is gl_ClipDistance and the driver lowers
1581 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001582 */
Paul Berry642e5b412012-01-04 13:57:52 -08001583 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001584
1585 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001586 * The vertex shader output location that the linker assigned for this
1587 * variable. -1 if a location hasn't been assigned yet.
1588 */
1589 int location;
1590
1591 /**
1592 * If location != -1, the number of vector elements in this variable, or 1
1593 * if this variable is a scalar.
1594 */
1595 unsigned vector_elements;
1596
1597 /**
1598 * If location != -1, the number of matrix columns in this variable, or 1
1599 * if this variable is not a matrix.
1600 */
1601 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001602
1603 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1604 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001605
1606 /**
1607 * If location != -1, the size that should be returned by
1608 * glGetTransformFeedbackVarying().
1609 */
1610 unsigned size;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001611
1612 /**
1613 * How many components to skip. If non-zero, this is
1614 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1615 */
1616 unsigned skip_components;
1617
1618 /**
1619 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1620 */
1621 bool next_buffer_separator;
Paul Berry871ddb92011-11-05 11:17:32 -07001622};
1623
1624
1625/**
1626 * Initialize this object based on a string that was passed to
1627 * glTransformFeedbackVaryings. If there is a parse error, the error is
1628 * reported using linker_error(), and false is returned.
1629 */
1630bool
Paul Berry456279b2011-12-26 19:39:25 -08001631tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1632 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001633{
1634 /* We don't have to be pedantic about what is a valid GLSL variable name,
1635 * because any variable with an invalid name can't exist in the IR anyway.
1636 */
1637
1638 this->location = -1;
1639 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001640 this->is_clip_distance_mesa = false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001641 this->skip_components = 0;
1642 this->next_buffer_separator = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001643
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001644 if (ctx->Extensions.ARB_transform_feedback3) {
1645 /* Parse gl_NextBuffer. */
1646 if (strcmp(input, "gl_NextBuffer") == 0) {
1647 this->next_buffer_separator = true;
1648 return true;
1649 }
1650
1651 /* Parse gl_SkipComponents. */
1652 if (strcmp(input, "gl_SkipComponents1") == 0)
1653 this->skip_components = 1;
1654 else if (strcmp(input, "gl_SkipComponents2") == 0)
1655 this->skip_components = 2;
1656 else if (strcmp(input, "gl_SkipComponents3") == 0)
1657 this->skip_components = 3;
1658 else if (strcmp(input, "gl_SkipComponents4") == 0)
1659 this->skip_components = 4;
1660
1661 if (this->skip_components)
1662 return true;
1663 }
1664
1665 /* Parse a declaration. */
Paul Berry871ddb92011-11-05 11:17:32 -07001666 const char *bracket = strrchr(input, '[');
1667
1668 if (bracket) {
1669 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001670 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001671 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1672 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001673 }
Paul Berry33fe0212012-01-03 20:41:34 -08001674 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001675 } else {
1676 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001677 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001678 }
1679
Paul Berry642e5b412012-01-04 13:57:52 -08001680 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1681 * class must behave specially to account for the fact that gl_ClipDistance
1682 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001683 */
1684 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1685 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001686 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001687 }
1688
1689 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001690}
1691
1692
1693/**
1694 * Determine whether two tfeedback_decl objects refer to the same variable and
1695 * array index (if applicable).
1696 */
1697bool
1698tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1699{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001700 assert(x.is_varying() && y.is_varying());
1701
Paul Berry871ddb92011-11-05 11:17:32 -07001702 if (strcmp(x.var_name, y.var_name) != 0)
1703 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001704 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001705 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001706 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001707 return false;
1708 return true;
1709}
1710
1711
1712/**
1713 * Assign a location for this tfeedback_decl object based on the location
1714 * assignment in output_var.
1715 *
1716 * If an error occurs, the error is reported through linker_error() and false
1717 * is returned.
1718 */
1719bool
1720tfeedback_decl::assign_location(struct gl_context *ctx,
1721 struct gl_shader_program *prog,
1722 ir_variable *output_var)
1723{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001724 assert(this->is_varying());
1725
Paul Berry871ddb92011-11-05 11:17:32 -07001726 if (output_var->type->is_array()) {
1727 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001728 const unsigned matrix_cols =
1729 output_var->type->fields.array->matrix_columns;
Paul Berry642e5b412012-01-04 13:57:52 -08001730 unsigned actual_array_size = this->is_clip_distance_mesa ?
1731 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001732
1733 if (this->is_subscripted) {
1734 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001735 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001736 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001737 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001738 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001739 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001740 return false;
1741 }
Paul Berry642e5b412012-01-04 13:57:52 -08001742 if (this->is_clip_distance_mesa) {
1743 this->location =
1744 output_var->location + this->array_subscript / 4;
1745 } else {
1746 this->location =
1747 output_var->location + this->array_subscript * matrix_cols;
1748 }
Paul Berry33fe0212012-01-03 20:41:34 -08001749 this->size = 1;
1750 } else {
1751 this->location = output_var->location;
Paul Berry642e5b412012-01-04 13:57:52 -08001752 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001753 }
Paul Berry871ddb92011-11-05 11:17:32 -07001754 this->vector_elements = output_var->type->fields.array->vector_elements;
1755 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001756 if (this->is_clip_distance_mesa)
1757 this->type = GL_FLOAT;
1758 else
1759 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001760 } else {
1761 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001762 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001763 linker_error(prog, "Transform feedback varying %s requested, "
1764 "but %s is not an array.",
1765 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001766 return false;
1767 }
1768 this->location = output_var->location;
Paul Berry33fe0212012-01-03 20:41:34 -08001769 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001770 this->vector_elements = output_var->type->vector_elements;
1771 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001772 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001773 }
Paul Berry642e5b412012-01-04 13:57:52 -08001774
Paul Berry871ddb92011-11-05 11:17:32 -07001775 /* From GL_EXT_transform_feedback:
1776 * A program will fail to link if:
1777 *
1778 * * the total number of components to capture in any varying
1779 * variable in <varyings> is greater than the constant
1780 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1781 * buffer mode is SEPARATE_ATTRIBS_EXT;
1782 */
1783 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1784 this->num_components() >
1785 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1786 linker_error(prog, "Transform feedback varying %s exceeds "
1787 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1788 this->orig_name);
1789 return false;
1790 }
1791
1792 return true;
1793}
1794
1795
Paul Berry871ddb92011-11-05 11:17:32 -07001796bool
Christoph Bumillerd540af52012-01-20 13:24:46 +01001797tfeedback_decl::accumulate_num_outputs(struct gl_shader_program *prog,
1798 unsigned *count)
Paul Berry871ddb92011-11-05 11:17:32 -07001799{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001800 if (!this->is_varying()) {
1801 return true;
1802 }
1803
Paul Berry871ddb92011-11-05 11:17:32 -07001804 if (!this->is_assigned()) {
1805 /* From GL_EXT_transform_feedback:
1806 * A program will fail to link if:
1807 *
1808 * * any variable name specified in the <varyings> array is not
1809 * declared as an output in the geometry shader (if present) or
1810 * the vertex shader (if no geometry shader is present);
1811 */
1812 linker_error(prog, "Transform feedback varying %s undeclared.",
1813 this->orig_name);
1814 return false;
1815 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001816
Christoph Bumillerd540af52012-01-20 13:24:46 +01001817 unsigned translated_size = this->size;
1818 if (this->is_clip_distance_mesa)
1819 translated_size = (translated_size + 3) / 4;
1820
1821 *count += translated_size * this->matrix_columns;
1822
1823 return true;
1824}
1825
1826
1827/**
1828 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1829 *
1830 * If an error occurs, the error is reported through linker_error() and false
1831 * is returned.
1832 */
1833bool
1834tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1835 struct gl_transform_feedback_info *info,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001836 unsigned buffer, const unsigned max_outputs) const
Christoph Bumillerd540af52012-01-20 13:24:46 +01001837{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001838 assert(!this->next_buffer_separator);
1839
1840 /* Handle gl_SkipComponents. */
1841 if (this->skip_components) {
1842 info->BufferStride[buffer] += this->skip_components;
1843 return true;
1844 }
1845
Paul Berryd3150eb2012-01-09 11:25:14 -08001846 /* From GL_EXT_transform_feedback:
1847 * A program will fail to link if:
1848 *
1849 * * the total number of components to capture is greater than
1850 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1851 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1852 */
1853 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1854 info->BufferStride[buffer] + this->num_components() >
1855 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1856 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1857 "limit has been exceeded.");
1858 return false;
1859 }
1860
Paul Berry642e5b412012-01-04 13:57:52 -08001861 unsigned translated_size = this->size;
1862 if (this->is_clip_distance_mesa)
1863 translated_size = (translated_size + 3) / 4;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001864 unsigned components_so_far = 0;
Paul Berry642e5b412012-01-04 13:57:52 -08001865 for (unsigned index = 0; index < translated_size; ++index) {
Paul Berry33fe0212012-01-03 20:41:34 -08001866 for (unsigned v = 0; v < this->matrix_columns; ++v) {
Paul Berry642e5b412012-01-04 13:57:52 -08001867 unsigned num_components = this->vector_elements;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001868 assert(info->NumOutputs < max_outputs);
Paul Berry642e5b412012-01-04 13:57:52 -08001869 info->Outputs[info->NumOutputs].ComponentOffset = 0;
1870 if (this->is_clip_distance_mesa) {
1871 if (this->is_subscripted) {
1872 num_components = 1;
1873 info->Outputs[info->NumOutputs].ComponentOffset =
1874 this->array_subscript % 4;
1875 } else {
1876 num_components = MIN2(4, this->size - components_so_far);
1877 }
1878 }
Paul Berry33fe0212012-01-03 20:41:34 -08001879 info->Outputs[info->NumOutputs].OutputRegister =
1880 this->location + v + index * this->matrix_columns;
1881 info->Outputs[info->NumOutputs].NumComponents = num_components;
1882 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1883 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
Paul Berry33fe0212012-01-03 20:41:34 -08001884 ++info->NumOutputs;
1885 info->BufferStride[buffer] += num_components;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001886 components_so_far += num_components;
Paul Berry33fe0212012-01-03 20:41:34 -08001887 }
Paul Berry871ddb92011-11-05 11:17:32 -07001888 }
Paul Berrybe4e9f72012-01-04 12:21:55 -08001889 assert(components_so_far == this->num_components());
Eric Anholt9d36c962012-01-02 17:08:13 -08001890
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001891 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1892 info->Varyings[info->NumVarying].Type = this->type;
1893 info->Varyings[info->NumVarying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001894 info->NumVarying++;
1895
Paul Berry871ddb92011-11-05 11:17:32 -07001896 return true;
1897}
1898
1899
1900/**
1901 * Parse all the transform feedback declarations that were passed to
1902 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1903 *
1904 * If an error occurs, the error is reported through linker_error() and false
1905 * is returned.
1906 */
1907static bool
Paul Berry456279b2011-12-26 19:39:25 -08001908parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1909 const void *mem_ctx, unsigned num_names,
1910 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001911{
1912 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001913 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001914 return false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001915
1916 if (!decls[i].is_varying())
1917 continue;
1918
Paul Berry871ddb92011-11-05 11:17:32 -07001919 /* From GL_EXT_transform_feedback:
1920 * A program will fail to link if:
1921 *
1922 * * any two entries in the <varyings> array specify the same varying
1923 * variable;
1924 *
1925 * We interpret this to mean "any two entries in the <varyings> array
1926 * specify the same varying variable and array index", since transform
1927 * feedback of arrays would be useless otherwise.
1928 */
1929 for (unsigned j = 0; j < i; ++j) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001930 if (!decls[j].is_varying())
1931 continue;
1932
Paul Berry871ddb92011-11-05 11:17:32 -07001933 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1934 linker_error(prog, "Transform feedback varying %s specified "
1935 "more than once.", varying_names[i]);
1936 return false;
1937 }
1938 }
1939 }
1940 return true;
1941}
1942
1943
1944/**
1945 * Assign a location for a variable that is produced in one pipeline stage
1946 * (the "producer") and consumed in the next stage (the "consumer").
1947 *
1948 * \param input_var is the input variable declaration in the consumer.
1949 *
1950 * \param output_var is the output variable declaration in the producer.
1951 *
1952 * \param input_index is the counter that keeps track of assigned input
1953 * locations in the consumer.
1954 *
1955 * \param output_index is the counter that keeps track of assigned output
1956 * locations in the producer.
1957 *
1958 * It is permissible for \c input_var to be NULL (this happens if a variable
1959 * is output by the producer and consumed by transform feedback, but not
1960 * consumed by the consumer).
1961 *
1962 * If the variable has already been assigned a location, this function has no
1963 * effect.
1964 */
1965void
1966assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1967 unsigned *input_index, unsigned *output_index)
1968{
1969 if (output_var->location != -1) {
1970 /* Location already assigned. */
1971 return;
1972 }
1973
1974 if (input_var) {
1975 assert(input_var->location == -1);
1976 input_var->location = *input_index;
1977 }
1978
1979 output_var->location = *output_index;
1980
1981 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1982 assert(!output_var->type->is_record());
1983
1984 if (output_var->type->is_array()) {
1985 const unsigned slots = output_var->type->length
1986 * output_var->type->fields.array->matrix_columns;
1987
1988 *output_index += slots;
1989 *input_index += slots;
1990 } else {
1991 const unsigned slots = output_var->type->matrix_columns;
1992
1993 *output_index += slots;
1994 *input_index += slots;
1995 }
1996}
1997
1998
1999/**
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002000 * Is the given variable a varying variable to be counted against the
2001 * limit in ctx->Const.MaxVarying?
2002 * This includes variables such as texcoords, colors and generic
2003 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2004 */
2005static bool
2006is_varying_var(GLenum shaderType, const ir_variable *var)
2007{
2008 /* Only fragment shaders will take a varying variable as an input */
2009 if (shaderType == GL_FRAGMENT_SHADER &&
2010 var->mode == ir_var_in &&
2011 var->explicit_location) {
2012 switch (var->location) {
2013 case FRAG_ATTRIB_WPOS:
2014 case FRAG_ATTRIB_FACE:
2015 case FRAG_ATTRIB_PNTC:
2016 return false;
2017 default:
2018 return true;
2019 }
2020 }
2021 return false;
2022}
2023
2024
2025/**
Paul Berry871ddb92011-11-05 11:17:32 -07002026 * Assign locations for all variables that are produced in one pipeline stage
2027 * (the "producer") and consumed in the next stage (the "consumer").
2028 *
2029 * Variables produced by the producer may also be consumed by transform
2030 * feedback.
2031 *
2032 * \param num_tfeedback_decls is the number of declarations indicating
2033 * variables that may be consumed by transform feedback.
2034 *
2035 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2036 * representing the result of parsing the strings passed to
2037 * glTransformFeedbackVaryings(). assign_location() will be called for
2038 * each of these objects that matches one of the outputs of the
2039 * producer.
2040 *
2041 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2042 * be NULL. In this case, varying locations are assigned solely based on the
2043 * requirements of transform feedback.
2044 */
Ian Romanickde773242011-06-09 13:31:32 -07002045bool
2046assign_varying_locations(struct gl_context *ctx,
2047 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07002048 gl_shader *producer, gl_shader *consumer,
2049 unsigned num_tfeedback_decls,
2050 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07002051{
2052 /* FINISHME: Set dynamically when geometry shader support is added. */
2053 unsigned output_index = VERT_RESULT_VAR0;
2054 unsigned input_index = FRAG_ATTRIB_VAR0;
2055
2056 /* Operate in a total of three passes.
2057 *
2058 * 1. Assign locations for any matching inputs and outputs.
2059 *
2060 * 2. Mark output variables in the producer that do not have locations as
2061 * not being outputs. This lets the optimizer eliminate them.
2062 *
2063 * 3. Mark input variables in the consumer that do not have locations as
2064 * not being inputs. This lets the optimizer eliminate them.
2065 */
2066
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07002067 link_invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
Paul Berry871ddb92011-11-05 11:17:32 -07002068 if (consumer)
2069 link_invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
Ian Romanick0e59b262010-06-23 11:23:01 -07002070
Eric Anholt16b68b12010-06-30 11:05:43 -07002071 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07002072 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2073
Paul Berry871ddb92011-11-05 11:17:32 -07002074 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07002075 continue;
2076
Paul Berry871ddb92011-11-05 11:17:32 -07002077 ir_variable *input_var =
2078 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002079
Paul Berry871ddb92011-11-05 11:17:32 -07002080 if (input_var && input_var->mode != ir_var_in)
2081 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002082
Paul Berry871ddb92011-11-05 11:17:32 -07002083 if (input_var) {
2084 assign_varying_location(input_var, output_var, &input_index,
2085 &output_index);
2086 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002087
Paul Berry871ddb92011-11-05 11:17:32 -07002088 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002089 if (!tfeedback_decls[i].is_varying())
2090 continue;
2091
Paul Berry871ddb92011-11-05 11:17:32 -07002092 if (!tfeedback_decls[i].is_assigned() &&
2093 tfeedback_decls[i].matches_var(output_var)) {
2094 if (output_var->location == -1) {
2095 assign_varying_location(input_var, output_var, &input_index,
2096 &output_index);
2097 }
2098 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2099 return false;
2100 }
Ian Romanickdf869d92010-08-30 15:37:44 -07002101 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002102 }
2103
Ian Romanickde773242011-06-09 13:31:32 -07002104 unsigned varying_vectors = 0;
2105
Paul Berry871ddb92011-11-05 11:17:32 -07002106 if (consumer) {
2107 foreach_list(node, consumer->ir) {
2108 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07002109
Paul Berry871ddb92011-11-05 11:17:32 -07002110 if ((var == NULL) || (var->mode != ir_var_in))
2111 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07002112
Paul Berry871ddb92011-11-05 11:17:32 -07002113 if (var->location == -1) {
2114 if (prog->Version <= 120) {
2115 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2116 *
2117 * Only those varying variables used (i.e. read) in
2118 * the fragment shader executable must be written to
2119 * by the vertex shader executable; declaring
2120 * superfluous varying variables in a vertex shader is
2121 * permissible.
2122 *
2123 * We interpret this text as meaning that the VS must
2124 * write the variable for the FS to read it. See
2125 * "glsl1-varying read but not written" in piglit.
2126 */
Eric Anholtb7062832010-07-28 13:52:23 -07002127
Paul Berry871ddb92011-11-05 11:17:32 -07002128 linker_error(prog, "fragment shader varying %s not written "
2129 "by vertex shader\n.", var->name);
2130 }
Eric Anholtb7062832010-07-28 13:52:23 -07002131
Paul Berry871ddb92011-11-05 11:17:32 -07002132 /* An 'in' variable is only really a shader input if its
2133 * value is written by the previous stage.
2134 */
2135 var->mode = ir_var_auto;
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002136 } else if (is_varying_var(consumer->Type, var)) {
Paul Berry871ddb92011-11-05 11:17:32 -07002137 /* The packing rules are used for vertex shader inputs are also
2138 * used for fragment shader inputs.
2139 */
2140 varying_vectors += count_attribute_slots(var->type);
2141 }
Eric Anholtb7062832010-07-28 13:52:23 -07002142 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002143 }
Ian Romanickde773242011-06-09 13:31:32 -07002144
2145 if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
2146 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002147 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2148 linker_warning(prog, "shader uses too many varying vectors "
2149 "(%u > %u), but the driver will try to optimize "
2150 "them out; this is non-portable out-of-spec "
2151 "behavior\n",
2152 varying_vectors, ctx->Const.MaxVarying);
2153 } else {
2154 linker_error(prog, "shader uses too many varying vectors "
2155 "(%u > %u)\n",
2156 varying_vectors, ctx->Const.MaxVarying);
2157 return false;
2158 }
Ian Romanickde773242011-06-09 13:31:32 -07002159 }
2160 } else {
2161 const unsigned float_components = varying_vectors * 4;
2162 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002163 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2164 linker_warning(prog, "shader uses too many varying components "
2165 "(%u > %u), but the driver will try to optimize "
2166 "them out; this is non-portable out-of-spec "
2167 "behavior\n",
2168 float_components, ctx->Const.MaxVarying * 4);
2169 } else {
2170 linker_error(prog, "shader uses too many varying components "
2171 "(%u > %u)\n",
2172 float_components, ctx->Const.MaxVarying * 4);
2173 return false;
2174 }
Ian Romanickde773242011-06-09 13:31:32 -07002175 }
2176 }
2177
2178 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07002179}
2180
2181
Paul Berry871ddb92011-11-05 11:17:32 -07002182/**
2183 * Store transform feedback location assignments into
2184 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2185 *
2186 * If an error occurs, the error is reported through linker_error() and false
2187 * is returned.
2188 */
2189static bool
2190store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2191 unsigned num_tfeedback_decls,
2192 tfeedback_decl *tfeedback_decls)
2193{
Paul Berryebfad9f2011-12-29 15:55:01 -08002194 bool separate_attribs_mode =
2195 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08002196
2197 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01002198 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08002199
2200 memset(&prog->LinkedTransformFeedback, 0,
2201 sizeof(prog->LinkedTransformFeedback));
2202
2203 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08002204 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08002205 struct gl_transform_feedback_varying_info,
2206 num_tfeedback_decls);
2207
Christoph Bumillerd540af52012-01-20 13:24:46 +01002208 unsigned num_outputs = 0;
2209 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
2210 if (!tfeedback_decls[i].accumulate_num_outputs(prog, &num_outputs))
2211 return false;
2212
2213 prog->LinkedTransformFeedback.Outputs =
2214 rzalloc_array(prog,
2215 struct gl_transform_feedback_output,
2216 num_outputs);
2217
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002218 unsigned num_buffers = 0;
2219
2220 if (separate_attribs_mode) {
2221 /* GL_SEPARATE_ATTRIBS */
2222 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2223 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2224 num_buffers, num_outputs))
2225 return false;
2226
2227 num_buffers++;
2228 }
Paul Berry871ddb92011-11-05 11:17:32 -07002229 }
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002230 else {
2231 /* GL_INVERLEAVED_ATTRIBS */
2232 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2233 if (tfeedback_decls[i].is_next_buffer_separator()) {
2234 num_buffers++;
2235 continue;
2236 }
2237
2238 if (!tfeedback_decls[i].store(ctx, prog,
2239 &prog->LinkedTransformFeedback,
2240 num_buffers, num_outputs))
2241 return false;
2242 }
2243 num_buffers++;
2244 }
2245
Christoph Bumillerd540af52012-01-20 13:24:46 +01002246 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002247
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002248 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
Paul Berry871ddb92011-11-05 11:17:32 -07002249 return true;
2250}
2251
Ian Romanick92f81592011-11-08 12:37:19 -08002252/**
Marek Olšákec174a42011-11-18 15:00:10 +01002253 * Store the gl_FragDepth layout in the gl_shader_program struct.
2254 */
2255static void
2256store_fragdepth_layout(struct gl_shader_program *prog)
2257{
2258 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2259 return;
2260 }
2261
2262 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2263
2264 /* We don't look up the gl_FragDepth symbol directly because if
2265 * gl_FragDepth is not used in the shader, it's removed from the IR.
2266 * However, the symbol won't be removed from the symbol table.
2267 *
2268 * We're only interested in the cases where the variable is NOT removed
2269 * from the IR.
2270 */
2271 foreach_list(node, ir) {
2272 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2273
2274 if (var == NULL || var->mode != ir_var_out) {
2275 continue;
2276 }
2277
2278 if (strcmp(var->name, "gl_FragDepth") == 0) {
2279 switch (var->depth_layout) {
2280 case ir_depth_layout_none:
2281 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2282 return;
2283 case ir_depth_layout_any:
2284 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2285 return;
2286 case ir_depth_layout_greater:
2287 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2288 return;
2289 case ir_depth_layout_less:
2290 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2291 return;
2292 case ir_depth_layout_unchanged:
2293 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2294 return;
2295 default:
2296 assert(0);
2297 return;
2298 }
2299 }
2300 }
2301}
2302
2303/**
Ian Romanick92f81592011-11-08 12:37:19 -08002304 * Validate the resources used by a program versus the implementation limits
2305 */
2306static bool
2307check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2308{
2309 static const char *const shader_names[MESA_SHADER_TYPES] = {
2310 "vertex", "fragment", "geometry"
2311 };
2312
2313 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2314 ctx->Const.MaxVertexTextureImageUnits,
2315 ctx->Const.MaxTextureImageUnits,
2316 ctx->Const.MaxGeometryTextureImageUnits
2317 };
2318
2319 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2320 ctx->Const.VertexProgram.MaxUniformComponents,
2321 ctx->Const.FragmentProgram.MaxUniformComponents,
2322 0 /* FINISHME: Geometry shaders. */
2323 };
2324
2325 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2326 struct gl_shader *sh = prog->_LinkedShaders[i];
2327
2328 if (sh == NULL)
2329 continue;
2330
2331 if (sh->num_samplers > max_samplers[i]) {
2332 linker_error(prog, "Too many %s shader texture samplers",
2333 shader_names[i]);
2334 }
2335
2336 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002337 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2338 linker_warning(prog, "Too many %s shader uniform components, "
2339 "but the driver will try to optimize them out; "
2340 "this is non-portable out-of-spec behavior\n",
2341 shader_names[i]);
2342 } else {
2343 linker_error(prog, "Too many %s shader uniform components",
2344 shader_names[i]);
2345 }
Ian Romanick92f81592011-11-08 12:37:19 -08002346 }
2347 }
2348
2349 return prog->LinkStatus;
2350}
Paul Berry871ddb92011-11-05 11:17:32 -07002351
Ian Romanick0e59b262010-06-23 11:23:01 -07002352void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002353link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002354{
Paul Berry871ddb92011-11-05 11:17:32 -07002355 tfeedback_decl *tfeedback_decls = NULL;
2356 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2357
Kenneth Graunked3073f52011-01-21 14:32:31 -08002358 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002359
Ian Romanick832dfa52010-06-17 15:04:20 -07002360 prog->LinkStatus = false;
2361 prog->Validated = false;
2362 prog->_Used = false;
2363
Eric Anholtf609cf72012-04-27 13:52:56 -07002364 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08002365 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002366
Eric Anholtf609cf72012-04-27 13:52:56 -07002367 ralloc_free(prog->UniformBlocks);
2368 prog->UniformBlocks = NULL;
2369 prog->NumUniformBlocks = 0;
2370 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2371 ralloc_free(prog->UniformBlockStageIndex[i]);
2372 prog->UniformBlockStageIndex[i] = NULL;
2373 }
2374
Ian Romanick832dfa52010-06-17 15:04:20 -07002375 /* Separate the shaders into groups based on their type.
2376 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002377 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002378 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002379 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002380 unsigned num_frag_shaders = 0;
2381
Eric Anholt16b68b12010-06-30 11:05:43 -07002382 vert_shader_list = (struct gl_shader **)
2383 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002384 frag_shader_list = &vert_shader_list[prog->NumShaders];
2385
Ian Romanick25f51d32010-07-16 15:51:50 -07002386 unsigned min_version = UINT_MAX;
2387 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07002388 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002389 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2390 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2391
Ian Romanick832dfa52010-06-17 15:04:20 -07002392 switch (prog->Shaders[i]->Type) {
2393 case GL_VERTEX_SHADER:
2394 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2395 num_vert_shaders++;
2396 break;
2397 case GL_FRAGMENT_SHADER:
2398 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2399 num_frag_shaders++;
2400 break;
2401 case GL_GEOMETRY_SHADER:
2402 /* FINISHME: Support geometry shaders. */
2403 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2404 break;
2405 }
2406 }
2407
Ian Romanick25f51d32010-07-16 15:51:50 -07002408 /* Previous to GLSL version 1.30, different compilation units could mix and
2409 * match shading language versions. With GLSL 1.30 and later, the versions
2410 * of all shaders must match.
2411 */
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002412 assert(min_version >= 100);
Eric Anholtc5ff9a82012-03-08 13:49:15 -08002413 assert(max_version <= 140);
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002414 if ((max_version >= 130 || min_version == 100)
2415 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002416 linker_error(prog, "all shaders must use same shading "
2417 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002418 goto done;
2419 }
2420
2421 prog->Version = max_version;
2422
Ian Romanick3322fba2010-10-14 13:28:42 -07002423 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2424 if (prog->_LinkedShaders[i] != NULL)
2425 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2426
2427 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002428 }
2429
Ian Romanickcd6764e2010-07-16 16:00:07 -07002430 /* Link all shaders for a particular stage and validate the result.
2431 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002432 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002433 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002434 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2435 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002436
2437 if (sh == NULL)
2438 goto done;
2439
2440 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002441 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002442
Ian Romanick3322fba2010-10-14 13:28:42 -07002443 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2444 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002445 }
2446
2447 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002448 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002449 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2450 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002451
2452 if (sh == NULL)
2453 goto done;
2454
2455 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002456 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002457
Ian Romanick3322fba2010-10-14 13:28:42 -07002458 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2459 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002460 }
2461
Ian Romanick3ed850e2010-06-23 12:18:21 -07002462 /* Here begins the inter-stage linking phase. Some initial validation is
2463 * performed, then locations are assigned for uniforms, attributes, and
2464 * varyings.
2465 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002466 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002467 unsigned prev;
2468
2469 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2470 if (prog->_LinkedShaders[prev] != NULL)
2471 break;
2472 }
2473
Bryan Cainf18a0862011-04-23 19:29:15 -05002474 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002475 * stage.
2476 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002477 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2478 if (prog->_LinkedShaders[i] == NULL)
2479 continue;
2480
Ian Romanickf36460e2010-06-23 12:07:22 -07002481 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002482 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002483 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002484 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002485
2486 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002487 }
2488
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002489 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002490 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002491
Eric Anholt3de13952012-05-04 13:08:46 -07002492 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2493 * it before optimization because we want most of the checks to get
2494 * dropped thanks to constant propagation.
2495 */
2496 if (max_version >= 130) {
2497 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2498 if (sh) {
2499 lower_discard_flow(sh->ir);
2500 }
2501 }
2502
Eric Anholtf609cf72012-04-27 13:52:56 -07002503 if (!interstage_cross_validate_uniform_blocks(prog))
2504 goto done;
2505
Eric Anholt2f4fe152010-08-10 13:06:49 -07002506 /* Do common optimization before assigning storage for attributes,
2507 * uniforms, and varyings. Later optimization could possibly make
2508 * some of that unused.
2509 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002510 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2511 if (prog->_LinkedShaders[i] == NULL)
2512 continue;
2513
Ian Romanick02c5ae12011-07-11 10:46:01 -07002514 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2515 if (!prog->LinkStatus)
2516 goto done;
2517
Paul Berryc06e3252011-08-11 20:58:21 -07002518 if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
2519 lower_clip_distance(prog->_LinkedShaders[i]->ir);
2520
Brian Paul7feabfe2012-03-20 17:43:12 -06002521 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2522
2523 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002524 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002525 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002526
Ian Romanickd32d4f72011-06-27 17:59:58 -07002527 /* FINISHME: The value of the max_attribute_index parameter is
2528 * FINISHME: implementation dependent based on the value of
2529 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2530 * FINISHME: at least 16, so hardcode 16 for now.
2531 */
2532 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002533 goto done;
2534 }
2535
Dave Airlie1256a5d2012-03-24 13:33:41 +00002536 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002537 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002538 }
2539
Ian Romanick3322fba2010-10-14 13:28:42 -07002540 unsigned prev;
2541 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2542 if (prog->_LinkedShaders[prev] != NULL)
2543 break;
2544 }
2545
Paul Berry871ddb92011-11-05 11:17:32 -07002546 if (num_tfeedback_decls != 0) {
2547 /* From GL_EXT_transform_feedback:
2548 * A program will fail to link if:
2549 *
2550 * * the <count> specified by TransformFeedbackVaryingsEXT is
2551 * non-zero, but the program object has no vertex or geometry
2552 * shader;
2553 */
2554 if (prev >= MESA_SHADER_FRAGMENT) {
2555 linker_error(prog, "Transform feedback varyings specified, but "
2556 "no vertex or geometry shader is present.");
2557 goto done;
2558 }
2559
2560 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2561 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002562 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002563 prog->TransformFeedback.VaryingNames,
2564 tfeedback_decls))
2565 goto done;
2566 }
2567
Ian Romanick3322fba2010-10-14 13:28:42 -07002568 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2569 if (prog->_LinkedShaders[i] == NULL)
2570 continue;
2571
Paul Berry871ddb92011-11-05 11:17:32 -07002572 if (!assign_varying_locations(
2573 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2574 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2575 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002576 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002577
Ian Romanick3322fba2010-10-14 13:28:42 -07002578 prev = i;
2579 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002580
Paul Berry871ddb92011-11-05 11:17:32 -07002581 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2582 /* There was no fragment shader, but we still have to assign varying
2583 * locations for use by transform feedback.
2584 */
2585 if (!assign_varying_locations(
2586 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2587 tfeedback_decls))
2588 goto done;
2589 }
2590
2591 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2592 goto done;
2593
Ian Romanickcc90e622010-10-19 17:59:10 -07002594 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2595 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2596 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002597
2598 /* Eliminate code that is now dead due to unused vertex outputs being
2599 * demoted.
2600 */
2601 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2602 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002603 }
2604
2605 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2606 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2607
2608 demote_shader_inputs_and_outputs(sh, ir_var_in);
2609 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2610 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002611
2612 /* Eliminate code that is now dead due to unused geometry outputs being
2613 * demoted.
2614 */
2615 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2616 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002617 }
2618
2619 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2620 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2621
2622 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002623
2624 /* Eliminate code that is now dead due to unused fragment inputs being
2625 * demoted. This shouldn't actually do anything other than remove
2626 * declarations of the (now unused) global variables.
2627 */
2628 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2629 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002630 }
2631
Ian Romanick960d7222011-10-21 11:21:02 -07002632 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002633 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002634 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002635
Ian Romanick92f81592011-11-08 12:37:19 -08002636 if (!check_resources(ctx, prog))
2637 goto done;
2638
Ian Romanickce9171f2011-02-03 17:10:14 -08002639 /* OpenGL ES requires that a vertex shader and a fragment shader both be
2640 * present in a linked program. By checking for use of shading language
2641 * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
2642 */
Eric Anholt57f79782011-07-22 12:57:47 -07002643 if (!prog->InternalSeparateShader &&
2644 (ctx->API == API_OPENGLES2 || prog->Version == 100)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002645 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002646 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002647 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002648 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002649 }
2650 }
2651
Ian Romanick13e10e42010-06-21 12:03:24 -07002652 /* FINISHME: Assign fragment shader output locations. */
2653
Ian Romanick832dfa52010-06-17 15:04:20 -07002654done:
2655 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002656
2657 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2658 if (prog->_LinkedShaders[i] == NULL)
2659 continue;
2660
2661 /* Retain any live IR, but trash the rest. */
2662 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002663
2664 /* The symbol table in the linked shaders may contain references to
2665 * variables that were removed (e.g., unused uniforms). Since it may
2666 * contain junk, there is no possible valid use. Delete it and set the
2667 * pointer to NULL.
2668 */
2669 delete prog->_LinkedShaders[i]->symbols;
2670 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002671 }
2672
Kenneth Graunked3073f52011-01-21 14:32:31 -08002673 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002674}