blob: db1fe9a0d85fc4be063be0ade998c1e6122dacaf [file] [log] [blame]
Ian Romanick832dfa52010-06-17 15:04:20 -07001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * \file linker.cpp
26 * GLSL linker implementation
27 *
28 * Given a set of shaders that are to be linked to generate a final program,
29 * there are three distinct stages.
30 *
31 * In the first stage shaders are partitioned into groups based on the shader
32 * type. All shaders of a particular type (e.g., vertex shaders) are linked
33 * together.
34 *
35 * - Undefined references in each shader are resolve to definitions in
36 * another shader.
37 * - Types and qualifiers of uniforms, outputs, and global variables defined
38 * in multiple shaders with the same name are verified to be the same.
39 * - Initializers for uniforms and global variables defined
40 * in multiple shaders with the same name are verified to be the same.
41 *
42 * The result, in the terminology of the GLSL spec, is a set of shader
43 * executables for each processing unit.
44 *
45 * After the first stage is complete, a series of semantic checks are performed
46 * on each of the shader executables.
47 *
48 * - Each shader executable must define a \c main function.
49 * - Each vertex shader executable must write to \c gl_Position.
50 * - Each fragment shader executable must write to either \c gl_FragData or
51 * \c gl_FragColor.
52 *
53 * In the final stage individual shader executables are linked to create a
54 * complete exectuable.
55 *
56 * - Types of uniforms defined in multiple shader stages with the same name
57 * are verified to be the same.
58 * - Initializers for uniforms defined in multiple shader stages with the
59 * same name are verified to be the same.
60 * - Types and qualifiers of outputs defined in one stage are verified to
61 * be the same as the types and qualifiers of inputs defined with the same
62 * name in a later stage.
63 *
64 * \author Ian Romanick <ian.d.romanick@intel.com>
65 */
Ian Romanickf36460e2010-06-23 12:07:22 -070066
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080067#include "main/core.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070068#include "glsl_symbol_table.h"
Eric Anholtfaf3dba2013-06-12 16:57:11 -070069#include "glsl_parser_extras.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070070#include "ir.h"
71#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030072#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070073#include "linker.h"
Paul Berry4b11b572012-12-17 14:20:35 -080074#include "link_varyings.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070075#include "ir_optimization.h"
Bryan Cain25480922013-02-15 09:46:50 -060076#include "ir_rvalue_visitor.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070077
Ian Romanick3322fba2010-10-14 13:28:42 -070078extern "C" {
79#include "main/shaderobj.h"
Eric Anholt6065a872013-06-12 18:12:40 -070080#include "main/enums.h"
Ian Romanick3322fba2010-10-14 13:28:42 -070081}
82
Bryan Cain25480922013-02-15 09:46:50 -060083void linker_error(gl_shader_program *, const char *, ...);
84
Eric Anholt10ef9492013-09-20 11:03:44 -070085namespace {
86
Ian Romanick832dfa52010-06-17 15:04:20 -070087/**
88 * Visitor that determines whether or not a variable is ever written.
89 */
90class find_assignment_visitor : public ir_hierarchical_visitor {
91public:
92 find_assignment_visitor(const char *name)
93 : name(name), found(false)
94 {
95 /* empty */
96 }
97
98 virtual ir_visitor_status visit_enter(ir_assignment *ir)
99 {
100 ir_variable *const var = ir->lhs->variable_referenced();
101
102 if (strcmp(name, var->name) == 0) {
103 found = true;
104 return visit_stop;
105 }
106
107 return visit_continue_with_parent;
108 }
109
Eric Anholt18a60232010-08-23 11:29:25 -0700110 virtual ir_visitor_status visit_enter(ir_call *ir)
111 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700112 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700113 foreach_iter(exec_list_iterator, iter, *ir) {
114 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
115 ir_variable *sig_param = (ir_variable *)sig_iter.get();
116
Paul Berry42a29d82013-01-11 14:39:32 -0800117 if (sig_param->mode == ir_var_function_out ||
118 sig_param->mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700119 ir_variable *var = param_rval->variable_referenced();
120 if (var && strcmp(name, var->name) == 0) {
121 found = true;
122 return visit_stop;
123 }
124 }
125 sig_iter.next();
126 }
127
Kenneth Graunked884f602012-03-20 15:56:37 -0700128 if (ir->return_deref != NULL) {
129 ir_variable *const var = ir->return_deref->variable_referenced();
130
131 if (strcmp(name, var->name) == 0) {
132 found = true;
133 return visit_stop;
134 }
135 }
136
Eric Anholt18a60232010-08-23 11:29:25 -0700137 return visit_continue_with_parent;
138 }
139
Ian Romanick832dfa52010-06-17 15:04:20 -0700140 bool variable_found()
141 {
142 return found;
143 }
144
145private:
146 const char *name; /**< Find writes to a variable with this name. */
147 bool found; /**< Was a write to the variable found? */
148};
149
Ian Romanickc93b8f12010-06-17 15:20:22 -0700150
Ian Romanickc33e78f2010-08-13 12:30:41 -0700151/**
152 * Visitor that determines whether or not a variable is ever read.
153 */
154class find_deref_visitor : public ir_hierarchical_visitor {
155public:
156 find_deref_visitor(const char *name)
157 : name(name), found(false)
158 {
159 /* empty */
160 }
161
162 virtual ir_visitor_status visit(ir_dereference_variable *ir)
163 {
164 if (strcmp(this->name, ir->var->name) == 0) {
165 this->found = true;
166 return visit_stop;
167 }
168
169 return visit_continue;
170 }
171
172 bool variable_found() const
173 {
174 return this->found;
175 }
176
177private:
178 const char *name; /**< Find writes to a variable with this name. */
179 bool found; /**< Was a write to the variable found? */
180};
181
182
Paul Berry7cfefe62013-07-30 21:13:48 -0700183class geom_array_resize_visitor : public ir_hierarchical_visitor {
184public:
185 unsigned num_vertices;
186 gl_shader_program *prog;
187
188 geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
189 {
190 this->num_vertices = num_vertices;
191 this->prog = prog;
192 }
193
194 virtual ~geom_array_resize_visitor()
195 {
196 /* empty */
197 }
198
199 virtual ir_visitor_status visit(ir_variable *var)
200 {
201 if (!var->type->is_array() || var->mode != ir_var_shader_in)
202 return visit_continue;
203
204 unsigned size = var->type->length;
205
206 /* Generate a link error if the shader has declared this array with an
207 * incorrect size.
208 */
209 if (size && size != this->num_vertices) {
210 linker_error(this->prog, "size of array %s declared as %u, "
211 "but number of input vertices is %u\n",
212 var->name, size, this->num_vertices);
213 return visit_continue;
214 }
215
216 /* Generate a link error if the shader attempts to access an input
217 * array using an index too large for its actual size assigned at link
218 * time.
219 */
220 if (var->max_array_access >= this->num_vertices) {
221 linker_error(this->prog, "geometry shader accesses element %i of "
222 "%s, but only %i input vertices\n",
223 var->max_array_access, var->name, this->num_vertices);
224 return visit_continue;
225 }
226
227 var->type = glsl_type::get_array_instance(var->type->element_type(),
228 this->num_vertices);
229 var->max_array_access = this->num_vertices - 1;
230
231 return visit_continue;
232 }
233
234 /* Dereferences of input variables need to be updated so that their type
235 * matches the newly assigned type of the variable they are accessing. */
236 virtual ir_visitor_status visit(ir_dereference_variable *ir)
237 {
238 ir->type = ir->var->type;
239 return visit_continue;
240 }
241
242 /* Dereferences of 2D input arrays need to be updated so that their type
243 * matches the newly assigned type of the array they are accessing. */
244 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
245 {
246 const glsl_type *const vt = ir->array->type;
247 if (vt->is_array())
248 ir->type = vt->element_type();
249 return visit_continue;
250 }
251};
252
253
Paul Berry1a33e022013-08-18 20:59:37 -0700254/**
255 * Visitor that determines whether or not a shader uses ir_end_primitive.
256 */
257class find_end_primitive_visitor : public ir_hierarchical_visitor {
258public:
259 find_end_primitive_visitor()
260 : found(false)
261 {
262 /* empty */
263 }
264
265 virtual ir_visitor_status visit(ir_end_primitive *)
266 {
267 found = true;
268 return visit_stop;
269 }
270
271 bool end_primitive_found()
272 {
273 return found;
274 }
275
276private:
277 bool found;
278};
279
Eric Anholt10ef9492013-09-20 11:03:44 -0700280} /* anonymous namespace */
Paul Berry1a33e022013-08-18 20:59:37 -0700281
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700282void
Ian Romanick586e7412011-07-28 14:04:09 -0700283linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700284{
285 va_list ap;
286
Kenneth Graunked3073f52011-01-21 14:32:31 -0800287 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700288 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800289 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700290 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700291
292 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700293}
294
295
296void
Ian Romanick379a32f2011-07-28 14:09:06 -0700297linker_warning(gl_shader_program *prog, const char *fmt, ...)
298{
299 va_list ap;
300
301 ralloc_strcat(&prog->InfoLog, "error: ");
302 va_start(ap, fmt);
303 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
304 va_end(ap);
305
306}
307
308
Paul Berryb92900d2013-01-28 14:21:59 -0800309/**
310 * Given a string identifying a program resource, break it into a base name
311 * and an optional array index in square brackets.
312 *
313 * If an array index is present, \c out_base_name_end is set to point to the
314 * "[" that precedes the array index, and the array index itself is returned
315 * as a long.
316 *
317 * If no array index is present (or if the array index is negative or
318 * mal-formed), \c out_base_name_end, is set to point to the null terminator
319 * at the end of the input string, and -1 is returned.
320 *
321 * Only the final array index is parsed; if the string contains other array
322 * indices (or structure field accesses), they are left in the base name.
323 *
324 * No attempt is made to check that the base name is properly formed;
325 * typically the caller will look up the base name in a hash table, so
326 * ill-formed base names simply turn into hash table lookup failures.
327 */
328long
329parse_program_resource_name(const GLchar *name,
330 const GLchar **out_base_name_end)
331{
332 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
333 *
334 * "When an integer array element or block instance number is part of
335 * the name string, it will be specified in decimal form without a "+"
336 * or "-" sign or any extra leading zeroes. Additionally, the name
337 * string will not include white space anywhere in the string."
338 */
339
340 const size_t len = strlen(name);
341 *out_base_name_end = name + len;
342
343 if (len == 0 || name[len-1] != ']')
344 return -1;
345
346 /* Walk backwards over the string looking for a non-digit character. This
347 * had better be the opening bracket for an array index.
348 *
349 * Initially, i specifies the location of the ']'. Since the string may
350 * contain only the ']' charcater, walk backwards very carefully.
351 */
352 unsigned i;
353 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
354 /* empty */ ;
355
356 if ((i == 0) || name[i-1] != '[')
357 return -1;
358
359 long array_index = strtol(&name[i], NULL, 10);
360 if (array_index < 0)
361 return -1;
362
363 *out_base_name_end = name + (i - 1);
364 return array_index;
365}
366
367
Ian Romanick379a32f2011-07-28 14:09:06 -0700368void
Paul Berry50895d42012-12-05 07:17:07 -0800369link_invalidate_variable_locations(gl_shader *sh, int input_base,
370 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700371{
Eric Anholt16b68b12010-06-30 11:05:43 -0700372 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700373 ir_variable *const var = ((ir_instruction *) node)->as_variable();
374
Paul Berry50895d42012-12-05 07:17:07 -0800375 if (var == NULL)
376 continue;
377
378 int base;
379 switch (var->mode) {
Paul Berry42a29d82013-01-11 14:39:32 -0800380 case ir_var_shader_in:
Paul Berry50895d42012-12-05 07:17:07 -0800381 base = input_base;
382 break;
Paul Berry42a29d82013-01-11 14:39:32 -0800383 case ir_var_shader_out:
Paul Berry50895d42012-12-05 07:17:07 -0800384 base = output_base;
385 break;
386 default:
387 continue;
388 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700389
390 /* Only assign locations for generic attributes / varyings / etc.
391 */
Paul Berry50895d42012-12-05 07:17:07 -0800392 if ((var->location >= base) && !var->explicit_location)
393 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800394
Paul Berry3e81c662012-12-05 10:47:55 -0800395 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800396 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800397 var->location_frac = 0;
398 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800399 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800400 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700401 }
402}
403
404
Ian Romanickc93b8f12010-06-17 15:20:22 -0700405/**
Paul Berry44e07de2013-06-11 14:11:05 -0700406 * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
407 *
408 * Also check for errors based on incorrect usage of gl_ClipVertex and
409 * gl_ClipDistance.
410 *
411 * Return false if an error was reported.
412 */
413static void
414analyze_clip_usage(const char *shader_type, struct gl_shader_program *prog,
415 struct gl_shader *shader, GLboolean *UsesClipDistance,
416 GLuint *ClipDistanceArraySize)
417{
418 *ClipDistanceArraySize = 0;
419
420 if (!prog->IsES && prog->Version >= 130) {
421 /* From section 7.1 (Vertex Shader Special Variables) of the
422 * GLSL 1.30 spec:
423 *
424 * "It is an error for a shader to statically write both
425 * gl_ClipVertex and gl_ClipDistance."
426 *
427 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
428 * gl_ClipVertex nor gl_ClipDistance.
429 */
430 find_assignment_visitor clip_vertex("gl_ClipVertex");
431 find_assignment_visitor clip_distance("gl_ClipDistance");
432
433 clip_vertex.run(shader->ir);
434 clip_distance.run(shader->ir);
435 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
436 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
437 "and `gl_ClipDistance'\n", shader_type);
438 return;
439 }
440 *UsesClipDistance = clip_distance.variable_found();
441 ir_variable *clip_distance_var =
442 shader->symbols->get_variable("gl_ClipDistance");
443 if (clip_distance_var)
444 *ClipDistanceArraySize = clip_distance_var->type->length;
445 } else {
446 *UsesClipDistance = false;
447 }
448}
449
450
451/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700452 * Verify that a vertex shader executable meets all semantic requirements.
453 *
Paul Berry642e5b412012-01-04 13:57:52 -0800454 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
455 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700456 *
457 * \param shader Vertex shader executable to be verified
458 */
Paul Berryb95d2372013-07-27 11:08:31 -0700459void
Eric Anholt849e1812010-06-30 11:49:17 -0700460validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700461 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700462{
463 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700464 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700465
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700466 /* From the GLSL 1.10 spec, page 48:
467 *
468 * "The variable gl_Position is available only in the vertex
469 * language and is intended for writing the homogeneous vertex
470 * position. All executions of a well-formed vertex shader
471 * executable must write a value into this variable. [...] The
472 * variable gl_Position is available only in the vertex
473 * language and is intended for writing the homogeneous vertex
474 * position. All executions of a well-formed vertex shader
475 * executable must write a value into this variable."
476 *
477 * while in GLSL 1.40 this text is changed to:
478 *
479 * "The variable gl_Position is available only in the vertex
480 * language and is intended for writing the homogeneous vertex
481 * position. It can be written at any time during shader
482 * execution. It may also be read back by a vertex shader
483 * after being written. This value will be used by primitive
484 * assembly, clipping, culling, and other fixed functionality
485 * operations, if present, that operate on primitives after
486 * vertex processing has occurred. Its value is undefined if
487 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700488 *
489 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
490 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700491 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700492 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700493 find_assignment_visitor find("gl_Position");
494 find.run(shader->ir);
495 if (!find.variable_found()) {
496 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700497 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700498 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700499 }
500
Paul Berry44e07de2013-06-11 14:11:05 -0700501 analyze_clip_usage("vertex", prog, shader, &prog->Vert.UsesClipDistance,
502 &prog->Vert.ClipDistanceArraySize);
Ian Romanick832dfa52010-06-17 15:04:20 -0700503}
504
505
Ian Romanickc93b8f12010-06-17 15:20:22 -0700506/**
507 * Verify that a fragment shader executable meets all semantic requirements
508 *
509 * \param shader Fragment shader executable to be verified
510 */
Paul Berryb95d2372013-07-27 11:08:31 -0700511void
Eric Anholt849e1812010-06-30 11:49:17 -0700512validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700513 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700514{
515 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700516 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700517
Ian Romanick832dfa52010-06-17 15:04:20 -0700518 find_assignment_visitor frag_color("gl_FragColor");
519 find_assignment_visitor frag_data("gl_FragData");
520
Eric Anholt16b68b12010-06-30 11:05:43 -0700521 frag_color.run(shader->ir);
522 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700523
Ian Romanick832dfa52010-06-17 15:04:20 -0700524 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700525 linker_error(prog, "fragment shader writes to both "
526 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700527 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700528}
529
Bryan Cain25480922013-02-15 09:46:50 -0600530/**
531 * Verify that a geometry shader executable meets all semantic requirements
532 *
Paul Berry44e07de2013-06-11 14:11:05 -0700533 * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
534 * prog->Geom.ClipDistanceArraySize as a side effect.
Bryan Cain25480922013-02-15 09:46:50 -0600535 *
536 * \param shader Geometry shader executable to be verified
537 */
538void
539validate_geometry_shader_executable(struct gl_shader_program *prog,
540 struct gl_shader *shader)
541{
542 if (shader == NULL)
543 return;
544
545 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
546 prog->Geom.VerticesIn = num_vertices;
Paul Berry44e07de2013-06-11 14:11:05 -0700547
548 analyze_clip_usage("geometry", prog, shader, &prog->Geom.UsesClipDistance,
549 &prog->Geom.ClipDistanceArraySize);
Paul Berry1a33e022013-08-18 20:59:37 -0700550
551 find_end_primitive_visitor end_primitive;
552 end_primitive.run(shader->ir);
553 prog->Geom.UsesEndPrimitive = end_primitive.end_primitive_found();
Bryan Cain25480922013-02-15 09:46:50 -0600554}
555
Ian Romanick832dfa52010-06-17 15:04:20 -0700556
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700557/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700558 * Generate a string describing the mode of a variable
559 */
560static const char *
561mode_string(const ir_variable *var)
562{
563 switch (var->mode) {
564 case ir_var_auto:
565 return (var->read_only) ? "global constant" : "global variable";
566
Paul Berry42a29d82013-01-11 14:39:32 -0800567 case ir_var_uniform: return "uniform";
568 case ir_var_shader_in: return "shader input";
569 case ir_var_shader_out: return "shader output";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700570
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800571 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700572 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700573 default:
574 assert(!"Should not get here.");
575 return "invalid variable";
576 }
577}
578
579
580/**
581 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700582 */
Paul Berryb95d2372013-07-27 11:08:31 -0700583void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700584cross_validate_globals(struct gl_shader_program *prog,
585 struct gl_shader **shader_list,
586 unsigned num_shaders,
587 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700588{
589 /* Examine all of the uniforms in all of the shaders and cross validate
590 * them.
591 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700592 glsl_symbol_table variables;
593 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700594 if (shader_list[i] == NULL)
595 continue;
596
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700597 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700598 ir_variable *const var = ((ir_instruction *) node)->as_variable();
599
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700600 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700601 continue;
602
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700603 if (uniforms_only && (var->mode != ir_var_uniform))
604 continue;
605
Ian Romanick7e2aa912010-07-19 17:12:42 -0700606 /* Don't cross validate temporaries that are at global scope. These
607 * will eventually get pulled into the shaders 'main'.
608 */
609 if (var->mode == ir_var_temporary)
610 continue;
611
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700612 /* If a global with this name has already been seen, verify that the
613 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700614 * initializers, the values of the initializers must be the same.
615 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700616 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700617 if (existing != NULL) {
618 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700619 /* Consider the types to be "the same" if both types are arrays
620 * of the same type and one of the arrays is implicitly sized.
621 * In addition, set the type of the linked variable to the
622 * explicitly sized array.
623 */
624 if (var->type->is_array()
625 && existing->type->is_array()
626 && (var->type->fields.array == existing->type->fields.array)
627 && ((var->type->length == 0)
628 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800629 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700630 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800631 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700632 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700633 linker_error(prog, "%s `%s' declared as type "
634 "`%s' and type `%s'\n",
635 mode_string(var),
636 var->name, var->type->name,
637 existing->type->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700638 return;
Ian Romanicka2711d62010-08-29 22:07:49 -0700639 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700640 }
641
Ian Romanick68a4fc92010-10-07 17:21:22 -0700642 if (var->explicit_location) {
643 if (existing->explicit_location
644 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700645 linker_error(prog, "explicit locations for %s "
646 "`%s' have differing values\n",
647 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700648 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700649 }
650
651 existing->location = var->location;
652 existing->explicit_location = true;
653 }
654
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700655 /* From the GLSL 4.20 specification:
656 * "A link error will result if two compilation units in a program
657 * specify different integer-constant bindings for the same
658 * opaque-uniform name. However, it is not an error to specify a
659 * binding on some but not all declarations for the same name"
660 */
661 if (var->explicit_binding) {
662 if (existing->explicit_binding &&
663 var->binding != existing->binding) {
664 linker_error(prog, "explicit bindings for %s "
665 "`%s' have differing values\n",
666 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700667 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700668 }
669
670 existing->binding = var->binding;
671 existing->explicit_binding = true;
672 }
673
Ian Romanick46173f92011-10-31 13:07:06 -0700674 /* Validate layout qualifiers for gl_FragDepth.
675 *
676 * From the AMD/ARB_conservative_depth specs:
677 *
678 * "If gl_FragDepth is redeclared in any fragment shader in a
679 * program, it must be redeclared in all fragment shaders in
680 * that program that have static assignments to
681 * gl_FragDepth. All redeclarations of gl_FragDepth in all
682 * fragment shaders in a single program must have the same set
683 * of qualifiers."
684 */
685 if (strcmp(var->name, "gl_FragDepth") == 0) {
686 bool layout_declared = var->depth_layout != ir_depth_layout_none;
687 bool layout_differs =
688 var->depth_layout != existing->depth_layout;
689
690 if (layout_declared && layout_differs) {
691 linker_error(prog,
692 "All redeclarations of gl_FragDepth in all "
693 "fragment shaders in a single program must have "
694 "the same set of qualifiers.");
695 }
696
697 if (var->used && layout_differs) {
698 linker_error(prog,
699 "If gl_FragDepth is redeclared with a layout "
700 "qualifier in any fragment shader, it must be "
701 "redeclared with the same layout qualifier in "
702 "all fragment shaders that have assignments to "
703 "gl_FragDepth");
704 }
705 }
Chad Versaceaddae332011-01-27 01:40:31 -0800706
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700707 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
708 *
709 * "If a shared global has multiple initializers, the
710 * initializers must all be constant expressions, and they
711 * must all have the same value. Otherwise, a link error will
712 * result. (A shared global having only one initializer does
713 * not require that initializer to be a constant expression.)"
714 *
715 * Previous to 4.20 the GLSL spec simply said that initializers
716 * must have the same value. In this case of non-constant
717 * initializers, this was impossible to determine. As a result,
718 * no vendor actually implemented that behavior. The 4.20
719 * behavior matches the implemented behavior of at least one other
720 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700721 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700722 if (var->constant_initializer != NULL) {
723 if (existing->constant_initializer != NULL) {
724 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700725 linker_error(prog, "initializers for %s "
726 "`%s' have differing values\n",
727 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700728 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700729 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700730 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700731 /* If the first-seen instance of a particular uniform did not
732 * have an initializer but a later instance does, copy the
733 * initializer to the version stored in the symbol table.
734 */
Ian Romanickde415b72010-07-14 13:22:12 -0700735 /* FINISHME: This is wrong. The constant_value field should
736 * FINISHME: not be modified! Imagine a case where a shader
737 * FINISHME: without an initializer is linked in two different
738 * FINISHME: programs with shaders that have differing
739 * FINISHME: initializers. Linking with the first will
740 * FINISHME: modify the shader, and linking with the second
741 * FINISHME: will fail.
742 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700743 existing->constant_initializer =
744 var->constant_initializer->clone(ralloc_parent(existing),
745 NULL);
746 }
747 }
748
749 if (var->has_initializer) {
750 if (existing->has_initializer
751 && (var->constant_initializer == NULL
752 || existing->constant_initializer == NULL)) {
753 linker_error(prog,
754 "shared global variable `%s' has multiple "
755 "non-constant initializers.\n",
756 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700757 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700758 }
759
760 /* Some instance had an initializer, so keep track of that. In
761 * this location, all sorts of initializers (constant or
762 * otherwise) will propagate the existence to the variable
763 * stored in the symbol table.
764 */
765 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700766 }
Chad Versace7528f142010-11-17 14:34:38 -0800767
768 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700769 linker_error(prog, "declarations for %s `%s' have "
770 "mismatching invariant qualifiers\n",
771 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700772 return;
Chad Versace7528f142010-11-17 14:34:38 -0800773 }
Chad Versace61428dd2011-01-10 15:29:30 -0800774 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700775 linker_error(prog, "declarations for %s `%s' have "
776 "mismatching centroid qualifiers\n",
777 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700778 return;
Chad Versace61428dd2011-01-10 15:29:30 -0800779 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700780 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700781 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700782 }
783 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700784}
785
786
Ian Romanick37101922010-06-18 19:02:10 -0700787/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700788 * Perform validation of uniforms used across multiple shader stages
789 */
Paul Berryb95d2372013-07-27 11:08:31 -0700790void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700791cross_validate_uniforms(struct gl_shader_program *prog)
792{
Paul Berryb95d2372013-07-27 11:08:31 -0700793 cross_validate_globals(prog, prog->_LinkedShaders,
794 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700795}
796
Eric Anholtf609cf72012-04-27 13:52:56 -0700797/**
798 * Accumulates the array of prog->UniformBlocks and checks that all
799 * definitons of blocks agree on their contents.
800 */
801static bool
802interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
803{
804 unsigned max_num_uniform_blocks = 0;
805 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
806 if (prog->_LinkedShaders[i])
807 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
808 }
809
810 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
811 struct gl_shader *sh = prog->_LinkedShaders[i];
812
813 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
814 max_num_uniform_blocks);
815 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
816 prog->UniformBlockStageIndex[i][j] = -1;
817
818 if (sh == NULL)
819 continue;
820
821 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
822 int index = link_cross_validate_uniform_block(prog,
823 &prog->UniformBlocks,
824 &prog->NumUniformBlocks,
825 &sh->UniformBlocks[j]);
826
827 if (index == -1) {
828 linker_error(prog, "uniform block `%s' has mismatching definitions",
829 sh->UniformBlocks[j].Name);
830 return false;
831 }
832
833 prog->UniformBlockStageIndex[i][index] = j;
834 }
835 }
836
837 return true;
838}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700839
Ian Romanick37101922010-06-18 19:02:10 -0700840
Ian Romanick3fb87872010-07-09 14:09:34 -0700841/**
842 * Populates a shaders symbol table with all global declarations
843 */
844static void
845populate_symbol_table(gl_shader *sh)
846{
847 sh->symbols = new(sh) glsl_symbol_table;
848
849 foreach_list(node, sh->ir) {
850 ir_instruction *const inst = (ir_instruction *) node;
851 ir_variable *var;
852 ir_function *func;
853
854 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700855 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700856 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700857 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700858 }
859 }
860}
861
862
863/**
Ian Romanick31a97862010-07-12 18:48:50 -0700864 * Remap variables referenced in an instruction tree
865 *
866 * This is used when instruction trees are cloned from one shader and placed in
867 * another. These trees will contain references to \c ir_variable nodes that
868 * do not exist in the target shader. This function finds these \c ir_variable
869 * references and replaces the references with matching variables in the target
870 * shader.
871 *
872 * If there is no matching variable in the target shader, a clone of the
873 * \c ir_variable is made and added to the target shader. The new variable is
874 * added to \b both the instruction stream and the symbol table.
875 *
876 * \param inst IR tree that is to be processed.
877 * \param symbols Symbol table containing global scope symbols in the
878 * linked shader.
879 * \param instructions Instruction stream where new variable declarations
880 * should be added.
881 */
882void
Eric Anholt8273bd42010-08-04 12:34:56 -0700883remap_variables(ir_instruction *inst, struct gl_shader *target,
884 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700885{
886 class remap_visitor : public ir_hierarchical_visitor {
887 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700888 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700889 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700890 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700891 this->target = target;
892 this->symbols = target->symbols;
893 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700894 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700895 }
896
897 virtual ir_visitor_status visit(ir_dereference_variable *ir)
898 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700899 if (ir->var->mode == ir_var_temporary) {
900 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
901
902 assert(var != NULL);
903 ir->var = var;
904 return visit_continue;
905 }
906
Ian Romanick31a97862010-07-12 18:48:50 -0700907 ir_variable *const existing =
908 this->symbols->get_variable(ir->var->name);
909 if (existing != NULL)
910 ir->var = existing;
911 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700912 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700913
Eric Anholt001eee52010-11-05 06:11:24 -0700914 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700915 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700916 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700917 }
918
919 return visit_continue;
920 }
921
922 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700923 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700924 glsl_symbol_table *symbols;
925 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700926 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700927 };
928
Eric Anholt8273bd42010-08-04 12:34:56 -0700929 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700930
931 inst->accept(&v);
932}
933
934
935/**
936 * Move non-declarations from one instruction stream to another
937 *
938 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700939 * 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 -0700940 * pointer) for \c last and \c false for \c make_copies on the first
941 * call. Successive calls pass the return value of the previous call for
942 * \c last and \c true for \c make_copies.
943 *
944 * \param instructions Source instruction stream
945 * \param last Instruction after which new instructions should be
946 * inserted in the target instruction stream
947 * \param make_copies Flag selecting whether instructions in \c instructions
948 * should be copied (via \c ir_instruction::clone) into the
949 * target list or moved.
950 *
951 * \return
952 * The new "last" instruction in the target instruction stream. This pointer
953 * is suitable for use as the \c last parameter of a later call to this
954 * function.
955 */
956exec_node *
957move_non_declarations(exec_list *instructions, exec_node *last,
958 bool make_copies, gl_shader *target)
959{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700960 hash_table *temps = NULL;
961
962 if (make_copies)
963 temps = hash_table_ctor(0, hash_table_pointer_hash,
964 hash_table_pointer_compare);
965
Ian Romanick303c99f2010-07-19 12:34:56 -0700966 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700967 ir_instruction *inst = (ir_instruction *) node;
968
Ian Romanick7e2aa912010-07-19 17:12:42 -0700969 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700970 continue;
971
Ian Romanick7e2aa912010-07-19 17:12:42 -0700972 ir_variable *var = inst->as_variable();
973 if ((var != NULL) && (var->mode != ir_var_temporary))
974 continue;
975
976 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700977 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700978 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700979 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700980
981 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700982 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700983
984 if (var != NULL)
985 hash_table_insert(temps, inst, var);
986 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700987 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700988 } else {
989 inst->remove();
990 }
991
992 last->insert_after(inst);
993 last = inst;
994 }
995
Ian Romanick7e2aa912010-07-19 17:12:42 -0700996 if (make_copies)
997 hash_table_dtor(temps);
998
Ian Romanick31a97862010-07-12 18:48:50 -0700999 return last;
1000}
1001
1002/**
Ian Romanick15ce87e2010-07-09 15:28:22 -07001003 * Get the function signature for main from a shader
1004 */
1005static ir_function_signature *
1006get_main_function_signature(gl_shader *sh)
1007{
1008 ir_function *const f = sh->symbols->get_function("main");
1009 if (f != NULL) {
1010 exec_list void_parameters;
1011
1012 /* Look for the 'void main()' signature and ensure that it's defined.
1013 * This keeps the linker from accidentally pick a shader that just
1014 * contains a prototype for main.
1015 *
1016 * We don't have to check for multiple definitions of main (in multiple
1017 * shaders) because that would have already been caught above.
1018 */
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001019 ir_function_signature *sig = f->matching_signature(NULL, &void_parameters);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001020 if ((sig != NULL) && sig->is_defined) {
1021 return sig;
1022 }
1023 }
1024
1025 return NULL;
1026}
1027
1028
1029/**
Brian Paul84a12732012-02-02 20:10:40 -07001030 * This class is only used in link_intrastage_shaders() below but declaring
1031 * it inside that function leads to compiler warnings with some versions of
1032 * gcc.
1033 */
1034class array_sizing_visitor : public ir_hierarchical_visitor {
1035public:
1036 virtual ir_visitor_status visit(ir_variable *var)
1037 {
Paul Berrye2266692013-09-23 10:44:19 -07001038 fixup_type(&var->type, var->max_array_access);
1039 if (var->type->is_interface()) {
1040 if (interface_contains_unsized_arrays(var->type)) {
1041 const glsl_type *new_type =
1042 resize_interface_members(var->type, var->max_ifc_array_access);
1043 var->type = new_type;
1044 var->change_interface_type(new_type);
1045 }
1046 } else if (var->type->is_array() &&
1047 var->type->fields.array->is_interface()) {
1048 if (interface_contains_unsized_arrays(var->type->fields.array)) {
1049 const glsl_type *new_type =
1050 resize_interface_members(var->type->fields.array,
1051 var->max_ifc_array_access);
1052 var->change_interface_type(new_type);
1053 var->type =
1054 glsl_type::get_array_instance(new_type, var->type->length);
1055 }
Brian Paul84a12732012-02-02 20:10:40 -07001056 }
1057 return visit_continue;
1058 }
Paul Berrye2266692013-09-23 10:44:19 -07001059
1060private:
1061 /**
1062 * If the type pointed to by \c type represents an unsized array, replace
1063 * it with a sized array whose size is determined by max_array_access.
1064 */
1065 static void fixup_type(const glsl_type **type, unsigned max_array_access)
1066 {
1067 if ((*type)->is_array() && (*type)->length == 0) {
1068 *type = glsl_type::get_array_instance((*type)->fields.array,
1069 max_array_access + 1);
1070 assert(*type != NULL);
1071 }
1072 }
1073
1074 /**
1075 * Determine whether the given interface type contains unsized arrays (if
1076 * it doesn't, array_sizing_visitor doesn't need to process it).
1077 */
1078 static bool interface_contains_unsized_arrays(const glsl_type *type)
1079 {
1080 for (unsigned i = 0; i < type->length; i++) {
1081 const glsl_type *elem_type = type->fields.structure[i].type;
1082 if (elem_type->is_array() && elem_type->length == 0)
1083 return true;
1084 }
1085 return false;
1086 }
1087
1088 /**
1089 * Create a new interface type based on the given type, with unsized arrays
1090 * replaced by sized arrays whose size is determined by
1091 * max_ifc_array_access.
1092 */
1093 static const glsl_type *
1094 resize_interface_members(const glsl_type *type,
1095 const unsigned *max_ifc_array_access)
1096 {
1097 unsigned num_fields = type->length;
1098 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1099 memcpy(fields, type->fields.structure,
1100 num_fields * sizeof(*fields));
1101 for (unsigned i = 0; i < num_fields; i++) {
1102 fixup_type(&fields[i].type, max_ifc_array_access[i]);
1103 }
1104 glsl_interface_packing packing =
1105 (glsl_interface_packing) type->interface_packing;
1106 const glsl_type *new_ifc_type =
1107 glsl_type::get_interface_instance(fields, num_fields,
1108 packing, type->name);
1109 delete [] fields;
1110 return new_ifc_type;
1111 }
Brian Paul84a12732012-02-02 20:10:40 -07001112};
1113
Brian Paul84a12732012-02-02 20:10:40 -07001114/**
Eric Anholt6065a872013-06-12 18:12:40 -07001115 * Performs the cross-validation of geometry shader max_vertices and
1116 * primitive type layout qualifiers for the attached geometry shaders,
1117 * and propagates them to the linked GS and linked shader program.
1118 */
1119static void
1120link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
1121 struct gl_shader *linked_shader,
1122 struct gl_shader **shader_list,
1123 unsigned num_shaders)
1124{
1125 linked_shader->Geom.VerticesOut = 0;
1126 linked_shader->Geom.InputType = PRIM_UNKNOWN;
1127 linked_shader->Geom.OutputType = PRIM_UNKNOWN;
1128
1129 /* No in/out qualifiers defined for anything but GLSL 1.50+
1130 * geometry shaders so far.
1131 */
1132 if (linked_shader->Type != GL_GEOMETRY_SHADER || prog->Version < 150)
1133 return;
1134
1135 /* From the GLSL 1.50 spec, page 46:
1136 *
1137 * "All geometry shader output layout declarations in a program
1138 * must declare the same layout and same value for
1139 * max_vertices. There must be at least one geometry output
1140 * layout declaration somewhere in a program, but not all
1141 * geometry shaders (compilation units) are required to
1142 * declare it."
1143 */
1144
1145 for (unsigned i = 0; i < num_shaders; i++) {
1146 struct gl_shader *shader = shader_list[i];
1147
1148 if (shader->Geom.InputType != PRIM_UNKNOWN) {
1149 if (linked_shader->Geom.InputType != PRIM_UNKNOWN &&
1150 linked_shader->Geom.InputType != shader->Geom.InputType) {
1151 linker_error(prog, "geometry shader defined with conflicting "
1152 "input types\n");
1153 return;
1154 }
1155 linked_shader->Geom.InputType = shader->Geom.InputType;
1156 }
1157
1158 if (shader->Geom.OutputType != PRIM_UNKNOWN) {
1159 if (linked_shader->Geom.OutputType != PRIM_UNKNOWN &&
1160 linked_shader->Geom.OutputType != shader->Geom.OutputType) {
1161 linker_error(prog, "geometry shader defined with conflicting "
1162 "output types\n");
1163 return;
1164 }
1165 linked_shader->Geom.OutputType = shader->Geom.OutputType;
1166 }
1167
1168 if (shader->Geom.VerticesOut != 0) {
1169 if (linked_shader->Geom.VerticesOut != 0 &&
1170 linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) {
1171 linker_error(prog, "geometry shader defined with conflicting "
1172 "output vertex count (%d and %d)\n",
1173 linked_shader->Geom.VerticesOut,
1174 shader->Geom.VerticesOut);
1175 return;
1176 }
1177 linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut;
1178 }
1179 }
1180
1181 /* Just do the intrastage -> interstage propagation right now,
1182 * since we already know we're in the right type of shader program
1183 * for doing it.
1184 */
1185 if (linked_shader->Geom.InputType == PRIM_UNKNOWN) {
1186 linker_error(prog,
1187 "geometry shader didn't declare primitive input type\n");
1188 return;
1189 }
1190 prog->Geom.InputType = linked_shader->Geom.InputType;
1191
1192 if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) {
1193 linker_error(prog,
1194 "geometry shader didn't declare primitive output type\n");
1195 return;
1196 }
1197 prog->Geom.OutputType = linked_shader->Geom.OutputType;
1198
1199 if (linked_shader->Geom.VerticesOut == 0) {
1200 linker_error(prog,
1201 "geometry shader didn't declare max_vertices\n");
1202 return;
1203 }
1204 prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut;
1205}
1206
1207/**
Ian Romanick3fb87872010-07-09 14:09:34 -07001208 * Combine a group of shaders for a single stage to generate a linked shader
1209 *
1210 * \note
1211 * If this function is supplied a single shader, it is cloned, and the new
1212 * shader is returned.
1213 */
1214static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001215link_intrastage_shaders(void *mem_ctx,
1216 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -07001217 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -07001218 struct gl_shader **shader_list,
1219 unsigned num_shaders)
1220{
Eric Anholtf609cf72012-04-27 13:52:56 -07001221 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -07001222
Ian Romanick13f782c2010-06-29 18:53:38 -07001223 /* Check that global variables defined in multiple shaders are consistent.
1224 */
Paul Berryb95d2372013-07-27 11:08:31 -07001225 cross_validate_globals(prog, shader_list, num_shaders, false);
1226 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -07001227 return NULL;
1228
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001229 /* Check that interface blocks defined in multiple shaders are consistent.
1230 */
Paul Berryb95d2372013-07-27 11:08:31 -07001231 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
1232 num_shaders);
1233 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001234 return NULL;
1235
Paul Berry4682b9b2013-07-27 15:07:08 -07001236 /* Link up uniform blocks defined within this stage. */
1237 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -05001238 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
1239 &uniform_blocks);
Eric Anholtf609cf72012-04-27 13:52:56 -07001240
Ian Romanick13f782c2010-06-29 18:53:38 -07001241 /* Check that there is only a single definition of each function signature
1242 * across all shaders.
1243 */
1244 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1245 foreach_list(node, shader_list[i]->ir) {
1246 ir_function *const f = ((ir_instruction *) node)->as_function();
1247
1248 if (f == NULL)
1249 continue;
1250
1251 for (unsigned j = i + 1; j < num_shaders; j++) {
1252 ir_function *const other =
1253 shader_list[j]->symbols->get_function(f->name);
1254
1255 /* If the other shader has no function (and therefore no function
1256 * signatures) with the same name, skip to the next shader.
1257 */
1258 if (other == NULL)
1259 continue;
1260
1261 foreach_iter (exec_list_iterator, iter, *f) {
1262 ir_function_signature *sig =
1263 (ir_function_signature *) iter.get();
1264
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001265 if (!sig->is_defined || sig->is_builtin())
Ian Romanick13f782c2010-06-29 18:53:38 -07001266 continue;
1267
1268 ir_function_signature *other_sig =
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001269 other->exact_matching_signature(NULL, &sig->parameters);
Ian Romanick13f782c2010-06-29 18:53:38 -07001270
1271 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001272 && !other_sig->is_builtin()) {
Ian Romanick586e7412011-07-28 14:04:09 -07001273 linker_error(prog, "function `%s' is multiply defined",
1274 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001275 return NULL;
1276 }
1277 }
1278 }
1279 }
1280 }
1281
1282 /* Find the shader that defines main, and make a clone of it.
1283 *
1284 * Starting with the clone, search for undefined references. If one is
1285 * found, find the shader that defines it. Clone the reference and add
1286 * it to the shader. Repeat until there are no undefined references or
1287 * until a reference cannot be resolved.
1288 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001289 gl_shader *main = NULL;
1290 for (unsigned i = 0; i < num_shaders; i++) {
1291 if (get_main_function_signature(shader_list[i]) != NULL) {
1292 main = shader_list[i];
1293 break;
1294 }
1295 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001296
Ian Romanick15ce87e2010-07-09 15:28:22 -07001297 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001298 linker_error(prog, "%s shader lacks `main'\n",
Eric Anholtfaf3dba2013-06-12 16:57:11 -07001299 _mesa_glsl_shader_target_name(shader_list[0]->Type));
Ian Romanick15ce87e2010-07-09 15:28:22 -07001300 return NULL;
1301 }
1302
Ian Romanick4a455952010-10-13 15:13:02 -07001303 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001304 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001305 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001306
Eric Anholtf609cf72012-04-27 13:52:56 -07001307 linked->UniformBlocks = uniform_blocks;
1308 linked->NumUniformBlocks = num_uniform_blocks;
1309 ralloc_steal(linked, linked->UniformBlocks);
1310
Eric Anholt6065a872013-06-12 18:12:40 -07001311 link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
1312
Ian Romanick15ce87e2010-07-09 15:28:22 -07001313 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001314
Ian Romanick31a97862010-07-12 18:48:50 -07001315 /* The a pointer to the main function in the final linked shader (i.e., the
1316 * copy of the original shader that contained the main function).
1317 */
1318 ir_function_signature *const main_sig = get_main_function_signature(linked);
1319
1320 /* Move any instructions other than variable declarations or function
1321 * declarations into main.
1322 */
Ian Romanick9303e352010-07-19 12:33:54 -07001323 exec_node *insertion_point =
1324 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1325 linked);
1326
Ian Romanick31a97862010-07-12 18:48:50 -07001327 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001328 if (shader_list[i] == main)
1329 continue;
1330
Ian Romanick31a97862010-07-12 18:48:50 -07001331 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001332 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001333 }
1334
Ian Romanick13f782c2010-06-29 18:53:38 -07001335 /* Resolve initializers for global variables in the linked shader.
1336 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001337 unsigned num_linking_shaders = num_shaders;
1338 for (unsigned i = 0; i < num_shaders; i++)
1339 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1340
1341 gl_shader **linking_shaders =
1342 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1343
1344 memcpy(linking_shaders, shader_list,
1345 sizeof(linking_shaders[0]) * num_shaders);
1346
1347 unsigned idx = num_shaders;
1348 for (unsigned i = 0; i < num_shaders; i++) {
1349 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1350 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1351 idx += shader_list[i]->num_builtins_to_link;
1352 }
1353
1354 assert(idx == num_linking_shaders);
1355
Ian Romanick4a455952010-10-13 15:13:02 -07001356 if (!link_function_calls(prog, linked, linking_shaders,
1357 num_linking_shaders)) {
1358 ctx->Driver.DeleteShader(ctx, linked);
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001359 free(linking_shaders);
1360 return NULL;
Ian Romanick4a455952010-10-13 15:13:02 -07001361 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001362
1363 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001364
Paul Berryc148ef62011-08-03 15:37:01 -07001365 /* At this point linked should contain all of the linked IR, so
1366 * validate it to make sure nothing went wrong.
1367 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001368 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07001369
Paul Berry7cfefe62013-07-30 21:13:48 -07001370 /* Set the size of geometry shader input arrays */
1371 if (linked->Type == GL_GEOMETRY_SHADER) {
1372 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
1373 geom_array_resize_visitor input_resize_visitor(num_vertices, prog);
1374 foreach_iter(exec_list_iterator, iter, *linked->ir) {
1375 ir_instruction *ir = (ir_instruction *)iter.get();
1376 ir->accept(&input_resize_visitor);
1377 }
1378 }
1379
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001380 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001381 * unspecified sizes have a size specified. The size is inferred from the
1382 * max_array_access field.
1383 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001384 array_sizing_visitor v;
1385 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001386
Ian Romanick3fb87872010-07-09 14:09:34 -07001387 return linked;
1388}
1389
Eric Anholta721abf2010-08-23 10:32:01 -07001390/**
1391 * Update the sizes of linked shader uniform arrays to the maximum
1392 * array index used.
1393 *
1394 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1395 *
1396 * If one or more elements of an array are active,
1397 * GetActiveUniform will return the name of the array in name,
1398 * subject to the restrictions listed above. The type of the array
1399 * is returned in type. The size parameter contains the highest
1400 * array element index used, plus one. The compiler or linker
1401 * determines the highest index used. There will be only one
1402 * active uniform reported by the GL per uniform array.
1403
1404 */
1405static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001406update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001407{
Ian Romanick3322fba2010-10-14 13:28:42 -07001408 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1409 if (prog->_LinkedShaders[i] == NULL)
1410 continue;
1411
Eric Anholta721abf2010-08-23 10:32:01 -07001412 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1413 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1414
Paul Berry6a2baf32013-06-10 14:01:45 -07001415 if ((var == NULL) || (var->mode != ir_var_uniform) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001416 !var->type->is_array())
1417 continue;
1418
Eric Anholt9feb4032012-05-01 14:43:31 -07001419 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1420 * will not be eliminated. Since we always do std140, just
1421 * don't resize arrays in UBOs.
1422 */
Ian Romanick13be1f42012-12-14 12:00:14 -08001423 if (var->is_in_uniform_block())
Eric Anholt9feb4032012-05-01 14:43:31 -07001424 continue;
1425
Eric Anholta721abf2010-08-23 10:32:01 -07001426 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001427 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1428 if (prog->_LinkedShaders[j] == NULL)
1429 continue;
1430
Eric Anholta721abf2010-08-23 10:32:01 -07001431 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1432 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1433 if (!other_var)
1434 continue;
1435
1436 if (strcmp(var->name, other_var->name) == 0 &&
1437 other_var->max_array_access > size) {
1438 size = other_var->max_array_access;
1439 }
1440 }
1441 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001442
Fabian Bieler63684782013-06-14 13:37:07 +02001443 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001444 /* If this is a built-in uniform (i.e., it's backed by some
1445 * fixed-function state), adjust the number of state slots to
1446 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001447 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001448 * slots is an integer multiple of the number of array elements.
1449 * Determine the number of slots per array element by dividing by
1450 * the old (total) size.
1451 */
1452 if (var->num_state_slots > 0) {
1453 var->num_state_slots = (size + 1)
1454 * (var->num_state_slots / var->type->length);
1455 }
1456
Eric Anholta721abf2010-08-23 10:32:01 -07001457 var->type = glsl_type::get_array_instance(var->type->fields.array,
1458 size + 1);
1459 /* FINISHME: We should update the types of array
1460 * dereferences of this variable now.
1461 */
1462 }
1463 }
1464 }
1465}
1466
Ian Romanick69846702010-06-22 17:29:19 -07001467/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001468 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001469 *
1470 * \param used_mask Bits representing used (1) and unused (0) locations
1471 * \param needed_count Number of contiguous bits needed.
1472 *
1473 * \return
1474 * Base location of the available bits on success or -1 on failure.
1475 */
1476int
1477find_available_slots(unsigned used_mask, unsigned needed_count)
1478{
1479 unsigned needed_mask = (1 << needed_count) - 1;
1480 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1481
1482 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1483 * cannot optimize possibly infinite loops" for the loop below.
1484 */
1485 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1486 return -1;
1487
1488 for (int i = 0; i <= max_bit_to_test; i++) {
1489 if ((needed_mask & ~used_mask) == needed_mask)
1490 return i;
1491
1492 needed_mask <<= 1;
1493 }
1494
1495 return -1;
1496}
1497
1498
Ian Romanickd32d4f72011-06-27 17:59:58 -07001499/**
1500 * Assign locations for either VS inputs for FS outputs
1501 *
1502 * \param prog Shader program whose variables need locations assigned
1503 * \param target_index Selector for the program target to receive location
1504 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1505 * \c MESA_SHADER_FRAGMENT.
1506 * \param max_index Maximum number of generic locations. This corresponds
1507 * to either the maximum number of draw buffers or the
1508 * maximum number of generic attributes.
1509 *
1510 * \return
1511 * If locations are successfully assigned, true is returned. Otherwise an
1512 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001513 */
Ian Romanick69846702010-06-22 17:29:19 -07001514bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001515assign_attribute_or_color_locations(gl_shader_program *prog,
1516 unsigned target_index,
1517 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001518{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001519 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001520 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001521 unsigned used_locations = (max_index >= 32)
1522 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001523
Ian Romanickd32d4f72011-06-27 17:59:58 -07001524 assert((target_index == MESA_SHADER_VERTEX)
1525 || (target_index == MESA_SHADER_FRAGMENT));
1526
1527 gl_shader *const sh = prog->_LinkedShaders[target_index];
1528 if (sh == NULL)
1529 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001530
Ian Romanick69846702010-06-22 17:29:19 -07001531 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001532 *
1533 * 1. Invalidate the location assignments for all vertex shader inputs.
1534 *
1535 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001536 * glBindVertexAttribLocation) locations and outputs that have
1537 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001538 *
Ian Romanick69846702010-06-22 17:29:19 -07001539 * 3. Sort the attributes without assigned locations by number of slots
1540 * required in decreasing order. Fragmentation caused by attribute
1541 * locations assigned by the application may prevent large attributes
1542 * from having enough contiguous space.
1543 *
1544 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001545 */
1546
Ian Romanickd32d4f72011-06-27 17:59:58 -07001547 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001548 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001549
Ian Romanickd32d4f72011-06-27 17:59:58 -07001550 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001551 (target_index == MESA_SHADER_VERTEX)
1552 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001553
1554
Ian Romanick69846702010-06-22 17:29:19 -07001555 /* Temporary storage for the set of attributes that need locations assigned.
1556 */
1557 struct temp_attr {
1558 unsigned slots;
1559 ir_variable *var;
1560
1561 /* Used below in the call to qsort. */
1562 static int compare(const void *a, const void *b)
1563 {
1564 const temp_attr *const l = (const temp_attr *) a;
1565 const temp_attr *const r = (const temp_attr *) b;
1566
1567 /* Reversed because we want a descending order sort below. */
1568 return r->slots - l->slots;
1569 }
1570 } to_assign[16];
1571
1572 unsigned num_attr = 0;
1573
Eric Anholt16b68b12010-06-30 11:05:43 -07001574 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001575 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1576
Brian Paul4470ff22011-07-19 21:10:25 -06001577 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001578 continue;
1579
Ian Romanick68a4fc92010-10-07 17:21:22 -07001580 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001581 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001582 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001583 linker_error(prog,
1584 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001585 (var->location < 0)
1586 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001587 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001588 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001589 }
1590 } else if (target_index == MESA_SHADER_VERTEX) {
1591 unsigned binding;
1592
1593 if (prog->AttributeBindings->get(binding, var->name)) {
1594 assert(binding >= VERT_ATTRIB_GENERIC0);
1595 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001596 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001597 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001598 } else if (target_index == MESA_SHADER_FRAGMENT) {
1599 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001600 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001601
1602 if (prog->FragDataBindings->get(binding, var->name)) {
1603 assert(binding >= FRAG_RESULT_DATA0);
1604 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001605 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001606
1607 if (prog->FragDataIndexBindings->get(index, var->name)) {
1608 var->index = index;
1609 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001610 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001611 }
1612
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001613 /* If the variable is not a built-in and has a location statically
1614 * assigned in the shader (presumably via a layout qualifier), make sure
1615 * that it doesn't collide with other assigned locations. Otherwise,
1616 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001617 */
Paul Berry0026ad42013-07-31 08:15:08 -07001618 const unsigned slots = var->type->count_attribute_slots();
Ian Romanick523b6112011-08-17 15:40:03 -07001619 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001620 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001621 /* From page 61 of the OpenGL 4.0 spec:
1622 *
1623 * "LinkProgram will fail if the attribute bindings assigned
1624 * by BindAttribLocation do not leave not enough space to
1625 * assign a location for an active matrix attribute or an
1626 * active attribute array, both of which require multiple
1627 * contiguous generic attributes."
1628 *
1629 * Previous versions of the spec contain similar language but omit
1630 * the bit about attribute arrays.
1631 *
1632 * Page 61 of the OpenGL 4.0 spec also says:
1633 *
1634 * "It is possible for an application to bind more than one
1635 * attribute name to the same location. This is referred to as
1636 * aliasing. This will only work if only one of the aliased
1637 * attributes is active in the executable program, or if no
1638 * path through the shader consumes more than one attribute of
1639 * a set of attributes aliased to the same location. A link
1640 * error can occur if the linker determines that every path
1641 * through the shader consumes multiple aliased attributes,
1642 * but implementations are not required to generate an error
1643 * in this case."
1644 *
1645 * These two paragraphs are either somewhat contradictory, or I
1646 * don't fully understand one or both of them.
1647 */
1648 /* FINISHME: The code as currently written does not support
1649 * FINISHME: attribute location aliasing (see comment above).
1650 */
1651 /* Mask representing the contiguous slots that will be used by
1652 * this attribute.
1653 */
1654 const unsigned attr = var->location - generic_base;
1655 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001656
Ian Romanick523b6112011-08-17 15:40:03 -07001657 /* Generate a link error if the set of bits requested for this
1658 * attribute overlaps any previously allocated bits.
1659 */
1660 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001661 const char *const string = (target_index == MESA_SHADER_VERTEX)
1662 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001663 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001664 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001665 "available for %s `%s' %d %d %d", string,
1666 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001667 return false;
1668 }
1669
1670 used_locations |= (use_mask << attr);
1671 }
1672
1673 continue;
1674 }
1675
1676 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001677 to_assign[num_attr].var = var;
1678 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001679 }
Ian Romanick69846702010-06-22 17:29:19 -07001680
1681 /* If all of the attributes were assigned locations by the application (or
1682 * are built-in attributes with fixed locations), return early. This should
1683 * be the common case.
1684 */
1685 if (num_attr == 0)
1686 return true;
1687
1688 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1689
Ian Romanickd32d4f72011-06-27 17:59:58 -07001690 if (target_index == MESA_SHADER_VERTEX) {
1691 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1692 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1693 * reserved to prevent it from being automatically allocated below.
1694 */
1695 find_deref_visitor find("gl_Vertex");
1696 find.run(sh->ir);
1697 if (find.variable_found())
1698 used_locations |= (1 << 0);
1699 }
Ian Romanick982e3792010-06-29 18:58:20 -07001700
Ian Romanick69846702010-06-22 17:29:19 -07001701 for (unsigned i = 0; i < num_attr; i++) {
1702 /* Mask representing the contiguous slots that will be used by this
1703 * attribute.
1704 */
1705 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1706
1707 int location = find_available_slots(used_locations, to_assign[i].slots);
1708
1709 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001710 const char *const string = (target_index == MESA_SHADER_VERTEX)
1711 ? "vertex shader input" : "fragment shader output";
1712
Ian Romanick586e7412011-07-28 14:04:09 -07001713 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001714 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001715 "available for %s `%s'",
1716 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001717 return false;
1718 }
1719
Ian Romanickd32d4f72011-06-27 17:59:58 -07001720 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001721 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001722 used_locations |= (use_mask << location);
1723 }
1724
1725 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001726}
1727
1728
Ian Romanick40e114b2010-08-17 14:55:50 -07001729/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001730 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001731 */
1732void
Ian Romanickcc90e622010-10-19 17:59:10 -07001733demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001734{
1735 foreach_list(node, sh->ir) {
1736 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1737
Ian Romanickcc90e622010-10-19 17:59:10 -07001738 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001739 continue;
1740
Ian Romanickcc90e622010-10-19 17:59:10 -07001741 /* A shader 'in' or 'out' variable is only really an input or output if
1742 * its value is used by other shader stages. This will cause the variable
1743 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001744 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001745 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001746 var->mode = ir_var_auto;
1747 }
1748 }
1749}
1750
1751
Paul Berry871ddb92011-11-05 11:17:32 -07001752/**
Marek Olšákec174a42011-11-18 15:00:10 +01001753 * Store the gl_FragDepth layout in the gl_shader_program struct.
1754 */
1755static void
1756store_fragdepth_layout(struct gl_shader_program *prog)
1757{
1758 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1759 return;
1760 }
1761
1762 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1763
1764 /* We don't look up the gl_FragDepth symbol directly because if
1765 * gl_FragDepth is not used in the shader, it's removed from the IR.
1766 * However, the symbol won't be removed from the symbol table.
1767 *
1768 * We're only interested in the cases where the variable is NOT removed
1769 * from the IR.
1770 */
1771 foreach_list(node, ir) {
1772 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1773
Paul Berry42a29d82013-01-11 14:39:32 -08001774 if (var == NULL || var->mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001775 continue;
1776 }
1777
1778 if (strcmp(var->name, "gl_FragDepth") == 0) {
1779 switch (var->depth_layout) {
1780 case ir_depth_layout_none:
1781 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1782 return;
1783 case ir_depth_layout_any:
1784 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1785 return;
1786 case ir_depth_layout_greater:
1787 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1788 return;
1789 case ir_depth_layout_less:
1790 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1791 return;
1792 case ir_depth_layout_unchanged:
1793 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1794 return;
1795 default:
1796 assert(0);
1797 return;
1798 }
1799 }
1800 }
1801}
1802
1803/**
Ian Romanick92f81592011-11-08 12:37:19 -08001804 * Validate the resources used by a program versus the implementation limits
1805 */
Paul Berryb95d2372013-07-27 11:08:31 -07001806static void
Ian Romanick92f81592011-11-08 12:37:19 -08001807check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1808{
1809 static const char *const shader_names[MESA_SHADER_TYPES] = {
Marek Olšák030ca232013-06-12 17:15:46 +02001810 "vertex", "geometry", "fragment"
Ian Romanick92f81592011-11-08 12:37:19 -08001811 };
1812
1813 const unsigned max_samplers[MESA_SHADER_TYPES] = {
Marek Olšák5e784332013-05-02 02:30:44 +02001814 ctx->Const.VertexProgram.MaxTextureImageUnits,
Marek Olšák030ca232013-06-12 17:15:46 +02001815 ctx->Const.GeometryProgram.MaxTextureImageUnits,
1816 ctx->Const.FragmentProgram.MaxTextureImageUnits
Ian Romanick92f81592011-11-08 12:37:19 -08001817 };
1818
Eric Anholt38e77e52013-05-23 11:10:15 -07001819 const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = {
Ian Romanick92f81592011-11-08 12:37:19 -08001820 ctx->Const.VertexProgram.MaxUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001821 ctx->Const.GeometryProgram.MaxUniformComponents,
1822 ctx->Const.FragmentProgram.MaxUniformComponents
Ian Romanick92f81592011-11-08 12:37:19 -08001823 };
1824
Eric Anholt38e77e52013-05-23 11:10:15 -07001825 const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = {
1826 ctx->Const.VertexProgram.MaxCombinedUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001827 ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
1828 ctx->Const.FragmentProgram.MaxCombinedUniformComponents
Eric Anholt38e77e52013-05-23 11:10:15 -07001829 };
1830
Eric Anholt877a8972012-06-25 12:47:01 -07001831 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
1832 ctx->Const.VertexProgram.MaxUniformBlocks,
Eric Anholt877a8972012-06-25 12:47:01 -07001833 ctx->Const.GeometryProgram.MaxUniformBlocks,
Marek Olšák030ca232013-06-12 17:15:46 +02001834 ctx->Const.FragmentProgram.MaxUniformBlocks
Eric Anholt877a8972012-06-25 12:47:01 -07001835 };
1836
Ian Romanick92f81592011-11-08 12:37:19 -08001837 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1838 struct gl_shader *sh = prog->_LinkedShaders[i];
1839
1840 if (sh == NULL)
1841 continue;
1842
1843 if (sh->num_samplers > max_samplers[i]) {
1844 linker_error(prog, "Too many %s shader texture samplers",
1845 shader_names[i]);
1846 }
1847
Eric Anholt38e77e52013-05-23 11:10:15 -07001848 if (sh->num_uniform_components > max_default_uniform_components[i]) {
1849 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1850 linker_warning(prog, "Too many %s shader default uniform block "
1851 "components, but the driver will try to optimize "
1852 "them out; this is non-portable out-of-spec "
1853 "behavior\n",
1854 shader_names[i]);
1855 } else {
1856 linker_error(prog, "Too many %s shader default uniform block "
1857 "components",
1858 shader_names[i]);
1859 }
1860 }
1861
1862 if (sh->num_combined_uniform_components >
1863 max_combined_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001864 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1865 linker_warning(prog, "Too many %s shader uniform components, "
1866 "but the driver will try to optimize them out; "
1867 "this is non-portable out-of-spec behavior\n",
1868 shader_names[i]);
1869 } else {
1870 linker_error(prog, "Too many %s shader uniform components",
1871 shader_names[i]);
1872 }
Ian Romanick92f81592011-11-08 12:37:19 -08001873 }
1874 }
1875
Eric Anholt877a8972012-06-25 12:47:01 -07001876 unsigned blocks[MESA_SHADER_TYPES] = {0};
1877 unsigned total_uniform_blocks = 0;
1878
1879 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
1880 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1881 if (prog->UniformBlockStageIndex[j][i] != -1) {
1882 blocks[j]++;
1883 total_uniform_blocks++;
1884 }
1885 }
1886
1887 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1888 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1889 prog->NumUniformBlocks,
1890 ctx->Const.MaxCombinedUniformBlocks);
1891 } else {
1892 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1893 if (blocks[i] > max_uniform_blocks[i]) {
1894 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
1895 shader_names[i],
1896 blocks[i],
1897 max_uniform_blocks[i]);
1898 break;
1899 }
1900 }
1901 }
1902 }
Ian Romanick92f81592011-11-08 12:37:19 -08001903}
Paul Berry871ddb92011-11-05 11:17:32 -07001904
Ian Romanick0e59b262010-06-23 11:23:01 -07001905void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001906link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001907{
Paul Berry871ddb92011-11-05 11:17:32 -07001908 tfeedback_decl *tfeedback_decls = NULL;
1909 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1910
Kenneth Graunked3073f52011-01-21 14:32:31 -08001911 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001912
Paul Berryb95d2372013-07-27 11:08:31 -07001913 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07001914 prog->Validated = false;
1915 prog->_Used = false;
1916
Eric Anholtf609cf72012-04-27 13:52:56 -07001917 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08001918 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07001919
Eric Anholtf609cf72012-04-27 13:52:56 -07001920 ralloc_free(prog->UniformBlocks);
1921 prog->UniformBlocks = NULL;
1922 prog->NumUniformBlocks = 0;
1923 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
1924 ralloc_free(prog->UniformBlockStageIndex[i]);
1925 prog->UniformBlockStageIndex[i] = NULL;
1926 }
1927
Ian Romanick832dfa52010-06-17 15:04:20 -07001928 /* Separate the shaders into groups based on their type.
1929 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001930 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001931 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001932 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001933 unsigned num_frag_shaders = 0;
Bryan Cain25480922013-02-15 09:46:50 -06001934 struct gl_shader **geom_shader_list;
1935 unsigned num_geom_shaders = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001936
Eric Anholt16b68b12010-06-30 11:05:43 -07001937 vert_shader_list = (struct gl_shader **)
Paul Berry844bd712013-07-30 22:38:43 -07001938 calloc(prog->NumShaders, sizeof(struct gl_shader *));
1939 frag_shader_list = (struct gl_shader **)
1940 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Bryan Cain25480922013-02-15 09:46:50 -06001941 geom_shader_list = (struct gl_shader **)
1942 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001943
Ian Romanick25f51d32010-07-16 15:51:50 -07001944 unsigned min_version = UINT_MAX;
1945 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07001946 const bool is_es_prog =
1947 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07001948 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001949 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1950 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1951
Paul Berrya9f34dc2012-08-02 17:49:44 -07001952 if (prog->Shaders[i]->IsES != is_es_prog) {
1953 linker_error(prog, "all shaders must use same shading "
1954 "language version\n");
1955 goto done;
1956 }
1957
Ian Romanick832dfa52010-06-17 15:04:20 -07001958 switch (prog->Shaders[i]->Type) {
1959 case GL_VERTEX_SHADER:
1960 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1961 num_vert_shaders++;
1962 break;
1963 case GL_FRAGMENT_SHADER:
1964 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1965 num_frag_shaders++;
1966 break;
1967 case GL_GEOMETRY_SHADER:
Bryan Cain25480922013-02-15 09:46:50 -06001968 geom_shader_list[num_geom_shaders] = prog->Shaders[i];
1969 num_geom_shaders++;
Ian Romanick832dfa52010-06-17 15:04:20 -07001970 break;
1971 }
1972 }
1973
Ian Romanick25f51d32010-07-16 15:51:50 -07001974 /* Previous to GLSL version 1.30, different compilation units could mix and
1975 * match shading language versions. With GLSL 1.30 and later, the versions
1976 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07001977 *
1978 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07001979 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07001980 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001981 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07001982 linker_error(prog, "all shaders must use same shading "
1983 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07001984 goto done;
1985 }
1986
1987 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07001988 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07001989
Fabian Bielerbd85ba02013-05-24 23:26:54 +02001990 /* Geometry shaders have to be linked with vertex shaders.
1991 */
1992 if (num_geom_shaders > 0 && num_vert_shaders == 0) {
1993 linker_error(prog, "Geometry shader must be linked with "
1994 "vertex shader\n");
1995 goto done;
1996 }
1997
Ian Romanick3322fba2010-10-14 13:28:42 -07001998 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1999 if (prog->_LinkedShaders[i] != NULL)
2000 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2001
2002 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002003 }
2004
Ian Romanickcd6764e2010-07-16 16:00:07 -07002005 /* Link all shaders for a particular stage and validate the result.
2006 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002007 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002008 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002009 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2010 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002011
Paul Berryb95d2372013-07-27 11:08:31 -07002012 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07002013 goto done;
2014
Paul Berryb95d2372013-07-27 11:08:31 -07002015 validate_vertex_shader_executable(prog, sh);
2016 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002017 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002018
Ian Romanick3322fba2010-10-14 13:28:42 -07002019 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2020 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002021 }
2022
2023 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002024 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002025 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2026 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002027
Paul Berryb95d2372013-07-27 11:08:31 -07002028 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07002029 goto done;
2030
Paul Berryb95d2372013-07-27 11:08:31 -07002031 validate_fragment_shader_executable(prog, sh);
2032 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002033 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002034
Ian Romanick3322fba2010-10-14 13:28:42 -07002035 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2036 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002037 }
2038
Bryan Cain25480922013-02-15 09:46:50 -06002039 if (num_geom_shaders > 0) {
2040 gl_shader *const sh =
2041 link_intrastage_shaders(mem_ctx, ctx, prog, geom_shader_list,
2042 num_geom_shaders);
2043
2044 if (!prog->LinkStatus)
2045 goto done;
2046
2047 validate_geometry_shader_executable(prog, sh);
2048 if (!prog->LinkStatus)
2049 goto done;
2050
2051 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
2052 sh);
2053 }
2054
Ian Romanick3ed850e2010-06-23 12:18:21 -07002055 /* Here begins the inter-stage linking phase. Some initial validation is
2056 * performed, then locations are assigned for uniforms, attributes, and
2057 * varyings.
2058 */
Paul Berryb95d2372013-07-27 11:08:31 -07002059 cross_validate_uniforms(prog);
2060 if (!prog->LinkStatus)
2061 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002062
Paul Berryb95d2372013-07-27 11:08:31 -07002063 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07002064
Paul Berryb95d2372013-07-27 11:08:31 -07002065 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2066 if (prog->_LinkedShaders[prev] != NULL)
2067 break;
2068 }
Ian Romanick3322fba2010-10-14 13:28:42 -07002069
Paul Berryb95d2372013-07-27 11:08:31 -07002070 /* Validate the inputs of each stage with the output of the preceding
2071 * stage.
2072 */
2073 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2074 if (prog->_LinkedShaders[i] == NULL)
2075 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07002076
Paul Berryb95d2372013-07-27 11:08:31 -07002077 validate_interstage_interface_blocks(prog, prog->_LinkedShaders[prev],
2078 prog->_LinkedShaders[i]);
2079 if (!prog->LinkStatus)
2080 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002081
Paul Berryb95d2372013-07-27 11:08:31 -07002082 cross_validate_outputs_to_inputs(prog,
2083 prog->_LinkedShaders[prev],
2084 prog->_LinkedShaders[i]);
2085 if (!prog->LinkStatus)
2086 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07002087
Paul Berryb95d2372013-07-27 11:08:31 -07002088 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002089 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002090
Jordan Justen5ebf5472013-03-10 03:20:03 -07002091
2092 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2093 if (prog->_LinkedShaders[i] != NULL)
2094 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
2095 }
2096
Eric Anholt3de13952012-05-04 13:08:46 -07002097 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2098 * it before optimization because we want most of the checks to get
2099 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002100 *
2101 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002102 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002103 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002104 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2105 if (sh) {
2106 lower_discard_flow(sh->ir);
2107 }
2108 }
2109
Eric Anholtf609cf72012-04-27 13:52:56 -07002110 if (!interstage_cross_validate_uniform_blocks(prog))
2111 goto done;
2112
Eric Anholt2f4fe152010-08-10 13:06:49 -07002113 /* Do common optimization before assigning storage for attributes,
2114 * uniforms, and varyings. Later optimization could possibly make
2115 * some of that unused.
2116 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002117 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2118 if (prog->_LinkedShaders[i] == NULL)
2119 continue;
2120
Ian Romanick02c5ae12011-07-11 10:46:01 -07002121 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2122 if (!prog->LinkStatus)
2123 goto done;
2124
Paul Berry18392442012-12-04 11:11:02 -08002125 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2126 lower_clip_distance(prog->_LinkedShaders[i]);
2127 }
Paul Berryc06e3252011-08-11 20:58:21 -07002128
Brian Paul7feabfe2012-03-20 17:43:12 -06002129 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2130
Kenneth Graunkeb7657402013-04-17 17:30:22 -07002131 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002132 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002133 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002134
Paul Berry50895d42012-12-05 07:17:07 -08002135 /* Mark all generic shader inputs and outputs as unpaired. */
2136 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2137 link_invalidate_variable_locations(
2138 prog->_LinkedShaders[MESA_SHADER_VERTEX],
Paul Berry36b252e2013-02-23 07:22:01 -08002139 VERT_ATTRIB_GENERIC0, VARYING_SLOT_VAR0);
Paul Berry50895d42012-12-05 07:17:07 -08002140 }
Bryan Cain25480922013-02-15 09:46:50 -06002141 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2142 link_invalidate_variable_locations(
2143 prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
2144 VARYING_SLOT_VAR0, VARYING_SLOT_VAR0);
2145 }
Paul Berry50895d42012-12-05 07:17:07 -08002146 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2147 link_invalidate_variable_locations(
2148 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
Paul Berryeed6baf2013-02-23 09:00:58 -08002149 VARYING_SLOT_VAR0, FRAG_RESULT_DATA0);
Paul Berry50895d42012-12-05 07:17:07 -08002150 }
2151
Ian Romanickd32d4f72011-06-27 17:59:58 -07002152 /* FINISHME: The value of the max_attribute_index parameter is
2153 * FINISHME: implementation dependent based on the value of
2154 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2155 * FINISHME: at least 16, so hardcode 16 for now.
2156 */
2157 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002158 goto done;
2159 }
2160
Dave Airlie1256a5d2012-03-24 13:33:41 +00002161 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002162 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002163 }
2164
Marek Olšák284d9542013-06-12 02:18:09 +02002165 unsigned first;
2166 for (first = 0; first < MESA_SHADER_TYPES; first++) {
2167 if (prog->_LinkedShaders[first] != NULL)
Ian Romanick3322fba2010-10-14 13:28:42 -07002168 break;
2169 }
2170
Paul Berry871ddb92011-11-05 11:17:32 -07002171 if (num_tfeedback_decls != 0) {
2172 /* From GL_EXT_transform_feedback:
2173 * A program will fail to link if:
2174 *
2175 * * the <count> specified by TransformFeedbackVaryingsEXT is
2176 * non-zero, but the program object has no vertex or geometry
2177 * shader;
2178 */
Bryan Cain25480922013-02-15 09:46:50 -06002179 if (first == MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07002180 linker_error(prog, "Transform feedback varyings specified, but "
2181 "no vertex or geometry shader is present.");
2182 goto done;
2183 }
2184
2185 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2186 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002187 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002188 prog->TransformFeedback.VaryingNames,
2189 tfeedback_decls))
2190 goto done;
2191 }
2192
Marek Olšák284d9542013-06-12 02:18:09 +02002193 /* Linking the stages in the opposite order (from fragment to vertex)
2194 * ensures that inter-shader outputs written to in an earlier stage are
2195 * eliminated if they are (transitively) not used in a later stage.
2196 */
2197 int last, next;
2198 for (last = MESA_SHADER_TYPES-1; last >= 0; last--) {
2199 if (prog->_LinkedShaders[last] != NULL)
2200 break;
Ian Romanick3322fba2010-10-14 13:28:42 -07002201 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002202
Marek Olšák284d9542013-06-12 02:18:09 +02002203 if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
2204 gl_shader *const sh = prog->_LinkedShaders[last];
2205
2206 if (num_tfeedback_decls != 0) {
2207 /* There was no fragment shader, but we still have to assign varying
2208 * locations for use by transform feedback.
2209 */
2210 if (!assign_varying_locations(ctx, mem_ctx, prog,
2211 sh, NULL,
Paul Berry3b0cf702013-04-10 06:48:42 -07002212 num_tfeedback_decls, tfeedback_decls,
2213 0))
Marek Olšák284d9542013-06-12 02:18:09 +02002214 goto done;
2215 }
2216
Marek Olšákd13003f2013-08-09 22:34:45 +02002217 do_dead_builtin_varyings(ctx, sh, NULL,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002218 num_tfeedback_decls, tfeedback_decls);
2219
Marek Olšák284d9542013-06-12 02:18:09 +02002220 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
2221
2222 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07002223 */
Marek Olšák284d9542013-06-12 02:18:09 +02002224 while (do_dead_code(sh->ir, false))
2225 ;
2226 }
2227 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002228 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02002229 */
2230 gl_shader *const sh = prog->_LinkedShaders[first];
2231
Marek Olšákd13003f2013-08-09 22:34:45 +02002232 do_dead_builtin_varyings(ctx, NULL, sh,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002233 num_tfeedback_decls, tfeedback_decls);
2234
Marek Olšák284d9542013-06-12 02:18:09 +02002235 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
2236
2237 while (do_dead_code(sh->ir, false))
2238 ;
2239 }
2240
2241 next = last;
2242 for (int i = next - 1; i >= 0; i--) {
2243 if (prog->_LinkedShaders[i] == NULL)
2244 continue;
2245
2246 gl_shader *const sh_i = prog->_LinkedShaders[i];
2247 gl_shader *const sh_next = prog->_LinkedShaders[next];
Paul Berry3b0cf702013-04-10 06:48:42 -07002248 unsigned gs_input_vertices =
2249 next == MESA_SHADER_GEOMETRY ? prog->Geom.VerticesIn : 0;
Marek Olšák284d9542013-06-12 02:18:09 +02002250
2251 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
2252 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
Paul Berry3b0cf702013-04-10 06:48:42 -07002253 tfeedback_decls, gs_input_vertices))
Paul Berry871ddb92011-11-05 11:17:32 -07002254 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02002255
Marek Olšákd13003f2013-08-09 22:34:45 +02002256 do_dead_builtin_varyings(ctx, sh_i, sh_next,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002257 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2258 tfeedback_decls);
2259
Marek Olšák284d9542013-06-12 02:18:09 +02002260 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
2261 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
2262
2263 /* Eliminate code that is now dead due to unused outputs being demoted.
2264 */
2265 while (do_dead_code(sh_i->ir, false))
2266 ;
2267 while (do_dead_code(sh_next->ir, false))
2268 ;
2269
Marek Olšák3c555822013-06-13 03:17:22 +02002270 /* This must be done after all dead varyings are eliminated. */
Ian Romanick42305fb2013-09-10 12:00:34 -05002271 if (!check_against_output_limit(ctx, prog, sh_i))
2272 goto done;
2273 if (!check_against_input_limit(ctx, prog, sh_next))
Marek Olšák3c555822013-06-13 03:17:22 +02002274 goto done;
2275
Marek Olšák284d9542013-06-12 02:18:09 +02002276 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07002277 }
2278
2279 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2280 goto done;
2281
Ian Romanick960d7222011-10-21 11:21:02 -07002282 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002283 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002284 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002285
Paul Berryb95d2372013-07-27 11:08:31 -07002286 check_resources(ctx, prog);
2287 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08002288 goto done;
2289
Ian Romanickce9171f2011-02-03 17:10:14 -08002290 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002291 * present in a linked program. By checking prog->IsES, we also
2292 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002293 */
Eric Anholt57f79782011-07-22 12:57:47 -07002294 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002295 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002296 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002297 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002298 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002299 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002300 }
2301 }
2302
Ian Romanick13e10e42010-06-21 12:03:24 -07002303 /* FINISHME: Assign fragment shader output locations. */
2304
Ian Romanick832dfa52010-06-17 15:04:20 -07002305done:
2306 free(vert_shader_list);
Paul Berry844bd712013-07-30 22:38:43 -07002307 free(frag_shader_list);
Bryan Cain25480922013-02-15 09:46:50 -06002308 free(geom_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002309
2310 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2311 if (prog->_LinkedShaders[i] == NULL)
2312 continue;
2313
2314 /* Retain any live IR, but trash the rest. */
2315 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002316
2317 /* The symbol table in the linked shaders may contain references to
2318 * variables that were removed (e.g., unused uniforms). Since it may
2319 * contain junk, there is no possible valid use. Delete it and set the
2320 * pointer to NULL.
2321 */
2322 delete prog->_LinkedShaders[i]->symbols;
2323 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002324 }
2325
Kenneth Graunked3073f52011-01-21 14:32:31 -08002326 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002327}