blob: 31094475216b3b010a9e64c548816671381e378d [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
584
585/**
Ian Romanick37101922010-06-18 19:02:10 -0700586 * Validate that outputs from one stage match inputs of another
587 */
588bool
Eric Anholt849e1812010-06-30 11:49:17 -0700589cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700590 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700591{
592 glsl_symbol_table parameters;
593 /* FINISHME: Figure these out dynamically. */
594 const char *const producer_stage = "vertex";
595 const char *const consumer_stage = "fragment";
596
597 /* Find all shader outputs in the "producer" stage.
598 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700599 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700600 ir_variable *const var = ((ir_instruction *) node)->as_variable();
601
602 /* FINISHME: For geometry shaders, this should also look for inout
603 * FINISHME: variables.
604 */
605 if ((var == NULL) || (var->mode != ir_var_out))
606 continue;
607
Eric Anholt001eee52010-11-05 06:11:24 -0700608 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700609 }
610
611
612 /* Find all shader inputs in the "consumer" stage. Any variables that have
613 * matching outputs already in the symbol table must have the same type and
614 * qualifiers.
615 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700616 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700617 ir_variable *const input = ((ir_instruction *) node)->as_variable();
618
619 /* FINISHME: For geometry shaders, this should also look for inout
620 * FINISHME: variables.
621 */
622 if ((input == NULL) || (input->mode != ir_var_in))
623 continue;
624
625 ir_variable *const output = parameters.get_variable(input->name);
626 if (output != NULL) {
627 /* Check that the types match between stages.
628 */
629 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800630 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500631 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800632 * access it must redeclare it with a size. There is some
633 * language in the GLSL spec that implies the fragment shader
634 * and vertex shader do not have to agree on this size. Other
635 * driver behave this way, and one or two applications seem to
636 * rely on it.
637 *
638 * Neither declaration needs to be modified here because the array
639 * sizes are fixed later when update_array_sizes is called.
640 *
641 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
642 *
643 * "Unlike user-defined varying variables, the built-in
644 * varying variables don't have a strict one-to-one
645 * correspondence between the vertex language and the
646 * fragment language."
647 */
648 if (!output->type->is_array()
649 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700650 linker_error(prog,
651 "%s shader output `%s' declared as type `%s', "
652 "but %s shader input declared as type `%s'\n",
653 producer_stage, output->name,
654 output->type->name,
655 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800656 return false;
657 }
Ian Romanick37101922010-06-18 19:02:10 -0700658 }
659
660 /* Check that all of the qualifiers match between stages.
661 */
662 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700663 linker_error(prog,
664 "%s shader output `%s' %s centroid qualifier, "
665 "but %s shader input %s centroid qualifier\n",
666 producer_stage,
667 output->name,
668 (output->centroid) ? "has" : "lacks",
669 consumer_stage,
670 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700671 return false;
672 }
673
674 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700675 linker_error(prog,
676 "%s shader output `%s' %s invariant qualifier, "
677 "but %s shader input %s invariant qualifier\n",
678 producer_stage,
679 output->name,
680 (output->invariant) ? "has" : "lacks",
681 consumer_stage,
682 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700683 return false;
684 }
685
686 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700687 linker_error(prog,
688 "%s shader output `%s' specifies %s "
689 "interpolation qualifier, "
690 "but %s shader input specifies %s "
691 "interpolation qualifier\n",
692 producer_stage,
693 output->name,
694 output->interpolation_string(),
695 consumer_stage,
696 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700697 return false;
698 }
699 }
700 }
701
702 return true;
703}
704
705
Ian Romanick3fb87872010-07-09 14:09:34 -0700706/**
707 * Populates a shaders symbol table with all global declarations
708 */
709static void
710populate_symbol_table(gl_shader *sh)
711{
712 sh->symbols = new(sh) glsl_symbol_table;
713
714 foreach_list(node, sh->ir) {
715 ir_instruction *const inst = (ir_instruction *) node;
716 ir_variable *var;
717 ir_function *func;
718
719 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700720 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700721 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700722 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700723 }
724 }
725}
726
727
728/**
Ian Romanick31a97862010-07-12 18:48:50 -0700729 * Remap variables referenced in an instruction tree
730 *
731 * This is used when instruction trees are cloned from one shader and placed in
732 * another. These trees will contain references to \c ir_variable nodes that
733 * do not exist in the target shader. This function finds these \c ir_variable
734 * references and replaces the references with matching variables in the target
735 * shader.
736 *
737 * If there is no matching variable in the target shader, a clone of the
738 * \c ir_variable is made and added to the target shader. The new variable is
739 * added to \b both the instruction stream and the symbol table.
740 *
741 * \param inst IR tree that is to be processed.
742 * \param symbols Symbol table containing global scope symbols in the
743 * linked shader.
744 * \param instructions Instruction stream where new variable declarations
745 * should be added.
746 */
747void
Eric Anholt8273bd42010-08-04 12:34:56 -0700748remap_variables(ir_instruction *inst, struct gl_shader *target,
749 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700750{
751 class remap_visitor : public ir_hierarchical_visitor {
752 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700753 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700754 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700755 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700756 this->target = target;
757 this->symbols = target->symbols;
758 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700759 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700760 }
761
762 virtual ir_visitor_status visit(ir_dereference_variable *ir)
763 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700764 if (ir->var->mode == ir_var_temporary) {
765 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
766
767 assert(var != NULL);
768 ir->var = var;
769 return visit_continue;
770 }
771
Ian Romanick31a97862010-07-12 18:48:50 -0700772 ir_variable *const existing =
773 this->symbols->get_variable(ir->var->name);
774 if (existing != NULL)
775 ir->var = existing;
776 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700777 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700778
Eric Anholt001eee52010-11-05 06:11:24 -0700779 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700780 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700781 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700782 }
783
784 return visit_continue;
785 }
786
787 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700788 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700789 glsl_symbol_table *symbols;
790 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700791 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700792 };
793
Eric Anholt8273bd42010-08-04 12:34:56 -0700794 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700795
796 inst->accept(&v);
797}
798
799
800/**
801 * Move non-declarations from one instruction stream to another
802 *
803 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700804 * 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 -0700805 * pointer) for \c last and \c false for \c make_copies on the first
806 * call. Successive calls pass the return value of the previous call for
807 * \c last and \c true for \c make_copies.
808 *
809 * \param instructions Source instruction stream
810 * \param last Instruction after which new instructions should be
811 * inserted in the target instruction stream
812 * \param make_copies Flag selecting whether instructions in \c instructions
813 * should be copied (via \c ir_instruction::clone) into the
814 * target list or moved.
815 *
816 * \return
817 * The new "last" instruction in the target instruction stream. This pointer
818 * is suitable for use as the \c last parameter of a later call to this
819 * function.
820 */
821exec_node *
822move_non_declarations(exec_list *instructions, exec_node *last,
823 bool make_copies, gl_shader *target)
824{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700825 hash_table *temps = NULL;
826
827 if (make_copies)
828 temps = hash_table_ctor(0, hash_table_pointer_hash,
829 hash_table_pointer_compare);
830
Ian Romanick303c99f2010-07-19 12:34:56 -0700831 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700832 ir_instruction *inst = (ir_instruction *) node;
833
Ian Romanick7e2aa912010-07-19 17:12:42 -0700834 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700835 continue;
836
Ian Romanick7e2aa912010-07-19 17:12:42 -0700837 ir_variable *var = inst->as_variable();
838 if ((var != NULL) && (var->mode != ir_var_temporary))
839 continue;
840
841 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700842 || inst->as_call()
Ian Romanick7e2aa912010-07-19 17:12:42 -0700843 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700844
845 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700846 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700847
848 if (var != NULL)
849 hash_table_insert(temps, inst, var);
850 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700851 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700852 } else {
853 inst->remove();
854 }
855
856 last->insert_after(inst);
857 last = inst;
858 }
859
Ian Romanick7e2aa912010-07-19 17:12:42 -0700860 if (make_copies)
861 hash_table_dtor(temps);
862
Ian Romanick31a97862010-07-12 18:48:50 -0700863 return last;
864}
865
866/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700867 * Get the function signature for main from a shader
868 */
869static ir_function_signature *
870get_main_function_signature(gl_shader *sh)
871{
872 ir_function *const f = sh->symbols->get_function("main");
873 if (f != NULL) {
874 exec_list void_parameters;
875
876 /* Look for the 'void main()' signature and ensure that it's defined.
877 * This keeps the linker from accidentally pick a shader that just
878 * contains a prototype for main.
879 *
880 * We don't have to check for multiple definitions of main (in multiple
881 * shaders) because that would have already been caught above.
882 */
883 ir_function_signature *sig = f->matching_signature(&void_parameters);
884 if ((sig != NULL) && sig->is_defined) {
885 return sig;
886 }
887 }
888
889 return NULL;
890}
891
892
893/**
Brian Paul84a12732012-02-02 20:10:40 -0700894 * This class is only used in link_intrastage_shaders() below but declaring
895 * it inside that function leads to compiler warnings with some versions of
896 * gcc.
897 */
898class array_sizing_visitor : public ir_hierarchical_visitor {
899public:
900 virtual ir_visitor_status visit(ir_variable *var)
901 {
902 if (var->type->is_array() && (var->type->length == 0)) {
903 const glsl_type *type =
904 glsl_type::get_array_instance(var->type->fields.array,
905 var->max_array_access + 1);
906 assert(type != NULL);
907 var->type = type;
908 }
909 return visit_continue;
910 }
911};
912
913
914/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700915 * Combine a group of shaders for a single stage to generate a linked shader
916 *
917 * \note
918 * If this function is supplied a single shader, it is cloned, and the new
919 * shader is returned.
920 */
921static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800922link_intrastage_shaders(void *mem_ctx,
923 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700924 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700925 struct gl_shader **shader_list,
926 unsigned num_shaders)
927{
Ian Romanick13f782c2010-06-29 18:53:38 -0700928 /* Check that global variables defined in multiple shaders are consistent.
929 */
930 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
931 return NULL;
932
933 /* Check that there is only a single definition of each function signature
934 * across all shaders.
935 */
936 for (unsigned i = 0; i < (num_shaders - 1); i++) {
937 foreach_list(node, shader_list[i]->ir) {
938 ir_function *const f = ((ir_instruction *) node)->as_function();
939
940 if (f == NULL)
941 continue;
942
943 for (unsigned j = i + 1; j < num_shaders; j++) {
944 ir_function *const other =
945 shader_list[j]->symbols->get_function(f->name);
946
947 /* If the other shader has no function (and therefore no function
948 * signatures) with the same name, skip to the next shader.
949 */
950 if (other == NULL)
951 continue;
952
953 foreach_iter (exec_list_iterator, iter, *f) {
954 ir_function_signature *sig =
955 (ir_function_signature *) iter.get();
956
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700957 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700958 continue;
959
960 ir_function_signature *other_sig =
961 other->exact_matching_signature(& sig->parameters);
962
963 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700964 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -0700965 linker_error(prog, "function `%s' is multiply defined",
966 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -0700967 return NULL;
968 }
969 }
970 }
971 }
972 }
973
974 /* Find the shader that defines main, and make a clone of it.
975 *
976 * Starting with the clone, search for undefined references. If one is
977 * found, find the shader that defines it. Clone the reference and add
978 * it to the shader. Repeat until there are no undefined references or
979 * until a reference cannot be resolved.
980 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700981 gl_shader *main = NULL;
982 for (unsigned i = 0; i < num_shaders; i++) {
983 if (get_main_function_signature(shader_list[i]) != NULL) {
984 main = shader_list[i];
985 break;
986 }
987 }
Ian Romanick13f782c2010-06-29 18:53:38 -0700988
Ian Romanick15ce87e2010-07-09 15:28:22 -0700989 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -0700990 linker_error(prog, "%s shader lacks `main'\n",
991 (shader_list[0]->Type == GL_VERTEX_SHADER)
992 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -0700993 return NULL;
994 }
995
Ian Romanick4a455952010-10-13 15:13:02 -0700996 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700997 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800998 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -0700999
1000 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001001
Ian Romanick31a97862010-07-12 18:48:50 -07001002 /* The a pointer to the main function in the final linked shader (i.e., the
1003 * copy of the original shader that contained the main function).
1004 */
1005 ir_function_signature *const main_sig = get_main_function_signature(linked);
1006
1007 /* Move any instructions other than variable declarations or function
1008 * declarations into main.
1009 */
Ian Romanick9303e352010-07-19 12:33:54 -07001010 exec_node *insertion_point =
1011 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1012 linked);
1013
Ian Romanick31a97862010-07-12 18:48:50 -07001014 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001015 if (shader_list[i] == main)
1016 continue;
1017
Ian Romanick31a97862010-07-12 18:48:50 -07001018 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001019 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001020 }
1021
Ian Romanick13f782c2010-06-29 18:53:38 -07001022 /* Resolve initializers for global variables in the linked shader.
1023 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001024 unsigned num_linking_shaders = num_shaders;
1025 for (unsigned i = 0; i < num_shaders; i++)
1026 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1027
1028 gl_shader **linking_shaders =
1029 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1030
1031 memcpy(linking_shaders, shader_list,
1032 sizeof(linking_shaders[0]) * num_shaders);
1033
1034 unsigned idx = num_shaders;
1035 for (unsigned i = 0; i < num_shaders; i++) {
1036 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1037 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1038 idx += shader_list[i]->num_builtins_to_link;
1039 }
1040
1041 assert(idx == num_linking_shaders);
1042
Ian Romanick4a455952010-10-13 15:13:02 -07001043 if (!link_function_calls(prog, linked, linking_shaders,
1044 num_linking_shaders)) {
1045 ctx->Driver.DeleteShader(ctx, linked);
1046 linked = NULL;
1047 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001048
1049 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001050
Paul Berryc148ef62011-08-03 15:37:01 -07001051#ifdef DEBUG
1052 /* At this point linked should contain all of the linked IR, so
1053 * validate it to make sure nothing went wrong.
1054 */
1055 if (linked)
1056 validate_ir_tree(linked->ir);
1057#endif
1058
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001059 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001060 * unspecified sizes have a size specified. The size is inferred from the
1061 * max_array_access field.
1062 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001063 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001064 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001065
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001066 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001067 }
1068
Ian Romanick3fb87872010-07-09 14:09:34 -07001069 return linked;
1070}
1071
Eric Anholta721abf2010-08-23 10:32:01 -07001072/**
1073 * Update the sizes of linked shader uniform arrays to the maximum
1074 * array index used.
1075 *
1076 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1077 *
1078 * If one or more elements of an array are active,
1079 * GetActiveUniform will return the name of the array in name,
1080 * subject to the restrictions listed above. The type of the array
1081 * is returned in type. The size parameter contains the highest
1082 * array element index used, plus one. The compiler or linker
1083 * determines the highest index used. There will be only one
1084 * active uniform reported by the GL per uniform array.
1085
1086 */
1087static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001088update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001089{
Ian Romanick3322fba2010-10-14 13:28:42 -07001090 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1091 if (prog->_LinkedShaders[i] == NULL)
1092 continue;
1093
Eric Anholta721abf2010-08-23 10:32:01 -07001094 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1095 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1096
Eric Anholt586b4b52010-09-28 14:32:16 -07001097 if ((var == NULL) || (var->mode != ir_var_uniform &&
1098 var->mode != ir_var_in &&
1099 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001100 !var->type->is_array())
1101 continue;
1102
1103 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001104 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1105 if (prog->_LinkedShaders[j] == NULL)
1106 continue;
1107
Eric Anholta721abf2010-08-23 10:32:01 -07001108 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1109 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1110 if (!other_var)
1111 continue;
1112
1113 if (strcmp(var->name, other_var->name) == 0 &&
1114 other_var->max_array_access > size) {
1115 size = other_var->max_array_access;
1116 }
1117 }
1118 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001119
Eric Anholta721abf2010-08-23 10:32:01 -07001120 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001121 /* If this is a built-in uniform (i.e., it's backed by some
1122 * fixed-function state), adjust the number of state slots to
1123 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001124 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001125 * slots is an integer multiple of the number of array elements.
1126 * Determine the number of slots per array element by dividing by
1127 * the old (total) size.
1128 */
1129 if (var->num_state_slots > 0) {
1130 var->num_state_slots = (size + 1)
1131 * (var->num_state_slots / var->type->length);
1132 }
1133
Eric Anholta721abf2010-08-23 10:32:01 -07001134 var->type = glsl_type::get_array_instance(var->type->fields.array,
1135 size + 1);
1136 /* FINISHME: We should update the types of array
1137 * dereferences of this variable now.
1138 */
1139 }
1140 }
1141 }
1142}
1143
Ian Romanick69846702010-06-22 17:29:19 -07001144/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001145 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001146 *
1147 * \param used_mask Bits representing used (1) and unused (0) locations
1148 * \param needed_count Number of contiguous bits needed.
1149 *
1150 * \return
1151 * Base location of the available bits on success or -1 on failure.
1152 */
1153int
1154find_available_slots(unsigned used_mask, unsigned needed_count)
1155{
1156 unsigned needed_mask = (1 << needed_count) - 1;
1157 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1158
1159 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1160 * cannot optimize possibly infinite loops" for the loop below.
1161 */
1162 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1163 return -1;
1164
1165 for (int i = 0; i <= max_bit_to_test; i++) {
1166 if ((needed_mask & ~used_mask) == needed_mask)
1167 return i;
1168
1169 needed_mask <<= 1;
1170 }
1171
1172 return -1;
1173}
1174
1175
Ian Romanickd32d4f72011-06-27 17:59:58 -07001176/**
1177 * Assign locations for either VS inputs for FS outputs
1178 *
1179 * \param prog Shader program whose variables need locations assigned
1180 * \param target_index Selector for the program target to receive location
1181 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1182 * \c MESA_SHADER_FRAGMENT.
1183 * \param max_index Maximum number of generic locations. This corresponds
1184 * to either the maximum number of draw buffers or the
1185 * maximum number of generic attributes.
1186 *
1187 * \return
1188 * If locations are successfully assigned, true is returned. Otherwise an
1189 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001190 */
Ian Romanick69846702010-06-22 17:29:19 -07001191bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001192assign_attribute_or_color_locations(gl_shader_program *prog,
1193 unsigned target_index,
1194 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001195{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001196 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001197 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001198 unsigned used_locations = (max_index >= 32)
1199 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001200
Ian Romanickd32d4f72011-06-27 17:59:58 -07001201 assert((target_index == MESA_SHADER_VERTEX)
1202 || (target_index == MESA_SHADER_FRAGMENT));
1203
1204 gl_shader *const sh = prog->_LinkedShaders[target_index];
1205 if (sh == NULL)
1206 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001207
Ian Romanick69846702010-06-22 17:29:19 -07001208 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001209 *
1210 * 1. Invalidate the location assignments for all vertex shader inputs.
1211 *
1212 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001213 * glBindVertexAttribLocation) locations and outputs that have
1214 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001215 *
Ian Romanick69846702010-06-22 17:29:19 -07001216 * 3. Sort the attributes without assigned locations by number of slots
1217 * required in decreasing order. Fragmentation caused by attribute
1218 * locations assigned by the application may prevent large attributes
1219 * from having enough contiguous space.
1220 *
1221 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001222 */
1223
Ian Romanickd32d4f72011-06-27 17:59:58 -07001224 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001225 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001226
Ian Romanickd32d4f72011-06-27 17:59:58 -07001227 const enum ir_variable_mode direction =
1228 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1229
1230
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07001231 link_invalidate_variable_locations(sh, direction, generic_base);
Ian Romanickd32d4f72011-06-27 17:59:58 -07001232
Ian Romanick69846702010-06-22 17:29:19 -07001233 /* Temporary storage for the set of attributes that need locations assigned.
1234 */
1235 struct temp_attr {
1236 unsigned slots;
1237 ir_variable *var;
1238
1239 /* Used below in the call to qsort. */
1240 static int compare(const void *a, const void *b)
1241 {
1242 const temp_attr *const l = (const temp_attr *) a;
1243 const temp_attr *const r = (const temp_attr *) b;
1244
1245 /* Reversed because we want a descending order sort below. */
1246 return r->slots - l->slots;
1247 }
1248 } to_assign[16];
1249
1250 unsigned num_attr = 0;
1251
Eric Anholt16b68b12010-06-30 11:05:43 -07001252 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001253 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1254
Brian Paul4470ff22011-07-19 21:10:25 -06001255 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001256 continue;
1257
Ian Romanick68a4fc92010-10-07 17:21:22 -07001258 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001259 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001260 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001261 linker_error(prog,
1262 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001263 (var->location < 0)
1264 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001265 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001266 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001267 }
1268 } else if (target_index == MESA_SHADER_VERTEX) {
1269 unsigned binding;
1270
1271 if (prog->AttributeBindings->get(binding, var->name)) {
1272 assert(binding >= VERT_ATTRIB_GENERIC0);
1273 var->location = binding;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001274 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001275 } else if (target_index == MESA_SHADER_FRAGMENT) {
1276 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001277 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001278
1279 if (prog->FragDataBindings->get(binding, var->name)) {
1280 assert(binding >= FRAG_RESULT_DATA0);
1281 var->location = binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001282
1283 if (prog->FragDataIndexBindings->get(index, var->name)) {
1284 var->index = index;
1285 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001286 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001287 }
1288
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001289 /* If the variable is not a built-in and has a location statically
1290 * assigned in the shader (presumably via a layout qualifier), make sure
1291 * that it doesn't collide with other assigned locations. Otherwise,
1292 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001293 */
Ian Romanick523b6112011-08-17 15:40:03 -07001294 const unsigned slots = count_attribute_slots(var->type);
1295 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001296 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001297 /* From page 61 of the OpenGL 4.0 spec:
1298 *
1299 * "LinkProgram will fail if the attribute bindings assigned
1300 * by BindAttribLocation do not leave not enough space to
1301 * assign a location for an active matrix attribute or an
1302 * active attribute array, both of which require multiple
1303 * contiguous generic attributes."
1304 *
1305 * Previous versions of the spec contain similar language but omit
1306 * the bit about attribute arrays.
1307 *
1308 * Page 61 of the OpenGL 4.0 spec also says:
1309 *
1310 * "It is possible for an application to bind more than one
1311 * attribute name to the same location. This is referred to as
1312 * aliasing. This will only work if only one of the aliased
1313 * attributes is active in the executable program, or if no
1314 * path through the shader consumes more than one attribute of
1315 * a set of attributes aliased to the same location. A link
1316 * error can occur if the linker determines that every path
1317 * through the shader consumes multiple aliased attributes,
1318 * but implementations are not required to generate an error
1319 * in this case."
1320 *
1321 * These two paragraphs are either somewhat contradictory, or I
1322 * don't fully understand one or both of them.
1323 */
1324 /* FINISHME: The code as currently written does not support
1325 * FINISHME: attribute location aliasing (see comment above).
1326 */
1327 /* Mask representing the contiguous slots that will be used by
1328 * this attribute.
1329 */
1330 const unsigned attr = var->location - generic_base;
1331 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001332
Ian Romanick523b6112011-08-17 15:40:03 -07001333 /* Generate a link error if the set of bits requested for this
1334 * attribute overlaps any previously allocated bits.
1335 */
1336 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001337 const char *const string = (target_index == MESA_SHADER_VERTEX)
1338 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001339 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001340 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001341 "available for %s `%s' %d %d %d", string,
1342 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001343 return false;
1344 }
1345
1346 used_locations |= (use_mask << attr);
1347 }
1348
1349 continue;
1350 }
1351
1352 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001353 to_assign[num_attr].var = var;
1354 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001355 }
Ian Romanick69846702010-06-22 17:29:19 -07001356
1357 /* If all of the attributes were assigned locations by the application (or
1358 * are built-in attributes with fixed locations), return early. This should
1359 * be the common case.
1360 */
1361 if (num_attr == 0)
1362 return true;
1363
1364 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1365
Ian Romanickd32d4f72011-06-27 17:59:58 -07001366 if (target_index == MESA_SHADER_VERTEX) {
1367 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1368 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1369 * reserved to prevent it from being automatically allocated below.
1370 */
1371 find_deref_visitor find("gl_Vertex");
1372 find.run(sh->ir);
1373 if (find.variable_found())
1374 used_locations |= (1 << 0);
1375 }
Ian Romanick982e3792010-06-29 18:58:20 -07001376
Ian Romanick69846702010-06-22 17:29:19 -07001377 for (unsigned i = 0; i < num_attr; i++) {
1378 /* Mask representing the contiguous slots that will be used by this
1379 * attribute.
1380 */
1381 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1382
1383 int location = find_available_slots(used_locations, to_assign[i].slots);
1384
1385 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001386 const char *const string = (target_index == MESA_SHADER_VERTEX)
1387 ? "vertex shader input" : "fragment shader output";
1388
Ian Romanick586e7412011-07-28 14:04:09 -07001389 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001390 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001391 "available for %s `%s'",
1392 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001393 return false;
1394 }
1395
Ian Romanickd32d4f72011-06-27 17:59:58 -07001396 to_assign[i].var->location = generic_base + location;
Ian Romanick69846702010-06-22 17:29:19 -07001397 used_locations |= (use_mask << location);
1398 }
1399
1400 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001401}
1402
1403
Ian Romanick40e114b2010-08-17 14:55:50 -07001404/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001405 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001406 */
1407void
Ian Romanickcc90e622010-10-19 17:59:10 -07001408demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001409{
1410 foreach_list(node, sh->ir) {
1411 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1412
Ian Romanickcc90e622010-10-19 17:59:10 -07001413 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001414 continue;
1415
Ian Romanickcc90e622010-10-19 17:59:10 -07001416 /* A shader 'in' or 'out' variable is only really an input or output if
1417 * its value is used by other shader stages. This will cause the variable
1418 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001419 */
1420 if (var->location == -1) {
1421 var->mode = ir_var_auto;
1422 }
1423 }
1424}
1425
1426
Paul Berry871ddb92011-11-05 11:17:32 -07001427/**
1428 * Data structure tracking information about a transform feedback declaration
1429 * during linking.
1430 */
1431class tfeedback_decl
1432{
1433public:
Paul Berry456279b2011-12-26 19:39:25 -08001434 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1435 const void *mem_ctx, const char *input);
Paul Berry871ddb92011-11-05 11:17:32 -07001436 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1437 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1438 ir_variable *output_var);
Christoph Bumillerd540af52012-01-20 13:24:46 +01001439 bool accumulate_num_outputs(struct gl_shader_program *prog, unsigned *count);
Paul Berryd3150eb2012-01-09 11:25:14 -08001440 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001441 struct gl_transform_feedback_info *info, unsigned buffer,
Christoph Bumillerd540af52012-01-20 13:24:46 +01001442 unsigned varying, const unsigned max_outputs) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001443
1444
1445 /**
1446 * True if assign_location() has been called for this object.
1447 */
1448 bool is_assigned() const
1449 {
1450 return this->location != -1;
1451 }
1452
1453 /**
1454 * Determine whether this object refers to the variable var.
1455 */
1456 bool matches_var(ir_variable *var) const
1457 {
Paul Berry642e5b412012-01-04 13:57:52 -08001458 if (this->is_clip_distance_mesa)
1459 return strcmp(var->name, "gl_ClipDistanceMESA") == 0;
1460 else
1461 return strcmp(var->name, this->var_name) == 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001462 }
1463
1464 /**
1465 * The total number of varying components taken up by this variable. Only
1466 * valid if is_assigned() is true.
1467 */
1468 unsigned num_components() const
1469 {
Paul Berry642e5b412012-01-04 13:57:52 -08001470 if (this->is_clip_distance_mesa)
1471 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001472 else
Paul Berry642e5b412012-01-04 13:57:52 -08001473 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001474 }
1475
1476private:
1477 /**
1478 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001479 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001480 */
1481 const char *orig_name;
1482
1483 /**
1484 * The name of the variable, parsed from orig_name.
1485 */
Paul Berry913a5c22011-12-27 08:24:57 -08001486 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001487
1488 /**
1489 * True if the declaration in orig_name represents an array.
1490 */
Paul Berry33fe0212012-01-03 20:41:34 -08001491 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001492
1493 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001494 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001495 */
Paul Berry33fe0212012-01-03 20:41:34 -08001496 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001497
1498 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001499 * True if the variable is gl_ClipDistance and the driver lowers
1500 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001501 */
Paul Berry642e5b412012-01-04 13:57:52 -08001502 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001503
1504 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001505 * The vertex shader output location that the linker assigned for this
1506 * variable. -1 if a location hasn't been assigned yet.
1507 */
1508 int location;
1509
1510 /**
1511 * If location != -1, the number of vector elements in this variable, or 1
1512 * if this variable is a scalar.
1513 */
1514 unsigned vector_elements;
1515
1516 /**
1517 * If location != -1, the number of matrix columns in this variable, or 1
1518 * if this variable is not a matrix.
1519 */
1520 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001521
1522 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1523 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001524
1525 /**
1526 * If location != -1, the size that should be returned by
1527 * glGetTransformFeedbackVarying().
1528 */
1529 unsigned size;
Paul Berry871ddb92011-11-05 11:17:32 -07001530};
1531
1532
1533/**
1534 * Initialize this object based on a string that was passed to
1535 * glTransformFeedbackVaryings. If there is a parse error, the error is
1536 * reported using linker_error(), and false is returned.
1537 */
1538bool
Paul Berry456279b2011-12-26 19:39:25 -08001539tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1540 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001541{
1542 /* We don't have to be pedantic about what is a valid GLSL variable name,
1543 * because any variable with an invalid name can't exist in the IR anyway.
1544 */
1545
1546 this->location = -1;
1547 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001548 this->is_clip_distance_mesa = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001549
1550 const char *bracket = strrchr(input, '[');
1551
1552 if (bracket) {
1553 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001554 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001555 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1556 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001557 }
Paul Berry33fe0212012-01-03 20:41:34 -08001558 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001559 } else {
1560 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001561 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001562 }
1563
Paul Berry642e5b412012-01-04 13:57:52 -08001564 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1565 * class must behave specially to account for the fact that gl_ClipDistance
1566 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001567 */
1568 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1569 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001570 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001571 }
1572
1573 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001574}
1575
1576
1577/**
1578 * Determine whether two tfeedback_decl objects refer to the same variable and
1579 * array index (if applicable).
1580 */
1581bool
1582tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1583{
1584 if (strcmp(x.var_name, y.var_name) != 0)
1585 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001586 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001587 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001588 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001589 return false;
1590 return true;
1591}
1592
1593
1594/**
1595 * Assign a location for this tfeedback_decl object based on the location
1596 * assignment in output_var.
1597 *
1598 * If an error occurs, the error is reported through linker_error() and false
1599 * is returned.
1600 */
1601bool
1602tfeedback_decl::assign_location(struct gl_context *ctx,
1603 struct gl_shader_program *prog,
1604 ir_variable *output_var)
1605{
1606 if (output_var->type->is_array()) {
1607 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001608 const unsigned matrix_cols =
1609 output_var->type->fields.array->matrix_columns;
Paul Berry642e5b412012-01-04 13:57:52 -08001610 unsigned actual_array_size = this->is_clip_distance_mesa ?
1611 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001612
1613 if (this->is_subscripted) {
1614 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001615 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001616 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001617 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001618 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001619 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001620 return false;
1621 }
Paul Berry642e5b412012-01-04 13:57:52 -08001622 if (this->is_clip_distance_mesa) {
1623 this->location =
1624 output_var->location + this->array_subscript / 4;
1625 } else {
1626 this->location =
1627 output_var->location + this->array_subscript * matrix_cols;
1628 }
Paul Berry33fe0212012-01-03 20:41:34 -08001629 this->size = 1;
1630 } else {
1631 this->location = output_var->location;
Paul Berry642e5b412012-01-04 13:57:52 -08001632 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001633 }
Paul Berry871ddb92011-11-05 11:17:32 -07001634 this->vector_elements = output_var->type->fields.array->vector_elements;
1635 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001636 if (this->is_clip_distance_mesa)
1637 this->type = GL_FLOAT;
1638 else
1639 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001640 } else {
1641 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001642 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001643 linker_error(prog, "Transform feedback varying %s requested, "
1644 "but %s is not an array.",
1645 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001646 return false;
1647 }
1648 this->location = output_var->location;
Paul Berry33fe0212012-01-03 20:41:34 -08001649 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001650 this->vector_elements = output_var->type->vector_elements;
1651 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001652 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001653 }
Paul Berry642e5b412012-01-04 13:57:52 -08001654
Paul Berry871ddb92011-11-05 11:17:32 -07001655 /* From GL_EXT_transform_feedback:
1656 * A program will fail to link if:
1657 *
1658 * * the total number of components to capture in any varying
1659 * variable in <varyings> is greater than the constant
1660 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1661 * buffer mode is SEPARATE_ATTRIBS_EXT;
1662 */
1663 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1664 this->num_components() >
1665 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1666 linker_error(prog, "Transform feedback varying %s exceeds "
1667 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1668 this->orig_name);
1669 return false;
1670 }
1671
1672 return true;
1673}
1674
1675
Paul Berry871ddb92011-11-05 11:17:32 -07001676bool
Christoph Bumillerd540af52012-01-20 13:24:46 +01001677tfeedback_decl::accumulate_num_outputs(struct gl_shader_program *prog,
1678 unsigned *count)
Paul Berry871ddb92011-11-05 11:17:32 -07001679{
1680 if (!this->is_assigned()) {
1681 /* From GL_EXT_transform_feedback:
1682 * A program will fail to link if:
1683 *
1684 * * any variable name specified in the <varyings> array is not
1685 * declared as an output in the geometry shader (if present) or
1686 * the vertex shader (if no geometry shader is present);
1687 */
1688 linker_error(prog, "Transform feedback varying %s undeclared.",
1689 this->orig_name);
1690 return false;
1691 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001692
Christoph Bumillerd540af52012-01-20 13:24:46 +01001693 unsigned translated_size = this->size;
1694 if (this->is_clip_distance_mesa)
1695 translated_size = (translated_size + 3) / 4;
1696
1697 *count += translated_size * this->matrix_columns;
1698
1699 return true;
1700}
1701
1702
1703/**
1704 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1705 *
1706 * If an error occurs, the error is reported through linker_error() and false
1707 * is returned.
1708 */
1709bool
1710tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1711 struct gl_transform_feedback_info *info,
1712 unsigned buffer,
1713 unsigned varying, const unsigned max_outputs) const
1714{
Paul Berryd3150eb2012-01-09 11:25:14 -08001715 /* From GL_EXT_transform_feedback:
1716 * A program will fail to link if:
1717 *
1718 * * the total number of components to capture is greater than
1719 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1720 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1721 */
1722 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1723 info->BufferStride[buffer] + this->num_components() >
1724 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1725 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1726 "limit has been exceeded.");
1727 return false;
1728 }
1729
Paul Berry642e5b412012-01-04 13:57:52 -08001730 unsigned translated_size = this->size;
1731 if (this->is_clip_distance_mesa)
1732 translated_size = (translated_size + 3) / 4;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001733 unsigned components_so_far = 0;
Paul Berry642e5b412012-01-04 13:57:52 -08001734 for (unsigned index = 0; index < translated_size; ++index) {
Paul Berry33fe0212012-01-03 20:41:34 -08001735 for (unsigned v = 0; v < this->matrix_columns; ++v) {
Paul Berry642e5b412012-01-04 13:57:52 -08001736 unsigned num_components = this->vector_elements;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001737 assert(info->NumOutputs < max_outputs);
Paul Berry642e5b412012-01-04 13:57:52 -08001738 info->Outputs[info->NumOutputs].ComponentOffset = 0;
1739 if (this->is_clip_distance_mesa) {
1740 if (this->is_subscripted) {
1741 num_components = 1;
1742 info->Outputs[info->NumOutputs].ComponentOffset =
1743 this->array_subscript % 4;
1744 } else {
1745 num_components = MIN2(4, this->size - components_so_far);
1746 }
1747 }
Paul Berry33fe0212012-01-03 20:41:34 -08001748 info->Outputs[info->NumOutputs].OutputRegister =
1749 this->location + v + index * this->matrix_columns;
1750 info->Outputs[info->NumOutputs].NumComponents = num_components;
1751 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1752 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
Paul Berry33fe0212012-01-03 20:41:34 -08001753 ++info->NumOutputs;
1754 info->BufferStride[buffer] += num_components;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001755 components_so_far += num_components;
Paul Berry33fe0212012-01-03 20:41:34 -08001756 }
Paul Berry871ddb92011-11-05 11:17:32 -07001757 }
Paul Berrybe4e9f72012-01-04 12:21:55 -08001758 assert(components_so_far == this->num_components());
Eric Anholt9d36c962012-01-02 17:08:13 -08001759
1760 info->Varyings[varying].Name = ralloc_strdup(prog, this->orig_name);
1761 info->Varyings[varying].Type = this->type;
Paul Berry33fe0212012-01-03 20:41:34 -08001762 info->Varyings[varying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001763 info->NumVarying++;
1764
Paul Berry871ddb92011-11-05 11:17:32 -07001765 return true;
1766}
1767
1768
1769/**
1770 * Parse all the transform feedback declarations that were passed to
1771 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1772 *
1773 * If an error occurs, the error is reported through linker_error() and false
1774 * is returned.
1775 */
1776static bool
Paul Berry456279b2011-12-26 19:39:25 -08001777parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1778 const void *mem_ctx, unsigned num_names,
1779 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001780{
1781 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001782 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001783 return false;
1784 /* From GL_EXT_transform_feedback:
1785 * A program will fail to link if:
1786 *
1787 * * any two entries in the <varyings> array specify the same varying
1788 * variable;
1789 *
1790 * We interpret this to mean "any two entries in the <varyings> array
1791 * specify the same varying variable and array index", since transform
1792 * feedback of arrays would be useless otherwise.
1793 */
1794 for (unsigned j = 0; j < i; ++j) {
1795 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1796 linker_error(prog, "Transform feedback varying %s specified "
1797 "more than once.", varying_names[i]);
1798 return false;
1799 }
1800 }
1801 }
1802 return true;
1803}
1804
1805
1806/**
1807 * Assign a location for a variable that is produced in one pipeline stage
1808 * (the "producer") and consumed in the next stage (the "consumer").
1809 *
1810 * \param input_var is the input variable declaration in the consumer.
1811 *
1812 * \param output_var is the output variable declaration in the producer.
1813 *
1814 * \param input_index is the counter that keeps track of assigned input
1815 * locations in the consumer.
1816 *
1817 * \param output_index is the counter that keeps track of assigned output
1818 * locations in the producer.
1819 *
1820 * It is permissible for \c input_var to be NULL (this happens if a variable
1821 * is output by the producer and consumed by transform feedback, but not
1822 * consumed by the consumer).
1823 *
1824 * If the variable has already been assigned a location, this function has no
1825 * effect.
1826 */
1827void
1828assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1829 unsigned *input_index, unsigned *output_index)
1830{
1831 if (output_var->location != -1) {
1832 /* Location already assigned. */
1833 return;
1834 }
1835
1836 if (input_var) {
1837 assert(input_var->location == -1);
1838 input_var->location = *input_index;
1839 }
1840
1841 output_var->location = *output_index;
1842
1843 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1844 assert(!output_var->type->is_record());
1845
1846 if (output_var->type->is_array()) {
1847 const unsigned slots = output_var->type->length
1848 * output_var->type->fields.array->matrix_columns;
1849
1850 *output_index += slots;
1851 *input_index += slots;
1852 } else {
1853 const unsigned slots = output_var->type->matrix_columns;
1854
1855 *output_index += slots;
1856 *input_index += slots;
1857 }
1858}
1859
1860
1861/**
Brian Paul8fb1e4a2012-06-26 13:06:47 -06001862 * Is the given variable a varying variable to be counted against the
1863 * limit in ctx->Const.MaxVarying?
1864 * This includes variables such as texcoords, colors and generic
1865 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
1866 */
1867static bool
1868is_varying_var(GLenum shaderType, const ir_variable *var)
1869{
1870 /* Only fragment shaders will take a varying variable as an input */
1871 if (shaderType == GL_FRAGMENT_SHADER &&
1872 var->mode == ir_var_in &&
1873 var->explicit_location) {
1874 switch (var->location) {
1875 case FRAG_ATTRIB_WPOS:
1876 case FRAG_ATTRIB_FACE:
1877 case FRAG_ATTRIB_PNTC:
1878 return false;
1879 default:
1880 return true;
1881 }
1882 }
1883 return false;
1884}
1885
1886
1887/**
Paul Berry871ddb92011-11-05 11:17:32 -07001888 * Assign locations for all variables that are produced in one pipeline stage
1889 * (the "producer") and consumed in the next stage (the "consumer").
1890 *
1891 * Variables produced by the producer may also be consumed by transform
1892 * feedback.
1893 *
1894 * \param num_tfeedback_decls is the number of declarations indicating
1895 * variables that may be consumed by transform feedback.
1896 *
1897 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
1898 * representing the result of parsing the strings passed to
1899 * glTransformFeedbackVaryings(). assign_location() will be called for
1900 * each of these objects that matches one of the outputs of the
1901 * producer.
1902 *
1903 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
1904 * be NULL. In this case, varying locations are assigned solely based on the
1905 * requirements of transform feedback.
1906 */
Ian Romanickde773242011-06-09 13:31:32 -07001907bool
1908assign_varying_locations(struct gl_context *ctx,
1909 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07001910 gl_shader *producer, gl_shader *consumer,
1911 unsigned num_tfeedback_decls,
1912 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07001913{
1914 /* FINISHME: Set dynamically when geometry shader support is added. */
1915 unsigned output_index = VERT_RESULT_VAR0;
1916 unsigned input_index = FRAG_ATTRIB_VAR0;
1917
1918 /* Operate in a total of three passes.
1919 *
1920 * 1. Assign locations for any matching inputs and outputs.
1921 *
1922 * 2. Mark output variables in the producer that do not have locations as
1923 * not being outputs. This lets the optimizer eliminate them.
1924 *
1925 * 3. Mark input variables in the consumer that do not have locations as
1926 * not being inputs. This lets the optimizer eliminate them.
1927 */
1928
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07001929 link_invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
Paul Berry871ddb92011-11-05 11:17:32 -07001930 if (consumer)
1931 link_invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
Ian Romanick0e59b262010-06-23 11:23:01 -07001932
Eric Anholt16b68b12010-06-30 11:05:43 -07001933 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07001934 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1935
Paul Berry871ddb92011-11-05 11:17:32 -07001936 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07001937 continue;
1938
Paul Berry871ddb92011-11-05 11:17:32 -07001939 ir_variable *input_var =
1940 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07001941
Paul Berry871ddb92011-11-05 11:17:32 -07001942 if (input_var && input_var->mode != ir_var_in)
1943 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07001944
Paul Berry871ddb92011-11-05 11:17:32 -07001945 if (input_var) {
1946 assign_varying_location(input_var, output_var, &input_index,
1947 &output_index);
1948 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001949
Paul Berry871ddb92011-11-05 11:17:32 -07001950 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
1951 if (!tfeedback_decls[i].is_assigned() &&
1952 tfeedback_decls[i].matches_var(output_var)) {
1953 if (output_var->location == -1) {
1954 assign_varying_location(input_var, output_var, &input_index,
1955 &output_index);
1956 }
1957 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
1958 return false;
1959 }
Ian Romanickdf869d92010-08-30 15:37:44 -07001960 }
Ian Romanick0e59b262010-06-23 11:23:01 -07001961 }
1962
Ian Romanickde773242011-06-09 13:31:32 -07001963 unsigned varying_vectors = 0;
1964
Paul Berry871ddb92011-11-05 11:17:32 -07001965 if (consumer) {
1966 foreach_list(node, consumer->ir) {
1967 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07001968
Paul Berry871ddb92011-11-05 11:17:32 -07001969 if ((var == NULL) || (var->mode != ir_var_in))
1970 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07001971
Paul Berry871ddb92011-11-05 11:17:32 -07001972 if (var->location == -1) {
1973 if (prog->Version <= 120) {
1974 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1975 *
1976 * Only those varying variables used (i.e. read) in
1977 * the fragment shader executable must be written to
1978 * by the vertex shader executable; declaring
1979 * superfluous varying variables in a vertex shader is
1980 * permissible.
1981 *
1982 * We interpret this text as meaning that the VS must
1983 * write the variable for the FS to read it. See
1984 * "glsl1-varying read but not written" in piglit.
1985 */
Eric Anholtb7062832010-07-28 13:52:23 -07001986
Paul Berry871ddb92011-11-05 11:17:32 -07001987 linker_error(prog, "fragment shader varying %s not written "
1988 "by vertex shader\n.", var->name);
1989 }
Eric Anholtb7062832010-07-28 13:52:23 -07001990
Paul Berry871ddb92011-11-05 11:17:32 -07001991 /* An 'in' variable is only really a shader input if its
1992 * value is written by the previous stage.
1993 */
1994 var->mode = ir_var_auto;
Brian Paul8fb1e4a2012-06-26 13:06:47 -06001995 } else if (is_varying_var(consumer->Type, var)) {
Paul Berry871ddb92011-11-05 11:17:32 -07001996 /* The packing rules are used for vertex shader inputs are also
1997 * used for fragment shader inputs.
1998 */
1999 varying_vectors += count_attribute_slots(var->type);
2000 }
Eric Anholtb7062832010-07-28 13:52:23 -07002001 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002002 }
Ian Romanickde773242011-06-09 13:31:32 -07002003
2004 if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
2005 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002006 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2007 linker_warning(prog, "shader uses too many varying vectors "
2008 "(%u > %u), but the driver will try to optimize "
2009 "them out; this is non-portable out-of-spec "
2010 "behavior\n",
2011 varying_vectors, ctx->Const.MaxVarying);
2012 } else {
2013 linker_error(prog, "shader uses too many varying vectors "
2014 "(%u > %u)\n",
2015 varying_vectors, ctx->Const.MaxVarying);
2016 return false;
2017 }
Ian Romanickde773242011-06-09 13:31:32 -07002018 }
2019 } else {
2020 const unsigned float_components = varying_vectors * 4;
2021 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002022 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2023 linker_warning(prog, "shader uses too many varying components "
2024 "(%u > %u), but the driver will try to optimize "
2025 "them out; this is non-portable out-of-spec "
2026 "behavior\n",
2027 float_components, ctx->Const.MaxVarying * 4);
2028 } else {
2029 linker_error(prog, "shader uses too many varying components "
2030 "(%u > %u)\n",
2031 float_components, ctx->Const.MaxVarying * 4);
2032 return false;
2033 }
Ian Romanickde773242011-06-09 13:31:32 -07002034 }
2035 }
2036
2037 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07002038}
2039
2040
Paul Berry871ddb92011-11-05 11:17:32 -07002041/**
2042 * Store transform feedback location assignments into
2043 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2044 *
2045 * If an error occurs, the error is reported through linker_error() and false
2046 * is returned.
2047 */
2048static bool
2049store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2050 unsigned num_tfeedback_decls,
2051 tfeedback_decl *tfeedback_decls)
2052{
Paul Berryebfad9f2011-12-29 15:55:01 -08002053 bool separate_attribs_mode =
2054 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08002055
2056 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01002057 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08002058
2059 memset(&prog->LinkedTransformFeedback, 0,
2060 sizeof(prog->LinkedTransformFeedback));
2061
Paul Berry1be0fd82012-01-05 13:06:36 -08002062 prog->LinkedTransformFeedback.NumBuffers =
2063 separate_attribs_mode ? num_tfeedback_decls : 1;
2064
Eric Anholt9d36c962012-01-02 17:08:13 -08002065 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08002066 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08002067 struct gl_transform_feedback_varying_info,
2068 num_tfeedback_decls);
2069
Christoph Bumillerd540af52012-01-20 13:24:46 +01002070 unsigned num_outputs = 0;
2071 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
2072 if (!tfeedback_decls[i].accumulate_num_outputs(prog, &num_outputs))
2073 return false;
2074
2075 prog->LinkedTransformFeedback.Outputs =
2076 rzalloc_array(prog,
2077 struct gl_transform_feedback_output,
2078 num_outputs);
2079
Paul Berry871ddb92011-11-05 11:17:32 -07002080 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
Paul Berryebfad9f2011-12-29 15:55:01 -08002081 unsigned buffer = separate_attribs_mode ? i : 0;
Paul Berryd3150eb2012-01-09 11:25:14 -08002082 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
Christoph Bumillerd540af52012-01-20 13:24:46 +01002083 buffer, i, num_outputs))
Paul Berry871ddb92011-11-05 11:17:32 -07002084 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07002085 }
Christoph Bumillerd540af52012-01-20 13:24:46 +01002086 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002087
2088 return true;
2089}
2090
Ian Romanick92f81592011-11-08 12:37:19 -08002091/**
Marek Olšákec174a42011-11-18 15:00:10 +01002092 * Store the gl_FragDepth layout in the gl_shader_program struct.
2093 */
2094static void
2095store_fragdepth_layout(struct gl_shader_program *prog)
2096{
2097 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2098 return;
2099 }
2100
2101 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2102
2103 /* We don't look up the gl_FragDepth symbol directly because if
2104 * gl_FragDepth is not used in the shader, it's removed from the IR.
2105 * However, the symbol won't be removed from the symbol table.
2106 *
2107 * We're only interested in the cases where the variable is NOT removed
2108 * from the IR.
2109 */
2110 foreach_list(node, ir) {
2111 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2112
2113 if (var == NULL || var->mode != ir_var_out) {
2114 continue;
2115 }
2116
2117 if (strcmp(var->name, "gl_FragDepth") == 0) {
2118 switch (var->depth_layout) {
2119 case ir_depth_layout_none:
2120 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2121 return;
2122 case ir_depth_layout_any:
2123 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2124 return;
2125 case ir_depth_layout_greater:
2126 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2127 return;
2128 case ir_depth_layout_less:
2129 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2130 return;
2131 case ir_depth_layout_unchanged:
2132 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2133 return;
2134 default:
2135 assert(0);
2136 return;
2137 }
2138 }
2139 }
2140}
2141
2142/**
Ian Romanick92f81592011-11-08 12:37:19 -08002143 * Validate the resources used by a program versus the implementation limits
2144 */
2145static bool
2146check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2147{
2148 static const char *const shader_names[MESA_SHADER_TYPES] = {
2149 "vertex", "fragment", "geometry"
2150 };
2151
2152 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2153 ctx->Const.MaxVertexTextureImageUnits,
2154 ctx->Const.MaxTextureImageUnits,
2155 ctx->Const.MaxGeometryTextureImageUnits
2156 };
2157
2158 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2159 ctx->Const.VertexProgram.MaxUniformComponents,
2160 ctx->Const.FragmentProgram.MaxUniformComponents,
2161 0 /* FINISHME: Geometry shaders. */
2162 };
2163
2164 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2165 struct gl_shader *sh = prog->_LinkedShaders[i];
2166
2167 if (sh == NULL)
2168 continue;
2169
2170 if (sh->num_samplers > max_samplers[i]) {
2171 linker_error(prog, "Too many %s shader texture samplers",
2172 shader_names[i]);
2173 }
2174
2175 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002176 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2177 linker_warning(prog, "Too many %s shader uniform components, "
2178 "but the driver will try to optimize them out; "
2179 "this is non-portable out-of-spec behavior\n",
2180 shader_names[i]);
2181 } else {
2182 linker_error(prog, "Too many %s shader uniform components",
2183 shader_names[i]);
2184 }
Ian Romanick92f81592011-11-08 12:37:19 -08002185 }
2186 }
2187
2188 return prog->LinkStatus;
2189}
Paul Berry871ddb92011-11-05 11:17:32 -07002190
Ian Romanick0e59b262010-06-23 11:23:01 -07002191void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002192link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002193{
Paul Berry871ddb92011-11-05 11:17:32 -07002194 tfeedback_decl *tfeedback_decls = NULL;
2195 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2196
Kenneth Graunked3073f52011-01-21 14:32:31 -08002197 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002198
Ian Romanick832dfa52010-06-17 15:04:20 -07002199 prog->LinkStatus = false;
2200 prog->Validated = false;
2201 prog->_Used = false;
2202
Ian Romanickf36460e2010-06-23 12:07:22 -07002203 if (prog->InfoLog != NULL)
Kenneth Graunked3073f52011-01-21 14:32:31 -08002204 ralloc_free(prog->InfoLog);
Ian Romanickf36460e2010-06-23 12:07:22 -07002205
Kenneth Graunked3073f52011-01-21 14:32:31 -08002206 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002207
Ian Romanick832dfa52010-06-17 15:04:20 -07002208 /* Separate the shaders into groups based on their type.
2209 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002210 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002211 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002212 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002213 unsigned num_frag_shaders = 0;
2214
Eric Anholt16b68b12010-06-30 11:05:43 -07002215 vert_shader_list = (struct gl_shader **)
2216 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002217 frag_shader_list = &vert_shader_list[prog->NumShaders];
2218
Ian Romanick25f51d32010-07-16 15:51:50 -07002219 unsigned min_version = UINT_MAX;
2220 unsigned max_version = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07002221 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002222 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2223 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2224
Ian Romanick832dfa52010-06-17 15:04:20 -07002225 switch (prog->Shaders[i]->Type) {
2226 case GL_VERTEX_SHADER:
2227 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2228 num_vert_shaders++;
2229 break;
2230 case GL_FRAGMENT_SHADER:
2231 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2232 num_frag_shaders++;
2233 break;
2234 case GL_GEOMETRY_SHADER:
2235 /* FINISHME: Support geometry shaders. */
2236 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2237 break;
2238 }
2239 }
2240
Ian Romanick25f51d32010-07-16 15:51:50 -07002241 /* Previous to GLSL version 1.30, different compilation units could mix and
2242 * match shading language versions. With GLSL 1.30 and later, the versions
2243 * of all shaders must match.
2244 */
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002245 assert(min_version >= 100);
Eric Anholtc5ff9a82012-03-08 13:49:15 -08002246 assert(max_version <= 140);
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002247 if ((max_version >= 130 || min_version == 100)
2248 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002249 linker_error(prog, "all shaders must use same shading "
2250 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002251 goto done;
2252 }
2253
2254 prog->Version = max_version;
2255
Ian Romanick3322fba2010-10-14 13:28:42 -07002256 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2257 if (prog->_LinkedShaders[i] != NULL)
2258 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2259
2260 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002261 }
2262
Ian Romanickcd6764e2010-07-16 16:00:07 -07002263 /* Link all shaders for a particular stage and validate the result.
2264 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002265 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002266 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002267 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2268 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002269
2270 if (sh == NULL)
2271 goto done;
2272
2273 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002274 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002275
Ian Romanick3322fba2010-10-14 13:28:42 -07002276 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2277 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002278 }
2279
2280 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002281 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002282 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2283 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002284
2285 if (sh == NULL)
2286 goto done;
2287
2288 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002289 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002290
Ian Romanick3322fba2010-10-14 13:28:42 -07002291 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2292 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002293 }
2294
Ian Romanick3ed850e2010-06-23 12:18:21 -07002295 /* Here begins the inter-stage linking phase. Some initial validation is
2296 * performed, then locations are assigned for uniforms, attributes, and
2297 * varyings.
2298 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002299 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002300 unsigned prev;
2301
2302 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2303 if (prog->_LinkedShaders[prev] != NULL)
2304 break;
2305 }
2306
Bryan Cainf18a0862011-04-23 19:29:15 -05002307 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002308 * stage.
2309 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002310 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2311 if (prog->_LinkedShaders[i] == NULL)
2312 continue;
2313
Ian Romanickf36460e2010-06-23 12:07:22 -07002314 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002315 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002316 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002317 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002318
2319 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002320 }
2321
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002322 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002323 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002324
Eric Anholt3de13952012-05-04 13:08:46 -07002325 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2326 * it before optimization because we want most of the checks to get
2327 * dropped thanks to constant propagation.
2328 */
2329 if (max_version >= 130) {
2330 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2331 if (sh) {
2332 lower_discard_flow(sh->ir);
2333 }
2334 }
2335
Eric Anholt2f4fe152010-08-10 13:06:49 -07002336 /* Do common optimization before assigning storage for attributes,
2337 * uniforms, and varyings. Later optimization could possibly make
2338 * some of that unused.
2339 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002340 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2341 if (prog->_LinkedShaders[i] == NULL)
2342 continue;
2343
Ian Romanick02c5ae12011-07-11 10:46:01 -07002344 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2345 if (!prog->LinkStatus)
2346 goto done;
2347
Paul Berryc06e3252011-08-11 20:58:21 -07002348 if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
2349 lower_clip_distance(prog->_LinkedShaders[i]->ir);
2350
Brian Paul7feabfe2012-03-20 17:43:12 -06002351 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2352
2353 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002354 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002355 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002356
Ian Romanickd32d4f72011-06-27 17:59:58 -07002357 /* FINISHME: The value of the max_attribute_index parameter is
2358 * FINISHME: implementation dependent based on the value of
2359 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2360 * FINISHME: at least 16, so hardcode 16 for now.
2361 */
2362 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002363 goto done;
2364 }
2365
Dave Airlie1256a5d2012-03-24 13:33:41 +00002366 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002367 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002368 }
2369
Ian Romanick3322fba2010-10-14 13:28:42 -07002370 unsigned prev;
2371 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2372 if (prog->_LinkedShaders[prev] != NULL)
2373 break;
2374 }
2375
Paul Berry871ddb92011-11-05 11:17:32 -07002376 if (num_tfeedback_decls != 0) {
2377 /* From GL_EXT_transform_feedback:
2378 * A program will fail to link if:
2379 *
2380 * * the <count> specified by TransformFeedbackVaryingsEXT is
2381 * non-zero, but the program object has no vertex or geometry
2382 * shader;
2383 */
2384 if (prev >= MESA_SHADER_FRAGMENT) {
2385 linker_error(prog, "Transform feedback varyings specified, but "
2386 "no vertex or geometry shader is present.");
2387 goto done;
2388 }
2389
2390 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2391 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002392 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002393 prog->TransformFeedback.VaryingNames,
2394 tfeedback_decls))
2395 goto done;
2396 }
2397
Ian Romanick3322fba2010-10-14 13:28:42 -07002398 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2399 if (prog->_LinkedShaders[i] == NULL)
2400 continue;
2401
Paul Berry871ddb92011-11-05 11:17:32 -07002402 if (!assign_varying_locations(
2403 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2404 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2405 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002406 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002407
Ian Romanick3322fba2010-10-14 13:28:42 -07002408 prev = i;
2409 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002410
Paul Berry871ddb92011-11-05 11:17:32 -07002411 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2412 /* There was no fragment shader, but we still have to assign varying
2413 * locations for use by transform feedback.
2414 */
2415 if (!assign_varying_locations(
2416 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2417 tfeedback_decls))
2418 goto done;
2419 }
2420
2421 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2422 goto done;
2423
Ian Romanickcc90e622010-10-19 17:59:10 -07002424 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2425 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2426 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002427
2428 /* Eliminate code that is now dead due to unused vertex outputs being
2429 * demoted.
2430 */
2431 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2432 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002433 }
2434
2435 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2436 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2437
2438 demote_shader_inputs_and_outputs(sh, ir_var_in);
2439 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2440 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002441
2442 /* Eliminate code that is now dead due to unused geometry outputs being
2443 * demoted.
2444 */
2445 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2446 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002447 }
2448
2449 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2450 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2451
2452 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002453
2454 /* Eliminate code that is now dead due to unused fragment inputs being
2455 * demoted. This shouldn't actually do anything other than remove
2456 * declarations of the (now unused) global variables.
2457 */
2458 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2459 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002460 }
2461
Ian Romanick960d7222011-10-21 11:21:02 -07002462 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002463 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002464 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002465
Ian Romanick92f81592011-11-08 12:37:19 -08002466 if (!check_resources(ctx, prog))
2467 goto done;
2468
Ian Romanickce9171f2011-02-03 17:10:14 -08002469 /* OpenGL ES requires that a vertex shader and a fragment shader both be
2470 * present in a linked program. By checking for use of shading language
2471 * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
2472 */
Eric Anholt57f79782011-07-22 12:57:47 -07002473 if (!prog->InternalSeparateShader &&
2474 (ctx->API == API_OPENGLES2 || prog->Version == 100)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002475 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002476 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002477 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002478 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002479 }
2480 }
2481
Ian Romanick13e10e42010-06-21 12:03:24 -07002482 /* FINISHME: Assign fragment shader output locations. */
2483
Ian Romanick832dfa52010-06-17 15:04:20 -07002484done:
2485 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002486
2487 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2488 if (prog->_LinkedShaders[i] == NULL)
2489 continue;
2490
2491 /* Retain any live IR, but trash the rest. */
2492 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002493
2494 /* The symbol table in the linked shaders may contain references to
2495 * variables that were removed (e.g., unused uniforms). Since it may
2496 * contain junk, there is no possible valid use. Delete it and set the
2497 * pointer to NULL.
2498 */
2499 delete prog->_LinkedShaders[i]->symbols;
2500 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002501 }
2502
Kenneth Graunked3073f52011-01-21 14:32:31 -08002503 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002504}