blob: fd69dbc2c73d4602bf74473682a6c0c00cd9405b [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
Brian Paulddf4b2e2015-02-24 16:56:54 -070067#include <ctype.h>
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080068#include "main/core.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070069#include "glsl_symbol_table.h"
Eric Anholtfaf3dba2013-06-12 16:57:11 -070070#include "glsl_parser_extras.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070071#include "ir.h"
72#include "program.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030073#include "program/hash_table.h"
Ian Romanick8fe8a812010-07-13 17:36:13 -070074#include "linker.h"
Paul Berry4b11b572012-12-17 14:20:35 -080075#include "link_varyings.h"
Ian Romanicka7ba9a72010-07-20 13:36:32 -070076#include "ir_optimization.h"
Bryan Cain25480922013-02-15 09:46:50 -060077#include "ir_rvalue_visitor.h"
Tapani Pällieca9d162014-04-08 08:45:36 +030078#include "ir_uniform.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070079
Ian Romanick3322fba2010-10-14 13:28:42 -070080#include "main/shaderobj.h"
Eric Anholt6065a872013-06-12 18:12:40 -070081#include "main/enums.h"
Brian Paul241c5992014-12-15 16:41:58 -070082
Ian Romanick3322fba2010-10-14 13:28:42 -070083
Bryan Cain25480922013-02-15 09:46:50 -060084void linker_error(gl_shader_program *, const char *, ...);
85
Eric Anholt10ef9492013-09-20 11:03:44 -070086namespace {
87
Ian Romanick832dfa52010-06-17 15:04:20 -070088/**
89 * Visitor that determines whether or not a variable is ever written.
90 */
91class find_assignment_visitor : public ir_hierarchical_visitor {
92public:
93 find_assignment_visitor(const char *name)
94 : name(name), found(false)
95 {
96 /* empty */
97 }
98
99 virtual ir_visitor_status visit_enter(ir_assignment *ir)
100 {
101 ir_variable *const var = ir->lhs->variable_referenced();
102
103 if (strcmp(name, var->name) == 0) {
104 found = true;
105 return visit_stop;
106 }
107
108 return visit_continue_with_parent;
109 }
110
Eric Anholt18a60232010-08-23 11:29:25 -0700111 virtual ir_visitor_status visit_enter(ir_call *ir)
112 {
Kenneth Graunke48d0faa2014-01-10 16:39:17 -0800113 foreach_two_lists(formal_node, &ir->callee->parameters,
114 actual_node, &ir->actual_parameters) {
115 ir_rvalue *param_rval = (ir_rvalue *) actual_node;
116 ir_variable *sig_param = (ir_variable *) formal_node;
Eric Anholt18a60232010-08-23 11:29:25 -0700117
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200118 if (sig_param->data.mode == ir_var_function_out ||
119 sig_param->data.mode == ir_var_function_inout) {
Eric Anholt18a60232010-08-23 11:29:25 -0700120 ir_variable *var = param_rval->variable_referenced();
121 if (var && strcmp(name, var->name) == 0) {
122 found = true;
123 return visit_stop;
124 }
125 }
Eric Anholt18a60232010-08-23 11:29:25 -0700126 }
127
Kenneth Graunked884f602012-03-20 15:56:37 -0700128 if (ir->return_deref != NULL) {
129 ir_variable *const var = ir->return_deref->variable_referenced();
130
131 if (strcmp(name, var->name) == 0) {
132 found = true;
133 return visit_stop;
134 }
135 }
136
Eric Anholt18a60232010-08-23 11:29:25 -0700137 return visit_continue_with_parent;
138 }
139
Ian Romanick832dfa52010-06-17 15:04:20 -0700140 bool variable_found()
141 {
142 return found;
143 }
144
145private:
146 const char *name; /**< Find writes to a variable with this name. */
147 bool found; /**< Was a write to the variable found? */
148};
149
Ian Romanickc93b8f12010-06-17 15:20:22 -0700150
Ian Romanickc33e78f2010-08-13 12:30:41 -0700151/**
152 * Visitor that determines whether or not a variable is ever read.
153 */
154class find_deref_visitor : public ir_hierarchical_visitor {
155public:
156 find_deref_visitor(const char *name)
157 : name(name), found(false)
158 {
159 /* empty */
160 }
161
162 virtual ir_visitor_status visit(ir_dereference_variable *ir)
163 {
164 if (strcmp(this->name, ir->var->name) == 0) {
165 this->found = true;
166 return visit_stop;
167 }
168
169 return visit_continue;
170 }
171
172 bool variable_found() const
173 {
174 return this->found;
175 }
176
177private:
178 const char *name; /**< Find writes to a variable with this name. */
179 bool found; /**< Was a write to the variable found? */
180};
181
182
Paul Berry7cfefe62013-07-30 21:13:48 -0700183class geom_array_resize_visitor : public ir_hierarchical_visitor {
184public:
185 unsigned num_vertices;
186 gl_shader_program *prog;
187
188 geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
189 {
190 this->num_vertices = num_vertices;
191 this->prog = prog;
192 }
193
194 virtual ~geom_array_resize_visitor()
195 {
196 /* empty */
197 }
198
199 virtual ir_visitor_status visit(ir_variable *var)
200 {
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200201 if (!var->type->is_array() || var->data.mode != ir_var_shader_in)
Paul Berry7cfefe62013-07-30 21:13:48 -0700202 return visit_continue;
203
204 unsigned size = var->type->length;
205
206 /* Generate a link error if the shader has declared this array with an
207 * incorrect size.
208 */
209 if (size && size != this->num_vertices) {
210 linker_error(this->prog, "size of array %s declared as %u, "
211 "but number of input vertices is %u\n",
212 var->name, size, this->num_vertices);
213 return visit_continue;
214 }
215
216 /* Generate a link error if the shader attempts to access an input
217 * array using an index too large for its actual size assigned at link
218 * time.
219 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200220 if (var->data.max_array_access >= this->num_vertices) {
Paul Berry7cfefe62013-07-30 21:13:48 -0700221 linker_error(this->prog, "geometry shader accesses element %i of "
222 "%s, but only %i input vertices\n",
Tapani Pälli447bb902013-12-12 15:08:59 +0200223 var->data.max_array_access, var->name, this->num_vertices);
Paul Berry7cfefe62013-07-30 21:13:48 -0700224 return visit_continue;
225 }
226
Timothy Arcerid67515b2015-04-30 20:45:54 +1000227 var->type = glsl_type::get_array_instance(var->type->fields.array,
Paul Berry7cfefe62013-07-30 21:13:48 -0700228 this->num_vertices);
Tapani Pälli447bb902013-12-12 15:08:59 +0200229 var->data.max_array_access = this->num_vertices - 1;
Paul Berry7cfefe62013-07-30 21:13:48 -0700230
231 return visit_continue;
232 }
233
234 /* Dereferences of input variables need to be updated so that their type
235 * matches the newly assigned type of the variable they are accessing. */
236 virtual ir_visitor_status visit(ir_dereference_variable *ir)
237 {
238 ir->type = ir->var->type;
239 return visit_continue;
240 }
241
242 /* Dereferences of 2D input arrays need to be updated so that their type
243 * matches the newly assigned type of the array they are accessing. */
244 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
245 {
246 const glsl_type *const vt = ir->array->type;
247 if (vt->is_array())
Timothy Arcerid67515b2015-04-30 20:45:54 +1000248 ir->type = vt->fields.array;
Paul Berry7cfefe62013-07-30 21:13:48 -0700249 return visit_continue;
250 }
251};
252
Chris Forbes7c758c52014-09-21 13:33:14 +1200253class tess_eval_array_resize_visitor : public ir_hierarchical_visitor {
254public:
255 unsigned num_vertices;
256 gl_shader_program *prog;
257
258 tess_eval_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
259 {
260 this->num_vertices = num_vertices;
261 this->prog = prog;
262 }
263
264 virtual ~tess_eval_array_resize_visitor()
265 {
266 /* empty */
267 }
268
269 virtual ir_visitor_status visit(ir_variable *var)
270 {
271 if (!var->type->is_array() || var->data.mode != ir_var_shader_in || var->data.patch)
272 return visit_continue;
273
274 var->type = glsl_type::get_array_instance(var->type->fields.array,
275 this->num_vertices);
276 var->data.max_array_access = this->num_vertices - 1;
277
278 return visit_continue;
279 }
280
281 /* Dereferences of input variables need to be updated so that their type
282 * matches the newly assigned type of the variable they are accessing. */
283 virtual ir_visitor_status visit(ir_dereference_variable *ir)
284 {
285 ir->type = ir->var->type;
286 return visit_continue;
287 }
288
289 /* Dereferences of 2D input arrays need to be updated so that their type
290 * matches the newly assigned type of the array they are accessing. */
291 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
292 {
293 const glsl_type *const vt = ir->array->type;
294 if (vt->is_array())
295 ir->type = vt->fields.array;
296 return visit_continue;
297 }
298};
299
Chris Forbes8cf72972014-09-07 21:42:50 +1200300class barrier_use_visitor : public ir_hierarchical_visitor {
301public:
302 barrier_use_visitor(gl_shader_program *prog)
303 : prog(prog), in_main(false), after_return(false), control_flow(0)
304 {
305 }
306
307 virtual ~barrier_use_visitor()
308 {
309 /* empty */
310 }
311
312 virtual ir_visitor_status visit_enter(ir_function *ir)
313 {
314 if (strcmp(ir->name, "main") == 0)
315 in_main = true;
316
317 return visit_continue;
318 }
319
Ian Romanick4ff9e592015-08-19 13:36:22 -0700320 virtual ir_visitor_status visit_leave(ir_function *)
Chris Forbes8cf72972014-09-07 21:42:50 +1200321 {
322 in_main = false;
323 after_return = false;
324 return visit_continue;
325 }
326
Ian Romanick4ff9e592015-08-19 13:36:22 -0700327 virtual ir_visitor_status visit_leave(ir_return *)
Chris Forbes8cf72972014-09-07 21:42:50 +1200328 {
329 after_return = true;
330 return visit_continue;
331 }
332
Ian Romanick4ff9e592015-08-19 13:36:22 -0700333 virtual ir_visitor_status visit_enter(ir_if *)
Chris Forbes8cf72972014-09-07 21:42:50 +1200334 {
335 ++control_flow;
336 return visit_continue;
337 }
338
Ian Romanick4ff9e592015-08-19 13:36:22 -0700339 virtual ir_visitor_status visit_leave(ir_if *)
Chris Forbes8cf72972014-09-07 21:42:50 +1200340 {
341 --control_flow;
342 return visit_continue;
343 }
344
Ian Romanick4ff9e592015-08-19 13:36:22 -0700345 virtual ir_visitor_status visit_enter(ir_loop *)
Chris Forbes8cf72972014-09-07 21:42:50 +1200346 {
347 ++control_flow;
348 return visit_continue;
349 }
350
Ian Romanick4ff9e592015-08-19 13:36:22 -0700351 virtual ir_visitor_status visit_leave(ir_loop *)
Chris Forbes8cf72972014-09-07 21:42:50 +1200352 {
353 --control_flow;
354 return visit_continue;
355 }
356
357 /* FINISHME: `switch` is not expressed at the IR level -- it's already
358 * been lowered to a mess of `if`s. We'll correctly disallow any use of
359 * barrier() in a conditional path within the switch, but not in a path
360 * which is always hit.
361 */
362
363 virtual ir_visitor_status visit_enter(ir_call *ir)
364 {
365 if (ir->use_builtin && strcmp(ir->callee_name(), "barrier") == 0) {
366 /* Use of barrier(); determine if it is legal: */
367 if (!in_main) {
368 linker_error(prog, "Builtin barrier() may only be used in main");
369 return visit_stop;
370 }
371
372 if (after_return) {
373 linker_error(prog, "Builtin barrier() may not be used after return");
374 return visit_stop;
375 }
376
377 if (control_flow != 0) {
378 linker_error(prog, "Builtin barrier() may not be used inside control flow");
379 return visit_stop;
380 }
381 }
382 return visit_continue;
383 }
384
385private:
386 gl_shader_program *prog;
387 bool in_main, after_return;
388 int control_flow;
389};
390
Paul Berry1a33e022013-08-18 20:59:37 -0700391/**
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200392 * Visitor that determines the highest stream id to which a (geometry) shader
393 * emits vertices. It also checks whether End{Stream}Primitive is ever called.
Paul Berry1a33e022013-08-18 20:59:37 -0700394 */
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200395class find_emit_vertex_visitor : public ir_hierarchical_visitor {
Paul Berry1a33e022013-08-18 20:59:37 -0700396public:
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200397 find_emit_vertex_visitor(int max_allowed)
398 : max_stream_allowed(max_allowed),
399 invalid_stream_id(0),
400 invalid_stream_id_from_emit_vertex(false),
401 end_primitive_found(false),
402 uses_non_zero_stream(false)
Paul Berry1a33e022013-08-18 20:59:37 -0700403 {
404 /* empty */
405 }
406
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200407 virtual ir_visitor_status visit_leave(ir_emit_vertex *ir)
Paul Berry1a33e022013-08-18 20:59:37 -0700408 {
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200409 int stream_id = ir->stream_id();
410
411 if (stream_id < 0) {
412 invalid_stream_id = stream_id;
413 invalid_stream_id_from_emit_vertex = true;
414 return visit_stop;
415 }
416
417 if (stream_id > max_stream_allowed) {
418 invalid_stream_id = stream_id;
419 invalid_stream_id_from_emit_vertex = true;
420 return visit_stop;
421 }
422
423 if (stream_id != 0)
424 uses_non_zero_stream = true;
425
426 return visit_continue;
Paul Berry1a33e022013-08-18 20:59:37 -0700427 }
428
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200429 virtual ir_visitor_status visit_leave(ir_end_primitive *ir)
Paul Berry1a33e022013-08-18 20:59:37 -0700430 {
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200431 end_primitive_found = true;
432
433 int stream_id = ir->stream_id();
434
435 if (stream_id < 0) {
436 invalid_stream_id = stream_id;
437 invalid_stream_id_from_emit_vertex = false;
438 return visit_stop;
439 }
440
441 if (stream_id > max_stream_allowed) {
442 invalid_stream_id = stream_id;
443 invalid_stream_id_from_emit_vertex = false;
444 return visit_stop;
445 }
446
447 if (stream_id != 0)
448 uses_non_zero_stream = true;
449
450 return visit_continue;
451 }
452
453 bool error()
454 {
455 return invalid_stream_id != 0;
456 }
457
458 const char *error_func()
459 {
460 return invalid_stream_id_from_emit_vertex ?
461 "EmitStreamVertex" : "EndStreamPrimitive";
462 }
463
464 int error_stream()
465 {
466 return invalid_stream_id;
467 }
468
469 bool uses_streams()
470 {
471 return uses_non_zero_stream;
472 }
473
474 bool uses_end_primitive()
475 {
476 return end_primitive_found;
Paul Berry1a33e022013-08-18 20:59:37 -0700477 }
478
479private:
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200480 int max_stream_allowed;
481 int invalid_stream_id;
482 bool invalid_stream_id_from_emit_vertex;
483 bool end_primitive_found;
484 bool uses_non_zero_stream;
Paul Berry1a33e022013-08-18 20:59:37 -0700485};
486
Tapani Pälli9350ea62015-05-19 15:01:49 +0300487/* Class that finds array derefs and check if indexes are dynamic. */
488class dynamic_sampler_array_indexing_visitor : public ir_hierarchical_visitor
489{
490public:
491 dynamic_sampler_array_indexing_visitor() :
492 dynamic_sampler_array_indexing(false)
493 {
494 }
495
496 ir_visitor_status visit_enter(ir_dereference_array *ir)
497 {
498 if (!ir->variable_referenced())
499 return visit_continue;
500
501 if (!ir->variable_referenced()->type->contains_sampler())
502 return visit_continue;
503
504 if (!ir->array_index->constant_expression_value()) {
505 dynamic_sampler_array_indexing = true;
506 return visit_stop;
507 }
508 return visit_continue;
509 }
510
511 bool uses_dynamic_sampler_array_indexing()
512 {
513 return dynamic_sampler_array_indexing;
514 }
515
516private:
517 bool dynamic_sampler_array_indexing;
518};
519
Eric Anholt10ef9492013-09-20 11:03:44 -0700520} /* anonymous namespace */
Paul Berry1a33e022013-08-18 20:59:37 -0700521
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700522void
Ian Romanick586e7412011-07-28 14:04:09 -0700523linker_error(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700524{
525 va_list ap;
526
Kenneth Graunked3073f52011-01-21 14:32:31 -0800527 ralloc_strcat(&prog->InfoLog, "error: ");
Ian Romanickf36460e2010-06-23 12:07:22 -0700528 va_start(ap, fmt);
Kenneth Graunked3073f52011-01-21 14:32:31 -0800529 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
Ian Romanickf36460e2010-06-23 12:07:22 -0700530 va_end(ap);
Ian Romanick586e7412011-07-28 14:04:09 -0700531
532 prog->LinkStatus = false;
Ian Romanickf36460e2010-06-23 12:07:22 -0700533}
534
535
536void
Ian Romanick379a32f2011-07-28 14:09:06 -0700537linker_warning(gl_shader_program *prog, const char *fmt, ...)
538{
539 va_list ap;
540
Anuj Phogat80b4a362014-03-07 16:48:35 -0800541 ralloc_strcat(&prog->InfoLog, "warning: ");
Ian Romanick379a32f2011-07-28 14:09:06 -0700542 va_start(ap, fmt);
543 ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
544 va_end(ap);
545
546}
547
548
Paul Berryb92900d2013-01-28 14:21:59 -0800549/**
550 * Given a string identifying a program resource, break it into a base name
551 * and an optional array index in square brackets.
552 *
553 * If an array index is present, \c out_base_name_end is set to point to the
554 * "[" that precedes the array index, and the array index itself is returned
555 * as a long.
556 *
557 * If no array index is present (or if the array index is negative or
558 * mal-formed), \c out_base_name_end, is set to point to the null terminator
559 * at the end of the input string, and -1 is returned.
560 *
561 * Only the final array index is parsed; if the string contains other array
562 * indices (or structure field accesses), they are left in the base name.
563 *
564 * No attempt is made to check that the base name is properly formed;
565 * typically the caller will look up the base name in a hash table, so
566 * ill-formed base names simply turn into hash table lookup failures.
567 */
568long
569parse_program_resource_name(const GLchar *name,
570 const GLchar **out_base_name_end)
571{
572 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
573 *
574 * "When an integer array element or block instance number is part of
575 * the name string, it will be specified in decimal form without a "+"
576 * or "-" sign or any extra leading zeroes. Additionally, the name
577 * string will not include white space anywhere in the string."
578 */
579
580 const size_t len = strlen(name);
581 *out_base_name_end = name + len;
582
583 if (len == 0 || name[len-1] != ']')
584 return -1;
585
586 /* Walk backwards over the string looking for a non-digit character. This
587 * had better be the opening bracket for an array index.
588 *
589 * Initially, i specifies the location of the ']'. Since the string may
590 * contain only the ']' charcater, walk backwards very carefully.
591 */
592 unsigned i;
593 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
594 /* empty */ ;
595
596 if ((i == 0) || name[i-1] != '[')
597 return -1;
598
599 long array_index = strtol(&name[i], NULL, 10);
600 if (array_index < 0)
601 return -1;
602
Timothy Arceri09c440c2015-07-03 08:45:30 +1000603 /* Check for leading zero */
604 if (name[i] == '0' && name[i+1] != ']')
605 return -1;
606
Paul Berryb92900d2013-01-28 14:21:59 -0800607 *out_base_name_end = name + (i - 1);
608 return array_index;
609}
610
611
Ian Romanick379a32f2011-07-28 14:09:06 -0700612void
Ian Romanick63974c02013-10-04 10:46:29 -0700613link_invalidate_variable_locations(exec_list *ir)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700614{
Matt Turner4d784462014-06-24 21:34:05 -0700615 foreach_in_list(ir_instruction, node, ir) {
616 ir_variable *const var = node->as_variable();
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700617
Paul Berry50895d42012-12-05 07:17:07 -0800618 if (var == NULL)
619 continue;
620
Ian Romanick63974c02013-10-04 10:46:29 -0700621 /* Only assign locations for variables that lack an explicit location.
622 * Explicit locations are set for all built-in variables, generic vertex
623 * shader inputs (via layout(location=...)), and generic fragment shader
624 * outputs (also via layout(location=...)).
625 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200626 if (!var->data.explicit_location) {
627 var->data.location = -1;
628 var->data.location_frac = 0;
Paul Berry50895d42012-12-05 07:17:07 -0800629 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700630
Ian Romanick63974c02013-10-04 10:46:29 -0700631 /* ir_variable::is_unmatched_generic_inout is used by the linker while
632 * connecting outputs from one stage to inputs of the next stage.
633 *
634 * There are two implicit assumptions here. First, we assume that any
635 * built-in variable (i.e., non-generic in or out) will have
636 * explicit_location set. Second, we assume that any generic in or out
637 * will not have explicit_location set.
638 *
639 * This second assumption will only be valid until
640 * GL_ARB_separate_shader_objects is supported. When that extension is
641 * implemented, this function will need some modifications.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700642 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200643 if (!var->data.explicit_location) {
644 var->data.is_unmatched_generic_inout = 1;
Paul Berry3e81c662012-12-05 10:47:55 -0800645 } else {
Tapani Pälli447bb902013-12-12 15:08:59 +0200646 var->data.is_unmatched_generic_inout = 0;
Paul Berry3e81c662012-12-05 10:47:55 -0800647 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700648 }
649}
650
651
Ian Romanickc93b8f12010-06-17 15:20:22 -0700652/**
Paul Berry44e07de2013-06-11 14:11:05 -0700653 * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
654 *
655 * Also check for errors based on incorrect usage of gl_ClipVertex and
656 * gl_ClipDistance.
657 *
658 * Return false if an error was reported.
659 */
660static void
Paul Berryb30e25f2013-12-17 09:49:43 -0800661analyze_clip_usage(struct gl_shader_program *prog,
Paul Berry44e07de2013-06-11 14:11:05 -0700662 struct gl_shader *shader, GLboolean *UsesClipDistance,
663 GLuint *ClipDistanceArraySize)
664{
665 *ClipDistanceArraySize = 0;
666
667 if (!prog->IsES && prog->Version >= 130) {
668 /* From section 7.1 (Vertex Shader Special Variables) of the
669 * GLSL 1.30 spec:
670 *
671 * "It is an error for a shader to statically write both
672 * gl_ClipVertex and gl_ClipDistance."
673 *
674 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
675 * gl_ClipVertex nor gl_ClipDistance.
676 */
677 find_assignment_visitor clip_vertex("gl_ClipVertex");
678 find_assignment_visitor clip_distance("gl_ClipDistance");
679
680 clip_vertex.run(shader->ir);
681 clip_distance.run(shader->ir);
682 if (clip_vertex.variable_found() && clip_distance.variable_found()) {
683 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
Paul Berryb30e25f2013-12-17 09:49:43 -0800684 "and `gl_ClipDistance'\n",
Paul Berrye3b86f02014-01-07 10:58:56 -0800685 _mesa_shader_stage_to_string(shader->Stage));
Paul Berry44e07de2013-06-11 14:11:05 -0700686 return;
687 }
688 *UsesClipDistance = clip_distance.variable_found();
689 ir_variable *clip_distance_var =
690 shader->symbols->get_variable("gl_ClipDistance");
691 if (clip_distance_var)
692 *ClipDistanceArraySize = clip_distance_var->type->length;
693 } else {
694 *UsesClipDistance = false;
695 }
696}
697
698
699/**
Paul Berry1ad54ae2011-09-17 09:42:02 -0700700 * Verify that a vertex shader executable meets all semantic requirements.
701 *
Paul Berry642e5b412012-01-04 13:57:52 -0800702 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
703 * as a side effect.
Ian Romanickc93b8f12010-06-17 15:20:22 -0700704 *
705 * \param shader Vertex shader executable to be verified
706 */
Paul Berryb95d2372013-07-27 11:08:31 -0700707void
Eric Anholt849e1812010-06-30 11:49:17 -0700708validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700709 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700710{
711 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700712 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700713
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700714 /* From the GLSL 1.10 spec, page 48:
715 *
716 * "The variable gl_Position is available only in the vertex
717 * language and is intended for writing the homogeneous vertex
718 * position. All executions of a well-formed vertex shader
719 * executable must write a value into this variable. [...] The
720 * variable gl_Position is available only in the vertex
721 * language and is intended for writing the homogeneous vertex
722 * position. All executions of a well-formed vertex shader
723 * executable must write a value into this variable."
724 *
725 * while in GLSL 1.40 this text is changed to:
726 *
727 * "The variable gl_Position is available only in the vertex
728 * language and is intended for writing the homogeneous vertex
729 * position. It can be written at any time during shader
730 * execution. It may also be read back by a vertex shader
731 * after being written. This value will be used by primitive
732 * assembly, clipping, culling, and other fixed functionality
733 * operations, if present, that operate on primitives after
734 * vertex processing has occurred. Its value is undefined if
735 * the vertex shader executable does not write gl_Position."
Paul Berry15ba2a52012-08-02 17:51:02 -0700736 *
Kalyan Kondapally78c92012014-09-08 11:10:42 +0300737 * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
738 * gl_Position is not an error.
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700739 */
Kalyan Kondapallydbc2d812014-09-10 20:20:23 -0700740 if (prog->Version < (prog->IsES ? 300 : 140)) {
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700741 find_assignment_visitor find("gl_Position");
742 find.run(shader->ir);
743 if (!find.variable_found()) {
Kalyan Kondapallydbc2d812014-09-10 20:20:23 -0700744 if (prog->IsES) {
745 linker_warning(prog,
746 "vertex shader does not write to `gl_Position'."
747 "It's value is undefined. \n");
748 } else {
749 linker_error(prog,
750 "vertex shader does not write to `gl_Position'. \n");
751 }
Paul Berryb95d2372013-07-27 11:08:31 -0700752 return;
Eric Anholtf1c1c9e2012-03-19 22:43:27 -0700753 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700754 }
755
Paul Berryb30e25f2013-12-17 09:49:43 -0800756 analyze_clip_usage(prog, shader, &prog->Vert.UsesClipDistance,
Paul Berry44e07de2013-06-11 14:11:05 -0700757 &prog->Vert.ClipDistanceArraySize);
Ian Romanick832dfa52010-06-17 15:04:20 -0700758}
759
Chris Forbesdf16e0d2014-09-09 19:25:02 +1200760void
761validate_tess_eval_shader_executable(struct gl_shader_program *prog,
762 struct gl_shader *shader)
763{
764 if (shader == NULL)
765 return;
766
767 analyze_clip_usage(prog, shader, &prog->TessEval.UsesClipDistance,
768 &prog->TessEval.ClipDistanceArraySize);
769}
770
Ian Romanick832dfa52010-06-17 15:04:20 -0700771
Ian Romanickc93b8f12010-06-17 15:20:22 -0700772/**
773 * Verify that a fragment shader executable meets all semantic requirements
774 *
775 * \param shader Fragment shader executable to be verified
776 */
Paul Berryb95d2372013-07-27 11:08:31 -0700777void
Eric Anholt849e1812010-06-30 11:49:17 -0700778validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700779 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700780{
781 if (shader == NULL)
Paul Berryb95d2372013-07-27 11:08:31 -0700782 return;
Ian Romanick832dfa52010-06-17 15:04:20 -0700783
Ian Romanick832dfa52010-06-17 15:04:20 -0700784 find_assignment_visitor frag_color("gl_FragColor");
785 find_assignment_visitor frag_data("gl_FragData");
786
Eric Anholt16b68b12010-06-30 11:05:43 -0700787 frag_color.run(shader->ir);
788 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700789
Ian Romanick832dfa52010-06-17 15:04:20 -0700790 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanick586e7412011-07-28 14:04:09 -0700791 linker_error(prog, "fragment shader writes to both "
792 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700793 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700794}
795
Bryan Cain25480922013-02-15 09:46:50 -0600796/**
797 * Verify that a geometry shader executable meets all semantic requirements
798 *
Paul Berry44e07de2013-06-11 14:11:05 -0700799 * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
800 * prog->Geom.ClipDistanceArraySize as a side effect.
Bryan Cain25480922013-02-15 09:46:50 -0600801 *
802 * \param shader Geometry shader executable to be verified
803 */
804void
805validate_geometry_shader_executable(struct gl_shader_program *prog,
806 struct gl_shader *shader)
807{
808 if (shader == NULL)
809 return;
810
811 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
812 prog->Geom.VerticesIn = num_vertices;
Paul Berry44e07de2013-06-11 14:11:05 -0700813
Paul Berryb30e25f2013-12-17 09:49:43 -0800814 analyze_clip_usage(prog, shader, &prog->Geom.UsesClipDistance,
Paul Berry44e07de2013-06-11 14:11:05 -0700815 &prog->Geom.ClipDistanceArraySize);
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200816}
Paul Berry1a33e022013-08-18 20:59:37 -0700817
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200818/**
819 * Check if geometry shaders emit to non-zero streams and do corresponding
820 * validations.
821 */
822static void
823validate_geometry_shader_emissions(struct gl_context *ctx,
824 struct gl_shader_program *prog)
825{
826 if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
827 find_emit_vertex_visitor emit_vertex(ctx->Const.MaxVertexStreams - 1);
828 emit_vertex.run(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir);
829 if (emit_vertex.error()) {
830 linker_error(prog, "Invalid call %s(%d). Accepted values for the "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -0700831 "stream parameter are in the range [0, %d].\n",
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200832 emit_vertex.error_func(),
833 emit_vertex.error_stream(),
834 ctx->Const.MaxVertexStreams - 1);
835 }
836 prog->Geom.UsesStreams = emit_vertex.uses_streams();
837 prog->Geom.UsesEndPrimitive = emit_vertex.uses_end_primitive();
838
839 /* From the ARB_gpu_shader5 spec:
840 *
841 * "Multiple vertex streams are supported only if the output primitive
842 * type is declared to be "points". A program will fail to link if it
843 * contains a geometry shader calling EmitStreamVertex() or
844 * EndStreamPrimitive() if its output primitive type is not "points".
845 *
846 * However, in the same spec:
847 *
848 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
849 * with <stream> set to zero."
850 *
851 * And:
852 *
853 * "The function EndPrimitive() is equivalent to calling
854 * EndStreamPrimitive() with <stream> set to zero."
855 *
856 * Since we can call EmitVertex() and EndPrimitive() when we output
857 * primitives other than points, calling EmitStreamVertex(0) or
858 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
859 * does. Currently we only set prog->Geom.UsesStreams to TRUE when
860 * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
861 * stream.
862 */
863 if (prog->Geom.UsesStreams && prog->Geom.OutputType != GL_POINTS) {
864 linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -0700865 "with n>0 requires point output\n");
Iago Toral Quiroga75896832014-06-16 16:09:53 +0200866 }
867 }
Bryan Cain25480922013-02-15 09:46:50 -0600868}
869
Timothy Arceri50859c62015-02-21 21:47:14 +1100870bool
871validate_intrastage_arrays(struct gl_shader_program *prog,
872 ir_variable *const var,
873 ir_variable *const existing)
874{
875 /* Consider the types to be "the same" if both types are arrays
876 * of the same type and one of the arrays is implicitly sized.
877 * In addition, set the type of the linked variable to the
878 * explicitly sized array.
879 */
880 if (var->type->is_array() && existing->type->is_array() &&
881 (var->type->fields.array == existing->type->fields.array) &&
882 ((var->type->length == 0)|| (existing->type->length == 0))) {
883 if (var->type->length != 0) {
884 if (var->type->length <= existing->data.max_array_access) {
885 linker_error(prog, "%s `%s' declared as type "
886 "`%s' but outermost dimension has an index"
887 " of `%i'\n",
888 mode_string(var),
889 var->name, var->type->name,
890 existing->data.max_array_access);
891 }
892 existing->type = var->type;
893 return true;
894 } else if (existing->type->length != 0) {
895 if(existing->type->length <= var->data.max_array_access) {
896 linker_error(prog, "%s `%s' declared as type "
897 "`%s' but outermost dimension has an index"
898 " of `%i'\n",
899 mode_string(var),
900 var->name, existing->type->name,
901 var->data.max_array_access);
902 }
903 return true;
904 }
905 }
906 return false;
907}
908
Ian Romanick832dfa52010-06-17 15:04:20 -0700909
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700910/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700911 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700912 */
Paul Berryb95d2372013-07-27 11:08:31 -0700913void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700914cross_validate_globals(struct gl_shader_program *prog,
915 struct gl_shader **shader_list,
916 unsigned num_shaders,
917 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700918{
919 /* Examine all of the uniforms in all of the shaders and cross validate
920 * them.
921 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700922 glsl_symbol_table variables;
923 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -0700924 if (shader_list[i] == NULL)
925 continue;
926
Matt Turner4d784462014-06-24 21:34:05 -0700927 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
928 ir_variable *const var = node->as_variable();
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700929
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700930 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700931 continue;
932
Kristian Høgsberga78a5892015-05-13 11:17:23 +0200933 if (uniforms_only && (var->data.mode != ir_var_uniform && var->data.mode != ir_var_shader_storage))
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700934 continue;
935
Dave Airlie60266862015-04-20 10:27:36 +1000936 /* don't cross validate subroutine uniforms */
937 if (var->type->contains_subroutine())
938 continue;
939
Ian Romanick7e2aa912010-07-19 17:12:42 -0700940 /* Don't cross validate temporaries that are at global scope. These
941 * will eventually get pulled into the shaders 'main'.
942 */
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200943 if (var->data.mode == ir_var_temporary)
Ian Romanick7e2aa912010-07-19 17:12:42 -0700944 continue;
945
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700946 /* If a global with this name has already been seen, verify that the
947 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700948 * initializers, the values of the initializers must be the same.
949 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700950 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700951 if (existing != NULL) {
Timothy Arceri50859c62015-02-21 21:47:14 +1100952 /* Check if types match. Interface blocks have some special
953 * rules so we handle those elsewhere.
954 */
Timothy Arceri1a96d9e2015-02-24 17:28:51 +1100955 if (var->type != existing->type &&
956 !var->is_interface_instance()) {
Timothy Arceri50859c62015-02-21 21:47:14 +1100957 if (!validate_intrastage_arrays(prog, var, existing)) {
958 if (var->type->is_record() && existing->type->is_record()
959 && existing->type->record_compare(var->type)) {
960 existing->type = var->type;
961 } else {
Timothy Arcerida4fb3e2014-11-25 23:04:23 +1100962 linker_error(prog, "%s `%s' declared as type "
Timothy Arceri50859c62015-02-21 21:47:14 +1100963 "`%s' and type `%s'\n",
Timothy Arcerida4fb3e2014-11-25 23:04:23 +1100964 mode_string(var),
Timothy Arceri50859c62015-02-21 21:47:14 +1100965 var->name, var->type->name,
966 existing->type->name);
Timothy Arcerida4fb3e2014-11-25 23:04:23 +1100967 return;
968 }
Ian Romanicka2711d62010-08-29 22:07:49 -0700969 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700970 }
971
Tapani Pälli447bb902013-12-12 15:08:59 +0200972 if (var->data.explicit_location) {
973 if (existing->data.explicit_location
974 && (var->data.location != existing->data.location)) {
Ian Romanick586e7412011-07-28 14:04:09 -0700975 linker_error(prog, "explicit locations for %s "
976 "`%s' have differing values\n",
977 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700978 return;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700979 }
980
Tapani Pälli447bb902013-12-12 15:08:59 +0200981 existing->data.location = var->data.location;
982 existing->data.explicit_location = true;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700983 }
984
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700985 /* From the GLSL 4.20 specification:
986 * "A link error will result if two compilation units in a program
987 * specify different integer-constant bindings for the same
988 * opaque-uniform name. However, it is not an error to specify a
989 * binding on some but not all declarations for the same name"
990 */
Tapani Pälli447bb902013-12-12 15:08:59 +0200991 if (var->data.explicit_binding) {
992 if (existing->data.explicit_binding &&
993 var->data.binding != existing->data.binding) {
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700994 linker_error(prog, "explicit bindings for %s "
995 "`%s' have differing values\n",
996 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -0700997 return;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -0700998 }
999
Tapani Pälli447bb902013-12-12 15:08:59 +02001000 existing->data.binding = var->data.binding;
1001 existing->data.explicit_binding = true;
Kenneth Graunke9a9a8302013-07-16 12:18:57 -07001002 }
1003
Francisco Jerez5c114932013-09-11 12:14:46 -07001004 if (var->type->contains_atomic() &&
Tapani Pälli447bb902013-12-12 15:08:59 +02001005 var->data.atomic.offset != existing->data.atomic.offset) {
Francisco Jerez5c114932013-09-11 12:14:46 -07001006 linker_error(prog, "offset specifications for %s "
1007 "`%s' have differing values\n",
1008 mode_string(var), var->name);
1009 return;
1010 }
1011
Ian Romanick46173f92011-10-31 13:07:06 -07001012 /* Validate layout qualifiers for gl_FragDepth.
1013 *
1014 * From the AMD/ARB_conservative_depth specs:
1015 *
1016 * "If gl_FragDepth is redeclared in any fragment shader in a
1017 * program, it must be redeclared in all fragment shaders in
1018 * that program that have static assignments to
1019 * gl_FragDepth. All redeclarations of gl_FragDepth in all
1020 * fragment shaders in a single program must have the same set
1021 * of qualifiers."
1022 */
1023 if (strcmp(var->name, "gl_FragDepth") == 0) {
Tapani Pälli447bb902013-12-12 15:08:59 +02001024 bool layout_declared = var->data.depth_layout != ir_depth_layout_none;
Ian Romanick46173f92011-10-31 13:07:06 -07001025 bool layout_differs =
Tapani Pälli447bb902013-12-12 15:08:59 +02001026 var->data.depth_layout != existing->data.depth_layout;
Ian Romanick46173f92011-10-31 13:07:06 -07001027
1028 if (layout_declared && layout_differs) {
1029 linker_error(prog,
1030 "All redeclarations of gl_FragDepth in all "
1031 "fragment shaders in a single program must have "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07001032 "the same set of qualifiers.\n");
Ian Romanick46173f92011-10-31 13:07:06 -07001033 }
1034
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001035 if (var->data.used && layout_differs) {
Ian Romanick46173f92011-10-31 13:07:06 -07001036 linker_error(prog,
1037 "If gl_FragDepth is redeclared with a layout "
1038 "qualifier in any fragment shader, it must be "
1039 "redeclared with the same layout qualifier in "
1040 "all fragment shaders that have assignments to "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07001041 "gl_FragDepth\n");
Ian Romanick46173f92011-10-31 13:07:06 -07001042 }
1043 }
Chad Versaceaddae332011-01-27 01:40:31 -08001044
Ian Romanickf37b1ad2011-10-31 14:31:07 -07001045 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1046 *
1047 * "If a shared global has multiple initializers, the
1048 * initializers must all be constant expressions, and they
1049 * must all have the same value. Otherwise, a link error will
1050 * result. (A shared global having only one initializer does
1051 * not require that initializer to be a constant expression.)"
1052 *
1053 * Previous to 4.20 the GLSL spec simply said that initializers
1054 * must have the same value. In this case of non-constant
1055 * initializers, this was impossible to determine. As a result,
1056 * no vendor actually implemented that behavior. The 4.20
1057 * behavior matches the implemented behavior of at least one other
1058 * vendor, so we'll implement that for all GLSL versions.
Ian Romanicke2e5d0d2010-06-29 18:47:11 -07001059 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -07001060 if (var->constant_initializer != NULL) {
1061 if (existing->constant_initializer != NULL) {
1062 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
Ian Romanick586e7412011-07-28 14:04:09 -07001063 linker_error(prog, "initializers for %s "
1064 "`%s' have differing values\n",
1065 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -07001066 return;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001067 }
Ian Romanickf37b1ad2011-10-31 14:31:07 -07001068 } else {
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001069 /* If the first-seen instance of a particular uniform did not
1070 * have an initializer but a later instance does, copy the
1071 * initializer to the version stored in the symbol table.
1072 */
Ian Romanickde415b72010-07-14 13:22:12 -07001073 /* FINISHME: This is wrong. The constant_value field should
1074 * FINISHME: not be modified! Imagine a case where a shader
1075 * FINISHME: without an initializer is linked in two different
1076 * FINISHME: programs with shaders that have differing
1077 * FINISHME: initializers. Linking with the first will
1078 * FINISHME: modify the shader, and linking with the second
1079 * FINISHME: will fail.
1080 */
Ian Romanickf37b1ad2011-10-31 14:31:07 -07001081 existing->constant_initializer =
1082 var->constant_initializer->clone(ralloc_parent(existing),
1083 NULL);
1084 }
1085 }
1086
Tapani Pälli447bb902013-12-12 15:08:59 +02001087 if (var->data.has_initializer) {
1088 if (existing->data.has_initializer
Ian Romanickf37b1ad2011-10-31 14:31:07 -07001089 && (var->constant_initializer == NULL
1090 || existing->constant_initializer == NULL)) {
1091 linker_error(prog,
1092 "shared global variable `%s' has multiple "
1093 "non-constant initializers.\n",
1094 var->name);
Paul Berryb95d2372013-07-27 11:08:31 -07001095 return;
Ian Romanickf37b1ad2011-10-31 14:31:07 -07001096 }
1097
1098 /* Some instance had an initializer, so keep track of that. In
1099 * this location, all sorts of initializers (constant or
1100 * otherwise) will propagate the existence to the variable
1101 * stored in the symbol table.
1102 */
Tapani Pälli447bb902013-12-12 15:08:59 +02001103 existing->data.has_initializer = true;
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001104 }
Chad Versace7528f142010-11-17 14:34:38 -08001105
Tapani Pällic1d30802013-12-12 12:57:57 +02001106 if (existing->data.invariant != var->data.invariant) {
Ian Romanick586e7412011-07-28 14:04:09 -07001107 linker_error(prog, "declarations for %s `%s' have "
1108 "mismatching invariant qualifiers\n",
1109 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -07001110 return;
Chad Versace7528f142010-11-17 14:34:38 -08001111 }
Tapani Pällic1d30802013-12-12 12:57:57 +02001112 if (existing->data.centroid != var->data.centroid) {
Ian Romanick586e7412011-07-28 14:04:09 -07001113 linker_error(prog, "declarations for %s `%s' have "
1114 "mismatching centroid qualifiers\n",
1115 mode_string(var), var->name);
Paul Berryb95d2372013-07-27 11:08:31 -07001116 return;
Chad Versace61428dd2011-01-10 15:29:30 -08001117 }
Tapani Pällic1d30802013-12-12 12:57:57 +02001118 if (existing->data.sample != var->data.sample) {
Chris Forbes51c5fc82013-11-29 21:26:10 +13001119 linker_error(prog, "declarations for %s `%s` have "
1120 "mismatching sample qualifiers\n",
1121 mode_string(var), var->name);
1122 return;
1123 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001124 } else
Eric Anholt001eee52010-11-05 06:11:24 -07001125 variables.add_variable(var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001126 }
1127 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -07001128}
1129
1130
Ian Romanick37101922010-06-18 19:02:10 -07001131/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -07001132 * Perform validation of uniforms used across multiple shader stages
1133 */
Paul Berryb95d2372013-07-27 11:08:31 -07001134void
Ian Romanicke2e5d0d2010-06-29 18:47:11 -07001135cross_validate_uniforms(struct gl_shader_program *prog)
1136{
Paul Berryb95d2372013-07-27 11:08:31 -07001137 cross_validate_globals(prog, prog->_LinkedShaders,
Paul Berry665b8d72014-01-07 10:11:39 -08001138 MESA_SHADER_STAGES, true);
Ian Romanicke2e5d0d2010-06-29 18:47:11 -07001139}
1140
Eric Anholtf609cf72012-04-27 13:52:56 -07001141/**
1142 * Accumulates the array of prog->UniformBlocks and checks that all
1143 * definitons of blocks agree on their contents.
1144 */
1145static bool
1146interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
1147{
1148 unsigned max_num_uniform_blocks = 0;
Paul Berry665b8d72014-01-07 10:11:39 -08001149 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholtf609cf72012-04-27 13:52:56 -07001150 if (prog->_LinkedShaders[i])
1151 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
1152 }
1153
Paul Berry665b8d72014-01-07 10:11:39 -08001154 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Eric Anholtf609cf72012-04-27 13:52:56 -07001155 struct gl_shader *sh = prog->_LinkedShaders[i];
1156
1157 prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
1158 max_num_uniform_blocks);
1159 for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
1160 prog->UniformBlockStageIndex[i][j] = -1;
1161
1162 if (sh == NULL)
1163 continue;
1164
1165 for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
1166 int index = link_cross_validate_uniform_block(prog,
1167 &prog->UniformBlocks,
1168 &prog->NumUniformBlocks,
1169 &sh->UniformBlocks[j]);
1170
1171 if (index == -1) {
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07001172 linker_error(prog, "uniform block `%s' has mismatching definitions\n",
Eric Anholtf609cf72012-04-27 13:52:56 -07001173 sh->UniformBlocks[j].Name);
1174 return false;
1175 }
1176
1177 prog->UniformBlockStageIndex[i][index] = j;
1178 }
1179 }
1180
1181 return true;
1182}
Ian Romanicke2e5d0d2010-06-29 18:47:11 -07001183
Ian Romanick37101922010-06-18 19:02:10 -07001184
Ian Romanick3fb87872010-07-09 14:09:34 -07001185/**
1186 * Populates a shaders symbol table with all global declarations
1187 */
1188static void
1189populate_symbol_table(gl_shader *sh)
1190{
1191 sh->symbols = new(sh) glsl_symbol_table;
1192
Matt Turner4d784462014-06-24 21:34:05 -07001193 foreach_in_list(ir_instruction, inst, sh->ir) {
Ian Romanick3fb87872010-07-09 14:09:34 -07001194 ir_variable *var;
1195 ir_function *func;
1196
1197 if ((func = inst->as_function()) != NULL) {
Eric Anholte8f5ebf2010-11-05 06:08:45 -07001198 sh->symbols->add_function(func);
Ian Romanick3fb87872010-07-09 14:09:34 -07001199 } else if ((var = inst->as_variable()) != NULL) {
Ian Romanicka9948242014-07-08 18:53:09 -07001200 if (var->data.mode != ir_var_temporary)
1201 sh->symbols->add_variable(var);
Ian Romanick3fb87872010-07-09 14:09:34 -07001202 }
1203 }
1204}
1205
1206
1207/**
Ian Romanick31a97862010-07-12 18:48:50 -07001208 * Remap variables referenced in an instruction tree
1209 *
1210 * This is used when instruction trees are cloned from one shader and placed in
1211 * another. These trees will contain references to \c ir_variable nodes that
1212 * do not exist in the target shader. This function finds these \c ir_variable
1213 * references and replaces the references with matching variables in the target
1214 * shader.
1215 *
1216 * If there is no matching variable in the target shader, a clone of the
1217 * \c ir_variable is made and added to the target shader. The new variable is
1218 * added to \b both the instruction stream and the symbol table.
1219 *
1220 * \param inst IR tree that is to be processed.
1221 * \param symbols Symbol table containing global scope symbols in the
1222 * linked shader.
1223 * \param instructions Instruction stream where new variable declarations
1224 * should be added.
1225 */
1226void
Eric Anholt8273bd42010-08-04 12:34:56 -07001227remap_variables(ir_instruction *inst, struct gl_shader *target,
1228 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -07001229{
1230 class remap_visitor : public ir_hierarchical_visitor {
1231 public:
Eric Anholt8273bd42010-08-04 12:34:56 -07001232 remap_visitor(struct gl_shader *target,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001233 hash_table *temps)
Ian Romanick31a97862010-07-12 18:48:50 -07001234 {
Eric Anholt8273bd42010-08-04 12:34:56 -07001235 this->target = target;
1236 this->symbols = target->symbols;
1237 this->instructions = target->ir;
Ian Romanick7e2aa912010-07-19 17:12:42 -07001238 this->temps = temps;
Ian Romanick31a97862010-07-12 18:48:50 -07001239 }
1240
1241 virtual ir_visitor_status visit(ir_dereference_variable *ir)
1242 {
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001243 if (ir->var->data.mode == ir_var_temporary) {
Ian Romanick7e2aa912010-07-19 17:12:42 -07001244 ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
1245
1246 assert(var != NULL);
1247 ir->var = var;
1248 return visit_continue;
1249 }
1250
Ian Romanick31a97862010-07-12 18:48:50 -07001251 ir_variable *const existing =
1252 this->symbols->get_variable(ir->var->name);
1253 if (existing != NULL)
1254 ir->var = existing;
1255 else {
Eric Anholt8273bd42010-08-04 12:34:56 -07001256 ir_variable *copy = ir->var->clone(this->target, NULL);
Ian Romanick31a97862010-07-12 18:48:50 -07001257
Eric Anholt001eee52010-11-05 06:11:24 -07001258 this->symbols->add_variable(copy);
Ian Romanick31a97862010-07-12 18:48:50 -07001259 this->instructions->push_head(copy);
Ian Romanick7e2aa912010-07-19 17:12:42 -07001260 ir->var = copy;
Ian Romanick31a97862010-07-12 18:48:50 -07001261 }
1262
1263 return visit_continue;
1264 }
1265
1266 private:
Eric Anholt8273bd42010-08-04 12:34:56 -07001267 struct gl_shader *target;
Ian Romanick31a97862010-07-12 18:48:50 -07001268 glsl_symbol_table *symbols;
1269 exec_list *instructions;
Ian Romanick7e2aa912010-07-19 17:12:42 -07001270 hash_table *temps;
Ian Romanick31a97862010-07-12 18:48:50 -07001271 };
1272
Eric Anholt8273bd42010-08-04 12:34:56 -07001273 remap_visitor v(target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -07001274
1275 inst->accept(&v);
1276}
1277
1278
1279/**
1280 * Move non-declarations from one instruction stream to another
1281 *
1282 * The intended usage pattern of this function is to pass the pointer to the
Eric Anholt62c47632010-07-29 13:52:25 -07001283 * 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 -07001284 * pointer) for \c last and \c false for \c make_copies on the first
1285 * call. Successive calls pass the return value of the previous call for
1286 * \c last and \c true for \c make_copies.
1287 *
1288 * \param instructions Source instruction stream
1289 * \param last Instruction after which new instructions should be
1290 * inserted in the target instruction stream
1291 * \param make_copies Flag selecting whether instructions in \c instructions
1292 * should be copied (via \c ir_instruction::clone) into the
1293 * target list or moved.
1294 *
1295 * \return
1296 * The new "last" instruction in the target instruction stream. This pointer
1297 * is suitable for use as the \c last parameter of a later call to this
1298 * function.
1299 */
1300exec_node *
1301move_non_declarations(exec_list *instructions, exec_node *last,
1302 bool make_copies, gl_shader *target)
1303{
Ian Romanick7e2aa912010-07-19 17:12:42 -07001304 hash_table *temps = NULL;
1305
1306 if (make_copies)
1307 temps = hash_table_ctor(0, hash_table_pointer_hash,
1308 hash_table_pointer_compare);
1309
Matt Turnerc6a16f62014-06-24 21:58:35 -07001310 foreach_in_list_safe(ir_instruction, inst, instructions) {
Ian Romanick7e2aa912010-07-19 17:12:42 -07001311 if (inst->as_function())
Ian Romanick31a97862010-07-12 18:48:50 -07001312 continue;
1313
Ian Romanick7e2aa912010-07-19 17:12:42 -07001314 ir_variable *var = inst->as_variable();
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001315 if ((var != NULL) && (var->data.mode != ir_var_temporary))
Ian Romanick7e2aa912010-07-19 17:12:42 -07001316 continue;
1317
1318 assert(inst->as_assignment()
Kenneth Graunked884f602012-03-20 15:56:37 -07001319 || inst->as_call()
Kenneth Graunkeb45a68e2012-10-24 13:17:24 -07001320 || inst->as_if() /* for initializers with the ?: operator */
Tapani Pälli33ee2c62013-12-12 13:51:01 +02001321 || ((var != NULL) && (var->data.mode == ir_var_temporary)));
Ian Romanick31a97862010-07-12 18:48:50 -07001322
1323 if (make_copies) {
Eric Anholt8273bd42010-08-04 12:34:56 -07001324 inst = inst->clone(target, NULL);
Ian Romanick7e2aa912010-07-19 17:12:42 -07001325
1326 if (var != NULL)
1327 hash_table_insert(temps, inst, var);
1328 else
Eric Anholt8273bd42010-08-04 12:34:56 -07001329 remap_variables(inst, target, temps);
Ian Romanick31a97862010-07-12 18:48:50 -07001330 } else {
1331 inst->remove();
1332 }
1333
1334 last->insert_after(inst);
1335 last = inst;
1336 }
1337
Ian Romanick7e2aa912010-07-19 17:12:42 -07001338 if (make_copies)
1339 hash_table_dtor(temps);
1340
Ian Romanick31a97862010-07-12 18:48:50 -07001341 return last;
1342}
1343
Ian Romanick15ce87e2010-07-09 15:28:22 -07001344
1345/**
Brian Paul84a12732012-02-02 20:10:40 -07001346 * This class is only used in link_intrastage_shaders() below but declaring
1347 * it inside that function leads to compiler warnings with some versions of
1348 * gcc.
1349 */
1350class array_sizing_visitor : public ir_hierarchical_visitor {
1351public:
Paul Berry15e05b92013-09-25 14:07:37 -07001352 array_sizing_visitor()
1353 : mem_ctx(ralloc_context(NULL)),
1354 unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash,
1355 hash_table_pointer_compare))
1356 {
1357 }
1358
1359 ~array_sizing_visitor()
1360 {
1361 hash_table_dtor(this->unnamed_interfaces);
1362 ralloc_free(this->mem_ctx);
1363 }
1364
Brian Paul84a12732012-02-02 20:10:40 -07001365 virtual ir_visitor_status visit(ir_variable *var)
1366 {
Tapani Pälli447bb902013-12-12 15:08:59 +02001367 fixup_type(&var->type, var->data.max_array_access);
Paul Berrye2266692013-09-23 10:44:19 -07001368 if (var->type->is_interface()) {
1369 if (interface_contains_unsized_arrays(var->type)) {
1370 const glsl_type *new_type =
Ian Romanick21df0162014-05-23 18:57:36 -07001371 resize_interface_members(var->type,
1372 var->get_max_ifc_array_access());
Paul Berrye2266692013-09-23 10:44:19 -07001373 var->type = new_type;
1374 var->change_interface_type(new_type);
1375 }
1376 } else if (var->type->is_array() &&
1377 var->type->fields.array->is_interface()) {
1378 if (interface_contains_unsized_arrays(var->type->fields.array)) {
1379 const glsl_type *new_type =
1380 resize_interface_members(var->type->fields.array,
Ian Romanick21df0162014-05-23 18:57:36 -07001381 var->get_max_ifc_array_access());
Paul Berrye2266692013-09-23 10:44:19 -07001382 var->change_interface_type(new_type);
Timothy Arceri939dc282015-03-14 12:40:20 +11001383 var->type = update_interface_members_array(var->type, new_type);
Paul Berrye2266692013-09-23 10:44:19 -07001384 }
Paul Berry15e05b92013-09-25 14:07:37 -07001385 } else if (const glsl_type *ifc_type = var->get_interface_type()) {
1386 /* Store a pointer to the variable in the unnamed_interfaces
1387 * hashtable.
1388 */
1389 ir_variable **interface_vars = (ir_variable **)
1390 hash_table_find(this->unnamed_interfaces, ifc_type);
1391 if (interface_vars == NULL) {
1392 interface_vars = rzalloc_array(mem_ctx, ir_variable *,
1393 ifc_type->length);
1394 hash_table_insert(this->unnamed_interfaces, interface_vars,
1395 ifc_type);
1396 }
1397 unsigned index = ifc_type->field_index(var->name);
1398 assert(index < ifc_type->length);
1399 assert(interface_vars[index] == NULL);
1400 interface_vars[index] = var;
Brian Paul84a12732012-02-02 20:10:40 -07001401 }
1402 return visit_continue;
1403 }
Paul Berrye2266692013-09-23 10:44:19 -07001404
Paul Berry15e05b92013-09-25 14:07:37 -07001405 /**
1406 * For each unnamed interface block that was discovered while running the
1407 * visitor, adjust the interface type to reflect the newly assigned array
1408 * sizes, and fix up the ir_variable nodes to point to the new interface
1409 * type.
1410 */
1411 void fixup_unnamed_interface_types()
1412 {
1413 hash_table_call_foreach(this->unnamed_interfaces,
1414 fixup_unnamed_interface_type, NULL);
1415 }
1416
Paul Berrye2266692013-09-23 10:44:19 -07001417private:
1418 /**
1419 * If the type pointed to by \c type represents an unsized array, replace
1420 * it with a sized array whose size is determined by max_array_access.
1421 */
1422 static void fixup_type(const glsl_type **type, unsigned max_array_access)
1423 {
Timothy Arcerib59c5922013-10-23 21:31:27 +11001424 if ((*type)->is_unsized_array()) {
Paul Berrye2266692013-09-23 10:44:19 -07001425 *type = glsl_type::get_array_instance((*type)->fields.array,
1426 max_array_access + 1);
1427 assert(*type != NULL);
1428 }
1429 }
1430
Timothy Arceri939dc282015-03-14 12:40:20 +11001431 static const glsl_type *
1432 update_interface_members_array(const glsl_type *type,
1433 const glsl_type *new_interface_type)
1434 {
1435 const glsl_type *element_type = type->fields.array;
1436 if (element_type->is_array()) {
1437 const glsl_type *new_array_type =
1438 update_interface_members_array(element_type, new_interface_type);
1439 return glsl_type::get_array_instance(new_array_type, type->length);
1440 } else {
1441 return glsl_type::get_array_instance(new_interface_type,
1442 type->length);
1443 }
1444 }
1445
Paul Berrye2266692013-09-23 10:44:19 -07001446 /**
1447 * Determine whether the given interface type contains unsized arrays (if
1448 * it doesn't, array_sizing_visitor doesn't need to process it).
1449 */
1450 static bool interface_contains_unsized_arrays(const glsl_type *type)
1451 {
1452 for (unsigned i = 0; i < type->length; i++) {
1453 const glsl_type *elem_type = type->fields.structure[i].type;
Timothy Arcerib59c5922013-10-23 21:31:27 +11001454 if (elem_type->is_unsized_array())
Paul Berrye2266692013-09-23 10:44:19 -07001455 return true;
1456 }
1457 return false;
1458 }
1459
1460 /**
1461 * Create a new interface type based on the given type, with unsized arrays
1462 * replaced by sized arrays whose size is determined by
1463 * max_ifc_array_access.
1464 */
1465 static const glsl_type *
1466 resize_interface_members(const glsl_type *type,
1467 const unsigned *max_ifc_array_access)
1468 {
1469 unsigned num_fields = type->length;
1470 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1471 memcpy(fields, type->fields.structure,
1472 num_fields * sizeof(*fields));
1473 for (unsigned i = 0; i < num_fields; i++) {
1474 fixup_type(&fields[i].type, max_ifc_array_access[i]);
1475 }
1476 glsl_interface_packing packing =
1477 (glsl_interface_packing) type->interface_packing;
1478 const glsl_type *new_ifc_type =
1479 glsl_type::get_interface_instance(fields, num_fields,
1480 packing, type->name);
1481 delete [] fields;
1482 return new_ifc_type;
1483 }
Paul Berry15e05b92013-09-25 14:07:37 -07001484
1485 static void fixup_unnamed_interface_type(const void *key, void *data,
1486 void *)
1487 {
1488 const glsl_type *ifc_type = (const glsl_type *) key;
1489 ir_variable **interface_vars = (ir_variable **) data;
1490 unsigned num_fields = ifc_type->length;
1491 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1492 memcpy(fields, ifc_type->fields.structure,
1493 num_fields * sizeof(*fields));
1494 bool interface_type_changed = false;
1495 for (unsigned i = 0; i < num_fields; i++) {
1496 if (interface_vars[i] != NULL &&
1497 fields[i].type != interface_vars[i]->type) {
1498 fields[i].type = interface_vars[i]->type;
1499 interface_type_changed = true;
1500 }
1501 }
1502 if (!interface_type_changed) {
1503 delete [] fields;
1504 return;
1505 }
1506 glsl_interface_packing packing =
1507 (glsl_interface_packing) ifc_type->interface_packing;
1508 const glsl_type *new_ifc_type =
1509 glsl_type::get_interface_instance(fields, num_fields, packing,
1510 ifc_type->name);
1511 delete [] fields;
1512 for (unsigned i = 0; i < num_fields; i++) {
1513 if (interface_vars[i] != NULL)
1514 interface_vars[i]->change_interface_type(new_ifc_type);
1515 }
1516 }
1517
1518 /**
1519 * Memory context used to allocate the data in \c unnamed_interfaces.
1520 */
1521 void *mem_ctx;
1522
1523 /**
1524 * Hash table from const glsl_type * to an array of ir_variable *'s
1525 * pointing to the ir_variables constituting each unnamed interface block.
1526 */
1527 hash_table *unnamed_interfaces;
Brian Paul84a12732012-02-02 20:10:40 -07001528};
1529
Chris Forbes7c758c52014-09-21 13:33:14 +12001530
1531/**
1532 * Performs the cross-validation of tessellation control shader vertices and
1533 * layout qualifiers for the attached tessellation control shaders,
1534 * and propagates them to the linked TCS and linked shader program.
1535 */
1536static void
1537link_tcs_out_layout_qualifiers(struct gl_shader_program *prog,
1538 struct gl_shader *linked_shader,
1539 struct gl_shader **shader_list,
1540 unsigned num_shaders)
1541{
1542 linked_shader->TessCtrl.VerticesOut = 0;
1543
1544 if (linked_shader->Stage != MESA_SHADER_TESS_CTRL)
1545 return;
1546
1547 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1548 *
1549 * "All tessellation control shader layout declarations in a program
1550 * must specify the same output patch vertex count. There must be at
1551 * least one layout qualifier specifying an output patch vertex count
1552 * in any program containing tessellation control shaders; however,
1553 * such a declaration is not required in all tessellation control
1554 * shaders."
1555 */
1556
1557 for (unsigned i = 0; i < num_shaders; i++) {
1558 struct gl_shader *shader = shader_list[i];
1559
1560 if (shader->TessCtrl.VerticesOut != 0) {
1561 if (linked_shader->TessCtrl.VerticesOut != 0 &&
1562 linked_shader->TessCtrl.VerticesOut != shader->TessCtrl.VerticesOut) {
1563 linker_error(prog, "tessellation control shader defined with "
1564 "conflicting output vertex count (%d and %d)\n",
1565 linked_shader->TessCtrl.VerticesOut,
1566 shader->TessCtrl.VerticesOut);
1567 return;
1568 }
1569 linked_shader->TessCtrl.VerticesOut = shader->TessCtrl.VerticesOut;
1570 }
1571 }
1572
1573 /* Just do the intrastage -> interstage propagation right now,
1574 * since we already know we're in the right type of shader program
1575 * for doing it.
1576 */
1577 if (linked_shader->TessCtrl.VerticesOut == 0) {
1578 linker_error(prog, "tessellation control shader didn't declare "
1579 "vertices out layout qualifier\n");
1580 return;
1581 }
1582 prog->TessCtrl.VerticesOut = linked_shader->TessCtrl.VerticesOut;
1583}
1584
1585
1586/**
1587 * Performs the cross-validation of tessellation evaluation shader
1588 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1589 * for the attached tessellation evaluation shaders, and propagates them
1590 * to the linked TES and linked shader program.
1591 */
1592static void
1593link_tes_in_layout_qualifiers(struct gl_shader_program *prog,
1594 struct gl_shader *linked_shader,
1595 struct gl_shader **shader_list,
1596 unsigned num_shaders)
1597{
1598 linked_shader->TessEval.PrimitiveMode = PRIM_UNKNOWN;
1599 linked_shader->TessEval.Spacing = 0;
1600 linked_shader->TessEval.VertexOrder = 0;
1601 linked_shader->TessEval.PointMode = -1;
1602
1603 if (linked_shader->Stage != MESA_SHADER_TESS_EVAL)
1604 return;
1605
1606 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1607 *
1608 * "At least one tessellation evaluation shader (compilation unit) in
1609 * a program must declare a primitive mode in its input layout.
1610 * Declaration vertex spacing, ordering, and point mode identifiers is
1611 * optional. It is not required that all tessellation evaluation
1612 * shaders in a program declare a primitive mode. If spacing or
1613 * vertex ordering declarations are omitted, the tessellation
1614 * primitive generator will use equal spacing or counter-clockwise
1615 * vertex ordering, respectively. If a point mode declaration is
1616 * omitted, the tessellation primitive generator will produce lines or
1617 * triangles according to the primitive mode."
1618 */
1619
1620 for (unsigned i = 0; i < num_shaders; i++) {
1621 struct gl_shader *shader = shader_list[i];
1622
1623 if (shader->TessEval.PrimitiveMode != PRIM_UNKNOWN) {
1624 if (linked_shader->TessEval.PrimitiveMode != PRIM_UNKNOWN &&
1625 linked_shader->TessEval.PrimitiveMode != shader->TessEval.PrimitiveMode) {
1626 linker_error(prog, "tessellation evaluation shader defined with "
1627 "conflicting input primitive modes.\n");
1628 return;
1629 }
1630 linked_shader->TessEval.PrimitiveMode = shader->TessEval.PrimitiveMode;
1631 }
1632
1633 if (shader->TessEval.Spacing != 0) {
1634 if (linked_shader->TessEval.Spacing != 0 &&
1635 linked_shader->TessEval.Spacing != shader->TessEval.Spacing) {
1636 linker_error(prog, "tessellation evaluation shader defined with "
1637 "conflicting vertex spacing.\n");
1638 return;
1639 }
1640 linked_shader->TessEval.Spacing = shader->TessEval.Spacing;
1641 }
1642
1643 if (shader->TessEval.VertexOrder != 0) {
1644 if (linked_shader->TessEval.VertexOrder != 0 &&
1645 linked_shader->TessEval.VertexOrder != shader->TessEval.VertexOrder) {
1646 linker_error(prog, "tessellation evaluation shader defined with "
1647 "conflicting ordering.\n");
1648 return;
1649 }
1650 linked_shader->TessEval.VertexOrder = shader->TessEval.VertexOrder;
1651 }
1652
1653 if (shader->TessEval.PointMode != -1) {
1654 if (linked_shader->TessEval.PointMode != -1 &&
1655 linked_shader->TessEval.PointMode != shader->TessEval.PointMode) {
1656 linker_error(prog, "tessellation evaluation shader defined with "
1657 "conflicting point modes.\n");
1658 return;
1659 }
1660 linked_shader->TessEval.PointMode = shader->TessEval.PointMode;
1661 }
1662
1663 }
1664
1665 /* Just do the intrastage -> interstage propagation right now,
1666 * since we already know we're in the right type of shader program
1667 * for doing it.
1668 */
1669 if (linked_shader->TessEval.PrimitiveMode == PRIM_UNKNOWN) {
1670 linker_error(prog,
1671 "tessellation evaluation shader didn't declare input "
1672 "primitive modes.\n");
1673 return;
1674 }
1675 prog->TessEval.PrimitiveMode = linked_shader->TessEval.PrimitiveMode;
1676
1677 if (linked_shader->TessEval.Spacing == 0)
1678 linked_shader->TessEval.Spacing = GL_EQUAL;
1679 prog->TessEval.Spacing = linked_shader->TessEval.Spacing;
1680
1681 if (linked_shader->TessEval.VertexOrder == 0)
1682 linked_shader->TessEval.VertexOrder = GL_CCW;
1683 prog->TessEval.VertexOrder = linked_shader->TessEval.VertexOrder;
1684
1685 if (linked_shader->TessEval.PointMode == -1)
1686 linked_shader->TessEval.PointMode = GL_FALSE;
1687 prog->TessEval.PointMode = linked_shader->TessEval.PointMode;
1688}
1689
1690
Brian Paul84a12732012-02-02 20:10:40 -07001691/**
Anuj Phogat35f11e82014-02-05 15:01:58 -08001692 * Performs the cross-validation of layout qualifiers specified in
1693 * redeclaration of gl_FragCoord for the attached fragment shaders,
1694 * and propagates them to the linked FS and linked shader program.
1695 */
1696static void
1697link_fs_input_layout_qualifiers(struct gl_shader_program *prog,
1698 struct gl_shader *linked_shader,
1699 struct gl_shader **shader_list,
1700 unsigned num_shaders)
1701{
1702 linked_shader->redeclares_gl_fragcoord = false;
1703 linked_shader->uses_gl_fragcoord = false;
1704 linked_shader->origin_upper_left = false;
1705 linked_shader->pixel_center_integer = false;
1706
Anuj Phogat9bcb0a82014-02-10 14:12:40 -08001707 if (linked_shader->Stage != MESA_SHADER_FRAGMENT ||
1708 (prog->Version < 150 && !prog->ARB_fragment_coord_conventions_enable))
Anuj Phogat35f11e82014-02-05 15:01:58 -08001709 return;
1710
1711 for (unsigned i = 0; i < num_shaders; i++) {
1712 struct gl_shader *shader = shader_list[i];
1713 /* From the GLSL 1.50 spec, page 39:
1714 *
1715 * "If gl_FragCoord is redeclared in any fragment shader in a program,
1716 * it must be redeclared in all the fragment shaders in that program
1717 * that have a static use gl_FragCoord."
Anuj Phogat35f11e82014-02-05 15:01:58 -08001718 */
1719 if ((linked_shader->redeclares_gl_fragcoord
1720 && !shader->redeclares_gl_fragcoord
Anuj Phogatd8208312015-03-05 11:07:52 -08001721 && shader->uses_gl_fragcoord)
Anuj Phogat35f11e82014-02-05 15:01:58 -08001722 || (shader->redeclares_gl_fragcoord
1723 && !linked_shader->redeclares_gl_fragcoord
Anuj Phogatd8208312015-03-05 11:07:52 -08001724 && linked_shader->uses_gl_fragcoord)) {
Anuj Phogat35f11e82014-02-05 15:01:58 -08001725 linker_error(prog, "fragment shader defined with conflicting "
1726 "layout qualifiers for gl_FragCoord\n");
1727 }
1728
1729 /* From the GLSL 1.50 spec, page 39:
1730 *
1731 * "All redeclarations of gl_FragCoord in all fragment shaders in a
1732 * single program must have the same set of qualifiers."
1733 */
1734 if (linked_shader->redeclares_gl_fragcoord && shader->redeclares_gl_fragcoord
1735 && (shader->origin_upper_left != linked_shader->origin_upper_left
1736 || shader->pixel_center_integer != linked_shader->pixel_center_integer)) {
1737 linker_error(prog, "fragment shader defined with conflicting "
1738 "layout qualifiers for gl_FragCoord\n");
1739 }
1740
Martin Peres87a4bc52015-05-21 15:51:09 +03001741 /* Update the linked shader state. Note that uses_gl_fragcoord should
1742 * accumulate the results. The other values should replace. If there
Anuj Phogat35f11e82014-02-05 15:01:58 -08001743 * are multiple redeclarations, all the fields except uses_gl_fragcoord
1744 * are already known to be the same.
1745 */
1746 if (shader->redeclares_gl_fragcoord || shader->uses_gl_fragcoord) {
1747 linked_shader->redeclares_gl_fragcoord =
1748 shader->redeclares_gl_fragcoord;
1749 linked_shader->uses_gl_fragcoord = linked_shader->uses_gl_fragcoord
1750 || shader->uses_gl_fragcoord;
1751 linked_shader->origin_upper_left = shader->origin_upper_left;
1752 linked_shader->pixel_center_integer = shader->pixel_center_integer;
1753 }
Francisco Jerezce0e1512015-01-28 17:42:37 +02001754
1755 linked_shader->EarlyFragmentTests |= shader->EarlyFragmentTests;
Anuj Phogat35f11e82014-02-05 15:01:58 -08001756 }
1757}
1758
1759/**
Eric Anholt6065a872013-06-12 18:12:40 -07001760 * Performs the cross-validation of geometry shader max_vertices and
1761 * primitive type layout qualifiers for the attached geometry shaders,
1762 * and propagates them to the linked GS and linked shader program.
1763 */
1764static void
1765link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
1766 struct gl_shader *linked_shader,
1767 struct gl_shader **shader_list,
1768 unsigned num_shaders)
1769{
1770 linked_shader->Geom.VerticesOut = 0;
Jordan Justen31340202014-01-25 02:17:21 -08001771 linked_shader->Geom.Invocations = 0;
Eric Anholt6065a872013-06-12 18:12:40 -07001772 linked_shader->Geom.InputType = PRIM_UNKNOWN;
1773 linked_shader->Geom.OutputType = PRIM_UNKNOWN;
1774
1775 /* No in/out qualifiers defined for anything but GLSL 1.50+
1776 * geometry shaders so far.
1777 */
Paul Berrye3b86f02014-01-07 10:58:56 -08001778 if (linked_shader->Stage != MESA_SHADER_GEOMETRY || prog->Version < 150)
Eric Anholt6065a872013-06-12 18:12:40 -07001779 return;
1780
1781 /* From the GLSL 1.50 spec, page 46:
1782 *
1783 * "All geometry shader output layout declarations in a program
1784 * must declare the same layout and same value for
1785 * max_vertices. There must be at least one geometry output
1786 * layout declaration somewhere in a program, but not all
1787 * geometry shaders (compilation units) are required to
1788 * declare it."
1789 */
1790
1791 for (unsigned i = 0; i < num_shaders; i++) {
1792 struct gl_shader *shader = shader_list[i];
1793
1794 if (shader->Geom.InputType != PRIM_UNKNOWN) {
1795 if (linked_shader->Geom.InputType != PRIM_UNKNOWN &&
1796 linked_shader->Geom.InputType != shader->Geom.InputType) {
1797 linker_error(prog, "geometry shader defined with conflicting "
1798 "input types\n");
1799 return;
1800 }
1801 linked_shader->Geom.InputType = shader->Geom.InputType;
1802 }
1803
1804 if (shader->Geom.OutputType != PRIM_UNKNOWN) {
1805 if (linked_shader->Geom.OutputType != PRIM_UNKNOWN &&
1806 linked_shader->Geom.OutputType != shader->Geom.OutputType) {
1807 linker_error(prog, "geometry shader defined with conflicting "
1808 "output types\n");
1809 return;
1810 }
1811 linked_shader->Geom.OutputType = shader->Geom.OutputType;
1812 }
1813
1814 if (shader->Geom.VerticesOut != 0) {
1815 if (linked_shader->Geom.VerticesOut != 0 &&
1816 linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) {
1817 linker_error(prog, "geometry shader defined with conflicting "
1818 "output vertex count (%d and %d)\n",
1819 linked_shader->Geom.VerticesOut,
1820 shader->Geom.VerticesOut);
1821 return;
1822 }
1823 linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut;
1824 }
Jordan Justen31340202014-01-25 02:17:21 -08001825
1826 if (shader->Geom.Invocations != 0) {
1827 if (linked_shader->Geom.Invocations != 0 &&
1828 linked_shader->Geom.Invocations != shader->Geom.Invocations) {
1829 linker_error(prog, "geometry shader defined with conflicting "
1830 "invocation count (%d and %d)\n",
1831 linked_shader->Geom.Invocations,
1832 shader->Geom.Invocations);
1833 return;
1834 }
1835 linked_shader->Geom.Invocations = shader->Geom.Invocations;
1836 }
Eric Anholt6065a872013-06-12 18:12:40 -07001837 }
1838
1839 /* Just do the intrastage -> interstage propagation right now,
1840 * since we already know we're in the right type of shader program
1841 * for doing it.
1842 */
1843 if (linked_shader->Geom.InputType == PRIM_UNKNOWN) {
1844 linker_error(prog,
1845 "geometry shader didn't declare primitive input type\n");
1846 return;
1847 }
1848 prog->Geom.InputType = linked_shader->Geom.InputType;
1849
1850 if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) {
1851 linker_error(prog,
1852 "geometry shader didn't declare primitive output type\n");
1853 return;
1854 }
1855 prog->Geom.OutputType = linked_shader->Geom.OutputType;
1856
1857 if (linked_shader->Geom.VerticesOut == 0) {
1858 linker_error(prog,
1859 "geometry shader didn't declare max_vertices\n");
1860 return;
1861 }
1862 prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut;
Jordan Justen31340202014-01-25 02:17:21 -08001863
1864 if (linked_shader->Geom.Invocations == 0)
1865 linked_shader->Geom.Invocations = 1;
1866
1867 prog->Geom.Invocations = linked_shader->Geom.Invocations;
Eric Anholt6065a872013-06-12 18:12:40 -07001868}
1869
Paul Berry28ce6042014-01-08 11:59:28 -08001870
1871/**
1872 * Perform cross-validation of compute shader local_size_{x,y,z} layout
1873 * qualifiers for the attached compute shaders, and propagate them to the
1874 * linked CS and linked shader program.
1875 */
1876static void
1877link_cs_input_layout_qualifiers(struct gl_shader_program *prog,
1878 struct gl_shader *linked_shader,
1879 struct gl_shader **shader_list,
1880 unsigned num_shaders)
1881{
1882 for (int i = 0; i < 3; i++)
1883 linked_shader->Comp.LocalSize[i] = 0;
1884
1885 /* This function is called for all shader stages, but it only has an effect
1886 * for compute shaders.
1887 */
1888 if (linked_shader->Stage != MESA_SHADER_COMPUTE)
1889 return;
1890
1891 /* From the ARB_compute_shader spec, in the section describing local size
1892 * declarations:
1893 *
1894 * If multiple compute shaders attached to a single program object
1895 * declare local work-group size, the declarations must be identical;
1896 * otherwise a link-time error results. Furthermore, if a program
1897 * object contains any compute shaders, at least one must contain an
1898 * input layout qualifier specifying the local work sizes of the
1899 * program, or a link-time error will occur.
1900 */
1901 for (unsigned sh = 0; sh < num_shaders; sh++) {
1902 struct gl_shader *shader = shader_list[sh];
1903
1904 if (shader->Comp.LocalSize[0] != 0) {
1905 if (linked_shader->Comp.LocalSize[0] != 0) {
1906 for (int i = 0; i < 3; i++) {
1907 if (linked_shader->Comp.LocalSize[i] !=
1908 shader->Comp.LocalSize[i]) {
1909 linker_error(prog, "compute shader defined with conflicting "
1910 "local sizes\n");
1911 return;
1912 }
1913 }
1914 }
1915 for (int i = 0; i < 3; i++)
1916 linked_shader->Comp.LocalSize[i] = shader->Comp.LocalSize[i];
1917 }
1918 }
1919
1920 /* Just do the intrastage -> interstage propagation right now,
1921 * since we already know we're in the right type of shader program
1922 * for doing it.
1923 */
1924 if (linked_shader->Comp.LocalSize[0] == 0) {
1925 linker_error(prog, "compute shader didn't declare local size\n");
1926 return;
1927 }
1928 for (int i = 0; i < 3; i++)
1929 prog->Comp.LocalSize[i] = linked_shader->Comp.LocalSize[i];
1930}
1931
1932
Eric Anholt6065a872013-06-12 18:12:40 -07001933/**
Ian Romanick3fb87872010-07-09 14:09:34 -07001934 * Combine a group of shaders for a single stage to generate a linked shader
1935 *
1936 * \note
1937 * If this function is supplied a single shader, it is cloned, and the new
1938 * shader is returned.
1939 */
1940static struct gl_shader *
Kenneth Graunke2da02e72010-11-17 11:03:57 -08001941link_intrastage_shaders(void *mem_ctx,
1942 struct gl_context *ctx,
Eric Anholt5d0f4302010-08-18 12:02:35 -07001943 struct gl_shader_program *prog,
Ian Romanick3fb87872010-07-09 14:09:34 -07001944 struct gl_shader **shader_list,
1945 unsigned num_shaders)
1946{
Eric Anholtf609cf72012-04-27 13:52:56 -07001947 struct gl_uniform_block *uniform_blocks = NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -07001948
Ian Romanick13f782c2010-06-29 18:53:38 -07001949 /* Check that global variables defined in multiple shaders are consistent.
1950 */
Paul Berryb95d2372013-07-27 11:08:31 -07001951 cross_validate_globals(prog, shader_list, num_shaders, false);
1952 if (!prog->LinkStatus)
Ian Romanick13f782c2010-06-29 18:53:38 -07001953 return NULL;
1954
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001955 /* Check that interface blocks defined in multiple shaders are consistent.
1956 */
Paul Berryb95d2372013-07-27 11:08:31 -07001957 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
1958 num_shaders);
1959 if (!prog->LinkStatus)
Jordan Justen4a0bcd92013-05-20 23:42:49 -07001960 return NULL;
1961
Paul Berry4682b9b2013-07-27 15:07:08 -07001962 /* Link up uniform blocks defined within this stage. */
1963 const unsigned num_uniform_blocks =
Ian Romanick514f8c72013-01-22 01:09:16 -05001964 link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
1965 &uniform_blocks);
Juha-Pekka Heikkila088da372014-04-03 17:06:42 +03001966 if (!prog->LinkStatus)
1967 return NULL;
Eric Anholtf609cf72012-04-27 13:52:56 -07001968
Ian Romanick13f782c2010-06-29 18:53:38 -07001969 /* Check that there is only a single definition of each function signature
1970 * across all shaders.
1971 */
1972 for (unsigned i = 0; i < (num_shaders - 1); i++) {
Matt Turner4d784462014-06-24 21:34:05 -07001973 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
1974 ir_function *const f = node->as_function();
Ian Romanick13f782c2010-06-29 18:53:38 -07001975
1976 if (f == NULL)
1977 continue;
1978
1979 for (unsigned j = i + 1; j < num_shaders; j++) {
1980 ir_function *const other =
1981 shader_list[j]->symbols->get_function(f->name);
1982
1983 /* If the other shader has no function (and therefore no function
1984 * signatures) with the same name, skip to the next shader.
1985 */
1986 if (other == NULL)
1987 continue;
1988
Matt Turner4d784462014-06-24 21:34:05 -07001989 foreach_in_list(ir_function_signature, sig, &f->signatures) {
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001990 if (!sig->is_defined || sig->is_builtin())
Ian Romanick13f782c2010-06-29 18:53:38 -07001991 continue;
1992
1993 ir_function_signature *other_sig =
Kenneth Graunke3e820e32013-08-30 23:11:55 -07001994 other->exact_matching_signature(NULL, &sig->parameters);
Ian Romanick13f782c2010-06-29 18:53:38 -07001995
1996 if ((other_sig != NULL) && other_sig->is_defined
Kenneth Graunke4b0bac02013-08-30 16:12:55 -07001997 && !other_sig->is_builtin()) {
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07001998 linker_error(prog, "function `%s' is multiply defined\n",
Ian Romanick586e7412011-07-28 14:04:09 -07001999 f->name);
Ian Romanick13f782c2010-06-29 18:53:38 -07002000 return NULL;
2001 }
2002 }
2003 }
2004 }
2005 }
2006
2007 /* Find the shader that defines main, and make a clone of it.
2008 *
2009 * Starting with the clone, search for undefined references. If one is
2010 * found, find the shader that defines it. Clone the reference and add
2011 * it to the shader. Repeat until there are no undefined references or
2012 * until a reference cannot be resolved.
2013 */
Ian Romanick15ce87e2010-07-09 15:28:22 -07002014 gl_shader *main = NULL;
2015 for (unsigned i = 0; i < num_shaders; i++) {
Jordan Justenc4d049f2015-08-17 12:22:34 -07002016 if (_mesa_get_main_function_signature(shader_list[i]) != NULL) {
Ian Romanick15ce87e2010-07-09 15:28:22 -07002017 main = shader_list[i];
2018 break;
2019 }
2020 }
Ian Romanick13f782c2010-06-29 18:53:38 -07002021
Ian Romanick15ce87e2010-07-09 15:28:22 -07002022 if (main == NULL) {
Ian Romanick586e7412011-07-28 14:04:09 -07002023 linker_error(prog, "%s shader lacks `main'\n",
Paul Berrye3b86f02014-01-07 10:58:56 -08002024 _mesa_shader_stage_to_string(shader_list[0]->Stage));
Ian Romanick15ce87e2010-07-09 15:28:22 -07002025 return NULL;
2026 }
2027
Ian Romanick4a455952010-10-13 15:13:02 -07002028 gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
Ian Romanick15ce87e2010-07-09 15:28:22 -07002029 linked->ir = new(linked) exec_list;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08002030 clone_ir_list(mem_ctx, linked->ir, main->ir);
Ian Romanick15ce87e2010-07-09 15:28:22 -07002031
Eric Anholtf609cf72012-04-27 13:52:56 -07002032 linked->UniformBlocks = uniform_blocks;
2033 linked->NumUniformBlocks = num_uniform_blocks;
2034 ralloc_steal(linked, linked->UniformBlocks);
2035
Anuj Phogat35f11e82014-02-05 15:01:58 -08002036 link_fs_input_layout_qualifiers(prog, linked, shader_list, num_shaders);
Chris Forbes7c758c52014-09-21 13:33:14 +12002037 link_tcs_out_layout_qualifiers(prog, linked, shader_list, num_shaders);
2038 link_tes_in_layout_qualifiers(prog, linked, shader_list, num_shaders);
Eric Anholt6065a872013-06-12 18:12:40 -07002039 link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
Paul Berry28ce6042014-01-08 11:59:28 -08002040 link_cs_input_layout_qualifiers(prog, linked, shader_list, num_shaders);
Eric Anholt6065a872013-06-12 18:12:40 -07002041
Ian Romanick15ce87e2010-07-09 15:28:22 -07002042 populate_symbol_table(linked);
Ian Romanick13f782c2010-06-29 18:53:38 -07002043
Andres Gomezb0e0c262014-10-24 16:51:09 +03002044 /* The pointer to the main function in the final linked shader (i.e., the
Ian Romanick31a97862010-07-12 18:48:50 -07002045 * copy of the original shader that contained the main function).
2046 */
Ian Romanick04d33232014-06-19 12:05:20 -07002047 ir_function_signature *const main_sig =
Jordan Justenc4d049f2015-08-17 12:22:34 -07002048 _mesa_get_main_function_signature(linked);
Ian Romanick31a97862010-07-12 18:48:50 -07002049
2050 /* Move any instructions other than variable declarations or function
2051 * declarations into main.
2052 */
Ian Romanick9303e352010-07-19 12:33:54 -07002053 exec_node *insertion_point =
2054 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
2055 linked);
2056
Ian Romanick31a97862010-07-12 18:48:50 -07002057 for (unsigned i = 0; i < num_shaders; i++) {
Ian Romanick9303e352010-07-19 12:33:54 -07002058 if (shader_list[i] == main)
2059 continue;
2060
Ian Romanick31a97862010-07-12 18:48:50 -07002061 insertion_point = move_non_declarations(shader_list[i]->ir,
Ian Romanick9303e352010-07-19 12:33:54 -07002062 insertion_point, true, linked);
Ian Romanick31a97862010-07-12 18:48:50 -07002063 }
2064
Kenneth Graunke5b331f62013-11-23 12:11:34 -08002065 /* Check if any shader needs built-in functions. */
2066 bool need_builtins = false;
Ian Romanickd5be2ac2010-07-20 11:29:46 -07002067 for (unsigned i = 0; i < num_shaders; i++) {
Kenneth Graunke5b331f62013-11-23 12:11:34 -08002068 if (shader_list[i]->uses_builtin_functions) {
2069 need_builtins = true;
2070 break;
2071 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07002072 }
2073
Kenneth Graunke5b331f62013-11-23 12:11:34 -08002074 bool ok;
2075 if (need_builtins) {
2076 /* Make a temporary array one larger than shader_list, which will hold
2077 * the built-in function shader as well.
2078 */
2079 gl_shader **linking_shaders = (gl_shader **)
2080 calloc(num_shaders + 1, sizeof(gl_shader *));
Ian Romanickd5be2ac2010-07-20 11:29:46 -07002081
Juha-Pekka Heikkilad2f04422014-05-07 16:20:12 +03002082 ok = linking_shaders != NULL;
Kenneth Graunke5b331f62013-11-23 12:11:34 -08002083
Juha-Pekka Heikkilad2f04422014-05-07 16:20:12 +03002084 if (ok) {
2085 memcpy(linking_shaders, shader_list, num_shaders * sizeof(gl_shader *));
2086 linking_shaders[num_shaders] = _mesa_glsl_get_builtin_function_shader();
2087
2088 ok = link_function_calls(prog, linked, linking_shaders, num_shaders + 1);
2089
2090 free(linking_shaders);
2091 } else {
2092 _mesa_error_no_memory(__func__);
2093 }
Kenneth Graunke5b331f62013-11-23 12:11:34 -08002094 } else {
2095 ok = link_function_calls(prog, linked, shader_list, num_shaders);
2096 }
2097
2098
2099 if (!ok) {
2100 ctx->Driver.DeleteShader(ctx, linked);
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07002101 return NULL;
Ian Romanick4a455952010-10-13 15:13:02 -07002102 }
Ian Romanickd5be2ac2010-07-20 11:29:46 -07002103
Paul Berryc148ef62011-08-03 15:37:01 -07002104 /* At this point linked should contain all of the linked IR, so
2105 * validate it to make sure nothing went wrong.
2106 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07002107 validate_ir_tree(linked->ir);
Paul Berryc148ef62011-08-03 15:37:01 -07002108
Paul Berry7cfefe62013-07-30 21:13:48 -07002109 /* Set the size of geometry shader input arrays */
Paul Berrye3b86f02014-01-07 10:58:56 -08002110 if (linked->Stage == MESA_SHADER_GEOMETRY) {
Paul Berry7cfefe62013-07-30 21:13:48 -07002111 unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
2112 geom_array_resize_visitor input_resize_visitor(num_vertices, prog);
Matt Turner4d784462014-06-24 21:34:05 -07002113 foreach_in_list(ir_instruction, ir, linked->ir) {
Paul Berry7cfefe62013-07-30 21:13:48 -07002114 ir->accept(&input_resize_visitor);
2115 }
2116 }
2117
Ian Romanickec08b5e2014-06-19 12:06:42 -07002118 if (ctx->Const.VertexID_is_zero_based)
2119 lower_vertex_id(linked);
2120
Chris Forbes8cf72972014-09-07 21:42:50 +12002121 /* Validate correct usage of barrier() in the tess control shader */
2122 if (linked->Stage == MESA_SHADER_TESS_CTRL) {
2123 barrier_use_visitor visitor(prog);
2124 foreach_in_list(ir_instruction, ir, linked->ir) {
2125 ir->accept(&visitor);
2126 }
2127 }
2128
Ian Romanickc87e9ef2011-01-25 12:04:08 -08002129 /* Make a pass over all variable declarations to ensure that arrays with
Ian Romanick6f539212010-12-07 18:30:33 -08002130 * unspecified sizes have a size specified. The size is inferred from the
2131 * max_array_access field.
2132 */
Kenneth Graunke7d2423a2013-08-02 00:35:05 -07002133 array_sizing_visitor v;
2134 v.run(linked->ir);
Paul Berry15e05b92013-09-25 14:07:37 -07002135 v.fixup_unnamed_interface_types();
Ian Romanick6f539212010-12-07 18:30:33 -08002136
Ian Romanick3fb87872010-07-09 14:09:34 -07002137 return linked;
2138}
2139
Eric Anholta721abf2010-08-23 10:32:01 -07002140/**
2141 * Update the sizes of linked shader uniform arrays to the maximum
2142 * array index used.
2143 *
2144 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
2145 *
2146 * If one or more elements of an array are active,
2147 * GetActiveUniform will return the name of the array in name,
2148 * subject to the restrictions listed above. The type of the array
2149 * is returned in type. The size parameter contains the highest
2150 * array element index used, plus one. The compiler or linker
2151 * determines the highest index used. There will be only one
2152 * active uniform reported by the GL per uniform array.
2153
2154 */
2155static void
Eric Anholt586b4b52010-09-28 14:32:16 -07002156update_array_sizes(struct gl_shader_program *prog)
Eric Anholta721abf2010-08-23 10:32:01 -07002157{
Paul Berry665b8d72014-01-07 10:11:39 -08002158 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002159 if (prog->_LinkedShaders[i] == NULL)
2160 continue;
2161
Matt Turner4d784462014-06-24 21:34:05 -07002162 foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) {
2163 ir_variable *const var = node->as_variable();
Eric Anholta721abf2010-08-23 10:32:01 -07002164
Tapani Pälli33ee2c62013-12-12 13:51:01 +02002165 if ((var == NULL) || (var->data.mode != ir_var_uniform) ||
Eric Anholta721abf2010-08-23 10:32:01 -07002166 !var->type->is_array())
2167 continue;
2168
Eric Anholt9feb4032012-05-01 14:43:31 -07002169 /* GL_ARB_uniform_buffer_object says that std140 uniforms
2170 * will not be eliminated. Since we always do std140, just
2171 * don't resize arrays in UBOs.
Francisco Jerez5c114932013-09-11 12:14:46 -07002172 *
2173 * Atomic counters are supposed to get deterministic
2174 * locations assigned based on the declaration ordering and
2175 * sizes, array compaction would mess that up.
Dave Airlie60266862015-04-20 10:27:36 +10002176 *
2177 * Subroutine uniforms are not removed.
Eric Anholt9feb4032012-05-01 14:43:31 -07002178 */
Dave Airlie60266862015-04-20 10:27:36 +10002179 if (var->is_in_buffer_block() || var->type->contains_atomic() ||
2180 var->type->contains_subroutine())
Eric Anholt9feb4032012-05-01 14:43:31 -07002181 continue;
2182
Tapani Pälli447bb902013-12-12 15:08:59 +02002183 unsigned int size = var->data.max_array_access;
Paul Berry665b8d72014-01-07 10:11:39 -08002184 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07002185 if (prog->_LinkedShaders[j] == NULL)
2186 continue;
2187
Matt Turner4d784462014-06-24 21:34:05 -07002188 foreach_in_list(ir_instruction, node2, prog->_LinkedShaders[j]->ir) {
2189 ir_variable *other_var = node2->as_variable();
Eric Anholta721abf2010-08-23 10:32:01 -07002190 if (!other_var)
2191 continue;
2192
2193 if (strcmp(var->name, other_var->name) == 0 &&
Tapani Pälli447bb902013-12-12 15:08:59 +02002194 other_var->data.max_array_access > size) {
2195 size = other_var->data.max_array_access;
Eric Anholta721abf2010-08-23 10:32:01 -07002196 }
2197 }
2198 }
Eric Anholt586b4b52010-09-28 14:32:16 -07002199
Fabian Bieler63684782013-06-14 13:37:07 +02002200 if (size + 1 != var->type->length) {
Ian Romanick89d81ab2011-01-25 10:41:20 -08002201 /* If this is a built-in uniform (i.e., it's backed by some
2202 * fixed-function state), adjust the number of state slots to
2203 * match the new array size. The number of slots per array entry
Bryan Cainf18a0862011-04-23 19:29:15 -05002204 * is not known. It seems safe to assume that the total number of
Ian Romanick89d81ab2011-01-25 10:41:20 -08002205 * slots is an integer multiple of the number of array elements.
2206 * Determine the number of slots per array element by dividing by
2207 * the old (total) size.
2208 */
Ian Romanick5aa8d812014-05-14 19:47:28 -07002209 const unsigned num_slots = var->get_num_state_slots();
2210 if (num_slots > 0) {
2211 var->set_num_state_slots((size + 1)
2212 * (num_slots / var->type->length));
Ian Romanick89d81ab2011-01-25 10:41:20 -08002213 }
2214
Eric Anholta721abf2010-08-23 10:32:01 -07002215 var->type = glsl_type::get_array_instance(var->type->fields.array,
2216 size + 1);
2217 /* FINISHME: We should update the types of array
2218 * dereferences of this variable now.
2219 */
2220 }
2221 }
2222 }
2223}
2224
Ian Romanick69846702010-06-22 17:29:19 -07002225/**
Chris Forbes7c758c52014-09-21 13:33:14 +12002226 * Resize tessellation evaluation per-vertex inputs to the size of
2227 * tessellation control per-vertex outputs.
2228 */
2229static void
2230resize_tes_inputs(struct gl_context *ctx,
2231 struct gl_shader_program *prog)
2232{
2233 if (prog->_LinkedShaders[MESA_SHADER_TESS_EVAL] == NULL)
2234 return;
2235
2236 gl_shader *const tcs = prog->_LinkedShaders[MESA_SHADER_TESS_CTRL];
2237 gl_shader *const tes = prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
2238
2239 /* If no control shader is present, then the TES inputs are statically
2240 * sized to MaxPatchVertices; the actual size of the arrays won't be
2241 * known until draw time.
2242 */
2243 const int num_vertices = tcs
2244 ? tcs->TessCtrl.VerticesOut
2245 : ctx->Const.MaxPatchVertices;
2246
2247 tess_eval_array_resize_visitor input_resize_visitor(num_vertices, prog);
2248 foreach_in_list(ir_instruction, ir, tes->ir) {
2249 ir->accept(&input_resize_visitor);
2250 }
2251}
2252
2253/**
Bryan Cainf18a0862011-04-23 19:29:15 -05002254 * Find a contiguous set of available bits in a bitmask.
Ian Romanick69846702010-06-22 17:29:19 -07002255 *
2256 * \param used_mask Bits representing used (1) and unused (0) locations
2257 * \param needed_count Number of contiguous bits needed.
2258 *
2259 * \return
2260 * Base location of the available bits on success or -1 on failure.
2261 */
2262int
2263find_available_slots(unsigned used_mask, unsigned needed_count)
2264{
2265 unsigned needed_mask = (1 << needed_count) - 1;
2266 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
2267
2268 /* The comparison to 32 is redundant, but without it GCC emits "warning:
2269 * cannot optimize possibly infinite loops" for the loop below.
2270 */
2271 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
2272 return -1;
2273
2274 for (int i = 0; i <= max_bit_to_test; i++) {
2275 if ((needed_mask & ~used_mask) == needed_mask)
2276 return i;
2277
2278 needed_mask <<= 1;
2279 }
2280
2281 return -1;
2282}
2283
2284
Ian Romanickd32d4f72011-06-27 17:59:58 -07002285/**
Andres Gomezb0e0c262014-10-24 16:51:09 +03002286 * Assign locations for either VS inputs or FS outputs
Ian Romanickd32d4f72011-06-27 17:59:58 -07002287 *
2288 * \param prog Shader program whose variables need locations assigned
Tapani Pällib8689712015-07-27 13:29:20 +03002289 * \param constants Driver specific constant values for the program.
Ian Romanickd32d4f72011-06-27 17:59:58 -07002290 * \param target_index Selector for the program target to receive location
2291 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
2292 * \c MESA_SHADER_FRAGMENT.
Ian Romanickd32d4f72011-06-27 17:59:58 -07002293 *
2294 * \return
2295 * If locations are successfully assigned, true is returned. Otherwise an
2296 * error is emitted to the shader link log and false is returned.
Ian Romanickd32d4f72011-06-27 17:59:58 -07002297 */
Ian Romanick69846702010-06-22 17:29:19 -07002298bool
Ian Romanickd32d4f72011-06-27 17:59:58 -07002299assign_attribute_or_color_locations(gl_shader_program *prog,
Tapani Pällib8689712015-07-27 13:29:20 +03002300 struct gl_constants *constants,
2301 unsigned target_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002302{
Tapani Pällib8689712015-07-27 13:29:20 +03002303 /* Maximum number of generic locations. This corresponds to either the
2304 * maximum number of draw buffers or the maximum number of generic
2305 * attributes.
2306 */
2307 unsigned max_index = (target_index == MESA_SHADER_VERTEX) ?
2308 constants->Program[target_index].MaxAttribs :
2309 MAX2(constants->MaxDrawBuffers, constants->MaxDualSourceDrawBuffers);
2310
Ian Romanickd32d4f72011-06-27 17:59:58 -07002311 /* Mark invalid locations as being used.
Ian Romanick9342d262010-06-22 17:41:37 -07002312 */
Ian Romanickd32d4f72011-06-27 17:59:58 -07002313 unsigned used_locations = (max_index >= 32)
2314 ? ~0 : ~((1 << max_index) - 1);
Kenneth Graunkec3294ca2015-09-02 10:42:57 -07002315 unsigned double_storage_locations = 0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002316
Ian Romanickd32d4f72011-06-27 17:59:58 -07002317 assert((target_index == MESA_SHADER_VERTEX)
2318 || (target_index == MESA_SHADER_FRAGMENT));
2319
2320 gl_shader *const sh = prog->_LinkedShaders[target_index];
2321 if (sh == NULL)
2322 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002323
Ian Romanick69846702010-06-22 17:29:19 -07002324 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002325 *
2326 * 1. Invalidate the location assignments for all vertex shader inputs.
2327 *
2328 * 2. Assign locations for inputs that have user-defined (via
Ian Romanickb12b5d92011-11-04 16:08:52 -07002329 * glBindVertexAttribLocation) locations and outputs that have
2330 * user-defined locations (via glBindFragDataLocation).
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002331 *
Ian Romanick69846702010-06-22 17:29:19 -07002332 * 3. Sort the attributes without assigned locations by number of slots
2333 * required in decreasing order. Fragmentation caused by attribute
2334 * locations assigned by the application may prevent large attributes
2335 * from having enough contiguous space.
2336 *
2337 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002338 */
2339
Ian Romanickd32d4f72011-06-27 17:59:58 -07002340 const int generic_base = (target_index == MESA_SHADER_VERTEX)
Brian Paul7eb7d672011-07-07 16:47:59 -06002341 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002342
Ian Romanickd32d4f72011-06-27 17:59:58 -07002343 const enum ir_variable_mode direction =
Paul Berry42a29d82013-01-11 14:39:32 -08002344 (target_index == MESA_SHADER_VERTEX)
2345 ? ir_var_shader_in : ir_var_shader_out;
Ian Romanickd32d4f72011-06-27 17:59:58 -07002346
2347
Ian Romanick69846702010-06-22 17:29:19 -07002348 /* Temporary storage for the set of attributes that need locations assigned.
2349 */
2350 struct temp_attr {
2351 unsigned slots;
2352 ir_variable *var;
2353
2354 /* Used below in the call to qsort. */
2355 static int compare(const void *a, const void *b)
2356 {
2357 const temp_attr *const l = (const temp_attr *) a;
2358 const temp_attr *const r = (const temp_attr *) b;
2359
2360 /* Reversed because we want a descending order sort below. */
2361 return r->slots - l->slots;
2362 }
2363 } to_assign[16];
2364
2365 unsigned num_attr = 0;
2366
Matt Turner4d784462014-06-24 21:34:05 -07002367 foreach_in_list(ir_instruction, node, sh->ir) {
2368 ir_variable *const var = node->as_variable();
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002369
Tapani Pälli33ee2c62013-12-12 13:51:01 +02002370 if ((var == NULL) || (var->data.mode != (unsigned) direction))
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002371 continue;
2372
Tapani Pälli447bb902013-12-12 15:08:59 +02002373 if (var->data.explicit_location) {
2374 if ((var->data.location >= (int)(max_index + generic_base))
2375 || (var->data.location < 0)) {
Ian Romanick586e7412011-07-28 14:04:09 -07002376 linker_error(prog,
2377 "invalid explicit location %d specified for `%s'\n",
Tapani Pälli447bb902013-12-12 15:08:59 +02002378 (var->data.location < 0)
2379 ? var->data.location
2380 : var->data.location - generic_base,
Ian Romanick586e7412011-07-28 14:04:09 -07002381 var->name);
Ian Romanick68a4fc92010-10-07 17:21:22 -07002382 return false;
Ian Romanick523b6112011-08-17 15:40:03 -07002383 }
2384 } else if (target_index == MESA_SHADER_VERTEX) {
2385 unsigned binding;
2386
2387 if (prog->AttributeBindings->get(binding, var->name)) {
2388 assert(binding >= VERT_ATTRIB_GENERIC0);
Tapani Pälli447bb902013-12-12 15:08:59 +02002389 var->data.location = binding;
2390 var->data.is_unmatched_generic_inout = 0;
Ian Romanick68a4fc92010-10-07 17:21:22 -07002391 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07002392 } else if (target_index == MESA_SHADER_FRAGMENT) {
2393 unsigned binding;
Dave Airlie1256a5d2012-03-24 13:33:41 +00002394 unsigned index;
Ian Romanickb12b5d92011-11-04 16:08:52 -07002395
2396 if (prog->FragDataBindings->get(binding, var->name)) {
2397 assert(binding >= FRAG_RESULT_DATA0);
Tapani Pälli447bb902013-12-12 15:08:59 +02002398 var->data.location = binding;
2399 var->data.is_unmatched_generic_inout = 0;
Dave Airlie1256a5d2012-03-24 13:33:41 +00002400
2401 if (prog->FragDataIndexBindings->get(index, var->name)) {
Tapani Pälli447bb902013-12-12 15:08:59 +02002402 var->data.index = index;
Dave Airlie1256a5d2012-03-24 13:33:41 +00002403 }
Ian Romanickb12b5d92011-11-04 16:08:52 -07002404 }
Ian Romanick68a4fc92010-10-07 17:21:22 -07002405 }
2406
Tapani Pällie17056f2015-07-03 10:19:23 +03002407 /* From GL4.5 core spec, section 15.2 (Shader Execution):
2408 *
2409 * "Output binding assignments will cause LinkProgram to fail:
2410 * ...
2411 * If the program has an active output assigned to a location greater
2412 * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
2413 * an active output assigned an index greater than or equal to one;"
2414 */
2415 if (target_index == MESA_SHADER_FRAGMENT && var->data.index >= 1 &&
2416 var->data.location - generic_base >=
2417 (int) constants->MaxDualSourceDrawBuffers) {
2418 linker_error(prog,
2419 "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
2420 "with index %u for %s\n",
2421 var->data.location - generic_base, var->data.index,
2422 var->name);
2423 return false;
2424 }
2425
Dave Airliead208d92015-04-30 10:42:06 +10002426 const unsigned slots = var->type->count_attribute_slots();
2427
Ian Romanick9f0e98d2011-10-06 10:25:34 -07002428 /* If the variable is not a built-in and has a location statically
2429 * assigned in the shader (presumably via a layout qualifier), make sure
2430 * that it doesn't collide with other assigned locations. Otherwise,
2431 * add it to the list of variables that need linker-assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002432 */
Tapani Pälli447bb902013-12-12 15:08:59 +02002433 if (var->data.location != -1) {
2434 if (var->data.location >= generic_base && var->data.index < 1) {
Ian Romanick523b6112011-08-17 15:40:03 -07002435 /* From page 61 of the OpenGL 4.0 spec:
2436 *
2437 * "LinkProgram will fail if the attribute bindings assigned
2438 * by BindAttribLocation do not leave not enough space to
2439 * assign a location for an active matrix attribute or an
2440 * active attribute array, both of which require multiple
2441 * contiguous generic attributes."
2442 *
Anuj Phogat8c61b6a2014-03-04 17:03:28 -08002443 * I think above text prohibits the aliasing of explicit and
2444 * automatic assignments. But, aliasing is allowed in manual
2445 * assignments of attribute locations. See below comments for
2446 * the details.
Ian Romanick523b6112011-08-17 15:40:03 -07002447 *
Anuj Phogat8c61b6a2014-03-04 17:03:28 -08002448 * From OpenGL 4.0 spec, page 61:
Ian Romanick523b6112011-08-17 15:40:03 -07002449 *
2450 * "It is possible for an application to bind more than one
2451 * attribute name to the same location. This is referred to as
2452 * aliasing. This will only work if only one of the aliased
2453 * attributes is active in the executable program, or if no
2454 * path through the shader consumes more than one attribute of
2455 * a set of attributes aliased to the same location. A link
2456 * error can occur if the linker determines that every path
2457 * through the shader consumes multiple aliased attributes,
2458 * but implementations are not required to generate an error
2459 * in this case."
2460 *
Anuj Phogat8c61b6a2014-03-04 17:03:28 -08002461 * From GLSL 4.30 spec, page 54:
2462 *
2463 * "A program will fail to link if any two non-vertex shader
2464 * input variables are assigned to the same location. For
2465 * vertex shaders, multiple input variables may be assigned
2466 * to the same location using either layout qualifiers or via
2467 * the OpenGL API. However, such aliasing is intended only to
2468 * support vertex shaders where each execution path accesses
2469 * at most one input per each location. Implementations are
2470 * permitted, but not required, to generate link-time errors
2471 * if they detect that every path through the vertex shader
2472 * executable accesses multiple inputs assigned to any single
2473 * location. For all shader types, a program will fail to link
2474 * if explicit location assignments leave the linker unable
2475 * to find space for other variables without explicit
2476 * assignments."
2477 *
2478 * From OpenGL ES 3.0 spec, page 56:
2479 *
2480 * "Binding more than one attribute name to the same location
2481 * is referred to as aliasing, and is not permitted in OpenGL
2482 * ES Shading Language 3.00 vertex shaders. LinkProgram will
2483 * fail when this condition exists. However, aliasing is
2484 * possible in OpenGL ES Shading Language 1.00 vertex shaders.
2485 * This will only work if only one of the aliased attributes
2486 * is active in the executable program, or if no path through
2487 * the shader consumes more than one attribute of a set of
2488 * attributes aliased to the same location. A link error can
2489 * occur if the linker determines that every path through the
2490 * shader consumes multiple aliased attributes, but implemen-
2491 * tations are not required to generate an error in this case."
2492 *
2493 * After looking at above references from OpenGL, OpenGL ES and
2494 * GLSL specifications, we allow aliasing of vertex input variables
2495 * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
2496 *
2497 * NOTE: This is not required by the spec but its worth mentioning
2498 * here that we're not doing anything to make sure that no path
2499 * through the vertex shader executable accesses multiple inputs
2500 * assigned to any single location.
Ian Romanick523b6112011-08-17 15:40:03 -07002501 */
Anuj Phogat8c61b6a2014-03-04 17:03:28 -08002502
Ian Romanick523b6112011-08-17 15:40:03 -07002503 /* Mask representing the contiguous slots that will be used by
2504 * this attribute.
2505 */
Tapani Pälli447bb902013-12-12 15:08:59 +02002506 const unsigned attr = var->data.location - generic_base;
Ian Romanick523b6112011-08-17 15:40:03 -07002507 const unsigned use_mask = (1 << slots) - 1;
Anuj Phogat8c61b6a2014-03-04 17:03:28 -08002508 const char *const string = (target_index == MESA_SHADER_VERTEX)
2509 ? "vertex shader input" : "fragment shader output";
2510
2511 /* Generate a link error if the requested locations for this
2512 * attribute exceed the maximum allowed attribute location.
2513 */
2514 if (attr + slots > max_index) {
2515 linker_error(prog,
2516 "insufficient contiguous locations "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002517 "available for %s `%s' %d %d %d\n", string,
Anuj Phogat8c61b6a2014-03-04 17:03:28 -08002518 var->name, used_locations, use_mask, attr);
2519 return false;
2520 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002521
Ian Romanick523b6112011-08-17 15:40:03 -07002522 /* Generate a link error if the set of bits requested for this
2523 * attribute overlaps any previously allocated bits.
2524 */
2525 if ((~(use_mask << attr) & used_locations) != used_locations) {
Anuj Phogat8c61b6a2014-03-04 17:03:28 -08002526 if (target_index == MESA_SHADER_FRAGMENT ||
2527 (prog->IsES && prog->Version >= 300)) {
2528 linker_error(prog,
2529 "overlapping location is assigned "
2530 "to %s `%s' %d %d %d\n", string,
2531 var->name, used_locations, use_mask, attr);
2532 return false;
2533 } else {
2534 linker_warning(prog,
2535 "overlapping location is assigned "
2536 "to %s `%s' %d %d %d\n", string,
2537 var->name, used_locations, use_mask, attr);
2538 }
Ian Romanick523b6112011-08-17 15:40:03 -07002539 }
2540
2541 used_locations |= (use_mask << attr);
Kenneth Graunkec3294ca2015-09-02 10:42:57 -07002542
2543 /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
2544 *
2545 * "A program with more than the value of MAX_VERTEX_ATTRIBS
2546 * active attribute variables may fail to link, unless
2547 * device-dependent optimizations are able to make the program
2548 * fit within available hardware resources. For the purposes
2549 * of this test, attribute variables of the type dvec3, dvec4,
2550 * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
2551 * count as consuming twice as many attributes as equivalent
2552 * single-precision types. While these types use the same number
2553 * of generic attributes as their single-precision equivalents,
2554 * implementations are permitted to consume two single-precision
2555 * vectors of internal storage for each three- or four-component
2556 * double-precision vector."
2557 *
2558 * Mark this attribute slot as taking up twice as much space
2559 * so we can count it properly against limits. According to
2560 * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
2561 * is optional behavior, but it seems preferable.
2562 */
2563 const glsl_type *type = var->type->without_array();
2564 if (type == glsl_type::dvec3_type ||
2565 type == glsl_type::dvec4_type ||
2566 type == glsl_type::dmat2x3_type ||
2567 type == glsl_type::dmat2x4_type ||
2568 type == glsl_type::dmat3_type ||
2569 type == glsl_type::dmat3x4_type ||
2570 type == glsl_type::dmat4x3_type ||
2571 type == glsl_type::dmat4_type) {
2572 double_storage_locations |= (use_mask << attr);
2573 }
Ian Romanick523b6112011-08-17 15:40:03 -07002574 }
2575
2576 continue;
2577 }
2578
2579 to_assign[num_attr].slots = slots;
Ian Romanick69846702010-06-22 17:29:19 -07002580 to_assign[num_attr].var = var;
2581 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002582 }
Ian Romanick69846702010-06-22 17:29:19 -07002583
Dave Airliead208d92015-04-30 10:42:06 +10002584 if (target_index == MESA_SHADER_VERTEX) {
Kenneth Graunkec3294ca2015-09-02 10:42:57 -07002585 unsigned total_attribs_size =
2586 _mesa_bitcount(used_locations & ((1 << max_index) - 1)) +
2587 _mesa_bitcount(double_storage_locations);
Dave Airliead208d92015-04-30 10:42:06 +10002588 if (total_attribs_size > max_index) {
2589 linker_error(prog,
2590 "attempt to use %d vertex attribute slots only %d available ",
2591 total_attribs_size, max_index);
2592 return false;
2593 }
2594 }
2595
Ian Romanick69846702010-06-22 17:29:19 -07002596 /* If all of the attributes were assigned locations by the application (or
2597 * are built-in attributes with fixed locations), return early. This should
2598 * be the common case.
2599 */
2600 if (num_attr == 0)
2601 return true;
2602
2603 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
2604
Ian Romanickd32d4f72011-06-27 17:59:58 -07002605 if (target_index == MESA_SHADER_VERTEX) {
2606 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
2607 * only be explicitly assigned by via glBindAttribLocation. Mark it as
2608 * reserved to prevent it from being automatically allocated below.
2609 */
2610 find_deref_visitor find("gl_Vertex");
2611 find.run(sh->ir);
2612 if (find.variable_found())
2613 used_locations |= (1 << 0);
2614 }
Ian Romanick982e3792010-06-29 18:58:20 -07002615
Ian Romanick69846702010-06-22 17:29:19 -07002616 for (unsigned i = 0; i < num_attr; i++) {
2617 /* Mask representing the contiguous slots that will be used by this
2618 * attribute.
2619 */
2620 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
2621
2622 int location = find_available_slots(used_locations, to_assign[i].slots);
2623
2624 if (location < 0) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07002625 const char *const string = (target_index == MESA_SHADER_VERTEX)
2626 ? "vertex shader input" : "fragment shader output";
2627
Ian Romanick586e7412011-07-28 14:04:09 -07002628 linker_error(prog,
Dave Airlie7449ae42011-11-20 19:56:35 +00002629 "insufficient contiguous locations "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002630 "available for %s `%s'\n",
Ian Romanick586e7412011-07-28 14:04:09 -07002631 string, to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -07002632 return false;
2633 }
2634
Tapani Pälli447bb902013-12-12 15:08:59 +02002635 to_assign[i].var->data.location = generic_base + location;
2636 to_assign[i].var->data.is_unmatched_generic_inout = 0;
Ian Romanick69846702010-06-22 17:29:19 -07002637 used_locations |= (use_mask << location);
2638 }
2639
2640 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -07002641}
2642
2643
Ian Romanick40e114b2010-08-17 14:55:50 -07002644/**
Ian Romanickcc90e622010-10-19 17:59:10 -07002645 * Demote shader inputs and outputs that are not used in other stages
Ian Romanick40e114b2010-08-17 14:55:50 -07002646 */
2647void
Ian Romanickcc90e622010-10-19 17:59:10 -07002648demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
Ian Romanick40e114b2010-08-17 14:55:50 -07002649{
Matt Turner4d784462014-06-24 21:34:05 -07002650 foreach_in_list(ir_instruction, node, sh->ir) {
2651 ir_variable *const var = node->as_variable();
Ian Romanick40e114b2010-08-17 14:55:50 -07002652
Tapani Pälli33ee2c62013-12-12 13:51:01 +02002653 if ((var == NULL) || (var->data.mode != int(mode)))
Ian Romanick40e114b2010-08-17 14:55:50 -07002654 continue;
2655
Ian Romanickcc90e622010-10-19 17:59:10 -07002656 /* A shader 'in' or 'out' variable is only really an input or output if
2657 * its value is used by other shader stages. This will cause the variable
2658 * to have a location assigned.
Ian Romanick40e114b2010-08-17 14:55:50 -07002659 */
Tapani Pälli447bb902013-12-12 15:08:59 +02002660 if (var->data.is_unmatched_generic_inout) {
Ian Romanicka9948242014-07-08 18:53:09 -07002661 assert(var->data.mode != ir_var_temporary);
Tapani Pälli33ee2c62013-12-12 13:51:01 +02002662 var->data.mode = ir_var_auto;
Ian Romanick40e114b2010-08-17 14:55:50 -07002663 }
2664 }
2665}
2666
2667
Paul Berry871ddb92011-11-05 11:17:32 -07002668/**
Marek Olšákec174a42011-11-18 15:00:10 +01002669 * Store the gl_FragDepth layout in the gl_shader_program struct.
2670 */
2671static void
2672store_fragdepth_layout(struct gl_shader_program *prog)
2673{
2674 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2675 return;
2676 }
2677
2678 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2679
2680 /* We don't look up the gl_FragDepth symbol directly because if
2681 * gl_FragDepth is not used in the shader, it's removed from the IR.
2682 * However, the symbol won't be removed from the symbol table.
2683 *
2684 * We're only interested in the cases where the variable is NOT removed
2685 * from the IR.
2686 */
Matt Turner4d784462014-06-24 21:34:05 -07002687 foreach_in_list(ir_instruction, node, ir) {
2688 ir_variable *const var = node->as_variable();
Marek Olšákec174a42011-11-18 15:00:10 +01002689
Tapani Pälli33ee2c62013-12-12 13:51:01 +02002690 if (var == NULL || var->data.mode != ir_var_shader_out) {
Marek Olšákec174a42011-11-18 15:00:10 +01002691 continue;
2692 }
2693
2694 if (strcmp(var->name, "gl_FragDepth") == 0) {
Tapani Pälli447bb902013-12-12 15:08:59 +02002695 switch (var->data.depth_layout) {
Marek Olšákec174a42011-11-18 15:00:10 +01002696 case ir_depth_layout_none:
2697 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2698 return;
2699 case ir_depth_layout_any:
2700 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2701 return;
2702 case ir_depth_layout_greater:
2703 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2704 return;
2705 case ir_depth_layout_less:
2706 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2707 return;
2708 case ir_depth_layout_unchanged:
2709 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2710 return;
2711 default:
2712 assert(0);
2713 return;
2714 }
2715 }
2716 }
2717}
2718
2719/**
Ian Romanick92f81592011-11-08 12:37:19 -08002720 * Validate the resources used by a program versus the implementation limits
2721 */
Paul Berryb95d2372013-07-27 11:08:31 -07002722static void
Ian Romanick92f81592011-11-08 12:37:19 -08002723check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2724{
Paul Berry665b8d72014-01-07 10:11:39 -08002725 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick92f81592011-11-08 12:37:19 -08002726 struct gl_shader *sh = prog->_LinkedShaders[i];
2727
2728 if (sh == NULL)
2729 continue;
2730
Paul Berrybce8bc02014-01-08 10:17:01 -08002731 if (sh->num_samplers > ctx->Const.Program[i].MaxTextureImageUnits) {
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002732 linker_error(prog, "Too many %s shader texture samplers\n",
Paul Berry665b8d72014-01-07 10:11:39 -08002733 _mesa_shader_stage_to_string(i));
Ian Romanick92f81592011-11-08 12:37:19 -08002734 }
2735
Paul Berrybce8bc02014-01-08 10:17:01 -08002736 if (sh->num_uniform_components >
2737 ctx->Const.Program[i].MaxUniformComponents) {
Eric Anholt38e77e52013-05-23 11:10:15 -07002738 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2739 linker_warning(prog, "Too many %s shader default uniform block "
2740 "components, but the driver will try to optimize "
2741 "them out; this is non-portable out-of-spec "
2742 "behavior\n",
Paul Berry665b8d72014-01-07 10:11:39 -08002743 _mesa_shader_stage_to_string(i));
Eric Anholt38e77e52013-05-23 11:10:15 -07002744 } else {
2745 linker_error(prog, "Too many %s shader default uniform block "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002746 "components\n",
Paul Berry665b8d72014-01-07 10:11:39 -08002747 _mesa_shader_stage_to_string(i));
Eric Anholt38e77e52013-05-23 11:10:15 -07002748 }
2749 }
2750
2751 if (sh->num_combined_uniform_components >
Paul Berrybce8bc02014-01-08 10:17:01 -08002752 ctx->Const.Program[i].MaxCombinedUniformComponents) {
Marek Olšákdf809ae2011-12-10 04:14:46 +01002753 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2754 linker_warning(prog, "Too many %s shader uniform components, "
2755 "but the driver will try to optimize them out; "
2756 "this is non-portable out-of-spec behavior\n",
Paul Berry665b8d72014-01-07 10:11:39 -08002757 _mesa_shader_stage_to_string(i));
Marek Olšákdf809ae2011-12-10 04:14:46 +01002758 } else {
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002759 linker_error(prog, "Too many %s shader uniform components\n",
Paul Berry665b8d72014-01-07 10:11:39 -08002760 _mesa_shader_stage_to_string(i));
Marek Olšákdf809ae2011-12-10 04:14:46 +01002761 }
Ian Romanick92f81592011-11-08 12:37:19 -08002762 }
2763 }
2764
Paul Berry665b8d72014-01-07 10:11:39 -08002765 unsigned blocks[MESA_SHADER_STAGES] = {0};
Eric Anholt877a8972012-06-25 12:47:01 -07002766 unsigned total_uniform_blocks = 0;
2767
2768 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
Jose Fonsecaf734d252015-06-15 18:29:02 +01002769 if (prog->UniformBlocks[i].UniformBufferSize > ctx->Const.MaxUniformBlockSize) {
2770 linker_error(prog, "Uniform block %s too big (%d/%d)\n",
2771 prog->UniformBlocks[i].Name,
2772 prog->UniformBlocks[i].UniformBufferSize,
2773 ctx->Const.MaxUniformBlockSize);
2774 }
2775
Paul Berry665b8d72014-01-07 10:11:39 -08002776 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
Eric Anholt877a8972012-06-25 12:47:01 -07002777 if (prog->UniformBlockStageIndex[j][i] != -1) {
2778 blocks[j]++;
2779 total_uniform_blocks++;
2780 }
2781 }
2782
2783 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002784 linker_error(prog, "Too many combined uniform blocks (%d/%d)\n",
Eric Anholt877a8972012-06-25 12:47:01 -07002785 prog->NumUniformBlocks,
2786 ctx->Const.MaxCombinedUniformBlocks);
2787 } else {
Paul Berry665b8d72014-01-07 10:11:39 -08002788 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Paul Berrybce8bc02014-01-08 10:17:01 -08002789 const unsigned max_uniform_blocks =
2790 ctx->Const.Program[i].MaxUniformBlocks;
2791 if (blocks[i] > max_uniform_blocks) {
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002792 linker_error(prog, "Too many %s uniform blocks (%d/%d)\n",
Paul Berry665b8d72014-01-07 10:11:39 -08002793 _mesa_shader_stage_to_string(i),
Eric Anholt877a8972012-06-25 12:47:01 -07002794 blocks[i],
Paul Berrybce8bc02014-01-08 10:17:01 -08002795 max_uniform_blocks);
Eric Anholt877a8972012-06-25 12:47:01 -07002796 break;
2797 }
2798 }
2799 }
2800 }
Ian Romanick92f81592011-11-08 12:37:19 -08002801}
Paul Berry871ddb92011-11-05 11:17:32 -07002802
Dave Airlie60266862015-04-20 10:27:36 +10002803static void
Ian Romanick4ff9e592015-08-19 13:36:22 -07002804link_calculate_subroutine_compat(struct gl_shader_program *prog)
Dave Airlie60266862015-04-20 10:27:36 +10002805{
2806 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2807 struct gl_shader *sh = prog->_LinkedShaders[i];
2808 int count;
2809 if (!sh)
2810 continue;
2811
2812 for (unsigned j = 0; j < sh->NumSubroutineUniformRemapTable; j++) {
2813 struct gl_uniform_storage *uni = sh->SubroutineUniformRemapTable[j];
2814
2815 if (!uni)
2816 continue;
2817
2818 count = 0;
2819 for (unsigned f = 0; f < sh->NumSubroutineFunctions; f++) {
2820 struct gl_subroutine_function *fn = &sh->SubroutineFunctions[f];
2821 for (int k = 0; k < fn->num_compat_types; k++) {
2822 if (fn->types[k] == uni->type) {
2823 count++;
2824 break;
2825 }
2826 }
2827 }
2828 uni->num_compatible_subroutines = count;
2829 }
2830 }
2831}
2832
2833static void
Ian Romanick4ff9e592015-08-19 13:36:22 -07002834check_subroutine_resources(struct gl_shader_program *prog)
Dave Airlie60266862015-04-20 10:27:36 +10002835{
2836 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2837 struct gl_shader *sh = prog->_LinkedShaders[i];
2838
2839 if (sh) {
2840 if (sh->NumSubroutineUniformRemapTable > MAX_SUBROUTINE_UNIFORM_LOCATIONS)
2841 linker_error(prog, "Too many %s shader subroutine uniforms\n",
2842 _mesa_shader_stage_to_string(i));
2843 }
2844 }
2845}
Francisco Jereze51158f2013-11-22 15:53:26 -08002846/**
2847 * Validate shader image resources.
2848 */
2849static void
2850check_image_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2851{
2852 unsigned total_image_units = 0;
2853 unsigned fragment_outputs = 0;
2854
2855 if (!ctx->Extensions.ARB_shader_image_load_store)
2856 return;
2857
2858 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2859 struct gl_shader *sh = prog->_LinkedShaders[i];
2860
2861 if (sh) {
2862 if (sh->NumImages > ctx->Const.Program[i].MaxImageUniforms)
Timothy Arcerib8f63b32015-08-12 17:01:52 +10002863 linker_error(prog, "Too many %s shader image uniforms (%u > %u)\n",
2864 _mesa_shader_stage_to_string(i), sh->NumImages,
2865 ctx->Const.Program[i].MaxImageUniforms);
Francisco Jereze51158f2013-11-22 15:53:26 -08002866
2867 total_image_units += sh->NumImages;
2868
2869 if (i == MESA_SHADER_FRAGMENT) {
Matt Turner4d784462014-06-24 21:34:05 -07002870 foreach_in_list(ir_instruction, node, sh->ir) {
2871 ir_variable *var = node->as_variable();
Francisco Jereze51158f2013-11-22 15:53:26 -08002872 if (var && var->data.mode == ir_var_shader_out)
2873 fragment_outputs += var->type->count_attribute_slots();
2874 }
2875 }
2876 }
2877 }
2878
2879 if (total_image_units > ctx->Const.MaxCombinedImageUniforms)
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002880 linker_error(prog, "Too many combined image uniforms\n");
Francisco Jereze51158f2013-11-22 15:53:26 -08002881
2882 if (total_image_units + fragment_outputs >
Francisco Jerez47e0d5b2015-08-17 19:10:46 +03002883 ctx->Const.MaxCombinedShaderOutputResources)
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002884 linker_error(prog, "Too many combined image uniforms and fragment outputs\n");
Francisco Jereze51158f2013-11-22 15:53:26 -08002885}
2886
Tapani Pällieca9d162014-04-08 08:45:36 +03002887
2888/**
2889 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
2890 * for a variable, checks for overlaps between other uniforms using explicit
2891 * locations.
2892 */
2893static bool
2894reserve_explicit_locations(struct gl_shader_program *prog,
2895 string_to_uint_map *map, ir_variable *var)
2896{
2897 unsigned slots = var->type->uniform_locations();
2898 unsigned max_loc = var->data.location + slots - 1;
2899
2900 /* Resize remap table if locations do not fit in the current one. */
2901 if (max_loc + 1 > prog->NumUniformRemapTable) {
2902 prog->UniformRemapTable =
2903 reralloc(prog, prog->UniformRemapTable,
2904 gl_uniform_storage *,
2905 max_loc + 1);
2906
2907 if (!prog->UniformRemapTable) {
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002908 linker_error(prog, "Out of memory during linking.\n");
Tapani Pällieca9d162014-04-08 08:45:36 +03002909 return false;
2910 }
2911
2912 /* Initialize allocated space. */
2913 for (unsigned i = prog->NumUniformRemapTable; i < max_loc + 1; i++)
2914 prog->UniformRemapTable[i] = NULL;
2915
2916 prog->NumUniformRemapTable = max_loc + 1;
2917 }
2918
2919 for (unsigned i = 0; i < slots; i++) {
2920 unsigned loc = var->data.location + i;
2921
2922 /* Check if location is already used. */
2923 if (prog->UniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
2924
2925 /* Possibly same uniform from a different stage, this is ok. */
2926 unsigned hash_loc;
2927 if (map->get(hash_loc, var->name) && hash_loc == loc - i)
2928 continue;
2929
2930 /* ARB_explicit_uniform_location specification states:
2931 *
2932 * "No two default-block uniform variables in the program can have
2933 * the same location, even if they are unused, otherwise a compiler
2934 * or linker error will be generated."
2935 */
2936 linker_error(prog,
Neil Roberts352f8f22014-11-13 15:31:44 +00002937 "location qualifier for uniform %s overlaps "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07002938 "previously used location\n",
Tapani Pällieca9d162014-04-08 08:45:36 +03002939 var->name);
2940 return false;
2941 }
2942
2943 /* Initialize location as inactive before optimization
2944 * rounds and location assignment.
2945 */
2946 prog->UniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
2947 }
2948
2949 /* Note, base location used for arrays. */
2950 map->put(var->data.location, var->name);
2951
2952 return true;
2953}
2954
Dave Airlie60266862015-04-20 10:27:36 +10002955static bool
2956reserve_subroutine_explicit_locations(struct gl_shader_program *prog,
2957 struct gl_shader *sh,
2958 ir_variable *var)
2959{
2960 unsigned slots = var->type->uniform_locations();
2961 unsigned max_loc = var->data.location + slots - 1;
2962
2963 /* Resize remap table if locations do not fit in the current one. */
2964 if (max_loc + 1 > sh->NumSubroutineUniformRemapTable) {
2965 sh->SubroutineUniformRemapTable =
2966 reralloc(sh, sh->SubroutineUniformRemapTable,
2967 gl_uniform_storage *,
2968 max_loc + 1);
2969
2970 if (!sh->SubroutineUniformRemapTable) {
2971 linker_error(prog, "Out of memory during linking.\n");
2972 return false;
2973 }
2974
2975 /* Initialize allocated space. */
2976 for (unsigned i = sh->NumSubroutineUniformRemapTable; i < max_loc + 1; i++)
2977 sh->SubroutineUniformRemapTable[i] = NULL;
2978
2979 sh->NumSubroutineUniformRemapTable = max_loc + 1;
2980 }
2981
2982 for (unsigned i = 0; i < slots; i++) {
2983 unsigned loc = var->data.location + i;
2984
2985 /* Check if location is already used. */
2986 if (sh->SubroutineUniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
2987
2988 /* ARB_explicit_uniform_location specification states:
2989 * "No two subroutine uniform variables can have the same location
2990 * in the same shader stage, otherwise a compiler or linker error
2991 * will be generated."
2992 */
2993 linker_error(prog,
2994 "location qualifier for uniform %s overlaps "
2995 "previously used location\n",
2996 var->name);
2997 return false;
2998 }
2999
3000 /* Initialize location as inactive before optimization
3001 * rounds and location assignment.
3002 */
3003 sh->SubroutineUniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
3004 }
3005
3006 return true;
3007}
Tapani Pällieca9d162014-04-08 08:45:36 +03003008/**
3009 * Check and reserve all explicit uniform locations, called before
3010 * any optimizations happen to handle also inactive uniforms and
3011 * inactive array elements that may get trimmed away.
3012 */
3013static void
3014check_explicit_uniform_locations(struct gl_context *ctx,
3015 struct gl_shader_program *prog)
3016{
3017 if (!ctx->Extensions.ARB_explicit_uniform_location)
3018 return;
3019
3020 /* This map is used to detect if overlapping explicit locations
3021 * occur with the same uniform (from different stage) or a different one.
3022 */
3023 string_to_uint_map *uniform_map = new string_to_uint_map;
3024
3025 if (!uniform_map) {
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07003026 linker_error(prog, "Out of memory during linking.\n");
Tapani Pällieca9d162014-04-08 08:45:36 +03003027 return;
3028 }
3029
3030 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3031 struct gl_shader *sh = prog->_LinkedShaders[i];
3032
3033 if (!sh)
3034 continue;
3035
Matt Turner4d784462014-06-24 21:34:05 -07003036 foreach_in_list(ir_instruction, node, sh->ir) {
3037 ir_variable *var = node->as_variable();
Kristian Høgsberga78a5892015-05-13 11:17:23 +02003038 if (var && (var->data.mode == ir_var_uniform || var->data.mode == ir_var_shader_storage) &&
Tapani Pällieca9d162014-04-08 08:45:36 +03003039 var->data.explicit_location) {
Dave Airlie60266862015-04-20 10:27:36 +10003040 bool ret;
3041 if (var->type->is_subroutine())
3042 ret = reserve_subroutine_explicit_locations(prog, sh, var);
3043 else
3044 ret = reserve_explicit_locations(prog, uniform_map, var);
3045 if (!ret) {
Dave Airlie2d5d1f52014-09-02 09:54:36 +10003046 delete uniform_map;
Tapani Pällieca9d162014-04-08 08:45:36 +03003047 return;
Dave Airlie2d5d1f52014-09-02 09:54:36 +10003048 }
Tapani Pällieca9d162014-04-08 08:45:36 +03003049 }
3050 }
3051 }
3052
3053 delete uniform_map;
3054}
3055
Tapani Pällic796ce42015-03-06 09:14:49 +02003056static bool
3057add_program_resource(struct gl_shader_program *prog, GLenum type,
3058 const void *data, uint8_t stages)
3059{
3060 assert(data);
3061
3062 /* If resource already exists, do not add it again. */
3063 for (unsigned i = 0; i < prog->NumProgramResourceList; i++)
3064 if (prog->ProgramResourceList[i].Data == data)
3065 return true;
3066
3067 prog->ProgramResourceList =
3068 reralloc(prog,
3069 prog->ProgramResourceList,
3070 gl_program_resource,
3071 prog->NumProgramResourceList + 1);
3072
3073 if (!prog->ProgramResourceList) {
3074 linker_error(prog, "Out of memory during linking.\n");
3075 return false;
3076 }
3077
3078 struct gl_program_resource *res =
3079 &prog->ProgramResourceList[prog->NumProgramResourceList];
3080
3081 res->Type = type;
3082 res->Data = data;
3083 res->StageReferences = stages;
3084
3085 prog->NumProgramResourceList++;
3086
3087 return true;
3088}
3089
3090/**
3091 * Function builds a stage reference bitmask from variable name.
3092 */
3093static uint8_t
Tapani Pälli18c5cdb2015-08-03 08:48:32 +03003094build_stageref(struct gl_shader_program *shProg, const char *name,
3095 unsigned mode)
Tapani Pällic796ce42015-03-06 09:14:49 +02003096{
3097 uint8_t stages = 0;
3098
3099 /* Note, that we assume MAX 8 stages, if there will be more stages, type
3100 * used for reference mask in gl_program_resource will need to be changed.
3101 */
3102 assert(MESA_SHADER_STAGES < 8);
3103
3104 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3105 struct gl_shader *sh = shProg->_LinkedShaders[i];
3106 if (!sh)
3107 continue;
Tapani Pälliccaf37f2015-06-29 14:19:00 +03003108
3109 /* Shader symbol table may contain variables that have
3110 * been optimized away. Search IR for the variable instead.
3111 */
3112 foreach_in_list(ir_instruction, node, sh->ir) {
3113 ir_variable *var = node->as_variable();
Timothy Arceri75a96ce2015-07-04 15:43:15 +10003114 if (var) {
3115 unsigned baselen = strlen(var->name);
Tapani Pälli18c5cdb2015-08-03 08:48:32 +03003116
3117 /* Type needs to match if specified, otherwise we might
3118 * pick a variable with same name but different interface.
3119 */
Timothy Arceri42d283a2015-08-05 21:05:52 +10003120 if (var->data.mode != mode)
Tapani Pälli18c5cdb2015-08-03 08:48:32 +03003121 continue;
3122
Timothy Arceri75a96ce2015-07-04 15:43:15 +10003123 if (strncmp(var->name, name, baselen) == 0) {
3124 /* Check for exact name matches but also check for arrays and
3125 * structs.
3126 */
3127 if (name[baselen] == '\0' ||
3128 name[baselen] == '[' ||
3129 name[baselen] == '.') {
3130 stages |= (1 << i);
3131 break;
3132 }
3133 }
Tapani Pälliccaf37f2015-06-29 14:19:00 +03003134 }
3135 }
Tapani Pällic796ce42015-03-06 09:14:49 +02003136 }
3137 return stages;
3138}
3139
3140static bool
3141add_interface_variables(struct gl_shader_program *shProg,
Jose Fonseca037e0e72015-04-16 10:19:57 +01003142 struct gl_shader *sh, GLenum programInterface)
Tapani Pällic796ce42015-03-06 09:14:49 +02003143{
3144 foreach_in_list(ir_instruction, node, sh->ir) {
3145 ir_variable *var = node->as_variable();
Tapani Pälli3706e5d2015-04-30 09:27:00 +03003146 uint8_t mask = 0;
Tapani Pällic796ce42015-03-06 09:14:49 +02003147
3148 if (!var)
3149 continue;
3150
3151 switch (var->data.mode) {
3152 /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
3153 * "For GetActiveAttrib, all active vertex shader input variables
3154 * are enumerated, including the special built-in inputs gl_VertexID
3155 * and gl_InstanceID."
3156 */
3157 case ir_var_system_value:
3158 if (var->data.location != SYSTEM_VALUE_VERTEX_ID &&
3159 var->data.location != SYSTEM_VALUE_VERTEX_ID_ZERO_BASE &&
3160 var->data.location != SYSTEM_VALUE_INSTANCE_ID)
Tapani Pälli5917ca32015-04-21 08:25:16 +03003161 continue;
Tapani Pälli3706e5d2015-04-30 09:27:00 +03003162 /* Mark special built-in inputs referenced by the vertex stage so
3163 * that they are considered active by the shader queries.
3164 */
3165 mask = (1 << (MESA_SHADER_VERTEX));
Tapani Pällied10f9c2015-04-21 20:11:43 +03003166 /* FALLTHROUGH */
Tapani Pällic796ce42015-03-06 09:14:49 +02003167 case ir_var_shader_in:
Jose Fonseca037e0e72015-04-16 10:19:57 +01003168 if (programInterface != GL_PROGRAM_INPUT)
Tapani Pällic796ce42015-03-06 09:14:49 +02003169 continue;
3170 break;
3171 case ir_var_shader_out:
Jose Fonseca037e0e72015-04-16 10:19:57 +01003172 if (programInterface != GL_PROGRAM_OUTPUT)
Tapani Pällic796ce42015-03-06 09:14:49 +02003173 continue;
3174 break;
3175 default:
3176 continue;
3177 };
3178
Kenneth Graunke6218c682015-06-28 22:17:16 -07003179 if (!add_program_resource(shProg, programInterface, var,
Tapani Pälli18c5cdb2015-08-03 08:48:32 +03003180 build_stageref(shProg, var->name,
3181 var->data.mode) | mask))
Tapani Pällic796ce42015-03-06 09:14:49 +02003182 return false;
3183 }
3184 return true;
3185}
3186
3187/**
3188 * Builds up a list of program resources that point to existing
3189 * resource data.
3190 */
Tapani Pälli73afa312015-06-29 14:39:05 +03003191void
Ian Romanickbd0245b2015-08-26 13:38:49 +01003192build_program_resource_list(struct gl_shader_program *shProg)
Tapani Pällic796ce42015-03-06 09:14:49 +02003193{
3194 /* Rebuild resource list. */
3195 if (shProg->ProgramResourceList) {
3196 ralloc_free(shProg->ProgramResourceList);
3197 shProg->ProgramResourceList = NULL;
3198 shProg->NumProgramResourceList = 0;
3199 }
3200
3201 int input_stage = MESA_SHADER_STAGES, output_stage = 0;
3202
3203 /* Determine first input and final output stage. These are used to
3204 * detect which variables should be enumerated in the resource list
3205 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
3206 */
3207 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3208 if (!shProg->_LinkedShaders[i])
3209 continue;
3210 if (input_stage == MESA_SHADER_STAGES)
3211 input_stage = i;
3212 output_stage = i;
3213 }
3214
3215 /* Empty shader, no resources. */
3216 if (input_stage == MESA_SHADER_STAGES && output_stage == 0)
3217 return;
3218
3219 /* Add inputs and outputs to the resource list. */
3220 if (!add_interface_variables(shProg, shProg->_LinkedShaders[input_stage],
3221 GL_PROGRAM_INPUT))
3222 return;
3223
3224 if (!add_interface_variables(shProg, shProg->_LinkedShaders[output_stage],
3225 GL_PROGRAM_OUTPUT))
3226 return;
3227
3228 /* Add transform feedback varyings. */
3229 if (shProg->LinkedTransformFeedback.NumVarying > 0) {
3230 for (int i = 0; i < shProg->LinkedTransformFeedback.NumVarying; i++) {
Tapani Pällic796ce42015-03-06 09:14:49 +02003231 if (!add_program_resource(shProg, GL_TRANSFORM_FEEDBACK_VARYING,
3232 &shProg->LinkedTransformFeedback.Varyings[i],
Timothy Arceri42d283a2015-08-05 21:05:52 +10003233 0))
Tapani Pällic796ce42015-03-06 09:14:49 +02003234 return;
3235 }
3236 }
3237
3238 /* Add uniforms from uniform storage. */
Martin Peres87a4bc52015-05-21 15:51:09 +03003239 for (unsigned i = 0; i < shProg->NumUniformStorage; i++) {
Tapani Pällic796ce42015-03-06 09:14:49 +02003240 /* Do not add uniforms internally used by Mesa. */
3241 if (shProg->UniformStorage[i].hidden)
3242 continue;
3243
3244 uint8_t stageref =
Timothy Arceri42d283a2015-08-05 21:05:52 +10003245 build_stageref(shProg, shProg->UniformStorage[i].name,
3246 ir_var_uniform);
Tapani Pälli9f4eaba2015-05-11 13:24:20 +03003247
3248 /* Add stagereferences for uniforms in a uniform block. */
3249 int block_index = shProg->UniformStorage[i].block_index;
3250 if (block_index != -1) {
3251 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
3252 if (shProg->UniformBlockStageIndex[j][block_index] != -1)
3253 stageref |= (1 << j);
3254 }
3255 }
3256
Tapani Pällic796ce42015-03-06 09:14:49 +02003257 if (!add_program_resource(shProg, GL_UNIFORM,
3258 &shProg->UniformStorage[i], stageref))
3259 return;
3260 }
3261
3262 /* Add program uniform blocks. */
3263 for (unsigned i = 0; i < shProg->NumUniformBlocks; i++) {
3264 if (!add_program_resource(shProg, GL_UNIFORM_BLOCK,
3265 &shProg->UniformBlocks[i], 0))
3266 return;
3267 }
3268
3269 /* Add atomic counter buffers. */
3270 for (unsigned i = 0; i < shProg->NumAtomicBuffers; i++) {
3271 if (!add_program_resource(shProg, GL_ATOMIC_COUNTER_BUFFER,
3272 &shProg->AtomicBuffers[i], 0))
3273 return;
3274 }
3275
Dave Airlie60266862015-04-20 10:27:36 +10003276 for (unsigned i = 0; i < shProg->NumUniformStorage; i++) {
3277 GLenum type;
3278 if (!shProg->UniformStorage[i].hidden)
3279 continue;
3280
3281 for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) {
3282 if (!shProg->UniformStorage[i].subroutine[j].active)
3283 continue;
3284
3285 type = _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j);
3286 /* add shader subroutines */
3287 if (!add_program_resource(shProg, type, &shProg->UniformStorage[i], 0))
3288 return;
3289 }
3290 }
3291
3292 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3293 struct gl_shader *sh = shProg->_LinkedShaders[i];
3294 GLuint type;
3295
3296 if (!sh)
3297 continue;
3298
3299 type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i);
3300 for (unsigned j = 0; j < sh->NumSubroutineFunctions; j++) {
3301 if (!add_program_resource(shProg, type, &sh->SubroutineFunctions[j], 0))
3302 return;
3303 }
3304 }
3305
Tapani Pällic796ce42015-03-06 09:14:49 +02003306 /* TODO - following extensions will require more resource types:
3307 *
3308 * GL_ARB_shader_storage_buffer_object
Tapani Pällic796ce42015-03-06 09:14:49 +02003309 */
3310}
3311
Tapani Pälli9350ea62015-05-19 15:01:49 +03003312/**
3313 * This check is done to make sure we allow only constant expression
3314 * indexing and "constant-index-expression" (indexing with an expression
3315 * that includes loop induction variable).
3316 */
3317static bool
3318validate_sampler_array_indexing(struct gl_context *ctx,
3319 struct gl_shader_program *prog)
3320{
3321 dynamic_sampler_array_indexing_visitor v;
3322 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3323 if (prog->_LinkedShaders[i] == NULL)
3324 continue;
3325
3326 bool no_dynamic_indexing =
3327 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler;
3328
3329 /* Search for array derefs in shader. */
3330 v.run(prog->_LinkedShaders[i]->ir);
3331 if (v.uses_dynamic_sampler_array_indexing()) {
3332 const char *msg = "sampler arrays indexed with non-constant "
3333 "expressions is forbidden in GLSL %s %u";
3334 /* Backend has indicated that it has no dynamic indexing support. */
3335 if (no_dynamic_indexing) {
3336 linker_error(prog, msg, prog->IsES ? "ES" : "", prog->Version);
3337 return false;
3338 } else {
3339 linker_warning(prog, msg, prog->IsES ? "ES" : "", prog->Version);
3340 }
3341 }
3342 }
3343 return true;
3344}
3345
Ian Romanick4ff9e592015-08-19 13:36:22 -07003346static void
3347link_assign_subroutine_types(struct gl_shader_program *prog)
Dave Airlie60266862015-04-20 10:27:36 +10003348{
3349 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3350 gl_shader *sh = prog->_LinkedShaders[i];
3351
3352 if (sh == NULL)
3353 continue;
3354
3355 foreach_in_list(ir_instruction, node, sh->ir) {
3356 ir_function *fn = node->as_function();
3357 if (!fn)
3358 continue;
3359
3360 if (fn->is_subroutine)
3361 sh->NumSubroutineUniformTypes++;
3362
3363 if (!fn->num_subroutine_types)
3364 continue;
3365
3366 sh->SubroutineFunctions = reralloc(sh, sh->SubroutineFunctions,
3367 struct gl_subroutine_function,
3368 sh->NumSubroutineFunctions + 1);
3369 sh->SubroutineFunctions[sh->NumSubroutineFunctions].name = ralloc_strdup(sh, fn->name);
3370 sh->SubroutineFunctions[sh->NumSubroutineFunctions].num_compat_types = fn->num_subroutine_types;
3371 sh->SubroutineFunctions[sh->NumSubroutineFunctions].types =
3372 ralloc_array(sh, const struct glsl_type *,
3373 fn->num_subroutine_types);
3374 for (int j = 0; j < fn->num_subroutine_types; j++)
3375 sh->SubroutineFunctions[sh->NumSubroutineFunctions].types[j] = fn->subroutine_types[j];
3376 sh->NumSubroutineFunctions++;
3377 }
3378 }
3379}
Tapani Pällic796ce42015-03-06 09:14:49 +02003380
Ian Romanick0e59b262010-06-23 11:23:01 -07003381void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04003382link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -07003383{
Paul Berry871ddb92011-11-05 11:17:32 -07003384 tfeedback_decl *tfeedback_decls = NULL;
3385 unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
3386
Kenneth Graunked3073f52011-01-21 14:32:31 -08003387 void *mem_ctx = ralloc_context(NULL); // temporary linker context
Kenneth Graunke2da02e72010-11-17 11:03:57 -08003388
Paul Berryb95d2372013-07-27 11:08:31 -07003389 prog->LinkStatus = true; /* All error paths will set this to false */
Ian Romanick832dfa52010-06-17 15:04:20 -07003390 prog->Validated = false;
3391 prog->_Used = false;
3392
Anuj Phogat9bcb0a82014-02-10 14:12:40 -08003393 prog->ARB_fragment_coord_conventions_enable = false;
Francisco Jerez5c114932013-09-11 12:14:46 -07003394
Ian Romanick832dfa52010-06-17 15:04:20 -07003395 /* Separate the shaders into groups based on their type.
3396 */
Paul Berrycd18ba12014-01-07 08:56:57 -08003397 struct gl_shader **shader_list[MESA_SHADER_STAGES];
3398 unsigned num_shaders[MESA_SHADER_STAGES];
Ian Romanick832dfa52010-06-17 15:04:20 -07003399
Paul Berrycd18ba12014-01-07 08:56:57 -08003400 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
3401 shader_list[i] = (struct gl_shader **)
3402 calloc(prog->NumShaders, sizeof(struct gl_shader *));
3403 num_shaders[i] = 0;
3404 }
Ian Romanick832dfa52010-06-17 15:04:20 -07003405
Ian Romanick25f51d32010-07-16 15:51:50 -07003406 unsigned min_version = UINT_MAX;
3407 unsigned max_version = 0;
Paul Berrya9f34dc2012-08-02 17:49:44 -07003408 const bool is_es_prog =
3409 (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
Ian Romanick832dfa52010-06-17 15:04:20 -07003410 for (unsigned i = 0; i < prog->NumShaders; i++) {
Ian Romanick25f51d32010-07-16 15:51:50 -07003411 min_version = MIN2(min_version, prog->Shaders[i]->Version);
3412 max_version = MAX2(max_version, prog->Shaders[i]->Version);
3413
Paul Berrya9f34dc2012-08-02 17:49:44 -07003414 if (prog->Shaders[i]->IsES != is_es_prog) {
3415 linker_error(prog, "all shaders must use same shading "
3416 "language version\n");
3417 goto done;
3418 }
3419
Jose Fonsecad01a7cda2015-03-18 14:21:15 +00003420 if (prog->Shaders[i]->ARB_fragment_coord_conventions_enable) {
3421 prog->ARB_fragment_coord_conventions_enable = true;
3422 }
Anuj Phogat9bcb0a82014-02-10 14:12:40 -08003423
Paul Berrycd18ba12014-01-07 08:56:57 -08003424 gl_shader_stage shader_type = prog->Shaders[i]->Stage;
3425 shader_list[shader_type][num_shaders[shader_type]] = prog->Shaders[i];
3426 num_shaders[shader_type]++;
Ian Romanick832dfa52010-06-17 15:04:20 -07003427 }
3428
Paul Berry672fab02013-10-13 18:01:11 -07003429 /* In desktop GLSL, different shader versions may be linked together. In
3430 * GLSL ES, all shader versions must be the same.
Ian Romanick25f51d32010-07-16 15:51:50 -07003431 */
Paul Berry672fab02013-10-13 18:01:11 -07003432 if (is_es_prog && min_version != max_version) {
Ian Romanick586e7412011-07-28 14:04:09 -07003433 linker_error(prog, "all shaders must use same shading "
3434 "language version\n");
Ian Romanick25f51d32010-07-16 15:51:50 -07003435 goto done;
3436 }
3437
3438 prog->Version = max_version;
Paul Berry91c92bb2012-08-02 17:50:43 -07003439 prog->IsES = is_es_prog;
Ian Romanick25f51d32010-07-16 15:51:50 -07003440
Tapani Pälli69678952015-09-03 14:20:46 +03003441 /* From OpenGL 4.5 Core specification (7.3 Program Objects):
3442 * "Linking can fail for a variety of reasons as specified in the OpenGL
3443 * Shading Language Specification, as well as any of the following
3444 * reasons:
3445 *
3446 * * No shader objects are attached to program.
3447 *
3448 * ..."
3449 *
3450 * Same rule applies for OpenGL ES >= 3.1.
3451 */
3452
3453 if (prog->NumShaders == 0 &&
3454 ((ctx->API == API_OPENGL_CORE && ctx->Version >= 45) ||
3455 (ctx->API == API_OPENGLES2 && ctx->Version >= 31))) {
3456 linker_error(prog, "No shader objects are attached to program.\n");
3457 goto done;
3458 }
3459
Chris Forbes7c758c52014-09-21 13:33:14 +12003460 /* Some shaders have to be linked with some other shaders present.
Fabian Bielerbd85ba02013-05-24 23:26:54 +02003461 */
Paul Berrycd18ba12014-01-07 08:56:57 -08003462 if (num_shaders[MESA_SHADER_GEOMETRY] > 0 &&
Ian Romanickc557eb72014-01-23 18:26:29 -08003463 num_shaders[MESA_SHADER_VERTEX] == 0 &&
3464 !prog->SeparateShader) {
Fabian Bielerbd85ba02013-05-24 23:26:54 +02003465 linker_error(prog, "Geometry shader must be linked with "
3466 "vertex shader\n");
3467 goto done;
3468 }
Chris Forbes7c758c52014-09-21 13:33:14 +12003469 if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 &&
3470 num_shaders[MESA_SHADER_VERTEX] == 0 &&
3471 !prog->SeparateShader) {
3472 linker_error(prog, "Tessellation evaluation shader must be linked with "
3473 "vertex shader\n");
3474 goto done;
3475 }
3476 if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
3477 num_shaders[MESA_SHADER_VERTEX] == 0 &&
3478 !prog->SeparateShader) {
3479 linker_error(prog, "Tessellation control shader must be linked with "
3480 "vertex shader\n");
3481 goto done;
3482 }
3483
3484 /* The spec is self-contradictory here. It allows linking without a tess
3485 * eval shader, but that can only be used with transform feedback and
3486 * rasterization disabled. However, transform feedback isn't allowed
3487 * with GL_PATCHES, so it can't be used.
3488 *
3489 * More investigation showed that the idea of transform feedback after
3490 * a tess control shader was dropped, because some hw vendors couldn't
3491 * support tessellation without a tess eval shader, but the linker section
3492 * wasn't updated to reflect that.
3493 *
3494 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
3495 * spec bug.
3496 *
3497 * Do what's reasonable and always require a tess eval shader if a tess
3498 * control shader is present.
3499 */
3500 if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
3501 num_shaders[MESA_SHADER_TESS_EVAL] == 0 &&
3502 !prog->SeparateShader) {
3503 linker_error(prog, "Tessellation control shader must be linked with "
3504 "tessellation evaluation shader\n");
3505 goto done;
3506 }
Fabian Bielerbd85ba02013-05-24 23:26:54 +02003507
Paul Berry1fe274b2014-01-08 11:40:23 -08003508 /* Compute shaders have additional restrictions. */
3509 if (num_shaders[MESA_SHADER_COMPUTE] > 0 &&
3510 num_shaders[MESA_SHADER_COMPUTE] != prog->NumShaders) {
3511 linker_error(prog, "Compute shaders may not be linked with any other "
3512 "type of shader\n");
3513 }
3514
Paul Berry665b8d72014-01-07 10:11:39 -08003515 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07003516 if (prog->_LinkedShaders[i] != NULL)
3517 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
3518
3519 prog->_LinkedShaders[i] = NULL;
Eric Anholt5d0f4302010-08-18 12:02:35 -07003520 }
3521
Ian Romanickcd6764e2010-07-16 16:00:07 -07003522 /* Link all shaders for a particular stage and validate the result.
3523 */
Paul Berrycd18ba12014-01-07 08:56:57 -08003524 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
3525 if (num_shaders[stage] > 0) {
3526 gl_shader *const sh =
3527 link_intrastage_shaders(mem_ctx, ctx, prog, shader_list[stage],
3528 num_shaders[stage]);
Ian Romanick3fb87872010-07-09 14:09:34 -07003529
Ilia Mirkin5646f0f2015-05-17 17:56:44 -04003530 if (!prog->LinkStatus) {
3531 if (sh)
3532 ctx->Driver.DeleteShader(ctx, sh);
Paul Berrycd18ba12014-01-07 08:56:57 -08003533 goto done;
Ilia Mirkin5646f0f2015-05-17 17:56:44 -04003534 }
Ian Romanick3fb87872010-07-09 14:09:34 -07003535
Paul Berrycd18ba12014-01-07 08:56:57 -08003536 switch (stage) {
3537 case MESA_SHADER_VERTEX:
3538 validate_vertex_shader_executable(prog, sh);
3539 break;
Chris Forbesdf16e0d2014-09-09 19:25:02 +12003540 case MESA_SHADER_TESS_CTRL:
3541 /* nothing to be done */
3542 break;
3543 case MESA_SHADER_TESS_EVAL:
3544 validate_tess_eval_shader_executable(prog, sh);
3545 break;
Paul Berrycd18ba12014-01-07 08:56:57 -08003546 case MESA_SHADER_GEOMETRY:
3547 validate_geometry_shader_executable(prog, sh);
3548 break;
3549 case MESA_SHADER_FRAGMENT:
3550 validate_fragment_shader_executable(prog, sh);
3551 break;
3552 }
Ilia Mirkin5646f0f2015-05-17 17:56:44 -04003553 if (!prog->LinkStatus) {
3554 if (sh)
3555 ctx->Driver.DeleteShader(ctx, sh);
Paul Berrycd18ba12014-01-07 08:56:57 -08003556 goto done;
Ilia Mirkin5646f0f2015-05-17 17:56:44 -04003557 }
Ian Romanick3fb87872010-07-09 14:09:34 -07003558
Paul Berrycd18ba12014-01-07 08:56:57 -08003559 _mesa_reference_shader(ctx, &prog->_LinkedShaders[stage], sh);
3560 }
Ian Romanickcc22c5a2010-06-18 17:13:42 -07003561 }
3562
Paul Berrycd18ba12014-01-07 08:56:57 -08003563 if (num_shaders[MESA_SHADER_GEOMETRY] > 0)
Paul Berry44b7ebe2013-10-23 12:55:24 -07003564 prog->LastClipDistanceArraySize = prog->Geom.ClipDistanceArraySize;
Chris Forbesdf16e0d2014-09-09 19:25:02 +12003565 else if (num_shaders[MESA_SHADER_TESS_EVAL] > 0)
3566 prog->LastClipDistanceArraySize = prog->TessEval.ClipDistanceArraySize;
Paul Berrycd18ba12014-01-07 08:56:57 -08003567 else if (num_shaders[MESA_SHADER_VERTEX] > 0)
3568 prog->LastClipDistanceArraySize = prog->Vert.ClipDistanceArraySize;
3569 else
3570 prog->LastClipDistanceArraySize = 0; /* Not used */
Bryan Cain25480922013-02-15 09:46:50 -06003571
Ian Romanick3ed850e2010-06-23 12:18:21 -07003572 /* Here begins the inter-stage linking phase. Some initial validation is
3573 * performed, then locations are assigned for uniforms, attributes, and
3574 * varyings.
3575 */
Paul Berryb95d2372013-07-27 11:08:31 -07003576 cross_validate_uniforms(prog);
3577 if (!prog->LinkStatus)
3578 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07003579
Paul Berryb95d2372013-07-27 11:08:31 -07003580 unsigned prev;
Ian Romanick3322fba2010-10-14 13:28:42 -07003581
Paul Berry28e526d2014-01-06 19:47:25 -08003582 for (prev = 0; prev <= MESA_SHADER_FRAGMENT; prev++) {
Paul Berryb95d2372013-07-27 11:08:31 -07003583 if (prog->_LinkedShaders[prev] != NULL)
3584 break;
3585 }
Ian Romanick3322fba2010-10-14 13:28:42 -07003586
Tapani Pällieca9d162014-04-08 08:45:36 +03003587 check_explicit_uniform_locations(ctx, prog);
Ian Romanick4ff9e592015-08-19 13:36:22 -07003588 link_assign_subroutine_types(prog);
Dave Airlie60266862015-04-20 10:27:36 +10003589
Tapani Pällieca9d162014-04-08 08:45:36 +03003590 if (!prog->LinkStatus)
3591 goto done;
3592
Chris Forbes7c758c52014-09-21 13:33:14 +12003593 resize_tes_inputs(ctx, prog);
3594
Paul Berryb95d2372013-07-27 11:08:31 -07003595 /* Validate the inputs of each stage with the output of the preceding
3596 * stage.
3597 */
Paul Berry28e526d2014-01-06 19:47:25 -08003598 for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) {
Paul Berryb95d2372013-07-27 11:08:31 -07003599 if (prog->_LinkedShaders[i] == NULL)
3600 continue;
Kenneth Graunke3ddfccb2013-05-20 23:46:16 -07003601
Paul Berry544e3122013-11-15 14:23:45 -08003602 validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev],
3603 prog->_LinkedShaders[i]);
Paul Berryb95d2372013-07-27 11:08:31 -07003604 if (!prog->LinkStatus)
3605 goto done;
Ian Romanick3322fba2010-10-14 13:28:42 -07003606
Paul Berryb95d2372013-07-27 11:08:31 -07003607 cross_validate_outputs_to_inputs(prog,
3608 prog->_LinkedShaders[prev],
3609 prog->_LinkedShaders[i]);
3610 if (!prog->LinkStatus)
3611 goto done;
Ian Romanick37101922010-06-18 19:02:10 -07003612
Paul Berryb95d2372013-07-27 11:08:31 -07003613 prev = i;
Ian Romanick37101922010-06-18 19:02:10 -07003614 }
Ian Romanick832dfa52010-06-17 15:04:20 -07003615
Paul Berry544e3122013-11-15 14:23:45 -08003616 /* Cross-validate uniform blocks between shader stages */
3617 validate_interstage_uniform_blocks(prog, prog->_LinkedShaders,
Paul Berry665b8d72014-01-07 10:11:39 -08003618 MESA_SHADER_STAGES);
Paul Berry544e3122013-11-15 14:23:45 -08003619 if (!prog->LinkStatus)
3620 goto done;
Jordan Justen5ebf5472013-03-10 03:20:03 -07003621
Paul Berry665b8d72014-01-07 10:11:39 -08003622 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
Jordan Justen5ebf5472013-03-10 03:20:03 -07003623 if (prog->_LinkedShaders[i] != NULL)
3624 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
3625 }
3626
Eric Anholt3de13952012-05-04 13:08:46 -07003627 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
3628 * it before optimization because we want most of the checks to get
3629 * dropped thanks to constant propagation.
Paul Berry15ba2a52012-08-02 17:51:02 -07003630 *
3631 * This rule also applies to GLSL ES 3.00.
Eric Anholt3de13952012-05-04 13:08:46 -07003632 */
Paul Berry15ba2a52012-08-02 17:51:02 -07003633 if (max_version >= (is_es_prog ? 300 : 130)) {
Eric Anholt3de13952012-05-04 13:08:46 -07003634 struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
3635 if (sh) {
3636 lower_discard_flow(sh->ir);
3637 }
3638 }
3639
Eric Anholtf609cf72012-04-27 13:52:56 -07003640 if (!interstage_cross_validate_uniform_blocks(prog))
3641 goto done;
3642
Eric Anholt2f4fe152010-08-10 13:06:49 -07003643 /* Do common optimization before assigning storage for attributes,
3644 * uniforms, and varyings. Later optimization could possibly make
3645 * some of that unused.
3646 */
Paul Berry665b8d72014-01-07 10:11:39 -08003647 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Ian Romanick3322fba2010-10-14 13:28:42 -07003648 if (prog->_LinkedShaders[i] == NULL)
3649 continue;
3650
Ian Romanick02c5ae12011-07-11 10:46:01 -07003651 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
3652 if (!prog->LinkStatus)
3653 goto done;
3654
Marek Olšák002211f2014-08-03 04:31:56 +02003655 if (ctx->Const.ShaderCompilerOptions[i].LowerClipDistance) {
Paul Berry18392442012-12-04 11:11:02 -08003656 lower_clip_distance(prog->_LinkedShaders[i]);
3657 }
Paul Berryc06e3252011-08-11 20:58:21 -07003658
Fabian Bieler73a9a152014-03-10 17:55:36 +01003659 if (ctx->Const.LowerTessLevel) {
3660 lower_tess_level(prog->_LinkedShaders[i]);
3661 }
3662
Kenneth Graunke169c6452014-04-06 23:25:00 -07003663 while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false,
Marek Olšák002211f2014-08-03 04:31:56 +02003664 &ctx->Const.ShaderCompilerOptions[i],
Kenneth Graunke169c6452014-04-06 23:25:00 -07003665 ctx->Const.NativeIntegers))
Eric Anholt2f4fe152010-08-10 13:06:49 -07003666 ;
Kenneth Graunke4f22db52014-04-26 00:18:54 -07003667
3668 lower_const_arrays_to_uniforms(prog->_LinkedShaders[i]->ir);
Ian Romanicka7ba9a72010-07-20 13:36:32 -07003669 }
Ian Romanick13e10e42010-06-21 12:03:24 -07003670
Tapani Pälli9350ea62015-05-19 15:01:49 +03003671 /* Validation for special cases where we allow sampler array indexing
3672 * with loop induction variable. This check emits a warning or error
3673 * depending if backend can handle dynamic indexing.
3674 */
3675 if ((!prog->IsES && prog->Version < 130) ||
3676 (prog->IsES && prog->Version < 300)) {
3677 if (!validate_sampler_array_indexing(ctx, prog))
3678 goto done;
3679 }
3680
Iago Toral Quiroga75896832014-06-16 16:09:53 +02003681 /* Check and validate stream emissions in geometry shaders */
3682 validate_geometry_shader_emissions(ctx, prog);
3683
Paul Berry50895d42012-12-05 07:17:07 -08003684 /* Mark all generic shader inputs and outputs as unpaired. */
Ian Romanick6bdc1d92014-02-11 16:37:56 -08003685 for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) {
3686 if (prog->_LinkedShaders[i] != NULL) {
3687 link_invalidate_variable_locations(prog->_LinkedShaders[i]->ir);
3688 }
Paul Berry50895d42012-12-05 07:17:07 -08003689 }
3690
Tapani Pällib8689712015-07-27 13:29:20 +03003691 if (!assign_attribute_or_color_locations(prog, &ctx->Const,
3692 MESA_SHADER_VERTEX)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07003693 goto done;
3694 }
3695
Tapani Pällib8689712015-07-27 13:29:20 +03003696 if (!assign_attribute_or_color_locations(prog, &ctx->Const,
3697 MESA_SHADER_FRAGMENT)) {
Ian Romanickd32d4f72011-06-27 17:59:58 -07003698 goto done;
Ian Romanick40e114b2010-08-17 14:55:50 -07003699 }
3700
Tapani Pälli993b9b62015-03-17 13:58:57 +02003701 unsigned first, last;
3702
3703 first = MESA_SHADER_STAGES;
3704 last = 0;
3705
3706 /* Determine first and last stage. */
3707 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3708 if (!prog->_LinkedShaders[i])
3709 continue;
3710 if (first == MESA_SHADER_STAGES)
3711 first = i;
3712 last = i;
Ian Romanick3322fba2010-10-14 13:28:42 -07003713 }
3714
Paul Berry871ddb92011-11-05 11:17:32 -07003715 if (num_tfeedback_decls != 0) {
3716 /* From GL_EXT_transform_feedback:
3717 * A program will fail to link if:
3718 *
3719 * * the <count> specified by TransformFeedbackVaryingsEXT is
3720 * non-zero, but the program object has no vertex or geometry
3721 * shader;
3722 */
Bryan Cain25480922013-02-15 09:46:50 -06003723 if (first == MESA_SHADER_FRAGMENT) {
Paul Berry871ddb92011-11-05 11:17:32 -07003724 linker_error(prog, "Transform feedback varyings specified, but "
Andres Gomezf9fc3ae2014-11-18 08:43:35 -07003725 "no vertex or geometry shader is present.\n");
Paul Berry871ddb92011-11-05 11:17:32 -07003726 goto done;
3727 }
3728
3729 tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
3730 prog->TransformFeedback.NumVarying);
Paul Berry456279b2011-12-26 19:39:25 -08003731 if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
Paul Berry871ddb92011-11-05 11:17:32 -07003732 prog->TransformFeedback.VaryingNames,
3733 tfeedback_decls))
3734 goto done;
3735 }
3736
Marek Olšák284d9542013-06-12 02:18:09 +02003737 /* Linking the stages in the opposite order (from fragment to vertex)
3738 * ensures that inter-shader outputs written to in an earlier stage are
3739 * eliminated if they are (transitively) not used in a later stage.
3740 */
Tapani Pälli993b9b62015-03-17 13:58:57 +02003741 int next;
Ian Romanick13e10e42010-06-21 12:03:24 -07003742
Tapani Pälli993b9b62015-03-17 13:58:57 +02003743 if (first < MESA_SHADER_FRAGMENT) {
Marek Olšák284d9542013-06-12 02:18:09 +02003744 gl_shader *const sh = prog->_LinkedShaders[last];
3745
Ian Romanicka909b992014-12-01 14:07:30 -08003746 if (first == MESA_SHADER_GEOMETRY) {
3747 /* There was no vertex shader, but we still have to assign varying
3748 * locations for use by geometry shader inputs in SSO.
3749 *
3750 * If the shader is not separable (i.e., prog->SeparateShader is
3751 * false), linking will have already failed when first is
3752 * MESA_SHADER_GEOMETRY.
3753 */
3754 if (!assign_varying_locations(ctx, mem_ctx, prog,
Tapani Pälli993b9b62015-03-17 13:58:57 +02003755 NULL, prog->_LinkedShaders[first],
Chris Forbes0e94f352014-09-07 18:19:15 +12003756 num_tfeedback_decls, tfeedback_decls))
Ian Romanicka909b992014-12-01 14:07:30 -08003757 goto done;
3758 }
3759
Tapani Pälli993b9b62015-03-17 13:58:57 +02003760 if (last != MESA_SHADER_FRAGMENT &&
3761 (num_tfeedback_decls != 0 || prog->SeparateShader)) {
Marek Olšák284d9542013-06-12 02:18:09 +02003762 /* There was no fragment shader, but we still have to assign varying
3763 * locations for use by transform feedback.
3764 */
3765 if (!assign_varying_locations(ctx, mem_ctx, prog,
3766 sh, NULL,
Chris Forbes0e94f352014-09-07 18:19:15 +12003767 num_tfeedback_decls, tfeedback_decls))
Marek Olšák284d9542013-06-12 02:18:09 +02003768 goto done;
3769 }
3770
Marek Olšákd13003f2013-08-09 22:34:45 +02003771 do_dead_builtin_varyings(ctx, sh, NULL,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02003772 num_tfeedback_decls, tfeedback_decls);
3773
Ian Romanick1ff5a2b2014-02-14 12:10:27 -08003774 if (!prog->SeparateShader)
3775 demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
Marek Olšák284d9542013-06-12 02:18:09 +02003776
3777 /* Eliminate code that is now dead due to unused outputs being demoted.
Paul Berry871ddb92011-11-05 11:17:32 -07003778 */
Marek Olšák284d9542013-06-12 02:18:09 +02003779 while (do_dead_code(sh->ir, false))
3780 ;
3781 }
3782 else if (first == MESA_SHADER_FRAGMENT) {
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02003783 /* If the program only contains a fragment shader...
Marek Olšák284d9542013-06-12 02:18:09 +02003784 */
3785 gl_shader *const sh = prog->_LinkedShaders[first];
3786
Marek Olšákd13003f2013-08-09 22:34:45 +02003787 do_dead_builtin_varyings(ctx, NULL, sh,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02003788 num_tfeedback_decls, tfeedback_decls);
3789
Ian Romanick1ff5a2b2014-02-14 12:10:27 -08003790 if (prog->SeparateShader) {
3791 if (!assign_varying_locations(ctx, mem_ctx, prog,
3792 NULL /* producer */,
3793 sh /* consumer */,
3794 0 /* num_tfeedback_decls */,
Chris Forbes0e94f352014-09-07 18:19:15 +12003795 NULL /* tfeedback_decls */))
Ian Romanick1ff5a2b2014-02-14 12:10:27 -08003796 goto done;
3797 } else
3798 demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
Marek Olšák284d9542013-06-12 02:18:09 +02003799
3800 while (do_dead_code(sh->ir, false))
3801 ;
3802 }
3803
3804 next = last;
3805 for (int i = next - 1; i >= 0; i--) {
3806 if (prog->_LinkedShaders[i] == NULL)
3807 continue;
3808
3809 gl_shader *const sh_i = prog->_LinkedShaders[i];
3810 gl_shader *const sh_next = prog->_LinkedShaders[next];
3811
3812 if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
3813 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
Chris Forbes0e94f352014-09-07 18:19:15 +12003814 tfeedback_decls))
Paul Berry871ddb92011-11-05 11:17:32 -07003815 goto done;
Marek Olšák284d9542013-06-12 02:18:09 +02003816
Marek Olšákd13003f2013-08-09 22:34:45 +02003817 do_dead_builtin_varyings(ctx, sh_i, sh_next,
Marek Olšákb3d8b4c2013-06-12 13:23:48 +02003818 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
3819 tfeedback_decls);
3820
Marek Olšák284d9542013-06-12 02:18:09 +02003821 demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
3822 demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
3823
3824 /* Eliminate code that is now dead due to unused outputs being demoted.
3825 */
3826 while (do_dead_code(sh_i->ir, false))
3827 ;
3828 while (do_dead_code(sh_next->ir, false))
3829 ;
3830
Marek Olšák3c555822013-06-13 03:17:22 +02003831 /* This must be done after all dead varyings are eliminated. */
Ian Romanick42305fb2013-09-10 12:00:34 -05003832 if (!check_against_output_limit(ctx, prog, sh_i))
3833 goto done;
3834 if (!check_against_input_limit(ctx, prog, sh_next))
Marek Olšák3c555822013-06-13 03:17:22 +02003835 goto done;
3836
Marek Olšák284d9542013-06-12 02:18:09 +02003837 next = i;
Paul Berry871ddb92011-11-05 11:17:32 -07003838 }
3839
3840 if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
3841 goto done;
3842
Ian Romanick960d7222011-10-21 11:21:02 -07003843 update_array_sizes(prog);
Matt Turner9e2e7c72014-08-08 19:46:05 -07003844 link_assign_uniform_locations(prog, ctx->Const.UniformBooleanTrue);
Francisco Jerez5c114932013-09-11 12:14:46 -07003845 link_assign_atomic_counter_resources(ctx, prog);
Marek Olšákec174a42011-11-18 15:00:10 +01003846 store_fragdepth_layout(prog);
Ian Romanick960d7222011-10-21 11:21:02 -07003847
Ian Romanick4ff9e592015-08-19 13:36:22 -07003848 link_calculate_subroutine_compat(prog);
Paul Berryb95d2372013-07-27 11:08:31 -07003849 check_resources(ctx, prog);
Ian Romanick4ff9e592015-08-19 13:36:22 -07003850 check_subroutine_resources(prog);
Francisco Jereze51158f2013-11-22 15:53:26 -08003851 check_image_resources(ctx, prog);
Francisco Jerez5c114932013-09-11 12:14:46 -07003852 link_check_atomic_counter_resources(ctx, prog);
3853
Paul Berryb95d2372013-07-27 11:08:31 -07003854 if (!prog->LinkStatus)
Ian Romanick92f81592011-11-08 12:37:19 -08003855 goto done;
3856
Ian Romanickce9171f2011-02-03 17:10:14 -08003857 /* OpenGL ES requires that a vertex shader and a fragment shader both be
Anuj Phogat03597cf2013-12-19 14:17:19 -08003858 * present in a linked program. GL_ARB_ES2_compatibility doesn't say
3859 * anything about shader linking when one of the shaders (vertex or
3860 * fragment shader) is absent. So, the extension shouldn't change the
3861 * behavior specified in GLSL specification.
Ian Romanickce9171f2011-02-03 17:10:14 -08003862 */
Ian Romanickf64bfb22014-03-27 10:29:30 -07003863 if (!prog->SeparateShader && ctx->API == API_OPENGLES2) {
Tapani Pälli08e90492015-09-03 14:26:48 +03003864 /* With ES < 3.1 one needs to have always vertex + fragment shader. */
3865 if (ctx->Version < 31) {
3866 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
3867 linker_error(prog, "program lacks a vertex shader\n");
3868 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
3869 linker_error(prog, "program lacks a fragment shader\n");
3870 }
3871 } else {
3872 /* From OpenGL ES 3.1 specification (7.3 Program Objects):
3873 * "Linking can fail for a variety of reasons as specified in the
3874 * OpenGL ES Shading Language Specification, as well as any of the
3875 * following reasons:
3876 *
3877 * ...
3878 *
3879 * * program contains objects to form either a vertex shader or
3880 * fragment shader, and program is not separable, and does not
3881 * contain objects to form both a vertex shader and fragment
3882 * shader."
3883 */
3884 if (!!prog->_LinkedShaders[MESA_SHADER_VERTEX] ^
3885 !!prog->_LinkedShaders[MESA_SHADER_FRAGMENT]) {
3886 linker_error(prog, "Program needs to contain both vertex and "
3887 "fragment shaders.\n");
3888 }
Ian Romanickce9171f2011-02-03 17:10:14 -08003889 }
3890 }
3891
Ian Romanick13e10e42010-06-21 12:03:24 -07003892 /* FINISHME: Assign fragment shader output locations. */
3893
Ian Romanick832dfa52010-06-17 15:04:20 -07003894done:
Paul Berry665b8d72014-01-07 10:11:39 -08003895 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
Paul Berrycd18ba12014-01-07 08:56:57 -08003896 free(shader_list[i]);
Kenneth Graunke2da02e72010-11-17 11:03:57 -08003897 if (prog->_LinkedShaders[i] == NULL)
3898 continue;
3899
Paul Berryd7fa9eb2013-11-22 12:37:22 -08003900 /* Do a final validation step to make sure that the IR wasn't
3901 * invalidated by any modifications performed after intrastage linking.
3902 */
3903 validate_ir_tree(prog->_LinkedShaders[i]->ir);
3904
Kenneth Graunke2da02e72010-11-17 11:03:57 -08003905 /* Retain any live IR, but trash the rest. */
3906 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
Ian Romanick7bbcc0b2011-09-30 14:21:10 -07003907
3908 /* The symbol table in the linked shaders may contain references to
3909 * variables that were removed (e.g., unused uniforms). Since it may
3910 * contain junk, there is no possible valid use. Delete it and set the
3911 * pointer to NULL.
3912 */
3913 delete prog->_LinkedShaders[i]->symbols;
3914 prog->_LinkedShaders[i]->symbols = NULL;
Kenneth Graunke2da02e72010-11-17 11:03:57 -08003915 }
3916
Kenneth Graunked3073f52011-01-21 14:32:31 -08003917 ralloc_free(mem_ctx);
Ian Romanick832dfa52010-06-17 15:04:20 -07003918}