blob: 94ea54ceb6907a727d37b60e5b8ff475275c6115 [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
Ian Romanick832dfa52010-06-17 15:04:20 -070085/**
86 * Visitor that determines whether or not a variable is ever written.
87 */
88class find_assignment_visitor : public ir_hierarchical_visitor {
89public:
90 find_assignment_visitor(const char *name)
91 : name(name), found(false)
92 {
93 /* empty */
94 }
95
96 virtual ir_visitor_status visit_enter(ir_assignment *ir)
97 {
98 ir_variable *const var = ir->lhs->variable_referenced();
99
100 if (strcmp(name, var->name) == 0) {
101 found = true;
102 return visit_stop;
103 }
104
105 return visit_continue_with_parent;
106 }
107
Eric Anholt18a60232010-08-23 11:29:25 -0700108 virtual ir_visitor_status visit_enter(ir_call *ir)
109 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700110 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700111 foreach_iter(exec_list_iterator, iter, *ir) {
112 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
113 ir_variable *sig_param = (ir_variable *)sig_iter.get();
114
Paul Berry42a29d82013-01-11 14:39:32 -0800115 if (sig_param->mode == ir_var_function_out ||
116 sig_param->mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700117 ir_variable *var = param_rval->variable_referenced();
118 if (var && strcmp(name, var->name) == 0) {
119 found = true;
120 return visit_stop;
121 }
122 }
123 sig_iter.next();
124 }
125
Kenneth Graunked884f602012-03-20 15:56:37 -0700126 if (ir->return_deref != NULL) {
127 ir_variable *const var = ir->return_deref->variable_referenced();
128
129 if (strcmp(name, var->name) == 0) {
130 found = true;
131 return visit_stop;
132 }
133 }
134
Eric Anholt18a60232010-08-23 11:29:25 -0700135 return visit_continue_with_parent;
136 }
137
Ian Romanick832dfa52010-06-17 15:04:20 -0700138 bool variable_found()
139 {
140 return found;
141 }
142
143private:
144 const char *name; /**< Find writes to a variable with this name. */
145 bool found; /**< Was a write to the variable found? */
146};
147
Ian Romanickc93b8f12010-06-17 15:20:22 -0700148
Ian Romanickc33e78f2010-08-13 12:30:41 -0700149/**
150 * Visitor that determines whether or not a variable is ever read.
151 */
152class find_deref_visitor : public ir_hierarchical_visitor {
153public:
154 find_deref_visitor(const char *name)
155 : name(name), found(false)
156 {
157 /* empty */
158 }
159
160 virtual ir_visitor_status visit(ir_dereference_variable *ir)
161 {
162 if (strcmp(this->name, ir->var->name) == 0) {
163 this->found = true;
164 return visit_stop;
165 }
166
167 return visit_continue;
168 }
169
170 bool variable_found() const
171 {
172 return this->found;
173 }
174
175private:
176 const char *name; /**< Find writes to a variable with this name. */
177 bool found; /**< Was a write to the variable found? */
178};
179
180
Paul Berry7cfefe62013-07-30 21:13:48 -0700181class geom_array_resize_visitor : public ir_hierarchical_visitor {
182public:
183 unsigned num_vertices;
184 gl_shader_program *prog;
185
186 geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
187 {
188 this->num_vertices = num_vertices;
189 this->prog = prog;
190 }
191
192 virtual ~geom_array_resize_visitor()
193 {
194 /* empty */
195 }
196
197 virtual ir_visitor_status visit(ir_variable *var)
198 {
199 if (!var->type->is_array() || var->mode != ir_var_shader_in)
200 return visit_continue;
201
202 unsigned size = var->type->length;
203
204 /* Generate a link error if the shader has declared this array with an
205 * incorrect size.
206 */
207 if (size && size != this->num_vertices) {
208 linker_error(this->prog, "size of array %s declared as %u, "
209 "but number of input vertices is %u\n",
210 var->name, size, this->num_vertices);
211 return visit_continue;
212 }
213
214 /* Generate a link error if the shader attempts to access an input
215 * array using an index too large for its actual size assigned at link
216 * time.
217 */
218 if (var->max_array_access >= this->num_vertices) {
219 linker_error(this->prog, "geometry shader accesses element %i of "
220 "%s, but only %i input vertices\n",
221 var->max_array_access, var->name, this->num_vertices);
222 return visit_continue;
223 }
224
225 var->type = glsl_type::get_array_instance(var->type->element_type(),
226 this->num_vertices);
227 var->max_array_access = this->num_vertices - 1;
228
229 return visit_continue;
230 }
231
232 /* Dereferences of input variables need to be updated so that their type
233 * matches the newly assigned type of the variable they are accessing. */
234 virtual ir_visitor_status visit(ir_dereference_variable *ir)
235 {
236 ir->type = ir->var->type;
237 return visit_continue;
238 }
239
240 /* Dereferences of 2D input arrays need to be updated so that their type
241 * matches the newly assigned type of the array they are accessing. */
242 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
243 {
244 const glsl_type *const vt = ir->array->type;
245 if (vt->is_array())
246 ir->type = vt->element_type();
247 return visit_continue;
248 }
249};
250
251
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700252void
Ian Romanick586e7412011-07-28 14:04:09 -0700253linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700254{
255 va_list ap;
256
Kenneth Graunked3073f52011-01-21 14:32:31 -0800257 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700258 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800259 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700260 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700261
262 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700263}
264
265
266void
Ian Romanick379a32f2011-07-28 14:09:06 -0700267linker_warning(gl_shader_program *prog, const char *fmt, ...)
268{
269 va_list ap;
270
271 ralloc_strcat(&prog->InfoLog, "error: ");
272 va_start(ap, fmt);
273 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
274 va_end(ap);
275
276}
277
278
Paul Berryb92900d2013-01-28 14:21:59 -0800279/**
280 * Given a string identifying a program resource, break it into a base name
281 * and an optional array index in square brackets.
282 *
283 * If an array index is present, \c out_base_name_end is set to point to the
284 * "[" that precedes the array index, and the array index itself is returned
285 * as a long.
286 *
287 * If no array index is present (or if the array index is negative or
288 * mal-formed), \c out_base_name_end, is set to point to the null terminator
289 * at the end of the input string, and -1 is returned.
290 *
291 * Only the final array index is parsed; if the string contains other array
292 * indices (or structure field accesses), they are left in the base name.
293 *
294 * No attempt is made to check that the base name is properly formed;
295 * typically the caller will look up the base name in a hash table, so
296 * ill-formed base names simply turn into hash table lookup failures.
297 */
298long
299parse_program_resource_name(const GLchar *name,
300 const GLchar **out_base_name_end)
301{
302 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
303 *
304 * "When an integer array element or block instance number is part of
305 * the name string, it will be specified in decimal form without a "+"
306 * or "-" sign or any extra leading zeroes. Additionally, the name
307 * string will not include white space anywhere in the string."
308 */
309
310 const size_t len = strlen(name);
311 *out_base_name_end = name + len;
312
313 if (len == 0 || name[len-1] != ']')
314 return -1;
315
316 /* Walk backwards over the string looking for a non-digit character. This
317 * had better be the opening bracket for an array index.
318 *
319 * Initially, i specifies the location of the ']'. Since the string may
320 * contain only the ']' charcater, walk backwards very carefully.
321 */
322 unsigned i;
323 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
324 /* empty */ ;
325
326 if ((i == 0) || name[i-1] != '[')
327 return -1;
328
329 long array_index = strtol(&name[i], NULL, 10);
330 if (array_index < 0)
331 return -1;
332
333 *out_base_name_end = name + (i - 1);
334 return array_index;
335}
336
337
Ian Romanick379a32f2011-07-28 14:09:06 -0700338void
Paul Berry50895d42012-12-05 07:17:07 -0800339link_invalidate_variable_locations(gl_shader *sh, int input_base,
340 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700341{
Eric Anholt16b68b12010-06-30 11:05:43 -0700342 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700343 ir_variable *const var = ((ir_instruction *) node)->as_variable();
344
Paul Berry50895d42012-12-05 07:17:07 -0800345 if (var == NULL)
346 continue;
347
348 int base;
349 switch (var->mode) {
Paul Berry42a29d82013-01-11 14:39:32 -0800350 case ir_var_shader_in:
Paul Berry50895d42012-12-05 07:17:07 -0800351 base = input_base;
352 break;
Paul Berry42a29d82013-01-11 14:39:32 -0800353 case ir_var_shader_out:
Paul Berry50895d42012-12-05 07:17:07 -0800354 base = output_base;
355 break;
356 default:
357 continue;
358 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700359
360 /* Only assign locations for generic attributes / varyings / etc.
361 */
Paul Berry50895d42012-12-05 07:17:07 -0800362 if ((var->location >= base) && !var->explicit_location)
363 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800364
Paul Berry3e81c662012-12-05 10:47:55 -0800365 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800366 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800367 var->location_frac = 0;
368 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800369 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800370 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700371 }
372}
373
374
Ian Romanickc93b8f12010-06-17 15:20:22 -0700375/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700376 * Verify that a vertex shader executable meets all semantic requirements.
377 *
Paul Berry642e5b412012-01-04 13:57:52 -0800378 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
379 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700380 *
381 * \param shader Vertex shader executable to be verified
382 */
Paul Berryb95d2372013-07-27 11:08:31 -0700383void
Eric Anholt849e1812010-06-30 11:49:17 -0700384validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700385 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700386{
387 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700388 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700389
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700390 /* From the GLSL 1.10 spec, page 48:
391 *
392 * "The variable gl_Position is available only in the vertex
393 * language and is intended for writing the homogeneous vertex
394 * position. All executions of a well-formed vertex shader
395 * executable must write a value into this variable. [...] The
396 * variable gl_Position is available only in the vertex
397 * language and is intended for writing the homogeneous vertex
398 * position. All executions of a well-formed vertex shader
399 * executable must write a value into this variable."
400 *
401 * while in GLSL 1.40 this text is changed to:
402 *
403 * "The variable gl_Position is available only in the vertex
404 * language and is intended for writing the homogeneous vertex
405 * position. It can be written at any time during shader
406 * execution. It may also be read back by a vertex shader
407 * after being written. This value will be used by primitive
408 * assembly, clipping, culling, and other fixed functionality
409 * operations, if present, that operate on primitives after
410 * vertex processing has occurred. Its value is undefined if
411 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700412 *
413 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
414 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700415 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700416 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700417 find_assignment_visitor find("gl_Position");
418 find.run(shader->ir);
419 if (!find.variable_found()) {
420 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700421 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700422 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700423 }
424
Paul Berry642e5b412012-01-04 13:57:52 -0800425 prog->Vert.ClipDistanceArraySize = 0;
426
Paul Berry15ba2a52012-08-02 17:51:02 -0700427 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700428 /* From section 7.1 (Vertex Shader Special Variables) of the
429 * GLSL 1.30 spec:
430 *
431 * "It is an error for a shader to statically write both
432 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700433 *
434 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
435 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700436 */
437 find_assignment_visitor clip_vertex("gl_ClipVertex");
438 find_assignment_visitor clip_distance("gl_ClipDistance");
439
440 clip_vertex.run(shader->ir);
441 clip_distance.run(shader->ir);
442 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
443 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
444 "and `gl_ClipDistance'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700445 return;
Paul Berryb453ba22011-08-11 18:10:22 -0700446 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700447 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800448 ir_variable *clip_distance_var =
449 shader->symbols->get_variable("gl_ClipDistance");
450 if (clip_distance_var)
451 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700452 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700453}
454
455
Ian Romanickc93b8f12010-06-17 15:20:22 -0700456/**
457 * Verify that a fragment shader executable meets all semantic requirements
458 *
459 * \param shader Fragment shader executable to be verified
460 */
Paul Berryb95d2372013-07-27 11:08:31 -0700461void
Eric Anholt849e1812010-06-30 11:49:17 -0700462validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700463 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700464{
465 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700466 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700467
Ian Romanick832dfa52010-06-17 15:04:20 -0700468 find_assignment_visitor frag_color("gl_FragColor");
469 find_assignment_visitor frag_data("gl_FragData");
470
Eric Anholt16b68b12010-06-30 11:05:43 -0700471 frag_color.run(shader->ir);
472 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700473
Ian Romanick832dfa52010-06-17 15:04:20 -0700474 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700475 linker_error(prog, "fragment shader writes to both "
476 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700477 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700478}
479
Bryan Cain25480922013-02-15 09:46:50 -0600480/**
481 * Verify that a geometry shader executable meets all semantic requirements
482 *
483 * Also sets prog->Geom.VerticesIn as a side effect.
484 *
485 * \param shader Geometry shader executable to be verified
486 */
487void
488validate_geometry_shader_executable(struct gl_shader_program *prog,
489 struct gl_shader *shader)
490{
491 if (shader == NULL)
492 return;
493
494 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
495 prog->Geom.VerticesIn = num_vertices;
496}
497
Ian Romanick832dfa52010-06-17 15:04:20 -0700498
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700499/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700500 * Generate a string describing the mode of a variable
501 */
502static const char *
503mode_string(const ir_variable *var)
504{
505 switch (var->mode) {
506 case ir_var_auto:
507 return (var->read_only) ? "global constant" : "global variable";
508
Paul Berry42a29d82013-01-11 14:39:32 -0800509 case ir_var_uniform: return "uniform";
510 case ir_var_shader_in: return "shader input";
511 case ir_var_shader_out: return "shader output";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700512
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800513 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700514 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700515 default:
516 assert(!"Should not get here.");
517 return "invalid variable";
518 }
519}
520
521
522/**
523 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700524 */
Paul Berryb95d2372013-07-27 11:08:31 -0700525void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700526cross_validate_globals(struct gl_shader_program *prog,
527 struct gl_shader **shader_list,
528 unsigned num_shaders,
529 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700530{
531 /* Examine all of the uniforms in all of the shaders and cross validate
532 * them.
533 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700534 glsl_symbol_table variables;
535 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700536 if (shader_list[i] == NULL)
537 continue;
538
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700539 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700540 ir_variable *const var = ((ir_instruction *) node)->as_variable();
541
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700542 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700543 continue;
544
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700545 if (uniforms_only && (var->mode != ir_var_uniform))
546 continue;
547
Ian Romanick7e2aa912010-07-19 17:12:42 -0700548 /* Don't cross validate temporaries that are at global scope. These
549 * will eventually get pulled into the shaders 'main'.
550 */
551 if (var->mode == ir_var_temporary)
552 continue;
553
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700554 /* If a global with this name has already been seen, verify that the
555 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700556 * initializers, the values of the initializers must be the same.
557 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700558 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700559 if (existing != NULL) {
560 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700561 /* Consider the types to be "the same" if both types are arrays
562 * of the same type and one of the arrays is implicitly sized.
563 * In addition, set the type of the linked variable to the
564 * explicitly sized array.
565 */
566 if (var->type->is_array()
567 && existing->type->is_array()
568 && (var->type->fields.array == existing->type->fields.array)
569 && ((var->type->length == 0)
570 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800571 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700572 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800573 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700574 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700575 linker_error(prog, "%s `%s' declared as type "
576 "`%s' and type `%s'\n",
577 mode_string(var),
578 var->name, var->type->name,
579 existing->type->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700580 return;
Ian Romanicka2711d62010-08-29 22:07:49 -0700581 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700582 }
583
Ian Romanick68a4fc92010-10-07 17:21:22 -0700584 if (var->explicit_location) {
585 if (existing->explicit_location
586 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700587 linker_error(prog, "explicit locations for %s "
588 "`%s' have differing values\n",
589 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700590 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700591 }
592
593 existing->location = var->location;
594 existing->explicit_location = true;
595 }
596
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700597 /* From the GLSL 4.20 specification:
598 * "A link error will result if two compilation units in a program
599 * specify different integer-constant bindings for the same
600 * opaque-uniform name. However, it is not an error to specify a
601 * binding on some but not all declarations for the same name"
602 */
603 if (var->explicit_binding) {
604 if (existing->explicit_binding &&
605 var->binding != existing->binding) {
606 linker_error(prog, "explicit bindings for %s "
607 "`%s' have differing values\n",
608 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700609 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700610 }
611
612 existing->binding = var->binding;
613 existing->explicit_binding = true;
614 }
615
Ian Romanick46173f92011-10-31 13:07:06 -0700616 /* Validate layout qualifiers for gl_FragDepth.
617 *
618 * From the AMD/ARB_conservative_depth specs:
619 *
620 * "If gl_FragDepth is redeclared in any fragment shader in a
621 * program, it must be redeclared in all fragment shaders in
622 * that program that have static assignments to
623 * gl_FragDepth. All redeclarations of gl_FragDepth in all
624 * fragment shaders in a single program must have the same set
625 * of qualifiers."
626 */
627 if (strcmp(var->name, "gl_FragDepth") == 0) {
628 bool layout_declared = var->depth_layout != ir_depth_layout_none;
629 bool layout_differs =
630 var->depth_layout != existing->depth_layout;
631
632 if (layout_declared && layout_differs) {
633 linker_error(prog,
634 "All redeclarations of gl_FragDepth in all "
635 "fragment shaders in a single program must have "
636 "the same set of qualifiers.");
637 }
638
639 if (var->used && layout_differs) {
640 linker_error(prog,
641 "If gl_FragDepth is redeclared with a layout "
642 "qualifier in any fragment shader, it must be "
643 "redeclared with the same layout qualifier in "
644 "all fragment shaders that have assignments to "
645 "gl_FragDepth");
646 }
647 }
Chad Versaceaddae332011-01-27 01:40:31 -0800648
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700649 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
650 *
651 * "If a shared global has multiple initializers, the
652 * initializers must all be constant expressions, and they
653 * must all have the same value. Otherwise, a link error will
654 * result. (A shared global having only one initializer does
655 * not require that initializer to be a constant expression.)"
656 *
657 * Previous to 4.20 the GLSL spec simply said that initializers
658 * must have the same value. In this case of non-constant
659 * initializers, this was impossible to determine. As a result,
660 * no vendor actually implemented that behavior. The 4.20
661 * behavior matches the implemented behavior of at least one other
662 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700663 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700664 if (var->constant_initializer != NULL) {
665 if (existing->constant_initializer != NULL) {
666 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700667 linker_error(prog, "initializers for %s "
668 "`%s' have differing values\n",
669 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700670 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700671 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700672 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700673 /* If the first-seen instance of a particular uniform did not
674 * have an initializer but a later instance does, copy the
675 * initializer to the version stored in the symbol table.
676 */
Ian Romanickde415b72010-07-14 13:22:12 -0700677 /* FINISHME: This is wrong. The constant_value field should
678 * FINISHME: not be modified! Imagine a case where a shader
679 * FINISHME: without an initializer is linked in two different
680 * FINISHME: programs with shaders that have differing
681 * FINISHME: initializers. Linking with the first will
682 * FINISHME: modify the shader, and linking with the second
683 * FINISHME: will fail.
684 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700685 existing->constant_initializer =
686 var->constant_initializer->clone(ralloc_parent(existing),
687 NULL);
688 }
689 }
690
691 if (var->has_initializer) {
692 if (existing->has_initializer
693 && (var->constant_initializer == NULL
694 || existing->constant_initializer == NULL)) {
695 linker_error(prog,
696 "shared global variable `%s' has multiple "
697 "non-constant initializers.\n",
698 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700699 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700700 }
701
702 /* Some instance had an initializer, so keep track of that. In
703 * this location, all sorts of initializers (constant or
704 * otherwise) will propagate the existence to the variable
705 * stored in the symbol table.
706 */
707 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700708 }
Chad Versace7528f142010-11-17 14:34:38 -0800709
710 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700711 linker_error(prog, "declarations for %s `%s' have "
712 "mismatching invariant qualifiers\n",
713 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700714 return;
Chad Versace7528f142010-11-17 14:34:38 -0800715 }
Chad Versace61428dd2011-01-10 15:29:30 -0800716 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700717 linker_error(prog, "declarations for %s `%s' have "
718 "mismatching centroid qualifiers\n",
719 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700720 return;
Chad Versace61428dd2011-01-10 15:29:30 -0800721 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700722 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700723 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700724 }
725 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700726}
727
728
Ian Romanick37101922010-06-18 19:02:10 -0700729/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700730 * Perform validation of uniforms used across multiple shader stages
731 */
Paul Berryb95d2372013-07-27 11:08:31 -0700732void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700733cross_validate_uniforms(struct gl_shader_program *prog)
734{
Paul Berryb95d2372013-07-27 11:08:31 -0700735 cross_validate_globals(prog, prog->_LinkedShaders,
736 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700737}
738
Eric Anholtf609cf72012-04-27 13:52:56 -0700739/**
740 * Accumulates the array of prog->UniformBlocks and checks that all
741 * definitons of blocks agree on their contents.
742 */
743static bool
744interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
745{
746 unsigned max_num_uniform_blocks = 0;
747 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
748 if (prog->_LinkedShaders[i])
749 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
750 }
751
752 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
753 struct gl_shader *sh = prog->_LinkedShaders[i];
754
755 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
756 max_num_uniform_blocks);
757 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
758 prog->UniformBlockStageIndex[i][j] = -1;
759
760 if (sh == NULL)
761 continue;
762
763 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
764 int index = link_cross_validate_uniform_block(prog,
765 &prog->UniformBlocks,
766 &prog->NumUniformBlocks,
767 &sh->UniformBlocks[j]);
768
769 if (index == -1) {
770 linker_error(prog, "uniform block `%s' has mismatching definitions",
771 sh->UniformBlocks[j].Name);
772 return false;
773 }
774
775 prog->UniformBlockStageIndex[i][index] = j;
776 }
777 }
778
779 return true;
780}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700781
Ian Romanick37101922010-06-18 19:02:10 -0700782
Ian Romanick3fb87872010-07-09 14:09:34 -0700783/**
784 * Populates a shaders symbol table with all global declarations
785 */
786static void
787populate_symbol_table(gl_shader *sh)
788{
789 sh->symbols = new(sh) glsl_symbol_table;
790
791 foreach_list(node, sh->ir) {
792 ir_instruction *const inst = (ir_instruction *) node;
793 ir_variable *var;
794 ir_function *func;
795
796 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700797 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700798 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700799 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700800 }
801 }
802}
803
804
805/**
Ian Romanick31a97862010-07-12 18:48:50 -0700806 * Remap variables referenced in an instruction tree
807 *
808 * This is used when instruction trees are cloned from one shader and placed in
809 * another. These trees will contain references to \c ir_variable nodes that
810 * do not exist in the target shader. This function finds these \c ir_variable
811 * references and replaces the references with matching variables in the target
812 * shader.
813 *
814 * If there is no matching variable in the target shader, a clone of the
815 * \c ir_variable is made and added to the target shader. The new variable is
816 * added to \b both the instruction stream and the symbol table.
817 *
818 * \param inst IR tree that is to be processed.
819 * \param symbols Symbol table containing global scope symbols in the
820 * linked shader.
821 * \param instructions Instruction stream where new variable declarations
822 * should be added.
823 */
824void
Eric Anholt8273bd42010-08-04 12:34:56 -0700825remap_variables(ir_instruction *inst, struct gl_shader *target,
826 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700827{
828 class remap_visitor : public ir_hierarchical_visitor {
829 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700830 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700831 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700832 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700833 this->target = target;
834 this->symbols = target->symbols;
835 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700836 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700837 }
838
839 virtual ir_visitor_status visit(ir_dereference_variable *ir)
840 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700841 if (ir->var->mode == ir_var_temporary) {
842 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
843
844 assert(var != NULL);
845 ir->var = var;
846 return visit_continue;
847 }
848
Ian Romanick31a97862010-07-12 18:48:50 -0700849 ir_variable *const existing =
850 this->symbols->get_variable(ir->var->name);
851 if (existing != NULL)
852 ir->var = existing;
853 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700854 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700855
Eric Anholt001eee52010-11-05 06:11:24 -0700856 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700857 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700858 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700859 }
860
861 return visit_continue;
862 }
863
864 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700865 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700866 glsl_symbol_table *symbols;
867 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700868 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700869 };
870
Eric Anholt8273bd42010-08-04 12:34:56 -0700871 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700872
873 inst->accept(&v);
874}
875
876
877/**
878 * Move non-declarations from one instruction stream to another
879 *
880 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700881 * 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 -0700882 * pointer) for \c last and \c false for \c make_copies on the first
883 * call. Successive calls pass the return value of the previous call for
884 * \c last and \c true for \c make_copies.
885 *
886 * \param instructions Source instruction stream
887 * \param last Instruction after which new instructions should be
888 * inserted in the target instruction stream
889 * \param make_copies Flag selecting whether instructions in \c instructions
890 * should be copied (via \c ir_instruction::clone) into the
891 * target list or moved.
892 *
893 * \return
894 * The new "last" instruction in the target instruction stream. This pointer
895 * is suitable for use as the \c last parameter of a later call to this
896 * function.
897 */
898exec_node *
899move_non_declarations(exec_list *instructions, exec_node *last,
900 bool make_copies, gl_shader *target)
901{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700902 hash_table *temps = NULL;
903
904 if (make_copies)
905 temps = hash_table_ctor(0, hash_table_pointer_hash,
906 hash_table_pointer_compare);
907
Ian Romanick303c99f2010-07-19 12:34:56 -0700908 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700909 ir_instruction *inst = (ir_instruction *) node;
910
Ian Romanick7e2aa912010-07-19 17:12:42 -0700911 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700912 continue;
913
Ian Romanick7e2aa912010-07-19 17:12:42 -0700914 ir_variable *var = inst->as_variable();
915 if ((var != NULL) && (var->mode != ir_var_temporary))
916 continue;
917
918 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700919 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700920 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700921 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700922
923 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700924 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700925
926 if (var != NULL)
927 hash_table_insert(temps, inst, var);
928 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700929 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700930 } else {
931 inst->remove();
932 }
933
934 last->insert_after(inst);
935 last = inst;
936 }
937
Ian Romanick7e2aa912010-07-19 17:12:42 -0700938 if (make_copies)
939 hash_table_dtor(temps);
940
Ian Romanick31a97862010-07-12 18:48:50 -0700941 return last;
942}
943
944/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700945 * Get the function signature for main from a shader
946 */
947static ir_function_signature *
948get_main_function_signature(gl_shader *sh)
949{
950 ir_function *const f = sh->symbols->get_function("main");
951 if (f != NULL) {
952 exec_list void_parameters;
953
954 /* Look for the 'void main()' signature and ensure that it's defined.
955 * This keeps the linker from accidentally pick a shader that just
956 * contains a prototype for main.
957 *
958 * We don't have to check for multiple definitions of main (in multiple
959 * shaders) because that would have already been caught above.
960 */
961 ir_function_signature *sig = f->matching_signature(&void_parameters);
962 if ((sig != NULL) && sig->is_defined) {
963 return sig;
964 }
965 }
966
967 return NULL;
968}
969
970
971/**
Brian Paul84a12732012-02-02 20:10:40 -0700972 * This class is only used in link_intrastage_shaders() below but declaring
973 * it inside that function leads to compiler warnings with some versions of
974 * gcc.
975 */
976class array_sizing_visitor : public ir_hierarchical_visitor {
977public:
978 virtual ir_visitor_status visit(ir_variable *var)
979 {
980 if (var->type->is_array() && (var->type->length == 0)) {
981 const glsl_type *type =
982 glsl_type::get_array_instance(var->type->fields.array,
983 var->max_array_access + 1);
984 assert(type != NULL);
985 var->type = type;
986 }
987 return visit_continue;
988 }
989};
990
Brian Paul84a12732012-02-02 20:10:40 -0700991/**
Eric Anholt6065a872013-06-12 18:12:40 -0700992 * Performs the cross-validation of geometry shader max_vertices and
993 * primitive type layout qualifiers for the attached geometry shaders,
994 * and propagates them to the linked GS and linked shader program.
995 */
996static void
997link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
998 struct gl_shader *linked_shader,
999 struct gl_shader **shader_list,
1000 unsigned num_shaders)
1001{
1002 linked_shader->Geom.VerticesOut = 0;
1003 linked_shader->Geom.InputType = PRIM_UNKNOWN;
1004 linked_shader->Geom.OutputType = PRIM_UNKNOWN;
1005
1006 /* No in/out qualifiers defined for anything but GLSL 1.50+
1007 * geometry shaders so far.
1008 */
1009 if (linked_shader->Type != GL_GEOMETRY_SHADER || prog->Version < 150)
1010 return;
1011
1012 /* From the GLSL 1.50 spec, page 46:
1013 *
1014 * "All geometry shader output layout declarations in a program
1015 * must declare the same layout and same value for
1016 * max_vertices. There must be at least one geometry output
1017 * layout declaration somewhere in a program, but not all
1018 * geometry shaders (compilation units) are required to
1019 * declare it."
1020 */
1021
1022 for (unsigned i = 0; i < num_shaders; i++) {
1023 struct gl_shader *shader = shader_list[i];
1024
1025 if (shader->Geom.InputType != PRIM_UNKNOWN) {
1026 if (linked_shader->Geom.InputType != PRIM_UNKNOWN &&
1027 linked_shader->Geom.InputType != shader->Geom.InputType) {
1028 linker_error(prog, "geometry shader defined with conflicting "
1029 "input types\n");
1030 return;
1031 }
1032 linked_shader->Geom.InputType = shader->Geom.InputType;
1033 }
1034
1035 if (shader->Geom.OutputType != PRIM_UNKNOWN) {
1036 if (linked_shader->Geom.OutputType != PRIM_UNKNOWN &&
1037 linked_shader->Geom.OutputType != shader->Geom.OutputType) {
1038 linker_error(prog, "geometry shader defined with conflicting "
1039 "output types\n");
1040 return;
1041 }
1042 linked_shader->Geom.OutputType = shader->Geom.OutputType;
1043 }
1044
1045 if (shader->Geom.VerticesOut != 0) {
1046 if (linked_shader->Geom.VerticesOut != 0 &&
1047 linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) {
1048 linker_error(prog, "geometry shader defined with conflicting "
1049 "output vertex count (%d and %d)\n",
1050 linked_shader->Geom.VerticesOut,
1051 shader->Geom.VerticesOut);
1052 return;
1053 }
1054 linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut;
1055 }
1056 }
1057
1058 /* Just do the intrastage -> interstage propagation right now,
1059 * since we already know we're in the right type of shader program
1060 * for doing it.
1061 */
1062 if (linked_shader->Geom.InputType == PRIM_UNKNOWN) {
1063 linker_error(prog,
1064 "geometry shader didn't declare primitive input type\n");
1065 return;
1066 }
1067 prog->Geom.InputType = linked_shader->Geom.InputType;
1068
1069 if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) {
1070 linker_error(prog,
1071 "geometry shader didn't declare primitive output type\n");
1072 return;
1073 }
1074 prog->Geom.OutputType = linked_shader->Geom.OutputType;
1075
1076 if (linked_shader->Geom.VerticesOut == 0) {
1077 linker_error(prog,
1078 "geometry shader didn't declare max_vertices\n");
1079 return;
1080 }
1081 prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut;
1082}
1083
1084/**
Ian Romanick3fb87872010-07-09 14:09:34 -07001085 * Combine a group of shaders for a single stage to generate a linked shader
1086 *
1087 * \note
1088 * If this function is supplied a single shader, it is cloned, and the new
1089 * shader is returned.
1090 */
1091static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001092link_intrastage_shaders(void *mem_ctx,
1093 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -07001094 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -07001095 struct gl_shader **shader_list,
1096 unsigned num_shaders)
1097{
Eric Anholtf609cf72012-04-27 13:52:56 -07001098 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -07001099
Ian Romanick13f782c2010-06-29 18:53:38 -07001100 /* Check that global variables defined in multiple shaders are consistent.
1101 */
Paul Berryb95d2372013-07-27 11:08:31 -07001102 cross_validate_globals(prog, shader_list, num_shaders, false);
1103 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -07001104 return NULL;
1105
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001106 /* Check that interface blocks defined in multiple shaders are consistent.
1107 */
Paul Berryb95d2372013-07-27 11:08:31 -07001108 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
1109 num_shaders);
1110 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001111 return NULL;
1112
Paul Berry4682b9b2013-07-27 15:07:08 -07001113 /* Link up uniform blocks defined within this stage. */
1114 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -05001115 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
1116 &uniform_blocks);
Eric Anholtf609cf72012-04-27 13:52:56 -07001117
Ian Romanick13f782c2010-06-29 18:53:38 -07001118 /* Check that there is only a single definition of each function signature
1119 * across all shaders.
1120 */
1121 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1122 foreach_list(node, shader_list[i]->ir) {
1123 ir_function *const f = ((ir_instruction *) node)->as_function();
1124
1125 if (f == NULL)
1126 continue;
1127
1128 for (unsigned j = i + 1; j < num_shaders; j++) {
1129 ir_function *const other =
1130 shader_list[j]->symbols->get_function(f->name);
1131
1132 /* If the other shader has no function (and therefore no function
1133 * signatures) with the same name, skip to the next shader.
1134 */
1135 if (other == NULL)
1136 continue;
1137
1138 foreach_iter (exec_list_iterator, iter, *f) {
1139 ir_function_signature *sig =
1140 (ir_function_signature *) iter.get();
1141
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001142 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -07001143 continue;
1144
1145 ir_function_signature *other_sig =
1146 other->exact_matching_signature(& sig->parameters);
1147
1148 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001149 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -07001150 linker_error(prog, "function `%s' is multiply defined",
1151 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001152 return NULL;
1153 }
1154 }
1155 }
1156 }
1157 }
1158
1159 /* Find the shader that defines main, and make a clone of it.
1160 *
1161 * Starting with the clone, search for undefined references. If one is
1162 * found, find the shader that defines it. Clone the reference and add
1163 * it to the shader. Repeat until there are no undefined references or
1164 * until a reference cannot be resolved.
1165 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001166 gl_shader *main = NULL;
1167 for (unsigned i = 0; i < num_shaders; i++) {
1168 if (get_main_function_signature(shader_list[i]) != NULL) {
1169 main = shader_list[i];
1170 break;
1171 }
1172 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001173
Ian Romanick15ce87e2010-07-09 15:28:22 -07001174 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001175 linker_error(prog, "%s shader lacks `main'\n",
Eric Anholtfaf3dba2013-06-12 16:57:11 -07001176 _mesa_glsl_shader_target_name(shader_list[0]->Type));
Ian Romanick15ce87e2010-07-09 15:28:22 -07001177 return NULL;
1178 }
1179
Ian Romanick4a455952010-10-13 15:13:02 -07001180 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001181 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001182 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001183
Eric Anholtf609cf72012-04-27 13:52:56 -07001184 linked->UniformBlocks = uniform_blocks;
1185 linked->NumUniformBlocks = num_uniform_blocks;
1186 ralloc_steal(linked, linked->UniformBlocks);
1187
Eric Anholt6065a872013-06-12 18:12:40 -07001188 link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
1189
Ian Romanick15ce87e2010-07-09 15:28:22 -07001190 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001191
Ian Romanick31a97862010-07-12 18:48:50 -07001192 /* The a pointer to the main function in the final linked shader (i.e., the
1193 * copy of the original shader that contained the main function).
1194 */
1195 ir_function_signature *const main_sig = get_main_function_signature(linked);
1196
1197 /* Move any instructions other than variable declarations or function
1198 * declarations into main.
1199 */
Ian Romanick9303e352010-07-19 12:33:54 -07001200 exec_node *insertion_point =
1201 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1202 linked);
1203
Ian Romanick31a97862010-07-12 18:48:50 -07001204 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001205 if (shader_list[i] == main)
1206 continue;
1207
Ian Romanick31a97862010-07-12 18:48:50 -07001208 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001209 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001210 }
1211
Ian Romanick13f782c2010-06-29 18:53:38 -07001212 /* Resolve initializers for global variables in the linked shader.
1213 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001214 unsigned num_linking_shaders = num_shaders;
1215 for (unsigned i = 0; i < num_shaders; i++)
1216 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1217
1218 gl_shader **linking_shaders =
1219 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1220
1221 memcpy(linking_shaders, shader_list,
1222 sizeof(linking_shaders[0]) * num_shaders);
1223
1224 unsigned idx = num_shaders;
1225 for (unsigned i = 0; i < num_shaders; i++) {
1226 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1227 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1228 idx += shader_list[i]->num_builtins_to_link;
1229 }
1230
1231 assert(idx == num_linking_shaders);
1232
Ian Romanick4a455952010-10-13 15:13:02 -07001233 if (!link_function_calls(prog, linked, linking_shaders,
1234 num_linking_shaders)) {
1235 ctx->Driver.DeleteShader(ctx, linked);
1236 linked = NULL;
1237 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001238
1239 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001240
Paul Berryc148ef62011-08-03 15:37:01 -07001241 /* At this point linked should contain all of the linked IR, so
1242 * validate it to make sure nothing went wrong.
1243 */
1244 if (linked)
1245 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07001246
Paul Berry7cfefe62013-07-30 21:13:48 -07001247 /* Set the size of geometry shader input arrays */
1248 if (linked->Type == GL_GEOMETRY_SHADER) {
1249 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
1250 geom_array_resize_visitor input_resize_visitor(num_vertices, prog);
1251 foreach_iter(exec_list_iterator, iter, *linked->ir) {
1252 ir_instruction *ir = (ir_instruction *)iter.get();
1253 ir->accept(&input_resize_visitor);
1254 }
1255 }
1256
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001257 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001258 * unspecified sizes have a size specified. The size is inferred from the
1259 * max_array_access field.
1260 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001261 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001262 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001263
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001264 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001265 }
1266
Ian Romanick3fb87872010-07-09 14:09:34 -07001267 return linked;
1268}
1269
Eric Anholta721abf2010-08-23 10:32:01 -07001270/**
1271 * Update the sizes of linked shader uniform arrays to the maximum
1272 * array index used.
1273 *
1274 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1275 *
1276 * If one or more elements of an array are active,
1277 * GetActiveUniform will return the name of the array in name,
1278 * subject to the restrictions listed above. The type of the array
1279 * is returned in type. The size parameter contains the highest
1280 * array element index used, plus one. The compiler or linker
1281 * determines the highest index used. There will be only one
1282 * active uniform reported by the GL per uniform array.
1283
1284 */
1285static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001286update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001287{
Ian Romanick3322fba2010-10-14 13:28:42 -07001288 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1289 if (prog->_LinkedShaders[i] == NULL)
1290 continue;
1291
Eric Anholta721abf2010-08-23 10:32:01 -07001292 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1293 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1294
Paul Berry6a2baf32013-06-10 14:01:45 -07001295 if ((var == NULL) || (var->mode != ir_var_uniform) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001296 !var->type->is_array())
1297 continue;
1298
Eric Anholt9feb4032012-05-01 14:43:31 -07001299 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1300 * will not be eliminated. Since we always do std140, just
1301 * don't resize arrays in UBOs.
1302 */
Ian Romanick13be1f42012-12-14 12:00:14 -08001303 if (var->is_in_uniform_block())
Eric Anholt9feb4032012-05-01 14:43:31 -07001304 continue;
1305
Eric Anholta721abf2010-08-23 10:32:01 -07001306 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001307 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1308 if (prog->_LinkedShaders[j] == NULL)
1309 continue;
1310
Eric Anholta721abf2010-08-23 10:32:01 -07001311 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1312 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1313 if (!other_var)
1314 continue;
1315
1316 if (strcmp(var->name, other_var->name) == 0 &&
1317 other_var->max_array_access > size) {
1318 size = other_var->max_array_access;
1319 }
1320 }
1321 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001322
Fabian Bieler63684782013-06-14 13:37:07 +02001323 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001324 /* If this is a built-in uniform (i.e., it's backed by some
1325 * fixed-function state), adjust the number of state slots to
1326 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001327 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001328 * slots is an integer multiple of the number of array elements.
1329 * Determine the number of slots per array element by dividing by
1330 * the old (total) size.
1331 */
1332 if (var->num_state_slots > 0) {
1333 var->num_state_slots = (size + 1)
1334 * (var->num_state_slots / var->type->length);
1335 }
1336
Eric Anholta721abf2010-08-23 10:32:01 -07001337 var->type = glsl_type::get_array_instance(var->type->fields.array,
1338 size + 1);
1339 /* FINISHME: We should update the types of array
1340 * dereferences of this variable now.
1341 */
1342 }
1343 }
1344 }
1345}
1346
Ian Romanick69846702010-06-22 17:29:19 -07001347/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001348 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001349 *
1350 * \param used_mask Bits representing used (1) and unused (0) locations
1351 * \param needed_count Number of contiguous bits needed.
1352 *
1353 * \return
1354 * Base location of the available bits on success or -1 on failure.
1355 */
1356int
1357find_available_slots(unsigned used_mask, unsigned needed_count)
1358{
1359 unsigned needed_mask = (1 << needed_count) - 1;
1360 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1361
1362 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1363 * cannot optimize possibly infinite loops" for the loop below.
1364 */
1365 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1366 return -1;
1367
1368 for (int i = 0; i <= max_bit_to_test; i++) {
1369 if ((needed_mask & ~used_mask) == needed_mask)
1370 return i;
1371
1372 needed_mask <<= 1;
1373 }
1374
1375 return -1;
1376}
1377
1378
Ian Romanickd32d4f72011-06-27 17:59:58 -07001379/**
1380 * Assign locations for either VS inputs for FS outputs
1381 *
1382 * \param prog Shader program whose variables need locations assigned
1383 * \param target_index Selector for the program target to receive location
1384 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1385 * \c MESA_SHADER_FRAGMENT.
1386 * \param max_index Maximum number of generic locations. This corresponds
1387 * to either the maximum number of draw buffers or the
1388 * maximum number of generic attributes.
1389 *
1390 * \return
1391 * If locations are successfully assigned, true is returned. Otherwise an
1392 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001393 */
Ian Romanick69846702010-06-22 17:29:19 -07001394bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001395assign_attribute_or_color_locations(gl_shader_program *prog,
1396 unsigned target_index,
1397 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001398{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001399 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001400 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001401 unsigned used_locations = (max_index >= 32)
1402 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001403
Ian Romanickd32d4f72011-06-27 17:59:58 -07001404 assert((target_index == MESA_SHADER_VERTEX)
1405 || (target_index == MESA_SHADER_FRAGMENT));
1406
1407 gl_shader *const sh = prog->_LinkedShaders[target_index];
1408 if (sh == NULL)
1409 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001410
Ian Romanick69846702010-06-22 17:29:19 -07001411 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001412 *
1413 * 1. Invalidate the location assignments for all vertex shader inputs.
1414 *
1415 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001416 * glBindVertexAttribLocation) locations and outputs that have
1417 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001418 *
Ian Romanick69846702010-06-22 17:29:19 -07001419 * 3. Sort the attributes without assigned locations by number of slots
1420 * required in decreasing order. Fragmentation caused by attribute
1421 * locations assigned by the application may prevent large attributes
1422 * from having enough contiguous space.
1423 *
1424 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001425 */
1426
Ian Romanickd32d4f72011-06-27 17:59:58 -07001427 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001428 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001429
Ian Romanickd32d4f72011-06-27 17:59:58 -07001430 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001431 (target_index == MESA_SHADER_VERTEX)
1432 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001433
1434
Ian Romanick69846702010-06-22 17:29:19 -07001435 /* Temporary storage for the set of attributes that need locations assigned.
1436 */
1437 struct temp_attr {
1438 unsigned slots;
1439 ir_variable *var;
1440
1441 /* Used below in the call to qsort. */
1442 static int compare(const void *a, const void *b)
1443 {
1444 const temp_attr *const l = (const temp_attr *) a;
1445 const temp_attr *const r = (const temp_attr *) b;
1446
1447 /* Reversed because we want a descending order sort below. */
1448 return r->slots - l->slots;
1449 }
1450 } to_assign[16];
1451
1452 unsigned num_attr = 0;
1453
Eric Anholt16b68b12010-06-30 11:05:43 -07001454 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001455 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1456
Brian Paul4470ff22011-07-19 21:10:25 -06001457 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001458 continue;
1459
Ian Romanick68a4fc92010-10-07 17:21:22 -07001460 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001461 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001462 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001463 linker_error(prog,
1464 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001465 (var->location < 0)
1466 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001467 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001468 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001469 }
1470 } else if (target_index == MESA_SHADER_VERTEX) {
1471 unsigned binding;
1472
1473 if (prog->AttributeBindings->get(binding, var->name)) {
1474 assert(binding >= VERT_ATTRIB_GENERIC0);
1475 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001476 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001477 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001478 } else if (target_index == MESA_SHADER_FRAGMENT) {
1479 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001480 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001481
1482 if (prog->FragDataBindings->get(binding, var->name)) {
1483 assert(binding >= FRAG_RESULT_DATA0);
1484 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001485 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001486
1487 if (prog->FragDataIndexBindings->get(index, var->name)) {
1488 var->index = index;
1489 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001490 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001491 }
1492
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001493 /* If the variable is not a built-in and has a location statically
1494 * assigned in the shader (presumably via a layout qualifier), make sure
1495 * that it doesn't collide with other assigned locations. Otherwise,
1496 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001497 */
Paul Berry0026ad42013-07-31 08:15:08 -07001498 const unsigned slots = var->type->count_attribute_slots();
Ian Romanick523b6112011-08-17 15:40:03 -07001499 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001500 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001501 /* From page 61 of the OpenGL 4.0 spec:
1502 *
1503 * "LinkProgram will fail if the attribute bindings assigned
1504 * by BindAttribLocation do not leave not enough space to
1505 * assign a location for an active matrix attribute or an
1506 * active attribute array, both of which require multiple
1507 * contiguous generic attributes."
1508 *
1509 * Previous versions of the spec contain similar language but omit
1510 * the bit about attribute arrays.
1511 *
1512 * Page 61 of the OpenGL 4.0 spec also says:
1513 *
1514 * "It is possible for an application to bind more than one
1515 * attribute name to the same location. This is referred to as
1516 * aliasing. This will only work if only one of the aliased
1517 * attributes is active in the executable program, or if no
1518 * path through the shader consumes more than one attribute of
1519 * a set of attributes aliased to the same location. A link
1520 * error can occur if the linker determines that every path
1521 * through the shader consumes multiple aliased attributes,
1522 * but implementations are not required to generate an error
1523 * in this case."
1524 *
1525 * These two paragraphs are either somewhat contradictory, or I
1526 * don't fully understand one or both of them.
1527 */
1528 /* FINISHME: The code as currently written does not support
1529 * FINISHME: attribute location aliasing (see comment above).
1530 */
1531 /* Mask representing the contiguous slots that will be used by
1532 * this attribute.
1533 */
1534 const unsigned attr = var->location - generic_base;
1535 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001536
Ian Romanick523b6112011-08-17 15:40:03 -07001537 /* Generate a link error if the set of bits requested for this
1538 * attribute overlaps any previously allocated bits.
1539 */
1540 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001541 const char *const string = (target_index == MESA_SHADER_VERTEX)
1542 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001543 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001544 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001545 "available for %s `%s' %d %d %d", string,
1546 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001547 return false;
1548 }
1549
1550 used_locations |= (use_mask << attr);
1551 }
1552
1553 continue;
1554 }
1555
1556 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001557 to_assign[num_attr].var = var;
1558 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001559 }
Ian Romanick69846702010-06-22 17:29:19 -07001560
1561 /* If all of the attributes were assigned locations by the application (or
1562 * are built-in attributes with fixed locations), return early. This should
1563 * be the common case.
1564 */
1565 if (num_attr == 0)
1566 return true;
1567
1568 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1569
Ian Romanickd32d4f72011-06-27 17:59:58 -07001570 if (target_index == MESA_SHADER_VERTEX) {
1571 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1572 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1573 * reserved to prevent it from being automatically allocated below.
1574 */
1575 find_deref_visitor find("gl_Vertex");
1576 find.run(sh->ir);
1577 if (find.variable_found())
1578 used_locations |= (1 << 0);
1579 }
Ian Romanick982e3792010-06-29 18:58:20 -07001580
Ian Romanick69846702010-06-22 17:29:19 -07001581 for (unsigned i = 0; i < num_attr; i++) {
1582 /* Mask representing the contiguous slots that will be used by this
1583 * attribute.
1584 */
1585 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1586
1587 int location = find_available_slots(used_locations, to_assign[i].slots);
1588
1589 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001590 const char *const string = (target_index == MESA_SHADER_VERTEX)
1591 ? "vertex shader input" : "fragment shader output";
1592
Ian Romanick586e7412011-07-28 14:04:09 -07001593 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001594 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001595 "available for %s `%s'",
1596 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001597 return false;
1598 }
1599
Ian Romanickd32d4f72011-06-27 17:59:58 -07001600 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001601 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001602 used_locations |= (use_mask << location);
1603 }
1604
1605 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001606}
1607
1608
Ian Romanick40e114b2010-08-17 14:55:50 -07001609/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001610 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001611 */
1612void
Ian Romanickcc90e622010-10-19 17:59:10 -07001613demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001614{
1615 foreach_list(node, sh->ir) {
1616 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1617
Ian Romanickcc90e622010-10-19 17:59:10 -07001618 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001619 continue;
1620
Ian Romanickcc90e622010-10-19 17:59:10 -07001621 /* A shader 'in' or 'out' variable is only really an input or output if
1622 * its value is used by other shader stages. This will cause the variable
1623 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001624 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001625 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001626 var->mode = ir_var_auto;
1627 }
1628 }
1629}
1630
1631
Paul Berry871ddb92011-11-05 11:17:32 -07001632/**
Marek Olšákec174a42011-11-18 15:00:10 +01001633 * Store the gl_FragDepth layout in the gl_shader_program struct.
1634 */
1635static void
1636store_fragdepth_layout(struct gl_shader_program *prog)
1637{
1638 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1639 return;
1640 }
1641
1642 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1643
1644 /* We don't look up the gl_FragDepth symbol directly because if
1645 * gl_FragDepth is not used in the shader, it's removed from the IR.
1646 * However, the symbol won't be removed from the symbol table.
1647 *
1648 * We're only interested in the cases where the variable is NOT removed
1649 * from the IR.
1650 */
1651 foreach_list(node, ir) {
1652 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1653
Paul Berry42a29d82013-01-11 14:39:32 -08001654 if (var == NULL || var->mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001655 continue;
1656 }
1657
1658 if (strcmp(var->name, "gl_FragDepth") == 0) {
1659 switch (var->depth_layout) {
1660 case ir_depth_layout_none:
1661 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1662 return;
1663 case ir_depth_layout_any:
1664 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1665 return;
1666 case ir_depth_layout_greater:
1667 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1668 return;
1669 case ir_depth_layout_less:
1670 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1671 return;
1672 case ir_depth_layout_unchanged:
1673 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1674 return;
1675 default:
1676 assert(0);
1677 return;
1678 }
1679 }
1680 }
1681}
1682
1683/**
Ian Romanick92f81592011-11-08 12:37:19 -08001684 * Validate the resources used by a program versus the implementation limits
1685 */
Paul Berryb95d2372013-07-27 11:08:31 -07001686static void
Ian Romanick92f81592011-11-08 12:37:19 -08001687check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1688{
1689 static const char *const shader_names[MESA_SHADER_TYPES] = {
Marek Olšák030ca232013-06-12 17:15:46 +02001690 "vertex", "geometry", "fragment"
Ian Romanick92f81592011-11-08 12:37:19 -08001691 };
1692
1693 const unsigned max_samplers[MESA_SHADER_TYPES] = {
Marek Olšák5e784332013-05-02 02:30:44 +02001694 ctx->Const.VertexProgram.MaxTextureImageUnits,
Marek Olšák030ca232013-06-12 17:15:46 +02001695 ctx->Const.GeometryProgram.MaxTextureImageUnits,
1696 ctx->Const.FragmentProgram.MaxTextureImageUnits
Ian Romanick92f81592011-11-08 12:37:19 -08001697 };
1698
Eric Anholt38e77e52013-05-23 11:10:15 -07001699 const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = {
Ian Romanick92f81592011-11-08 12:37:19 -08001700 ctx->Const.VertexProgram.MaxUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001701 ctx->Const.GeometryProgram.MaxUniformComponents,
1702 ctx->Const.FragmentProgram.MaxUniformComponents
Ian Romanick92f81592011-11-08 12:37:19 -08001703 };
1704
Eric Anholt38e77e52013-05-23 11:10:15 -07001705 const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = {
1706 ctx->Const.VertexProgram.MaxCombinedUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001707 ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
1708 ctx->Const.FragmentProgram.MaxCombinedUniformComponents
Eric Anholt38e77e52013-05-23 11:10:15 -07001709 };
1710
Eric Anholt877a8972012-06-25 12:47:01 -07001711 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
1712 ctx->Const.VertexProgram.MaxUniformBlocks,
Eric Anholt877a8972012-06-25 12:47:01 -07001713 ctx->Const.GeometryProgram.MaxUniformBlocks,
Marek Olšák030ca232013-06-12 17:15:46 +02001714 ctx->Const.FragmentProgram.MaxUniformBlocks
Eric Anholt877a8972012-06-25 12:47:01 -07001715 };
1716
Ian Romanick92f81592011-11-08 12:37:19 -08001717 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1718 struct gl_shader *sh = prog->_LinkedShaders[i];
1719
1720 if (sh == NULL)
1721 continue;
1722
1723 if (sh->num_samplers > max_samplers[i]) {
1724 linker_error(prog, "Too many %s shader texture samplers",
1725 shader_names[i]);
1726 }
1727
Eric Anholt38e77e52013-05-23 11:10:15 -07001728 if (sh->num_uniform_components > max_default_uniform_components[i]) {
1729 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1730 linker_warning(prog, "Too many %s shader default uniform block "
1731 "components, but the driver will try to optimize "
1732 "them out; this is non-portable out-of-spec "
1733 "behavior\n",
1734 shader_names[i]);
1735 } else {
1736 linker_error(prog, "Too many %s shader default uniform block "
1737 "components",
1738 shader_names[i]);
1739 }
1740 }
1741
1742 if (sh->num_combined_uniform_components >
1743 max_combined_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001744 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1745 linker_warning(prog, "Too many %s shader uniform components, "
1746 "but the driver will try to optimize them out; "
1747 "this is non-portable out-of-spec behavior\n",
1748 shader_names[i]);
1749 } else {
1750 linker_error(prog, "Too many %s shader uniform components",
1751 shader_names[i]);
1752 }
Ian Romanick92f81592011-11-08 12:37:19 -08001753 }
1754 }
1755
Eric Anholt877a8972012-06-25 12:47:01 -07001756 unsigned blocks[MESA_SHADER_TYPES] = {0};
1757 unsigned total_uniform_blocks = 0;
1758
1759 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
1760 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1761 if (prog->UniformBlockStageIndex[j][i] != -1) {
1762 blocks[j]++;
1763 total_uniform_blocks++;
1764 }
1765 }
1766
1767 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1768 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1769 prog->NumUniformBlocks,
1770 ctx->Const.MaxCombinedUniformBlocks);
1771 } else {
1772 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1773 if (blocks[i] > max_uniform_blocks[i]) {
1774 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
1775 shader_names[i],
1776 blocks[i],
1777 max_uniform_blocks[i]);
1778 break;
1779 }
1780 }
1781 }
1782 }
Ian Romanick92f81592011-11-08 12:37:19 -08001783}
Paul Berry871ddb92011-11-05 11:17:32 -07001784
Ian Romanick0e59b262010-06-23 11:23:01 -07001785void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001786link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001787{
Paul Berry871ddb92011-11-05 11:17:32 -07001788 tfeedback_decl *tfeedback_decls = NULL;
1789 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1790
Kenneth Graunked3073f52011-01-21 14:32:31 -08001791 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001792
Paul Berryb95d2372013-07-27 11:08:31 -07001793 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07001794 prog->Validated = false;
1795 prog->_Used = false;
1796
Eric Anholtf609cf72012-04-27 13:52:56 -07001797 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08001798 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07001799
Eric Anholtf609cf72012-04-27 13:52:56 -07001800 ralloc_free(prog->UniformBlocks);
1801 prog->UniformBlocks = NULL;
1802 prog->NumUniformBlocks = 0;
1803 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
1804 ralloc_free(prog->UniformBlockStageIndex[i]);
1805 prog->UniformBlockStageIndex[i] = NULL;
1806 }
1807
Ian Romanick832dfa52010-06-17 15:04:20 -07001808 /* Separate the shaders into groups based on their type.
1809 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001810 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001811 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001812 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001813 unsigned num_frag_shaders = 0;
Bryan Cain25480922013-02-15 09:46:50 -06001814 struct gl_shader **geom_shader_list;
1815 unsigned num_geom_shaders = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001816
Eric Anholt16b68b12010-06-30 11:05:43 -07001817 vert_shader_list = (struct gl_shader **)
Paul Berry844bd712013-07-30 22:38:43 -07001818 calloc(prog->NumShaders, sizeof(struct gl_shader *));
1819 frag_shader_list = (struct gl_shader **)
1820 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Bryan Cain25480922013-02-15 09:46:50 -06001821 geom_shader_list = (struct gl_shader **)
1822 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001823
Ian Romanick25f51d32010-07-16 15:51:50 -07001824 unsigned min_version = UINT_MAX;
1825 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07001826 const bool is_es_prog =
1827 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07001828 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001829 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1830 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1831
Paul Berrya9f34dc2012-08-02 17:49:44 -07001832 if (prog->Shaders[i]->IsES != is_es_prog) {
1833 linker_error(prog, "all shaders must use same shading "
1834 "language version\n");
1835 goto done;
1836 }
1837
Ian Romanick832dfa52010-06-17 15:04:20 -07001838 switch (prog->Shaders[i]->Type) {
1839 case GL_VERTEX_SHADER:
1840 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1841 num_vert_shaders++;
1842 break;
1843 case GL_FRAGMENT_SHADER:
1844 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1845 num_frag_shaders++;
1846 break;
1847 case GL_GEOMETRY_SHADER:
Bryan Cain25480922013-02-15 09:46:50 -06001848 geom_shader_list[num_geom_shaders] = prog->Shaders[i];
1849 num_geom_shaders++;
Ian Romanick832dfa52010-06-17 15:04:20 -07001850 break;
1851 }
1852 }
1853
Ian Romanick25f51d32010-07-16 15:51:50 -07001854 /* Previous to GLSL version 1.30, different compilation units could mix and
1855 * match shading language versions. With GLSL 1.30 and later, the versions
1856 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07001857 *
1858 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07001859 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07001860 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001861 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07001862 linker_error(prog, "all shaders must use same shading "
1863 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07001864 goto done;
1865 }
1866
1867 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07001868 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07001869
Fabian Bielerbd85ba02013-05-24 23:26:54 +02001870 /* Geometry shaders have to be linked with vertex shaders.
1871 */
1872 if (num_geom_shaders > 0 && num_vert_shaders == 0) {
1873 linker_error(prog, "Geometry shader must be linked with "
1874 "vertex shader\n");
1875 goto done;
1876 }
1877
Ian Romanick3322fba2010-10-14 13:28:42 -07001878 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1879 if (prog->_LinkedShaders[i] != NULL)
1880 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1881
1882 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07001883 }
1884
Ian Romanickcd6764e2010-07-16 16:00:07 -07001885 /* Link all shaders for a particular stage and validate the result.
1886 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001887 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001888 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001889 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1890 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001891
Paul Berryb95d2372013-07-27 11:08:31 -07001892 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07001893 goto done;
1894
Paul Berryb95d2372013-07-27 11:08:31 -07001895 validate_vertex_shader_executable(prog, sh);
1896 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001897 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001898
Ian Romanick3322fba2010-10-14 13:28:42 -07001899 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1900 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001901 }
1902
1903 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001904 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001905 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1906 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001907
Paul Berryb95d2372013-07-27 11:08:31 -07001908 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07001909 goto done;
1910
Paul Berryb95d2372013-07-27 11:08:31 -07001911 validate_fragment_shader_executable(prog, sh);
1912 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001913 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001914
Ian Romanick3322fba2010-10-14 13:28:42 -07001915 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1916 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001917 }
1918
Bryan Cain25480922013-02-15 09:46:50 -06001919 if (num_geom_shaders > 0) {
1920 gl_shader *const sh =
1921 link_intrastage_shaders(mem_ctx, ctx, prog, geom_shader_list,
1922 num_geom_shaders);
1923
1924 if (!prog->LinkStatus)
1925 goto done;
1926
1927 validate_geometry_shader_executable(prog, sh);
1928 if (!prog->LinkStatus)
1929 goto done;
1930
1931 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
1932 sh);
1933 }
1934
Ian Romanick3ed850e2010-06-23 12:18:21 -07001935 /* Here begins the inter-stage linking phase. Some initial validation is
1936 * performed, then locations are assigned for uniforms, attributes, and
1937 * varyings.
1938 */
Paul Berryb95d2372013-07-27 11:08:31 -07001939 cross_validate_uniforms(prog);
1940 if (!prog->LinkStatus)
1941 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001942
Paul Berryb95d2372013-07-27 11:08:31 -07001943 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07001944
Paul Berryb95d2372013-07-27 11:08:31 -07001945 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1946 if (prog->_LinkedShaders[prev] != NULL)
1947 break;
1948 }
Ian Romanick3322fba2010-10-14 13:28:42 -07001949
Paul Berryb95d2372013-07-27 11:08:31 -07001950 /* Validate the inputs of each stage with the output of the preceding
1951 * stage.
1952 */
1953 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1954 if (prog->_LinkedShaders[i] == NULL)
1955 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07001956
Paul Berryb95d2372013-07-27 11:08:31 -07001957 validate_interstage_interface_blocks(prog, prog->_LinkedShaders[prev],
1958 prog->_LinkedShaders[i]);
1959 if (!prog->LinkStatus)
1960 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001961
Paul Berryb95d2372013-07-27 11:08:31 -07001962 cross_validate_outputs_to_inputs(prog,
1963 prog->_LinkedShaders[prev],
1964 prog->_LinkedShaders[i]);
1965 if (!prog->LinkStatus)
1966 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07001967
Paul Berryb95d2372013-07-27 11:08:31 -07001968 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07001969 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001970
Jordan Justen5ebf5472013-03-10 03:20:03 -07001971
1972 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1973 if (prog->_LinkedShaders[i] != NULL)
1974 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
1975 }
1976
Eric Anholt3de13952012-05-04 13:08:46 -07001977 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
1978 * it before optimization because we want most of the checks to get
1979 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07001980 *
1981 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07001982 */
Paul Berry15ba2a52012-08-02 17:51:02 -07001983 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07001984 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1985 if (sh) {
1986 lower_discard_flow(sh->ir);
1987 }
1988 }
1989
Eric Anholtf609cf72012-04-27 13:52:56 -07001990 if (!interstage_cross_validate_uniform_blocks(prog))
1991 goto done;
1992
Eric Anholt2f4fe152010-08-10 13:06:49 -07001993 /* Do common optimization before assigning storage for attributes,
1994 * uniforms, and varyings. Later optimization could possibly make
1995 * some of that unused.
1996 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001997 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1998 if (prog->_LinkedShaders[i] == NULL)
1999 continue;
2000
Ian Romanick02c5ae12011-07-11 10:46:01 -07002001 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2002 if (!prog->LinkStatus)
2003 goto done;
2004
Paul Berry18392442012-12-04 11:11:02 -08002005 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2006 lower_clip_distance(prog->_LinkedShaders[i]);
2007 }
Paul Berryc06e3252011-08-11 20:58:21 -07002008
Brian Paul7feabfe2012-03-20 17:43:12 -06002009 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2010
Kenneth Graunkeb7657402013-04-17 17:30:22 -07002011 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002012 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002013 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002014
Paul Berry50895d42012-12-05 07:17:07 -08002015 /* Mark all generic shader inputs and outputs as unpaired. */
2016 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2017 link_invalidate_variable_locations(
2018 prog->_LinkedShaders[MESA_SHADER_VERTEX],
Paul Berry36b252e2013-02-23 07:22:01 -08002019 VERT_ATTRIB_GENERIC0, VARYING_SLOT_VAR0);
Paul Berry50895d42012-12-05 07:17:07 -08002020 }
Bryan Cain25480922013-02-15 09:46:50 -06002021 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2022 link_invalidate_variable_locations(
2023 prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
2024 VARYING_SLOT_VAR0, VARYING_SLOT_VAR0);
2025 }
Paul Berry50895d42012-12-05 07:17:07 -08002026 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2027 link_invalidate_variable_locations(
2028 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
Paul Berryeed6baf2013-02-23 09:00:58 -08002029 VARYING_SLOT_VAR0, FRAG_RESULT_DATA0);
Paul Berry50895d42012-12-05 07:17:07 -08002030 }
2031
Ian Romanickd32d4f72011-06-27 17:59:58 -07002032 /* FINISHME: The value of the max_attribute_index parameter is
2033 * FINISHME: implementation dependent based on the value of
2034 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2035 * FINISHME: at least 16, so hardcode 16 for now.
2036 */
2037 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002038 goto done;
2039 }
2040
Dave Airlie1256a5d2012-03-24 13:33:41 +00002041 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002042 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002043 }
2044
Marek Olšák284d9542013-06-12 02:18:09 +02002045 unsigned first;
2046 for (first = 0; first < MESA_SHADER_TYPES; first++) {
2047 if (prog->_LinkedShaders[first] != NULL)
Ian Romanick3322fba2010-10-14 13:28:42 -07002048 break;
2049 }
2050
Paul Berry871ddb92011-11-05 11:17:32 -07002051 if (num_tfeedback_decls != 0) {
2052 /* From GL_EXT_transform_feedback:
2053 * A program will fail to link if:
2054 *
2055 * * the <count> specified by TransformFeedbackVaryingsEXT is
2056 * non-zero, but the program object has no vertex or geometry
2057 * shader;
2058 */
Bryan Cain25480922013-02-15 09:46:50 -06002059 if (first == MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07002060 linker_error(prog, "Transform feedback varyings specified, but "
2061 "no vertex or geometry shader is present.");
2062 goto done;
2063 }
2064
2065 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2066 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002067 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002068 prog->TransformFeedback.VaryingNames,
2069 tfeedback_decls))
2070 goto done;
2071 }
2072
Marek Olšák284d9542013-06-12 02:18:09 +02002073 /* Linking the stages in the opposite order (from fragment to vertex)
2074 * ensures that inter-shader outputs written to in an earlier stage are
2075 * eliminated if they are (transitively) not used in a later stage.
2076 */
2077 int last, next;
2078 for (last = MESA_SHADER_TYPES-1; last >= 0; last--) {
2079 if (prog->_LinkedShaders[last] != NULL)
2080 break;
Ian Romanick3322fba2010-10-14 13:28:42 -07002081 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002082
Marek Olšák284d9542013-06-12 02:18:09 +02002083 if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
2084 gl_shader *const sh = prog->_LinkedShaders[last];
2085
2086 if (num_tfeedback_decls != 0) {
2087 /* There was no fragment shader, but we still have to assign varying
2088 * locations for use by transform feedback.
2089 */
2090 if (!assign_varying_locations(ctx, mem_ctx, prog,
2091 sh, NULL,
Paul Berry3b0cf702013-04-10 06:48:42 -07002092 num_tfeedback_decls, tfeedback_decls,
2093 0))
Marek Olšák284d9542013-06-12 02:18:09 +02002094 goto done;
2095 }
2096
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002097 do_dead_builtin_varyings(ctx, sh->ir, NULL,
2098 num_tfeedback_decls, tfeedback_decls);
2099
Marek Olšák284d9542013-06-12 02:18:09 +02002100 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
2101
2102 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07002103 */
Marek Olšák284d9542013-06-12 02:18:09 +02002104 while (do_dead_code(sh->ir, false))
2105 ;
2106 }
2107 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002108 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02002109 */
2110 gl_shader *const sh = prog->_LinkedShaders[first];
2111
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002112 do_dead_builtin_varyings(ctx, NULL, sh->ir,
2113 num_tfeedback_decls, tfeedback_decls);
2114
Marek Olšák284d9542013-06-12 02:18:09 +02002115 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
2116
2117 while (do_dead_code(sh->ir, false))
2118 ;
2119 }
2120
2121 next = last;
2122 for (int i = next - 1; i >= 0; i--) {
2123 if (prog->_LinkedShaders[i] == NULL)
2124 continue;
2125
2126 gl_shader *const sh_i = prog->_LinkedShaders[i];
2127 gl_shader *const sh_next = prog->_LinkedShaders[next];
Paul Berry3b0cf702013-04-10 06:48:42 -07002128 unsigned gs_input_vertices =
2129 next == MESA_SHADER_GEOMETRY ? prog->Geom.VerticesIn : 0;
Marek Olšák284d9542013-06-12 02:18:09 +02002130
2131 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
2132 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
Paul Berry3b0cf702013-04-10 06:48:42 -07002133 tfeedback_decls, gs_input_vertices))
Paul Berry871ddb92011-11-05 11:17:32 -07002134 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02002135
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002136 do_dead_builtin_varyings(ctx, sh_i->ir, sh_next->ir,
2137 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2138 tfeedback_decls);
2139
Marek Olšák284d9542013-06-12 02:18:09 +02002140 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
2141 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
2142
2143 /* Eliminate code that is now dead due to unused outputs being demoted.
2144 */
2145 while (do_dead_code(sh_i->ir, false))
2146 ;
2147 while (do_dead_code(sh_next->ir, false))
2148 ;
2149
Marek Olšák3c555822013-06-13 03:17:22 +02002150 /* This must be done after all dead varyings are eliminated. */
2151 if (!check_against_varying_limit(ctx, prog, sh_next))
2152 goto done;
2153
Marek Olšák284d9542013-06-12 02:18:09 +02002154 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07002155 }
2156
2157 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2158 goto done;
2159
Ian Romanick960d7222011-10-21 11:21:02 -07002160 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002161 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002162 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002163
Paul Berryb95d2372013-07-27 11:08:31 -07002164 check_resources(ctx, prog);
2165 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08002166 goto done;
2167
Ian Romanickce9171f2011-02-03 17:10:14 -08002168 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002169 * present in a linked program. By checking prog->IsES, we also
2170 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002171 */
Eric Anholt57f79782011-07-22 12:57:47 -07002172 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002173 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002174 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002175 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002176 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002177 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002178 }
2179 }
2180
Ian Romanick13e10e42010-06-21 12:03:24 -07002181 /* FINISHME: Assign fragment shader output locations. */
2182
Ian Romanick832dfa52010-06-17 15:04:20 -07002183done:
2184 free(vert_shader_list);
Paul Berry844bd712013-07-30 22:38:43 -07002185 free(frag_shader_list);
Bryan Cain25480922013-02-15 09:46:50 -06002186 free(geom_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002187
2188 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2189 if (prog->_LinkedShaders[i] == NULL)
2190 continue;
2191
2192 /* Retain any live IR, but trash the rest. */
2193 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002194
2195 /* The symbol table in the linked shaders may contain references to
2196 * variables that were removed (e.g., unused uniforms). Since it may
2197 * contain junk, there is no possible valid use. Delete it and set the
2198 * pointer to NULL.
2199 */
2200 delete prog->_LinkedShaders[i]->symbols;
2201 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002202 }
2203
Kenneth Graunked3073f52011-01-21 14:32:31 -08002204 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002205}