blob: 57e7a9ad3696148690017d85ee0d814a14785418 [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"
Paul Berry4b11b572012-12-17 14:20:35 -080073#include "link_varyings.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070074#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070075
Ian Romanick3322fba2010-10-14 13:28:42 -070076extern "C" {
77#include "main/shaderobj.h"
78}
79
Ian Romanick832dfa52010-06-17 15:04:20 -070080/**
81 * Visitor that determines whether or not a variable is ever written.
82 */
83class find_assignment_visitor : public ir_hierarchical_visitor {
84public:
85 find_assignment_visitor(const char *name)
86 : name(name), found(false)
87 {
88 /* empty */
89 }
90
91 virtual ir_visitor_status visit_enter(ir_assignment *ir)
92 {
93 ir_variable *const var = ir->lhs->variable_referenced();
94
95 if (strcmp(name, var->name) == 0) {
96 found = true;
97 return visit_stop;
98 }
99
100 return visit_continue_with_parent;
101 }
102
Eric Anholt18a60232010-08-23 11:29:25 -0700103 virtual ir_visitor_status visit_enter(ir_call *ir)
104 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700105 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700106 foreach_iter(exec_list_iterator, iter, *ir) {
107 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
108 ir_variable *sig_param = (ir_variable *)sig_iter.get();
109
Paul Berry42a29d82013-01-11 14:39:32 -0800110 if (sig_param->mode == ir_var_function_out ||
111 sig_param->mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700112 ir_variable *var = param_rval->variable_referenced();
113 if (var && strcmp(name, var->name) == 0) {
114 found = true;
115 return visit_stop;
116 }
117 }
118 sig_iter.next();
119 }
120
Kenneth Graunked884f602012-03-20 15:56:37 -0700121 if (ir->return_deref != NULL) {
122 ir_variable *const var = ir->return_deref->variable_referenced();
123
124 if (strcmp(name, var->name) == 0) {
125 found = true;
126 return visit_stop;
127 }
128 }
129
Eric Anholt18a60232010-08-23 11:29:25 -0700130 return visit_continue_with_parent;
131 }
132
Ian Romanick832dfa52010-06-17 15:04:20 -0700133 bool variable_found()
134 {
135 return found;
136 }
137
138private:
139 const char *name; /**< Find writes to a variable with this name. */
140 bool found; /**< Was a write to the variable found? */
141};
142
Ian Romanickc93b8f12010-06-17 15:20:22 -0700143
Ian Romanickc33e78f2010-08-13 12:30:41 -0700144/**
145 * Visitor that determines whether or not a variable is ever read.
146 */
147class find_deref_visitor : public ir_hierarchical_visitor {
148public:
149 find_deref_visitor(const char *name)
150 : name(name), found(false)
151 {
152 /* empty */
153 }
154
155 virtual ir_visitor_status visit(ir_dereference_variable *ir)
156 {
157 if (strcmp(this->name, ir->var->name) == 0) {
158 this->found = true;
159 return visit_stop;
160 }
161
162 return visit_continue;
163 }
164
165 bool variable_found() const
166 {
167 return this->found;
168 }
169
170private:
171 const char *name; /**< Find writes to a variable with this name. */
172 bool found; /**< Was a write to the variable found? */
173};
174
175
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700176void
Ian Romanick586e7412011-07-28 14:04:09 -0700177linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700178{
179 va_list ap;
180
Kenneth Graunked3073f52011-01-21 14:32:31 -0800181 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700182 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800183 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700184 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700185
186 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700187}
188
189
190void
Ian Romanick379a32f2011-07-28 14:09:06 -0700191linker_warning(gl_shader_program *prog, const char *fmt, ...)
192{
193 va_list ap;
194
195 ralloc_strcat(&prog->InfoLog, "error: ");
196 va_start(ap, fmt);
197 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
198 va_end(ap);
199
200}
201
202
Paul Berryb92900d2013-01-28 14:21:59 -0800203/**
204 * Given a string identifying a program resource, break it into a base name
205 * and an optional array index in square brackets.
206 *
207 * If an array index is present, \c out_base_name_end is set to point to the
208 * "[" that precedes the array index, and the array index itself is returned
209 * as a long.
210 *
211 * If no array index is present (or if the array index is negative or
212 * mal-formed), \c out_base_name_end, is set to point to the null terminator
213 * at the end of the input string, and -1 is returned.
214 *
215 * Only the final array index is parsed; if the string contains other array
216 * indices (or structure field accesses), they are left in the base name.
217 *
218 * No attempt is made to check that the base name is properly formed;
219 * typically the caller will look up the base name in a hash table, so
220 * ill-formed base names simply turn into hash table lookup failures.
221 */
222long
223parse_program_resource_name(const GLchar *name,
224 const GLchar **out_base_name_end)
225{
226 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
227 *
228 * "When an integer array element or block instance number is part of
229 * the name string, it will be specified in decimal form without a "+"
230 * or "-" sign or any extra leading zeroes. Additionally, the name
231 * string will not include white space anywhere in the string."
232 */
233
234 const size_t len = strlen(name);
235 *out_base_name_end = name + len;
236
237 if (len == 0 || name[len-1] != ']')
238 return -1;
239
240 /* Walk backwards over the string looking for a non-digit character. This
241 * had better be the opening bracket for an array index.
242 *
243 * Initially, i specifies the location of the ']'. Since the string may
244 * contain only the ']' charcater, walk backwards very carefully.
245 */
246 unsigned i;
247 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
248 /* empty */ ;
249
250 if ((i == 0) || name[i-1] != '[')
251 return -1;
252
253 long array_index = strtol(&name[i], NULL, 10);
254 if (array_index < 0)
255 return -1;
256
257 *out_base_name_end = name + (i - 1);
258 return array_index;
259}
260
261
Ian Romanick379a32f2011-07-28 14:09:06 -0700262void
Paul Berry50895d42012-12-05 07:17:07 -0800263link_invalidate_variable_locations(gl_shader *sh, int input_base,
264 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700265{
Eric Anholt16b68b12010-06-30 11:05:43 -0700266 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700267 ir_variable *const var = ((ir_instruction *) node)->as_variable();
268
Paul Berry50895d42012-12-05 07:17:07 -0800269 if (var == NULL)
270 continue;
271
272 int base;
273 switch (var->mode) {
Paul Berry42a29d82013-01-11 14:39:32 -0800274 case ir_var_shader_in:
Paul Berry50895d42012-12-05 07:17:07 -0800275 base = input_base;
276 break;
Paul Berry42a29d82013-01-11 14:39:32 -0800277 case ir_var_shader_out:
Paul Berry50895d42012-12-05 07:17:07 -0800278 base = output_base;
279 break;
280 default:
281 continue;
282 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700283
284 /* Only assign locations for generic attributes / varyings / etc.
285 */
Paul Berry50895d42012-12-05 07:17:07 -0800286 if ((var->location >= base) && !var->explicit_location)
287 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800288
Paul Berry3e81c662012-12-05 10:47:55 -0800289 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800290 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800291 var->location_frac = 0;
292 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800293 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800294 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700295 }
296}
297
298
Ian Romanickc93b8f12010-06-17 15:20:22 -0700299/**
Ian Romanick69846702010-06-22 17:29:19 -0700300 * Determine the number of attribute slots required for a particular type
301 *
302 * This code is here because it implements the language rules of a specific
303 * GLSL version. Since it's a property of the language and not a property of
304 * types in general, it doesn't really belong in glsl_type.
305 */
306unsigned
307count_attribute_slots(const glsl_type *t)
308{
309 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
310 *
311 * "A scalar input counts the same amount against this limit as a vec4,
312 * so applications may want to consider packing groups of four
313 * unrelated float inputs together into a vector to better utilize the
314 * capabilities of the underlying hardware. A matrix input will use up
315 * multiple locations. The number of locations used will equal the
316 * number of columns in the matrix."
317 *
318 * The spec does not explicitly say how arrays are counted. However, it
319 * should be safe to assume the total number of slots consumed by an array
320 * is the number of entries in the array multiplied by the number of slots
321 * consumed by a single element of the array.
322 */
323
324 if (t->is_array())
325 return t->array_size() * count_attribute_slots(t->element_type());
326
327 if (t->is_matrix())
328 return t->matrix_columns;
329
330 return 1;
331}
332
333
334/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700335 * Verify that a vertex shader executable meets all semantic requirements.
336 *
Paul Berry642e5b412012-01-04 13:57:52 -0800337 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
338 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700339 *
340 * \param shader Vertex shader executable to be verified
341 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700342bool
Eric Anholt849e1812010-06-30 11:49:17 -0700343validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700344 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700345{
346 if (shader == NULL)
347 return true;
348
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700349 /* From the GLSL 1.10 spec, page 48:
350 *
351 * "The variable gl_Position is available only in the vertex
352 * language and is intended for writing the homogeneous vertex
353 * position. All executions of a well-formed vertex shader
354 * executable must write a value into this variable. [...] The
355 * variable gl_Position is available only in the vertex
356 * language and is intended for writing the homogeneous vertex
357 * position. All executions of a well-formed vertex shader
358 * executable must write a value into this variable."
359 *
360 * while in GLSL 1.40 this text is changed to:
361 *
362 * "The variable gl_Position is available only in the vertex
363 * language and is intended for writing the homogeneous vertex
364 * position. It can be written at any time during shader
365 * execution. It may also be read back by a vertex shader
366 * after being written. This value will be used by primitive
367 * assembly, clipping, culling, and other fixed functionality
368 * operations, if present, that operate on primitives after
369 * vertex processing has occurred. Its value is undefined if
370 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700371 *
372 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
373 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700374 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700375 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700376 find_assignment_visitor find("gl_Position");
377 find.run(shader->ir);
378 if (!find.variable_found()) {
379 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
380 return false;
381 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700382 }
383
Paul Berry642e5b412012-01-04 13:57:52 -0800384 prog->Vert.ClipDistanceArraySize = 0;
385
Paul Berry15ba2a52012-08-02 17:51:02 -0700386 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700387 /* From section 7.1 (Vertex Shader Special Variables) of the
388 * GLSL 1.30 spec:
389 *
390 * "It is an error for a shader to statically write both
391 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700392 *
393 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
394 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700395 */
396 find_assignment_visitor clip_vertex("gl_ClipVertex");
397 find_assignment_visitor clip_distance("gl_ClipDistance");
398
399 clip_vertex.run(shader->ir);
400 clip_distance.run(shader->ir);
401 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
402 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
403 "and `gl_ClipDistance'\n");
404 return false;
405 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700406 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800407 ir_variable *clip_distance_var =
408 shader->symbols->get_variable("gl_ClipDistance");
409 if (clip_distance_var)
410 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700411 }
412
Ian Romanick832dfa52010-06-17 15:04:20 -0700413 return true;
414}
415
416
Ian Romanickc93b8f12010-06-17 15:20:22 -0700417/**
418 * Verify that a fragment shader executable meets all semantic requirements
419 *
420 * \param shader Fragment shader executable to be verified
421 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700422bool
Eric Anholt849e1812010-06-30 11:49:17 -0700423validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700424 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700425{
426 if (shader == NULL)
427 return true;
428
Ian Romanick832dfa52010-06-17 15:04:20 -0700429 find_assignment_visitor frag_color("gl_FragColor");
430 find_assignment_visitor frag_data("gl_FragData");
431
Eric Anholt16b68b12010-06-30 11:05:43 -0700432 frag_color.run(shader->ir);
433 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700434
Ian Romanick832dfa52010-06-17 15:04:20 -0700435 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700436 linker_error(prog, "fragment shader writes to both "
437 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700438 return false;
439 }
440
441 return true;
442}
443
444
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700445/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700446 * Generate a string describing the mode of a variable
447 */
448static const char *
449mode_string(const ir_variable *var)
450{
451 switch (var->mode) {
452 case ir_var_auto:
453 return (var->read_only) ? "global constant" : "global variable";
454
Paul Berry42a29d82013-01-11 14:39:32 -0800455 case ir_var_uniform: return "uniform";
456 case ir_var_shader_in: return "shader input";
457 case ir_var_shader_out: return "shader output";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700458
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800459 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700460 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700461 default:
462 assert(!"Should not get here.");
463 return "invalid variable";
464 }
465}
466
467
468/**
469 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700470 */
471bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700472cross_validate_globals(struct gl_shader_program *prog,
473 struct gl_shader **shader_list,
474 unsigned num_shaders,
475 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700476{
477 /* Examine all of the uniforms in all of the shaders and cross validate
478 * them.
479 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700480 glsl_symbol_table variables;
481 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700482 if (shader_list[i] == NULL)
483 continue;
484
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700485 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700486 ir_variable *const var = ((ir_instruction *) node)->as_variable();
487
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700488 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700489 continue;
490
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700491 if (uniforms_only && (var->mode != ir_var_uniform))
492 continue;
493
Ian Romanick7e2aa912010-07-19 17:12:42 -0700494 /* Don't cross validate temporaries that are at global scope. These
495 * will eventually get pulled into the shaders 'main'.
496 */
497 if (var->mode == ir_var_temporary)
498 continue;
499
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700500 /* If a global with this name has already been seen, verify that the
501 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700502 * initializers, the values of the initializers must be the same.
503 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700504 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700505 if (existing != NULL) {
506 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700507 /* Consider the types to be "the same" if both types are arrays
508 * of the same type and one of the arrays is implicitly sized.
509 * In addition, set the type of the linked variable to the
510 * explicitly sized array.
511 */
512 if (var->type->is_array()
513 && existing->type->is_array()
514 && (var->type->fields.array == existing->type->fields.array)
515 && ((var->type->length == 0)
516 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800517 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700518 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800519 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700520 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700521 linker_error(prog, "%s `%s' declared as type "
522 "`%s' and type `%s'\n",
523 mode_string(var),
524 var->name, var->type->name,
525 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700526 return false;
527 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700528 }
529
Ian Romanick68a4fc92010-10-07 17:21:22 -0700530 if (var->explicit_location) {
531 if (existing->explicit_location
532 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700533 linker_error(prog, "explicit locations for %s "
534 "`%s' have differing values\n",
535 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700536 return false;
537 }
538
539 existing->location = var->location;
540 existing->explicit_location = true;
541 }
542
Ian Romanick46173f92011-10-31 13:07:06 -0700543 /* Validate layout qualifiers for gl_FragDepth.
544 *
545 * From the AMD/ARB_conservative_depth specs:
546 *
547 * "If gl_FragDepth is redeclared in any fragment shader in a
548 * program, it must be redeclared in all fragment shaders in
549 * that program that have static assignments to
550 * gl_FragDepth. All redeclarations of gl_FragDepth in all
551 * fragment shaders in a single program must have the same set
552 * of qualifiers."
553 */
554 if (strcmp(var->name, "gl_FragDepth") == 0) {
555 bool layout_declared = var->depth_layout != ir_depth_layout_none;
556 bool layout_differs =
557 var->depth_layout != existing->depth_layout;
558
559 if (layout_declared && layout_differs) {
560 linker_error(prog,
561 "All redeclarations of gl_FragDepth in all "
562 "fragment shaders in a single program must have "
563 "the same set of qualifiers.");
564 }
565
566 if (var->used && layout_differs) {
567 linker_error(prog,
568 "If gl_FragDepth is redeclared with a layout "
569 "qualifier in any fragment shader, it must be "
570 "redeclared with the same layout qualifier in "
571 "all fragment shaders that have assignments to "
572 "gl_FragDepth");
573 }
574 }
Chad Versaceaddae332011-01-27 01:40:31 -0800575
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700576 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
577 *
578 * "If a shared global has multiple initializers, the
579 * initializers must all be constant expressions, and they
580 * must all have the same value. Otherwise, a link error will
581 * result. (A shared global having only one initializer does
582 * not require that initializer to be a constant expression.)"
583 *
584 * Previous to 4.20 the GLSL spec simply said that initializers
585 * must have the same value. In this case of non-constant
586 * initializers, this was impossible to determine. As a result,
587 * no vendor actually implemented that behavior. The 4.20
588 * behavior matches the implemented behavior of at least one other
589 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700590 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700591 if (var->constant_initializer != NULL) {
592 if (existing->constant_initializer != NULL) {
593 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700594 linker_error(prog, "initializers for %s "
595 "`%s' have differing values\n",
596 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700597 return false;
598 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700599 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700600 /* If the first-seen instance of a particular uniform did not
601 * have an initializer but a later instance does, copy the
602 * initializer to the version stored in the symbol table.
603 */
Ian Romanickde415b72010-07-14 13:22:12 -0700604 /* FINISHME: This is wrong. The constant_value field should
605 * FINISHME: not be modified! Imagine a case where a shader
606 * FINISHME: without an initializer is linked in two different
607 * FINISHME: programs with shaders that have differing
608 * FINISHME: initializers. Linking with the first will
609 * FINISHME: modify the shader, and linking with the second
610 * FINISHME: will fail.
611 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700612 existing->constant_initializer =
613 var->constant_initializer->clone(ralloc_parent(existing),
614 NULL);
615 }
616 }
617
618 if (var->has_initializer) {
619 if (existing->has_initializer
620 && (var->constant_initializer == NULL
621 || existing->constant_initializer == NULL)) {
622 linker_error(prog,
623 "shared global variable `%s' has multiple "
624 "non-constant initializers.\n",
625 var->name);
626 return false;
627 }
628
629 /* Some instance had an initializer, so keep track of that. In
630 * this location, all sorts of initializers (constant or
631 * otherwise) will propagate the existence to the variable
632 * stored in the symbol table.
633 */
634 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700635 }
Chad Versace7528f142010-11-17 14:34:38 -0800636
637 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700638 linker_error(prog, "declarations for %s `%s' have "
639 "mismatching invariant qualifiers\n",
640 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800641 return false;
642 }
Chad Versace61428dd2011-01-10 15:29:30 -0800643 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700644 linker_error(prog, "declarations for %s `%s' have "
645 "mismatching centroid qualifiers\n",
646 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800647 return false;
648 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700649 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700650 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700651 }
652 }
653
654 return true;
655}
656
657
Ian Romanick37101922010-06-18 19:02:10 -0700658/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700659 * Perform validation of uniforms used across multiple shader stages
660 */
661bool
662cross_validate_uniforms(struct gl_shader_program *prog)
663{
664 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700665 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700666}
667
Eric Anholtf609cf72012-04-27 13:52:56 -0700668/**
669 * Accumulates the array of prog->UniformBlocks and checks that all
670 * definitons of blocks agree on their contents.
671 */
672static bool
673interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
674{
675 unsigned max_num_uniform_blocks = 0;
676 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
677 if (prog->_LinkedShaders[i])
678 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
679 }
680
681 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
682 struct gl_shader *sh = prog->_LinkedShaders[i];
683
684 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
685 max_num_uniform_blocks);
686 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
687 prog->UniformBlockStageIndex[i][j] = -1;
688
689 if (sh == NULL)
690 continue;
691
692 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
693 int index = link_cross_validate_uniform_block(prog,
694 &prog->UniformBlocks,
695 &prog->NumUniformBlocks,
696 &sh->UniformBlocks[j]);
697
698 if (index == -1) {
699 linker_error(prog, "uniform block `%s' has mismatching definitions",
700 sh->UniformBlocks[j].Name);
701 return false;
702 }
703
704 prog->UniformBlockStageIndex[i][index] = j;
705 }
706 }
707
708 return true;
709}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700710
Ian Romanick37101922010-06-18 19:02:10 -0700711
Ian Romanick3fb87872010-07-09 14:09:34 -0700712/**
713 * Populates a shaders symbol table with all global declarations
714 */
715static void
716populate_symbol_table(gl_shader *sh)
717{
718 sh->symbols = new(sh) glsl_symbol_table;
719
720 foreach_list(node, sh->ir) {
721 ir_instruction *const inst = (ir_instruction *) node;
722 ir_variable *var;
723 ir_function *func;
724
725 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700726 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700727 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700728 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700729 }
730 }
731}
732
733
734/**
Ian Romanick31a97862010-07-12 18:48:50 -0700735 * Remap variables referenced in an instruction tree
736 *
737 * This is used when instruction trees are cloned from one shader and placed in
738 * another. These trees will contain references to \c ir_variable nodes that
739 * do not exist in the target shader. This function finds these \c ir_variable
740 * references and replaces the references with matching variables in the target
741 * shader.
742 *
743 * If there is no matching variable in the target shader, a clone of the
744 * \c ir_variable is made and added to the target shader. The new variable is
745 * added to \b both the instruction stream and the symbol table.
746 *
747 * \param inst IR tree that is to be processed.
748 * \param symbols Symbol table containing global scope symbols in the
749 * linked shader.
750 * \param instructions Instruction stream where new variable declarations
751 * should be added.
752 */
753void
Eric Anholt8273bd42010-08-04 12:34:56 -0700754remap_variables(ir_instruction *inst, struct gl_shader *target,
755 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700756{
757 class remap_visitor : public ir_hierarchical_visitor {
758 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700759 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700760 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700761 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700762 this->target = target;
763 this->symbols = target->symbols;
764 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700765 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700766 }
767
768 virtual ir_visitor_status visit(ir_dereference_variable *ir)
769 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700770 if (ir->var->mode == ir_var_temporary) {
771 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
772
773 assert(var != NULL);
774 ir->var = var;
775 return visit_continue;
776 }
777
Ian Romanick31a97862010-07-12 18:48:50 -0700778 ir_variable *const existing =
779 this->symbols->get_variable(ir->var->name);
780 if (existing != NULL)
781 ir->var = existing;
782 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700783 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700784
Eric Anholt001eee52010-11-05 06:11:24 -0700785 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700786 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700787 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700788 }
789
790 return visit_continue;
791 }
792
793 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700794 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700795 glsl_symbol_table *symbols;
796 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700797 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700798 };
799
Eric Anholt8273bd42010-08-04 12:34:56 -0700800 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700801
802 inst->accept(&v);
803}
804
805
806/**
807 * Move non-declarations from one instruction stream to another
808 *
809 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700810 * 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 -0700811 * pointer) for \c last and \c false for \c make_copies on the first
812 * call. Successive calls pass the return value of the previous call for
813 * \c last and \c true for \c make_copies.
814 *
815 * \param instructions Source instruction stream
816 * \param last Instruction after which new instructions should be
817 * inserted in the target instruction stream
818 * \param make_copies Flag selecting whether instructions in \c instructions
819 * should be copied (via \c ir_instruction::clone) into the
820 * target list or moved.
821 *
822 * \return
823 * The new "last" instruction in the target instruction stream. This pointer
824 * is suitable for use as the \c last parameter of a later call to this
825 * function.
826 */
827exec_node *
828move_non_declarations(exec_list *instructions, exec_node *last,
829 bool make_copies, gl_shader *target)
830{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700831 hash_table *temps = NULL;
832
833 if (make_copies)
834 temps = hash_table_ctor(0, hash_table_pointer_hash,
835 hash_table_pointer_compare);
836
Ian Romanick303c99f2010-07-19 12:34:56 -0700837 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700838 ir_instruction *inst = (ir_instruction *) node;
839
Ian Romanick7e2aa912010-07-19 17:12:42 -0700840 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700841 continue;
842
Ian Romanick7e2aa912010-07-19 17:12:42 -0700843 ir_variable *var = inst->as_variable();
844 if ((var != NULL) && (var->mode != ir_var_temporary))
845 continue;
846
847 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700848 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700849 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700850 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700851
852 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700853 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700854
855 if (var != NULL)
856 hash_table_insert(temps, inst, var);
857 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700858 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700859 } else {
860 inst->remove();
861 }
862
863 last->insert_after(inst);
864 last = inst;
865 }
866
Ian Romanick7e2aa912010-07-19 17:12:42 -0700867 if (make_copies)
868 hash_table_dtor(temps);
869
Ian Romanick31a97862010-07-12 18:48:50 -0700870 return last;
871}
872
873/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700874 * Get the function signature for main from a shader
875 */
876static ir_function_signature *
877get_main_function_signature(gl_shader *sh)
878{
879 ir_function *const f = sh->symbols->get_function("main");
880 if (f != NULL) {
881 exec_list void_parameters;
882
883 /* Look for the 'void main()' signature and ensure that it's defined.
884 * This keeps the linker from accidentally pick a shader that just
885 * contains a prototype for main.
886 *
887 * We don't have to check for multiple definitions of main (in multiple
888 * shaders) because that would have already been caught above.
889 */
890 ir_function_signature *sig = f->matching_signature(&void_parameters);
891 if ((sig != NULL) && sig->is_defined) {
892 return sig;
893 }
894 }
895
896 return NULL;
897}
898
899
900/**
Brian Paul84a12732012-02-02 20:10:40 -0700901 * This class is only used in link_intrastage_shaders() below but declaring
902 * it inside that function leads to compiler warnings with some versions of
903 * gcc.
904 */
905class array_sizing_visitor : public ir_hierarchical_visitor {
906public:
907 virtual ir_visitor_status visit(ir_variable *var)
908 {
909 if (var->type->is_array() && (var->type->length == 0)) {
910 const glsl_type *type =
911 glsl_type::get_array_instance(var->type->fields.array,
912 var->max_array_access + 1);
913 assert(type != NULL);
914 var->type = type;
915 }
916 return visit_continue;
917 }
918};
919
Brian Paul84a12732012-02-02 20:10:40 -0700920/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700921 * Combine a group of shaders for a single stage to generate a linked shader
922 *
923 * \note
924 * If this function is supplied a single shader, it is cloned, and the new
925 * shader is returned.
926 */
927static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800928link_intrastage_shaders(void *mem_ctx,
929 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700930 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700931 struct gl_shader **shader_list,
932 unsigned num_shaders)
933{
Eric Anholtf609cf72012-04-27 13:52:56 -0700934 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -0700935
Ian Romanick13f782c2010-06-29 18:53:38 -0700936 /* Check that global variables defined in multiple shaders are consistent.
937 */
938 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
939 return NULL;
940
Eric Anholtf609cf72012-04-27 13:52:56 -0700941 /* Check that uniform blocks between shaders for a stage agree. */
Ian Romanick514f8c72013-01-22 01:09:16 -0500942 const int num_uniform_blocks =
943 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
944 &uniform_blocks);
945 if (num_uniform_blocks < 0)
946 return NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -0700947
Ian Romanick13f782c2010-06-29 18:53:38 -0700948 /* Check that there is only a single definition of each function signature
949 * across all shaders.
950 */
951 for (unsigned i = 0; i < (num_shaders - 1); i++) {
952 foreach_list(node, shader_list[i]->ir) {
953 ir_function *const f = ((ir_instruction *) node)->as_function();
954
955 if (f == NULL)
956 continue;
957
958 for (unsigned j = i + 1; j < num_shaders; j++) {
959 ir_function *const other =
960 shader_list[j]->symbols->get_function(f->name);
961
962 /* If the other shader has no function (and therefore no function
963 * signatures) with the same name, skip to the next shader.
964 */
965 if (other == NULL)
966 continue;
967
968 foreach_iter (exec_list_iterator, iter, *f) {
969 ir_function_signature *sig =
970 (ir_function_signature *) iter.get();
971
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700972 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700973 continue;
974
975 ir_function_signature *other_sig =
976 other->exact_matching_signature(& sig->parameters);
977
978 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700979 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -0700980 linker_error(prog, "function `%s' is multiply defined",
981 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -0700982 return NULL;
983 }
984 }
985 }
986 }
987 }
988
989 /* Find the shader that defines main, and make a clone of it.
990 *
991 * Starting with the clone, search for undefined references. If one is
992 * found, find the shader that defines it. Clone the reference and add
993 * it to the shader. Repeat until there are no undefined references or
994 * until a reference cannot be resolved.
995 */
Ian Romanick15ce87e2010-07-09 15:28:22 -0700996 gl_shader *main = NULL;
997 for (unsigned i = 0; i < num_shaders; i++) {
998 if (get_main_function_signature(shader_list[i]) != NULL) {
999 main = shader_list[i];
1000 break;
1001 }
1002 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001003
Ian Romanick15ce87e2010-07-09 15:28:22 -07001004 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001005 linker_error(prog, "%s shader lacks `main'\n",
1006 (shader_list[0]->Type == GL_VERTEX_SHADER)
1007 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -07001008 return NULL;
1009 }
1010
Ian Romanick4a455952010-10-13 15:13:02 -07001011 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001012 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001013 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001014
Eric Anholtf609cf72012-04-27 13:52:56 -07001015 linked->UniformBlocks = uniform_blocks;
1016 linked->NumUniformBlocks = num_uniform_blocks;
1017 ralloc_steal(linked, linked->UniformBlocks);
1018
Ian Romanick15ce87e2010-07-09 15:28:22 -07001019 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001020
Ian Romanick31a97862010-07-12 18:48:50 -07001021 /* The a pointer to the main function in the final linked shader (i.e., the
1022 * copy of the original shader that contained the main function).
1023 */
1024 ir_function_signature *const main_sig = get_main_function_signature(linked);
1025
1026 /* Move any instructions other than variable declarations or function
1027 * declarations into main.
1028 */
Ian Romanick9303e352010-07-19 12:33:54 -07001029 exec_node *insertion_point =
1030 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1031 linked);
1032
Ian Romanick31a97862010-07-12 18:48:50 -07001033 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001034 if (shader_list[i] == main)
1035 continue;
1036
Ian Romanick31a97862010-07-12 18:48:50 -07001037 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001038 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001039 }
1040
Ian Romanick13f782c2010-06-29 18:53:38 -07001041 /* Resolve initializers for global variables in the linked shader.
1042 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001043 unsigned num_linking_shaders = num_shaders;
1044 for (unsigned i = 0; i < num_shaders; i++)
1045 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1046
1047 gl_shader **linking_shaders =
1048 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1049
1050 memcpy(linking_shaders, shader_list,
1051 sizeof(linking_shaders[0]) * num_shaders);
1052
1053 unsigned idx = num_shaders;
1054 for (unsigned i = 0; i < num_shaders; i++) {
1055 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1056 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1057 idx += shader_list[i]->num_builtins_to_link;
1058 }
1059
1060 assert(idx == num_linking_shaders);
1061
Ian Romanick4a455952010-10-13 15:13:02 -07001062 if (!link_function_calls(prog, linked, linking_shaders,
1063 num_linking_shaders)) {
1064 ctx->Driver.DeleteShader(ctx, linked);
1065 linked = NULL;
1066 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001067
1068 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001069
Paul Berryc148ef62011-08-03 15:37:01 -07001070#ifdef DEBUG
1071 /* At this point linked should contain all of the linked IR, so
1072 * validate it to make sure nothing went wrong.
1073 */
1074 if (linked)
1075 validate_ir_tree(linked->ir);
1076#endif
1077
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001078 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001079 * unspecified sizes have a size specified. The size is inferred from the
1080 * max_array_access field.
1081 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001082 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001083 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001084
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001085 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001086 }
1087
Ian Romanick3fb87872010-07-09 14:09:34 -07001088 return linked;
1089}
1090
Eric Anholta721abf2010-08-23 10:32:01 -07001091/**
1092 * Update the sizes of linked shader uniform arrays to the maximum
1093 * array index used.
1094 *
1095 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1096 *
1097 * If one or more elements of an array are active,
1098 * GetActiveUniform will return the name of the array in name,
1099 * subject to the restrictions listed above. The type of the array
1100 * is returned in type. The size parameter contains the highest
1101 * array element index used, plus one. The compiler or linker
1102 * determines the highest index used. There will be only one
1103 * active uniform reported by the GL per uniform array.
1104
1105 */
1106static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001107update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001108{
Ian Romanick3322fba2010-10-14 13:28:42 -07001109 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1110 if (prog->_LinkedShaders[i] == NULL)
1111 continue;
1112
Eric Anholta721abf2010-08-23 10:32:01 -07001113 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1114 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1115
Eric Anholt586b4b52010-09-28 14:32:16 -07001116 if ((var == NULL) || (var->mode != ir_var_uniform &&
Paul Berry42a29d82013-01-11 14:39:32 -08001117 var->mode != ir_var_shader_in &&
1118 var->mode != ir_var_shader_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001119 !var->type->is_array())
1120 continue;
1121
Eric Anholt9feb4032012-05-01 14:43:31 -07001122 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1123 * will not be eliminated. Since we always do std140, just
1124 * don't resize arrays in UBOs.
1125 */
Ian Romanick13be1f42012-12-14 12:00:14 -08001126 if (var->is_in_uniform_block())
Eric Anholt9feb4032012-05-01 14:43:31 -07001127 continue;
1128
Eric Anholta721abf2010-08-23 10:32:01 -07001129 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001130 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1131 if (prog->_LinkedShaders[j] == NULL)
1132 continue;
1133
Eric Anholta721abf2010-08-23 10:32:01 -07001134 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1135 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1136 if (!other_var)
1137 continue;
1138
1139 if (strcmp(var->name, other_var->name) == 0 &&
1140 other_var->max_array_access > size) {
1141 size = other_var->max_array_access;
1142 }
1143 }
1144 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001145
Eric Anholta721abf2010-08-23 10:32:01 -07001146 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001147 /* If this is a built-in uniform (i.e., it's backed by some
1148 * fixed-function state), adjust the number of state slots to
1149 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001150 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001151 * slots is an integer multiple of the number of array elements.
1152 * Determine the number of slots per array element by dividing by
1153 * the old (total) size.
1154 */
1155 if (var->num_state_slots > 0) {
1156 var->num_state_slots = (size + 1)
1157 * (var->num_state_slots / var->type->length);
1158 }
1159
Eric Anholta721abf2010-08-23 10:32:01 -07001160 var->type = glsl_type::get_array_instance(var->type->fields.array,
1161 size + 1);
1162 /* FINISHME: We should update the types of array
1163 * dereferences of this variable now.
1164 */
1165 }
1166 }
1167 }
1168}
1169
Ian Romanick69846702010-06-22 17:29:19 -07001170/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001171 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001172 *
1173 * \param used_mask Bits representing used (1) and unused (0) locations
1174 * \param needed_count Number of contiguous bits needed.
1175 *
1176 * \return
1177 * Base location of the available bits on success or -1 on failure.
1178 */
1179int
1180find_available_slots(unsigned used_mask, unsigned needed_count)
1181{
1182 unsigned needed_mask = (1 << needed_count) - 1;
1183 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1184
1185 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1186 * cannot optimize possibly infinite loops" for the loop below.
1187 */
1188 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1189 return -1;
1190
1191 for (int i = 0; i <= max_bit_to_test; i++) {
1192 if ((needed_mask & ~used_mask) == needed_mask)
1193 return i;
1194
1195 needed_mask <<= 1;
1196 }
1197
1198 return -1;
1199}
1200
1201
Ian Romanickd32d4f72011-06-27 17:59:58 -07001202/**
1203 * Assign locations for either VS inputs for FS outputs
1204 *
1205 * \param prog Shader program whose variables need locations assigned
1206 * \param target_index Selector for the program target to receive location
1207 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1208 * \c MESA_SHADER_FRAGMENT.
1209 * \param max_index Maximum number of generic locations. This corresponds
1210 * to either the maximum number of draw buffers or the
1211 * maximum number of generic attributes.
1212 *
1213 * \return
1214 * If locations are successfully assigned, true is returned. Otherwise an
1215 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001216 */
Ian Romanick69846702010-06-22 17:29:19 -07001217bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001218assign_attribute_or_color_locations(gl_shader_program *prog,
1219 unsigned target_index,
1220 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001221{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001222 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001223 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001224 unsigned used_locations = (max_index >= 32)
1225 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001226
Ian Romanickd32d4f72011-06-27 17:59:58 -07001227 assert((target_index == MESA_SHADER_VERTEX)
1228 || (target_index == MESA_SHADER_FRAGMENT));
1229
1230 gl_shader *const sh = prog->_LinkedShaders[target_index];
1231 if (sh == NULL)
1232 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001233
Ian Romanick69846702010-06-22 17:29:19 -07001234 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001235 *
1236 * 1. Invalidate the location assignments for all vertex shader inputs.
1237 *
1238 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001239 * glBindVertexAttribLocation) locations and outputs that have
1240 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001241 *
Ian Romanick69846702010-06-22 17:29:19 -07001242 * 3. Sort the attributes without assigned locations by number of slots
1243 * required in decreasing order. Fragmentation caused by attribute
1244 * locations assigned by the application may prevent large attributes
1245 * from having enough contiguous space.
1246 *
1247 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001248 */
1249
Ian Romanickd32d4f72011-06-27 17:59:58 -07001250 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001251 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001252
Ian Romanickd32d4f72011-06-27 17:59:58 -07001253 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001254 (target_index == MESA_SHADER_VERTEX)
1255 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001256
1257
Ian Romanick69846702010-06-22 17:29:19 -07001258 /* Temporary storage for the set of attributes that need locations assigned.
1259 */
1260 struct temp_attr {
1261 unsigned slots;
1262 ir_variable *var;
1263
1264 /* Used below in the call to qsort. */
1265 static int compare(const void *a, const void *b)
1266 {
1267 const temp_attr *const l = (const temp_attr *) a;
1268 const temp_attr *const r = (const temp_attr *) b;
1269
1270 /* Reversed because we want a descending order sort below. */
1271 return r->slots - l->slots;
1272 }
1273 } to_assign[16];
1274
1275 unsigned num_attr = 0;
1276
Eric Anholt16b68b12010-06-30 11:05:43 -07001277 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001278 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1279
Brian Paul4470ff22011-07-19 21:10:25 -06001280 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001281 continue;
1282
Ian Romanick68a4fc92010-10-07 17:21:22 -07001283 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001284 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001285 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001286 linker_error(prog,
1287 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001288 (var->location < 0)
1289 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001290 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001291 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001292 }
1293 } else if (target_index == MESA_SHADER_VERTEX) {
1294 unsigned binding;
1295
1296 if (prog->AttributeBindings->get(binding, var->name)) {
1297 assert(binding >= VERT_ATTRIB_GENERIC0);
1298 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001299 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001300 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001301 } else if (target_index == MESA_SHADER_FRAGMENT) {
1302 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001303 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001304
1305 if (prog->FragDataBindings->get(binding, var->name)) {
1306 assert(binding >= FRAG_RESULT_DATA0);
1307 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001308 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001309
1310 if (prog->FragDataIndexBindings->get(index, var->name)) {
1311 var->index = index;
1312 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001313 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001314 }
1315
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001316 /* If the variable is not a built-in and has a location statically
1317 * assigned in the shader (presumably via a layout qualifier), make sure
1318 * that it doesn't collide with other assigned locations. Otherwise,
1319 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001320 */
Ian Romanick523b6112011-08-17 15:40:03 -07001321 const unsigned slots = count_attribute_slots(var->type);
1322 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001323 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001324 /* From page 61 of the OpenGL 4.0 spec:
1325 *
1326 * "LinkProgram will fail if the attribute bindings assigned
1327 * by BindAttribLocation do not leave not enough space to
1328 * assign a location for an active matrix attribute or an
1329 * active attribute array, both of which require multiple
1330 * contiguous generic attributes."
1331 *
1332 * Previous versions of the spec contain similar language but omit
1333 * the bit about attribute arrays.
1334 *
1335 * Page 61 of the OpenGL 4.0 spec also says:
1336 *
1337 * "It is possible for an application to bind more than one
1338 * attribute name to the same location. This is referred to as
1339 * aliasing. This will only work if only one of the aliased
1340 * attributes is active in the executable program, or if no
1341 * path through the shader consumes more than one attribute of
1342 * a set of attributes aliased to the same location. A link
1343 * error can occur if the linker determines that every path
1344 * through the shader consumes multiple aliased attributes,
1345 * but implementations are not required to generate an error
1346 * in this case."
1347 *
1348 * These two paragraphs are either somewhat contradictory, or I
1349 * don't fully understand one or both of them.
1350 */
1351 /* FINISHME: The code as currently written does not support
1352 * FINISHME: attribute location aliasing (see comment above).
1353 */
1354 /* Mask representing the contiguous slots that will be used by
1355 * this attribute.
1356 */
1357 const unsigned attr = var->location - generic_base;
1358 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001359
Ian Romanick523b6112011-08-17 15:40:03 -07001360 /* Generate a link error if the set of bits requested for this
1361 * attribute overlaps any previously allocated bits.
1362 */
1363 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001364 const char *const string = (target_index == MESA_SHADER_VERTEX)
1365 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001366 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001367 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001368 "available for %s `%s' %d %d %d", string,
1369 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001370 return false;
1371 }
1372
1373 used_locations |= (use_mask << attr);
1374 }
1375
1376 continue;
1377 }
1378
1379 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001380 to_assign[num_attr].var = var;
1381 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001382 }
Ian Romanick69846702010-06-22 17:29:19 -07001383
1384 /* If all of the attributes were assigned locations by the application (or
1385 * are built-in attributes with fixed locations), return early. This should
1386 * be the common case.
1387 */
1388 if (num_attr == 0)
1389 return true;
1390
1391 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1392
Ian Romanickd32d4f72011-06-27 17:59:58 -07001393 if (target_index == MESA_SHADER_VERTEX) {
1394 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1395 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1396 * reserved to prevent it from being automatically allocated below.
1397 */
1398 find_deref_visitor find("gl_Vertex");
1399 find.run(sh->ir);
1400 if (find.variable_found())
1401 used_locations |= (1 << 0);
1402 }
Ian Romanick982e3792010-06-29 18:58:20 -07001403
Ian Romanick69846702010-06-22 17:29:19 -07001404 for (unsigned i = 0; i < num_attr; i++) {
1405 /* Mask representing the contiguous slots that will be used by this
1406 * attribute.
1407 */
1408 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1409
1410 int location = find_available_slots(used_locations, to_assign[i].slots);
1411
1412 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001413 const char *const string = (target_index == MESA_SHADER_VERTEX)
1414 ? "vertex shader input" : "fragment shader output";
1415
Ian Romanick586e7412011-07-28 14:04:09 -07001416 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001417 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001418 "available for %s `%s'",
1419 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001420 return false;
1421 }
1422
Ian Romanickd32d4f72011-06-27 17:59:58 -07001423 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001424 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001425 used_locations |= (use_mask << location);
1426 }
1427
1428 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001429}
1430
1431
Ian Romanick40e114b2010-08-17 14:55:50 -07001432/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001433 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001434 */
1435void
Ian Romanickcc90e622010-10-19 17:59:10 -07001436demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001437{
1438 foreach_list(node, sh->ir) {
1439 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1440
Ian Romanickcc90e622010-10-19 17:59:10 -07001441 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001442 continue;
1443
Ian Romanickcc90e622010-10-19 17:59:10 -07001444 /* A shader 'in' or 'out' variable is only really an input or output if
1445 * its value is used by other shader stages. This will cause the variable
1446 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001447 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001448 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001449 var->mode = ir_var_auto;
1450 }
1451 }
1452}
1453
1454
Paul Berry871ddb92011-11-05 11:17:32 -07001455/**
Marek Olšákec174a42011-11-18 15:00:10 +01001456 * Store the gl_FragDepth layout in the gl_shader_program struct.
1457 */
1458static void
1459store_fragdepth_layout(struct gl_shader_program *prog)
1460{
1461 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1462 return;
1463 }
1464
1465 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1466
1467 /* We don't look up the gl_FragDepth symbol directly because if
1468 * gl_FragDepth is not used in the shader, it's removed from the IR.
1469 * However, the symbol won't be removed from the symbol table.
1470 *
1471 * We're only interested in the cases where the variable is NOT removed
1472 * from the IR.
1473 */
1474 foreach_list(node, ir) {
1475 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1476
Paul Berry42a29d82013-01-11 14:39:32 -08001477 if (var == NULL || var->mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001478 continue;
1479 }
1480
1481 if (strcmp(var->name, "gl_FragDepth") == 0) {
1482 switch (var->depth_layout) {
1483 case ir_depth_layout_none:
1484 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1485 return;
1486 case ir_depth_layout_any:
1487 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1488 return;
1489 case ir_depth_layout_greater:
1490 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1491 return;
1492 case ir_depth_layout_less:
1493 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1494 return;
1495 case ir_depth_layout_unchanged:
1496 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1497 return;
1498 default:
1499 assert(0);
1500 return;
1501 }
1502 }
1503 }
1504}
1505
1506/**
Ian Romanick92f81592011-11-08 12:37:19 -08001507 * Validate the resources used by a program versus the implementation limits
1508 */
1509static bool
1510check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1511{
1512 static const char *const shader_names[MESA_SHADER_TYPES] = {
1513 "vertex", "fragment", "geometry"
1514 };
1515
1516 const unsigned max_samplers[MESA_SHADER_TYPES] = {
1517 ctx->Const.MaxVertexTextureImageUnits,
1518 ctx->Const.MaxTextureImageUnits,
1519 ctx->Const.MaxGeometryTextureImageUnits
1520 };
1521
1522 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
1523 ctx->Const.VertexProgram.MaxUniformComponents,
1524 ctx->Const.FragmentProgram.MaxUniformComponents,
1525 0 /* FINISHME: Geometry shaders. */
1526 };
1527
Eric Anholt877a8972012-06-25 12:47:01 -07001528 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
1529 ctx->Const.VertexProgram.MaxUniformBlocks,
1530 ctx->Const.FragmentProgram.MaxUniformBlocks,
1531 ctx->Const.GeometryProgram.MaxUniformBlocks,
1532 };
1533
Ian Romanick92f81592011-11-08 12:37:19 -08001534 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1535 struct gl_shader *sh = prog->_LinkedShaders[i];
1536
1537 if (sh == NULL)
1538 continue;
1539
1540 if (sh->num_samplers > max_samplers[i]) {
1541 linker_error(prog, "Too many %s shader texture samplers",
1542 shader_names[i]);
1543 }
1544
1545 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001546 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1547 linker_warning(prog, "Too many %s shader uniform components, "
1548 "but the driver will try to optimize them out; "
1549 "this is non-portable out-of-spec behavior\n",
1550 shader_names[i]);
1551 } else {
1552 linker_error(prog, "Too many %s shader uniform components",
1553 shader_names[i]);
1554 }
Ian Romanick92f81592011-11-08 12:37:19 -08001555 }
1556 }
1557
Eric Anholt877a8972012-06-25 12:47:01 -07001558 unsigned blocks[MESA_SHADER_TYPES] = {0};
1559 unsigned total_uniform_blocks = 0;
1560
1561 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
1562 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1563 if (prog->UniformBlockStageIndex[j][i] != -1) {
1564 blocks[j]++;
1565 total_uniform_blocks++;
1566 }
1567 }
1568
1569 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1570 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1571 prog->NumUniformBlocks,
1572 ctx->Const.MaxCombinedUniformBlocks);
1573 } else {
1574 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1575 if (blocks[i] > max_uniform_blocks[i]) {
1576 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
1577 shader_names[i],
1578 blocks[i],
1579 max_uniform_blocks[i]);
1580 break;
1581 }
1582 }
1583 }
1584 }
1585
Ian Romanick92f81592011-11-08 12:37:19 -08001586 return prog->LinkStatus;
1587}
Paul Berry871ddb92011-11-05 11:17:32 -07001588
Ian Romanick0e59b262010-06-23 11:23:01 -07001589void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001590link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001591{
Paul Berry871ddb92011-11-05 11:17:32 -07001592 tfeedback_decl *tfeedback_decls = NULL;
1593 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1594
Kenneth Graunked3073f52011-01-21 14:32:31 -08001595 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001596
Ian Romanick832dfa52010-06-17 15:04:20 -07001597 prog->LinkStatus = false;
1598 prog->Validated = false;
1599 prog->_Used = false;
1600
Eric Anholtf609cf72012-04-27 13:52:56 -07001601 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08001602 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07001603
Eric Anholtf609cf72012-04-27 13:52:56 -07001604 ralloc_free(prog->UniformBlocks);
1605 prog->UniformBlocks = NULL;
1606 prog->NumUniformBlocks = 0;
1607 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
1608 ralloc_free(prog->UniformBlockStageIndex[i]);
1609 prog->UniformBlockStageIndex[i] = NULL;
1610 }
1611
Ian Romanick832dfa52010-06-17 15:04:20 -07001612 /* Separate the shaders into groups based on their type.
1613 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001614 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001615 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001616 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001617 unsigned num_frag_shaders = 0;
1618
Eric Anholt16b68b12010-06-30 11:05:43 -07001619 vert_shader_list = (struct gl_shader **)
1620 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001621 frag_shader_list = &vert_shader_list[prog->NumShaders];
1622
Ian Romanick25f51d32010-07-16 15:51:50 -07001623 unsigned min_version = UINT_MAX;
1624 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07001625 const bool is_es_prog =
1626 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07001627 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001628 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1629 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1630
Paul Berrya9f34dc2012-08-02 17:49:44 -07001631 if (prog->Shaders[i]->IsES != is_es_prog) {
1632 linker_error(prog, "all shaders must use same shading "
1633 "language version\n");
1634 goto done;
1635 }
1636
Ian Romanick832dfa52010-06-17 15:04:20 -07001637 switch (prog->Shaders[i]->Type) {
1638 case GL_VERTEX_SHADER:
1639 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1640 num_vert_shaders++;
1641 break;
1642 case GL_FRAGMENT_SHADER:
1643 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1644 num_frag_shaders++;
1645 break;
1646 case GL_GEOMETRY_SHADER:
1647 /* FINISHME: Support geometry shaders. */
1648 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1649 break;
1650 }
1651 }
1652
Ian Romanick25f51d32010-07-16 15:51:50 -07001653 /* Previous to GLSL version 1.30, different compilation units could mix and
1654 * match shading language versions. With GLSL 1.30 and later, the versions
1655 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07001656 *
1657 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07001658 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07001659 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001660 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07001661 linker_error(prog, "all shaders must use same shading "
1662 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07001663 goto done;
1664 }
1665
1666 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07001667 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07001668
Ian Romanick3322fba2010-10-14 13:28:42 -07001669 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1670 if (prog->_LinkedShaders[i] != NULL)
1671 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1672
1673 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07001674 }
1675
Ian Romanickcd6764e2010-07-16 16:00:07 -07001676 /* Link all shaders for a particular stage and validate the result.
1677 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001678 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001679 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001680 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1681 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001682
1683 if (sh == NULL)
1684 goto done;
1685
1686 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001687 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001688
Ian Romanick3322fba2010-10-14 13:28:42 -07001689 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1690 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001691 }
1692
1693 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001694 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001695 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1696 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001697
1698 if (sh == NULL)
1699 goto done;
1700
1701 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001702 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001703
Ian Romanick3322fba2010-10-14 13:28:42 -07001704 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1705 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001706 }
1707
Ian Romanick3ed850e2010-06-23 12:18:21 -07001708 /* Here begins the inter-stage linking phase. Some initial validation is
1709 * performed, then locations are assigned for uniforms, attributes, and
1710 * varyings.
1711 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07001712 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07001713 unsigned prev;
1714
1715 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1716 if (prog->_LinkedShaders[prev] != NULL)
1717 break;
1718 }
1719
Bryan Cainf18a0862011-04-23 19:29:15 -05001720 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07001721 * stage.
1722 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001723 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1724 if (prog->_LinkedShaders[i] == NULL)
1725 continue;
1726
Ian Romanickf36460e2010-06-23 12:07:22 -07001727 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07001728 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07001729 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07001730 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001731
1732 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07001733 }
1734
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001735 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07001736 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001737
Eric Anholt3de13952012-05-04 13:08:46 -07001738 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
1739 * it before optimization because we want most of the checks to get
1740 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07001741 *
1742 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07001743 */
Paul Berry15ba2a52012-08-02 17:51:02 -07001744 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07001745 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1746 if (sh) {
1747 lower_discard_flow(sh->ir);
1748 }
1749 }
1750
Eric Anholtf609cf72012-04-27 13:52:56 -07001751 if (!interstage_cross_validate_uniform_blocks(prog))
1752 goto done;
1753
Eric Anholt2f4fe152010-08-10 13:06:49 -07001754 /* Do common optimization before assigning storage for attributes,
1755 * uniforms, and varyings. Later optimization could possibly make
1756 * some of that unused.
1757 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001758 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1759 if (prog->_LinkedShaders[i] == NULL)
1760 continue;
1761
Ian Romanick02c5ae12011-07-11 10:46:01 -07001762 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
1763 if (!prog->LinkStatus)
1764 goto done;
1765
Paul Berry18392442012-12-04 11:11:02 -08001766 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
1767 lower_clip_distance(prog->_LinkedShaders[i]);
1768 }
Paul Berryc06e3252011-08-11 20:58:21 -07001769
Brian Paul7feabfe2012-03-20 17:43:12 -06001770 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
1771
1772 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07001773 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001774 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001775
Paul Berry50895d42012-12-05 07:17:07 -08001776 /* Mark all generic shader inputs and outputs as unpaired. */
1777 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1778 link_invalidate_variable_locations(
1779 prog->_LinkedShaders[MESA_SHADER_VERTEX],
1780 VERT_ATTRIB_GENERIC0, VERT_RESULT_VAR0);
1781 }
1782 /* FINISHME: Geometry shaders not implemented yet */
1783 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
1784 link_invalidate_variable_locations(
1785 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1786 FRAG_ATTRIB_VAR0, FRAG_RESULT_DATA0);
1787 }
1788
Ian Romanickd32d4f72011-06-27 17:59:58 -07001789 /* FINISHME: The value of the max_attribute_index parameter is
1790 * FINISHME: implementation dependent based on the value of
1791 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1792 * FINISHME: at least 16, so hardcode 16 for now.
1793 */
1794 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001795 goto done;
1796 }
1797
Dave Airlie1256a5d2012-03-24 13:33:41 +00001798 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001799 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07001800 }
1801
Ian Romanick3322fba2010-10-14 13:28:42 -07001802 unsigned prev;
1803 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1804 if (prog->_LinkedShaders[prev] != NULL)
1805 break;
1806 }
1807
Paul Berry871ddb92011-11-05 11:17:32 -07001808 if (num_tfeedback_decls != 0) {
1809 /* From GL_EXT_transform_feedback:
1810 * A program will fail to link if:
1811 *
1812 * * the <count> specified by TransformFeedbackVaryingsEXT is
1813 * non-zero, but the program object has no vertex or geometry
1814 * shader;
1815 */
1816 if (prev >= MESA_SHADER_FRAGMENT) {
1817 linker_error(prog, "Transform feedback varyings specified, but "
1818 "no vertex or geometry shader is present.");
1819 goto done;
1820 }
1821
1822 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
1823 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08001824 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07001825 prog->TransformFeedback.VaryingNames,
1826 tfeedback_decls))
1827 goto done;
1828 }
1829
Ian Romanick3322fba2010-10-14 13:28:42 -07001830 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1831 if (prog->_LinkedShaders[i] == NULL)
1832 continue;
1833
Paul Berry871ddb92011-11-05 11:17:32 -07001834 if (!assign_varying_locations(
Dave Airlie7d7a5492012-12-15 13:24:52 +10001835 ctx, mem_ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
Paul Berry871ddb92011-11-05 11:17:32 -07001836 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
1837 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07001838 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07001839
Ian Romanick3322fba2010-10-14 13:28:42 -07001840 prev = i;
1841 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001842
Paul Berry871ddb92011-11-05 11:17:32 -07001843 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
1844 /* There was no fragment shader, but we still have to assign varying
1845 * locations for use by transform feedback.
1846 */
1847 if (!assign_varying_locations(
Dave Airlie7d7a5492012-12-15 13:24:52 +10001848 ctx, mem_ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07001849 tfeedback_decls))
1850 goto done;
1851 }
1852
1853 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
1854 goto done;
1855
Ian Romanickcc90e622010-10-19 17:59:10 -07001856 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1857 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
Paul Berry42a29d82013-01-11 14:39:32 -08001858 ir_var_shader_out);
Ian Romanick960d7222011-10-21 11:21:02 -07001859
1860 /* Eliminate code that is now dead due to unused vertex outputs being
1861 * demoted.
1862 */
1863 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
1864 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07001865 }
1866
1867 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
1868 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
1869
Paul Berry42a29d82013-01-11 14:39:32 -08001870 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
1871 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
Ian Romanick960d7222011-10-21 11:21:02 -07001872
1873 /* Eliminate code that is now dead due to unused geometry outputs being
1874 * demoted.
1875 */
1876 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
1877 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07001878 }
1879
1880 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
1881 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1882
Paul Berry42a29d82013-01-11 14:39:32 -08001883 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
Ian Romanick960d7222011-10-21 11:21:02 -07001884
1885 /* Eliminate code that is now dead due to unused fragment inputs being
1886 * demoted. This shouldn't actually do anything other than remove
1887 * declarations of the (now unused) global variables.
1888 */
1889 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
1890 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07001891 }
1892
Ian Romanick960d7222011-10-21 11:21:02 -07001893 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07001894 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01001895 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07001896
Ian Romanick92f81592011-11-08 12:37:19 -08001897 if (!check_resources(ctx, prog))
1898 goto done;
1899
Ian Romanickce9171f2011-02-03 17:10:14 -08001900 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07001901 * present in a linked program. By checking prog->IsES, we also
1902 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08001903 */
Eric Anholt57f79782011-07-22 12:57:47 -07001904 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07001905 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08001906 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001907 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08001908 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001909 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08001910 }
1911 }
1912
Ian Romanick13e10e42010-06-21 12:03:24 -07001913 /* FINISHME: Assign fragment shader output locations. */
1914
Ian Romanick832dfa52010-06-17 15:04:20 -07001915done:
1916 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001917
1918 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1919 if (prog->_LinkedShaders[i] == NULL)
1920 continue;
1921
1922 /* Retain any live IR, but trash the rest. */
1923 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07001924
1925 /* The symbol table in the linked shaders may contain references to
1926 * variables that were removed (e.g., unused uniforms). Since it may
1927 * contain junk, there is no possible valid use. Delete it and set the
1928 * pointer to NULL.
1929 */
1930 delete prog->_LinkedShaders[i]->symbols;
1931 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001932 }
1933
Kenneth Graunked3073f52011-01-21 14:32:31 -08001934 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07001935}