blob: 85a4d388326f28d6ee31d94a9e05b80aa0362956 [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 Graunke48d0faa2014-01-10 16:39:17 -0800112 foreach_two_lists(formal_node, &ir->callee->parameters,
113 actual_node, &ir->actual_parameters) {
114 ir_rvalue *param_rval = (ir_rvalue *) actual_node;
115 ir_variable *sig_param = (ir_variable *) formal_node;
Eric Anholt18a60232010-08-23 11:29:25 -0700116
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 }
Eric Anholt18a60232010-08-23 11:29:25 -0700125 }
126
Kenneth Graunked884f602012-03-20 15:56:37 -0700127 if (ir->return_deref != NULL) {
128 ir_variable *const var = ir->return_deref->variable_referenced();
129
130 if (strcmp(name, var->name) == 0) {
131 found = true;
132 return visit_stop;
133 }
134 }
135
Eric Anholt18a60232010-08-23 11:29:25 -0700136 return visit_continue_with_parent;
137 }
138
Ian Romanick832dfa52010-06-17 15:04:20 -0700139 bool variable_found()
140 {
141 return found;
142 }
143
144private:
145 const char *name; /**< Find writes to a variable with this name. */
146 bool found; /**< Was a write to the variable found? */
147};
148
Ian Romanickc93b8f12010-06-17 15:20:22 -0700149
Ian Romanickc33e78f2010-08-13 12:30:41 -0700150/**
151 * Visitor that determines whether or not a variable is ever read.
152 */
153class find_deref_visitor : public ir_hierarchical_visitor {
154public:
155 find_deref_visitor(const char *name)
156 : name(name), found(false)
157 {
158 /* empty */
159 }
160
161 virtual ir_visitor_status visit(ir_dereference_variable *ir)
162 {
163 if (strcmp(this->name, ir->var->name) == 0) {
164 this->found = true;
165 return visit_stop;
166 }
167
168 return visit_continue;
169 }
170
171 bool variable_found() const
172 {
173 return this->found;
174 }
175
176private:
177 const char *name; /**< Find writes to a variable with this name. */
178 bool found; /**< Was a write to the variable found? */
179};
180
181
Paul Berry7cfefe62013-07-30 21:13:48 -0700182class geom_array_resize_visitor : public ir_hierarchical_visitor {
183public:
184 unsigned num_vertices;
185 gl_shader_program *prog;
186
187 geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
188 {
189 this->num_vertices = num_vertices;
190 this->prog = prog;
191 }
192
193 virtual ~geom_array_resize_visitor()
194 {
195 /* empty */
196 }
197
198 virtual ir_visitor_status visit(ir_variable *var)
199 {
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200200 if (!var->type->is_array() || var->data.mode != ir_var_shader_in)
Paul Berry7cfefe62013-07-30 21:13:48 -0700201 return visit_continue;
202
203 unsigned size = var->type->length;
204
205 /* Generate a link error if the shader has declared this array with an
206 * incorrect size.
207 */
208 if (size && size != this->num_vertices) {
209 linker_error(this->prog, "size of array %s declared as %u, "
210 "but number of input vertices is %u\n",
211 var->name, size, this->num_vertices);
212 return visit_continue;
213 }
214
215 /* Generate a link error if the shader attempts to access an input
216 * array using an index too large for its actual size assigned at link
217 * time.
218 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200219 if (var->data.max_array_access >= this->num_vertices) {
Paul Berry7cfefe62013-07-30 21:13:48 -0700220 linker_error(this->prog, "geometry shader accesses element %i of "
221 "%s, but only %i input vertices\n",
Tapani Pälli447bb902013-12-12 15:08:59 +0200222 var->data.max_array_access, var->name, this->num_vertices);
Paul Berry7cfefe62013-07-30 21:13:48 -0700223 return visit_continue;
224 }
225
226 var->type = glsl_type::get_array_instance(var->type->element_type(),
227 this->num_vertices);
Tapani Pälli447bb902013-12-12 15:08:59 +0200228 var->data.max_array_access = this->num_vertices - 1;
Paul Berry7cfefe62013-07-30 21:13:48 -0700229
230 return visit_continue;
231 }
232
233 /* Dereferences of input variables need to be updated so that their type
234 * matches the newly assigned type of the variable they are accessing. */
235 virtual ir_visitor_status visit(ir_dereference_variable *ir)
236 {
237 ir->type = ir->var->type;
238 return visit_continue;
239 }
240
241 /* Dereferences of 2D input arrays need to be updated so that their type
242 * matches the newly assigned type of the array they are accessing. */
243 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
244 {
245 const glsl_type *const vt = ir->array->type;
246 if (vt->is_array())
247 ir->type = vt->element_type();
248 return visit_continue;
249 }
250};
251
252
Paul Berry1a33e022013-08-18 20:59:37 -0700253/**
254 * Visitor that determines whether or not a shader uses ir_end_primitive.
255 */
256class find_end_primitive_visitor : public ir_hierarchical_visitor {
257public:
258 find_end_primitive_visitor()
259 : found(false)
260 {
261 /* empty */
262 }
263
264 virtual ir_visitor_status visit(ir_end_primitive *)
265 {
266 found = true;
267 return visit_stop;
268 }
269
270 bool end_primitive_found()
271 {
272 return found;
273 }
274
275private:
276 bool found;
277};
278
Eric Anholt10ef9492013-09-20 11:03:44 -0700279} /* anonymous namespace */
Paul Berry1a33e022013-08-18 20:59:37 -0700280
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700281void
Ian Romanick586e7412011-07-28 14:04:09 -0700282linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700283{
284 va_list ap;
285
Kenneth Graunked3073f52011-01-21 14:32:31 -0800286 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700287 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800288 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700289 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700290
291 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700292}
293
294
295void
Ian Romanick379a32f2011-07-28 14:09:06 -0700296linker_warning(gl_shader_program *prog, const char *fmt, ...)
297{
298 va_list ap;
299
300 ralloc_strcat(&prog->InfoLog, "error: ");
301 va_start(ap, fmt);
302 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
303 va_end(ap);
304
305}
306
307
Paul Berryb92900d2013-01-28 14:21:59 -0800308/**
309 * Given a string identifying a program resource, break it into a base name
310 * and an optional array index in square brackets.
311 *
312 * If an array index is present, \c out_base_name_end is set to point to the
313 * "[" that precedes the array index, and the array index itself is returned
314 * as a long.
315 *
316 * If no array index is present (or if the array index is negative or
317 * mal-formed), \c out_base_name_end, is set to point to the null terminator
318 * at the end of the input string, and -1 is returned.
319 *
320 * Only the final array index is parsed; if the string contains other array
321 * indices (or structure field accesses), they are left in the base name.
322 *
323 * No attempt is made to check that the base name is properly formed;
324 * typically the caller will look up the base name in a hash table, so
325 * ill-formed base names simply turn into hash table lookup failures.
326 */
327long
328parse_program_resource_name(const GLchar *name,
329 const GLchar **out_base_name_end)
330{
331 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
332 *
333 * "When an integer array element or block instance number is part of
334 * the name string, it will be specified in decimal form without a "+"
335 * or "-" sign or any extra leading zeroes. Additionally, the name
336 * string will not include white space anywhere in the string."
337 */
338
339 const size_t len = strlen(name);
340 *out_base_name_end = name + len;
341
342 if (len == 0 || name[len-1] != ']')
343 return -1;
344
345 /* Walk backwards over the string looking for a non-digit character. This
346 * had better be the opening bracket for an array index.
347 *
348 * Initially, i specifies the location of the ']'. Since the string may
349 * contain only the ']' charcater, walk backwards very carefully.
350 */
351 unsigned i;
352 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
353 /* empty */ ;
354
355 if ((i == 0) || name[i-1] != '[')
356 return -1;
357
358 long array_index = strtol(&name[i], NULL, 10);
359 if (array_index < 0)
360 return -1;
361
362 *out_base_name_end = name + (i - 1);
363 return array_index;
364}
365
366
Ian Romanick379a32f2011-07-28 14:09:06 -0700367void
Ian Romanick63974c02013-10-04 10:46:29 -0700368link_invalidate_variable_locations(exec_list *ir)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700369{
Ian Romanickcf8b14c2013-10-22 15:07:00 -0700370 foreach_list(node, ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700371 ir_variable *const var = ((ir_instruction *) node)->as_variable();
372
Paul Berry50895d42012-12-05 07:17:07 -0800373 if (var == NULL)
374 continue;
375
Ian Romanick63974c02013-10-04 10:46:29 -0700376 /* Only assign locations for variables that lack an explicit location.
377 * Explicit locations are set for all built-in variables, generic vertex
378 * shader inputs (via layout(location=...)), and generic fragment shader
379 * outputs (also via layout(location=...)).
380 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200381 if (!var->data.explicit_location) {
382 var->data.location = -1;
383 var->data.location_frac = 0;
Paul Berry50895d42012-12-05 07:17:07 -0800384 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700385
Ian Romanick63974c02013-10-04 10:46:29 -0700386 /* ir_variable::is_unmatched_generic_inout is used by the linker while
387 * connecting outputs from one stage to inputs of the next stage.
388 *
389 * There are two implicit assumptions here. First, we assume that any
390 * built-in variable (i.e., non-generic in or out) will have
391 * explicit_location set. Second, we assume that any generic in or out
392 * will not have explicit_location set.
393 *
394 * This second assumption will only be valid until
395 * GL_ARB_separate_shader_objects is supported. When that extension is
396 * implemented, this function will need some modifications.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700397 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200398 if (!var->data.explicit_location) {
399 var->data.is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800400 } else {
Tapani Pälli447bb902013-12-12 15:08:59 +0200401 var->data.is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800402 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700403 }
404}
405
406
Ian Romanickc93b8f12010-06-17 15:20:22 -0700407/**
Paul Berry44e07de2013-06-11 14:11:05 -0700408 * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
409 *
410 * Also check for errors based on incorrect usage of gl_ClipVertex and
411 * gl_ClipDistance.
412 *
413 * Return false if an error was reported.
414 */
415static void
Paul Berryb30e25f2013-12-17 09:49:43 -0800416analyze_clip_usage(struct gl_shader_program *prog,
Paul Berry44e07de2013-06-11 14:11:05 -0700417 struct gl_shader *shader, GLboolean *UsesClipDistance,
418 GLuint *ClipDistanceArraySize)
419{
420 *ClipDistanceArraySize = 0;
421
422 if (!prog->IsES && prog->Version >= 130) {
423 /* From section 7.1 (Vertex Shader Special Variables) of the
424 * GLSL 1.30 spec:
425 *
426 * "It is an error for a shader to statically write both
427 * gl_ClipVertex and gl_ClipDistance."
428 *
429 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
430 * gl_ClipVertex nor gl_ClipDistance.
431 */
432 find_assignment_visitor clip_vertex("gl_ClipVertex");
433 find_assignment_visitor clip_distance("gl_ClipDistance");
434
435 clip_vertex.run(shader->ir);
436 clip_distance.run(shader->ir);
437 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
438 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
Paul Berryb30e25f2013-12-17 09:49:43 -0800439 "and `gl_ClipDistance'\n",
Paul Berrye3b86f02014-01-07 10:58:56 -0800440 _mesa_shader_stage_to_string(shader->Stage));
Paul Berry44e07de2013-06-11 14:11:05 -0700441 return;
442 }
443 *UsesClipDistance = clip_distance.variable_found();
444 ir_variable *clip_distance_var =
445 shader->symbols->get_variable("gl_ClipDistance");
446 if (clip_distance_var)
447 *ClipDistanceArraySize = clip_distance_var->type->length;
448 } else {
449 *UsesClipDistance = false;
450 }
451}
452
453
454/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700455 * Verify that a vertex shader executable meets all semantic requirements.
456 *
Paul Berry642e5b412012-01-04 13:57:52 -0800457 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
458 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700459 *
460 * \param shader Vertex shader executable to be verified
461 */
Paul Berryb95d2372013-07-27 11:08:31 -0700462void
Eric Anholt849e1812010-06-30 11:49:17 -0700463validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700464 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700465{
466 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700467 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700468
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700469 /* From the GLSL 1.10 spec, page 48:
470 *
471 * "The variable gl_Position is available only in the vertex
472 * language and is intended for writing the homogeneous vertex
473 * position. All executions of a well-formed vertex shader
474 * executable must write a value into this variable. [...] The
475 * variable gl_Position is available only in the vertex
476 * language and is intended for writing the homogeneous vertex
477 * position. All executions of a well-formed vertex shader
478 * executable must write a value into this variable."
479 *
480 * while in GLSL 1.40 this text is changed to:
481 *
482 * "The variable gl_Position is available only in the vertex
483 * language and is intended for writing the homogeneous vertex
484 * position. It can be written at any time during shader
485 * execution. It may also be read back by a vertex shader
486 * after being written. This value will be used by primitive
487 * assembly, clipping, culling, and other fixed functionality
488 * operations, if present, that operate on primitives after
489 * vertex processing has occurred. Its value is undefined if
490 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700491 *
492 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
493 * not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700494 */
Paul Berry15ba2a52012-08-02 17:51:02 -0700495 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700496 find_assignment_visitor find("gl_Position");
497 find.run(shader->ir);
498 if (!find.variable_found()) {
499 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
Paul Berryb95d2372013-07-27 11:08:31 -0700500 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700501 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700502 }
503
Paul Berryb30e25f2013-12-17 09:49:43 -0800504 analyze_clip_usage(prog, shader, &prog->Vert.UsesClipDistance,
Paul Berry44e07de2013-06-11 14:11:05 -0700505 &prog->Vert.ClipDistanceArraySize);
Ian Romanick832dfa52010-06-17 15:04:20 -0700506}
507
508
Ian Romanickc93b8f12010-06-17 15:20:22 -0700509/**
510 * Verify that a fragment shader executable meets all semantic requirements
511 *
512 * \param shader Fragment shader executable to be verified
513 */
Paul Berryb95d2372013-07-27 11:08:31 -0700514void
Eric Anholt849e1812010-06-30 11:49:17 -0700515validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700516 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700517{
518 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700519 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700520
Ian Romanick832dfa52010-06-17 15:04:20 -0700521 find_assignment_visitor frag_color("gl_FragColor");
522 find_assignment_visitor frag_data("gl_FragData");
523
Eric Anholt16b68b12010-06-30 11:05:43 -0700524 frag_color.run(shader->ir);
525 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700526
Ian Romanick832dfa52010-06-17 15:04:20 -0700527 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700528 linker_error(prog, "fragment shader writes to both "
529 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700530 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700531}
532
Bryan Cain25480922013-02-15 09:46:50 -0600533/**
534 * Verify that a geometry shader executable meets all semantic requirements
535 *
Paul Berry44e07de2013-06-11 14:11:05 -0700536 * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
537 * prog->Geom.ClipDistanceArraySize as a side effect.
Bryan Cain25480922013-02-15 09:46:50 -0600538 *
539 * \param shader Geometry shader executable to be verified
540 */
541void
542validate_geometry_shader_executable(struct gl_shader_program *prog,
543 struct gl_shader *shader)
544{
545 if (shader == NULL)
546 return;
547
548 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
549 prog->Geom.VerticesIn = num_vertices;
Paul Berry44e07de2013-06-11 14:11:05 -0700550
Paul Berryb30e25f2013-12-17 09:49:43 -0800551 analyze_clip_usage(prog, shader, &prog->Geom.UsesClipDistance,
Paul Berry44e07de2013-06-11 14:11:05 -0700552 &prog->Geom.ClipDistanceArraySize);
Paul Berry1a33e022013-08-18 20:59:37 -0700553
554 find_end_primitive_visitor end_primitive;
555 end_primitive.run(shader->ir);
556 prog->Geom.UsesEndPrimitive = end_primitive.end_primitive_found();
Bryan Cain25480922013-02-15 09:46:50 -0600557}
558
Ian Romanick832dfa52010-06-17 15:04:20 -0700559
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700560/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700561 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700562 */
Paul Berryb95d2372013-07-27 11:08:31 -0700563void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700564cross_validate_globals(struct gl_shader_program *prog,
565 struct gl_shader **shader_list,
566 unsigned num_shaders,
567 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700568{
569 /* Examine all of the uniforms in all of the shaders and cross validate
570 * them.
571 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700572 glsl_symbol_table variables;
573 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700574 if (shader_list[i] == NULL)
575 continue;
576
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700577 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700578 ir_variable *const var = ((ir_instruction *) node)->as_variable();
579
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700580 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700581 continue;
582
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200583 if (uniforms_only && (var->data.mode != ir_var_uniform))
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700584 continue;
585
Ian Romanick7e2aa912010-07-19 17:12:42 -0700586 /* Don't cross validate temporaries that are at global scope. These
587 * will eventually get pulled into the shaders 'main'.
588 */
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200589 if (var->data.mode == ir_var_temporary)
Ian Romanick7e2aa912010-07-19 17:12:42 -0700590 continue;
591
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700592 /* If a global with this name has already been seen, verify that the
593 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700594 * initializers, the values of the initializers must be the same.
595 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700596 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700597 if (existing != NULL) {
598 if (var->type != existing->type) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700599 /* Consider the types to be "the same" if both types are arrays
600 * of the same type and one of the arrays is implicitly sized.
601 * In addition, set the type of the linked variable to the
602 * explicitly sized array.
603 */
604 if (var->type->is_array()
605 && existing->type->is_array()
606 && (var->type->fields.array == existing->type->fields.array)
607 && ((var->type->length == 0)
608 || (existing->type->length == 0))) {
Ian Romanick0f4b2a02011-01-25 12:06:18 -0800609 if (var->type->length != 0) {
Ian Romanicka2711d62010-08-29 22:07:49 -0700610 existing->type = var->type;
Ian Romanick6f539212010-12-07 18:30:33 -0800611 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700612 } else {
Ian Romanick586e7412011-07-28 14:04:09 -0700613 linker_error(prog, "%s `%s' declared as type "
614 "`%s' and type `%s'\n",
615 mode_string(var),
616 var->name, var->type->name,
617 existing->type->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700618 return;
Ian Romanicka2711d62010-08-29 22:07:49 -0700619 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700620 }
621
Tapani Pälli447bb902013-12-12 15:08:59 +0200622 if (var->data.explicit_location) {
623 if (existing->data.explicit_location
624 && (var->data.location != existing->data.location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700625 linker_error(prog, "explicit locations for %s "
626 "`%s' have differing values\n",
627 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700628 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700629 }
630
Tapani Pälli447bb902013-12-12 15:08:59 +0200631 existing->data.location = var->data.location;
632 existing->data.explicit_location = true;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700633 }
634
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700635 /* From the GLSL 4.20 specification:
636 * "A link error will result if two compilation units in a program
637 * specify different integer-constant bindings for the same
638 * opaque-uniform name. However, it is not an error to specify a
639 * binding on some but not all declarations for the same name"
640 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200641 if (var->data.explicit_binding) {
642 if (existing->data.explicit_binding &&
643 var->data.binding != existing->data.binding) {
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700644 linker_error(prog, "explicit bindings for %s "
645 "`%s' have differing values\n",
646 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700647 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700648 }
649
Tapani Pälli447bb902013-12-12 15:08:59 +0200650 existing->data.binding = var->data.binding;
651 existing->data.explicit_binding = true;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700652 }
653
Francisco Jerez5c114932013-09-11 12:14:46 -0700654 if (var->type->contains_atomic() &&
Tapani Pälli447bb902013-12-12 15:08:59 +0200655 var->data.atomic.offset != existing->data.atomic.offset) {
Francisco Jerez5c114932013-09-11 12:14:46 -0700656 linker_error(prog, "offset specifications for %s "
657 "`%s' have differing values\n",
658 mode_string(var), var->name);
659 return;
660 }
661
Ian Romanick46173f92011-10-31 13:07:06 -0700662 /* Validate layout qualifiers for gl_FragDepth.
663 *
664 * From the AMD/ARB_conservative_depth specs:
665 *
666 * "If gl_FragDepth is redeclared in any fragment shader in a
667 * program, it must be redeclared in all fragment shaders in
668 * that program that have static assignments to
669 * gl_FragDepth. All redeclarations of gl_FragDepth in all
670 * fragment shaders in a single program must have the same set
671 * of qualifiers."
672 */
673 if (strcmp(var->name, "gl_FragDepth") == 0) {
Tapani Pälli447bb902013-12-12 15:08:59 +0200674 bool layout_declared = var->data.depth_layout != ir_depth_layout_none;
Ian Romanick46173f92011-10-31 13:07:06 -0700675 bool layout_differs =
Tapani Pälli447bb902013-12-12 15:08:59 +0200676 var->data.depth_layout != existing->data.depth_layout;
Ian Romanick46173f92011-10-31 13:07:06 -0700677
678 if (layout_declared && layout_differs) {
679 linker_error(prog,
680 "All redeclarations of gl_FragDepth in all "
681 "fragment shaders in a single program must have "
682 "the same set of qualifiers.");
683 }
684
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200685 if (var->data.used && layout_differs) {
Ian Romanick46173f92011-10-31 13:07:06 -0700686 linker_error(prog,
687 "If gl_FragDepth is redeclared with a layout "
688 "qualifier in any fragment shader, it must be "
689 "redeclared with the same layout qualifier in "
690 "all fragment shaders that have assignments to "
691 "gl_FragDepth");
692 }
693 }
Chad Versaceaddae332011-01-27 01:40:31 -0800694
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700695 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
696 *
697 * "If a shared global has multiple initializers, the
698 * initializers must all be constant expressions, and they
699 * must all have the same value. Otherwise, a link error will
700 * result. (A shared global having only one initializer does
701 * not require that initializer to be a constant expression.)"
702 *
703 * Previous to 4.20 the GLSL spec simply said that initializers
704 * must have the same value. In this case of non-constant
705 * initializers, this was impossible to determine. As a result,
706 * no vendor actually implemented that behavior. The 4.20
707 * behavior matches the implemented behavior of at least one other
708 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700709 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700710 if (var->constant_initializer != NULL) {
711 if (existing->constant_initializer != NULL) {
712 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700713 linker_error(prog, "initializers for %s "
714 "`%s' have differing values\n",
715 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700716 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700717 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700718 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700719 /* If the first-seen instance of a particular uniform did not
720 * have an initializer but a later instance does, copy the
721 * initializer to the version stored in the symbol table.
722 */
Ian Romanickde415b72010-07-14 13:22:12 -0700723 /* FINISHME: This is wrong. The constant_value field should
724 * FINISHME: not be modified! Imagine a case where a shader
725 * FINISHME: without an initializer is linked in two different
726 * FINISHME: programs with shaders that have differing
727 * FINISHME: initializers. Linking with the first will
728 * FINISHME: modify the shader, and linking with the second
729 * FINISHME: will fail.
730 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700731 existing->constant_initializer =
732 var->constant_initializer->clone(ralloc_parent(existing),
733 NULL);
734 }
735 }
736
Tapani Pälli447bb902013-12-12 15:08:59 +0200737 if (var->data.has_initializer) {
738 if (existing->data.has_initializer
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700739 && (var->constant_initializer == NULL
740 || existing->constant_initializer == NULL)) {
741 linker_error(prog,
742 "shared global variable `%s' has multiple "
743 "non-constant initializers.\n",
744 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700745 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700746 }
747
748 /* Some instance had an initializer, so keep track of that. In
749 * this location, all sorts of initializers (constant or
750 * otherwise) will propagate the existence to the variable
751 * stored in the symbol table.
752 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200753 existing->data.has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700754 }
Chad Versace7528f142010-11-17 14:34:38 -0800755
Tapani Pällic1d30802013-12-12 12:57:57 +0200756 if (existing->data.invariant != var->data.invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -0700757 linker_error(prog, "declarations for %s `%s' have "
758 "mismatching invariant qualifiers\n",
759 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700760 return;
Chad Versace7528f142010-11-17 14:34:38 -0800761 }
Tapani Pällic1d30802013-12-12 12:57:57 +0200762 if (existing->data.centroid != var->data.centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -0700763 linker_error(prog, "declarations for %s `%s' have "
764 "mismatching centroid qualifiers\n",
765 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700766 return;
Chad Versace61428dd2011-01-10 15:29:30 -0800767 }
Tapani Pällic1d30802013-12-12 12:57:57 +0200768 if (existing->data.sample != var->data.sample) {
Chris Forbes51c5fc82013-11-29 21:26:10 +1300769 linker_error(prog, "declarations for %s `%s` have "
770 "mismatching sample qualifiers\n",
771 mode_string(var), var->name);
772 return;
773 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700774 } else
Eric Anholt001eee52010-11-05 06:11:24 -0700775 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700776 }
777 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700778}
779
780
Ian Romanick37101922010-06-18 19:02:10 -0700781/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700782 * Perform validation of uniforms used across multiple shader stages
783 */
Paul Berryb95d2372013-07-27 11:08:31 -0700784void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700785cross_validate_uniforms(struct gl_shader_program *prog)
786{
Paul Berryb95d2372013-07-27 11:08:31 -0700787 cross_validate_globals(prog, prog->_LinkedShaders,
Paul Berry665b8d72014-01-07 10:11:39 -0800788 MESA_SHADER_STAGES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700789}
790
Eric Anholtf609cf72012-04-27 13:52:56 -0700791/**
792 * Accumulates the array of prog->UniformBlocks and checks that all
793 * definitons of blocks agree on their contents.
794 */
795static bool
796interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
797{
798 unsigned max_num_uniform_blocks = 0;
Paul Berry665b8d72014-01-07 10:11:39 -0800799 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholtf609cf72012-04-27 13:52:56 -0700800 if (prog->_LinkedShaders[i])
801 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
802 }
803
Paul Berry665b8d72014-01-07 10:11:39 -0800804 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholtf609cf72012-04-27 13:52:56 -0700805 struct gl_shader *sh = prog->_LinkedShaders[i];
806
807 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
808 max_num_uniform_blocks);
809 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
810 prog->UniformBlockStageIndex[i][j] = -1;
811
812 if (sh == NULL)
813 continue;
814
815 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
816 int index = link_cross_validate_uniform_block(prog,
817 &prog->UniformBlocks,
818 &prog->NumUniformBlocks,
819 &sh->UniformBlocks[j]);
820
821 if (index == -1) {
822 linker_error(prog, "uniform block `%s' has mismatching definitions",
823 sh->UniformBlocks[j].Name);
824 return false;
825 }
826
827 prog->UniformBlockStageIndex[i][index] = j;
828 }
829 }
830
831 return true;
832}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700833
Ian Romanick37101922010-06-18 19:02:10 -0700834
Ian Romanick3fb87872010-07-09 14:09:34 -0700835/**
836 * Populates a shaders symbol table with all global declarations
837 */
838static void
839populate_symbol_table(gl_shader *sh)
840{
841 sh->symbols = new(sh) glsl_symbol_table;
842
843 foreach_list(node, sh->ir) {
844 ir_instruction *const inst = (ir_instruction *) node;
845 ir_variable *var;
846 ir_function *func;
847
848 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -0700849 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -0700850 } else if ((var = inst->as_variable()) != NULL) {
Eric Anholt001eee52010-11-05 06:11:24 -0700851 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -0700852 }
853 }
854}
855
856
857/**
Ian Romanick31a97862010-07-12 18:48:50 -0700858 * Remap variables referenced in an instruction tree
859 *
860 * This is used when instruction trees are cloned from one shader and placed in
861 * another. These trees will contain references to \c ir_variable nodes that
862 * do not exist in the target shader. This function finds these \c ir_variable
863 * references and replaces the references with matching variables in the target
864 * shader.
865 *
866 * If there is no matching variable in the target shader, a clone of the
867 * \c ir_variable is made and added to the target shader. The new variable is
868 * added to \b both the instruction stream and the symbol table.
869 *
870 * \param inst IR tree that is to be processed.
871 * \param symbols Symbol table containing global scope symbols in the
872 * linked shader.
873 * \param instructions Instruction stream where new variable declarations
874 * should be added.
875 */
876void
Eric Anholt8273bd42010-08-04 12:34:56 -0700877remap_variables(ir_instruction *inst, struct gl_shader *target,
878 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700879{
880 class remap_visitor : public ir_hierarchical_visitor {
881 public:
Eric Anholt8273bd42010-08-04 12:34:56 -0700882 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700883 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -0700884 {
Eric Anholt8273bd42010-08-04 12:34:56 -0700885 this->target = target;
886 this->symbols = target->symbols;
887 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700888 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700889 }
890
891 virtual ir_visitor_status visit(ir_dereference_variable *ir)
892 {
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200893 if (ir->var->data.mode == ir_var_temporary) {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700894 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
895
896 assert(var != NULL);
897 ir->var = var;
898 return visit_continue;
899 }
900
Ian Romanick31a97862010-07-12 18:48:50 -0700901 ir_variable *const existing =
902 this->symbols->get_variable(ir->var->name);
903 if (existing != NULL)
904 ir->var = existing;
905 else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700906 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -0700907
Eric Anholt001eee52010-11-05 06:11:24 -0700908 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -0700909 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700910 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -0700911 }
912
913 return visit_continue;
914 }
915
916 private:
Eric Anholt8273bd42010-08-04 12:34:56 -0700917 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -0700918 glsl_symbol_table *symbols;
919 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -0700920 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -0700921 };
922
Eric Anholt8273bd42010-08-04 12:34:56 -0700923 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700924
925 inst->accept(&v);
926}
927
928
929/**
930 * Move non-declarations from one instruction stream to another
931 *
932 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -0700933 * 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 -0700934 * pointer) for \c last and \c false for \c make_copies on the first
935 * call. Successive calls pass the return value of the previous call for
936 * \c last and \c true for \c make_copies.
937 *
938 * \param instructions Source instruction stream
939 * \param last Instruction after which new instructions should be
940 * inserted in the target instruction stream
941 * \param make_copies Flag selecting whether instructions in \c instructions
942 * should be copied (via \c ir_instruction::clone) into the
943 * target list or moved.
944 *
945 * \return
946 * The new "last" instruction in the target instruction stream. This pointer
947 * is suitable for use as the \c last parameter of a later call to this
948 * function.
949 */
950exec_node *
951move_non_declarations(exec_list *instructions, exec_node *last,
952 bool make_copies, gl_shader *target)
953{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700954 hash_table *temps = NULL;
955
956 if (make_copies)
957 temps = hash_table_ctor(0, hash_table_pointer_hash,
958 hash_table_pointer_compare);
959
Ian Romanick303c99f2010-07-19 12:34:56 -0700960 foreach_list_safe(node, instructions) {
Ian Romanick31a97862010-07-12 18:48:50 -0700961 ir_instruction *inst = (ir_instruction *) node;
962
Ian Romanick7e2aa912010-07-19 17:12:42 -0700963 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -0700964 continue;
965
Ian Romanick7e2aa912010-07-19 17:12:42 -0700966 ir_variable *var = inst->as_variable();
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200967 if ((var != NULL) && (var->data.mode != ir_var_temporary))
Ian Romanick7e2aa912010-07-19 17:12:42 -0700968 continue;
969
970 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -0700971 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -0700972 || inst->as_if() /* for initializers with the ?: operator */
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200973 || ((var != NULL) && (var->data.mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -0700974
975 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -0700976 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -0700977
978 if (var != NULL)
979 hash_table_insert(temps, inst, var);
980 else
Eric Anholt8273bd42010-08-04 12:34:56 -0700981 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -0700982 } else {
983 inst->remove();
984 }
985
986 last->insert_after(inst);
987 last = inst;
988 }
989
Ian Romanick7e2aa912010-07-19 17:12:42 -0700990 if (make_copies)
991 hash_table_dtor(temps);
992
Ian Romanick31a97862010-07-12 18:48:50 -0700993 return last;
994}
995
996/**
Ian Romanick15ce87e2010-07-09 15:28:22 -0700997 * Get the function signature for main from a shader
998 */
999static ir_function_signature *
1000get_main_function_signature(gl_shader *sh)
1001{
1002 ir_function *const f = sh->symbols->get_function("main");
1003 if (f != NULL) {
1004 exec_list void_parameters;
1005
1006 /* Look for the 'void main()' signature and ensure that it's defined.
1007 * This keeps the linker from accidentally pick a shader that just
1008 * contains a prototype for main.
1009 *
1010 * We don't have to check for multiple definitions of main (in multiple
1011 * shaders) because that would have already been caught above.
1012 */
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001013 ir_function_signature *sig = f->matching_signature(NULL, &void_parameters);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001014 if ((sig != NULL) && sig->is_defined) {
1015 return sig;
1016 }
1017 }
1018
1019 return NULL;
1020}
1021
1022
1023/**
Brian Paul84a12732012-02-02 20:10:40 -07001024 * This class is only used in link_intrastage_shaders() below but declaring
1025 * it inside that function leads to compiler warnings with some versions of
1026 * gcc.
1027 */
1028class array_sizing_visitor : public ir_hierarchical_visitor {
1029public:
Paul Berry15e05b92013-09-25 14:07:37 -07001030 array_sizing_visitor()
1031 : mem_ctx(ralloc_context(NULL)),
1032 unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash,
1033 hash_table_pointer_compare))
1034 {
1035 }
1036
1037 ~array_sizing_visitor()
1038 {
1039 hash_table_dtor(this->unnamed_interfaces);
1040 ralloc_free(this->mem_ctx);
1041 }
1042
Brian Paul84a12732012-02-02 20:10:40 -07001043 virtual ir_visitor_status visit(ir_variable *var)
1044 {
Tapani Pälli447bb902013-12-12 15:08:59 +02001045 fixup_type(&var->type, var->data.max_array_access);
Paul Berrye2266692013-09-23 10:44:19 -07001046 if (var->type->is_interface()) {
1047 if (interface_contains_unsized_arrays(var->type)) {
1048 const glsl_type *new_type =
1049 resize_interface_members(var->type, var->max_ifc_array_access);
1050 var->type = new_type;
1051 var->change_interface_type(new_type);
1052 }
1053 } else if (var->type->is_array() &&
1054 var->type->fields.array->is_interface()) {
1055 if (interface_contains_unsized_arrays(var->type->fields.array)) {
1056 const glsl_type *new_type =
1057 resize_interface_members(var->type->fields.array,
1058 var->max_ifc_array_access);
1059 var->change_interface_type(new_type);
1060 var->type =
1061 glsl_type::get_array_instance(new_type, var->type->length);
1062 }
Paul Berry15e05b92013-09-25 14:07:37 -07001063 } else if (const glsl_type *ifc_type = var->get_interface_type()) {
1064 /* Store a pointer to the variable in the unnamed_interfaces
1065 * hashtable.
1066 */
1067 ir_variable **interface_vars = (ir_variable **)
1068 hash_table_find(this->unnamed_interfaces, ifc_type);
1069 if (interface_vars == NULL) {
1070 interface_vars = rzalloc_array(mem_ctx, ir_variable *,
1071 ifc_type->length);
1072 hash_table_insert(this->unnamed_interfaces, interface_vars,
1073 ifc_type);
1074 }
1075 unsigned index = ifc_type->field_index(var->name);
1076 assert(index < ifc_type->length);
1077 assert(interface_vars[index] == NULL);
1078 interface_vars[index] = var;
Brian Paul84a12732012-02-02 20:10:40 -07001079 }
1080 return visit_continue;
1081 }
Paul Berrye2266692013-09-23 10:44:19 -07001082
Paul Berry15e05b92013-09-25 14:07:37 -07001083 /**
1084 * For each unnamed interface block that was discovered while running the
1085 * visitor, adjust the interface type to reflect the newly assigned array
1086 * sizes, and fix up the ir_variable nodes to point to the new interface
1087 * type.
1088 */
1089 void fixup_unnamed_interface_types()
1090 {
1091 hash_table_call_foreach(this->unnamed_interfaces,
1092 fixup_unnamed_interface_type, NULL);
1093 }
1094
Paul Berrye2266692013-09-23 10:44:19 -07001095private:
1096 /**
1097 * If the type pointed to by \c type represents an unsized array, replace
1098 * it with a sized array whose size is determined by max_array_access.
1099 */
1100 static void fixup_type(const glsl_type **type, unsigned max_array_access)
1101 {
Timothy Arcerib59c5922013-10-23 21:31:27 +11001102 if ((*type)->is_unsized_array()) {
Paul Berrye2266692013-09-23 10:44:19 -07001103 *type = glsl_type::get_array_instance((*type)->fields.array,
1104 max_array_access + 1);
1105 assert(*type != NULL);
1106 }
1107 }
1108
1109 /**
1110 * Determine whether the given interface type contains unsized arrays (if
1111 * it doesn't, array_sizing_visitor doesn't need to process it).
1112 */
1113 static bool interface_contains_unsized_arrays(const glsl_type *type)
1114 {
1115 for (unsigned i = 0; i < type->length; i++) {
1116 const glsl_type *elem_type = type->fields.structure[i].type;
Timothy Arcerib59c5922013-10-23 21:31:27 +11001117 if (elem_type->is_unsized_array())
Paul Berrye2266692013-09-23 10:44:19 -07001118 return true;
1119 }
1120 return false;
1121 }
1122
1123 /**
1124 * Create a new interface type based on the given type, with unsized arrays
1125 * replaced by sized arrays whose size is determined by
1126 * max_ifc_array_access.
1127 */
1128 static const glsl_type *
1129 resize_interface_members(const glsl_type *type,
1130 const unsigned *max_ifc_array_access)
1131 {
1132 unsigned num_fields = type->length;
1133 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1134 memcpy(fields, type->fields.structure,
1135 num_fields * sizeof(*fields));
1136 for (unsigned i = 0; i < num_fields; i++) {
1137 fixup_type(&fields[i].type, max_ifc_array_access[i]);
1138 }
1139 glsl_interface_packing packing =
1140 (glsl_interface_packing) type->interface_packing;
1141 const glsl_type *new_ifc_type =
1142 glsl_type::get_interface_instance(fields, num_fields,
1143 packing, type->name);
1144 delete [] fields;
1145 return new_ifc_type;
1146 }
Paul Berry15e05b92013-09-25 14:07:37 -07001147
1148 static void fixup_unnamed_interface_type(const void *key, void *data,
1149 void *)
1150 {
1151 const glsl_type *ifc_type = (const glsl_type *) key;
1152 ir_variable **interface_vars = (ir_variable **) data;
1153 unsigned num_fields = ifc_type->length;
1154 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1155 memcpy(fields, ifc_type->fields.structure,
1156 num_fields * sizeof(*fields));
1157 bool interface_type_changed = false;
1158 for (unsigned i = 0; i < num_fields; i++) {
1159 if (interface_vars[i] != NULL &&
1160 fields[i].type != interface_vars[i]->type) {
1161 fields[i].type = interface_vars[i]->type;
1162 interface_type_changed = true;
1163 }
1164 }
1165 if (!interface_type_changed) {
1166 delete [] fields;
1167 return;
1168 }
1169 glsl_interface_packing packing =
1170 (glsl_interface_packing) ifc_type->interface_packing;
1171 const glsl_type *new_ifc_type =
1172 glsl_type::get_interface_instance(fields, num_fields, packing,
1173 ifc_type->name);
1174 delete [] fields;
1175 for (unsigned i = 0; i < num_fields; i++) {
1176 if (interface_vars[i] != NULL)
1177 interface_vars[i]->change_interface_type(new_ifc_type);
1178 }
1179 }
1180
1181 /**
1182 * Memory context used to allocate the data in \c unnamed_interfaces.
1183 */
1184 void *mem_ctx;
1185
1186 /**
1187 * Hash table from const glsl_type * to an array of ir_variable *'s
1188 * pointing to the ir_variables constituting each unnamed interface block.
1189 */
1190 hash_table *unnamed_interfaces;
Brian Paul84a12732012-02-02 20:10:40 -07001191};
1192
Brian Paul84a12732012-02-02 20:10:40 -07001193/**
Eric Anholt6065a872013-06-12 18:12:40 -07001194 * Performs the cross-validation of geometry shader max_vertices and
1195 * primitive type layout qualifiers for the attached geometry shaders,
1196 * and propagates them to the linked GS and linked shader program.
1197 */
1198static void
1199link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
1200 struct gl_shader *linked_shader,
1201 struct gl_shader **shader_list,
1202 unsigned num_shaders)
1203{
1204 linked_shader->Geom.VerticesOut = 0;
1205 linked_shader->Geom.InputType = PRIM_UNKNOWN;
1206 linked_shader->Geom.OutputType = PRIM_UNKNOWN;
1207
1208 /* No in/out qualifiers defined for anything but GLSL 1.50+
1209 * geometry shaders so far.
1210 */
Paul Berrye3b86f02014-01-07 10:58:56 -08001211 if (linked_shader->Stage != MESA_SHADER_GEOMETRY || prog->Version < 150)
Eric Anholt6065a872013-06-12 18:12:40 -07001212 return;
1213
1214 /* From the GLSL 1.50 spec, page 46:
1215 *
1216 * "All geometry shader output layout declarations in a program
1217 * must declare the same layout and same value for
1218 * max_vertices. There must be at least one geometry output
1219 * layout declaration somewhere in a program, but not all
1220 * geometry shaders (compilation units) are required to
1221 * declare it."
1222 */
1223
1224 for (unsigned i = 0; i < num_shaders; i++) {
1225 struct gl_shader *shader = shader_list[i];
1226
1227 if (shader->Geom.InputType != PRIM_UNKNOWN) {
1228 if (linked_shader->Geom.InputType != PRIM_UNKNOWN &&
1229 linked_shader->Geom.InputType != shader->Geom.InputType) {
1230 linker_error(prog, "geometry shader defined with conflicting "
1231 "input types\n");
1232 return;
1233 }
1234 linked_shader->Geom.InputType = shader->Geom.InputType;
1235 }
1236
1237 if (shader->Geom.OutputType != PRIM_UNKNOWN) {
1238 if (linked_shader->Geom.OutputType != PRIM_UNKNOWN &&
1239 linked_shader->Geom.OutputType != shader->Geom.OutputType) {
1240 linker_error(prog, "geometry shader defined with conflicting "
1241 "output types\n");
1242 return;
1243 }
1244 linked_shader->Geom.OutputType = shader->Geom.OutputType;
1245 }
1246
1247 if (shader->Geom.VerticesOut != 0) {
1248 if (linked_shader->Geom.VerticesOut != 0 &&
1249 linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) {
1250 linker_error(prog, "geometry shader defined with conflicting "
1251 "output vertex count (%d and %d)\n",
1252 linked_shader->Geom.VerticesOut,
1253 shader->Geom.VerticesOut);
1254 return;
1255 }
1256 linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut;
1257 }
1258 }
1259
1260 /* Just do the intrastage -> interstage propagation right now,
1261 * since we already know we're in the right type of shader program
1262 * for doing it.
1263 */
1264 if (linked_shader->Geom.InputType == PRIM_UNKNOWN) {
1265 linker_error(prog,
1266 "geometry shader didn't declare primitive input type\n");
1267 return;
1268 }
1269 prog->Geom.InputType = linked_shader->Geom.InputType;
1270
1271 if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) {
1272 linker_error(prog,
1273 "geometry shader didn't declare primitive output type\n");
1274 return;
1275 }
1276 prog->Geom.OutputType = linked_shader->Geom.OutputType;
1277
1278 if (linked_shader->Geom.VerticesOut == 0) {
1279 linker_error(prog,
1280 "geometry shader didn't declare max_vertices\n");
1281 return;
1282 }
1283 prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut;
1284}
1285
1286/**
Ian Romanick3fb87872010-07-09 14:09:34 -07001287 * Combine a group of shaders for a single stage to generate a linked shader
1288 *
1289 * \note
1290 * If this function is supplied a single shader, it is cloned, and the new
1291 * shader is returned.
1292 */
1293static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001294link_intrastage_shaders(void *mem_ctx,
1295 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -07001296 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -07001297 struct gl_shader **shader_list,
1298 unsigned num_shaders)
1299{
Eric Anholtf609cf72012-04-27 13:52:56 -07001300 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -07001301
Ian Romanick13f782c2010-06-29 18:53:38 -07001302 /* Check that global variables defined in multiple shaders are consistent.
1303 */
Paul Berryb95d2372013-07-27 11:08:31 -07001304 cross_validate_globals(prog, shader_list, num_shaders, false);
1305 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -07001306 return NULL;
1307
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001308 /* Check that interface blocks defined in multiple shaders are consistent.
1309 */
Paul Berryb95d2372013-07-27 11:08:31 -07001310 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
1311 num_shaders);
1312 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001313 return NULL;
1314
Paul Berry4682b9b2013-07-27 15:07:08 -07001315 /* Link up uniform blocks defined within this stage. */
1316 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -05001317 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
1318 &uniform_blocks);
Eric Anholtf609cf72012-04-27 13:52:56 -07001319
Ian Romanick13f782c2010-06-29 18:53:38 -07001320 /* Check that there is only a single definition of each function signature
1321 * across all shaders.
1322 */
1323 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1324 foreach_list(node, shader_list[i]->ir) {
1325 ir_function *const f = ((ir_instruction *) node)->as_function();
1326
1327 if (f == NULL)
1328 continue;
1329
1330 for (unsigned j = i + 1; j < num_shaders; j++) {
1331 ir_function *const other =
1332 shader_list[j]->symbols->get_function(f->name);
1333
1334 /* If the other shader has no function (and therefore no function
1335 * signatures) with the same name, skip to the next shader.
1336 */
1337 if (other == NULL)
1338 continue;
1339
Kenneth Graunke5f7e7782013-11-22 01:25:42 -08001340 foreach_list(n, &f->signatures) {
1341 ir_function_signature *sig = (ir_function_signature *) n;
Ian Romanick13f782c2010-06-29 18:53:38 -07001342
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001343 if (!sig->is_defined || sig->is_builtin())
Ian Romanick13f782c2010-06-29 18:53:38 -07001344 continue;
1345
1346 ir_function_signature *other_sig =
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001347 other->exact_matching_signature(NULL, &sig->parameters);
Ian Romanick13f782c2010-06-29 18:53:38 -07001348
1349 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001350 && !other_sig->is_builtin()) {
Ian Romanick586e7412011-07-28 14:04:09 -07001351 linker_error(prog, "function `%s' is multiply defined",
1352 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07001353 return NULL;
1354 }
1355 }
1356 }
1357 }
1358 }
1359
1360 /* Find the shader that defines main, and make a clone of it.
1361 *
1362 * Starting with the clone, search for undefined references. If one is
1363 * found, find the shader that defines it. Clone the reference and add
1364 * it to the shader. Repeat until there are no undefined references or
1365 * until a reference cannot be resolved.
1366 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07001367 gl_shader *main = NULL;
1368 for (unsigned i = 0; i < num_shaders; i++) {
1369 if (get_main_function_signature(shader_list[i]) != NULL) {
1370 main = shader_list[i];
1371 break;
1372 }
1373 }
Ian Romanick13f782c2010-06-29 18:53:38 -07001374
Ian Romanick15ce87e2010-07-09 15:28:22 -07001375 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07001376 linker_error(prog, "%s shader lacks `main'\n",
Paul Berrye3b86f02014-01-07 10:58:56 -08001377 _mesa_shader_stage_to_string(shader_list[0]->Stage));
Ian Romanick15ce87e2010-07-09 15:28:22 -07001378 return NULL;
1379 }
1380
Ian Romanick4a455952010-10-13 15:13:02 -07001381 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001382 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001383 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07001384
Eric Anholtf609cf72012-04-27 13:52:56 -07001385 linked->UniformBlocks = uniform_blocks;
1386 linked->NumUniformBlocks = num_uniform_blocks;
1387 ralloc_steal(linked, linked->UniformBlocks);
1388
Eric Anholt6065a872013-06-12 18:12:40 -07001389 link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
1390
Ian Romanick15ce87e2010-07-09 15:28:22 -07001391 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07001392
Ian Romanick31a97862010-07-12 18:48:50 -07001393 /* The a pointer to the main function in the final linked shader (i.e., the
1394 * copy of the original shader that contained the main function).
1395 */
1396 ir_function_signature *const main_sig = get_main_function_signature(linked);
1397
1398 /* Move any instructions other than variable declarations or function
1399 * declarations into main.
1400 */
Ian Romanick9303e352010-07-19 12:33:54 -07001401 exec_node *insertion_point =
1402 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1403 linked);
1404
Ian Romanick31a97862010-07-12 18:48:50 -07001405 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07001406 if (shader_list[i] == main)
1407 continue;
1408
Ian Romanick31a97862010-07-12 18:48:50 -07001409 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07001410 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07001411 }
1412
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001413 /* Check if any shader needs built-in functions. */
1414 bool need_builtins = false;
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001415 for (unsigned i = 0; i < num_shaders; i++) {
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001416 if (shader_list[i]->uses_builtin_functions) {
1417 need_builtins = true;
1418 break;
1419 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001420 }
1421
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001422 bool ok;
1423 if (need_builtins) {
1424 /* Make a temporary array one larger than shader_list, which will hold
1425 * the built-in function shader as well.
1426 */
1427 gl_shader **linking_shaders = (gl_shader **)
1428 calloc(num_shaders + 1, sizeof(gl_shader *));
1429 memcpy(linking_shaders, shader_list, num_shaders * sizeof(gl_shader *));
1430 linking_shaders[num_shaders] = _mesa_glsl_get_builtin_function_shader();
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001431
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001432 ok = link_function_calls(prog, linked, linking_shaders, num_shaders + 1);
1433
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001434 free(linking_shaders);
Kenneth Graunke5b331f62013-11-23 12:11:34 -08001435 } else {
1436 ok = link_function_calls(prog, linked, shader_list, num_shaders);
1437 }
1438
1439
1440 if (!ok) {
1441 ctx->Driver.DeleteShader(ctx, linked);
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001442 return NULL;
Ian Romanick4a455952010-10-13 15:13:02 -07001443 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07001444
Paul Berryc148ef62011-08-03 15:37:01 -07001445 /* At this point linked should contain all of the linked IR, so
1446 * validate it to make sure nothing went wrong.
1447 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001448 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07001449
Paul Berry7cfefe62013-07-30 21:13:48 -07001450 /* Set the size of geometry shader input arrays */
Paul Berrye3b86f02014-01-07 10:58:56 -08001451 if (linked->Stage == MESA_SHADER_GEOMETRY) {
Paul Berry7cfefe62013-07-30 21:13:48 -07001452 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
1453 geom_array_resize_visitor input_resize_visitor(num_vertices, prog);
Kenneth Graunke5f7e7782013-11-22 01:25:42 -08001454 foreach_list(n, linked->ir) {
1455 ir_instruction *ir = (ir_instruction *) n;
Paul Berry7cfefe62013-07-30 21:13:48 -07001456 ir->accept(&input_resize_visitor);
1457 }
1458 }
1459
Ian Romanickc87e9ef2011-01-25 12:04:08 -08001460 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08001461 * unspecified sizes have a size specified. The size is inferred from the
1462 * max_array_access field.
1463 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07001464 array_sizing_visitor v;
1465 v.run(linked->ir);
Paul Berry15e05b92013-09-25 14:07:37 -07001466 v.fixup_unnamed_interface_types();
Ian Romanick6f539212010-12-07 18:30:33 -08001467
Ian Romanick3fb87872010-07-09 14:09:34 -07001468 return linked;
1469}
1470
Eric Anholta721abf2010-08-23 10:32:01 -07001471/**
1472 * Update the sizes of linked shader uniform arrays to the maximum
1473 * array index used.
1474 *
1475 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1476 *
1477 * If one or more elements of an array are active,
1478 * GetActiveUniform will return the name of the array in name,
1479 * subject to the restrictions listed above. The type of the array
1480 * is returned in type. The size parameter contains the highest
1481 * array element index used, plus one. The compiler or linker
1482 * determines the highest index used. There will be only one
1483 * active uniform reported by the GL per uniform array.
1484
1485 */
1486static void
Eric Anholt586b4b52010-09-28 14:32:16 -07001487update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07001488{
Paul Berry665b8d72014-01-07 10:11:39 -08001489 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07001490 if (prog->_LinkedShaders[i] == NULL)
1491 continue;
1492
Eric Anholta721abf2010-08-23 10:32:01 -07001493 foreach_list(node, prog->_LinkedShaders[i]->ir) {
1494 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1495
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001496 if ((var == NULL) || (var->data.mode != ir_var_uniform) ||
Eric Anholta721abf2010-08-23 10:32:01 -07001497 !var->type->is_array())
1498 continue;
1499
Eric Anholt9feb4032012-05-01 14:43:31 -07001500 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1501 * will not be eliminated. Since we always do std140, just
1502 * don't resize arrays in UBOs.
Francisco Jerez5c114932013-09-11 12:14:46 -07001503 *
1504 * Atomic counters are supposed to get deterministic
1505 * locations assigned based on the declaration ordering and
1506 * sizes, array compaction would mess that up.
Eric Anholt9feb4032012-05-01 14:43:31 -07001507 */
Francisco Jerez5c114932013-09-11 12:14:46 -07001508 if (var->is_in_uniform_block() || var->type->contains_atomic())
Eric Anholt9feb4032012-05-01 14:43:31 -07001509 continue;
1510
Tapani Pälli447bb902013-12-12 15:08:59 +02001511 unsigned int size = var->data.max_array_access;
Paul Berry665b8d72014-01-07 10:11:39 -08001512 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07001513 if (prog->_LinkedShaders[j] == NULL)
1514 continue;
1515
Eric Anholta721abf2010-08-23 10:32:01 -07001516 foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1517 ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1518 if (!other_var)
1519 continue;
1520
1521 if (strcmp(var->name, other_var->name) == 0 &&
Tapani Pälli447bb902013-12-12 15:08:59 +02001522 other_var->data.max_array_access > size) {
1523 size = other_var->data.max_array_access;
Eric Anholta721abf2010-08-23 10:32:01 -07001524 }
1525 }
1526 }
Eric Anholt586b4b52010-09-28 14:32:16 -07001527
Fabian Bieler63684782013-06-14 13:37:07 +02001528 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08001529 /* If this is a built-in uniform (i.e., it's backed by some
1530 * fixed-function state), adjust the number of state slots to
1531 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05001532 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08001533 * slots is an integer multiple of the number of array elements.
1534 * Determine the number of slots per array element by dividing by
1535 * the old (total) size.
1536 */
1537 if (var->num_state_slots > 0) {
1538 var->num_state_slots = (size + 1)
1539 * (var->num_state_slots / var->type->length);
1540 }
1541
Eric Anholta721abf2010-08-23 10:32:01 -07001542 var->type = glsl_type::get_array_instance(var->type->fields.array,
1543 size + 1);
1544 /* FINISHME: We should update the types of array
1545 * dereferences of this variable now.
1546 */
1547 }
1548 }
1549 }
1550}
1551
Ian Romanick69846702010-06-22 17:29:19 -07001552/**
Bryan Cainf18a0862011-04-23 19:29:15 -05001553 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07001554 *
1555 * \param used_mask Bits representing used (1) and unused (0) locations
1556 * \param needed_count Number of contiguous bits needed.
1557 *
1558 * \return
1559 * Base location of the available bits on success or -1 on failure.
1560 */
1561int
1562find_available_slots(unsigned used_mask, unsigned needed_count)
1563{
1564 unsigned needed_mask = (1 << needed_count) - 1;
1565 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1566
1567 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1568 * cannot optimize possibly infinite loops" for the loop below.
1569 */
1570 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1571 return -1;
1572
1573 for (int i = 0; i <= max_bit_to_test; i++) {
1574 if ((needed_mask & ~used_mask) == needed_mask)
1575 return i;
1576
1577 needed_mask <<= 1;
1578 }
1579
1580 return -1;
1581}
1582
1583
Ian Romanickd32d4f72011-06-27 17:59:58 -07001584/**
1585 * Assign locations for either VS inputs for FS outputs
1586 *
1587 * \param prog Shader program whose variables need locations assigned
1588 * \param target_index Selector for the program target to receive location
1589 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1590 * \c MESA_SHADER_FRAGMENT.
1591 * \param max_index Maximum number of generic locations. This corresponds
1592 * to either the maximum number of draw buffers or the
1593 * maximum number of generic attributes.
1594 *
1595 * \return
1596 * If locations are successfully assigned, true is returned. Otherwise an
1597 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07001598 */
Ian Romanick69846702010-06-22 17:29:19 -07001599bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07001600assign_attribute_or_color_locations(gl_shader_program *prog,
1601 unsigned target_index,
1602 unsigned max_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001603{
Ian Romanickd32d4f72011-06-27 17:59:58 -07001604 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07001605 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07001606 unsigned used_locations = (max_index >= 32)
1607 ? ~0 : ~((1 << max_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001608
Ian Romanickd32d4f72011-06-27 17:59:58 -07001609 assert((target_index == MESA_SHADER_VERTEX)
1610 || (target_index == MESA_SHADER_FRAGMENT));
1611
1612 gl_shader *const sh = prog->_LinkedShaders[target_index];
1613 if (sh == NULL)
1614 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001615
Ian Romanick69846702010-06-22 17:29:19 -07001616 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001617 *
1618 * 1. Invalidate the location assignments for all vertex shader inputs.
1619 *
1620 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07001621 * glBindVertexAttribLocation) locations and outputs that have
1622 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001623 *
Ian Romanick69846702010-06-22 17:29:19 -07001624 * 3. Sort the attributes without assigned locations by number of slots
1625 * required in decreasing order. Fragmentation caused by attribute
1626 * locations assigned by the application may prevent large attributes
1627 * from having enough contiguous space.
1628 *
1629 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001630 */
1631
Ian Romanickd32d4f72011-06-27 17:59:58 -07001632 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06001633 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001634
Ian Romanickd32d4f72011-06-27 17:59:58 -07001635 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08001636 (target_index == MESA_SHADER_VERTEX)
1637 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07001638
1639
Ian Romanick69846702010-06-22 17:29:19 -07001640 /* Temporary storage for the set of attributes that need locations assigned.
1641 */
1642 struct temp_attr {
1643 unsigned slots;
1644 ir_variable *var;
1645
1646 /* Used below in the call to qsort. */
1647 static int compare(const void *a, const void *b)
1648 {
1649 const temp_attr *const l = (const temp_attr *) a;
1650 const temp_attr *const r = (const temp_attr *) b;
1651
1652 /* Reversed because we want a descending order sort below. */
1653 return r->slots - l->slots;
1654 }
1655 } to_assign[16];
1656
1657 unsigned num_attr = 0;
1658
Eric Anholt16b68b12010-06-30 11:05:43 -07001659 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001660 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1661
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001662 if ((var == NULL) || (var->data.mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001663 continue;
1664
Tapani Pälli447bb902013-12-12 15:08:59 +02001665 if (var->data.explicit_location) {
1666 if ((var->data.location >= (int)(max_index + generic_base))
1667 || (var->data.location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001668 linker_error(prog,
1669 "invalid explicit location %d specified for `%s'\n",
Tapani Pälli447bb902013-12-12 15:08:59 +02001670 (var->data.location < 0)
1671 ? var->data.location
1672 : var->data.location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07001673 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07001674 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07001675 }
1676 } else if (target_index == MESA_SHADER_VERTEX) {
1677 unsigned binding;
1678
1679 if (prog->AttributeBindings->get(binding, var->name)) {
1680 assert(binding >= VERT_ATTRIB_GENERIC0);
Tapani Pälli447bb902013-12-12 15:08:59 +02001681 var->data.location = binding;
1682 var->data.is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001683 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001684 } else if (target_index == MESA_SHADER_FRAGMENT) {
1685 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001686 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07001687
1688 if (prog->FragDataBindings->get(binding, var->name)) {
1689 assert(binding >= FRAG_RESULT_DATA0);
Tapani Pälli447bb902013-12-12 15:08:59 +02001690 var->data.location = binding;
1691 var->data.is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001692
1693 if (prog->FragDataIndexBindings->get(index, var->name)) {
Tapani Pälli447bb902013-12-12 15:08:59 +02001694 var->data.index = index;
Dave Airlie1256a5d2012-03-24 13:33:41 +00001695 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07001696 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07001697 }
1698
Ian Romanick9f0e98d2011-10-06 10:25:34 -07001699 /* If the variable is not a built-in and has a location statically
1700 * assigned in the shader (presumably via a layout qualifier), make sure
1701 * that it doesn't collide with other assigned locations. Otherwise,
1702 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001703 */
Paul Berry0026ad42013-07-31 08:15:08 -07001704 const unsigned slots = var->type->count_attribute_slots();
Tapani Pälli447bb902013-12-12 15:08:59 +02001705 if (var->data.location != -1) {
1706 if (var->data.location >= generic_base && var->data.index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07001707 /* From page 61 of the OpenGL 4.0 spec:
1708 *
1709 * "LinkProgram will fail if the attribute bindings assigned
1710 * by BindAttribLocation do not leave not enough space to
1711 * assign a location for an active matrix attribute or an
1712 * active attribute array, both of which require multiple
1713 * contiguous generic attributes."
1714 *
1715 * Previous versions of the spec contain similar language but omit
1716 * the bit about attribute arrays.
1717 *
1718 * Page 61 of the OpenGL 4.0 spec also says:
1719 *
1720 * "It is possible for an application to bind more than one
1721 * attribute name to the same location. This is referred to as
1722 * aliasing. This will only work if only one of the aliased
1723 * attributes is active in the executable program, or if no
1724 * path through the shader consumes more than one attribute of
1725 * a set of attributes aliased to the same location. A link
1726 * error can occur if the linker determines that every path
1727 * through the shader consumes multiple aliased attributes,
1728 * but implementations are not required to generate an error
1729 * in this case."
1730 *
1731 * These two paragraphs are either somewhat contradictory, or I
1732 * don't fully understand one or both of them.
1733 */
1734 /* FINISHME: The code as currently written does not support
1735 * FINISHME: attribute location aliasing (see comment above).
1736 */
1737 /* Mask representing the contiguous slots that will be used by
1738 * this attribute.
1739 */
Tapani Pälli447bb902013-12-12 15:08:59 +02001740 const unsigned attr = var->data.location - generic_base;
Ian Romanick523b6112011-08-17 15:40:03 -07001741 const unsigned use_mask = (1 << slots) - 1;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001742
Ian Romanick523b6112011-08-17 15:40:03 -07001743 /* Generate a link error if the set of bits requested for this
1744 * attribute overlaps any previously allocated bits.
1745 */
1746 if ((~(use_mask << attr) & used_locations) != used_locations) {
Dave Airlie7449ae42011-11-20 19:56:35 +00001747 const char *const string = (target_index == MESA_SHADER_VERTEX)
1748 ? "vertex shader input" : "fragment shader output";
Ian Romanick523b6112011-08-17 15:40:03 -07001749 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001750 "insufficient contiguous locations "
Dave Airlie1256a5d2012-03-24 13:33:41 +00001751 "available for %s `%s' %d %d %d", string,
1752 var->name, used_locations, use_mask, attr);
Ian Romanick523b6112011-08-17 15:40:03 -07001753 return false;
1754 }
1755
1756 used_locations |= (use_mask << attr);
1757 }
1758
1759 continue;
1760 }
1761
1762 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07001763 to_assign[num_attr].var = var;
1764 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001765 }
Ian Romanick69846702010-06-22 17:29:19 -07001766
1767 /* If all of the attributes were assigned locations by the application (or
1768 * are built-in attributes with fixed locations), return early. This should
1769 * be the common case.
1770 */
1771 if (num_attr == 0)
1772 return true;
1773
1774 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1775
Ian Romanickd32d4f72011-06-27 17:59:58 -07001776 if (target_index == MESA_SHADER_VERTEX) {
1777 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1778 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1779 * reserved to prevent it from being automatically allocated below.
1780 */
1781 find_deref_visitor find("gl_Vertex");
1782 find.run(sh->ir);
1783 if (find.variable_found())
1784 used_locations |= (1 << 0);
1785 }
Ian Romanick982e3792010-06-29 18:58:20 -07001786
Ian Romanick69846702010-06-22 17:29:19 -07001787 for (unsigned i = 0; i < num_attr; i++) {
1788 /* Mask representing the contiguous slots that will be used by this
1789 * attribute.
1790 */
1791 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1792
1793 int location = find_available_slots(used_locations, to_assign[i].slots);
1794
1795 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07001796 const char *const string = (target_index == MESA_SHADER_VERTEX)
1797 ? "vertex shader input" : "fragment shader output";
1798
Ian Romanick586e7412011-07-28 14:04:09 -07001799 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00001800 "insufficient contiguous locations "
Ian Romanick586e7412011-07-28 14:04:09 -07001801 "available for %s `%s'",
1802 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07001803 return false;
1804 }
1805
Tapani Pälli447bb902013-12-12 15:08:59 +02001806 to_assign[i].var->data.location = generic_base + location;
1807 to_assign[i].var->data.is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07001808 used_locations |= (use_mask << location);
1809 }
1810
1811 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001812}
1813
1814
Ian Romanick40e114b2010-08-17 14:55:50 -07001815/**
Ian Romanickcc90e622010-10-19 17:59:10 -07001816 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07001817 */
1818void
Ian Romanickcc90e622010-10-19 17:59:10 -07001819demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07001820{
1821 foreach_list(node, sh->ir) {
1822 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1823
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001824 if ((var == NULL) || (var->data.mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07001825 continue;
1826
Ian Romanickcc90e622010-10-19 17:59:10 -07001827 /* A shader 'in' or 'out' variable is only really an input or output if
1828 * its value is used by other shader stages. This will cause the variable
1829 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07001830 */
Tapani Pälli447bb902013-12-12 15:08:59 +02001831 if (var->data.is_unmatched_generic_inout) {
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001832 var->data.mode = ir_var_auto;
Ian Romanick40e114b2010-08-17 14:55:50 -07001833 }
1834 }
1835}
1836
1837
Paul Berry871ddb92011-11-05 11:17:32 -07001838/**
Marek Olšákec174a42011-11-18 15:00:10 +01001839 * Store the gl_FragDepth layout in the gl_shader_program struct.
1840 */
1841static void
1842store_fragdepth_layout(struct gl_shader_program *prog)
1843{
1844 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1845 return;
1846 }
1847
1848 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
1849
1850 /* We don't look up the gl_FragDepth symbol directly because if
1851 * gl_FragDepth is not used in the shader, it's removed from the IR.
1852 * However, the symbol won't be removed from the symbol table.
1853 *
1854 * We're only interested in the cases where the variable is NOT removed
1855 * from the IR.
1856 */
1857 foreach_list(node, ir) {
1858 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1859
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001860 if (var == NULL || var->data.mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01001861 continue;
1862 }
1863
1864 if (strcmp(var->name, "gl_FragDepth") == 0) {
Tapani Pälli447bb902013-12-12 15:08:59 +02001865 switch (var->data.depth_layout) {
Marek Olšákec174a42011-11-18 15:00:10 +01001866 case ir_depth_layout_none:
1867 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1868 return;
1869 case ir_depth_layout_any:
1870 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1871 return;
1872 case ir_depth_layout_greater:
1873 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1874 return;
1875 case ir_depth_layout_less:
1876 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1877 return;
1878 case ir_depth_layout_unchanged:
1879 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1880 return;
1881 default:
1882 assert(0);
1883 return;
1884 }
1885 }
1886 }
1887}
1888
1889/**
Ian Romanick92f81592011-11-08 12:37:19 -08001890 * Validate the resources used by a program versus the implementation limits
1891 */
Paul Berryb95d2372013-07-27 11:08:31 -07001892static void
Ian Romanick92f81592011-11-08 12:37:19 -08001893check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
1894{
Paul Berry665b8d72014-01-07 10:11:39 -08001895 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick92f81592011-11-08 12:37:19 -08001896 struct gl_shader *sh = prog->_LinkedShaders[i];
1897
1898 if (sh == NULL)
1899 continue;
1900
Paul Berrybce8bc02014-01-08 10:17:01 -08001901 if (sh->num_samplers > ctx->Const.Program[i].MaxTextureImageUnits) {
Ian Romanick92f81592011-11-08 12:37:19 -08001902 linker_error(prog, "Too many %s shader texture samplers",
Paul Berry665b8d72014-01-07 10:11:39 -08001903 _mesa_shader_stage_to_string(i));
Ian Romanick92f81592011-11-08 12:37:19 -08001904 }
1905
Paul Berrybce8bc02014-01-08 10:17:01 -08001906 if (sh->num_uniform_components >
1907 ctx->Const.Program[i].MaxUniformComponents) {
Eric Anholt38e77e52013-05-23 11:10:15 -07001908 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1909 linker_warning(prog, "Too many %s shader default uniform block "
1910 "components, but the driver will try to optimize "
1911 "them out; this is non-portable out-of-spec "
1912 "behavior\n",
Paul Berry665b8d72014-01-07 10:11:39 -08001913 _mesa_shader_stage_to_string(i));
Eric Anholt38e77e52013-05-23 11:10:15 -07001914 } else {
1915 linker_error(prog, "Too many %s shader default uniform block "
1916 "components",
Paul Berry665b8d72014-01-07 10:11:39 -08001917 _mesa_shader_stage_to_string(i));
Eric Anholt38e77e52013-05-23 11:10:15 -07001918 }
1919 }
1920
1921 if (sh->num_combined_uniform_components >
Paul Berrybce8bc02014-01-08 10:17:01 -08001922 ctx->Const.Program[i].MaxCombinedUniformComponents) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01001923 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
1924 linker_warning(prog, "Too many %s shader uniform components, "
1925 "but the driver will try to optimize them out; "
1926 "this is non-portable out-of-spec behavior\n",
Paul Berry665b8d72014-01-07 10:11:39 -08001927 _mesa_shader_stage_to_string(i));
Marek Olšákdf809ae2011-12-10 04:14:46 +01001928 } else {
1929 linker_error(prog, "Too many %s shader uniform components",
Paul Berry665b8d72014-01-07 10:11:39 -08001930 _mesa_shader_stage_to_string(i));
Marek Olšákdf809ae2011-12-10 04:14:46 +01001931 }
Ian Romanick92f81592011-11-08 12:37:19 -08001932 }
1933 }
1934
Paul Berry665b8d72014-01-07 10:11:39 -08001935 unsigned blocks[MESA_SHADER_STAGES] = {0};
Eric Anholt877a8972012-06-25 12:47:01 -07001936 unsigned total_uniform_blocks = 0;
1937
1938 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
Paul Berry665b8d72014-01-07 10:11:39 -08001939 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
Eric Anholt877a8972012-06-25 12:47:01 -07001940 if (prog->UniformBlockStageIndex[j][i] != -1) {
1941 blocks[j]++;
1942 total_uniform_blocks++;
1943 }
1944 }
1945
1946 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
1947 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
1948 prog->NumUniformBlocks,
1949 ctx->Const.MaxCombinedUniformBlocks);
1950 } else {
Paul Berry665b8d72014-01-07 10:11:39 -08001951 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Paul Berrybce8bc02014-01-08 10:17:01 -08001952 const unsigned max_uniform_blocks =
1953 ctx->Const.Program[i].MaxUniformBlocks;
1954 if (blocks[i] > max_uniform_blocks) {
Eric Anholt877a8972012-06-25 12:47:01 -07001955 linker_error(prog, "Too many %s uniform blocks (%d/%d)",
Paul Berry665b8d72014-01-07 10:11:39 -08001956 _mesa_shader_stage_to_string(i),
Eric Anholt877a8972012-06-25 12:47:01 -07001957 blocks[i],
Paul Berrybce8bc02014-01-08 10:17:01 -08001958 max_uniform_blocks);
Eric Anholt877a8972012-06-25 12:47:01 -07001959 break;
1960 }
1961 }
1962 }
1963 }
Ian Romanick92f81592011-11-08 12:37:19 -08001964}
Paul Berry871ddb92011-11-05 11:17:32 -07001965
Ian Romanick0e59b262010-06-23 11:23:01 -07001966void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001967link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07001968{
Paul Berry871ddb92011-11-05 11:17:32 -07001969 tfeedback_decl *tfeedback_decls = NULL;
1970 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
1971
Kenneth Graunked3073f52011-01-21 14:32:31 -08001972 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001973
Paul Berryb95d2372013-07-27 11:08:31 -07001974 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07001975 prog->Validated = false;
1976 prog->_Used = false;
1977
Eric Anholtf609cf72012-04-27 13:52:56 -07001978 ralloc_free(prog->InfoLog);
Kenneth Graunked3073f52011-01-21 14:32:31 -08001979 prog->InfoLog = ralloc_strdup(NULL, "");
Ian Romanickf36460e2010-06-23 12:07:22 -07001980
Eric Anholtf609cf72012-04-27 13:52:56 -07001981 ralloc_free(prog->UniformBlocks);
1982 prog->UniformBlocks = NULL;
1983 prog->NumUniformBlocks = 0;
Paul Berry665b8d72014-01-07 10:11:39 -08001984 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholtf609cf72012-04-27 13:52:56 -07001985 ralloc_free(prog->UniformBlockStageIndex[i]);
1986 prog->UniformBlockStageIndex[i] = NULL;
1987 }
1988
Francisco Jerez5c114932013-09-11 12:14:46 -07001989 ralloc_free(prog->AtomicBuffers);
1990 prog->AtomicBuffers = NULL;
1991 prog->NumAtomicBuffers = 0;
1992
Ian Romanick832dfa52010-06-17 15:04:20 -07001993 /* Separate the shaders into groups based on their type.
1994 */
Eric Anholt16b68b12010-06-30 11:05:43 -07001995 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001996 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -07001997 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -07001998 unsigned num_frag_shaders = 0;
Bryan Cain25480922013-02-15 09:46:50 -06001999 struct gl_shader **geom_shader_list;
2000 unsigned num_geom_shaders = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -07002001
Eric Anholt16b68b12010-06-30 11:05:43 -07002002 vert_shader_list = (struct gl_shader **)
Paul Berry844bd712013-07-30 22:38:43 -07002003 calloc(prog->NumShaders, sizeof(struct gl_shader *));
2004 frag_shader_list = (struct gl_shader **)
2005 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Bryan Cain25480922013-02-15 09:46:50 -06002006 geom_shader_list = (struct gl_shader **)
2007 calloc(prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -07002008
Ian Romanick25f51d32010-07-16 15:51:50 -07002009 unsigned min_version = UINT_MAX;
2010 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07002011 const bool is_es_prog =
2012 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07002013 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07002014 min_version = MIN2(min_version, prog->Shaders[i]->Version);
2015 max_version = MAX2(max_version, prog->Shaders[i]->Version);
2016
Paul Berrya9f34dc2012-08-02 17:49:44 -07002017 if (prog->Shaders[i]->IsES != is_es_prog) {
2018 linker_error(prog, "all shaders must use same shading "
2019 "language version\n");
2020 goto done;
2021 }
2022
Paul Berrye3b86f02014-01-07 10:58:56 -08002023 switch (prog->Shaders[i]->Stage) {
2024 case MESA_SHADER_VERTEX:
Ian Romanick832dfa52010-06-17 15:04:20 -07002025 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2026 num_vert_shaders++;
2027 break;
Paul Berrye3b86f02014-01-07 10:58:56 -08002028 case MESA_SHADER_FRAGMENT:
Ian Romanick832dfa52010-06-17 15:04:20 -07002029 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2030 num_frag_shaders++;
2031 break;
Paul Berrye3b86f02014-01-07 10:58:56 -08002032 case MESA_SHADER_GEOMETRY:
Bryan Cain25480922013-02-15 09:46:50 -06002033 geom_shader_list[num_geom_shaders] = prog->Shaders[i];
2034 num_geom_shaders++;
Ian Romanick832dfa52010-06-17 15:04:20 -07002035 break;
2036 }
2037 }
2038
Paul Berry672fab02013-10-13 18:01:11 -07002039 /* In desktop GLSL, different shader versions may be linked together. In
2040 * GLSL ES, all shader versions must be the same.
Ian Romanick25f51d32010-07-16 15:51:50 -07002041 */
Paul Berry672fab02013-10-13 18:01:11 -07002042 if (is_es_prog && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07002043 linker_error(prog, "all shaders must use same shading "
2044 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07002045 goto done;
2046 }
2047
2048 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07002049 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07002050
Fabian Bielerbd85ba02013-05-24 23:26:54 +02002051 /* Geometry shaders have to be linked with vertex shaders.
2052 */
2053 if (num_geom_shaders > 0 && num_vert_shaders == 0) {
2054 linker_error(prog, "Geometry shader must be linked with "
2055 "vertex shader\n");
2056 goto done;
2057 }
2058
Paul Berry665b8d72014-01-07 10:11:39 -08002059 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002060 if (prog->_LinkedShaders[i] != NULL)
2061 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2062
2063 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07002064 }
2065
Ian Romanickcd6764e2010-07-16 16:00:07 -07002066 /* Link all shaders for a particular stage and validate the result.
2067 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002068 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002069 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002070 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2071 num_vert_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002072
Paul Berryb95d2372013-07-27 11:08:31 -07002073 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07002074 goto done;
2075
Paul Berryb95d2372013-07-27 11:08:31 -07002076 validate_vertex_shader_executable(prog, sh);
2077 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002078 goto done;
Paul Berry44b7ebe2013-10-23 12:55:24 -07002079 prog->LastClipDistanceArraySize = prog->Vert.ClipDistanceArraySize;
Ian Romanick3fb87872010-07-09 14:09:34 -07002080
Ian Romanick3322fba2010-10-14 13:28:42 -07002081 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2082 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002083 }
2084
2085 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -07002086 gl_shader *const sh =
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002087 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2088 num_frag_shaders);
Ian Romanick3fb87872010-07-09 14:09:34 -07002089
Paul Berryb95d2372013-07-27 11:08:31 -07002090 if (!prog->LinkStatus)
Ian Romanick3fb87872010-07-09 14:09:34 -07002091 goto done;
2092
Paul Berryb95d2372013-07-27 11:08:31 -07002093 validate_fragment_shader_executable(prog, sh);
2094 if (!prog->LinkStatus)
Ian Romanickf29ff6e2010-10-14 17:55:17 -07002095 goto done;
Ian Romanick3fb87872010-07-09 14:09:34 -07002096
Ian Romanick3322fba2010-10-14 13:28:42 -07002097 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2098 sh);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07002099 }
2100
Bryan Cain25480922013-02-15 09:46:50 -06002101 if (num_geom_shaders > 0) {
2102 gl_shader *const sh =
2103 link_intrastage_shaders(mem_ctx, ctx, prog, geom_shader_list,
2104 num_geom_shaders);
2105
2106 if (!prog->LinkStatus)
2107 goto done;
2108
2109 validate_geometry_shader_executable(prog, sh);
2110 if (!prog->LinkStatus)
2111 goto done;
Paul Berry44b7ebe2013-10-23 12:55:24 -07002112 prog->LastClipDistanceArraySize = prog->Geom.ClipDistanceArraySize;
Bryan Cain25480922013-02-15 09:46:50 -06002113
2114 _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_GEOMETRY],
2115 sh);
2116 }
2117
Ian Romanick3ed850e2010-06-23 12:18:21 -07002118 /* Here begins the inter-stage linking phase. Some initial validation is
2119 * performed, then locations are assigned for uniforms, attributes, and
2120 * varyings.
2121 */
Paul Berryb95d2372013-07-27 11:08:31 -07002122 cross_validate_uniforms(prog);
2123 if (!prog->LinkStatus)
2124 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002125
Paul Berryb95d2372013-07-27 11:08:31 -07002126 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07002127
Paul Berry665b8d72014-01-07 10:11:39 -08002128 for (prev = 0; prev < MESA_SHADER_STAGES; prev++) {
Paul Berryb95d2372013-07-27 11:08:31 -07002129 if (prog->_LinkedShaders[prev] != NULL)
2130 break;
2131 }
Ian Romanick3322fba2010-10-14 13:28:42 -07002132
Paul Berryb95d2372013-07-27 11:08:31 -07002133 /* Validate the inputs of each stage with the output of the preceding
2134 * stage.
2135 */
Paul Berry665b8d72014-01-07 10:11:39 -08002136 for (unsigned i = prev + 1; i < MESA_SHADER_STAGES; i++) {
Paul Berryb95d2372013-07-27 11:08:31 -07002137 if (prog->_LinkedShaders[i] == NULL)
2138 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07002139
Paul Berry544e3122013-11-15 14:23:45 -08002140 validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev],
2141 prog->_LinkedShaders[i]);
Paul Berryb95d2372013-07-27 11:08:31 -07002142 if (!prog->LinkStatus)
2143 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07002144
Paul Berryb95d2372013-07-27 11:08:31 -07002145 cross_validate_outputs_to_inputs(prog,
2146 prog->_LinkedShaders[prev],
2147 prog->_LinkedShaders[i]);
2148 if (!prog->LinkStatus)
2149 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07002150
Paul Berryb95d2372013-07-27 11:08:31 -07002151 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07002152 }
Ian Romanick832dfa52010-06-17 15:04:20 -07002153
Paul Berry544e3122013-11-15 14:23:45 -08002154 /* Cross-validate uniform blocks between shader stages */
2155 validate_interstage_uniform_blocks(prog, prog->_LinkedShaders,
Paul Berry665b8d72014-01-07 10:11:39 -08002156 MESA_SHADER_STAGES);
Paul Berry544e3122013-11-15 14:23:45 -08002157 if (!prog->LinkStatus)
2158 goto done;
Jordan Justen5ebf5472013-03-10 03:20:03 -07002159
Paul Berry665b8d72014-01-07 10:11:39 -08002160 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
Jordan Justen5ebf5472013-03-10 03:20:03 -07002161 if (prog->_LinkedShaders[i] != NULL)
2162 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
2163 }
2164
Eric Anholt3de13952012-05-04 13:08:46 -07002165 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2166 * it before optimization because we want most of the checks to get
2167 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07002168 *
2169 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07002170 */
Paul Berry15ba2a52012-08-02 17:51:02 -07002171 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07002172 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2173 if (sh) {
2174 lower_discard_flow(sh->ir);
2175 }
2176 }
2177
Eric Anholtf609cf72012-04-27 13:52:56 -07002178 if (!interstage_cross_validate_uniform_blocks(prog))
2179 goto done;
2180
Eric Anholt2f4fe152010-08-10 13:06:49 -07002181 /* Do common optimization before assigning storage for attributes,
2182 * uniforms, and varyings. Later optimization could possibly make
2183 * some of that unused.
2184 */
Paul Berry665b8d72014-01-07 10:11:39 -08002185 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002186 if (prog->_LinkedShaders[i] == NULL)
2187 continue;
2188
Ian Romanick02c5ae12011-07-11 10:46:01 -07002189 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2190 if (!prog->LinkStatus)
2191 goto done;
2192
Paul Berry18392442012-12-04 11:11:02 -08002193 if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
2194 lower_clip_distance(prog->_LinkedShaders[i]);
2195 }
Paul Berryc06e3252011-08-11 20:58:21 -07002196
Brian Paul7feabfe2012-03-20 17:43:12 -06002197 unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2198
Kenneth Graunkeb7657402013-04-17 17:30:22 -07002199 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
Eric Anholt2f4fe152010-08-10 13:06:49 -07002200 ;
Ian Romanicka7ba9a72010-07-20 13:36:32 -07002201 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002202
Paul Berry50895d42012-12-05 07:17:07 -08002203 /* Mark all generic shader inputs and outputs as unpaired. */
2204 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2205 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002206 prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir);
Paul Berry50895d42012-12-05 07:17:07 -08002207 }
Bryan Cain25480922013-02-15 09:46:50 -06002208 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2209 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002210 prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir);
Bryan Cain25480922013-02-15 09:46:50 -06002211 }
Paul Berry50895d42012-12-05 07:17:07 -08002212 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2213 link_invalidate_variable_locations(
Ian Romanick63974c02013-10-04 10:46:29 -07002214 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir);
Paul Berry50895d42012-12-05 07:17:07 -08002215 }
2216
Ian Romanickd32d4f72011-06-27 17:59:58 -07002217 /* FINISHME: The value of the max_attribute_index parameter is
2218 * FINISHME: implementation dependent based on the value of
2219 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2220 * FINISHME: at least 16, so hardcode 16 for now.
2221 */
2222 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002223 goto done;
2224 }
2225
Dave Airlie1256a5d2012-03-24 13:33:41 +00002226 if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002227 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07002228 }
2229
Marek Olšák284d9542013-06-12 02:18:09 +02002230 unsigned first;
Paul Berry665b8d72014-01-07 10:11:39 -08002231 for (first = 0; first < MESA_SHADER_STAGES; first++) {
Marek Olšák284d9542013-06-12 02:18:09 +02002232 if (prog->_LinkedShaders[first] != NULL)
Ian Romanick3322fba2010-10-14 13:28:42 -07002233 break;
2234 }
2235
Paul Berry871ddb92011-11-05 11:17:32 -07002236 if (num_tfeedback_decls != 0) {
2237 /* From GL_EXT_transform_feedback:
2238 * A program will fail to link if:
2239 *
2240 * * the <count> specified by TransformFeedbackVaryingsEXT is
2241 * non-zero, but the program object has no vertex or geometry
2242 * shader;
2243 */
Bryan Cain25480922013-02-15 09:46:50 -06002244 if (first == MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07002245 linker_error(prog, "Transform feedback varyings specified, but "
2246 "no vertex or geometry shader is present.");
2247 goto done;
2248 }
2249
2250 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2251 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08002252 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07002253 prog->TransformFeedback.VaryingNames,
2254 tfeedback_decls))
2255 goto done;
2256 }
2257
Marek Olšák284d9542013-06-12 02:18:09 +02002258 /* Linking the stages in the opposite order (from fragment to vertex)
2259 * ensures that inter-shader outputs written to in an earlier stage are
2260 * eliminated if they are (transitively) not used in a later stage.
2261 */
2262 int last, next;
Paul Berry665b8d72014-01-07 10:11:39 -08002263 for (last = MESA_SHADER_STAGES-1; last >= 0; last--) {
Marek Olšák284d9542013-06-12 02:18:09 +02002264 if (prog->_LinkedShaders[last] != NULL)
2265 break;
Ian Romanick3322fba2010-10-14 13:28:42 -07002266 }
Ian Romanick13e10e42010-06-21 12:03:24 -07002267
Marek Olšák284d9542013-06-12 02:18:09 +02002268 if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
2269 gl_shader *const sh = prog->_LinkedShaders[last];
2270
2271 if (num_tfeedback_decls != 0) {
2272 /* There was no fragment shader, but we still have to assign varying
2273 * locations for use by transform feedback.
2274 */
2275 if (!assign_varying_locations(ctx, mem_ctx, prog,
2276 sh, NULL,
Paul Berry3b0cf702013-04-10 06:48:42 -07002277 num_tfeedback_decls, tfeedback_decls,
2278 0))
Marek Olšák284d9542013-06-12 02:18:09 +02002279 goto done;
2280 }
2281
Marek Olšákd13003f2013-08-09 22:34:45 +02002282 do_dead_builtin_varyings(ctx, sh, NULL,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002283 num_tfeedback_decls, tfeedback_decls);
2284
Marek Olšák284d9542013-06-12 02:18:09 +02002285 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
2286
2287 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07002288 */
Marek Olšák284d9542013-06-12 02:18:09 +02002289 while (do_dead_code(sh->ir, false))
2290 ;
2291 }
2292 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002293 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02002294 */
2295 gl_shader *const sh = prog->_LinkedShaders[first];
2296
Marek Olšákd13003f2013-08-09 22:34:45 +02002297 do_dead_builtin_varyings(ctx, NULL, sh,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002298 num_tfeedback_decls, tfeedback_decls);
2299
Marek Olšák284d9542013-06-12 02:18:09 +02002300 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
2301
2302 while (do_dead_code(sh->ir, false))
2303 ;
2304 }
2305
2306 next = last;
2307 for (int i = next - 1; i >= 0; i--) {
2308 if (prog->_LinkedShaders[i] == NULL)
2309 continue;
2310
2311 gl_shader *const sh_i = prog->_LinkedShaders[i];
2312 gl_shader *const sh_next = prog->_LinkedShaders[next];
Paul Berry3b0cf702013-04-10 06:48:42 -07002313 unsigned gs_input_vertices =
2314 next == MESA_SHADER_GEOMETRY ? prog->Geom.VerticesIn : 0;
Marek Olšák284d9542013-06-12 02:18:09 +02002315
2316 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
2317 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
Paul Berry3b0cf702013-04-10 06:48:42 -07002318 tfeedback_decls, gs_input_vertices))
Paul Berry871ddb92011-11-05 11:17:32 -07002319 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02002320
Marek Olšákd13003f2013-08-09 22:34:45 +02002321 do_dead_builtin_varyings(ctx, sh_i, sh_next,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02002322 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2323 tfeedback_decls);
2324
Marek Olšák284d9542013-06-12 02:18:09 +02002325 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
2326 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
2327
2328 /* Eliminate code that is now dead due to unused outputs being demoted.
2329 */
2330 while (do_dead_code(sh_i->ir, false))
2331 ;
2332 while (do_dead_code(sh_next->ir, false))
2333 ;
2334
Marek Olšák3c555822013-06-13 03:17:22 +02002335 /* This must be done after all dead varyings are eliminated. */
Ian Romanick42305fb2013-09-10 12:00:34 -05002336 if (!check_against_output_limit(ctx, prog, sh_i))
2337 goto done;
2338 if (!check_against_input_limit(ctx, prog, sh_next))
Marek Olšák3c555822013-06-13 03:17:22 +02002339 goto done;
2340
Marek Olšák284d9542013-06-12 02:18:09 +02002341 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07002342 }
2343
2344 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2345 goto done;
2346
Ian Romanick960d7222011-10-21 11:21:02 -07002347 update_array_sizes(prog);
Ian Romanick71990962011-10-18 16:01:49 -07002348 link_assign_uniform_locations(prog);
Francisco Jerez5c114932013-09-11 12:14:46 -07002349 link_assign_atomic_counter_resources(ctx, prog);
Marek Olšákec174a42011-11-18 15:00:10 +01002350 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07002351
Paul Berryb95d2372013-07-27 11:08:31 -07002352 check_resources(ctx, prog);
Francisco Jerez5c114932013-09-11 12:14:46 -07002353 link_check_atomic_counter_resources(ctx, prog);
2354
Paul Berryb95d2372013-07-27 11:08:31 -07002355 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08002356 goto done;
2357
Ian Romanickce9171f2011-02-03 17:10:14 -08002358 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Paul Berry15ba2a52012-08-02 17:51:02 -07002359 * present in a linked program. By checking prog->IsES, we also
2360 * catch the GL_ARB_ES2_compatibility case.
Ian Romanickce9171f2011-02-03 17:10:14 -08002361 */
Eric Anholt57f79782011-07-22 12:57:47 -07002362 if (!prog->InternalSeparateShader &&
Paul Berry15ba2a52012-08-02 17:51:02 -07002363 (ctx->API == API_OPENGLES2 || prog->IsES)) {
Ian Romanickce9171f2011-02-03 17:10:14 -08002364 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002365 linker_error(prog, "program lacks a vertex shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002366 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002367 linker_error(prog, "program lacks a fragment shader\n");
Ian Romanickce9171f2011-02-03 17:10:14 -08002368 }
2369 }
2370
Ian Romanick13e10e42010-06-21 12:03:24 -07002371 /* FINISHME: Assign fragment shader output locations. */
2372
Ian Romanick832dfa52010-06-17 15:04:20 -07002373done:
2374 free(vert_shader_list);
Paul Berry844bd712013-07-30 22:38:43 -07002375 free(frag_shader_list);
Bryan Cain25480922013-02-15 09:46:50 -06002376 free(geom_shader_list);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002377
Paul Berry665b8d72014-01-07 10:11:39 -08002378 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002379 if (prog->_LinkedShaders[i] == NULL)
2380 continue;
2381
Paul Berryd7fa9eb2013-11-22 12:37:22 -08002382 /* Do a final validation step to make sure that the IR wasn't
2383 * invalidated by any modifications performed after intrastage linking.
2384 */
2385 validate_ir_tree(prog->_LinkedShaders[i]->ir);
2386
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002387 /* Retain any live IR, but trash the rest. */
2388 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07002389
2390 /* The symbol table in the linked shaders may contain references to
2391 * variables that were removed (e.g., unused uniforms). Since it may
2392 * contain junk, there is no possible valid use. Delete it and set the
2393 * pointer to NULL.
2394 */
2395 delete prog->_LinkedShaders[i]->symbols;
2396 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002397 }
2398
Kenneth Graunked3073f52011-01-21 14:32:31 -08002399 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07002400}