blob: b13a6aa1ab649e318b8ac3503aa3590cc1a6e9dd [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"
Ian Romanick832dfa52010-06-17 15:04:20 -070069#include "ir.h"
70#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030071#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070072#include "linker.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070073#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070074
Ian Romanick3322fba2010-10-14 13:28:42 -070075extern "C" {
76#include "main/shaderobj.h"
77}
78
Ian Romanick832dfa52010-06-17 15:04:20 -070079/**
80 * Visitor that determines whether or not a variable is ever written.
81 */
82class find_assignment_visitor : public ir_hierarchical_visitor {
83public:
84 find_assignment_visitor(const char *name)
85 : name(name), found(false)
86 {
87 /* empty */
88 }
89
90 virtual ir_visitor_status visit_enter(ir_assignment *ir)
91 {
92 ir_variable *const var = ir->lhs->variable_referenced();
93
94 if (strcmp(name, var->name) == 0) {
95 found = true;
96 return visit_stop;
97 }
98
99 return visit_continue_with_parent;
100 }
101
Eric Anholt18a60232010-08-23 11:29:25 -0700102 virtual ir_visitor_status visit_enter(ir_call *ir)
103 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700104 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700105 foreach_iter(exec_list_iterator, iter, *ir) {
106 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
107 ir_variable *sig_param = (ir_variable *)sig_iter.get();
108
109 if (sig_param->mode == ir_var_out ||
110 sig_param->mode == ir_var_inout) {
111 ir_variable *var = param_rval->variable_referenced();
112 if (var && strcmp(name, var->name) == 0) {
113 found = true;
114 return visit_stop;
115 }
116 }
117 sig_iter.next();
118 }
119
Kenneth Graunked884f602012-03-20 15:56:37 -0700120 if (ir->return_deref != NULL) {
121 ir_variable *const var = ir->return_deref->variable_referenced();
122
123 if (strcmp(name, var->name) == 0) {
124 found = true;
125 return visit_stop;
126 }
127 }
128
Eric Anholt18a60232010-08-23 11:29:25 -0700129 return visit_continue_with_parent;
130 }
131
Ian Romanick832dfa52010-06-17 15:04:20 -0700132 bool variable_found()
133 {
134 return found;
135 }
136
137private:
138 const char *name; /**< Find writes to a variable with this name. */
139 bool found; /**< Was a write to the variable found? */
140};
141
Ian Romanickc93b8f12010-06-17 15:20:22 -0700142
Ian Romanickc33e78f2010-08-13 12:30:41 -0700143/**
144 * Visitor that determines whether or not a variable is ever read.
145 */
146class find_deref_visitor : public ir_hierarchical_visitor {
147public:
148 find_deref_visitor(const char *name)
149 : name(name), found(false)
150 {
151 /* empty */
152 }
153
154 virtual ir_visitor_status visit(ir_dereference_variable *ir)
155 {
156 if (strcmp(this->name, ir->var->name) == 0) {
157 this->found = true;
158 return visit_stop;
159 }
160
161 return visit_continue;
162 }
163
164 bool variable_found() const
165 {
166 return this->found;
167 }
168
169private:
170 const char *name; /**< Find writes to a variable with this name. */
171 bool found; /**< Was a write to the variable found? */
172};
173
174
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700175void
Ian Romanick586e7412011-07-28 14:04:09 -0700176linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700177{
178 va_list ap;
179
Kenneth Graunked3073f52011-01-21 14:32:31 -0800180 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700181 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800182 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700183 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700184
185 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700186}
187
188
189void
Ian Romanick379a32f2011-07-28 14:09:06 -0700190linker_warning(gl_shader_program *prog, const char *fmt, ...)
191{
192 va_list ap;
193
194 ralloc_strcat(&prog->InfoLog, "error: ");
195 va_start(ap, fmt);
196 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
197 va_end(ap);
198
199}
200
201
202void
Paul Berry50895d42012-12-05 07:17:07 -0800203link_invalidate_variable_locations(gl_shader *sh, int input_base,
204 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700205{
Eric Anholt16b68b12010-06-30 11:05:43 -0700206 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700207 ir_variable *const var = ((ir_instruction *) node)->as_variable();
208
Paul Berry50895d42012-12-05 07:17:07 -0800209 if (var == NULL)
210 continue;
211
212 int base;
213 switch (var->mode) {
214 case ir_var_in:
215 base = input_base;
216 break;
217 case ir_var_out:
218 base = output_base;
219 break;
220 default:
221 continue;
222 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700223
224 /* Only assign locations for generic attributes / varyings / etc.
225 */
Paul Berry50895d42012-12-05 07:17:07 -0800226 if ((var->location >= base) && !var->explicit_location)
227 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800228
Paul Berry3e81c662012-12-05 10:47:55 -0800229 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800230 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800231 var->location_frac = 0;
232 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800233 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800234 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700235 }
236}
237
238
Ian Romanickc93b8f12010-06-17 15:20:22 -0700239/**
Ian Romanick69846702010-06-22 17:29:19 -0700240 * Determine the number of attribute slots required for a particular type
241 *
242 * This code is here because it implements the language rules of a specific
243 * GLSL version. Since it's a property of the language and not a property of
244 * types in general, it doesn't really belong in glsl_type.
245 */
246unsigned
247count_attribute_slots(const glsl_type *t)
248{
249 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
250 *
251 * "A scalar input counts the same amount against this limit as a vec4,
252 * so applications may want to consider packing groups of four
253 * unrelated float inputs together into a vector to better utilize the
254 * capabilities of the underlying hardware. A matrix input will use up
255 * multiple locations. The number of locations used will equal the
256 * number of columns in the matrix."
257 *
258 * The spec does not explicitly say how arrays are counted. However, it
259 * should be safe to assume the total number of slots consumed by an array
260 * is the number of entries in the array multiplied by the number of slots
261 * consumed by a single element of the array.
262 */
263
264 if (t->is_array())
265 return t->array_size() * count_attribute_slots(t->element_type());
266
267 if (t->is_matrix())
268 return t->matrix_columns;
269
270 return 1;
271}
272
273
274/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700275 * Verify that a vertex shader executable meets all semantic requirements.
276 *
Paul Berry642e5b412012-01-04 13:57:52 -0800277 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
278 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700279 *
280 * \param shader Vertex shader executable to be verified
281 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700282bool
Eric Anholt849e1812010-06-30 11:49:17 -0700283validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700284 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700285{
286 if (shader == NULL)
287 return true;
288
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700289 /* From the GLSL 1.10 spec, page 48:
290 *
291 * "The variable gl_Position is available only in the vertex
292 * language and is intended for writing the homogeneous vertex
293 * position. All executions of a well-formed vertex shader
294 * executable must write a value into this variable. [...] The
295 * variable gl_Position is available only in the vertex
296 * language and is intended for writing the homogeneous vertex
297 * position. All executions of a well-formed vertex shader
298 * executable must write a value into this variable."
299 *
300 * while in GLSL 1.40 this text is changed to:
301 *
302 * "The variable gl_Position is available only in the vertex
303 * language and is intended for writing the homogeneous vertex
304 * position. It can be written at any time during shader
305 * execution. It may also be read back by a vertex shader
306 * after being written. This value will be used by primitive
307 * assembly, clipping, culling, and other fixed functionality
308 * operations, if present, that operate on primitives after
309 * vertex processing has occurred. Its value is undefined if
310 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700311 *
312 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
313 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700314 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700315 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700316 find_assignment_visitor find("gl_Position");
317 find.run(shader->ir);
318 if (!find.variable_found()) {
319 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
320 return false;
321 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700322 }
323
Paul Berry642e5b412012-01-04 13:57:52 -0800324 prog->Vert.ClipDistanceArraySize = 0;
325
Paul Berry15ba2a52012-08-02 17:51:02 -0700326 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700327 /* From section 7.1 (Vertex Shader Special Variables) of the
328 * GLSL 1.30 spec:
329 *
330 * "It is an error for a shader to statically write both
331 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700332 *
333 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
334 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700335 */
336 find_assignment_visitor clip_vertex("gl_ClipVertex");
337 find_assignment_visitor clip_distance("gl_ClipDistance");
338
339 clip_vertex.run(shader->ir);
340 clip_distance.run(shader->ir);
341 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
342 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
343 "and `gl_ClipDistance'\n");
344 return false;
345 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700346 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800347 ir_variable *clip_distance_var =
348 shader->symbols->get_variable("gl_ClipDistance");
349 if (clip_distance_var)
350 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700351 }
352
Ian Romanick832dfa52010-06-17 15:04:20 -0700353 return true;
354}
355
356
Ian Romanickc93b8f12010-06-17 15:20:22 -0700357/**
358 * Verify that a fragment shader executable meets all semantic requirements
359 *
360 * \param shader Fragment shader executable to be verified
361 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700362bool
Eric Anholt849e1812010-06-30 11:49:17 -0700363validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700364 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700365{
366 if (shader == NULL)
367 return true;
368
Ian Romanick832dfa52010-06-17 15:04:20 -0700369 find_assignment_visitor frag_color("gl_FragColor");
370 find_assignment_visitor frag_data("gl_FragData");
371
Eric Anholt16b68b12010-06-30 11:05:43 -0700372 frag_color.run(shader->ir);
373 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700374
Ian Romanick832dfa52010-06-17 15:04:20 -0700375 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700376 linker_error(prog, "fragment shader writes to both "
377 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700378 return false;
379 }
380
381 return true;
382}
383
384
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700385/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700386 * Generate a string describing the mode of a variable
387 */
388static const char *
389mode_string(const ir_variable *var)
390{
391 switch (var->mode) {
392 case ir_var_auto:
393 return (var->read_only) ? "global constant" : "global variable";
394
395 case ir_var_uniform: return "uniform";
396 case ir_var_in: return "shader input";
397 case ir_var_out: return "shader output";
398 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700399
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800400 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700401 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700402 default:
403 assert(!"Should not get here.");
404 return "invalid variable";
405 }
406}
407
408
409/**
410 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700411 */
412bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700413cross_validate_globals(struct gl_shader_program *prog,
414 struct gl_shader **shader_list,
415 unsigned num_shaders,
416 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700417{
418 /* Examine all of the uniforms in all of the shaders and cross validate
419 * them.
420 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700421 glsl_symbol_table variables;
422 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700423 if (shader_list[i] == NULL)
424 continue;
425
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700426 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700427 ir_variable *const var = ((ir_instruction *) node)->as_variable();
428
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700429 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700430 continue;
431
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700432 if (uniforms_only && (var->mode != ir_var_uniform))
433 continue;
434
Ian Romanick7e2aa912010-07-19 17:12:42 -0700435 /* Don't cross validate temporaries that are at global scope. These
436 * will eventually get pulled into the shaders 'main'.
437 */
438 if (var->mode == ir_var_temporary)
439 continue;
440
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700441 /* If a global with this name has already been seen, verify that the
442 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700443 * initializers, the values of the initializers must be the same.
444 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700445 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700446 if (existing != NULL) {
447 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700448 /* Consider the types to be "the same" if both types are arrays
449 * of the same type and one of the arrays is implicitly sized.
450 * In addition, set the type of the linked variable to the
451 * explicitly sized array.
452 */
453 if (var->type->is_array()
454 && existing->type->is_array()
455 && (var->type->fields.array == existing->type->fields.array)
456 && ((var->type->length == 0)
457 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800458 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700459 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800460 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700461 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700462 linker_error(prog, "%s `%s' declared as type "
463 "`%s' and type `%s'\n",
464 mode_string(var),
465 var->name, var->type->name,
466 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700467 return false;
468 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700469 }
470
Ian Romanick68a4fc92010-10-07 17:21:22 -0700471 if (var->explicit_location) {
472 if (existing->explicit_location
473 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700474 linker_error(prog, "explicit locations for %s "
475 "`%s' have differing values\n",
476 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700477 return false;
478 }
479
480 existing->location = var->location;
481 existing->explicit_location = true;
482 }
483
Ian Romanick46173f92011-10-31 13:07:06 -0700484 /* Validate layout qualifiers for gl_FragDepth.
485 *
486 * From the AMD/ARB_conservative_depth specs:
487 *
488 * "If gl_FragDepth is redeclared in any fragment shader in a
489 * program, it must be redeclared in all fragment shaders in
490 * that program that have static assignments to
491 * gl_FragDepth. All redeclarations of gl_FragDepth in all
492 * fragment shaders in a single program must have the same set
493 * of qualifiers."
494 */
495 if (strcmp(var->name, "gl_FragDepth") == 0) {
496 bool layout_declared = var->depth_layout != ir_depth_layout_none;
497 bool layout_differs =
498 var->depth_layout != existing->depth_layout;
499
500 if (layout_declared && layout_differs) {
501 linker_error(prog,
502 "All redeclarations of gl_FragDepth in all "
503 "fragment shaders in a single program must have "
504 "the same set of qualifiers.");
505 }
506
507 if (var->used && layout_differs) {
508 linker_error(prog,
509 "If gl_FragDepth is redeclared with a layout "
510 "qualifier in any fragment shader, it must be "
511 "redeclared with the same layout qualifier in "
512 "all fragment shaders that have assignments to "
513 "gl_FragDepth");
514 }
515 }
Chad Versaceaddae332011-01-27 01:40:31 -0800516
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700517 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
518 *
519 * "If a shared global has multiple initializers, the
520 * initializers must all be constant expressions, and they
521 * must all have the same value. Otherwise, a link error will
522 * result. (A shared global having only one initializer does
523 * not require that initializer to be a constant expression.)"
524 *
525 * Previous to 4.20 the GLSL spec simply said that initializers
526 * must have the same value. In this case of non-constant
527 * initializers, this was impossible to determine. As a result,
528 * no vendor actually implemented that behavior. The 4.20
529 * behavior matches the implemented behavior of at least one other
530 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700531 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700532 if (var->constant_initializer != NULL) {
533 if (existing->constant_initializer != NULL) {
534 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700535 linker_error(prog, "initializers for %s "
536 "`%s' have differing values\n",
537 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700538 return false;
539 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700540 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700541 /* If the first-seen instance of a particular uniform did not
542 * have an initializer but a later instance does, copy the
543 * initializer to the version stored in the symbol table.
544 */
Ian Romanickde415b72010-07-14 13:22:12 -0700545 /* FINISHME: This is wrong. The constant_value field should
546 * FINISHME: not be modified! Imagine a case where a shader
547 * FINISHME: without an initializer is linked in two different
548 * FINISHME: programs with shaders that have differing
549 * FINISHME: initializers. Linking with the first will
550 * FINISHME: modify the shader, and linking with the second
551 * FINISHME: will fail.
552 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700553 existing->constant_initializer =
554 var->constant_initializer->clone(ralloc_parent(existing),
555 NULL);
556 }
557 }
558
559 if (var->has_initializer) {
560 if (existing->has_initializer
561 && (var->constant_initializer == NULL
562 || existing->constant_initializer == NULL)) {
563 linker_error(prog,
564 "shared global variable `%s' has multiple "
565 "non-constant initializers.\n",
566 var->name);
567 return false;
568 }
569
570 /* Some instance had an initializer, so keep track of that. In
571 * this location, all sorts of initializers (constant or
572 * otherwise) will propagate the existence to the variable
573 * stored in the symbol table.
574 */
575 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700576 }
Chad Versace7528f142010-11-17 14:34:38 -0800577
578 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700579 linker_error(prog, "declarations for %s `%s' have "
580 "mismatching invariant qualifiers\n",
581 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800582 return false;
583 }
Chad Versace61428dd2011-01-10 15:29:30 -0800584 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700585 linker_error(prog, "declarations for %s `%s' have "
586 "mismatching centroid qualifiers\n",
587 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800588 return false;
589 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700590 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700591 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700592 }
593 }
594
595 return true;
596}
597
598
Ian Romanick37101922010-06-18 19:02:10 -0700599/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700600 * Perform validation of uniforms used across multiple shader stages
601 */
602bool
603cross_validate_uniforms(struct gl_shader_program *prog)
604{
605 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700606 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700607}
608
Eric Anholtf609cf72012-04-27 13:52:56 -0700609/**
610 * Accumulates the array of prog->UniformBlocks and checks that all
611 * definitons of blocks agree on their contents.
612 */
613static bool
614interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
615{
616 unsigned max_num_uniform_blocks = 0;
617 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
618 if (prog->_LinkedShaders[i])
619 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
620 }
621
622 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
623 struct gl_shader *sh = prog->_LinkedShaders[i];
624
625 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
626 max_num_uniform_blocks);
627 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
628 prog->UniformBlockStageIndex[i][j] = -1;
629
630 if (sh == NULL)
631 continue;
632
633 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
634 int index = link_cross_validate_uniform_block(prog,
635 &prog->UniformBlocks,
636 &prog->NumUniformBlocks,
637 &sh->UniformBlocks[j]);
638
639 if (index == -1) {
640 linker_error(prog, "uniform block `%s' has mismatching definitions",
641 sh->UniformBlocks[j].Name);
642 return false;
643 }
644
645 prog->UniformBlockStageIndex[i][index] = j;
646 }
647 }
648
649 return true;
650}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700651
652/**
Ian Romanick37101922010-06-18 19:02:10 -0700653 * Validate that outputs from one stage match inputs of another
654 */
655bool
Eric Anholt849e1812010-06-30 11:49:17 -0700656cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700657 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700658{
659 glsl_symbol_table parameters;
660 /* FINISHME: Figure these out dynamically. */
661 const char *const producer_stage = "vertex";
662 const char *const consumer_stage = "fragment";
663
664 /* Find all shader outputs in the "producer" stage.
665 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700666 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700667 ir_variable *const var = ((ir_instruction *) node)->as_variable();
668
669 /* FINISHME: For geometry shaders, this should also look for inout
670 * FINISHME: variables.
671 */
672 if ((var == NULL) || (var->mode != ir_var_out))
673 continue;
674
Eric Anholt001eee52010-11-05 06:11:24 -0700675 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700676 }
677
678
679 /* Find all shader inputs in the "consumer" stage. Any variables that have
680 * matching outputs already in the symbol table must have the same type and
681 * qualifiers.
682 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700683 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700684 ir_variable *const input = ((ir_instruction *) node)->as_variable();
685
686 /* FINISHME: For geometry shaders, this should also look for inout
687 * FINISHME: variables.
688 */
689 if ((input == NULL) || (input->mode != ir_var_in))
690 continue;
691
692 ir_variable *const output = parameters.get_variable(input->name);
693 if (output != NULL) {
694 /* Check that the types match between stages.
695 */
696 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800697 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500698 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800699 * access it must redeclare it with a size. There is some
700 * language in the GLSL spec that implies the fragment shader
701 * and vertex shader do not have to agree on this size. Other
702 * driver behave this way, and one or two applications seem to
703 * rely on it.
704 *
705 * Neither declaration needs to be modified here because the array
706 * sizes are fixed later when update_array_sizes is called.
707 *
708 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
709 *
710 * "Unlike user-defined varying variables, the built-in
711 * varying variables don't have a strict one-to-one
712 * correspondence between the vertex language and the
713 * fragment language."
714 */
715 if (!output->type->is_array()
716 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700717 linker_error(prog,
718 "%s shader output `%s' declared as type `%s', "
719 "but %s shader input declared as type `%s'\n",
720 producer_stage, output->name,
721 output->type->name,
722 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800723 return false;
724 }
Ian Romanick37101922010-06-18 19:02:10 -0700725 }
726
727 /* Check that all of the qualifiers match between stages.
728 */
729 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700730 linker_error(prog,
731 "%s shader output `%s' %s centroid qualifier, "
732 "but %s shader input %s centroid qualifier\n",
733 producer_stage,
734 output->name,
735 (output->centroid) ? "has" : "lacks",
736 consumer_stage,
737 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700738 return false;
739 }
740
741 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700742 linker_error(prog,
743 "%s shader output `%s' %s invariant qualifier, "
744 "but %s shader input %s invariant qualifier\n",
745 producer_stage,
746 output->name,
747 (output->invariant) ? "has" : "lacks",
748 consumer_stage,
749 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700750 return false;
751 }
752
753 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700754 linker_error(prog,
755 "%s shader output `%s' specifies %s "
756 "interpolation qualifier, "
757 "but %s shader input specifies %s "
758 "interpolation qualifier\n",
759 producer_stage,
760 output->name,
761 output->interpolation_string(),
762 consumer_stage,
763 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700764 return false;
765 }
766 }
767 }
768
769 return true;
770}
771
772
Ian Romanick3fb87872010-07-09 14:09:34 -0700773/**
774 * Populates a shaders symbol table with all global declarations
775 */
776static void
777populate_symbol_table(gl_shader *sh)
778{
779 sh->symbols = new(sh) glsl_symbol_table;
780
781 foreach_list(node, sh->ir) {
782 ir_instruction *const inst = (ir_instruction *) node;
783 ir_variable *var;
784 ir_function *func;
785
786 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700787 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700788 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700789 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700790 }
791 }
792}
793
794
795/**
Ian Romanick31a97862010-07-12 18:48:50 -0700796 * Remap variables referenced in an instruction tree
797 *
798 * This is used when instruction trees are cloned from one shader and placed in
799 * another. These trees will contain references to \c ir_variable nodes that
800 * do not exist in the target shader. This function finds these \c ir_variable
801 * references and replaces the references with matching variables in the target
802 * shader.
803 *
804 * If there is no matching variable in the target shader, a clone of the
805 * \c ir_variable is made and added to the target shader. The new variable is
806 * added to \b both the instruction stream and the symbol table.
807 *
808 * \param inst IR tree that is to be processed.
809 * \param symbols Symbol table containing global scope symbols in the
810 * linked shader.
811 * \param instructions Instruction stream where new variable declarations
812 * should be added.
813 */
814void
Eric Anholt8273bd42010-08-04 12:34:56 -0700815remap_variables(ir_instruction *inst, struct gl_shader *target,
816 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700817{
818 class remap_visitor : public ir_hierarchical_visitor {
819 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700820 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700821 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700822 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700823 this->target = target;
824 this->symbols = target->symbols;
825 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700826 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700827 }
828
829 virtual ir_visitor_status visit(ir_dereference_variable *ir)
830 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700831 if (ir->var->mode == ir_var_temporary) {
832 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
833
834 assert(var != NULL);
835 ir->var = var;
836 return visit_continue;
837 }
838
Ian Romanick31a97862010-07-12 18:48:50 -0700839 ir_variable *const existing =
840 this->symbols->get_variable(ir->var->name);
841 if (existing != NULL)
842 ir->var = existing;
843 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700844 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700845
Eric Anholt001eee52010-11-05 06:11:24 -0700846 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700847 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700848 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700849 }
850
851 return visit_continue;
852 }
853
854 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700855 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700856 glsl_symbol_table *symbols;
857 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700858 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700859 };
860
Eric Anholt8273bd42010-08-04 12:34:56 -0700861 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700862
863 inst->accept(&v);
864}
865
866
867/**
868 * Move non-declarations from one instruction stream to another
869 *
870 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700871 * 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 -0700872 * pointer) for \c last and \c false for \c make_copies on the first
873 * call. Successive calls pass the return value of the previous call for
874 * \c last and \c true for \c make_copies.
875 *
876 * \param instructions Source instruction stream
877 * \param last Instruction after which new instructions should be
878 * inserted in the target instruction stream
879 * \param make_copies Flag selecting whether instructions in \c instructions
880 * should be copied (via \c ir_instruction::clone) into the
881 * target list or moved.
882 *
883 * \return
884 * The new "last" instruction in the target instruction stream. This pointer
885 * is suitable for use as the \c last parameter of a later call to this
886 * function.
887 */
888exec_node *
889move_non_declarations(exec_list *instructions, exec_node *last,
890 bool make_copies, gl_shader *target)
891{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700892 hash_table *temps = NULL;
893
894 if (make_copies)
895 temps = hash_table_ctor(0, hash_table_pointer_hash,
896 hash_table_pointer_compare);
897
Ian Romanick303c99f2010-07-19 12:34:56 -0700898 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700899 ir_instruction *inst = (ir_instruction *) node;
900
Ian Romanick7e2aa912010-07-19 17:12:42 -0700901 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700902 continue;
903
Ian Romanick7e2aa912010-07-19 17:12:42 -0700904 ir_variable *var = inst->as_variable();
905 if ((var != NULL) && (var->mode != ir_var_temporary))
906 continue;
907
908 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700909 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700910 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700911 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700912
913 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700914 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700915
916 if (var != NULL)
917 hash_table_insert(temps, inst, var);
918 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700919 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700920 } else {
921 inst->remove();
922 }
923
924 last->insert_after(inst);
925 last = inst;
926 }
927
Ian Romanick7e2aa912010-07-19 17:12:42 -0700928 if (make_copies)
929 hash_table_dtor(temps);
930
Ian Romanick31a97862010-07-12 18:48:50 -0700931 return last;
932}
933
934/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700935 * Get the function signature for main from a shader
936 */
937static ir_function_signature *
938get_main_function_signature(gl_shader *sh)
939{
940 ir_function *const f = sh->symbols->get_function("main");
941 if (f != NULL) {
942 exec_list void_parameters;
943
944 /* Look for the 'void main()' signature and ensure that it's defined.
945 * This keeps the linker from accidentally pick a shader that just
946 * contains a prototype for main.
947 *
948 * We don't have to check for multiple definitions of main (in multiple
949 * shaders) because that would have already been caught above.
950 */
951 ir_function_signature *sig = f->matching_signature(&void_parameters);
952 if ((sig != NULL) && sig->is_defined) {
953 return sig;
954 }
955 }
956
957 return NULL;
958}
959
960
961/**
Brian Paul84a12732012-02-02 20:10:40 -0700962 * This class is only used in link_intrastage_shaders() below but declaring
963 * it inside that function leads to compiler warnings with some versions of
964 * gcc.
965 */
966class array_sizing_visitor : public ir_hierarchical_visitor {
967public:
968 virtual ir_visitor_status visit(ir_variable *var)
969 {
970 if (var->type->is_array() && (var->type->length == 0)) {
971 const glsl_type *type =
972 glsl_type::get_array_instance(var->type->fields.array,
973 var->max_array_access + 1);
974 assert(type != NULL);
975 var->type = type;
976 }
977 return visit_continue;
978 }
979};
980
Brian Paul84a12732012-02-02 20:10:40 -0700981/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700982 * Combine a group of shaders for a single stage to generate a linked shader
983 *
984 * \note
985 * If this function is supplied a single shader, it is cloned, and the new
986 * shader is returned.
987 */
988static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800989link_intrastage_shaders(void *mem_ctx,
990 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700991 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700992 struct gl_shader **shader_list,
993 unsigned num_shaders)
994{
Eric Anholtf609cf72012-04-27 13:52:56 -0700995 struct gl_uniform_block *uniform_blocks = NULL;
996 unsigned num_uniform_blocks = 0;
997
Ian Romanick13f782c2010-06-29 18:53:38 -0700998 /* Check that global variables defined in multiple shaders are consistent.
999 */
1000 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
1001 return NULL;
1002
Eric Anholtf609cf72012-04-27 13:52:56 -07001003 /* Check that uniform blocks between shaders for a stage agree. */
1004 for (unsigned i = 0; i < num_shaders; i++) {
1005 struct gl_shader *sh = shader_list[i];
1006
1007 for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) {
Eric Anholt8ab58422012-05-01 15:10:14 -07001008 link_assign_uniform_block_offsets(shader_list[i]);
1009
Eric Anholtf609cf72012-04-27 13:52:56 -07001010 int index = link_cross_validate_uniform_block(mem_ctx,
1011 &uniform_blocks,
1012 &num_uniform_blocks,
1013 &sh->UniformBlocks[j]);
1014 if (index == -1) {
1015 linker_error(prog, "uniform block `%s' has mismatching definitions",
1016 sh->UniformBlocks[j].Name);
1017 return NULL;
1018 }
1019 }
1020 }
1021
Ian Romanick13f782c2010-06-29 18:53:38 -07001022 /* Check that there is only a single definition of each function signature
1023 * across all shaders.
1024 */
1025 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1026 foreach_list(node, shader_list[i]->ir) {
1027 ir_function *const f = ((ir_instruction *) node)->as_function();
1028
1029 if (f == NULL)
1030 continue;
1031
1032 for (unsigned j = i + 1; j < num_shaders; j++) {
1033 ir_function *const other =
1034 shader_list[j]->symbols->get_function(f->name);
1035
1036 /* If the other shader has no function (and therefore no function
1037 * signatures) with the same name, skip to the next shader.
1038 */
1039 if (other == NULL)
1040 continue;
1041
1042 foreach_iter (exec_list_iterator, iter, *f) {
1043 ir_function_signature *sig =
1044 (ir_function_signature *) iter.get();
1045
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001046 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -07001047 continue;
1048
1049 ir_function_signature *other_sig =
1050 other->exact_matching_signature(& sig->parameters);
1051
1052 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001053 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -07001054 linker_error(prog, "function `%s' is multiply defined",
1055 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001056 return NULL;
1057 }
1058 }
1059 }
1060 }
1061 }
1062
1063 /* Find the shader that defines main, and make a clone of it.
1064 *
1065 * Starting with the clone, search for undefined references. If one is
1066 * found, find the shader that defines it. Clone the reference and add
1067 * it to the shader. Repeat until there are no undefined references or
1068 * until a reference cannot be resolved.
1069 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001070 gl_shader *main = NULL;
1071 for (unsigned i = 0; i < num_shaders; i++) {
1072 if (get_main_function_signature(shader_list[i]) != NULL) {
1073 main = shader_list[i];
1074 break;
1075 }
1076 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001077
Ian Romanick15ce87e2010-07-09 15:28:22 -07001078 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001079 linker_error(prog, "%s shader lacks `main'\n",
1080 (shader_list[0]->Type == GL_VERTEX_SHADER)
1081 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -07001082 return NULL;
1083 }
1084
Ian Romanick4a455952010-10-13 15:13:02 -07001085 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001086 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001087 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001088
Eric Anholtf609cf72012-04-27 13:52:56 -07001089 linked->UniformBlocks = uniform_blocks;
1090 linked->NumUniformBlocks = num_uniform_blocks;
1091 ralloc_steal(linked, linked->UniformBlocks);
1092
Ian Romanick15ce87e2010-07-09 15:28:22 -07001093 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001094
Ian Romanick31a97862010-07-12 18:48:50 -07001095 /* The a pointer to the main function in the final linked shader (i.e., the
1096 * copy of the original shader that contained the main function).
1097 */
1098 ir_function_signature *const main_sig = get_main_function_signature(linked);
1099
1100 /* Move any instructions other than variable declarations or function
1101 * declarations into main.
1102 */
Ian Romanick9303e352010-07-19 12:33:54 -07001103 exec_node *insertion_point =
1104 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1105 linked);
1106
Ian Romanick31a97862010-07-12 18:48:50 -07001107 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001108 if (shader_list[i] == main)
1109 continue;
1110
Ian Romanick31a97862010-07-12 18:48:50 -07001111 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001112 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001113 }
1114
Ian Romanick13f782c2010-06-29 18:53:38 -07001115 /* Resolve initializers for global variables in the linked shader.
1116 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001117 unsigned num_linking_shaders = num_shaders;
1118 for (unsigned i = 0; i < num_shaders; i++)
1119 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1120
1121 gl_shader **linking_shaders =
1122 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1123
1124 memcpy(linking_shaders, shader_list,
1125 sizeof(linking_shaders[0]) * num_shaders);
1126
1127 unsigned idx = num_shaders;
1128 for (unsigned i = 0; i < num_shaders; i++) {
1129 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1130 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1131 idx += shader_list[i]->num_builtins_to_link;
1132 }
1133
1134 assert(idx == num_linking_shaders);
1135
Ian Romanick4a455952010-10-13 15:13:02 -07001136 if (!link_function_calls(prog, linked, linking_shaders,
1137 num_linking_shaders)) {
1138 ctx->Driver.DeleteShader(ctx, linked);
1139 linked = NULL;
1140 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001141
1142 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001143
Paul Berryc148ef62011-08-03 15:37:01 -07001144#ifdef DEBUG
1145 /* At this point linked should contain all of the linked IR, so
1146 * validate it to make sure nothing went wrong.
1147 */
1148 if (linked)
1149 validate_ir_tree(linked->ir);
1150#endif
1151
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001152 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001153 * unspecified sizes have a size specified. The size is inferred from the
1154 * max_array_access field.
1155 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001156 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001157 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001158
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001159 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001160 }
1161
Ian Romanick3fb87872010-07-09 14:09:34 -07001162 return linked;
1163}
1164
Eric Anholta721abf2010-08-23 10:32:01 -07001165/**
1166 * Update the sizes of linked shader uniform arrays to the maximum
1167 * array index used.
1168 *
1169 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1170 *
1171 * If one or more elements of an array are active,
1172 * GetActiveUniform will return the name of the array in name,
1173 * subject to the restrictions listed above. The type of the array
1174 * is returned in type. The size parameter contains the highest
1175 * array element index used, plus one. The compiler or linker
1176 * determines the highest index used. There will be only one
1177 * active uniform reported by the GL per uniform array.
1178
1179 */
1180static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001181update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001182{
Ian Romanick3322fba2010-10-14 13:28:42 -07001183 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1184 if (prog->_LinkedShaders[i] == NULL)
1185 continue;
1186
Eric Anholta721abf2010-08-23 10:32:01 -07001187 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1188 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1189
Eric Anholt586b4b52010-09-28 14:32:16 -07001190 if ((var == NULL) || (var->mode != ir_var_uniform &&
1191 var->mode != ir_var_in &&
1192 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001193 !var->type->is_array())
1194 continue;
1195
Eric Anholt9feb4032012-05-01 14:43:31 -07001196 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1197 * will not be eliminated. Since we always do std140, just
1198 * don't resize arrays in UBOs.
1199 */
1200 if (var->uniform_block != -1)
1201 continue;
1202
Eric Anholta721abf2010-08-23 10:32:01 -07001203 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001204 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1205 if (prog->_LinkedShaders[j] == NULL)
1206 continue;
1207
Eric Anholta721abf2010-08-23 10:32:01 -07001208 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1209 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1210 if (!other_var)
1211 continue;
1212
1213 if (strcmp(var->name, other_var->name) == 0 &&
1214 other_var->max_array_access > size) {
1215 size = other_var->max_array_access;
1216 }
1217 }
1218 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001219
Eric Anholta721abf2010-08-23 10:32:01 -07001220 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001221 /* If this is a built-in uniform (i.e., it's backed by some
1222 * fixed-function state), adjust the number of state slots to
1223 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001224 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001225 * slots is an integer multiple of the number of array elements.
1226 * Determine the number of slots per array element by dividing by
1227 * the old (total) size.
1228 */
1229 if (var->num_state_slots > 0) {
1230 var->num_state_slots = (size + 1)
1231 * (var->num_state_slots / var->type->length);
1232 }
1233
Eric Anholta721abf2010-08-23 10:32:01 -07001234 var->type = glsl_type::get_array_instance(var->type->fields.array,
1235 size + 1);
1236 /* FINISHME: We should update the types of array
1237 * dereferences of this variable now.
1238 */
1239 }
1240 }
1241 }
1242}
1243
Ian Romanick69846702010-06-22 17:29:19 -07001244/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001245 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001246 *
1247 * \param used_mask Bits representing used (1) and unused (0) locations
1248 * \param needed_count Number of contiguous bits needed.
1249 *
1250 * \return
1251 * Base location of the available bits on success or -1 on failure.
1252 */
1253int
1254find_available_slots(unsigned used_mask, unsigned needed_count)
1255{
1256 unsigned needed_mask = (1 << needed_count) - 1;
1257 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1258
1259 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1260 * cannot optimize possibly infinite loops" for the loop below.
1261 */
1262 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1263 return -1;
1264
1265 for (int i = 0; i <= max_bit_to_test; i++) {
1266 if ((needed_mask & ~used_mask) == needed_mask)
1267 return i;
1268
1269 needed_mask <<= 1;
1270 }
1271
1272 return -1;
1273}
1274
1275
Ian Romanickd32d4f72011-06-27 17:59:58 -07001276/**
1277 * Assign locations for either VS inputs for FS outputs
1278 *
1279 * \param prog Shader program whose variables need locations assigned
1280 * \param target_index Selector for the program target to receive location
1281 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1282 * \c MESA_SHADER_FRAGMENT.
1283 * \param max_index Maximum number of generic locations. This corresponds
1284 * to either the maximum number of draw buffers or the
1285 * maximum number of generic attributes.
1286 *
1287 * \return
1288 * If locations are successfully assigned, true is returned. Otherwise an
1289 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001290 */
Ian Romanick69846702010-06-22 17:29:19 -07001291bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001292assign_attribute_or_color_locations(gl_shader_program *prog,
1293 unsigned target_index,
1294 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001295{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001296 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001297 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001298 unsigned used_locations = (max_index >= 32)
1299 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001300
Ian Romanickd32d4f72011-06-27 17:59:58 -07001301 assert((target_index == MESA_SHADER_VERTEX)
1302 || (target_index == MESA_SHADER_FRAGMENT));
1303
1304 gl_shader *const sh = prog->_LinkedShaders[target_index];
1305 if (sh == NULL)
1306 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001307
Ian Romanick69846702010-06-22 17:29:19 -07001308 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001309 *
1310 * 1. Invalidate the location assignments for all vertex shader inputs.
1311 *
1312 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001313 * glBindVertexAttribLocation) locations and outputs that have
1314 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001315 *
Ian Romanick69846702010-06-22 17:29:19 -07001316 * 3. Sort the attributes without assigned locations by number of slots
1317 * required in decreasing order. Fragmentation caused by attribute
1318 * locations assigned by the application may prevent large attributes
1319 * from having enough contiguous space.
1320 *
1321 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001322 */
1323
Ian Romanickd32d4f72011-06-27 17:59:58 -07001324 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001325 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001326
Ian Romanickd32d4f72011-06-27 17:59:58 -07001327 const enum ir_variable_mode direction =
1328 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1329
1330
Ian Romanick69846702010-06-22 17:29:19 -07001331 /* Temporary storage for the set of attributes that need locations assigned.
1332 */
1333 struct temp_attr {
1334 unsigned slots;
1335 ir_variable *var;
1336
1337 /* Used below in the call to qsort. */
1338 static int compare(const void *a, const void *b)
1339 {
1340 const temp_attr *const l = (const temp_attr *) a;
1341 const temp_attr *const r = (const temp_attr *) b;
1342
1343 /* Reversed because we want a descending order sort below. */
1344 return r->slots - l->slots;
1345 }
1346 } to_assign[16];
1347
1348 unsigned num_attr = 0;
1349
Eric Anholt16b68b12010-06-30 11:05:43 -07001350 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001351 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1352
Brian Paul4470ff22011-07-19 21:10:25 -06001353 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001354 continue;
1355
Ian Romanick68a4fc92010-10-07 17:21:22 -07001356 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001357 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001358 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001359 linker_error(prog,
1360 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001361 (var->location < 0)
1362 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001363 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001364 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001365 }
1366 } else if (target_index == MESA_SHADER_VERTEX) {
1367 unsigned binding;
1368
1369 if (prog->AttributeBindings->get(binding, var->name)) {
1370 assert(binding >= VERT_ATTRIB_GENERIC0);
1371 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001372 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001373 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001374 } else if (target_index == MESA_SHADER_FRAGMENT) {
1375 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001376 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001377
1378 if (prog->FragDataBindings->get(binding, var->name)) {
1379 assert(binding >= FRAG_RESULT_DATA0);
1380 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001381 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001382
1383 if (prog->FragDataIndexBindings->get(index, var->name)) {
1384 var->index = index;
1385 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001386 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001387 }
1388
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001389 /* If the variable is not a built-in and has a location statically
1390 * assigned in the shader (presumably via a layout qualifier), make sure
1391 * that it doesn't collide with other assigned locations. Otherwise,
1392 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001393 */
Ian Romanick523b6112011-08-17 15:40:03 -07001394 const unsigned slots = count_attribute_slots(var->type);
1395 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001396 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001397 /* From page 61 of the OpenGL 4.0 spec:
1398 *
1399 * "LinkProgram will fail if the attribute bindings assigned
1400 * by BindAttribLocation do not leave not enough space to
1401 * assign a location for an active matrix attribute or an
1402 * active attribute array, both of which require multiple
1403 * contiguous generic attributes."
1404 *
1405 * Previous versions of the spec contain similar language but omit
1406 * the bit about attribute arrays.
1407 *
1408 * Page 61 of the OpenGL 4.0 spec also says:
1409 *
1410 * "It is possible for an application to bind more than one
1411 * attribute name to the same location. This is referred to as
1412 * aliasing. This will only work if only one of the aliased
1413 * attributes is active in the executable program, or if no
1414 * path through the shader consumes more than one attribute of
1415 * a set of attributes aliased to the same location. A link
1416 * error can occur if the linker determines that every path
1417 * through the shader consumes multiple aliased attributes,
1418 * but implementations are not required to generate an error
1419 * in this case."
1420 *
1421 * These two paragraphs are either somewhat contradictory, or I
1422 * don't fully understand one or both of them.
1423 */
1424 /* FINISHME: The code as currently written does not support
1425 * FINISHME: attribute location aliasing (see comment above).
1426 */
1427 /* Mask representing the contiguous slots that will be used by
1428 * this attribute.
1429 */
1430 const unsigned attr = var->location - generic_base;
1431 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001432
Ian Romanick523b6112011-08-17 15:40:03 -07001433 /* Generate a link error if the set of bits requested for this
1434 * attribute overlaps any previously allocated bits.
1435 */
1436 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001437 const char *const string = (target_index == MESA_SHADER_VERTEX)
1438 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001439 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001440 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001441 "available for %s `%s' %d %d %d", string,
1442 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001443 return false;
1444 }
1445
1446 used_locations |= (use_mask << attr);
1447 }
1448
1449 continue;
1450 }
1451
1452 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001453 to_assign[num_attr].var = var;
1454 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001455 }
Ian Romanick69846702010-06-22 17:29:19 -07001456
1457 /* If all of the attributes were assigned locations by the application (or
1458 * are built-in attributes with fixed locations), return early. This should
1459 * be the common case.
1460 */
1461 if (num_attr == 0)
1462 return true;
1463
1464 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1465
Ian Romanickd32d4f72011-06-27 17:59:58 -07001466 if (target_index == MESA_SHADER_VERTEX) {
1467 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1468 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1469 * reserved to prevent it from being automatically allocated below.
1470 */
1471 find_deref_visitor find("gl_Vertex");
1472 find.run(sh->ir);
1473 if (find.variable_found())
1474 used_locations |= (1 << 0);
1475 }
Ian Romanick982e3792010-06-29 18:58:20 -07001476
Ian Romanick69846702010-06-22 17:29:19 -07001477 for (unsigned i = 0; i < num_attr; i++) {
1478 /* Mask representing the contiguous slots that will be used by this
1479 * attribute.
1480 */
1481 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1482
1483 int location = find_available_slots(used_locations, to_assign[i].slots);
1484
1485 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001486 const char *const string = (target_index == MESA_SHADER_VERTEX)
1487 ? "vertex shader input" : "fragment shader output";
1488
Ian Romanick586e7412011-07-28 14:04:09 -07001489 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001490 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001491 "available for %s `%s'",
1492 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001493 return false;
1494 }
1495
Ian Romanickd32d4f72011-06-27 17:59:58 -07001496 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001497 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001498 used_locations |= (use_mask << location);
1499 }
1500
1501 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001502}
1503
1504
Ian Romanick40e114b2010-08-17 14:55:50 -07001505/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001506 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001507 */
1508void
Ian Romanickcc90e622010-10-19 17:59:10 -07001509demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001510{
1511 foreach_list(node, sh->ir) {
1512 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1513
Ian Romanickcc90e622010-10-19 17:59:10 -07001514 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001515 continue;
1516
Ian Romanickcc90e622010-10-19 17:59:10 -07001517 /* A shader 'in' or 'out' variable is only really an input or output if
1518 * its value is used by other shader stages. This will cause the variable
1519 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001520 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001521 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001522 var->mode = ir_var_auto;
1523 }
1524 }
1525}
1526
1527
Paul Berry871ddb92011-11-05 11:17:32 -07001528/**
1529 * Data structure tracking information about a transform feedback declaration
1530 * during linking.
1531 */
1532class tfeedback_decl
1533{
1534public:
Paul Berry456279b2011-12-26 19:39:25 -08001535 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1536 const void *mem_ctx, const char *input);
Paul Berry871ddb92011-11-05 11:17:32 -07001537 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1538 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1539 ir_variable *output_var);
Christoph Bumillerd540af52012-01-20 13:24:46 +01001540 bool accumulate_num_outputs(struct gl_shader_program *prog, unsigned *count);
Paul Berryd3150eb2012-01-09 11:25:14 -08001541 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001542 struct gl_transform_feedback_info *info, unsigned buffer,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001543 const unsigned max_outputs) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001544
1545 /**
1546 * True if assign_location() has been called for this object.
1547 */
1548 bool is_assigned() const
1549 {
1550 return this->location != -1;
1551 }
1552
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001553 bool is_next_buffer_separator() const
1554 {
1555 return this->next_buffer_separator;
1556 }
1557
1558 bool is_varying() const
1559 {
1560 return !this->next_buffer_separator && !this->skip_components;
1561 }
1562
Paul Berry871ddb92011-11-05 11:17:32 -07001563 /**
1564 * Determine whether this object refers to the variable var.
1565 */
1566 bool matches_var(ir_variable *var) const
1567 {
Paul Berry642e5b412012-01-04 13:57:52 -08001568 if (this->is_clip_distance_mesa)
1569 return strcmp(var->name, "gl_ClipDistanceMESA") == 0;
1570 else
1571 return strcmp(var->name, this->var_name) == 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001572 }
1573
1574 /**
1575 * The total number of varying components taken up by this variable. Only
1576 * valid if is_assigned() is true.
1577 */
1578 unsigned num_components() const
1579 {
Paul Berry642e5b412012-01-04 13:57:52 -08001580 if (this->is_clip_distance_mesa)
1581 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001582 else
Paul Berry642e5b412012-01-04 13:57:52 -08001583 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001584 }
1585
1586private:
1587 /**
1588 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001589 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001590 */
1591 const char *orig_name;
1592
1593 /**
1594 * The name of the variable, parsed from orig_name.
1595 */
Paul Berry913a5c22011-12-27 08:24:57 -08001596 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001597
1598 /**
1599 * True if the declaration in orig_name represents an array.
1600 */
Paul Berry33fe0212012-01-03 20:41:34 -08001601 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001602
1603 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001604 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001605 */
Paul Berry33fe0212012-01-03 20:41:34 -08001606 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001607
1608 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001609 * True if the variable is gl_ClipDistance and the driver lowers
1610 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001611 */
Paul Berry642e5b412012-01-04 13:57:52 -08001612 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001613
1614 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001615 * The vertex shader output location that the linker assigned for this
1616 * variable. -1 if a location hasn't been assigned yet.
1617 */
1618 int location;
1619
1620 /**
1621 * If location != -1, the number of vector elements in this variable, or 1
1622 * if this variable is a scalar.
1623 */
1624 unsigned vector_elements;
1625
1626 /**
1627 * If location != -1, the number of matrix columns in this variable, or 1
1628 * if this variable is not a matrix.
1629 */
1630 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001631
1632 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1633 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001634
1635 /**
1636 * If location != -1, the size that should be returned by
1637 * glGetTransformFeedbackVarying().
1638 */
1639 unsigned size;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001640
1641 /**
1642 * How many components to skip. If non-zero, this is
1643 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1644 */
1645 unsigned skip_components;
1646
1647 /**
1648 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1649 */
1650 bool next_buffer_separator;
Paul Berry871ddb92011-11-05 11:17:32 -07001651};
1652
1653
1654/**
1655 * Initialize this object based on a string that was passed to
1656 * glTransformFeedbackVaryings. If there is a parse error, the error is
1657 * reported using linker_error(), and false is returned.
1658 */
1659bool
Paul Berry456279b2011-12-26 19:39:25 -08001660tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1661 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001662{
1663 /* We don't have to be pedantic about what is a valid GLSL variable name,
1664 * because any variable with an invalid name can't exist in the IR anyway.
1665 */
1666
1667 this->location = -1;
1668 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001669 this->is_clip_distance_mesa = false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001670 this->skip_components = 0;
1671 this->next_buffer_separator = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001672
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001673 if (ctx->Extensions.ARB_transform_feedback3) {
1674 /* Parse gl_NextBuffer. */
1675 if (strcmp(input, "gl_NextBuffer") == 0) {
1676 this->next_buffer_separator = true;
1677 return true;
1678 }
1679
1680 /* Parse gl_SkipComponents. */
1681 if (strcmp(input, "gl_SkipComponents1") == 0)
1682 this->skip_components = 1;
1683 else if (strcmp(input, "gl_SkipComponents2") == 0)
1684 this->skip_components = 2;
1685 else if (strcmp(input, "gl_SkipComponents3") == 0)
1686 this->skip_components = 3;
1687 else if (strcmp(input, "gl_SkipComponents4") == 0)
1688 this->skip_components = 4;
1689
1690 if (this->skip_components)
1691 return true;
1692 }
1693
1694 /* Parse a declaration. */
Paul Berry871ddb92011-11-05 11:17:32 -07001695 const char *bracket = strrchr(input, '[');
1696
1697 if (bracket) {
1698 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001699 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001700 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1701 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001702 }
Paul Berry33fe0212012-01-03 20:41:34 -08001703 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001704 } else {
1705 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001706 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001707 }
1708
Paul Berry642e5b412012-01-04 13:57:52 -08001709 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1710 * class must behave specially to account for the fact that gl_ClipDistance
1711 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001712 */
1713 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1714 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001715 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001716 }
1717
1718 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001719}
1720
1721
1722/**
1723 * Determine whether two tfeedback_decl objects refer to the same variable and
1724 * array index (if applicable).
1725 */
1726bool
1727tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1728{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001729 assert(x.is_varying() && y.is_varying());
1730
Paul Berry871ddb92011-11-05 11:17:32 -07001731 if (strcmp(x.var_name, y.var_name) != 0)
1732 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001733 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001734 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001735 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001736 return false;
1737 return true;
1738}
1739
1740
1741/**
1742 * Assign a location for this tfeedback_decl object based on the location
1743 * assignment in output_var.
1744 *
1745 * If an error occurs, the error is reported through linker_error() and false
1746 * is returned.
1747 */
1748bool
1749tfeedback_decl::assign_location(struct gl_context *ctx,
1750 struct gl_shader_program *prog,
1751 ir_variable *output_var)
1752{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001753 assert(this->is_varying());
1754
Paul Berry871ddb92011-11-05 11:17:32 -07001755 if (output_var->type->is_array()) {
1756 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001757 const unsigned matrix_cols =
1758 output_var->type->fields.array->matrix_columns;
Paul Berry642e5b412012-01-04 13:57:52 -08001759 unsigned actual_array_size = this->is_clip_distance_mesa ?
1760 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001761
1762 if (this->is_subscripted) {
1763 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001764 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001765 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001766 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001767 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001768 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001769 return false;
1770 }
Paul Berry642e5b412012-01-04 13:57:52 -08001771 if (this->is_clip_distance_mesa) {
1772 this->location =
1773 output_var->location + this->array_subscript / 4;
1774 } else {
1775 this->location =
1776 output_var->location + this->array_subscript * matrix_cols;
1777 }
Paul Berry33fe0212012-01-03 20:41:34 -08001778 this->size = 1;
1779 } else {
1780 this->location = output_var->location;
Paul Berry642e5b412012-01-04 13:57:52 -08001781 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001782 }
Paul Berry871ddb92011-11-05 11:17:32 -07001783 this->vector_elements = output_var->type->fields.array->vector_elements;
1784 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001785 if (this->is_clip_distance_mesa)
1786 this->type = GL_FLOAT;
1787 else
1788 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001789 } else {
1790 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001791 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001792 linker_error(prog, "Transform feedback varying %s requested, "
1793 "but %s is not an array.",
1794 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001795 return false;
1796 }
1797 this->location = output_var->location;
Paul Berry33fe0212012-01-03 20:41:34 -08001798 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001799 this->vector_elements = output_var->type->vector_elements;
1800 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001801 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001802 }
Paul Berry642e5b412012-01-04 13:57:52 -08001803
Paul Berry871ddb92011-11-05 11:17:32 -07001804 /* From GL_EXT_transform_feedback:
1805 * A program will fail to link if:
1806 *
1807 * * the total number of components to capture in any varying
1808 * variable in <varyings> is greater than the constant
1809 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1810 * buffer mode is SEPARATE_ATTRIBS_EXT;
1811 */
1812 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1813 this->num_components() >
1814 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1815 linker_error(prog, "Transform feedback varying %s exceeds "
1816 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1817 this->orig_name);
1818 return false;
1819 }
1820
1821 return true;
1822}
1823
1824
Paul Berry871ddb92011-11-05 11:17:32 -07001825bool
Christoph Bumillerd540af52012-01-20 13:24:46 +01001826tfeedback_decl::accumulate_num_outputs(struct gl_shader_program *prog,
1827 unsigned *count)
Paul Berry871ddb92011-11-05 11:17:32 -07001828{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001829 if (!this->is_varying()) {
1830 return true;
1831 }
1832
Paul Berry871ddb92011-11-05 11:17:32 -07001833 if (!this->is_assigned()) {
1834 /* From GL_EXT_transform_feedback:
1835 * A program will fail to link if:
1836 *
1837 * * any variable name specified in the <varyings> array is not
1838 * declared as an output in the geometry shader (if present) or
1839 * the vertex shader (if no geometry shader is present);
1840 */
1841 linker_error(prog, "Transform feedback varying %s undeclared.",
1842 this->orig_name);
1843 return false;
1844 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001845
Christoph Bumillerd540af52012-01-20 13:24:46 +01001846 unsigned translated_size = this->size;
1847 if (this->is_clip_distance_mesa)
1848 translated_size = (translated_size + 3) / 4;
1849
1850 *count += translated_size * this->matrix_columns;
1851
1852 return true;
1853}
1854
1855
1856/**
1857 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1858 *
1859 * If an error occurs, the error is reported through linker_error() and false
1860 * is returned.
1861 */
1862bool
1863tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1864 struct gl_transform_feedback_info *info,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001865 unsigned buffer, const unsigned max_outputs) const
Christoph Bumillerd540af52012-01-20 13:24:46 +01001866{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001867 assert(!this->next_buffer_separator);
1868
1869 /* Handle gl_SkipComponents. */
1870 if (this->skip_components) {
1871 info->BufferStride[buffer] += this->skip_components;
1872 return true;
1873 }
1874
Paul Berryd3150eb2012-01-09 11:25:14 -08001875 /* From GL_EXT_transform_feedback:
1876 * A program will fail to link if:
1877 *
1878 * * the total number of components to capture is greater than
1879 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1880 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1881 */
1882 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1883 info->BufferStride[buffer] + this->num_components() >
1884 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1885 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1886 "limit has been exceeded.");
1887 return false;
1888 }
1889
Paul Berry642e5b412012-01-04 13:57:52 -08001890 unsigned translated_size = this->size;
1891 if (this->is_clip_distance_mesa)
1892 translated_size = (translated_size + 3) / 4;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001893 unsigned components_so_far = 0;
Paul Berry642e5b412012-01-04 13:57:52 -08001894 for (unsigned index = 0; index < translated_size; ++index) {
Paul Berry33fe0212012-01-03 20:41:34 -08001895 for (unsigned v = 0; v < this->matrix_columns; ++v) {
Paul Berry642e5b412012-01-04 13:57:52 -08001896 unsigned num_components = this->vector_elements;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001897 assert(info->NumOutputs < max_outputs);
Paul Berry642e5b412012-01-04 13:57:52 -08001898 info->Outputs[info->NumOutputs].ComponentOffset = 0;
1899 if (this->is_clip_distance_mesa) {
1900 if (this->is_subscripted) {
1901 num_components = 1;
1902 info->Outputs[info->NumOutputs].ComponentOffset =
1903 this->array_subscript % 4;
1904 } else {
1905 num_components = MIN2(4, this->size - components_so_far);
1906 }
1907 }
Paul Berry33fe0212012-01-03 20:41:34 -08001908 info->Outputs[info->NumOutputs].OutputRegister =
1909 this->location + v + index * this->matrix_columns;
1910 info->Outputs[info->NumOutputs].NumComponents = num_components;
1911 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1912 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
Paul Berry33fe0212012-01-03 20:41:34 -08001913 ++info->NumOutputs;
1914 info->BufferStride[buffer] += num_components;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001915 components_so_far += num_components;
Paul Berry33fe0212012-01-03 20:41:34 -08001916 }
Paul Berry871ddb92011-11-05 11:17:32 -07001917 }
Paul Berrybe4e9f72012-01-04 12:21:55 -08001918 assert(components_so_far == this->num_components());
Eric Anholt9d36c962012-01-02 17:08:13 -08001919
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001920 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1921 info->Varyings[info->NumVarying].Type = this->type;
1922 info->Varyings[info->NumVarying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001923 info->NumVarying++;
1924
Paul Berry871ddb92011-11-05 11:17:32 -07001925 return true;
1926}
1927
1928
1929/**
1930 * Parse all the transform feedback declarations that were passed to
1931 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1932 *
1933 * If an error occurs, the error is reported through linker_error() and false
1934 * is returned.
1935 */
1936static bool
Paul Berry456279b2011-12-26 19:39:25 -08001937parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1938 const void *mem_ctx, unsigned num_names,
1939 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001940{
1941 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001942 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001943 return false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001944
1945 if (!decls[i].is_varying())
1946 continue;
1947
Paul Berry871ddb92011-11-05 11:17:32 -07001948 /* From GL_EXT_transform_feedback:
1949 * A program will fail to link if:
1950 *
1951 * * any two entries in the <varyings> array specify the same varying
1952 * variable;
1953 *
1954 * We interpret this to mean "any two entries in the <varyings> array
1955 * specify the same varying variable and array index", since transform
1956 * feedback of arrays would be useless otherwise.
1957 */
1958 for (unsigned j = 0; j < i; ++j) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001959 if (!decls[j].is_varying())
1960 continue;
1961
Paul Berry871ddb92011-11-05 11:17:32 -07001962 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1963 linker_error(prog, "Transform feedback varying %s specified "
1964 "more than once.", varying_names[i]);
1965 return false;
1966 }
1967 }
1968 }
1969 return true;
1970}
1971
1972
1973/**
1974 * Assign a location for a variable that is produced in one pipeline stage
1975 * (the "producer") and consumed in the next stage (the "consumer").
1976 *
1977 * \param input_var is the input variable declaration in the consumer.
1978 *
1979 * \param output_var is the output variable declaration in the producer.
1980 *
1981 * \param input_index is the counter that keeps track of assigned input
1982 * locations in the consumer.
1983 *
1984 * \param output_index is the counter that keeps track of assigned output
1985 * locations in the producer.
1986 *
1987 * It is permissible for \c input_var to be NULL (this happens if a variable
1988 * is output by the producer and consumed by transform feedback, but not
1989 * consumed by the consumer).
1990 *
1991 * If the variable has already been assigned a location, this function has no
1992 * effect.
1993 */
1994void
1995assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1996 unsigned *input_index, unsigned *output_index)
1997{
Paul Berry3c9c17d2012-12-04 15:17:01 -08001998 if (!output_var->is_unmatched_generic_inout) {
Paul Berry871ddb92011-11-05 11:17:32 -07001999 /* Location already assigned. */
2000 return;
2001 }
2002
2003 if (input_var) {
2004 assert(input_var->location == -1);
2005 input_var->location = *input_index;
Paul Berry3c9c17d2012-12-04 15:17:01 -08002006 input_var->is_unmatched_generic_inout = 0;
Paul Berry871ddb92011-11-05 11:17:32 -07002007 }
2008
2009 output_var->location = *output_index;
Paul Berry3c9c17d2012-12-04 15:17:01 -08002010 output_var->is_unmatched_generic_inout = 0;
Paul Berry871ddb92011-11-05 11:17:32 -07002011
2012 /* FINISHME: Support for "varying" records in GLSL 1.50. */
2013 assert(!output_var->type->is_record());
2014
2015 if (output_var->type->is_array()) {
2016 const unsigned slots = output_var->type->length
2017 * output_var->type->fields.array->matrix_columns;
2018
2019 *output_index += slots;
2020 *input_index += slots;
2021 } else {
2022 const unsigned slots = output_var->type->matrix_columns;
2023
2024 *output_index += slots;
2025 *input_index += slots;
2026 }
2027}
2028
2029
2030/**
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002031 * Is the given variable a varying variable to be counted against the
2032 * limit in ctx->Const.MaxVarying?
2033 * This includes variables such as texcoords, colors and generic
2034 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2035 */
2036static bool
2037is_varying_var(GLenum shaderType, const ir_variable *var)
2038{
2039 /* Only fragment shaders will take a varying variable as an input */
2040 if (shaderType == GL_FRAGMENT_SHADER &&
Eric Anholt94e82b22012-11-13 14:40:22 -08002041 var->mode == ir_var_in) {
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002042 switch (var->location) {
2043 case FRAG_ATTRIB_WPOS:
2044 case FRAG_ATTRIB_FACE:
2045 case FRAG_ATTRIB_PNTC:
2046 return false;
2047 default:
2048 return true;
2049 }
2050 }
2051 return false;
2052}
2053
2054
2055/**
Paul Berry871ddb92011-11-05 11:17:32 -07002056 * Assign locations for all variables that are produced in one pipeline stage
2057 * (the "producer") and consumed in the next stage (the "consumer").
2058 *
2059 * Variables produced by the producer may also be consumed by transform
2060 * feedback.
2061 *
2062 * \param num_tfeedback_decls is the number of declarations indicating
2063 * variables that may be consumed by transform feedback.
2064 *
2065 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2066 * representing the result of parsing the strings passed to
2067 * glTransformFeedbackVaryings(). assign_location() will be called for
2068 * each of these objects that matches one of the outputs of the
2069 * producer.
2070 *
2071 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2072 * be NULL. In this case, varying locations are assigned solely based on the
2073 * requirements of transform feedback.
2074 */
Ian Romanickde773242011-06-09 13:31:32 -07002075bool
2076assign_varying_locations(struct gl_context *ctx,
2077 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07002078 gl_shader *producer, gl_shader *consumer,
2079 unsigned num_tfeedback_decls,
2080 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07002081{
2082 /* FINISHME: Set dynamically when geometry shader support is added. */
2083 unsigned output_index = VERT_RESULT_VAR0;
2084 unsigned input_index = FRAG_ATTRIB_VAR0;
2085
2086 /* Operate in a total of three passes.
2087 *
2088 * 1. Assign locations for any matching inputs and outputs.
2089 *
2090 * 2. Mark output variables in the producer that do not have locations as
2091 * not being outputs. This lets the optimizer eliminate them.
2092 *
2093 * 3. Mark input variables in the consumer that do not have locations as
2094 * not being inputs. This lets the optimizer eliminate them.
2095 */
2096
Eric Anholt16b68b12010-06-30 11:05:43 -07002097 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07002098 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2099
Paul Berry871ddb92011-11-05 11:17:32 -07002100 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07002101 continue;
2102
Paul Berry871ddb92011-11-05 11:17:32 -07002103 ir_variable *input_var =
2104 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002105
Paul Berry871ddb92011-11-05 11:17:32 -07002106 if (input_var && input_var->mode != ir_var_in)
2107 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002108
Paul Berry871ddb92011-11-05 11:17:32 -07002109 if (input_var) {
2110 assign_varying_location(input_var, output_var, &input_index,
2111 &output_index);
2112 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002113
Paul Berry871ddb92011-11-05 11:17:32 -07002114 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002115 if (!tfeedback_decls[i].is_varying())
2116 continue;
2117
Paul Berry871ddb92011-11-05 11:17:32 -07002118 if (!tfeedback_decls[i].is_assigned() &&
2119 tfeedback_decls[i].matches_var(output_var)) {
Paul Berry3c9c17d2012-12-04 15:17:01 -08002120 if (output_var->is_unmatched_generic_inout) {
Paul Berry871ddb92011-11-05 11:17:32 -07002121 assign_varying_location(input_var, output_var, &input_index,
2122 &output_index);
2123 }
2124 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2125 return false;
2126 }
Ian Romanickdf869d92010-08-30 15:37:44 -07002127 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002128 }
2129
Ian Romanickde773242011-06-09 13:31:32 -07002130 unsigned varying_vectors = 0;
2131
Paul Berry871ddb92011-11-05 11:17:32 -07002132 if (consumer) {
2133 foreach_list(node, consumer->ir) {
2134 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07002135
Paul Berry871ddb92011-11-05 11:17:32 -07002136 if ((var == NULL) || (var->mode != ir_var_in))
2137 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07002138
Paul Berry3c9c17d2012-12-04 15:17:01 -08002139 if (var->is_unmatched_generic_inout) {
Paul Berry871ddb92011-11-05 11:17:32 -07002140 if (prog->Version <= 120) {
2141 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2142 *
2143 * Only those varying variables used (i.e. read) in
2144 * the fragment shader executable must be written to
2145 * by the vertex shader executable; declaring
2146 * superfluous varying variables in a vertex shader is
2147 * permissible.
2148 *
2149 * We interpret this text as meaning that the VS must
2150 * write the variable for the FS to read it. See
2151 * "glsl1-varying read but not written" in piglit.
2152 */
Eric Anholtb7062832010-07-28 13:52:23 -07002153
Paul Berry871ddb92011-11-05 11:17:32 -07002154 linker_error(prog, "fragment shader varying %s not written "
2155 "by vertex shader\n.", var->name);
2156 }
Eric Anholtb7062832010-07-28 13:52:23 -07002157
Paul Berry871ddb92011-11-05 11:17:32 -07002158 /* An 'in' variable is only really a shader input if its
2159 * value is written by the previous stage.
2160 */
2161 var->mode = ir_var_auto;
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002162 } else if (is_varying_var(consumer->Type, var)) {
Paul Berry871ddb92011-11-05 11:17:32 -07002163 /* The packing rules are used for vertex shader inputs are also
2164 * used for fragment shader inputs.
2165 */
2166 varying_vectors += count_attribute_slots(var->type);
2167 }
Eric Anholtb7062832010-07-28 13:52:23 -07002168 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002169 }
Ian Romanickde773242011-06-09 13:31:32 -07002170
Paul Berry15ba2a52012-08-02 17:51:02 -07002171 if (ctx->API == API_OPENGLES2 || prog->IsES) {
Ian Romanickde773242011-06-09 13:31:32 -07002172 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002173 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2174 linker_warning(prog, "shader uses too many varying vectors "
2175 "(%u > %u), but the driver will try to optimize "
2176 "them out; this is non-portable out-of-spec "
2177 "behavior\n",
2178 varying_vectors, ctx->Const.MaxVarying);
2179 } else {
2180 linker_error(prog, "shader uses too many varying vectors "
2181 "(%u > %u)\n",
2182 varying_vectors, ctx->Const.MaxVarying);
2183 return false;
2184 }
Ian Romanickde773242011-06-09 13:31:32 -07002185 }
2186 } else {
2187 const unsigned float_components = varying_vectors * 4;
2188 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002189 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2190 linker_warning(prog, "shader uses too many varying components "
2191 "(%u > %u), but the driver will try to optimize "
2192 "them out; this is non-portable out-of-spec "
2193 "behavior\n",
2194 float_components, ctx->Const.MaxVarying * 4);
2195 } else {
2196 linker_error(prog, "shader uses too many varying components "
2197 "(%u > %u)\n",
2198 float_components, ctx->Const.MaxVarying * 4);
2199 return false;
2200 }
Ian Romanickde773242011-06-09 13:31:32 -07002201 }
2202 }
2203
2204 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07002205}
2206
2207
Paul Berry871ddb92011-11-05 11:17:32 -07002208/**
2209 * Store transform feedback location assignments into
2210 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2211 *
2212 * If an error occurs, the error is reported through linker_error() and false
2213 * is returned.
2214 */
2215static bool
2216store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2217 unsigned num_tfeedback_decls,
2218 tfeedback_decl *tfeedback_decls)
2219{
Paul Berryebfad9f2011-12-29 15:55:01 -08002220 bool separate_attribs_mode =
2221 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08002222
2223 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01002224 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08002225
2226 memset(&prog->LinkedTransformFeedback, 0,
2227 sizeof(prog->LinkedTransformFeedback));
2228
2229 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08002230 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08002231 struct gl_transform_feedback_varying_info,
2232 num_tfeedback_decls);
2233
Christoph Bumillerd540af52012-01-20 13:24:46 +01002234 unsigned num_outputs = 0;
2235 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
2236 if (!tfeedback_decls[i].accumulate_num_outputs(prog, &num_outputs))
2237 return false;
2238
2239 prog->LinkedTransformFeedback.Outputs =
2240 rzalloc_array(prog,
2241 struct gl_transform_feedback_output,
2242 num_outputs);
2243
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002244 unsigned num_buffers = 0;
2245
2246 if (separate_attribs_mode) {
2247 /* GL_SEPARATE_ATTRIBS */
2248 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2249 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2250 num_buffers, num_outputs))
2251 return false;
2252
2253 num_buffers++;
2254 }
Paul Berry871ddb92011-11-05 11:17:32 -07002255 }
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002256 else {
2257 /* GL_INVERLEAVED_ATTRIBS */
2258 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2259 if (tfeedback_decls[i].is_next_buffer_separator()) {
2260 num_buffers++;
2261 continue;
2262 }
2263
2264 if (!tfeedback_decls[i].store(ctx, prog,
2265 &prog->LinkedTransformFeedback,
2266 num_buffers, num_outputs))
2267 return false;
2268 }
2269 num_buffers++;
2270 }
2271
Christoph Bumillerd540af52012-01-20 13:24:46 +01002272 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002273
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002274 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
Paul Berry871ddb92011-11-05 11:17:32 -07002275 return true;
2276}
2277
Ian Romanick92f81592011-11-08 12:37:19 -08002278/**
Marek Olšákec174a42011-11-18 15:00:10 +01002279 * Store the gl_FragDepth layout in the gl_shader_program struct.
2280 */
2281static void
2282store_fragdepth_layout(struct gl_shader_program *prog)
2283{
2284 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2285 return;
2286 }
2287
2288 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2289
2290 /* We don't look up the gl_FragDepth symbol directly because if
2291 * gl_FragDepth is not used in the shader, it's removed from the IR.
2292 * However, the symbol won't be removed from the symbol table.
2293 *
2294 * We're only interested in the cases where the variable is NOT removed
2295 * from the IR.
2296 */
2297 foreach_list(node, ir) {
2298 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2299
2300 if (var == NULL || var->mode != ir_var_out) {
2301 continue;
2302 }
2303
2304 if (strcmp(var->name, "gl_FragDepth") == 0) {
2305 switch (var->depth_layout) {
2306 case ir_depth_layout_none:
2307 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2308 return;
2309 case ir_depth_layout_any:
2310 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2311 return;
2312 case ir_depth_layout_greater:
2313 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2314 return;
2315 case ir_depth_layout_less:
2316 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2317 return;
2318 case ir_depth_layout_unchanged:
2319 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2320 return;
2321 default:
2322 assert(0);
2323 return;
2324 }
2325 }
2326 }
2327}
2328
2329/**
Ian Romanick92f81592011-11-08 12:37:19 -08002330 * Validate the resources used by a program versus the implementation limits
2331 */
2332static bool
2333check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2334{
2335 static const char *const shader_names[MESA_SHADER_TYPES] = {
2336 "vertex", "fragment", "geometry"
2337 };
2338
2339 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2340 ctx->Const.MaxVertexTextureImageUnits,
2341 ctx->Const.MaxTextureImageUnits,
2342 ctx->Const.MaxGeometryTextureImageUnits
2343 };
2344
2345 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2346 ctx->Const.VertexProgram.MaxUniformComponents,
2347 ctx->Const.FragmentProgram.MaxUniformComponents,
2348 0 /* FINISHME: Geometry shaders. */
2349 };
2350
Eric Anholt877a8972012-06-25 12:47:01 -07002351 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
2352 ctx->Const.VertexProgram.MaxUniformBlocks,
2353 ctx->Const.FragmentProgram.MaxUniformBlocks,
2354 ctx->Const.GeometryProgram.MaxUniformBlocks,
2355 };
2356
Ian Romanick92f81592011-11-08 12:37:19 -08002357 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2358 struct gl_shader *sh = prog->_LinkedShaders[i];
2359
2360 if (sh == NULL)
2361 continue;
2362
2363 if (sh->num_samplers > max_samplers[i]) {
2364 linker_error(prog, "Too many %s shader texture samplers",
2365 shader_names[i]);
2366 }
2367
2368 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002369 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2370 linker_warning(prog, "Too many %s shader uniform components, "
2371 "but the driver will try to optimize them out; "
2372 "this is non-portable out-of-spec behavior\n",
2373 shader_names[i]);
2374 } else {
2375 linker_error(prog, "Too many %s shader uniform components",
2376 shader_names[i]);
2377 }
Ian Romanick92f81592011-11-08 12:37:19 -08002378 }
2379 }
2380
Eric Anholt877a8972012-06-25 12:47:01 -07002381 unsigned blocks[MESA_SHADER_TYPES] = {0};
2382 unsigned total_uniform_blocks = 0;
2383
2384 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
2385 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
2386 if (prog->UniformBlockStageIndex[j][i] != -1) {
2387 blocks[j]++;
2388 total_uniform_blocks++;
2389 }
2390 }
2391
2392 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
2393 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
2394 prog->NumUniformBlocks,
2395 ctx->Const.MaxCombinedUniformBlocks);
2396 } else {
2397 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2398 if (blocks[i] > max_uniform_blocks[i]) {
2399 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
2400 shader_names[i],
2401 blocks[i],
2402 max_uniform_blocks[i]);
2403 break;
2404 }
2405 }
2406 }
2407 }
2408
Ian Romanick92f81592011-11-08 12:37:19 -08002409 return prog->LinkStatus;
2410}
Paul Berry871ddb92011-11-05 11:17:32 -07002411
Ian Romanick0e59b262010-06-23 11:23:01 -07002412void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002413link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002414{
Paul Berry871ddb92011-11-05 11:17:32 -07002415 tfeedback_decl *tfeedback_decls = NULL;
2416 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2417
Kenneth Graunked3073f52011-01-21 14:32:31 -08002418 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002419
Ian Romanick832dfa52010-06-17 15:04:20 -07002420 prog->LinkStatus = false;
2421 prog->Validated = false;
2422 prog->_Used = false;
2423
Eric Anholtf609cf72012-04-27 13:52:56 -07002424 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08002425 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002426
Eric Anholtf609cf72012-04-27 13:52:56 -07002427 ralloc_free(prog->UniformBlocks);
2428 prog->UniformBlocks = NULL;
2429 prog->NumUniformBlocks = 0;
2430 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2431 ralloc_free(prog->UniformBlockStageIndex[i]);
2432 prog->UniformBlockStageIndex[i] = NULL;
2433 }
2434
Ian Romanick832dfa52010-06-17 15:04:20 -07002435 /* Separate the shaders into groups based on their type.
2436 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002437 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002438 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002439 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002440 unsigned num_frag_shaders = 0;
2441
Eric Anholt16b68b12010-06-30 11:05:43 -07002442 vert_shader_list = (struct gl_shader **)
2443 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002444 frag_shader_list = &vert_shader_list[prog->NumShaders];
2445
Ian Romanick25f51d32010-07-16 15:51:50 -07002446 unsigned min_version = UINT_MAX;
2447 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002448 const bool is_es_prog =
2449 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002450 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002451 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2452 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2453
Paul Berrya9f34dc2012-08-02 17:49:44 -07002454 if (prog->Shaders[i]->IsES != is_es_prog) {
2455 linker_error(prog, "all shaders must use same shading "
2456 "language version\n");
2457 goto done;
2458 }
2459
Ian Romanick832dfa52010-06-17 15:04:20 -07002460 switch (prog->Shaders[i]->Type) {
2461 case GL_VERTEX_SHADER:
2462 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2463 num_vert_shaders++;
2464 break;
2465 case GL_FRAGMENT_SHADER:
2466 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2467 num_frag_shaders++;
2468 break;
2469 case GL_GEOMETRY_SHADER:
2470 /* FINISHME: Support geometry shaders. */
2471 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2472 break;
2473 }
2474 }
2475
Ian Romanick25f51d32010-07-16 15:51:50 -07002476 /* Previous to GLSL version 1.30, different compilation units could mix and
2477 * match shading language versions. With GLSL 1.30 and later, the versions
2478 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07002479 *
2480 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07002481 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07002482 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002483 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002484 linker_error(prog, "all shaders must use same shading "
2485 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002486 goto done;
2487 }
2488
2489 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002490 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002491
Ian Romanick3322fba2010-10-14 13:28:42 -07002492 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2493 if (prog->_LinkedShaders[i] != NULL)
2494 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2495
2496 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002497 }
2498
Ian Romanickcd6764e2010-07-16 16:00:07 -07002499 /* Link all shaders for a particular stage and validate the result.
2500 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002501 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002502 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002503 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2504 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002505
2506 if (sh == NULL)
2507 goto done;
2508
2509 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002510 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002511
Ian Romanick3322fba2010-10-14 13:28:42 -07002512 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2513 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002514 }
2515
2516 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002517 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002518 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2519 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002520
2521 if (sh == NULL)
2522 goto done;
2523
2524 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002525 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002526
Ian Romanick3322fba2010-10-14 13:28:42 -07002527 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2528 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002529 }
2530
Ian Romanick3ed850e2010-06-23 12:18:21 -07002531 /* Here begins the inter-stage linking phase. Some initial validation is
2532 * performed, then locations are assigned for uniforms, attributes, and
2533 * varyings.
2534 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002535 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002536 unsigned prev;
2537
2538 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2539 if (prog->_LinkedShaders[prev] != NULL)
2540 break;
2541 }
2542
Bryan Cainf18a0862011-04-23 19:29:15 -05002543 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002544 * stage.
2545 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002546 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2547 if (prog->_LinkedShaders[i] == NULL)
2548 continue;
2549
Ian Romanickf36460e2010-06-23 12:07:22 -07002550 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002551 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002552 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002553 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002554
2555 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002556 }
2557
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002558 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002559 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002560
Eric Anholt3de13952012-05-04 13:08:46 -07002561 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2562 * it before optimization because we want most of the checks to get
2563 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002564 *
2565 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002566 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002567 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002568 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2569 if (sh) {
2570 lower_discard_flow(sh->ir);
2571 }
2572 }
2573
Eric Anholtf609cf72012-04-27 13:52:56 -07002574 if (!interstage_cross_validate_uniform_blocks(prog))
2575 goto done;
2576
Eric Anholt2f4fe152010-08-10 13:06:49 -07002577 /* Do common optimization before assigning storage for attributes,
2578 * uniforms, and varyings. Later optimization could possibly make
2579 * some of that unused.
2580 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002581 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2582 if (prog->_LinkedShaders[i] == NULL)
2583 continue;
2584
Ian Romanick02c5ae12011-07-11 10:46:01 -07002585 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2586 if (!prog->LinkStatus)
2587 goto done;
2588
Paul Berry18392442012-12-04 11:11:02 -08002589 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2590 lower_clip_distance(prog->_LinkedShaders[i]);
2591 }
Paul Berryc06e3252011-08-11 20:58:21 -07002592
Brian Paul7feabfe2012-03-20 17:43:12 -06002593 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2594
2595 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002596 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002597 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002598
Paul Berry50895d42012-12-05 07:17:07 -08002599 /* Mark all generic shader inputs and outputs as unpaired. */
2600 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2601 link_invalidate_variable_locations(
2602 prog->_LinkedShaders[MESA_SHADER_VERTEX],
2603 VERT_ATTRIB_GENERIC0, VERT_RESULT_VAR0);
2604 }
2605 /* FINISHME: Geometry shaders not implemented yet */
2606 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2607 link_invalidate_variable_locations(
2608 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2609 FRAG_ATTRIB_VAR0, FRAG_RESULT_DATA0);
2610 }
2611
Ian Romanickd32d4f72011-06-27 17:59:58 -07002612 /* FINISHME: The value of the max_attribute_index parameter is
2613 * FINISHME: implementation dependent based on the value of
2614 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2615 * FINISHME: at least 16, so hardcode 16 for now.
2616 */
2617 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002618 goto done;
2619 }
2620
Dave Airlie1256a5d2012-03-24 13:33:41 +00002621 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002622 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002623 }
2624
Ian Romanick3322fba2010-10-14 13:28:42 -07002625 unsigned prev;
2626 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2627 if (prog->_LinkedShaders[prev] != NULL)
2628 break;
2629 }
2630
Paul Berry871ddb92011-11-05 11:17:32 -07002631 if (num_tfeedback_decls != 0) {
2632 /* From GL_EXT_transform_feedback:
2633 * A program will fail to link if:
2634 *
2635 * * the <count> specified by TransformFeedbackVaryingsEXT is
2636 * non-zero, but the program object has no vertex or geometry
2637 * shader;
2638 */
2639 if (prev >= MESA_SHADER_FRAGMENT) {
2640 linker_error(prog, "Transform feedback varyings specified, but "
2641 "no vertex or geometry shader is present.");
2642 goto done;
2643 }
2644
2645 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2646 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002647 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002648 prog->TransformFeedback.VaryingNames,
2649 tfeedback_decls))
2650 goto done;
2651 }
2652
Ian Romanick3322fba2010-10-14 13:28:42 -07002653 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2654 if (prog->_LinkedShaders[i] == NULL)
2655 continue;
2656
Paul Berry871ddb92011-11-05 11:17:32 -07002657 if (!assign_varying_locations(
2658 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2659 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2660 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002661 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002662
Ian Romanick3322fba2010-10-14 13:28:42 -07002663 prev = i;
2664 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002665
Paul Berry871ddb92011-11-05 11:17:32 -07002666 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2667 /* There was no fragment shader, but we still have to assign varying
2668 * locations for use by transform feedback.
2669 */
2670 if (!assign_varying_locations(
2671 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2672 tfeedback_decls))
2673 goto done;
2674 }
2675
2676 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2677 goto done;
2678
Ian Romanickcc90e622010-10-19 17:59:10 -07002679 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2680 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2681 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002682
2683 /* Eliminate code that is now dead due to unused vertex outputs being
2684 * demoted.
2685 */
2686 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2687 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002688 }
2689
2690 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2691 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2692
2693 demote_shader_inputs_and_outputs(sh, ir_var_in);
2694 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2695 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002696
2697 /* Eliminate code that is now dead due to unused geometry outputs being
2698 * demoted.
2699 */
2700 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2701 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002702 }
2703
2704 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2705 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2706
2707 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002708
2709 /* Eliminate code that is now dead due to unused fragment inputs being
2710 * demoted. This shouldn't actually do anything other than remove
2711 * declarations of the (now unused) global variables.
2712 */
2713 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2714 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002715 }
2716
Ian Romanick960d7222011-10-21 11:21:02 -07002717 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002718 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002719 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002720
Ian Romanick92f81592011-11-08 12:37:19 -08002721 if (!check_resources(ctx, prog))
2722 goto done;
2723
Ian Romanickce9171f2011-02-03 17:10:14 -08002724 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002725 * present in a linked program. By checking prog->IsES, we also
2726 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002727 */
Eric Anholt57f79782011-07-22 12:57:47 -07002728 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002729 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002730 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002731 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002732 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002733 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002734 }
2735 }
2736
Ian Romanick13e10e42010-06-21 12:03:24 -07002737 /* FINISHME: Assign fragment shader output locations. */
2738
Ian Romanick832dfa52010-06-17 15:04:20 -07002739done:
2740 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002741
2742 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2743 if (prog->_LinkedShaders[i] == NULL)
2744 continue;
2745
2746 /* Retain any live IR, but trash the rest. */
2747 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002748
2749 /* The symbol table in the linked shaders may contain references to
2750 * variables that were removed (e.g., unused uniforms). Since it may
2751 * contain junk, there is no possible valid use. Delete it and set the
2752 * pointer to NULL.
2753 */
2754 delete prog->_LinkedShaders[i]->symbols;
2755 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002756 }
2757
Kenneth Graunked3073f52011-01-21 14:32:31 -08002758 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002759}