blob: 83d0e8710054167596956408b80fdb9e35a2b18a [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
Paul Berryca7e8912012-12-05 14:37:19 -080079#define ALIGN(value, alignment) (((value) + alignment - 1) & ~(alignment - 1))
80
Ian Romanick832dfa52010-06-17 15:04:20 -070081/**
82 * Visitor that determines whether or not a variable is ever written.
83 */
84class find_assignment_visitor : public ir_hierarchical_visitor {
85public:
86 find_assignment_visitor(const char *name)
87 : name(name), found(false)
88 {
89 /* empty */
90 }
91
92 virtual ir_visitor_status visit_enter(ir_assignment *ir)
93 {
94 ir_variable *const var = ir->lhs->variable_referenced();
95
96 if (strcmp(name, var->name) == 0) {
97 found = true;
98 return visit_stop;
99 }
100
101 return visit_continue_with_parent;
102 }
103
Eric Anholt18a60232010-08-23 11:29:25 -0700104 virtual ir_visitor_status visit_enter(ir_call *ir)
105 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700106 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700107 foreach_iter(exec_list_iterator, iter, *ir) {
108 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
109 ir_variable *sig_param = (ir_variable *)sig_iter.get();
110
111 if (sig_param->mode == ir_var_out ||
112 sig_param->mode == ir_var_inout) {
113 ir_variable *var = param_rval->variable_referenced();
114 if (var && strcmp(name, var->name) == 0) {
115 found = true;
116 return visit_stop;
117 }
118 }
119 sig_iter.next();
120 }
121
Kenneth Graunked884f602012-03-20 15:56:37 -0700122 if (ir->return_deref != NULL) {
123 ir_variable *const var = ir->return_deref->variable_referenced();
124
125 if (strcmp(name, var->name) == 0) {
126 found = true;
127 return visit_stop;
128 }
129 }
130
Eric Anholt18a60232010-08-23 11:29:25 -0700131 return visit_continue_with_parent;
132 }
133
Ian Romanick832dfa52010-06-17 15:04:20 -0700134 bool variable_found()
135 {
136 return found;
137 }
138
139private:
140 const char *name; /**< Find writes to a variable with this name. */
141 bool found; /**< Was a write to the variable found? */
142};
143
Ian Romanickc93b8f12010-06-17 15:20:22 -0700144
Ian Romanickc33e78f2010-08-13 12:30:41 -0700145/**
146 * Visitor that determines whether or not a variable is ever read.
147 */
148class find_deref_visitor : public ir_hierarchical_visitor {
149public:
150 find_deref_visitor(const char *name)
151 : name(name), found(false)
152 {
153 /* empty */
154 }
155
156 virtual ir_visitor_status visit(ir_dereference_variable *ir)
157 {
158 if (strcmp(this->name, ir->var->name) == 0) {
159 this->found = true;
160 return visit_stop;
161 }
162
163 return visit_continue;
164 }
165
166 bool variable_found() const
167 {
168 return this->found;
169 }
170
171private:
172 const char *name; /**< Find writes to a variable with this name. */
173 bool found; /**< Was a write to the variable found? */
174};
175
176
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700177void
Ian Romanick586e7412011-07-28 14:04:09 -0700178linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700179{
180 va_list ap;
181
Kenneth Graunked3073f52011-01-21 14:32:31 -0800182 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700183 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800184 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700185 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700186
187 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700188}
189
190
191void
Ian Romanick379a32f2011-07-28 14:09:06 -0700192linker_warning(gl_shader_program *prog, const char *fmt, ...)
193{
194 va_list ap;
195
196 ralloc_strcat(&prog->InfoLog, "error: ");
197 va_start(ap, fmt);
198 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
199 va_end(ap);
200
201}
202
203
204void
Paul Berry50895d42012-12-05 07:17:07 -0800205link_invalidate_variable_locations(gl_shader *sh, int input_base,
206 int output_base)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700207{
Eric Anholt16b68b12010-06-30 11:05:43 -0700208 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700209 ir_variable *const var = ((ir_instruction *) node)->as_variable();
210
Paul Berry50895d42012-12-05 07:17:07 -0800211 if (var == NULL)
212 continue;
213
214 int base;
215 switch (var->mode) {
216 case ir_var_in:
217 base = input_base;
218 break;
219 case ir_var_out:
220 base = output_base;
221 break;
222 default:
223 continue;
224 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700225
226 /* Only assign locations for generic attributes / varyings / etc.
227 */
Paul Berry50895d42012-12-05 07:17:07 -0800228 if ((var->location >= base) && !var->explicit_location)
229 var->location = -1;
Paul Berry3c9c17d2012-12-04 15:17:01 -0800230
Paul Berry3e81c662012-12-05 10:47:55 -0800231 if ((var->location == -1) && !var->explicit_location) {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800232 var->is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800233 var->location_frac = 0;
234 } else {
Paul Berry3c9c17d2012-12-04 15:17:01 -0800235 var->is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800236 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700237 }
238}
239
240
Ian Romanickc93b8f12010-06-17 15:20:22 -0700241/**
Ian Romanick69846702010-06-22 17:29:19 -0700242 * Determine the number of attribute slots required for a particular type
243 *
244 * This code is here because it implements the language rules of a specific
245 * GLSL version. Since it's a property of the language and not a property of
246 * types in general, it doesn't really belong in glsl_type.
247 */
248unsigned
249count_attribute_slots(const glsl_type *t)
250{
251 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
252 *
253 * "A scalar input counts the same amount against this limit as a vec4,
254 * so applications may want to consider packing groups of four
255 * unrelated float inputs together into a vector to better utilize the
256 * capabilities of the underlying hardware. A matrix input will use up
257 * multiple locations. The number of locations used will equal the
258 * number of columns in the matrix."
259 *
260 * The spec does not explicitly say how arrays are counted. However, it
261 * should be safe to assume the total number of slots consumed by an array
262 * is the number of entries in the array multiplied by the number of slots
263 * consumed by a single element of the array.
264 */
265
266 if (t->is_array())
267 return t->array_size() * count_attribute_slots(t->element_type());
268
269 if (t->is_matrix())
270 return t->matrix_columns;
271
272 return 1;
273}
274
275
276/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700277 * Verify that a vertex shader executable meets all semantic requirements.
278 *
Paul Berry642e5b412012-01-04 13:57:52 -0800279 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
280 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700281 *
282 * \param shader Vertex shader executable to be verified
283 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700284bool
Eric Anholt849e1812010-06-30 11:49:17 -0700285validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700286 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700287{
288 if (shader == NULL)
289 return true;
290
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700291 /* From the GLSL 1.10 spec, page 48:
292 *
293 * "The variable gl_Position is available only in the vertex
294 * language and is intended for writing the homogeneous vertex
295 * position. All executions of a well-formed vertex shader
296 * executable must write a value into this variable. [...] The
297 * variable gl_Position is available only in the vertex
298 * language and is intended for writing the homogeneous vertex
299 * position. All executions of a well-formed vertex shader
300 * executable must write a value into this variable."
301 *
302 * while in GLSL 1.40 this text is changed to:
303 *
304 * "The variable gl_Position is available only in the vertex
305 * language and is intended for writing the homogeneous vertex
306 * position. It can be written at any time during shader
307 * execution. It may also be read back by a vertex shader
308 * after being written. This value will be used by primitive
309 * assembly, clipping, culling, and other fixed functionality
310 * operations, if present, that operate on primitives after
311 * vertex processing has occurred. Its value is undefined if
312 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700313 *
314 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
315 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700316 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700317 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700318 find_assignment_visitor find("gl_Position");
319 find.run(shader->ir);
320 if (!find.variable_found()) {
321 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
322 return false;
323 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700324 }
325
Paul Berry642e5b412012-01-04 13:57:52 -0800326 prog->Vert.ClipDistanceArraySize = 0;
327
Paul Berry15ba2a52012-08-02 17:51:02 -0700328 if (!prog->IsES && prog->Version >= 130) {
Paul Berryb453ba22011-08-11 18:10:22 -0700329 /* From section 7.1 (Vertex Shader Special Variables) of the
330 * GLSL 1.30 spec:
331 *
332 * "It is an error for a shader to statically write both
333 * gl_ClipVertex and gl_ClipDistance."
Paul Berry15ba2a52012-08-02 17:51:02 -0700334 *
335 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
336 * gl_ClipVertex nor gl_ClipDistance.
Paul Berryb453ba22011-08-11 18:10:22 -0700337 */
338 find_assignment_visitor clip_vertex("gl_ClipVertex");
339 find_assignment_visitor clip_distance("gl_ClipDistance");
340
341 clip_vertex.run(shader->ir);
342 clip_distance.run(shader->ir);
343 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
344 linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
345 "and `gl_ClipDistance'\n");
346 return false;
347 }
Paul Berry1ad54ae2011-09-17 09:42:02 -0700348 prog->Vert.UsesClipDistance = clip_distance.variable_found();
Paul Berry642e5b412012-01-04 13:57:52 -0800349 ir_variable *clip_distance_var =
350 shader->symbols->get_variable("gl_ClipDistance");
351 if (clip_distance_var)
352 prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
Paul Berryb453ba22011-08-11 18:10:22 -0700353 }
354
Ian Romanick832dfa52010-06-17 15:04:20 -0700355 return true;
356}
357
358
Ian Romanickc93b8f12010-06-17 15:20:22 -0700359/**
360 * Verify that a fragment shader executable meets all semantic requirements
361 *
362 * \param shader Fragment shader executable to be verified
363 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700364bool
Eric Anholt849e1812010-06-30 11:49:17 -0700365validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700366 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700367{
368 if (shader == NULL)
369 return true;
370
Ian Romanick832dfa52010-06-17 15:04:20 -0700371 find_assignment_visitor frag_color("gl_FragColor");
372 find_assignment_visitor frag_data("gl_FragData");
373
Eric Anholt16b68b12010-06-30 11:05:43 -0700374 frag_color.run(shader->ir);
375 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700376
Ian Romanick832dfa52010-06-17 15:04:20 -0700377 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700378 linker_error(prog, "fragment shader writes to both "
379 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700380 return false;
381 }
382
383 return true;
384}
385
386
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700387/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700388 * Generate a string describing the mode of a variable
389 */
390static const char *
391mode_string(const ir_variable *var)
392{
393 switch (var->mode) {
394 case ir_var_auto:
395 return (var->read_only) ? "global constant" : "global variable";
396
397 case ir_var_uniform: return "uniform";
398 case ir_var_in: return "shader input";
399 case ir_var_out: return "shader output";
400 case ir_var_inout: return "shader inout";
Ian Romanick7e2aa912010-07-19 17:12:42 -0700401
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800402 case ir_var_const_in:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700403 case ir_var_temporary:
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700404 default:
405 assert(!"Should not get here.");
406 return "invalid variable";
407 }
408}
409
410
411/**
412 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700413 */
414bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700415cross_validate_globals(struct gl_shader_program *prog,
416 struct gl_shader **shader_list,
417 unsigned num_shaders,
418 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700419{
420 /* Examine all of the uniforms in all of the shaders and cross validate
421 * them.
422 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700423 glsl_symbol_table variables;
424 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700425 if (shader_list[i] == NULL)
426 continue;
427
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700428 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700429 ir_variable *const var = ((ir_instruction *) node)->as_variable();
430
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700431 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700432 continue;
433
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700434 if (uniforms_only && (var->mode != ir_var_uniform))
435 continue;
436
Ian Romanick7e2aa912010-07-19 17:12:42 -0700437 /* Don't cross validate temporaries that are at global scope. These
438 * will eventually get pulled into the shaders 'main'.
439 */
440 if (var->mode == ir_var_temporary)
441 continue;
442
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700443 /* If a global with this name has already been seen, verify that the
444 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700445 * initializers, the values of the initializers must be the same.
446 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700447 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700448 if (existing != NULL) {
449 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700450 /* Consider the types to be "the same" if both types are arrays
451 * of the same type and one of the arrays is implicitly sized.
452 * In addition, set the type of the linked variable to the
453 * explicitly sized array.
454 */
455 if (var->type->is_array()
456 && existing->type->is_array()
457 && (var->type->fields.array == existing->type->fields.array)
458 && ((var->type->length == 0)
459 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800460 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700461 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800462 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700463 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700464 linker_error(prog, "%s `%s' declared as type "
465 "`%s' and type `%s'\n",
466 mode_string(var),
467 var->name, var->type->name,
468 existing->type->name);
Ian Romanicka2711d62010-08-29 22:07:49 -0700469 return false;
470 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700471 }
472
Ian Romanick68a4fc92010-10-07 17:21:22 -0700473 if (var->explicit_location) {
474 if (existing->explicit_location
475 && (var->location != existing->location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700476 linker_error(prog, "explicit locations for %s "
477 "`%s' have differing values\n",
478 mode_string(var), var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -0700479 return false;
480 }
481
482 existing->location = var->location;
483 existing->explicit_location = true;
484 }
485
Ian Romanick46173f92011-10-31 13:07:06 -0700486 /* Validate layout qualifiers for gl_FragDepth.
487 *
488 * From the AMD/ARB_conservative_depth specs:
489 *
490 * "If gl_FragDepth is redeclared in any fragment shader in a
491 * program, it must be redeclared in all fragment shaders in
492 * that program that have static assignments to
493 * gl_FragDepth. All redeclarations of gl_FragDepth in all
494 * fragment shaders in a single program must have the same set
495 * of qualifiers."
496 */
497 if (strcmp(var->name, "gl_FragDepth") == 0) {
498 bool layout_declared = var->depth_layout != ir_depth_layout_none;
499 bool layout_differs =
500 var->depth_layout != existing->depth_layout;
501
502 if (layout_declared && layout_differs) {
503 linker_error(prog,
504 "All redeclarations of gl_FragDepth in all "
505 "fragment shaders in a single program must have "
506 "the same set of qualifiers.");
507 }
508
509 if (var->used && layout_differs) {
510 linker_error(prog,
511 "If gl_FragDepth is redeclared with a layout "
512 "qualifier in any fragment shader, it must be "
513 "redeclared with the same layout qualifier in "
514 "all fragment shaders that have assignments to "
515 "gl_FragDepth");
516 }
517 }
Chad Versaceaddae332011-01-27 01:40:31 -0800518
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700519 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
520 *
521 * "If a shared global has multiple initializers, the
522 * initializers must all be constant expressions, and they
523 * must all have the same value. Otherwise, a link error will
524 * result. (A shared global having only one initializer does
525 * not require that initializer to be a constant expression.)"
526 *
527 * Previous to 4.20 the GLSL spec simply said that initializers
528 * must have the same value. In this case of non-constant
529 * initializers, this was impossible to determine. As a result,
530 * no vendor actually implemented that behavior. The 4.20
531 * behavior matches the implemented behavior of at least one other
532 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700533 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700534 if (var->constant_initializer != NULL) {
535 if (existing->constant_initializer != NULL) {
536 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700537 linker_error(prog, "initializers for %s "
538 "`%s' have differing values\n",
539 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700540 return false;
541 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700542 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700543 /* If the first-seen instance of a particular uniform did not
544 * have an initializer but a later instance does, copy the
545 * initializer to the version stored in the symbol table.
546 */
Ian Romanickde415b72010-07-14 13:22:12 -0700547 /* FINISHME: This is wrong. The constant_value field should
548 * FINISHME: not be modified! Imagine a case where a shader
549 * FINISHME: without an initializer is linked in two different
550 * FINISHME: programs with shaders that have differing
551 * FINISHME: initializers. Linking with the first will
552 * FINISHME: modify the shader, and linking with the second
553 * FINISHME: will fail.
554 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700555 existing->constant_initializer =
556 var->constant_initializer->clone(ralloc_parent(existing),
557 NULL);
558 }
559 }
560
561 if (var->has_initializer) {
562 if (existing->has_initializer
563 && (var->constant_initializer == NULL
564 || existing->constant_initializer == NULL)) {
565 linker_error(prog,
566 "shared global variable `%s' has multiple "
567 "non-constant initializers.\n",
568 var->name);
569 return false;
570 }
571
572 /* Some instance had an initializer, so keep track of that. In
573 * this location, all sorts of initializers (constant or
574 * otherwise) will propagate the existence to the variable
575 * stored in the symbol table.
576 */
577 existing->has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700578 }
Chad Versace7528f142010-11-17 14:34:38 -0800579
580 if (existing->invariant != var->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700581 linker_error(prog, "declarations for %s `%s' have "
582 "mismatching invariant qualifiers\n",
583 mode_string(var), var->name);
Chad Versace7528f142010-11-17 14:34:38 -0800584 return false;
585 }
Chad Versace61428dd2011-01-10 15:29:30 -0800586 if (existing->centroid != var->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700587 linker_error(prog, "declarations for %s `%s' have "
588 "mismatching centroid qualifiers\n",
589 mode_string(var), var->name);
Chad Versace61428dd2011-01-10 15:29:30 -0800590 return false;
591 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700592 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700593 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700594 }
595 }
596
597 return true;
598}
599
600
Ian Romanick37101922010-06-18 19:02:10 -0700601/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700602 * Perform validation of uniforms used across multiple shader stages
603 */
604bool
605cross_validate_uniforms(struct gl_shader_program *prog)
606{
607 return cross_validate_globals(prog, prog->_LinkedShaders,
Ian Romanick3322fba2010-10-14 13:28:42 -0700608 MESA_SHADER_TYPES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700609}
610
Eric Anholtf609cf72012-04-27 13:52:56 -0700611/**
612 * Accumulates the array of prog->UniformBlocks and checks that all
613 * definitons of blocks agree on their contents.
614 */
615static bool
616interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
617{
618 unsigned max_num_uniform_blocks = 0;
619 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
620 if (prog->_LinkedShaders[i])
621 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
622 }
623
624 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
625 struct gl_shader *sh = prog->_LinkedShaders[i];
626
627 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
628 max_num_uniform_blocks);
629 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
630 prog->UniformBlockStageIndex[i][j] = -1;
631
632 if (sh == NULL)
633 continue;
634
635 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
636 int index = link_cross_validate_uniform_block(prog,
637 &prog->UniformBlocks,
638 &prog->NumUniformBlocks,
639 &sh->UniformBlocks[j]);
640
641 if (index == -1) {
642 linker_error(prog, "uniform block `%s' has mismatching definitions",
643 sh->UniformBlocks[j].Name);
644 return false;
645 }
646
647 prog->UniformBlockStageIndex[i][index] = j;
648 }
649 }
650
651 return true;
652}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700653
654/**
Ian Romanick37101922010-06-18 19:02:10 -0700655 * Validate that outputs from one stage match inputs of another
656 */
657bool
Eric Anholt849e1812010-06-30 11:49:17 -0700658cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700659 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700660{
661 glsl_symbol_table parameters;
662 /* FINISHME: Figure these out dynamically. */
663 const char *const producer_stage = "vertex";
664 const char *const consumer_stage = "fragment";
665
666 /* Find all shader outputs in the "producer" stage.
667 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700668 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700669 ir_variable *const var = ((ir_instruction *) node)->as_variable();
670
671 /* FINISHME: For geometry shaders, this should also look for inout
672 * FINISHME: variables.
673 */
674 if ((var == NULL) || (var->mode != ir_var_out))
675 continue;
676
Eric Anholt001eee52010-11-05 06:11:24 -0700677 parameters.add_variable(var);
Ian Romanick37101922010-06-18 19:02:10 -0700678 }
679
680
681 /* Find all shader inputs in the "consumer" stage. Any variables that have
682 * matching outputs already in the symbol table must have the same type and
683 * qualifiers.
684 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700685 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700686 ir_variable *const input = ((ir_instruction *) node)->as_variable();
687
688 /* FINISHME: For geometry shaders, this should also look for inout
689 * FINISHME: variables.
690 */
691 if ((input == NULL) || (input->mode != ir_var_in))
692 continue;
693
694 ir_variable *const output = parameters.get_variable(input->name);
695 if (output != NULL) {
696 /* Check that the types match between stages.
697 */
698 if (input->type != output->type) {
Ian Romanickcb2b5472010-12-13 15:16:39 -0800699 /* There is a bit of a special case for gl_TexCoord. This
Bryan Cainf18a0862011-04-23 19:29:15 -0500700 * built-in is unsized by default. Applications that variable
Ian Romanickcb2b5472010-12-13 15:16:39 -0800701 * access it must redeclare it with a size. There is some
702 * language in the GLSL spec that implies the fragment shader
703 * and vertex shader do not have to agree on this size. Other
704 * driver behave this way, and one or two applications seem to
705 * rely on it.
706 *
707 * Neither declaration needs to be modified here because the array
708 * sizes are fixed later when update_array_sizes is called.
709 *
710 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
711 *
712 * "Unlike user-defined varying variables, the built-in
713 * varying variables don't have a strict one-to-one
714 * correspondence between the vertex language and the
715 * fragment language."
716 */
717 if (!output->type->is_array()
718 || (strncmp("gl_", output->name, 3) != 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700719 linker_error(prog,
720 "%s shader output `%s' declared as type `%s', "
721 "but %s shader input declared as type `%s'\n",
722 producer_stage, output->name,
723 output->type->name,
724 consumer_stage, input->type->name);
Ian Romanickcb2b5472010-12-13 15:16:39 -0800725 return false;
726 }
Ian Romanick37101922010-06-18 19:02:10 -0700727 }
728
729 /* Check that all of the qualifiers match between stages.
730 */
731 if (input->centroid != output->centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700732 linker_error(prog,
733 "%s shader output `%s' %s centroid qualifier, "
734 "but %s shader input %s centroid qualifier\n",
735 producer_stage,
736 output->name,
737 (output->centroid) ? "has" : "lacks",
738 consumer_stage,
739 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700740 return false;
741 }
742
743 if (input->invariant != output->invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700744 linker_error(prog,
745 "%s shader output `%s' %s invariant qualifier, "
746 "but %s shader input %s invariant qualifier\n",
747 producer_stage,
748 output->name,
749 (output->invariant) ? "has" : "lacks",
750 consumer_stage,
751 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700752 return false;
753 }
754
755 if (input->interpolation != output->interpolation) {
Ian Romanick586e7412011-07-28 14:04:09 -0700756 linker_error(prog,
757 "%s shader output `%s' specifies %s "
758 "interpolation qualifier, "
759 "but %s shader input specifies %s "
760 "interpolation qualifier\n",
761 producer_stage,
762 output->name,
763 output->interpolation_string(),
764 consumer_stage,
765 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700766 return false;
767 }
768 }
769 }
770
771 return true;
772}
773
774
Ian Romanick3fb87872010-07-09 14:09:34 -0700775/**
776 * Populates a shaders symbol table with all global declarations
777 */
778static void
779populate_symbol_table(gl_shader *sh)
780{
781 sh->symbols = new(sh) glsl_symbol_table;
782
783 foreach_list(node, sh->ir) {
784 ir_instruction *const inst = (ir_instruction *) node;
785 ir_variable *var;
786 ir_function *func;
787
788 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700789 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700790 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700791 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700792 }
793 }
794}
795
796
797/**
Ian Romanick31a97862010-07-12 18:48:50 -0700798 * Remap variables referenced in an instruction tree
799 *
800 * This is used when instruction trees are cloned from one shader and placed in
801 * another. These trees will contain references to \c ir_variable nodes that
802 * do not exist in the target shader. This function finds these \c ir_variable
803 * references and replaces the references with matching variables in the target
804 * shader.
805 *
806 * If there is no matching variable in the target shader, a clone of the
807 * \c ir_variable is made and added to the target shader. The new variable is
808 * added to \b both the instruction stream and the symbol table.
809 *
810 * \param inst IR tree that is to be processed.
811 * \param symbols Symbol table containing global scope symbols in the
812 * linked shader.
813 * \param instructions Instruction stream where new variable declarations
814 * should be added.
815 */
816void
Eric Anholt8273bd42010-08-04 12:34:56 -0700817remap_variables(ir_instruction *inst, struct gl_shader *target,
818 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700819{
820 class remap_visitor : public ir_hierarchical_visitor {
821 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700822 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700823 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700824 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700825 this->target = target;
826 this->symbols = target->symbols;
827 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700828 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700829 }
830
831 virtual ir_visitor_status visit(ir_dereference_variable *ir)
832 {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700833 if (ir->var->mode == ir_var_temporary) {
834 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
835
836 assert(var != NULL);
837 ir->var = var;
838 return visit_continue;
839 }
840
Ian Romanick31a97862010-07-12 18:48:50 -0700841 ir_variable *const existing =
842 this->symbols->get_variable(ir->var->name);
843 if (existing != NULL)
844 ir->var = existing;
845 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700846 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700847
Eric Anholt001eee52010-11-05 06:11:24 -0700848 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700849 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700850 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700851 }
852
853 return visit_continue;
854 }
855
856 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700857 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700858 glsl_symbol_table *symbols;
859 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700860 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700861 };
862
Eric Anholt8273bd42010-08-04 12:34:56 -0700863 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700864
865 inst->accept(&v);
866}
867
868
869/**
870 * Move non-declarations from one instruction stream to another
871 *
872 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700873 * 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 -0700874 * pointer) for \c last and \c false for \c make_copies on the first
875 * call. Successive calls pass the return value of the previous call for
876 * \c last and \c true for \c make_copies.
877 *
878 * \param instructions Source instruction stream
879 * \param last Instruction after which new instructions should be
880 * inserted in the target instruction stream
881 * \param make_copies Flag selecting whether instructions in \c instructions
882 * should be copied (via \c ir_instruction::clone) into the
883 * target list or moved.
884 *
885 * \return
886 * The new "last" instruction in the target instruction stream. This pointer
887 * is suitable for use as the \c last parameter of a later call to this
888 * function.
889 */
890exec_node *
891move_non_declarations(exec_list *instructions, exec_node *last,
892 bool make_copies, gl_shader *target)
893{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700894 hash_table *temps = NULL;
895
896 if (make_copies)
897 temps = hash_table_ctor(0, hash_table_pointer_hash,
898 hash_table_pointer_compare);
899
Ian Romanick303c99f2010-07-19 12:34:56 -0700900 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700901 ir_instruction *inst = (ir_instruction *) node;
902
Ian Romanick7e2aa912010-07-19 17:12:42 -0700903 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700904 continue;
905
Ian Romanick7e2aa912010-07-19 17:12:42 -0700906 ir_variable *var = inst->as_variable();
907 if ((var != NULL) && (var->mode != ir_var_temporary))
908 continue;
909
910 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700911 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700912 || inst->as_if() /* for initializers with the ?: operator */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700913 || ((var != NULL) && (var->mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700914
915 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700916 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700917
918 if (var != NULL)
919 hash_table_insert(temps, inst, var);
920 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700921 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700922 } else {
923 inst->remove();
924 }
925
926 last->insert_after(inst);
927 last = inst;
928 }
929
Ian Romanick7e2aa912010-07-19 17:12:42 -0700930 if (make_copies)
931 hash_table_dtor(temps);
932
Ian Romanick31a97862010-07-12 18:48:50 -0700933 return last;
934}
935
936/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700937 * Get the function signature for main from a shader
938 */
939static ir_function_signature *
940get_main_function_signature(gl_shader *sh)
941{
942 ir_function *const f = sh->symbols->get_function("main");
943 if (f != NULL) {
944 exec_list void_parameters;
945
946 /* Look for the 'void main()' signature and ensure that it's defined.
947 * This keeps the linker from accidentally pick a shader that just
948 * contains a prototype for main.
949 *
950 * We don't have to check for multiple definitions of main (in multiple
951 * shaders) because that would have already been caught above.
952 */
953 ir_function_signature *sig = f->matching_signature(&void_parameters);
954 if ((sig != NULL) && sig->is_defined) {
955 return sig;
956 }
957 }
958
959 return NULL;
960}
961
962
963/**
Brian Paul84a12732012-02-02 20:10:40 -0700964 * This class is only used in link_intrastage_shaders() below but declaring
965 * it inside that function leads to compiler warnings with some versions of
966 * gcc.
967 */
968class array_sizing_visitor : public ir_hierarchical_visitor {
969public:
970 virtual ir_visitor_status visit(ir_variable *var)
971 {
972 if (var->type->is_array() && (var->type->length == 0)) {
973 const glsl_type *type =
974 glsl_type::get_array_instance(var->type->fields.array,
975 var->max_array_access + 1);
976 assert(type != NULL);
977 var->type = type;
978 }
979 return visit_continue;
980 }
981};
982
Brian Paul84a12732012-02-02 20:10:40 -0700983/**
Ian Romanick3fb87872010-07-09 14:09:34 -0700984 * Combine a group of shaders for a single stage to generate a linked shader
985 *
986 * \note
987 * If this function is supplied a single shader, it is cloned, and the new
988 * shader is returned.
989 */
990static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -0800991link_intrastage_shaders(void *mem_ctx,
992 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -0700993 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -0700994 struct gl_shader **shader_list,
995 unsigned num_shaders)
996{
Eric Anholtf609cf72012-04-27 13:52:56 -0700997 struct gl_uniform_block *uniform_blocks = NULL;
998 unsigned num_uniform_blocks = 0;
999
Ian Romanick13f782c2010-06-29 18:53:38 -07001000 /* Check that global variables defined in multiple shaders are consistent.
1001 */
1002 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
1003 return NULL;
1004
Eric Anholtf609cf72012-04-27 13:52:56 -07001005 /* Check that uniform blocks between shaders for a stage agree. */
1006 for (unsigned i = 0; i < num_shaders; i++) {
1007 struct gl_shader *sh = shader_list[i];
1008
1009 for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) {
Eric Anholt8ab58422012-05-01 15:10:14 -07001010 link_assign_uniform_block_offsets(shader_list[i]);
1011
Eric Anholtf609cf72012-04-27 13:52:56 -07001012 int index = link_cross_validate_uniform_block(mem_ctx,
1013 &uniform_blocks,
1014 &num_uniform_blocks,
1015 &sh->UniformBlocks[j]);
1016 if (index == -1) {
1017 linker_error(prog, "uniform block `%s' has mismatching definitions",
1018 sh->UniformBlocks[j].Name);
1019 return NULL;
1020 }
1021 }
1022 }
1023
Ian Romanick13f782c2010-06-29 18:53:38 -07001024 /* Check that there is only a single definition of each function signature
1025 * across all shaders.
1026 */
1027 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1028 foreach_list(node, shader_list[i]->ir) {
1029 ir_function *const f = ((ir_instruction *) node)->as_function();
1030
1031 if (f == NULL)
1032 continue;
1033
1034 for (unsigned j = i + 1; j < num_shaders; j++) {
1035 ir_function *const other =
1036 shader_list[j]->symbols->get_function(f->name);
1037
1038 /* If the other shader has no function (and therefore no function
1039 * signatures) with the same name, skip to the next shader.
1040 */
1041 if (other == NULL)
1042 continue;
1043
1044 foreach_iter (exec_list_iterator, iter, *f) {
1045 ir_function_signature *sig =
1046 (ir_function_signature *) iter.get();
1047
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001048 if (!sig->is_defined || sig->is_builtin)
Ian Romanick13f782c2010-06-29 18:53:38 -07001049 continue;
1050
1051 ir_function_signature *other_sig =
1052 other->exact_matching_signature(& sig->parameters);
1053
1054 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunkef412fac2010-09-05 01:48:11 -07001055 && !other_sig->is_builtin) {
Ian Romanick586e7412011-07-28 14:04:09 -07001056 linker_error(prog, "function `%s' is multiply defined",
1057 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001058 return NULL;
1059 }
1060 }
1061 }
1062 }
1063 }
1064
1065 /* Find the shader that defines main, and make a clone of it.
1066 *
1067 * Starting with the clone, search for undefined references. If one is
1068 * found, find the shader that defines it. Clone the reference and add
1069 * it to the shader. Repeat until there are no undefined references or
1070 * until a reference cannot be resolved.
1071 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001072 gl_shader *main = NULL;
1073 for (unsigned i = 0; i < num_shaders; i++) {
1074 if (get_main_function_signature(shader_list[i]) != NULL) {
1075 main = shader_list[i];
1076 break;
1077 }
1078 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001079
Ian Romanick15ce87e2010-07-09 15:28:22 -07001080 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001081 linker_error(prog, "%s shader lacks `main'\n",
1082 (shader_list[0]->Type == GL_VERTEX_SHADER)
1083 ? "vertex" : "fragment");
Ian Romanick15ce87e2010-07-09 15:28:22 -07001084 return NULL;
1085 }
1086
Ian Romanick4a455952010-10-13 15:13:02 -07001087 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001088 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001089 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001090
Eric Anholtf609cf72012-04-27 13:52:56 -07001091 linked->UniformBlocks = uniform_blocks;
1092 linked->NumUniformBlocks = num_uniform_blocks;
1093 ralloc_steal(linked, linked->UniformBlocks);
1094
Ian Romanick15ce87e2010-07-09 15:28:22 -07001095 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001096
Ian Romanick31a97862010-07-12 18:48:50 -07001097 /* The a pointer to the main function in the final linked shader (i.e., the
1098 * copy of the original shader that contained the main function).
1099 */
1100 ir_function_signature *const main_sig = get_main_function_signature(linked);
1101
1102 /* Move any instructions other than variable declarations or function
1103 * declarations into main.
1104 */
Ian Romanick9303e352010-07-19 12:33:54 -07001105 exec_node *insertion_point =
1106 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1107 linked);
1108
Ian Romanick31a97862010-07-12 18:48:50 -07001109 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001110 if (shader_list[i] == main)
1111 continue;
1112
Ian Romanick31a97862010-07-12 18:48:50 -07001113 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001114 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001115 }
1116
Ian Romanick13f782c2010-06-29 18:53:38 -07001117 /* Resolve initializers for global variables in the linked shader.
1118 */
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001119 unsigned num_linking_shaders = num_shaders;
1120 for (unsigned i = 0; i < num_shaders; i++)
1121 num_linking_shaders += shader_list[i]->num_builtins_to_link;
1122
1123 gl_shader **linking_shaders =
1124 (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1125
1126 memcpy(linking_shaders, shader_list,
1127 sizeof(linking_shaders[0]) * num_shaders);
1128
1129 unsigned idx = num_shaders;
1130 for (unsigned i = 0; i < num_shaders; i++) {
1131 memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1132 sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1133 idx += shader_list[i]->num_builtins_to_link;
1134 }
1135
1136 assert(idx == num_linking_shaders);
1137
Ian Romanick4a455952010-10-13 15:13:02 -07001138 if (!link_function_calls(prog, linked, linking_shaders,
1139 num_linking_shaders)) {
1140 ctx->Driver.DeleteShader(ctx, linked);
1141 linked = NULL;
1142 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001143
1144 free(linking_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07001145
Paul Berryc148ef62011-08-03 15:37:01 -07001146#ifdef DEBUG
1147 /* At this point linked should contain all of the linked IR, so
1148 * validate it to make sure nothing went wrong.
1149 */
1150 if (linked)
1151 validate_ir_tree(linked->ir);
1152#endif
1153
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001154 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001155 * unspecified sizes have a size specified. The size is inferred from the
1156 * max_array_access field.
1157 */
Ian Romanick002cd2c2010-12-07 19:00:44 -08001158 if (linked != NULL) {
Brian Paul84a12732012-02-02 20:10:40 -07001159 array_sizing_visitor v;
Ian Romanick6f539212010-12-07 18:30:33 -08001160
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001161 v.run(linked->ir);
Ian Romanick6f539212010-12-07 18:30:33 -08001162 }
1163
Ian Romanick3fb87872010-07-09 14:09:34 -07001164 return linked;
1165}
1166
Eric Anholta721abf2010-08-23 10:32:01 -07001167/**
1168 * Update the sizes of linked shader uniform arrays to the maximum
1169 * array index used.
1170 *
1171 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1172 *
1173 * If one or more elements of an array are active,
1174 * GetActiveUniform will return the name of the array in name,
1175 * subject to the restrictions listed above. The type of the array
1176 * is returned in type. The size parameter contains the highest
1177 * array element index used, plus one. The compiler or linker
1178 * determines the highest index used. There will be only one
1179 * active uniform reported by the GL per uniform array.
1180
1181 */
1182static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001183update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001184{
Ian Romanick3322fba2010-10-14 13:28:42 -07001185 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1186 if (prog->_LinkedShaders[i] == NULL)
1187 continue;
1188
Eric Anholta721abf2010-08-23 10:32:01 -07001189 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1190 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1191
Eric Anholt586b4b52010-09-28 14:32:16 -07001192 if ((var == NULL) || (var->mode != ir_var_uniform &&
1193 var->mode != ir_var_in &&
1194 var->mode != ir_var_out) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001195 !var->type->is_array())
1196 continue;
1197
Eric Anholt9feb4032012-05-01 14:43:31 -07001198 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1199 * will not be eliminated. Since we always do std140, just
1200 * don't resize arrays in UBOs.
1201 */
1202 if (var->uniform_block != -1)
1203 continue;
1204
Eric Anholta721abf2010-08-23 10:32:01 -07001205 unsigned int size = var->max_array_access;
Ian Romanick3322fba2010-10-14 13:28:42 -07001206 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1207 if (prog->_LinkedShaders[j] == NULL)
1208 continue;
1209
Eric Anholta721abf2010-08-23 10:32:01 -07001210 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1211 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1212 if (!other_var)
1213 continue;
1214
1215 if (strcmp(var->name, other_var->name) == 0 &&
1216 other_var->max_array_access > size) {
1217 size = other_var->max_array_access;
1218 }
1219 }
1220 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001221
Eric Anholta721abf2010-08-23 10:32:01 -07001222 if (size + 1 != var->type->fields.array->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001223 /* If this is a built-in uniform (i.e., it's backed by some
1224 * fixed-function state), adjust the number of state slots to
1225 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001226 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001227 * slots is an integer multiple of the number of array elements.
1228 * Determine the number of slots per array element by dividing by
1229 * the old (total) size.
1230 */
1231 if (var->num_state_slots > 0) {
1232 var->num_state_slots = (size + 1)
1233 * (var->num_state_slots / var->type->length);
1234 }
1235
Eric Anholta721abf2010-08-23 10:32:01 -07001236 var->type = glsl_type::get_array_instance(var->type->fields.array,
1237 size + 1);
1238 /* FINISHME: We should update the types of array
1239 * dereferences of this variable now.
1240 */
1241 }
1242 }
1243 }
1244}
1245
Ian Romanick69846702010-06-22 17:29:19 -07001246/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001247 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001248 *
1249 * \param used_mask Bits representing used (1) and unused (0) locations
1250 * \param needed_count Number of contiguous bits needed.
1251 *
1252 * \return
1253 * Base location of the available bits on success or -1 on failure.
1254 */
1255int
1256find_available_slots(unsigned used_mask, unsigned needed_count)
1257{
1258 unsigned needed_mask = (1 << needed_count) - 1;
1259 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1260
1261 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1262 * cannot optimize possibly infinite loops" for the loop below.
1263 */
1264 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1265 return -1;
1266
1267 for (int i = 0; i <= max_bit_to_test; i++) {
1268 if ((needed_mask & ~used_mask) == needed_mask)
1269 return i;
1270
1271 needed_mask <<= 1;
1272 }
1273
1274 return -1;
1275}
1276
1277
Ian Romanickd32d4f72011-06-27 17:59:58 -07001278/**
1279 * Assign locations for either VS inputs for FS outputs
1280 *
1281 * \param prog Shader program whose variables need locations assigned
1282 * \param target_index Selector for the program target to receive location
1283 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1284 * \c MESA_SHADER_FRAGMENT.
1285 * \param max_index Maximum number of generic locations. This corresponds
1286 * to either the maximum number of draw buffers or the
1287 * maximum number of generic attributes.
1288 *
1289 * \return
1290 * If locations are successfully assigned, true is returned. Otherwise an
1291 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001292 */
Ian Romanick69846702010-06-22 17:29:19 -07001293bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001294assign_attribute_or_color_locations(gl_shader_program *prog,
1295 unsigned target_index,
1296 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001297{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001298 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001299 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001300 unsigned used_locations = (max_index >= 32)
1301 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001302
Ian Romanickd32d4f72011-06-27 17:59:58 -07001303 assert((target_index == MESA_SHADER_VERTEX)
1304 || (target_index == MESA_SHADER_FRAGMENT));
1305
1306 gl_shader *const sh = prog->_LinkedShaders[target_index];
1307 if (sh == NULL)
1308 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001309
Ian Romanick69846702010-06-22 17:29:19 -07001310 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001311 *
1312 * 1. Invalidate the location assignments for all vertex shader inputs.
1313 *
1314 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001315 * glBindVertexAttribLocation) locations and outputs that have
1316 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001317 *
Ian Romanick69846702010-06-22 17:29:19 -07001318 * 3. Sort the attributes without assigned locations by number of slots
1319 * required in decreasing order. Fragmentation caused by attribute
1320 * locations assigned by the application may prevent large attributes
1321 * from having enough contiguous space.
1322 *
1323 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001324 */
1325
Ian Romanickd32d4f72011-06-27 17:59:58 -07001326 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001327 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001328
Ian Romanickd32d4f72011-06-27 17:59:58 -07001329 const enum ir_variable_mode direction =
1330 (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
1331
1332
Ian Romanick69846702010-06-22 17:29:19 -07001333 /* Temporary storage for the set of attributes that need locations assigned.
1334 */
1335 struct temp_attr {
1336 unsigned slots;
1337 ir_variable *var;
1338
1339 /* Used below in the call to qsort. */
1340 static int compare(const void *a, const void *b)
1341 {
1342 const temp_attr *const l = (const temp_attr *) a;
1343 const temp_attr *const r = (const temp_attr *) b;
1344
1345 /* Reversed because we want a descending order sort below. */
1346 return r->slots - l->slots;
1347 }
1348 } to_assign[16];
1349
1350 unsigned num_attr = 0;
1351
Eric Anholt16b68b12010-06-30 11:05:43 -07001352 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001353 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1354
Brian Paul4470ff22011-07-19 21:10:25 -06001355 if ((var == NULL) || (var->mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001356 continue;
1357
Ian Romanick68a4fc92010-10-07 17:21:22 -07001358 if (var->explicit_location) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001359 if ((var->location >= (int)(max_index + generic_base))
Ian Romanick68a4fc92010-10-07 17:21:22 -07001360 || (var->location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001361 linker_error(prog,
1362 "invalid explicit location %d specified for `%s'\n",
Ian Romanick523b6112011-08-17 15:40:03 -07001363 (var->location < 0)
1364 ? var->location : var->location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001365 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001366 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001367 }
1368 } else if (target_index == MESA_SHADER_VERTEX) {
1369 unsigned binding;
1370
1371 if (prog->AttributeBindings->get(binding, var->name)) {
1372 assert(binding >= VERT_ATTRIB_GENERIC0);
1373 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001374 var->is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001375 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001376 } else if (target_index == MESA_SHADER_FRAGMENT) {
1377 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001378 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001379
1380 if (prog->FragDataBindings->get(binding, var->name)) {
1381 assert(binding >= FRAG_RESULT_DATA0);
1382 var->location = binding;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001383 var->is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001384
1385 if (prog->FragDataIndexBindings->get(index, var->name)) {
1386 var->index = index;
1387 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001388 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001389 }
1390
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001391 /* If the variable is not a built-in and has a location statically
1392 * assigned in the shader (presumably via a layout qualifier), make sure
1393 * that it doesn't collide with other assigned locations. Otherwise,
1394 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001395 */
Ian Romanick523b6112011-08-17 15:40:03 -07001396 const unsigned slots = count_attribute_slots(var->type);
1397 if (var->location != -1) {
Dave Airlie1256a5d2012-03-24 13:33:41 +00001398 if (var->location >= generic_base && var->index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001399 /* From page 61 of the OpenGL 4.0 spec:
1400 *
1401 * "LinkProgram will fail if the attribute bindings assigned
1402 * by BindAttribLocation do not leave not enough space to
1403 * assign a location for an active matrix attribute or an
1404 * active attribute array, both of which require multiple
1405 * contiguous generic attributes."
1406 *
1407 * Previous versions of the spec contain similar language but omit
1408 * the bit about attribute arrays.
1409 *
1410 * Page 61 of the OpenGL 4.0 spec also says:
1411 *
1412 * "It is possible for an application to bind more than one
1413 * attribute name to the same location. This is referred to as
1414 * aliasing. This will only work if only one of the aliased
1415 * attributes is active in the executable program, or if no
1416 * path through the shader consumes more than one attribute of
1417 * a set of attributes aliased to the same location. A link
1418 * error can occur if the linker determines that every path
1419 * through the shader consumes multiple aliased attributes,
1420 * but implementations are not required to generate an error
1421 * in this case."
1422 *
1423 * These two paragraphs are either somewhat contradictory, or I
1424 * don't fully understand one or both of them.
1425 */
1426 /* FINISHME: The code as currently written does not support
1427 * FINISHME: attribute location aliasing (see comment above).
1428 */
1429 /* Mask representing the contiguous slots that will be used by
1430 * this attribute.
1431 */
1432 const unsigned attr = var->location - generic_base;
1433 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001434
Ian Romanick523b6112011-08-17 15:40:03 -07001435 /* Generate a link error if the set of bits requested for this
1436 * attribute overlaps any previously allocated bits.
1437 */
1438 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001439 const char *const string = (target_index == MESA_SHADER_VERTEX)
1440 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001441 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001442 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001443 "available for %s `%s' %d %d %d", string,
1444 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001445 return false;
1446 }
1447
1448 used_locations |= (use_mask << attr);
1449 }
1450
1451 continue;
1452 }
1453
1454 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001455 to_assign[num_attr].var = var;
1456 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001457 }
Ian Romanick69846702010-06-22 17:29:19 -07001458
1459 /* If all of the attributes were assigned locations by the application (or
1460 * are built-in attributes with fixed locations), return early. This should
1461 * be the common case.
1462 */
1463 if (num_attr == 0)
1464 return true;
1465
1466 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1467
Ian Romanickd32d4f72011-06-27 17:59:58 -07001468 if (target_index == MESA_SHADER_VERTEX) {
1469 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1470 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1471 * reserved to prevent it from being automatically allocated below.
1472 */
1473 find_deref_visitor find("gl_Vertex");
1474 find.run(sh->ir);
1475 if (find.variable_found())
1476 used_locations |= (1 << 0);
1477 }
Ian Romanick982e3792010-06-29 18:58:20 -07001478
Ian Romanick69846702010-06-22 17:29:19 -07001479 for (unsigned i = 0; i < num_attr; i++) {
1480 /* Mask representing the contiguous slots that will be used by this
1481 * attribute.
1482 */
1483 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1484
1485 int location = find_available_slots(used_locations, to_assign[i].slots);
1486
1487 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001488 const char *const string = (target_index == MESA_SHADER_VERTEX)
1489 ? "vertex shader input" : "fragment shader output";
1490
Ian Romanick586e7412011-07-28 14:04:09 -07001491 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001492 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001493 "available for %s `%s'",
1494 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001495 return false;
1496 }
1497
Ian Romanickd32d4f72011-06-27 17:59:58 -07001498 to_assign[i].var->location = generic_base + location;
Paul Berry3c9c17d2012-12-04 15:17:01 -08001499 to_assign[i].var->is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001500 used_locations |= (use_mask << location);
1501 }
1502
1503 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001504}
1505
1506
Ian Romanick40e114b2010-08-17 14:55:50 -07001507/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001508 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001509 */
1510void
Ian Romanickcc90e622010-10-19 17:59:10 -07001511demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001512{
1513 foreach_list(node, sh->ir) {
1514 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1515
Ian Romanickcc90e622010-10-19 17:59:10 -07001516 if ((var == NULL) || (var->mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001517 continue;
1518
Ian Romanickcc90e622010-10-19 17:59:10 -07001519 /* A shader 'in' or 'out' variable is only really an input or output if
1520 * its value is used by other shader stages. This will cause the variable
1521 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001522 */
Paul Berry3c9c17d2012-12-04 15:17:01 -08001523 if (var->is_unmatched_generic_inout) {
Ian Romanick40e114b2010-08-17 14:55:50 -07001524 var->mode = ir_var_auto;
1525 }
1526 }
1527}
1528
1529
Paul Berry871ddb92011-11-05 11:17:32 -07001530/**
1531 * Data structure tracking information about a transform feedback declaration
1532 * during linking.
1533 */
1534class tfeedback_decl
1535{
1536public:
Paul Berry456279b2011-12-26 19:39:25 -08001537 bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1538 const void *mem_ctx, const char *input);
Paul Berry871ddb92011-11-05 11:17:32 -07001539 static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1540 bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1541 ir_variable *output_var);
Paul Berry25ed3be2012-12-04 10:34:45 -08001542 unsigned get_num_outputs() const;
Paul Berryd3150eb2012-01-09 11:25:14 -08001543 bool store(struct gl_context *ctx, struct gl_shader_program *prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08001544 struct gl_transform_feedback_info *info, unsigned buffer,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001545 const unsigned max_outputs) const;
Paul Berry25ed3be2012-12-04 10:34:45 -08001546 ir_variable *find_output_var(gl_shader_program *prog,
1547 gl_shader *producer) const;
Paul Berry871ddb92011-11-05 11:17:32 -07001548
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001549 bool is_next_buffer_separator() const
1550 {
1551 return this->next_buffer_separator;
1552 }
1553
1554 bool is_varying() const
1555 {
1556 return !this->next_buffer_separator && !this->skip_components;
1557 }
1558
Paul Berry871ddb92011-11-05 11:17:32 -07001559 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001560 * The total number of varying components taken up by this variable. Only
Paul Berry25ed3be2012-12-04 10:34:45 -08001561 * valid if assign_location() has been called.
Paul Berry871ddb92011-11-05 11:17:32 -07001562 */
1563 unsigned num_components() const
1564 {
Paul Berry642e5b412012-01-04 13:57:52 -08001565 if (this->is_clip_distance_mesa)
1566 return this->size;
Paul Berrybe4e9f72012-01-04 12:21:55 -08001567 else
Paul Berry642e5b412012-01-04 13:57:52 -08001568 return this->vector_elements * this->matrix_columns * this->size;
Paul Berry871ddb92011-11-05 11:17:32 -07001569 }
1570
1571private:
1572 /**
1573 * The name that was supplied to glTransformFeedbackVaryings. Used for
Eric Anholt9d36c962012-01-02 17:08:13 -08001574 * error reporting and glGetTransformFeedbackVarying().
Paul Berry871ddb92011-11-05 11:17:32 -07001575 */
1576 const char *orig_name;
1577
1578 /**
1579 * The name of the variable, parsed from orig_name.
1580 */
Paul Berry913a5c22011-12-27 08:24:57 -08001581 const char *var_name;
Paul Berry871ddb92011-11-05 11:17:32 -07001582
1583 /**
1584 * True if the declaration in orig_name represents an array.
1585 */
Paul Berry33fe0212012-01-03 20:41:34 -08001586 bool is_subscripted;
Paul Berry871ddb92011-11-05 11:17:32 -07001587
1588 /**
Paul Berry33fe0212012-01-03 20:41:34 -08001589 * If is_subscripted is true, the subscript that was specified in orig_name.
Paul Berry871ddb92011-11-05 11:17:32 -07001590 */
Paul Berry33fe0212012-01-03 20:41:34 -08001591 unsigned array_subscript;
Paul Berry871ddb92011-11-05 11:17:32 -07001592
1593 /**
Paul Berry642e5b412012-01-04 13:57:52 -08001594 * True if the variable is gl_ClipDistance and the driver lowers
1595 * gl_ClipDistance to gl_ClipDistanceMESA.
Paul Berry456279b2011-12-26 19:39:25 -08001596 */
Paul Berry642e5b412012-01-04 13:57:52 -08001597 bool is_clip_distance_mesa;
Paul Berry456279b2011-12-26 19:39:25 -08001598
1599 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001600 * The vertex shader output location that the linker assigned for this
1601 * variable. -1 if a location hasn't been assigned yet.
1602 */
1603 int location;
1604
1605 /**
Paul Berrydf877222012-12-09 20:59:26 -08001606 * If non-zero, then this variable may be packed along with other variables
1607 * into a single varying slot, so this offset should be applied when
1608 * accessing components. For example, an offset of 1 means that the x
1609 * component of this variable is actually stored in component y of the
1610 * location specified by \c location.
1611 *
1612 * Only valid if location != -1.
1613 */
1614 unsigned location_frac;
1615
1616 /**
Paul Berry871ddb92011-11-05 11:17:32 -07001617 * If location != -1, the number of vector elements in this variable, or 1
1618 * if this variable is a scalar.
1619 */
1620 unsigned vector_elements;
1621
1622 /**
1623 * If location != -1, the number of matrix columns in this variable, or 1
1624 * if this variable is not a matrix.
1625 */
1626 unsigned matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001627
1628 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1629 GLenum type;
Paul Berry33fe0212012-01-03 20:41:34 -08001630
1631 /**
1632 * If location != -1, the size that should be returned by
1633 * glGetTransformFeedbackVarying().
1634 */
1635 unsigned size;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001636
1637 /**
1638 * How many components to skip. If non-zero, this is
1639 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1640 */
1641 unsigned skip_components;
1642
1643 /**
1644 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1645 */
1646 bool next_buffer_separator;
Paul Berry871ddb92011-11-05 11:17:32 -07001647};
1648
1649
1650/**
1651 * Initialize this object based on a string that was passed to
1652 * glTransformFeedbackVaryings. If there is a parse error, the error is
1653 * reported using linker_error(), and false is returned.
1654 */
1655bool
Paul Berry456279b2011-12-26 19:39:25 -08001656tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1657 const void *mem_ctx, const char *input)
Paul Berry871ddb92011-11-05 11:17:32 -07001658{
1659 /* We don't have to be pedantic about what is a valid GLSL variable name,
1660 * because any variable with an invalid name can't exist in the IR anyway.
1661 */
1662
1663 this->location = -1;
1664 this->orig_name = input;
Paul Berry642e5b412012-01-04 13:57:52 -08001665 this->is_clip_distance_mesa = false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001666 this->skip_components = 0;
1667 this->next_buffer_separator = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001668
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001669 if (ctx->Extensions.ARB_transform_feedback3) {
1670 /* Parse gl_NextBuffer. */
1671 if (strcmp(input, "gl_NextBuffer") == 0) {
1672 this->next_buffer_separator = true;
1673 return true;
1674 }
1675
1676 /* Parse gl_SkipComponents. */
1677 if (strcmp(input, "gl_SkipComponents1") == 0)
1678 this->skip_components = 1;
1679 else if (strcmp(input, "gl_SkipComponents2") == 0)
1680 this->skip_components = 2;
1681 else if (strcmp(input, "gl_SkipComponents3") == 0)
1682 this->skip_components = 3;
1683 else if (strcmp(input, "gl_SkipComponents4") == 0)
1684 this->skip_components = 4;
1685
1686 if (this->skip_components)
1687 return true;
1688 }
1689
1690 /* Parse a declaration. */
Paul Berry871ddb92011-11-05 11:17:32 -07001691 const char *bracket = strrchr(input, '[');
1692
1693 if (bracket) {
1694 this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
Paul Berry33fe0212012-01-03 20:41:34 -08001695 if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
Paul Berry456279b2011-12-26 19:39:25 -08001696 linker_error(prog, "Cannot parse transform feedback varying %s", input);
1697 return false;
Paul Berry871ddb92011-11-05 11:17:32 -07001698 }
Paul Berry33fe0212012-01-03 20:41:34 -08001699 this->is_subscripted = true;
Paul Berry871ddb92011-11-05 11:17:32 -07001700 } else {
1701 this->var_name = ralloc_strdup(mem_ctx, input);
Paul Berry33fe0212012-01-03 20:41:34 -08001702 this->is_subscripted = false;
Paul Berry871ddb92011-11-05 11:17:32 -07001703 }
1704
Paul Berry642e5b412012-01-04 13:57:52 -08001705 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1706 * class must behave specially to account for the fact that gl_ClipDistance
1707 * is converted from a float[8] to a vec4[2].
Paul Berry456279b2011-12-26 19:39:25 -08001708 */
1709 if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1710 strcmp(this->var_name, "gl_ClipDistance") == 0) {
Paul Berry642e5b412012-01-04 13:57:52 -08001711 this->is_clip_distance_mesa = true;
Paul Berry456279b2011-12-26 19:39:25 -08001712 }
1713
1714 return true;
Paul Berry871ddb92011-11-05 11:17:32 -07001715}
1716
1717
1718/**
1719 * Determine whether two tfeedback_decl objects refer to the same variable and
1720 * array index (if applicable).
1721 */
1722bool
1723tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1724{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001725 assert(x.is_varying() && y.is_varying());
1726
Paul Berry871ddb92011-11-05 11:17:32 -07001727 if (strcmp(x.var_name, y.var_name) != 0)
1728 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001729 if (x.is_subscripted != y.is_subscripted)
Paul Berry871ddb92011-11-05 11:17:32 -07001730 return false;
Paul Berry33fe0212012-01-03 20:41:34 -08001731 if (x.is_subscripted && x.array_subscript != y.array_subscript)
Paul Berry871ddb92011-11-05 11:17:32 -07001732 return false;
1733 return true;
1734}
1735
1736
1737/**
1738 * Assign a location for this tfeedback_decl object based on the location
1739 * assignment in output_var.
1740 *
1741 * If an error occurs, the error is reported through linker_error() and false
1742 * is returned.
1743 */
1744bool
1745tfeedback_decl::assign_location(struct gl_context *ctx,
1746 struct gl_shader_program *prog,
1747 ir_variable *output_var)
1748{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001749 assert(this->is_varying());
1750
Paul Berry871ddb92011-11-05 11:17:32 -07001751 if (output_var->type->is_array()) {
1752 /* Array variable */
Paul Berry871ddb92011-11-05 11:17:32 -07001753 const unsigned matrix_cols =
1754 output_var->type->fields.array->matrix_columns;
Paul Berrydf877222012-12-09 20:59:26 -08001755 const unsigned vector_elements =
1756 output_var->type->fields.array->vector_elements;
Paul Berry642e5b412012-01-04 13:57:52 -08001757 unsigned actual_array_size = this->is_clip_distance_mesa ?
1758 prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
Paul Berry33fe0212012-01-03 20:41:34 -08001759
1760 if (this->is_subscripted) {
1761 /* Check array bounds. */
Paul Berry642e5b412012-01-04 13:57:52 -08001762 if (this->array_subscript >= actual_array_size) {
Paul Berry33fe0212012-01-03 20:41:34 -08001763 linker_error(prog, "Transform feedback varying %s has index "
Paul Berry642e5b412012-01-04 13:57:52 -08001764 "%i, but the array size is %u.",
Paul Berry33fe0212012-01-03 20:41:34 -08001765 this->orig_name, this->array_subscript,
Paul Berry642e5b412012-01-04 13:57:52 -08001766 actual_array_size);
Paul Berry33fe0212012-01-03 20:41:34 -08001767 return false;
1768 }
Paul Berry642e5b412012-01-04 13:57:52 -08001769 if (this->is_clip_distance_mesa) {
1770 this->location =
1771 output_var->location + this->array_subscript / 4;
Paul Berrydf877222012-12-09 20:59:26 -08001772 this->location_frac = this->array_subscript % 4;
Paul Berry642e5b412012-01-04 13:57:52 -08001773 } else {
Paul Berrydf877222012-12-09 20:59:26 -08001774 unsigned fine_location
1775 = output_var->location * 4 + output_var->location_frac;
1776 unsigned array_elem_size = vector_elements * matrix_cols;
1777 fine_location += array_elem_size * this->array_subscript;
1778 this->location = fine_location / 4;
1779 this->location_frac = fine_location % 4;
Paul Berry642e5b412012-01-04 13:57:52 -08001780 }
Paul Berry33fe0212012-01-03 20:41:34 -08001781 this->size = 1;
1782 } else {
1783 this->location = output_var->location;
Paul Berrydf877222012-12-09 20:59:26 -08001784 this->location_frac = output_var->location_frac;
Paul Berry642e5b412012-01-04 13:57:52 -08001785 this->size = actual_array_size;
Paul Berry33fe0212012-01-03 20:41:34 -08001786 }
Paul Berrydf877222012-12-09 20:59:26 -08001787 this->vector_elements = vector_elements;
Paul Berry871ddb92011-11-05 11:17:32 -07001788 this->matrix_columns = matrix_cols;
Paul Berry642e5b412012-01-04 13:57:52 -08001789 if (this->is_clip_distance_mesa)
1790 this->type = GL_FLOAT;
1791 else
1792 this->type = output_var->type->fields.array->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001793 } else {
1794 /* Regular variable (scalar, vector, or matrix) */
Paul Berry33fe0212012-01-03 20:41:34 -08001795 if (this->is_subscripted) {
Paul Berry108cba22012-01-04 15:17:52 -08001796 linker_error(prog, "Transform feedback varying %s requested, "
1797 "but %s is not an array.",
1798 this->orig_name, this->var_name);
Paul Berry871ddb92011-11-05 11:17:32 -07001799 return false;
1800 }
1801 this->location = output_var->location;
Paul Berrydf877222012-12-09 20:59:26 -08001802 this->location_frac = output_var->location_frac;
Paul Berry33fe0212012-01-03 20:41:34 -08001803 this->size = 1;
Paul Berry871ddb92011-11-05 11:17:32 -07001804 this->vector_elements = output_var->type->vector_elements;
1805 this->matrix_columns = output_var->type->matrix_columns;
Eric Anholt9d36c962012-01-02 17:08:13 -08001806 this->type = output_var->type->gl_type;
Paul Berry871ddb92011-11-05 11:17:32 -07001807 }
Paul Berry642e5b412012-01-04 13:57:52 -08001808
Paul Berry871ddb92011-11-05 11:17:32 -07001809 /* From GL_EXT_transform_feedback:
1810 * A program will fail to link if:
1811 *
1812 * * the total number of components to capture in any varying
1813 * variable in <varyings> is greater than the constant
1814 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1815 * buffer mode is SEPARATE_ATTRIBS_EXT;
1816 */
1817 if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1818 this->num_components() >
1819 ctx->Const.MaxTransformFeedbackSeparateComponents) {
1820 linker_error(prog, "Transform feedback varying %s exceeds "
1821 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1822 this->orig_name);
1823 return false;
1824 }
1825
1826 return true;
1827}
1828
1829
Paul Berry25ed3be2012-12-04 10:34:45 -08001830unsigned
1831tfeedback_decl::get_num_outputs() const
Paul Berry871ddb92011-11-05 11:17:32 -07001832{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001833 if (!this->is_varying()) {
Paul Berry25ed3be2012-12-04 10:34:45 -08001834 return 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001835 }
Paul Berryd3150eb2012-01-09 11:25:14 -08001836
Paul Berrydf877222012-12-09 20:59:26 -08001837 return (this->num_components() + this->location_frac + 3)/4;
Christoph Bumillerd540af52012-01-20 13:24:46 +01001838}
1839
1840
1841/**
1842 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1843 *
1844 * If an error occurs, the error is reported through linker_error() and false
1845 * is returned.
1846 */
1847bool
1848tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1849 struct gl_transform_feedback_info *info,
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001850 unsigned buffer, const unsigned max_outputs) const
Christoph Bumillerd540af52012-01-20 13:24:46 +01001851{
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001852 assert(!this->next_buffer_separator);
1853
1854 /* Handle gl_SkipComponents. */
1855 if (this->skip_components) {
1856 info->BufferStride[buffer] += this->skip_components;
1857 return true;
1858 }
1859
Paul Berryd3150eb2012-01-09 11:25:14 -08001860 /* From GL_EXT_transform_feedback:
1861 * A program will fail to link if:
1862 *
1863 * * the total number of components to capture is greater than
1864 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1865 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1866 */
1867 if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1868 info->BufferStride[buffer] + this->num_components() >
1869 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1870 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1871 "limit has been exceeded.");
1872 return false;
1873 }
1874
Paul Berrydf877222012-12-09 20:59:26 -08001875 unsigned location = this->location;
1876 unsigned location_frac = this->location_frac;
1877 unsigned num_components = this->num_components();
1878 while (num_components > 0) {
1879 unsigned output_size = MIN2(num_components, 4 - location_frac);
1880 assert(info->NumOutputs < max_outputs);
1881 info->Outputs[info->NumOutputs].ComponentOffset = location_frac;
1882 info->Outputs[info->NumOutputs].OutputRegister = location;
1883 info->Outputs[info->NumOutputs].NumComponents = output_size;
1884 info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1885 info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
1886 ++info->NumOutputs;
1887 info->BufferStride[buffer] += output_size;
1888 num_components -= output_size;
1889 location++;
1890 location_frac = 0;
Paul Berry871ddb92011-11-05 11:17:32 -07001891 }
Eric Anholt9d36c962012-01-02 17:08:13 -08001892
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001893 info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1894 info->Varyings[info->NumVarying].Type = this->type;
1895 info->Varyings[info->NumVarying].Size = this->size;
Eric Anholt9d36c962012-01-02 17:08:13 -08001896 info->NumVarying++;
1897
Paul Berry871ddb92011-11-05 11:17:32 -07001898 return true;
1899}
1900
1901
Paul Berry25ed3be2012-12-04 10:34:45 -08001902ir_variable *
1903tfeedback_decl::find_output_var(gl_shader_program *prog,
1904 gl_shader *producer) const
1905{
1906 const char *name = this->is_clip_distance_mesa
1907 ? "gl_ClipDistanceMESA" : this->var_name;
1908 ir_variable *var = producer->symbols->get_variable(name);
1909 if (var && var->mode == ir_var_out)
1910 return var;
1911
1912 /* From GL_EXT_transform_feedback:
1913 * A program will fail to link if:
1914 *
1915 * * any variable name specified in the <varyings> array is not
1916 * declared as an output in the geometry shader (if present) or
1917 * the vertex shader (if no geometry shader is present);
1918 */
1919 linker_error(prog, "Transform feedback varying %s undeclared.",
1920 this->orig_name);
1921 return NULL;
1922}
1923
1924
Paul Berry871ddb92011-11-05 11:17:32 -07001925/**
1926 * Parse all the transform feedback declarations that were passed to
1927 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1928 *
1929 * If an error occurs, the error is reported through linker_error() and false
1930 * is returned.
1931 */
1932static bool
Paul Berry456279b2011-12-26 19:39:25 -08001933parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1934 const void *mem_ctx, unsigned num_names,
1935 char **varying_names, tfeedback_decl *decls)
Paul Berry871ddb92011-11-05 11:17:32 -07001936{
1937 for (unsigned i = 0; i < num_names; ++i) {
Paul Berry456279b2011-12-26 19:39:25 -08001938 if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
Paul Berry871ddb92011-11-05 11:17:32 -07001939 return false;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001940
1941 if (!decls[i].is_varying())
1942 continue;
1943
Paul Berry871ddb92011-11-05 11:17:32 -07001944 /* From GL_EXT_transform_feedback:
1945 * A program will fail to link if:
1946 *
1947 * * any two entries in the <varyings> array specify the same varying
1948 * variable;
1949 *
1950 * We interpret this to mean "any two entries in the <varyings> array
1951 * specify the same varying variable and array index", since transform
1952 * feedback of arrays would be useless otherwise.
1953 */
1954 for (unsigned j = 0; j < i; ++j) {
Marek Olšák21cb5ed2011-12-18 02:39:34 +01001955 if (!decls[j].is_varying())
1956 continue;
1957
Paul Berry871ddb92011-11-05 11:17:32 -07001958 if (tfeedback_decl::is_same(decls[i], decls[j])) {
1959 linker_error(prog, "Transform feedback varying %s specified "
1960 "more than once.", varying_names[i]);
1961 return false;
1962 }
1963 }
1964 }
1965 return true;
1966}
1967
1968
1969/**
Paul Berryeb989e32012-12-04 15:55:59 -08001970 * Data structure recording the relationship between outputs of one shader
1971 * stage (the "producer") and inputs of another (the "consumer").
1972 */
1973class varying_matches
1974{
1975public:
Paul Berryca7e8912012-12-05 14:37:19 -08001976 varying_matches(bool disable_varying_packing);
Paul Berryeb989e32012-12-04 15:55:59 -08001977 ~varying_matches();
1978 void record(ir_variable *producer_var, ir_variable *consumer_var);
1979 unsigned assign_locations();
1980 void store_locations(unsigned producer_base, unsigned consumer_base) const;
1981
1982private:
1983 /**
Paul Berryca7e8912012-12-05 14:37:19 -08001984 * If true, this driver disables varying packing, so all varyings need to
1985 * be aligned on slot boundaries, and take up a number of slots equal to
1986 * their number of matrix columns times their array size.
1987 */
1988 const bool disable_varying_packing;
1989
1990 /**
Paul Berryf3993102012-12-05 10:19:19 -08001991 * Enum representing the order in which varyings are packed within a
1992 * packing class.
1993 *
1994 * Currently we pack vec4's first, then vec2's, then scalar values, then
1995 * vec3's. This order ensures that the only vectors that are at risk of
1996 * having to be "double parked" (split between two adjacent varying slots)
1997 * are the vec3's.
1998 */
1999 enum packing_order_enum {
2000 PACKING_ORDER_VEC4,
2001 PACKING_ORDER_VEC2,
2002 PACKING_ORDER_SCALAR,
2003 PACKING_ORDER_VEC3,
2004 };
2005
2006 static unsigned compute_packing_class(ir_variable *var);
2007 static packing_order_enum compute_packing_order(ir_variable *var);
2008 static int match_comparator(const void *x_generic, const void *y_generic);
2009
2010 /**
Paul Berryeb989e32012-12-04 15:55:59 -08002011 * Structure recording the relationship between a single producer output
2012 * and a single consumer input.
2013 */
2014 struct match {
2015 /**
Paul Berryf3993102012-12-05 10:19:19 -08002016 * Packing class for this varying, computed by compute_packing_class().
2017 */
2018 unsigned packing_class;
2019
2020 /**
2021 * Packing order for this varying, computed by compute_packing_order().
2022 */
2023 packing_order_enum packing_order;
Paul Berryca7e8912012-12-05 14:37:19 -08002024 unsigned num_components;
Paul Berryf3993102012-12-05 10:19:19 -08002025
2026 /**
Paul Berryeb989e32012-12-04 15:55:59 -08002027 * The output variable in the producer stage.
2028 */
2029 ir_variable *producer_var;
2030
2031 /**
2032 * The input variable in the consumer stage.
2033 */
2034 ir_variable *consumer_var;
2035
2036 /**
2037 * The location which has been assigned for this varying. This is
2038 * expressed in multiples of a float, with the first generic varying
2039 * (i.e. the one referred to by VERT_RESULT_VAR0 or FRAG_ATTRIB_VAR0)
2040 * represented by the value 0.
2041 */
2042 unsigned generic_location;
2043 } *matches;
2044
2045 /**
2046 * The number of elements in the \c matches array that are currently in
2047 * use.
2048 */
2049 unsigned num_matches;
2050
2051 /**
2052 * The number of elements that were set aside for the \c matches array when
2053 * it was allocated.
2054 */
2055 unsigned matches_capacity;
2056};
2057
2058
Paul Berryca7e8912012-12-05 14:37:19 -08002059varying_matches::varying_matches(bool disable_varying_packing)
2060 : disable_varying_packing(disable_varying_packing)
Paul Berryeb989e32012-12-04 15:55:59 -08002061{
2062 /* Note: this initial capacity is rather arbitrarily chosen to be large
2063 * enough for many cases without wasting an unreasonable amount of space.
2064 * varying_matches::record() will resize the array if there are more than
2065 * this number of varyings.
2066 */
2067 this->matches_capacity = 8;
2068 this->matches = (match *)
2069 malloc(sizeof(*this->matches) * this->matches_capacity);
2070 this->num_matches = 0;
2071}
2072
2073
2074varying_matches::~varying_matches()
2075{
2076 free(this->matches);
2077}
2078
2079
2080/**
2081 * Record the given producer/consumer variable pair in the list of variables
2082 * that should later be assigned locations.
Paul Berry871ddb92011-11-05 11:17:32 -07002083 *
Paul Berryeb989e32012-12-04 15:55:59 -08002084 * It is permissible for \c consumer_var to be NULL (this happens if a
2085 * variable is output by the producer and consumed by transform feedback, but
2086 * not consumed by the consumer).
Paul Berry871ddb92011-11-05 11:17:32 -07002087 *
Paul Berryeb989e32012-12-04 15:55:59 -08002088 * If \c producer_var has already been paired up with a consumer_var, or
2089 * producer_var is part of fixed pipeline functionality (and hence already has
2090 * a location assigned), this function has no effect.
Paul Berry871ddb92011-11-05 11:17:32 -07002091 */
2092void
Paul Berryeb989e32012-12-04 15:55:59 -08002093varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
Paul Berry871ddb92011-11-05 11:17:32 -07002094{
Paul Berryeb989e32012-12-04 15:55:59 -08002095 if (!producer_var->is_unmatched_generic_inout) {
2096 /* Either a location already exists for this variable (since it is part
2097 * of fixed functionality), or it has already been recorded as part of a
2098 * previous match.
2099 */
Paul Berry871ddb92011-11-05 11:17:32 -07002100 return;
2101 }
2102
Paul Berryeb989e32012-12-04 15:55:59 -08002103 if (this->num_matches == this->matches_capacity) {
2104 this->matches_capacity *= 2;
2105 this->matches = (match *)
2106 realloc(this->matches,
2107 sizeof(*this->matches) * this->matches_capacity);
2108 }
Paul Berryf3993102012-12-05 10:19:19 -08002109 this->matches[this->num_matches].packing_class
2110 = this->compute_packing_class(producer_var);
2111 this->matches[this->num_matches].packing_order
2112 = this->compute_packing_order(producer_var);
Paul Berryca7e8912012-12-05 14:37:19 -08002113 if (this->disable_varying_packing) {
2114 unsigned slots = producer_var->type->is_array()
2115 ? (producer_var->type->length
2116 * producer_var->type->fields.array->matrix_columns)
2117 : producer_var->type->matrix_columns;
2118 this->matches[this->num_matches].num_components = 4 * slots;
2119 } else {
2120 this->matches[this->num_matches].num_components
2121 = producer_var->type->component_slots();
2122 }
Paul Berryeb989e32012-12-04 15:55:59 -08002123 this->matches[this->num_matches].producer_var = producer_var;
2124 this->matches[this->num_matches].consumer_var = consumer_var;
2125 this->num_matches++;
2126 producer_var->is_unmatched_generic_inout = 0;
2127 if (consumer_var)
2128 consumer_var->is_unmatched_generic_inout = 0;
2129}
2130
2131
2132/**
2133 * Choose locations for all of the variable matches that were previously
2134 * passed to varying_matches::record().
2135 */
2136unsigned
2137varying_matches::assign_locations()
2138{
Paul Berryf3993102012-12-05 10:19:19 -08002139 /* Sort varying matches into an order that makes them easy to pack. */
2140 qsort(this->matches, this->num_matches, sizeof(*this->matches),
2141 &varying_matches::match_comparator);
2142
Paul Berryeb989e32012-12-04 15:55:59 -08002143 unsigned generic_location = 0;
2144
2145 for (unsigned i = 0; i < this->num_matches; i++) {
Paul Berryca7e8912012-12-05 14:37:19 -08002146 /* Advance to the next slot if this varying has a different packing
2147 * class than the previous one, and we're not already on a slot
2148 * boundary.
2149 */
2150 if (i > 0 &&
2151 this->matches[i - 1].packing_class
2152 != this->matches[i].packing_class) {
2153 generic_location = ALIGN(generic_location, 4);
2154 }
2155
Paul Berryeb989e32012-12-04 15:55:59 -08002156 this->matches[i].generic_location = generic_location;
2157
Paul Berryca7e8912012-12-05 14:37:19 -08002158 generic_location += this->matches[i].num_components;
Paul Berry871ddb92011-11-05 11:17:32 -07002159 }
2160
Paul Berryeb989e32012-12-04 15:55:59 -08002161 return (generic_location + 3) / 4;
2162}
Paul Berry871ddb92011-11-05 11:17:32 -07002163
Paul Berry871ddb92011-11-05 11:17:32 -07002164
Paul Berryeb989e32012-12-04 15:55:59 -08002165/**
2166 * Update the producer and consumer shaders to reflect the locations
2167 * assignments that were made by varying_matches::assign_locations().
2168 */
2169void
2170varying_matches::store_locations(unsigned producer_base,
2171 unsigned consumer_base) const
2172{
2173 for (unsigned i = 0; i < this->num_matches; i++) {
2174 ir_variable *producer_var = this->matches[i].producer_var;
2175 ir_variable *consumer_var = this->matches[i].consumer_var;
2176 unsigned generic_location = this->matches[i].generic_location;
2177 unsigned slot = generic_location / 4;
2178 unsigned offset = generic_location % 4;
Paul Berry871ddb92011-11-05 11:17:32 -07002179
Paul Berryeb989e32012-12-04 15:55:59 -08002180 producer_var->location = producer_base + slot;
2181 producer_var->location_frac = offset;
2182 if (consumer_var) {
2183 assert(consumer_var->location == -1);
2184 consumer_var->location = consumer_base + slot;
2185 consumer_var->location_frac = offset;
2186 }
Paul Berry871ddb92011-11-05 11:17:32 -07002187 }
2188}
2189
2190
2191/**
Paul Berryf3993102012-12-05 10:19:19 -08002192 * Compute the "packing class" of the given varying. This is an unsigned
2193 * integer with the property that two variables in the same packing class can
2194 * be safely backed into the same vec4.
2195 */
2196unsigned
2197varying_matches::compute_packing_class(ir_variable *var)
2198{
2199 /* In this initial implementation we conservatively assume that variables
2200 * can only be packed if their base type (float/int/uint/bool) matches and
2201 * their interpolation and centroid qualifiers match.
2202 *
2203 * TODO: relax these restrictions when the driver back-end permits.
2204 */
2205 unsigned packing_class = var->centroid ? 1 : 0;
2206 packing_class *= 4;
2207 packing_class += var->interpolation;
2208 packing_class *= GLSL_TYPE_ERROR;
2209 packing_class += var->type->get_scalar_type()->base_type;
2210 return packing_class;
2211}
2212
2213
2214/**
2215 * Compute the "packing order" of the given varying. This is a sort key we
2216 * use to determine when to attempt to pack the given varying relative to
2217 * other varyings in the same packing class.
2218 */
2219varying_matches::packing_order_enum
2220varying_matches::compute_packing_order(ir_variable *var)
2221{
2222 const glsl_type *element_type = var->type;
2223
2224 /* FINISHME: Support for "varying" records in GLSL 1.50. */
2225 while (element_type->base_type == GLSL_TYPE_ARRAY) {
2226 element_type = element_type->fields.array;
2227 }
2228
2229 switch (element_type->vector_elements) {
2230 case 1: return PACKING_ORDER_SCALAR;
2231 case 2: return PACKING_ORDER_VEC2;
2232 case 3: return PACKING_ORDER_VEC3;
2233 case 4: return PACKING_ORDER_VEC4;
2234 default:
2235 assert(!"Unexpected value of vector_elements");
2236 return PACKING_ORDER_VEC4;
2237 }
2238}
2239
2240
2241/**
2242 * Comparison function passed to qsort() to sort varyings by packing_class and
2243 * then by packing_order.
2244 */
2245int
2246varying_matches::match_comparator(const void *x_generic, const void *y_generic)
2247{
2248 const match *x = (const match *) x_generic;
2249 const match *y = (const match *) y_generic;
2250
2251 if (x->packing_class != y->packing_class)
2252 return x->packing_class - y->packing_class;
2253 return x->packing_order - y->packing_order;
2254}
2255
2256
2257/**
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002258 * Is the given variable a varying variable to be counted against the
2259 * limit in ctx->Const.MaxVarying?
2260 * This includes variables such as texcoords, colors and generic
2261 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2262 */
2263static bool
2264is_varying_var(GLenum shaderType, const ir_variable *var)
2265{
2266 /* Only fragment shaders will take a varying variable as an input */
2267 if (shaderType == GL_FRAGMENT_SHADER &&
Eric Anholt94e82b22012-11-13 14:40:22 -08002268 var->mode == ir_var_in) {
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002269 switch (var->location) {
2270 case FRAG_ATTRIB_WPOS:
2271 case FRAG_ATTRIB_FACE:
2272 case FRAG_ATTRIB_PNTC:
2273 return false;
2274 default:
2275 return true;
2276 }
2277 }
2278 return false;
2279}
2280
2281
2282/**
Paul Berry871ddb92011-11-05 11:17:32 -07002283 * Assign locations for all variables that are produced in one pipeline stage
2284 * (the "producer") and consumed in the next stage (the "consumer").
2285 *
2286 * Variables produced by the producer may also be consumed by transform
2287 * feedback.
2288 *
2289 * \param num_tfeedback_decls is the number of declarations indicating
2290 * variables that may be consumed by transform feedback.
2291 *
2292 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2293 * representing the result of parsing the strings passed to
2294 * glTransformFeedbackVaryings(). assign_location() will be called for
2295 * each of these objects that matches one of the outputs of the
2296 * producer.
2297 *
2298 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2299 * be NULL. In this case, varying locations are assigned solely based on the
2300 * requirements of transform feedback.
2301 */
Ian Romanickde773242011-06-09 13:31:32 -07002302bool
2303assign_varying_locations(struct gl_context *ctx,
2304 struct gl_shader_program *prog,
Paul Berry871ddb92011-11-05 11:17:32 -07002305 gl_shader *producer, gl_shader *consumer,
2306 unsigned num_tfeedback_decls,
2307 tfeedback_decl *tfeedback_decls)
Ian Romanick0e59b262010-06-23 11:23:01 -07002308{
2309 /* FINISHME: Set dynamically when geometry shader support is added. */
Paul Berryeb989e32012-12-04 15:55:59 -08002310 const unsigned producer_base = VERT_RESULT_VAR0;
2311 const unsigned consumer_base = FRAG_ATTRIB_VAR0;
Paul Berryca7e8912012-12-05 14:37:19 -08002312 varying_matches matches(ctx->Const.DisableVaryingPacking);
Ian Romanick0e59b262010-06-23 11:23:01 -07002313
2314 /* Operate in a total of three passes.
2315 *
2316 * 1. Assign locations for any matching inputs and outputs.
2317 *
2318 * 2. Mark output variables in the producer that do not have locations as
2319 * not being outputs. This lets the optimizer eliminate them.
2320 *
2321 * 3. Mark input variables in the consumer that do not have locations as
2322 * not being inputs. This lets the optimizer eliminate them.
2323 */
2324
Eric Anholt16b68b12010-06-30 11:05:43 -07002325 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -07002326 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2327
Paul Berry871ddb92011-11-05 11:17:32 -07002328 if ((output_var == NULL) || (output_var->mode != ir_var_out))
Ian Romanick0e59b262010-06-23 11:23:01 -07002329 continue;
2330
Paul Berry871ddb92011-11-05 11:17:32 -07002331 ir_variable *input_var =
2332 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002333
Paul Berry871ddb92011-11-05 11:17:32 -07002334 if (input_var && input_var->mode != ir_var_in)
2335 input_var = NULL;
Ian Romanick0e59b262010-06-23 11:23:01 -07002336
Paul Berry871ddb92011-11-05 11:17:32 -07002337 if (input_var) {
Paul Berryeb989e32012-12-04 15:55:59 -08002338 matches.record(output_var, input_var);
Paul Berry871ddb92011-11-05 11:17:32 -07002339 }
Paul Berry25ed3be2012-12-04 10:34:45 -08002340 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002341
Paul Berry25ed3be2012-12-04 10:34:45 -08002342 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2343 if (!tfeedback_decls[i].is_varying())
2344 continue;
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002345
Paul Berry25ed3be2012-12-04 10:34:45 -08002346 ir_variable *output_var
2347 = tfeedback_decls[i].find_output_var(prog, producer);
2348
2349 if (output_var == NULL)
2350 return false;
2351
2352 if (output_var->is_unmatched_generic_inout) {
Paul Berryeb989e32012-12-04 15:55:59 -08002353 matches.record(output_var, NULL);
Ian Romanickdf869d92010-08-30 15:37:44 -07002354 }
Paul Berryeb989e32012-12-04 15:55:59 -08002355 }
2356
Paul Berrydf877222012-12-09 20:59:26 -08002357 const unsigned slots_used = matches.assign_locations();
Paul Berryeb989e32012-12-04 15:55:59 -08002358 matches.store_locations(producer_base, consumer_base);
2359
2360 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2361 if (!tfeedback_decls[i].is_varying())
2362 continue;
2363
2364 ir_variable *output_var
2365 = tfeedback_decls[i].find_output_var(prog, producer);
Paul Berry25ed3be2012-12-04 10:34:45 -08002366
2367 if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2368 return false;
Ian Romanick0e59b262010-06-23 11:23:01 -07002369 }
2370
Paul Berrydf877222012-12-09 20:59:26 -08002371 if (ctx->Const.DisableVaryingPacking) {
2372 /* Transform feedback code assumes varyings are packed, so if the driver
2373 * has disabled varying packing, make sure it does not support transform
2374 * feedback.
2375 */
2376 assert(!ctx->Extensions.EXT_transform_feedback);
2377 } else {
2378 lower_packed_varyings(ctx, producer_base, slots_used, ir_var_out,
2379 producer);
2380 if (consumer) {
2381 lower_packed_varyings(ctx, consumer_base, slots_used, ir_var_in,
2382 consumer);
2383 }
2384 }
2385
Ian Romanickde773242011-06-09 13:31:32 -07002386 unsigned varying_vectors = 0;
2387
Paul Berry871ddb92011-11-05 11:17:32 -07002388 if (consumer) {
2389 foreach_list(node, consumer->ir) {
2390 ir_variable *const var = ((ir_instruction *) node)->as_variable();
Ian Romanick0e59b262010-06-23 11:23:01 -07002391
Paul Berry871ddb92011-11-05 11:17:32 -07002392 if ((var == NULL) || (var->mode != ir_var_in))
2393 continue;
Ian Romanick0e59b262010-06-23 11:23:01 -07002394
Paul Berry3c9c17d2012-12-04 15:17:01 -08002395 if (var->is_unmatched_generic_inout) {
Paul Berry871ddb92011-11-05 11:17:32 -07002396 if (prog->Version <= 120) {
2397 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2398 *
2399 * Only those varying variables used (i.e. read) in
2400 * the fragment shader executable must be written to
2401 * by the vertex shader executable; declaring
2402 * superfluous varying variables in a vertex shader is
2403 * permissible.
2404 *
2405 * We interpret this text as meaning that the VS must
2406 * write the variable for the FS to read it. See
2407 * "glsl1-varying read but not written" in piglit.
2408 */
Eric Anholtb7062832010-07-28 13:52:23 -07002409
Paul Berry871ddb92011-11-05 11:17:32 -07002410 linker_error(prog, "fragment shader varying %s not written "
2411 "by vertex shader\n.", var->name);
2412 }
Eric Anholtb7062832010-07-28 13:52:23 -07002413
Paul Berry871ddb92011-11-05 11:17:32 -07002414 /* An 'in' variable is only really a shader input if its
2415 * value is written by the previous stage.
2416 */
2417 var->mode = ir_var_auto;
Brian Paul8fb1e4a2012-06-26 13:06:47 -06002418 } else if (is_varying_var(consumer->Type, var)) {
Paul Berry871ddb92011-11-05 11:17:32 -07002419 /* The packing rules are used for vertex shader inputs are also
2420 * used for fragment shader inputs.
2421 */
2422 varying_vectors += count_attribute_slots(var->type);
2423 }
Eric Anholtb7062832010-07-28 13:52:23 -07002424 }
Ian Romanick0e59b262010-06-23 11:23:01 -07002425 }
Ian Romanickde773242011-06-09 13:31:32 -07002426
Paul Berry15ba2a52012-08-02 17:51:02 -07002427 if (ctx->API == API_OPENGLES2 || prog->IsES) {
Ian Romanickde773242011-06-09 13:31:32 -07002428 if (varying_vectors > ctx->Const.MaxVarying) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002429 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2430 linker_warning(prog, "shader uses too many varying vectors "
2431 "(%u > %u), but the driver will try to optimize "
2432 "them out; this is non-portable out-of-spec "
2433 "behavior\n",
2434 varying_vectors, ctx->Const.MaxVarying);
2435 } else {
2436 linker_error(prog, "shader uses too many varying vectors "
2437 "(%u > %u)\n",
2438 varying_vectors, ctx->Const.MaxVarying);
2439 return false;
2440 }
Ian Romanickde773242011-06-09 13:31:32 -07002441 }
2442 } else {
2443 const unsigned float_components = varying_vectors * 4;
2444 if (float_components > ctx->Const.MaxVarying * 4) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002445 if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2446 linker_warning(prog, "shader uses too many varying components "
2447 "(%u > %u), but the driver will try to optimize "
2448 "them out; this is non-portable out-of-spec "
2449 "behavior\n",
2450 float_components, ctx->Const.MaxVarying * 4);
2451 } else {
2452 linker_error(prog, "shader uses too many varying components "
2453 "(%u > %u)\n",
2454 float_components, ctx->Const.MaxVarying * 4);
2455 return false;
2456 }
Ian Romanickde773242011-06-09 13:31:32 -07002457 }
2458 }
2459
2460 return true;
Ian Romanick0e59b262010-06-23 11:23:01 -07002461}
2462
2463
Paul Berry871ddb92011-11-05 11:17:32 -07002464/**
2465 * Store transform feedback location assignments into
2466 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2467 *
2468 * If an error occurs, the error is reported through linker_error() and false
2469 * is returned.
2470 */
2471static bool
2472store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2473 unsigned num_tfeedback_decls,
2474 tfeedback_decl *tfeedback_decls)
2475{
Paul Berryebfad9f2011-12-29 15:55:01 -08002476 bool separate_attribs_mode =
2477 prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
Eric Anholt9d36c962012-01-02 17:08:13 -08002478
2479 ralloc_free(prog->LinkedTransformFeedback.Varyings);
Christoph Bumillerd540af52012-01-20 13:24:46 +01002480 ralloc_free(prog->LinkedTransformFeedback.Outputs);
Eric Anholt9d36c962012-01-02 17:08:13 -08002481
2482 memset(&prog->LinkedTransformFeedback, 0,
2483 sizeof(prog->LinkedTransformFeedback));
2484
2485 prog->LinkedTransformFeedback.Varyings =
Eric Anholt5a0f3952012-01-12 13:10:26 -08002486 rzalloc_array(prog,
Eric Anholt9d36c962012-01-02 17:08:13 -08002487 struct gl_transform_feedback_varying_info,
2488 num_tfeedback_decls);
2489
Christoph Bumillerd540af52012-01-20 13:24:46 +01002490 unsigned num_outputs = 0;
2491 for (unsigned i = 0; i < num_tfeedback_decls; ++i)
Paul Berry25ed3be2012-12-04 10:34:45 -08002492 num_outputs += tfeedback_decls[i].get_num_outputs();
Christoph Bumillerd540af52012-01-20 13:24:46 +01002493
2494 prog->LinkedTransformFeedback.Outputs =
2495 rzalloc_array(prog,
2496 struct gl_transform_feedback_output,
2497 num_outputs);
2498
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002499 unsigned num_buffers = 0;
2500
2501 if (separate_attribs_mode) {
2502 /* GL_SEPARATE_ATTRIBS */
2503 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2504 if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2505 num_buffers, num_outputs))
2506 return false;
2507
2508 num_buffers++;
2509 }
Paul Berry871ddb92011-11-05 11:17:32 -07002510 }
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002511 else {
2512 /* GL_INVERLEAVED_ATTRIBS */
2513 for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2514 if (tfeedback_decls[i].is_next_buffer_separator()) {
2515 num_buffers++;
2516 continue;
2517 }
2518
2519 if (!tfeedback_decls[i].store(ctx, prog,
2520 &prog->LinkedTransformFeedback,
2521 num_buffers, num_outputs))
2522 return false;
2523 }
2524 num_buffers++;
2525 }
2526
Christoph Bumillerd540af52012-01-20 13:24:46 +01002527 assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
Paul Berry871ddb92011-11-05 11:17:32 -07002528
Marek Olšák21cb5ed2011-12-18 02:39:34 +01002529 prog->LinkedTransformFeedback.NumBuffers = num_buffers;
Paul Berry871ddb92011-11-05 11:17:32 -07002530 return true;
2531}
2532
Ian Romanick92f81592011-11-08 12:37:19 -08002533/**
Marek Olšákec174a42011-11-18 15:00:10 +01002534 * Store the gl_FragDepth layout in the gl_shader_program struct.
2535 */
2536static void
2537store_fragdepth_layout(struct gl_shader_program *prog)
2538{
2539 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2540 return;
2541 }
2542
2543 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2544
2545 /* We don't look up the gl_FragDepth symbol directly because if
2546 * gl_FragDepth is not used in the shader, it's removed from the IR.
2547 * However, the symbol won't be removed from the symbol table.
2548 *
2549 * We're only interested in the cases where the variable is NOT removed
2550 * from the IR.
2551 */
2552 foreach_list(node, ir) {
2553 ir_variable *const var = ((ir_instruction *) node)->as_variable();
2554
2555 if (var == NULL || var->mode != ir_var_out) {
2556 continue;
2557 }
2558
2559 if (strcmp(var->name, "gl_FragDepth") == 0) {
2560 switch (var->depth_layout) {
2561 case ir_depth_layout_none:
2562 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2563 return;
2564 case ir_depth_layout_any:
2565 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2566 return;
2567 case ir_depth_layout_greater:
2568 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2569 return;
2570 case ir_depth_layout_less:
2571 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2572 return;
2573 case ir_depth_layout_unchanged:
2574 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2575 return;
2576 default:
2577 assert(0);
2578 return;
2579 }
2580 }
2581 }
2582}
2583
2584/**
Ian Romanick92f81592011-11-08 12:37:19 -08002585 * Validate the resources used by a program versus the implementation limits
2586 */
2587static bool
2588check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2589{
2590 static const char *const shader_names[MESA_SHADER_TYPES] = {
2591 "vertex", "fragment", "geometry"
2592 };
2593
2594 const unsigned max_samplers[MESA_SHADER_TYPES] = {
2595 ctx->Const.MaxVertexTextureImageUnits,
2596 ctx->Const.MaxTextureImageUnits,
2597 ctx->Const.MaxGeometryTextureImageUnits
2598 };
2599
2600 const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2601 ctx->Const.VertexProgram.MaxUniformComponents,
2602 ctx->Const.FragmentProgram.MaxUniformComponents,
2603 0 /* FINISHME: Geometry shaders. */
2604 };
2605
Eric Anholt877a8972012-06-25 12:47:01 -07002606 const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
2607 ctx->Const.VertexProgram.MaxUniformBlocks,
2608 ctx->Const.FragmentProgram.MaxUniformBlocks,
2609 ctx->Const.GeometryProgram.MaxUniformBlocks,
2610 };
2611
Ian Romanick92f81592011-11-08 12:37:19 -08002612 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2613 struct gl_shader *sh = prog->_LinkedShaders[i];
2614
2615 if (sh == NULL)
2616 continue;
2617
2618 if (sh->num_samplers > max_samplers[i]) {
2619 linker_error(prog, "Too many %s shader texture samplers",
2620 shader_names[i]);
2621 }
2622
2623 if (sh->num_uniform_components > max_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002624 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2625 linker_warning(prog, "Too many %s shader uniform components, "
2626 "but the driver will try to optimize them out; "
2627 "this is non-portable out-of-spec behavior\n",
2628 shader_names[i]);
2629 } else {
2630 linker_error(prog, "Too many %s shader uniform components",
2631 shader_names[i]);
2632 }
Ian Romanick92f81592011-11-08 12:37:19 -08002633 }
2634 }
2635
Eric Anholt877a8972012-06-25 12:47:01 -07002636 unsigned blocks[MESA_SHADER_TYPES] = {0};
2637 unsigned total_uniform_blocks = 0;
2638
2639 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
2640 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
2641 if (prog->UniformBlockStageIndex[j][i] != -1) {
2642 blocks[j]++;
2643 total_uniform_blocks++;
2644 }
2645 }
2646
2647 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
2648 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
2649 prog->NumUniformBlocks,
2650 ctx->Const.MaxCombinedUniformBlocks);
2651 } else {
2652 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2653 if (blocks[i] > max_uniform_blocks[i]) {
2654 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
2655 shader_names[i],
2656 blocks[i],
2657 max_uniform_blocks[i]);
2658 break;
2659 }
2660 }
2661 }
2662 }
2663
Ian Romanick92f81592011-11-08 12:37:19 -08002664 return prog->LinkStatus;
2665}
Paul Berry871ddb92011-11-05 11:17:32 -07002666
Ian Romanick0e59b262010-06-23 11:23:01 -07002667void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002668link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07002669{
Paul Berry871ddb92011-11-05 11:17:32 -07002670 tfeedback_decl *tfeedback_decls = NULL;
2671 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2672
Kenneth Graunked3073f52011-01-21 14:32:31 -08002673 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002674
Ian Romanick832dfa52010-06-17 15:04:20 -07002675 prog->LinkStatus = false;
2676 prog->Validated = false;
2677 prog->_Used = false;
2678
Eric Anholtf609cf72012-04-27 13:52:56 -07002679 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08002680 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002681
Eric Anholtf609cf72012-04-27 13:52:56 -07002682 ralloc_free(prog->UniformBlocks);
2683 prog->UniformBlocks = NULL;
2684 prog->NumUniformBlocks = 0;
2685 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2686 ralloc_free(prog->UniformBlockStageIndex[i]);
2687 prog->UniformBlockStageIndex[i] = NULL;
2688 }
2689
Ian Romanick832dfa52010-06-17 15:04:20 -07002690 /* Separate the shaders into groups based on their type.
2691 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002692 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002693 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002694 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002695 unsigned num_frag_shaders = 0;
2696
Eric Anholt16b68b12010-06-30 11:05:43 -07002697 vert_shader_list = (struct gl_shader **)
2698 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002699 frag_shader_list = &vert_shader_list[prog->NumShaders];
2700
Ian Romanick25f51d32010-07-16 15:51:50 -07002701 unsigned min_version = UINT_MAX;
2702 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002703 const bool is_es_prog =
2704 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002705 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002706 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2707 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2708
Paul Berrya9f34dc2012-08-02 17:49:44 -07002709 if (prog->Shaders[i]->IsES != is_es_prog) {
2710 linker_error(prog, "all shaders must use same shading "
2711 "language version\n");
2712 goto done;
2713 }
2714
Ian Romanick832dfa52010-06-17 15:04:20 -07002715 switch (prog->Shaders[i]->Type) {
2716 case GL_VERTEX_SHADER:
2717 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2718 num_vert_shaders++;
2719 break;
2720 case GL_FRAGMENT_SHADER:
2721 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2722 num_frag_shaders++;
2723 break;
2724 case GL_GEOMETRY_SHADER:
2725 /* FINISHME: Support geometry shaders. */
2726 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2727 break;
2728 }
2729 }
2730
Ian Romanick25f51d32010-07-16 15:51:50 -07002731 /* Previous to GLSL version 1.30, different compilation units could mix and
2732 * match shading language versions. With GLSL 1.30 and later, the versions
2733 * of all shaders must match.
Paul Berrya9f34dc2012-08-02 17:49:44 -07002734 *
2735 * GLSL ES has never allowed mixing of shading language versions.
Ian Romanick25f51d32010-07-16 15:51:50 -07002736 */
Paul Berrya9f34dc2012-08-02 17:49:44 -07002737 if ((is_es_prog || max_version >= 130)
Kenneth Graunke5a81d052010-08-31 09:33:58 -07002738 && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002739 linker_error(prog, "all shaders must use same shading "
2740 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002741 goto done;
2742 }
2743
2744 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002745 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002746
Ian Romanick3322fba2010-10-14 13:28:42 -07002747 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2748 if (prog->_LinkedShaders[i] != NULL)
2749 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2750
2751 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002752 }
2753
Ian Romanickcd6764e2010-07-16 16:00:07 -07002754 /* Link all shaders for a particular stage and validate the result.
2755 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002756 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002757 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002758 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2759 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002760
2761 if (sh == NULL)
2762 goto done;
2763
2764 if (!validate_vertex_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002765 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002766
Ian Romanick3322fba2010-10-14 13:28:42 -07002767 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2768 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002769 }
2770
2771 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002772 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002773 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2774 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002775
2776 if (sh == NULL)
2777 goto done;
2778
2779 if (!validate_fragment_shader_executable(prog, sh))
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002780 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002781
Ian Romanick3322fba2010-10-14 13:28:42 -07002782 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2783 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002784 }
2785
Ian Romanick3ed850e2010-06-23 12:18:21 -07002786 /* Here begins the inter-stage linking phase. Some initial validation is
2787 * performed, then locations are assigned for uniforms, attributes, and
2788 * varyings.
2789 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -07002790 if (cross_validate_uniforms(prog)) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002791 unsigned prev;
2792
2793 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2794 if (prog->_LinkedShaders[prev] != NULL)
2795 break;
2796 }
2797
Bryan Cainf18a0862011-04-23 19:29:15 -05002798 /* Validate the inputs of each stage with the output of the preceding
Ian Romanick37101922010-06-18 19:02:10 -07002799 * stage.
2800 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002801 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2802 if (prog->_LinkedShaders[i] == NULL)
2803 continue;
2804
Ian Romanickf36460e2010-06-23 12:07:22 -07002805 if (!cross_validate_outputs_to_inputs(prog,
Ian Romanick3322fba2010-10-14 13:28:42 -07002806 prog->_LinkedShaders[prev],
Ian Romanickabee16e2010-06-21 16:16:05 -07002807 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -07002808 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002809
2810 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002811 }
2812
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002813 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -07002814 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002815
Eric Anholt3de13952012-05-04 13:08:46 -07002816 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2817 * it before optimization because we want most of the checks to get
2818 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002819 *
2820 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002821 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002822 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002823 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2824 if (sh) {
2825 lower_discard_flow(sh->ir);
2826 }
2827 }
2828
Eric Anholtf609cf72012-04-27 13:52:56 -07002829 if (!interstage_cross_validate_uniform_blocks(prog))
2830 goto done;
2831
Eric Anholt2f4fe152010-08-10 13:06:49 -07002832 /* Do common optimization before assigning storage for attributes,
2833 * uniforms, and varyings. Later optimization could possibly make
2834 * some of that unused.
2835 */
Ian Romanick3322fba2010-10-14 13:28:42 -07002836 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2837 if (prog->_LinkedShaders[i] == NULL)
2838 continue;
2839
Ian Romanick02c5ae12011-07-11 10:46:01 -07002840 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2841 if (!prog->LinkStatus)
2842 goto done;
2843
Paul Berry18392442012-12-04 11:11:02 -08002844 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2845 lower_clip_distance(prog->_LinkedShaders[i]);
2846 }
Paul Berryc06e3252011-08-11 20:58:21 -07002847
Brian Paul7feabfe2012-03-20 17:43:12 -06002848 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2849
2850 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002851 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002852 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002853
Paul Berry50895d42012-12-05 07:17:07 -08002854 /* Mark all generic shader inputs and outputs as unpaired. */
2855 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2856 link_invalidate_variable_locations(
2857 prog->_LinkedShaders[MESA_SHADER_VERTEX],
2858 VERT_ATTRIB_GENERIC0, VERT_RESULT_VAR0);
2859 }
2860 /* FINISHME: Geometry shaders not implemented yet */
2861 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2862 link_invalidate_variable_locations(
2863 prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2864 FRAG_ATTRIB_VAR0, FRAG_RESULT_DATA0);
2865 }
2866
Ian Romanickd32d4f72011-06-27 17:59:58 -07002867 /* FINISHME: The value of the max_attribute_index parameter is
2868 * FINISHME: implementation dependent based on the value of
2869 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2870 * FINISHME: at least 16, so hardcode 16 for now.
2871 */
2872 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002873 goto done;
2874 }
2875
Dave Airlie1256a5d2012-03-24 13:33:41 +00002876 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002877 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002878 }
2879
Ian Romanick3322fba2010-10-14 13:28:42 -07002880 unsigned prev;
2881 for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2882 if (prog->_LinkedShaders[prev] != NULL)
2883 break;
2884 }
2885
Paul Berry871ddb92011-11-05 11:17:32 -07002886 if (num_tfeedback_decls != 0) {
2887 /* From GL_EXT_transform_feedback:
2888 * A program will fail to link if:
2889 *
2890 * * the <count> specified by TransformFeedbackVaryingsEXT is
2891 * non-zero, but the program object has no vertex or geometry
2892 * shader;
2893 */
2894 if (prev >= MESA_SHADER_FRAGMENT) {
2895 linker_error(prog, "Transform feedback varyings specified, but "
2896 "no vertex or geometry shader is present.");
2897 goto done;
2898 }
2899
2900 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2901 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002902 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002903 prog->TransformFeedback.VaryingNames,
2904 tfeedback_decls))
2905 goto done;
2906 }
2907
Ian Romanick3322fba2010-10-14 13:28:42 -07002908 for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2909 if (prog->_LinkedShaders[i] == NULL)
2910 continue;
2911
Paul Berry871ddb92011-11-05 11:17:32 -07002912 if (!assign_varying_locations(
2913 ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2914 i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2915 tfeedback_decls))
Ian Romanickde773242011-06-09 13:31:32 -07002916 goto done;
Ian Romanickde773242011-06-09 13:31:32 -07002917
Ian Romanick3322fba2010-10-14 13:28:42 -07002918 prev = i;
2919 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002920
Paul Berry871ddb92011-11-05 11:17:32 -07002921 if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2922 /* There was no fragment shader, but we still have to assign varying
2923 * locations for use by transform feedback.
2924 */
2925 if (!assign_varying_locations(
2926 ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2927 tfeedback_decls))
2928 goto done;
2929 }
2930
2931 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2932 goto done;
2933
Ian Romanickcc90e622010-10-19 17:59:10 -07002934 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2935 demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2936 ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002937
2938 /* Eliminate code that is now dead due to unused vertex outputs being
2939 * demoted.
2940 */
2941 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2942 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002943 }
2944
2945 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2946 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2947
2948 demote_shader_inputs_and_outputs(sh, ir_var_in);
2949 demote_shader_inputs_and_outputs(sh, ir_var_inout);
2950 demote_shader_inputs_and_outputs(sh, ir_var_out);
Ian Romanick960d7222011-10-21 11:21:02 -07002951
2952 /* Eliminate code that is now dead due to unused geometry outputs being
2953 * demoted.
2954 */
2955 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2956 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002957 }
2958
2959 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2960 gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2961
2962 demote_shader_inputs_and_outputs(sh, ir_var_in);
Ian Romanick960d7222011-10-21 11:21:02 -07002963
2964 /* Eliminate code that is now dead due to unused fragment inputs being
2965 * demoted. This shouldn't actually do anything other than remove
2966 * declarations of the (now unused) global variables.
2967 */
2968 while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2969 ;
Ian Romanickcc90e622010-10-19 17:59:10 -07002970 }
2971
Ian Romanick960d7222011-10-21 11:21:02 -07002972 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002973 link_assign_uniform_locations(prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002974 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002975
Ian Romanick92f81592011-11-08 12:37:19 -08002976 if (!check_resources(ctx, prog))
2977 goto done;
2978
Ian Romanickce9171f2011-02-03 17:10:14 -08002979 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002980 * present in a linked program. By checking prog->IsES, we also
2981 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002982 */
Eric Anholt57f79782011-07-22 12:57:47 -07002983 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002984 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002985 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002986 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002987 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002988 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002989 }
2990 }
2991
Ian Romanick13e10e42010-06-21 12:03:24 -07002992 /* FINISHME: Assign fragment shader output locations. */
2993
Ian Romanick832dfa52010-06-17 15:04:20 -07002994done:
2995 free(vert_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002996
2997 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2998 if (prog->_LinkedShaders[i] == NULL)
2999 continue;
3000
3001 /* Retain any live IR, but trash the rest. */
3002 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07003003
3004 /* The symbol table in the linked shaders may contain references to
3005 * variables that were removed (e.g., unused uniforms). Since it may
3006 * contain junk, there is no possible valid use. Delete it and set the
3007 * pointer to NULL.
3008 */
3009 delete prog->_LinkedShaders[i]->symbols;
3010 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08003011 }
3012
Kenneth Graunked3073f52011-01-21 14:32:31 -08003013 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07003014}