blob: 381440efd59b3eaa954e319a4488ba26e1a1b02f [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"
80}
81
Bryan Cain25480922013-02-15 09:46:50 -060082void linker_error(gl_shader_program *, const char *, ...);
83
Ian Romanick832dfa52010-06-17 15:04:20 -070084/**
85 * Visitor that determines whether or not a variable is ever written.
86 */
87class find_assignment_visitor : public ir_hierarchical_visitor {
88public:
89 find_assignment_visitor(const char *name)
90 : name(name), found(false)
91 {
92 /* empty */
93 }
94
95 virtual ir_visitor_status visit_enter(ir_assignment *ir)
96 {
97 ir_variable *const var = ir->lhs->variable_referenced();
98
99 if (strcmp(name, var->name) == 0) {
100 found = true;
101 return visit_stop;
102 }
103
104 return visit_continue_with_parent;
105 }
106
Eric Anholt18a60232010-08-23 11:29:25 -0700107 virtual ir_visitor_status visit_enter(ir_call *ir)
108 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700109 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700110 foreach_iter(exec_list_iterator, iter, *ir) {
111 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
112 ir_variable *sig_param = (ir_variable *)sig_iter.get();
113
Paul Berry42a29d82013-01-11 14:39:32 -0800114 if (sig_param->mode == ir_var_function_out ||
115 sig_param->mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700116 ir_variable *var = param_rval->variable_referenced();
117 if (var && strcmp(name, var->name) == 0) {
118 found = true;
119 return visit_stop;
120 }
121 }
122 sig_iter.next();
123 }
124
Kenneth Graunked884f602012-03-20 15:56:37 -0700125 if (ir->return_deref != NULL) {
126 ir_variable *const var = ir->return_deref->variable_referenced();
127
128 if (strcmp(name, var->name) == 0) {
129 found = true;
130 return visit_stop;
131 }
132 }
133
Eric Anholt18a60232010-08-23 11:29:25 -0700134 return visit_continue_with_parent;
135 }
136
Ian Romanick832dfa52010-06-17 15:04:20 -0700137 bool variable_found()
138 {
139 return found;
140 }
141
142private:
143 const char *name; /**< Find writes to a variable with this name. */
144 bool found; /**< Was a write to the variable found? */
145};
146
Ian Romanickc93b8f12010-06-17 15:20:22 -0700147
Ian Romanickc33e78f2010-08-13 12:30:41 -0700148/**
149 * Visitor that determines whether or not a variable is ever read.
150 */
151class find_deref_visitor : public ir_hierarchical_visitor {
152public:
153 find_deref_visitor(const char *name)
154 : name(name), found(false)
155 {
156 /* empty */
157 }
158
159 virtual ir_visitor_status visit(ir_dereference_variable *ir)
160 {
161 if (strcmp(this->name, ir->var->name) == 0) {
162 this->found = true;
163 return visit_stop;
164 }
165
166 return visit_continue;
167 }
168
169 bool variable_found() const
170 {
171 return this->found;
172 }
173
174private:
175 const char *name; /**< Find writes to a variable with this name. */
176 bool found; /**< Was a write to the variable found? */
177};
178
179
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700180void
Ian Romanick586e7412011-07-28 14:04:09 -0700181linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700182{
183 va_list ap;
184
Kenneth Graunked3073f52011-01-21 14:32:31 -0800185 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700186 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800187 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700188 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700189
190 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700191}
192
193
194void
Ian Romanick379a32f2011-07-28 14:09:06 -0700195linker_warning(gl_shader_program *prog, const char *fmt, ...)
196{
197 va_list ap;
198
199 ralloc_strcat(&prog->InfoLog, "error: ");
200 va_start(ap, fmt);
201 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
202 va_end(ap);
203
204}
205
206
Paul Berryb92900d2013-01-28 14:21:59 -0800207/**
208 * Given a string identifying a program resource, break it into a base name
209 * and an optional array index in square brackets.
210 *
211 * If an array index is present, \c out_base_name_end is set to point to the
212 * "[" that precedes the array index, and the array index itself is returned
213 * as a long.
214 *
215 * If no array index is present (or if the array index is negative or
216 * mal-formed), \c out_base_name_end, is set to point to the null terminator
217 * at the end of the input string, and -1 is returned.
218 *
219 * Only the final array index is parsed; if the string contains other array
220 * indices (or structure field accesses), they are left in the base name.
221 *
222 * No attempt is made to check that the base name is properly formed;
223 * typically the caller will look up the base name in a hash table, so
224 * ill-formed base names simply turn into hash table lookup failures.
225 */
226long
227parse_program_resource_name(const GLchar *name,
228 const GLchar **out_base_name_end)
229{
230 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
231 *
232 * "When an integer array element or block instance number is part of
233 * the name string, it will be specified in decimal form without a "+"
234 * or "-" sign or any extra leading zeroes. Additionally, the name
235 * string will not include white space anywhere in the string."
236 */
237
238 const size_t len = strlen(name);
239 *out_base_name_end = name + len;
240
241 if (len == 0 || name[len-1] != ']')
242 return -1;
243
244 /* Walk backwards over the string looking for a non-digit character. This
245 * had better be the opening bracket for an array index.
246 *
247 * Initially, i specifies the location of the ']'. Since the string may
248 * contain only the ']' charcater, walk backwards very carefully.
249 */
250 unsigned i;
251 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
252 /* empty */ ;
253
254 if ((i == 0) || name[i-1] != '[')
255 return -1;
256
257 long array_index = strtol(&name[i], NULL, 10);
258 if (array_index < 0)
259 return -1;
260
261 *out_base_name_end = name + (i - 1);
262 return array_index;
263}
264
265
Ian Romanick379a32f2011-07-28 14:09:06 -0700266void
Paul Berry50895d42012-12-05 07:17:07 -0800267link_invalidate_variable_locations(gl_shader *sh, int input_base,
268 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700269{
Eric Anholt16b68b12010-06-30 11:05:43 -0700270 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700271 ir_variable *const var = ((ir_instruction *) node)->as_variable();
272
Paul Berry50895d42012-12-05 07:17:07 -0800273 if (var == NULL)
274 continue;
275
276 int base;
277 switch (var->mode) {
Paul Berry42a29d82013-01-11 14:39:32 -0800278 case ir_var_shader_in:
Paul Berry50895d42012-12-05 07:17:07 -0800279 base = input_base;
280 break;
Paul Berry42a29d82013-01-11 14:39:32 -0800281 case ir_var_shader_out:
Paul Berry50895d42012-12-05 07:17:07 -0800282 base = output_base;
283 break;
284 default:
285 continue;
286 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700287
288 /* Only assign locations for generic attributes / varyings / etc.
289 */
Paul Berry50895d42012-12-05 07:17:07 -0800290 if ((var->location >= base) && !var->explicit_location)
291 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800292
Paul Berry3e81c662012-12-05 10:47:55 -0800293 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800294 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800295 var->location_frac = 0;
296 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800297 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800298 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700299 }
300}
301
302
Ian Romanickc93b8f12010-06-17 15:20:22 -0700303/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700304 * Verify that a vertex shader executable meets all semantic requirements.
305 *
Paul Berry642e5b412012-01-04 13:57:52 -0800306 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
307 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700308 *
309 * \param shader Vertex shader executable to be verified
310 */
Paul Berryb95d2372013-07-27 11:08:31 -0700311void
Eric Anholt849e1812010-06-30 11:49:17 -0700312validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700313 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700314{
315 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700316 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700317
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700318 /* From the GLSL 1.10 spec, page 48:
319 *
320 * "The variable gl_Position is available only in the vertex
321 * language and is intended for writing the homogeneous vertex
322 * position. All executions of a well-formed vertex shader
323 * executable must write a value into this variable. [...] The
324 * variable gl_Position is available only in the vertex
325 * language and is intended for writing the homogeneous vertex
326 * position. All executions of a well-formed vertex shader
327 * executable must write a value into this variable."
328 *
329 * while in GLSL 1.40 this text is changed to:
330 *
331 * "The variable gl_Position is available only in the vertex
332 * language and is intended for writing the homogeneous vertex
333 * position. It can be written at any time during shader
334 * execution. It may also be read back by a vertex shader
335 * after being written. This value will be used by primitive
336 * assembly, clipping, culling, and other fixed functionality
337 * operations, if present, that operate on primitives after
338 * vertex processing has occurred. Its value is undefined if
339 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700340 *
341 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
342 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700343 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700344 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700345 find_assignment_visitor find("gl_Position");
346 find.run(shader->ir);
347 if (!find.variable_found()) {
348 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700349 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700350 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700351 }
352
Paul Berry642e5b412012-01-04 13:57:52 -0800353 prog->Vert.ClipDistanceArraySize = 0;
354
Paul Berry15ba2a52012-08-02 17:51:02 -0700355 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700356 /* From section 7.1 (Vertex Shader Special Variables) of the
357 * GLSL 1.30 spec:
358 *
359 * "It is an error for a shader to statically write both
360 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700361 *
362 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
363 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700364 */
365 find_assignment_visitor clip_vertex("gl_ClipVertex");
366 find_assignment_visitor clip_distance("gl_ClipDistance");
367
368 clip_vertex.run(shader->ir);
369 clip_distance.run(shader->ir);
370 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
371 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
372 "and `gl_ClipDistance'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700373 return;
Paul Berryb453ba22011-08-11 18:10:22 -0700374 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700375 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800376 ir_variable *clip_distance_var =
377 shader->symbols->get_variable("gl_ClipDistance");
378 if (clip_distance_var)
379 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700380 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700381}
382
383
Ian Romanickc93b8f12010-06-17 15:20:22 -0700384/**
385 * Verify that a fragment shader executable meets all semantic requirements
386 *
387 * \param shader Fragment shader executable to be verified
388 */
Paul Berryb95d2372013-07-27 11:08:31 -0700389void
Eric Anholt849e1812010-06-30 11:49:17 -0700390validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700391 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700392{
393 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700394 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700395
Ian Romanick832dfa52010-06-17 15:04:20 -0700396 find_assignment_visitor frag_color("gl_FragColor");
397 find_assignment_visitor frag_data("gl_FragData");
398
Eric Anholt16b68b12010-06-30 11:05:43 -0700399 frag_color.run(shader->ir);
400 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700401
Ian Romanick832dfa52010-06-17 15:04:20 -0700402 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700403 linker_error(prog, "fragment shader writes to both "
404 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700405 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700406}
407
Bryan Cain25480922013-02-15 09:46:50 -0600408/**
409 * Verify that a geometry shader executable meets all semantic requirements
410 *
411 * Also sets prog->Geom.VerticesIn as a side effect.
412 *
413 * \param shader Geometry shader executable to be verified
414 */
415void
416validate_geometry_shader_executable(struct gl_shader_program *prog,
417 struct gl_shader *shader)
418{
419 if (shader == NULL)
420 return;
421
422 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
423 prog->Geom.VerticesIn = num_vertices;
424}
425
Ian Romanick832dfa52010-06-17 15:04:20 -0700426
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700427/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700428 * Generate a string describing the mode of a variable
429 */
430static const char *
431mode_string(const ir_variable *var)
432{
433 switch (var->mode) {
434 case ir_var_auto:
435 return (var->read_only) ? "global constant" : "global variable";
436
Paul Berry42a29d82013-01-11 14:39:32 -0800437 case ir_var_uniform: return "uniform";
438 case ir_var_shader_in: return "shader input";
439 case ir_var_shader_out: return "shader output";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700440
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800441 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700442 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700443 default:
444 assert(!"Should not get here.");
445 return "invalid variable";
446 }
447}
448
449
450/**
451 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700452 */
Paul Berryb95d2372013-07-27 11:08:31 -0700453void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700454cross_validate_globals(struct gl_shader_program *prog,
455 struct gl_shader **shader_list,
456 unsigned num_shaders,
457 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700458{
459 /* Examine all of the uniforms in all of the shaders and cross validate
460 * them.
461 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700462 glsl_symbol_table variables;
463 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700464 if (shader_list[i] == NULL)
465 continue;
466
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700467 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700468 ir_variable *const var = ((ir_instruction *) node)->as_variable();
469
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700470 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700471 continue;
472
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700473 if (uniforms_only && (var->mode != ir_var_uniform))
474 continue;
475
Ian Romanick7e2aa912010-07-19 17:12:42 -0700476 /* Don't cross validate temporaries that are at global scope. These
477 * will eventually get pulled into the shaders 'main'.
478 */
479 if (var->mode == ir_var_temporary)
480 continue;
481
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700482 /* If a global with this name has already been seen, verify that the
483 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700484 * initializers, the values of the initializers must be the same.
485 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700486 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700487 if (existing != NULL) {
488 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700489 /* Consider the types to be "the same" if both types are arrays
490 * of the same type and one of the arrays is implicitly sized.
491 * In addition, set the type of the linked variable to the
492 * explicitly sized array.
493 */
494 if (var->type->is_array()
495 && existing->type->is_array()
496 && (var->type->fields.array == existing->type->fields.array)
497 && ((var->type->length == 0)
498 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800499 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700500 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800501 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700502 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700503 linker_error(prog, "%s `%s' declared as type "
504 "`%s' and type `%s'\n",
505 mode_string(var),
506 var->name, var->type->name,
507 existing->type->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700508 return;
Ian Romanicka2711d62010-08-29 22:07:49 -0700509 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700510 }
511
Ian Romanick68a4fc92010-10-07 17:21:22 -0700512 if (var->explicit_location) {
513 if (existing->explicit_location
514 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700515 linker_error(prog, "explicit locations for %s "
516 "`%s' have differing values\n",
517 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700518 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700519 }
520
521 existing->location = var->location;
522 existing->explicit_location = true;
523 }
524
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700525 /* From the GLSL 4.20 specification:
526 * "A link error will result if two compilation units in a program
527 * specify different integer-constant bindings for the same
528 * opaque-uniform name. However, it is not an error to specify a
529 * binding on some but not all declarations for the same name"
530 */
531 if (var->explicit_binding) {
532 if (existing->explicit_binding &&
533 var->binding != existing->binding) {
534 linker_error(prog, "explicit bindings for %s "
535 "`%s' have differing values\n",
536 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700537 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700538 }
539
540 existing->binding = var->binding;
541 existing->explicit_binding = true;
542 }
543
Ian Romanick46173f92011-10-31 13:07:06 -0700544 /* Validate layout qualifiers for gl_FragDepth.
545 *
546 * From the AMD/ARB_conservative_depth specs:
547 *
548 * "If gl_FragDepth is redeclared in any fragment shader in a
549 * program, it must be redeclared in all fragment shaders in
550 * that program that have static assignments to
551 * gl_FragDepth. All redeclarations of gl_FragDepth in all
552 * fragment shaders in a single program must have the same set
553 * of qualifiers."
554 */
555 if (strcmp(var->name, "gl_FragDepth") == 0) {
556 bool layout_declared = var->depth_layout != ir_depth_layout_none;
557 bool layout_differs =
558 var->depth_layout != existing->depth_layout;
559
560 if (layout_declared && layout_differs) {
561 linker_error(prog,
562 "All redeclarations of gl_FragDepth in all "
563 "fragment shaders in a single program must have "
564 "the same set of qualifiers.");
565 }
566
567 if (var->used && layout_differs) {
568 linker_error(prog,
569 "If gl_FragDepth is redeclared with a layout "
570 "qualifier in any fragment shader, it must be "
571 "redeclared with the same layout qualifier in "
572 "all fragment shaders that have assignments to "
573 "gl_FragDepth");
574 }
575 }
Chad Versaceaddae332011-01-27 01:40:31 -0800576
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700577 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
578 *
579 * "If a shared global has multiple initializers, the
580 * initializers must all be constant expressions, and they
581 * must all have the same value. Otherwise, a link error will
582 * result. (A shared global having only one initializer does
583 * not require that initializer to be a constant expression.)"
584 *
585 * Previous to 4.20 the GLSL spec simply said that initializers
586 * must have the same value. In this case of non-constant
587 * initializers, this was impossible to determine. As a result,
588 * no vendor actually implemented that behavior. The 4.20
589 * behavior matches the implemented behavior of at least one other
590 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700591 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700592 if (var->constant_initializer != NULL) {
593 if (existing->constant_initializer != NULL) {
594 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700595 linker_error(prog, "initializers for %s "
596 "`%s' have differing values\n",
597 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700598 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700599 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700600 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700601 /* If the first-seen instance of a particular uniform did not
602 * have an initializer but a later instance does, copy the
603 * initializer to the version stored in the symbol table.
604 */
Ian Romanickde415b72010-07-14 13:22:12 -0700605 /* FINISHME: This is wrong. The constant_value field should
606 * FINISHME: not be modified! Imagine a case where a shader
607 * FINISHME: without an initializer is linked in two different
608 * FINISHME: programs with shaders that have differing
609 * FINISHME: initializers. Linking with the first will
610 * FINISHME: modify the shader, and linking with the second
611 * FINISHME: will fail.
612 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700613 existing->constant_initializer =
614 var->constant_initializer->clone(ralloc_parent(existing),
615 NULL);
616 }
617 }
618
619 if (var->has_initializer) {
620 if (existing->has_initializer
621 && (var->constant_initializer == NULL
622 || existing->constant_initializer == NULL)) {
623 linker_error(prog,
624 "shared global variable `%s' has multiple "
625 "non-constant initializers.\n",
626 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700627 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700628 }
629
630 /* Some instance had an initializer, so keep track of that. In
631 * this location, all sorts of initializers (constant or
632 * otherwise) will propagate the existence to the variable
633 * stored in the symbol table.
634 */
635 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700636 }
Chad Versace7528f142010-11-17 14:34:38 -0800637
638 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700639 linker_error(prog, "declarations for %s `%s' have "
640 "mismatching invariant qualifiers\n",
641 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700642 return;
Chad Versace7528f142010-11-17 14:34:38 -0800643 }
Chad Versace61428dd2011-01-10 15:29:30 -0800644 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700645 linker_error(prog, "declarations for %s `%s' have "
646 "mismatching centroid qualifiers\n",
647 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700648 return;
Chad Versace61428dd2011-01-10 15:29:30 -0800649 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700650 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700651 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700652 }
653 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700654}
655
656
Ian Romanick37101922010-06-18 19:02:10 -0700657/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700658 * Perform validation of uniforms used across multiple shader stages
659 */
Paul Berryb95d2372013-07-27 11:08:31 -0700660void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700661cross_validate_uniforms(struct gl_shader_program *prog)
662{
Paul Berryb95d2372013-07-27 11:08:31 -0700663 cross_validate_globals(prog, prog->_LinkedShaders,
664 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700665}
666
Eric Anholtf609cf72012-04-27 13:52:56 -0700667/**
668 * Accumulates the array of prog->UniformBlocks and checks that all
669 * definitons of blocks agree on their contents.
670 */
671static bool
672interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
673{
674 unsigned max_num_uniform_blocks = 0;
675 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
676 if (prog->_LinkedShaders[i])
677 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
678 }
679
680 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
681 struct gl_shader *sh = prog->_LinkedShaders[i];
682
683 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
684 max_num_uniform_blocks);
685 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
686 prog->UniformBlockStageIndex[i][j] = -1;
687
688 if (sh == NULL)
689 continue;
690
691 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
692 int index = link_cross_validate_uniform_block(prog,
693 &prog->UniformBlocks,
694 &prog->NumUniformBlocks,
695 &sh->UniformBlocks[j]);
696
697 if (index == -1) {
698 linker_error(prog, "uniform block `%s' has mismatching definitions",
699 sh->UniformBlocks[j].Name);
700 return false;
701 }
702
703 prog->UniformBlockStageIndex[i][index] = j;
704 }
705 }
706
707 return true;
708}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700709
Ian Romanick37101922010-06-18 19:02:10 -0700710
Ian Romanick3fb87872010-07-09 14:09:34 -0700711/**
712 * Populates a shaders symbol table with all global declarations
713 */
714static void
715populate_symbol_table(gl_shader *sh)
716{
717 sh->symbols = new(sh) glsl_symbol_table;
718
719 foreach_list(node, sh->ir) {
720 ir_instruction *const inst = (ir_instruction *) node;
721 ir_variable *var;
722 ir_function *func;
723
724 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700725 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700726 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700727 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700728 }
729 }
730}
731
732
733/**
Ian Romanick31a97862010-07-12 18:48:50 -0700734 * Remap variables referenced in an instruction tree
735 *
736 * This is used when instruction trees are cloned from one shader and placed in
737 * another. These trees will contain references to \c ir_variable nodes that
738 * do not exist in the target shader. This function finds these \c ir_variable
739 * references and replaces the references with matching variables in the target
740 * shader.
741 *
742 * If there is no matching variable in the target shader, a clone of the
743 * \c ir_variable is made and added to the target shader. The new variable is
744 * added to \b both the instruction stream and the symbol table.
745 *
746 * \param inst IR tree that is to be processed.
747 * \param symbols Symbol table containing global scope symbols in the
748 * linked shader.
749 * \param instructions Instruction stream where new variable declarations
750 * should be added.
751 */
752void
Eric Anholt8273bd42010-08-04 12:34:56 -0700753remap_variables(ir_instruction *inst, struct gl_shader *target,
754 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700755{
756 class remap_visitor : public ir_hierarchical_visitor {
757 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700758 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700759 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700760 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700761 this->target = target;
762 this->symbols = target->symbols;
763 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700764 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700765 }
766
767 virtual ir_visitor_status visit(ir_dereference_variable *ir)
768 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700769 if (ir->var->mode == ir_var_temporary) {
770 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
771
772 assert(var != NULL);
773 ir->var = var;
774 return visit_continue;
775 }
776
Ian Romanick31a97862010-07-12 18:48:50 -0700777 ir_variable *const existing =
778 this->symbols->get_variable(ir->var->name);
779 if (existing != NULL)
780 ir->var = existing;
781 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700782 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700783
Eric Anholt001eee52010-11-05 06:11:24 -0700784 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700785 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700786 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700787 }
788
789 return visit_continue;
790 }
791
792 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700793 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700794 glsl_symbol_table *symbols;
795 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700796 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700797 };
798
Eric Anholt8273bd42010-08-04 12:34:56 -0700799 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700800
801 inst->accept(&v);
802}
803
804
805/**
806 * Move non-declarations from one instruction stream to another
807 *
808 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700809 * 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 -0700810 * pointer) for \c last and \c false for \c make_copies on the first
811 * call. Successive calls pass the return value of the previous call for
812 * \c last and \c true for \c make_copies.
813 *
814 * \param instructions Source instruction stream
815 * \param last Instruction after which new instructions should be
816 * inserted in the target instruction stream
817 * \param make_copies Flag selecting whether instructions in \c instructions
818 * should be copied (via \c ir_instruction::clone) into the
819 * target list or moved.
820 *
821 * \return
822 * The new "last" instruction in the target instruction stream. This pointer
823 * is suitable for use as the \c last parameter of a later call to this
824 * function.
825 */
826exec_node *
827move_non_declarations(exec_list *instructions, exec_node *last,
828 bool make_copies, gl_shader *target)
829{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700830 hash_table *temps = NULL;
831
832 if (make_copies)
833 temps = hash_table_ctor(0, hash_table_pointer_hash,
834 hash_table_pointer_compare);
835
Ian Romanick303c99f2010-07-19 12:34:56 -0700836 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700837 ir_instruction *inst = (ir_instruction *) node;
838
Ian Romanick7e2aa912010-07-19 17:12:42 -0700839 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700840 continue;
841
Ian Romanick7e2aa912010-07-19 17:12:42 -0700842 ir_variable *var = inst->as_variable();
843 if ((var != NULL) && (var->mode != ir_var_temporary))
844 continue;
845
846 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700847 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700848 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700849 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700850
851 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700852 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700853
854 if (var != NULL)
855 hash_table_insert(temps, inst, var);
856 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700857 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700858 } else {
859 inst->remove();
860 }
861
862 last->insert_after(inst);
863 last = inst;
864 }
865
Ian Romanick7e2aa912010-07-19 17:12:42 -0700866 if (make_copies)
867 hash_table_dtor(temps);
868
Ian Romanick31a97862010-07-12 18:48:50 -0700869 return last;
870}
871
872/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700873 * Get the function signature for main from a shader
874 */
875static ir_function_signature *
876get_main_function_signature(gl_shader *sh)
877{
878 ir_function *const f = sh->symbols->get_function("main");
879 if (f != NULL) {
880 exec_list void_parameters;
881
882 /* Look for the 'void main()' signature and ensure that it's defined.
883 * This keeps the linker from accidentally pick a shader that just
884 * contains a prototype for main.
885 *
886 * We don't have to check for multiple definitions of main (in multiple
887 * shaders) because that would have already been caught above.
888 */
889 ir_function_signature *sig = f->matching_signature(&void_parameters);
890 if ((sig != NULL) && sig->is_defined) {
891 return sig;
892 }
893 }
894
895 return NULL;
896}
897
898
899/**
Brian Paul84a12732012-02-02 20:10:40 -0700900 * This class is only used in link_intrastage_shaders() below but declaring
901 * it inside that function leads to compiler warnings with some versions of
902 * gcc.
903 */
904class array_sizing_visitor : public ir_hierarchical_visitor {
905public:
906 virtual ir_visitor_status visit(ir_variable *var)
907 {
908 if (var->type->is_array() && (var->type->length == 0)) {
909 const glsl_type *type =
910 glsl_type::get_array_instance(var->type->fields.array,
911 var->max_array_access + 1);
912 assert(type != NULL);
913 var->type = type;
914 }
915 return visit_continue;
916 }
917};
918
Brian Paul84a12732012-02-02 20:10:40 -0700919/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700920 * Combine a group of shaders for a single stage to generate a linked shader
921 *
922 * \note
923 * If this function is supplied a single shader, it is cloned, and the new
924 * shader is returned.
925 */
926static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800927link_intrastage_shaders(void *mem_ctx,
928 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700929 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700930 struct gl_shader **shader_list,
931 unsigned num_shaders)
932{
Eric Anholtf609cf72012-04-27 13:52:56 -0700933 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -0700934
Ian Romanick13f782c2010-06-29 18:53:38 -0700935 /* Check that global variables defined in multiple shaders are consistent.
936 */
Paul Berryb95d2372013-07-27 11:08:31 -0700937 cross_validate_globals(prog, shader_list, num_shaders, false);
938 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -0700939 return NULL;
940
Jordan Justen4a0bcd92013-05-20 23:42:49 -0700941 /* Check that interface blocks defined in multiple shaders are consistent.
942 */
Paul Berryb95d2372013-07-27 11:08:31 -0700943 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
944 num_shaders);
945 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -0700946 return NULL;
947
Paul Berry4682b9b2013-07-27 15:07:08 -0700948 /* Link up uniform blocks defined within this stage. */
949 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -0500950 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
951 &uniform_blocks);
Eric Anholtf609cf72012-04-27 13:52:56 -0700952
Ian Romanick13f782c2010-06-29 18:53:38 -0700953 /* Check that there is only a single definition of each function signature
954 * across all shaders.
955 */
956 for (unsigned i = 0; i < (num_shaders - 1); i++) {
957 foreach_list(node, shader_list[i]->ir) {
958 ir_function *const f = ((ir_instruction *) node)->as_function();
959
960 if (f == NULL)
961 continue;
962
963 for (unsigned j = i + 1; j < num_shaders; j++) {
964 ir_function *const other =
965 shader_list[j]->symbols->get_function(f->name);
966
967 /* If the other shader has no function (and therefore no function
968 * signatures) with the same name, skip to the next shader.
969 */
970 if (other == NULL)
971 continue;
972
973 foreach_iter (exec_list_iterator, iter, *f) {
974 ir_function_signature *sig =
975 (ir_function_signature *) iter.get();
976
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700977 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700978 continue;
979
980 ir_function_signature *other_sig =
981 other->exact_matching_signature(& sig->parameters);
982
983 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700984 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -0700985 linker_error(prog, "function `%s' is multiply defined",
986 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -0700987 return NULL;
988 }
989 }
990 }
991 }
992 }
993
994 /* Find the shader that defines main, and make a clone of it.
995 *
996 * Starting with the clone, search for undefined references. If one is
997 * found, find the shader that defines it. Clone the reference and add
998 * it to the shader. Repeat until there are no undefined references or
999 * until a reference cannot be resolved.
1000 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001001 gl_shader *main = NULL;
1002 for (unsigned i = 0; i < num_shaders; i++) {
1003 if (get_main_function_signature(shader_list[i]) != NULL) {
1004 main = shader_list[i];
1005 break;
1006 }
1007 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001008
Ian Romanick15ce87e2010-07-09 15:28:22 -07001009 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001010 linker_error(prog, "%s shader lacks `main'\n",
Eric Anholtfaf3dba2013-06-12 16:57:11 -07001011 _mesa_glsl_shader_target_name(shader_list[0]->Type));
Ian Romanick15ce87e2010-07-09 15:28:22 -07001012 return NULL;
1013 }
1014
Ian Romanick4a455952010-10-13 15:13:02 -07001015 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001016 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001017 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001018
Eric Anholtf609cf72012-04-27 13:52:56 -07001019 linked->UniformBlocks = uniform_blocks;
1020 linked->NumUniformBlocks = num_uniform_blocks;
1021 ralloc_steal(linked, linked->UniformBlocks);
1022
Ian Romanick15ce87e2010-07-09 15:28:22 -07001023 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001024
Ian Romanick31a97862010-07-12 18:48:50 -07001025 /* The a pointer to the main function in the final linked shader (i.e., the
1026 * copy of the original shader that contained the main function).
1027 */
1028 ir_function_signature *const main_sig = get_main_function_signature(linked);
1029
1030 /* Move any instructions other than variable declarations or function
1031 * declarations into main.
1032 */
Ian Romanick9303e352010-07-19 12:33:54 -07001033 exec_node *insertion_point =
1034 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1035 linked);
1036
Ian Romanick31a97862010-07-12 18:48:50 -07001037 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001038 if (shader_list[i] == main)
1039 continue;
1040
Ian Romanick31a97862010-07-12 18:48:50 -07001041 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001042 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001043 }
1044
Ian Romanick13f782c2010-06-29 18:53:38 -07001045 /* Resolve initializers for global variables in the linked shader.
1046 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001047 unsigned num_linking_shaders = num_shaders;
1048 for (unsigned i = 0; i < num_shaders; i++)
1049 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1050
1051 gl_shader **linking_shaders =
1052 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1053
1054 memcpy(linking_shaders, shader_list,
1055 sizeof(linking_shaders[0]) * num_shaders);
1056
1057 unsigned idx = num_shaders;
1058 for (unsigned i = 0; i < num_shaders; i++) {
1059 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1060 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1061 idx += shader_list[i]->num_builtins_to_link;
1062 }
1063
1064 assert(idx == num_linking_shaders);
1065
Ian Romanick4a455952010-10-13 15:13:02 -07001066 if (!link_function_calls(prog, linked, linking_shaders,
1067 num_linking_shaders)) {
1068 ctx->Driver.DeleteShader(ctx, linked);
1069 linked = NULL;
1070 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001071
1072 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001073
Paul Berryc148ef62011-08-03 15:37:01 -07001074 /* At this point linked should contain all of the linked IR, so
1075 * validate it to make sure nothing went wrong.
1076 */
1077 if (linked)
1078 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07001079
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001080 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001081 * unspecified sizes have a size specified. The size is inferred from the
1082 * max_array_access field.
1083 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001084 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001085 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001086
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001087 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001088 }
1089
Ian Romanick3fb87872010-07-09 14:09:34 -07001090 return linked;
1091}
1092
Eric Anholta721abf2010-08-23 10:32:01 -07001093/**
1094 * Update the sizes of linked shader uniform arrays to the maximum
1095 * array index used.
1096 *
1097 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1098 *
1099 * If one or more elements of an array are active,
1100 * GetActiveUniform will return the name of the array in name,
1101 * subject to the restrictions listed above. The type of the array
1102 * is returned in type. The size parameter contains the highest
1103 * array element index used, plus one. The compiler or linker
1104 * determines the highest index used. There will be only one
1105 * active uniform reported by the GL per uniform array.
1106
1107 */
1108static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001109update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001110{
Ian Romanick3322fba2010-10-14 13:28:42 -07001111 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1112 if (prog->_LinkedShaders[i] == NULL)
1113 continue;
1114
Eric Anholta721abf2010-08-23 10:32:01 -07001115 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1116 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1117
Paul Berry6a2baf32013-06-10 14:01:45 -07001118 if ((var == NULL) || (var->mode != ir_var_uniform) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001119 !var->type->is_array())
1120 continue;
1121
Eric Anholt9feb4032012-05-01 14:43:31 -07001122 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1123 * will not be eliminated. Since we always do std140, just
1124 * don't resize arrays in UBOs.
1125 */
Ian Romanick13be1f42012-12-14 12:00:14 -08001126 if (var->is_in_uniform_block())
Eric Anholt9feb4032012-05-01 14:43:31 -07001127 continue;
1128
Eric Anholta721abf2010-08-23 10:32:01 -07001129 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001130 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1131 if (prog->_LinkedShaders[j] == NULL)
1132 continue;
1133
Eric Anholta721abf2010-08-23 10:32:01 -07001134 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1135 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1136 if (!other_var)
1137 continue;
1138
1139 if (strcmp(var->name, other_var->name) == 0 &&
1140 other_var->max_array_access > size) {
1141 size = other_var->max_array_access;
1142 }
1143 }
1144 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001145
Fabian Bieler63684782013-06-14 13:37:07 +02001146 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001147 /* If this is a built-in uniform (i.e., it's backed by some
1148 * fixed-function state), adjust the number of state slots to
1149 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001150 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001151 * slots is an integer multiple of the number of array elements.
1152 * Determine the number of slots per array element by dividing by
1153 * the old (total) size.
1154 */
1155 if (var->num_state_slots > 0) {
1156 var->num_state_slots = (size + 1)
1157 * (var->num_state_slots / var->type->length);
1158 }
1159
Eric Anholta721abf2010-08-23 10:32:01 -07001160 var->type = glsl_type::get_array_instance(var->type->fields.array,
1161 size + 1);
1162 /* FINISHME: We should update the types of array
1163 * dereferences of this variable now.
1164 */
1165 }
1166 }
1167 }
1168}
1169
Ian Romanick69846702010-06-22 17:29:19 -07001170/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001171 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001172 *
1173 * \param used_mask Bits representing used (1) and unused (0) locations
1174 * \param needed_count Number of contiguous bits needed.
1175 *
1176 * \return
1177 * Base location of the available bits on success or -1 on failure.
1178 */
1179int
1180find_available_slots(unsigned used_mask, unsigned needed_count)
1181{
1182 unsigned needed_mask = (1 << needed_count) - 1;
1183 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1184
1185 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1186 * cannot optimize possibly infinite loops" for the loop below.
1187 */
1188 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1189 return -1;
1190
1191 for (int i = 0; i <= max_bit_to_test; i++) {
1192 if ((needed_mask & ~used_mask) == needed_mask)
1193 return i;
1194
1195 needed_mask <<= 1;
1196 }
1197
1198 return -1;
1199}
1200
1201
Ian Romanickd32d4f72011-06-27 17:59:58 -07001202/**
1203 * Assign locations for either VS inputs for FS outputs
1204 *
1205 * \param prog Shader program whose variables need locations assigned
1206 * \param target_index Selector for the program target to receive location
1207 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1208 * \c MESA_SHADER_FRAGMENT.
1209 * \param max_index Maximum number of generic locations. This corresponds
1210 * to either the maximum number of draw buffers or the
1211 * maximum number of generic attributes.
1212 *
1213 * \return
1214 * If locations are successfully assigned, true is returned. Otherwise an
1215 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001216 */
Ian Romanick69846702010-06-22 17:29:19 -07001217bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001218assign_attribute_or_color_locations(gl_shader_program *prog,
1219 unsigned target_index,
1220 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001221{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001222 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001223 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001224 unsigned used_locations = (max_index >= 32)
1225 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001226
Ian Romanickd32d4f72011-06-27 17:59:58 -07001227 assert((target_index == MESA_SHADER_VERTEX)
1228 || (target_index == MESA_SHADER_FRAGMENT));
1229
1230 gl_shader *const sh = prog->_LinkedShaders[target_index];
1231 if (sh == NULL)
1232 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001233
Ian Romanick69846702010-06-22 17:29:19 -07001234 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001235 *
1236 * 1. Invalidate the location assignments for all vertex shader inputs.
1237 *
1238 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001239 * glBindVertexAttribLocation) locations and outputs that have
1240 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001241 *
Ian Romanick69846702010-06-22 17:29:19 -07001242 * 3. Sort the attributes without assigned locations by number of slots
1243 * required in decreasing order. Fragmentation caused by attribute
1244 * locations assigned by the application may prevent large attributes
1245 * from having enough contiguous space.
1246 *
1247 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001248 */
1249
Ian Romanickd32d4f72011-06-27 17:59:58 -07001250 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001251 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001252
Ian Romanickd32d4f72011-06-27 17:59:58 -07001253 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001254 (target_index == MESA_SHADER_VERTEX)
1255 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001256
1257
Ian Romanick69846702010-06-22 17:29:19 -07001258 /* Temporary storage for the set of attributes that need locations assigned.
1259 */
1260 struct temp_attr {
1261 unsigned slots;
1262 ir_variable *var;
1263
1264 /* Used below in the call to qsort. */
1265 static int compare(const void *a, const void *b)
1266 {
1267 const temp_attr *const l = (const temp_attr *) a;
1268 const temp_attr *const r = (const temp_attr *) b;
1269
1270 /* Reversed because we want a descending order sort below. */
1271 return r->slots - l->slots;
1272 }
1273 } to_assign[16];
1274
1275 unsigned num_attr = 0;
1276
Eric Anholt16b68b12010-06-30 11:05:43 -07001277 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001278 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1279
Brian Paul4470ff22011-07-19 21:10:25 -06001280 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001281 continue;
1282
Ian Romanick68a4fc92010-10-07 17:21:22 -07001283 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001284 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001285 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001286 linker_error(prog,
1287 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001288 (var->location < 0)
1289 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001290 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001291 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001292 }
1293 } else if (target_index == MESA_SHADER_VERTEX) {
1294 unsigned binding;
1295
1296 if (prog->AttributeBindings->get(binding, var->name)) {
1297 assert(binding >= VERT_ATTRIB_GENERIC0);
1298 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001299 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001300 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001301 } else if (target_index == MESA_SHADER_FRAGMENT) {
1302 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001303 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001304
1305 if (prog->FragDataBindings->get(binding, var->name)) {
1306 assert(binding >= FRAG_RESULT_DATA0);
1307 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001308 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001309
1310 if (prog->FragDataIndexBindings->get(index, var->name)) {
1311 var->index = index;
1312 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001313 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001314 }
1315
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001316 /* If the variable is not a built-in and has a location statically
1317 * assigned in the shader (presumably via a layout qualifier), make sure
1318 * that it doesn't collide with other assigned locations. Otherwise,
1319 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001320 */
Paul Berry0026ad42013-07-31 08:15:08 -07001321 const unsigned slots = var->type->count_attribute_slots();
Ian Romanick523b6112011-08-17 15:40:03 -07001322 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001323 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001324 /* From page 61 of the OpenGL 4.0 spec:
1325 *
1326 * "LinkProgram will fail if the attribute bindings assigned
1327 * by BindAttribLocation do not leave not enough space to
1328 * assign a location for an active matrix attribute or an
1329 * active attribute array, both of which require multiple
1330 * contiguous generic attributes."
1331 *
1332 * Previous versions of the spec contain similar language but omit
1333 * the bit about attribute arrays.
1334 *
1335 * Page 61 of the OpenGL 4.0 spec also says:
1336 *
1337 * "It is possible for an application to bind more than one
1338 * attribute name to the same location. This is referred to as
1339 * aliasing. This will only work if only one of the aliased
1340 * attributes is active in the executable program, or if no
1341 * path through the shader consumes more than one attribute of
1342 * a set of attributes aliased to the same location. A link
1343 * error can occur if the linker determines that every path
1344 * through the shader consumes multiple aliased attributes,
1345 * but implementations are not required to generate an error
1346 * in this case."
1347 *
1348 * These two paragraphs are either somewhat contradictory, or I
1349 * don't fully understand one or both of them.
1350 */
1351 /* FINISHME: The code as currently written does not support
1352 * FINISHME: attribute location aliasing (see comment above).
1353 */
1354 /* Mask representing the contiguous slots that will be used by
1355 * this attribute.
1356 */
1357 const unsigned attr = var->location - generic_base;
1358 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001359
Ian Romanick523b6112011-08-17 15:40:03 -07001360 /* Generate a link error if the set of bits requested for this
1361 * attribute overlaps any previously allocated bits.
1362 */
1363 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001364 const char *const string = (target_index == MESA_SHADER_VERTEX)
1365 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001366 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001367 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001368 "available for %s `%s' %d %d %d", string,
1369 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001370 return false;
1371 }
1372
1373 used_locations |= (use_mask << attr);
1374 }
1375
1376 continue;
1377 }
1378
1379 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001380 to_assign[num_attr].var = var;
1381 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001382 }
Ian Romanick69846702010-06-22 17:29:19 -07001383
1384 /* If all of the attributes were assigned locations by the application (or
1385 * are built-in attributes with fixed locations), return early. This should
1386 * be the common case.
1387 */
1388 if (num_attr == 0)
1389 return true;
1390
1391 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1392
Ian Romanickd32d4f72011-06-27 17:59:58 -07001393 if (target_index == MESA_SHADER_VERTEX) {
1394 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1395 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1396 * reserved to prevent it from being automatically allocated below.
1397 */
1398 find_deref_visitor find("gl_Vertex");
1399 find.run(sh->ir);
1400 if (find.variable_found())
1401 used_locations |= (1 << 0);
1402 }
Ian Romanick982e3792010-06-29 18:58:20 -07001403
Ian Romanick69846702010-06-22 17:29:19 -07001404 for (unsigned i = 0; i < num_attr; i++) {
1405 /* Mask representing the contiguous slots that will be used by this
1406 * attribute.
1407 */
1408 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1409
1410 int location = find_available_slots(used_locations, to_assign[i].slots);
1411
1412 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001413 const char *const string = (target_index == MESA_SHADER_VERTEX)
1414 ? "vertex shader input" : "fragment shader output";
1415
Ian Romanick586e7412011-07-28 14:04:09 -07001416 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001417 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001418 "available for %s `%s'",
1419 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001420 return false;
1421 }
1422
Ian Romanickd32d4f72011-06-27 17:59:58 -07001423 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001424 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001425 used_locations |= (use_mask << location);
1426 }
1427
1428 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001429}
1430
1431
Ian Romanick40e114b2010-08-17 14:55:50 -07001432/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001433 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001434 */
1435void
Ian Romanickcc90e622010-10-19 17:59:10 -07001436demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001437{
1438 foreach_list(node, sh->ir) {
1439 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1440
Ian Romanickcc90e622010-10-19 17:59:10 -07001441 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001442 continue;
1443
Ian Romanickcc90e622010-10-19 17:59:10 -07001444 /* A shader 'in' or 'out' variable is only really an input or output if
1445 * its value is used by other shader stages. This will cause the variable
1446 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001447 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001448 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001449 var->mode = ir_var_auto;
1450 }
1451 }
1452}
1453
1454
Paul Berry871ddb92011-11-05 11:17:32 -07001455/**
Marek Olšákec174a42011-11-18 15:00:10 +01001456 * Store the gl_FragDepth layout in the gl_shader_program struct.
1457 */
1458static void
1459store_fragdepth_layout(struct gl_shader_program *prog)
1460{
1461 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1462 return;
1463 }
1464
1465 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1466
1467 /* We don't look up the gl_FragDepth symbol directly because if
1468 * gl_FragDepth is not used in the shader, it's removed from the IR.
1469 * However, the symbol won't be removed from the symbol table.
1470 *
1471 * We're only interested in the cases where the variable is NOT removed
1472 * from the IR.
1473 */
1474 foreach_list(node, ir) {
1475 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1476
Paul Berry42a29d82013-01-11 14:39:32 -08001477 if (var == NULL || var->mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001478 continue;
1479 }
1480
1481 if (strcmp(var->name, "gl_FragDepth") == 0) {
1482 switch (var->depth_layout) {
1483 case ir_depth_layout_none:
1484 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1485 return;
1486 case ir_depth_layout_any:
1487 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1488 return;
1489 case ir_depth_layout_greater:
1490 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1491 return;
1492 case ir_depth_layout_less:
1493 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1494 return;
1495 case ir_depth_layout_unchanged:
1496 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1497 return;
1498 default:
1499 assert(0);
1500 return;
1501 }
1502 }
1503 }
1504}
1505
1506/**
Ian Romanick92f81592011-11-08 12:37:19 -08001507 * Validate the resources used by a program versus the implementation limits
1508 */
Paul Berryb95d2372013-07-27 11:08:31 -07001509static void
Ian Romanick92f81592011-11-08 12:37:19 -08001510check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1511{
1512 static const char *const shader_names[MESA_SHADER_TYPES] = {
Marek Olšák030ca232013-06-12 17:15:46 +02001513 "vertex", "geometry", "fragment"
Ian Romanick92f81592011-11-08 12:37:19 -08001514 };
1515
1516 const unsigned max_samplers[MESA_SHADER_TYPES] = {
Marek Olšák5e784332013-05-02 02:30:44 +02001517 ctx->Const.VertexProgram.MaxTextureImageUnits,
Marek Olšák030ca232013-06-12 17:15:46 +02001518 ctx->Const.GeometryProgram.MaxTextureImageUnits,
1519 ctx->Const.FragmentProgram.MaxTextureImageUnits
Ian Romanick92f81592011-11-08 12:37:19 -08001520 };
1521
Eric Anholt38e77e52013-05-23 11:10:15 -07001522 const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = {
Ian Romanick92f81592011-11-08 12:37:19 -08001523 ctx->Const.VertexProgram.MaxUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001524 ctx->Const.GeometryProgram.MaxUniformComponents,
1525 ctx->Const.FragmentProgram.MaxUniformComponents
Ian Romanick92f81592011-11-08 12:37:19 -08001526 };
1527
Eric Anholt38e77e52013-05-23 11:10:15 -07001528 const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = {
1529 ctx->Const.VertexProgram.MaxCombinedUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001530 ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
1531 ctx->Const.FragmentProgram.MaxCombinedUniformComponents
Eric Anholt38e77e52013-05-23 11:10:15 -07001532 };
1533
Eric Anholt877a8972012-06-25 12:47:01 -07001534 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
1535 ctx->Const.VertexProgram.MaxUniformBlocks,
Eric Anholt877a8972012-06-25 12:47:01 -07001536 ctx->Const.GeometryProgram.MaxUniformBlocks,
Marek Olšák030ca232013-06-12 17:15:46 +02001537 ctx->Const.FragmentProgram.MaxUniformBlocks
Eric Anholt877a8972012-06-25 12:47:01 -07001538 };
1539
Ian Romanick92f81592011-11-08 12:37:19 -08001540 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1541 struct gl_shader *sh = prog->_LinkedShaders[i];
1542
1543 if (sh == NULL)
1544 continue;
1545
1546 if (sh->num_samplers > max_samplers[i]) {
1547 linker_error(prog, "Too many %s shader texture samplers",
1548 shader_names[i]);
1549 }
1550
Eric Anholt38e77e52013-05-23 11:10:15 -07001551 if (sh->num_uniform_components > max_default_uniform_components[i]) {
1552 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1553 linker_warning(prog, "Too many %s shader default uniform block "
1554 "components, but the driver will try to optimize "
1555 "them out; this is non-portable out-of-spec "
1556 "behavior\n",
1557 shader_names[i]);
1558 } else {
1559 linker_error(prog, "Too many %s shader default uniform block "
1560 "components",
1561 shader_names[i]);
1562 }
1563 }
1564
1565 if (sh->num_combined_uniform_components >
1566 max_combined_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001567 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1568 linker_warning(prog, "Too many %s shader uniform components, "
1569 "but the driver will try to optimize them out; "
1570 "this is non-portable out-of-spec behavior\n",
1571 shader_names[i]);
1572 } else {
1573 linker_error(prog, "Too many %s shader uniform components",
1574 shader_names[i]);
1575 }
Ian Romanick92f81592011-11-08 12:37:19 -08001576 }
1577 }
1578
Eric Anholt877a8972012-06-25 12:47:01 -07001579 unsigned blocks[MESA_SHADER_TYPES] = {0};
1580 unsigned total_uniform_blocks = 0;
1581
1582 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
1583 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1584 if (prog->UniformBlockStageIndex[j][i] != -1) {
1585 blocks[j]++;
1586 total_uniform_blocks++;
1587 }
1588 }
1589
1590 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1591 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1592 prog->NumUniformBlocks,
1593 ctx->Const.MaxCombinedUniformBlocks);
1594 } else {
1595 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1596 if (blocks[i] > max_uniform_blocks[i]) {
1597 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
1598 shader_names[i],
1599 blocks[i],
1600 max_uniform_blocks[i]);
1601 break;
1602 }
1603 }
1604 }
1605 }
Ian Romanick92f81592011-11-08 12:37:19 -08001606}
Paul Berry871ddb92011-11-05 11:17:32 -07001607
Ian Romanick0e59b262010-06-23 11:23:01 -07001608void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001609link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001610{
Paul Berry871ddb92011-11-05 11:17:32 -07001611 tfeedback_decl *tfeedback_decls = NULL;
1612 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1613
Kenneth Graunked3073f52011-01-21 14:32:31 -08001614 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001615
Paul Berryb95d2372013-07-27 11:08:31 -07001616 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07001617 prog->Validated = false;
1618 prog->_Used = false;
1619
Eric Anholtf609cf72012-04-27 13:52:56 -07001620 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08001621 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07001622
Eric Anholtf609cf72012-04-27 13:52:56 -07001623 ralloc_free(prog->UniformBlocks);
1624 prog->UniformBlocks = NULL;
1625 prog->NumUniformBlocks = 0;
1626 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
1627 ralloc_free(prog->UniformBlockStageIndex[i]);
1628 prog->UniformBlockStageIndex[i] = NULL;
1629 }
1630
Ian Romanick832dfa52010-06-17 15:04:20 -07001631 /* Separate the shaders into groups based on their type.
1632 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001633 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001634 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001635 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001636 unsigned num_frag_shaders = 0;
Bryan Cain25480922013-02-15 09:46:50 -06001637 struct gl_shader **geom_shader_list;
1638 unsigned num_geom_shaders = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001639
Eric Anholt16b68b12010-06-30 11:05:43 -07001640 vert_shader_list = (struct gl_shader **)
Paul Berry844bd712013-07-30 22:38:43 -07001641 calloc(prog->NumShaders, sizeof(struct gl_shader *));
1642 frag_shader_list = (struct gl_shader **)
1643 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Bryan Cain25480922013-02-15 09:46:50 -06001644 geom_shader_list = (struct gl_shader **)
1645 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001646
Ian Romanick25f51d32010-07-16 15:51:50 -07001647 unsigned min_version = UINT_MAX;
1648 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07001649 const bool is_es_prog =
1650 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07001651 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001652 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1653 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1654
Paul Berrya9f34dc2012-08-02 17:49:44 -07001655 if (prog->Shaders[i]->IsES != is_es_prog) {
1656 linker_error(prog, "all shaders must use same shading "
1657 "language version\n");
1658 goto done;
1659 }
1660
Ian Romanick832dfa52010-06-17 15:04:20 -07001661 switch (prog->Shaders[i]->Type) {
1662 case GL_VERTEX_SHADER:
1663 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1664 num_vert_shaders++;
1665 break;
1666 case GL_FRAGMENT_SHADER:
1667 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1668 num_frag_shaders++;
1669 break;
1670 case GL_GEOMETRY_SHADER:
Bryan Cain25480922013-02-15 09:46:50 -06001671 geom_shader_list[num_geom_shaders] = prog->Shaders[i];
1672 num_geom_shaders++;
Ian Romanick832dfa52010-06-17 15:04:20 -07001673 break;
1674 }
1675 }
1676
Ian Romanick25f51d32010-07-16 15:51:50 -07001677 /* Previous to GLSL version 1.30, different compilation units could mix and
1678 * match shading language versions. With GLSL 1.30 and later, the versions
1679 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07001680 *
1681 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07001682 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07001683 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001684 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07001685 linker_error(prog, "all shaders must use same shading "
1686 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07001687 goto done;
1688 }
1689
1690 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07001691 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07001692
Fabian Bielerbd85ba02013-05-24 23:26:54 +02001693 /* Geometry shaders have to be linked with vertex shaders.
1694 */
1695 if (num_geom_shaders > 0 && num_vert_shaders == 0) {
1696 linker_error(prog, "Geometry shader must be linked with "
1697 "vertex shader\n");
1698 goto done;
1699 }
1700
Ian Romanick3322fba2010-10-14 13:28:42 -07001701 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1702 if (prog->_LinkedShaders[i] != NULL)
1703 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1704
1705 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07001706 }
1707
Ian Romanickcd6764e2010-07-16 16:00:07 -07001708 /* Link all shaders for a particular stage and validate the result.
1709 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001710 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001711 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001712 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1713 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001714
Paul Berryb95d2372013-07-27 11:08:31 -07001715 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07001716 goto done;
1717
Paul Berryb95d2372013-07-27 11:08:31 -07001718 validate_vertex_shader_executable(prog, sh);
1719 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001720 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001721
Ian Romanick3322fba2010-10-14 13:28:42 -07001722 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1723 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001724 }
1725
1726 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001727 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001728 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1729 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001730
Paul Berryb95d2372013-07-27 11:08:31 -07001731 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07001732 goto done;
1733
Paul Berryb95d2372013-07-27 11:08:31 -07001734 validate_fragment_shader_executable(prog, sh);
1735 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001736 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001737
Ian Romanick3322fba2010-10-14 13:28:42 -07001738 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1739 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001740 }
1741
Bryan Cain25480922013-02-15 09:46:50 -06001742 if (num_geom_shaders > 0) {
1743 gl_shader *const sh =
1744 link_intrastage_shaders(mem_ctx, ctx, prog, geom_shader_list,
1745 num_geom_shaders);
1746
1747 if (!prog->LinkStatus)
1748 goto done;
1749
1750 validate_geometry_shader_executable(prog, sh);
1751 if (!prog->LinkStatus)
1752 goto done;
1753
1754 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
1755 sh);
1756 }
1757
Ian Romanick3ed850e2010-06-23 12:18:21 -07001758 /* Here begins the inter-stage linking phase. Some initial validation is
1759 * performed, then locations are assigned for uniforms, attributes, and
1760 * varyings.
1761 */
Paul Berryb95d2372013-07-27 11:08:31 -07001762 cross_validate_uniforms(prog);
1763 if (!prog->LinkStatus)
1764 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001765
Paul Berryb95d2372013-07-27 11:08:31 -07001766 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07001767
Paul Berryb95d2372013-07-27 11:08:31 -07001768 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1769 if (prog->_LinkedShaders[prev] != NULL)
1770 break;
1771 }
Ian Romanick3322fba2010-10-14 13:28:42 -07001772
Paul Berryb95d2372013-07-27 11:08:31 -07001773 /* Validate the inputs of each stage with the output of the preceding
1774 * stage.
1775 */
1776 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1777 if (prog->_LinkedShaders[i] == NULL)
1778 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07001779
Paul Berryb95d2372013-07-27 11:08:31 -07001780 validate_interstage_interface_blocks(prog, prog->_LinkedShaders[prev],
1781 prog->_LinkedShaders[i]);
1782 if (!prog->LinkStatus)
1783 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001784
Paul Berryb95d2372013-07-27 11:08:31 -07001785 cross_validate_outputs_to_inputs(prog,
1786 prog->_LinkedShaders[prev],
1787 prog->_LinkedShaders[i]);
1788 if (!prog->LinkStatus)
1789 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07001790
Paul Berryb95d2372013-07-27 11:08:31 -07001791 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07001792 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001793
Jordan Justen5ebf5472013-03-10 03:20:03 -07001794
1795 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1796 if (prog->_LinkedShaders[i] != NULL)
1797 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
1798 }
1799
Eric Anholt3de13952012-05-04 13:08:46 -07001800 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
1801 * it before optimization because we want most of the checks to get
1802 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07001803 *
1804 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07001805 */
Paul Berry15ba2a52012-08-02 17:51:02 -07001806 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07001807 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1808 if (sh) {
1809 lower_discard_flow(sh->ir);
1810 }
1811 }
1812
Eric Anholtf609cf72012-04-27 13:52:56 -07001813 if (!interstage_cross_validate_uniform_blocks(prog))
1814 goto done;
1815
Eric Anholt2f4fe152010-08-10 13:06:49 -07001816 /* Do common optimization before assigning storage for attributes,
1817 * uniforms, and varyings. Later optimization could possibly make
1818 * some of that unused.
1819 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001820 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1821 if (prog->_LinkedShaders[i] == NULL)
1822 continue;
1823
Ian Romanick02c5ae12011-07-11 10:46:01 -07001824 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
1825 if (!prog->LinkStatus)
1826 goto done;
1827
Paul Berry18392442012-12-04 11:11:02 -08001828 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
1829 lower_clip_distance(prog->_LinkedShaders[i]);
1830 }
Paul Berryc06e3252011-08-11 20:58:21 -07001831
Brian Paul7feabfe2012-03-20 17:43:12 -06001832 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
1833
Kenneth Graunkeb7657402013-04-17 17:30:22 -07001834 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
Eric Anholt2f4fe152010-08-10 13:06:49 -07001835 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001836 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001837
Paul Berry50895d42012-12-05 07:17:07 -08001838 /* Mark all generic shader inputs and outputs as unpaired. */
1839 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1840 link_invalidate_variable_locations(
1841 prog->_LinkedShaders[MESA_SHADER_VERTEX],
Paul Berry36b252e2013-02-23 07:22:01 -08001842 VERT_ATTRIB_GENERIC0, VARYING_SLOT_VAR0);
Paul Berry50895d42012-12-05 07:17:07 -08001843 }
Bryan Cain25480922013-02-15 09:46:50 -06001844 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
1845 link_invalidate_variable_locations(
1846 prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
1847 VARYING_SLOT_VAR0, VARYING_SLOT_VAR0);
1848 }
Paul Berry50895d42012-12-05 07:17:07 -08001849 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
1850 link_invalidate_variable_locations(
1851 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
Paul Berryeed6baf2013-02-23 09:00:58 -08001852 VARYING_SLOT_VAR0, FRAG_RESULT_DATA0);
Paul Berry50895d42012-12-05 07:17:07 -08001853 }
1854
Ian Romanickd32d4f72011-06-27 17:59:58 -07001855 /* FINISHME: The value of the max_attribute_index parameter is
1856 * FINISHME: implementation dependent based on the value of
1857 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1858 * FINISHME: at least 16, so hardcode 16 for now.
1859 */
1860 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001861 goto done;
1862 }
1863
Dave Airlie1256a5d2012-03-24 13:33:41 +00001864 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001865 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07001866 }
1867
Marek Olšák284d9542013-06-12 02:18:09 +02001868 unsigned first;
1869 for (first = 0; first < MESA_SHADER_TYPES; first++) {
1870 if (prog->_LinkedShaders[first] != NULL)
Ian Romanick3322fba2010-10-14 13:28:42 -07001871 break;
1872 }
1873
Paul Berry871ddb92011-11-05 11:17:32 -07001874 if (num_tfeedback_decls != 0) {
1875 /* From GL_EXT_transform_feedback:
1876 * A program will fail to link if:
1877 *
1878 * * the <count> specified by TransformFeedbackVaryingsEXT is
1879 * non-zero, but the program object has no vertex or geometry
1880 * shader;
1881 */
Bryan Cain25480922013-02-15 09:46:50 -06001882 if (first == MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07001883 linker_error(prog, "Transform feedback varyings specified, but "
1884 "no vertex or geometry shader is present.");
1885 goto done;
1886 }
1887
1888 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
1889 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08001890 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07001891 prog->TransformFeedback.VaryingNames,
1892 tfeedback_decls))
1893 goto done;
1894 }
1895
Marek Olšák284d9542013-06-12 02:18:09 +02001896 /* Linking the stages in the opposite order (from fragment to vertex)
1897 * ensures that inter-shader outputs written to in an earlier stage are
1898 * eliminated if they are (transitively) not used in a later stage.
1899 */
1900 int last, next;
1901 for (last = MESA_SHADER_TYPES-1; last >= 0; last--) {
1902 if (prog->_LinkedShaders[last] != NULL)
1903 break;
Ian Romanick3322fba2010-10-14 13:28:42 -07001904 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001905
Marek Olšák284d9542013-06-12 02:18:09 +02001906 if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
1907 gl_shader *const sh = prog->_LinkedShaders[last];
1908
1909 if (num_tfeedback_decls != 0) {
1910 /* There was no fragment shader, but we still have to assign varying
1911 * locations for use by transform feedback.
1912 */
1913 if (!assign_varying_locations(ctx, mem_ctx, prog,
1914 sh, NULL,
Paul Berry3b0cf702013-04-10 06:48:42 -07001915 num_tfeedback_decls, tfeedback_decls,
1916 0))
Marek Olšák284d9542013-06-12 02:18:09 +02001917 goto done;
1918 }
1919
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02001920 do_dead_builtin_varyings(ctx, sh->ir, NULL,
1921 num_tfeedback_decls, tfeedback_decls);
1922
Marek Olšák284d9542013-06-12 02:18:09 +02001923 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
1924
1925 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07001926 */
Marek Olšák284d9542013-06-12 02:18:09 +02001927 while (do_dead_code(sh->ir, false))
1928 ;
1929 }
1930 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02001931 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02001932 */
1933 gl_shader *const sh = prog->_LinkedShaders[first];
1934
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02001935 do_dead_builtin_varyings(ctx, NULL, sh->ir,
1936 num_tfeedback_decls, tfeedback_decls);
1937
Marek Olšák284d9542013-06-12 02:18:09 +02001938 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
1939
1940 while (do_dead_code(sh->ir, false))
1941 ;
1942 }
1943
1944 next = last;
1945 for (int i = next - 1; i >= 0; i--) {
1946 if (prog->_LinkedShaders[i] == NULL)
1947 continue;
1948
1949 gl_shader *const sh_i = prog->_LinkedShaders[i];
1950 gl_shader *const sh_next = prog->_LinkedShaders[next];
Paul Berry3b0cf702013-04-10 06:48:42 -07001951 unsigned gs_input_vertices =
1952 next == MESA_SHADER_GEOMETRY ? prog->Geom.VerticesIn : 0;
Marek Olšák284d9542013-06-12 02:18:09 +02001953
1954 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
1955 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
Paul Berry3b0cf702013-04-10 06:48:42 -07001956 tfeedback_decls, gs_input_vertices))
Paul Berry871ddb92011-11-05 11:17:32 -07001957 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02001958
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02001959 do_dead_builtin_varyings(ctx, sh_i->ir, sh_next->ir,
1960 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
1961 tfeedback_decls);
1962
Marek Olšák284d9542013-06-12 02:18:09 +02001963 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
1964 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
1965
1966 /* Eliminate code that is now dead due to unused outputs being demoted.
1967 */
1968 while (do_dead_code(sh_i->ir, false))
1969 ;
1970 while (do_dead_code(sh_next->ir, false))
1971 ;
1972
Marek Olšák3c555822013-06-13 03:17:22 +02001973 /* This must be done after all dead varyings are eliminated. */
1974 if (!check_against_varying_limit(ctx, prog, sh_next))
1975 goto done;
1976
Marek Olšák284d9542013-06-12 02:18:09 +02001977 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07001978 }
1979
1980 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
1981 goto done;
1982
Ian Romanick960d7222011-10-21 11:21:02 -07001983 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07001984 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01001985 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07001986
Paul Berryb95d2372013-07-27 11:08:31 -07001987 check_resources(ctx, prog);
1988 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08001989 goto done;
1990
Ian Romanickce9171f2011-02-03 17:10:14 -08001991 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07001992 * present in a linked program. By checking prog->IsES, we also
1993 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08001994 */
Eric Anholt57f79782011-07-22 12:57:47 -07001995 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07001996 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08001997 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001998 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08001999 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002000 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002001 }
2002 }
2003
Ian Romanick13e10e42010-06-21 12:03:24 -07002004 /* FINISHME: Assign fragment shader output locations. */
2005
Ian Romanick832dfa52010-06-17 15:04:20 -07002006done:
2007 free(vert_shader_list);
Paul Berry844bd712013-07-30 22:38:43 -07002008 free(frag_shader_list);
Bryan Cain25480922013-02-15 09:46:50 -06002009 free(geom_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002010
2011 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2012 if (prog->_LinkedShaders[i] == NULL)
2013 continue;
2014
2015 /* Retain any live IR, but trash the rest. */
2016 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002017
2018 /* The symbol table in the linked shaders may contain references to
2019 * variables that were removed (e.g., unused uniforms). Since it may
2020 * contain junk, there is no possible valid use. Delete it and set the
2021 * pointer to NULL.
2022 */
2023 delete prog->_LinkedShaders[i]->symbols;
2024 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002025 }
2026
Kenneth Graunked3073f52011-01-21 14:32:31 -08002027 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002028}