blob: 49bb142a80a6343a73a6b4e7c91d0f46ffdb60a2 [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"
Eric Anholtfaf3dba2013-06-12 16:57:11 -070069#include "glsl_parser_extras.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070070#include "ir.h"
71#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030072#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070073#include "linker.h"
Paul Berry4b11b572012-12-17 14:20:35 -080074#include "link_varyings.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070075#include "ir_optimization.h"
Bryan Cain25480922013-02-15 09:46:50 -060076#include "ir_rvalue_visitor.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070077
Ian Romanick3322fba2010-10-14 13:28:42 -070078extern "C" {
79#include "main/shaderobj.h"
Eric Anholt6065a872013-06-12 18:12:40 -070080#include "main/enums.h"
Ian Romanick3322fba2010-10-14 13:28:42 -070081}
82
Bryan Cain25480922013-02-15 09:46:50 -060083void linker_error(gl_shader_program *, const char *, ...);
84
Eric Anholt10ef9492013-09-20 11:03:44 -070085namespace {
86
Ian Romanick832dfa52010-06-17 15:04:20 -070087/**
88 * Visitor that determines whether or not a variable is ever written.
89 */
90class find_assignment_visitor : public ir_hierarchical_visitor {
91public:
92 find_assignment_visitor(const char *name)
93 : name(name), found(false)
94 {
95 /* empty */
96 }
97
98 virtual ir_visitor_status visit_enter(ir_assignment *ir)
99 {
100 ir_variable *const var = ir->lhs->variable_referenced();
101
102 if (strcmp(name, var->name) == 0) {
103 found = true;
104 return visit_stop;
105 }
106
107 return visit_continue_with_parent;
108 }
109
Eric Anholt18a60232010-08-23 11:29:25 -0700110 virtual ir_visitor_status visit_enter(ir_call *ir)
111 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700112 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700113 foreach_iter(exec_list_iterator, iter, *ir) {
114 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
115 ir_variable *sig_param = (ir_variable *)sig_iter.get();
116
Paul Berry42a29d82013-01-11 14:39:32 -0800117 if (sig_param->mode == ir_var_function_out ||
118 sig_param->mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700119 ir_variable *var = param_rval->variable_referenced();
120 if (var && strcmp(name, var->name) == 0) {
121 found = true;
122 return visit_stop;
123 }
124 }
125 sig_iter.next();
126 }
127
Kenneth Graunked884f602012-03-20 15:56:37 -0700128 if (ir->return_deref != NULL) {
129 ir_variable *const var = ir->return_deref->variable_referenced();
130
131 if (strcmp(name, var->name) == 0) {
132 found = true;
133 return visit_stop;
134 }
135 }
136
Eric Anholt18a60232010-08-23 11:29:25 -0700137 return visit_continue_with_parent;
138 }
139
Ian Romanick832dfa52010-06-17 15:04:20 -0700140 bool variable_found()
141 {
142 return found;
143 }
144
145private:
146 const char *name; /**< Find writes to a variable with this name. */
147 bool found; /**< Was a write to the variable found? */
148};
149
Ian Romanickc93b8f12010-06-17 15:20:22 -0700150
Ian Romanickc33e78f2010-08-13 12:30:41 -0700151/**
152 * Visitor that determines whether or not a variable is ever read.
153 */
154class find_deref_visitor : public ir_hierarchical_visitor {
155public:
156 find_deref_visitor(const char *name)
157 : name(name), found(false)
158 {
159 /* empty */
160 }
161
162 virtual ir_visitor_status visit(ir_dereference_variable *ir)
163 {
164 if (strcmp(this->name, ir->var->name) == 0) {
165 this->found = true;
166 return visit_stop;
167 }
168
169 return visit_continue;
170 }
171
172 bool variable_found() const
173 {
174 return this->found;
175 }
176
177private:
178 const char *name; /**< Find writes to a variable with this name. */
179 bool found; /**< Was a write to the variable found? */
180};
181
182
Paul Berry7cfefe62013-07-30 21:13:48 -0700183class geom_array_resize_visitor : public ir_hierarchical_visitor {
184public:
185 unsigned num_vertices;
186 gl_shader_program *prog;
187
188 geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
189 {
190 this->num_vertices = num_vertices;
191 this->prog = prog;
192 }
193
194 virtual ~geom_array_resize_visitor()
195 {
196 /* empty */
197 }
198
199 virtual ir_visitor_status visit(ir_variable *var)
200 {
201 if (!var->type->is_array() || var->mode != ir_var_shader_in)
202 return visit_continue;
203
204 unsigned size = var->type->length;
205
206 /* Generate a link error if the shader has declared this array with an
207 * incorrect size.
208 */
209 if (size && size != this->num_vertices) {
210 linker_error(this->prog, "size of array %s declared as %u, "
211 "but number of input vertices is %u\n",
212 var->name, size, this->num_vertices);
213 return visit_continue;
214 }
215
216 /* Generate a link error if the shader attempts to access an input
217 * array using an index too large for its actual size assigned at link
218 * time.
219 */
220 if (var->max_array_access >= this->num_vertices) {
221 linker_error(this->prog, "geometry shader accesses element %i of "
222 "%s, but only %i input vertices\n",
223 var->max_array_access, var->name, this->num_vertices);
224 return visit_continue;
225 }
226
227 var->type = glsl_type::get_array_instance(var->type->element_type(),
228 this->num_vertices);
229 var->max_array_access = this->num_vertices - 1;
230
231 return visit_continue;
232 }
233
234 /* Dereferences of input variables need to be updated so that their type
235 * matches the newly assigned type of the variable they are accessing. */
236 virtual ir_visitor_status visit(ir_dereference_variable *ir)
237 {
238 ir->type = ir->var->type;
239 return visit_continue;
240 }
241
242 /* Dereferences of 2D input arrays need to be updated so that their type
243 * matches the newly assigned type of the array they are accessing. */
244 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
245 {
246 const glsl_type *const vt = ir->array->type;
247 if (vt->is_array())
248 ir->type = vt->element_type();
249 return visit_continue;
250 }
251};
252
253
Paul Berry1a33e022013-08-18 20:59:37 -0700254/**
255 * Visitor that determines whether or not a shader uses ir_end_primitive.
256 */
257class find_end_primitive_visitor : public ir_hierarchical_visitor {
258public:
259 find_end_primitive_visitor()
260 : found(false)
261 {
262 /* empty */
263 }
264
265 virtual ir_visitor_status visit(ir_end_primitive *)
266 {
267 found = true;
268 return visit_stop;
269 }
270
271 bool end_primitive_found()
272 {
273 return found;
274 }
275
276private:
277 bool found;
278};
279
Eric Anholt10ef9492013-09-20 11:03:44 -0700280} /* anonymous namespace */
Paul Berry1a33e022013-08-18 20:59:37 -0700281
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700282void
Ian Romanick586e7412011-07-28 14:04:09 -0700283linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700284{
285 va_list ap;
286
Kenneth Graunked3073f52011-01-21 14:32:31 -0800287 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700288 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800289 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700290 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700291
292 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700293}
294
295
296void
Ian Romanick379a32f2011-07-28 14:09:06 -0700297linker_warning(gl_shader_program *prog, const char *fmt, ...)
298{
299 va_list ap;
300
301 ralloc_strcat(&prog->InfoLog, "error: ");
302 va_start(ap, fmt);
303 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
304 va_end(ap);
305
306}
307
308
Paul Berryb92900d2013-01-28 14:21:59 -0800309/**
310 * Given a string identifying a program resource, break it into a base name
311 * and an optional array index in square brackets.
312 *
313 * If an array index is present, \c out_base_name_end is set to point to the
314 * "[" that precedes the array index, and the array index itself is returned
315 * as a long.
316 *
317 * If no array index is present (or if the array index is negative or
318 * mal-formed), \c out_base_name_end, is set to point to the null terminator
319 * at the end of the input string, and -1 is returned.
320 *
321 * Only the final array index is parsed; if the string contains other array
322 * indices (or structure field accesses), they are left in the base name.
323 *
324 * No attempt is made to check that the base name is properly formed;
325 * typically the caller will look up the base name in a hash table, so
326 * ill-formed base names simply turn into hash table lookup failures.
327 */
328long
329parse_program_resource_name(const GLchar *name,
330 const GLchar **out_base_name_end)
331{
332 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
333 *
334 * "When an integer array element or block instance number is part of
335 * the name string, it will be specified in decimal form without a "+"
336 * or "-" sign or any extra leading zeroes. Additionally, the name
337 * string will not include white space anywhere in the string."
338 */
339
340 const size_t len = strlen(name);
341 *out_base_name_end = name + len;
342
343 if (len == 0 || name[len-1] != ']')
344 return -1;
345
346 /* Walk backwards over the string looking for a non-digit character. This
347 * had better be the opening bracket for an array index.
348 *
349 * Initially, i specifies the location of the ']'. Since the string may
350 * contain only the ']' charcater, walk backwards very carefully.
351 */
352 unsigned i;
353 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
354 /* empty */ ;
355
356 if ((i == 0) || name[i-1] != '[')
357 return -1;
358
359 long array_index = strtol(&name[i], NULL, 10);
360 if (array_index < 0)
361 return -1;
362
363 *out_base_name_end = name + (i - 1);
364 return array_index;
365}
366
367
Ian Romanick379a32f2011-07-28 14:09:06 -0700368void
Ian Romanick63974c02013-10-04 10:46:29 -0700369link_invalidate_variable_locations(exec_list *ir)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700370{
Ian Romanickcf8b14c2013-10-22 15:07:00 -0700371 foreach_list(node, ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700372 ir_variable *const var = ((ir_instruction *) node)->as_variable();
373
Paul Berry50895d42012-12-05 07:17:07 -0800374 if (var == NULL)
375 continue;
376
Ian Romanick63974c02013-10-04 10:46:29 -0700377 /* Only assign locations for variables that lack an explicit location.
378 * Explicit locations are set for all built-in variables, generic vertex
379 * shader inputs (via layout(location=...)), and generic fragment shader
380 * outputs (also via layout(location=...)).
381 */
382 if (!var->explicit_location) {
383 var->location = -1;
384 var->location_frac = 0;
Paul Berry50895d42012-12-05 07:17:07 -0800385 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700386
Ian Romanick63974c02013-10-04 10:46:29 -0700387 /* ir_variable::is_unmatched_generic_inout is used by the linker while
388 * connecting outputs from one stage to inputs of the next stage.
389 *
390 * There are two implicit assumptions here. First, we assume that any
391 * built-in variable (i.e., non-generic in or out) will have
392 * explicit_location set. Second, we assume that any generic in or out
393 * will not have explicit_location set.
394 *
395 * This second assumption will only be valid until
396 * GL_ARB_separate_shader_objects is supported. When that extension is
397 * implemented, this function will need some modifications.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700398 */
Ian Romanick63974c02013-10-04 10:46:29 -0700399 if (!var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800400 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800401 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800402 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800403 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700404 }
405}
406
407
Ian Romanickc93b8f12010-06-17 15:20:22 -0700408/**
Paul Berry44e07de2013-06-11 14:11:05 -0700409 * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
410 *
411 * Also check for errors based on incorrect usage of gl_ClipVertex and
412 * gl_ClipDistance.
413 *
414 * Return false if an error was reported.
415 */
416static void
417analyze_clip_usage(const char *shader_type, struct gl_shader_program *prog,
418 struct gl_shader *shader, GLboolean *UsesClipDistance,
419 GLuint *ClipDistanceArraySize)
420{
421 *ClipDistanceArraySize = 0;
422
423 if (!prog->IsES && prog->Version >= 130) {
424 /* From section 7.1 (Vertex Shader Special Variables) of the
425 * GLSL 1.30 spec:
426 *
427 * "It is an error for a shader to statically write both
428 * gl_ClipVertex and gl_ClipDistance."
429 *
430 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
431 * gl_ClipVertex nor gl_ClipDistance.
432 */
433 find_assignment_visitor clip_vertex("gl_ClipVertex");
434 find_assignment_visitor clip_distance("gl_ClipDistance");
435
436 clip_vertex.run(shader->ir);
437 clip_distance.run(shader->ir);
438 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
439 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
440 "and `gl_ClipDistance'\n", shader_type);
441 return;
442 }
443 *UsesClipDistance = clip_distance.variable_found();
444 ir_variable *clip_distance_var =
445 shader->symbols->get_variable("gl_ClipDistance");
446 if (clip_distance_var)
447 *ClipDistanceArraySize = clip_distance_var->type->length;
448 } else {
449 *UsesClipDistance = false;
450 }
451}
452
453
454/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700455 * Verify that a vertex shader executable meets all semantic requirements.
456 *
Paul Berry642e5b412012-01-04 13:57:52 -0800457 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
458 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700459 *
460 * \param shader Vertex shader executable to be verified
461 */
Paul Berryb95d2372013-07-27 11:08:31 -0700462void
Eric Anholt849e1812010-06-30 11:49:17 -0700463validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700464 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700465{
466 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700467 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700468
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700469 /* From the GLSL 1.10 spec, page 48:
470 *
471 * "The variable gl_Position is available only in the vertex
472 * language and is intended for writing the homogeneous vertex
473 * position. All executions of a well-formed vertex shader
474 * executable must write a value into this variable. [...] The
475 * variable gl_Position is available only in the vertex
476 * language and is intended for writing the homogeneous vertex
477 * position. All executions of a well-formed vertex shader
478 * executable must write a value into this variable."
479 *
480 * while in GLSL 1.40 this text is changed to:
481 *
482 * "The variable gl_Position is available only in the vertex
483 * language and is intended for writing the homogeneous vertex
484 * position. It can be written at any time during shader
485 * execution. It may also be read back by a vertex shader
486 * after being written. This value will be used by primitive
487 * assembly, clipping, culling, and other fixed functionality
488 * operations, if present, that operate on primitives after
489 * vertex processing has occurred. Its value is undefined if
490 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700491 *
492 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
493 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700494 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700495 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700496 find_assignment_visitor find("gl_Position");
497 find.run(shader->ir);
498 if (!find.variable_found()) {
499 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700500 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700501 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700502 }
503
Paul Berry44e07de2013-06-11 14:11:05 -0700504 analyze_clip_usage("vertex", prog, shader, &prog->Vert.UsesClipDistance,
505 &prog->Vert.ClipDistanceArraySize);
Ian Romanick832dfa52010-06-17 15:04:20 -0700506}
507
508
Ian Romanickc93b8f12010-06-17 15:20:22 -0700509/**
510 * Verify that a fragment shader executable meets all semantic requirements
511 *
512 * \param shader Fragment shader executable to be verified
513 */
Paul Berryb95d2372013-07-27 11:08:31 -0700514void
Eric Anholt849e1812010-06-30 11:49:17 -0700515validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700516 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700517{
518 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700519 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700520
Ian Romanick832dfa52010-06-17 15:04:20 -0700521 find_assignment_visitor frag_color("gl_FragColor");
522 find_assignment_visitor frag_data("gl_FragData");
523
Eric Anholt16b68b12010-06-30 11:05:43 -0700524 frag_color.run(shader->ir);
525 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700526
Ian Romanick832dfa52010-06-17 15:04:20 -0700527 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700528 linker_error(prog, "fragment shader writes to both "
529 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700530 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700531}
532
Bryan Cain25480922013-02-15 09:46:50 -0600533/**
534 * Verify that a geometry shader executable meets all semantic requirements
535 *
Paul Berry44e07de2013-06-11 14:11:05 -0700536 * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
537 * prog->Geom.ClipDistanceArraySize as a side effect.
Bryan Cain25480922013-02-15 09:46:50 -0600538 *
539 * \param shader Geometry shader executable to be verified
540 */
541void
542validate_geometry_shader_executable(struct gl_shader_program *prog,
543 struct gl_shader *shader)
544{
545 if (shader == NULL)
546 return;
547
548 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
549 prog->Geom.VerticesIn = num_vertices;
Paul Berry44e07de2013-06-11 14:11:05 -0700550
551 analyze_clip_usage("geometry", prog, shader, &prog->Geom.UsesClipDistance,
552 &prog->Geom.ClipDistanceArraySize);
Paul Berry1a33e022013-08-18 20:59:37 -0700553
554 find_end_primitive_visitor end_primitive;
555 end_primitive.run(shader->ir);
556 prog->Geom.UsesEndPrimitive = end_primitive.end_primitive_found();
Bryan Cain25480922013-02-15 09:46:50 -0600557}
558
Ian Romanick832dfa52010-06-17 15:04:20 -0700559
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700560/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700561 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700562 */
Paul Berryb95d2372013-07-27 11:08:31 -0700563void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700564cross_validate_globals(struct gl_shader_program *prog,
565 struct gl_shader **shader_list,
566 unsigned num_shaders,
567 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700568{
569 /* Examine all of the uniforms in all of the shaders and cross validate
570 * them.
571 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700572 glsl_symbol_table variables;
573 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700574 if (shader_list[i] == NULL)
575 continue;
576
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700577 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700578 ir_variable *const var = ((ir_instruction *) node)->as_variable();
579
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700580 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700581 continue;
582
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700583 if (uniforms_only && (var->mode != ir_var_uniform))
584 continue;
585
Ian Romanick7e2aa912010-07-19 17:12:42 -0700586 /* Don't cross validate temporaries that are at global scope. These
587 * will eventually get pulled into the shaders 'main'.
588 */
589 if (var->mode == ir_var_temporary)
590 continue;
591
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700592 /* If a global with this name has already been seen, verify that the
593 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700594 * initializers, the values of the initializers must be the same.
595 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700596 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700597 if (existing != NULL) {
598 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700599 /* Consider the types to be "the same" if both types are arrays
600 * of the same type and one of the arrays is implicitly sized.
601 * In addition, set the type of the linked variable to the
602 * explicitly sized array.
603 */
604 if (var->type->is_array()
605 && existing->type->is_array()
606 && (var->type->fields.array == existing->type->fields.array)
607 && ((var->type->length == 0)
608 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800609 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700610 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800611 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700612 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700613 linker_error(prog, "%s `%s' declared as type "
614 "`%s' and type `%s'\n",
615 mode_string(var),
616 var->name, var->type->name,
617 existing->type->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700618 return;
Ian Romanicka2711d62010-08-29 22:07:49 -0700619 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700620 }
621
Ian Romanick68a4fc92010-10-07 17:21:22 -0700622 if (var->explicit_location) {
623 if (existing->explicit_location
624 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700625 linker_error(prog, "explicit locations for %s "
626 "`%s' have differing values\n",
627 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700628 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700629 }
630
631 existing->location = var->location;
632 existing->explicit_location = true;
633 }
634
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700635 /* From the GLSL 4.20 specification:
636 * "A link error will result if two compilation units in a program
637 * specify different integer-constant bindings for the same
638 * opaque-uniform name. However, it is not an error to specify a
639 * binding on some but not all declarations for the same name"
640 */
641 if (var->explicit_binding) {
642 if (existing->explicit_binding &&
643 var->binding != existing->binding) {
644 linker_error(prog, "explicit bindings for %s "
645 "`%s' have differing values\n",
646 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700647 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700648 }
649
650 existing->binding = var->binding;
651 existing->explicit_binding = true;
652 }
653
Ian Romanick46173f92011-10-31 13:07:06 -0700654 /* Validate layout qualifiers for gl_FragDepth.
655 *
656 * From the AMD/ARB_conservative_depth specs:
657 *
658 * "If gl_FragDepth is redeclared in any fragment shader in a
659 * program, it must be redeclared in all fragment shaders in
660 * that program that have static assignments to
661 * gl_FragDepth. All redeclarations of gl_FragDepth in all
662 * fragment shaders in a single program must have the same set
663 * of qualifiers."
664 */
665 if (strcmp(var->name, "gl_FragDepth") == 0) {
666 bool layout_declared = var->depth_layout != ir_depth_layout_none;
667 bool layout_differs =
668 var->depth_layout != existing->depth_layout;
669
670 if (layout_declared && layout_differs) {
671 linker_error(prog,
672 "All redeclarations of gl_FragDepth in all "
673 "fragment shaders in a single program must have "
674 "the same set of qualifiers.");
675 }
676
677 if (var->used && layout_differs) {
678 linker_error(prog,
679 "If gl_FragDepth is redeclared with a layout "
680 "qualifier in any fragment shader, it must be "
681 "redeclared with the same layout qualifier in "
682 "all fragment shaders that have assignments to "
683 "gl_FragDepth");
684 }
685 }
Chad Versaceaddae332011-01-27 01:40:31 -0800686
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700687 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
688 *
689 * "If a shared global has multiple initializers, the
690 * initializers must all be constant expressions, and they
691 * must all have the same value. Otherwise, a link error will
692 * result. (A shared global having only one initializer does
693 * not require that initializer to be a constant expression.)"
694 *
695 * Previous to 4.20 the GLSL spec simply said that initializers
696 * must have the same value. In this case of non-constant
697 * initializers, this was impossible to determine. As a result,
698 * no vendor actually implemented that behavior. The 4.20
699 * behavior matches the implemented behavior of at least one other
700 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700701 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700702 if (var->constant_initializer != NULL) {
703 if (existing->constant_initializer != NULL) {
704 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700705 linker_error(prog, "initializers for %s "
706 "`%s' have differing values\n",
707 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700708 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700709 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700710 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700711 /* If the first-seen instance of a particular uniform did not
712 * have an initializer but a later instance does, copy the
713 * initializer to the version stored in the symbol table.
714 */
Ian Romanickde415b72010-07-14 13:22:12 -0700715 /* FINISHME: This is wrong. The constant_value field should
716 * FINISHME: not be modified! Imagine a case where a shader
717 * FINISHME: without an initializer is linked in two different
718 * FINISHME: programs with shaders that have differing
719 * FINISHME: initializers. Linking with the first will
720 * FINISHME: modify the shader, and linking with the second
721 * FINISHME: will fail.
722 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700723 existing->constant_initializer =
724 var->constant_initializer->clone(ralloc_parent(existing),
725 NULL);
726 }
727 }
728
729 if (var->has_initializer) {
730 if (existing->has_initializer
731 && (var->constant_initializer == NULL
732 || existing->constant_initializer == NULL)) {
733 linker_error(prog,
734 "shared global variable `%s' has multiple "
735 "non-constant initializers.\n",
736 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700737 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700738 }
739
740 /* Some instance had an initializer, so keep track of that. In
741 * this location, all sorts of initializers (constant or
742 * otherwise) will propagate the existence to the variable
743 * stored in the symbol table.
744 */
745 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700746 }
Chad Versace7528f142010-11-17 14:34:38 -0800747
748 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700749 linker_error(prog, "declarations for %s `%s' have "
750 "mismatching invariant qualifiers\n",
751 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700752 return;
Chad Versace7528f142010-11-17 14:34:38 -0800753 }
Chad Versace61428dd2011-01-10 15:29:30 -0800754 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700755 linker_error(prog, "declarations for %s `%s' have "
756 "mismatching centroid qualifiers\n",
757 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700758 return;
Chad Versace61428dd2011-01-10 15:29:30 -0800759 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700760 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700761 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700762 }
763 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700764}
765
766
Ian Romanick37101922010-06-18 19:02:10 -0700767/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700768 * Perform validation of uniforms used across multiple shader stages
769 */
Paul Berryb95d2372013-07-27 11:08:31 -0700770void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700771cross_validate_uniforms(struct gl_shader_program *prog)
772{
Paul Berryb95d2372013-07-27 11:08:31 -0700773 cross_validate_globals(prog, prog->_LinkedShaders,
774 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700775}
776
Eric Anholtf609cf72012-04-27 13:52:56 -0700777/**
778 * Accumulates the array of prog->UniformBlocks and checks that all
779 * definitons of blocks agree on their contents.
780 */
781static bool
782interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
783{
784 unsigned max_num_uniform_blocks = 0;
785 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
786 if (prog->_LinkedShaders[i])
787 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
788 }
789
790 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
791 struct gl_shader *sh = prog->_LinkedShaders[i];
792
793 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
794 max_num_uniform_blocks);
795 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
796 prog->UniformBlockStageIndex[i][j] = -1;
797
798 if (sh == NULL)
799 continue;
800
801 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
802 int index = link_cross_validate_uniform_block(prog,
803 &prog->UniformBlocks,
804 &prog->NumUniformBlocks,
805 &sh->UniformBlocks[j]);
806
807 if (index == -1) {
808 linker_error(prog, "uniform block `%s' has mismatching definitions",
809 sh->UniformBlocks[j].Name);
810 return false;
811 }
812
813 prog->UniformBlockStageIndex[i][index] = j;
814 }
815 }
816
817 return true;
818}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700819
Ian Romanick37101922010-06-18 19:02:10 -0700820
Ian Romanick3fb87872010-07-09 14:09:34 -0700821/**
822 * Populates a shaders symbol table with all global declarations
823 */
824static void
825populate_symbol_table(gl_shader *sh)
826{
827 sh->symbols = new(sh) glsl_symbol_table;
828
829 foreach_list(node, sh->ir) {
830 ir_instruction *const inst = (ir_instruction *) node;
831 ir_variable *var;
832 ir_function *func;
833
834 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700835 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700836 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700837 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700838 }
839 }
840}
841
842
843/**
Ian Romanick31a97862010-07-12 18:48:50 -0700844 * Remap variables referenced in an instruction tree
845 *
846 * This is used when instruction trees are cloned from one shader and placed in
847 * another. These trees will contain references to \c ir_variable nodes that
848 * do not exist in the target shader. This function finds these \c ir_variable
849 * references and replaces the references with matching variables in the target
850 * shader.
851 *
852 * If there is no matching variable in the target shader, a clone of the
853 * \c ir_variable is made and added to the target shader. The new variable is
854 * added to \b both the instruction stream and the symbol table.
855 *
856 * \param inst IR tree that is to be processed.
857 * \param symbols Symbol table containing global scope symbols in the
858 * linked shader.
859 * \param instructions Instruction stream where new variable declarations
860 * should be added.
861 */
862void
Eric Anholt8273bd42010-08-04 12:34:56 -0700863remap_variables(ir_instruction *inst, struct gl_shader *target,
864 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700865{
866 class remap_visitor : public ir_hierarchical_visitor {
867 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700868 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700869 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700870 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700871 this->target = target;
872 this->symbols = target->symbols;
873 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700874 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700875 }
876
877 virtual ir_visitor_status visit(ir_dereference_variable *ir)
878 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700879 if (ir->var->mode == ir_var_temporary) {
880 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
881
882 assert(var != NULL);
883 ir->var = var;
884 return visit_continue;
885 }
886
Ian Romanick31a97862010-07-12 18:48:50 -0700887 ir_variable *const existing =
888 this->symbols->get_variable(ir->var->name);
889 if (existing != NULL)
890 ir->var = existing;
891 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700892 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700893
Eric Anholt001eee52010-11-05 06:11:24 -0700894 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700895 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700896 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700897 }
898
899 return visit_continue;
900 }
901
902 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700903 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700904 glsl_symbol_table *symbols;
905 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700906 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700907 };
908
Eric Anholt8273bd42010-08-04 12:34:56 -0700909 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700910
911 inst->accept(&v);
912}
913
914
915/**
916 * Move non-declarations from one instruction stream to another
917 *
918 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700919 * 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 -0700920 * pointer) for \c last and \c false for \c make_copies on the first
921 * call. Successive calls pass the return value of the previous call for
922 * \c last and \c true for \c make_copies.
923 *
924 * \param instructions Source instruction stream
925 * \param last Instruction after which new instructions should be
926 * inserted in the target instruction stream
927 * \param make_copies Flag selecting whether instructions in \c instructions
928 * should be copied (via \c ir_instruction::clone) into the
929 * target list or moved.
930 *
931 * \return
932 * The new "last" instruction in the target instruction stream. This pointer
933 * is suitable for use as the \c last parameter of a later call to this
934 * function.
935 */
936exec_node *
937move_non_declarations(exec_list *instructions, exec_node *last,
938 bool make_copies, gl_shader *target)
939{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700940 hash_table *temps = NULL;
941
942 if (make_copies)
943 temps = hash_table_ctor(0, hash_table_pointer_hash,
944 hash_table_pointer_compare);
945
Ian Romanick303c99f2010-07-19 12:34:56 -0700946 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700947 ir_instruction *inst = (ir_instruction *) node;
948
Ian Romanick7e2aa912010-07-19 17:12:42 -0700949 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700950 continue;
951
Ian Romanick7e2aa912010-07-19 17:12:42 -0700952 ir_variable *var = inst->as_variable();
953 if ((var != NULL) && (var->mode != ir_var_temporary))
954 continue;
955
956 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700957 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700958 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700959 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700960
961 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700962 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700963
964 if (var != NULL)
965 hash_table_insert(temps, inst, var);
966 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700967 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700968 } else {
969 inst->remove();
970 }
971
972 last->insert_after(inst);
973 last = inst;
974 }
975
Ian Romanick7e2aa912010-07-19 17:12:42 -0700976 if (make_copies)
977 hash_table_dtor(temps);
978
Ian Romanick31a97862010-07-12 18:48:50 -0700979 return last;
980}
981
982/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700983 * Get the function signature for main from a shader
984 */
985static ir_function_signature *
986get_main_function_signature(gl_shader *sh)
987{
988 ir_function *const f = sh->symbols->get_function("main");
989 if (f != NULL) {
990 exec_list void_parameters;
991
992 /* Look for the 'void main()' signature and ensure that it's defined.
993 * This keeps the linker from accidentally pick a shader that just
994 * contains a prototype for main.
995 *
996 * We don't have to check for multiple definitions of main (in multiple
997 * shaders) because that would have already been caught above.
998 */
Kenneth Graunke3e820e32013-08-30 23:11:55 -0700999 ir_function_signature *sig = f->matching_signature(NULL, &void_parameters);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001000 if ((sig != NULL) && sig->is_defined) {
1001 return sig;
1002 }
1003 }
1004
1005 return NULL;
1006}
1007
1008
1009/**
Brian Paul84a12732012-02-02 20:10:40 -07001010 * This class is only used in link_intrastage_shaders() below but declaring
1011 * it inside that function leads to compiler warnings with some versions of
1012 * gcc.
1013 */
1014class array_sizing_visitor : public ir_hierarchical_visitor {
1015public:
Paul Berry15e05b92013-09-25 14:07:37 -07001016 array_sizing_visitor()
1017 : mem_ctx(ralloc_context(NULL)),
1018 unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash,
1019 hash_table_pointer_compare))
1020 {
1021 }
1022
1023 ~array_sizing_visitor()
1024 {
1025 hash_table_dtor(this->unnamed_interfaces);
1026 ralloc_free(this->mem_ctx);
1027 }
1028
Brian Paul84a12732012-02-02 20:10:40 -07001029 virtual ir_visitor_status visit(ir_variable *var)
1030 {
Paul Berrye2266692013-09-23 10:44:19 -07001031 fixup_type(&var->type, var->max_array_access);
1032 if (var->type->is_interface()) {
1033 if (interface_contains_unsized_arrays(var->type)) {
1034 const glsl_type *new_type =
1035 resize_interface_members(var->type, var->max_ifc_array_access);
1036 var->type = new_type;
1037 var->change_interface_type(new_type);
1038 }
1039 } else if (var->type->is_array() &&
1040 var->type->fields.array->is_interface()) {
1041 if (interface_contains_unsized_arrays(var->type->fields.array)) {
1042 const glsl_type *new_type =
1043 resize_interface_members(var->type->fields.array,
1044 var->max_ifc_array_access);
1045 var->change_interface_type(new_type);
1046 var->type =
1047 glsl_type::get_array_instance(new_type, var->type->length);
1048 }
Paul Berry15e05b92013-09-25 14:07:37 -07001049 } else if (const glsl_type *ifc_type = var->get_interface_type()) {
1050 /* Store a pointer to the variable in the unnamed_interfaces
1051 * hashtable.
1052 */
1053 ir_variable **interface_vars = (ir_variable **)
1054 hash_table_find(this->unnamed_interfaces, ifc_type);
1055 if (interface_vars == NULL) {
1056 interface_vars = rzalloc_array(mem_ctx, ir_variable *,
1057 ifc_type->length);
1058 hash_table_insert(this->unnamed_interfaces, interface_vars,
1059 ifc_type);
1060 }
1061 unsigned index = ifc_type->field_index(var->name);
1062 assert(index < ifc_type->length);
1063 assert(interface_vars[index] == NULL);
1064 interface_vars[index] = var;
Brian Paul84a12732012-02-02 20:10:40 -07001065 }
1066 return visit_continue;
1067 }
Paul Berrye2266692013-09-23 10:44:19 -07001068
Paul Berry15e05b92013-09-25 14:07:37 -07001069 /**
1070 * For each unnamed interface block that was discovered while running the
1071 * visitor, adjust the interface type to reflect the newly assigned array
1072 * sizes, and fix up the ir_variable nodes to point to the new interface
1073 * type.
1074 */
1075 void fixup_unnamed_interface_types()
1076 {
1077 hash_table_call_foreach(this->unnamed_interfaces,
1078 fixup_unnamed_interface_type, NULL);
1079 }
1080
Paul Berrye2266692013-09-23 10:44:19 -07001081private:
1082 /**
1083 * If the type pointed to by \c type represents an unsized array, replace
1084 * it with a sized array whose size is determined by max_array_access.
1085 */
1086 static void fixup_type(const glsl_type **type, unsigned max_array_access)
1087 {
Timothy Arcerib59c5922013-10-23 21:31:27 +11001088 if ((*type)->is_unsized_array()) {
Paul Berrye2266692013-09-23 10:44:19 -07001089 *type = glsl_type::get_array_instance((*type)->fields.array,
1090 max_array_access + 1);
1091 assert(*type != NULL);
1092 }
1093 }
1094
1095 /**
1096 * Determine whether the given interface type contains unsized arrays (if
1097 * it doesn't, array_sizing_visitor doesn't need to process it).
1098 */
1099 static bool interface_contains_unsized_arrays(const glsl_type *type)
1100 {
1101 for (unsigned i = 0; i < type->length; i++) {
1102 const glsl_type *elem_type = type->fields.structure[i].type;
Timothy Arcerib59c5922013-10-23 21:31:27 +11001103 if (elem_type->is_unsized_array())
Paul Berrye2266692013-09-23 10:44:19 -07001104 return true;
1105 }
1106 return false;
1107 }
1108
1109 /**
1110 * Create a new interface type based on the given type, with unsized arrays
1111 * replaced by sized arrays whose size is determined by
1112 * max_ifc_array_access.
1113 */
1114 static const glsl_type *
1115 resize_interface_members(const glsl_type *type,
1116 const unsigned *max_ifc_array_access)
1117 {
1118 unsigned num_fields = type->length;
1119 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1120 memcpy(fields, type->fields.structure,
1121 num_fields * sizeof(*fields));
1122 for (unsigned i = 0; i < num_fields; i++) {
1123 fixup_type(&fields[i].type, max_ifc_array_access[i]);
1124 }
1125 glsl_interface_packing packing =
1126 (glsl_interface_packing) type->interface_packing;
1127 const glsl_type *new_ifc_type =
1128 glsl_type::get_interface_instance(fields, num_fields,
1129 packing, type->name);
1130 delete [] fields;
1131 return new_ifc_type;
1132 }
Paul Berry15e05b92013-09-25 14:07:37 -07001133
1134 static void fixup_unnamed_interface_type(const void *key, void *data,
1135 void *)
1136 {
1137 const glsl_type *ifc_type = (const glsl_type *) key;
1138 ir_variable **interface_vars = (ir_variable **) data;
1139 unsigned num_fields = ifc_type->length;
1140 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1141 memcpy(fields, ifc_type->fields.structure,
1142 num_fields * sizeof(*fields));
1143 bool interface_type_changed = false;
1144 for (unsigned i = 0; i < num_fields; i++) {
1145 if (interface_vars[i] != NULL &&
1146 fields[i].type != interface_vars[i]->type) {
1147 fields[i].type = interface_vars[i]->type;
1148 interface_type_changed = true;
1149 }
1150 }
1151 if (!interface_type_changed) {
1152 delete [] fields;
1153 return;
1154 }
1155 glsl_interface_packing packing =
1156 (glsl_interface_packing) ifc_type->interface_packing;
1157 const glsl_type *new_ifc_type =
1158 glsl_type::get_interface_instance(fields, num_fields, packing,
1159 ifc_type->name);
1160 delete [] fields;
1161 for (unsigned i = 0; i < num_fields; i++) {
1162 if (interface_vars[i] != NULL)
1163 interface_vars[i]->change_interface_type(new_ifc_type);
1164 }
1165 }
1166
1167 /**
1168 * Memory context used to allocate the data in \c unnamed_interfaces.
1169 */
1170 void *mem_ctx;
1171
1172 /**
1173 * Hash table from const glsl_type * to an array of ir_variable *'s
1174 * pointing to the ir_variables constituting each unnamed interface block.
1175 */
1176 hash_table *unnamed_interfaces;
Brian Paul84a12732012-02-02 20:10:40 -07001177};
1178
Brian Paul84a12732012-02-02 20:10:40 -07001179/**
Eric Anholt6065a872013-06-12 18:12:40 -07001180 * Performs the cross-validation of geometry shader max_vertices and
1181 * primitive type layout qualifiers for the attached geometry shaders,
1182 * and propagates them to the linked GS and linked shader program.
1183 */
1184static void
1185link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
1186 struct gl_shader *linked_shader,
1187 struct gl_shader **shader_list,
1188 unsigned num_shaders)
1189{
1190 linked_shader->Geom.VerticesOut = 0;
1191 linked_shader->Geom.InputType = PRIM_UNKNOWN;
1192 linked_shader->Geom.OutputType = PRIM_UNKNOWN;
1193
1194 /* No in/out qualifiers defined for anything but GLSL 1.50+
1195 * geometry shaders so far.
1196 */
1197 if (linked_shader->Type != GL_GEOMETRY_SHADER || prog->Version < 150)
1198 return;
1199
1200 /* From the GLSL 1.50 spec, page 46:
1201 *
1202 * "All geometry shader output layout declarations in a program
1203 * must declare the same layout and same value for
1204 * max_vertices. There must be at least one geometry output
1205 * layout declaration somewhere in a program, but not all
1206 * geometry shaders (compilation units) are required to
1207 * declare it."
1208 */
1209
1210 for (unsigned i = 0; i < num_shaders; i++) {
1211 struct gl_shader *shader = shader_list[i];
1212
1213 if (shader->Geom.InputType != PRIM_UNKNOWN) {
1214 if (linked_shader->Geom.InputType != PRIM_UNKNOWN &&
1215 linked_shader->Geom.InputType != shader->Geom.InputType) {
1216 linker_error(prog, "geometry shader defined with conflicting "
1217 "input types\n");
1218 return;
1219 }
1220 linked_shader->Geom.InputType = shader->Geom.InputType;
1221 }
1222
1223 if (shader->Geom.OutputType != PRIM_UNKNOWN) {
1224 if (linked_shader->Geom.OutputType != PRIM_UNKNOWN &&
1225 linked_shader->Geom.OutputType != shader->Geom.OutputType) {
1226 linker_error(prog, "geometry shader defined with conflicting "
1227 "output types\n");
1228 return;
1229 }
1230 linked_shader->Geom.OutputType = shader->Geom.OutputType;
1231 }
1232
1233 if (shader->Geom.VerticesOut != 0) {
1234 if (linked_shader->Geom.VerticesOut != 0 &&
1235 linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) {
1236 linker_error(prog, "geometry shader defined with conflicting "
1237 "output vertex count (%d and %d)\n",
1238 linked_shader->Geom.VerticesOut,
1239 shader->Geom.VerticesOut);
1240 return;
1241 }
1242 linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut;
1243 }
1244 }
1245
1246 /* Just do the intrastage -> interstage propagation right now,
1247 * since we already know we're in the right type of shader program
1248 * for doing it.
1249 */
1250 if (linked_shader->Geom.InputType == PRIM_UNKNOWN) {
1251 linker_error(prog,
1252 "geometry shader didn't declare primitive input type\n");
1253 return;
1254 }
1255 prog->Geom.InputType = linked_shader->Geom.InputType;
1256
1257 if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) {
1258 linker_error(prog,
1259 "geometry shader didn't declare primitive output type\n");
1260 return;
1261 }
1262 prog->Geom.OutputType = linked_shader->Geom.OutputType;
1263
1264 if (linked_shader->Geom.VerticesOut == 0) {
1265 linker_error(prog,
1266 "geometry shader didn't declare max_vertices\n");
1267 return;
1268 }
1269 prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut;
1270}
1271
1272/**
Ian Romanick3fb87872010-07-09 14:09:34 -07001273 * Combine a group of shaders for a single stage to generate a linked shader
1274 *
1275 * \note
1276 * If this function is supplied a single shader, it is cloned, and the new
1277 * shader is returned.
1278 */
1279static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001280link_intrastage_shaders(void *mem_ctx,
1281 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -07001282 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -07001283 struct gl_shader **shader_list,
1284 unsigned num_shaders)
1285{
Eric Anholtf609cf72012-04-27 13:52:56 -07001286 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -07001287
Ian Romanick13f782c2010-06-29 18:53:38 -07001288 /* Check that global variables defined in multiple shaders are consistent.
1289 */
Paul Berryb95d2372013-07-27 11:08:31 -07001290 cross_validate_globals(prog, shader_list, num_shaders, false);
1291 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -07001292 return NULL;
1293
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001294 /* Check that interface blocks defined in multiple shaders are consistent.
1295 */
Paul Berryb95d2372013-07-27 11:08:31 -07001296 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
1297 num_shaders);
1298 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001299 return NULL;
1300
Paul Berry4682b9b2013-07-27 15:07:08 -07001301 /* Link up uniform blocks defined within this stage. */
1302 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -05001303 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
1304 &uniform_blocks);
Eric Anholtf609cf72012-04-27 13:52:56 -07001305
Ian Romanick13f782c2010-06-29 18:53:38 -07001306 /* Check that there is only a single definition of each function signature
1307 * across all shaders.
1308 */
1309 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1310 foreach_list(node, shader_list[i]->ir) {
1311 ir_function *const f = ((ir_instruction *) node)->as_function();
1312
1313 if (f == NULL)
1314 continue;
1315
1316 for (unsigned j = i + 1; j < num_shaders; j++) {
1317 ir_function *const other =
1318 shader_list[j]->symbols->get_function(f->name);
1319
1320 /* If the other shader has no function (and therefore no function
1321 * signatures) with the same name, skip to the next shader.
1322 */
1323 if (other == NULL)
1324 continue;
1325
1326 foreach_iter (exec_list_iterator, iter, *f) {
1327 ir_function_signature *sig =
1328 (ir_function_signature *) iter.get();
1329
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001330 if (!sig->is_defined || sig->is_builtin())
Ian Romanick13f782c2010-06-29 18:53:38 -07001331 continue;
1332
1333 ir_function_signature *other_sig =
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001334 other->exact_matching_signature(NULL, &sig->parameters);
Ian Romanick13f782c2010-06-29 18:53:38 -07001335
1336 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001337 && !other_sig->is_builtin()) {
Ian Romanick586e7412011-07-28 14:04:09 -07001338 linker_error(prog, "function `%s' is multiply defined",
1339 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001340 return NULL;
1341 }
1342 }
1343 }
1344 }
1345 }
1346
1347 /* Find the shader that defines main, and make a clone of it.
1348 *
1349 * Starting with the clone, search for undefined references. If one is
1350 * found, find the shader that defines it. Clone the reference and add
1351 * it to the shader. Repeat until there are no undefined references or
1352 * until a reference cannot be resolved.
1353 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001354 gl_shader *main = NULL;
1355 for (unsigned i = 0; i < num_shaders; i++) {
1356 if (get_main_function_signature(shader_list[i]) != NULL) {
1357 main = shader_list[i];
1358 break;
1359 }
1360 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001361
Ian Romanick15ce87e2010-07-09 15:28:22 -07001362 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001363 linker_error(prog, "%s shader lacks `main'\n",
Eric Anholtfaf3dba2013-06-12 16:57:11 -07001364 _mesa_glsl_shader_target_name(shader_list[0]->Type));
Ian Romanick15ce87e2010-07-09 15:28:22 -07001365 return NULL;
1366 }
1367
Ian Romanick4a455952010-10-13 15:13:02 -07001368 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001369 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001370 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001371
Eric Anholtf609cf72012-04-27 13:52:56 -07001372 linked->UniformBlocks = uniform_blocks;
1373 linked->NumUniformBlocks = num_uniform_blocks;
1374 ralloc_steal(linked, linked->UniformBlocks);
1375
Eric Anholt6065a872013-06-12 18:12:40 -07001376 link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
1377
Ian Romanick15ce87e2010-07-09 15:28:22 -07001378 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001379
Ian Romanick31a97862010-07-12 18:48:50 -07001380 /* The a pointer to the main function in the final linked shader (i.e., the
1381 * copy of the original shader that contained the main function).
1382 */
1383 ir_function_signature *const main_sig = get_main_function_signature(linked);
1384
1385 /* Move any instructions other than variable declarations or function
1386 * declarations into main.
1387 */
Ian Romanick9303e352010-07-19 12:33:54 -07001388 exec_node *insertion_point =
1389 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1390 linked);
1391
Ian Romanick31a97862010-07-12 18:48:50 -07001392 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001393 if (shader_list[i] == main)
1394 continue;
1395
Ian Romanick31a97862010-07-12 18:48:50 -07001396 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001397 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001398 }
1399
Ian Romanick13f782c2010-06-29 18:53:38 -07001400 /* Resolve initializers for global variables in the linked shader.
1401 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001402 unsigned num_linking_shaders = num_shaders;
1403 for (unsigned i = 0; i < num_shaders; i++)
1404 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1405
1406 gl_shader **linking_shaders =
1407 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1408
1409 memcpy(linking_shaders, shader_list,
1410 sizeof(linking_shaders[0]) * num_shaders);
1411
1412 unsigned idx = num_shaders;
1413 for (unsigned i = 0; i < num_shaders; i++) {
1414 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1415 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1416 idx += shader_list[i]->num_builtins_to_link;
1417 }
1418
1419 assert(idx == num_linking_shaders);
1420
Ian Romanick4a455952010-10-13 15:13:02 -07001421 if (!link_function_calls(prog, linked, linking_shaders,
1422 num_linking_shaders)) {
1423 ctx->Driver.DeleteShader(ctx, linked);
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001424 free(linking_shaders);
1425 return NULL;
Ian Romanick4a455952010-10-13 15:13:02 -07001426 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001427
1428 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001429
Paul Berryc148ef62011-08-03 15:37:01 -07001430 /* At this point linked should contain all of the linked IR, so
1431 * validate it to make sure nothing went wrong.
1432 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001433 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07001434
Paul Berry7cfefe62013-07-30 21:13:48 -07001435 /* Set the size of geometry shader input arrays */
1436 if (linked->Type == GL_GEOMETRY_SHADER) {
1437 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
1438 geom_array_resize_visitor input_resize_visitor(num_vertices, prog);
1439 foreach_iter(exec_list_iterator, iter, *linked->ir) {
1440 ir_instruction *ir = (ir_instruction *)iter.get();
1441 ir->accept(&input_resize_visitor);
1442 }
1443 }
1444
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001445 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001446 * unspecified sizes have a size specified. The size is inferred from the
1447 * max_array_access field.
1448 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001449 array_sizing_visitor v;
1450 v.run(linked->ir);
Paul Berry15e05b92013-09-25 14:07:37 -07001451 v.fixup_unnamed_interface_types();
Ian Romanick6f539212010-12-07 18:30:33 -08001452
Ian Romanick3fb87872010-07-09 14:09:34 -07001453 return linked;
1454}
1455
Eric Anholta721abf2010-08-23 10:32:01 -07001456/**
1457 * Update the sizes of linked shader uniform arrays to the maximum
1458 * array index used.
1459 *
1460 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1461 *
1462 * If one or more elements of an array are active,
1463 * GetActiveUniform will return the name of the array in name,
1464 * subject to the restrictions listed above. The type of the array
1465 * is returned in type. The size parameter contains the highest
1466 * array element index used, plus one. The compiler or linker
1467 * determines the highest index used. There will be only one
1468 * active uniform reported by the GL per uniform array.
1469
1470 */
1471static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001472update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001473{
Ian Romanick3322fba2010-10-14 13:28:42 -07001474 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1475 if (prog->_LinkedShaders[i] == NULL)
1476 continue;
1477
Eric Anholta721abf2010-08-23 10:32:01 -07001478 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1479 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1480
Paul Berry6a2baf32013-06-10 14:01:45 -07001481 if ((var == NULL) || (var->mode != ir_var_uniform) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001482 !var->type->is_array())
1483 continue;
1484
Eric Anholt9feb4032012-05-01 14:43:31 -07001485 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1486 * will not be eliminated. Since we always do std140, just
1487 * don't resize arrays in UBOs.
1488 */
Ian Romanick13be1f42012-12-14 12:00:14 -08001489 if (var->is_in_uniform_block())
Eric Anholt9feb4032012-05-01 14:43:31 -07001490 continue;
1491
Eric Anholta721abf2010-08-23 10:32:01 -07001492 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001493 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1494 if (prog->_LinkedShaders[j] == NULL)
1495 continue;
1496
Eric Anholta721abf2010-08-23 10:32:01 -07001497 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1498 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1499 if (!other_var)
1500 continue;
1501
1502 if (strcmp(var->name, other_var->name) == 0 &&
1503 other_var->max_array_access > size) {
1504 size = other_var->max_array_access;
1505 }
1506 }
1507 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001508
Fabian Bieler63684782013-06-14 13:37:07 +02001509 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001510 /* If this is a built-in uniform (i.e., it's backed by some
1511 * fixed-function state), adjust the number of state slots to
1512 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001513 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001514 * slots is an integer multiple of the number of array elements.
1515 * Determine the number of slots per array element by dividing by
1516 * the old (total) size.
1517 */
1518 if (var->num_state_slots > 0) {
1519 var->num_state_slots = (size + 1)
1520 * (var->num_state_slots / var->type->length);
1521 }
1522
Eric Anholta721abf2010-08-23 10:32:01 -07001523 var->type = glsl_type::get_array_instance(var->type->fields.array,
1524 size + 1);
1525 /* FINISHME: We should update the types of array
1526 * dereferences of this variable now.
1527 */
1528 }
1529 }
1530 }
1531}
1532
Ian Romanick69846702010-06-22 17:29:19 -07001533/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001534 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001535 *
1536 * \param used_mask Bits representing used (1) and unused (0) locations
1537 * \param needed_count Number of contiguous bits needed.
1538 *
1539 * \return
1540 * Base location of the available bits on success or -1 on failure.
1541 */
1542int
1543find_available_slots(unsigned used_mask, unsigned needed_count)
1544{
1545 unsigned needed_mask = (1 << needed_count) - 1;
1546 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1547
1548 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1549 * cannot optimize possibly infinite loops" for the loop below.
1550 */
1551 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1552 return -1;
1553
1554 for (int i = 0; i <= max_bit_to_test; i++) {
1555 if ((needed_mask & ~used_mask) == needed_mask)
1556 return i;
1557
1558 needed_mask <<= 1;
1559 }
1560
1561 return -1;
1562}
1563
1564
Ian Romanickd32d4f72011-06-27 17:59:58 -07001565/**
1566 * Assign locations for either VS inputs for FS outputs
1567 *
1568 * \param prog Shader program whose variables need locations assigned
1569 * \param target_index Selector for the program target to receive location
1570 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1571 * \c MESA_SHADER_FRAGMENT.
1572 * \param max_index Maximum number of generic locations. This corresponds
1573 * to either the maximum number of draw buffers or the
1574 * maximum number of generic attributes.
1575 *
1576 * \return
1577 * If locations are successfully assigned, true is returned. Otherwise an
1578 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001579 */
Ian Romanick69846702010-06-22 17:29:19 -07001580bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001581assign_attribute_or_color_locations(gl_shader_program *prog,
1582 unsigned target_index,
1583 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001584{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001585 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001586 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001587 unsigned used_locations = (max_index >= 32)
1588 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001589
Ian Romanickd32d4f72011-06-27 17:59:58 -07001590 assert((target_index == MESA_SHADER_VERTEX)
1591 || (target_index == MESA_SHADER_FRAGMENT));
1592
1593 gl_shader *const sh = prog->_LinkedShaders[target_index];
1594 if (sh == NULL)
1595 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001596
Ian Romanick69846702010-06-22 17:29:19 -07001597 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001598 *
1599 * 1. Invalidate the location assignments for all vertex shader inputs.
1600 *
1601 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001602 * glBindVertexAttribLocation) locations and outputs that have
1603 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001604 *
Ian Romanick69846702010-06-22 17:29:19 -07001605 * 3. Sort the attributes without assigned locations by number of slots
1606 * required in decreasing order. Fragmentation caused by attribute
1607 * locations assigned by the application may prevent large attributes
1608 * from having enough contiguous space.
1609 *
1610 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001611 */
1612
Ian Romanickd32d4f72011-06-27 17:59:58 -07001613 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001614 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001615
Ian Romanickd32d4f72011-06-27 17:59:58 -07001616 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001617 (target_index == MESA_SHADER_VERTEX)
1618 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001619
1620
Ian Romanick69846702010-06-22 17:29:19 -07001621 /* Temporary storage for the set of attributes that need locations assigned.
1622 */
1623 struct temp_attr {
1624 unsigned slots;
1625 ir_variable *var;
1626
1627 /* Used below in the call to qsort. */
1628 static int compare(const void *a, const void *b)
1629 {
1630 const temp_attr *const l = (const temp_attr *) a;
1631 const temp_attr *const r = (const temp_attr *) b;
1632
1633 /* Reversed because we want a descending order sort below. */
1634 return r->slots - l->slots;
1635 }
1636 } to_assign[16];
1637
1638 unsigned num_attr = 0;
1639
Eric Anholt16b68b12010-06-30 11:05:43 -07001640 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001641 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1642
Brian Paul4470ff22011-07-19 21:10:25 -06001643 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001644 continue;
1645
Ian Romanick68a4fc92010-10-07 17:21:22 -07001646 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001647 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001648 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001649 linker_error(prog,
1650 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001651 (var->location < 0)
1652 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001653 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001654 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001655 }
1656 } else if (target_index == MESA_SHADER_VERTEX) {
1657 unsigned binding;
1658
1659 if (prog->AttributeBindings->get(binding, var->name)) {
1660 assert(binding >= VERT_ATTRIB_GENERIC0);
1661 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001662 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001663 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001664 } else if (target_index == MESA_SHADER_FRAGMENT) {
1665 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001666 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001667
1668 if (prog->FragDataBindings->get(binding, var->name)) {
1669 assert(binding >= FRAG_RESULT_DATA0);
1670 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001671 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001672
1673 if (prog->FragDataIndexBindings->get(index, var->name)) {
1674 var->index = index;
1675 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001676 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001677 }
1678
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001679 /* If the variable is not a built-in and has a location statically
1680 * assigned in the shader (presumably via a layout qualifier), make sure
1681 * that it doesn't collide with other assigned locations. Otherwise,
1682 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001683 */
Paul Berry0026ad42013-07-31 08:15:08 -07001684 const unsigned slots = var->type->count_attribute_slots();
Ian Romanick523b6112011-08-17 15:40:03 -07001685 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001686 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001687 /* From page 61 of the OpenGL 4.0 spec:
1688 *
1689 * "LinkProgram will fail if the attribute bindings assigned
1690 * by BindAttribLocation do not leave not enough space to
1691 * assign a location for an active matrix attribute or an
1692 * active attribute array, both of which require multiple
1693 * contiguous generic attributes."
1694 *
1695 * Previous versions of the spec contain similar language but omit
1696 * the bit about attribute arrays.
1697 *
1698 * Page 61 of the OpenGL 4.0 spec also says:
1699 *
1700 * "It is possible for an application to bind more than one
1701 * attribute name to the same location. This is referred to as
1702 * aliasing. This will only work if only one of the aliased
1703 * attributes is active in the executable program, or if no
1704 * path through the shader consumes more than one attribute of
1705 * a set of attributes aliased to the same location. A link
1706 * error can occur if the linker determines that every path
1707 * through the shader consumes multiple aliased attributes,
1708 * but implementations are not required to generate an error
1709 * in this case."
1710 *
1711 * These two paragraphs are either somewhat contradictory, or I
1712 * don't fully understand one or both of them.
1713 */
1714 /* FINISHME: The code as currently written does not support
1715 * FINISHME: attribute location aliasing (see comment above).
1716 */
1717 /* Mask representing the contiguous slots that will be used by
1718 * this attribute.
1719 */
1720 const unsigned attr = var->location - generic_base;
1721 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001722
Ian Romanick523b6112011-08-17 15:40:03 -07001723 /* Generate a link error if the set of bits requested for this
1724 * attribute overlaps any previously allocated bits.
1725 */
1726 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001727 const char *const string = (target_index == MESA_SHADER_VERTEX)
1728 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001729 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001730 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001731 "available for %s `%s' %d %d %d", string,
1732 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001733 return false;
1734 }
1735
1736 used_locations |= (use_mask << attr);
1737 }
1738
1739 continue;
1740 }
1741
1742 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001743 to_assign[num_attr].var = var;
1744 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001745 }
Ian Romanick69846702010-06-22 17:29:19 -07001746
1747 /* If all of the attributes were assigned locations by the application (or
1748 * are built-in attributes with fixed locations), return early. This should
1749 * be the common case.
1750 */
1751 if (num_attr == 0)
1752 return true;
1753
1754 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1755
Ian Romanickd32d4f72011-06-27 17:59:58 -07001756 if (target_index == MESA_SHADER_VERTEX) {
1757 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1758 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1759 * reserved to prevent it from being automatically allocated below.
1760 */
1761 find_deref_visitor find("gl_Vertex");
1762 find.run(sh->ir);
1763 if (find.variable_found())
1764 used_locations |= (1 << 0);
1765 }
Ian Romanick982e3792010-06-29 18:58:20 -07001766
Ian Romanick69846702010-06-22 17:29:19 -07001767 for (unsigned i = 0; i < num_attr; i++) {
1768 /* Mask representing the contiguous slots that will be used by this
1769 * attribute.
1770 */
1771 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1772
1773 int location = find_available_slots(used_locations, to_assign[i].slots);
1774
1775 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001776 const char *const string = (target_index == MESA_SHADER_VERTEX)
1777 ? "vertex shader input" : "fragment shader output";
1778
Ian Romanick586e7412011-07-28 14:04:09 -07001779 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001780 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001781 "available for %s `%s'",
1782 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001783 return false;
1784 }
1785
Ian Romanickd32d4f72011-06-27 17:59:58 -07001786 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001787 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001788 used_locations |= (use_mask << location);
1789 }
1790
1791 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001792}
1793
1794
Ian Romanick40e114b2010-08-17 14:55:50 -07001795/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001796 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001797 */
1798void
Ian Romanickcc90e622010-10-19 17:59:10 -07001799demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001800{
1801 foreach_list(node, sh->ir) {
1802 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1803
Ian Romanickcc90e622010-10-19 17:59:10 -07001804 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001805 continue;
1806
Ian Romanickcc90e622010-10-19 17:59:10 -07001807 /* A shader 'in' or 'out' variable is only really an input or output if
1808 * its value is used by other shader stages. This will cause the variable
1809 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001810 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001811 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001812 var->mode = ir_var_auto;
1813 }
1814 }
1815}
1816
1817
Paul Berry871ddb92011-11-05 11:17:32 -07001818/**
Marek Olšákec174a42011-11-18 15:00:10 +01001819 * Store the gl_FragDepth layout in the gl_shader_program struct.
1820 */
1821static void
1822store_fragdepth_layout(struct gl_shader_program *prog)
1823{
1824 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1825 return;
1826 }
1827
1828 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1829
1830 /* We don't look up the gl_FragDepth symbol directly because if
1831 * gl_FragDepth is not used in the shader, it's removed from the IR.
1832 * However, the symbol won't be removed from the symbol table.
1833 *
1834 * We're only interested in the cases where the variable is NOT removed
1835 * from the IR.
1836 */
1837 foreach_list(node, ir) {
1838 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1839
Paul Berry42a29d82013-01-11 14:39:32 -08001840 if (var == NULL || var->mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001841 continue;
1842 }
1843
1844 if (strcmp(var->name, "gl_FragDepth") == 0) {
1845 switch (var->depth_layout) {
1846 case ir_depth_layout_none:
1847 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1848 return;
1849 case ir_depth_layout_any:
1850 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1851 return;
1852 case ir_depth_layout_greater:
1853 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1854 return;
1855 case ir_depth_layout_less:
1856 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1857 return;
1858 case ir_depth_layout_unchanged:
1859 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1860 return;
1861 default:
1862 assert(0);
1863 return;
1864 }
1865 }
1866 }
1867}
1868
1869/**
Ian Romanick92f81592011-11-08 12:37:19 -08001870 * Validate the resources used by a program versus the implementation limits
1871 */
Paul Berryb95d2372013-07-27 11:08:31 -07001872static void
Ian Romanick92f81592011-11-08 12:37:19 -08001873check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1874{
1875 static const char *const shader_names[MESA_SHADER_TYPES] = {
Marek Olšák030ca232013-06-12 17:15:46 +02001876 "vertex", "geometry", "fragment"
Ian Romanick92f81592011-11-08 12:37:19 -08001877 };
1878
1879 const unsigned max_samplers[MESA_SHADER_TYPES] = {
Marek Olšák5e784332013-05-02 02:30:44 +02001880 ctx->Const.VertexProgram.MaxTextureImageUnits,
Marek Olšák030ca232013-06-12 17:15:46 +02001881 ctx->Const.GeometryProgram.MaxTextureImageUnits,
1882 ctx->Const.FragmentProgram.MaxTextureImageUnits
Ian Romanick92f81592011-11-08 12:37:19 -08001883 };
1884
Eric Anholt38e77e52013-05-23 11:10:15 -07001885 const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = {
Ian Romanick92f81592011-11-08 12:37:19 -08001886 ctx->Const.VertexProgram.MaxUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001887 ctx->Const.GeometryProgram.MaxUniformComponents,
1888 ctx->Const.FragmentProgram.MaxUniformComponents
Ian Romanick92f81592011-11-08 12:37:19 -08001889 };
1890
Eric Anholt38e77e52013-05-23 11:10:15 -07001891 const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = {
1892 ctx->Const.VertexProgram.MaxCombinedUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001893 ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
1894 ctx->Const.FragmentProgram.MaxCombinedUniformComponents
Eric Anholt38e77e52013-05-23 11:10:15 -07001895 };
1896
Eric Anholt877a8972012-06-25 12:47:01 -07001897 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
1898 ctx->Const.VertexProgram.MaxUniformBlocks,
Eric Anholt877a8972012-06-25 12:47:01 -07001899 ctx->Const.GeometryProgram.MaxUniformBlocks,
Marek Olšák030ca232013-06-12 17:15:46 +02001900 ctx->Const.FragmentProgram.MaxUniformBlocks
Eric Anholt877a8972012-06-25 12:47:01 -07001901 };
1902
Ian Romanick92f81592011-11-08 12:37:19 -08001903 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1904 struct gl_shader *sh = prog->_LinkedShaders[i];
1905
1906 if (sh == NULL)
1907 continue;
1908
1909 if (sh->num_samplers > max_samplers[i]) {
1910 linker_error(prog, "Too many %s shader texture samplers",
1911 shader_names[i]);
1912 }
1913
Eric Anholt38e77e52013-05-23 11:10:15 -07001914 if (sh->num_uniform_components > max_default_uniform_components[i]) {
1915 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1916 linker_warning(prog, "Too many %s shader default uniform block "
1917 "components, but the driver will try to optimize "
1918 "them out; this is non-portable out-of-spec "
1919 "behavior\n",
1920 shader_names[i]);
1921 } else {
1922 linker_error(prog, "Too many %s shader default uniform block "
1923 "components",
1924 shader_names[i]);
1925 }
1926 }
1927
1928 if (sh->num_combined_uniform_components >
1929 max_combined_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001930 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1931 linker_warning(prog, "Too many %s shader uniform components, "
1932 "but the driver will try to optimize them out; "
1933 "this is non-portable out-of-spec behavior\n",
1934 shader_names[i]);
1935 } else {
1936 linker_error(prog, "Too many %s shader uniform components",
1937 shader_names[i]);
1938 }
Ian Romanick92f81592011-11-08 12:37:19 -08001939 }
1940 }
1941
Eric Anholt877a8972012-06-25 12:47:01 -07001942 unsigned blocks[MESA_SHADER_TYPES] = {0};
1943 unsigned total_uniform_blocks = 0;
1944
1945 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
1946 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1947 if (prog->UniformBlockStageIndex[j][i] != -1) {
1948 blocks[j]++;
1949 total_uniform_blocks++;
1950 }
1951 }
1952
1953 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1954 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1955 prog->NumUniformBlocks,
1956 ctx->Const.MaxCombinedUniformBlocks);
1957 } else {
1958 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1959 if (blocks[i] > max_uniform_blocks[i]) {
1960 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
1961 shader_names[i],
1962 blocks[i],
1963 max_uniform_blocks[i]);
1964 break;
1965 }
1966 }
1967 }
1968 }
Ian Romanick92f81592011-11-08 12:37:19 -08001969}
Paul Berry871ddb92011-11-05 11:17:32 -07001970
Ian Romanick0e59b262010-06-23 11:23:01 -07001971void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001972link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001973{
Paul Berry871ddb92011-11-05 11:17:32 -07001974 tfeedback_decl *tfeedback_decls = NULL;
1975 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1976
Kenneth Graunked3073f52011-01-21 14:32:31 -08001977 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001978
Paul Berryb95d2372013-07-27 11:08:31 -07001979 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07001980 prog->Validated = false;
1981 prog->_Used = false;
1982
Eric Anholtf609cf72012-04-27 13:52:56 -07001983 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08001984 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07001985
Eric Anholtf609cf72012-04-27 13:52:56 -07001986 ralloc_free(prog->UniformBlocks);
1987 prog->UniformBlocks = NULL;
1988 prog->NumUniformBlocks = 0;
1989 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
1990 ralloc_free(prog->UniformBlockStageIndex[i]);
1991 prog->UniformBlockStageIndex[i] = NULL;
1992 }
1993
Ian Romanick832dfa52010-06-17 15:04:20 -07001994 /* Separate the shaders into groups based on their type.
1995 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001996 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001997 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001998 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001999 unsigned num_frag_shaders = 0;
Bryan Cain25480922013-02-15 09:46:50 -06002000 struct gl_shader **geom_shader_list;
2001 unsigned num_geom_shaders = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07002002
Eric Anholt16b68b12010-06-30 11:05:43 -07002003 vert_shader_list = (struct gl_shader **)
Paul Berry844bd712013-07-30 22:38:43 -07002004 calloc(prog->NumShaders, sizeof(struct gl_shader *));
2005 frag_shader_list = (struct gl_shader **)
2006 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Bryan Cain25480922013-02-15 09:46:50 -06002007 geom_shader_list = (struct gl_shader **)
2008 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002009
Ian Romanick25f51d32010-07-16 15:51:50 -07002010 unsigned min_version = UINT_MAX;
2011 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002012 const bool is_es_prog =
2013 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002014 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002015 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2016 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2017
Paul Berrya9f34dc2012-08-02 17:49:44 -07002018 if (prog->Shaders[i]->IsES != is_es_prog) {
2019 linker_error(prog, "all shaders must use same shading "
2020 "language version\n");
2021 goto done;
2022 }
2023
Ian Romanick832dfa52010-06-17 15:04:20 -07002024 switch (prog->Shaders[i]->Type) {
2025 case GL_VERTEX_SHADER:
2026 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2027 num_vert_shaders++;
2028 break;
2029 case GL_FRAGMENT_SHADER:
2030 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2031 num_frag_shaders++;
2032 break;
2033 case GL_GEOMETRY_SHADER:
Bryan Cain25480922013-02-15 09:46:50 -06002034 geom_shader_list[num_geom_shaders] = prog->Shaders[i];
2035 num_geom_shaders++;
Ian Romanick832dfa52010-06-17 15:04:20 -07002036 break;
2037 }
2038 }
2039
Paul Berry672fab02013-10-13 18:01:11 -07002040 /* In desktop GLSL, different shader versions may be linked together. In
2041 * GLSL ES, all shader versions must be the same.
Ian Romanick25f51d32010-07-16 15:51:50 -07002042 */
Paul Berry672fab02013-10-13 18:01:11 -07002043 if (is_es_prog && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002044 linker_error(prog, "all shaders must use same shading "
2045 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002046 goto done;
2047 }
2048
2049 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002050 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002051
Fabian Bielerbd85ba02013-05-24 23:26:54 +02002052 /* Geometry shaders have to be linked with vertex shaders.
2053 */
2054 if (num_geom_shaders > 0 && num_vert_shaders == 0) {
2055 linker_error(prog, "Geometry shader must be linked with "
2056 "vertex shader\n");
2057 goto done;
2058 }
2059
Ian Romanick3322fba2010-10-14 13:28:42 -07002060 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2061 if (prog->_LinkedShaders[i] != NULL)
2062 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2063
2064 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002065 }
2066
Ian Romanickcd6764e2010-07-16 16:00:07 -07002067 /* Link all shaders for a particular stage and validate the result.
2068 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002069 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002070 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002071 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2072 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002073
Paul Berryb95d2372013-07-27 11:08:31 -07002074 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07002075 goto done;
2076
Paul Berryb95d2372013-07-27 11:08:31 -07002077 validate_vertex_shader_executable(prog, sh);
2078 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002079 goto done;
Paul Berry44b7ebe2013-10-23 12:55:24 -07002080 prog->LastClipDistanceArraySize = prog->Vert.ClipDistanceArraySize;
Ian Romanick3fb87872010-07-09 14:09:34 -07002081
Ian Romanick3322fba2010-10-14 13:28:42 -07002082 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2083 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002084 }
2085
2086 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002087 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002088 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2089 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002090
Paul Berryb95d2372013-07-27 11:08:31 -07002091 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07002092 goto done;
2093
Paul Berryb95d2372013-07-27 11:08:31 -07002094 validate_fragment_shader_executable(prog, sh);
2095 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002096 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002097
Ian Romanick3322fba2010-10-14 13:28:42 -07002098 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2099 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002100 }
2101
Bryan Cain25480922013-02-15 09:46:50 -06002102 if (num_geom_shaders > 0) {
2103 gl_shader *const sh =
2104 link_intrastage_shaders(mem_ctx, ctx, prog, geom_shader_list,
2105 num_geom_shaders);
2106
2107 if (!prog->LinkStatus)
2108 goto done;
2109
2110 validate_geometry_shader_executable(prog, sh);
2111 if (!prog->LinkStatus)
2112 goto done;
Paul Berry44b7ebe2013-10-23 12:55:24 -07002113 prog->LastClipDistanceArraySize = prog->Geom.ClipDistanceArraySize;
Bryan Cain25480922013-02-15 09:46:50 -06002114
2115 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
2116 sh);
2117 }
2118
Ian Romanick3ed850e2010-06-23 12:18:21 -07002119 /* Here begins the inter-stage linking phase. Some initial validation is
2120 * performed, then locations are assigned for uniforms, attributes, and
2121 * varyings.
2122 */
Paul Berryb95d2372013-07-27 11:08:31 -07002123 cross_validate_uniforms(prog);
2124 if (!prog->LinkStatus)
2125 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002126
Paul Berryb95d2372013-07-27 11:08:31 -07002127 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07002128
Paul Berryb95d2372013-07-27 11:08:31 -07002129 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2130 if (prog->_LinkedShaders[prev] != NULL)
2131 break;
2132 }
Ian Romanick3322fba2010-10-14 13:28:42 -07002133
Paul Berryb95d2372013-07-27 11:08:31 -07002134 /* Validate the inputs of each stage with the output of the preceding
2135 * stage.
2136 */
2137 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2138 if (prog->_LinkedShaders[i] == NULL)
2139 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07002140
Paul Berryb95d2372013-07-27 11:08:31 -07002141 validate_interstage_interface_blocks(prog, prog->_LinkedShaders[prev],
2142 prog->_LinkedShaders[i]);
2143 if (!prog->LinkStatus)
2144 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002145
Paul Berryb95d2372013-07-27 11:08:31 -07002146 cross_validate_outputs_to_inputs(prog,
2147 prog->_LinkedShaders[prev],
2148 prog->_LinkedShaders[i]);
2149 if (!prog->LinkStatus)
2150 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07002151
Paul Berryb95d2372013-07-27 11:08:31 -07002152 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002153 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002154
Jordan Justen5ebf5472013-03-10 03:20:03 -07002155
2156 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2157 if (prog->_LinkedShaders[i] != NULL)
2158 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
2159 }
2160
Eric Anholt3de13952012-05-04 13:08:46 -07002161 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2162 * it before optimization because we want most of the checks to get
2163 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002164 *
2165 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002166 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002167 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002168 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2169 if (sh) {
2170 lower_discard_flow(sh->ir);
2171 }
2172 }
2173
Eric Anholtf609cf72012-04-27 13:52:56 -07002174 if (!interstage_cross_validate_uniform_blocks(prog))
2175 goto done;
2176
Eric Anholt2f4fe152010-08-10 13:06:49 -07002177 /* Do common optimization before assigning storage for attributes,
2178 * uniforms, and varyings. Later optimization could possibly make
2179 * some of that unused.
2180 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002181 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2182 if (prog->_LinkedShaders[i] == NULL)
2183 continue;
2184
Ian Romanick02c5ae12011-07-11 10:46:01 -07002185 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2186 if (!prog->LinkStatus)
2187 goto done;
2188
Paul Berry18392442012-12-04 11:11:02 -08002189 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2190 lower_clip_distance(prog->_LinkedShaders[i]);
2191 }
Paul Berryc06e3252011-08-11 20:58:21 -07002192
Brian Paul7feabfe2012-03-20 17:43:12 -06002193 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2194
Kenneth Graunkeb7657402013-04-17 17:30:22 -07002195 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002196 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002197 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002198
Paul Berry50895d42012-12-05 07:17:07 -08002199 /* Mark all generic shader inputs and outputs as unpaired. */
2200 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2201 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002202 prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir);
Paul Berry50895d42012-12-05 07:17:07 -08002203 }
Bryan Cain25480922013-02-15 09:46:50 -06002204 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2205 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002206 prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir);
Bryan Cain25480922013-02-15 09:46:50 -06002207 }
Paul Berry50895d42012-12-05 07:17:07 -08002208 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2209 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002210 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir);
Paul Berry50895d42012-12-05 07:17:07 -08002211 }
2212
Ian Romanickd32d4f72011-06-27 17:59:58 -07002213 /* FINISHME: The value of the max_attribute_index parameter is
2214 * FINISHME: implementation dependent based on the value of
2215 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2216 * FINISHME: at least 16, so hardcode 16 for now.
2217 */
2218 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002219 goto done;
2220 }
2221
Dave Airlie1256a5d2012-03-24 13:33:41 +00002222 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002223 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002224 }
2225
Marek Olšák284d9542013-06-12 02:18:09 +02002226 unsigned first;
2227 for (first = 0; first < MESA_SHADER_TYPES; first++) {
2228 if (prog->_LinkedShaders[first] != NULL)
Ian Romanick3322fba2010-10-14 13:28:42 -07002229 break;
2230 }
2231
Paul Berry871ddb92011-11-05 11:17:32 -07002232 if (num_tfeedback_decls != 0) {
2233 /* From GL_EXT_transform_feedback:
2234 * A program will fail to link if:
2235 *
2236 * * the <count> specified by TransformFeedbackVaryingsEXT is
2237 * non-zero, but the program object has no vertex or geometry
2238 * shader;
2239 */
Bryan Cain25480922013-02-15 09:46:50 -06002240 if (first == MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07002241 linker_error(prog, "Transform feedback varyings specified, but "
2242 "no vertex or geometry shader is present.");
2243 goto done;
2244 }
2245
2246 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2247 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002248 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002249 prog->TransformFeedback.VaryingNames,
2250 tfeedback_decls))
2251 goto done;
2252 }
2253
Marek Olšák284d9542013-06-12 02:18:09 +02002254 /* Linking the stages in the opposite order (from fragment to vertex)
2255 * ensures that inter-shader outputs written to in an earlier stage are
2256 * eliminated if they are (transitively) not used in a later stage.
2257 */
2258 int last, next;
2259 for (last = MESA_SHADER_TYPES-1; last >= 0; last--) {
2260 if (prog->_LinkedShaders[last] != NULL)
2261 break;
Ian Romanick3322fba2010-10-14 13:28:42 -07002262 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002263
Marek Olšák284d9542013-06-12 02:18:09 +02002264 if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
2265 gl_shader *const sh = prog->_LinkedShaders[last];
2266
2267 if (num_tfeedback_decls != 0) {
2268 /* There was no fragment shader, but we still have to assign varying
2269 * locations for use by transform feedback.
2270 */
2271 if (!assign_varying_locations(ctx, mem_ctx, prog,
2272 sh, NULL,
Paul Berry3b0cf702013-04-10 06:48:42 -07002273 num_tfeedback_decls, tfeedback_decls,
2274 0))
Marek Olšák284d9542013-06-12 02:18:09 +02002275 goto done;
2276 }
2277
Marek Olšákd13003f2013-08-09 22:34:45 +02002278 do_dead_builtin_varyings(ctx, sh, NULL,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002279 num_tfeedback_decls, tfeedback_decls);
2280
Marek Olšák284d9542013-06-12 02:18:09 +02002281 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
2282
2283 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07002284 */
Marek Olšák284d9542013-06-12 02:18:09 +02002285 while (do_dead_code(sh->ir, false))
2286 ;
2287 }
2288 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002289 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02002290 */
2291 gl_shader *const sh = prog->_LinkedShaders[first];
2292
Marek Olšákd13003f2013-08-09 22:34:45 +02002293 do_dead_builtin_varyings(ctx, NULL, sh,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002294 num_tfeedback_decls, tfeedback_decls);
2295
Marek Olšák284d9542013-06-12 02:18:09 +02002296 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
2297
2298 while (do_dead_code(sh->ir, false))
2299 ;
2300 }
2301
2302 next = last;
2303 for (int i = next - 1; i >= 0; i--) {
2304 if (prog->_LinkedShaders[i] == NULL)
2305 continue;
2306
2307 gl_shader *const sh_i = prog->_LinkedShaders[i];
2308 gl_shader *const sh_next = prog->_LinkedShaders[next];
Paul Berry3b0cf702013-04-10 06:48:42 -07002309 unsigned gs_input_vertices =
2310 next == MESA_SHADER_GEOMETRY ? prog->Geom.VerticesIn : 0;
Marek Olšák284d9542013-06-12 02:18:09 +02002311
2312 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
2313 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
Paul Berry3b0cf702013-04-10 06:48:42 -07002314 tfeedback_decls, gs_input_vertices))
Paul Berry871ddb92011-11-05 11:17:32 -07002315 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02002316
Marek Olšákd13003f2013-08-09 22:34:45 +02002317 do_dead_builtin_varyings(ctx, sh_i, sh_next,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002318 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2319 tfeedback_decls);
2320
Marek Olšák284d9542013-06-12 02:18:09 +02002321 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
2322 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
2323
2324 /* Eliminate code that is now dead due to unused outputs being demoted.
2325 */
2326 while (do_dead_code(sh_i->ir, false))
2327 ;
2328 while (do_dead_code(sh_next->ir, false))
2329 ;
2330
Marek Olšák3c555822013-06-13 03:17:22 +02002331 /* This must be done after all dead varyings are eliminated. */
Ian Romanick42305fb2013-09-10 12:00:34 -05002332 if (!check_against_output_limit(ctx, prog, sh_i))
2333 goto done;
2334 if (!check_against_input_limit(ctx, prog, sh_next))
Marek Olšák3c555822013-06-13 03:17:22 +02002335 goto done;
2336
Marek Olšák284d9542013-06-12 02:18:09 +02002337 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07002338 }
2339
2340 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2341 goto done;
2342
Ian Romanick960d7222011-10-21 11:21:02 -07002343 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002344 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002345 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002346
Paul Berryb95d2372013-07-27 11:08:31 -07002347 check_resources(ctx, prog);
2348 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08002349 goto done;
2350
Ian Romanickce9171f2011-02-03 17:10:14 -08002351 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002352 * present in a linked program. By checking prog->IsES, we also
2353 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002354 */
Eric Anholt57f79782011-07-22 12:57:47 -07002355 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002356 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002357 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002358 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002359 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002360 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002361 }
2362 }
2363
Ian Romanick13e10e42010-06-21 12:03:24 -07002364 /* FINISHME: Assign fragment shader output locations. */
2365
Ian Romanick832dfa52010-06-17 15:04:20 -07002366done:
2367 free(vert_shader_list);
Paul Berry844bd712013-07-30 22:38:43 -07002368 free(frag_shader_list);
Bryan Cain25480922013-02-15 09:46:50 -06002369 free(geom_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002370
2371 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2372 if (prog->_LinkedShaders[i] == NULL)
2373 continue;
2374
2375 /* Retain any live IR, but trash the rest. */
2376 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002377
2378 /* The symbol table in the linked shaders may contain references to
2379 * variables that were removed (e.g., unused uniforms). Since it may
2380 * contain junk, there is no possible valid use. Delete it and set the
2381 * pointer to NULL.
2382 */
2383 delete prog->_LinkedShaders[i]->symbols;
2384 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002385 }
2386
Kenneth Graunked3073f52011-01-21 14:32:31 -08002387 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002388}