blob: 2523dc99d6ff97fe2bc295957af6d702d1feee9c [file] [log] [blame]
Ian Romanick832dfa52010-06-17 15:04:20 -07001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * \file linker.cpp
26 * GLSL linker implementation
27 *
28 * Given a set of shaders that are to be linked to generate a final program,
29 * there are three distinct stages.
30 *
31 * In the first stage shaders are partitioned into groups based on the shader
32 * type. All shaders of a particular type (e.g., vertex shaders) are linked
33 * together.
34 *
35 * - Undefined references in each shader are resolve to definitions in
36 * another shader.
37 * - Types and qualifiers of uniforms, outputs, and global variables defined
38 * in multiple shaders with the same name are verified to be the same.
39 * - Initializers for uniforms and global variables defined
40 * in multiple shaders with the same name are verified to be the same.
41 *
42 * The result, in the terminology of the GLSL spec, is a set of shader
43 * executables for each processing unit.
44 *
45 * After the first stage is complete, a series of semantic checks are performed
46 * on each of the shader executables.
47 *
48 * - Each shader executable must define a \c main function.
49 * - Each vertex shader executable must write to \c gl_Position.
50 * - Each fragment shader executable must write to either \c gl_FragData or
51 * \c gl_FragColor.
52 *
53 * In the final stage individual shader executables are linked to create a
54 * complete exectuable.
55 *
56 * - Types of uniforms defined in multiple shader stages with the same name
57 * are verified to be the same.
58 * - Initializers for uniforms defined in multiple shader stages with the
59 * same name are verified to be the same.
60 * - Types and qualifiers of outputs defined in one stage are verified to
61 * be the same as the types and qualifiers of inputs defined with the same
62 * name in a later stage.
63 *
64 * \author Ian Romanick <ian.d.romanick@intel.com>
65 */
Ian Romanickf36460e2010-06-23 12:07:22 -070066
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080067#include "main/core.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070068#include "glsl_symbol_table.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070069#include "ir.h"
70#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030071#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070072#include "linker.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070073#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070074
Ian Romanick3322fba2010-10-14 13:28:42 -070075extern "C" {
76#include "main/shaderobj.h"
77}
78
Ian Romanick832dfa52010-06-17 15:04:20 -070079/**
80 * Visitor that determines whether or not a variable is ever written.
81 */
82class find_assignment_visitor : public ir_hierarchical_visitor {
83public:
84 find_assignment_visitor(const char *name)
85 : name(name), found(false)
86 {
87 /* empty */
88 }
89
90 virtual ir_visitor_status visit_enter(ir_assignment *ir)
91 {
92 ir_variable *const var = ir->lhs->variable_referenced();
93
94 if (strcmp(name, var->name) == 0) {
95 found = true;
96 return visit_stop;
97 }
98
99 return visit_continue_with_parent;
100 }
101
Eric Anholt18a60232010-08-23 11:29:25 -0700102 virtual ir_visitor_status visit_enter(ir_call *ir)
103 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700104 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700105 foreach_iter(exec_list_iterator, iter, *ir) {
106 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
107 ir_variable *sig_param = (ir_variable *)sig_iter.get();
108
109 if (sig_param->mode == ir_var_out ||
110 sig_param->mode == ir_var_inout) {
111 ir_variable *var = param_rval->variable_referenced();
112 if (var && strcmp(name, var->name) == 0) {
113 found = true;
114 return visit_stop;
115 }
116 }
117 sig_iter.next();
118 }
119
Kenneth Graunked884f602012-03-20 15:56:37 -0700120 if (ir->return_deref != NULL) {
121 ir_variable *const var = ir->return_deref->variable_referenced();
122
123 if (strcmp(name, var->name) == 0) {
124 found = true;
125 return visit_stop;
126 }
127 }
128
Eric Anholt18a60232010-08-23 11:29:25 -0700129 return visit_continue_with_parent;
130 }
131
Ian Romanick832dfa52010-06-17 15:04:20 -0700132 bool variable_found()
133 {
134 return found;
135 }
136
137private:
138 const char *name; /**< Find writes to a variable with this name. */
139 bool found; /**< Was a write to the variable found? */
140};
141
Ian Romanickc93b8f12010-06-17 15:20:22 -0700142
Ian Romanickc33e78f2010-08-13 12:30:41 -0700143/**
144 * Visitor that determines whether or not a variable is ever read.
145 */
146class find_deref_visitor : public ir_hierarchical_visitor {
147public:
148 find_deref_visitor(const char *name)
149 : name(name), found(false)
150 {
151 /* empty */
152 }
153
154 virtual ir_visitor_status visit(ir_dereference_variable *ir)
155 {
156 if (strcmp(this->name, ir->var->name) == 0) {
157 this->found = true;
158 return visit_stop;
159 }
160
161 return visit_continue;
162 }
163
164 bool variable_found() const
165 {
166 return this->found;
167 }
168
169private:
170 const char *name; /**< Find writes to a variable with this name. */
171 bool found; /**< Was a write to the variable found? */
172};
173
174
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700175void
Ian Romanick586e7412011-07-28 14:04:09 -0700176linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700177{
178 va_list ap;
179
Kenneth Graunked3073f52011-01-21 14:32:31 -0800180 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700181 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800182 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700183 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700184
185 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700186}
187
188
189void
Ian Romanick379a32f2011-07-28 14:09:06 -0700190linker_warning(gl_shader_program *prog, const char *fmt, ...)
191{
192 va_list ap;
193
194 ralloc_strcat(&prog->InfoLog, "error: ");
195 va_start(ap, fmt);
196 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
197 va_end(ap);
198
199}
200
201
202void
Paul Berry50895d42012-12-05 07:17:07 -0800203link_invalidate_variable_locations(gl_shader *sh, int input_base,
204 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700205{
Eric Anholt16b68b12010-06-30 11:05:43 -0700206 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700207 ir_variable *const var = ((ir_instruction *) node)->as_variable();
208
Paul Berry50895d42012-12-05 07:17:07 -0800209 if (var == NULL)
210 continue;
211
212 int base;
213 switch (var->mode) {
214 case ir_var_in:
215 base = input_base;
216 break;
217 case ir_var_out:
218 base = output_base;
219 break;
220 default:
221 continue;
222 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700223
224 /* Only assign locations for generic attributes / varyings / etc.
225 */
Paul Berry50895d42012-12-05 07:17:07 -0800226 if ((var->location >= base) && !var->explicit_location)
227 var->location = -1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700228 }
229}
230
231
Ian Romanickc93b8f12010-06-17 15:20:22 -0700232/**
Ian Romanick69846702010-06-22 17:29:19 -0700233 * Determine the number of attribute slots required for a particular type
234 *
235 * This code is here because it implements the language rules of a specific
236 * GLSL version. Since it's a property of the language and not a property of
237 * types in general, it doesn't really belong in glsl_type.
238 */
239unsigned
240count_attribute_slots(const glsl_type *t)
241{
242 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
243 *
244 * "A scalar input counts the same amount against this limit as a vec4,
245 * so applications may want to consider packing groups of four
246 * unrelated float inputs together into a vector to better utilize the
247 * capabilities of the underlying hardware. A matrix input will use up
248 * multiple locations. The number of locations used will equal the
249 * number of columns in the matrix."
250 *
251 * The spec does not explicitly say how arrays are counted. However, it
252 * should be safe to assume the total number of slots consumed by an array
253 * is the number of entries in the array multiplied by the number of slots
254 * consumed by a single element of the array.
255 */
256
257 if (t->is_array())
258 return t->array_size() * count_attribute_slots(t->element_type());
259
260 if (t->is_matrix())
261 return t->matrix_columns;
262
263 return 1;
264}
265
266
267/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700268 * Verify that a vertex shader executable meets all semantic requirements.
269 *
Paul Berry642e5b412012-01-04 13:57:52 -0800270 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
271 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700272 *
273 * \param shader Vertex shader executable to be verified
274 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700275bool
Eric Anholt849e1812010-06-30 11:49:17 -0700276validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700277 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700278{
279 if (shader == NULL)
280 return true;
281
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700282 /* From the GLSL 1.10 spec, page 48:
283 *
284 * "The variable gl_Position is available only in the vertex
285 * language and is intended for writing the homogeneous vertex
286 * position. All executions of a well-formed vertex shader
287 * executable must write a value into this variable. [...] The
288 * variable gl_Position is available only in the vertex
289 * language and is intended for writing the homogeneous vertex
290 * position. All executions of a well-formed vertex shader
291 * executable must write a value into this variable."
292 *
293 * while in GLSL 1.40 this text is changed to:
294 *
295 * "The variable gl_Position is available only in the vertex
296 * language and is intended for writing the homogeneous vertex
297 * position. It can be written at any time during shader
298 * execution. It may also be read back by a vertex shader
299 * after being written. This value will be used by primitive
300 * assembly, clipping, culling, and other fixed functionality
301 * operations, if present, that operate on primitives after
302 * vertex processing has occurred. Its value is undefined if
303 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700304 *
305 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
306 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700307 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700308 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700309 find_assignment_visitor find("gl_Position");
310 find.run(shader->ir);
311 if (!find.variable_found()) {
312 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
313 return false;
314 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700315 }
316
Paul Berry642e5b412012-01-04 13:57:52 -0800317 prog->Vert.ClipDistanceArraySize = 0;
318
Paul Berry15ba2a52012-08-02 17:51:02 -0700319 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700320 /* From section 7.1 (Vertex Shader Special Variables) of the
321 * GLSL 1.30 spec:
322 *
323 * "It is an error for a shader to statically write both
324 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700325 *
326 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
327 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700328 */
329 find_assignment_visitor clip_vertex("gl_ClipVertex");
330 find_assignment_visitor clip_distance("gl_ClipDistance");
331
332 clip_vertex.run(shader->ir);
333 clip_distance.run(shader->ir);
334 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
335 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
336 "and `gl_ClipDistance'\n");
337 return false;
338 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700339 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800340 ir_variable *clip_distance_var =
341 shader->symbols->get_variable("gl_ClipDistance");
342 if (clip_distance_var)
343 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700344 }
345
Ian Romanick832dfa52010-06-17 15:04:20 -0700346 return true;
347}
348
349
Ian Romanickc93b8f12010-06-17 15:20:22 -0700350/**
351 * Verify that a fragment shader executable meets all semantic requirements
352 *
353 * \param shader Fragment shader executable to be verified
354 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700355bool
Eric Anholt849e1812010-06-30 11:49:17 -0700356validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700357 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700358{
359 if (shader == NULL)
360 return true;
361
Ian Romanick832dfa52010-06-17 15:04:20 -0700362 find_assignment_visitor frag_color("gl_FragColor");
363 find_assignment_visitor frag_data("gl_FragData");
364
Eric Anholt16b68b12010-06-30 11:05:43 -0700365 frag_color.run(shader->ir);
366 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700367
Ian Romanick832dfa52010-06-17 15:04:20 -0700368 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700369 linker_error(prog, "fragment shader writes to both "
370 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700371 return false;
372 }
373
374 return true;
375}
376
377
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700378/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700379 * Generate a string describing the mode of a variable
380 */
381static const char *
382mode_string(const ir_variable *var)
383{
384 switch (var->mode) {
385 case ir_var_auto:
386 return (var->read_only) ? "global constant" : "global variable";
387
388 case ir_var_uniform: return "uniform";
389 case ir_var_in: return "shader input";
390 case ir_var_out: return "shader output";
391 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700392
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800393 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700394 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700395 default:
396 assert(!"Should not get here.");
397 return "invalid variable";
398 }
399}
400
401
402/**
403 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700404 */
405bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700406cross_validate_globals(struct gl_shader_program *prog,
407 struct gl_shader **shader_list,
408 unsigned num_shaders,
409 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700410{
411 /* Examine all of the uniforms in all of the shaders and cross validate
412 * them.
413 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700414 glsl_symbol_table variables;
415 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700416 if (shader_list[i] == NULL)
417 continue;
418
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700419 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700420 ir_variable *const var = ((ir_instruction *) node)->as_variable();
421
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700422 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700423 continue;
424
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700425 if (uniforms_only && (var->mode != ir_var_uniform))
426 continue;
427
Ian Romanick7e2aa912010-07-19 17:12:42 -0700428 /* Don't cross validate temporaries that are at global scope. These
429 * will eventually get pulled into the shaders 'main'.
430 */
431 if (var->mode == ir_var_temporary)
432 continue;
433
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700434 /* If a global with this name has already been seen, verify that the
435 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700436 * initializers, the values of the initializers must be the same.
437 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700438 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700439 if (existing != NULL) {
440 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700441 /* Consider the types to be "the same" if both types are arrays
442 * of the same type and one of the arrays is implicitly sized.
443 * In addition, set the type of the linked variable to the
444 * explicitly sized array.
445 */
446 if (var->type->is_array()
447 && existing->type->is_array()
448 && (var->type->fields.array == existing->type->fields.array)
449 && ((var->type->length == 0)
450 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800451 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700452 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800453 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700454 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700455 linker_error(prog, "%s `%s' declared as type "
456 "`%s' and type `%s'\n",
457 mode_string(var),
458 var->name, var->type->name,
459 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700460 return false;
461 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700462 }
463
Ian Romanick68a4fc92010-10-07 17:21:22 -0700464 if (var->explicit_location) {
465 if (existing->explicit_location
466 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700467 linker_error(prog, "explicit locations for %s "
468 "`%s' have differing values\n",
469 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700470 return false;
471 }
472
473 existing->location = var->location;
474 existing->explicit_location = true;
475 }
476
Ian Romanick46173f92011-10-31 13:07:06 -0700477 /* Validate layout qualifiers for gl_FragDepth.
478 *
479 * From the AMD/ARB_conservative_depth specs:
480 *
481 * "If gl_FragDepth is redeclared in any fragment shader in a
482 * program, it must be redeclared in all fragment shaders in
483 * that program that have static assignments to
484 * gl_FragDepth. All redeclarations of gl_FragDepth in all
485 * fragment shaders in a single program must have the same set
486 * of qualifiers."
487 */
488 if (strcmp(var->name, "gl_FragDepth") == 0) {
489 bool layout_declared = var->depth_layout != ir_depth_layout_none;
490 bool layout_differs =
491 var->depth_layout != existing->depth_layout;
492
493 if (layout_declared && layout_differs) {
494 linker_error(prog,
495 "All redeclarations of gl_FragDepth in all "
496 "fragment shaders in a single program must have "
497 "the same set of qualifiers.");
498 }
499
500 if (var->used && layout_differs) {
501 linker_error(prog,
502 "If gl_FragDepth is redeclared with a layout "
503 "qualifier in any fragment shader, it must be "
504 "redeclared with the same layout qualifier in "
505 "all fragment shaders that have assignments to "
506 "gl_FragDepth");
507 }
508 }
Chad Versaceaddae332011-01-27 01:40:31 -0800509
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700510 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
511 *
512 * "If a shared global has multiple initializers, the
513 * initializers must all be constant expressions, and they
514 * must all have the same value. Otherwise, a link error will
515 * result. (A shared global having only one initializer does
516 * not require that initializer to be a constant expression.)"
517 *
518 * Previous to 4.20 the GLSL spec simply said that initializers
519 * must have the same value. In this case of non-constant
520 * initializers, this was impossible to determine. As a result,
521 * no vendor actually implemented that behavior. The 4.20
522 * behavior matches the implemented behavior of at least one other
523 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700524 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700525 if (var->constant_initializer != NULL) {
526 if (existing->constant_initializer != NULL) {
527 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700528 linker_error(prog, "initializers for %s "
529 "`%s' have differing values\n",
530 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700531 return false;
532 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700533 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700534 /* If the first-seen instance of a particular uniform did not
535 * have an initializer but a later instance does, copy the
536 * initializer to the version stored in the symbol table.
537 */
Ian Romanickde415b72010-07-14 13:22:12 -0700538 /* FINISHME: This is wrong. The constant_value field should
539 * FINISHME: not be modified! Imagine a case where a shader
540 * FINISHME: without an initializer is linked in two different
541 * FINISHME: programs with shaders that have differing
542 * FINISHME: initializers. Linking with the first will
543 * FINISHME: modify the shader, and linking with the second
544 * FINISHME: will fail.
545 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700546 existing->constant_initializer =
547 var->constant_initializer->clone(ralloc_parent(existing),
548 NULL);
549 }
550 }
551
552 if (var->has_initializer) {
553 if (existing->has_initializer
554 && (var->constant_initializer == NULL
555 || existing->constant_initializer == NULL)) {
556 linker_error(prog,
557 "shared global variable `%s' has multiple "
558 "non-constant initializers.\n",
559 var->name);
560 return false;
561 }
562
563 /* Some instance had an initializer, so keep track of that. In
564 * this location, all sorts of initializers (constant or
565 * otherwise) will propagate the existence to the variable
566 * stored in the symbol table.
567 */
568 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700569 }
Chad Versace7528f142010-11-17 14:34:38 -0800570
571 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700572 linker_error(prog, "declarations for %s `%s' have "
573 "mismatching invariant qualifiers\n",
574 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800575 return false;
576 }
Chad Versace61428dd2011-01-10 15:29:30 -0800577 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700578 linker_error(prog, "declarations for %s `%s' have "
579 "mismatching centroid qualifiers\n",
580 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800581 return false;
582 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700583 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700584 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700585 }
586 }
587
588 return true;
589}
590
591
Ian Romanick37101922010-06-18 19:02:10 -0700592/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700593 * Perform validation of uniforms used across multiple shader stages
594 */
595bool
596cross_validate_uniforms(struct gl_shader_program *prog)
597{
598 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700599 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700600}
601
Eric Anholtf609cf72012-04-27 13:52:56 -0700602/**
603 * Accumulates the array of prog->UniformBlocks and checks that all
604 * definitons of blocks agree on their contents.
605 */
606static bool
607interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
608{
609 unsigned max_num_uniform_blocks = 0;
610 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
611 if (prog->_LinkedShaders[i])
612 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
613 }
614
615 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
616 struct gl_shader *sh = prog->_LinkedShaders[i];
617
618 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
619 max_num_uniform_blocks);
620 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
621 prog->UniformBlockStageIndex[i][j] = -1;
622
623 if (sh == NULL)
624 continue;
625
626 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
627 int index = link_cross_validate_uniform_block(prog,
628 &prog->UniformBlocks,
629 &prog->NumUniformBlocks,
630 &sh->UniformBlocks[j]);
631
632 if (index == -1) {
633 linker_error(prog, "uniform block `%s' has mismatching definitions",
634 sh->UniformBlocks[j].Name);
635 return false;
636 }
637
638 prog->UniformBlockStageIndex[i][index] = j;
639 }
640 }
641
642 return true;
643}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700644
645/**
Ian Romanick37101922010-06-18 19:02:10 -0700646 * Validate that outputs from one stage match inputs of another
647 */
648bool
Eric Anholt849e1812010-06-30 11:49:17 -0700649cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700650 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700651{
652 glsl_symbol_table parameters;
653 /* FINISHME: Figure these out dynamically. */
654 const char *const producer_stage = "vertex";
655 const char *const consumer_stage = "fragment";
656
657 /* Find all shader outputs in the "producer" stage.
658 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700659 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700660 ir_variable *const var = ((ir_instruction *) node)->as_variable();
661
662 /* FINISHME: For geometry shaders, this should also look for inout
663 * FINISHME: variables.
664 */
665 if ((var == NULL) || (var->mode != ir_var_out))
666 continue;
667
Eric Anholt001eee52010-11-05 06:11:24 -0700668 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700669 }
670
671
672 /* Find all shader inputs in the "consumer" stage. Any variables that have
673 * matching outputs already in the symbol table must have the same type and
674 * qualifiers.
675 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700676 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700677 ir_variable *const input = ((ir_instruction *) node)->as_variable();
678
679 /* FINISHME: For geometry shaders, this should also look for inout
680 * FINISHME: variables.
681 */
682 if ((input == NULL) || (input->mode != ir_var_in))
683 continue;
684
685 ir_variable *const output = parameters.get_variable(input->name);
686 if (output != NULL) {
687 /* Check that the types match between stages.
688 */
689 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800690 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500691 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800692 * access it must redeclare it with a size. There is some
693 * language in the GLSL spec that implies the fragment shader
694 * and vertex shader do not have to agree on this size. Other
695 * driver behave this way, and one or two applications seem to
696 * rely on it.
697 *
698 * Neither declaration needs to be modified here because the array
699 * sizes are fixed later when update_array_sizes is called.
700 *
701 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
702 *
703 * "Unlike user-defined varying variables, the built-in
704 * varying variables don't have a strict one-to-one
705 * correspondence between the vertex language and the
706 * fragment language."
707 */
708 if (!output->type->is_array()
709 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700710 linker_error(prog,
711 "%s shader output `%s' declared as type `%s', "
712 "but %s shader input declared as type `%s'\n",
713 producer_stage, output->name,
714 output->type->name,
715 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800716 return false;
717 }
Ian Romanick37101922010-06-18 19:02:10 -0700718 }
719
720 /* Check that all of the qualifiers match between stages.
721 */
722 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700723 linker_error(prog,
724 "%s shader output `%s' %s centroid qualifier, "
725 "but %s shader input %s centroid qualifier\n",
726 producer_stage,
727 output->name,
728 (output->centroid) ? "has" : "lacks",
729 consumer_stage,
730 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700731 return false;
732 }
733
734 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700735 linker_error(prog,
736 "%s shader output `%s' %s invariant qualifier, "
737 "but %s shader input %s invariant qualifier\n",
738 producer_stage,
739 output->name,
740 (output->invariant) ? "has" : "lacks",
741 consumer_stage,
742 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700743 return false;
744 }
745
746 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700747 linker_error(prog,
748 "%s shader output `%s' specifies %s "
749 "interpolation qualifier, "
750 "but %s shader input specifies %s "
751 "interpolation qualifier\n",
752 producer_stage,
753 output->name,
754 output->interpolation_string(),
755 consumer_stage,
756 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700757 return false;
758 }
759 }
760 }
761
762 return true;
763}
764
765
Ian Romanick3fb87872010-07-09 14:09:34 -0700766/**
767 * Populates a shaders symbol table with all global declarations
768 */
769static void
770populate_symbol_table(gl_shader *sh)
771{
772 sh->symbols = new(sh) glsl_symbol_table;
773
774 foreach_list(node, sh->ir) {
775 ir_instruction *const inst = (ir_instruction *) node;
776 ir_variable *var;
777 ir_function *func;
778
779 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700780 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700781 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700782 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700783 }
784 }
785}
786
787
788/**
Ian Romanick31a97862010-07-12 18:48:50 -0700789 * Remap variables referenced in an instruction tree
790 *
791 * This is used when instruction trees are cloned from one shader and placed in
792 * another. These trees will contain references to \c ir_variable nodes that
793 * do not exist in the target shader. This function finds these \c ir_variable
794 * references and replaces the references with matching variables in the target
795 * shader.
796 *
797 * If there is no matching variable in the target shader, a clone of the
798 * \c ir_variable is made and added to the target shader. The new variable is
799 * added to \b both the instruction stream and the symbol table.
800 *
801 * \param inst IR tree that is to be processed.
802 * \param symbols Symbol table containing global scope symbols in the
803 * linked shader.
804 * \param instructions Instruction stream where new variable declarations
805 * should be added.
806 */
807void
Eric Anholt8273bd42010-08-04 12:34:56 -0700808remap_variables(ir_instruction *inst, struct gl_shader *target,
809 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700810{
811 class remap_visitor : public ir_hierarchical_visitor {
812 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700813 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700814 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700815 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700816 this->target = target;
817 this->symbols = target->symbols;
818 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700819 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700820 }
821
822 virtual ir_visitor_status visit(ir_dereference_variable *ir)
823 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700824 if (ir->var->mode == ir_var_temporary) {
825 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
826
827 assert(var != NULL);
828 ir->var = var;
829 return visit_continue;
830 }
831
Ian Romanick31a97862010-07-12 18:48:50 -0700832 ir_variable *const existing =
833 this->symbols->get_variable(ir->var->name);
834 if (existing != NULL)
835 ir->var = existing;
836 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700837 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700838
Eric Anholt001eee52010-11-05 06:11:24 -0700839 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700840 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700841 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700842 }
843
844 return visit_continue;
845 }
846
847 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700848 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700849 glsl_symbol_table *symbols;
850 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700851 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700852 };
853
Eric Anholt8273bd42010-08-04 12:34:56 -0700854 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700855
856 inst->accept(&v);
857}
858
859
860/**
861 * Move non-declarations from one instruction stream to another
862 *
863 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700864 * 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 -0700865 * pointer) for \c last and \c false for \c make_copies on the first
866 * call. Successive calls pass the return value of the previous call for
867 * \c last and \c true for \c make_copies.
868 *
869 * \param instructions Source instruction stream
870 * \param last Instruction after which new instructions should be
871 * inserted in the target instruction stream
872 * \param make_copies Flag selecting whether instructions in \c instructions
873 * should be copied (via \c ir_instruction::clone) into the
874 * target list or moved.
875 *
876 * \return
877 * The new "last" instruction in the target instruction stream. This pointer
878 * is suitable for use as the \c last parameter of a later call to this
879 * function.
880 */
881exec_node *
882move_non_declarations(exec_list *instructions, exec_node *last,
883 bool make_copies, gl_shader *target)
884{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700885 hash_table *temps = NULL;
886
887 if (make_copies)
888 temps = hash_table_ctor(0, hash_table_pointer_hash,
889 hash_table_pointer_compare);
890
Ian Romanick303c99f2010-07-19 12:34:56 -0700891 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700892 ir_instruction *inst = (ir_instruction *) node;
893
Ian Romanick7e2aa912010-07-19 17:12:42 -0700894 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700895 continue;
896
Ian Romanick7e2aa912010-07-19 17:12:42 -0700897 ir_variable *var = inst->as_variable();
898 if ((var != NULL) && (var->mode != ir_var_temporary))
899 continue;
900
901 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700902 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700903 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700904 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700905
906 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700907 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700908
909 if (var != NULL)
910 hash_table_insert(temps, inst, var);
911 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700912 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700913 } else {
914 inst->remove();
915 }
916
917 last->insert_after(inst);
918 last = inst;
919 }
920
Ian Romanick7e2aa912010-07-19 17:12:42 -0700921 if (make_copies)
922 hash_table_dtor(temps);
923
Ian Romanick31a97862010-07-12 18:48:50 -0700924 return last;
925}
926
927/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700928 * Get the function signature for main from a shader
929 */
930static ir_function_signature *
931get_main_function_signature(gl_shader *sh)
932{
933 ir_function *const f = sh->symbols->get_function("main");
934 if (f != NULL) {
935 exec_list void_parameters;
936
937 /* Look for the 'void main()' signature and ensure that it's defined.
938 * This keeps the linker from accidentally pick a shader that just
939 * contains a prototype for main.
940 *
941 * We don't have to check for multiple definitions of main (in multiple
942 * shaders) because that would have already been caught above.
943 */
944 ir_function_signature *sig = f->matching_signature(&void_parameters);
945 if ((sig != NULL) && sig->is_defined) {
946 return sig;
947 }
948 }
949
950 return NULL;
951}
952
953
954/**
Brian Paul84a12732012-02-02 20:10:40 -0700955 * This class is only used in link_intrastage_shaders() below but declaring
956 * it inside that function leads to compiler warnings with some versions of
957 * gcc.
958 */
959class array_sizing_visitor : public ir_hierarchical_visitor {
960public:
961 virtual ir_visitor_status visit(ir_variable *var)
962 {
963 if (var->type->is_array() && (var->type->length == 0)) {
964 const glsl_type *type =
965 glsl_type::get_array_instance(var->type->fields.array,
966 var->max_array_access + 1);
967 assert(type != NULL);
968 var->type = type;
969 }
970 return visit_continue;
971 }
972};
973
Brian Paul84a12732012-02-02 20:10:40 -0700974/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700975 * Combine a group of shaders for a single stage to generate a linked shader
976 *
977 * \note
978 * If this function is supplied a single shader, it is cloned, and the new
979 * shader is returned.
980 */
981static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800982link_intrastage_shaders(void *mem_ctx,
983 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700984 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700985 struct gl_shader **shader_list,
986 unsigned num_shaders)
987{
Eric Anholtf609cf72012-04-27 13:52:56 -0700988 struct gl_uniform_block *uniform_blocks = NULL;
989 unsigned num_uniform_blocks = 0;
990
Ian Romanick13f782c2010-06-29 18:53:38 -0700991 /* Check that global variables defined in multiple shaders are consistent.
992 */
993 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
994 return NULL;
995
Eric Anholtf609cf72012-04-27 13:52:56 -0700996 /* Check that uniform blocks between shaders for a stage agree. */
997 for (unsigned i = 0; i < num_shaders; i++) {
998 struct gl_shader *sh = shader_list[i];
999
1000 for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) {
Eric Anholt8ab58422012-05-01 15:10:14 -07001001 link_assign_uniform_block_offsets(shader_list[i]);
1002
Eric Anholtf609cf72012-04-27 13:52:56 -07001003 int index = link_cross_validate_uniform_block(mem_ctx,
1004 &uniform_blocks,
1005 &num_uniform_blocks,
1006 &sh->UniformBlocks[j]);
1007 if (index == -1) {
1008 linker_error(prog, "uniform block `%s' has mismatching definitions",
1009 sh->UniformBlocks[j].Name);
1010 return NULL;
1011 }
1012 }
1013 }
1014
Ian Romanick13f782c2010-06-29 18:53:38 -07001015 /* Check that there is only a single definition of each function signature
1016 * across all shaders.
1017 */
1018 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1019 foreach_list(node, shader_list[i]->ir) {
1020 ir_function *const f = ((ir_instruction *) node)->as_function();
1021
1022 if (f == NULL)
1023 continue;
1024
1025 for (unsigned j = i + 1; j < num_shaders; j++) {
1026 ir_function *const other =
1027 shader_list[j]->symbols->get_function(f->name);
1028
1029 /* If the other shader has no function (and therefore no function
1030 * signatures) with the same name, skip to the next shader.
1031 */
1032 if (other == NULL)
1033 continue;
1034
1035 foreach_iter (exec_list_iterator, iter, *f) {
1036 ir_function_signature *sig =
1037 (ir_function_signature *) iter.get();
1038
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001039 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -07001040 continue;
1041
1042 ir_function_signature *other_sig =
1043 other->exact_matching_signature(& sig->parameters);
1044
1045 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001046 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -07001047 linker_error(prog, "function `%s' is multiply defined",
1048 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001049 return NULL;
1050 }
1051 }
1052 }
1053 }
1054 }
1055
1056 /* Find the shader that defines main, and make a clone of it.
1057 *
1058 * Starting with the clone, search for undefined references. If one is
1059 * found, find the shader that defines it. Clone the reference and add
1060 * it to the shader. Repeat until there are no undefined references or
1061 * until a reference cannot be resolved.
1062 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001063 gl_shader *main = NULL;
1064 for (unsigned i = 0; i < num_shaders; i++) {
1065 if (get_main_function_signature(shader_list[i]) != NULL) {
1066 main = shader_list[i];
1067 break;
1068 }
1069 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001070
Ian Romanick15ce87e2010-07-09 15:28:22 -07001071 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001072 linker_error(prog, "%s shader lacks `main'\n",
1073 (shader_list[0]->Type == GL_VERTEX_SHADER)
1074 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -07001075 return NULL;
1076 }
1077
Ian Romanick4a455952010-10-13 15:13:02 -07001078 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001079 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001080 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001081
Eric Anholtf609cf72012-04-27 13:52:56 -07001082 linked->UniformBlocks = uniform_blocks;
1083 linked->NumUniformBlocks = num_uniform_blocks;
1084 ralloc_steal(linked, linked->UniformBlocks);
1085
Ian Romanick15ce87e2010-07-09 15:28:22 -07001086 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001087
Ian Romanick31a97862010-07-12 18:48:50 -07001088 /* The a pointer to the main function in the final linked shader (i.e., the
1089 * copy of the original shader that contained the main function).
1090 */
1091 ir_function_signature *const main_sig = get_main_function_signature(linked);
1092
1093 /* Move any instructions other than variable declarations or function
1094 * declarations into main.
1095 */
Ian Romanick9303e352010-07-19 12:33:54 -07001096 exec_node *insertion_point =
1097 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1098 linked);
1099
Ian Romanick31a97862010-07-12 18:48:50 -07001100 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001101 if (shader_list[i] == main)
1102 continue;
1103
Ian Romanick31a97862010-07-12 18:48:50 -07001104 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001105 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001106 }
1107
Ian Romanick13f782c2010-06-29 18:53:38 -07001108 /* Resolve initializers for global variables in the linked shader.
1109 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001110 unsigned num_linking_shaders = num_shaders;
1111 for (unsigned i = 0; i < num_shaders; i++)
1112 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1113
1114 gl_shader **linking_shaders =
1115 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1116
1117 memcpy(linking_shaders, shader_list,
1118 sizeof(linking_shaders[0]) * num_shaders);
1119
1120 unsigned idx = num_shaders;
1121 for (unsigned i = 0; i < num_shaders; i++) {
1122 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1123 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1124 idx += shader_list[i]->num_builtins_to_link;
1125 }
1126
1127 assert(idx == num_linking_shaders);
1128
Ian Romanick4a455952010-10-13 15:13:02 -07001129 if (!link_function_calls(prog, linked, linking_shaders,
1130 num_linking_shaders)) {
1131 ctx->Driver.DeleteShader(ctx, linked);
1132 linked = NULL;
1133 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001134
1135 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001136
Paul Berryc148ef62011-08-03 15:37:01 -07001137#ifdef DEBUG
1138 /* At this point linked should contain all of the linked IR, so
1139 * validate it to make sure nothing went wrong.
1140 */
1141 if (linked)
1142 validate_ir_tree(linked->ir);
1143#endif
1144
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001145 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001146 * unspecified sizes have a size specified. The size is inferred from the
1147 * max_array_access field.
1148 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001149 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001150 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001151
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001152 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001153 }
1154
Ian Romanick3fb87872010-07-09 14:09:34 -07001155 return linked;
1156}
1157
Eric Anholta721abf2010-08-23 10:32:01 -07001158/**
1159 * Update the sizes of linked shader uniform arrays to the maximum
1160 * array index used.
1161 *
1162 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1163 *
1164 * If one or more elements of an array are active,
1165 * GetActiveUniform will return the name of the array in name,
1166 * subject to the restrictions listed above. The type of the array
1167 * is returned in type. The size parameter contains the highest
1168 * array element index used, plus one. The compiler or linker
1169 * determines the highest index used. There will be only one
1170 * active uniform reported by the GL per uniform array.
1171
1172 */
1173static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001174update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001175{
Ian Romanick3322fba2010-10-14 13:28:42 -07001176 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1177 if (prog->_LinkedShaders[i] == NULL)
1178 continue;
1179
Eric Anholta721abf2010-08-23 10:32:01 -07001180 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1181 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1182
Eric Anholt586b4b52010-09-28 14:32:16 -07001183 if ((var == NULL) || (var->mode != ir_var_uniform &&
1184 var->mode != ir_var_in &&
1185 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001186 !var->type->is_array())
1187 continue;
1188
Eric Anholt9feb4032012-05-01 14:43:31 -07001189 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1190 * will not be eliminated. Since we always do std140, just
1191 * don't resize arrays in UBOs.
1192 */
1193 if (var->uniform_block != -1)
1194 continue;
1195
Eric Anholta721abf2010-08-23 10:32:01 -07001196 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001197 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1198 if (prog->_LinkedShaders[j] == NULL)
1199 continue;
1200
Eric Anholta721abf2010-08-23 10:32:01 -07001201 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1202 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1203 if (!other_var)
1204 continue;
1205
1206 if (strcmp(var->name, other_var->name) == 0 &&
1207 other_var->max_array_access > size) {
1208 size = other_var->max_array_access;
1209 }
1210 }
1211 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001212
Eric Anholta721abf2010-08-23 10:32:01 -07001213 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001214 /* If this is a built-in uniform (i.e., it's backed by some
1215 * fixed-function state), adjust the number of state slots to
1216 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001217 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001218 * slots is an integer multiple of the number of array elements.
1219 * Determine the number of slots per array element by dividing by
1220 * the old (total) size.
1221 */
1222 if (var->num_state_slots > 0) {
1223 var->num_state_slots = (size + 1)
1224 * (var->num_state_slots / var->type->length);
1225 }
1226
Eric Anholta721abf2010-08-23 10:32:01 -07001227 var->type = glsl_type::get_array_instance(var->type->fields.array,
1228 size + 1);
1229 /* FINISHME: We should update the types of array
1230 * dereferences of this variable now.
1231 */
1232 }
1233 }
1234 }
1235}
1236
Ian Romanick69846702010-06-22 17:29:19 -07001237/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001238 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001239 *
1240 * \param used_mask Bits representing used (1) and unused (0) locations
1241 * \param needed_count Number of contiguous bits needed.
1242 *
1243 * \return
1244 * Base location of the available bits on success or -1 on failure.
1245 */
1246int
1247find_available_slots(unsigned used_mask, unsigned needed_count)
1248{
1249 unsigned needed_mask = (1 << needed_count) - 1;
1250 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1251
1252 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1253 * cannot optimize possibly infinite loops" for the loop below.
1254 */
1255 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1256 return -1;
1257
1258 for (int i = 0; i <= max_bit_to_test; i++) {
1259 if ((needed_mask & ~used_mask) == needed_mask)
1260 return i;
1261
1262 needed_mask <<= 1;
1263 }
1264
1265 return -1;
1266}
1267
1268
Ian Romanickd32d4f72011-06-27 17:59:58 -07001269/**
1270 * Assign locations for either VS inputs for FS outputs
1271 *
1272 * \param prog Shader program whose variables need locations assigned
1273 * \param target_index Selector for the program target to receive location
1274 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1275 * \c MESA_SHADER_FRAGMENT.
1276 * \param max_index Maximum number of generic locations. This corresponds
1277 * to either the maximum number of draw buffers or the
1278 * maximum number of generic attributes.
1279 *
1280 * \return
1281 * If locations are successfully assigned, true is returned. Otherwise an
1282 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001283 */
Ian Romanick69846702010-06-22 17:29:19 -07001284bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001285assign_attribute_or_color_locations(gl_shader_program *prog,
1286 unsigned target_index,
1287 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001288{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001289 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001290 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001291 unsigned used_locations = (max_index >= 32)
1292 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001293
Ian Romanickd32d4f72011-06-27 17:59:58 -07001294 assert((target_index == MESA_SHADER_VERTEX)
1295 || (target_index == MESA_SHADER_FRAGMENT));
1296
1297 gl_shader *const sh = prog->_LinkedShaders[target_index];
1298 if (sh == NULL)
1299 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001300
Ian Romanick69846702010-06-22 17:29:19 -07001301 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001302 *
1303 * 1. Invalidate the location assignments for all vertex shader inputs.
1304 *
1305 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001306 * glBindVertexAttribLocation) locations and outputs that have
1307 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001308 *
Ian Romanick69846702010-06-22 17:29:19 -07001309 * 3. Sort the attributes without assigned locations by number of slots
1310 * required in decreasing order. Fragmentation caused by attribute
1311 * locations assigned by the application may prevent large attributes
1312 * from having enough contiguous space.
1313 *
1314 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001315 */
1316
Ian Romanickd32d4f72011-06-27 17:59:58 -07001317 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001318 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001319
Ian Romanickd32d4f72011-06-27 17:59:58 -07001320 const enum ir_variable_mode direction =
1321 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1322
1323
Ian Romanick69846702010-06-22 17:29:19 -07001324 /* Temporary storage for the set of attributes that need locations assigned.
1325 */
1326 struct temp_attr {
1327 unsigned slots;
1328 ir_variable *var;
1329
1330 /* Used below in the call to qsort. */
1331 static int compare(const void *a, const void *b)
1332 {
1333 const temp_attr *const l = (const temp_attr *) a;
1334 const temp_attr *const r = (const temp_attr *) b;
1335
1336 /* Reversed because we want a descending order sort below. */
1337 return r->slots - l->slots;
1338 }
1339 } to_assign[16];
1340
1341 unsigned num_attr = 0;
1342
Eric Anholt16b68b12010-06-30 11:05:43 -07001343 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001344 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1345
Brian Paul4470ff22011-07-19 21:10:25 -06001346 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001347 continue;
1348
Ian Romanick68a4fc92010-10-07 17:21:22 -07001349 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001350 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001351 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001352 linker_error(prog,
1353 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001354 (var->location < 0)
1355 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001356 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001357 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001358 }
1359 } else if (target_index == MESA_SHADER_VERTEX) {
1360 unsigned binding;
1361
1362 if (prog->AttributeBindings->get(binding, var->name)) {
1363 assert(binding >= VERT_ATTRIB_GENERIC0);
1364 var->location = binding;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001365 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001366 } else if (target_index == MESA_SHADER_FRAGMENT) {
1367 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001368 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001369
1370 if (prog->FragDataBindings->get(binding, var->name)) {
1371 assert(binding >= FRAG_RESULT_DATA0);
1372 var->location = binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001373
1374 if (prog->FragDataIndexBindings->get(index, var->name)) {
1375 var->index = index;
1376 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001377 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001378 }
1379
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001380 /* If the variable is not a built-in and has a location statically
1381 * assigned in the shader (presumably via a layout qualifier), make sure
1382 * that it doesn't collide with other assigned locations. Otherwise,
1383 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001384 */
Ian Romanick523b6112011-08-17 15:40:03 -07001385 const unsigned slots = count_attribute_slots(var->type);
1386 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001387 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001388 /* From page 61 of the OpenGL 4.0 spec:
1389 *
1390 * "LinkProgram will fail if the attribute bindings assigned
1391 * by BindAttribLocation do not leave not enough space to
1392 * assign a location for an active matrix attribute or an
1393 * active attribute array, both of which require multiple
1394 * contiguous generic attributes."
1395 *
1396 * Previous versions of the spec contain similar language but omit
1397 * the bit about attribute arrays.
1398 *
1399 * Page 61 of the OpenGL 4.0 spec also says:
1400 *
1401 * "It is possible for an application to bind more than one
1402 * attribute name to the same location. This is referred to as
1403 * aliasing. This will only work if only one of the aliased
1404 * attributes is active in the executable program, or if no
1405 * path through the shader consumes more than one attribute of
1406 * a set of attributes aliased to the same location. A link
1407 * error can occur if the linker determines that every path
1408 * through the shader consumes multiple aliased attributes,
1409 * but implementations are not required to generate an error
1410 * in this case."
1411 *
1412 * These two paragraphs are either somewhat contradictory, or I
1413 * don't fully understand one or both of them.
1414 */
1415 /* FINISHME: The code as currently written does not support
1416 * FINISHME: attribute location aliasing (see comment above).
1417 */
1418 /* Mask representing the contiguous slots that will be used by
1419 * this attribute.
1420 */
1421 const unsigned attr = var->location - generic_base;
1422 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001423
Ian Romanick523b6112011-08-17 15:40:03 -07001424 /* Generate a link error if the set of bits requested for this
1425 * attribute overlaps any previously allocated bits.
1426 */
1427 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001428 const char *const string = (target_index == MESA_SHADER_VERTEX)
1429 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001430 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001431 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001432 "available for %s `%s' %d %d %d", string,
1433 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001434 return false;
1435 }
1436
1437 used_locations |= (use_mask << attr);
1438 }
1439
1440 continue;
1441 }
1442
1443 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001444 to_assign[num_attr].var = var;
1445 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001446 }
Ian Romanick69846702010-06-22 17:29:19 -07001447
1448 /* If all of the attributes were assigned locations by the application (or
1449 * are built-in attributes with fixed locations), return early. This should
1450 * be the common case.
1451 */
1452 if (num_attr == 0)
1453 return true;
1454
1455 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1456
Ian Romanickd32d4f72011-06-27 17:59:58 -07001457 if (target_index == MESA_SHADER_VERTEX) {
1458 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1459 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1460 * reserved to prevent it from being automatically allocated below.
1461 */
1462 find_deref_visitor find("gl_Vertex");
1463 find.run(sh->ir);
1464 if (find.variable_found())
1465 used_locations |= (1 << 0);
1466 }
Ian Romanick982e3792010-06-29 18:58:20 -07001467
Ian Romanick69846702010-06-22 17:29:19 -07001468 for (unsigned i = 0; i < num_attr; i++) {
1469 /* Mask representing the contiguous slots that will be used by this
1470 * attribute.
1471 */
1472 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1473
1474 int location = find_available_slots(used_locations, to_assign[i].slots);
1475
1476 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001477 const char *const string = (target_index == MESA_SHADER_VERTEX)
1478 ? "vertex shader input" : "fragment shader output";
1479
Ian Romanick586e7412011-07-28 14:04:09 -07001480 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001481 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001482 "available for %s `%s'",
1483 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001484 return false;
1485 }
1486
Ian Romanickd32d4f72011-06-27 17:59:58 -07001487 to_assign[i].var->location = generic_base + location;
Ian Romanick69846702010-06-22 17:29:19 -07001488 used_locations |= (use_mask << location);
1489 }
1490
1491 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001492}
1493
1494
Ian Romanick40e114b2010-08-17 14:55:50 -07001495/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001496 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001497 */
1498void
Ian Romanickcc90e622010-10-19 17:59:10 -07001499demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001500{
1501 foreach_list(node, sh->ir) {
1502 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1503
Ian Romanickcc90e622010-10-19 17:59:10 -07001504 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001505 continue;
1506
Ian Romanickcc90e622010-10-19 17:59:10 -07001507 /* A shader 'in' or 'out' variable is only really an input or output if
1508 * its value is used by other shader stages. This will cause the variable
1509 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001510 */
1511 if (var->location == -1) {
1512 var->mode = ir_var_auto;
1513 }
1514 }
1515}
1516
1517
Paul Berry871ddb92011-11-05 11:17:32 -07001518/**
1519 * Data structure tracking information about a transform feedback declaration
1520 * during linking.
1521 */
1522class tfeedback_decl
1523{
1524public:
Paul Berry456279b2011-12-26 19:39:25 -08001525 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1526 const void *mem_ctx, const char *input);
Paul Berry871ddb92011-11-05 11:17:32 -07001527 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1528 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1529 ir_variable *output_var);
Christoph Bumillerd540af52012-01-20 13:24:46 +01001530 bool accumulate_num_outputs(struct gl_shader_program *prog, unsigned *count);
Paul Berryd3150eb2012-01-09 11:25:14 -08001531 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001532 struct gl_transform_feedback_info *info, unsigned buffer,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001533 const unsigned max_outputs) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001534
1535 /**
1536 * True if assign_location() has been called for this object.
1537 */
1538 bool is_assigned() const
1539 {
1540 return this->location != -1;
1541 }
1542
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001543 bool is_next_buffer_separator() const
1544 {
1545 return this->next_buffer_separator;
1546 }
1547
1548 bool is_varying() const
1549 {
1550 return !this->next_buffer_separator && !this->skip_components;
1551 }
1552
Paul Berry871ddb92011-11-05 11:17:32 -07001553 /**
1554 * Determine whether this object refers to the variable var.
1555 */
1556 bool matches_var(ir_variable *var) const
1557 {
Paul Berry642e5b412012-01-04 13:57:52 -08001558 if (this->is_clip_distance_mesa)
1559 return strcmp(var->name, "gl_ClipDistanceMESA") == 0;
1560 else
1561 return strcmp(var->name, this->var_name) == 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001562 }
1563
1564 /**
1565 * The total number of varying components taken up by this variable. Only
1566 * valid if is_assigned() is true.
1567 */
1568 unsigned num_components() const
1569 {
Paul Berry642e5b412012-01-04 13:57:52 -08001570 if (this->is_clip_distance_mesa)
1571 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001572 else
Paul Berry642e5b412012-01-04 13:57:52 -08001573 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001574 }
1575
1576private:
1577 /**
1578 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001579 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001580 */
1581 const char *orig_name;
1582
1583 /**
1584 * The name of the variable, parsed from orig_name.
1585 */
Paul Berry913a5c22011-12-27 08:24:57 -08001586 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001587
1588 /**
1589 * True if the declaration in orig_name represents an array.
1590 */
Paul Berry33fe0212012-01-03 20:41:34 -08001591 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001592
1593 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001594 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001595 */
Paul Berry33fe0212012-01-03 20:41:34 -08001596 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001597
1598 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001599 * True if the variable is gl_ClipDistance and the driver lowers
1600 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001601 */
Paul Berry642e5b412012-01-04 13:57:52 -08001602 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001603
1604 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001605 * The vertex shader output location that the linker assigned for this
1606 * variable. -1 if a location hasn't been assigned yet.
1607 */
1608 int location;
1609
1610 /**
1611 * If location != -1, the number of vector elements in this variable, or 1
1612 * if this variable is a scalar.
1613 */
1614 unsigned vector_elements;
1615
1616 /**
1617 * If location != -1, the number of matrix columns in this variable, or 1
1618 * if this variable is not a matrix.
1619 */
1620 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001621
1622 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1623 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001624
1625 /**
1626 * If location != -1, the size that should be returned by
1627 * glGetTransformFeedbackVarying().
1628 */
1629 unsigned size;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001630
1631 /**
1632 * How many components to skip. If non-zero, this is
1633 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1634 */
1635 unsigned skip_components;
1636
1637 /**
1638 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1639 */
1640 bool next_buffer_separator;
Paul Berry871ddb92011-11-05 11:17:32 -07001641};
1642
1643
1644/**
1645 * Initialize this object based on a string that was passed to
1646 * glTransformFeedbackVaryings. If there is a parse error, the error is
1647 * reported using linker_error(), and false is returned.
1648 */
1649bool
Paul Berry456279b2011-12-26 19:39:25 -08001650tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1651 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001652{
1653 /* We don't have to be pedantic about what is a valid GLSL variable name,
1654 * because any variable with an invalid name can't exist in the IR anyway.
1655 */
1656
1657 this->location = -1;
1658 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001659 this->is_clip_distance_mesa = false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001660 this->skip_components = 0;
1661 this->next_buffer_separator = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001662
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001663 if (ctx->Extensions.ARB_transform_feedback3) {
1664 /* Parse gl_NextBuffer. */
1665 if (strcmp(input, "gl_NextBuffer") == 0) {
1666 this->next_buffer_separator = true;
1667 return true;
1668 }
1669
1670 /* Parse gl_SkipComponents. */
1671 if (strcmp(input, "gl_SkipComponents1") == 0)
1672 this->skip_components = 1;
1673 else if (strcmp(input, "gl_SkipComponents2") == 0)
1674 this->skip_components = 2;
1675 else if (strcmp(input, "gl_SkipComponents3") == 0)
1676 this->skip_components = 3;
1677 else if (strcmp(input, "gl_SkipComponents4") == 0)
1678 this->skip_components = 4;
1679
1680 if (this->skip_components)
1681 return true;
1682 }
1683
1684 /* Parse a declaration. */
Paul Berry871ddb92011-11-05 11:17:32 -07001685 const char *bracket = strrchr(input, '[');
1686
1687 if (bracket) {
1688 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001689 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001690 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1691 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001692 }
Paul Berry33fe0212012-01-03 20:41:34 -08001693 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001694 } else {
1695 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001696 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001697 }
1698
Paul Berry642e5b412012-01-04 13:57:52 -08001699 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1700 * class must behave specially to account for the fact that gl_ClipDistance
1701 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001702 */
1703 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1704 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001705 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001706 }
1707
1708 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001709}
1710
1711
1712/**
1713 * Determine whether two tfeedback_decl objects refer to the same variable and
1714 * array index (if applicable).
1715 */
1716bool
1717tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1718{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001719 assert(x.is_varying() && y.is_varying());
1720
Paul Berry871ddb92011-11-05 11:17:32 -07001721 if (strcmp(x.var_name, y.var_name) != 0)
1722 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001723 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001724 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001725 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001726 return false;
1727 return true;
1728}
1729
1730
1731/**
1732 * Assign a location for this tfeedback_decl object based on the location
1733 * assignment in output_var.
1734 *
1735 * If an error occurs, the error is reported through linker_error() and false
1736 * is returned.
1737 */
1738bool
1739tfeedback_decl::assign_location(struct gl_context *ctx,
1740 struct gl_shader_program *prog,
1741 ir_variable *output_var)
1742{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001743 assert(this->is_varying());
1744
Paul Berry871ddb92011-11-05 11:17:32 -07001745 if (output_var->type->is_array()) {
1746 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001747 const unsigned matrix_cols =
1748 output_var->type->fields.array->matrix_columns;
Paul Berry642e5b412012-01-04 13:57:52 -08001749 unsigned actual_array_size = this->is_clip_distance_mesa ?
1750 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001751
1752 if (this->is_subscripted) {
1753 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001754 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001755 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001756 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001757 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001758 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001759 return false;
1760 }
Paul Berry642e5b412012-01-04 13:57:52 -08001761 if (this->is_clip_distance_mesa) {
1762 this->location =
1763 output_var->location + this->array_subscript / 4;
1764 } else {
1765 this->location =
1766 output_var->location + this->array_subscript * matrix_cols;
1767 }
Paul Berry33fe0212012-01-03 20:41:34 -08001768 this->size = 1;
1769 } else {
1770 this->location = output_var->location;
Paul Berry642e5b412012-01-04 13:57:52 -08001771 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001772 }
Paul Berry871ddb92011-11-05 11:17:32 -07001773 this->vector_elements = output_var->type->fields.array->vector_elements;
1774 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001775 if (this->is_clip_distance_mesa)
1776 this->type = GL_FLOAT;
1777 else
1778 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001779 } else {
1780 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001781 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001782 linker_error(prog, "Transform feedback varying %s requested, "
1783 "but %s is not an array.",
1784 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001785 return false;
1786 }
1787 this->location = output_var->location;
Paul Berry33fe0212012-01-03 20:41:34 -08001788 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001789 this->vector_elements = output_var->type->vector_elements;
1790 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001791 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001792 }
Paul Berry642e5b412012-01-04 13:57:52 -08001793
Paul Berry871ddb92011-11-05 11:17:32 -07001794 /* From GL_EXT_transform_feedback:
1795 * A program will fail to link if:
1796 *
1797 * * the total number of components to capture in any varying
1798 * variable in <varyings> is greater than the constant
1799 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1800 * buffer mode is SEPARATE_ATTRIBS_EXT;
1801 */
1802 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1803 this->num_components() >
1804 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1805 linker_error(prog, "Transform feedback varying %s exceeds "
1806 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1807 this->orig_name);
1808 return false;
1809 }
1810
1811 return true;
1812}
1813
1814
Paul Berry871ddb92011-11-05 11:17:32 -07001815bool
Christoph Bumillerd540af52012-01-20 13:24:46 +01001816tfeedback_decl::accumulate_num_outputs(struct gl_shader_program *prog,
1817 unsigned *count)
Paul Berry871ddb92011-11-05 11:17:32 -07001818{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001819 if (!this->is_varying()) {
1820 return true;
1821 }
1822
Paul Berry871ddb92011-11-05 11:17:32 -07001823 if (!this->is_assigned()) {
1824 /* From GL_EXT_transform_feedback:
1825 * A program will fail to link if:
1826 *
1827 * * any variable name specified in the <varyings> array is not
1828 * declared as an output in the geometry shader (if present) or
1829 * the vertex shader (if no geometry shader is present);
1830 */
1831 linker_error(prog, "Transform feedback varying %s undeclared.",
1832 this->orig_name);
1833 return false;
1834 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001835
Christoph Bumillerd540af52012-01-20 13:24:46 +01001836 unsigned translated_size = this->size;
1837 if (this->is_clip_distance_mesa)
1838 translated_size = (translated_size + 3) / 4;
1839
1840 *count += translated_size * this->matrix_columns;
1841
1842 return true;
1843}
1844
1845
1846/**
1847 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1848 *
1849 * If an error occurs, the error is reported through linker_error() and false
1850 * is returned.
1851 */
1852bool
1853tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1854 struct gl_transform_feedback_info *info,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001855 unsigned buffer, const unsigned max_outputs) const
Christoph Bumillerd540af52012-01-20 13:24:46 +01001856{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001857 assert(!this->next_buffer_separator);
1858
1859 /* Handle gl_SkipComponents. */
1860 if (this->skip_components) {
1861 info->BufferStride[buffer] += this->skip_components;
1862 return true;
1863 }
1864
Paul Berryd3150eb2012-01-09 11:25:14 -08001865 /* From GL_EXT_transform_feedback:
1866 * A program will fail to link if:
1867 *
1868 * * the total number of components to capture is greater than
1869 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1870 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1871 */
1872 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1873 info->BufferStride[buffer] + this->num_components() >
1874 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1875 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1876 "limit has been exceeded.");
1877 return false;
1878 }
1879
Paul Berry642e5b412012-01-04 13:57:52 -08001880 unsigned translated_size = this->size;
1881 if (this->is_clip_distance_mesa)
1882 translated_size = (translated_size + 3) / 4;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001883 unsigned components_so_far = 0;
Paul Berry642e5b412012-01-04 13:57:52 -08001884 for (unsigned index = 0; index < translated_size; ++index) {
Paul Berry33fe0212012-01-03 20:41:34 -08001885 for (unsigned v = 0; v < this->matrix_columns; ++v) {
Paul Berry642e5b412012-01-04 13:57:52 -08001886 unsigned num_components = this->vector_elements;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001887 assert(info->NumOutputs < max_outputs);
Paul Berry642e5b412012-01-04 13:57:52 -08001888 info->Outputs[info->NumOutputs].ComponentOffset = 0;
1889 if (this->is_clip_distance_mesa) {
1890 if (this->is_subscripted) {
1891 num_components = 1;
1892 info->Outputs[info->NumOutputs].ComponentOffset =
1893 this->array_subscript % 4;
1894 } else {
1895 num_components = MIN2(4, this->size - components_so_far);
1896 }
1897 }
Paul Berry33fe0212012-01-03 20:41:34 -08001898 info->Outputs[info->NumOutputs].OutputRegister =
1899 this->location + v + index * this->matrix_columns;
1900 info->Outputs[info->NumOutputs].NumComponents = num_components;
1901 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1902 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
Paul Berry33fe0212012-01-03 20:41:34 -08001903 ++info->NumOutputs;
1904 info->BufferStride[buffer] += num_components;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001905 components_so_far += num_components;
Paul Berry33fe0212012-01-03 20:41:34 -08001906 }
Paul Berry871ddb92011-11-05 11:17:32 -07001907 }
Paul Berrybe4e9f72012-01-04 12:21:55 -08001908 assert(components_so_far == this->num_components());
Eric Anholt9d36c962012-01-02 17:08:13 -08001909
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001910 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1911 info->Varyings[info->NumVarying].Type = this->type;
1912 info->Varyings[info->NumVarying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001913 info->NumVarying++;
1914
Paul Berry871ddb92011-11-05 11:17:32 -07001915 return true;
1916}
1917
1918
1919/**
1920 * Parse all the transform feedback declarations that were passed to
1921 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1922 *
1923 * If an error occurs, the error is reported through linker_error() and false
1924 * is returned.
1925 */
1926static bool
Paul Berry456279b2011-12-26 19:39:25 -08001927parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1928 const void *mem_ctx, unsigned num_names,
1929 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001930{
1931 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001932 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001933 return false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001934
1935 if (!decls[i].is_varying())
1936 continue;
1937
Paul Berry871ddb92011-11-05 11:17:32 -07001938 /* From GL_EXT_transform_feedback:
1939 * A program will fail to link if:
1940 *
1941 * * any two entries in the <varyings> array specify the same varying
1942 * variable;
1943 *
1944 * We interpret this to mean "any two entries in the <varyings> array
1945 * specify the same varying variable and array index", since transform
1946 * feedback of arrays would be useless otherwise.
1947 */
1948 for (unsigned j = 0; j < i; ++j) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001949 if (!decls[j].is_varying())
1950 continue;
1951
Paul Berry871ddb92011-11-05 11:17:32 -07001952 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1953 linker_error(prog, "Transform feedback varying %s specified "
1954 "more than once.", varying_names[i]);
1955 return false;
1956 }
1957 }
1958 }
1959 return true;
1960}
1961
1962
1963/**
1964 * Assign a location for a variable that is produced in one pipeline stage
1965 * (the "producer") and consumed in the next stage (the "consumer").
1966 *
1967 * \param input_var is the input variable declaration in the consumer.
1968 *
1969 * \param output_var is the output variable declaration in the producer.
1970 *
1971 * \param input_index is the counter that keeps track of assigned input
1972 * locations in the consumer.
1973 *
1974 * \param output_index is the counter that keeps track of assigned output
1975 * locations in the producer.
1976 *
1977 * It is permissible for \c input_var to be NULL (this happens if a variable
1978 * is output by the producer and consumed by transform feedback, but not
1979 * consumed by the consumer).
1980 *
1981 * If the variable has already been assigned a location, this function has no
1982 * effect.
1983 */
1984void
1985assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1986 unsigned *input_index, unsigned *output_index)
1987{
1988 if (output_var->location != -1) {
1989 /* Location already assigned. */
1990 return;
1991 }
1992
1993 if (input_var) {
1994 assert(input_var->location == -1);
1995 input_var->location = *input_index;
1996 }
1997
1998 output_var->location = *output_index;
1999
2000 /* FINISHME: Support for "varying" records in GLSL 1.50. */
2001 assert(!output_var->type->is_record());
2002
2003 if (output_var->type->is_array()) {
2004 const unsigned slots = output_var->type->length
2005 * output_var->type->fields.array->matrix_columns;
2006
2007 *output_index += slots;
2008 *input_index += slots;
2009 } else {
2010 const unsigned slots = output_var->type->matrix_columns;
2011
2012 *output_index += slots;
2013 *input_index += slots;
2014 }
2015}
2016
2017
2018/**
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002019 * Is the given variable a varying variable to be counted against the
2020 * limit in ctx->Const.MaxVarying?
2021 * This includes variables such as texcoords, colors and generic
2022 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2023 */
2024static bool
2025is_varying_var(GLenum shaderType, const ir_variable *var)
2026{
2027 /* Only fragment shaders will take a varying variable as an input */
2028 if (shaderType == GL_FRAGMENT_SHADER &&
Eric Anholt94e82b22012-11-13 14:40:22 -08002029 var->mode == ir_var_in) {
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002030 switch (var->location) {
2031 case FRAG_ATTRIB_WPOS:
2032 case FRAG_ATTRIB_FACE:
2033 case FRAG_ATTRIB_PNTC:
2034 return false;
2035 default:
2036 return true;
2037 }
2038 }
2039 return false;
2040}
2041
2042
2043/**
Paul Berry871ddb92011-11-05 11:17:32 -07002044 * Assign locations for all variables that are produced in one pipeline stage
2045 * (the "producer") and consumed in the next stage (the "consumer").
2046 *
2047 * Variables produced by the producer may also be consumed by transform
2048 * feedback.
2049 *
2050 * \param num_tfeedback_decls is the number of declarations indicating
2051 * variables that may be consumed by transform feedback.
2052 *
2053 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2054 * representing the result of parsing the strings passed to
2055 * glTransformFeedbackVaryings(). assign_location() will be called for
2056 * each of these objects that matches one of the outputs of the
2057 * producer.
2058 *
2059 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2060 * be NULL. In this case, varying locations are assigned solely based on the
2061 * requirements of transform feedback.
2062 */
Ian Romanickde773242011-06-09 13:31:32 -07002063bool
2064assign_varying_locations(struct gl_context *ctx,
2065 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07002066 gl_shader *producer, gl_shader *consumer,
2067 unsigned num_tfeedback_decls,
2068 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07002069{
2070 /* FINISHME: Set dynamically when geometry shader support is added. */
2071 unsigned output_index = VERT_RESULT_VAR0;
2072 unsigned input_index = FRAG_ATTRIB_VAR0;
2073
2074 /* Operate in a total of three passes.
2075 *
2076 * 1. Assign locations for any matching inputs and outputs.
2077 *
2078 * 2. Mark output variables in the producer that do not have locations as
2079 * not being outputs. This lets the optimizer eliminate them.
2080 *
2081 * 3. Mark input variables in the consumer that do not have locations as
2082 * not being inputs. This lets the optimizer eliminate them.
2083 */
2084
Eric Anholt16b68b12010-06-30 11:05:43 -07002085 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07002086 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2087
Paul Berry871ddb92011-11-05 11:17:32 -07002088 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07002089 continue;
2090
Paul Berry871ddb92011-11-05 11:17:32 -07002091 ir_variable *input_var =
2092 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002093
Paul Berry871ddb92011-11-05 11:17:32 -07002094 if (input_var && input_var->mode != ir_var_in)
2095 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002096
Paul Berry871ddb92011-11-05 11:17:32 -07002097 if (input_var) {
2098 assign_varying_location(input_var, output_var, &input_index,
2099 &output_index);
2100 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002101
Paul Berry871ddb92011-11-05 11:17:32 -07002102 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002103 if (!tfeedback_decls[i].is_varying())
2104 continue;
2105
Paul Berry871ddb92011-11-05 11:17:32 -07002106 if (!tfeedback_decls[i].is_assigned() &&
2107 tfeedback_decls[i].matches_var(output_var)) {
2108 if (output_var->location == -1) {
2109 assign_varying_location(input_var, output_var, &input_index,
2110 &output_index);
2111 }
2112 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2113 return false;
2114 }
Ian Romanickdf869d92010-08-30 15:37:44 -07002115 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002116 }
2117
Ian Romanickde773242011-06-09 13:31:32 -07002118 unsigned varying_vectors = 0;
2119
Paul Berry871ddb92011-11-05 11:17:32 -07002120 if (consumer) {
2121 foreach_list(node, consumer->ir) {
2122 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07002123
Paul Berry871ddb92011-11-05 11:17:32 -07002124 if ((var == NULL) || (var->mode != ir_var_in))
2125 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07002126
Paul Berry871ddb92011-11-05 11:17:32 -07002127 if (var->location == -1) {
2128 if (prog->Version <= 120) {
2129 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2130 *
2131 * Only those varying variables used (i.e. read) in
2132 * the fragment shader executable must be written to
2133 * by the vertex shader executable; declaring
2134 * superfluous varying variables in a vertex shader is
2135 * permissible.
2136 *
2137 * We interpret this text as meaning that the VS must
2138 * write the variable for the FS to read it. See
2139 * "glsl1-varying read but not written" in piglit.
2140 */
Eric Anholtb7062832010-07-28 13:52:23 -07002141
Paul Berry871ddb92011-11-05 11:17:32 -07002142 linker_error(prog, "fragment shader varying %s not written "
2143 "by vertex shader\n.", var->name);
2144 }
Eric Anholtb7062832010-07-28 13:52:23 -07002145
Paul Berry871ddb92011-11-05 11:17:32 -07002146 /* An 'in' variable is only really a shader input if its
2147 * value is written by the previous stage.
2148 */
2149 var->mode = ir_var_auto;
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002150 } else if (is_varying_var(consumer->Type, var)) {
Paul Berry871ddb92011-11-05 11:17:32 -07002151 /* The packing rules are used for vertex shader inputs are also
2152 * used for fragment shader inputs.
2153 */
2154 varying_vectors += count_attribute_slots(var->type);
2155 }
Eric Anholtb7062832010-07-28 13:52:23 -07002156 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002157 }
Ian Romanickde773242011-06-09 13:31:32 -07002158
Paul Berry15ba2a52012-08-02 17:51:02 -07002159 if (ctx->API == API_OPENGLES2 || prog->IsES) {
Ian Romanickde773242011-06-09 13:31:32 -07002160 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002161 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2162 linker_warning(prog, "shader uses too many varying vectors "
2163 "(%u > %u), but the driver will try to optimize "
2164 "them out; this is non-portable out-of-spec "
2165 "behavior\n",
2166 varying_vectors, ctx->Const.MaxVarying);
2167 } else {
2168 linker_error(prog, "shader uses too many varying vectors "
2169 "(%u > %u)\n",
2170 varying_vectors, ctx->Const.MaxVarying);
2171 return false;
2172 }
Ian Romanickde773242011-06-09 13:31:32 -07002173 }
2174 } else {
2175 const unsigned float_components = varying_vectors * 4;
2176 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002177 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2178 linker_warning(prog, "shader uses too many varying components "
2179 "(%u > %u), but the driver will try to optimize "
2180 "them out; this is non-portable out-of-spec "
2181 "behavior\n",
2182 float_components, ctx->Const.MaxVarying * 4);
2183 } else {
2184 linker_error(prog, "shader uses too many varying components "
2185 "(%u > %u)\n",
2186 float_components, ctx->Const.MaxVarying * 4);
2187 return false;
2188 }
Ian Romanickde773242011-06-09 13:31:32 -07002189 }
2190 }
2191
2192 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07002193}
2194
2195
Paul Berry871ddb92011-11-05 11:17:32 -07002196/**
2197 * Store transform feedback location assignments into
2198 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2199 *
2200 * If an error occurs, the error is reported through linker_error() and false
2201 * is returned.
2202 */
2203static bool
2204store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2205 unsigned num_tfeedback_decls,
2206 tfeedback_decl *tfeedback_decls)
2207{
Paul Berryebfad9f2011-12-29 15:55:01 -08002208 bool separate_attribs_mode =
2209 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08002210
2211 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01002212 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08002213
2214 memset(&prog->LinkedTransformFeedback, 0,
2215 sizeof(prog->LinkedTransformFeedback));
2216
2217 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08002218 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08002219 struct gl_transform_feedback_varying_info,
2220 num_tfeedback_decls);
2221
Christoph Bumillerd540af52012-01-20 13:24:46 +01002222 unsigned num_outputs = 0;
2223 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
2224 if (!tfeedback_decls[i].accumulate_num_outputs(prog, &num_outputs))
2225 return false;
2226
2227 prog->LinkedTransformFeedback.Outputs =
2228 rzalloc_array(prog,
2229 struct gl_transform_feedback_output,
2230 num_outputs);
2231
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002232 unsigned num_buffers = 0;
2233
2234 if (separate_attribs_mode) {
2235 /* GL_SEPARATE_ATTRIBS */
2236 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2237 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2238 num_buffers, num_outputs))
2239 return false;
2240
2241 num_buffers++;
2242 }
Paul Berry871ddb92011-11-05 11:17:32 -07002243 }
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002244 else {
2245 /* GL_INVERLEAVED_ATTRIBS */
2246 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2247 if (tfeedback_decls[i].is_next_buffer_separator()) {
2248 num_buffers++;
2249 continue;
2250 }
2251
2252 if (!tfeedback_decls[i].store(ctx, prog,
2253 &prog->LinkedTransformFeedback,
2254 num_buffers, num_outputs))
2255 return false;
2256 }
2257 num_buffers++;
2258 }
2259
Christoph Bumillerd540af52012-01-20 13:24:46 +01002260 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002261
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002262 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
Paul Berry871ddb92011-11-05 11:17:32 -07002263 return true;
2264}
2265
Ian Romanick92f81592011-11-08 12:37:19 -08002266/**
Marek Olšákec174a42011-11-18 15:00:10 +01002267 * Store the gl_FragDepth layout in the gl_shader_program struct.
2268 */
2269static void
2270store_fragdepth_layout(struct gl_shader_program *prog)
2271{
2272 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2273 return;
2274 }
2275
2276 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2277
2278 /* We don't look up the gl_FragDepth symbol directly because if
2279 * gl_FragDepth is not used in the shader, it's removed from the IR.
2280 * However, the symbol won't be removed from the symbol table.
2281 *
2282 * We're only interested in the cases where the variable is NOT removed
2283 * from the IR.
2284 */
2285 foreach_list(node, ir) {
2286 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2287
2288 if (var == NULL || var->mode != ir_var_out) {
2289 continue;
2290 }
2291
2292 if (strcmp(var->name, "gl_FragDepth") == 0) {
2293 switch (var->depth_layout) {
2294 case ir_depth_layout_none:
2295 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2296 return;
2297 case ir_depth_layout_any:
2298 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2299 return;
2300 case ir_depth_layout_greater:
2301 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2302 return;
2303 case ir_depth_layout_less:
2304 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2305 return;
2306 case ir_depth_layout_unchanged:
2307 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2308 return;
2309 default:
2310 assert(0);
2311 return;
2312 }
2313 }
2314 }
2315}
2316
2317/**
Ian Romanick92f81592011-11-08 12:37:19 -08002318 * Validate the resources used by a program versus the implementation limits
2319 */
2320static bool
2321check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2322{
2323 static const char *const shader_names[MESA_SHADER_TYPES] = {
2324 "vertex", "fragment", "geometry"
2325 };
2326
2327 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2328 ctx->Const.MaxVertexTextureImageUnits,
2329 ctx->Const.MaxTextureImageUnits,
2330 ctx->Const.MaxGeometryTextureImageUnits
2331 };
2332
2333 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2334 ctx->Const.VertexProgram.MaxUniformComponents,
2335 ctx->Const.FragmentProgram.MaxUniformComponents,
2336 0 /* FINISHME: Geometry shaders. */
2337 };
2338
Eric Anholt877a8972012-06-25 12:47:01 -07002339 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
2340 ctx->Const.VertexProgram.MaxUniformBlocks,
2341 ctx->Const.FragmentProgram.MaxUniformBlocks,
2342 ctx->Const.GeometryProgram.MaxUniformBlocks,
2343 };
2344
Ian Romanick92f81592011-11-08 12:37:19 -08002345 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2346 struct gl_shader *sh = prog->_LinkedShaders[i];
2347
2348 if (sh == NULL)
2349 continue;
2350
2351 if (sh->num_samplers > max_samplers[i]) {
2352 linker_error(prog, "Too many %s shader texture samplers",
2353 shader_names[i]);
2354 }
2355
2356 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002357 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2358 linker_warning(prog, "Too many %s shader uniform components, "
2359 "but the driver will try to optimize them out; "
2360 "this is non-portable out-of-spec behavior\n",
2361 shader_names[i]);
2362 } else {
2363 linker_error(prog, "Too many %s shader uniform components",
2364 shader_names[i]);
2365 }
Ian Romanick92f81592011-11-08 12:37:19 -08002366 }
2367 }
2368
Eric Anholt877a8972012-06-25 12:47:01 -07002369 unsigned blocks[MESA_SHADER_TYPES] = {0};
2370 unsigned total_uniform_blocks = 0;
2371
2372 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
2373 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
2374 if (prog->UniformBlockStageIndex[j][i] != -1) {
2375 blocks[j]++;
2376 total_uniform_blocks++;
2377 }
2378 }
2379
2380 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
2381 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
2382 prog->NumUniformBlocks,
2383 ctx->Const.MaxCombinedUniformBlocks);
2384 } else {
2385 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2386 if (blocks[i] > max_uniform_blocks[i]) {
2387 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
2388 shader_names[i],
2389 blocks[i],
2390 max_uniform_blocks[i]);
2391 break;
2392 }
2393 }
2394 }
2395 }
2396
Ian Romanick92f81592011-11-08 12:37:19 -08002397 return prog->LinkStatus;
2398}
Paul Berry871ddb92011-11-05 11:17:32 -07002399
Ian Romanick0e59b262010-06-23 11:23:01 -07002400void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002401link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002402{
Paul Berry871ddb92011-11-05 11:17:32 -07002403 tfeedback_decl *tfeedback_decls = NULL;
2404 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2405
Kenneth Graunked3073f52011-01-21 14:32:31 -08002406 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002407
Ian Romanick832dfa52010-06-17 15:04:20 -07002408 prog->LinkStatus = false;
2409 prog->Validated = false;
2410 prog->_Used = false;
2411
Eric Anholtf609cf72012-04-27 13:52:56 -07002412 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08002413 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002414
Eric Anholtf609cf72012-04-27 13:52:56 -07002415 ralloc_free(prog->UniformBlocks);
2416 prog->UniformBlocks = NULL;
2417 prog->NumUniformBlocks = 0;
2418 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2419 ralloc_free(prog->UniformBlockStageIndex[i]);
2420 prog->UniformBlockStageIndex[i] = NULL;
2421 }
2422
Ian Romanick832dfa52010-06-17 15:04:20 -07002423 /* Separate the shaders into groups based on their type.
2424 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002425 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002426 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002427 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002428 unsigned num_frag_shaders = 0;
2429
Eric Anholt16b68b12010-06-30 11:05:43 -07002430 vert_shader_list = (struct gl_shader **)
2431 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002432 frag_shader_list = &vert_shader_list[prog->NumShaders];
2433
Ian Romanick25f51d32010-07-16 15:51:50 -07002434 unsigned min_version = UINT_MAX;
2435 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002436 const bool is_es_prog =
2437 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002438 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002439 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2440 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2441
Paul Berrya9f34dc2012-08-02 17:49:44 -07002442 if (prog->Shaders[i]->IsES != is_es_prog) {
2443 linker_error(prog, "all shaders must use same shading "
2444 "language version\n");
2445 goto done;
2446 }
2447
Ian Romanick832dfa52010-06-17 15:04:20 -07002448 switch (prog->Shaders[i]->Type) {
2449 case GL_VERTEX_SHADER:
2450 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2451 num_vert_shaders++;
2452 break;
2453 case GL_FRAGMENT_SHADER:
2454 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2455 num_frag_shaders++;
2456 break;
2457 case GL_GEOMETRY_SHADER:
2458 /* FINISHME: Support geometry shaders. */
2459 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2460 break;
2461 }
2462 }
2463
Ian Romanick25f51d32010-07-16 15:51:50 -07002464 /* Previous to GLSL version 1.30, different compilation units could mix and
2465 * match shading language versions. With GLSL 1.30 and later, the versions
2466 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07002467 *
2468 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07002469 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07002470 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002471 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002472 linker_error(prog, "all shaders must use same shading "
2473 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002474 goto done;
2475 }
2476
2477 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002478 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002479
Ian Romanick3322fba2010-10-14 13:28:42 -07002480 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2481 if (prog->_LinkedShaders[i] != NULL)
2482 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2483
2484 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002485 }
2486
Ian Romanickcd6764e2010-07-16 16:00:07 -07002487 /* Link all shaders for a particular stage and validate the result.
2488 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002489 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002490 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002491 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2492 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002493
2494 if (sh == NULL)
2495 goto done;
2496
2497 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002498 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002499
Ian Romanick3322fba2010-10-14 13:28:42 -07002500 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2501 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002502 }
2503
2504 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002505 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002506 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2507 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002508
2509 if (sh == NULL)
2510 goto done;
2511
2512 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002513 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002514
Ian Romanick3322fba2010-10-14 13:28:42 -07002515 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2516 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002517 }
2518
Ian Romanick3ed850e2010-06-23 12:18:21 -07002519 /* Here begins the inter-stage linking phase. Some initial validation is
2520 * performed, then locations are assigned for uniforms, attributes, and
2521 * varyings.
2522 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002523 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002524 unsigned prev;
2525
2526 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2527 if (prog->_LinkedShaders[prev] != NULL)
2528 break;
2529 }
2530
Bryan Cainf18a0862011-04-23 19:29:15 -05002531 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002532 * stage.
2533 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002534 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2535 if (prog->_LinkedShaders[i] == NULL)
2536 continue;
2537
Ian Romanickf36460e2010-06-23 12:07:22 -07002538 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002539 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002540 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002541 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002542
2543 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002544 }
2545
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002546 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002547 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002548
Eric Anholt3de13952012-05-04 13:08:46 -07002549 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2550 * it before optimization because we want most of the checks to get
2551 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002552 *
2553 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002554 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002555 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002556 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2557 if (sh) {
2558 lower_discard_flow(sh->ir);
2559 }
2560 }
2561
Eric Anholtf609cf72012-04-27 13:52:56 -07002562 if (!interstage_cross_validate_uniform_blocks(prog))
2563 goto done;
2564
Eric Anholt2f4fe152010-08-10 13:06:49 -07002565 /* Do common optimization before assigning storage for attributes,
2566 * uniforms, and varyings. Later optimization could possibly make
2567 * some of that unused.
2568 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002569 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2570 if (prog->_LinkedShaders[i] == NULL)
2571 continue;
2572
Ian Romanick02c5ae12011-07-11 10:46:01 -07002573 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2574 if (!prog->LinkStatus)
2575 goto done;
2576
Paul Berry18392442012-12-04 11:11:02 -08002577 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2578 lower_clip_distance(prog->_LinkedShaders[i]);
2579 }
Paul Berryc06e3252011-08-11 20:58:21 -07002580
Brian Paul7feabfe2012-03-20 17:43:12 -06002581 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2582
2583 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002584 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002585 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002586
Paul Berry50895d42012-12-05 07:17:07 -08002587 /* Mark all generic shader inputs and outputs as unpaired. */
2588 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2589 link_invalidate_variable_locations(
2590 prog->_LinkedShaders[MESA_SHADER_VERTEX],
2591 VERT_ATTRIB_GENERIC0, VERT_RESULT_VAR0);
2592 }
2593 /* FINISHME: Geometry shaders not implemented yet */
2594 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2595 link_invalidate_variable_locations(
2596 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2597 FRAG_ATTRIB_VAR0, FRAG_RESULT_DATA0);
2598 }
2599
Ian Romanickd32d4f72011-06-27 17:59:58 -07002600 /* FINISHME: The value of the max_attribute_index parameter is
2601 * FINISHME: implementation dependent based on the value of
2602 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2603 * FINISHME: at least 16, so hardcode 16 for now.
2604 */
2605 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002606 goto done;
2607 }
2608
Dave Airlie1256a5d2012-03-24 13:33:41 +00002609 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002610 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002611 }
2612
Ian Romanick3322fba2010-10-14 13:28:42 -07002613 unsigned prev;
2614 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2615 if (prog->_LinkedShaders[prev] != NULL)
2616 break;
2617 }
2618
Paul Berry871ddb92011-11-05 11:17:32 -07002619 if (num_tfeedback_decls != 0) {
2620 /* From GL_EXT_transform_feedback:
2621 * A program will fail to link if:
2622 *
2623 * * the <count> specified by TransformFeedbackVaryingsEXT is
2624 * non-zero, but the program object has no vertex or geometry
2625 * shader;
2626 */
2627 if (prev >= MESA_SHADER_FRAGMENT) {
2628 linker_error(prog, "Transform feedback varyings specified, but "
2629 "no vertex or geometry shader is present.");
2630 goto done;
2631 }
2632
2633 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2634 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002635 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002636 prog->TransformFeedback.VaryingNames,
2637 tfeedback_decls))
2638 goto done;
2639 }
2640
Ian Romanick3322fba2010-10-14 13:28:42 -07002641 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2642 if (prog->_LinkedShaders[i] == NULL)
2643 continue;
2644
Paul Berry871ddb92011-11-05 11:17:32 -07002645 if (!assign_varying_locations(
2646 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2647 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2648 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002649 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002650
Ian Romanick3322fba2010-10-14 13:28:42 -07002651 prev = i;
2652 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002653
Paul Berry871ddb92011-11-05 11:17:32 -07002654 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2655 /* There was no fragment shader, but we still have to assign varying
2656 * locations for use by transform feedback.
2657 */
2658 if (!assign_varying_locations(
2659 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2660 tfeedback_decls))
2661 goto done;
2662 }
2663
2664 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2665 goto done;
2666
Ian Romanickcc90e622010-10-19 17:59:10 -07002667 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2668 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2669 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002670
2671 /* Eliminate code that is now dead due to unused vertex outputs being
2672 * demoted.
2673 */
2674 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2675 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002676 }
2677
2678 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2679 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2680
2681 demote_shader_inputs_and_outputs(sh, ir_var_in);
2682 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2683 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002684
2685 /* Eliminate code that is now dead due to unused geometry outputs being
2686 * demoted.
2687 */
2688 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2689 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002690 }
2691
2692 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2693 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2694
2695 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002696
2697 /* Eliminate code that is now dead due to unused fragment inputs being
2698 * demoted. This shouldn't actually do anything other than remove
2699 * declarations of the (now unused) global variables.
2700 */
2701 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2702 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002703 }
2704
Ian Romanick960d7222011-10-21 11:21:02 -07002705 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002706 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002707 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002708
Ian Romanick92f81592011-11-08 12:37:19 -08002709 if (!check_resources(ctx, prog))
2710 goto done;
2711
Ian Romanickce9171f2011-02-03 17:10:14 -08002712 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002713 * present in a linked program. By checking prog->IsES, we also
2714 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002715 */
Eric Anholt57f79782011-07-22 12:57:47 -07002716 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002717 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002718 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002719 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002720 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002721 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002722 }
2723 }
2724
Ian Romanick13e10e42010-06-21 12:03:24 -07002725 /* FINISHME: Assign fragment shader output locations. */
2726
Ian Romanick832dfa52010-06-17 15:04:20 -07002727done:
2728 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002729
2730 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2731 if (prog->_LinkedShaders[i] == NULL)
2732 continue;
2733
2734 /* Retain any live IR, but trash the rest. */
2735 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002736
2737 /* The symbol table in the linked shaders may contain references to
2738 * variables that were removed (e.g., unused uniforms). Since it may
2739 * contain junk, there is no possible valid use. Delete it and set the
2740 * pointer to NULL.
2741 */
2742 delete prog->_LinkedShaders[i]->symbols;
2743 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002744 }
2745
Kenneth Graunked3073f52011-01-21 14:32:31 -08002746 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002747}