blob: 55d23d1c34261c3d9c6585062a04297e36d92833 [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
Paul Berry50895d42012-12-05 07:17:07 -0800203link_invalidate_variable_locations(gl_shader *sh, int input_base,
204 int output_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
Paul Berry50895d42012-12-05 07:17:07 -0800209 if (var == NULL)
210 continue;
211
212 int base;
213 switch (var->mode) {
214 case ir_var_in:
215 base = input_base;
216 break;
217 case ir_var_out:
218 base = output_base;
219 break;
220 default:
221 continue;
222 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700223
224 /* Only assign locations for generic attributes / varyings / etc.
225 */
Paul Berry50895d42012-12-05 07:17:07 -0800226 if ((var->location >= base) && !var->explicit_location)
227 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800228
Paul Berry3e81c662012-12-05 10:47:55 -0800229 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800230 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800231 var->location_frac = 0;
232 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800233 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800234 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700235 }
236}
237
238
Ian Romanickc93b8f12010-06-17 15:20:22 -0700239/**
Ian Romanick69846702010-06-22 17:29:19 -0700240 * Determine the number of attribute slots required for a particular type
241 *
242 * This code is here because it implements the language rules of a specific
243 * GLSL version. Since it's a property of the language and not a property of
244 * types in general, it doesn't really belong in glsl_type.
245 */
246unsigned
247count_attribute_slots(const glsl_type *t)
248{
249 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
250 *
251 * "A scalar input counts the same amount against this limit as a vec4,
252 * so applications may want to consider packing groups of four
253 * unrelated float inputs together into a vector to better utilize the
254 * capabilities of the underlying hardware. A matrix input will use up
255 * multiple locations. The number of locations used will equal the
256 * number of columns in the matrix."
257 *
258 * The spec does not explicitly say how arrays are counted. However, it
259 * should be safe to assume the total number of slots consumed by an array
260 * is the number of entries in the array multiplied by the number of slots
261 * consumed by a single element of the array.
262 */
263
264 if (t->is_array())
265 return t->array_size() * count_attribute_slots(t->element_type());
266
267 if (t->is_matrix())
268 return t->matrix_columns;
269
270 return 1;
271}
272
273
274/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700275 * Verify that a vertex shader executable meets all semantic requirements.
276 *
Paul Berry642e5b412012-01-04 13:57:52 -0800277 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
278 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700279 *
280 * \param shader Vertex shader executable to be verified
281 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700282bool
Eric Anholt849e1812010-06-30 11:49:17 -0700283validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700284 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700285{
286 if (shader == NULL)
287 return true;
288
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700289 /* From the GLSL 1.10 spec, page 48:
290 *
291 * "The variable gl_Position is available only in the vertex
292 * language and is intended for writing the homogeneous vertex
293 * position. All executions of a well-formed vertex shader
294 * executable must write a value into this variable. [...] The
295 * variable gl_Position is available only in the vertex
296 * language and is intended for writing the homogeneous vertex
297 * position. All executions of a well-formed vertex shader
298 * executable must write a value into this variable."
299 *
300 * while in GLSL 1.40 this text is changed to:
301 *
302 * "The variable gl_Position is available only in the vertex
303 * language and is intended for writing the homogeneous vertex
304 * position. It can be written at any time during shader
305 * execution. It may also be read back by a vertex shader
306 * after being written. This value will be used by primitive
307 * assembly, clipping, culling, and other fixed functionality
308 * operations, if present, that operate on primitives after
309 * vertex processing has occurred. Its value is undefined if
310 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700311 *
312 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
313 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700314 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700315 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700316 find_assignment_visitor find("gl_Position");
317 find.run(shader->ir);
318 if (!find.variable_found()) {
319 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
320 return false;
321 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700322 }
323
Paul Berry642e5b412012-01-04 13:57:52 -0800324 prog->Vert.ClipDistanceArraySize = 0;
325
Paul Berry15ba2a52012-08-02 17:51:02 -0700326 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700327 /* From section 7.1 (Vertex Shader Special Variables) of the
328 * GLSL 1.30 spec:
329 *
330 * "It is an error for a shader to statically write both
331 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700332 *
333 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
334 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700335 */
336 find_assignment_visitor clip_vertex("gl_ClipVertex");
337 find_assignment_visitor clip_distance("gl_ClipDistance");
338
339 clip_vertex.run(shader->ir);
340 clip_distance.run(shader->ir);
341 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
342 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
343 "and `gl_ClipDistance'\n");
344 return false;
345 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700346 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800347 ir_variable *clip_distance_var =
348 shader->symbols->get_variable("gl_ClipDistance");
349 if (clip_distance_var)
350 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700351 }
352
Ian Romanick832dfa52010-06-17 15:04:20 -0700353 return true;
354}
355
356
Ian Romanickc93b8f12010-06-17 15:20:22 -0700357/**
358 * Verify that a fragment shader executable meets all semantic requirements
359 *
360 * \param shader Fragment shader executable to be verified
361 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700362bool
Eric Anholt849e1812010-06-30 11:49:17 -0700363validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700364 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700365{
366 if (shader == NULL)
367 return true;
368
Ian Romanick832dfa52010-06-17 15:04:20 -0700369 find_assignment_visitor frag_color("gl_FragColor");
370 find_assignment_visitor frag_data("gl_FragData");
371
Eric Anholt16b68b12010-06-30 11:05:43 -0700372 frag_color.run(shader->ir);
373 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700374
Ian Romanick832dfa52010-06-17 15:04:20 -0700375 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700376 linker_error(prog, "fragment shader writes to both "
377 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700378 return false;
379 }
380
381 return true;
382}
383
384
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700385/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700386 * Generate a string describing the mode of a variable
387 */
388static const char *
389mode_string(const ir_variable *var)
390{
391 switch (var->mode) {
392 case ir_var_auto:
393 return (var->read_only) ? "global constant" : "global variable";
394
395 case ir_var_uniform: return "uniform";
396 case ir_var_in: return "shader input";
397 case ir_var_out: return "shader output";
398 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700399
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800400 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700401 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700402 default:
403 assert(!"Should not get here.");
404 return "invalid variable";
405 }
406}
407
408
409/**
410 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700411 */
412bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700413cross_validate_globals(struct gl_shader_program *prog,
414 struct gl_shader **shader_list,
415 unsigned num_shaders,
416 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700417{
418 /* Examine all of the uniforms in all of the shaders and cross validate
419 * them.
420 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700421 glsl_symbol_table variables;
422 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700423 if (shader_list[i] == NULL)
424 continue;
425
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700426 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700427 ir_variable *const var = ((ir_instruction *) node)->as_variable();
428
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700429 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700430 continue;
431
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700432 if (uniforms_only && (var->mode != ir_var_uniform))
433 continue;
434
Ian Romanick7e2aa912010-07-19 17:12:42 -0700435 /* Don't cross validate temporaries that are at global scope. These
436 * will eventually get pulled into the shaders 'main'.
437 */
438 if (var->mode == ir_var_temporary)
439 continue;
440
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700441 /* If a global with this name has already been seen, verify that the
442 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700443 * initializers, the values of the initializers must be the same.
444 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700445 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700446 if (existing != NULL) {
447 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700448 /* Consider the types to be "the same" if both types are arrays
449 * of the same type and one of the arrays is implicitly sized.
450 * In addition, set the type of the linked variable to the
451 * explicitly sized array.
452 */
453 if (var->type->is_array()
454 && existing->type->is_array()
455 && (var->type->fields.array == existing->type->fields.array)
456 && ((var->type->length == 0)
457 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800458 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700459 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800460 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700461 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700462 linker_error(prog, "%s `%s' declared as type "
463 "`%s' and type `%s'\n",
464 mode_string(var),
465 var->name, var->type->name,
466 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700467 return false;
468 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700469 }
470
Ian Romanick68a4fc92010-10-07 17:21:22 -0700471 if (var->explicit_location) {
472 if (existing->explicit_location
473 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700474 linker_error(prog, "explicit locations for %s "
475 "`%s' have differing values\n",
476 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700477 return false;
478 }
479
480 existing->location = var->location;
481 existing->explicit_location = true;
482 }
483
Ian Romanick46173f92011-10-31 13:07:06 -0700484 /* Validate layout qualifiers for gl_FragDepth.
485 *
486 * From the AMD/ARB_conservative_depth specs:
487 *
488 * "If gl_FragDepth is redeclared in any fragment shader in a
489 * program, it must be redeclared in all fragment shaders in
490 * that program that have static assignments to
491 * gl_FragDepth. All redeclarations of gl_FragDepth in all
492 * fragment shaders in a single program must have the same set
493 * of qualifiers."
494 */
495 if (strcmp(var->name, "gl_FragDepth") == 0) {
496 bool layout_declared = var->depth_layout != ir_depth_layout_none;
497 bool layout_differs =
498 var->depth_layout != existing->depth_layout;
499
500 if (layout_declared && layout_differs) {
501 linker_error(prog,
502 "All redeclarations of gl_FragDepth in all "
503 "fragment shaders in a single program must have "
504 "the same set of qualifiers.");
505 }
506
507 if (var->used && layout_differs) {
508 linker_error(prog,
509 "If gl_FragDepth is redeclared with a layout "
510 "qualifier in any fragment shader, it must be "
511 "redeclared with the same layout qualifier in "
512 "all fragment shaders that have assignments to "
513 "gl_FragDepth");
514 }
515 }
Chad Versaceaddae332011-01-27 01:40:31 -0800516
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700517 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
518 *
519 * "If a shared global has multiple initializers, the
520 * initializers must all be constant expressions, and they
521 * must all have the same value. Otherwise, a link error will
522 * result. (A shared global having only one initializer does
523 * not require that initializer to be a constant expression.)"
524 *
525 * Previous to 4.20 the GLSL spec simply said that initializers
526 * must have the same value. In this case of non-constant
527 * initializers, this was impossible to determine. As a result,
528 * no vendor actually implemented that behavior. The 4.20
529 * behavior matches the implemented behavior of at least one other
530 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700531 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700532 if (var->constant_initializer != NULL) {
533 if (existing->constant_initializer != NULL) {
534 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700535 linker_error(prog, "initializers for %s "
536 "`%s' have differing values\n",
537 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700538 return false;
539 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700540 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700541 /* If the first-seen instance of a particular uniform did not
542 * have an initializer but a later instance does, copy the
543 * initializer to the version stored in the symbol table.
544 */
Ian Romanickde415b72010-07-14 13:22:12 -0700545 /* FINISHME: This is wrong. The constant_value field should
546 * FINISHME: not be modified! Imagine a case where a shader
547 * FINISHME: without an initializer is linked in two different
548 * FINISHME: programs with shaders that have differing
549 * FINISHME: initializers. Linking with the first will
550 * FINISHME: modify the shader, and linking with the second
551 * FINISHME: will fail.
552 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700553 existing->constant_initializer =
554 var->constant_initializer->clone(ralloc_parent(existing),
555 NULL);
556 }
557 }
558
559 if (var->has_initializer) {
560 if (existing->has_initializer
561 && (var->constant_initializer == NULL
562 || existing->constant_initializer == NULL)) {
563 linker_error(prog,
564 "shared global variable `%s' has multiple "
565 "non-constant initializers.\n",
566 var->name);
567 return false;
568 }
569
570 /* Some instance had an initializer, so keep track of that. In
571 * this location, all sorts of initializers (constant or
572 * otherwise) will propagate the existence to the variable
573 * stored in the symbol table.
574 */
575 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700576 }
Chad Versace7528f142010-11-17 14:34:38 -0800577
578 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700579 linker_error(prog, "declarations for %s `%s' have "
580 "mismatching invariant qualifiers\n",
581 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800582 return false;
583 }
Chad Versace61428dd2011-01-10 15:29:30 -0800584 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700585 linker_error(prog, "declarations for %s `%s' have "
586 "mismatching centroid qualifiers\n",
587 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800588 return false;
589 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700590 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700591 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700592 }
593 }
594
595 return true;
596}
597
598
Ian Romanick37101922010-06-18 19:02:10 -0700599/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700600 * Perform validation of uniforms used across multiple shader stages
601 */
602bool
603cross_validate_uniforms(struct gl_shader_program *prog)
604{
605 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700606 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700607}
608
Eric Anholtf609cf72012-04-27 13:52:56 -0700609/**
610 * Accumulates the array of prog->UniformBlocks and checks that all
611 * definitons of blocks agree on their contents.
612 */
613static bool
614interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
615{
616 unsigned max_num_uniform_blocks = 0;
617 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
618 if (prog->_LinkedShaders[i])
619 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
620 }
621
622 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
623 struct gl_shader *sh = prog->_LinkedShaders[i];
624
625 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
626 max_num_uniform_blocks);
627 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
628 prog->UniformBlockStageIndex[i][j] = -1;
629
630 if (sh == NULL)
631 continue;
632
633 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
634 int index = link_cross_validate_uniform_block(prog,
635 &prog->UniformBlocks,
636 &prog->NumUniformBlocks,
637 &sh->UniformBlocks[j]);
638
639 if (index == -1) {
640 linker_error(prog, "uniform block `%s' has mismatching definitions",
641 sh->UniformBlocks[j].Name);
642 return false;
643 }
644
645 prog->UniformBlockStageIndex[i][index] = j;
646 }
647 }
648
649 return true;
650}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700651
652/**
Ian Romanick37101922010-06-18 19:02:10 -0700653 * Validate that outputs from one stage match inputs of another
654 */
655bool
Eric Anholt849e1812010-06-30 11:49:17 -0700656cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700657 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700658{
659 glsl_symbol_table parameters;
660 /* FINISHME: Figure these out dynamically. */
661 const char *const producer_stage = "vertex";
662 const char *const consumer_stage = "fragment";
663
664 /* Find all shader outputs in the "producer" stage.
665 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700666 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700667 ir_variable *const var = ((ir_instruction *) node)->as_variable();
668
669 /* FINISHME: For geometry shaders, this should also look for inout
670 * FINISHME: variables.
671 */
672 if ((var == NULL) || (var->mode != ir_var_out))
673 continue;
674
Eric Anholt001eee52010-11-05 06:11:24 -0700675 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700676 }
677
678
679 /* Find all shader inputs in the "consumer" stage. Any variables that have
680 * matching outputs already in the symbol table must have the same type and
681 * qualifiers.
682 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700683 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700684 ir_variable *const input = ((ir_instruction *) node)->as_variable();
685
686 /* FINISHME: For geometry shaders, this should also look for inout
687 * FINISHME: variables.
688 */
689 if ((input == NULL) || (input->mode != ir_var_in))
690 continue;
691
692 ir_variable *const output = parameters.get_variable(input->name);
693 if (output != NULL) {
694 /* Check that the types match between stages.
695 */
696 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800697 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500698 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800699 * access it must redeclare it with a size. There is some
700 * language in the GLSL spec that implies the fragment shader
701 * and vertex shader do not have to agree on this size. Other
702 * driver behave this way, and one or two applications seem to
703 * rely on it.
704 *
705 * Neither declaration needs to be modified here because the array
706 * sizes are fixed later when update_array_sizes is called.
707 *
708 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
709 *
710 * "Unlike user-defined varying variables, the built-in
711 * varying variables don't have a strict one-to-one
712 * correspondence between the vertex language and the
713 * fragment language."
714 */
715 if (!output->type->is_array()
716 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700717 linker_error(prog,
718 "%s shader output `%s' declared as type `%s', "
719 "but %s shader input declared as type `%s'\n",
720 producer_stage, output->name,
721 output->type->name,
722 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800723 return false;
724 }
Ian Romanick37101922010-06-18 19:02:10 -0700725 }
726
727 /* Check that all of the qualifiers match between stages.
728 */
729 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700730 linker_error(prog,
731 "%s shader output `%s' %s centroid qualifier, "
732 "but %s shader input %s centroid qualifier\n",
733 producer_stage,
734 output->name,
735 (output->centroid) ? "has" : "lacks",
736 consumer_stage,
737 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700738 return false;
739 }
740
741 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700742 linker_error(prog,
743 "%s shader output `%s' %s invariant qualifier, "
744 "but %s shader input %s invariant qualifier\n",
745 producer_stage,
746 output->name,
747 (output->invariant) ? "has" : "lacks",
748 consumer_stage,
749 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700750 return false;
751 }
752
753 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700754 linker_error(prog,
755 "%s shader output `%s' specifies %s "
756 "interpolation qualifier, "
757 "but %s shader input specifies %s "
758 "interpolation qualifier\n",
759 producer_stage,
760 output->name,
761 output->interpolation_string(),
762 consumer_stage,
763 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700764 return false;
765 }
766 }
767 }
768
769 return true;
770}
771
772
Ian Romanick3fb87872010-07-09 14:09:34 -0700773/**
774 * Populates a shaders symbol table with all global declarations
775 */
776static void
777populate_symbol_table(gl_shader *sh)
778{
779 sh->symbols = new(sh) glsl_symbol_table;
780
781 foreach_list(node, sh->ir) {
782 ir_instruction *const inst = (ir_instruction *) node;
783 ir_variable *var;
784 ir_function *func;
785
786 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700787 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700788 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700789 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700790 }
791 }
792}
793
794
795/**
Ian Romanick31a97862010-07-12 18:48:50 -0700796 * Remap variables referenced in an instruction tree
797 *
798 * This is used when instruction trees are cloned from one shader and placed in
799 * another. These trees will contain references to \c ir_variable nodes that
800 * do not exist in the target shader. This function finds these \c ir_variable
801 * references and replaces the references with matching variables in the target
802 * shader.
803 *
804 * If there is no matching variable in the target shader, a clone of the
805 * \c ir_variable is made and added to the target shader. The new variable is
806 * added to \b both the instruction stream and the symbol table.
807 *
808 * \param inst IR tree that is to be processed.
809 * \param symbols Symbol table containing global scope symbols in the
810 * linked shader.
811 * \param instructions Instruction stream where new variable declarations
812 * should be added.
813 */
814void
Eric Anholt8273bd42010-08-04 12:34:56 -0700815remap_variables(ir_instruction *inst, struct gl_shader *target,
816 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700817{
818 class remap_visitor : public ir_hierarchical_visitor {
819 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700820 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700821 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700822 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700823 this->target = target;
824 this->symbols = target->symbols;
825 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700826 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700827 }
828
829 virtual ir_visitor_status visit(ir_dereference_variable *ir)
830 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700831 if (ir->var->mode == ir_var_temporary) {
832 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
833
834 assert(var != NULL);
835 ir->var = var;
836 return visit_continue;
837 }
838
Ian Romanick31a97862010-07-12 18:48:50 -0700839 ir_variable *const existing =
840 this->symbols->get_variable(ir->var->name);
841 if (existing != NULL)
842 ir->var = existing;
843 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700844 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700845
Eric Anholt001eee52010-11-05 06:11:24 -0700846 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700847 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700848 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700849 }
850
851 return visit_continue;
852 }
853
854 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700855 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700856 glsl_symbol_table *symbols;
857 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700858 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700859 };
860
Eric Anholt8273bd42010-08-04 12:34:56 -0700861 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700862
863 inst->accept(&v);
864}
865
866
867/**
868 * Move non-declarations from one instruction stream to another
869 *
870 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700871 * 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 -0700872 * pointer) for \c last and \c false for \c make_copies on the first
873 * call. Successive calls pass the return value of the previous call for
874 * \c last and \c true for \c make_copies.
875 *
876 * \param instructions Source instruction stream
877 * \param last Instruction after which new instructions should be
878 * inserted in the target instruction stream
879 * \param make_copies Flag selecting whether instructions in \c instructions
880 * should be copied (via \c ir_instruction::clone) into the
881 * target list or moved.
882 *
883 * \return
884 * The new "last" instruction in the target instruction stream. This pointer
885 * is suitable for use as the \c last parameter of a later call to this
886 * function.
887 */
888exec_node *
889move_non_declarations(exec_list *instructions, exec_node *last,
890 bool make_copies, gl_shader *target)
891{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700892 hash_table *temps = NULL;
893
894 if (make_copies)
895 temps = hash_table_ctor(0, hash_table_pointer_hash,
896 hash_table_pointer_compare);
897
Ian Romanick303c99f2010-07-19 12:34:56 -0700898 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700899 ir_instruction *inst = (ir_instruction *) node;
900
Ian Romanick7e2aa912010-07-19 17:12:42 -0700901 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700902 continue;
903
Ian Romanick7e2aa912010-07-19 17:12:42 -0700904 ir_variable *var = inst->as_variable();
905 if ((var != NULL) && (var->mode != ir_var_temporary))
906 continue;
907
908 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700909 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700910 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700911 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700912
913 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700914 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700915
916 if (var != NULL)
917 hash_table_insert(temps, inst, var);
918 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700919 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700920 } else {
921 inst->remove();
922 }
923
924 last->insert_after(inst);
925 last = inst;
926 }
927
Ian Romanick7e2aa912010-07-19 17:12:42 -0700928 if (make_copies)
929 hash_table_dtor(temps);
930
Ian Romanick31a97862010-07-12 18:48:50 -0700931 return last;
932}
933
934/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700935 * Get the function signature for main from a shader
936 */
937static ir_function_signature *
938get_main_function_signature(gl_shader *sh)
939{
940 ir_function *const f = sh->symbols->get_function("main");
941 if (f != NULL) {
942 exec_list void_parameters;
943
944 /* Look for the 'void main()' signature and ensure that it's defined.
945 * This keeps the linker from accidentally pick a shader that just
946 * contains a prototype for main.
947 *
948 * We don't have to check for multiple definitions of main (in multiple
949 * shaders) because that would have already been caught above.
950 */
951 ir_function_signature *sig = f->matching_signature(&void_parameters);
952 if ((sig != NULL) && sig->is_defined) {
953 return sig;
954 }
955 }
956
957 return NULL;
958}
959
960
961/**
Brian Paul84a12732012-02-02 20:10:40 -0700962 * This class is only used in link_intrastage_shaders() below but declaring
963 * it inside that function leads to compiler warnings with some versions of
964 * gcc.
965 */
966class array_sizing_visitor : public ir_hierarchical_visitor {
967public:
968 virtual ir_visitor_status visit(ir_variable *var)
969 {
970 if (var->type->is_array() && (var->type->length == 0)) {
971 const glsl_type *type =
972 glsl_type::get_array_instance(var->type->fields.array,
973 var->max_array_access + 1);
974 assert(type != NULL);
975 var->type = type;
976 }
977 return visit_continue;
978 }
979};
980
Brian Paul84a12732012-02-02 20:10:40 -0700981/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700982 * Combine a group of shaders for a single stage to generate a linked shader
983 *
984 * \note
985 * If this function is supplied a single shader, it is cloned, and the new
986 * shader is returned.
987 */
988static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800989link_intrastage_shaders(void *mem_ctx,
990 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700991 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700992 struct gl_shader **shader_list,
993 unsigned num_shaders)
994{
Eric Anholtf609cf72012-04-27 13:52:56 -0700995 struct gl_uniform_block *uniform_blocks = NULL;
996 unsigned num_uniform_blocks = 0;
997
Ian Romanick13f782c2010-06-29 18:53:38 -0700998 /* Check that global variables defined in multiple shaders are consistent.
999 */
1000 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
1001 return NULL;
1002
Eric Anholtf609cf72012-04-27 13:52:56 -07001003 /* Check that uniform blocks between shaders for a stage agree. */
1004 for (unsigned i = 0; i < num_shaders; i++) {
1005 struct gl_shader *sh = shader_list[i];
1006
1007 for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) {
Eric Anholt8ab58422012-05-01 15:10:14 -07001008 link_assign_uniform_block_offsets(shader_list[i]);
1009
Eric Anholtf609cf72012-04-27 13:52:56 -07001010 int index = link_cross_validate_uniform_block(mem_ctx,
1011 &uniform_blocks,
1012 &num_uniform_blocks,
1013 &sh->UniformBlocks[j]);
1014 if (index == -1) {
1015 linker_error(prog, "uniform block `%s' has mismatching definitions",
1016 sh->UniformBlocks[j].Name);
1017 return NULL;
1018 }
1019 }
1020 }
1021
Ian Romanick13f782c2010-06-29 18:53:38 -07001022 /* Check that there is only a single definition of each function signature
1023 * across all shaders.
1024 */
1025 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1026 foreach_list(node, shader_list[i]->ir) {
1027 ir_function *const f = ((ir_instruction *) node)->as_function();
1028
1029 if (f == NULL)
1030 continue;
1031
1032 for (unsigned j = i + 1; j < num_shaders; j++) {
1033 ir_function *const other =
1034 shader_list[j]->symbols->get_function(f->name);
1035
1036 /* If the other shader has no function (and therefore no function
1037 * signatures) with the same name, skip to the next shader.
1038 */
1039 if (other == NULL)
1040 continue;
1041
1042 foreach_iter (exec_list_iterator, iter, *f) {
1043 ir_function_signature *sig =
1044 (ir_function_signature *) iter.get();
1045
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001046 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -07001047 continue;
1048
1049 ir_function_signature *other_sig =
1050 other->exact_matching_signature(& sig->parameters);
1051
1052 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001053 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -07001054 linker_error(prog, "function `%s' is multiply defined",
1055 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001056 return NULL;
1057 }
1058 }
1059 }
1060 }
1061 }
1062
1063 /* Find the shader that defines main, and make a clone of it.
1064 *
1065 * Starting with the clone, search for undefined references. If one is
1066 * found, find the shader that defines it. Clone the reference and add
1067 * it to the shader. Repeat until there are no undefined references or
1068 * until a reference cannot be resolved.
1069 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001070 gl_shader *main = NULL;
1071 for (unsigned i = 0; i < num_shaders; i++) {
1072 if (get_main_function_signature(shader_list[i]) != NULL) {
1073 main = shader_list[i];
1074 break;
1075 }
1076 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001077
Ian Romanick15ce87e2010-07-09 15:28:22 -07001078 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001079 linker_error(prog, "%s shader lacks `main'\n",
1080 (shader_list[0]->Type == GL_VERTEX_SHADER)
1081 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -07001082 return NULL;
1083 }
1084
Ian Romanick4a455952010-10-13 15:13:02 -07001085 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001086 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001087 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001088
Eric Anholtf609cf72012-04-27 13:52:56 -07001089 linked->UniformBlocks = uniform_blocks;
1090 linked->NumUniformBlocks = num_uniform_blocks;
1091 ralloc_steal(linked, linked->UniformBlocks);
1092
Ian Romanick15ce87e2010-07-09 15:28:22 -07001093 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001094
Ian Romanick31a97862010-07-12 18:48:50 -07001095 /* The a pointer to the main function in the final linked shader (i.e., the
1096 * copy of the original shader that contained the main function).
1097 */
1098 ir_function_signature *const main_sig = get_main_function_signature(linked);
1099
1100 /* Move any instructions other than variable declarations or function
1101 * declarations into main.
1102 */
Ian Romanick9303e352010-07-19 12:33:54 -07001103 exec_node *insertion_point =
1104 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1105 linked);
1106
Ian Romanick31a97862010-07-12 18:48:50 -07001107 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001108 if (shader_list[i] == main)
1109 continue;
1110
Ian Romanick31a97862010-07-12 18:48:50 -07001111 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001112 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001113 }
1114
Ian Romanick13f782c2010-06-29 18:53:38 -07001115 /* Resolve initializers for global variables in the linked shader.
1116 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001117 unsigned num_linking_shaders = num_shaders;
1118 for (unsigned i = 0; i < num_shaders; i++)
1119 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1120
1121 gl_shader **linking_shaders =
1122 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1123
1124 memcpy(linking_shaders, shader_list,
1125 sizeof(linking_shaders[0]) * num_shaders);
1126
1127 unsigned idx = num_shaders;
1128 for (unsigned i = 0; i < num_shaders; i++) {
1129 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1130 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1131 idx += shader_list[i]->num_builtins_to_link;
1132 }
1133
1134 assert(idx == num_linking_shaders);
1135
Ian Romanick4a455952010-10-13 15:13:02 -07001136 if (!link_function_calls(prog, linked, linking_shaders,
1137 num_linking_shaders)) {
1138 ctx->Driver.DeleteShader(ctx, linked);
1139 linked = NULL;
1140 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001141
1142 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001143
Paul Berryc148ef62011-08-03 15:37:01 -07001144#ifdef DEBUG
1145 /* At this point linked should contain all of the linked IR, so
1146 * validate it to make sure nothing went wrong.
1147 */
1148 if (linked)
1149 validate_ir_tree(linked->ir);
1150#endif
1151
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001152 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001153 * unspecified sizes have a size specified. The size is inferred from the
1154 * max_array_access field.
1155 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001156 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001157 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001158
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001159 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001160 }
1161
Ian Romanick3fb87872010-07-09 14:09:34 -07001162 return linked;
1163}
1164
Eric Anholta721abf2010-08-23 10:32:01 -07001165/**
1166 * Update the sizes of linked shader uniform arrays to the maximum
1167 * array index used.
1168 *
1169 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1170 *
1171 * If one or more elements of an array are active,
1172 * GetActiveUniform will return the name of the array in name,
1173 * subject to the restrictions listed above. The type of the array
1174 * is returned in type. The size parameter contains the highest
1175 * array element index used, plus one. The compiler or linker
1176 * determines the highest index used. There will be only one
1177 * active uniform reported by the GL per uniform array.
1178
1179 */
1180static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001181update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001182{
Ian Romanick3322fba2010-10-14 13:28:42 -07001183 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1184 if (prog->_LinkedShaders[i] == NULL)
1185 continue;
1186
Eric Anholta721abf2010-08-23 10:32:01 -07001187 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1188 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1189
Eric Anholt586b4b52010-09-28 14:32:16 -07001190 if ((var == NULL) || (var->mode != ir_var_uniform &&
1191 var->mode != ir_var_in &&
1192 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001193 !var->type->is_array())
1194 continue;
1195
Eric Anholt9feb4032012-05-01 14:43:31 -07001196 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1197 * will not be eliminated. Since we always do std140, just
1198 * don't resize arrays in UBOs.
1199 */
1200 if (var->uniform_block != -1)
1201 continue;
1202
Eric Anholta721abf2010-08-23 10:32:01 -07001203 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001204 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1205 if (prog->_LinkedShaders[j] == NULL)
1206 continue;
1207
Eric Anholta721abf2010-08-23 10:32:01 -07001208 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1209 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1210 if (!other_var)
1211 continue;
1212
1213 if (strcmp(var->name, other_var->name) == 0 &&
1214 other_var->max_array_access > size) {
1215 size = other_var->max_array_access;
1216 }
1217 }
1218 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001219
Eric Anholta721abf2010-08-23 10:32:01 -07001220 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001221 /* If this is a built-in uniform (i.e., it's backed by some
1222 * fixed-function state), adjust the number of state slots to
1223 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001224 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001225 * slots is an integer multiple of the number of array elements.
1226 * Determine the number of slots per array element by dividing by
1227 * the old (total) size.
1228 */
1229 if (var->num_state_slots > 0) {
1230 var->num_state_slots = (size + 1)
1231 * (var->num_state_slots / var->type->length);
1232 }
1233
Eric Anholta721abf2010-08-23 10:32:01 -07001234 var->type = glsl_type::get_array_instance(var->type->fields.array,
1235 size + 1);
1236 /* FINISHME: We should update the types of array
1237 * dereferences of this variable now.
1238 */
1239 }
1240 }
1241 }
1242}
1243
Ian Romanick69846702010-06-22 17:29:19 -07001244/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001245 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001246 *
1247 * \param used_mask Bits representing used (1) and unused (0) locations
1248 * \param needed_count Number of contiguous bits needed.
1249 *
1250 * \return
1251 * Base location of the available bits on success or -1 on failure.
1252 */
1253int
1254find_available_slots(unsigned used_mask, unsigned needed_count)
1255{
1256 unsigned needed_mask = (1 << needed_count) - 1;
1257 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1258
1259 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1260 * cannot optimize possibly infinite loops" for the loop below.
1261 */
1262 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1263 return -1;
1264
1265 for (int i = 0; i <= max_bit_to_test; i++) {
1266 if ((needed_mask & ~used_mask) == needed_mask)
1267 return i;
1268
1269 needed_mask <<= 1;
1270 }
1271
1272 return -1;
1273}
1274
1275
Ian Romanickd32d4f72011-06-27 17:59:58 -07001276/**
1277 * Assign locations for either VS inputs for FS outputs
1278 *
1279 * \param prog Shader program whose variables need locations assigned
1280 * \param target_index Selector for the program target to receive location
1281 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1282 * \c MESA_SHADER_FRAGMENT.
1283 * \param max_index Maximum number of generic locations. This corresponds
1284 * to either the maximum number of draw buffers or the
1285 * maximum number of generic attributes.
1286 *
1287 * \return
1288 * If locations are successfully assigned, true is returned. Otherwise an
1289 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001290 */
Ian Romanick69846702010-06-22 17:29:19 -07001291bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001292assign_attribute_or_color_locations(gl_shader_program *prog,
1293 unsigned target_index,
1294 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001295{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001296 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001297 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001298 unsigned used_locations = (max_index >= 32)
1299 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001300
Ian Romanickd32d4f72011-06-27 17:59:58 -07001301 assert((target_index == MESA_SHADER_VERTEX)
1302 || (target_index == MESA_SHADER_FRAGMENT));
1303
1304 gl_shader *const sh = prog->_LinkedShaders[target_index];
1305 if (sh == NULL)
1306 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001307
Ian Romanick69846702010-06-22 17:29:19 -07001308 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001309 *
1310 * 1. Invalidate the location assignments for all vertex shader inputs.
1311 *
1312 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001313 * glBindVertexAttribLocation) locations and outputs that have
1314 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001315 *
Ian Romanick69846702010-06-22 17:29:19 -07001316 * 3. Sort the attributes without assigned locations by number of slots
1317 * required in decreasing order. Fragmentation caused by attribute
1318 * locations assigned by the application may prevent large attributes
1319 * from having enough contiguous space.
1320 *
1321 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001322 */
1323
Ian Romanickd32d4f72011-06-27 17:59:58 -07001324 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001325 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001326
Ian Romanickd32d4f72011-06-27 17:59:58 -07001327 const enum ir_variable_mode direction =
1328 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1329
1330
Ian Romanick69846702010-06-22 17:29:19 -07001331 /* Temporary storage for the set of attributes that need locations assigned.
1332 */
1333 struct temp_attr {
1334 unsigned slots;
1335 ir_variable *var;
1336
1337 /* Used below in the call to qsort. */
1338 static int compare(const void *a, const void *b)
1339 {
1340 const temp_attr *const l = (const temp_attr *) a;
1341 const temp_attr *const r = (const temp_attr *) b;
1342
1343 /* Reversed because we want a descending order sort below. */
1344 return r->slots - l->slots;
1345 }
1346 } to_assign[16];
1347
1348 unsigned num_attr = 0;
1349
Eric Anholt16b68b12010-06-30 11:05:43 -07001350 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001351 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1352
Brian Paul4470ff22011-07-19 21:10:25 -06001353 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001354 continue;
1355
Ian Romanick68a4fc92010-10-07 17:21:22 -07001356 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001357 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001358 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001359 linker_error(prog,
1360 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001361 (var->location < 0)
1362 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001363 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001364 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001365 }
1366 } else if (target_index == MESA_SHADER_VERTEX) {
1367 unsigned binding;
1368
1369 if (prog->AttributeBindings->get(binding, var->name)) {
1370 assert(binding >= VERT_ATTRIB_GENERIC0);
1371 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001372 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001373 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001374 } else if (target_index == MESA_SHADER_FRAGMENT) {
1375 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001376 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001377
1378 if (prog->FragDataBindings->get(binding, var->name)) {
1379 assert(binding >= FRAG_RESULT_DATA0);
1380 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001381 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001382
1383 if (prog->FragDataIndexBindings->get(index, var->name)) {
1384 var->index = index;
1385 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001386 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001387 }
1388
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001389 /* If the variable is not a built-in and has a location statically
1390 * assigned in the shader (presumably via a layout qualifier), make sure
1391 * that it doesn't collide with other assigned locations. Otherwise,
1392 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001393 */
Ian Romanick523b6112011-08-17 15:40:03 -07001394 const unsigned slots = count_attribute_slots(var->type);
1395 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001396 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001397 /* From page 61 of the OpenGL 4.0 spec:
1398 *
1399 * "LinkProgram will fail if the attribute bindings assigned
1400 * by BindAttribLocation do not leave not enough space to
1401 * assign a location for an active matrix attribute or an
1402 * active attribute array, both of which require multiple
1403 * contiguous generic attributes."
1404 *
1405 * Previous versions of the spec contain similar language but omit
1406 * the bit about attribute arrays.
1407 *
1408 * Page 61 of the OpenGL 4.0 spec also says:
1409 *
1410 * "It is possible for an application to bind more than one
1411 * attribute name to the same location. This is referred to as
1412 * aliasing. This will only work if only one of the aliased
1413 * attributes is active in the executable program, or if no
1414 * path through the shader consumes more than one attribute of
1415 * a set of attributes aliased to the same location. A link
1416 * error can occur if the linker determines that every path
1417 * through the shader consumes multiple aliased attributes,
1418 * but implementations are not required to generate an error
1419 * in this case."
1420 *
1421 * These two paragraphs are either somewhat contradictory, or I
1422 * don't fully understand one or both of them.
1423 */
1424 /* FINISHME: The code as currently written does not support
1425 * FINISHME: attribute location aliasing (see comment above).
1426 */
1427 /* Mask representing the contiguous slots that will be used by
1428 * this attribute.
1429 */
1430 const unsigned attr = var->location - generic_base;
1431 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001432
Ian Romanick523b6112011-08-17 15:40:03 -07001433 /* Generate a link error if the set of bits requested for this
1434 * attribute overlaps any previously allocated bits.
1435 */
1436 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001437 const char *const string = (target_index == MESA_SHADER_VERTEX)
1438 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001439 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001440 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001441 "available for %s `%s' %d %d %d", string,
1442 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001443 return false;
1444 }
1445
1446 used_locations |= (use_mask << attr);
1447 }
1448
1449 continue;
1450 }
1451
1452 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001453 to_assign[num_attr].var = var;
1454 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001455 }
Ian Romanick69846702010-06-22 17:29:19 -07001456
1457 /* If all of the attributes were assigned locations by the application (or
1458 * are built-in attributes with fixed locations), return early. This should
1459 * be the common case.
1460 */
1461 if (num_attr == 0)
1462 return true;
1463
1464 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1465
Ian Romanickd32d4f72011-06-27 17:59:58 -07001466 if (target_index == MESA_SHADER_VERTEX) {
1467 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1468 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1469 * reserved to prevent it from being automatically allocated below.
1470 */
1471 find_deref_visitor find("gl_Vertex");
1472 find.run(sh->ir);
1473 if (find.variable_found())
1474 used_locations |= (1 << 0);
1475 }
Ian Romanick982e3792010-06-29 18:58:20 -07001476
Ian Romanick69846702010-06-22 17:29:19 -07001477 for (unsigned i = 0; i < num_attr; i++) {
1478 /* Mask representing the contiguous slots that will be used by this
1479 * attribute.
1480 */
1481 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1482
1483 int location = find_available_slots(used_locations, to_assign[i].slots);
1484
1485 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001486 const char *const string = (target_index == MESA_SHADER_VERTEX)
1487 ? "vertex shader input" : "fragment shader output";
1488
Ian Romanick586e7412011-07-28 14:04:09 -07001489 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001490 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001491 "available for %s `%s'",
1492 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001493 return false;
1494 }
1495
Ian Romanickd32d4f72011-06-27 17:59:58 -07001496 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001497 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001498 used_locations |= (use_mask << location);
1499 }
1500
1501 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001502}
1503
1504
Ian Romanick40e114b2010-08-17 14:55:50 -07001505/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001506 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001507 */
1508void
Ian Romanickcc90e622010-10-19 17:59:10 -07001509demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001510{
1511 foreach_list(node, sh->ir) {
1512 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1513
Ian Romanickcc90e622010-10-19 17:59:10 -07001514 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001515 continue;
1516
Ian Romanickcc90e622010-10-19 17:59:10 -07001517 /* A shader 'in' or 'out' variable is only really an input or output if
1518 * its value is used by other shader stages. This will cause the variable
1519 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001520 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001521 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001522 var->mode = ir_var_auto;
1523 }
1524 }
1525}
1526
1527
Paul Berry871ddb92011-11-05 11:17:32 -07001528/**
1529 * Data structure tracking information about a transform feedback declaration
1530 * during linking.
1531 */
1532class tfeedback_decl
1533{
1534public:
Paul Berry456279b2011-12-26 19:39:25 -08001535 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1536 const void *mem_ctx, const char *input);
Paul Berry871ddb92011-11-05 11:17:32 -07001537 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1538 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1539 ir_variable *output_var);
Paul Berry25ed3be2012-12-04 10:34:45 -08001540 unsigned get_num_outputs() const;
Paul Berryd3150eb2012-01-09 11:25:14 -08001541 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001542 struct gl_transform_feedback_info *info, unsigned buffer,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001543 const unsigned max_outputs) const;
Paul Berry25ed3be2012-12-04 10:34:45 -08001544 ir_variable *find_output_var(gl_shader_program *prog,
1545 gl_shader *producer) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001546
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001547 bool is_next_buffer_separator() const
1548 {
1549 return this->next_buffer_separator;
1550 }
1551
1552 bool is_varying() const
1553 {
1554 return !this->next_buffer_separator && !this->skip_components;
1555 }
1556
Paul Berry871ddb92011-11-05 11:17:32 -07001557 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001558 * The total number of varying components taken up by this variable. Only
Paul Berry25ed3be2012-12-04 10:34:45 -08001559 * valid if assign_location() has been called.
Paul Berry871ddb92011-11-05 11:17:32 -07001560 */
1561 unsigned num_components() const
1562 {
Paul Berry642e5b412012-01-04 13:57:52 -08001563 if (this->is_clip_distance_mesa)
1564 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001565 else
Paul Berry642e5b412012-01-04 13:57:52 -08001566 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001567 }
1568
1569private:
1570 /**
1571 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001572 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001573 */
1574 const char *orig_name;
1575
1576 /**
1577 * The name of the variable, parsed from orig_name.
1578 */
Paul Berry913a5c22011-12-27 08:24:57 -08001579 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001580
1581 /**
1582 * True if the declaration in orig_name represents an array.
1583 */
Paul Berry33fe0212012-01-03 20:41:34 -08001584 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001585
1586 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001587 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001588 */
Paul Berry33fe0212012-01-03 20:41:34 -08001589 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001590
1591 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001592 * True if the variable is gl_ClipDistance and the driver lowers
1593 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001594 */
Paul Berry642e5b412012-01-04 13:57:52 -08001595 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001596
1597 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001598 * The vertex shader output location that the linker assigned for this
1599 * variable. -1 if a location hasn't been assigned yet.
1600 */
1601 int location;
1602
1603 /**
1604 * If location != -1, the number of vector elements in this variable, or 1
1605 * if this variable is a scalar.
1606 */
1607 unsigned vector_elements;
1608
1609 /**
1610 * If location != -1, the number of matrix columns in this variable, or 1
1611 * if this variable is not a matrix.
1612 */
1613 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001614
1615 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1616 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001617
1618 /**
1619 * If location != -1, the size that should be returned by
1620 * glGetTransformFeedbackVarying().
1621 */
1622 unsigned size;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001623
1624 /**
1625 * How many components to skip. If non-zero, this is
1626 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1627 */
1628 unsigned skip_components;
1629
1630 /**
1631 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1632 */
1633 bool next_buffer_separator;
Paul Berry871ddb92011-11-05 11:17:32 -07001634};
1635
1636
1637/**
1638 * Initialize this object based on a string that was passed to
1639 * glTransformFeedbackVaryings. If there is a parse error, the error is
1640 * reported using linker_error(), and false is returned.
1641 */
1642bool
Paul Berry456279b2011-12-26 19:39:25 -08001643tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1644 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001645{
1646 /* We don't have to be pedantic about what is a valid GLSL variable name,
1647 * because any variable with an invalid name can't exist in the IR anyway.
1648 */
1649
1650 this->location = -1;
1651 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001652 this->is_clip_distance_mesa = false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001653 this->skip_components = 0;
1654 this->next_buffer_separator = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001655
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001656 if (ctx->Extensions.ARB_transform_feedback3) {
1657 /* Parse gl_NextBuffer. */
1658 if (strcmp(input, "gl_NextBuffer") == 0) {
1659 this->next_buffer_separator = true;
1660 return true;
1661 }
1662
1663 /* Parse gl_SkipComponents. */
1664 if (strcmp(input, "gl_SkipComponents1") == 0)
1665 this->skip_components = 1;
1666 else if (strcmp(input, "gl_SkipComponents2") == 0)
1667 this->skip_components = 2;
1668 else if (strcmp(input, "gl_SkipComponents3") == 0)
1669 this->skip_components = 3;
1670 else if (strcmp(input, "gl_SkipComponents4") == 0)
1671 this->skip_components = 4;
1672
1673 if (this->skip_components)
1674 return true;
1675 }
1676
1677 /* Parse a declaration. */
Paul Berry871ddb92011-11-05 11:17:32 -07001678 const char *bracket = strrchr(input, '[');
1679
1680 if (bracket) {
1681 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001682 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001683 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1684 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001685 }
Paul Berry33fe0212012-01-03 20:41:34 -08001686 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001687 } else {
1688 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001689 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001690 }
1691
Paul Berry642e5b412012-01-04 13:57:52 -08001692 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1693 * class must behave specially to account for the fact that gl_ClipDistance
1694 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001695 */
1696 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1697 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001698 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001699 }
1700
1701 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001702}
1703
1704
1705/**
1706 * Determine whether two tfeedback_decl objects refer to the same variable and
1707 * array index (if applicable).
1708 */
1709bool
1710tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1711{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001712 assert(x.is_varying() && y.is_varying());
1713
Paul Berry871ddb92011-11-05 11:17:32 -07001714 if (strcmp(x.var_name, y.var_name) != 0)
1715 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001716 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001717 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001718 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001719 return false;
1720 return true;
1721}
1722
1723
1724/**
1725 * Assign a location for this tfeedback_decl object based on the location
1726 * assignment in output_var.
1727 *
1728 * If an error occurs, the error is reported through linker_error() and false
1729 * is returned.
1730 */
1731bool
1732tfeedback_decl::assign_location(struct gl_context *ctx,
1733 struct gl_shader_program *prog,
1734 ir_variable *output_var)
1735{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001736 assert(this->is_varying());
1737
Paul Berry871ddb92011-11-05 11:17:32 -07001738 if (output_var->type->is_array()) {
1739 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001740 const unsigned matrix_cols =
1741 output_var->type->fields.array->matrix_columns;
Paul Berry642e5b412012-01-04 13:57:52 -08001742 unsigned actual_array_size = this->is_clip_distance_mesa ?
1743 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001744
1745 if (this->is_subscripted) {
1746 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001747 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001748 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001749 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001750 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001751 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001752 return false;
1753 }
Paul Berry642e5b412012-01-04 13:57:52 -08001754 if (this->is_clip_distance_mesa) {
1755 this->location =
1756 output_var->location + this->array_subscript / 4;
1757 } else {
1758 this->location =
1759 output_var->location + this->array_subscript * matrix_cols;
1760 }
Paul Berry33fe0212012-01-03 20:41:34 -08001761 this->size = 1;
1762 } else {
1763 this->location = output_var->location;
Paul Berry642e5b412012-01-04 13:57:52 -08001764 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001765 }
Paul Berry871ddb92011-11-05 11:17:32 -07001766 this->vector_elements = output_var->type->fields.array->vector_elements;
1767 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001768 if (this->is_clip_distance_mesa)
1769 this->type = GL_FLOAT;
1770 else
1771 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001772 } else {
1773 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001774 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001775 linker_error(prog, "Transform feedback varying %s requested, "
1776 "but %s is not an array.",
1777 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001778 return false;
1779 }
1780 this->location = output_var->location;
Paul Berry33fe0212012-01-03 20:41:34 -08001781 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001782 this->vector_elements = output_var->type->vector_elements;
1783 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001784 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001785 }
Paul Berry642e5b412012-01-04 13:57:52 -08001786
Paul Berry871ddb92011-11-05 11:17:32 -07001787 /* From GL_EXT_transform_feedback:
1788 * A program will fail to link if:
1789 *
1790 * * the total number of components to capture in any varying
1791 * variable in <varyings> is greater than the constant
1792 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1793 * buffer mode is SEPARATE_ATTRIBS_EXT;
1794 */
1795 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1796 this->num_components() >
1797 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1798 linker_error(prog, "Transform feedback varying %s exceeds "
1799 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1800 this->orig_name);
1801 return false;
1802 }
1803
1804 return true;
1805}
1806
1807
Paul Berry25ed3be2012-12-04 10:34:45 -08001808unsigned
1809tfeedback_decl::get_num_outputs() const
Paul Berry871ddb92011-11-05 11:17:32 -07001810{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001811 if (!this->is_varying()) {
Paul Berry25ed3be2012-12-04 10:34:45 -08001812 return 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001813 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001814
Christoph Bumillerd540af52012-01-20 13:24:46 +01001815 unsigned translated_size = this->size;
1816 if (this->is_clip_distance_mesa)
1817 translated_size = (translated_size + 3) / 4;
1818
Paul Berry25ed3be2012-12-04 10:34:45 -08001819 return translated_size * this->matrix_columns;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001820}
1821
1822
1823/**
1824 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1825 *
1826 * If an error occurs, the error is reported through linker_error() and false
1827 * is returned.
1828 */
1829bool
1830tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1831 struct gl_transform_feedback_info *info,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001832 unsigned buffer, const unsigned max_outputs) const
Christoph Bumillerd540af52012-01-20 13:24:46 +01001833{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001834 assert(!this->next_buffer_separator);
1835
1836 /* Handle gl_SkipComponents. */
1837 if (this->skip_components) {
1838 info->BufferStride[buffer] += this->skip_components;
1839 return true;
1840 }
1841
Paul Berryd3150eb2012-01-09 11:25:14 -08001842 /* From GL_EXT_transform_feedback:
1843 * A program will fail to link if:
1844 *
1845 * * the total number of components to capture is greater than
1846 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1847 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1848 */
1849 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1850 info->BufferStride[buffer] + this->num_components() >
1851 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1852 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1853 "limit has been exceeded.");
1854 return false;
1855 }
1856
Paul Berry642e5b412012-01-04 13:57:52 -08001857 unsigned translated_size = this->size;
1858 if (this->is_clip_distance_mesa)
1859 translated_size = (translated_size + 3) / 4;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001860 unsigned components_so_far = 0;
Paul Berry642e5b412012-01-04 13:57:52 -08001861 for (unsigned index = 0; index < translated_size; ++index) {
Paul Berry33fe0212012-01-03 20:41:34 -08001862 for (unsigned v = 0; v < this->matrix_columns; ++v) {
Paul Berry642e5b412012-01-04 13:57:52 -08001863 unsigned num_components = this->vector_elements;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001864 assert(info->NumOutputs < max_outputs);
Paul Berry642e5b412012-01-04 13:57:52 -08001865 info->Outputs[info->NumOutputs].ComponentOffset = 0;
1866 if (this->is_clip_distance_mesa) {
1867 if (this->is_subscripted) {
1868 num_components = 1;
1869 info->Outputs[info->NumOutputs].ComponentOffset =
1870 this->array_subscript % 4;
1871 } else {
1872 num_components = MIN2(4, this->size - components_so_far);
1873 }
1874 }
Paul Berry33fe0212012-01-03 20:41:34 -08001875 info->Outputs[info->NumOutputs].OutputRegister =
1876 this->location + v + index * this->matrix_columns;
1877 info->Outputs[info->NumOutputs].NumComponents = num_components;
1878 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1879 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
Paul Berry33fe0212012-01-03 20:41:34 -08001880 ++info->NumOutputs;
1881 info->BufferStride[buffer] += num_components;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001882 components_so_far += num_components;
Paul Berry33fe0212012-01-03 20:41:34 -08001883 }
Paul Berry871ddb92011-11-05 11:17:32 -07001884 }
Paul Berrybe4e9f72012-01-04 12:21:55 -08001885 assert(components_so_far == this->num_components());
Eric Anholt9d36c962012-01-02 17:08:13 -08001886
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001887 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1888 info->Varyings[info->NumVarying].Type = this->type;
1889 info->Varyings[info->NumVarying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001890 info->NumVarying++;
1891
Paul Berry871ddb92011-11-05 11:17:32 -07001892 return true;
1893}
1894
1895
Paul Berry25ed3be2012-12-04 10:34:45 -08001896ir_variable *
1897tfeedback_decl::find_output_var(gl_shader_program *prog,
1898 gl_shader *producer) const
1899{
1900 const char *name = this->is_clip_distance_mesa
1901 ? "gl_ClipDistanceMESA" : this->var_name;
1902 ir_variable *var = producer->symbols->get_variable(name);
1903 if (var && var->mode == ir_var_out)
1904 return var;
1905
1906 /* From GL_EXT_transform_feedback:
1907 * A program will fail to link if:
1908 *
1909 * * any variable name specified in the <varyings> array is not
1910 * declared as an output in the geometry shader (if present) or
1911 * the vertex shader (if no geometry shader is present);
1912 */
1913 linker_error(prog, "Transform feedback varying %s undeclared.",
1914 this->orig_name);
1915 return NULL;
1916}
1917
1918
Paul Berry871ddb92011-11-05 11:17:32 -07001919/**
1920 * Parse all the transform feedback declarations that were passed to
1921 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1922 *
1923 * If an error occurs, the error is reported through linker_error() and false
1924 * is returned.
1925 */
1926static bool
Paul Berry456279b2011-12-26 19:39:25 -08001927parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1928 const void *mem_ctx, unsigned num_names,
1929 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001930{
1931 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001932 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001933 return false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001934
1935 if (!decls[i].is_varying())
1936 continue;
1937
Paul Berry871ddb92011-11-05 11:17:32 -07001938 /* From GL_EXT_transform_feedback:
1939 * A program will fail to link if:
1940 *
1941 * * any two entries in the <varyings> array specify the same varying
1942 * variable;
1943 *
1944 * We interpret this to mean "any two entries in the <varyings> array
1945 * specify the same varying variable and array index", since transform
1946 * feedback of arrays would be useless otherwise.
1947 */
1948 for (unsigned j = 0; j < i; ++j) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001949 if (!decls[j].is_varying())
1950 continue;
1951
Paul Berry871ddb92011-11-05 11:17:32 -07001952 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1953 linker_error(prog, "Transform feedback varying %s specified "
1954 "more than once.", varying_names[i]);
1955 return false;
1956 }
1957 }
1958 }
1959 return true;
1960}
1961
1962
1963/**
Paul Berryeb989e32012-12-04 15:55:59 -08001964 * Data structure recording the relationship between outputs of one shader
1965 * stage (the "producer") and inputs of another (the "consumer").
1966 */
1967class varying_matches
1968{
1969public:
1970 varying_matches();
1971 ~varying_matches();
1972 void record(ir_variable *producer_var, ir_variable *consumer_var);
1973 unsigned assign_locations();
1974 void store_locations(unsigned producer_base, unsigned consumer_base) const;
1975
1976private:
1977 /**
Paul Berryf3993102012-12-05 10:19:19 -08001978 * Enum representing the order in which varyings are packed within a
1979 * packing class.
1980 *
1981 * Currently we pack vec4's first, then vec2's, then scalar values, then
1982 * vec3's. This order ensures that the only vectors that are at risk of
1983 * having to be "double parked" (split between two adjacent varying slots)
1984 * are the vec3's.
1985 */
1986 enum packing_order_enum {
1987 PACKING_ORDER_VEC4,
1988 PACKING_ORDER_VEC2,
1989 PACKING_ORDER_SCALAR,
1990 PACKING_ORDER_VEC3,
1991 };
1992
1993 static unsigned compute_packing_class(ir_variable *var);
1994 static packing_order_enum compute_packing_order(ir_variable *var);
1995 static int match_comparator(const void *x_generic, const void *y_generic);
1996
1997 /**
Paul Berryeb989e32012-12-04 15:55:59 -08001998 * Structure recording the relationship between a single producer output
1999 * and a single consumer input.
2000 */
2001 struct match {
2002 /**
Paul Berryf3993102012-12-05 10:19:19 -08002003 * Packing class for this varying, computed by compute_packing_class().
2004 */
2005 unsigned packing_class;
2006
2007 /**
2008 * Packing order for this varying, computed by compute_packing_order().
2009 */
2010 packing_order_enum packing_order;
2011
2012 /**
Paul Berryeb989e32012-12-04 15:55:59 -08002013 * The output variable in the producer stage.
2014 */
2015 ir_variable *producer_var;
2016
2017 /**
2018 * The input variable in the consumer stage.
2019 */
2020 ir_variable *consumer_var;
2021
2022 /**
2023 * The location which has been assigned for this varying. This is
2024 * expressed in multiples of a float, with the first generic varying
2025 * (i.e. the one referred to by VERT_RESULT_VAR0 or FRAG_ATTRIB_VAR0)
2026 * represented by the value 0.
2027 */
2028 unsigned generic_location;
2029 } *matches;
2030
2031 /**
2032 * The number of elements in the \c matches array that are currently in
2033 * use.
2034 */
2035 unsigned num_matches;
2036
2037 /**
2038 * The number of elements that were set aside for the \c matches array when
2039 * it was allocated.
2040 */
2041 unsigned matches_capacity;
2042};
2043
2044
2045varying_matches::varying_matches()
2046{
2047 /* Note: this initial capacity is rather arbitrarily chosen to be large
2048 * enough for many cases without wasting an unreasonable amount of space.
2049 * varying_matches::record() will resize the array if there are more than
2050 * this number of varyings.
2051 */
2052 this->matches_capacity = 8;
2053 this->matches = (match *)
2054 malloc(sizeof(*this->matches) * this->matches_capacity);
2055 this->num_matches = 0;
2056}
2057
2058
2059varying_matches::~varying_matches()
2060{
2061 free(this->matches);
2062}
2063
2064
2065/**
2066 * Record the given producer/consumer variable pair in the list of variables
2067 * that should later be assigned locations.
Paul Berry871ddb92011-11-05 11:17:32 -07002068 *
Paul Berryeb989e32012-12-04 15:55:59 -08002069 * It is permissible for \c consumer_var to be NULL (this happens if a
2070 * variable is output by the producer and consumed by transform feedback, but
2071 * not consumed by the consumer).
Paul Berry871ddb92011-11-05 11:17:32 -07002072 *
Paul Berryeb989e32012-12-04 15:55:59 -08002073 * If \c producer_var has already been paired up with a consumer_var, or
2074 * producer_var is part of fixed pipeline functionality (and hence already has
2075 * a location assigned), this function has no effect.
Paul Berry871ddb92011-11-05 11:17:32 -07002076 */
2077void
Paul Berryeb989e32012-12-04 15:55:59 -08002078varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
Paul Berry871ddb92011-11-05 11:17:32 -07002079{
Paul Berryeb989e32012-12-04 15:55:59 -08002080 if (!producer_var->is_unmatched_generic_inout) {
2081 /* Either a location already exists for this variable (since it is part
2082 * of fixed functionality), or it has already been recorded as part of a
2083 * previous match.
2084 */
Paul Berry871ddb92011-11-05 11:17:32 -07002085 return;
2086 }
2087
Paul Berryeb989e32012-12-04 15:55:59 -08002088 if (this->num_matches == this->matches_capacity) {
2089 this->matches_capacity *= 2;
2090 this->matches = (match *)
2091 realloc(this->matches,
2092 sizeof(*this->matches) * this->matches_capacity);
2093 }
Paul Berryf3993102012-12-05 10:19:19 -08002094 this->matches[this->num_matches].packing_class
2095 = this->compute_packing_class(producer_var);
2096 this->matches[this->num_matches].packing_order
2097 = this->compute_packing_order(producer_var);
Paul Berryeb989e32012-12-04 15:55:59 -08002098 this->matches[this->num_matches].producer_var = producer_var;
2099 this->matches[this->num_matches].consumer_var = consumer_var;
2100 this->num_matches++;
2101 producer_var->is_unmatched_generic_inout = 0;
2102 if (consumer_var)
2103 consumer_var->is_unmatched_generic_inout = 0;
2104}
2105
2106
2107/**
2108 * Choose locations for all of the variable matches that were previously
2109 * passed to varying_matches::record().
2110 */
2111unsigned
2112varying_matches::assign_locations()
2113{
Paul Berryf3993102012-12-05 10:19:19 -08002114 /* Sort varying matches into an order that makes them easy to pack. */
2115 qsort(this->matches, this->num_matches, sizeof(*this->matches),
2116 &varying_matches::match_comparator);
2117
Paul Berryeb989e32012-12-04 15:55:59 -08002118 unsigned generic_location = 0;
2119
2120 for (unsigned i = 0; i < this->num_matches; i++) {
2121 this->matches[i].generic_location = generic_location;
2122
2123 ir_variable *producer_var = this->matches[i].producer_var;
2124
2125 if (producer_var->type->is_array()) {
2126 const unsigned slots = producer_var->type->length
2127 * producer_var->type->fields.array->matrix_columns;
2128
2129 generic_location += 4 * slots;
2130 } else {
2131 const unsigned slots = producer_var->type->matrix_columns;
2132
2133 generic_location += 4 * slots;
2134 }
Paul Berry871ddb92011-11-05 11:17:32 -07002135 }
2136
Paul Berryeb989e32012-12-04 15:55:59 -08002137 return (generic_location + 3) / 4;
2138}
Paul Berry871ddb92011-11-05 11:17:32 -07002139
Paul Berry871ddb92011-11-05 11:17:32 -07002140
Paul Berryeb989e32012-12-04 15:55:59 -08002141/**
2142 * Update the producer and consumer shaders to reflect the locations
2143 * assignments that were made by varying_matches::assign_locations().
2144 */
2145void
2146varying_matches::store_locations(unsigned producer_base,
2147 unsigned consumer_base) const
2148{
2149 for (unsigned i = 0; i < this->num_matches; i++) {
2150 ir_variable *producer_var = this->matches[i].producer_var;
2151 ir_variable *consumer_var = this->matches[i].consumer_var;
2152 unsigned generic_location = this->matches[i].generic_location;
2153 unsigned slot = generic_location / 4;
2154 unsigned offset = generic_location % 4;
Paul Berry871ddb92011-11-05 11:17:32 -07002155
Paul Berryeb989e32012-12-04 15:55:59 -08002156 producer_var->location = producer_base + slot;
2157 producer_var->location_frac = offset;
2158 if (consumer_var) {
2159 assert(consumer_var->location == -1);
2160 consumer_var->location = consumer_base + slot;
2161 consumer_var->location_frac = offset;
2162 }
Paul Berry871ddb92011-11-05 11:17:32 -07002163 }
2164}
2165
2166
2167/**
Paul Berryf3993102012-12-05 10:19:19 -08002168 * Compute the "packing class" of the given varying. This is an unsigned
2169 * integer with the property that two variables in the same packing class can
2170 * be safely backed into the same vec4.
2171 */
2172unsigned
2173varying_matches::compute_packing_class(ir_variable *var)
2174{
2175 /* In this initial implementation we conservatively assume that variables
2176 * can only be packed if their base type (float/int/uint/bool) matches and
2177 * their interpolation and centroid qualifiers match.
2178 *
2179 * TODO: relax these restrictions when the driver back-end permits.
2180 */
2181 unsigned packing_class = var->centroid ? 1 : 0;
2182 packing_class *= 4;
2183 packing_class += var->interpolation;
2184 packing_class *= GLSL_TYPE_ERROR;
2185 packing_class += var->type->get_scalar_type()->base_type;
2186 return packing_class;
2187}
2188
2189
2190/**
2191 * Compute the "packing order" of the given varying. This is a sort key we
2192 * use to determine when to attempt to pack the given varying relative to
2193 * other varyings in the same packing class.
2194 */
2195varying_matches::packing_order_enum
2196varying_matches::compute_packing_order(ir_variable *var)
2197{
2198 const glsl_type *element_type = var->type;
2199
2200 /* FINISHME: Support for "varying" records in GLSL 1.50. */
2201 while (element_type->base_type == GLSL_TYPE_ARRAY) {
2202 element_type = element_type->fields.array;
2203 }
2204
2205 switch (element_type->vector_elements) {
2206 case 1: return PACKING_ORDER_SCALAR;
2207 case 2: return PACKING_ORDER_VEC2;
2208 case 3: return PACKING_ORDER_VEC3;
2209 case 4: return PACKING_ORDER_VEC4;
2210 default:
2211 assert(!"Unexpected value of vector_elements");
2212 return PACKING_ORDER_VEC4;
2213 }
2214}
2215
2216
2217/**
2218 * Comparison function passed to qsort() to sort varyings by packing_class and
2219 * then by packing_order.
2220 */
2221int
2222varying_matches::match_comparator(const void *x_generic, const void *y_generic)
2223{
2224 const match *x = (const match *) x_generic;
2225 const match *y = (const match *) y_generic;
2226
2227 if (x->packing_class != y->packing_class)
2228 return x->packing_class - y->packing_class;
2229 return x->packing_order - y->packing_order;
2230}
2231
2232
2233/**
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002234 * Is the given variable a varying variable to be counted against the
2235 * limit in ctx->Const.MaxVarying?
2236 * This includes variables such as texcoords, colors and generic
2237 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2238 */
2239static bool
2240is_varying_var(GLenum shaderType, const ir_variable *var)
2241{
2242 /* Only fragment shaders will take a varying variable as an input */
2243 if (shaderType == GL_FRAGMENT_SHADER &&
Eric Anholt94e82b22012-11-13 14:40:22 -08002244 var->mode == ir_var_in) {
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002245 switch (var->location) {
2246 case FRAG_ATTRIB_WPOS:
2247 case FRAG_ATTRIB_FACE:
2248 case FRAG_ATTRIB_PNTC:
2249 return false;
2250 default:
2251 return true;
2252 }
2253 }
2254 return false;
2255}
2256
2257
2258/**
Paul Berry871ddb92011-11-05 11:17:32 -07002259 * Assign locations for all variables that are produced in one pipeline stage
2260 * (the "producer") and consumed in the next stage (the "consumer").
2261 *
2262 * Variables produced by the producer may also be consumed by transform
2263 * feedback.
2264 *
2265 * \param num_tfeedback_decls is the number of declarations indicating
2266 * variables that may be consumed by transform feedback.
2267 *
2268 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2269 * representing the result of parsing the strings passed to
2270 * glTransformFeedbackVaryings(). assign_location() will be called for
2271 * each of these objects that matches one of the outputs of the
2272 * producer.
2273 *
2274 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2275 * be NULL. In this case, varying locations are assigned solely based on the
2276 * requirements of transform feedback.
2277 */
Ian Romanickde773242011-06-09 13:31:32 -07002278bool
2279assign_varying_locations(struct gl_context *ctx,
2280 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07002281 gl_shader *producer, gl_shader *consumer,
2282 unsigned num_tfeedback_decls,
2283 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07002284{
2285 /* FINISHME: Set dynamically when geometry shader support is added. */
Paul Berryeb989e32012-12-04 15:55:59 -08002286 const unsigned producer_base = VERT_RESULT_VAR0;
2287 const unsigned consumer_base = FRAG_ATTRIB_VAR0;
2288 varying_matches matches;
Ian Romanick0e59b262010-06-23 11:23:01 -07002289
2290 /* Operate in a total of three passes.
2291 *
2292 * 1. Assign locations for any matching inputs and outputs.
2293 *
2294 * 2. Mark output variables in the producer that do not have locations as
2295 * not being outputs. This lets the optimizer eliminate them.
2296 *
2297 * 3. Mark input variables in the consumer that do not have locations as
2298 * not being inputs. This lets the optimizer eliminate them.
2299 */
2300
Eric Anholt16b68b12010-06-30 11:05:43 -07002301 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07002302 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2303
Paul Berry871ddb92011-11-05 11:17:32 -07002304 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07002305 continue;
2306
Paul Berry871ddb92011-11-05 11:17:32 -07002307 ir_variable *input_var =
2308 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002309
Paul Berry871ddb92011-11-05 11:17:32 -07002310 if (input_var && input_var->mode != ir_var_in)
2311 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002312
Paul Berry871ddb92011-11-05 11:17:32 -07002313 if (input_var) {
Paul Berryeb989e32012-12-04 15:55:59 -08002314 matches.record(output_var, input_var);
Paul Berry871ddb92011-11-05 11:17:32 -07002315 }
Paul Berry25ed3be2012-12-04 10:34:45 -08002316 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002317
Paul Berry25ed3be2012-12-04 10:34:45 -08002318 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2319 if (!tfeedback_decls[i].is_varying())
2320 continue;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002321
Paul Berry25ed3be2012-12-04 10:34:45 -08002322 ir_variable *output_var
2323 = tfeedback_decls[i].find_output_var(prog, producer);
2324
2325 if (output_var == NULL)
2326 return false;
2327
2328 if (output_var->is_unmatched_generic_inout) {
Paul Berryeb989e32012-12-04 15:55:59 -08002329 matches.record(output_var, NULL);
Ian Romanickdf869d92010-08-30 15:37:44 -07002330 }
Paul Berryeb989e32012-12-04 15:55:59 -08002331 }
2332
2333 matches.assign_locations();
2334 matches.store_locations(producer_base, consumer_base);
2335
2336 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2337 if (!tfeedback_decls[i].is_varying())
2338 continue;
2339
2340 ir_variable *output_var
2341 = tfeedback_decls[i].find_output_var(prog, producer);
Paul Berry25ed3be2012-12-04 10:34:45 -08002342
2343 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2344 return false;
Ian Romanick0e59b262010-06-23 11:23:01 -07002345 }
2346
Ian Romanickde773242011-06-09 13:31:32 -07002347 unsigned varying_vectors = 0;
2348
Paul Berry871ddb92011-11-05 11:17:32 -07002349 if (consumer) {
2350 foreach_list(node, consumer->ir) {
2351 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07002352
Paul Berry871ddb92011-11-05 11:17:32 -07002353 if ((var == NULL) || (var->mode != ir_var_in))
2354 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07002355
Paul Berry3c9c17d2012-12-04 15:17:01 -08002356 if (var->is_unmatched_generic_inout) {
Paul Berry871ddb92011-11-05 11:17:32 -07002357 if (prog->Version <= 120) {
2358 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2359 *
2360 * Only those varying variables used (i.e. read) in
2361 * the fragment shader executable must be written to
2362 * by the vertex shader executable; declaring
2363 * superfluous varying variables in a vertex shader is
2364 * permissible.
2365 *
2366 * We interpret this text as meaning that the VS must
2367 * write the variable for the FS to read it. See
2368 * "glsl1-varying read but not written" in piglit.
2369 */
Eric Anholtb7062832010-07-28 13:52:23 -07002370
Paul Berry871ddb92011-11-05 11:17:32 -07002371 linker_error(prog, "fragment shader varying %s not written "
2372 "by vertex shader\n.", var->name);
2373 }
Eric Anholtb7062832010-07-28 13:52:23 -07002374
Paul Berry871ddb92011-11-05 11:17:32 -07002375 /* An 'in' variable is only really a shader input if its
2376 * value is written by the previous stage.
2377 */
2378 var->mode = ir_var_auto;
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002379 } else if (is_varying_var(consumer->Type, var)) {
Paul Berry871ddb92011-11-05 11:17:32 -07002380 /* The packing rules are used for vertex shader inputs are also
2381 * used for fragment shader inputs.
2382 */
2383 varying_vectors += count_attribute_slots(var->type);
2384 }
Eric Anholtb7062832010-07-28 13:52:23 -07002385 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002386 }
Ian Romanickde773242011-06-09 13:31:32 -07002387
Paul Berry15ba2a52012-08-02 17:51:02 -07002388 if (ctx->API == API_OPENGLES2 || prog->IsES) {
Ian Romanickde773242011-06-09 13:31:32 -07002389 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002390 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2391 linker_warning(prog, "shader uses too many varying vectors "
2392 "(%u > %u), but the driver will try to optimize "
2393 "them out; this is non-portable out-of-spec "
2394 "behavior\n",
2395 varying_vectors, ctx->Const.MaxVarying);
2396 } else {
2397 linker_error(prog, "shader uses too many varying vectors "
2398 "(%u > %u)\n",
2399 varying_vectors, ctx->Const.MaxVarying);
2400 return false;
2401 }
Ian Romanickde773242011-06-09 13:31:32 -07002402 }
2403 } else {
2404 const unsigned float_components = varying_vectors * 4;
2405 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002406 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2407 linker_warning(prog, "shader uses too many varying components "
2408 "(%u > %u), but the driver will try to optimize "
2409 "them out; this is non-portable out-of-spec "
2410 "behavior\n",
2411 float_components, ctx->Const.MaxVarying * 4);
2412 } else {
2413 linker_error(prog, "shader uses too many varying components "
2414 "(%u > %u)\n",
2415 float_components, ctx->Const.MaxVarying * 4);
2416 return false;
2417 }
Ian Romanickde773242011-06-09 13:31:32 -07002418 }
2419 }
2420
2421 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07002422}
2423
2424
Paul Berry871ddb92011-11-05 11:17:32 -07002425/**
2426 * Store transform feedback location assignments into
2427 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2428 *
2429 * If an error occurs, the error is reported through linker_error() and false
2430 * is returned.
2431 */
2432static bool
2433store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2434 unsigned num_tfeedback_decls,
2435 tfeedback_decl *tfeedback_decls)
2436{
Paul Berryebfad9f2011-12-29 15:55:01 -08002437 bool separate_attribs_mode =
2438 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08002439
2440 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01002441 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08002442
2443 memset(&prog->LinkedTransformFeedback, 0,
2444 sizeof(prog->LinkedTransformFeedback));
2445
2446 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08002447 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08002448 struct gl_transform_feedback_varying_info,
2449 num_tfeedback_decls);
2450
Christoph Bumillerd540af52012-01-20 13:24:46 +01002451 unsigned num_outputs = 0;
2452 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
Paul Berry25ed3be2012-12-04 10:34:45 -08002453 num_outputs += tfeedback_decls[i].get_num_outputs();
Christoph Bumillerd540af52012-01-20 13:24:46 +01002454
2455 prog->LinkedTransformFeedback.Outputs =
2456 rzalloc_array(prog,
2457 struct gl_transform_feedback_output,
2458 num_outputs);
2459
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002460 unsigned num_buffers = 0;
2461
2462 if (separate_attribs_mode) {
2463 /* GL_SEPARATE_ATTRIBS */
2464 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2465 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2466 num_buffers, num_outputs))
2467 return false;
2468
2469 num_buffers++;
2470 }
Paul Berry871ddb92011-11-05 11:17:32 -07002471 }
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002472 else {
2473 /* GL_INVERLEAVED_ATTRIBS */
2474 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2475 if (tfeedback_decls[i].is_next_buffer_separator()) {
2476 num_buffers++;
2477 continue;
2478 }
2479
2480 if (!tfeedback_decls[i].store(ctx, prog,
2481 &prog->LinkedTransformFeedback,
2482 num_buffers, num_outputs))
2483 return false;
2484 }
2485 num_buffers++;
2486 }
2487
Christoph Bumillerd540af52012-01-20 13:24:46 +01002488 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002489
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002490 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
Paul Berry871ddb92011-11-05 11:17:32 -07002491 return true;
2492}
2493
Ian Romanick92f81592011-11-08 12:37:19 -08002494/**
Marek Olšákec174a42011-11-18 15:00:10 +01002495 * Store the gl_FragDepth layout in the gl_shader_program struct.
2496 */
2497static void
2498store_fragdepth_layout(struct gl_shader_program *prog)
2499{
2500 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2501 return;
2502 }
2503
2504 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2505
2506 /* We don't look up the gl_FragDepth symbol directly because if
2507 * gl_FragDepth is not used in the shader, it's removed from the IR.
2508 * However, the symbol won't be removed from the symbol table.
2509 *
2510 * We're only interested in the cases where the variable is NOT removed
2511 * from the IR.
2512 */
2513 foreach_list(node, ir) {
2514 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2515
2516 if (var == NULL || var->mode != ir_var_out) {
2517 continue;
2518 }
2519
2520 if (strcmp(var->name, "gl_FragDepth") == 0) {
2521 switch (var->depth_layout) {
2522 case ir_depth_layout_none:
2523 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2524 return;
2525 case ir_depth_layout_any:
2526 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2527 return;
2528 case ir_depth_layout_greater:
2529 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2530 return;
2531 case ir_depth_layout_less:
2532 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2533 return;
2534 case ir_depth_layout_unchanged:
2535 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2536 return;
2537 default:
2538 assert(0);
2539 return;
2540 }
2541 }
2542 }
2543}
2544
2545/**
Ian Romanick92f81592011-11-08 12:37:19 -08002546 * Validate the resources used by a program versus the implementation limits
2547 */
2548static bool
2549check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2550{
2551 static const char *const shader_names[MESA_SHADER_TYPES] = {
2552 "vertex", "fragment", "geometry"
2553 };
2554
2555 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2556 ctx->Const.MaxVertexTextureImageUnits,
2557 ctx->Const.MaxTextureImageUnits,
2558 ctx->Const.MaxGeometryTextureImageUnits
2559 };
2560
2561 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2562 ctx->Const.VertexProgram.MaxUniformComponents,
2563 ctx->Const.FragmentProgram.MaxUniformComponents,
2564 0 /* FINISHME: Geometry shaders. */
2565 };
2566
Eric Anholt877a8972012-06-25 12:47:01 -07002567 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
2568 ctx->Const.VertexProgram.MaxUniformBlocks,
2569 ctx->Const.FragmentProgram.MaxUniformBlocks,
2570 ctx->Const.GeometryProgram.MaxUniformBlocks,
2571 };
2572
Ian Romanick92f81592011-11-08 12:37:19 -08002573 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2574 struct gl_shader *sh = prog->_LinkedShaders[i];
2575
2576 if (sh == NULL)
2577 continue;
2578
2579 if (sh->num_samplers > max_samplers[i]) {
2580 linker_error(prog, "Too many %s shader texture samplers",
2581 shader_names[i]);
2582 }
2583
2584 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002585 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2586 linker_warning(prog, "Too many %s shader uniform components, "
2587 "but the driver will try to optimize them out; "
2588 "this is non-portable out-of-spec behavior\n",
2589 shader_names[i]);
2590 } else {
2591 linker_error(prog, "Too many %s shader uniform components",
2592 shader_names[i]);
2593 }
Ian Romanick92f81592011-11-08 12:37:19 -08002594 }
2595 }
2596
Eric Anholt877a8972012-06-25 12:47:01 -07002597 unsigned blocks[MESA_SHADER_TYPES] = {0};
2598 unsigned total_uniform_blocks = 0;
2599
2600 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
2601 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
2602 if (prog->UniformBlockStageIndex[j][i] != -1) {
2603 blocks[j]++;
2604 total_uniform_blocks++;
2605 }
2606 }
2607
2608 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
2609 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
2610 prog->NumUniformBlocks,
2611 ctx->Const.MaxCombinedUniformBlocks);
2612 } else {
2613 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2614 if (blocks[i] > max_uniform_blocks[i]) {
2615 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
2616 shader_names[i],
2617 blocks[i],
2618 max_uniform_blocks[i]);
2619 break;
2620 }
2621 }
2622 }
2623 }
2624
Ian Romanick92f81592011-11-08 12:37:19 -08002625 return prog->LinkStatus;
2626}
Paul Berry871ddb92011-11-05 11:17:32 -07002627
Ian Romanick0e59b262010-06-23 11:23:01 -07002628void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002629link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002630{
Paul Berry871ddb92011-11-05 11:17:32 -07002631 tfeedback_decl *tfeedback_decls = NULL;
2632 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2633
Kenneth Graunked3073f52011-01-21 14:32:31 -08002634 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002635
Ian Romanick832dfa52010-06-17 15:04:20 -07002636 prog->LinkStatus = false;
2637 prog->Validated = false;
2638 prog->_Used = false;
2639
Eric Anholtf609cf72012-04-27 13:52:56 -07002640 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08002641 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002642
Eric Anholtf609cf72012-04-27 13:52:56 -07002643 ralloc_free(prog->UniformBlocks);
2644 prog->UniformBlocks = NULL;
2645 prog->NumUniformBlocks = 0;
2646 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2647 ralloc_free(prog->UniformBlockStageIndex[i]);
2648 prog->UniformBlockStageIndex[i] = NULL;
2649 }
2650
Ian Romanick832dfa52010-06-17 15:04:20 -07002651 /* Separate the shaders into groups based on their type.
2652 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002653 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002654 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002655 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002656 unsigned num_frag_shaders = 0;
2657
Eric Anholt16b68b12010-06-30 11:05:43 -07002658 vert_shader_list = (struct gl_shader **)
2659 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002660 frag_shader_list = &vert_shader_list[prog->NumShaders];
2661
Ian Romanick25f51d32010-07-16 15:51:50 -07002662 unsigned min_version = UINT_MAX;
2663 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002664 const bool is_es_prog =
2665 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002666 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002667 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2668 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2669
Paul Berrya9f34dc2012-08-02 17:49:44 -07002670 if (prog->Shaders[i]->IsES != is_es_prog) {
2671 linker_error(prog, "all shaders must use same shading "
2672 "language version\n");
2673 goto done;
2674 }
2675
Ian Romanick832dfa52010-06-17 15:04:20 -07002676 switch (prog->Shaders[i]->Type) {
2677 case GL_VERTEX_SHADER:
2678 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2679 num_vert_shaders++;
2680 break;
2681 case GL_FRAGMENT_SHADER:
2682 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2683 num_frag_shaders++;
2684 break;
2685 case GL_GEOMETRY_SHADER:
2686 /* FINISHME: Support geometry shaders. */
2687 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2688 break;
2689 }
2690 }
2691
Ian Romanick25f51d32010-07-16 15:51:50 -07002692 /* Previous to GLSL version 1.30, different compilation units could mix and
2693 * match shading language versions. With GLSL 1.30 and later, the versions
2694 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07002695 *
2696 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07002697 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07002698 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002699 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002700 linker_error(prog, "all shaders must use same shading "
2701 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002702 goto done;
2703 }
2704
2705 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002706 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002707
Ian Romanick3322fba2010-10-14 13:28:42 -07002708 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2709 if (prog->_LinkedShaders[i] != NULL)
2710 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2711
2712 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002713 }
2714
Ian Romanickcd6764e2010-07-16 16:00:07 -07002715 /* Link all shaders for a particular stage and validate the result.
2716 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002717 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002718 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002719 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2720 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002721
2722 if (sh == NULL)
2723 goto done;
2724
2725 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002726 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002727
Ian Romanick3322fba2010-10-14 13:28:42 -07002728 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2729 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002730 }
2731
2732 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002733 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002734 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2735 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002736
2737 if (sh == NULL)
2738 goto done;
2739
2740 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002741 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002742
Ian Romanick3322fba2010-10-14 13:28:42 -07002743 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2744 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002745 }
2746
Ian Romanick3ed850e2010-06-23 12:18:21 -07002747 /* Here begins the inter-stage linking phase. Some initial validation is
2748 * performed, then locations are assigned for uniforms, attributes, and
2749 * varyings.
2750 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002751 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002752 unsigned prev;
2753
2754 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2755 if (prog->_LinkedShaders[prev] != NULL)
2756 break;
2757 }
2758
Bryan Cainf18a0862011-04-23 19:29:15 -05002759 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002760 * stage.
2761 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002762 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2763 if (prog->_LinkedShaders[i] == NULL)
2764 continue;
2765
Ian Romanickf36460e2010-06-23 12:07:22 -07002766 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002767 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002768 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002769 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002770
2771 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002772 }
2773
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002774 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002775 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002776
Eric Anholt3de13952012-05-04 13:08:46 -07002777 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2778 * it before optimization because we want most of the checks to get
2779 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002780 *
2781 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002782 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002783 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002784 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2785 if (sh) {
2786 lower_discard_flow(sh->ir);
2787 }
2788 }
2789
Eric Anholtf609cf72012-04-27 13:52:56 -07002790 if (!interstage_cross_validate_uniform_blocks(prog))
2791 goto done;
2792
Eric Anholt2f4fe152010-08-10 13:06:49 -07002793 /* Do common optimization before assigning storage for attributes,
2794 * uniforms, and varyings. Later optimization could possibly make
2795 * some of that unused.
2796 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002797 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2798 if (prog->_LinkedShaders[i] == NULL)
2799 continue;
2800
Ian Romanick02c5ae12011-07-11 10:46:01 -07002801 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2802 if (!prog->LinkStatus)
2803 goto done;
2804
Paul Berry18392442012-12-04 11:11:02 -08002805 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2806 lower_clip_distance(prog->_LinkedShaders[i]);
2807 }
Paul Berryc06e3252011-08-11 20:58:21 -07002808
Brian Paul7feabfe2012-03-20 17:43:12 -06002809 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2810
2811 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002812 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002813 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002814
Paul Berry50895d42012-12-05 07:17:07 -08002815 /* Mark all generic shader inputs and outputs as unpaired. */
2816 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2817 link_invalidate_variable_locations(
2818 prog->_LinkedShaders[MESA_SHADER_VERTEX],
2819 VERT_ATTRIB_GENERIC0, VERT_RESULT_VAR0);
2820 }
2821 /* FINISHME: Geometry shaders not implemented yet */
2822 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2823 link_invalidate_variable_locations(
2824 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2825 FRAG_ATTRIB_VAR0, FRAG_RESULT_DATA0);
2826 }
2827
Ian Romanickd32d4f72011-06-27 17:59:58 -07002828 /* FINISHME: The value of the max_attribute_index parameter is
2829 * FINISHME: implementation dependent based on the value of
2830 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2831 * FINISHME: at least 16, so hardcode 16 for now.
2832 */
2833 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002834 goto done;
2835 }
2836
Dave Airlie1256a5d2012-03-24 13:33:41 +00002837 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002838 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002839 }
2840
Ian Romanick3322fba2010-10-14 13:28:42 -07002841 unsigned prev;
2842 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2843 if (prog->_LinkedShaders[prev] != NULL)
2844 break;
2845 }
2846
Paul Berry871ddb92011-11-05 11:17:32 -07002847 if (num_tfeedback_decls != 0) {
2848 /* From GL_EXT_transform_feedback:
2849 * A program will fail to link if:
2850 *
2851 * * the <count> specified by TransformFeedbackVaryingsEXT is
2852 * non-zero, but the program object has no vertex or geometry
2853 * shader;
2854 */
2855 if (prev >= MESA_SHADER_FRAGMENT) {
2856 linker_error(prog, "Transform feedback varyings specified, but "
2857 "no vertex or geometry shader is present.");
2858 goto done;
2859 }
2860
2861 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2862 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002863 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002864 prog->TransformFeedback.VaryingNames,
2865 tfeedback_decls))
2866 goto done;
2867 }
2868
Ian Romanick3322fba2010-10-14 13:28:42 -07002869 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2870 if (prog->_LinkedShaders[i] == NULL)
2871 continue;
2872
Paul Berry871ddb92011-11-05 11:17:32 -07002873 if (!assign_varying_locations(
2874 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2875 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2876 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002877 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002878
Ian Romanick3322fba2010-10-14 13:28:42 -07002879 prev = i;
2880 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002881
Paul Berry871ddb92011-11-05 11:17:32 -07002882 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2883 /* There was no fragment shader, but we still have to assign varying
2884 * locations for use by transform feedback.
2885 */
2886 if (!assign_varying_locations(
2887 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2888 tfeedback_decls))
2889 goto done;
2890 }
2891
2892 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2893 goto done;
2894
Ian Romanickcc90e622010-10-19 17:59:10 -07002895 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2896 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2897 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002898
2899 /* Eliminate code that is now dead due to unused vertex outputs being
2900 * demoted.
2901 */
2902 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2903 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002904 }
2905
2906 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2907 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2908
2909 demote_shader_inputs_and_outputs(sh, ir_var_in);
2910 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2911 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002912
2913 /* Eliminate code that is now dead due to unused geometry outputs being
2914 * demoted.
2915 */
2916 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2917 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002918 }
2919
2920 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2921 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2922
2923 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002924
2925 /* Eliminate code that is now dead due to unused fragment inputs being
2926 * demoted. This shouldn't actually do anything other than remove
2927 * declarations of the (now unused) global variables.
2928 */
2929 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2930 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002931 }
2932
Ian Romanick960d7222011-10-21 11:21:02 -07002933 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002934 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002935 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002936
Ian Romanick92f81592011-11-08 12:37:19 -08002937 if (!check_resources(ctx, prog))
2938 goto done;
2939
Ian Romanickce9171f2011-02-03 17:10:14 -08002940 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002941 * present in a linked program. By checking prog->IsES, we also
2942 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002943 */
Eric Anholt57f79782011-07-22 12:57:47 -07002944 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002945 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002946 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002947 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002948 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002949 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002950 }
2951 }
2952
Ian Romanick13e10e42010-06-21 12:03:24 -07002953 /* FINISHME: Assign fragment shader output locations. */
2954
Ian Romanick832dfa52010-06-17 15:04:20 -07002955done:
2956 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002957
2958 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2959 if (prog->_LinkedShaders[i] == NULL)
2960 continue;
2961
2962 /* Retain any live IR, but trash the rest. */
2963 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002964
2965 /* The symbol table in the linked shaders may contain references to
2966 * variables that were removed (e.g., unused uniforms). Since it may
2967 * contain junk, there is no possible valid use. Delete it and set the
2968 * pointer to NULL.
2969 */
2970 delete prog->_LinkedShaders[i]->symbols;
2971 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002972 }
2973
Kenneth Graunked3073f52011-01-21 14:32:31 -08002974 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002975}