blob: e4d31d79d69223bf3a1dd15528caafa2cb77dac1 [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);
Paul Berry25ed3be2012-12-04 10:34:45 -08001540 unsigned get_num_outputs() const;
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 Berry25ed3be2012-12-04 10:34:45 -08001544 ir_variable *find_output_var(gl_shader_program *prog,
1545 gl_shader *producer) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001546
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001547 bool is_next_buffer_separator() const
1548 {
1549 return this->next_buffer_separator;
1550 }
1551
1552 bool is_varying() const
1553 {
1554 return !this->next_buffer_separator && !this->skip_components;
1555 }
1556
Paul Berry871ddb92011-11-05 11:17:32 -07001557 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001558 * The total number of varying components taken up by this variable. Only
Paul Berry25ed3be2012-12-04 10:34:45 -08001559 * valid if assign_location() has been called.
Paul Berry871ddb92011-11-05 11:17:32 -07001560 */
1561 unsigned num_components() const
1562 {
Paul Berry642e5b412012-01-04 13:57:52 -08001563 if (this->is_clip_distance_mesa)
1564 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001565 else
Paul Berry642e5b412012-01-04 13:57:52 -08001566 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001567 }
1568
1569private:
1570 /**
1571 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001572 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001573 */
1574 const char *orig_name;
1575
1576 /**
1577 * The name of the variable, parsed from orig_name.
1578 */
Paul Berry913a5c22011-12-27 08:24:57 -08001579 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001580
1581 /**
1582 * True if the declaration in orig_name represents an array.
1583 */
Paul Berry33fe0212012-01-03 20:41:34 -08001584 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001585
1586 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001587 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001588 */
Paul Berry33fe0212012-01-03 20:41:34 -08001589 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001590
1591 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001592 * True if the variable is gl_ClipDistance and the driver lowers
1593 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001594 */
Paul Berry642e5b412012-01-04 13:57:52 -08001595 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001596
1597 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001598 * The vertex shader output location that the linker assigned for this
1599 * variable. -1 if a location hasn't been assigned yet.
1600 */
1601 int location;
1602
1603 /**
1604 * If location != -1, the number of vector elements in this variable, or 1
1605 * if this variable is a scalar.
1606 */
1607 unsigned vector_elements;
1608
1609 /**
1610 * If location != -1, the number of matrix columns in this variable, or 1
1611 * if this variable is not a matrix.
1612 */
1613 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001614
1615 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1616 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001617
1618 /**
1619 * If location != -1, the size that should be returned by
1620 * glGetTransformFeedbackVarying().
1621 */
1622 unsigned size;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001623
1624 /**
1625 * How many components to skip. If non-zero, this is
1626 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1627 */
1628 unsigned skip_components;
1629
1630 /**
1631 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1632 */
1633 bool next_buffer_separator;
Paul Berry871ddb92011-11-05 11:17:32 -07001634};
1635
1636
1637/**
1638 * Initialize this object based on a string that was passed to
1639 * glTransformFeedbackVaryings. If there is a parse error, the error is
1640 * reported using linker_error(), and false is returned.
1641 */
1642bool
Paul Berry456279b2011-12-26 19:39:25 -08001643tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1644 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001645{
1646 /* We don't have to be pedantic about what is a valid GLSL variable name,
1647 * because any variable with an invalid name can't exist in the IR anyway.
1648 */
1649
1650 this->location = -1;
1651 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001652 this->is_clip_distance_mesa = false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001653 this->skip_components = 0;
1654 this->next_buffer_separator = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001655
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001656 if (ctx->Extensions.ARB_transform_feedback3) {
1657 /* Parse gl_NextBuffer. */
1658 if (strcmp(input, "gl_NextBuffer") == 0) {
1659 this->next_buffer_separator = true;
1660 return true;
1661 }
1662
1663 /* Parse gl_SkipComponents. */
1664 if (strcmp(input, "gl_SkipComponents1") == 0)
1665 this->skip_components = 1;
1666 else if (strcmp(input, "gl_SkipComponents2") == 0)
1667 this->skip_components = 2;
1668 else if (strcmp(input, "gl_SkipComponents3") == 0)
1669 this->skip_components = 3;
1670 else if (strcmp(input, "gl_SkipComponents4") == 0)
1671 this->skip_components = 4;
1672
1673 if (this->skip_components)
1674 return true;
1675 }
1676
1677 /* Parse a declaration. */
Paul Berry871ddb92011-11-05 11:17:32 -07001678 const char *bracket = strrchr(input, '[');
1679
1680 if (bracket) {
1681 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001682 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001683 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1684 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001685 }
Paul Berry33fe0212012-01-03 20:41:34 -08001686 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001687 } else {
1688 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001689 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001690 }
1691
Paul Berry642e5b412012-01-04 13:57:52 -08001692 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1693 * class must behave specially to account for the fact that gl_ClipDistance
1694 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001695 */
1696 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1697 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001698 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001699 }
1700
1701 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001702}
1703
1704
1705/**
1706 * Determine whether two tfeedback_decl objects refer to the same variable and
1707 * array index (if applicable).
1708 */
1709bool
1710tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1711{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001712 assert(x.is_varying() && y.is_varying());
1713
Paul Berry871ddb92011-11-05 11:17:32 -07001714 if (strcmp(x.var_name, y.var_name) != 0)
1715 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001716 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001717 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001718 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001719 return false;
1720 return true;
1721}
1722
1723
1724/**
1725 * Assign a location for this tfeedback_decl object based on the location
1726 * assignment in output_var.
1727 *
1728 * If an error occurs, the error is reported through linker_error() and false
1729 * is returned.
1730 */
1731bool
1732tfeedback_decl::assign_location(struct gl_context *ctx,
1733 struct gl_shader_program *prog,
1734 ir_variable *output_var)
1735{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001736 assert(this->is_varying());
1737
Paul Berry871ddb92011-11-05 11:17:32 -07001738 if (output_var->type->is_array()) {
1739 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001740 const unsigned matrix_cols =
1741 output_var->type->fields.array->matrix_columns;
Paul Berry642e5b412012-01-04 13:57:52 -08001742 unsigned actual_array_size = this->is_clip_distance_mesa ?
1743 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001744
1745 if (this->is_subscripted) {
1746 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001747 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001748 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001749 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001750 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001751 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001752 return false;
1753 }
Paul Berry642e5b412012-01-04 13:57:52 -08001754 if (this->is_clip_distance_mesa) {
1755 this->location =
1756 output_var->location + this->array_subscript / 4;
1757 } else {
1758 this->location =
1759 output_var->location + this->array_subscript * matrix_cols;
1760 }
Paul Berry33fe0212012-01-03 20:41:34 -08001761 this->size = 1;
1762 } else {
1763 this->location = output_var->location;
Paul Berry642e5b412012-01-04 13:57:52 -08001764 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001765 }
Paul Berry871ddb92011-11-05 11:17:32 -07001766 this->vector_elements = output_var->type->fields.array->vector_elements;
1767 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001768 if (this->is_clip_distance_mesa)
1769 this->type = GL_FLOAT;
1770 else
1771 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001772 } else {
1773 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001774 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001775 linker_error(prog, "Transform feedback varying %s requested, "
1776 "but %s is not an array.",
1777 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001778 return false;
1779 }
1780 this->location = output_var->location;
Paul Berry33fe0212012-01-03 20:41:34 -08001781 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001782 this->vector_elements = output_var->type->vector_elements;
1783 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001784 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001785 }
Paul Berry642e5b412012-01-04 13:57:52 -08001786
Paul Berry871ddb92011-11-05 11:17:32 -07001787 /* From GL_EXT_transform_feedback:
1788 * A program will fail to link if:
1789 *
1790 * * the total number of components to capture in any varying
1791 * variable in <varyings> is greater than the constant
1792 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1793 * buffer mode is SEPARATE_ATTRIBS_EXT;
1794 */
1795 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1796 this->num_components() >
1797 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1798 linker_error(prog, "Transform feedback varying %s exceeds "
1799 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1800 this->orig_name);
1801 return false;
1802 }
1803
1804 return true;
1805}
1806
1807
Paul Berry25ed3be2012-12-04 10:34:45 -08001808unsigned
1809tfeedback_decl::get_num_outputs() const
Paul Berry871ddb92011-11-05 11:17:32 -07001810{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001811 if (!this->is_varying()) {
Paul Berry25ed3be2012-12-04 10:34:45 -08001812 return 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001813 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001814
Christoph Bumillerd540af52012-01-20 13:24:46 +01001815 unsigned translated_size = this->size;
1816 if (this->is_clip_distance_mesa)
1817 translated_size = (translated_size + 3) / 4;
1818
Paul Berry25ed3be2012-12-04 10:34:45 -08001819 return translated_size * this->matrix_columns;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001820}
1821
1822
1823/**
1824 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1825 *
1826 * If an error occurs, the error is reported through linker_error() and false
1827 * is returned.
1828 */
1829bool
1830tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1831 struct gl_transform_feedback_info *info,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001832 unsigned buffer, const unsigned max_outputs) const
Christoph Bumillerd540af52012-01-20 13:24:46 +01001833{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001834 assert(!this->next_buffer_separator);
1835
1836 /* Handle gl_SkipComponents. */
1837 if (this->skip_components) {
1838 info->BufferStride[buffer] += this->skip_components;
1839 return true;
1840 }
1841
Paul Berryd3150eb2012-01-09 11:25:14 -08001842 /* From GL_EXT_transform_feedback:
1843 * A program will fail to link if:
1844 *
1845 * * the total number of components to capture is greater than
1846 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1847 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1848 */
1849 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1850 info->BufferStride[buffer] + this->num_components() >
1851 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1852 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1853 "limit has been exceeded.");
1854 return false;
1855 }
1856
Paul Berry642e5b412012-01-04 13:57:52 -08001857 unsigned translated_size = this->size;
1858 if (this->is_clip_distance_mesa)
1859 translated_size = (translated_size + 3) / 4;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001860 unsigned components_so_far = 0;
Paul Berry642e5b412012-01-04 13:57:52 -08001861 for (unsigned index = 0; index < translated_size; ++index) {
Paul Berry33fe0212012-01-03 20:41:34 -08001862 for (unsigned v = 0; v < this->matrix_columns; ++v) {
Paul Berry642e5b412012-01-04 13:57:52 -08001863 unsigned num_components = this->vector_elements;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001864 assert(info->NumOutputs < max_outputs);
Paul Berry642e5b412012-01-04 13:57:52 -08001865 info->Outputs[info->NumOutputs].ComponentOffset = 0;
1866 if (this->is_clip_distance_mesa) {
1867 if (this->is_subscripted) {
1868 num_components = 1;
1869 info->Outputs[info->NumOutputs].ComponentOffset =
1870 this->array_subscript % 4;
1871 } else {
1872 num_components = MIN2(4, this->size - components_so_far);
1873 }
1874 }
Paul Berry33fe0212012-01-03 20:41:34 -08001875 info->Outputs[info->NumOutputs].OutputRegister =
1876 this->location + v + index * this->matrix_columns;
1877 info->Outputs[info->NumOutputs].NumComponents = num_components;
1878 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1879 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
Paul Berry33fe0212012-01-03 20:41:34 -08001880 ++info->NumOutputs;
1881 info->BufferStride[buffer] += num_components;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001882 components_so_far += num_components;
Paul Berry33fe0212012-01-03 20:41:34 -08001883 }
Paul Berry871ddb92011-11-05 11:17:32 -07001884 }
Paul Berrybe4e9f72012-01-04 12:21:55 -08001885 assert(components_so_far == this->num_components());
Eric Anholt9d36c962012-01-02 17:08:13 -08001886
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001887 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1888 info->Varyings[info->NumVarying].Type = this->type;
1889 info->Varyings[info->NumVarying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001890 info->NumVarying++;
1891
Paul Berry871ddb92011-11-05 11:17:32 -07001892 return true;
1893}
1894
1895
Paul Berry25ed3be2012-12-04 10:34:45 -08001896ir_variable *
1897tfeedback_decl::find_output_var(gl_shader_program *prog,
1898 gl_shader *producer) const
1899{
1900 const char *name = this->is_clip_distance_mesa
1901 ? "gl_ClipDistanceMESA" : this->var_name;
1902 ir_variable *var = producer->symbols->get_variable(name);
1903 if (var && var->mode == ir_var_out)
1904 return var;
1905
1906 /* From GL_EXT_transform_feedback:
1907 * A program will fail to link if:
1908 *
1909 * * any variable name specified in the <varyings> array is not
1910 * declared as an output in the geometry shader (if present) or
1911 * the vertex shader (if no geometry shader is present);
1912 */
1913 linker_error(prog, "Transform feedback varying %s undeclared.",
1914 this->orig_name);
1915 return NULL;
1916}
1917
1918
Paul Berry871ddb92011-11-05 11:17:32 -07001919/**
1920 * Parse all the transform feedback declarations that were passed to
1921 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1922 *
1923 * If an error occurs, the error is reported through linker_error() and false
1924 * is returned.
1925 */
1926static bool
Paul Berry456279b2011-12-26 19:39:25 -08001927parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1928 const void *mem_ctx, unsigned num_names,
1929 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001930{
1931 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001932 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001933 return false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001934
1935 if (!decls[i].is_varying())
1936 continue;
1937
Paul Berry871ddb92011-11-05 11:17:32 -07001938 /* From GL_EXT_transform_feedback:
1939 * A program will fail to link if:
1940 *
1941 * * any two entries in the <varyings> array specify the same varying
1942 * variable;
1943 *
1944 * We interpret this to mean "any two entries in the <varyings> array
1945 * specify the same varying variable and array index", since transform
1946 * feedback of arrays would be useless otherwise.
1947 */
1948 for (unsigned j = 0; j < i; ++j) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001949 if (!decls[j].is_varying())
1950 continue;
1951
Paul Berry871ddb92011-11-05 11:17:32 -07001952 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1953 linker_error(prog, "Transform feedback varying %s specified "
1954 "more than once.", varying_names[i]);
1955 return false;
1956 }
1957 }
1958 }
1959 return true;
1960}
1961
1962
1963/**
1964 * Assign a location for a variable that is produced in one pipeline stage
1965 * (the "producer") and consumed in the next stage (the "consumer").
1966 *
1967 * \param input_var is the input variable declaration in the consumer.
1968 *
1969 * \param output_var is the output variable declaration in the producer.
1970 *
1971 * \param input_index is the counter that keeps track of assigned input
1972 * locations in the consumer.
1973 *
1974 * \param output_index is the counter that keeps track of assigned output
1975 * locations in the producer.
1976 *
1977 * It is permissible for \c input_var to be NULL (this happens if a variable
1978 * is output by the producer and consumed by transform feedback, but not
1979 * consumed by the consumer).
1980 *
1981 * If the variable has already been assigned a location, this function has no
1982 * effect.
1983 */
1984void
1985assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1986 unsigned *input_index, unsigned *output_index)
1987{
Paul Berry3c9c17d2012-12-04 15:17:01 -08001988 if (!output_var->is_unmatched_generic_inout) {
Paul Berry871ddb92011-11-05 11:17:32 -07001989 /* Location already assigned. */
1990 return;
1991 }
1992
1993 if (input_var) {
1994 assert(input_var->location == -1);
1995 input_var->location = *input_index;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001996 input_var->is_unmatched_generic_inout = 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001997 }
1998
1999 output_var->location = *output_index;
Paul Berry3c9c17d2012-12-04 15:17:01 -08002000 output_var->is_unmatched_generic_inout = 0;
Paul Berry871ddb92011-11-05 11:17:32 -07002001
2002 /* FINISHME: Support for "varying" records in GLSL 1.50. */
2003 assert(!output_var->type->is_record());
2004
2005 if (output_var->type->is_array()) {
2006 const unsigned slots = output_var->type->length
2007 * output_var->type->fields.array->matrix_columns;
2008
2009 *output_index += slots;
2010 *input_index += slots;
2011 } else {
2012 const unsigned slots = output_var->type->matrix_columns;
2013
2014 *output_index += slots;
2015 *input_index += slots;
2016 }
2017}
2018
2019
2020/**
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002021 * Is the given variable a varying variable to be counted against the
2022 * limit in ctx->Const.MaxVarying?
2023 * This includes variables such as texcoords, colors and generic
2024 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2025 */
2026static bool
2027is_varying_var(GLenum shaderType, const ir_variable *var)
2028{
2029 /* Only fragment shaders will take a varying variable as an input */
2030 if (shaderType == GL_FRAGMENT_SHADER &&
Eric Anholt94e82b22012-11-13 14:40:22 -08002031 var->mode == ir_var_in) {
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002032 switch (var->location) {
2033 case FRAG_ATTRIB_WPOS:
2034 case FRAG_ATTRIB_FACE:
2035 case FRAG_ATTRIB_PNTC:
2036 return false;
2037 default:
2038 return true;
2039 }
2040 }
2041 return false;
2042}
2043
2044
2045/**
Paul Berry871ddb92011-11-05 11:17:32 -07002046 * Assign locations for all variables that are produced in one pipeline stage
2047 * (the "producer") and consumed in the next stage (the "consumer").
2048 *
2049 * Variables produced by the producer may also be consumed by transform
2050 * feedback.
2051 *
2052 * \param num_tfeedback_decls is the number of declarations indicating
2053 * variables that may be consumed by transform feedback.
2054 *
2055 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2056 * representing the result of parsing the strings passed to
2057 * glTransformFeedbackVaryings(). assign_location() will be called for
2058 * each of these objects that matches one of the outputs of the
2059 * producer.
2060 *
2061 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2062 * be NULL. In this case, varying locations are assigned solely based on the
2063 * requirements of transform feedback.
2064 */
Ian Romanickde773242011-06-09 13:31:32 -07002065bool
2066assign_varying_locations(struct gl_context *ctx,
2067 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07002068 gl_shader *producer, gl_shader *consumer,
2069 unsigned num_tfeedback_decls,
2070 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07002071{
2072 /* FINISHME: Set dynamically when geometry shader support is added. */
2073 unsigned output_index = VERT_RESULT_VAR0;
2074 unsigned input_index = FRAG_ATTRIB_VAR0;
2075
2076 /* Operate in a total of three passes.
2077 *
2078 * 1. Assign locations for any matching inputs and outputs.
2079 *
2080 * 2. Mark output variables in the producer that do not have locations as
2081 * not being outputs. This lets the optimizer eliminate them.
2082 *
2083 * 3. Mark input variables in the consumer that do not have locations as
2084 * not being inputs. This lets the optimizer eliminate them.
2085 */
2086
Eric Anholt16b68b12010-06-30 11:05:43 -07002087 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07002088 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2089
Paul Berry871ddb92011-11-05 11:17:32 -07002090 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07002091 continue;
2092
Paul Berry871ddb92011-11-05 11:17:32 -07002093 ir_variable *input_var =
2094 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002095
Paul Berry871ddb92011-11-05 11:17:32 -07002096 if (input_var && input_var->mode != ir_var_in)
2097 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002098
Paul Berry871ddb92011-11-05 11:17:32 -07002099 if (input_var) {
2100 assign_varying_location(input_var, output_var, &input_index,
2101 &output_index);
2102 }
Paul Berry25ed3be2012-12-04 10:34:45 -08002103 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002104
Paul Berry25ed3be2012-12-04 10:34:45 -08002105 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2106 if (!tfeedback_decls[i].is_varying())
2107 continue;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002108
Paul Berry25ed3be2012-12-04 10:34:45 -08002109 ir_variable *output_var
2110 = tfeedback_decls[i].find_output_var(prog, producer);
2111
2112 if (output_var == NULL)
2113 return false;
2114
2115 if (output_var->is_unmatched_generic_inout) {
2116 assign_varying_location(NULL, output_var, &input_index,
2117 &output_index);
Ian Romanickdf869d92010-08-30 15:37:44 -07002118 }
Paul Berry25ed3be2012-12-04 10:34:45 -08002119
2120 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2121 return false;
Ian Romanick0e59b262010-06-23 11:23:01 -07002122 }
2123
Ian Romanickde773242011-06-09 13:31:32 -07002124 unsigned varying_vectors = 0;
2125
Paul Berry871ddb92011-11-05 11:17:32 -07002126 if (consumer) {
2127 foreach_list(node, consumer->ir) {
2128 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07002129
Paul Berry871ddb92011-11-05 11:17:32 -07002130 if ((var == NULL) || (var->mode != ir_var_in))
2131 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07002132
Paul Berry3c9c17d2012-12-04 15:17:01 -08002133 if (var->is_unmatched_generic_inout) {
Paul Berry871ddb92011-11-05 11:17:32 -07002134 if (prog->Version <= 120) {
2135 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2136 *
2137 * Only those varying variables used (i.e. read) in
2138 * the fragment shader executable must be written to
2139 * by the vertex shader executable; declaring
2140 * superfluous varying variables in a vertex shader is
2141 * permissible.
2142 *
2143 * We interpret this text as meaning that the VS must
2144 * write the variable for the FS to read it. See
2145 * "glsl1-varying read but not written" in piglit.
2146 */
Eric Anholtb7062832010-07-28 13:52:23 -07002147
Paul Berry871ddb92011-11-05 11:17:32 -07002148 linker_error(prog, "fragment shader varying %s not written "
2149 "by vertex shader\n.", var->name);
2150 }
Eric Anholtb7062832010-07-28 13:52:23 -07002151
Paul Berry871ddb92011-11-05 11:17:32 -07002152 /* An 'in' variable is only really a shader input if its
2153 * value is written by the previous stage.
2154 */
2155 var->mode = ir_var_auto;
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002156 } else if (is_varying_var(consumer->Type, var)) {
Paul Berry871ddb92011-11-05 11:17:32 -07002157 /* The packing rules are used for vertex shader inputs are also
2158 * used for fragment shader inputs.
2159 */
2160 varying_vectors += count_attribute_slots(var->type);
2161 }
Eric Anholtb7062832010-07-28 13:52:23 -07002162 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002163 }
Ian Romanickde773242011-06-09 13:31:32 -07002164
Paul Berry15ba2a52012-08-02 17:51:02 -07002165 if (ctx->API == API_OPENGLES2 || prog->IsES) {
Ian Romanickde773242011-06-09 13:31:32 -07002166 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002167 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2168 linker_warning(prog, "shader uses too many varying vectors "
2169 "(%u > %u), but the driver will try to optimize "
2170 "them out; this is non-portable out-of-spec "
2171 "behavior\n",
2172 varying_vectors, ctx->Const.MaxVarying);
2173 } else {
2174 linker_error(prog, "shader uses too many varying vectors "
2175 "(%u > %u)\n",
2176 varying_vectors, ctx->Const.MaxVarying);
2177 return false;
2178 }
Ian Romanickde773242011-06-09 13:31:32 -07002179 }
2180 } else {
2181 const unsigned float_components = varying_vectors * 4;
2182 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002183 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2184 linker_warning(prog, "shader uses too many varying components "
2185 "(%u > %u), but the driver will try to optimize "
2186 "them out; this is non-portable out-of-spec "
2187 "behavior\n",
2188 float_components, ctx->Const.MaxVarying * 4);
2189 } else {
2190 linker_error(prog, "shader uses too many varying components "
2191 "(%u > %u)\n",
2192 float_components, ctx->Const.MaxVarying * 4);
2193 return false;
2194 }
Ian Romanickde773242011-06-09 13:31:32 -07002195 }
2196 }
2197
2198 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07002199}
2200
2201
Paul Berry871ddb92011-11-05 11:17:32 -07002202/**
2203 * Store transform feedback location assignments into
2204 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2205 *
2206 * If an error occurs, the error is reported through linker_error() and false
2207 * is returned.
2208 */
2209static bool
2210store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2211 unsigned num_tfeedback_decls,
2212 tfeedback_decl *tfeedback_decls)
2213{
Paul Berryebfad9f2011-12-29 15:55:01 -08002214 bool separate_attribs_mode =
2215 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08002216
2217 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01002218 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08002219
2220 memset(&prog->LinkedTransformFeedback, 0,
2221 sizeof(prog->LinkedTransformFeedback));
2222
2223 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08002224 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08002225 struct gl_transform_feedback_varying_info,
2226 num_tfeedback_decls);
2227
Christoph Bumillerd540af52012-01-20 13:24:46 +01002228 unsigned num_outputs = 0;
2229 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
Paul Berry25ed3be2012-12-04 10:34:45 -08002230 num_outputs += tfeedback_decls[i].get_num_outputs();
Christoph Bumillerd540af52012-01-20 13:24:46 +01002231
2232 prog->LinkedTransformFeedback.Outputs =
2233 rzalloc_array(prog,
2234 struct gl_transform_feedback_output,
2235 num_outputs);
2236
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002237 unsigned num_buffers = 0;
2238
2239 if (separate_attribs_mode) {
2240 /* GL_SEPARATE_ATTRIBS */
2241 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2242 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2243 num_buffers, num_outputs))
2244 return false;
2245
2246 num_buffers++;
2247 }
Paul Berry871ddb92011-11-05 11:17:32 -07002248 }
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002249 else {
2250 /* GL_INVERLEAVED_ATTRIBS */
2251 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2252 if (tfeedback_decls[i].is_next_buffer_separator()) {
2253 num_buffers++;
2254 continue;
2255 }
2256
2257 if (!tfeedback_decls[i].store(ctx, prog,
2258 &prog->LinkedTransformFeedback,
2259 num_buffers, num_outputs))
2260 return false;
2261 }
2262 num_buffers++;
2263 }
2264
Christoph Bumillerd540af52012-01-20 13:24:46 +01002265 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002266
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002267 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
Paul Berry871ddb92011-11-05 11:17:32 -07002268 return true;
2269}
2270
Ian Romanick92f81592011-11-08 12:37:19 -08002271/**
Marek Olšákec174a42011-11-18 15:00:10 +01002272 * Store the gl_FragDepth layout in the gl_shader_program struct.
2273 */
2274static void
2275store_fragdepth_layout(struct gl_shader_program *prog)
2276{
2277 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2278 return;
2279 }
2280
2281 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2282
2283 /* We don't look up the gl_FragDepth symbol directly because if
2284 * gl_FragDepth is not used in the shader, it's removed from the IR.
2285 * However, the symbol won't be removed from the symbol table.
2286 *
2287 * We're only interested in the cases where the variable is NOT removed
2288 * from the IR.
2289 */
2290 foreach_list(node, ir) {
2291 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2292
2293 if (var == NULL || var->mode != ir_var_out) {
2294 continue;
2295 }
2296
2297 if (strcmp(var->name, "gl_FragDepth") == 0) {
2298 switch (var->depth_layout) {
2299 case ir_depth_layout_none:
2300 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2301 return;
2302 case ir_depth_layout_any:
2303 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2304 return;
2305 case ir_depth_layout_greater:
2306 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2307 return;
2308 case ir_depth_layout_less:
2309 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2310 return;
2311 case ir_depth_layout_unchanged:
2312 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2313 return;
2314 default:
2315 assert(0);
2316 return;
2317 }
2318 }
2319 }
2320}
2321
2322/**
Ian Romanick92f81592011-11-08 12:37:19 -08002323 * Validate the resources used by a program versus the implementation limits
2324 */
2325static bool
2326check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2327{
2328 static const char *const shader_names[MESA_SHADER_TYPES] = {
2329 "vertex", "fragment", "geometry"
2330 };
2331
2332 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2333 ctx->Const.MaxVertexTextureImageUnits,
2334 ctx->Const.MaxTextureImageUnits,
2335 ctx->Const.MaxGeometryTextureImageUnits
2336 };
2337
2338 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2339 ctx->Const.VertexProgram.MaxUniformComponents,
2340 ctx->Const.FragmentProgram.MaxUniformComponents,
2341 0 /* FINISHME: Geometry shaders. */
2342 };
2343
Eric Anholt877a8972012-06-25 12:47:01 -07002344 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
2345 ctx->Const.VertexProgram.MaxUniformBlocks,
2346 ctx->Const.FragmentProgram.MaxUniformBlocks,
2347 ctx->Const.GeometryProgram.MaxUniformBlocks,
2348 };
2349
Ian Romanick92f81592011-11-08 12:37:19 -08002350 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2351 struct gl_shader *sh = prog->_LinkedShaders[i];
2352
2353 if (sh == NULL)
2354 continue;
2355
2356 if (sh->num_samplers > max_samplers[i]) {
2357 linker_error(prog, "Too many %s shader texture samplers",
2358 shader_names[i]);
2359 }
2360
2361 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002362 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2363 linker_warning(prog, "Too many %s shader uniform components, "
2364 "but the driver will try to optimize them out; "
2365 "this is non-portable out-of-spec behavior\n",
2366 shader_names[i]);
2367 } else {
2368 linker_error(prog, "Too many %s shader uniform components",
2369 shader_names[i]);
2370 }
Ian Romanick92f81592011-11-08 12:37:19 -08002371 }
2372 }
2373
Eric Anholt877a8972012-06-25 12:47:01 -07002374 unsigned blocks[MESA_SHADER_TYPES] = {0};
2375 unsigned total_uniform_blocks = 0;
2376
2377 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
2378 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
2379 if (prog->UniformBlockStageIndex[j][i] != -1) {
2380 blocks[j]++;
2381 total_uniform_blocks++;
2382 }
2383 }
2384
2385 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
2386 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
2387 prog->NumUniformBlocks,
2388 ctx->Const.MaxCombinedUniformBlocks);
2389 } else {
2390 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2391 if (blocks[i] > max_uniform_blocks[i]) {
2392 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
2393 shader_names[i],
2394 blocks[i],
2395 max_uniform_blocks[i]);
2396 break;
2397 }
2398 }
2399 }
2400 }
2401
Ian Romanick92f81592011-11-08 12:37:19 -08002402 return prog->LinkStatus;
2403}
Paul Berry871ddb92011-11-05 11:17:32 -07002404
Ian Romanick0e59b262010-06-23 11:23:01 -07002405void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002406link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002407{
Paul Berry871ddb92011-11-05 11:17:32 -07002408 tfeedback_decl *tfeedback_decls = NULL;
2409 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2410
Kenneth Graunked3073f52011-01-21 14:32:31 -08002411 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002412
Ian Romanick832dfa52010-06-17 15:04:20 -07002413 prog->LinkStatus = false;
2414 prog->Validated = false;
2415 prog->_Used = false;
2416
Eric Anholtf609cf72012-04-27 13:52:56 -07002417 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08002418 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002419
Eric Anholtf609cf72012-04-27 13:52:56 -07002420 ralloc_free(prog->UniformBlocks);
2421 prog->UniformBlocks = NULL;
2422 prog->NumUniformBlocks = 0;
2423 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2424 ralloc_free(prog->UniformBlockStageIndex[i]);
2425 prog->UniformBlockStageIndex[i] = NULL;
2426 }
2427
Ian Romanick832dfa52010-06-17 15:04:20 -07002428 /* Separate the shaders into groups based on their type.
2429 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002430 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002431 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002432 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002433 unsigned num_frag_shaders = 0;
2434
Eric Anholt16b68b12010-06-30 11:05:43 -07002435 vert_shader_list = (struct gl_shader **)
2436 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002437 frag_shader_list = &vert_shader_list[prog->NumShaders];
2438
Ian Romanick25f51d32010-07-16 15:51:50 -07002439 unsigned min_version = UINT_MAX;
2440 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002441 const bool is_es_prog =
2442 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002443 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002444 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2445 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2446
Paul Berrya9f34dc2012-08-02 17:49:44 -07002447 if (prog->Shaders[i]->IsES != is_es_prog) {
2448 linker_error(prog, "all shaders must use same shading "
2449 "language version\n");
2450 goto done;
2451 }
2452
Ian Romanick832dfa52010-06-17 15:04:20 -07002453 switch (prog->Shaders[i]->Type) {
2454 case GL_VERTEX_SHADER:
2455 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2456 num_vert_shaders++;
2457 break;
2458 case GL_FRAGMENT_SHADER:
2459 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2460 num_frag_shaders++;
2461 break;
2462 case GL_GEOMETRY_SHADER:
2463 /* FINISHME: Support geometry shaders. */
2464 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2465 break;
2466 }
2467 }
2468
Ian Romanick25f51d32010-07-16 15:51:50 -07002469 /* Previous to GLSL version 1.30, different compilation units could mix and
2470 * match shading language versions. With GLSL 1.30 and later, the versions
2471 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07002472 *
2473 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07002474 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07002475 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002476 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002477 linker_error(prog, "all shaders must use same shading "
2478 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002479 goto done;
2480 }
2481
2482 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002483 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002484
Ian Romanick3322fba2010-10-14 13:28:42 -07002485 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2486 if (prog->_LinkedShaders[i] != NULL)
2487 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2488
2489 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002490 }
2491
Ian Romanickcd6764e2010-07-16 16:00:07 -07002492 /* Link all shaders for a particular stage and validate the result.
2493 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002494 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002495 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002496 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2497 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002498
2499 if (sh == NULL)
2500 goto done;
2501
2502 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002503 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002504
Ian Romanick3322fba2010-10-14 13:28:42 -07002505 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2506 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002507 }
2508
2509 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002510 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002511 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2512 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002513
2514 if (sh == NULL)
2515 goto done;
2516
2517 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002518 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002519
Ian Romanick3322fba2010-10-14 13:28:42 -07002520 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2521 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002522 }
2523
Ian Romanick3ed850e2010-06-23 12:18:21 -07002524 /* Here begins the inter-stage linking phase. Some initial validation is
2525 * performed, then locations are assigned for uniforms, attributes, and
2526 * varyings.
2527 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002528 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002529 unsigned prev;
2530
2531 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2532 if (prog->_LinkedShaders[prev] != NULL)
2533 break;
2534 }
2535
Bryan Cainf18a0862011-04-23 19:29:15 -05002536 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002537 * stage.
2538 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002539 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2540 if (prog->_LinkedShaders[i] == NULL)
2541 continue;
2542
Ian Romanickf36460e2010-06-23 12:07:22 -07002543 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002544 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002545 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002546 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002547
2548 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002549 }
2550
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002551 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002552 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002553
Eric Anholt3de13952012-05-04 13:08:46 -07002554 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2555 * it before optimization because we want most of the checks to get
2556 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002557 *
2558 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002559 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002560 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002561 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2562 if (sh) {
2563 lower_discard_flow(sh->ir);
2564 }
2565 }
2566
Eric Anholtf609cf72012-04-27 13:52:56 -07002567 if (!interstage_cross_validate_uniform_blocks(prog))
2568 goto done;
2569
Eric Anholt2f4fe152010-08-10 13:06:49 -07002570 /* Do common optimization before assigning storage for attributes,
2571 * uniforms, and varyings. Later optimization could possibly make
2572 * some of that unused.
2573 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002574 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2575 if (prog->_LinkedShaders[i] == NULL)
2576 continue;
2577
Ian Romanick02c5ae12011-07-11 10:46:01 -07002578 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2579 if (!prog->LinkStatus)
2580 goto done;
2581
Paul Berry18392442012-12-04 11:11:02 -08002582 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2583 lower_clip_distance(prog->_LinkedShaders[i]);
2584 }
Paul Berryc06e3252011-08-11 20:58:21 -07002585
Brian Paul7feabfe2012-03-20 17:43:12 -06002586 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2587
2588 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002589 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002590 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002591
Paul Berry50895d42012-12-05 07:17:07 -08002592 /* Mark all generic shader inputs and outputs as unpaired. */
2593 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2594 link_invalidate_variable_locations(
2595 prog->_LinkedShaders[MESA_SHADER_VERTEX],
2596 VERT_ATTRIB_GENERIC0, VERT_RESULT_VAR0);
2597 }
2598 /* FINISHME: Geometry shaders not implemented yet */
2599 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2600 link_invalidate_variable_locations(
2601 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2602 FRAG_ATTRIB_VAR0, FRAG_RESULT_DATA0);
2603 }
2604
Ian Romanickd32d4f72011-06-27 17:59:58 -07002605 /* FINISHME: The value of the max_attribute_index parameter is
2606 * FINISHME: implementation dependent based on the value of
2607 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2608 * FINISHME: at least 16, so hardcode 16 for now.
2609 */
2610 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002611 goto done;
2612 }
2613
Dave Airlie1256a5d2012-03-24 13:33:41 +00002614 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002615 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002616 }
2617
Ian Romanick3322fba2010-10-14 13:28:42 -07002618 unsigned prev;
2619 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2620 if (prog->_LinkedShaders[prev] != NULL)
2621 break;
2622 }
2623
Paul Berry871ddb92011-11-05 11:17:32 -07002624 if (num_tfeedback_decls != 0) {
2625 /* From GL_EXT_transform_feedback:
2626 * A program will fail to link if:
2627 *
2628 * * the <count> specified by TransformFeedbackVaryingsEXT is
2629 * non-zero, but the program object has no vertex or geometry
2630 * shader;
2631 */
2632 if (prev >= MESA_SHADER_FRAGMENT) {
2633 linker_error(prog, "Transform feedback varyings specified, but "
2634 "no vertex or geometry shader is present.");
2635 goto done;
2636 }
2637
2638 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2639 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002640 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002641 prog->TransformFeedback.VaryingNames,
2642 tfeedback_decls))
2643 goto done;
2644 }
2645
Ian Romanick3322fba2010-10-14 13:28:42 -07002646 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2647 if (prog->_LinkedShaders[i] == NULL)
2648 continue;
2649
Paul Berry871ddb92011-11-05 11:17:32 -07002650 if (!assign_varying_locations(
2651 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2652 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2653 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002654 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002655
Ian Romanick3322fba2010-10-14 13:28:42 -07002656 prev = i;
2657 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002658
Paul Berry871ddb92011-11-05 11:17:32 -07002659 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2660 /* There was no fragment shader, but we still have to assign varying
2661 * locations for use by transform feedback.
2662 */
2663 if (!assign_varying_locations(
2664 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2665 tfeedback_decls))
2666 goto done;
2667 }
2668
2669 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2670 goto done;
2671
Ian Romanickcc90e622010-10-19 17:59:10 -07002672 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2673 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2674 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002675
2676 /* Eliminate code that is now dead due to unused vertex outputs being
2677 * demoted.
2678 */
2679 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2680 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002681 }
2682
2683 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2684 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2685
2686 demote_shader_inputs_and_outputs(sh, ir_var_in);
2687 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2688 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002689
2690 /* Eliminate code that is now dead due to unused geometry outputs being
2691 * demoted.
2692 */
2693 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2694 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002695 }
2696
2697 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2698 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2699
2700 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002701
2702 /* Eliminate code that is now dead due to unused fragment inputs being
2703 * demoted. This shouldn't actually do anything other than remove
2704 * declarations of the (now unused) global variables.
2705 */
2706 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2707 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002708 }
2709
Ian Romanick960d7222011-10-21 11:21:02 -07002710 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002711 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002712 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002713
Ian Romanick92f81592011-11-08 12:37:19 -08002714 if (!check_resources(ctx, prog))
2715 goto done;
2716
Ian Romanickce9171f2011-02-03 17:10:14 -08002717 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002718 * present in a linked program. By checking prog->IsES, we also
2719 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002720 */
Eric Anholt57f79782011-07-22 12:57:47 -07002721 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002722 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002723 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002724 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002725 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002726 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002727 }
2728 }
2729
Ian Romanick13e10e42010-06-21 12:03:24 -07002730 /* FINISHME: Assign fragment shader output locations. */
2731
Ian Romanick832dfa52010-06-17 15:04:20 -07002732done:
2733 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002734
2735 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2736 if (prog->_LinkedShaders[i] == NULL)
2737 continue;
2738
2739 /* Retain any live IR, but trash the rest. */
2740 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002741
2742 /* The symbol table in the linked shaders may contain references to
2743 * variables that were removed (e.g., unused uniforms). Since it may
2744 * contain junk, there is no possible valid use. Delete it and set the
2745 * pointer to NULL.
2746 */
2747 delete prog->_LinkedShaders[i]->symbols;
2748 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002749 }
2750
Kenneth Graunked3073f52011-01-21 14:32:31 -08002751 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002752}