blob: 802323e32f013bd6ecaf57c6b84a7bd7359d99a1 [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
Ian Romanickf6ee7bc2011-10-11 16:15:47 -0700203link_invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
204 int generic_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
209 if ((var == NULL) || (var->mode != (unsigned) mode))
210 continue;
211
212 /* Only assign locations for generic attributes / varyings / etc.
213 */
Ian Romanick68a4fc92010-10-07 17:21:22 -0700214 if ((var->location >= generic_base) && !var->explicit_location)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700215 var->location = -1;
216 }
217}
218
219
Ian Romanickc93b8f12010-06-17 15:20:22 -0700220/**
Ian Romanick69846702010-06-22 17:29:19 -0700221 * Determine the number of attribute slots required for a particular type
222 *
223 * This code is here because it implements the language rules of a specific
224 * GLSL version. Since it's a property of the language and not a property of
225 * types in general, it doesn't really belong in glsl_type.
226 */
227unsigned
228count_attribute_slots(const glsl_type *t)
229{
230 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
231 *
232 * "A scalar input counts the same amount against this limit as a vec4,
233 * so applications may want to consider packing groups of four
234 * unrelated float inputs together into a vector to better utilize the
235 * capabilities of the underlying hardware. A matrix input will use up
236 * multiple locations. The number of locations used will equal the
237 * number of columns in the matrix."
238 *
239 * The spec does not explicitly say how arrays are counted. However, it
240 * should be safe to assume the total number of slots consumed by an array
241 * is the number of entries in the array multiplied by the number of slots
242 * consumed by a single element of the array.
243 */
244
245 if (t->is_array())
246 return t->array_size() * count_attribute_slots(t->element_type());
247
248 if (t->is_matrix())
249 return t->matrix_columns;
250
251 return 1;
252}
253
254
255/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700256 * Verify that a vertex shader executable meets all semantic requirements.
257 *
Paul Berry642e5b412012-01-04 13:57:52 -0800258 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
259 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700260 *
261 * \param shader Vertex shader executable to be verified
262 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700263bool
Eric Anholt849e1812010-06-30 11:49:17 -0700264validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700265 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700266{
267 if (shader == NULL)
268 return true;
269
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700270 /* From the GLSL 1.10 spec, page 48:
271 *
272 * "The variable gl_Position is available only in the vertex
273 * language and is intended for writing the homogeneous vertex
274 * position. All executions of a well-formed vertex shader
275 * executable must write a value into this variable. [...] The
276 * variable gl_Position is available only in the vertex
277 * language and is intended for writing the homogeneous vertex
278 * position. All executions of a well-formed vertex shader
279 * executable must write a value into this variable."
280 *
281 * while in GLSL 1.40 this text is changed to:
282 *
283 * "The variable gl_Position is available only in the vertex
284 * language and is intended for writing the homogeneous vertex
285 * position. It can be written at any time during shader
286 * execution. It may also be read back by a vertex shader
287 * after being written. This value will be used by primitive
288 * assembly, clipping, culling, and other fixed functionality
289 * operations, if present, that operate on primitives after
290 * vertex processing has occurred. Its value is undefined if
291 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700292 *
293 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
294 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700295 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700296 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700297 find_assignment_visitor find("gl_Position");
298 find.run(shader->ir);
299 if (!find.variable_found()) {
300 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
301 return false;
302 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700303 }
304
Paul Berry642e5b412012-01-04 13:57:52 -0800305 prog->Vert.ClipDistanceArraySize = 0;
306
Paul Berry15ba2a52012-08-02 17:51:02 -0700307 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700308 /* From section 7.1 (Vertex Shader Special Variables) of the
309 * GLSL 1.30 spec:
310 *
311 * "It is an error for a shader to statically write both
312 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700313 *
314 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
315 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700316 */
317 find_assignment_visitor clip_vertex("gl_ClipVertex");
318 find_assignment_visitor clip_distance("gl_ClipDistance");
319
320 clip_vertex.run(shader->ir);
321 clip_distance.run(shader->ir);
322 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
323 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
324 "and `gl_ClipDistance'\n");
325 return false;
326 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700327 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800328 ir_variable *clip_distance_var =
329 shader->symbols->get_variable("gl_ClipDistance");
330 if (clip_distance_var)
331 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700332 }
333
Ian Romanick832dfa52010-06-17 15:04:20 -0700334 return true;
335}
336
337
Ian Romanickc93b8f12010-06-17 15:20:22 -0700338/**
339 * Verify that a fragment shader executable meets all semantic requirements
340 *
341 * \param shader Fragment shader executable to be verified
342 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700343bool
Eric Anholt849e1812010-06-30 11:49:17 -0700344validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700345 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700346{
347 if (shader == NULL)
348 return true;
349
Ian Romanick832dfa52010-06-17 15:04:20 -0700350 find_assignment_visitor frag_color("gl_FragColor");
351 find_assignment_visitor frag_data("gl_FragData");
352
Eric Anholt16b68b12010-06-30 11:05:43 -0700353 frag_color.run(shader->ir);
354 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700355
Ian Romanick832dfa52010-06-17 15:04:20 -0700356 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700357 linker_error(prog, "fragment shader writes to both "
358 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700359 return false;
360 }
361
362 return true;
363}
364
365
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700366/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700367 * Generate a string describing the mode of a variable
368 */
369static const char *
370mode_string(const ir_variable *var)
371{
372 switch (var->mode) {
373 case ir_var_auto:
374 return (var->read_only) ? "global constant" : "global variable";
375
376 case ir_var_uniform: return "uniform";
377 case ir_var_in: return "shader input";
378 case ir_var_out: return "shader output";
379 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700380
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800381 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700382 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700383 default:
384 assert(!"Should not get here.");
385 return "invalid variable";
386 }
387}
388
389
390/**
391 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700392 */
393bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700394cross_validate_globals(struct gl_shader_program *prog,
395 struct gl_shader **shader_list,
396 unsigned num_shaders,
397 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700398{
399 /* Examine all of the uniforms in all of the shaders and cross validate
400 * them.
401 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700402 glsl_symbol_table variables;
403 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700404 if (shader_list[i] == NULL)
405 continue;
406
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700407 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700408 ir_variable *const var = ((ir_instruction *) node)->as_variable();
409
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700410 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700411 continue;
412
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700413 if (uniforms_only && (var->mode != ir_var_uniform))
414 continue;
415
Ian Romanick7e2aa912010-07-19 17:12:42 -0700416 /* Don't cross validate temporaries that are at global scope. These
417 * will eventually get pulled into the shaders 'main'.
418 */
419 if (var->mode == ir_var_temporary)
420 continue;
421
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700422 /* If a global with this name has already been seen, verify that the
423 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700424 * initializers, the values of the initializers must be the same.
425 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700426 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700427 if (existing != NULL) {
428 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700429 /* Consider the types to be "the same" if both types are arrays
430 * of the same type and one of the arrays is implicitly sized.
431 * In addition, set the type of the linked variable to the
432 * explicitly sized array.
433 */
434 if (var->type->is_array()
435 && existing->type->is_array()
436 && (var->type->fields.array == existing->type->fields.array)
437 && ((var->type->length == 0)
438 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800439 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700440 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800441 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700442 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700443 linker_error(prog, "%s `%s' declared as type "
444 "`%s' and type `%s'\n",
445 mode_string(var),
446 var->name, var->type->name,
447 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700448 return false;
449 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700450 }
451
Ian Romanick68a4fc92010-10-07 17:21:22 -0700452 if (var->explicit_location) {
453 if (existing->explicit_location
454 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700455 linker_error(prog, "explicit locations for %s "
456 "`%s' have differing values\n",
457 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700458 return false;
459 }
460
461 existing->location = var->location;
462 existing->explicit_location = true;
463 }
464
Ian Romanick46173f92011-10-31 13:07:06 -0700465 /* Validate layout qualifiers for gl_FragDepth.
466 *
467 * From the AMD/ARB_conservative_depth specs:
468 *
469 * "If gl_FragDepth is redeclared in any fragment shader in a
470 * program, it must be redeclared in all fragment shaders in
471 * that program that have static assignments to
472 * gl_FragDepth. All redeclarations of gl_FragDepth in all
473 * fragment shaders in a single program must have the same set
474 * of qualifiers."
475 */
476 if (strcmp(var->name, "gl_FragDepth") == 0) {
477 bool layout_declared = var->depth_layout != ir_depth_layout_none;
478 bool layout_differs =
479 var->depth_layout != existing->depth_layout;
480
481 if (layout_declared && layout_differs) {
482 linker_error(prog,
483 "All redeclarations of gl_FragDepth in all "
484 "fragment shaders in a single program must have "
485 "the same set of qualifiers.");
486 }
487
488 if (var->used && layout_differs) {
489 linker_error(prog,
490 "If gl_FragDepth is redeclared with a layout "
491 "qualifier in any fragment shader, it must be "
492 "redeclared with the same layout qualifier in "
493 "all fragment shaders that have assignments to "
494 "gl_FragDepth");
495 }
496 }
Chad Versaceaddae332011-01-27 01:40:31 -0800497
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700498 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
499 *
500 * "If a shared global has multiple initializers, the
501 * initializers must all be constant expressions, and they
502 * must all have the same value. Otherwise, a link error will
503 * result. (A shared global having only one initializer does
504 * not require that initializer to be a constant expression.)"
505 *
506 * Previous to 4.20 the GLSL spec simply said that initializers
507 * must have the same value. In this case of non-constant
508 * initializers, this was impossible to determine. As a result,
509 * no vendor actually implemented that behavior. The 4.20
510 * behavior matches the implemented behavior of at least one other
511 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700512 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700513 if (var->constant_initializer != NULL) {
514 if (existing->constant_initializer != NULL) {
515 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700516 linker_error(prog, "initializers for %s "
517 "`%s' have differing values\n",
518 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700519 return false;
520 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700521 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700522 /* If the first-seen instance of a particular uniform did not
523 * have an initializer but a later instance does, copy the
524 * initializer to the version stored in the symbol table.
525 */
Ian Romanickde415b72010-07-14 13:22:12 -0700526 /* FINISHME: This is wrong. The constant_value field should
527 * FINISHME: not be modified! Imagine a case where a shader
528 * FINISHME: without an initializer is linked in two different
529 * FINISHME: programs with shaders that have differing
530 * FINISHME: initializers. Linking with the first will
531 * FINISHME: modify the shader, and linking with the second
532 * FINISHME: will fail.
533 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700534 existing->constant_initializer =
535 var->constant_initializer->clone(ralloc_parent(existing),
536 NULL);
537 }
538 }
539
540 if (var->has_initializer) {
541 if (existing->has_initializer
542 && (var->constant_initializer == NULL
543 || existing->constant_initializer == NULL)) {
544 linker_error(prog,
545 "shared global variable `%s' has multiple "
546 "non-constant initializers.\n",
547 var->name);
548 return false;
549 }
550
551 /* Some instance had an initializer, so keep track of that. In
552 * this location, all sorts of initializers (constant or
553 * otherwise) will propagate the existence to the variable
554 * stored in the symbol table.
555 */
556 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700557 }
Chad Versace7528f142010-11-17 14:34:38 -0800558
559 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700560 linker_error(prog, "declarations for %s `%s' have "
561 "mismatching invariant qualifiers\n",
562 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800563 return false;
564 }
Chad Versace61428dd2011-01-10 15:29:30 -0800565 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700566 linker_error(prog, "declarations for %s `%s' have "
567 "mismatching centroid qualifiers\n",
568 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800569 return false;
570 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700571 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700572 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700573 }
574 }
575
576 return true;
577}
578
579
Ian Romanick37101922010-06-18 19:02:10 -0700580/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700581 * Perform validation of uniforms used across multiple shader stages
582 */
583bool
584cross_validate_uniforms(struct gl_shader_program *prog)
585{
586 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700587 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700588}
589
Eric Anholtf609cf72012-04-27 13:52:56 -0700590/**
591 * Accumulates the array of prog->UniformBlocks and checks that all
592 * definitons of blocks agree on their contents.
593 */
594static bool
595interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
596{
597 unsigned max_num_uniform_blocks = 0;
598 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
599 if (prog->_LinkedShaders[i])
600 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
601 }
602
603 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
604 struct gl_shader *sh = prog->_LinkedShaders[i];
605
606 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
607 max_num_uniform_blocks);
608 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
609 prog->UniformBlockStageIndex[i][j] = -1;
610
611 if (sh == NULL)
612 continue;
613
614 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
615 int index = link_cross_validate_uniform_block(prog,
616 &prog->UniformBlocks,
617 &prog->NumUniformBlocks,
618 &sh->UniformBlocks[j]);
619
620 if (index == -1) {
621 linker_error(prog, "uniform block `%s' has mismatching definitions",
622 sh->UniformBlocks[j].Name);
623 return false;
624 }
625
626 prog->UniformBlockStageIndex[i][index] = j;
627 }
628 }
629
630 return true;
631}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700632
633/**
Ian Romanick37101922010-06-18 19:02:10 -0700634 * Validate that outputs from one stage match inputs of another
635 */
636bool
Eric Anholt849e1812010-06-30 11:49:17 -0700637cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700638 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700639{
640 glsl_symbol_table parameters;
641 /* FINISHME: Figure these out dynamically. */
642 const char *const producer_stage = "vertex";
643 const char *const consumer_stage = "fragment";
644
645 /* Find all shader outputs in the "producer" stage.
646 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700647 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700648 ir_variable *const var = ((ir_instruction *) node)->as_variable();
649
650 /* FINISHME: For geometry shaders, this should also look for inout
651 * FINISHME: variables.
652 */
653 if ((var == NULL) || (var->mode != ir_var_out))
654 continue;
655
Eric Anholt001eee52010-11-05 06:11:24 -0700656 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700657 }
658
659
660 /* Find all shader inputs in the "consumer" stage. Any variables that have
661 * matching outputs already in the symbol table must have the same type and
662 * qualifiers.
663 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700664 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700665 ir_variable *const input = ((ir_instruction *) node)->as_variable();
666
667 /* FINISHME: For geometry shaders, this should also look for inout
668 * FINISHME: variables.
669 */
670 if ((input == NULL) || (input->mode != ir_var_in))
671 continue;
672
673 ir_variable *const output = parameters.get_variable(input->name);
674 if (output != NULL) {
675 /* Check that the types match between stages.
676 */
677 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800678 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500679 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800680 * access it must redeclare it with a size. There is some
681 * language in the GLSL spec that implies the fragment shader
682 * and vertex shader do not have to agree on this size. Other
683 * driver behave this way, and one or two applications seem to
684 * rely on it.
685 *
686 * Neither declaration needs to be modified here because the array
687 * sizes are fixed later when update_array_sizes is called.
688 *
689 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
690 *
691 * "Unlike user-defined varying variables, the built-in
692 * varying variables don't have a strict one-to-one
693 * correspondence between the vertex language and the
694 * fragment language."
695 */
696 if (!output->type->is_array()
697 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700698 linker_error(prog,
699 "%s shader output `%s' declared as type `%s', "
700 "but %s shader input declared as type `%s'\n",
701 producer_stage, output->name,
702 output->type->name,
703 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800704 return false;
705 }
Ian Romanick37101922010-06-18 19:02:10 -0700706 }
707
708 /* Check that all of the qualifiers match between stages.
709 */
710 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700711 linker_error(prog,
712 "%s shader output `%s' %s centroid qualifier, "
713 "but %s shader input %s centroid qualifier\n",
714 producer_stage,
715 output->name,
716 (output->centroid) ? "has" : "lacks",
717 consumer_stage,
718 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700719 return false;
720 }
721
722 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700723 linker_error(prog,
724 "%s shader output `%s' %s invariant qualifier, "
725 "but %s shader input %s invariant qualifier\n",
726 producer_stage,
727 output->name,
728 (output->invariant) ? "has" : "lacks",
729 consumer_stage,
730 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700731 return false;
732 }
733
734 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700735 linker_error(prog,
736 "%s shader output `%s' specifies %s "
737 "interpolation qualifier, "
738 "but %s shader input specifies %s "
739 "interpolation qualifier\n",
740 producer_stage,
741 output->name,
742 output->interpolation_string(),
743 consumer_stage,
744 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700745 return false;
746 }
747 }
748 }
749
750 return true;
751}
752
753
Ian Romanick3fb87872010-07-09 14:09:34 -0700754/**
755 * Populates a shaders symbol table with all global declarations
756 */
757static void
758populate_symbol_table(gl_shader *sh)
759{
760 sh->symbols = new(sh) glsl_symbol_table;
761
762 foreach_list(node, sh->ir) {
763 ir_instruction *const inst = (ir_instruction *) node;
764 ir_variable *var;
765 ir_function *func;
766
767 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700768 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700769 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700770 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700771 }
772 }
773}
774
775
776/**
Ian Romanick31a97862010-07-12 18:48:50 -0700777 * Remap variables referenced in an instruction tree
778 *
779 * This is used when instruction trees are cloned from one shader and placed in
780 * another. These trees will contain references to \c ir_variable nodes that
781 * do not exist in the target shader. This function finds these \c ir_variable
782 * references and replaces the references with matching variables in the target
783 * shader.
784 *
785 * If there is no matching variable in the target shader, a clone of the
786 * \c ir_variable is made and added to the target shader. The new variable is
787 * added to \b both the instruction stream and the symbol table.
788 *
789 * \param inst IR tree that is to be processed.
790 * \param symbols Symbol table containing global scope symbols in the
791 * linked shader.
792 * \param instructions Instruction stream where new variable declarations
793 * should be added.
794 */
795void
Eric Anholt8273bd42010-08-04 12:34:56 -0700796remap_variables(ir_instruction *inst, struct gl_shader *target,
797 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700798{
799 class remap_visitor : public ir_hierarchical_visitor {
800 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700801 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700802 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700803 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700804 this->target = target;
805 this->symbols = target->symbols;
806 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700807 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700808 }
809
810 virtual ir_visitor_status visit(ir_dereference_variable *ir)
811 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700812 if (ir->var->mode == ir_var_temporary) {
813 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
814
815 assert(var != NULL);
816 ir->var = var;
817 return visit_continue;
818 }
819
Ian Romanick31a97862010-07-12 18:48:50 -0700820 ir_variable *const existing =
821 this->symbols->get_variable(ir->var->name);
822 if (existing != NULL)
823 ir->var = existing;
824 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700825 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700826
Eric Anholt001eee52010-11-05 06:11:24 -0700827 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700828 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700829 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700830 }
831
832 return visit_continue;
833 }
834
835 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700836 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700837 glsl_symbol_table *symbols;
838 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700839 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700840 };
841
Eric Anholt8273bd42010-08-04 12:34:56 -0700842 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700843
844 inst->accept(&v);
845}
846
847
848/**
849 * Move non-declarations from one instruction stream to another
850 *
851 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700852 * 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 -0700853 * pointer) for \c last and \c false for \c make_copies on the first
854 * call. Successive calls pass the return value of the previous call for
855 * \c last and \c true for \c make_copies.
856 *
857 * \param instructions Source instruction stream
858 * \param last Instruction after which new instructions should be
859 * inserted in the target instruction stream
860 * \param make_copies Flag selecting whether instructions in \c instructions
861 * should be copied (via \c ir_instruction::clone) into the
862 * target list or moved.
863 *
864 * \return
865 * The new "last" instruction in the target instruction stream. This pointer
866 * is suitable for use as the \c last parameter of a later call to this
867 * function.
868 */
869exec_node *
870move_non_declarations(exec_list *instructions, exec_node *last,
871 bool make_copies, gl_shader *target)
872{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700873 hash_table *temps = NULL;
874
875 if (make_copies)
876 temps = hash_table_ctor(0, hash_table_pointer_hash,
877 hash_table_pointer_compare);
878
Ian Romanick303c99f2010-07-19 12:34:56 -0700879 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700880 ir_instruction *inst = (ir_instruction *) node;
881
Ian Romanick7e2aa912010-07-19 17:12:42 -0700882 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700883 continue;
884
Ian Romanick7e2aa912010-07-19 17:12:42 -0700885 ir_variable *var = inst->as_variable();
886 if ((var != NULL) && (var->mode != ir_var_temporary))
887 continue;
888
889 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700890 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700891 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700892 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700893
894 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700895 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700896
897 if (var != NULL)
898 hash_table_insert(temps, inst, var);
899 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700900 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700901 } else {
902 inst->remove();
903 }
904
905 last->insert_after(inst);
906 last = inst;
907 }
908
Ian Romanick7e2aa912010-07-19 17:12:42 -0700909 if (make_copies)
910 hash_table_dtor(temps);
911
Ian Romanick31a97862010-07-12 18:48:50 -0700912 return last;
913}
914
915/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700916 * Get the function signature for main from a shader
917 */
918static ir_function_signature *
919get_main_function_signature(gl_shader *sh)
920{
921 ir_function *const f = sh->symbols->get_function("main");
922 if (f != NULL) {
923 exec_list void_parameters;
924
925 /* Look for the 'void main()' signature and ensure that it's defined.
926 * This keeps the linker from accidentally pick a shader that just
927 * contains a prototype for main.
928 *
929 * We don't have to check for multiple definitions of main (in multiple
930 * shaders) because that would have already been caught above.
931 */
932 ir_function_signature *sig = f->matching_signature(&void_parameters);
933 if ((sig != NULL) && sig->is_defined) {
934 return sig;
935 }
936 }
937
938 return NULL;
939}
940
941
942/**
Brian Paul84a12732012-02-02 20:10:40 -0700943 * This class is only used in link_intrastage_shaders() below but declaring
944 * it inside that function leads to compiler warnings with some versions of
945 * gcc.
946 */
947class array_sizing_visitor : public ir_hierarchical_visitor {
948public:
949 virtual ir_visitor_status visit(ir_variable *var)
950 {
951 if (var->type->is_array() && (var->type->length == 0)) {
952 const glsl_type *type =
953 glsl_type::get_array_instance(var->type->fields.array,
954 var->max_array_access + 1);
955 assert(type != NULL);
956 var->type = type;
957 }
958 return visit_continue;
959 }
960};
961
Brian Paul84a12732012-02-02 20:10:40 -0700962/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700963 * Combine a group of shaders for a single stage to generate a linked shader
964 *
965 * \note
966 * If this function is supplied a single shader, it is cloned, and the new
967 * shader is returned.
968 */
969static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800970link_intrastage_shaders(void *mem_ctx,
971 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700972 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700973 struct gl_shader **shader_list,
974 unsigned num_shaders)
975{
Eric Anholtf609cf72012-04-27 13:52:56 -0700976 struct gl_uniform_block *uniform_blocks = NULL;
977 unsigned num_uniform_blocks = 0;
978
Ian Romanick13f782c2010-06-29 18:53:38 -0700979 /* Check that global variables defined in multiple shaders are consistent.
980 */
981 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
982 return NULL;
983
Eric Anholtf609cf72012-04-27 13:52:56 -0700984 /* Check that uniform blocks between shaders for a stage agree. */
985 for (unsigned i = 0; i < num_shaders; i++) {
986 struct gl_shader *sh = shader_list[i];
987
988 for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) {
Eric Anholt8ab58422012-05-01 15:10:14 -0700989 link_assign_uniform_block_offsets(shader_list[i]);
990
Eric Anholtf609cf72012-04-27 13:52:56 -0700991 int index = link_cross_validate_uniform_block(mem_ctx,
992 &uniform_blocks,
993 &num_uniform_blocks,
994 &sh->UniformBlocks[j]);
995 if (index == -1) {
996 linker_error(prog, "uniform block `%s' has mismatching definitions",
997 sh->UniformBlocks[j].Name);
998 return NULL;
999 }
1000 }
1001 }
1002
Ian Romanick13f782c2010-06-29 18:53:38 -07001003 /* Check that there is only a single definition of each function signature
1004 * across all shaders.
1005 */
1006 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1007 foreach_list(node, shader_list[i]->ir) {
1008 ir_function *const f = ((ir_instruction *) node)->as_function();
1009
1010 if (f == NULL)
1011 continue;
1012
1013 for (unsigned j = i + 1; j < num_shaders; j++) {
1014 ir_function *const other =
1015 shader_list[j]->symbols->get_function(f->name);
1016
1017 /* If the other shader has no function (and therefore no function
1018 * signatures) with the same name, skip to the next shader.
1019 */
1020 if (other == NULL)
1021 continue;
1022
1023 foreach_iter (exec_list_iterator, iter, *f) {
1024 ir_function_signature *sig =
1025 (ir_function_signature *) iter.get();
1026
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001027 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -07001028 continue;
1029
1030 ir_function_signature *other_sig =
1031 other->exact_matching_signature(& sig->parameters);
1032
1033 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001034 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -07001035 linker_error(prog, "function `%s' is multiply defined",
1036 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001037 return NULL;
1038 }
1039 }
1040 }
1041 }
1042 }
1043
1044 /* Find the shader that defines main, and make a clone of it.
1045 *
1046 * Starting with the clone, search for undefined references. If one is
1047 * found, find the shader that defines it. Clone the reference and add
1048 * it to the shader. Repeat until there are no undefined references or
1049 * until a reference cannot be resolved.
1050 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001051 gl_shader *main = NULL;
1052 for (unsigned i = 0; i < num_shaders; i++) {
1053 if (get_main_function_signature(shader_list[i]) != NULL) {
1054 main = shader_list[i];
1055 break;
1056 }
1057 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001058
Ian Romanick15ce87e2010-07-09 15:28:22 -07001059 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001060 linker_error(prog, "%s shader lacks `main'\n",
1061 (shader_list[0]->Type == GL_VERTEX_SHADER)
1062 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -07001063 return NULL;
1064 }
1065
Ian Romanick4a455952010-10-13 15:13:02 -07001066 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001067 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001068 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001069
Eric Anholtf609cf72012-04-27 13:52:56 -07001070 linked->UniformBlocks = uniform_blocks;
1071 linked->NumUniformBlocks = num_uniform_blocks;
1072 ralloc_steal(linked, linked->UniformBlocks);
1073
Ian Romanick15ce87e2010-07-09 15:28:22 -07001074 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001075
Ian Romanick31a97862010-07-12 18:48:50 -07001076 /* The a pointer to the main function in the final linked shader (i.e., the
1077 * copy of the original shader that contained the main function).
1078 */
1079 ir_function_signature *const main_sig = get_main_function_signature(linked);
1080
1081 /* Move any instructions other than variable declarations or function
1082 * declarations into main.
1083 */
Ian Romanick9303e352010-07-19 12:33:54 -07001084 exec_node *insertion_point =
1085 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1086 linked);
1087
Ian Romanick31a97862010-07-12 18:48:50 -07001088 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001089 if (shader_list[i] == main)
1090 continue;
1091
Ian Romanick31a97862010-07-12 18:48:50 -07001092 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001093 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001094 }
1095
Ian Romanick13f782c2010-06-29 18:53:38 -07001096 /* Resolve initializers for global variables in the linked shader.
1097 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001098 unsigned num_linking_shaders = num_shaders;
1099 for (unsigned i = 0; i < num_shaders; i++)
1100 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1101
1102 gl_shader **linking_shaders =
1103 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1104
1105 memcpy(linking_shaders, shader_list,
1106 sizeof(linking_shaders[0]) * num_shaders);
1107
1108 unsigned idx = num_shaders;
1109 for (unsigned i = 0; i < num_shaders; i++) {
1110 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1111 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1112 idx += shader_list[i]->num_builtins_to_link;
1113 }
1114
1115 assert(idx == num_linking_shaders);
1116
Ian Romanick4a455952010-10-13 15:13:02 -07001117 if (!link_function_calls(prog, linked, linking_shaders,
1118 num_linking_shaders)) {
1119 ctx->Driver.DeleteShader(ctx, linked);
1120 linked = NULL;
1121 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001122
1123 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001124
Paul Berryc148ef62011-08-03 15:37:01 -07001125#ifdef DEBUG
1126 /* At this point linked should contain all of the linked IR, so
1127 * validate it to make sure nothing went wrong.
1128 */
1129 if (linked)
1130 validate_ir_tree(linked->ir);
1131#endif
1132
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001133 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001134 * unspecified sizes have a size specified. The size is inferred from the
1135 * max_array_access field.
1136 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001137 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001138 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001139
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001140 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001141 }
1142
Ian Romanick3fb87872010-07-09 14:09:34 -07001143 return linked;
1144}
1145
Eric Anholta721abf2010-08-23 10:32:01 -07001146/**
1147 * Update the sizes of linked shader uniform arrays to the maximum
1148 * array index used.
1149 *
1150 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1151 *
1152 * If one or more elements of an array are active,
1153 * GetActiveUniform will return the name of the array in name,
1154 * subject to the restrictions listed above. The type of the array
1155 * is returned in type. The size parameter contains the highest
1156 * array element index used, plus one. The compiler or linker
1157 * determines the highest index used. There will be only one
1158 * active uniform reported by the GL per uniform array.
1159
1160 */
1161static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001162update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001163{
Ian Romanick3322fba2010-10-14 13:28:42 -07001164 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1165 if (prog->_LinkedShaders[i] == NULL)
1166 continue;
1167
Eric Anholta721abf2010-08-23 10:32:01 -07001168 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1169 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1170
Eric Anholt586b4b52010-09-28 14:32:16 -07001171 if ((var == NULL) || (var->mode != ir_var_uniform &&
1172 var->mode != ir_var_in &&
1173 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001174 !var->type->is_array())
1175 continue;
1176
Eric Anholt9feb4032012-05-01 14:43:31 -07001177 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1178 * will not be eliminated. Since we always do std140, just
1179 * don't resize arrays in UBOs.
1180 */
1181 if (var->uniform_block != -1)
1182 continue;
1183
Eric Anholta721abf2010-08-23 10:32:01 -07001184 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001185 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1186 if (prog->_LinkedShaders[j] == NULL)
1187 continue;
1188
Eric Anholta721abf2010-08-23 10:32:01 -07001189 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1190 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1191 if (!other_var)
1192 continue;
1193
1194 if (strcmp(var->name, other_var->name) == 0 &&
1195 other_var->max_array_access > size) {
1196 size = other_var->max_array_access;
1197 }
1198 }
1199 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001200
Eric Anholta721abf2010-08-23 10:32:01 -07001201 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001202 /* If this is a built-in uniform (i.e., it's backed by some
1203 * fixed-function state), adjust the number of state slots to
1204 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001205 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001206 * slots is an integer multiple of the number of array elements.
1207 * Determine the number of slots per array element by dividing by
1208 * the old (total) size.
1209 */
1210 if (var->num_state_slots > 0) {
1211 var->num_state_slots = (size + 1)
1212 * (var->num_state_slots / var->type->length);
1213 }
1214
Eric Anholta721abf2010-08-23 10:32:01 -07001215 var->type = glsl_type::get_array_instance(var->type->fields.array,
1216 size + 1);
1217 /* FINISHME: We should update the types of array
1218 * dereferences of this variable now.
1219 */
1220 }
1221 }
1222 }
1223}
1224
Ian Romanick69846702010-06-22 17:29:19 -07001225/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001226 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001227 *
1228 * \param used_mask Bits representing used (1) and unused (0) locations
1229 * \param needed_count Number of contiguous bits needed.
1230 *
1231 * \return
1232 * Base location of the available bits on success or -1 on failure.
1233 */
1234int
1235find_available_slots(unsigned used_mask, unsigned needed_count)
1236{
1237 unsigned needed_mask = (1 << needed_count) - 1;
1238 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1239
1240 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1241 * cannot optimize possibly infinite loops" for the loop below.
1242 */
1243 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1244 return -1;
1245
1246 for (int i = 0; i <= max_bit_to_test; i++) {
1247 if ((needed_mask & ~used_mask) == needed_mask)
1248 return i;
1249
1250 needed_mask <<= 1;
1251 }
1252
1253 return -1;
1254}
1255
1256
Ian Romanickd32d4f72011-06-27 17:59:58 -07001257/**
1258 * Assign locations for either VS inputs for FS outputs
1259 *
1260 * \param prog Shader program whose variables need locations assigned
1261 * \param target_index Selector for the program target to receive location
1262 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1263 * \c MESA_SHADER_FRAGMENT.
1264 * \param max_index Maximum number of generic locations. This corresponds
1265 * to either the maximum number of draw buffers or the
1266 * maximum number of generic attributes.
1267 *
1268 * \return
1269 * If locations are successfully assigned, true is returned. Otherwise an
1270 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001271 */
Ian Romanick69846702010-06-22 17:29:19 -07001272bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001273assign_attribute_or_color_locations(gl_shader_program *prog,
1274 unsigned target_index,
1275 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001276{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001277 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001278 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001279 unsigned used_locations = (max_index >= 32)
1280 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001281
Ian Romanickd32d4f72011-06-27 17:59:58 -07001282 assert((target_index == MESA_SHADER_VERTEX)
1283 || (target_index == MESA_SHADER_FRAGMENT));
1284
1285 gl_shader *const sh = prog->_LinkedShaders[target_index];
1286 if (sh == NULL)
1287 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001288
Ian Romanick69846702010-06-22 17:29:19 -07001289 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001290 *
1291 * 1. Invalidate the location assignments for all vertex shader inputs.
1292 *
1293 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001294 * glBindVertexAttribLocation) locations and outputs that have
1295 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001296 *
Ian Romanick69846702010-06-22 17:29:19 -07001297 * 3. Sort the attributes without assigned locations by number of slots
1298 * required in decreasing order. Fragmentation caused by attribute
1299 * locations assigned by the application may prevent large attributes
1300 * from having enough contiguous space.
1301 *
1302 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001303 */
1304
Ian Romanickd32d4f72011-06-27 17:59:58 -07001305 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001306 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001307
Ian Romanickd32d4f72011-06-27 17:59:58 -07001308 const enum ir_variable_mode direction =
1309 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1310
1311
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07001312 link_invalidate_variable_locations(sh, direction, generic_base);
Ian Romanickd32d4f72011-06-27 17:59:58 -07001313
Ian Romanick69846702010-06-22 17:29:19 -07001314 /* Temporary storage for the set of attributes that need locations assigned.
1315 */
1316 struct temp_attr {
1317 unsigned slots;
1318 ir_variable *var;
1319
1320 /* Used below in the call to qsort. */
1321 static int compare(const void *a, const void *b)
1322 {
1323 const temp_attr *const l = (const temp_attr *) a;
1324 const temp_attr *const r = (const temp_attr *) b;
1325
1326 /* Reversed because we want a descending order sort below. */
1327 return r->slots - l->slots;
1328 }
1329 } to_assign[16];
1330
1331 unsigned num_attr = 0;
1332
Eric Anholt16b68b12010-06-30 11:05:43 -07001333 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001334 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1335
Brian Paul4470ff22011-07-19 21:10:25 -06001336 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001337 continue;
1338
Ian Romanick68a4fc92010-10-07 17:21:22 -07001339 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001340 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001341 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001342 linker_error(prog,
1343 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001344 (var->location < 0)
1345 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001346 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001347 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001348 }
1349 } else if (target_index == MESA_SHADER_VERTEX) {
1350 unsigned binding;
1351
1352 if (prog->AttributeBindings->get(binding, var->name)) {
1353 assert(binding >= VERT_ATTRIB_GENERIC0);
1354 var->location = binding;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001355 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001356 } else if (target_index == MESA_SHADER_FRAGMENT) {
1357 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001358 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001359
1360 if (prog->FragDataBindings->get(binding, var->name)) {
1361 assert(binding >= FRAG_RESULT_DATA0);
1362 var->location = binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001363
1364 if (prog->FragDataIndexBindings->get(index, var->name)) {
1365 var->index = index;
1366 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001367 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001368 }
1369
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001370 /* If the variable is not a built-in and has a location statically
1371 * assigned in the shader (presumably via a layout qualifier), make sure
1372 * that it doesn't collide with other assigned locations. Otherwise,
1373 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001374 */
Ian Romanick523b6112011-08-17 15:40:03 -07001375 const unsigned slots = count_attribute_slots(var->type);
1376 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001377 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001378 /* From page 61 of the OpenGL 4.0 spec:
1379 *
1380 * "LinkProgram will fail if the attribute bindings assigned
1381 * by BindAttribLocation do not leave not enough space to
1382 * assign a location for an active matrix attribute or an
1383 * active attribute array, both of which require multiple
1384 * contiguous generic attributes."
1385 *
1386 * Previous versions of the spec contain similar language but omit
1387 * the bit about attribute arrays.
1388 *
1389 * Page 61 of the OpenGL 4.0 spec also says:
1390 *
1391 * "It is possible for an application to bind more than one
1392 * attribute name to the same location. This is referred to as
1393 * aliasing. This will only work if only one of the aliased
1394 * attributes is active in the executable program, or if no
1395 * path through the shader consumes more than one attribute of
1396 * a set of attributes aliased to the same location. A link
1397 * error can occur if the linker determines that every path
1398 * through the shader consumes multiple aliased attributes,
1399 * but implementations are not required to generate an error
1400 * in this case."
1401 *
1402 * These two paragraphs are either somewhat contradictory, or I
1403 * don't fully understand one or both of them.
1404 */
1405 /* FINISHME: The code as currently written does not support
1406 * FINISHME: attribute location aliasing (see comment above).
1407 */
1408 /* Mask representing the contiguous slots that will be used by
1409 * this attribute.
1410 */
1411 const unsigned attr = var->location - generic_base;
1412 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001413
Ian Romanick523b6112011-08-17 15:40:03 -07001414 /* Generate a link error if the set of bits requested for this
1415 * attribute overlaps any previously allocated bits.
1416 */
1417 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001418 const char *const string = (target_index == MESA_SHADER_VERTEX)
1419 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001420 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001421 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001422 "available for %s `%s' %d %d %d", string,
1423 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001424 return false;
1425 }
1426
1427 used_locations |= (use_mask << attr);
1428 }
1429
1430 continue;
1431 }
1432
1433 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001434 to_assign[num_attr].var = var;
1435 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001436 }
Ian Romanick69846702010-06-22 17:29:19 -07001437
1438 /* If all of the attributes were assigned locations by the application (or
1439 * are built-in attributes with fixed locations), return early. This should
1440 * be the common case.
1441 */
1442 if (num_attr == 0)
1443 return true;
1444
1445 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1446
Ian Romanickd32d4f72011-06-27 17:59:58 -07001447 if (target_index == MESA_SHADER_VERTEX) {
1448 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1449 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1450 * reserved to prevent it from being automatically allocated below.
1451 */
1452 find_deref_visitor find("gl_Vertex");
1453 find.run(sh->ir);
1454 if (find.variable_found())
1455 used_locations |= (1 << 0);
1456 }
Ian Romanick982e3792010-06-29 18:58:20 -07001457
Ian Romanick69846702010-06-22 17:29:19 -07001458 for (unsigned i = 0; i < num_attr; i++) {
1459 /* Mask representing the contiguous slots that will be used by this
1460 * attribute.
1461 */
1462 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1463
1464 int location = find_available_slots(used_locations, to_assign[i].slots);
1465
1466 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001467 const char *const string = (target_index == MESA_SHADER_VERTEX)
1468 ? "vertex shader input" : "fragment shader output";
1469
Ian Romanick586e7412011-07-28 14:04:09 -07001470 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001471 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001472 "available for %s `%s'",
1473 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001474 return false;
1475 }
1476
Ian Romanickd32d4f72011-06-27 17:59:58 -07001477 to_assign[i].var->location = generic_base + location;
Ian Romanick69846702010-06-22 17:29:19 -07001478 used_locations |= (use_mask << location);
1479 }
1480
1481 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001482}
1483
1484
Ian Romanick40e114b2010-08-17 14:55:50 -07001485/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001486 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001487 */
1488void
Ian Romanickcc90e622010-10-19 17:59:10 -07001489demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001490{
1491 foreach_list(node, sh->ir) {
1492 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1493
Ian Romanickcc90e622010-10-19 17:59:10 -07001494 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001495 continue;
1496
Ian Romanickcc90e622010-10-19 17:59:10 -07001497 /* A shader 'in' or 'out' variable is only really an input or output if
1498 * its value is used by other shader stages. This will cause the variable
1499 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001500 */
1501 if (var->location == -1) {
1502 var->mode = ir_var_auto;
1503 }
1504 }
1505}
1506
1507
Paul Berry871ddb92011-11-05 11:17:32 -07001508/**
1509 * Data structure tracking information about a transform feedback declaration
1510 * during linking.
1511 */
1512class tfeedback_decl
1513{
1514public:
Paul Berry456279b2011-12-26 19:39:25 -08001515 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1516 const void *mem_ctx, const char *input);
Paul Berry871ddb92011-11-05 11:17:32 -07001517 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1518 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1519 ir_variable *output_var);
Christoph Bumillerd540af52012-01-20 13:24:46 +01001520 bool accumulate_num_outputs(struct gl_shader_program *prog, unsigned *count);
Paul Berryd3150eb2012-01-09 11:25:14 -08001521 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001522 struct gl_transform_feedback_info *info, unsigned buffer,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001523 const unsigned max_outputs) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001524
1525 /**
1526 * True if assign_location() has been called for this object.
1527 */
1528 bool is_assigned() const
1529 {
1530 return this->location != -1;
1531 }
1532
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001533 bool is_next_buffer_separator() const
1534 {
1535 return this->next_buffer_separator;
1536 }
1537
1538 bool is_varying() const
1539 {
1540 return !this->next_buffer_separator && !this->skip_components;
1541 }
1542
Paul Berry871ddb92011-11-05 11:17:32 -07001543 /**
1544 * Determine whether this object refers to the variable var.
1545 */
1546 bool matches_var(ir_variable *var) const
1547 {
Paul Berry642e5b412012-01-04 13:57:52 -08001548 if (this->is_clip_distance_mesa)
1549 return strcmp(var->name, "gl_ClipDistanceMESA") == 0;
1550 else
1551 return strcmp(var->name, this->var_name) == 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001552 }
1553
1554 /**
1555 * The total number of varying components taken up by this variable. Only
1556 * valid if is_assigned() is true.
1557 */
1558 unsigned num_components() const
1559 {
Paul Berry642e5b412012-01-04 13:57:52 -08001560 if (this->is_clip_distance_mesa)
1561 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001562 else
Paul Berry642e5b412012-01-04 13:57:52 -08001563 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001564 }
1565
1566private:
1567 /**
1568 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001569 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001570 */
1571 const char *orig_name;
1572
1573 /**
1574 * The name of the variable, parsed from orig_name.
1575 */
Paul Berry913a5c22011-12-27 08:24:57 -08001576 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001577
1578 /**
1579 * True if the declaration in orig_name represents an array.
1580 */
Paul Berry33fe0212012-01-03 20:41:34 -08001581 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001582
1583 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001584 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001585 */
Paul Berry33fe0212012-01-03 20:41:34 -08001586 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001587
1588 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001589 * True if the variable is gl_ClipDistance and the driver lowers
1590 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001591 */
Paul Berry642e5b412012-01-04 13:57:52 -08001592 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001593
1594 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001595 * The vertex shader output location that the linker assigned for this
1596 * variable. -1 if a location hasn't been assigned yet.
1597 */
1598 int location;
1599
1600 /**
1601 * If location != -1, the number of vector elements in this variable, or 1
1602 * if this variable is a scalar.
1603 */
1604 unsigned vector_elements;
1605
1606 /**
1607 * If location != -1, the number of matrix columns in this variable, or 1
1608 * if this variable is not a matrix.
1609 */
1610 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001611
1612 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1613 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001614
1615 /**
1616 * If location != -1, the size that should be returned by
1617 * glGetTransformFeedbackVarying().
1618 */
1619 unsigned size;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001620
1621 /**
1622 * How many components to skip. If non-zero, this is
1623 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1624 */
1625 unsigned skip_components;
1626
1627 /**
1628 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1629 */
1630 bool next_buffer_separator;
Paul Berry871ddb92011-11-05 11:17:32 -07001631};
1632
1633
1634/**
1635 * Initialize this object based on a string that was passed to
1636 * glTransformFeedbackVaryings. If there is a parse error, the error is
1637 * reported using linker_error(), and false is returned.
1638 */
1639bool
Paul Berry456279b2011-12-26 19:39:25 -08001640tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1641 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001642{
1643 /* We don't have to be pedantic about what is a valid GLSL variable name,
1644 * because any variable with an invalid name can't exist in the IR anyway.
1645 */
1646
1647 this->location = -1;
1648 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001649 this->is_clip_distance_mesa = false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001650 this->skip_components = 0;
1651 this->next_buffer_separator = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001652
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001653 if (ctx->Extensions.ARB_transform_feedback3) {
1654 /* Parse gl_NextBuffer. */
1655 if (strcmp(input, "gl_NextBuffer") == 0) {
1656 this->next_buffer_separator = true;
1657 return true;
1658 }
1659
1660 /* Parse gl_SkipComponents. */
1661 if (strcmp(input, "gl_SkipComponents1") == 0)
1662 this->skip_components = 1;
1663 else if (strcmp(input, "gl_SkipComponents2") == 0)
1664 this->skip_components = 2;
1665 else if (strcmp(input, "gl_SkipComponents3") == 0)
1666 this->skip_components = 3;
1667 else if (strcmp(input, "gl_SkipComponents4") == 0)
1668 this->skip_components = 4;
1669
1670 if (this->skip_components)
1671 return true;
1672 }
1673
1674 /* Parse a declaration. */
Paul Berry871ddb92011-11-05 11:17:32 -07001675 const char *bracket = strrchr(input, '[');
1676
1677 if (bracket) {
1678 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001679 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001680 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1681 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001682 }
Paul Berry33fe0212012-01-03 20:41:34 -08001683 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001684 } else {
1685 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001686 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001687 }
1688
Paul Berry642e5b412012-01-04 13:57:52 -08001689 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1690 * class must behave specially to account for the fact that gl_ClipDistance
1691 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001692 */
1693 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1694 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001695 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001696 }
1697
1698 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001699}
1700
1701
1702/**
1703 * Determine whether two tfeedback_decl objects refer to the same variable and
1704 * array index (if applicable).
1705 */
1706bool
1707tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1708{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001709 assert(x.is_varying() && y.is_varying());
1710
Paul Berry871ddb92011-11-05 11:17:32 -07001711 if (strcmp(x.var_name, y.var_name) != 0)
1712 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001713 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001714 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001715 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001716 return false;
1717 return true;
1718}
1719
1720
1721/**
1722 * Assign a location for this tfeedback_decl object based on the location
1723 * assignment in output_var.
1724 *
1725 * If an error occurs, the error is reported through linker_error() and false
1726 * is returned.
1727 */
1728bool
1729tfeedback_decl::assign_location(struct gl_context *ctx,
1730 struct gl_shader_program *prog,
1731 ir_variable *output_var)
1732{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001733 assert(this->is_varying());
1734
Paul Berry871ddb92011-11-05 11:17:32 -07001735 if (output_var->type->is_array()) {
1736 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001737 const unsigned matrix_cols =
1738 output_var->type->fields.array->matrix_columns;
Paul Berry642e5b412012-01-04 13:57:52 -08001739 unsigned actual_array_size = this->is_clip_distance_mesa ?
1740 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001741
1742 if (this->is_subscripted) {
1743 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001744 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001745 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001746 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001747 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001748 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001749 return false;
1750 }
Paul Berry642e5b412012-01-04 13:57:52 -08001751 if (this->is_clip_distance_mesa) {
1752 this->location =
1753 output_var->location + this->array_subscript / 4;
1754 } else {
1755 this->location =
1756 output_var->location + this->array_subscript * matrix_cols;
1757 }
Paul Berry33fe0212012-01-03 20:41:34 -08001758 this->size = 1;
1759 } else {
1760 this->location = output_var->location;
Paul Berry642e5b412012-01-04 13:57:52 -08001761 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001762 }
Paul Berry871ddb92011-11-05 11:17:32 -07001763 this->vector_elements = output_var->type->fields.array->vector_elements;
1764 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001765 if (this->is_clip_distance_mesa)
1766 this->type = GL_FLOAT;
1767 else
1768 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001769 } else {
1770 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001771 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001772 linker_error(prog, "Transform feedback varying %s requested, "
1773 "but %s is not an array.",
1774 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001775 return false;
1776 }
1777 this->location = output_var->location;
Paul Berry33fe0212012-01-03 20:41:34 -08001778 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001779 this->vector_elements = output_var->type->vector_elements;
1780 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001781 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001782 }
Paul Berry642e5b412012-01-04 13:57:52 -08001783
Paul Berry871ddb92011-11-05 11:17:32 -07001784 /* From GL_EXT_transform_feedback:
1785 * A program will fail to link if:
1786 *
1787 * * the total number of components to capture in any varying
1788 * variable in <varyings> is greater than the constant
1789 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1790 * buffer mode is SEPARATE_ATTRIBS_EXT;
1791 */
1792 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1793 this->num_components() >
1794 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1795 linker_error(prog, "Transform feedback varying %s exceeds "
1796 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1797 this->orig_name);
1798 return false;
1799 }
1800
1801 return true;
1802}
1803
1804
Paul Berry871ddb92011-11-05 11:17:32 -07001805bool
Christoph Bumillerd540af52012-01-20 13:24:46 +01001806tfeedback_decl::accumulate_num_outputs(struct gl_shader_program *prog,
1807 unsigned *count)
Paul Berry871ddb92011-11-05 11:17:32 -07001808{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001809 if (!this->is_varying()) {
1810 return true;
1811 }
1812
Paul Berry871ddb92011-11-05 11:17:32 -07001813 if (!this->is_assigned()) {
1814 /* From GL_EXT_transform_feedback:
1815 * A program will fail to link if:
1816 *
1817 * * any variable name specified in the <varyings> array is not
1818 * declared as an output in the geometry shader (if present) or
1819 * the vertex shader (if no geometry shader is present);
1820 */
1821 linker_error(prog, "Transform feedback varying %s undeclared.",
1822 this->orig_name);
1823 return false;
1824 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001825
Christoph Bumillerd540af52012-01-20 13:24:46 +01001826 unsigned translated_size = this->size;
1827 if (this->is_clip_distance_mesa)
1828 translated_size = (translated_size + 3) / 4;
1829
1830 *count += translated_size * this->matrix_columns;
1831
1832 return true;
1833}
1834
1835
1836/**
1837 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1838 *
1839 * If an error occurs, the error is reported through linker_error() and false
1840 * is returned.
1841 */
1842bool
1843tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1844 struct gl_transform_feedback_info *info,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001845 unsigned buffer, const unsigned max_outputs) const
Christoph Bumillerd540af52012-01-20 13:24:46 +01001846{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001847 assert(!this->next_buffer_separator);
1848
1849 /* Handle gl_SkipComponents. */
1850 if (this->skip_components) {
1851 info->BufferStride[buffer] += this->skip_components;
1852 return true;
1853 }
1854
Paul Berryd3150eb2012-01-09 11:25:14 -08001855 /* From GL_EXT_transform_feedback:
1856 * A program will fail to link if:
1857 *
1858 * * the total number of components to capture is greater than
1859 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1860 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1861 */
1862 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1863 info->BufferStride[buffer] + this->num_components() >
1864 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1865 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1866 "limit has been exceeded.");
1867 return false;
1868 }
1869
Paul Berry642e5b412012-01-04 13:57:52 -08001870 unsigned translated_size = this->size;
1871 if (this->is_clip_distance_mesa)
1872 translated_size = (translated_size + 3) / 4;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001873 unsigned components_so_far = 0;
Paul Berry642e5b412012-01-04 13:57:52 -08001874 for (unsigned index = 0; index < translated_size; ++index) {
Paul Berry33fe0212012-01-03 20:41:34 -08001875 for (unsigned v = 0; v < this->matrix_columns; ++v) {
Paul Berry642e5b412012-01-04 13:57:52 -08001876 unsigned num_components = this->vector_elements;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001877 assert(info->NumOutputs < max_outputs);
Paul Berry642e5b412012-01-04 13:57:52 -08001878 info->Outputs[info->NumOutputs].ComponentOffset = 0;
1879 if (this->is_clip_distance_mesa) {
1880 if (this->is_subscripted) {
1881 num_components = 1;
1882 info->Outputs[info->NumOutputs].ComponentOffset =
1883 this->array_subscript % 4;
1884 } else {
1885 num_components = MIN2(4, this->size - components_so_far);
1886 }
1887 }
Paul Berry33fe0212012-01-03 20:41:34 -08001888 info->Outputs[info->NumOutputs].OutputRegister =
1889 this->location + v + index * this->matrix_columns;
1890 info->Outputs[info->NumOutputs].NumComponents = num_components;
1891 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1892 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
Paul Berry33fe0212012-01-03 20:41:34 -08001893 ++info->NumOutputs;
1894 info->BufferStride[buffer] += num_components;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001895 components_so_far += num_components;
Paul Berry33fe0212012-01-03 20:41:34 -08001896 }
Paul Berry871ddb92011-11-05 11:17:32 -07001897 }
Paul Berrybe4e9f72012-01-04 12:21:55 -08001898 assert(components_so_far == this->num_components());
Eric Anholt9d36c962012-01-02 17:08:13 -08001899
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001900 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1901 info->Varyings[info->NumVarying].Type = this->type;
1902 info->Varyings[info->NumVarying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001903 info->NumVarying++;
1904
Paul Berry871ddb92011-11-05 11:17:32 -07001905 return true;
1906}
1907
1908
1909/**
1910 * Parse all the transform feedback declarations that were passed to
1911 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1912 *
1913 * If an error occurs, the error is reported through linker_error() and false
1914 * is returned.
1915 */
1916static bool
Paul Berry456279b2011-12-26 19:39:25 -08001917parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1918 const void *mem_ctx, unsigned num_names,
1919 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001920{
1921 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001922 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001923 return false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001924
1925 if (!decls[i].is_varying())
1926 continue;
1927
Paul Berry871ddb92011-11-05 11:17:32 -07001928 /* From GL_EXT_transform_feedback:
1929 * A program will fail to link if:
1930 *
1931 * * any two entries in the <varyings> array specify the same varying
1932 * variable;
1933 *
1934 * We interpret this to mean "any two entries in the <varyings> array
1935 * specify the same varying variable and array index", since transform
1936 * feedback of arrays would be useless otherwise.
1937 */
1938 for (unsigned j = 0; j < i; ++j) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001939 if (!decls[j].is_varying())
1940 continue;
1941
Paul Berry871ddb92011-11-05 11:17:32 -07001942 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1943 linker_error(prog, "Transform feedback varying %s specified "
1944 "more than once.", varying_names[i]);
1945 return false;
1946 }
1947 }
1948 }
1949 return true;
1950}
1951
1952
1953/**
1954 * Assign a location for a variable that is produced in one pipeline stage
1955 * (the "producer") and consumed in the next stage (the "consumer").
1956 *
1957 * \param input_var is the input variable declaration in the consumer.
1958 *
1959 * \param output_var is the output variable declaration in the producer.
1960 *
1961 * \param input_index is the counter that keeps track of assigned input
1962 * locations in the consumer.
1963 *
1964 * \param output_index is the counter that keeps track of assigned output
1965 * locations in the producer.
1966 *
1967 * It is permissible for \c input_var to be NULL (this happens if a variable
1968 * is output by the producer and consumed by transform feedback, but not
1969 * consumed by the consumer).
1970 *
1971 * If the variable has already been assigned a location, this function has no
1972 * effect.
1973 */
1974void
1975assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1976 unsigned *input_index, unsigned *output_index)
1977{
1978 if (output_var->location != -1) {
1979 /* Location already assigned. */
1980 return;
1981 }
1982
1983 if (input_var) {
1984 assert(input_var->location == -1);
1985 input_var->location = *input_index;
1986 }
1987
1988 output_var->location = *output_index;
1989
1990 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1991 assert(!output_var->type->is_record());
1992
1993 if (output_var->type->is_array()) {
1994 const unsigned slots = output_var->type->length
1995 * output_var->type->fields.array->matrix_columns;
1996
1997 *output_index += slots;
1998 *input_index += slots;
1999 } else {
2000 const unsigned slots = output_var->type->matrix_columns;
2001
2002 *output_index += slots;
2003 *input_index += slots;
2004 }
2005}
2006
2007
2008/**
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002009 * Is the given variable a varying variable to be counted against the
2010 * limit in ctx->Const.MaxVarying?
2011 * This includes variables such as texcoords, colors and generic
2012 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2013 */
2014static bool
2015is_varying_var(GLenum shaderType, const ir_variable *var)
2016{
2017 /* Only fragment shaders will take a varying variable as an input */
2018 if (shaderType == GL_FRAGMENT_SHADER &&
Eric Anholt94e82b22012-11-13 14:40:22 -08002019 var->mode == ir_var_in) {
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002020 switch (var->location) {
2021 case FRAG_ATTRIB_WPOS:
2022 case FRAG_ATTRIB_FACE:
2023 case FRAG_ATTRIB_PNTC:
2024 return false;
2025 default:
2026 return true;
2027 }
2028 }
2029 return false;
2030}
2031
2032
2033/**
Paul Berry871ddb92011-11-05 11:17:32 -07002034 * Assign locations for all variables that are produced in one pipeline stage
2035 * (the "producer") and consumed in the next stage (the "consumer").
2036 *
2037 * Variables produced by the producer may also be consumed by transform
2038 * feedback.
2039 *
2040 * \param num_tfeedback_decls is the number of declarations indicating
2041 * variables that may be consumed by transform feedback.
2042 *
2043 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2044 * representing the result of parsing the strings passed to
2045 * glTransformFeedbackVaryings(). assign_location() will be called for
2046 * each of these objects that matches one of the outputs of the
2047 * producer.
2048 *
2049 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2050 * be NULL. In this case, varying locations are assigned solely based on the
2051 * requirements of transform feedback.
2052 */
Ian Romanickde773242011-06-09 13:31:32 -07002053bool
2054assign_varying_locations(struct gl_context *ctx,
2055 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07002056 gl_shader *producer, gl_shader *consumer,
2057 unsigned num_tfeedback_decls,
2058 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07002059{
2060 /* FINISHME: Set dynamically when geometry shader support is added. */
2061 unsigned output_index = VERT_RESULT_VAR0;
2062 unsigned input_index = FRAG_ATTRIB_VAR0;
2063
2064 /* Operate in a total of three passes.
2065 *
2066 * 1. Assign locations for any matching inputs and outputs.
2067 *
2068 * 2. Mark output variables in the producer that do not have locations as
2069 * not being outputs. This lets the optimizer eliminate them.
2070 *
2071 * 3. Mark input variables in the consumer that do not have locations as
2072 * not being inputs. This lets the optimizer eliminate them.
2073 */
2074
Ian Romanickf6ee7bc2011-10-11 16:15:47 -07002075 link_invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
Paul Berry871ddb92011-11-05 11:17:32 -07002076 if (consumer)
2077 link_invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
Ian Romanick0e59b262010-06-23 11:23:01 -07002078
Eric Anholt16b68b12010-06-30 11:05:43 -07002079 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07002080 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2081
Paul Berry871ddb92011-11-05 11:17:32 -07002082 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07002083 continue;
2084
Paul Berry871ddb92011-11-05 11:17:32 -07002085 ir_variable *input_var =
2086 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002087
Paul Berry871ddb92011-11-05 11:17:32 -07002088 if (input_var && input_var->mode != ir_var_in)
2089 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002090
Paul Berry871ddb92011-11-05 11:17:32 -07002091 if (input_var) {
2092 assign_varying_location(input_var, output_var, &input_index,
2093 &output_index);
2094 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002095
Paul Berry871ddb92011-11-05 11:17:32 -07002096 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002097 if (!tfeedback_decls[i].is_varying())
2098 continue;
2099
Paul Berry871ddb92011-11-05 11:17:32 -07002100 if (!tfeedback_decls[i].is_assigned() &&
2101 tfeedback_decls[i].matches_var(output_var)) {
2102 if (output_var->location == -1) {
2103 assign_varying_location(input_var, output_var, &input_index,
2104 &output_index);
2105 }
2106 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2107 return false;
2108 }
Ian Romanickdf869d92010-08-30 15:37:44 -07002109 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002110 }
2111
Ian Romanickde773242011-06-09 13:31:32 -07002112 unsigned varying_vectors = 0;
2113
Paul Berry871ddb92011-11-05 11:17:32 -07002114 if (consumer) {
2115 foreach_list(node, consumer->ir) {
2116 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07002117
Paul Berry871ddb92011-11-05 11:17:32 -07002118 if ((var == NULL) || (var->mode != ir_var_in))
2119 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07002120
Paul Berry871ddb92011-11-05 11:17:32 -07002121 if (var->location == -1) {
2122 if (prog->Version <= 120) {
2123 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2124 *
2125 * Only those varying variables used (i.e. read) in
2126 * the fragment shader executable must be written to
2127 * by the vertex shader executable; declaring
2128 * superfluous varying variables in a vertex shader is
2129 * permissible.
2130 *
2131 * We interpret this text as meaning that the VS must
2132 * write the variable for the FS to read it. See
2133 * "glsl1-varying read but not written" in piglit.
2134 */
Eric Anholtb7062832010-07-28 13:52:23 -07002135
Paul Berry871ddb92011-11-05 11:17:32 -07002136 linker_error(prog, "fragment shader varying %s not written "
2137 "by vertex shader\n.", var->name);
2138 }
Eric Anholtb7062832010-07-28 13:52:23 -07002139
Paul Berry871ddb92011-11-05 11:17:32 -07002140 /* An 'in' variable is only really a shader input if its
2141 * value is written by the previous stage.
2142 */
2143 var->mode = ir_var_auto;
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002144 } else if (is_varying_var(consumer->Type, var)) {
Paul Berry871ddb92011-11-05 11:17:32 -07002145 /* The packing rules are used for vertex shader inputs are also
2146 * used for fragment shader inputs.
2147 */
2148 varying_vectors += count_attribute_slots(var->type);
2149 }
Eric Anholtb7062832010-07-28 13:52:23 -07002150 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002151 }
Ian Romanickde773242011-06-09 13:31:32 -07002152
Paul Berry15ba2a52012-08-02 17:51:02 -07002153 if (ctx->API == API_OPENGLES2 || prog->IsES) {
Ian Romanickde773242011-06-09 13:31:32 -07002154 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002155 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2156 linker_warning(prog, "shader uses too many varying vectors "
2157 "(%u > %u), but the driver will try to optimize "
2158 "them out; this is non-portable out-of-spec "
2159 "behavior\n",
2160 varying_vectors, ctx->Const.MaxVarying);
2161 } else {
2162 linker_error(prog, "shader uses too many varying vectors "
2163 "(%u > %u)\n",
2164 varying_vectors, ctx->Const.MaxVarying);
2165 return false;
2166 }
Ian Romanickde773242011-06-09 13:31:32 -07002167 }
2168 } else {
2169 const unsigned float_components = varying_vectors * 4;
2170 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002171 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2172 linker_warning(prog, "shader uses too many varying components "
2173 "(%u > %u), but the driver will try to optimize "
2174 "them out; this is non-portable out-of-spec "
2175 "behavior\n",
2176 float_components, ctx->Const.MaxVarying * 4);
2177 } else {
2178 linker_error(prog, "shader uses too many varying components "
2179 "(%u > %u)\n",
2180 float_components, ctx->Const.MaxVarying * 4);
2181 return false;
2182 }
Ian Romanickde773242011-06-09 13:31:32 -07002183 }
2184 }
2185
2186 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07002187}
2188
2189
Paul Berry871ddb92011-11-05 11:17:32 -07002190/**
2191 * Store transform feedback location assignments into
2192 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2193 *
2194 * If an error occurs, the error is reported through linker_error() and false
2195 * is returned.
2196 */
2197static bool
2198store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2199 unsigned num_tfeedback_decls,
2200 tfeedback_decl *tfeedback_decls)
2201{
Paul Berryebfad9f2011-12-29 15:55:01 -08002202 bool separate_attribs_mode =
2203 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08002204
2205 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01002206 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08002207
2208 memset(&prog->LinkedTransformFeedback, 0,
2209 sizeof(prog->LinkedTransformFeedback));
2210
2211 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08002212 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08002213 struct gl_transform_feedback_varying_info,
2214 num_tfeedback_decls);
2215
Christoph Bumillerd540af52012-01-20 13:24:46 +01002216 unsigned num_outputs = 0;
2217 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
2218 if (!tfeedback_decls[i].accumulate_num_outputs(prog, &num_outputs))
2219 return false;
2220
2221 prog->LinkedTransformFeedback.Outputs =
2222 rzalloc_array(prog,
2223 struct gl_transform_feedback_output,
2224 num_outputs);
2225
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002226 unsigned num_buffers = 0;
2227
2228 if (separate_attribs_mode) {
2229 /* GL_SEPARATE_ATTRIBS */
2230 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2231 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2232 num_buffers, num_outputs))
2233 return false;
2234
2235 num_buffers++;
2236 }
Paul Berry871ddb92011-11-05 11:17:32 -07002237 }
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002238 else {
2239 /* GL_INVERLEAVED_ATTRIBS */
2240 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2241 if (tfeedback_decls[i].is_next_buffer_separator()) {
2242 num_buffers++;
2243 continue;
2244 }
2245
2246 if (!tfeedback_decls[i].store(ctx, prog,
2247 &prog->LinkedTransformFeedback,
2248 num_buffers, num_outputs))
2249 return false;
2250 }
2251 num_buffers++;
2252 }
2253
Christoph Bumillerd540af52012-01-20 13:24:46 +01002254 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002255
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002256 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
Paul Berry871ddb92011-11-05 11:17:32 -07002257 return true;
2258}
2259
Ian Romanick92f81592011-11-08 12:37:19 -08002260/**
Marek Olšákec174a42011-11-18 15:00:10 +01002261 * Store the gl_FragDepth layout in the gl_shader_program struct.
2262 */
2263static void
2264store_fragdepth_layout(struct gl_shader_program *prog)
2265{
2266 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2267 return;
2268 }
2269
2270 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2271
2272 /* We don't look up the gl_FragDepth symbol directly because if
2273 * gl_FragDepth is not used in the shader, it's removed from the IR.
2274 * However, the symbol won't be removed from the symbol table.
2275 *
2276 * We're only interested in the cases where the variable is NOT removed
2277 * from the IR.
2278 */
2279 foreach_list(node, ir) {
2280 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2281
2282 if (var == NULL || var->mode != ir_var_out) {
2283 continue;
2284 }
2285
2286 if (strcmp(var->name, "gl_FragDepth") == 0) {
2287 switch (var->depth_layout) {
2288 case ir_depth_layout_none:
2289 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2290 return;
2291 case ir_depth_layout_any:
2292 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2293 return;
2294 case ir_depth_layout_greater:
2295 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2296 return;
2297 case ir_depth_layout_less:
2298 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2299 return;
2300 case ir_depth_layout_unchanged:
2301 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2302 return;
2303 default:
2304 assert(0);
2305 return;
2306 }
2307 }
2308 }
2309}
2310
2311/**
Ian Romanick92f81592011-11-08 12:37:19 -08002312 * Validate the resources used by a program versus the implementation limits
2313 */
2314static bool
2315check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2316{
2317 static const char *const shader_names[MESA_SHADER_TYPES] = {
2318 "vertex", "fragment", "geometry"
2319 };
2320
2321 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2322 ctx->Const.MaxVertexTextureImageUnits,
2323 ctx->Const.MaxTextureImageUnits,
2324 ctx->Const.MaxGeometryTextureImageUnits
2325 };
2326
2327 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2328 ctx->Const.VertexProgram.MaxUniformComponents,
2329 ctx->Const.FragmentProgram.MaxUniformComponents,
2330 0 /* FINISHME: Geometry shaders. */
2331 };
2332
Eric Anholt877a8972012-06-25 12:47:01 -07002333 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
2334 ctx->Const.VertexProgram.MaxUniformBlocks,
2335 ctx->Const.FragmentProgram.MaxUniformBlocks,
2336 ctx->Const.GeometryProgram.MaxUniformBlocks,
2337 };
2338
Ian Romanick92f81592011-11-08 12:37:19 -08002339 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2340 struct gl_shader *sh = prog->_LinkedShaders[i];
2341
2342 if (sh == NULL)
2343 continue;
2344
2345 if (sh->num_samplers > max_samplers[i]) {
2346 linker_error(prog, "Too many %s shader texture samplers",
2347 shader_names[i]);
2348 }
2349
2350 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002351 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2352 linker_warning(prog, "Too many %s shader uniform components, "
2353 "but the driver will try to optimize them out; "
2354 "this is non-portable out-of-spec behavior\n",
2355 shader_names[i]);
2356 } else {
2357 linker_error(prog, "Too many %s shader uniform components",
2358 shader_names[i]);
2359 }
Ian Romanick92f81592011-11-08 12:37:19 -08002360 }
2361 }
2362
Eric Anholt877a8972012-06-25 12:47:01 -07002363 unsigned blocks[MESA_SHADER_TYPES] = {0};
2364 unsigned total_uniform_blocks = 0;
2365
2366 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
2367 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
2368 if (prog->UniformBlockStageIndex[j][i] != -1) {
2369 blocks[j]++;
2370 total_uniform_blocks++;
2371 }
2372 }
2373
2374 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
2375 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
2376 prog->NumUniformBlocks,
2377 ctx->Const.MaxCombinedUniformBlocks);
2378 } else {
2379 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2380 if (blocks[i] > max_uniform_blocks[i]) {
2381 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
2382 shader_names[i],
2383 blocks[i],
2384 max_uniform_blocks[i]);
2385 break;
2386 }
2387 }
2388 }
2389 }
2390
Ian Romanick92f81592011-11-08 12:37:19 -08002391 return prog->LinkStatus;
2392}
Paul Berry871ddb92011-11-05 11:17:32 -07002393
Ian Romanick0e59b262010-06-23 11:23:01 -07002394void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002395link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002396{
Paul Berry871ddb92011-11-05 11:17:32 -07002397 tfeedback_decl *tfeedback_decls = NULL;
2398 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2399
Kenneth Graunked3073f52011-01-21 14:32:31 -08002400 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002401
Ian Romanick832dfa52010-06-17 15:04:20 -07002402 prog->LinkStatus = false;
2403 prog->Validated = false;
2404 prog->_Used = false;
2405
Eric Anholtf609cf72012-04-27 13:52:56 -07002406 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08002407 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002408
Eric Anholtf609cf72012-04-27 13:52:56 -07002409 ralloc_free(prog->UniformBlocks);
2410 prog->UniformBlocks = NULL;
2411 prog->NumUniformBlocks = 0;
2412 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2413 ralloc_free(prog->UniformBlockStageIndex[i]);
2414 prog->UniformBlockStageIndex[i] = NULL;
2415 }
2416
Ian Romanick832dfa52010-06-17 15:04:20 -07002417 /* Separate the shaders into groups based on their type.
2418 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002419 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002420 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002421 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002422 unsigned num_frag_shaders = 0;
2423
Eric Anholt16b68b12010-06-30 11:05:43 -07002424 vert_shader_list = (struct gl_shader **)
2425 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002426 frag_shader_list = &vert_shader_list[prog->NumShaders];
2427
Ian Romanick25f51d32010-07-16 15:51:50 -07002428 unsigned min_version = UINT_MAX;
2429 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002430 const bool is_es_prog =
2431 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002432 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002433 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2434 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2435
Paul Berrya9f34dc2012-08-02 17:49:44 -07002436 if (prog->Shaders[i]->IsES != is_es_prog) {
2437 linker_error(prog, "all shaders must use same shading "
2438 "language version\n");
2439 goto done;
2440 }
2441
Ian Romanick832dfa52010-06-17 15:04:20 -07002442 switch (prog->Shaders[i]->Type) {
2443 case GL_VERTEX_SHADER:
2444 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2445 num_vert_shaders++;
2446 break;
2447 case GL_FRAGMENT_SHADER:
2448 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2449 num_frag_shaders++;
2450 break;
2451 case GL_GEOMETRY_SHADER:
2452 /* FINISHME: Support geometry shaders. */
2453 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2454 break;
2455 }
2456 }
2457
Ian Romanick25f51d32010-07-16 15:51:50 -07002458 /* Previous to GLSL version 1.30, different compilation units could mix and
2459 * match shading language versions. With GLSL 1.30 and later, the versions
2460 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07002461 *
2462 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07002463 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07002464 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002465 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002466 linker_error(prog, "all shaders must use same shading "
2467 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002468 goto done;
2469 }
2470
2471 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002472 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002473
Ian Romanick3322fba2010-10-14 13:28:42 -07002474 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2475 if (prog->_LinkedShaders[i] != NULL)
2476 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2477
2478 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002479 }
2480
Ian Romanickcd6764e2010-07-16 16:00:07 -07002481 /* Link all shaders for a particular stage and validate the result.
2482 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002483 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002484 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002485 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2486 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002487
2488 if (sh == NULL)
2489 goto done;
2490
2491 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002492 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002493
Ian Romanick3322fba2010-10-14 13:28:42 -07002494 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2495 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002496 }
2497
2498 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002499 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002500 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2501 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002502
2503 if (sh == NULL)
2504 goto done;
2505
2506 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002507 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002508
Ian Romanick3322fba2010-10-14 13:28:42 -07002509 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2510 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002511 }
2512
Ian Romanick3ed850e2010-06-23 12:18:21 -07002513 /* Here begins the inter-stage linking phase. Some initial validation is
2514 * performed, then locations are assigned for uniforms, attributes, and
2515 * varyings.
2516 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002517 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002518 unsigned prev;
2519
2520 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2521 if (prog->_LinkedShaders[prev] != NULL)
2522 break;
2523 }
2524
Bryan Cainf18a0862011-04-23 19:29:15 -05002525 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002526 * stage.
2527 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002528 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2529 if (prog->_LinkedShaders[i] == NULL)
2530 continue;
2531
Ian Romanickf36460e2010-06-23 12:07:22 -07002532 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002533 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002534 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002535 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002536
2537 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002538 }
2539
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002540 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002541 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002542
Eric Anholt3de13952012-05-04 13:08:46 -07002543 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2544 * it before optimization because we want most of the checks to get
2545 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002546 *
2547 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002548 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002549 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002550 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2551 if (sh) {
2552 lower_discard_flow(sh->ir);
2553 }
2554 }
2555
Eric Anholtf609cf72012-04-27 13:52:56 -07002556 if (!interstage_cross_validate_uniform_blocks(prog))
2557 goto done;
2558
Eric Anholt2f4fe152010-08-10 13:06:49 -07002559 /* Do common optimization before assigning storage for attributes,
2560 * uniforms, and varyings. Later optimization could possibly make
2561 * some of that unused.
2562 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002563 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2564 if (prog->_LinkedShaders[i] == NULL)
2565 continue;
2566
Ian Romanick02c5ae12011-07-11 10:46:01 -07002567 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2568 if (!prog->LinkStatus)
2569 goto done;
2570
Paul Berry18392442012-12-04 11:11:02 -08002571 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2572 lower_clip_distance(prog->_LinkedShaders[i]);
2573 }
Paul Berryc06e3252011-08-11 20:58:21 -07002574
Brian Paul7feabfe2012-03-20 17:43:12 -06002575 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2576
2577 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002578 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002579 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002580
Ian Romanickd32d4f72011-06-27 17:59:58 -07002581 /* FINISHME: The value of the max_attribute_index parameter is
2582 * FINISHME: implementation dependent based on the value of
2583 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2584 * FINISHME: at least 16, so hardcode 16 for now.
2585 */
2586 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002587 goto done;
2588 }
2589
Dave Airlie1256a5d2012-03-24 13:33:41 +00002590 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002591 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002592 }
2593
Ian Romanick3322fba2010-10-14 13:28:42 -07002594 unsigned prev;
2595 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2596 if (prog->_LinkedShaders[prev] != NULL)
2597 break;
2598 }
2599
Paul Berry871ddb92011-11-05 11:17:32 -07002600 if (num_tfeedback_decls != 0) {
2601 /* From GL_EXT_transform_feedback:
2602 * A program will fail to link if:
2603 *
2604 * * the <count> specified by TransformFeedbackVaryingsEXT is
2605 * non-zero, but the program object has no vertex or geometry
2606 * shader;
2607 */
2608 if (prev >= MESA_SHADER_FRAGMENT) {
2609 linker_error(prog, "Transform feedback varyings specified, but "
2610 "no vertex or geometry shader is present.");
2611 goto done;
2612 }
2613
2614 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2615 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002616 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002617 prog->TransformFeedback.VaryingNames,
2618 tfeedback_decls))
2619 goto done;
2620 }
2621
Ian Romanick3322fba2010-10-14 13:28:42 -07002622 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2623 if (prog->_LinkedShaders[i] == NULL)
2624 continue;
2625
Paul Berry871ddb92011-11-05 11:17:32 -07002626 if (!assign_varying_locations(
2627 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2628 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2629 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002630 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002631
Ian Romanick3322fba2010-10-14 13:28:42 -07002632 prev = i;
2633 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002634
Paul Berry871ddb92011-11-05 11:17:32 -07002635 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2636 /* There was no fragment shader, but we still have to assign varying
2637 * locations for use by transform feedback.
2638 */
2639 if (!assign_varying_locations(
2640 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2641 tfeedback_decls))
2642 goto done;
2643 }
2644
2645 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2646 goto done;
2647
Ian Romanickcc90e622010-10-19 17:59:10 -07002648 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2649 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2650 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002651
2652 /* Eliminate code that is now dead due to unused vertex outputs being
2653 * demoted.
2654 */
2655 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2656 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002657 }
2658
2659 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2660 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2661
2662 demote_shader_inputs_and_outputs(sh, ir_var_in);
2663 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2664 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002665
2666 /* Eliminate code that is now dead due to unused geometry outputs being
2667 * demoted.
2668 */
2669 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2670 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002671 }
2672
2673 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2674 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2675
2676 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002677
2678 /* Eliminate code that is now dead due to unused fragment inputs being
2679 * demoted. This shouldn't actually do anything other than remove
2680 * declarations of the (now unused) global variables.
2681 */
2682 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2683 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002684 }
2685
Ian Romanick960d7222011-10-21 11:21:02 -07002686 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002687 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002688 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002689
Ian Romanick92f81592011-11-08 12:37:19 -08002690 if (!check_resources(ctx, prog))
2691 goto done;
2692
Ian Romanickce9171f2011-02-03 17:10:14 -08002693 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002694 * present in a linked program. By checking prog->IsES, we also
2695 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002696 */
Eric Anholt57f79782011-07-22 12:57:47 -07002697 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002698 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002699 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002700 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002701 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002702 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002703 }
2704 }
2705
Ian Romanick13e10e42010-06-21 12:03:24 -07002706 /* FINISHME: Assign fragment shader output locations. */
2707
Ian Romanick832dfa52010-06-17 15:04:20 -07002708done:
2709 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002710
2711 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2712 if (prog->_LinkedShaders[i] == NULL)
2713 continue;
2714
2715 /* Retain any live IR, but trash the rest. */
2716 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002717
2718 /* The symbol table in the linked shaders may contain references to
2719 * variables that were removed (e.g., unused uniforms). Since it may
2720 * contain junk, there is no possible valid use. Delete it and set the
2721 * pointer to NULL.
2722 */
2723 delete prog->_LinkedShaders[i]->symbols;
2724 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002725 }
2726
Kenneth Graunked3073f52011-01-21 14:32:31 -08002727 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002728}