blob: 8a143fddfca8299576fd411fd6ce0d4f7cc43816 [file] [log] [blame]
Ian Romanick832dfa52010-06-17 15:04:20 -07001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * \file linker.cpp
26 * GLSL linker implementation
27 *
28 * Given a set of shaders that are to be linked to generate a final program,
29 * there are three distinct stages.
30 *
31 * In the first stage shaders are partitioned into groups based on the shader
32 * type. All shaders of a particular type (e.g., vertex shaders) are linked
33 * together.
34 *
35 * - Undefined references in each shader are resolve to definitions in
36 * another shader.
37 * - Types and qualifiers of uniforms, outputs, and global variables defined
38 * in multiple shaders with the same name are verified to be the same.
39 * - Initializers for uniforms and global variables defined
40 * in multiple shaders with the same name are verified to be the same.
41 *
42 * The result, in the terminology of the GLSL spec, is a set of shader
43 * executables for each processing unit.
44 *
45 * After the first stage is complete, a series of semantic checks are performed
46 * on each of the shader executables.
47 *
48 * - Each shader executable must define a \c main function.
49 * - Each vertex shader executable must write to \c gl_Position.
50 * - Each fragment shader executable must write to either \c gl_FragData or
51 * \c gl_FragColor.
52 *
53 * In the final stage individual shader executables are linked to create a
54 * complete exectuable.
55 *
56 * - Types of uniforms defined in multiple shader stages with the same name
57 * are verified to be the same.
58 * - Initializers for uniforms defined in multiple shader stages with the
59 * same name are verified to be the same.
60 * - Types and qualifiers of outputs defined in one stage are verified to
61 * be the same as the types and qualifiers of inputs defined with the same
62 * name in a later stage.
63 *
64 * \author Ian Romanick <ian.d.romanick@intel.com>
65 */
Ian Romanickf36460e2010-06-23 12:07:22 -070066
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080067#include "main/core.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070068#include "glsl_symbol_table.h"
Eric Anholtfaf3dba2013-06-12 16:57:11 -070069#include "glsl_parser_extras.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070070#include "ir.h"
71#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030072#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070073#include "linker.h"
Paul Berry4b11b572012-12-17 14:20:35 -080074#include "link_varyings.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070075#include "ir_optimization.h"
Bryan Cain25480922013-02-15 09:46:50 -060076#include "ir_rvalue_visitor.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070077
Ian Romanick3322fba2010-10-14 13:28:42 -070078extern "C" {
79#include "main/shaderobj.h"
Eric Anholt6065a872013-06-12 18:12:40 -070080#include "main/enums.h"
Ian Romanick3322fba2010-10-14 13:28:42 -070081}
82
Bryan Cain25480922013-02-15 09:46:50 -060083void linker_error(gl_shader_program *, const char *, ...);
84
Ian Romanick832dfa52010-06-17 15:04:20 -070085/**
86 * Visitor that determines whether or not a variable is ever written.
87 */
88class find_assignment_visitor : public ir_hierarchical_visitor {
89public:
90 find_assignment_visitor(const char *name)
91 : name(name), found(false)
92 {
93 /* empty */
94 }
95
96 virtual ir_visitor_status visit_enter(ir_assignment *ir)
97 {
98 ir_variable *const var = ir->lhs->variable_referenced();
99
100 if (strcmp(name, var->name) == 0) {
101 found = true;
102 return visit_stop;
103 }
104
105 return visit_continue_with_parent;
106 }
107
Eric Anholt18a60232010-08-23 11:29:25 -0700108 virtual ir_visitor_status visit_enter(ir_call *ir)
109 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700110 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700111 foreach_iter(exec_list_iterator, iter, *ir) {
112 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
113 ir_variable *sig_param = (ir_variable *)sig_iter.get();
114
Paul Berry42a29d82013-01-11 14:39:32 -0800115 if (sig_param->mode == ir_var_function_out ||
116 sig_param->mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700117 ir_variable *var = param_rval->variable_referenced();
118 if (var && strcmp(name, var->name) == 0) {
119 found = true;
120 return visit_stop;
121 }
122 }
123 sig_iter.next();
124 }
125
Kenneth Graunked884f602012-03-20 15:56:37 -0700126 if (ir->return_deref != NULL) {
127 ir_variable *const var = ir->return_deref->variable_referenced();
128
129 if (strcmp(name, var->name) == 0) {
130 found = true;
131 return visit_stop;
132 }
133 }
134
Eric Anholt18a60232010-08-23 11:29:25 -0700135 return visit_continue_with_parent;
136 }
137
Ian Romanick832dfa52010-06-17 15:04:20 -0700138 bool variable_found()
139 {
140 return found;
141 }
142
143private:
144 const char *name; /**< Find writes to a variable with this name. */
145 bool found; /**< Was a write to the variable found? */
146};
147
Ian Romanickc93b8f12010-06-17 15:20:22 -0700148
Ian Romanickc33e78f2010-08-13 12:30:41 -0700149/**
150 * Visitor that determines whether or not a variable is ever read.
151 */
152class find_deref_visitor : public ir_hierarchical_visitor {
153public:
154 find_deref_visitor(const char *name)
155 : name(name), found(false)
156 {
157 /* empty */
158 }
159
160 virtual ir_visitor_status visit(ir_dereference_variable *ir)
161 {
162 if (strcmp(this->name, ir->var->name) == 0) {
163 this->found = true;
164 return visit_stop;
165 }
166
167 return visit_continue;
168 }
169
170 bool variable_found() const
171 {
172 return this->found;
173 }
174
175private:
176 const char *name; /**< Find writes to a variable with this name. */
177 bool found; /**< Was a write to the variable found? */
178};
179
180
Paul Berry7cfefe62013-07-30 21:13:48 -0700181class geom_array_resize_visitor : public ir_hierarchical_visitor {
182public:
183 unsigned num_vertices;
184 gl_shader_program *prog;
185
186 geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
187 {
188 this->num_vertices = num_vertices;
189 this->prog = prog;
190 }
191
192 virtual ~geom_array_resize_visitor()
193 {
194 /* empty */
195 }
196
197 virtual ir_visitor_status visit(ir_variable *var)
198 {
199 if (!var->type->is_array() || var->mode != ir_var_shader_in)
200 return visit_continue;
201
202 unsigned size = var->type->length;
203
204 /* Generate a link error if the shader has declared this array with an
205 * incorrect size.
206 */
207 if (size && size != this->num_vertices) {
208 linker_error(this->prog, "size of array %s declared as %u, "
209 "but number of input vertices is %u\n",
210 var->name, size, this->num_vertices);
211 return visit_continue;
212 }
213
214 /* Generate a link error if the shader attempts to access an input
215 * array using an index too large for its actual size assigned at link
216 * time.
217 */
218 if (var->max_array_access >= this->num_vertices) {
219 linker_error(this->prog, "geometry shader accesses element %i of "
220 "%s, but only %i input vertices\n",
221 var->max_array_access, var->name, this->num_vertices);
222 return visit_continue;
223 }
224
225 var->type = glsl_type::get_array_instance(var->type->element_type(),
226 this->num_vertices);
227 var->max_array_access = this->num_vertices - 1;
228
229 return visit_continue;
230 }
231
232 /* Dereferences of input variables need to be updated so that their type
233 * matches the newly assigned type of the variable they are accessing. */
234 virtual ir_visitor_status visit(ir_dereference_variable *ir)
235 {
236 ir->type = ir->var->type;
237 return visit_continue;
238 }
239
240 /* Dereferences of 2D input arrays need to be updated so that their type
241 * matches the newly assigned type of the array they are accessing. */
242 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
243 {
244 const glsl_type *const vt = ir->array->type;
245 if (vt->is_array())
246 ir->type = vt->element_type();
247 return visit_continue;
248 }
249};
250
251
Paul Berry1a33e022013-08-18 20:59:37 -0700252/**
253 * Visitor that determines whether or not a shader uses ir_end_primitive.
254 */
255class find_end_primitive_visitor : public ir_hierarchical_visitor {
256public:
257 find_end_primitive_visitor()
258 : found(false)
259 {
260 /* empty */
261 }
262
263 virtual ir_visitor_status visit(ir_end_primitive *)
264 {
265 found = true;
266 return visit_stop;
267 }
268
269 bool end_primitive_found()
270 {
271 return found;
272 }
273
274private:
275 bool found;
276};
277
278
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700279void
Ian Romanick586e7412011-07-28 14:04:09 -0700280linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700281{
282 va_list ap;
283
Kenneth Graunked3073f52011-01-21 14:32:31 -0800284 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700285 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800286 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700287 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700288
289 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700290}
291
292
293void
Ian Romanick379a32f2011-07-28 14:09:06 -0700294linker_warning(gl_shader_program *prog, const char *fmt, ...)
295{
296 va_list ap;
297
298 ralloc_strcat(&prog->InfoLog, "error: ");
299 va_start(ap, fmt);
300 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
301 va_end(ap);
302
303}
304
305
Paul Berryb92900d2013-01-28 14:21:59 -0800306/**
307 * Given a string identifying a program resource, break it into a base name
308 * and an optional array index in square brackets.
309 *
310 * If an array index is present, \c out_base_name_end is set to point to the
311 * "[" that precedes the array index, and the array index itself is returned
312 * as a long.
313 *
314 * If no array index is present (or if the array index is negative or
315 * mal-formed), \c out_base_name_end, is set to point to the null terminator
316 * at the end of the input string, and -1 is returned.
317 *
318 * Only the final array index is parsed; if the string contains other array
319 * indices (or structure field accesses), they are left in the base name.
320 *
321 * No attempt is made to check that the base name is properly formed;
322 * typically the caller will look up the base name in a hash table, so
323 * ill-formed base names simply turn into hash table lookup failures.
324 */
325long
326parse_program_resource_name(const GLchar *name,
327 const GLchar **out_base_name_end)
328{
329 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
330 *
331 * "When an integer array element or block instance number is part of
332 * the name string, it will be specified in decimal form without a "+"
333 * or "-" sign or any extra leading zeroes. Additionally, the name
334 * string will not include white space anywhere in the string."
335 */
336
337 const size_t len = strlen(name);
338 *out_base_name_end = name + len;
339
340 if (len == 0 || name[len-1] != ']')
341 return -1;
342
343 /* Walk backwards over the string looking for a non-digit character. This
344 * had better be the opening bracket for an array index.
345 *
346 * Initially, i specifies the location of the ']'. Since the string may
347 * contain only the ']' charcater, walk backwards very carefully.
348 */
349 unsigned i;
350 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
351 /* empty */ ;
352
353 if ((i == 0) || name[i-1] != '[')
354 return -1;
355
356 long array_index = strtol(&name[i], NULL, 10);
357 if (array_index < 0)
358 return -1;
359
360 *out_base_name_end = name + (i - 1);
361 return array_index;
362}
363
364
Ian Romanick379a32f2011-07-28 14:09:06 -0700365void
Paul Berry50895d42012-12-05 07:17:07 -0800366link_invalidate_variable_locations(gl_shader *sh, int input_base,
367 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700368{
Eric Anholt16b68b12010-06-30 11:05:43 -0700369 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700370 ir_variable *const var = ((ir_instruction *) node)->as_variable();
371
Paul Berry50895d42012-12-05 07:17:07 -0800372 if (var == NULL)
373 continue;
374
375 int base;
376 switch (var->mode) {
Paul Berry42a29d82013-01-11 14:39:32 -0800377 case ir_var_shader_in:
Paul Berry50895d42012-12-05 07:17:07 -0800378 base = input_base;
379 break;
Paul Berry42a29d82013-01-11 14:39:32 -0800380 case ir_var_shader_out:
Paul Berry50895d42012-12-05 07:17:07 -0800381 base = output_base;
382 break;
383 default:
384 continue;
385 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700386
387 /* Only assign locations for generic attributes / varyings / etc.
388 */
Paul Berry50895d42012-12-05 07:17:07 -0800389 if ((var->location >= base) && !var->explicit_location)
390 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800391
Paul Berry3e81c662012-12-05 10:47:55 -0800392 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800393 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800394 var->location_frac = 0;
395 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800396 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800397 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700398 }
399}
400
401
Ian Romanickc93b8f12010-06-17 15:20:22 -0700402/**
Paul Berry44e07de2013-06-11 14:11:05 -0700403 * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
404 *
405 * Also check for errors based on incorrect usage of gl_ClipVertex and
406 * gl_ClipDistance.
407 *
408 * Return false if an error was reported.
409 */
410static void
411analyze_clip_usage(const char *shader_type, struct gl_shader_program *prog,
412 struct gl_shader *shader, GLboolean *UsesClipDistance,
413 GLuint *ClipDistanceArraySize)
414{
415 *ClipDistanceArraySize = 0;
416
417 if (!prog->IsES && prog->Version >= 130) {
418 /* From section 7.1 (Vertex Shader Special Variables) of the
419 * GLSL 1.30 spec:
420 *
421 * "It is an error for a shader to statically write both
422 * gl_ClipVertex and gl_ClipDistance."
423 *
424 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
425 * gl_ClipVertex nor gl_ClipDistance.
426 */
427 find_assignment_visitor clip_vertex("gl_ClipVertex");
428 find_assignment_visitor clip_distance("gl_ClipDistance");
429
430 clip_vertex.run(shader->ir);
431 clip_distance.run(shader->ir);
432 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
433 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
434 "and `gl_ClipDistance'\n", shader_type);
435 return;
436 }
437 *UsesClipDistance = clip_distance.variable_found();
438 ir_variable *clip_distance_var =
439 shader->symbols->get_variable("gl_ClipDistance");
440 if (clip_distance_var)
441 *ClipDistanceArraySize = clip_distance_var->type->length;
442 } else {
443 *UsesClipDistance = false;
444 }
445}
446
447
448/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700449 * Verify that a vertex shader executable meets all semantic requirements.
450 *
Paul Berry642e5b412012-01-04 13:57:52 -0800451 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
452 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700453 *
454 * \param shader Vertex shader executable to be verified
455 */
Paul Berryb95d2372013-07-27 11:08:31 -0700456void
Eric Anholt849e1812010-06-30 11:49:17 -0700457validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700458 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700459{
460 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700461 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700462
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700463 /* From the GLSL 1.10 spec, page 48:
464 *
465 * "The variable gl_Position is available only in the vertex
466 * language and is intended for writing the homogeneous vertex
467 * position. All executions of a well-formed vertex shader
468 * executable must write a value into this variable. [...] The
469 * variable gl_Position is available only in the vertex
470 * language and is intended for writing the homogeneous vertex
471 * position. All executions of a well-formed vertex shader
472 * executable must write a value into this variable."
473 *
474 * while in GLSL 1.40 this text is changed to:
475 *
476 * "The variable gl_Position is available only in the vertex
477 * language and is intended for writing the homogeneous vertex
478 * position. It can be written at any time during shader
479 * execution. It may also be read back by a vertex shader
480 * after being written. This value will be used by primitive
481 * assembly, clipping, culling, and other fixed functionality
482 * operations, if present, that operate on primitives after
483 * vertex processing has occurred. Its value is undefined if
484 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700485 *
486 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
487 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700488 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700489 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700490 find_assignment_visitor find("gl_Position");
491 find.run(shader->ir);
492 if (!find.variable_found()) {
493 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700494 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700495 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700496 }
497
Paul Berry44e07de2013-06-11 14:11:05 -0700498 analyze_clip_usage("vertex", prog, shader, &prog->Vert.UsesClipDistance,
499 &prog->Vert.ClipDistanceArraySize);
Ian Romanick832dfa52010-06-17 15:04:20 -0700500}
501
502
Ian Romanickc93b8f12010-06-17 15:20:22 -0700503/**
504 * Verify that a fragment shader executable meets all semantic requirements
505 *
506 * \param shader Fragment shader executable to be verified
507 */
Paul Berryb95d2372013-07-27 11:08:31 -0700508void
Eric Anholt849e1812010-06-30 11:49:17 -0700509validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700510 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700511{
512 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700513 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700514
Ian Romanick832dfa52010-06-17 15:04:20 -0700515 find_assignment_visitor frag_color("gl_FragColor");
516 find_assignment_visitor frag_data("gl_FragData");
517
Eric Anholt16b68b12010-06-30 11:05:43 -0700518 frag_color.run(shader->ir);
519 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700520
Ian Romanick832dfa52010-06-17 15:04:20 -0700521 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700522 linker_error(prog, "fragment shader writes to both "
523 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700524 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700525}
526
Bryan Cain25480922013-02-15 09:46:50 -0600527/**
528 * Verify that a geometry shader executable meets all semantic requirements
529 *
Paul Berry44e07de2013-06-11 14:11:05 -0700530 * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
531 * prog->Geom.ClipDistanceArraySize as a side effect.
Bryan Cain25480922013-02-15 09:46:50 -0600532 *
533 * \param shader Geometry shader executable to be verified
534 */
535void
536validate_geometry_shader_executable(struct gl_shader_program *prog,
537 struct gl_shader *shader)
538{
539 if (shader == NULL)
540 return;
541
542 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
543 prog->Geom.VerticesIn = num_vertices;
Paul Berry44e07de2013-06-11 14:11:05 -0700544
545 analyze_clip_usage("geometry", prog, shader, &prog->Geom.UsesClipDistance,
546 &prog->Geom.ClipDistanceArraySize);
Paul Berry1a33e022013-08-18 20:59:37 -0700547
548 find_end_primitive_visitor end_primitive;
549 end_primitive.run(shader->ir);
550 prog->Geom.UsesEndPrimitive = end_primitive.end_primitive_found();
Bryan Cain25480922013-02-15 09:46:50 -0600551}
552
Ian Romanick832dfa52010-06-17 15:04:20 -0700553
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700554/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700555 * Generate a string describing the mode of a variable
556 */
557static const char *
558mode_string(const ir_variable *var)
559{
560 switch (var->mode) {
561 case ir_var_auto:
562 return (var->read_only) ? "global constant" : "global variable";
563
Paul Berry42a29d82013-01-11 14:39:32 -0800564 case ir_var_uniform: return "uniform";
565 case ir_var_shader_in: return "shader input";
566 case ir_var_shader_out: return "shader output";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700567
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800568 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700569 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700570 default:
571 assert(!"Should not get here.");
572 return "invalid variable";
573 }
574}
575
576
577/**
578 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700579 */
Paul Berryb95d2372013-07-27 11:08:31 -0700580void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700581cross_validate_globals(struct gl_shader_program *prog,
582 struct gl_shader **shader_list,
583 unsigned num_shaders,
584 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700585{
586 /* Examine all of the uniforms in all of the shaders and cross validate
587 * them.
588 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700589 glsl_symbol_table variables;
590 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700591 if (shader_list[i] == NULL)
592 continue;
593
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700594 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700595 ir_variable *const var = ((ir_instruction *) node)->as_variable();
596
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700597 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700598 continue;
599
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700600 if (uniforms_only && (var->mode != ir_var_uniform))
601 continue;
602
Ian Romanick7e2aa912010-07-19 17:12:42 -0700603 /* Don't cross validate temporaries that are at global scope. These
604 * will eventually get pulled into the shaders 'main'.
605 */
606 if (var->mode == ir_var_temporary)
607 continue;
608
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700609 /* If a global with this name has already been seen, verify that the
610 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700611 * initializers, the values of the initializers must be the same.
612 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700613 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700614 if (existing != NULL) {
615 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700616 /* Consider the types to be "the same" if both types are arrays
617 * of the same type and one of the arrays is implicitly sized.
618 * In addition, set the type of the linked variable to the
619 * explicitly sized array.
620 */
621 if (var->type->is_array()
622 && existing->type->is_array()
623 && (var->type->fields.array == existing->type->fields.array)
624 && ((var->type->length == 0)
625 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800626 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700627 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800628 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700629 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700630 linker_error(prog, "%s `%s' declared as type "
631 "`%s' and type `%s'\n",
632 mode_string(var),
633 var->name, var->type->name,
634 existing->type->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700635 return;
Ian Romanicka2711d62010-08-29 22:07:49 -0700636 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700637 }
638
Ian Romanick68a4fc92010-10-07 17:21:22 -0700639 if (var->explicit_location) {
640 if (existing->explicit_location
641 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700642 linker_error(prog, "explicit locations for %s "
643 "`%s' have differing values\n",
644 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700645 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700646 }
647
648 existing->location = var->location;
649 existing->explicit_location = true;
650 }
651
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700652 /* From the GLSL 4.20 specification:
653 * "A link error will result if two compilation units in a program
654 * specify different integer-constant bindings for the same
655 * opaque-uniform name. However, it is not an error to specify a
656 * binding on some but not all declarations for the same name"
657 */
658 if (var->explicit_binding) {
659 if (existing->explicit_binding &&
660 var->binding != existing->binding) {
661 linker_error(prog, "explicit bindings for %s "
662 "`%s' have differing values\n",
663 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700664 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700665 }
666
667 existing->binding = var->binding;
668 existing->explicit_binding = true;
669 }
670
Ian Romanick46173f92011-10-31 13:07:06 -0700671 /* Validate layout qualifiers for gl_FragDepth.
672 *
673 * From the AMD/ARB_conservative_depth specs:
674 *
675 * "If gl_FragDepth is redeclared in any fragment shader in a
676 * program, it must be redeclared in all fragment shaders in
677 * that program that have static assignments to
678 * gl_FragDepth. All redeclarations of gl_FragDepth in all
679 * fragment shaders in a single program must have the same set
680 * of qualifiers."
681 */
682 if (strcmp(var->name, "gl_FragDepth") == 0) {
683 bool layout_declared = var->depth_layout != ir_depth_layout_none;
684 bool layout_differs =
685 var->depth_layout != existing->depth_layout;
686
687 if (layout_declared && layout_differs) {
688 linker_error(prog,
689 "All redeclarations of gl_FragDepth in all "
690 "fragment shaders in a single program must have "
691 "the same set of qualifiers.");
692 }
693
694 if (var->used && layout_differs) {
695 linker_error(prog,
696 "If gl_FragDepth is redeclared with a layout "
697 "qualifier in any fragment shader, it must be "
698 "redeclared with the same layout qualifier in "
699 "all fragment shaders that have assignments to "
700 "gl_FragDepth");
701 }
702 }
Chad Versaceaddae332011-01-27 01:40:31 -0800703
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700704 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
705 *
706 * "If a shared global has multiple initializers, the
707 * initializers must all be constant expressions, and they
708 * must all have the same value. Otherwise, a link error will
709 * result. (A shared global having only one initializer does
710 * not require that initializer to be a constant expression.)"
711 *
712 * Previous to 4.20 the GLSL spec simply said that initializers
713 * must have the same value. In this case of non-constant
714 * initializers, this was impossible to determine. As a result,
715 * no vendor actually implemented that behavior. The 4.20
716 * behavior matches the implemented behavior of at least one other
717 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700718 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700719 if (var->constant_initializer != NULL) {
720 if (existing->constant_initializer != NULL) {
721 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700722 linker_error(prog, "initializers for %s "
723 "`%s' have differing values\n",
724 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700725 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700726 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700727 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700728 /* If the first-seen instance of a particular uniform did not
729 * have an initializer but a later instance does, copy the
730 * initializer to the version stored in the symbol table.
731 */
Ian Romanickde415b72010-07-14 13:22:12 -0700732 /* FINISHME: This is wrong. The constant_value field should
733 * FINISHME: not be modified! Imagine a case where a shader
734 * FINISHME: without an initializer is linked in two different
735 * FINISHME: programs with shaders that have differing
736 * FINISHME: initializers. Linking with the first will
737 * FINISHME: modify the shader, and linking with the second
738 * FINISHME: will fail.
739 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700740 existing->constant_initializer =
741 var->constant_initializer->clone(ralloc_parent(existing),
742 NULL);
743 }
744 }
745
746 if (var->has_initializer) {
747 if (existing->has_initializer
748 && (var->constant_initializer == NULL
749 || existing->constant_initializer == NULL)) {
750 linker_error(prog,
751 "shared global variable `%s' has multiple "
752 "non-constant initializers.\n",
753 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700754 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700755 }
756
757 /* Some instance had an initializer, so keep track of that. In
758 * this location, all sorts of initializers (constant or
759 * otherwise) will propagate the existence to the variable
760 * stored in the symbol table.
761 */
762 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700763 }
Chad Versace7528f142010-11-17 14:34:38 -0800764
765 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700766 linker_error(prog, "declarations for %s `%s' have "
767 "mismatching invariant qualifiers\n",
768 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700769 return;
Chad Versace7528f142010-11-17 14:34:38 -0800770 }
Chad Versace61428dd2011-01-10 15:29:30 -0800771 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700772 linker_error(prog, "declarations for %s `%s' have "
773 "mismatching centroid qualifiers\n",
774 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700775 return;
Chad Versace61428dd2011-01-10 15:29:30 -0800776 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700777 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700778 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700779 }
780 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700781}
782
783
Ian Romanick37101922010-06-18 19:02:10 -0700784/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700785 * Perform validation of uniforms used across multiple shader stages
786 */
Paul Berryb95d2372013-07-27 11:08:31 -0700787void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700788cross_validate_uniforms(struct gl_shader_program *prog)
789{
Paul Berryb95d2372013-07-27 11:08:31 -0700790 cross_validate_globals(prog, prog->_LinkedShaders,
791 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700792}
793
Eric Anholtf609cf72012-04-27 13:52:56 -0700794/**
795 * Accumulates the array of prog->UniformBlocks and checks that all
796 * definitons of blocks agree on their contents.
797 */
798static bool
799interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
800{
801 unsigned max_num_uniform_blocks = 0;
802 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
803 if (prog->_LinkedShaders[i])
804 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
805 }
806
807 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
808 struct gl_shader *sh = prog->_LinkedShaders[i];
809
810 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
811 max_num_uniform_blocks);
812 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
813 prog->UniformBlockStageIndex[i][j] = -1;
814
815 if (sh == NULL)
816 continue;
817
818 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
819 int index = link_cross_validate_uniform_block(prog,
820 &prog->UniformBlocks,
821 &prog->NumUniformBlocks,
822 &sh->UniformBlocks[j]);
823
824 if (index == -1) {
825 linker_error(prog, "uniform block `%s' has mismatching definitions",
826 sh->UniformBlocks[j].Name);
827 return false;
828 }
829
830 prog->UniformBlockStageIndex[i][index] = j;
831 }
832 }
833
834 return true;
835}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700836
Ian Romanick37101922010-06-18 19:02:10 -0700837
Ian Romanick3fb87872010-07-09 14:09:34 -0700838/**
839 * Populates a shaders symbol table with all global declarations
840 */
841static void
842populate_symbol_table(gl_shader *sh)
843{
844 sh->symbols = new(sh) glsl_symbol_table;
845
846 foreach_list(node, sh->ir) {
847 ir_instruction *const inst = (ir_instruction *) node;
848 ir_variable *var;
849 ir_function *func;
850
851 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700852 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700853 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700854 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700855 }
856 }
857}
858
859
860/**
Ian Romanick31a97862010-07-12 18:48:50 -0700861 * Remap variables referenced in an instruction tree
862 *
863 * This is used when instruction trees are cloned from one shader and placed in
864 * another. These trees will contain references to \c ir_variable nodes that
865 * do not exist in the target shader. This function finds these \c ir_variable
866 * references and replaces the references with matching variables in the target
867 * shader.
868 *
869 * If there is no matching variable in the target shader, a clone of the
870 * \c ir_variable is made and added to the target shader. The new variable is
871 * added to \b both the instruction stream and the symbol table.
872 *
873 * \param inst IR tree that is to be processed.
874 * \param symbols Symbol table containing global scope symbols in the
875 * linked shader.
876 * \param instructions Instruction stream where new variable declarations
877 * should be added.
878 */
879void
Eric Anholt8273bd42010-08-04 12:34:56 -0700880remap_variables(ir_instruction *inst, struct gl_shader *target,
881 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700882{
883 class remap_visitor : public ir_hierarchical_visitor {
884 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700885 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700886 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700887 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700888 this->target = target;
889 this->symbols = target->symbols;
890 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700891 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700892 }
893
894 virtual ir_visitor_status visit(ir_dereference_variable *ir)
895 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700896 if (ir->var->mode == ir_var_temporary) {
897 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
898
899 assert(var != NULL);
900 ir->var = var;
901 return visit_continue;
902 }
903
Ian Romanick31a97862010-07-12 18:48:50 -0700904 ir_variable *const existing =
905 this->symbols->get_variable(ir->var->name);
906 if (existing != NULL)
907 ir->var = existing;
908 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700909 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700910
Eric Anholt001eee52010-11-05 06:11:24 -0700911 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700912 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700913 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700914 }
915
916 return visit_continue;
917 }
918
919 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700920 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700921 glsl_symbol_table *symbols;
922 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700923 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700924 };
925
Eric Anholt8273bd42010-08-04 12:34:56 -0700926 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700927
928 inst->accept(&v);
929}
930
931
932/**
933 * Move non-declarations from one instruction stream to another
934 *
935 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700936 * 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 -0700937 * pointer) for \c last and \c false for \c make_copies on the first
938 * call. Successive calls pass the return value of the previous call for
939 * \c last and \c true for \c make_copies.
940 *
941 * \param instructions Source instruction stream
942 * \param last Instruction after which new instructions should be
943 * inserted in the target instruction stream
944 * \param make_copies Flag selecting whether instructions in \c instructions
945 * should be copied (via \c ir_instruction::clone) into the
946 * target list or moved.
947 *
948 * \return
949 * The new "last" instruction in the target instruction stream. This pointer
950 * is suitable for use as the \c last parameter of a later call to this
951 * function.
952 */
953exec_node *
954move_non_declarations(exec_list *instructions, exec_node *last,
955 bool make_copies, gl_shader *target)
956{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700957 hash_table *temps = NULL;
958
959 if (make_copies)
960 temps = hash_table_ctor(0, hash_table_pointer_hash,
961 hash_table_pointer_compare);
962
Ian Romanick303c99f2010-07-19 12:34:56 -0700963 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700964 ir_instruction *inst = (ir_instruction *) node;
965
Ian Romanick7e2aa912010-07-19 17:12:42 -0700966 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700967 continue;
968
Ian Romanick7e2aa912010-07-19 17:12:42 -0700969 ir_variable *var = inst->as_variable();
970 if ((var != NULL) && (var->mode != ir_var_temporary))
971 continue;
972
973 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700974 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700975 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700976 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700977
978 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700979 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700980
981 if (var != NULL)
982 hash_table_insert(temps, inst, var);
983 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700984 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700985 } else {
986 inst->remove();
987 }
988
989 last->insert_after(inst);
990 last = inst;
991 }
992
Ian Romanick7e2aa912010-07-19 17:12:42 -0700993 if (make_copies)
994 hash_table_dtor(temps);
995
Ian Romanick31a97862010-07-12 18:48:50 -0700996 return last;
997}
998
999/**
Ian Romanick15ce87e2010-07-09 15:28:22 -07001000 * Get the function signature for main from a shader
1001 */
1002static ir_function_signature *
1003get_main_function_signature(gl_shader *sh)
1004{
1005 ir_function *const f = sh->symbols->get_function("main");
1006 if (f != NULL) {
1007 exec_list void_parameters;
1008
1009 /* Look for the 'void main()' signature and ensure that it's defined.
1010 * This keeps the linker from accidentally pick a shader that just
1011 * contains a prototype for main.
1012 *
1013 * We don't have to check for multiple definitions of main (in multiple
1014 * shaders) because that would have already been caught above.
1015 */
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001016 ir_function_signature *sig = f->matching_signature(NULL, &void_parameters);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001017 if ((sig != NULL) && sig->is_defined) {
1018 return sig;
1019 }
1020 }
1021
1022 return NULL;
1023}
1024
1025
1026/**
Brian Paul84a12732012-02-02 20:10:40 -07001027 * This class is only used in link_intrastage_shaders() below but declaring
1028 * it inside that function leads to compiler warnings with some versions of
1029 * gcc.
1030 */
1031class array_sizing_visitor : public ir_hierarchical_visitor {
1032public:
1033 virtual ir_visitor_status visit(ir_variable *var)
1034 {
1035 if (var->type->is_array() && (var->type->length == 0)) {
1036 const glsl_type *type =
1037 glsl_type::get_array_instance(var->type->fields.array,
1038 var->max_array_access + 1);
1039 assert(type != NULL);
1040 var->type = type;
1041 }
1042 return visit_continue;
1043 }
1044};
1045
Brian Paul84a12732012-02-02 20:10:40 -07001046/**
Eric Anholt6065a872013-06-12 18:12:40 -07001047 * Performs the cross-validation of geometry shader max_vertices and
1048 * primitive type layout qualifiers for the attached geometry shaders,
1049 * and propagates them to the linked GS and linked shader program.
1050 */
1051static void
1052link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
1053 struct gl_shader *linked_shader,
1054 struct gl_shader **shader_list,
1055 unsigned num_shaders)
1056{
1057 linked_shader->Geom.VerticesOut = 0;
1058 linked_shader->Geom.InputType = PRIM_UNKNOWN;
1059 linked_shader->Geom.OutputType = PRIM_UNKNOWN;
1060
1061 /* No in/out qualifiers defined for anything but GLSL 1.50+
1062 * geometry shaders so far.
1063 */
1064 if (linked_shader->Type != GL_GEOMETRY_SHADER || prog->Version < 150)
1065 return;
1066
1067 /* From the GLSL 1.50 spec, page 46:
1068 *
1069 * "All geometry shader output layout declarations in a program
1070 * must declare the same layout and same value for
1071 * max_vertices. There must be at least one geometry output
1072 * layout declaration somewhere in a program, but not all
1073 * geometry shaders (compilation units) are required to
1074 * declare it."
1075 */
1076
1077 for (unsigned i = 0; i < num_shaders; i++) {
1078 struct gl_shader *shader = shader_list[i];
1079
1080 if (shader->Geom.InputType != PRIM_UNKNOWN) {
1081 if (linked_shader->Geom.InputType != PRIM_UNKNOWN &&
1082 linked_shader->Geom.InputType != shader->Geom.InputType) {
1083 linker_error(prog, "geometry shader defined with conflicting "
1084 "input types\n");
1085 return;
1086 }
1087 linked_shader->Geom.InputType = shader->Geom.InputType;
1088 }
1089
1090 if (shader->Geom.OutputType != PRIM_UNKNOWN) {
1091 if (linked_shader->Geom.OutputType != PRIM_UNKNOWN &&
1092 linked_shader->Geom.OutputType != shader->Geom.OutputType) {
1093 linker_error(prog, "geometry shader defined with conflicting "
1094 "output types\n");
1095 return;
1096 }
1097 linked_shader->Geom.OutputType = shader->Geom.OutputType;
1098 }
1099
1100 if (shader->Geom.VerticesOut != 0) {
1101 if (linked_shader->Geom.VerticesOut != 0 &&
1102 linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) {
1103 linker_error(prog, "geometry shader defined with conflicting "
1104 "output vertex count (%d and %d)\n",
1105 linked_shader->Geom.VerticesOut,
1106 shader->Geom.VerticesOut);
1107 return;
1108 }
1109 linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut;
1110 }
1111 }
1112
1113 /* Just do the intrastage -> interstage propagation right now,
1114 * since we already know we're in the right type of shader program
1115 * for doing it.
1116 */
1117 if (linked_shader->Geom.InputType == PRIM_UNKNOWN) {
1118 linker_error(prog,
1119 "geometry shader didn't declare primitive input type\n");
1120 return;
1121 }
1122 prog->Geom.InputType = linked_shader->Geom.InputType;
1123
1124 if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) {
1125 linker_error(prog,
1126 "geometry shader didn't declare primitive output type\n");
1127 return;
1128 }
1129 prog->Geom.OutputType = linked_shader->Geom.OutputType;
1130
1131 if (linked_shader->Geom.VerticesOut == 0) {
1132 linker_error(prog,
1133 "geometry shader didn't declare max_vertices\n");
1134 return;
1135 }
1136 prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut;
1137}
1138
1139/**
Ian Romanick3fb87872010-07-09 14:09:34 -07001140 * Combine a group of shaders for a single stage to generate a linked shader
1141 *
1142 * \note
1143 * If this function is supplied a single shader, it is cloned, and the new
1144 * shader is returned.
1145 */
1146static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001147link_intrastage_shaders(void *mem_ctx,
1148 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -07001149 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -07001150 struct gl_shader **shader_list,
1151 unsigned num_shaders)
1152{
Eric Anholtf609cf72012-04-27 13:52:56 -07001153 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -07001154
Ian Romanick13f782c2010-06-29 18:53:38 -07001155 /* Check that global variables defined in multiple shaders are consistent.
1156 */
Paul Berryb95d2372013-07-27 11:08:31 -07001157 cross_validate_globals(prog, shader_list, num_shaders, false);
1158 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -07001159 return NULL;
1160
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001161 /* Check that interface blocks defined in multiple shaders are consistent.
1162 */
Paul Berryb95d2372013-07-27 11:08:31 -07001163 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
1164 num_shaders);
1165 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001166 return NULL;
1167
Paul Berry4682b9b2013-07-27 15:07:08 -07001168 /* Link up uniform blocks defined within this stage. */
1169 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -05001170 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
1171 &uniform_blocks);
Eric Anholtf609cf72012-04-27 13:52:56 -07001172
Ian Romanick13f782c2010-06-29 18:53:38 -07001173 /* Check that there is only a single definition of each function signature
1174 * across all shaders.
1175 */
1176 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1177 foreach_list(node, shader_list[i]->ir) {
1178 ir_function *const f = ((ir_instruction *) node)->as_function();
1179
1180 if (f == NULL)
1181 continue;
1182
1183 for (unsigned j = i + 1; j < num_shaders; j++) {
1184 ir_function *const other =
1185 shader_list[j]->symbols->get_function(f->name);
1186
1187 /* If the other shader has no function (and therefore no function
1188 * signatures) with the same name, skip to the next shader.
1189 */
1190 if (other == NULL)
1191 continue;
1192
1193 foreach_iter (exec_list_iterator, iter, *f) {
1194 ir_function_signature *sig =
1195 (ir_function_signature *) iter.get();
1196
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001197 if (!sig->is_defined || sig->is_builtin())
Ian Romanick13f782c2010-06-29 18:53:38 -07001198 continue;
1199
1200 ir_function_signature *other_sig =
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001201 other->exact_matching_signature(NULL, &sig->parameters);
Ian Romanick13f782c2010-06-29 18:53:38 -07001202
1203 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001204 && !other_sig->is_builtin()) {
Ian Romanick586e7412011-07-28 14:04:09 -07001205 linker_error(prog, "function `%s' is multiply defined",
1206 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001207 return NULL;
1208 }
1209 }
1210 }
1211 }
1212 }
1213
1214 /* Find the shader that defines main, and make a clone of it.
1215 *
1216 * Starting with the clone, search for undefined references. If one is
1217 * found, find the shader that defines it. Clone the reference and add
1218 * it to the shader. Repeat until there are no undefined references or
1219 * until a reference cannot be resolved.
1220 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001221 gl_shader *main = NULL;
1222 for (unsigned i = 0; i < num_shaders; i++) {
1223 if (get_main_function_signature(shader_list[i]) != NULL) {
1224 main = shader_list[i];
1225 break;
1226 }
1227 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001228
Ian Romanick15ce87e2010-07-09 15:28:22 -07001229 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001230 linker_error(prog, "%s shader lacks `main'\n",
Eric Anholtfaf3dba2013-06-12 16:57:11 -07001231 _mesa_glsl_shader_target_name(shader_list[0]->Type));
Ian Romanick15ce87e2010-07-09 15:28:22 -07001232 return NULL;
1233 }
1234
Ian Romanick4a455952010-10-13 15:13:02 -07001235 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001236 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001237 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001238
Eric Anholtf609cf72012-04-27 13:52:56 -07001239 linked->UniformBlocks = uniform_blocks;
1240 linked->NumUniformBlocks = num_uniform_blocks;
1241 ralloc_steal(linked, linked->UniformBlocks);
1242
Eric Anholt6065a872013-06-12 18:12:40 -07001243 link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
1244
Ian Romanick15ce87e2010-07-09 15:28:22 -07001245 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001246
Ian Romanick31a97862010-07-12 18:48:50 -07001247 /* The a pointer to the main function in the final linked shader (i.e., the
1248 * copy of the original shader that contained the main function).
1249 */
1250 ir_function_signature *const main_sig = get_main_function_signature(linked);
1251
1252 /* Move any instructions other than variable declarations or function
1253 * declarations into main.
1254 */
Ian Romanick9303e352010-07-19 12:33:54 -07001255 exec_node *insertion_point =
1256 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1257 linked);
1258
Ian Romanick31a97862010-07-12 18:48:50 -07001259 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001260 if (shader_list[i] == main)
1261 continue;
1262
Ian Romanick31a97862010-07-12 18:48:50 -07001263 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001264 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001265 }
1266
Ian Romanick13f782c2010-06-29 18:53:38 -07001267 /* Resolve initializers for global variables in the linked shader.
1268 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001269 unsigned num_linking_shaders = num_shaders;
1270 for (unsigned i = 0; i < num_shaders; i++)
1271 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1272
1273 gl_shader **linking_shaders =
1274 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1275
1276 memcpy(linking_shaders, shader_list,
1277 sizeof(linking_shaders[0]) * num_shaders);
1278
1279 unsigned idx = num_shaders;
1280 for (unsigned i = 0; i < num_shaders; i++) {
1281 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1282 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1283 idx += shader_list[i]->num_builtins_to_link;
1284 }
1285
1286 assert(idx == num_linking_shaders);
1287
Ian Romanick4a455952010-10-13 15:13:02 -07001288 if (!link_function_calls(prog, linked, linking_shaders,
1289 num_linking_shaders)) {
1290 ctx->Driver.DeleteShader(ctx, linked);
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001291 free(linking_shaders);
1292 return NULL;
Ian Romanick4a455952010-10-13 15:13:02 -07001293 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001294
1295 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001296
Paul Berryc148ef62011-08-03 15:37:01 -07001297 /* At this point linked should contain all of the linked IR, so
1298 * validate it to make sure nothing went wrong.
1299 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001300 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07001301
Paul Berry7cfefe62013-07-30 21:13:48 -07001302 /* Set the size of geometry shader input arrays */
1303 if (linked->Type == GL_GEOMETRY_SHADER) {
1304 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
1305 geom_array_resize_visitor input_resize_visitor(num_vertices, prog);
1306 foreach_iter(exec_list_iterator, iter, *linked->ir) {
1307 ir_instruction *ir = (ir_instruction *)iter.get();
1308 ir->accept(&input_resize_visitor);
1309 }
1310 }
1311
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001312 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001313 * unspecified sizes have a size specified. The size is inferred from the
1314 * max_array_access field.
1315 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001316 array_sizing_visitor v;
1317 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001318
Ian Romanick3fb87872010-07-09 14:09:34 -07001319 return linked;
1320}
1321
Eric Anholta721abf2010-08-23 10:32:01 -07001322/**
1323 * Update the sizes of linked shader uniform arrays to the maximum
1324 * array index used.
1325 *
1326 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1327 *
1328 * If one or more elements of an array are active,
1329 * GetActiveUniform will return the name of the array in name,
1330 * subject to the restrictions listed above. The type of the array
1331 * is returned in type. The size parameter contains the highest
1332 * array element index used, plus one. The compiler or linker
1333 * determines the highest index used. There will be only one
1334 * active uniform reported by the GL per uniform array.
1335
1336 */
1337static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001338update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001339{
Ian Romanick3322fba2010-10-14 13:28:42 -07001340 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1341 if (prog->_LinkedShaders[i] == NULL)
1342 continue;
1343
Eric Anholta721abf2010-08-23 10:32:01 -07001344 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1345 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1346
Paul Berry6a2baf32013-06-10 14:01:45 -07001347 if ((var == NULL) || (var->mode != ir_var_uniform) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001348 !var->type->is_array())
1349 continue;
1350
Eric Anholt9feb4032012-05-01 14:43:31 -07001351 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1352 * will not be eliminated. Since we always do std140, just
1353 * don't resize arrays in UBOs.
1354 */
Ian Romanick13be1f42012-12-14 12:00:14 -08001355 if (var->is_in_uniform_block())
Eric Anholt9feb4032012-05-01 14:43:31 -07001356 continue;
1357
Eric Anholta721abf2010-08-23 10:32:01 -07001358 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001359 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1360 if (prog->_LinkedShaders[j] == NULL)
1361 continue;
1362
Eric Anholta721abf2010-08-23 10:32:01 -07001363 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1364 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1365 if (!other_var)
1366 continue;
1367
1368 if (strcmp(var->name, other_var->name) == 0 &&
1369 other_var->max_array_access > size) {
1370 size = other_var->max_array_access;
1371 }
1372 }
1373 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001374
Fabian Bieler63684782013-06-14 13:37:07 +02001375 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001376 /* If this is a built-in uniform (i.e., it's backed by some
1377 * fixed-function state), adjust the number of state slots to
1378 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001379 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001380 * slots is an integer multiple of the number of array elements.
1381 * Determine the number of slots per array element by dividing by
1382 * the old (total) size.
1383 */
1384 if (var->num_state_slots > 0) {
1385 var->num_state_slots = (size + 1)
1386 * (var->num_state_slots / var->type->length);
1387 }
1388
Eric Anholta721abf2010-08-23 10:32:01 -07001389 var->type = glsl_type::get_array_instance(var->type->fields.array,
1390 size + 1);
1391 /* FINISHME: We should update the types of array
1392 * dereferences of this variable now.
1393 */
1394 }
1395 }
1396 }
1397}
1398
Ian Romanick69846702010-06-22 17:29:19 -07001399/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001400 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001401 *
1402 * \param used_mask Bits representing used (1) and unused (0) locations
1403 * \param needed_count Number of contiguous bits needed.
1404 *
1405 * \return
1406 * Base location of the available bits on success or -1 on failure.
1407 */
1408int
1409find_available_slots(unsigned used_mask, unsigned needed_count)
1410{
1411 unsigned needed_mask = (1 << needed_count) - 1;
1412 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1413
1414 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1415 * cannot optimize possibly infinite loops" for the loop below.
1416 */
1417 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1418 return -1;
1419
1420 for (int i = 0; i <= max_bit_to_test; i++) {
1421 if ((needed_mask & ~used_mask) == needed_mask)
1422 return i;
1423
1424 needed_mask <<= 1;
1425 }
1426
1427 return -1;
1428}
1429
1430
Ian Romanickd32d4f72011-06-27 17:59:58 -07001431/**
1432 * Assign locations for either VS inputs for FS outputs
1433 *
1434 * \param prog Shader program whose variables need locations assigned
1435 * \param target_index Selector for the program target to receive location
1436 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1437 * \c MESA_SHADER_FRAGMENT.
1438 * \param max_index Maximum number of generic locations. This corresponds
1439 * to either the maximum number of draw buffers or the
1440 * maximum number of generic attributes.
1441 *
1442 * \return
1443 * If locations are successfully assigned, true is returned. Otherwise an
1444 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001445 */
Ian Romanick69846702010-06-22 17:29:19 -07001446bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001447assign_attribute_or_color_locations(gl_shader_program *prog,
1448 unsigned target_index,
1449 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001450{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001451 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001452 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001453 unsigned used_locations = (max_index >= 32)
1454 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001455
Ian Romanickd32d4f72011-06-27 17:59:58 -07001456 assert((target_index == MESA_SHADER_VERTEX)
1457 || (target_index == MESA_SHADER_FRAGMENT));
1458
1459 gl_shader *const sh = prog->_LinkedShaders[target_index];
1460 if (sh == NULL)
1461 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001462
Ian Romanick69846702010-06-22 17:29:19 -07001463 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001464 *
1465 * 1. Invalidate the location assignments for all vertex shader inputs.
1466 *
1467 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001468 * glBindVertexAttribLocation) locations and outputs that have
1469 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001470 *
Ian Romanick69846702010-06-22 17:29:19 -07001471 * 3. Sort the attributes without assigned locations by number of slots
1472 * required in decreasing order. Fragmentation caused by attribute
1473 * locations assigned by the application may prevent large attributes
1474 * from having enough contiguous space.
1475 *
1476 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001477 */
1478
Ian Romanickd32d4f72011-06-27 17:59:58 -07001479 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001480 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001481
Ian Romanickd32d4f72011-06-27 17:59:58 -07001482 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001483 (target_index == MESA_SHADER_VERTEX)
1484 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001485
1486
Ian Romanick69846702010-06-22 17:29:19 -07001487 /* Temporary storage for the set of attributes that need locations assigned.
1488 */
1489 struct temp_attr {
1490 unsigned slots;
1491 ir_variable *var;
1492
1493 /* Used below in the call to qsort. */
1494 static int compare(const void *a, const void *b)
1495 {
1496 const temp_attr *const l = (const temp_attr *) a;
1497 const temp_attr *const r = (const temp_attr *) b;
1498
1499 /* Reversed because we want a descending order sort below. */
1500 return r->slots - l->slots;
1501 }
1502 } to_assign[16];
1503
1504 unsigned num_attr = 0;
1505
Eric Anholt16b68b12010-06-30 11:05:43 -07001506 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001507 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1508
Brian Paul4470ff22011-07-19 21:10:25 -06001509 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001510 continue;
1511
Ian Romanick68a4fc92010-10-07 17:21:22 -07001512 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001513 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001514 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001515 linker_error(prog,
1516 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001517 (var->location < 0)
1518 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001519 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001520 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001521 }
1522 } else if (target_index == MESA_SHADER_VERTEX) {
1523 unsigned binding;
1524
1525 if (prog->AttributeBindings->get(binding, var->name)) {
1526 assert(binding >= VERT_ATTRIB_GENERIC0);
1527 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001528 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001529 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001530 } else if (target_index == MESA_SHADER_FRAGMENT) {
1531 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001532 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001533
1534 if (prog->FragDataBindings->get(binding, var->name)) {
1535 assert(binding >= FRAG_RESULT_DATA0);
1536 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001537 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001538
1539 if (prog->FragDataIndexBindings->get(index, var->name)) {
1540 var->index = index;
1541 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001542 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001543 }
1544
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001545 /* If the variable is not a built-in and has a location statically
1546 * assigned in the shader (presumably via a layout qualifier), make sure
1547 * that it doesn't collide with other assigned locations. Otherwise,
1548 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001549 */
Paul Berry0026ad42013-07-31 08:15:08 -07001550 const unsigned slots = var->type->count_attribute_slots();
Ian Romanick523b6112011-08-17 15:40:03 -07001551 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001552 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001553 /* From page 61 of the OpenGL 4.0 spec:
1554 *
1555 * "LinkProgram will fail if the attribute bindings assigned
1556 * by BindAttribLocation do not leave not enough space to
1557 * assign a location for an active matrix attribute or an
1558 * active attribute array, both of which require multiple
1559 * contiguous generic attributes."
1560 *
1561 * Previous versions of the spec contain similar language but omit
1562 * the bit about attribute arrays.
1563 *
1564 * Page 61 of the OpenGL 4.0 spec also says:
1565 *
1566 * "It is possible for an application to bind more than one
1567 * attribute name to the same location. This is referred to as
1568 * aliasing. This will only work if only one of the aliased
1569 * attributes is active in the executable program, or if no
1570 * path through the shader consumes more than one attribute of
1571 * a set of attributes aliased to the same location. A link
1572 * error can occur if the linker determines that every path
1573 * through the shader consumes multiple aliased attributes,
1574 * but implementations are not required to generate an error
1575 * in this case."
1576 *
1577 * These two paragraphs are either somewhat contradictory, or I
1578 * don't fully understand one or both of them.
1579 */
1580 /* FINISHME: The code as currently written does not support
1581 * FINISHME: attribute location aliasing (see comment above).
1582 */
1583 /* Mask representing the contiguous slots that will be used by
1584 * this attribute.
1585 */
1586 const unsigned attr = var->location - generic_base;
1587 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001588
Ian Romanick523b6112011-08-17 15:40:03 -07001589 /* Generate a link error if the set of bits requested for this
1590 * attribute overlaps any previously allocated bits.
1591 */
1592 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001593 const char *const string = (target_index == MESA_SHADER_VERTEX)
1594 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001595 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001596 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001597 "available for %s `%s' %d %d %d", string,
1598 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001599 return false;
1600 }
1601
1602 used_locations |= (use_mask << attr);
1603 }
1604
1605 continue;
1606 }
1607
1608 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001609 to_assign[num_attr].var = var;
1610 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001611 }
Ian Romanick69846702010-06-22 17:29:19 -07001612
1613 /* If all of the attributes were assigned locations by the application (or
1614 * are built-in attributes with fixed locations), return early. This should
1615 * be the common case.
1616 */
1617 if (num_attr == 0)
1618 return true;
1619
1620 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1621
Ian Romanickd32d4f72011-06-27 17:59:58 -07001622 if (target_index == MESA_SHADER_VERTEX) {
1623 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1624 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1625 * reserved to prevent it from being automatically allocated below.
1626 */
1627 find_deref_visitor find("gl_Vertex");
1628 find.run(sh->ir);
1629 if (find.variable_found())
1630 used_locations |= (1 << 0);
1631 }
Ian Romanick982e3792010-06-29 18:58:20 -07001632
Ian Romanick69846702010-06-22 17:29:19 -07001633 for (unsigned i = 0; i < num_attr; i++) {
1634 /* Mask representing the contiguous slots that will be used by this
1635 * attribute.
1636 */
1637 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1638
1639 int location = find_available_slots(used_locations, to_assign[i].slots);
1640
1641 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001642 const char *const string = (target_index == MESA_SHADER_VERTEX)
1643 ? "vertex shader input" : "fragment shader output";
1644
Ian Romanick586e7412011-07-28 14:04:09 -07001645 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001646 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001647 "available for %s `%s'",
1648 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001649 return false;
1650 }
1651
Ian Romanickd32d4f72011-06-27 17:59:58 -07001652 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001653 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001654 used_locations |= (use_mask << location);
1655 }
1656
1657 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001658}
1659
1660
Ian Romanick40e114b2010-08-17 14:55:50 -07001661/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001662 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001663 */
1664void
Ian Romanickcc90e622010-10-19 17:59:10 -07001665demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001666{
1667 foreach_list(node, sh->ir) {
1668 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1669
Ian Romanickcc90e622010-10-19 17:59:10 -07001670 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001671 continue;
1672
Ian Romanickcc90e622010-10-19 17:59:10 -07001673 /* A shader 'in' or 'out' variable is only really an input or output if
1674 * its value is used by other shader stages. This will cause the variable
1675 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001676 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001677 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001678 var->mode = ir_var_auto;
1679 }
1680 }
1681}
1682
1683
Paul Berry871ddb92011-11-05 11:17:32 -07001684/**
Marek Olšákec174a42011-11-18 15:00:10 +01001685 * Store the gl_FragDepth layout in the gl_shader_program struct.
1686 */
1687static void
1688store_fragdepth_layout(struct gl_shader_program *prog)
1689{
1690 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1691 return;
1692 }
1693
1694 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1695
1696 /* We don't look up the gl_FragDepth symbol directly because if
1697 * gl_FragDepth is not used in the shader, it's removed from the IR.
1698 * However, the symbol won't be removed from the symbol table.
1699 *
1700 * We're only interested in the cases where the variable is NOT removed
1701 * from the IR.
1702 */
1703 foreach_list(node, ir) {
1704 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1705
Paul Berry42a29d82013-01-11 14:39:32 -08001706 if (var == NULL || var->mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001707 continue;
1708 }
1709
1710 if (strcmp(var->name, "gl_FragDepth") == 0) {
1711 switch (var->depth_layout) {
1712 case ir_depth_layout_none:
1713 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1714 return;
1715 case ir_depth_layout_any:
1716 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1717 return;
1718 case ir_depth_layout_greater:
1719 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1720 return;
1721 case ir_depth_layout_less:
1722 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1723 return;
1724 case ir_depth_layout_unchanged:
1725 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1726 return;
1727 default:
1728 assert(0);
1729 return;
1730 }
1731 }
1732 }
1733}
1734
1735/**
Ian Romanick92f81592011-11-08 12:37:19 -08001736 * Validate the resources used by a program versus the implementation limits
1737 */
Paul Berryb95d2372013-07-27 11:08:31 -07001738static void
Ian Romanick92f81592011-11-08 12:37:19 -08001739check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1740{
1741 static const char *const shader_names[MESA_SHADER_TYPES] = {
Marek Olšák030ca232013-06-12 17:15:46 +02001742 "vertex", "geometry", "fragment"
Ian Romanick92f81592011-11-08 12:37:19 -08001743 };
1744
1745 const unsigned max_samplers[MESA_SHADER_TYPES] = {
Marek Olšák5e784332013-05-02 02:30:44 +02001746 ctx->Const.VertexProgram.MaxTextureImageUnits,
Marek Olšák030ca232013-06-12 17:15:46 +02001747 ctx->Const.GeometryProgram.MaxTextureImageUnits,
1748 ctx->Const.FragmentProgram.MaxTextureImageUnits
Ian Romanick92f81592011-11-08 12:37:19 -08001749 };
1750
Eric Anholt38e77e52013-05-23 11:10:15 -07001751 const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = {
Ian Romanick92f81592011-11-08 12:37:19 -08001752 ctx->Const.VertexProgram.MaxUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001753 ctx->Const.GeometryProgram.MaxUniformComponents,
1754 ctx->Const.FragmentProgram.MaxUniformComponents
Ian Romanick92f81592011-11-08 12:37:19 -08001755 };
1756
Eric Anholt38e77e52013-05-23 11:10:15 -07001757 const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = {
1758 ctx->Const.VertexProgram.MaxCombinedUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001759 ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
1760 ctx->Const.FragmentProgram.MaxCombinedUniformComponents
Eric Anholt38e77e52013-05-23 11:10:15 -07001761 };
1762
Eric Anholt877a8972012-06-25 12:47:01 -07001763 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
1764 ctx->Const.VertexProgram.MaxUniformBlocks,
Eric Anholt877a8972012-06-25 12:47:01 -07001765 ctx->Const.GeometryProgram.MaxUniformBlocks,
Marek Olšák030ca232013-06-12 17:15:46 +02001766 ctx->Const.FragmentProgram.MaxUniformBlocks
Eric Anholt877a8972012-06-25 12:47:01 -07001767 };
1768
Ian Romanick92f81592011-11-08 12:37:19 -08001769 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1770 struct gl_shader *sh = prog->_LinkedShaders[i];
1771
1772 if (sh == NULL)
1773 continue;
1774
1775 if (sh->num_samplers > max_samplers[i]) {
1776 linker_error(prog, "Too many %s shader texture samplers",
1777 shader_names[i]);
1778 }
1779
Eric Anholt38e77e52013-05-23 11:10:15 -07001780 if (sh->num_uniform_components > max_default_uniform_components[i]) {
1781 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1782 linker_warning(prog, "Too many %s shader default uniform block "
1783 "components, but the driver will try to optimize "
1784 "them out; this is non-portable out-of-spec "
1785 "behavior\n",
1786 shader_names[i]);
1787 } else {
1788 linker_error(prog, "Too many %s shader default uniform block "
1789 "components",
1790 shader_names[i]);
1791 }
1792 }
1793
1794 if (sh->num_combined_uniform_components >
1795 max_combined_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001796 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1797 linker_warning(prog, "Too many %s shader uniform components, "
1798 "but the driver will try to optimize them out; "
1799 "this is non-portable out-of-spec behavior\n",
1800 shader_names[i]);
1801 } else {
1802 linker_error(prog, "Too many %s shader uniform components",
1803 shader_names[i]);
1804 }
Ian Romanick92f81592011-11-08 12:37:19 -08001805 }
1806 }
1807
Eric Anholt877a8972012-06-25 12:47:01 -07001808 unsigned blocks[MESA_SHADER_TYPES] = {0};
1809 unsigned total_uniform_blocks = 0;
1810
1811 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
1812 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1813 if (prog->UniformBlockStageIndex[j][i] != -1) {
1814 blocks[j]++;
1815 total_uniform_blocks++;
1816 }
1817 }
1818
1819 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1820 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1821 prog->NumUniformBlocks,
1822 ctx->Const.MaxCombinedUniformBlocks);
1823 } else {
1824 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1825 if (blocks[i] > max_uniform_blocks[i]) {
1826 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
1827 shader_names[i],
1828 blocks[i],
1829 max_uniform_blocks[i]);
1830 break;
1831 }
1832 }
1833 }
1834 }
Ian Romanick92f81592011-11-08 12:37:19 -08001835}
Paul Berry871ddb92011-11-05 11:17:32 -07001836
Ian Romanick0e59b262010-06-23 11:23:01 -07001837void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001838link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001839{
Paul Berry871ddb92011-11-05 11:17:32 -07001840 tfeedback_decl *tfeedback_decls = NULL;
1841 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1842
Kenneth Graunked3073f52011-01-21 14:32:31 -08001843 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001844
Paul Berryb95d2372013-07-27 11:08:31 -07001845 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07001846 prog->Validated = false;
1847 prog->_Used = false;
1848
Eric Anholtf609cf72012-04-27 13:52:56 -07001849 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08001850 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07001851
Eric Anholtf609cf72012-04-27 13:52:56 -07001852 ralloc_free(prog->UniformBlocks);
1853 prog->UniformBlocks = NULL;
1854 prog->NumUniformBlocks = 0;
1855 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
1856 ralloc_free(prog->UniformBlockStageIndex[i]);
1857 prog->UniformBlockStageIndex[i] = NULL;
1858 }
1859
Ian Romanick832dfa52010-06-17 15:04:20 -07001860 /* Separate the shaders into groups based on their type.
1861 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001862 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001863 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001864 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001865 unsigned num_frag_shaders = 0;
Bryan Cain25480922013-02-15 09:46:50 -06001866 struct gl_shader **geom_shader_list;
1867 unsigned num_geom_shaders = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07001868
Eric Anholt16b68b12010-06-30 11:05:43 -07001869 vert_shader_list = (struct gl_shader **)
Paul Berry844bd712013-07-30 22:38:43 -07001870 calloc(prog->NumShaders, sizeof(struct gl_shader *));
1871 frag_shader_list = (struct gl_shader **)
1872 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Bryan Cain25480922013-02-15 09:46:50 -06001873 geom_shader_list = (struct gl_shader **)
1874 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001875
Ian Romanick25f51d32010-07-16 15:51:50 -07001876 unsigned min_version = UINT_MAX;
1877 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07001878 const bool is_es_prog =
1879 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07001880 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001881 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1882 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1883
Paul Berrya9f34dc2012-08-02 17:49:44 -07001884 if (prog->Shaders[i]->IsES != is_es_prog) {
1885 linker_error(prog, "all shaders must use same shading "
1886 "language version\n");
1887 goto done;
1888 }
1889
Ian Romanick832dfa52010-06-17 15:04:20 -07001890 switch (prog->Shaders[i]->Type) {
1891 case GL_VERTEX_SHADER:
1892 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1893 num_vert_shaders++;
1894 break;
1895 case GL_FRAGMENT_SHADER:
1896 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1897 num_frag_shaders++;
1898 break;
1899 case GL_GEOMETRY_SHADER:
Bryan Cain25480922013-02-15 09:46:50 -06001900 geom_shader_list[num_geom_shaders] = prog->Shaders[i];
1901 num_geom_shaders++;
Ian Romanick832dfa52010-06-17 15:04:20 -07001902 break;
1903 }
1904 }
1905
Ian Romanick25f51d32010-07-16 15:51:50 -07001906 /* Previous to GLSL version 1.30, different compilation units could mix and
1907 * match shading language versions. With GLSL 1.30 and later, the versions
1908 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07001909 *
1910 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07001911 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07001912 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001913 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07001914 linker_error(prog, "all shaders must use same shading "
1915 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07001916 goto done;
1917 }
1918
1919 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07001920 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07001921
Fabian Bielerbd85ba02013-05-24 23:26:54 +02001922 /* Geometry shaders have to be linked with vertex shaders.
1923 */
1924 if (num_geom_shaders > 0 && num_vert_shaders == 0) {
1925 linker_error(prog, "Geometry shader must be linked with "
1926 "vertex shader\n");
1927 goto done;
1928 }
1929
Ian Romanick3322fba2010-10-14 13:28:42 -07001930 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1931 if (prog->_LinkedShaders[i] != NULL)
1932 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1933
1934 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07001935 }
1936
Ian Romanickcd6764e2010-07-16 16:00:07 -07001937 /* Link all shaders for a particular stage and validate the result.
1938 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001939 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001940 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001941 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1942 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001943
Paul Berryb95d2372013-07-27 11:08:31 -07001944 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07001945 goto done;
1946
Paul Berryb95d2372013-07-27 11:08:31 -07001947 validate_vertex_shader_executable(prog, sh);
1948 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001949 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001950
Ian Romanick3322fba2010-10-14 13:28:42 -07001951 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1952 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001953 }
1954
1955 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001956 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001957 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1958 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001959
Paul Berryb95d2372013-07-27 11:08:31 -07001960 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07001961 goto done;
1962
Paul Berryb95d2372013-07-27 11:08:31 -07001963 validate_fragment_shader_executable(prog, sh);
1964 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001965 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001966
Ian Romanick3322fba2010-10-14 13:28:42 -07001967 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1968 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001969 }
1970
Bryan Cain25480922013-02-15 09:46:50 -06001971 if (num_geom_shaders > 0) {
1972 gl_shader *const sh =
1973 link_intrastage_shaders(mem_ctx, ctx, prog, geom_shader_list,
1974 num_geom_shaders);
1975
1976 if (!prog->LinkStatus)
1977 goto done;
1978
1979 validate_geometry_shader_executable(prog, sh);
1980 if (!prog->LinkStatus)
1981 goto done;
1982
1983 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
1984 sh);
1985 }
1986
Ian Romanick3ed850e2010-06-23 12:18:21 -07001987 /* Here begins the inter-stage linking phase. Some initial validation is
1988 * performed, then locations are assigned for uniforms, attributes, and
1989 * varyings.
1990 */
Paul Berryb95d2372013-07-27 11:08:31 -07001991 cross_validate_uniforms(prog);
1992 if (!prog->LinkStatus)
1993 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001994
Paul Berryb95d2372013-07-27 11:08:31 -07001995 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07001996
Paul Berryb95d2372013-07-27 11:08:31 -07001997 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1998 if (prog->_LinkedShaders[prev] != NULL)
1999 break;
2000 }
Ian Romanick3322fba2010-10-14 13:28:42 -07002001
Paul Berryb95d2372013-07-27 11:08:31 -07002002 /* Validate the inputs of each stage with the output of the preceding
2003 * stage.
2004 */
2005 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2006 if (prog->_LinkedShaders[i] == NULL)
2007 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07002008
Paul Berryb95d2372013-07-27 11:08:31 -07002009 validate_interstage_interface_blocks(prog, prog->_LinkedShaders[prev],
2010 prog->_LinkedShaders[i]);
2011 if (!prog->LinkStatus)
2012 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002013
Paul Berryb95d2372013-07-27 11:08:31 -07002014 cross_validate_outputs_to_inputs(prog,
2015 prog->_LinkedShaders[prev],
2016 prog->_LinkedShaders[i]);
2017 if (!prog->LinkStatus)
2018 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07002019
Paul Berryb95d2372013-07-27 11:08:31 -07002020 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002021 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002022
Jordan Justen5ebf5472013-03-10 03:20:03 -07002023
2024 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2025 if (prog->_LinkedShaders[i] != NULL)
2026 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
2027 }
2028
Eric Anholt3de13952012-05-04 13:08:46 -07002029 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2030 * it before optimization because we want most of the checks to get
2031 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002032 *
2033 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002034 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002035 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002036 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2037 if (sh) {
2038 lower_discard_flow(sh->ir);
2039 }
2040 }
2041
Eric Anholtf609cf72012-04-27 13:52:56 -07002042 if (!interstage_cross_validate_uniform_blocks(prog))
2043 goto done;
2044
Eric Anholt2f4fe152010-08-10 13:06:49 -07002045 /* Do common optimization before assigning storage for attributes,
2046 * uniforms, and varyings. Later optimization could possibly make
2047 * some of that unused.
2048 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002049 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2050 if (prog->_LinkedShaders[i] == NULL)
2051 continue;
2052
Ian Romanick02c5ae12011-07-11 10:46:01 -07002053 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2054 if (!prog->LinkStatus)
2055 goto done;
2056
Paul Berry18392442012-12-04 11:11:02 -08002057 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2058 lower_clip_distance(prog->_LinkedShaders[i]);
2059 }
Paul Berryc06e3252011-08-11 20:58:21 -07002060
Brian Paul7feabfe2012-03-20 17:43:12 -06002061 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2062
Kenneth Graunkeb7657402013-04-17 17:30:22 -07002063 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002064 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002065 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002066
Paul Berry50895d42012-12-05 07:17:07 -08002067 /* Mark all generic shader inputs and outputs as unpaired. */
2068 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2069 link_invalidate_variable_locations(
2070 prog->_LinkedShaders[MESA_SHADER_VERTEX],
Paul Berry36b252e2013-02-23 07:22:01 -08002071 VERT_ATTRIB_GENERIC0, VARYING_SLOT_VAR0);
Paul Berry50895d42012-12-05 07:17:07 -08002072 }
Bryan Cain25480922013-02-15 09:46:50 -06002073 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2074 link_invalidate_variable_locations(
2075 prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
2076 VARYING_SLOT_VAR0, VARYING_SLOT_VAR0);
2077 }
Paul Berry50895d42012-12-05 07:17:07 -08002078 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2079 link_invalidate_variable_locations(
2080 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
Paul Berryeed6baf2013-02-23 09:00:58 -08002081 VARYING_SLOT_VAR0, FRAG_RESULT_DATA0);
Paul Berry50895d42012-12-05 07:17:07 -08002082 }
2083
Ian Romanickd32d4f72011-06-27 17:59:58 -07002084 /* FINISHME: The value of the max_attribute_index parameter is
2085 * FINISHME: implementation dependent based on the value of
2086 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2087 * FINISHME: at least 16, so hardcode 16 for now.
2088 */
2089 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002090 goto done;
2091 }
2092
Dave Airlie1256a5d2012-03-24 13:33:41 +00002093 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002094 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002095 }
2096
Marek Olšák284d9542013-06-12 02:18:09 +02002097 unsigned first;
2098 for (first = 0; first < MESA_SHADER_TYPES; first++) {
2099 if (prog->_LinkedShaders[first] != NULL)
Ian Romanick3322fba2010-10-14 13:28:42 -07002100 break;
2101 }
2102
Paul Berry871ddb92011-11-05 11:17:32 -07002103 if (num_tfeedback_decls != 0) {
2104 /* From GL_EXT_transform_feedback:
2105 * A program will fail to link if:
2106 *
2107 * * the <count> specified by TransformFeedbackVaryingsEXT is
2108 * non-zero, but the program object has no vertex or geometry
2109 * shader;
2110 */
Bryan Cain25480922013-02-15 09:46:50 -06002111 if (first == MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07002112 linker_error(prog, "Transform feedback varyings specified, but "
2113 "no vertex or geometry shader is present.");
2114 goto done;
2115 }
2116
2117 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2118 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002119 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002120 prog->TransformFeedback.VaryingNames,
2121 tfeedback_decls))
2122 goto done;
2123 }
2124
Marek Olšák284d9542013-06-12 02:18:09 +02002125 /* Linking the stages in the opposite order (from fragment to vertex)
2126 * ensures that inter-shader outputs written to in an earlier stage are
2127 * eliminated if they are (transitively) not used in a later stage.
2128 */
2129 int last, next;
2130 for (last = MESA_SHADER_TYPES-1; last >= 0; last--) {
2131 if (prog->_LinkedShaders[last] != NULL)
2132 break;
Ian Romanick3322fba2010-10-14 13:28:42 -07002133 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002134
Marek Olšák284d9542013-06-12 02:18:09 +02002135 if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
2136 gl_shader *const sh = prog->_LinkedShaders[last];
2137
2138 if (num_tfeedback_decls != 0) {
2139 /* There was no fragment shader, but we still have to assign varying
2140 * locations for use by transform feedback.
2141 */
2142 if (!assign_varying_locations(ctx, mem_ctx, prog,
2143 sh, NULL,
Paul Berry3b0cf702013-04-10 06:48:42 -07002144 num_tfeedback_decls, tfeedback_decls,
2145 0))
Marek Olšák284d9542013-06-12 02:18:09 +02002146 goto done;
2147 }
2148
Marek Olšákd13003f2013-08-09 22:34:45 +02002149 do_dead_builtin_varyings(ctx, sh, NULL,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002150 num_tfeedback_decls, tfeedback_decls);
2151
Marek Olšák284d9542013-06-12 02:18:09 +02002152 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
2153
2154 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07002155 */
Marek Olšák284d9542013-06-12 02:18:09 +02002156 while (do_dead_code(sh->ir, false))
2157 ;
2158 }
2159 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002160 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02002161 */
2162 gl_shader *const sh = prog->_LinkedShaders[first];
2163
Marek Olšákd13003f2013-08-09 22:34:45 +02002164 do_dead_builtin_varyings(ctx, NULL, sh,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002165 num_tfeedback_decls, tfeedback_decls);
2166
Marek Olšák284d9542013-06-12 02:18:09 +02002167 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
2168
2169 while (do_dead_code(sh->ir, false))
2170 ;
2171 }
2172
2173 next = last;
2174 for (int i = next - 1; i >= 0; i--) {
2175 if (prog->_LinkedShaders[i] == NULL)
2176 continue;
2177
2178 gl_shader *const sh_i = prog->_LinkedShaders[i];
2179 gl_shader *const sh_next = prog->_LinkedShaders[next];
Paul Berry3b0cf702013-04-10 06:48:42 -07002180 unsigned gs_input_vertices =
2181 next == MESA_SHADER_GEOMETRY ? prog->Geom.VerticesIn : 0;
Marek Olšák284d9542013-06-12 02:18:09 +02002182
2183 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
2184 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
Paul Berry3b0cf702013-04-10 06:48:42 -07002185 tfeedback_decls, gs_input_vertices))
Paul Berry871ddb92011-11-05 11:17:32 -07002186 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02002187
Marek Olšákd13003f2013-08-09 22:34:45 +02002188 do_dead_builtin_varyings(ctx, sh_i, sh_next,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002189 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2190 tfeedback_decls);
2191
Marek Olšák284d9542013-06-12 02:18:09 +02002192 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
2193 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
2194
2195 /* Eliminate code that is now dead due to unused outputs being demoted.
2196 */
2197 while (do_dead_code(sh_i->ir, false))
2198 ;
2199 while (do_dead_code(sh_next->ir, false))
2200 ;
2201
Marek Olšák3c555822013-06-13 03:17:22 +02002202 /* This must be done after all dead varyings are eliminated. */
2203 if (!check_against_varying_limit(ctx, prog, sh_next))
2204 goto done;
2205
Marek Olšák284d9542013-06-12 02:18:09 +02002206 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07002207 }
2208
2209 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2210 goto done;
2211
Ian Romanick960d7222011-10-21 11:21:02 -07002212 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002213 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002214 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002215
Paul Berryb95d2372013-07-27 11:08:31 -07002216 check_resources(ctx, prog);
2217 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08002218 goto done;
2219
Ian Romanickce9171f2011-02-03 17:10:14 -08002220 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002221 * present in a linked program. By checking prog->IsES, we also
2222 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002223 */
Eric Anholt57f79782011-07-22 12:57:47 -07002224 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002225 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002226 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002227 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002228 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002229 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002230 }
2231 }
2232
Ian Romanick13e10e42010-06-21 12:03:24 -07002233 /* FINISHME: Assign fragment shader output locations. */
2234
Ian Romanick832dfa52010-06-17 15:04:20 -07002235done:
2236 free(vert_shader_list);
Paul Berry844bd712013-07-30 22:38:43 -07002237 free(frag_shader_list);
Bryan Cain25480922013-02-15 09:46:50 -06002238 free(geom_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002239
2240 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2241 if (prog->_LinkedShaders[i] == NULL)
2242 continue;
2243
2244 /* Retain any live IR, but trash the rest. */
2245 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002246
2247 /* The symbol table in the linked shaders may contain references to
2248 * variables that were removed (e.g., unused uniforms). Since it may
2249 * contain junk, there is no possible valid use. Delete it and set the
2250 * pointer to NULL.
2251 */
2252 delete prog->_LinkedShaders[i]->symbols;
2253 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002254 }
2255
Kenneth Graunked3073f52011-01-21 14:32:31 -08002256 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002257}