blob: 91a7220fc8e1c7ab07c903960b373b04af2e08ac [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"
Eric Anholtfaf3dba2013-06-12 16:57:11 -070069#include "glsl_parser_extras.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070070#include "ir.h"
71#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030072#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070073#include "linker.h"
Paul Berry4b11b572012-12-17 14:20:35 -080074#include "link_varyings.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070075#include "ir_optimization.h"
Bryan Cain25480922013-02-15 09:46:50 -060076#include "ir_rvalue_visitor.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070077
Ian Romanick3322fba2010-10-14 13:28:42 -070078extern "C" {
79#include "main/shaderobj.h"
Eric Anholt6065a872013-06-12 18:12:40 -070080#include "main/enums.h"
Ian Romanick3322fba2010-10-14 13:28:42 -070081}
82
Bryan Cain25480922013-02-15 09:46:50 -060083void linker_error(gl_shader_program *, const char *, ...);
84
Eric Anholt10ef9492013-09-20 11:03:44 -070085namespace {
86
Ian Romanick832dfa52010-06-17 15:04:20 -070087/**
88 * Visitor that determines whether or not a variable is ever written.
89 */
90class find_assignment_visitor : public ir_hierarchical_visitor {
91public:
92 find_assignment_visitor(const char *name)
93 : name(name), found(false)
94 {
95 /* empty */
96 }
97
98 virtual ir_visitor_status visit_enter(ir_assignment *ir)
99 {
100 ir_variable *const var = ir->lhs->variable_referenced();
101
102 if (strcmp(name, var->name) == 0) {
103 found = true;
104 return visit_stop;
105 }
106
107 return visit_continue_with_parent;
108 }
109
Eric Anholt18a60232010-08-23 11:29:25 -0700110 virtual ir_visitor_status visit_enter(ir_call *ir)
111 {
Kenneth Graunke82065fa2011-09-20 18:08:11 -0700112 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
Eric Anholt18a60232010-08-23 11:29:25 -0700113 foreach_iter(exec_list_iterator, iter, *ir) {
114 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
115 ir_variable *sig_param = (ir_variable *)sig_iter.get();
116
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200117 if (sig_param->data.mode == ir_var_function_out ||
118 sig_param->data.mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700119 ir_variable *var = param_rval->variable_referenced();
120 if (var && strcmp(name, var->name) == 0) {
121 found = true;
122 return visit_stop;
123 }
124 }
125 sig_iter.next();
126 }
127
Kenneth Graunked884f602012-03-20 15:56:37 -0700128 if (ir->return_deref != NULL) {
129 ir_variable *const var = ir->return_deref->variable_referenced();
130
131 if (strcmp(name, var->name) == 0) {
132 found = true;
133 return visit_stop;
134 }
135 }
136
Eric Anholt18a60232010-08-23 11:29:25 -0700137 return visit_continue_with_parent;
138 }
139
Ian Romanick832dfa52010-06-17 15:04:20 -0700140 bool variable_found()
141 {
142 return found;
143 }
144
145private:
146 const char *name; /**< Find writes to a variable with this name. */
147 bool found; /**< Was a write to the variable found? */
148};
149
Ian Romanickc93b8f12010-06-17 15:20:22 -0700150
Ian Romanickc33e78f2010-08-13 12:30:41 -0700151/**
152 * Visitor that determines whether or not a variable is ever read.
153 */
154class find_deref_visitor : public ir_hierarchical_visitor {
155public:
156 find_deref_visitor(const char *name)
157 : name(name), found(false)
158 {
159 /* empty */
160 }
161
162 virtual ir_visitor_status visit(ir_dereference_variable *ir)
163 {
164 if (strcmp(this->name, ir->var->name) == 0) {
165 this->found = true;
166 return visit_stop;
167 }
168
169 return visit_continue;
170 }
171
172 bool variable_found() const
173 {
174 return this->found;
175 }
176
177private:
178 const char *name; /**< Find writes to a variable with this name. */
179 bool found; /**< Was a write to the variable found? */
180};
181
182
Paul Berry7cfefe62013-07-30 21:13:48 -0700183class geom_array_resize_visitor : public ir_hierarchical_visitor {
184public:
185 unsigned num_vertices;
186 gl_shader_program *prog;
187
188 geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
189 {
190 this->num_vertices = num_vertices;
191 this->prog = prog;
192 }
193
194 virtual ~geom_array_resize_visitor()
195 {
196 /* empty */
197 }
198
199 virtual ir_visitor_status visit(ir_variable *var)
200 {
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200201 if (!var->type->is_array() || var->data.mode != ir_var_shader_in)
Paul Berry7cfefe62013-07-30 21:13:48 -0700202 return visit_continue;
203
204 unsigned size = var->type->length;
205
206 /* Generate a link error if the shader has declared this array with an
207 * incorrect size.
208 */
209 if (size && size != this->num_vertices) {
210 linker_error(this->prog, "size of array %s declared as %u, "
211 "but number of input vertices is %u\n",
212 var->name, size, this->num_vertices);
213 return visit_continue;
214 }
215
216 /* Generate a link error if the shader attempts to access an input
217 * array using an index too large for its actual size assigned at link
218 * time.
219 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200220 if (var->data.max_array_access >= this->num_vertices) {
Paul Berry7cfefe62013-07-30 21:13:48 -0700221 linker_error(this->prog, "geometry shader accesses element %i of "
222 "%s, but only %i input vertices\n",
Tapani Pälli447bb902013-12-12 15:08:59 +0200223 var->data.max_array_access, var->name, this->num_vertices);
Paul Berry7cfefe62013-07-30 21:13:48 -0700224 return visit_continue;
225 }
226
227 var->type = glsl_type::get_array_instance(var->type->element_type(),
228 this->num_vertices);
Tapani Pälli447bb902013-12-12 15:08:59 +0200229 var->data.max_array_access = this->num_vertices - 1;
Paul Berry7cfefe62013-07-30 21:13:48 -0700230
231 return visit_continue;
232 }
233
234 /* Dereferences of input variables need to be updated so that their type
235 * matches the newly assigned type of the variable they are accessing. */
236 virtual ir_visitor_status visit(ir_dereference_variable *ir)
237 {
238 ir->type = ir->var->type;
239 return visit_continue;
240 }
241
242 /* Dereferences of 2D input arrays need to be updated so that their type
243 * matches the newly assigned type of the array they are accessing. */
244 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
245 {
246 const glsl_type *const vt = ir->array->type;
247 if (vt->is_array())
248 ir->type = vt->element_type();
249 return visit_continue;
250 }
251};
252
253
Paul Berry1a33e022013-08-18 20:59:37 -0700254/**
255 * Visitor that determines whether or not a shader uses ir_end_primitive.
256 */
257class find_end_primitive_visitor : public ir_hierarchical_visitor {
258public:
259 find_end_primitive_visitor()
260 : found(false)
261 {
262 /* empty */
263 }
264
265 virtual ir_visitor_status visit(ir_end_primitive *)
266 {
267 found = true;
268 return visit_stop;
269 }
270
271 bool end_primitive_found()
272 {
273 return found;
274 }
275
276private:
277 bool found;
278};
279
Eric Anholt10ef9492013-09-20 11:03:44 -0700280} /* anonymous namespace */
Paul Berry1a33e022013-08-18 20:59:37 -0700281
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700282void
Ian Romanick586e7412011-07-28 14:04:09 -0700283linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700284{
285 va_list ap;
286
Kenneth Graunked3073f52011-01-21 14:32:31 -0800287 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700288 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800289 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700290 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700291
292 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700293}
294
295
296void
Ian Romanick379a32f2011-07-28 14:09:06 -0700297linker_warning(gl_shader_program *prog, const char *fmt, ...)
298{
299 va_list ap;
300
301 ralloc_strcat(&prog->InfoLog, "error: ");
302 va_start(ap, fmt);
303 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
304 va_end(ap);
305
306}
307
308
Paul Berryb92900d2013-01-28 14:21:59 -0800309/**
310 * Given a string identifying a program resource, break it into a base name
311 * and an optional array index in square brackets.
312 *
313 * If an array index is present, \c out_base_name_end is set to point to the
314 * "[" that precedes the array index, and the array index itself is returned
315 * as a long.
316 *
317 * If no array index is present (or if the array index is negative or
318 * mal-formed), \c out_base_name_end, is set to point to the null terminator
319 * at the end of the input string, and -1 is returned.
320 *
321 * Only the final array index is parsed; if the string contains other array
322 * indices (or structure field accesses), they are left in the base name.
323 *
324 * No attempt is made to check that the base name is properly formed;
325 * typically the caller will look up the base name in a hash table, so
326 * ill-formed base names simply turn into hash table lookup failures.
327 */
328long
329parse_program_resource_name(const GLchar *name,
330 const GLchar **out_base_name_end)
331{
332 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
333 *
334 * "When an integer array element or block instance number is part of
335 * the name string, it will be specified in decimal form without a "+"
336 * or "-" sign or any extra leading zeroes. Additionally, the name
337 * string will not include white space anywhere in the string."
338 */
339
340 const size_t len = strlen(name);
341 *out_base_name_end = name + len;
342
343 if (len == 0 || name[len-1] != ']')
344 return -1;
345
346 /* Walk backwards over the string looking for a non-digit character. This
347 * had better be the opening bracket for an array index.
348 *
349 * Initially, i specifies the location of the ']'. Since the string may
350 * contain only the ']' charcater, walk backwards very carefully.
351 */
352 unsigned i;
353 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
354 /* empty */ ;
355
356 if ((i == 0) || name[i-1] != '[')
357 return -1;
358
359 long array_index = strtol(&name[i], NULL, 10);
360 if (array_index < 0)
361 return -1;
362
363 *out_base_name_end = name + (i - 1);
364 return array_index;
365}
366
367
Ian Romanick379a32f2011-07-28 14:09:06 -0700368void
Ian Romanick63974c02013-10-04 10:46:29 -0700369link_invalidate_variable_locations(exec_list *ir)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700370{
Ian Romanickcf8b14c2013-10-22 15:07:00 -0700371 foreach_list(node, ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700372 ir_variable *const var = ((ir_instruction *) node)->as_variable();
373
Paul Berry50895d42012-12-05 07:17:07 -0800374 if (var == NULL)
375 continue;
376
Ian Romanick63974c02013-10-04 10:46:29 -0700377 /* Only assign locations for variables that lack an explicit location.
378 * Explicit locations are set for all built-in variables, generic vertex
379 * shader inputs (via layout(location=...)), and generic fragment shader
380 * outputs (also via layout(location=...)).
381 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200382 if (!var->data.explicit_location) {
383 var->data.location = -1;
384 var->data.location_frac = 0;
Paul Berry50895d42012-12-05 07:17:07 -0800385 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700386
Ian Romanick63974c02013-10-04 10:46:29 -0700387 /* ir_variable::is_unmatched_generic_inout is used by the linker while
388 * connecting outputs from one stage to inputs of the next stage.
389 *
390 * There are two implicit assumptions here. First, we assume that any
391 * built-in variable (i.e., non-generic in or out) will have
392 * explicit_location set. Second, we assume that any generic in or out
393 * will not have explicit_location set.
394 *
395 * This second assumption will only be valid until
396 * GL_ARB_separate_shader_objects is supported. When that extension is
397 * implemented, this function will need some modifications.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700398 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200399 if (!var->data.explicit_location) {
400 var->data.is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800401 } else {
Tapani Pälli447bb902013-12-12 15:08:59 +0200402 var->data.is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800403 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700404 }
405}
406
407
Ian Romanickc93b8f12010-06-17 15:20:22 -0700408/**
Paul Berry44e07de2013-06-11 14:11:05 -0700409 * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
410 *
411 * Also check for errors based on incorrect usage of gl_ClipVertex and
412 * gl_ClipDistance.
413 *
414 * Return false if an error was reported.
415 */
416static void
Paul Berryb30e25f2013-12-17 09:49:43 -0800417analyze_clip_usage(struct gl_shader_program *prog,
Paul Berry44e07de2013-06-11 14:11:05 -0700418 struct gl_shader *shader, GLboolean *UsesClipDistance,
419 GLuint *ClipDistanceArraySize)
420{
421 *ClipDistanceArraySize = 0;
422
423 if (!prog->IsES && prog->Version >= 130) {
424 /* From section 7.1 (Vertex Shader Special Variables) of the
425 * GLSL 1.30 spec:
426 *
427 * "It is an error for a shader to statically write both
428 * gl_ClipVertex and gl_ClipDistance."
429 *
430 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
431 * gl_ClipVertex nor gl_ClipDistance.
432 */
433 find_assignment_visitor clip_vertex("gl_ClipVertex");
434 find_assignment_visitor clip_distance("gl_ClipDistance");
435
436 clip_vertex.run(shader->ir);
437 clip_distance.run(shader->ir);
438 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
439 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
Paul Berryb30e25f2013-12-17 09:49:43 -0800440 "and `gl_ClipDistance'\n",
Paul Berrye3b86f02014-01-07 10:58:56 -0800441 _mesa_shader_stage_to_string(shader->Stage));
Paul Berry44e07de2013-06-11 14:11:05 -0700442 return;
443 }
444 *UsesClipDistance = clip_distance.variable_found();
445 ir_variable *clip_distance_var =
446 shader->symbols->get_variable("gl_ClipDistance");
447 if (clip_distance_var)
448 *ClipDistanceArraySize = clip_distance_var->type->length;
449 } else {
450 *UsesClipDistance = false;
451 }
452}
453
454
455/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700456 * Verify that a vertex shader executable meets all semantic requirements.
457 *
Paul Berry642e5b412012-01-04 13:57:52 -0800458 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
459 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700460 *
461 * \param shader Vertex shader executable to be verified
462 */
Paul Berryb95d2372013-07-27 11:08:31 -0700463void
Eric Anholt849e1812010-06-30 11:49:17 -0700464validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700465 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700466{
467 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700468 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700469
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700470 /* From the GLSL 1.10 spec, page 48:
471 *
472 * "The variable gl_Position is available only in the vertex
473 * language and is intended for writing the homogeneous vertex
474 * position. All executions of a well-formed vertex shader
475 * executable must write a value into this variable. [...] The
476 * variable gl_Position is available only in the vertex
477 * language and is intended for writing the homogeneous vertex
478 * position. All executions of a well-formed vertex shader
479 * executable must write a value into this variable."
480 *
481 * while in GLSL 1.40 this text is changed to:
482 *
483 * "The variable gl_Position is available only in the vertex
484 * language and is intended for writing the homogeneous vertex
485 * position. It can be written at any time during shader
486 * execution. It may also be read back by a vertex shader
487 * after being written. This value will be used by primitive
488 * assembly, clipping, culling, and other fixed functionality
489 * operations, if present, that operate on primitives after
490 * vertex processing has occurred. Its value is undefined if
491 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700492 *
493 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
494 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700495 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700496 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700497 find_assignment_visitor find("gl_Position");
498 find.run(shader->ir);
499 if (!find.variable_found()) {
500 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700501 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700502 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700503 }
504
Paul Berryb30e25f2013-12-17 09:49:43 -0800505 analyze_clip_usage(prog, shader, &prog->Vert.UsesClipDistance,
Paul Berry44e07de2013-06-11 14:11:05 -0700506 &prog->Vert.ClipDistanceArraySize);
Ian Romanick832dfa52010-06-17 15:04:20 -0700507}
508
509
Ian Romanickc93b8f12010-06-17 15:20:22 -0700510/**
511 * Verify that a fragment shader executable meets all semantic requirements
512 *
513 * \param shader Fragment shader executable to be verified
514 */
Paul Berryb95d2372013-07-27 11:08:31 -0700515void
Eric Anholt849e1812010-06-30 11:49:17 -0700516validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700517 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700518{
519 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700520 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700521
Ian Romanick832dfa52010-06-17 15:04:20 -0700522 find_assignment_visitor frag_color("gl_FragColor");
523 find_assignment_visitor frag_data("gl_FragData");
524
Eric Anholt16b68b12010-06-30 11:05:43 -0700525 frag_color.run(shader->ir);
526 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700527
Ian Romanick832dfa52010-06-17 15:04:20 -0700528 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700529 linker_error(prog, "fragment shader writes to both "
530 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700531 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700532}
533
Bryan Cain25480922013-02-15 09:46:50 -0600534/**
535 * Verify that a geometry shader executable meets all semantic requirements
536 *
Paul Berry44e07de2013-06-11 14:11:05 -0700537 * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
538 * prog->Geom.ClipDistanceArraySize as a side effect.
Bryan Cain25480922013-02-15 09:46:50 -0600539 *
540 * \param shader Geometry shader executable to be verified
541 */
542void
543validate_geometry_shader_executable(struct gl_shader_program *prog,
544 struct gl_shader *shader)
545{
546 if (shader == NULL)
547 return;
548
549 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
550 prog->Geom.VerticesIn = num_vertices;
Paul Berry44e07de2013-06-11 14:11:05 -0700551
Paul Berryb30e25f2013-12-17 09:49:43 -0800552 analyze_clip_usage(prog, shader, &prog->Geom.UsesClipDistance,
Paul Berry44e07de2013-06-11 14:11:05 -0700553 &prog->Geom.ClipDistanceArraySize);
Paul Berry1a33e022013-08-18 20:59:37 -0700554
555 find_end_primitive_visitor end_primitive;
556 end_primitive.run(shader->ir);
557 prog->Geom.UsesEndPrimitive = end_primitive.end_primitive_found();
Bryan Cain25480922013-02-15 09:46:50 -0600558}
559
Ian Romanick832dfa52010-06-17 15:04:20 -0700560
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700561/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700562 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700563 */
Paul Berryb95d2372013-07-27 11:08:31 -0700564void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700565cross_validate_globals(struct gl_shader_program *prog,
566 struct gl_shader **shader_list,
567 unsigned num_shaders,
568 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700569{
570 /* Examine all of the uniforms in all of the shaders and cross validate
571 * them.
572 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700573 glsl_symbol_table variables;
574 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700575 if (shader_list[i] == NULL)
576 continue;
577
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700578 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700579 ir_variable *const var = ((ir_instruction *) node)->as_variable();
580
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700581 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700582 continue;
583
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200584 if (uniforms_only && (var->data.mode != ir_var_uniform))
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700585 continue;
586
Ian Romanick7e2aa912010-07-19 17:12:42 -0700587 /* Don't cross validate temporaries that are at global scope. These
588 * will eventually get pulled into the shaders 'main'.
589 */
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200590 if (var->data.mode == ir_var_temporary)
Ian Romanick7e2aa912010-07-19 17:12:42 -0700591 continue;
592
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700593 /* If a global with this name has already been seen, verify that the
594 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700595 * initializers, the values of the initializers must be the same.
596 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700597 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700598 if (existing != NULL) {
599 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700600 /* Consider the types to be "the same" if both types are arrays
601 * of the same type and one of the arrays is implicitly sized.
602 * In addition, set the type of the linked variable to the
603 * explicitly sized array.
604 */
605 if (var->type->is_array()
606 && existing->type->is_array()
607 && (var->type->fields.array == existing->type->fields.array)
608 && ((var->type->length == 0)
609 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800610 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700611 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800612 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700613 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700614 linker_error(prog, "%s `%s' declared as type "
615 "`%s' and type `%s'\n",
616 mode_string(var),
617 var->name, var->type->name,
618 existing->type->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700619 return;
Ian Romanicka2711d62010-08-29 22:07:49 -0700620 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700621 }
622
Tapani Pälli447bb902013-12-12 15:08:59 +0200623 if (var->data.explicit_location) {
624 if (existing->data.explicit_location
625 && (var->data.location != existing->data.location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700626 linker_error(prog, "explicit locations for %s "
627 "`%s' have differing values\n",
628 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700629 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700630 }
631
Tapani Pälli447bb902013-12-12 15:08:59 +0200632 existing->data.location = var->data.location;
633 existing->data.explicit_location = true;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700634 }
635
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700636 /* From the GLSL 4.20 specification:
637 * "A link error will result if two compilation units in a program
638 * specify different integer-constant bindings for the same
639 * opaque-uniform name. However, it is not an error to specify a
640 * binding on some but not all declarations for the same name"
641 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200642 if (var->data.explicit_binding) {
643 if (existing->data.explicit_binding &&
644 var->data.binding != existing->data.binding) {
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700645 linker_error(prog, "explicit bindings for %s "
646 "`%s' have differing values\n",
647 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700648 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700649 }
650
Tapani Pälli447bb902013-12-12 15:08:59 +0200651 existing->data.binding = var->data.binding;
652 existing->data.explicit_binding = true;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700653 }
654
Francisco Jerez5c114932013-09-11 12:14:46 -0700655 if (var->type->contains_atomic() &&
Tapani Pälli447bb902013-12-12 15:08:59 +0200656 var->data.atomic.offset != existing->data.atomic.offset) {
Francisco Jerez5c114932013-09-11 12:14:46 -0700657 linker_error(prog, "offset specifications for %s "
658 "`%s' have differing values\n",
659 mode_string(var), var->name);
660 return;
661 }
662
Ian Romanick46173f92011-10-31 13:07:06 -0700663 /* Validate layout qualifiers for gl_FragDepth.
664 *
665 * From the AMD/ARB_conservative_depth specs:
666 *
667 * "If gl_FragDepth is redeclared in any fragment shader in a
668 * program, it must be redeclared in all fragment shaders in
669 * that program that have static assignments to
670 * gl_FragDepth. All redeclarations of gl_FragDepth in all
671 * fragment shaders in a single program must have the same set
672 * of qualifiers."
673 */
674 if (strcmp(var->name, "gl_FragDepth") == 0) {
Tapani Pälli447bb902013-12-12 15:08:59 +0200675 bool layout_declared = var->data.depth_layout != ir_depth_layout_none;
Ian Romanick46173f92011-10-31 13:07:06 -0700676 bool layout_differs =
Tapani Pälli447bb902013-12-12 15:08:59 +0200677 var->data.depth_layout != existing->data.depth_layout;
Ian Romanick46173f92011-10-31 13:07:06 -0700678
679 if (layout_declared && layout_differs) {
680 linker_error(prog,
681 "All redeclarations of gl_FragDepth in all "
682 "fragment shaders in a single program must have "
683 "the same set of qualifiers.");
684 }
685
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200686 if (var->data.used && layout_differs) {
Ian Romanick46173f92011-10-31 13:07:06 -0700687 linker_error(prog,
688 "If gl_FragDepth is redeclared with a layout "
689 "qualifier in any fragment shader, it must be "
690 "redeclared with the same layout qualifier in "
691 "all fragment shaders that have assignments to "
692 "gl_FragDepth");
693 }
694 }
Chad Versaceaddae332011-01-27 01:40:31 -0800695
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700696 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
697 *
698 * "If a shared global has multiple initializers, the
699 * initializers must all be constant expressions, and they
700 * must all have the same value. Otherwise, a link error will
701 * result. (A shared global having only one initializer does
702 * not require that initializer to be a constant expression.)"
703 *
704 * Previous to 4.20 the GLSL spec simply said that initializers
705 * must have the same value. In this case of non-constant
706 * initializers, this was impossible to determine. As a result,
707 * no vendor actually implemented that behavior. The 4.20
708 * behavior matches the implemented behavior of at least one other
709 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700710 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700711 if (var->constant_initializer != NULL) {
712 if (existing->constant_initializer != NULL) {
713 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700714 linker_error(prog, "initializers for %s "
715 "`%s' have differing values\n",
716 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700717 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700718 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700719 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700720 /* If the first-seen instance of a particular uniform did not
721 * have an initializer but a later instance does, copy the
722 * initializer to the version stored in the symbol table.
723 */
Ian Romanickde415b72010-07-14 13:22:12 -0700724 /* FINISHME: This is wrong. The constant_value field should
725 * FINISHME: not be modified! Imagine a case where a shader
726 * FINISHME: without an initializer is linked in two different
727 * FINISHME: programs with shaders that have differing
728 * FINISHME: initializers. Linking with the first will
729 * FINISHME: modify the shader, and linking with the second
730 * FINISHME: will fail.
731 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700732 existing->constant_initializer =
733 var->constant_initializer->clone(ralloc_parent(existing),
734 NULL);
735 }
736 }
737
Tapani Pälli447bb902013-12-12 15:08:59 +0200738 if (var->data.has_initializer) {
739 if (existing->data.has_initializer
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700740 && (var->constant_initializer == NULL
741 || existing->constant_initializer == NULL)) {
742 linker_error(prog,
743 "shared global variable `%s' has multiple "
744 "non-constant initializers.\n",
745 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700746 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700747 }
748
749 /* Some instance had an initializer, so keep track of that. In
750 * this location, all sorts of initializers (constant or
751 * otherwise) will propagate the existence to the variable
752 * stored in the symbol table.
753 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200754 existing->data.has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700755 }
Chad Versace7528f142010-11-17 14:34:38 -0800756
Tapani Pällic1d30802013-12-12 12:57:57 +0200757 if (existing->data.invariant != var->data.invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700758 linker_error(prog, "declarations for %s `%s' have "
759 "mismatching invariant qualifiers\n",
760 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700761 return;
Chad Versace7528f142010-11-17 14:34:38 -0800762 }
Tapani Pällic1d30802013-12-12 12:57:57 +0200763 if (existing->data.centroid != var->data.centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700764 linker_error(prog, "declarations for %s `%s' have "
765 "mismatching centroid qualifiers\n",
766 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700767 return;
Chad Versace61428dd2011-01-10 15:29:30 -0800768 }
Tapani Pällic1d30802013-12-12 12:57:57 +0200769 if (existing->data.sample != var->data.sample) {
Chris Forbes51c5fc82013-11-29 21:26:10 +1300770 linker_error(prog, "declarations for %s `%s` have "
771 "mismatching sample qualifiers\n",
772 mode_string(var), var->name);
773 return;
774 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700775 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700776 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700777 }
778 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700779}
780
781
Ian Romanick37101922010-06-18 19:02:10 -0700782/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700783 * Perform validation of uniforms used across multiple shader stages
784 */
Paul Berryb95d2372013-07-27 11:08:31 -0700785void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700786cross_validate_uniforms(struct gl_shader_program *prog)
787{
Paul Berryb95d2372013-07-27 11:08:31 -0700788 cross_validate_globals(prog, prog->_LinkedShaders,
Paul Berry665b8d72014-01-07 10:11:39 -0800789 MESA_SHADER_STAGES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700790}
791
Eric Anholtf609cf72012-04-27 13:52:56 -0700792/**
793 * Accumulates the array of prog->UniformBlocks and checks that all
794 * definitons of blocks agree on their contents.
795 */
796static bool
797interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
798{
799 unsigned max_num_uniform_blocks = 0;
Paul Berry665b8d72014-01-07 10:11:39 -0800800 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholtf609cf72012-04-27 13:52:56 -0700801 if (prog->_LinkedShaders[i])
802 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
803 }
804
Paul Berry665b8d72014-01-07 10:11:39 -0800805 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholtf609cf72012-04-27 13:52:56 -0700806 struct gl_shader *sh = prog->_LinkedShaders[i];
807
808 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
809 max_num_uniform_blocks);
810 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
811 prog->UniformBlockStageIndex[i][j] = -1;
812
813 if (sh == NULL)
814 continue;
815
816 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
817 int index = link_cross_validate_uniform_block(prog,
818 &prog->UniformBlocks,
819 &prog->NumUniformBlocks,
820 &sh->UniformBlocks[j]);
821
822 if (index == -1) {
823 linker_error(prog, "uniform block `%s' has mismatching definitions",
824 sh->UniformBlocks[j].Name);
825 return false;
826 }
827
828 prog->UniformBlockStageIndex[i][index] = j;
829 }
830 }
831
832 return true;
833}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700834
Ian Romanick37101922010-06-18 19:02:10 -0700835
Ian Romanick3fb87872010-07-09 14:09:34 -0700836/**
837 * Populates a shaders symbol table with all global declarations
838 */
839static void
840populate_symbol_table(gl_shader *sh)
841{
842 sh->symbols = new(sh) glsl_symbol_table;
843
844 foreach_list(node, sh->ir) {
845 ir_instruction *const inst = (ir_instruction *) node;
846 ir_variable *var;
847 ir_function *func;
848
849 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700850 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700851 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700852 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700853 }
854 }
855}
856
857
858/**
Ian Romanick31a97862010-07-12 18:48:50 -0700859 * Remap variables referenced in an instruction tree
860 *
861 * This is used when instruction trees are cloned from one shader and placed in
862 * another. These trees will contain references to \c ir_variable nodes that
863 * do not exist in the target shader. This function finds these \c ir_variable
864 * references and replaces the references with matching variables in the target
865 * shader.
866 *
867 * If there is no matching variable in the target shader, a clone of the
868 * \c ir_variable is made and added to the target shader. The new variable is
869 * added to \b both the instruction stream and the symbol table.
870 *
871 * \param inst IR tree that is to be processed.
872 * \param symbols Symbol table containing global scope symbols in the
873 * linked shader.
874 * \param instructions Instruction stream where new variable declarations
875 * should be added.
876 */
877void
Eric Anholt8273bd42010-08-04 12:34:56 -0700878remap_variables(ir_instruction *inst, struct gl_shader *target,
879 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700880{
881 class remap_visitor : public ir_hierarchical_visitor {
882 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700883 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700884 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700885 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700886 this->target = target;
887 this->symbols = target->symbols;
888 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700889 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700890 }
891
892 virtual ir_visitor_status visit(ir_dereference_variable *ir)
893 {
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200894 if (ir->var->data.mode == ir_var_temporary) {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700895 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
896
897 assert(var != NULL);
898 ir->var = var;
899 return visit_continue;
900 }
901
Ian Romanick31a97862010-07-12 18:48:50 -0700902 ir_variable *const existing =
903 this->symbols->get_variable(ir->var->name);
904 if (existing != NULL)
905 ir->var = existing;
906 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700907 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700908
Eric Anholt001eee52010-11-05 06:11:24 -0700909 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700910 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700911 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700912 }
913
914 return visit_continue;
915 }
916
917 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700918 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700919 glsl_symbol_table *symbols;
920 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700921 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700922 };
923
Eric Anholt8273bd42010-08-04 12:34:56 -0700924 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700925
926 inst->accept(&v);
927}
928
929
930/**
931 * Move non-declarations from one instruction stream to another
932 *
933 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700934 * 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 -0700935 * pointer) for \c last and \c false for \c make_copies on the first
936 * call. Successive calls pass the return value of the previous call for
937 * \c last and \c true for \c make_copies.
938 *
939 * \param instructions Source instruction stream
940 * \param last Instruction after which new instructions should be
941 * inserted in the target instruction stream
942 * \param make_copies Flag selecting whether instructions in \c instructions
943 * should be copied (via \c ir_instruction::clone) into the
944 * target list or moved.
945 *
946 * \return
947 * The new "last" instruction in the target instruction stream. This pointer
948 * is suitable for use as the \c last parameter of a later call to this
949 * function.
950 */
951exec_node *
952move_non_declarations(exec_list *instructions, exec_node *last,
953 bool make_copies, gl_shader *target)
954{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700955 hash_table *temps = NULL;
956
957 if (make_copies)
958 temps = hash_table_ctor(0, hash_table_pointer_hash,
959 hash_table_pointer_compare);
960
Ian Romanick303c99f2010-07-19 12:34:56 -0700961 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700962 ir_instruction *inst = (ir_instruction *) node;
963
Ian Romanick7e2aa912010-07-19 17:12:42 -0700964 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700965 continue;
966
Ian Romanick7e2aa912010-07-19 17:12:42 -0700967 ir_variable *var = inst->as_variable();
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200968 if ((var != NULL) && (var->data.mode != ir_var_temporary))
Ian Romanick7e2aa912010-07-19 17:12:42 -0700969 continue;
970
971 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700972 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700973 || inst->as_if() /* for initializers with the ?: operator */
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200974 || ((var != NULL) && (var->data.mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700975
976 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700977 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700978
979 if (var != NULL)
980 hash_table_insert(temps, inst, var);
981 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700982 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700983 } else {
984 inst->remove();
985 }
986
987 last->insert_after(inst);
988 last = inst;
989 }
990
Ian Romanick7e2aa912010-07-19 17:12:42 -0700991 if (make_copies)
992 hash_table_dtor(temps);
993
Ian Romanick31a97862010-07-12 18:48:50 -0700994 return last;
995}
996
997/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700998 * Get the function signature for main from a shader
999 */
1000static ir_function_signature *
1001get_main_function_signature(gl_shader *sh)
1002{
1003 ir_function *const f = sh->symbols->get_function("main");
1004 if (f != NULL) {
1005 exec_list void_parameters;
1006
1007 /* Look for the 'void main()' signature and ensure that it's defined.
1008 * This keeps the linker from accidentally pick a shader that just
1009 * contains a prototype for main.
1010 *
1011 * We don't have to check for multiple definitions of main (in multiple
1012 * shaders) because that would have already been caught above.
1013 */
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001014 ir_function_signature *sig = f->matching_signature(NULL, &void_parameters);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001015 if ((sig != NULL) && sig->is_defined) {
1016 return sig;
1017 }
1018 }
1019
1020 return NULL;
1021}
1022
1023
1024/**
Brian Paul84a12732012-02-02 20:10:40 -07001025 * This class is only used in link_intrastage_shaders() below but declaring
1026 * it inside that function leads to compiler warnings with some versions of
1027 * gcc.
1028 */
1029class array_sizing_visitor : public ir_hierarchical_visitor {
1030public:
Paul Berry15e05b92013-09-25 14:07:37 -07001031 array_sizing_visitor()
1032 : mem_ctx(ralloc_context(NULL)),
1033 unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash,
1034 hash_table_pointer_compare))
1035 {
1036 }
1037
1038 ~array_sizing_visitor()
1039 {
1040 hash_table_dtor(this->unnamed_interfaces);
1041 ralloc_free(this->mem_ctx);
1042 }
1043
Brian Paul84a12732012-02-02 20:10:40 -07001044 virtual ir_visitor_status visit(ir_variable *var)
1045 {
Tapani Pälli447bb902013-12-12 15:08:59 +02001046 fixup_type(&var->type, var->data.max_array_access);
Paul Berrye2266692013-09-23 10:44:19 -07001047 if (var->type->is_interface()) {
1048 if (interface_contains_unsized_arrays(var->type)) {
1049 const glsl_type *new_type =
1050 resize_interface_members(var->type, var->max_ifc_array_access);
1051 var->type = new_type;
1052 var->change_interface_type(new_type);
1053 }
1054 } else if (var->type->is_array() &&
1055 var->type->fields.array->is_interface()) {
1056 if (interface_contains_unsized_arrays(var->type->fields.array)) {
1057 const glsl_type *new_type =
1058 resize_interface_members(var->type->fields.array,
1059 var->max_ifc_array_access);
1060 var->change_interface_type(new_type);
1061 var->type =
1062 glsl_type::get_array_instance(new_type, var->type->length);
1063 }
Paul Berry15e05b92013-09-25 14:07:37 -07001064 } else if (const glsl_type *ifc_type = var->get_interface_type()) {
1065 /* Store a pointer to the variable in the unnamed_interfaces
1066 * hashtable.
1067 */
1068 ir_variable **interface_vars = (ir_variable **)
1069 hash_table_find(this->unnamed_interfaces, ifc_type);
1070 if (interface_vars == NULL) {
1071 interface_vars = rzalloc_array(mem_ctx, ir_variable *,
1072 ifc_type->length);
1073 hash_table_insert(this->unnamed_interfaces, interface_vars,
1074 ifc_type);
1075 }
1076 unsigned index = ifc_type->field_index(var->name);
1077 assert(index < ifc_type->length);
1078 assert(interface_vars[index] == NULL);
1079 interface_vars[index] = var;
Brian Paul84a12732012-02-02 20:10:40 -07001080 }
1081 return visit_continue;
1082 }
Paul Berrye2266692013-09-23 10:44:19 -07001083
Paul Berry15e05b92013-09-25 14:07:37 -07001084 /**
1085 * For each unnamed interface block that was discovered while running the
1086 * visitor, adjust the interface type to reflect the newly assigned array
1087 * sizes, and fix up the ir_variable nodes to point to the new interface
1088 * type.
1089 */
1090 void fixup_unnamed_interface_types()
1091 {
1092 hash_table_call_foreach(this->unnamed_interfaces,
1093 fixup_unnamed_interface_type, NULL);
1094 }
1095
Paul Berrye2266692013-09-23 10:44:19 -07001096private:
1097 /**
1098 * If the type pointed to by \c type represents an unsized array, replace
1099 * it with a sized array whose size is determined by max_array_access.
1100 */
1101 static void fixup_type(const glsl_type **type, unsigned max_array_access)
1102 {
Timothy Arcerib59c5922013-10-23 21:31:27 +11001103 if ((*type)->is_unsized_array()) {
Paul Berrye2266692013-09-23 10:44:19 -07001104 *type = glsl_type::get_array_instance((*type)->fields.array,
1105 max_array_access + 1);
1106 assert(*type != NULL);
1107 }
1108 }
1109
1110 /**
1111 * Determine whether the given interface type contains unsized arrays (if
1112 * it doesn't, array_sizing_visitor doesn't need to process it).
1113 */
1114 static bool interface_contains_unsized_arrays(const glsl_type *type)
1115 {
1116 for (unsigned i = 0; i < type->length; i++) {
1117 const glsl_type *elem_type = type->fields.structure[i].type;
Timothy Arcerib59c5922013-10-23 21:31:27 +11001118 if (elem_type->is_unsized_array())
Paul Berrye2266692013-09-23 10:44:19 -07001119 return true;
1120 }
1121 return false;
1122 }
1123
1124 /**
1125 * Create a new interface type based on the given type, with unsized arrays
1126 * replaced by sized arrays whose size is determined by
1127 * max_ifc_array_access.
1128 */
1129 static const glsl_type *
1130 resize_interface_members(const glsl_type *type,
1131 const unsigned *max_ifc_array_access)
1132 {
1133 unsigned num_fields = type->length;
1134 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1135 memcpy(fields, type->fields.structure,
1136 num_fields * sizeof(*fields));
1137 for (unsigned i = 0; i < num_fields; i++) {
1138 fixup_type(&fields[i].type, max_ifc_array_access[i]);
1139 }
1140 glsl_interface_packing packing =
1141 (glsl_interface_packing) type->interface_packing;
1142 const glsl_type *new_ifc_type =
1143 glsl_type::get_interface_instance(fields, num_fields,
1144 packing, type->name);
1145 delete [] fields;
1146 return new_ifc_type;
1147 }
Paul Berry15e05b92013-09-25 14:07:37 -07001148
1149 static void fixup_unnamed_interface_type(const void *key, void *data,
1150 void *)
1151 {
1152 const glsl_type *ifc_type = (const glsl_type *) key;
1153 ir_variable **interface_vars = (ir_variable **) data;
1154 unsigned num_fields = ifc_type->length;
1155 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1156 memcpy(fields, ifc_type->fields.structure,
1157 num_fields * sizeof(*fields));
1158 bool interface_type_changed = false;
1159 for (unsigned i = 0; i < num_fields; i++) {
1160 if (interface_vars[i] != NULL &&
1161 fields[i].type != interface_vars[i]->type) {
1162 fields[i].type = interface_vars[i]->type;
1163 interface_type_changed = true;
1164 }
1165 }
1166 if (!interface_type_changed) {
1167 delete [] fields;
1168 return;
1169 }
1170 glsl_interface_packing packing =
1171 (glsl_interface_packing) ifc_type->interface_packing;
1172 const glsl_type *new_ifc_type =
1173 glsl_type::get_interface_instance(fields, num_fields, packing,
1174 ifc_type->name);
1175 delete [] fields;
1176 for (unsigned i = 0; i < num_fields; i++) {
1177 if (interface_vars[i] != NULL)
1178 interface_vars[i]->change_interface_type(new_ifc_type);
1179 }
1180 }
1181
1182 /**
1183 * Memory context used to allocate the data in \c unnamed_interfaces.
1184 */
1185 void *mem_ctx;
1186
1187 /**
1188 * Hash table from const glsl_type * to an array of ir_variable *'s
1189 * pointing to the ir_variables constituting each unnamed interface block.
1190 */
1191 hash_table *unnamed_interfaces;
Brian Paul84a12732012-02-02 20:10:40 -07001192};
1193
Brian Paul84a12732012-02-02 20:10:40 -07001194/**
Eric Anholt6065a872013-06-12 18:12:40 -07001195 * Performs the cross-validation of geometry shader max_vertices and
1196 * primitive type layout qualifiers for the attached geometry shaders,
1197 * and propagates them to the linked GS and linked shader program.
1198 */
1199static void
1200link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
1201 struct gl_shader *linked_shader,
1202 struct gl_shader **shader_list,
1203 unsigned num_shaders)
1204{
1205 linked_shader->Geom.VerticesOut = 0;
1206 linked_shader->Geom.InputType = PRIM_UNKNOWN;
1207 linked_shader->Geom.OutputType = PRIM_UNKNOWN;
1208
1209 /* No in/out qualifiers defined for anything but GLSL 1.50+
1210 * geometry shaders so far.
1211 */
Paul Berrye3b86f02014-01-07 10:58:56 -08001212 if (linked_shader->Stage != MESA_SHADER_GEOMETRY || prog->Version < 150)
Eric Anholt6065a872013-06-12 18:12:40 -07001213 return;
1214
1215 /* From the GLSL 1.50 spec, page 46:
1216 *
1217 * "All geometry shader output layout declarations in a program
1218 * must declare the same layout and same value for
1219 * max_vertices. There must be at least one geometry output
1220 * layout declaration somewhere in a program, but not all
1221 * geometry shaders (compilation units) are required to
1222 * declare it."
1223 */
1224
1225 for (unsigned i = 0; i < num_shaders; i++) {
1226 struct gl_shader *shader = shader_list[i];
1227
1228 if (shader->Geom.InputType != PRIM_UNKNOWN) {
1229 if (linked_shader->Geom.InputType != PRIM_UNKNOWN &&
1230 linked_shader->Geom.InputType != shader->Geom.InputType) {
1231 linker_error(prog, "geometry shader defined with conflicting "
1232 "input types\n");
1233 return;
1234 }
1235 linked_shader->Geom.InputType = shader->Geom.InputType;
1236 }
1237
1238 if (shader->Geom.OutputType != PRIM_UNKNOWN) {
1239 if (linked_shader->Geom.OutputType != PRIM_UNKNOWN &&
1240 linked_shader->Geom.OutputType != shader->Geom.OutputType) {
1241 linker_error(prog, "geometry shader defined with conflicting "
1242 "output types\n");
1243 return;
1244 }
1245 linked_shader->Geom.OutputType = shader->Geom.OutputType;
1246 }
1247
1248 if (shader->Geom.VerticesOut != 0) {
1249 if (linked_shader->Geom.VerticesOut != 0 &&
1250 linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) {
1251 linker_error(prog, "geometry shader defined with conflicting "
1252 "output vertex count (%d and %d)\n",
1253 linked_shader->Geom.VerticesOut,
1254 shader->Geom.VerticesOut);
1255 return;
1256 }
1257 linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut;
1258 }
1259 }
1260
1261 /* Just do the intrastage -> interstage propagation right now,
1262 * since we already know we're in the right type of shader program
1263 * for doing it.
1264 */
1265 if (linked_shader->Geom.InputType == PRIM_UNKNOWN) {
1266 linker_error(prog,
1267 "geometry shader didn't declare primitive input type\n");
1268 return;
1269 }
1270 prog->Geom.InputType = linked_shader->Geom.InputType;
1271
1272 if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) {
1273 linker_error(prog,
1274 "geometry shader didn't declare primitive output type\n");
1275 return;
1276 }
1277 prog->Geom.OutputType = linked_shader->Geom.OutputType;
1278
1279 if (linked_shader->Geom.VerticesOut == 0) {
1280 linker_error(prog,
1281 "geometry shader didn't declare max_vertices\n");
1282 return;
1283 }
1284 prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut;
1285}
1286
1287/**
Ian Romanick3fb87872010-07-09 14:09:34 -07001288 * Combine a group of shaders for a single stage to generate a linked shader
1289 *
1290 * \note
1291 * If this function is supplied a single shader, it is cloned, and the new
1292 * shader is returned.
1293 */
1294static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001295link_intrastage_shaders(void *mem_ctx,
1296 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -07001297 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -07001298 struct gl_shader **shader_list,
1299 unsigned num_shaders)
1300{
Eric Anholtf609cf72012-04-27 13:52:56 -07001301 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -07001302
Ian Romanick13f782c2010-06-29 18:53:38 -07001303 /* Check that global variables defined in multiple shaders are consistent.
1304 */
Paul Berryb95d2372013-07-27 11:08:31 -07001305 cross_validate_globals(prog, shader_list, num_shaders, false);
1306 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -07001307 return NULL;
1308
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001309 /* Check that interface blocks defined in multiple shaders are consistent.
1310 */
Paul Berryb95d2372013-07-27 11:08:31 -07001311 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
1312 num_shaders);
1313 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001314 return NULL;
1315
Paul Berry4682b9b2013-07-27 15:07:08 -07001316 /* Link up uniform blocks defined within this stage. */
1317 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -05001318 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
1319 &uniform_blocks);
Eric Anholtf609cf72012-04-27 13:52:56 -07001320
Ian Romanick13f782c2010-06-29 18:53:38 -07001321 /* Check that there is only a single definition of each function signature
1322 * across all shaders.
1323 */
1324 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1325 foreach_list(node, shader_list[i]->ir) {
1326 ir_function *const f = ((ir_instruction *) node)->as_function();
1327
1328 if (f == NULL)
1329 continue;
1330
1331 for (unsigned j = i + 1; j < num_shaders; j++) {
1332 ir_function *const other =
1333 shader_list[j]->symbols->get_function(f->name);
1334
1335 /* If the other shader has no function (and therefore no function
1336 * signatures) with the same name, skip to the next shader.
1337 */
1338 if (other == NULL)
1339 continue;
1340
1341 foreach_iter (exec_list_iterator, iter, *f) {
1342 ir_function_signature *sig =
1343 (ir_function_signature *) iter.get();
1344
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001345 if (!sig->is_defined || sig->is_builtin())
Ian Romanick13f782c2010-06-29 18:53:38 -07001346 continue;
1347
1348 ir_function_signature *other_sig =
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001349 other->exact_matching_signature(NULL, &sig->parameters);
Ian Romanick13f782c2010-06-29 18:53:38 -07001350
1351 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001352 && !other_sig->is_builtin()) {
Ian Romanick586e7412011-07-28 14:04:09 -07001353 linker_error(prog, "function `%s' is multiply defined",
1354 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001355 return NULL;
1356 }
1357 }
1358 }
1359 }
1360 }
1361
1362 /* Find the shader that defines main, and make a clone of it.
1363 *
1364 * Starting with the clone, search for undefined references. If one is
1365 * found, find the shader that defines it. Clone the reference and add
1366 * it to the shader. Repeat until there are no undefined references or
1367 * until a reference cannot be resolved.
1368 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001369 gl_shader *main = NULL;
1370 for (unsigned i = 0; i < num_shaders; i++) {
1371 if (get_main_function_signature(shader_list[i]) != NULL) {
1372 main = shader_list[i];
1373 break;
1374 }
1375 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001376
Ian Romanick15ce87e2010-07-09 15:28:22 -07001377 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001378 linker_error(prog, "%s shader lacks `main'\n",
Paul Berrye3b86f02014-01-07 10:58:56 -08001379 _mesa_shader_stage_to_string(shader_list[0]->Stage));
Ian Romanick15ce87e2010-07-09 15:28:22 -07001380 return NULL;
1381 }
1382
Ian Romanick4a455952010-10-13 15:13:02 -07001383 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001384 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001385 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001386
Eric Anholtf609cf72012-04-27 13:52:56 -07001387 linked->UniformBlocks = uniform_blocks;
1388 linked->NumUniformBlocks = num_uniform_blocks;
1389 ralloc_steal(linked, linked->UniformBlocks);
1390
Eric Anholt6065a872013-06-12 18:12:40 -07001391 link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
1392
Ian Romanick15ce87e2010-07-09 15:28:22 -07001393 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001394
Ian Romanick31a97862010-07-12 18:48:50 -07001395 /* The a pointer to the main function in the final linked shader (i.e., the
1396 * copy of the original shader that contained the main function).
1397 */
1398 ir_function_signature *const main_sig = get_main_function_signature(linked);
1399
1400 /* Move any instructions other than variable declarations or function
1401 * declarations into main.
1402 */
Ian Romanick9303e352010-07-19 12:33:54 -07001403 exec_node *insertion_point =
1404 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1405 linked);
1406
Ian Romanick31a97862010-07-12 18:48:50 -07001407 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001408 if (shader_list[i] == main)
1409 continue;
1410
Ian Romanick31a97862010-07-12 18:48:50 -07001411 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001412 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001413 }
1414
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001415 /* Check if any shader needs built-in functions. */
1416 bool need_builtins = false;
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001417 for (unsigned i = 0; i < num_shaders; i++) {
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001418 if (shader_list[i]->uses_builtin_functions) {
1419 need_builtins = true;
1420 break;
1421 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001422 }
1423
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001424 bool ok;
1425 if (need_builtins) {
1426 /* Make a temporary array one larger than shader_list, which will hold
1427 * the built-in function shader as well.
1428 */
1429 gl_shader **linking_shaders = (gl_shader **)
1430 calloc(num_shaders + 1, sizeof(gl_shader *));
1431 memcpy(linking_shaders, shader_list, num_shaders * sizeof(gl_shader *));
1432 linking_shaders[num_shaders] = _mesa_glsl_get_builtin_function_shader();
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001433
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001434 ok = link_function_calls(prog, linked, linking_shaders, num_shaders + 1);
1435
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001436 free(linking_shaders);
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001437 } else {
1438 ok = link_function_calls(prog, linked, shader_list, num_shaders);
1439 }
1440
1441
1442 if (!ok) {
1443 ctx->Driver.DeleteShader(ctx, linked);
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001444 return NULL;
Ian Romanick4a455952010-10-13 15:13:02 -07001445 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001446
Paul Berryc148ef62011-08-03 15:37:01 -07001447 /* At this point linked should contain all of the linked IR, so
1448 * validate it to make sure nothing went wrong.
1449 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001450 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07001451
Paul Berry7cfefe62013-07-30 21:13:48 -07001452 /* Set the size of geometry shader input arrays */
Paul Berrye3b86f02014-01-07 10:58:56 -08001453 if (linked->Stage == MESA_SHADER_GEOMETRY) {
Paul Berry7cfefe62013-07-30 21:13:48 -07001454 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
1455 geom_array_resize_visitor input_resize_visitor(num_vertices, prog);
1456 foreach_iter(exec_list_iterator, iter, *linked->ir) {
1457 ir_instruction *ir = (ir_instruction *)iter.get();
1458 ir->accept(&input_resize_visitor);
1459 }
1460 }
1461
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001462 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001463 * unspecified sizes have a size specified. The size is inferred from the
1464 * max_array_access field.
1465 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001466 array_sizing_visitor v;
1467 v.run(linked->ir);
Paul Berry15e05b92013-09-25 14:07:37 -07001468 v.fixup_unnamed_interface_types();
Ian Romanick6f539212010-12-07 18:30:33 -08001469
Ian Romanick3fb87872010-07-09 14:09:34 -07001470 return linked;
1471}
1472
Eric Anholta721abf2010-08-23 10:32:01 -07001473/**
1474 * Update the sizes of linked shader uniform arrays to the maximum
1475 * array index used.
1476 *
1477 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1478 *
1479 * If one or more elements of an array are active,
1480 * GetActiveUniform will return the name of the array in name,
1481 * subject to the restrictions listed above. The type of the array
1482 * is returned in type. The size parameter contains the highest
1483 * array element index used, plus one. The compiler or linker
1484 * determines the highest index used. There will be only one
1485 * active uniform reported by the GL per uniform array.
1486
1487 */
1488static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001489update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001490{
Paul Berry665b8d72014-01-07 10:11:39 -08001491 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07001492 if (prog->_LinkedShaders[i] == NULL)
1493 continue;
1494
Eric Anholta721abf2010-08-23 10:32:01 -07001495 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1496 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1497
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001498 if ((var == NULL) || (var->data.mode != ir_var_uniform) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001499 !var->type->is_array())
1500 continue;
1501
Eric Anholt9feb4032012-05-01 14:43:31 -07001502 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1503 * will not be eliminated. Since we always do std140, just
1504 * don't resize arrays in UBOs.
Francisco Jerez5c114932013-09-11 12:14:46 -07001505 *
1506 * Atomic counters are supposed to get deterministic
1507 * locations assigned based on the declaration ordering and
1508 * sizes, array compaction would mess that up.
Eric Anholt9feb4032012-05-01 14:43:31 -07001509 */
Francisco Jerez5c114932013-09-11 12:14:46 -07001510 if (var->is_in_uniform_block() || var->type->contains_atomic())
Eric Anholt9feb4032012-05-01 14:43:31 -07001511 continue;
1512
Tapani Pälli447bb902013-12-12 15:08:59 +02001513 unsigned int size = var->data.max_array_access;
Paul Berry665b8d72014-01-07 10:11:39 -08001514 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07001515 if (prog->_LinkedShaders[j] == NULL)
1516 continue;
1517
Eric Anholta721abf2010-08-23 10:32:01 -07001518 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1519 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1520 if (!other_var)
1521 continue;
1522
1523 if (strcmp(var->name, other_var->name) == 0 &&
Tapani Pälli447bb902013-12-12 15:08:59 +02001524 other_var->data.max_array_access > size) {
1525 size = other_var->data.max_array_access;
Eric Anholta721abf2010-08-23 10:32:01 -07001526 }
1527 }
1528 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001529
Fabian Bieler63684782013-06-14 13:37:07 +02001530 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001531 /* If this is a built-in uniform (i.e., it's backed by some
1532 * fixed-function state), adjust the number of state slots to
1533 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001534 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001535 * slots is an integer multiple of the number of array elements.
1536 * Determine the number of slots per array element by dividing by
1537 * the old (total) size.
1538 */
1539 if (var->num_state_slots > 0) {
1540 var->num_state_slots = (size + 1)
1541 * (var->num_state_slots / var->type->length);
1542 }
1543
Eric Anholta721abf2010-08-23 10:32:01 -07001544 var->type = glsl_type::get_array_instance(var->type->fields.array,
1545 size + 1);
1546 /* FINISHME: We should update the types of array
1547 * dereferences of this variable now.
1548 */
1549 }
1550 }
1551 }
1552}
1553
Ian Romanick69846702010-06-22 17:29:19 -07001554/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001555 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001556 *
1557 * \param used_mask Bits representing used (1) and unused (0) locations
1558 * \param needed_count Number of contiguous bits needed.
1559 *
1560 * \return
1561 * Base location of the available bits on success or -1 on failure.
1562 */
1563int
1564find_available_slots(unsigned used_mask, unsigned needed_count)
1565{
1566 unsigned needed_mask = (1 << needed_count) - 1;
1567 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1568
1569 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1570 * cannot optimize possibly infinite loops" for the loop below.
1571 */
1572 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1573 return -1;
1574
1575 for (int i = 0; i <= max_bit_to_test; i++) {
1576 if ((needed_mask & ~used_mask) == needed_mask)
1577 return i;
1578
1579 needed_mask <<= 1;
1580 }
1581
1582 return -1;
1583}
1584
1585
Ian Romanickd32d4f72011-06-27 17:59:58 -07001586/**
1587 * Assign locations for either VS inputs for FS outputs
1588 *
1589 * \param prog Shader program whose variables need locations assigned
1590 * \param target_index Selector for the program target to receive location
1591 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1592 * \c MESA_SHADER_FRAGMENT.
1593 * \param max_index Maximum number of generic locations. This corresponds
1594 * to either the maximum number of draw buffers or the
1595 * maximum number of generic attributes.
1596 *
1597 * \return
1598 * If locations are successfully assigned, true is returned. Otherwise an
1599 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001600 */
Ian Romanick69846702010-06-22 17:29:19 -07001601bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001602assign_attribute_or_color_locations(gl_shader_program *prog,
1603 unsigned target_index,
1604 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001605{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001606 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001607 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001608 unsigned used_locations = (max_index >= 32)
1609 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001610
Ian Romanickd32d4f72011-06-27 17:59:58 -07001611 assert((target_index == MESA_SHADER_VERTEX)
1612 || (target_index == MESA_SHADER_FRAGMENT));
1613
1614 gl_shader *const sh = prog->_LinkedShaders[target_index];
1615 if (sh == NULL)
1616 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001617
Ian Romanick69846702010-06-22 17:29:19 -07001618 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001619 *
1620 * 1. Invalidate the location assignments for all vertex shader inputs.
1621 *
1622 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001623 * glBindVertexAttribLocation) locations and outputs that have
1624 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001625 *
Ian Romanick69846702010-06-22 17:29:19 -07001626 * 3. Sort the attributes without assigned locations by number of slots
1627 * required in decreasing order. Fragmentation caused by attribute
1628 * locations assigned by the application may prevent large attributes
1629 * from having enough contiguous space.
1630 *
1631 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001632 */
1633
Ian Romanickd32d4f72011-06-27 17:59:58 -07001634 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001635 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001636
Ian Romanickd32d4f72011-06-27 17:59:58 -07001637 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001638 (target_index == MESA_SHADER_VERTEX)
1639 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001640
1641
Ian Romanick69846702010-06-22 17:29:19 -07001642 /* Temporary storage for the set of attributes that need locations assigned.
1643 */
1644 struct temp_attr {
1645 unsigned slots;
1646 ir_variable *var;
1647
1648 /* Used below in the call to qsort. */
1649 static int compare(const void *a, const void *b)
1650 {
1651 const temp_attr *const l = (const temp_attr *) a;
1652 const temp_attr *const r = (const temp_attr *) b;
1653
1654 /* Reversed because we want a descending order sort below. */
1655 return r->slots - l->slots;
1656 }
1657 } to_assign[16];
1658
1659 unsigned num_attr = 0;
1660
Eric Anholt16b68b12010-06-30 11:05:43 -07001661 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001662 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1663
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001664 if ((var == NULL) || (var->data.mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001665 continue;
1666
Tapani Pälli447bb902013-12-12 15:08:59 +02001667 if (var->data.explicit_location) {
1668 if ((var->data.location >= (int)(max_index + generic_base))
1669 || (var->data.location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001670 linker_error(prog,
1671 "invalid explicit location %d specified for `%s'\n",
Tapani Pälli447bb902013-12-12 15:08:59 +02001672 (var->data.location < 0)
1673 ? var->data.location
1674 : var->data.location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001675 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001676 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001677 }
1678 } else if (target_index == MESA_SHADER_VERTEX) {
1679 unsigned binding;
1680
1681 if (prog->AttributeBindings->get(binding, var->name)) {
1682 assert(binding >= VERT_ATTRIB_GENERIC0);
Tapani Pälli447bb902013-12-12 15:08:59 +02001683 var->data.location = binding;
1684 var->data.is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001685 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001686 } else if (target_index == MESA_SHADER_FRAGMENT) {
1687 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001688 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001689
1690 if (prog->FragDataBindings->get(binding, var->name)) {
1691 assert(binding >= FRAG_RESULT_DATA0);
Tapani Pälli447bb902013-12-12 15:08:59 +02001692 var->data.location = binding;
1693 var->data.is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001694
1695 if (prog->FragDataIndexBindings->get(index, var->name)) {
Tapani Pälli447bb902013-12-12 15:08:59 +02001696 var->data.index = index;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001697 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001698 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001699 }
1700
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001701 /* If the variable is not a built-in and has a location statically
1702 * assigned in the shader (presumably via a layout qualifier), make sure
1703 * that it doesn't collide with other assigned locations. Otherwise,
1704 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001705 */
Paul Berry0026ad42013-07-31 08:15:08 -07001706 const unsigned slots = var->type->count_attribute_slots();
Tapani Pälli447bb902013-12-12 15:08:59 +02001707 if (var->data.location != -1) {
1708 if (var->data.location >= generic_base && var->data.index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001709 /* From page 61 of the OpenGL 4.0 spec:
1710 *
1711 * "LinkProgram will fail if the attribute bindings assigned
1712 * by BindAttribLocation do not leave not enough space to
1713 * assign a location for an active matrix attribute or an
1714 * active attribute array, both of which require multiple
1715 * contiguous generic attributes."
1716 *
1717 * Previous versions of the spec contain similar language but omit
1718 * the bit about attribute arrays.
1719 *
1720 * Page 61 of the OpenGL 4.0 spec also says:
1721 *
1722 * "It is possible for an application to bind more than one
1723 * attribute name to the same location. This is referred to as
1724 * aliasing. This will only work if only one of the aliased
1725 * attributes is active in the executable program, or if no
1726 * path through the shader consumes more than one attribute of
1727 * a set of attributes aliased to the same location. A link
1728 * error can occur if the linker determines that every path
1729 * through the shader consumes multiple aliased attributes,
1730 * but implementations are not required to generate an error
1731 * in this case."
1732 *
1733 * These two paragraphs are either somewhat contradictory, or I
1734 * don't fully understand one or both of them.
1735 */
1736 /* FINISHME: The code as currently written does not support
1737 * FINISHME: attribute location aliasing (see comment above).
1738 */
1739 /* Mask representing the contiguous slots that will be used by
1740 * this attribute.
1741 */
Tapani Pälli447bb902013-12-12 15:08:59 +02001742 const unsigned attr = var->data.location - generic_base;
Ian Romanick523b6112011-08-17 15:40:03 -07001743 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001744
Ian Romanick523b6112011-08-17 15:40:03 -07001745 /* Generate a link error if the set of bits requested for this
1746 * attribute overlaps any previously allocated bits.
1747 */
1748 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001749 const char *const string = (target_index == MESA_SHADER_VERTEX)
1750 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001751 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001752 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001753 "available for %s `%s' %d %d %d", string,
1754 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001755 return false;
1756 }
1757
1758 used_locations |= (use_mask << attr);
1759 }
1760
1761 continue;
1762 }
1763
1764 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001765 to_assign[num_attr].var = var;
1766 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001767 }
Ian Romanick69846702010-06-22 17:29:19 -07001768
1769 /* If all of the attributes were assigned locations by the application (or
1770 * are built-in attributes with fixed locations), return early. This should
1771 * be the common case.
1772 */
1773 if (num_attr == 0)
1774 return true;
1775
1776 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1777
Ian Romanickd32d4f72011-06-27 17:59:58 -07001778 if (target_index == MESA_SHADER_VERTEX) {
1779 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1780 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1781 * reserved to prevent it from being automatically allocated below.
1782 */
1783 find_deref_visitor find("gl_Vertex");
1784 find.run(sh->ir);
1785 if (find.variable_found())
1786 used_locations |= (1 << 0);
1787 }
Ian Romanick982e3792010-06-29 18:58:20 -07001788
Ian Romanick69846702010-06-22 17:29:19 -07001789 for (unsigned i = 0; i < num_attr; i++) {
1790 /* Mask representing the contiguous slots that will be used by this
1791 * attribute.
1792 */
1793 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1794
1795 int location = find_available_slots(used_locations, to_assign[i].slots);
1796
1797 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001798 const char *const string = (target_index == MESA_SHADER_VERTEX)
1799 ? "vertex shader input" : "fragment shader output";
1800
Ian Romanick586e7412011-07-28 14:04:09 -07001801 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001802 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001803 "available for %s `%s'",
1804 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001805 return false;
1806 }
1807
Tapani Pälli447bb902013-12-12 15:08:59 +02001808 to_assign[i].var->data.location = generic_base + location;
1809 to_assign[i].var->data.is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001810 used_locations |= (use_mask << location);
1811 }
1812
1813 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001814}
1815
1816
Ian Romanick40e114b2010-08-17 14:55:50 -07001817/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001818 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001819 */
1820void
Ian Romanickcc90e622010-10-19 17:59:10 -07001821demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001822{
1823 foreach_list(node, sh->ir) {
1824 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1825
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001826 if ((var == NULL) || (var->data.mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001827 continue;
1828
Ian Romanickcc90e622010-10-19 17:59:10 -07001829 /* A shader 'in' or 'out' variable is only really an input or output if
1830 * its value is used by other shader stages. This will cause the variable
1831 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001832 */
Tapani Pälli447bb902013-12-12 15:08:59 +02001833 if (var->data.is_unmatched_generic_inout) {
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001834 var->data.mode = ir_var_auto;
Ian Romanick40e114b2010-08-17 14:55:50 -07001835 }
1836 }
1837}
1838
1839
Paul Berry871ddb92011-11-05 11:17:32 -07001840/**
Marek Olšákec174a42011-11-18 15:00:10 +01001841 * Store the gl_FragDepth layout in the gl_shader_program struct.
1842 */
1843static void
1844store_fragdepth_layout(struct gl_shader_program *prog)
1845{
1846 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1847 return;
1848 }
1849
1850 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1851
1852 /* We don't look up the gl_FragDepth symbol directly because if
1853 * gl_FragDepth is not used in the shader, it's removed from the IR.
1854 * However, the symbol won't be removed from the symbol table.
1855 *
1856 * We're only interested in the cases where the variable is NOT removed
1857 * from the IR.
1858 */
1859 foreach_list(node, ir) {
1860 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1861
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001862 if (var == NULL || var->data.mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001863 continue;
1864 }
1865
1866 if (strcmp(var->name, "gl_FragDepth") == 0) {
Tapani Pälli447bb902013-12-12 15:08:59 +02001867 switch (var->data.depth_layout) {
Marek Olšákec174a42011-11-18 15:00:10 +01001868 case ir_depth_layout_none:
1869 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1870 return;
1871 case ir_depth_layout_any:
1872 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1873 return;
1874 case ir_depth_layout_greater:
1875 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1876 return;
1877 case ir_depth_layout_less:
1878 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1879 return;
1880 case ir_depth_layout_unchanged:
1881 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1882 return;
1883 default:
1884 assert(0);
1885 return;
1886 }
1887 }
1888 }
1889}
1890
1891/**
Ian Romanick92f81592011-11-08 12:37:19 -08001892 * Validate the resources used by a program versus the implementation limits
1893 */
Paul Berryb95d2372013-07-27 11:08:31 -07001894static void
Ian Romanick92f81592011-11-08 12:37:19 -08001895check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1896{
Paul Berry99e822f2013-12-17 09:54:38 -08001897 const unsigned max_samplers[] = {
Marek Olšák5e784332013-05-02 02:30:44 +02001898 ctx->Const.VertexProgram.MaxTextureImageUnits,
Marek Olšák030ca232013-06-12 17:15:46 +02001899 ctx->Const.GeometryProgram.MaxTextureImageUnits,
1900 ctx->Const.FragmentProgram.MaxTextureImageUnits
Ian Romanick92f81592011-11-08 12:37:19 -08001901 };
Paul Berry665b8d72014-01-07 10:11:39 -08001902 STATIC_ASSERT(Elements(max_samplers) == MESA_SHADER_STAGES);
Ian Romanick92f81592011-11-08 12:37:19 -08001903
Paul Berry99e822f2013-12-17 09:54:38 -08001904 const unsigned max_default_uniform_components[] = {
Ian Romanick92f81592011-11-08 12:37:19 -08001905 ctx->Const.VertexProgram.MaxUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001906 ctx->Const.GeometryProgram.MaxUniformComponents,
1907 ctx->Const.FragmentProgram.MaxUniformComponents
Ian Romanick92f81592011-11-08 12:37:19 -08001908 };
Paul Berry99e822f2013-12-17 09:54:38 -08001909 STATIC_ASSERT(Elements(max_default_uniform_components) ==
Paul Berry665b8d72014-01-07 10:11:39 -08001910 MESA_SHADER_STAGES);
Ian Romanick92f81592011-11-08 12:37:19 -08001911
Paul Berry99e822f2013-12-17 09:54:38 -08001912 const unsigned max_combined_uniform_components[] = {
Eric Anholt38e77e52013-05-23 11:10:15 -07001913 ctx->Const.VertexProgram.MaxCombinedUniformComponents,
Marek Olšák030ca232013-06-12 17:15:46 +02001914 ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
1915 ctx->Const.FragmentProgram.MaxCombinedUniformComponents
Eric Anholt38e77e52013-05-23 11:10:15 -07001916 };
Paul Berry99e822f2013-12-17 09:54:38 -08001917 STATIC_ASSERT(Elements(max_combined_uniform_components) ==
Paul Berry665b8d72014-01-07 10:11:39 -08001918 MESA_SHADER_STAGES);
Eric Anholt38e77e52013-05-23 11:10:15 -07001919
Paul Berry99e822f2013-12-17 09:54:38 -08001920 const unsigned max_uniform_blocks[] = {
Eric Anholt877a8972012-06-25 12:47:01 -07001921 ctx->Const.VertexProgram.MaxUniformBlocks,
Eric Anholt877a8972012-06-25 12:47:01 -07001922 ctx->Const.GeometryProgram.MaxUniformBlocks,
Marek Olšák030ca232013-06-12 17:15:46 +02001923 ctx->Const.FragmentProgram.MaxUniformBlocks
Eric Anholt877a8972012-06-25 12:47:01 -07001924 };
Paul Berry665b8d72014-01-07 10:11:39 -08001925 STATIC_ASSERT(Elements(max_uniform_blocks) == MESA_SHADER_STAGES);
Eric Anholt877a8972012-06-25 12:47:01 -07001926
Paul Berry665b8d72014-01-07 10:11:39 -08001927 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick92f81592011-11-08 12:37:19 -08001928 struct gl_shader *sh = prog->_LinkedShaders[i];
1929
1930 if (sh == NULL)
1931 continue;
1932
1933 if (sh->num_samplers > max_samplers[i]) {
1934 linker_error(prog, "Too many %s shader texture samplers",
Paul Berry665b8d72014-01-07 10:11:39 -08001935 _mesa_shader_stage_to_string(i));
Ian Romanick92f81592011-11-08 12:37:19 -08001936 }
1937
Eric Anholt38e77e52013-05-23 11:10:15 -07001938 if (sh->num_uniform_components > max_default_uniform_components[i]) {
1939 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1940 linker_warning(prog, "Too many %s shader default uniform block "
1941 "components, but the driver will try to optimize "
1942 "them out; this is non-portable out-of-spec "
1943 "behavior\n",
Paul Berry665b8d72014-01-07 10:11:39 -08001944 _mesa_shader_stage_to_string(i));
Eric Anholt38e77e52013-05-23 11:10:15 -07001945 } else {
1946 linker_error(prog, "Too many %s shader default uniform block "
1947 "components",
Paul Berry665b8d72014-01-07 10:11:39 -08001948 _mesa_shader_stage_to_string(i));
Eric Anholt38e77e52013-05-23 11:10:15 -07001949 }
1950 }
1951
1952 if (sh->num_combined_uniform_components >
1953 max_combined_uniform_components[i]) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001954 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1955 linker_warning(prog, "Too many %s shader uniform components, "
1956 "but the driver will try to optimize them out; "
1957 "this is non-portable out-of-spec behavior\n",
Paul Berry665b8d72014-01-07 10:11:39 -08001958 _mesa_shader_stage_to_string(i));
Marek Olšákdf809ae2011-12-10 04:14:46 +01001959 } else {
1960 linker_error(prog, "Too many %s shader uniform components",
Paul Berry665b8d72014-01-07 10:11:39 -08001961 _mesa_shader_stage_to_string(i));
Marek Olšákdf809ae2011-12-10 04:14:46 +01001962 }
Ian Romanick92f81592011-11-08 12:37:19 -08001963 }
1964 }
1965
Paul Berry665b8d72014-01-07 10:11:39 -08001966 unsigned blocks[MESA_SHADER_STAGES] = {0};
Eric Anholt877a8972012-06-25 12:47:01 -07001967 unsigned total_uniform_blocks = 0;
1968
1969 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
Paul Berry665b8d72014-01-07 10:11:39 -08001970 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
Eric Anholt877a8972012-06-25 12:47:01 -07001971 if (prog->UniformBlockStageIndex[j][i] != -1) {
1972 blocks[j]++;
1973 total_uniform_blocks++;
1974 }
1975 }
1976
1977 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1978 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1979 prog->NumUniformBlocks,
1980 ctx->Const.MaxCombinedUniformBlocks);
1981 } else {
Paul Berry665b8d72014-01-07 10:11:39 -08001982 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholt877a8972012-06-25 12:47:01 -07001983 if (blocks[i] > max_uniform_blocks[i]) {
1984 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
Paul Berry665b8d72014-01-07 10:11:39 -08001985 _mesa_shader_stage_to_string(i),
Eric Anholt877a8972012-06-25 12:47:01 -07001986 blocks[i],
1987 max_uniform_blocks[i]);
1988 break;
1989 }
1990 }
1991 }
1992 }
Ian Romanick92f81592011-11-08 12:37:19 -08001993}
Paul Berry871ddb92011-11-05 11:17:32 -07001994
Ian Romanick0e59b262010-06-23 11:23:01 -07001995void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001996link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001997{
Paul Berry871ddb92011-11-05 11:17:32 -07001998 tfeedback_decl *tfeedback_decls = NULL;
1999 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2000
Kenneth Graunked3073f52011-01-21 14:32:31 -08002001 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002002
Paul Berryb95d2372013-07-27 11:08:31 -07002003 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07002004 prog->Validated = false;
2005 prog->_Used = false;
2006
Eric Anholtf609cf72012-04-27 13:52:56 -07002007 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08002008 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07002009
Eric Anholtf609cf72012-04-27 13:52:56 -07002010 ralloc_free(prog->UniformBlocks);
2011 prog->UniformBlocks = NULL;
2012 prog->NumUniformBlocks = 0;
Paul Berry665b8d72014-01-07 10:11:39 -08002013 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholtf609cf72012-04-27 13:52:56 -07002014 ralloc_free(prog->UniformBlockStageIndex[i]);
2015 prog->UniformBlockStageIndex[i] = NULL;
2016 }
2017
Francisco Jerez5c114932013-09-11 12:14:46 -07002018 ralloc_free(prog->AtomicBuffers);
2019 prog->AtomicBuffers = NULL;
2020 prog->NumAtomicBuffers = 0;
2021
Ian Romanick832dfa52010-06-17 15:04:20 -07002022 /* Separate the shaders into groups based on their type.
2023 */
Eric Anholt16b68b12010-06-30 11:05:43 -07002024 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002025 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07002026 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07002027 unsigned num_frag_shaders = 0;
Bryan Cain25480922013-02-15 09:46:50 -06002028 struct gl_shader **geom_shader_list;
2029 unsigned num_geom_shaders = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07002030
Eric Anholt16b68b12010-06-30 11:05:43 -07002031 vert_shader_list = (struct gl_shader **)
Paul Berry844bd712013-07-30 22:38:43 -07002032 calloc(prog->NumShaders, sizeof(struct gl_shader *));
2033 frag_shader_list = (struct gl_shader **)
2034 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Bryan Cain25480922013-02-15 09:46:50 -06002035 geom_shader_list = (struct gl_shader **)
2036 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002037
Ian Romanick25f51d32010-07-16 15:51:50 -07002038 unsigned min_version = UINT_MAX;
2039 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002040 const bool is_es_prog =
2041 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002042 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002043 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2044 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2045
Paul Berrya9f34dc2012-08-02 17:49:44 -07002046 if (prog->Shaders[i]->IsES != is_es_prog) {
2047 linker_error(prog, "all shaders must use same shading "
2048 "language version\n");
2049 goto done;
2050 }
2051
Paul Berrye3b86f02014-01-07 10:58:56 -08002052 switch (prog->Shaders[i]->Stage) {
2053 case MESA_SHADER_VERTEX:
Ian Romanick832dfa52010-06-17 15:04:20 -07002054 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2055 num_vert_shaders++;
2056 break;
Paul Berrye3b86f02014-01-07 10:58:56 -08002057 case MESA_SHADER_FRAGMENT:
Ian Romanick832dfa52010-06-17 15:04:20 -07002058 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2059 num_frag_shaders++;
2060 break;
Paul Berrye3b86f02014-01-07 10:58:56 -08002061 case MESA_SHADER_GEOMETRY:
Bryan Cain25480922013-02-15 09:46:50 -06002062 geom_shader_list[num_geom_shaders] = prog->Shaders[i];
2063 num_geom_shaders++;
Ian Romanick832dfa52010-06-17 15:04:20 -07002064 break;
2065 }
2066 }
2067
Paul Berry672fab02013-10-13 18:01:11 -07002068 /* In desktop GLSL, different shader versions may be linked together. In
2069 * GLSL ES, all shader versions must be the same.
Ian Romanick25f51d32010-07-16 15:51:50 -07002070 */
Paul Berry672fab02013-10-13 18:01:11 -07002071 if (is_es_prog && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002072 linker_error(prog, "all shaders must use same shading "
2073 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002074 goto done;
2075 }
2076
2077 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002078 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002079
Fabian Bielerbd85ba02013-05-24 23:26:54 +02002080 /* Geometry shaders have to be linked with vertex shaders.
2081 */
2082 if (num_geom_shaders > 0 && num_vert_shaders == 0) {
2083 linker_error(prog, "Geometry shader must be linked with "
2084 "vertex shader\n");
2085 goto done;
2086 }
2087
Paul Berry665b8d72014-01-07 10:11:39 -08002088 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002089 if (prog->_LinkedShaders[i] != NULL)
2090 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2091
2092 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002093 }
2094
Ian Romanickcd6764e2010-07-16 16:00:07 -07002095 /* Link all shaders for a particular stage and validate the result.
2096 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002097 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002098 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002099 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2100 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002101
Paul Berryb95d2372013-07-27 11:08:31 -07002102 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07002103 goto done;
2104
Paul Berryb95d2372013-07-27 11:08:31 -07002105 validate_vertex_shader_executable(prog, sh);
2106 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002107 goto done;
Paul Berry44b7ebe2013-10-23 12:55:24 -07002108 prog->LastClipDistanceArraySize = prog->Vert.ClipDistanceArraySize;
Ian Romanick3fb87872010-07-09 14:09:34 -07002109
Ian Romanick3322fba2010-10-14 13:28:42 -07002110 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2111 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002112 }
2113
2114 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002115 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002116 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2117 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002118
Paul Berryb95d2372013-07-27 11:08:31 -07002119 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07002120 goto done;
2121
Paul Berryb95d2372013-07-27 11:08:31 -07002122 validate_fragment_shader_executable(prog, sh);
2123 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002124 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002125
Ian Romanick3322fba2010-10-14 13:28:42 -07002126 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2127 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002128 }
2129
Bryan Cain25480922013-02-15 09:46:50 -06002130 if (num_geom_shaders > 0) {
2131 gl_shader *const sh =
2132 link_intrastage_shaders(mem_ctx, ctx, prog, geom_shader_list,
2133 num_geom_shaders);
2134
2135 if (!prog->LinkStatus)
2136 goto done;
2137
2138 validate_geometry_shader_executable(prog, sh);
2139 if (!prog->LinkStatus)
2140 goto done;
Paul Berry44b7ebe2013-10-23 12:55:24 -07002141 prog->LastClipDistanceArraySize = prog->Geom.ClipDistanceArraySize;
Bryan Cain25480922013-02-15 09:46:50 -06002142
2143 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
2144 sh);
2145 }
2146
Ian Romanick3ed850e2010-06-23 12:18:21 -07002147 /* Here begins the inter-stage linking phase. Some initial validation is
2148 * performed, then locations are assigned for uniforms, attributes, and
2149 * varyings.
2150 */
Paul Berryb95d2372013-07-27 11:08:31 -07002151 cross_validate_uniforms(prog);
2152 if (!prog->LinkStatus)
2153 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002154
Paul Berryb95d2372013-07-27 11:08:31 -07002155 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07002156
Paul Berry665b8d72014-01-07 10:11:39 -08002157 for (prev = 0; prev < MESA_SHADER_STAGES; prev++) {
Paul Berryb95d2372013-07-27 11:08:31 -07002158 if (prog->_LinkedShaders[prev] != NULL)
2159 break;
2160 }
Ian Romanick3322fba2010-10-14 13:28:42 -07002161
Paul Berryb95d2372013-07-27 11:08:31 -07002162 /* Validate the inputs of each stage with the output of the preceding
2163 * stage.
2164 */
Paul Berry665b8d72014-01-07 10:11:39 -08002165 for (unsigned i = prev + 1; i < MESA_SHADER_STAGES; i++) {
Paul Berryb95d2372013-07-27 11:08:31 -07002166 if (prog->_LinkedShaders[i] == NULL)
2167 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07002168
Paul Berry544e3122013-11-15 14:23:45 -08002169 validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev],
2170 prog->_LinkedShaders[i]);
Paul Berryb95d2372013-07-27 11:08:31 -07002171 if (!prog->LinkStatus)
2172 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002173
Paul Berryb95d2372013-07-27 11:08:31 -07002174 cross_validate_outputs_to_inputs(prog,
2175 prog->_LinkedShaders[prev],
2176 prog->_LinkedShaders[i]);
2177 if (!prog->LinkStatus)
2178 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07002179
Paul Berryb95d2372013-07-27 11:08:31 -07002180 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002181 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002182
Paul Berry544e3122013-11-15 14:23:45 -08002183 /* Cross-validate uniform blocks between shader stages */
2184 validate_interstage_uniform_blocks(prog, prog->_LinkedShaders,
Paul Berry665b8d72014-01-07 10:11:39 -08002185 MESA_SHADER_STAGES);
Paul Berry544e3122013-11-15 14:23:45 -08002186 if (!prog->LinkStatus)
2187 goto done;
Jordan Justen5ebf5472013-03-10 03:20:03 -07002188
Paul Berry665b8d72014-01-07 10:11:39 -08002189 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
Jordan Justen5ebf5472013-03-10 03:20:03 -07002190 if (prog->_LinkedShaders[i] != NULL)
2191 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
2192 }
2193
Eric Anholt3de13952012-05-04 13:08:46 -07002194 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2195 * it before optimization because we want most of the checks to get
2196 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002197 *
2198 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002199 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002200 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002201 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2202 if (sh) {
2203 lower_discard_flow(sh->ir);
2204 }
2205 }
2206
Eric Anholtf609cf72012-04-27 13:52:56 -07002207 if (!interstage_cross_validate_uniform_blocks(prog))
2208 goto done;
2209
Eric Anholt2f4fe152010-08-10 13:06:49 -07002210 /* Do common optimization before assigning storage for attributes,
2211 * uniforms, and varyings. Later optimization could possibly make
2212 * some of that unused.
2213 */
Paul Berry665b8d72014-01-07 10:11:39 -08002214 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002215 if (prog->_LinkedShaders[i] == NULL)
2216 continue;
2217
Ian Romanick02c5ae12011-07-11 10:46:01 -07002218 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2219 if (!prog->LinkStatus)
2220 goto done;
2221
Paul Berry18392442012-12-04 11:11:02 -08002222 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2223 lower_clip_distance(prog->_LinkedShaders[i]);
2224 }
Paul Berryc06e3252011-08-11 20:58:21 -07002225
Brian Paul7feabfe2012-03-20 17:43:12 -06002226 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2227
Kenneth Graunkeb7657402013-04-17 17:30:22 -07002228 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002229 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002230 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002231
Paul Berry50895d42012-12-05 07:17:07 -08002232 /* Mark all generic shader inputs and outputs as unpaired. */
2233 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2234 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002235 prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir);
Paul Berry50895d42012-12-05 07:17:07 -08002236 }
Bryan Cain25480922013-02-15 09:46:50 -06002237 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2238 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002239 prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir);
Bryan Cain25480922013-02-15 09:46:50 -06002240 }
Paul Berry50895d42012-12-05 07:17:07 -08002241 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2242 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002243 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir);
Paul Berry50895d42012-12-05 07:17:07 -08002244 }
2245
Ian Romanickd32d4f72011-06-27 17:59:58 -07002246 /* FINISHME: The value of the max_attribute_index parameter is
2247 * FINISHME: implementation dependent based on the value of
2248 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2249 * FINISHME: at least 16, so hardcode 16 for now.
2250 */
2251 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002252 goto done;
2253 }
2254
Dave Airlie1256a5d2012-03-24 13:33:41 +00002255 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002256 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002257 }
2258
Marek Olšák284d9542013-06-12 02:18:09 +02002259 unsigned first;
Paul Berry665b8d72014-01-07 10:11:39 -08002260 for (first = 0; first < MESA_SHADER_STAGES; first++) {
Marek Olšák284d9542013-06-12 02:18:09 +02002261 if (prog->_LinkedShaders[first] != NULL)
Ian Romanick3322fba2010-10-14 13:28:42 -07002262 break;
2263 }
2264
Paul Berry871ddb92011-11-05 11:17:32 -07002265 if (num_tfeedback_decls != 0) {
2266 /* From GL_EXT_transform_feedback:
2267 * A program will fail to link if:
2268 *
2269 * * the <count> specified by TransformFeedbackVaryingsEXT is
2270 * non-zero, but the program object has no vertex or geometry
2271 * shader;
2272 */
Bryan Cain25480922013-02-15 09:46:50 -06002273 if (first == MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07002274 linker_error(prog, "Transform feedback varyings specified, but "
2275 "no vertex or geometry shader is present.");
2276 goto done;
2277 }
2278
2279 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2280 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002281 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002282 prog->TransformFeedback.VaryingNames,
2283 tfeedback_decls))
2284 goto done;
2285 }
2286
Marek Olšák284d9542013-06-12 02:18:09 +02002287 /* Linking the stages in the opposite order (from fragment to vertex)
2288 * ensures that inter-shader outputs written to in an earlier stage are
2289 * eliminated if they are (transitively) not used in a later stage.
2290 */
2291 int last, next;
Paul Berry665b8d72014-01-07 10:11:39 -08002292 for (last = MESA_SHADER_STAGES-1; last >= 0; last--) {
Marek Olšák284d9542013-06-12 02:18:09 +02002293 if (prog->_LinkedShaders[last] != NULL)
2294 break;
Ian Romanick3322fba2010-10-14 13:28:42 -07002295 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002296
Marek Olšák284d9542013-06-12 02:18:09 +02002297 if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
2298 gl_shader *const sh = prog->_LinkedShaders[last];
2299
2300 if (num_tfeedback_decls != 0) {
2301 /* There was no fragment shader, but we still have to assign varying
2302 * locations for use by transform feedback.
2303 */
2304 if (!assign_varying_locations(ctx, mem_ctx, prog,
2305 sh, NULL,
Paul Berry3b0cf702013-04-10 06:48:42 -07002306 num_tfeedback_decls, tfeedback_decls,
2307 0))
Marek Olšák284d9542013-06-12 02:18:09 +02002308 goto done;
2309 }
2310
Marek Olšákd13003f2013-08-09 22:34:45 +02002311 do_dead_builtin_varyings(ctx, sh, NULL,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002312 num_tfeedback_decls, tfeedback_decls);
2313
Marek Olšák284d9542013-06-12 02:18:09 +02002314 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
2315
2316 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07002317 */
Marek Olšák284d9542013-06-12 02:18:09 +02002318 while (do_dead_code(sh->ir, false))
2319 ;
2320 }
2321 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002322 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02002323 */
2324 gl_shader *const sh = prog->_LinkedShaders[first];
2325
Marek Olšákd13003f2013-08-09 22:34:45 +02002326 do_dead_builtin_varyings(ctx, NULL, sh,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002327 num_tfeedback_decls, tfeedback_decls);
2328
Marek Olšák284d9542013-06-12 02:18:09 +02002329 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
2330
2331 while (do_dead_code(sh->ir, false))
2332 ;
2333 }
2334
2335 next = last;
2336 for (int i = next - 1; i >= 0; i--) {
2337 if (prog->_LinkedShaders[i] == NULL)
2338 continue;
2339
2340 gl_shader *const sh_i = prog->_LinkedShaders[i];
2341 gl_shader *const sh_next = prog->_LinkedShaders[next];
Paul Berry3b0cf702013-04-10 06:48:42 -07002342 unsigned gs_input_vertices =
2343 next == MESA_SHADER_GEOMETRY ? prog->Geom.VerticesIn : 0;
Marek Olšák284d9542013-06-12 02:18:09 +02002344
2345 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
2346 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
Paul Berry3b0cf702013-04-10 06:48:42 -07002347 tfeedback_decls, gs_input_vertices))
Paul Berry871ddb92011-11-05 11:17:32 -07002348 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02002349
Marek Olšákd13003f2013-08-09 22:34:45 +02002350 do_dead_builtin_varyings(ctx, sh_i, sh_next,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002351 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2352 tfeedback_decls);
2353
Marek Olšák284d9542013-06-12 02:18:09 +02002354 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
2355 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
2356
2357 /* Eliminate code that is now dead due to unused outputs being demoted.
2358 */
2359 while (do_dead_code(sh_i->ir, false))
2360 ;
2361 while (do_dead_code(sh_next->ir, false))
2362 ;
2363
Marek Olšák3c555822013-06-13 03:17:22 +02002364 /* This must be done after all dead varyings are eliminated. */
Ian Romanick42305fb2013-09-10 12:00:34 -05002365 if (!check_against_output_limit(ctx, prog, sh_i))
2366 goto done;
2367 if (!check_against_input_limit(ctx, prog, sh_next))
Marek Olšák3c555822013-06-13 03:17:22 +02002368 goto done;
2369
Marek Olšák284d9542013-06-12 02:18:09 +02002370 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07002371 }
2372
2373 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2374 goto done;
2375
Ian Romanick960d7222011-10-21 11:21:02 -07002376 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002377 link_assign_uniform_locations(prog);
Francisco Jerez5c114932013-09-11 12:14:46 -07002378 link_assign_atomic_counter_resources(ctx, prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002379 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002380
Paul Berryb95d2372013-07-27 11:08:31 -07002381 check_resources(ctx, prog);
Francisco Jerez5c114932013-09-11 12:14:46 -07002382 link_check_atomic_counter_resources(ctx, prog);
2383
Paul Berryb95d2372013-07-27 11:08:31 -07002384 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08002385 goto done;
2386
Ian Romanickce9171f2011-02-03 17:10:14 -08002387 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002388 * present in a linked program. By checking prog->IsES, we also
2389 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002390 */
Eric Anholt57f79782011-07-22 12:57:47 -07002391 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002392 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002393 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002394 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002395 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002396 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002397 }
2398 }
2399
Ian Romanick13e10e42010-06-21 12:03:24 -07002400 /* FINISHME: Assign fragment shader output locations. */
2401
Ian Romanick832dfa52010-06-17 15:04:20 -07002402done:
2403 free(vert_shader_list);
Paul Berry844bd712013-07-30 22:38:43 -07002404 free(frag_shader_list);
Bryan Cain25480922013-02-15 09:46:50 -06002405 free(geom_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002406
Paul Berry665b8d72014-01-07 10:11:39 -08002407 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002408 if (prog->_LinkedShaders[i] == NULL)
2409 continue;
2410
Paul Berryd7fa9eb2013-11-22 12:37:22 -08002411 /* Do a final validation step to make sure that the IR wasn't
2412 * invalidated by any modifications performed after intrastage linking.
2413 */
2414 validate_ir_tree(prog->_LinkedShaders[i]->ir);
2415
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002416 /* Retain any live IR, but trash the rest. */
2417 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002418
2419 /* The symbol table in the linked shaders may contain references to
2420 * variables that were removed (e.g., unused uniforms). Since it may
2421 * contain junk, there is no possible valid use. Delete it and set the
2422 * pointer to NULL.
2423 */
2424 delete prog->_LinkedShaders[i]->symbols;
2425 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002426 }
2427
Kenneth Graunked3073f52011-01-21 14:32:31 -08002428 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002429}