blob: 942f9061596cce44092113d5ca9dd7953d8e9960 [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"
Ian Romanick832dfa52010-06-17 15:04:20 -070076
Ian Romanick3322fba2010-10-14 13:28:42 -070077extern "C" {
78#include "main/shaderobj.h"
79}
80
Ian Romanick832dfa52010-06-17 15:04:20 -070081/**
82 * Visitor that determines whether or not a variable is ever written.
83 */
84class find_assignment_visitor : public ir_hierarchical_visitor {
85public:
86 find_assignment_visitor(const char *name)
87 : name(name), found(false)
88 {
89 /* empty */
90 }
91
92 virtual ir_visitor_status visit_enter(ir_assignment *ir)
93 {
94 ir_variable *const var = ir->lhs->variable_referenced();
95
96 if (strcmp(name, var->name) == 0) {
97 found = true;
98 return visit_stop;
99 }
100
101 return visit_continue_with_parent;
102 }
103
Eric Anholt18a60232010-08-23 11:29:25 -0700104 virtual ir_visitor_status visit_enter(ir_call *ir)
105 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700106 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700107 foreach_iter(exec_list_iterator, iter, *ir) {
108 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
109 ir_variable *sig_param = (ir_variable *)sig_iter.get();
110
Paul Berry42a29d82013-01-11 14:39:32 -0800111 if (sig_param->mode == ir_var_function_out ||
112 sig_param->mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700113 ir_variable *var = param_rval->variable_referenced();
114 if (var && strcmp(name, var->name) == 0) {
115 found = true;
116 return visit_stop;
117 }
118 }
119 sig_iter.next();
120 }
121
Kenneth Graunked884f602012-03-20 15:56:37 -0700122 if (ir->return_deref != NULL) {
123 ir_variable *const var = ir->return_deref->variable_referenced();
124
125 if (strcmp(name, var->name) == 0) {
126 found = true;
127 return visit_stop;
128 }
129 }
130
Eric Anholt18a60232010-08-23 11:29:25 -0700131 return visit_continue_with_parent;
132 }
133
Ian Romanick832dfa52010-06-17 15:04:20 -0700134 bool variable_found()
135 {
136 return found;
137 }
138
139private:
140 const char *name; /**< Find writes to a variable with this name. */
141 bool found; /**< Was a write to the variable found? */
142};
143
Ian Romanickc93b8f12010-06-17 15:20:22 -0700144
Ian Romanickc33e78f2010-08-13 12:30:41 -0700145/**
146 * Visitor that determines whether or not a variable is ever read.
147 */
148class find_deref_visitor : public ir_hierarchical_visitor {
149public:
150 find_deref_visitor(const char *name)
151 : name(name), found(false)
152 {
153 /* empty */
154 }
155
156 virtual ir_visitor_status visit(ir_dereference_variable *ir)
157 {
158 if (strcmp(this->name, ir->var->name) == 0) {
159 this->found = true;
160 return visit_stop;
161 }
162
163 return visit_continue;
164 }
165
166 bool variable_found() const
167 {
168 return this->found;
169 }
170
171private:
172 const char *name; /**< Find writes to a variable with this name. */
173 bool found; /**< Was a write to the variable found? */
174};
175
176
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700177void
Ian Romanick586e7412011-07-28 14:04:09 -0700178linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700179{
180 va_list ap;
181
Kenneth Graunked3073f52011-01-21 14:32:31 -0800182 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700183 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800184 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700185 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700186
187 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700188}
189
190
191void
Ian Romanick379a32f2011-07-28 14:09:06 -0700192linker_warning(gl_shader_program *prog, const char *fmt, ...)
193{
194 va_list ap;
195
196 ralloc_strcat(&prog->InfoLog, "error: ");
197 va_start(ap, fmt);
198 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
199 va_end(ap);
200
201}
202
203
Paul Berryb92900d2013-01-28 14:21:59 -0800204/**
205 * Given a string identifying a program resource, break it into a base name
206 * and an optional array index in square brackets.
207 *
208 * If an array index is present, \c out_base_name_end is set to point to the
209 * "[" that precedes the array index, and the array index itself is returned
210 * as a long.
211 *
212 * If no array index is present (or if the array index is negative or
213 * mal-formed), \c out_base_name_end, is set to point to the null terminator
214 * at the end of the input string, and -1 is returned.
215 *
216 * Only the final array index is parsed; if the string contains other array
217 * indices (or structure field accesses), they are left in the base name.
218 *
219 * No attempt is made to check that the base name is properly formed;
220 * typically the caller will look up the base name in a hash table, so
221 * ill-formed base names simply turn into hash table lookup failures.
222 */
223long
224parse_program_resource_name(const GLchar *name,
225 const GLchar **out_base_name_end)
226{
227 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
228 *
229 * "When an integer array element or block instance number is part of
230 * the name string, it will be specified in decimal form without a "+"
231 * or "-" sign or any extra leading zeroes. Additionally, the name
232 * string will not include white space anywhere in the string."
233 */
234
235 const size_t len = strlen(name);
236 *out_base_name_end = name + len;
237
238 if (len == 0 || name[len-1] != ']')
239 return -1;
240
241 /* Walk backwards over the string looking for a non-digit character. This
242 * had better be the opening bracket for an array index.
243 *
244 * Initially, i specifies the location of the ']'. Since the string may
245 * contain only the ']' charcater, walk backwards very carefully.
246 */
247 unsigned i;
248 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
249 /* empty */ ;
250
251 if ((i == 0) || name[i-1] != '[')
252 return -1;
253
254 long array_index = strtol(&name[i], NULL, 10);
255 if (array_index < 0)
256 return -1;
257
258 *out_base_name_end = name + (i - 1);
259 return array_index;
260}
261
262
Ian Romanick379a32f2011-07-28 14:09:06 -0700263void
Paul Berry50895d42012-12-05 07:17:07 -0800264link_invalidate_variable_locations(gl_shader *sh, int input_base,
265 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700266{
Eric Anholt16b68b12010-06-30 11:05:43 -0700267 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700268 ir_variable *const var = ((ir_instruction *) node)->as_variable();
269
Paul Berry50895d42012-12-05 07:17:07 -0800270 if (var == NULL)
271 continue;
272
273 int base;
274 switch (var->mode) {
Paul Berry42a29d82013-01-11 14:39:32 -0800275 case ir_var_shader_in:
Paul Berry50895d42012-12-05 07:17:07 -0800276 base = input_base;
277 break;
Paul Berry42a29d82013-01-11 14:39:32 -0800278 case ir_var_shader_out:
Paul Berry50895d42012-12-05 07:17:07 -0800279 base = output_base;
280 break;
281 default:
282 continue;
283 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700284
285 /* Only assign locations for generic attributes / varyings / etc.
286 */
Paul Berry50895d42012-12-05 07:17:07 -0800287 if ((var->location >= base) && !var->explicit_location)
288 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800289
Paul Berry3e81c662012-12-05 10:47:55 -0800290 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800291 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800292 var->location_frac = 0;
293 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800294 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800295 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700296 }
297}
298
299
Ian Romanickc93b8f12010-06-17 15:20:22 -0700300/**
Ian Romanick69846702010-06-22 17:29:19 -0700301 * Determine the number of attribute slots required for a particular type
302 *
303 * This code is here because it implements the language rules of a specific
304 * GLSL version. Since it's a property of the language and not a property of
305 * types in general, it doesn't really belong in glsl_type.
306 */
307unsigned
308count_attribute_slots(const glsl_type *t)
309{
310 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
311 *
312 * "A scalar input counts the same amount against this limit as a vec4,
313 * so applications may want to consider packing groups of four
314 * unrelated float inputs together into a vector to better utilize the
315 * capabilities of the underlying hardware. A matrix input will use up
316 * multiple locations. The number of locations used will equal the
317 * number of columns in the matrix."
318 *
319 * The spec does not explicitly say how arrays are counted. However, it
320 * should be safe to assume the total number of slots consumed by an array
321 * is the number of entries in the array multiplied by the number of slots
322 * consumed by a single element of the array.
323 */
324
325 if (t->is_array())
326 return t->array_size() * count_attribute_slots(t->element_type());
327
328 if (t->is_matrix())
329 return t->matrix_columns;
330
331 return 1;
332}
333
334
335/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700336 * Verify that a vertex shader executable meets all semantic requirements.
337 *
Paul Berry642e5b412012-01-04 13:57:52 -0800338 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
339 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700340 *
341 * \param shader Vertex shader executable to be verified
342 */
Paul Berryb95d2372013-07-27 11:08:31 -0700343void
Eric Anholt849e1812010-06-30 11:49:17 -0700344validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700345 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700346{
347 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700348 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700349
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700350 /* From the GLSL 1.10 spec, page 48:
351 *
352 * "The variable gl_Position is available only in the vertex
353 * language and is intended for writing the homogeneous vertex
354 * position. All executions of a well-formed vertex shader
355 * executable must write a value into this variable. [...] The
356 * variable gl_Position is available only in the vertex
357 * language and is intended for writing the homogeneous vertex
358 * position. All executions of a well-formed vertex shader
359 * executable must write a value into this variable."
360 *
361 * while in GLSL 1.40 this text is changed to:
362 *
363 * "The variable gl_Position is available only in the vertex
364 * language and is intended for writing the homogeneous vertex
365 * position. It can be written at any time during shader
366 * execution. It may also be read back by a vertex shader
367 * after being written. This value will be used by primitive
368 * assembly, clipping, culling, and other fixed functionality
369 * operations, if present, that operate on primitives after
370 * vertex processing has occurred. Its value is undefined if
371 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700372 *
373 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
374 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700375 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700376 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700377 find_assignment_visitor find("gl_Position");
378 find.run(shader->ir);
379 if (!find.variable_found()) {
380 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700381 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700382 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700383 }
384
Paul Berry642e5b412012-01-04 13:57:52 -0800385 prog->Vert.ClipDistanceArraySize = 0;
386
Paul Berry15ba2a52012-08-02 17:51:02 -0700387 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700388 /* From section 7.1 (Vertex Shader Special Variables) of the
389 * GLSL 1.30 spec:
390 *
391 * "It is an error for a shader to statically write both
392 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700393 *
394 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
395 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700396 */
397 find_assignment_visitor clip_vertex("gl_ClipVertex");
398 find_assignment_visitor clip_distance("gl_ClipDistance");
399
400 clip_vertex.run(shader->ir);
401 clip_distance.run(shader->ir);
402 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
403 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
404 "and `gl_ClipDistance'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700405 return;
Paul Berryb453ba22011-08-11 18:10:22 -0700406 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700407 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800408 ir_variable *clip_distance_var =
409 shader->symbols->get_variable("gl_ClipDistance");
410 if (clip_distance_var)
411 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700412 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700413}
414
415
Ian Romanickc93b8f12010-06-17 15:20:22 -0700416/**
417 * Verify that a fragment shader executable meets all semantic requirements
418 *
419 * \param shader Fragment shader executable to be verified
420 */
Paul Berryb95d2372013-07-27 11:08:31 -0700421void
Eric Anholt849e1812010-06-30 11:49:17 -0700422validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700423 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700424{
425 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700426 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700427
Ian Romanick832dfa52010-06-17 15:04:20 -0700428 find_assignment_visitor frag_color("gl_FragColor");
429 find_assignment_visitor frag_data("gl_FragData");
430
Eric Anholt16b68b12010-06-30 11:05:43 -0700431 frag_color.run(shader->ir);
432 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700433
Ian Romanick832dfa52010-06-17 15:04:20 -0700434 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700435 linker_error(prog, "fragment shader writes to both "
436 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700437 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700438}
439
440
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700441/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700442 * Generate a string describing the mode of a variable
443 */
444static const char *
445mode_string(const ir_variable *var)
446{
447 switch (var->mode) {
448 case ir_var_auto:
449 return (var->read_only) ? "global constant" : "global variable";
450
Paul Berry42a29d82013-01-11 14:39:32 -0800451 case ir_var_uniform: return "uniform";
452 case ir_var_shader_in: return "shader input";
453 case ir_var_shader_out: return "shader output";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700454
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800455 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700456 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700457 default:
458 assert(!"Should not get here.");
459 return "invalid variable";
460 }
461}
462
463
464/**
465 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700466 */
Paul Berryb95d2372013-07-27 11:08:31 -0700467void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700468cross_validate_globals(struct gl_shader_program *prog,
469 struct gl_shader **shader_list,
470 unsigned num_shaders,
471 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700472{
473 /* Examine all of the uniforms in all of the shaders and cross validate
474 * them.
475 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700476 glsl_symbol_table variables;
477 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700478 if (shader_list[i] == NULL)
479 continue;
480
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700481 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700482 ir_variable *const var = ((ir_instruction *) node)->as_variable();
483
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700484 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700485 continue;
486
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700487 if (uniforms_only && (var->mode != ir_var_uniform))
488 continue;
489
Ian Romanick7e2aa912010-07-19 17:12:42 -0700490 /* Don't cross validate temporaries that are at global scope. These
491 * will eventually get pulled into the shaders 'main'.
492 */
493 if (var->mode == ir_var_temporary)
494 continue;
495
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700496 /* If a global with this name has already been seen, verify that the
497 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700498 * initializers, the values of the initializers must be the same.
499 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700500 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700501 if (existing != NULL) {
502 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700503 /* Consider the types to be "the same" if both types are arrays
504 * of the same type and one of the arrays is implicitly sized.
505 * In addition, set the type of the linked variable to the
506 * explicitly sized array.
507 */
508 if (var->type->is_array()
509 && existing->type->is_array()
510 && (var->type->fields.array == existing->type->fields.array)
511 && ((var->type->length == 0)
512 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800513 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700514 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800515 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700516 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700517 linker_error(prog, "%s `%s' declared as type "
518 "`%s' and type `%s'\n",
519 mode_string(var),
520 var->name, var->type->name,
521 existing->type->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700522 return;
Ian Romanicka2711d62010-08-29 22:07:49 -0700523 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700524 }
525
Ian Romanick68a4fc92010-10-07 17:21:22 -0700526 if (var->explicit_location) {
527 if (existing->explicit_location
528 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700529 linker_error(prog, "explicit locations for %s "
530 "`%s' have differing values\n",
531 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700532 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700533 }
534
535 existing->location = var->location;
536 existing->explicit_location = true;
537 }
538
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700539 /* From the GLSL 4.20 specification:
540 * "A link error will result if two compilation units in a program
541 * specify different integer-constant bindings for the same
542 * opaque-uniform name. However, it is not an error to specify a
543 * binding on some but not all declarations for the same name"
544 */
545 if (var->explicit_binding) {
546 if (existing->explicit_binding &&
547 var->binding != existing->binding) {
548 linker_error(prog, "explicit bindings for %s "
549 "`%s' have differing values\n",
550 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700551 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700552 }
553
554 existing->binding = var->binding;
555 existing->explicit_binding = true;
556 }
557
Ian Romanick46173f92011-10-31 13:07:06 -0700558 /* Validate layout qualifiers for gl_FragDepth.
559 *
560 * From the AMD/ARB_conservative_depth specs:
561 *
562 * "If gl_FragDepth is redeclared in any fragment shader in a
563 * program, it must be redeclared in all fragment shaders in
564 * that program that have static assignments to
565 * gl_FragDepth. All redeclarations of gl_FragDepth in all
566 * fragment shaders in a single program must have the same set
567 * of qualifiers."
568 */
569 if (strcmp(var->name, "gl_FragDepth") == 0) {
570 bool layout_declared = var->depth_layout != ir_depth_layout_none;
571 bool layout_differs =
572 var->depth_layout != existing->depth_layout;
573
574 if (layout_declared && layout_differs) {
575 linker_error(prog,
576 "All redeclarations of gl_FragDepth in all "
577 "fragment shaders in a single program must have "
578 "the same set of qualifiers.");
579 }
580
581 if (var->used && layout_differs) {
582 linker_error(prog,
583 "If gl_FragDepth is redeclared with a layout "
584 "qualifier in any fragment shader, it must be "
585 "redeclared with the same layout qualifier in "
586 "all fragment shaders that have assignments to "
587 "gl_FragDepth");
588 }
589 }
Chad Versaceaddae332011-01-27 01:40:31 -0800590
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700591 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
592 *
593 * "If a shared global has multiple initializers, the
594 * initializers must all be constant expressions, and they
595 * must all have the same value. Otherwise, a link error will
596 * result. (A shared global having only one initializer does
597 * not require that initializer to be a constant expression.)"
598 *
599 * Previous to 4.20 the GLSL spec simply said that initializers
600 * must have the same value. In this case of non-constant
601 * initializers, this was impossible to determine. As a result,
602 * no vendor actually implemented that behavior. The 4.20
603 * behavior matches the implemented behavior of at least one other
604 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700605 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700606 if (var->constant_initializer != NULL) {
607 if (existing->constant_initializer != NULL) {
608 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700609 linker_error(prog, "initializers for %s "
610 "`%s' have differing values\n",
611 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700612 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700613 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700614 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700615 /* If the first-seen instance of a particular uniform did not
616 * have an initializer but a later instance does, copy the
617 * initializer to the version stored in the symbol table.
618 */
Ian Romanickde415b72010-07-14 13:22:12 -0700619 /* FINISHME: This is wrong. The constant_value field should
620 * FINISHME: not be modified! Imagine a case where a shader
621 * FINISHME: without an initializer is linked in two different
622 * FINISHME: programs with shaders that have differing
623 * FINISHME: initializers. Linking with the first will
624 * FINISHME: modify the shader, and linking with the second
625 * FINISHME: will fail.
626 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700627 existing->constant_initializer =
628 var->constant_initializer->clone(ralloc_parent(existing),
629 NULL);
630 }
631 }
632
633 if (var->has_initializer) {
634 if (existing->has_initializer
635 && (var->constant_initializer == NULL
636 || existing->constant_initializer == NULL)) {
637 linker_error(prog,
638 "shared global variable `%s' has multiple "
639 "non-constant initializers.\n",
640 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700641 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700642 }
643
644 /* Some instance had an initializer, so keep track of that. In
645 * this location, all sorts of initializers (constant or
646 * otherwise) will propagate the existence to the variable
647 * stored in the symbol table.
648 */
649 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700650 }
Chad Versace7528f142010-11-17 14:34:38 -0800651
652 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700653 linker_error(prog, "declarations for %s `%s' have "
654 "mismatching invariant qualifiers\n",
655 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700656 return;
Chad Versace7528f142010-11-17 14:34:38 -0800657 }
Chad Versace61428dd2011-01-10 15:29:30 -0800658 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700659 linker_error(prog, "declarations for %s `%s' have "
660 "mismatching centroid qualifiers\n",
661 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700662 return;
Chad Versace61428dd2011-01-10 15:29:30 -0800663 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700664 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700665 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700666 }
667 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700668}
669
670
Ian Romanick37101922010-06-18 19:02:10 -0700671/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700672 * Perform validation of uniforms used across multiple shader stages
673 */
Paul Berryb95d2372013-07-27 11:08:31 -0700674void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700675cross_validate_uniforms(struct gl_shader_program *prog)
676{
Paul Berryb95d2372013-07-27 11:08:31 -0700677 cross_validate_globals(prog, prog->_LinkedShaders,
678 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700679}
680
Eric Anholtf609cf72012-04-27 13:52:56 -0700681/**
682 * Accumulates the array of prog->UniformBlocks and checks that all
683 * definitons of blocks agree on their contents.
684 */
685static bool
686interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
687{
688 unsigned max_num_uniform_blocks = 0;
689 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
690 if (prog->_LinkedShaders[i])
691 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
692 }
693
694 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
695 struct gl_shader *sh = prog->_LinkedShaders[i];
696
697 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
698 max_num_uniform_blocks);
699 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
700 prog->UniformBlockStageIndex[i][j] = -1;
701
702 if (sh == NULL)
703 continue;
704
705 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
706 int index = link_cross_validate_uniform_block(prog,
707 &prog->UniformBlocks,
708 &prog->NumUniformBlocks,
709 &sh->UniformBlocks[j]);
710
711 if (index == -1) {
712 linker_error(prog, "uniform block `%s' has mismatching definitions",
713 sh->UniformBlocks[j].Name);
714 return false;
715 }
716
717 prog->UniformBlockStageIndex[i][index] = j;
718 }
719 }
720
721 return true;
722}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700723
Ian Romanick37101922010-06-18 19:02:10 -0700724
Ian Romanick3fb87872010-07-09 14:09:34 -0700725/**
726 * Populates a shaders symbol table with all global declarations
727 */
728static void
729populate_symbol_table(gl_shader *sh)
730{
731 sh->symbols = new(sh) glsl_symbol_table;
732
733 foreach_list(node, sh->ir) {
734 ir_instruction *const inst = (ir_instruction *) node;
735 ir_variable *var;
736 ir_function *func;
737
738 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700739 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700740 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700741 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700742 }
743 }
744}
745
746
747/**
Ian Romanick31a97862010-07-12 18:48:50 -0700748 * Remap variables referenced in an instruction tree
749 *
750 * This is used when instruction trees are cloned from one shader and placed in
751 * another. These trees will contain references to \c ir_variable nodes that
752 * do not exist in the target shader. This function finds these \c ir_variable
753 * references and replaces the references with matching variables in the target
754 * shader.
755 *
756 * If there is no matching variable in the target shader, a clone of the
757 * \c ir_variable is made and added to the target shader. The new variable is
758 * added to \b both the instruction stream and the symbol table.
759 *
760 * \param inst IR tree that is to be processed.
761 * \param symbols Symbol table containing global scope symbols in the
762 * linked shader.
763 * \param instructions Instruction stream where new variable declarations
764 * should be added.
765 */
766void
Eric Anholt8273bd42010-08-04 12:34:56 -0700767remap_variables(ir_instruction *inst, struct gl_shader *target,
768 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700769{
770 class remap_visitor : public ir_hierarchical_visitor {
771 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700772 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700773 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700774 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700775 this->target = target;
776 this->symbols = target->symbols;
777 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700778 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700779 }
780
781 virtual ir_visitor_status visit(ir_dereference_variable *ir)
782 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700783 if (ir->var->mode == ir_var_temporary) {
784 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
785
786 assert(var != NULL);
787 ir->var = var;
788 return visit_continue;
789 }
790
Ian Romanick31a97862010-07-12 18:48:50 -0700791 ir_variable *const existing =
792 this->symbols->get_variable(ir->var->name);
793 if (existing != NULL)
794 ir->var = existing;
795 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700796 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700797
Eric Anholt001eee52010-11-05 06:11:24 -0700798 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700799 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700800 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700801 }
802
803 return visit_continue;
804 }
805
806 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700807 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700808 glsl_symbol_table *symbols;
809 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700810 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700811 };
812
Eric Anholt8273bd42010-08-04 12:34:56 -0700813 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700814
815 inst->accept(&v);
816}
817
818
819/**
820 * Move non-declarations from one instruction stream to another
821 *
822 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700823 * 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 -0700824 * pointer) for \c last and \c false for \c make_copies on the first
825 * call. Successive calls pass the return value of the previous call for
826 * \c last and \c true for \c make_copies.
827 *
828 * \param instructions Source instruction stream
829 * \param last Instruction after which new instructions should be
830 * inserted in the target instruction stream
831 * \param make_copies Flag selecting whether instructions in \c instructions
832 * should be copied (via \c ir_instruction::clone) into the
833 * target list or moved.
834 *
835 * \return
836 * The new "last" instruction in the target instruction stream. This pointer
837 * is suitable for use as the \c last parameter of a later call to this
838 * function.
839 */
840exec_node *
841move_non_declarations(exec_list *instructions, exec_node *last,
842 bool make_copies, gl_shader *target)
843{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700844 hash_table *temps = NULL;
845
846 if (make_copies)
847 temps = hash_table_ctor(0, hash_table_pointer_hash,
848 hash_table_pointer_compare);
849
Ian Romanick303c99f2010-07-19 12:34:56 -0700850 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700851 ir_instruction *inst = (ir_instruction *) node;
852
Ian Romanick7e2aa912010-07-19 17:12:42 -0700853 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700854 continue;
855
Ian Romanick7e2aa912010-07-19 17:12:42 -0700856 ir_variable *var = inst->as_variable();
857 if ((var != NULL) && (var->mode != ir_var_temporary))
858 continue;
859
860 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700861 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700862 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700863 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700864
865 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700866 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700867
868 if (var != NULL)
869 hash_table_insert(temps, inst, var);
870 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700871 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700872 } else {
873 inst->remove();
874 }
875
876 last->insert_after(inst);
877 last = inst;
878 }
879
Ian Romanick7e2aa912010-07-19 17:12:42 -0700880 if (make_copies)
881 hash_table_dtor(temps);
882
Ian Romanick31a97862010-07-12 18:48:50 -0700883 return last;
884}
885
886/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700887 * Get the function signature for main from a shader
888 */
889static ir_function_signature *
890get_main_function_signature(gl_shader *sh)
891{
892 ir_function *const f = sh->symbols->get_function("main");
893 if (f != NULL) {
894 exec_list void_parameters;
895
896 /* Look for the 'void main()' signature and ensure that it's defined.
897 * This keeps the linker from accidentally pick a shader that just
898 * contains a prototype for main.
899 *
900 * We don't have to check for multiple definitions of main (in multiple
901 * shaders) because that would have already been caught above.
902 */
903 ir_function_signature *sig = f->matching_signature(&void_parameters);
904 if ((sig != NULL) && sig->is_defined) {
905 return sig;
906 }
907 }
908
909 return NULL;
910}
911
912
913/**
Brian Paul84a12732012-02-02 20:10:40 -0700914 * This class is only used in link_intrastage_shaders() below but declaring
915 * it inside that function leads to compiler warnings with some versions of
916 * gcc.
917 */
918class array_sizing_visitor : public ir_hierarchical_visitor {
919public:
920 virtual ir_visitor_status visit(ir_variable *var)
921 {
922 if (var->type->is_array() && (var->type->length == 0)) {
923 const glsl_type *type =
924 glsl_type::get_array_instance(var->type->fields.array,
925 var->max_array_access + 1);
926 assert(type != NULL);
927 var->type = type;
928 }
929 return visit_continue;
930 }
931};
932
Brian Paul84a12732012-02-02 20:10:40 -0700933/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700934 * Combine a group of shaders for a single stage to generate a linked shader
935 *
936 * \note
937 * If this function is supplied a single shader, it is cloned, and the new
938 * shader is returned.
939 */
940static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800941link_intrastage_shaders(void *mem_ctx,
942 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700943 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700944 struct gl_shader **shader_list,
945 unsigned num_shaders)
946{
Eric Anholtf609cf72012-04-27 13:52:56 -0700947 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -0700948
Ian Romanick13f782c2010-06-29 18:53:38 -0700949 /* Check that global variables defined in multiple shaders are consistent.
950 */
Paul Berryb95d2372013-07-27 11:08:31 -0700951 cross_validate_globals(prog, shader_list, num_shaders, false);
952 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -0700953 return NULL;
954
Jordan Justen4a0bcd92013-05-20 23:42:49 -0700955 /* Check that interface blocks defined in multiple shaders are consistent.
956 */
Paul Berryb95d2372013-07-27 11:08:31 -0700957 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
958 num_shaders);
959 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -0700960 return NULL;
961
Paul Berry4682b9b2013-07-27 15:07:08 -0700962 /* Link up uniform blocks defined within this stage. */
963 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -0500964 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
965 &uniform_blocks);
Eric Anholtf609cf72012-04-27 13:52:56 -0700966
Ian Romanick13f782c2010-06-29 18:53:38 -0700967 /* Check that there is only a single definition of each function signature
968 * across all shaders.
969 */
970 for (unsigned i = 0; i < (num_shaders - 1); i++) {
971 foreach_list(node, shader_list[i]->ir) {
972 ir_function *const f = ((ir_instruction *) node)->as_function();
973
974 if (f == NULL)
975 continue;
976
977 for (unsigned j = i + 1; j < num_shaders; j++) {
978 ir_function *const other =
979 shader_list[j]->symbols->get_function(f->name);
980
981 /* If the other shader has no function (and therefore no function
982 * signatures) with the same name, skip to the next shader.
983 */
984 if (other == NULL)
985 continue;
986
987 foreach_iter (exec_list_iterator, iter, *f) {
988 ir_function_signature *sig =
989 (ir_function_signature *) iter.get();
990
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700991 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -0700992 continue;
993
994 ir_function_signature *other_sig =
995 other->exact_matching_signature(& sig->parameters);
996
997 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -0700998 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -0700999 linker_error(prog, "function `%s' is multiply defined",
1000 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001001 return NULL;
1002 }
1003 }
1004 }
1005 }
1006 }
1007
1008 /* Find the shader that defines main, and make a clone of it.
1009 *
1010 * Starting with the clone, search for undefined references. If one is
1011 * found, find the shader that defines it. Clone the reference and add
1012 * it to the shader. Repeat until there are no undefined references or
1013 * until a reference cannot be resolved.
1014 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001015 gl_shader *main = NULL;
1016 for (unsigned i = 0; i < num_shaders; i++) {
1017 if (get_main_function_signature(shader_list[i]) != NULL) {
1018 main = shader_list[i];
1019 break;
1020 }
1021 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001022
Ian Romanick15ce87e2010-07-09 15:28:22 -07001023 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001024 linker_error(prog, "%s shader lacks `main'\n",
Eric Anholtfaf3dba2013-06-12 16:57:11 -07001025 _mesa_glsl_shader_target_name(shader_list[0]->Type));
Ian Romanick15ce87e2010-07-09 15:28:22 -07001026 return NULL;
1027 }
1028
Ian Romanick4a455952010-10-13 15:13:02 -07001029 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001030 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001031 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001032
Eric Anholtf609cf72012-04-27 13:52:56 -07001033 linked->UniformBlocks = uniform_blocks;
1034 linked->NumUniformBlocks = num_uniform_blocks;
1035 ralloc_steal(linked, linked->UniformBlocks);
1036
Ian Romanick15ce87e2010-07-09 15:28:22 -07001037 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001038
Ian Romanick31a97862010-07-12 18:48:50 -07001039 /* The a pointer to the main function in the final linked shader (i.e., the
1040 * copy of the original shader that contained the main function).
1041 */
1042 ir_function_signature *const main_sig = get_main_function_signature(linked);
1043
1044 /* Move any instructions other than variable declarations or function
1045 * declarations into main.
1046 */
Ian Romanick9303e352010-07-19 12:33:54 -07001047 exec_node *insertion_point =
1048 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1049 linked);
1050
Ian Romanick31a97862010-07-12 18:48:50 -07001051 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001052 if (shader_list[i] == main)
1053 continue;
1054
Ian Romanick31a97862010-07-12 18:48:50 -07001055 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001056 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001057 }
1058
Ian Romanick13f782c2010-06-29 18:53:38 -07001059 /* Resolve initializers for global variables in the linked shader.
1060 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001061 unsigned num_linking_shaders = num_shaders;
1062 for (unsigned i = 0; i < num_shaders; i++)
1063 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1064
1065 gl_shader **linking_shaders =
1066 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1067
1068 memcpy(linking_shaders, shader_list,
1069 sizeof(linking_shaders[0]) * num_shaders);
1070
1071 unsigned idx = num_shaders;
1072 for (unsigned i = 0; i < num_shaders; i++) {
1073 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1074 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1075 idx += shader_list[i]->num_builtins_to_link;
1076 }
1077
1078 assert(idx == num_linking_shaders);
1079
Ian Romanick4a455952010-10-13 15:13:02 -07001080 if (!link_function_calls(prog, linked, linking_shaders,
1081 num_linking_shaders)) {
1082 ctx->Driver.DeleteShader(ctx, linked);
1083 linked = NULL;
1084 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001085
1086 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001087
Paul Berryc148ef62011-08-03 15:37:01 -07001088 /* At this point linked should contain all of the linked IR, so
1089 * validate it to make sure nothing went wrong.
1090 */
1091 if (linked)
1092 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07001093
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001094 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001095 * unspecified sizes have a size specified. The size is inferred from the
1096 * max_array_access field.
1097 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001098 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001099 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001100
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001101 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001102 }
1103
Ian Romanick3fb87872010-07-09 14:09:34 -07001104 return linked;
1105}
1106
Eric Anholta721abf2010-08-23 10:32:01 -07001107/**
1108 * Update the sizes of linked shader uniform arrays to the maximum
1109 * array index used.
1110 *
1111 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1112 *
1113 * If one or more elements of an array are active,
1114 * GetActiveUniform will return the name of the array in name,
1115 * subject to the restrictions listed above. The type of the array
1116 * is returned in type. The size parameter contains the highest
1117 * array element index used, plus one. The compiler or linker
1118 * determines the highest index used. There will be only one
1119 * active uniform reported by the GL per uniform array.
1120
1121 */
1122static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001123update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001124{
Ian Romanick3322fba2010-10-14 13:28:42 -07001125 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1126 if (prog->_LinkedShaders[i] == NULL)
1127 continue;
1128
Eric Anholta721abf2010-08-23 10:32:01 -07001129 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1130 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1131
Eric Anholt586b4b52010-09-28 14:32:16 -07001132 if ((var == NULL) || (var->mode != ir_var_uniform &&
Paul Berry42a29d82013-01-11 14:39:32 -08001133 var->mode != ir_var_shader_in &&
1134 var->mode != ir_var_shader_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001135 !var->type->is_array())
1136 continue;
1137
Eric Anholt9feb4032012-05-01 14:43:31 -07001138 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1139 * will not be eliminated. Since we always do std140, just
1140 * don't resize arrays in UBOs.
1141 */
Ian Romanick13be1f42012-12-14 12:00:14 -08001142 if (var->is_in_uniform_block())
Eric Anholt9feb4032012-05-01 14:43:31 -07001143 continue;
1144
Eric Anholta721abf2010-08-23 10:32:01 -07001145 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001146 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1147 if (prog->_LinkedShaders[j] == NULL)
1148 continue;
1149
Eric Anholta721abf2010-08-23 10:32:01 -07001150 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1151 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1152 if (!other_var)
1153 continue;
1154
1155 if (strcmp(var->name, other_var->name) == 0 &&
1156 other_var->max_array_access > size) {
1157 size = other_var->max_array_access;
1158 }
1159 }
1160 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001161
Fabian Bieler63684782013-06-14 13:37:07 +02001162 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001163 /* If this is a built-in uniform (i.e., it's backed by some
1164 * fixed-function state), adjust the number of state slots to
1165 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001166 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001167 * slots is an integer multiple of the number of array elements.
1168 * Determine the number of slots per array element by dividing by
1169 * the old (total) size.
1170 */
1171 if (var->num_state_slots > 0) {
1172 var->num_state_slots = (size + 1)
1173 * (var->num_state_slots / var->type->length);
1174 }
1175
Eric Anholta721abf2010-08-23 10:32:01 -07001176 var->type = glsl_type::get_array_instance(var->type->fields.array,
1177 size + 1);
1178 /* FINISHME: We should update the types of array
1179 * dereferences of this variable now.
1180 */
1181 }
1182 }
1183 }
1184}
1185
Ian Romanick69846702010-06-22 17:29:19 -07001186/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001187 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001188 *
1189 * \param used_mask Bits representing used (1) and unused (0) locations
1190 * \param needed_count Number of contiguous bits needed.
1191 *
1192 * \return
1193 * Base location of the available bits on success or -1 on failure.
1194 */
1195int
1196find_available_slots(unsigned used_mask, unsigned needed_count)
1197{
1198 unsigned needed_mask = (1 << needed_count) - 1;
1199 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1200
1201 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1202 * cannot optimize possibly infinite loops" for the loop below.
1203 */
1204 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1205 return -1;
1206
1207 for (int i = 0; i <= max_bit_to_test; i++) {
1208 if ((needed_mask & ~used_mask) == needed_mask)
1209 return i;
1210
1211 needed_mask <<= 1;
1212 }
1213
1214 return -1;
1215}
1216
1217
Ian Romanickd32d4f72011-06-27 17:59:58 -07001218/**
1219 * Assign locations for either VS inputs for FS outputs
1220 *
1221 * \param prog Shader program whose variables need locations assigned
1222 * \param target_index Selector for the program target to receive location
1223 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1224 * \c MESA_SHADER_FRAGMENT.
1225 * \param max_index Maximum number of generic locations. This corresponds
1226 * to either the maximum number of draw buffers or the
1227 * maximum number of generic attributes.
1228 *
1229 * \return
1230 * If locations are successfully assigned, true is returned. Otherwise an
1231 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001232 */
Ian Romanick69846702010-06-22 17:29:19 -07001233bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001234assign_attribute_or_color_locations(gl_shader_program *prog,
1235 unsigned target_index,
1236 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001237{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001238 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001239 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001240 unsigned used_locations = (max_index >= 32)
1241 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001242
Ian Romanickd32d4f72011-06-27 17:59:58 -07001243 assert((target_index == MESA_SHADER_VERTEX)
1244 || (target_index == MESA_SHADER_FRAGMENT));
1245
1246 gl_shader *const sh = prog->_LinkedShaders[target_index];
1247 if (sh == NULL)
1248 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001249
Ian Romanick69846702010-06-22 17:29:19 -07001250 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001251 *
1252 * 1. Invalidate the location assignments for all vertex shader inputs.
1253 *
1254 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001255 * glBindVertexAttribLocation) locations and outputs that have
1256 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001257 *
Ian Romanick69846702010-06-22 17:29:19 -07001258 * 3. Sort the attributes without assigned locations by number of slots
1259 * required in decreasing order. Fragmentation caused by attribute
1260 * locations assigned by the application may prevent large attributes
1261 * from having enough contiguous space.
1262 *
1263 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001264 */
1265
Ian Romanickd32d4f72011-06-27 17:59:58 -07001266 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001267 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001268
Ian Romanickd32d4f72011-06-27 17:59:58 -07001269 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001270 (target_index == MESA_SHADER_VERTEX)
1271 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001272
1273
Ian Romanick69846702010-06-22 17:29:19 -07001274 /* Temporary storage for the set of attributes that need locations assigned.
1275 */
1276 struct temp_attr {
1277 unsigned slots;
1278 ir_variable *var;
1279
1280 /* Used below in the call to qsort. */
1281 static int compare(const void *a, const void *b)
1282 {
1283 const temp_attr *const l = (const temp_attr *) a;
1284 const temp_attr *const r = (const temp_attr *) b;
1285
1286 /* Reversed because we want a descending order sort below. */
1287 return r->slots - l->slots;
1288 }
1289 } to_assign[16];
1290
1291 unsigned num_attr = 0;
1292
Eric Anholt16b68b12010-06-30 11:05:43 -07001293 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001294 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1295
Brian Paul4470ff22011-07-19 21:10:25 -06001296 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001297 continue;
1298
Ian Romanick68a4fc92010-10-07 17:21:22 -07001299 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001300 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001301 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001302 linker_error(prog,
1303 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001304 (var->location < 0)
1305 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001306 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001307 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001308 }
1309 } else if (target_index == MESA_SHADER_VERTEX) {
1310 unsigned binding;
1311
1312 if (prog->AttributeBindings->get(binding, var->name)) {
1313 assert(binding >= VERT_ATTRIB_GENERIC0);
1314 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001315 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001316 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001317 } else if (target_index == MESA_SHADER_FRAGMENT) {
1318 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001319 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001320
1321 if (prog->FragDataBindings->get(binding, var->name)) {
1322 assert(binding >= FRAG_RESULT_DATA0);
1323 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001324 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001325
1326 if (prog->FragDataIndexBindings->get(index, var->name)) {
1327 var->index = index;
1328 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001329 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001330 }
1331
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001332 /* If the variable is not a built-in and has a location statically
1333 * assigned in the shader (presumably via a layout qualifier), make sure
1334 * that it doesn't collide with other assigned locations. Otherwise,
1335 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001336 */
Ian Romanick523b6112011-08-17 15:40:03 -07001337 const unsigned slots = count_attribute_slots(var->type);
1338 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001339 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001340 /* From page 61 of the OpenGL 4.0 spec:
1341 *
1342 * "LinkProgram will fail if the attribute bindings assigned
1343 * by BindAttribLocation do not leave not enough space to
1344 * assign a location for an active matrix attribute or an
1345 * active attribute array, both of which require multiple
1346 * contiguous generic attributes."
1347 *
1348 * Previous versions of the spec contain similar language but omit
1349 * the bit about attribute arrays.
1350 *
1351 * Page 61 of the OpenGL 4.0 spec also says:
1352 *
1353 * "It is possible for an application to bind more than one
1354 * attribute name to the same location. This is referred to as
1355 * aliasing. This will only work if only one of the aliased
1356 * attributes is active in the executable program, or if no
1357 * path through the shader consumes more than one attribute of
1358 * a set of attributes aliased to the same location. A link
1359 * error can occur if the linker determines that every path
1360 * through the shader consumes multiple aliased attributes,
1361 * but implementations are not required to generate an error
1362 * in this case."
1363 *
1364 * These two paragraphs are either somewhat contradictory, or I
1365 * don't fully understand one or both of them.
1366 */
1367 /* FINISHME: The code as currently written does not support
1368 * FINISHME: attribute location aliasing (see comment above).
1369 */
1370 /* Mask representing the contiguous slots that will be used by
1371 * this attribute.
1372 */
1373 const unsigned attr = var->location - generic_base;
1374 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001375
Ian Romanick523b6112011-08-17 15:40:03 -07001376 /* Generate a link error if the set of bits requested for this
1377 * attribute overlaps any previously allocated bits.
1378 */
1379 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001380 const char *const string = (target_index == MESA_SHADER_VERTEX)
1381 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001382 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001383 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001384 "available for %s `%s' %d %d %d", string,
1385 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001386 return false;
1387 }
1388
1389 used_locations |= (use_mask << attr);
1390 }
1391
1392 continue;
1393 }
1394
1395 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001396 to_assign[num_attr].var = var;
1397 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001398 }
Ian Romanick69846702010-06-22 17:29:19 -07001399
1400 /* If all of the attributes were assigned locations by the application (or
1401 * are built-in attributes with fixed locations), return early. This should
1402 * be the common case.
1403 */
1404 if (num_attr == 0)
1405 return true;
1406
1407 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1408
Ian Romanickd32d4f72011-06-27 17:59:58 -07001409 if (target_index == MESA_SHADER_VERTEX) {
1410 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1411 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1412 * reserved to prevent it from being automatically allocated below.
1413 */
1414 find_deref_visitor find("gl_Vertex");
1415 find.run(sh->ir);
1416 if (find.variable_found())
1417 used_locations |= (1 << 0);
1418 }
Ian Romanick982e3792010-06-29 18:58:20 -07001419
Ian Romanick69846702010-06-22 17:29:19 -07001420 for (unsigned i = 0; i < num_attr; i++) {
1421 /* Mask representing the contiguous slots that will be used by this
1422 * attribute.
1423 */
1424 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1425
1426 int location = find_available_slots(used_locations, to_assign[i].slots);
1427
1428 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001429 const char *const string = (target_index == MESA_SHADER_VERTEX)
1430 ? "vertex shader input" : "fragment shader output";
1431
Ian Romanick586e7412011-07-28 14:04:09 -07001432 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001433 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001434 "available for %s `%s'",
1435 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001436 return false;
1437 }
1438
Ian Romanickd32d4f72011-06-27 17:59:58 -07001439 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001440 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001441 used_locations |= (use_mask << location);
1442 }
1443
1444 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001445}
1446
1447
Ian Romanick40e114b2010-08-17 14:55:50 -07001448/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001449 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001450 */
1451void
Ian Romanickcc90e622010-10-19 17:59:10 -07001452demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001453{
1454 foreach_list(node, sh->ir) {
1455 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1456
Ian Romanickcc90e622010-10-19 17:59:10 -07001457 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001458 continue;
1459
Ian Romanickcc90e622010-10-19 17:59:10 -07001460 /* A shader 'in' or 'out' variable is only really an input or output if
1461 * its value is used by other shader stages. This will cause the variable
1462 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001463 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001464 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001465 var->mode = ir_var_auto;
1466 }
1467 }
1468}
1469
1470
Paul Berry871ddb92011-11-05 11:17:32 -07001471/**
Marek Olšákec174a42011-11-18 15:00:10 +01001472 * Store the gl_FragDepth layout in the gl_shader_program struct.
1473 */
1474static void
1475store_fragdepth_layout(struct gl_shader_program *prog)
1476{
1477 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1478 return;
1479 }
1480
1481 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1482
1483 /* We don't look up the gl_FragDepth symbol directly because if
1484 * gl_FragDepth is not used in the shader, it's removed from the IR.
1485 * However, the symbol won't be removed from the symbol table.
1486 *
1487 * We're only interested in the cases where the variable is NOT removed
1488 * from the IR.
1489 */
1490 foreach_list(node, ir) {
1491 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1492
Paul Berry42a29d82013-01-11 14:39:32 -08001493 if (var == NULL || var->mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001494 continue;
1495 }
1496
1497 if (strcmp(var->name, "gl_FragDepth") == 0) {
1498 switch (var->depth_layout) {
1499 case ir_depth_layout_none:
1500 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1501 return;
1502 case ir_depth_layout_any:
1503 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1504 return;
1505 case ir_depth_layout_greater:
1506 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1507 return;
1508 case ir_depth_layout_less:
1509 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1510 return;
1511 case ir_depth_layout_unchanged:
1512 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1513 return;
1514 default:
1515 assert(0);
1516 return;
1517 }
1518 }
1519 }
1520}
1521
1522/**
Ian Romanick92f81592011-11-08 12:37:19 -08001523 * Validate the resources used by a program versus the implementation limits
1524 */
Paul Berryb95d2372013-07-27 11:08:31 -07001525static void
Ian Romanick92f81592011-11-08 12:37:19 -08001526check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1527{
1528 static const char *const shader_names[MESA_SHADER_TYPES] = {
Marek Olšák030ca232013-06-12 17:15:46 +02001529 "vertex", "geometry", "fragment"
Ian Romanick92f81592011-11-08 12:37:19 -08001530 };
1531
1532 const unsigned max_samplers[MESA_SHADER_TYPES] = {
Marek Olšák5e784332013-05-02 02:30:44 +02001533 ctx->Const.VertexProgram.MaxTextureImageUnits,
Marek Olšák030ca232013-06-12 17:15:46 +02001534 ctx->Const.GeometryProgram.MaxTextureImageUnits,
1535 ctx->Const.FragmentProgram.MaxTextureImageUnits
Ian Romanick92f81592011-11-08 12:37:19 -08001536 };
1537
Eric Anholt38e77e52013-05-23 11:10:15 -07001538 const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = {
Ian Romanick92f81592011-11-08 12:37:19 -08001539 ctx->Const.VertexProgram.MaxUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001540 ctx->Const.GeometryProgram.MaxUniformComponents,
1541 ctx->Const.FragmentProgram.MaxUniformComponents
Ian Romanick92f81592011-11-08 12:37:19 -08001542 };
1543
Eric Anholt38e77e52013-05-23 11:10:15 -07001544 const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = {
1545 ctx->Const.VertexProgram.MaxCombinedUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001546 ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
1547 ctx->Const.FragmentProgram.MaxCombinedUniformComponents
Eric Anholt38e77e52013-05-23 11:10:15 -07001548 };
1549
Eric Anholt877a8972012-06-25 12:47:01 -07001550 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
1551 ctx->Const.VertexProgram.MaxUniformBlocks,
Eric Anholt877a8972012-06-25 12:47:01 -07001552 ctx->Const.GeometryProgram.MaxUniformBlocks,
Marek Olšák030ca232013-06-12 17:15:46 +02001553 ctx->Const.FragmentProgram.MaxUniformBlocks
Eric Anholt877a8972012-06-25 12:47:01 -07001554 };
1555
Ian Romanick92f81592011-11-08 12:37:19 -08001556 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1557 struct gl_shader *sh = prog->_LinkedShaders[i];
1558
1559 if (sh == NULL)
1560 continue;
1561
1562 if (sh->num_samplers > max_samplers[i]) {
1563 linker_error(prog, "Too many %s shader texture samplers",
1564 shader_names[i]);
1565 }
1566
Eric Anholt38e77e52013-05-23 11:10:15 -07001567 if (sh->num_uniform_components > max_default_uniform_components[i]) {
1568 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1569 linker_warning(prog, "Too many %s shader default uniform block "
1570 "components, but the driver will try to optimize "
1571 "them out; this is non-portable out-of-spec "
1572 "behavior\n",
1573 shader_names[i]);
1574 } else {
1575 linker_error(prog, "Too many %s shader default uniform block "
1576 "components",
1577 shader_names[i]);
1578 }
1579 }
1580
1581 if (sh->num_combined_uniform_components >
1582 max_combined_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001583 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1584 linker_warning(prog, "Too many %s shader uniform components, "
1585 "but the driver will try to optimize them out; "
1586 "this is non-portable out-of-spec behavior\n",
1587 shader_names[i]);
1588 } else {
1589 linker_error(prog, "Too many %s shader uniform components",
1590 shader_names[i]);
1591 }
Ian Romanick92f81592011-11-08 12:37:19 -08001592 }
1593 }
1594
Eric Anholt877a8972012-06-25 12:47:01 -07001595 unsigned blocks[MESA_SHADER_TYPES] = {0};
1596 unsigned total_uniform_blocks = 0;
1597
1598 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
1599 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1600 if (prog->UniformBlockStageIndex[j][i] != -1) {
1601 blocks[j]++;
1602 total_uniform_blocks++;
1603 }
1604 }
1605
1606 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1607 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1608 prog->NumUniformBlocks,
1609 ctx->Const.MaxCombinedUniformBlocks);
1610 } else {
1611 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1612 if (blocks[i] > max_uniform_blocks[i]) {
1613 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
1614 shader_names[i],
1615 blocks[i],
1616 max_uniform_blocks[i]);
1617 break;
1618 }
1619 }
1620 }
1621 }
Ian Romanick92f81592011-11-08 12:37:19 -08001622}
Paul Berry871ddb92011-11-05 11:17:32 -07001623
Ian Romanick0e59b262010-06-23 11:23:01 -07001624void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001625link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001626{
Paul Berry871ddb92011-11-05 11:17:32 -07001627 tfeedback_decl *tfeedback_decls = NULL;
1628 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1629
Kenneth Graunked3073f52011-01-21 14:32:31 -08001630 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001631
Paul Berryb95d2372013-07-27 11:08:31 -07001632 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07001633 prog->Validated = false;
1634 prog->_Used = false;
1635
Eric Anholtf609cf72012-04-27 13:52:56 -07001636 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08001637 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07001638
Eric Anholtf609cf72012-04-27 13:52:56 -07001639 ralloc_free(prog->UniformBlocks);
1640 prog->UniformBlocks = NULL;
1641 prog->NumUniformBlocks = 0;
1642 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
1643 ralloc_free(prog->UniformBlockStageIndex[i]);
1644 prog->UniformBlockStageIndex[i] = NULL;
1645 }
1646
Ian Romanick832dfa52010-06-17 15:04:20 -07001647 /* Separate the shaders into groups based on their type.
1648 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001649 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001650 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001651 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001652 unsigned num_frag_shaders = 0;
1653
Eric Anholt16b68b12010-06-30 11:05:43 -07001654 vert_shader_list = (struct gl_shader **)
1655 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07001656 frag_shader_list = &vert_shader_list[prog->NumShaders];
1657
Ian Romanick25f51d32010-07-16 15:51:50 -07001658 unsigned min_version = UINT_MAX;
1659 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07001660 const bool is_es_prog =
1661 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07001662 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07001663 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1664 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1665
Paul Berrya9f34dc2012-08-02 17:49:44 -07001666 if (prog->Shaders[i]->IsES != is_es_prog) {
1667 linker_error(prog, "all shaders must use same shading "
1668 "language version\n");
1669 goto done;
1670 }
1671
Ian Romanick832dfa52010-06-17 15:04:20 -07001672 switch (prog->Shaders[i]->Type) {
1673 case GL_VERTEX_SHADER:
1674 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1675 num_vert_shaders++;
1676 break;
1677 case GL_FRAGMENT_SHADER:
1678 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1679 num_frag_shaders++;
1680 break;
1681 case GL_GEOMETRY_SHADER:
1682 /* FINISHME: Support geometry shaders. */
1683 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1684 break;
1685 }
1686 }
1687
Ian Romanick25f51d32010-07-16 15:51:50 -07001688 /* Previous to GLSL version 1.30, different compilation units could mix and
1689 * match shading language versions. With GLSL 1.30 and later, the versions
1690 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07001691 *
1692 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07001693 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07001694 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07001695 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07001696 linker_error(prog, "all shaders must use same shading "
1697 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07001698 goto done;
1699 }
1700
1701 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07001702 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07001703
Ian Romanick3322fba2010-10-14 13:28:42 -07001704 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1705 if (prog->_LinkedShaders[i] != NULL)
1706 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1707
1708 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07001709 }
1710
Ian Romanickcd6764e2010-07-16 16:00:07 -07001711 /* Link all shaders for a particular stage and validate the result.
1712 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001713 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001714 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001715 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1716 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001717
Paul Berryb95d2372013-07-27 11:08:31 -07001718 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07001719 goto done;
1720
Paul Berryb95d2372013-07-27 11:08:31 -07001721 validate_vertex_shader_executable(prog, sh);
1722 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001723 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001724
Ian Romanick3322fba2010-10-14 13:28:42 -07001725 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1726 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001727 }
1728
1729 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001730 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001731 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1732 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001733
Paul Berryb95d2372013-07-27 11:08:31 -07001734 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07001735 goto done;
1736
Paul Berryb95d2372013-07-27 11:08:31 -07001737 validate_fragment_shader_executable(prog, sh);
1738 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07001739 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07001740
Ian Romanick3322fba2010-10-14 13:28:42 -07001741 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1742 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001743 }
1744
Ian Romanick3ed850e2010-06-23 12:18:21 -07001745 /* Here begins the inter-stage linking phase. Some initial validation is
1746 * performed, then locations are assigned for uniforms, attributes, and
1747 * varyings.
1748 */
Paul Berryb95d2372013-07-27 11:08:31 -07001749 cross_validate_uniforms(prog);
1750 if (!prog->LinkStatus)
1751 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001752
Paul Berryb95d2372013-07-27 11:08:31 -07001753 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07001754
Paul Berryb95d2372013-07-27 11:08:31 -07001755 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1756 if (prog->_LinkedShaders[prev] != NULL)
1757 break;
1758 }
Ian Romanick3322fba2010-10-14 13:28:42 -07001759
Paul Berryb95d2372013-07-27 11:08:31 -07001760 /* Validate the inputs of each stage with the output of the preceding
1761 * stage.
1762 */
1763 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1764 if (prog->_LinkedShaders[i] == NULL)
1765 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07001766
Paul Berryb95d2372013-07-27 11:08:31 -07001767 validate_interstage_interface_blocks(prog, prog->_LinkedShaders[prev],
1768 prog->_LinkedShaders[i]);
1769 if (!prog->LinkStatus)
1770 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07001771
Paul Berryb95d2372013-07-27 11:08:31 -07001772 cross_validate_outputs_to_inputs(prog,
1773 prog->_LinkedShaders[prev],
1774 prog->_LinkedShaders[i]);
1775 if (!prog->LinkStatus)
1776 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07001777
Paul Berryb95d2372013-07-27 11:08:31 -07001778 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07001779 }
Ian Romanick832dfa52010-06-17 15:04:20 -07001780
Jordan Justen5ebf5472013-03-10 03:20:03 -07001781
1782 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1783 if (prog->_LinkedShaders[i] != NULL)
1784 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
1785 }
1786
Eric Anholt3de13952012-05-04 13:08:46 -07001787 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
1788 * it before optimization because we want most of the checks to get
1789 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07001790 *
1791 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07001792 */
Paul Berry15ba2a52012-08-02 17:51:02 -07001793 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07001794 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1795 if (sh) {
1796 lower_discard_flow(sh->ir);
1797 }
1798 }
1799
Eric Anholtf609cf72012-04-27 13:52:56 -07001800 if (!interstage_cross_validate_uniform_blocks(prog))
1801 goto done;
1802
Eric Anholt2f4fe152010-08-10 13:06:49 -07001803 /* Do common optimization before assigning storage for attributes,
1804 * uniforms, and varyings. Later optimization could possibly make
1805 * some of that unused.
1806 */
Ian Romanick3322fba2010-10-14 13:28:42 -07001807 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1808 if (prog->_LinkedShaders[i] == NULL)
1809 continue;
1810
Ian Romanick02c5ae12011-07-11 10:46:01 -07001811 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
1812 if (!prog->LinkStatus)
1813 goto done;
1814
Paul Berry18392442012-12-04 11:11:02 -08001815 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
1816 lower_clip_distance(prog->_LinkedShaders[i]);
1817 }
Paul Berryc06e3252011-08-11 20:58:21 -07001818
Brian Paul7feabfe2012-03-20 17:43:12 -06001819 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
1820
Kenneth Graunkeb7657402013-04-17 17:30:22 -07001821 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
Eric Anholt2f4fe152010-08-10 13:06:49 -07001822 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07001823 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001824
Paul Berry50895d42012-12-05 07:17:07 -08001825 /* Mark all generic shader inputs and outputs as unpaired. */
1826 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1827 link_invalidate_variable_locations(
1828 prog->_LinkedShaders[MESA_SHADER_VERTEX],
Paul Berry36b252e2013-02-23 07:22:01 -08001829 VERT_ATTRIB_GENERIC0, VARYING_SLOT_VAR0);
Paul Berry50895d42012-12-05 07:17:07 -08001830 }
1831 /* FINISHME: Geometry shaders not implemented yet */
1832 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
1833 link_invalidate_variable_locations(
1834 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
Paul Berryeed6baf2013-02-23 09:00:58 -08001835 VARYING_SLOT_VAR0, FRAG_RESULT_DATA0);
Paul Berry50895d42012-12-05 07:17:07 -08001836 }
1837
Ian Romanickd32d4f72011-06-27 17:59:58 -07001838 /* FINISHME: The value of the max_attribute_index parameter is
1839 * FINISHME: implementation dependent based on the value of
1840 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1841 * FINISHME: at least 16, so hardcode 16 for now.
1842 */
1843 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001844 goto done;
1845 }
1846
Dave Airlie1256a5d2012-03-24 13:33:41 +00001847 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001848 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07001849 }
1850
Marek Olšák284d9542013-06-12 02:18:09 +02001851 unsigned first;
1852 for (first = 0; first < MESA_SHADER_TYPES; first++) {
1853 if (prog->_LinkedShaders[first] != NULL)
Ian Romanick3322fba2010-10-14 13:28:42 -07001854 break;
1855 }
1856
Paul Berry871ddb92011-11-05 11:17:32 -07001857 if (num_tfeedback_decls != 0) {
1858 /* From GL_EXT_transform_feedback:
1859 * A program will fail to link if:
1860 *
1861 * * the <count> specified by TransformFeedbackVaryingsEXT is
1862 * non-zero, but the program object has no vertex or geometry
1863 * shader;
1864 */
Marek Olšák284d9542013-06-12 02:18:09 +02001865 if (first >= MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07001866 linker_error(prog, "Transform feedback varyings specified, but "
1867 "no vertex or geometry shader is present.");
1868 goto done;
1869 }
1870
1871 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
1872 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08001873 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07001874 prog->TransformFeedback.VaryingNames,
1875 tfeedback_decls))
1876 goto done;
1877 }
1878
Marek Olšák284d9542013-06-12 02:18:09 +02001879 /* Linking the stages in the opposite order (from fragment to vertex)
1880 * ensures that inter-shader outputs written to in an earlier stage are
1881 * eliminated if they are (transitively) not used in a later stage.
1882 */
1883 int last, next;
1884 for (last = MESA_SHADER_TYPES-1; last >= 0; last--) {
1885 if (prog->_LinkedShaders[last] != NULL)
1886 break;
Ian Romanick3322fba2010-10-14 13:28:42 -07001887 }
Ian Romanick13e10e42010-06-21 12:03:24 -07001888
Marek Olšák284d9542013-06-12 02:18:09 +02001889 if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
1890 gl_shader *const sh = prog->_LinkedShaders[last];
1891
1892 if (num_tfeedback_decls != 0) {
1893 /* There was no fragment shader, but we still have to assign varying
1894 * locations for use by transform feedback.
1895 */
1896 if (!assign_varying_locations(ctx, mem_ctx, prog,
1897 sh, NULL,
1898 num_tfeedback_decls, tfeedback_decls))
1899 goto done;
1900 }
1901
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02001902 do_dead_builtin_varyings(ctx, sh->ir, NULL,
1903 num_tfeedback_decls, tfeedback_decls);
1904
Marek Olšák284d9542013-06-12 02:18:09 +02001905 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
1906
1907 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07001908 */
Marek Olšák284d9542013-06-12 02:18:09 +02001909 while (do_dead_code(sh->ir, false))
1910 ;
1911 }
1912 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02001913 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02001914 */
1915 gl_shader *const sh = prog->_LinkedShaders[first];
1916
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02001917 do_dead_builtin_varyings(ctx, NULL, sh->ir,
1918 num_tfeedback_decls, tfeedback_decls);
1919
Marek Olšák284d9542013-06-12 02:18:09 +02001920 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
1921
1922 while (do_dead_code(sh->ir, false))
1923 ;
1924 }
1925
1926 next = last;
1927 for (int i = next - 1; i >= 0; i--) {
1928 if (prog->_LinkedShaders[i] == NULL)
1929 continue;
1930
1931 gl_shader *const sh_i = prog->_LinkedShaders[i];
1932 gl_shader *const sh_next = prog->_LinkedShaders[next];
1933
1934 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
1935 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
1936 tfeedback_decls))
Paul Berry871ddb92011-11-05 11:17:32 -07001937 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02001938
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02001939 do_dead_builtin_varyings(ctx, sh_i->ir, sh_next->ir,
1940 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
1941 tfeedback_decls);
1942
Marek Olšák284d9542013-06-12 02:18:09 +02001943 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
1944 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
1945
1946 /* Eliminate code that is now dead due to unused outputs being demoted.
1947 */
1948 while (do_dead_code(sh_i->ir, false))
1949 ;
1950 while (do_dead_code(sh_next->ir, false))
1951 ;
1952
Marek Olšák3c555822013-06-13 03:17:22 +02001953 /* This must be done after all dead varyings are eliminated. */
1954 if (!check_against_varying_limit(ctx, prog, sh_next))
1955 goto done;
1956
Marek Olšák284d9542013-06-12 02:18:09 +02001957 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07001958 }
1959
1960 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
1961 goto done;
1962
Ian Romanick960d7222011-10-21 11:21:02 -07001963 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07001964 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01001965 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07001966
Paul Berryb95d2372013-07-27 11:08:31 -07001967 check_resources(ctx, prog);
1968 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08001969 goto done;
1970
Ian Romanickce9171f2011-02-03 17:10:14 -08001971 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07001972 * present in a linked program. By checking prog->IsES, we also
1973 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08001974 */
Eric Anholt57f79782011-07-22 12:57:47 -07001975 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07001976 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08001977 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001978 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08001979 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001980 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08001981 }
1982 }
1983
Ian Romanick13e10e42010-06-21 12:03:24 -07001984 /* FINISHME: Assign fragment shader output locations. */
1985
Ian Romanick832dfa52010-06-17 15:04:20 -07001986done:
1987 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001988
1989 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1990 if (prog->_LinkedShaders[i] == NULL)
1991 continue;
1992
1993 /* Retain any live IR, but trash the rest. */
1994 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07001995
1996 /* The symbol table in the linked shaders may contain references to
1997 * variables that were removed (e.g., unused uniforms). Since it may
1998 * contain junk, there is no possible valid use. Delete it and set the
1999 * pointer to NULL.
2000 */
2001 delete prog->_LinkedShaders[i]->symbols;
2002 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002003 }
2004
Kenneth Graunked3073f52011-01-21 14:32:31 -08002005 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002006}