glsl/linker: Check the invariance of built-in special variables
[mesa.git] / src / compiler / glsl / linker.cpp
1 /*
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 */
66
67 #include <ctype.h>
68 #include "util/strndup.h"
69 #include "glsl_symbol_table.h"
70 #include "glsl_parser_extras.h"
71 #include "ir.h"
72 #include "program.h"
73 #include "program/prog_instruction.h"
74 #include "program/program.h"
75 #include "util/mesa-sha1.h"
76 #include "util/set.h"
77 #include "string_to_uint_map.h"
78 #include "linker.h"
79 #include "linker_util.h"
80 #include "link_varyings.h"
81 #include "ir_optimization.h"
82 #include "ir_rvalue_visitor.h"
83 #include "ir_uniform.h"
84 #include "builtin_functions.h"
85 #include "shader_cache.h"
86 #include "util/u_string.h"
87 #include "util/u_math.h"
88
89 #include "main/imports.h"
90 #include "main/shaderobj.h"
91 #include "main/enums.h"
92 #include "main/mtypes.h"
93
94
95 namespace {
96
97 struct find_variable {
98 const char *name;
99 bool found;
100
101 find_variable(const char *name) : name(name), found(false) {}
102 };
103
104 /**
105 * Visitor that determines whether or not a variable is ever written.
106 *
107 * Use \ref find_assignments for convenience.
108 */
109 class find_assignment_visitor : public ir_hierarchical_visitor {
110 public:
111 find_assignment_visitor(unsigned num_vars,
112 find_variable * const *vars)
113 : num_variables(num_vars), num_found(0), variables(vars)
114 {
115 }
116
117 virtual ir_visitor_status visit_enter(ir_assignment *ir)
118 {
119 ir_variable *const var = ir->lhs->variable_referenced();
120
121 return check_variable_name(var->name);
122 }
123
124 virtual ir_visitor_status visit_enter(ir_call *ir)
125 {
126 foreach_two_lists(formal_node, &ir->callee->parameters,
127 actual_node, &ir->actual_parameters) {
128 ir_rvalue *param_rval = (ir_rvalue *) actual_node;
129 ir_variable *sig_param = (ir_variable *) formal_node;
130
131 if (sig_param->data.mode == ir_var_function_out ||
132 sig_param->data.mode == ir_var_function_inout) {
133 ir_variable *var = param_rval->variable_referenced();
134 if (var && check_variable_name(var->name) == visit_stop)
135 return visit_stop;
136 }
137 }
138
139 if (ir->return_deref != NULL) {
140 ir_variable *const var = ir->return_deref->variable_referenced();
141
142 if (check_variable_name(var->name) == visit_stop)
143 return visit_stop;
144 }
145
146 return visit_continue_with_parent;
147 }
148
149 private:
150 ir_visitor_status check_variable_name(const char *name)
151 {
152 for (unsigned i = 0; i < num_variables; ++i) {
153 if (strcmp(variables[i]->name, name) == 0) {
154 if (!variables[i]->found) {
155 variables[i]->found = true;
156
157 assert(num_found < num_variables);
158 if (++num_found == num_variables)
159 return visit_stop;
160 }
161 break;
162 }
163 }
164
165 return visit_continue_with_parent;
166 }
167
168 private:
169 unsigned num_variables; /**< Number of variables to find */
170 unsigned num_found; /**< Number of variables already found */
171 find_variable * const *variables; /**< Variables to find */
172 };
173
174 /**
175 * Determine whether or not any of NULL-terminated list of variables is ever
176 * written to.
177 */
178 static void
179 find_assignments(exec_list *ir, find_variable * const *vars)
180 {
181 unsigned num_variables = 0;
182
183 for (find_variable * const *v = vars; *v; ++v)
184 num_variables++;
185
186 find_assignment_visitor visitor(num_variables, vars);
187 visitor.run(ir);
188 }
189
190 /**
191 * Determine whether or not the given variable is ever written to.
192 */
193 static void
194 find_assignments(exec_list *ir, find_variable *var)
195 {
196 find_assignment_visitor visitor(1, &var);
197 visitor.run(ir);
198 }
199
200 /**
201 * Visitor that determines whether or not a variable is ever read.
202 */
203 class find_deref_visitor : public ir_hierarchical_visitor {
204 public:
205 find_deref_visitor(const char *name)
206 : name(name), found(false)
207 {
208 /* empty */
209 }
210
211 virtual ir_visitor_status visit(ir_dereference_variable *ir)
212 {
213 if (strcmp(this->name, ir->var->name) == 0) {
214 this->found = true;
215 return visit_stop;
216 }
217
218 return visit_continue;
219 }
220
221 bool variable_found() const
222 {
223 return this->found;
224 }
225
226 private:
227 const char *name; /**< Find writes to a variable with this name. */
228 bool found; /**< Was a write to the variable found? */
229 };
230
231
232 /**
233 * A visitor helper that provides methods for updating the types of
234 * ir_dereferences. Classes that update variable types (say, updating
235 * array sizes) will want to use this so that dereference types stay in sync.
236 */
237 class deref_type_updater : public ir_hierarchical_visitor {
238 public:
239 virtual ir_visitor_status visit(ir_dereference_variable *ir)
240 {
241 ir->type = ir->var->type;
242 return visit_continue;
243 }
244
245 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
246 {
247 const glsl_type *const vt = ir->array->type;
248 if (vt->is_array())
249 ir->type = vt->fields.array;
250 return visit_continue;
251 }
252
253 virtual ir_visitor_status visit_leave(ir_dereference_record *ir)
254 {
255 ir->type = ir->record->type->fields.structure[ir->field_idx].type;
256 return visit_continue;
257 }
258 };
259
260
261 class array_resize_visitor : public deref_type_updater {
262 public:
263 unsigned num_vertices;
264 gl_shader_program *prog;
265 gl_shader_stage stage;
266
267 array_resize_visitor(unsigned num_vertices,
268 gl_shader_program *prog,
269 gl_shader_stage stage)
270 {
271 this->num_vertices = num_vertices;
272 this->prog = prog;
273 this->stage = stage;
274 }
275
276 virtual ~array_resize_visitor()
277 {
278 /* empty */
279 }
280
281 virtual ir_visitor_status visit(ir_variable *var)
282 {
283 if (!var->type->is_array() || var->data.mode != ir_var_shader_in ||
284 var->data.patch)
285 return visit_continue;
286
287 unsigned size = var->type->length;
288
289 if (stage == MESA_SHADER_GEOMETRY) {
290 /* Generate a link error if the shader has declared this array with
291 * an incorrect size.
292 */
293 if (!var->data.implicit_sized_array &&
294 size && size != this->num_vertices) {
295 linker_error(this->prog, "size of array %s declared as %u, "
296 "but number of input vertices is %u\n",
297 var->name, size, this->num_vertices);
298 return visit_continue;
299 }
300
301 /* Generate a link error if the shader attempts to access an input
302 * array using an index too large for its actual size assigned at
303 * link time.
304 */
305 if (var->data.max_array_access >= (int)this->num_vertices) {
306 linker_error(this->prog, "%s shader accesses element %i of "
307 "%s, but only %i input vertices\n",
308 _mesa_shader_stage_to_string(this->stage),
309 var->data.max_array_access, var->name, this->num_vertices);
310 return visit_continue;
311 }
312 }
313
314 var->type = glsl_type::get_array_instance(var->type->fields.array,
315 this->num_vertices);
316 var->data.max_array_access = this->num_vertices - 1;
317
318 return visit_continue;
319 }
320 };
321
322 /**
323 * Visitor that determines the highest stream id to which a (geometry) shader
324 * emits vertices. It also checks whether End{Stream}Primitive is ever called.
325 */
326 class find_emit_vertex_visitor : public ir_hierarchical_visitor {
327 public:
328 find_emit_vertex_visitor(int max_allowed)
329 : max_stream_allowed(max_allowed),
330 invalid_stream_id(0),
331 invalid_stream_id_from_emit_vertex(false),
332 end_primitive_found(false),
333 uses_non_zero_stream(false)
334 {
335 /* empty */
336 }
337
338 virtual ir_visitor_status visit_leave(ir_emit_vertex *ir)
339 {
340 int stream_id = ir->stream_id();
341
342 if (stream_id < 0) {
343 invalid_stream_id = stream_id;
344 invalid_stream_id_from_emit_vertex = true;
345 return visit_stop;
346 }
347
348 if (stream_id > max_stream_allowed) {
349 invalid_stream_id = stream_id;
350 invalid_stream_id_from_emit_vertex = true;
351 return visit_stop;
352 }
353
354 if (stream_id != 0)
355 uses_non_zero_stream = true;
356
357 return visit_continue;
358 }
359
360 virtual ir_visitor_status visit_leave(ir_end_primitive *ir)
361 {
362 end_primitive_found = true;
363
364 int stream_id = ir->stream_id();
365
366 if (stream_id < 0) {
367 invalid_stream_id = stream_id;
368 invalid_stream_id_from_emit_vertex = false;
369 return visit_stop;
370 }
371
372 if (stream_id > max_stream_allowed) {
373 invalid_stream_id = stream_id;
374 invalid_stream_id_from_emit_vertex = false;
375 return visit_stop;
376 }
377
378 if (stream_id != 0)
379 uses_non_zero_stream = true;
380
381 return visit_continue;
382 }
383
384 bool error()
385 {
386 return invalid_stream_id != 0;
387 }
388
389 const char *error_func()
390 {
391 return invalid_stream_id_from_emit_vertex ?
392 "EmitStreamVertex" : "EndStreamPrimitive";
393 }
394
395 int error_stream()
396 {
397 return invalid_stream_id;
398 }
399
400 bool uses_streams()
401 {
402 return uses_non_zero_stream;
403 }
404
405 bool uses_end_primitive()
406 {
407 return end_primitive_found;
408 }
409
410 private:
411 int max_stream_allowed;
412 int invalid_stream_id;
413 bool invalid_stream_id_from_emit_vertex;
414 bool end_primitive_found;
415 bool uses_non_zero_stream;
416 };
417
418 /* Class that finds array derefs and check if indexes are dynamic. */
419 class dynamic_sampler_array_indexing_visitor : public ir_hierarchical_visitor
420 {
421 public:
422 dynamic_sampler_array_indexing_visitor() :
423 dynamic_sampler_array_indexing(false)
424 {
425 }
426
427 ir_visitor_status visit_enter(ir_dereference_array *ir)
428 {
429 if (!ir->variable_referenced())
430 return visit_continue;
431
432 if (!ir->variable_referenced()->type->contains_sampler())
433 return visit_continue;
434
435 if (!ir->array_index->constant_expression_value(ralloc_parent(ir))) {
436 dynamic_sampler_array_indexing = true;
437 return visit_stop;
438 }
439 return visit_continue;
440 }
441
442 bool uses_dynamic_sampler_array_indexing()
443 {
444 return dynamic_sampler_array_indexing;
445 }
446
447 private:
448 bool dynamic_sampler_array_indexing;
449 };
450
451 } /* anonymous namespace */
452
453 void
454 linker_error(gl_shader_program *prog, const char *fmt, ...)
455 {
456 va_list ap;
457
458 ralloc_strcat(&prog->data->InfoLog, "error: ");
459 va_start(ap, fmt);
460 ralloc_vasprintf_append(&prog->data->InfoLog, fmt, ap);
461 va_end(ap);
462
463 prog->data->LinkStatus = LINKING_FAILURE;
464 }
465
466
467 void
468 linker_warning(gl_shader_program *prog, const char *fmt, ...)
469 {
470 va_list ap;
471
472 ralloc_strcat(&prog->data->InfoLog, "warning: ");
473 va_start(ap, fmt);
474 ralloc_vasprintf_append(&prog->data->InfoLog, fmt, ap);
475 va_end(ap);
476
477 }
478
479
480 /**
481 * Given a string identifying a program resource, break it into a base name
482 * and an optional array index in square brackets.
483 *
484 * If an array index is present, \c out_base_name_end is set to point to the
485 * "[" that precedes the array index, and the array index itself is returned
486 * as a long.
487 *
488 * If no array index is present (or if the array index is negative or
489 * mal-formed), \c out_base_name_end, is set to point to the null terminator
490 * at the end of the input string, and -1 is returned.
491 *
492 * Only the final array index is parsed; if the string contains other array
493 * indices (or structure field accesses), they are left in the base name.
494 *
495 * No attempt is made to check that the base name is properly formed;
496 * typically the caller will look up the base name in a hash table, so
497 * ill-formed base names simply turn into hash table lookup failures.
498 */
499 long
500 parse_program_resource_name(const GLchar *name,
501 const GLchar **out_base_name_end)
502 {
503 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
504 *
505 * "When an integer array element or block instance number is part of
506 * the name string, it will be specified in decimal form without a "+"
507 * or "-" sign or any extra leading zeroes. Additionally, the name
508 * string will not include white space anywhere in the string."
509 */
510
511 const size_t len = strlen(name);
512 *out_base_name_end = name + len;
513
514 if (len == 0 || name[len-1] != ']')
515 return -1;
516
517 /* Walk backwards over the string looking for a non-digit character. This
518 * had better be the opening bracket for an array index.
519 *
520 * Initially, i specifies the location of the ']'. Since the string may
521 * contain only the ']' charcater, walk backwards very carefully.
522 */
523 unsigned i;
524 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
525 /* empty */ ;
526
527 if ((i == 0) || name[i-1] != '[')
528 return -1;
529
530 long array_index = strtol(&name[i], NULL, 10);
531 if (array_index < 0)
532 return -1;
533
534 /* Check for leading zero */
535 if (name[i] == '0' && name[i+1] != ']')
536 return -1;
537
538 *out_base_name_end = name + (i - 1);
539 return array_index;
540 }
541
542
543 void
544 link_invalidate_variable_locations(exec_list *ir)
545 {
546 foreach_in_list(ir_instruction, node, ir) {
547 ir_variable *const var = node->as_variable();
548
549 if (var == NULL)
550 continue;
551
552 /* Only assign locations for variables that lack an explicit location.
553 * Explicit locations are set for all built-in variables, generic vertex
554 * shader inputs (via layout(location=...)), and generic fragment shader
555 * outputs (also via layout(location=...)).
556 */
557 if (!var->data.explicit_location) {
558 var->data.location = -1;
559 var->data.location_frac = 0;
560 }
561
562 /* ir_variable::is_unmatched_generic_inout is used by the linker while
563 * connecting outputs from one stage to inputs of the next stage.
564 */
565 if (var->data.explicit_location &&
566 var->data.location < VARYING_SLOT_VAR0) {
567 var->data.is_unmatched_generic_inout = 0;
568 } else {
569 var->data.is_unmatched_generic_inout = 1;
570 }
571 }
572 }
573
574
575 /**
576 * Set clip_distance_array_size based and cull_distance_array_size on the given
577 * shader.
578 *
579 * Also check for errors based on incorrect usage of gl_ClipVertex and
580 * gl_ClipDistance and gl_CullDistance.
581 * Additionally test whether the arrays gl_ClipDistance and gl_CullDistance
582 * exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances.
583 *
584 * Return false if an error was reported.
585 */
586 static void
587 analyze_clip_cull_usage(struct gl_shader_program *prog,
588 struct gl_linked_shader *shader,
589 struct gl_context *ctx,
590 GLuint *clip_distance_array_size,
591 GLuint *cull_distance_array_size)
592 {
593 *clip_distance_array_size = 0;
594 *cull_distance_array_size = 0;
595
596 if (prog->data->Version >= (prog->IsES ? 300 : 130)) {
597 /* From section 7.1 (Vertex Shader Special Variables) of the
598 * GLSL 1.30 spec:
599 *
600 * "It is an error for a shader to statically write both
601 * gl_ClipVertex and gl_ClipDistance."
602 *
603 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
604 * gl_ClipVertex nor gl_ClipDistance. However with
605 * GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0.
606 */
607 find_variable gl_ClipDistance("gl_ClipDistance");
608 find_variable gl_CullDistance("gl_CullDistance");
609 find_variable gl_ClipVertex("gl_ClipVertex");
610 find_variable * const variables[] = {
611 &gl_ClipDistance,
612 &gl_CullDistance,
613 !prog->IsES ? &gl_ClipVertex : NULL,
614 NULL
615 };
616 find_assignments(shader->ir, variables);
617
618 /* From the ARB_cull_distance spec:
619 *
620 * It is a compile-time or link-time error for the set of shaders forming
621 * a program to statically read or write both gl_ClipVertex and either
622 * gl_ClipDistance or gl_CullDistance.
623 *
624 * This does not apply to GLSL ES shaders, since GLSL ES doesn't define
625 * gl_ClipVertex.
626 */
627 if (!prog->IsES) {
628 if (gl_ClipVertex.found && gl_ClipDistance.found) {
629 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
630 "and `gl_ClipDistance'\n",
631 _mesa_shader_stage_to_string(shader->Stage));
632 return;
633 }
634 if (gl_ClipVertex.found && gl_CullDistance.found) {
635 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
636 "and `gl_CullDistance'\n",
637 _mesa_shader_stage_to_string(shader->Stage));
638 return;
639 }
640 }
641
642 if (gl_ClipDistance.found) {
643 ir_variable *clip_distance_var =
644 shader->symbols->get_variable("gl_ClipDistance");
645 assert(clip_distance_var);
646 *clip_distance_array_size = clip_distance_var->type->length;
647 }
648 if (gl_CullDistance.found) {
649 ir_variable *cull_distance_var =
650 shader->symbols->get_variable("gl_CullDistance");
651 assert(cull_distance_var);
652 *cull_distance_array_size = cull_distance_var->type->length;
653 }
654 /* From the ARB_cull_distance spec:
655 *
656 * It is a compile-time or link-time error for the set of shaders forming
657 * a program to have the sum of the sizes of the gl_ClipDistance and
658 * gl_CullDistance arrays to be larger than
659 * gl_MaxCombinedClipAndCullDistances.
660 */
661 if ((*clip_distance_array_size + *cull_distance_array_size) >
662 ctx->Const.MaxClipPlanes) {
663 linker_error(prog, "%s shader: the combined size of "
664 "'gl_ClipDistance' and 'gl_CullDistance' size cannot "
665 "be larger than "
666 "gl_MaxCombinedClipAndCullDistances (%u)",
667 _mesa_shader_stage_to_string(shader->Stage),
668 ctx->Const.MaxClipPlanes);
669 }
670 }
671 }
672
673
674 /**
675 * Verify that a vertex shader executable meets all semantic requirements.
676 *
677 * Also sets info.clip_distance_array_size and
678 * info.cull_distance_array_size as a side effect.
679 *
680 * \param shader Vertex shader executable to be verified
681 */
682 static void
683 validate_vertex_shader_executable(struct gl_shader_program *prog,
684 struct gl_linked_shader *shader,
685 struct gl_context *ctx)
686 {
687 if (shader == NULL)
688 return;
689
690 /* From the GLSL 1.10 spec, page 48:
691 *
692 * "The variable gl_Position is available only in the vertex
693 * language and is intended for writing the homogeneous vertex
694 * position. All executions of a well-formed vertex shader
695 * executable must write a value into this variable. [...] The
696 * variable gl_Position is available only in the vertex
697 * language and is intended for writing the homogeneous vertex
698 * position. All executions of a well-formed vertex shader
699 * executable must write a value into this variable."
700 *
701 * while in GLSL 1.40 this text is changed to:
702 *
703 * "The variable gl_Position is available only in the vertex
704 * language and is intended for writing the homogeneous vertex
705 * position. It can be written at any time during shader
706 * execution. It may also be read back by a vertex shader
707 * after being written. This value will be used by primitive
708 * assembly, clipping, culling, and other fixed functionality
709 * operations, if present, that operate on primitives after
710 * vertex processing has occurred. Its value is undefined if
711 * the vertex shader executable does not write gl_Position."
712 *
713 * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
714 * gl_Position is not an error.
715 */
716 if (prog->data->Version < (prog->IsES ? 300 : 140)) {
717 find_variable gl_Position("gl_Position");
718 find_assignments(shader->ir, &gl_Position);
719 if (!gl_Position.found) {
720 if (prog->IsES) {
721 linker_warning(prog,
722 "vertex shader does not write to `gl_Position'. "
723 "Its value is undefined. \n");
724 } else {
725 linker_error(prog,
726 "vertex shader does not write to `gl_Position'. \n");
727 }
728 return;
729 }
730 }
731
732 analyze_clip_cull_usage(prog, shader, ctx,
733 &shader->Program->info.clip_distance_array_size,
734 &shader->Program->info.cull_distance_array_size);
735 }
736
737 static void
738 validate_tess_eval_shader_executable(struct gl_shader_program *prog,
739 struct gl_linked_shader *shader,
740 struct gl_context *ctx)
741 {
742 if (shader == NULL)
743 return;
744
745 analyze_clip_cull_usage(prog, shader, ctx,
746 &shader->Program->info.clip_distance_array_size,
747 &shader->Program->info.cull_distance_array_size);
748 }
749
750
751 /**
752 * Verify that a fragment shader executable meets all semantic requirements
753 *
754 * \param shader Fragment shader executable to be verified
755 */
756 static void
757 validate_fragment_shader_executable(struct gl_shader_program *prog,
758 struct gl_linked_shader *shader)
759 {
760 if (shader == NULL)
761 return;
762
763 find_variable gl_FragColor("gl_FragColor");
764 find_variable gl_FragData("gl_FragData");
765 find_variable * const variables[] = { &gl_FragColor, &gl_FragData, NULL };
766 find_assignments(shader->ir, variables);
767
768 if (gl_FragColor.found && gl_FragData.found) {
769 linker_error(prog, "fragment shader writes to both "
770 "`gl_FragColor' and `gl_FragData'\n");
771 }
772 }
773
774 /**
775 * Verify that a geometry shader executable meets all semantic requirements
776 *
777 * Also sets prog->Geom.VerticesIn, and info.clip_distance_array_sizeand
778 * info.cull_distance_array_size as a side effect.
779 *
780 * \param shader Geometry shader executable to be verified
781 */
782 static void
783 validate_geometry_shader_executable(struct gl_shader_program *prog,
784 struct gl_linked_shader *shader,
785 struct gl_context *ctx)
786 {
787 if (shader == NULL)
788 return;
789
790 unsigned num_vertices =
791 vertices_per_prim(shader->Program->info.gs.input_primitive);
792 prog->Geom.VerticesIn = num_vertices;
793
794 analyze_clip_cull_usage(prog, shader, ctx,
795 &shader->Program->info.clip_distance_array_size,
796 &shader->Program->info.cull_distance_array_size);
797 }
798
799 /**
800 * Check if geometry shaders emit to non-zero streams and do corresponding
801 * validations.
802 */
803 static void
804 validate_geometry_shader_emissions(struct gl_context *ctx,
805 struct gl_shader_program *prog)
806 {
807 struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
808
809 if (sh != NULL) {
810 find_emit_vertex_visitor emit_vertex(ctx->Const.MaxVertexStreams - 1);
811 emit_vertex.run(sh->ir);
812 if (emit_vertex.error()) {
813 linker_error(prog, "Invalid call %s(%d). Accepted values for the "
814 "stream parameter are in the range [0, %d].\n",
815 emit_vertex.error_func(),
816 emit_vertex.error_stream(),
817 ctx->Const.MaxVertexStreams - 1);
818 }
819 prog->Geom.UsesStreams = emit_vertex.uses_streams();
820 prog->Geom.UsesEndPrimitive = emit_vertex.uses_end_primitive();
821
822 /* From the ARB_gpu_shader5 spec:
823 *
824 * "Multiple vertex streams are supported only if the output primitive
825 * type is declared to be "points". A program will fail to link if it
826 * contains a geometry shader calling EmitStreamVertex() or
827 * EndStreamPrimitive() if its output primitive type is not "points".
828 *
829 * However, in the same spec:
830 *
831 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
832 * with <stream> set to zero."
833 *
834 * And:
835 *
836 * "The function EndPrimitive() is equivalent to calling
837 * EndStreamPrimitive() with <stream> set to zero."
838 *
839 * Since we can call EmitVertex() and EndPrimitive() when we output
840 * primitives other than points, calling EmitStreamVertex(0) or
841 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
842 * does. Currently we only set prog->Geom.UsesStreams to TRUE when
843 * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
844 * stream.
845 */
846 if (prog->Geom.UsesStreams &&
847 sh->Program->info.gs.output_primitive != GL_POINTS) {
848 linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
849 "with n>0 requires point output\n");
850 }
851 }
852 }
853
854 bool
855 validate_intrastage_arrays(struct gl_shader_program *prog,
856 ir_variable *const var,
857 ir_variable *const existing)
858 {
859 /* Consider the types to be "the same" if both types are arrays
860 * of the same type and one of the arrays is implicitly sized.
861 * In addition, set the type of the linked variable to the
862 * explicitly sized array.
863 */
864 if (var->type->is_array() && existing->type->is_array()) {
865 if ((var->type->fields.array == existing->type->fields.array) &&
866 ((var->type->length == 0)|| (existing->type->length == 0))) {
867 if (var->type->length != 0) {
868 if ((int)var->type->length <= existing->data.max_array_access) {
869 linker_error(prog, "%s `%s' declared as type "
870 "`%s' but outermost dimension has an index"
871 " of `%i'\n",
872 mode_string(var),
873 var->name, var->type->name,
874 existing->data.max_array_access);
875 }
876 existing->type = var->type;
877 return true;
878 } else if (existing->type->length != 0) {
879 if((int)existing->type->length <= var->data.max_array_access &&
880 !existing->data.from_ssbo_unsized_array) {
881 linker_error(prog, "%s `%s' declared as type "
882 "`%s' but outermost dimension has an index"
883 " of `%i'\n",
884 mode_string(var),
885 var->name, existing->type->name,
886 var->data.max_array_access);
887 }
888 return true;
889 }
890 }
891 }
892 return false;
893 }
894
895
896 /**
897 * Perform validation of global variables used across multiple shaders
898 */
899 static void
900 cross_validate_globals(struct gl_context *ctx, struct gl_shader_program *prog,
901 struct exec_list *ir, glsl_symbol_table *variables,
902 bool uniforms_only)
903 {
904 foreach_in_list(ir_instruction, node, ir) {
905 ir_variable *const var = node->as_variable();
906
907 if (var == NULL)
908 continue;
909
910 if (uniforms_only && (var->data.mode != ir_var_uniform && var->data.mode != ir_var_shader_storage))
911 continue;
912
913 /* don't cross validate subroutine uniforms */
914 if (var->type->contains_subroutine())
915 continue;
916
917 /* Don't cross validate interface instances. These are only relevant
918 * inside a shader. The cross validation is done at the Interface Block
919 * name level.
920 */
921 if (var->is_interface_instance())
922 continue;
923
924 /* Don't cross validate temporaries that are at global scope. These
925 * will eventually get pulled into the shaders 'main'.
926 */
927 if (var->data.mode == ir_var_temporary)
928 continue;
929
930 /* If a global with this name has already been seen, verify that the
931 * new instance has the same type. In addition, if the globals have
932 * initializers, the values of the initializers must be the same.
933 */
934 ir_variable *const existing = variables->get_variable(var->name);
935 if (existing != NULL) {
936 /* Check if types match. */
937 if (var->type != existing->type) {
938 if (!validate_intrastage_arrays(prog, var, existing)) {
939 /* If it is an unsized array in a Shader Storage Block,
940 * two different shaders can access to different elements.
941 * Because of that, they might be converted to different
942 * sized arrays, then check that they are compatible but
943 * ignore the array size.
944 */
945 if (!(var->data.mode == ir_var_shader_storage &&
946 var->data.from_ssbo_unsized_array &&
947 existing->data.mode == ir_var_shader_storage &&
948 existing->data.from_ssbo_unsized_array &&
949 var->type->gl_type == existing->type->gl_type)) {
950 linker_error(prog, "%s `%s' declared as type "
951 "`%s' and type `%s'\n",
952 mode_string(var),
953 var->name, var->type->name,
954 existing->type->name);
955 return;
956 }
957 }
958 }
959
960 if (var->data.explicit_location) {
961 if (existing->data.explicit_location
962 && (var->data.location != existing->data.location)) {
963 linker_error(prog, "explicit locations for %s "
964 "`%s' have differing values\n",
965 mode_string(var), var->name);
966 return;
967 }
968
969 if (var->data.location_frac != existing->data.location_frac) {
970 linker_error(prog, "explicit components for %s `%s' have "
971 "differing values\n", mode_string(var), var->name);
972 return;
973 }
974
975 existing->data.location = var->data.location;
976 existing->data.explicit_location = true;
977 } else {
978 /* Check if uniform with implicit location was marked explicit
979 * by earlier shader stage. If so, mark it explicit in this stage
980 * too to make sure later processing does not treat it as
981 * implicit one.
982 */
983 if (existing->data.explicit_location) {
984 var->data.location = existing->data.location;
985 var->data.explicit_location = true;
986 }
987 }
988
989 /* From the GLSL 4.20 specification:
990 * "A link error will result if two compilation units in a program
991 * specify different integer-constant bindings for the same
992 * opaque-uniform name. However, it is not an error to specify a
993 * binding on some but not all declarations for the same name"
994 */
995 if (var->data.explicit_binding) {
996 if (existing->data.explicit_binding &&
997 var->data.binding != existing->data.binding) {
998 linker_error(prog, "explicit bindings for %s "
999 "`%s' have differing values\n",
1000 mode_string(var), var->name);
1001 return;
1002 }
1003
1004 existing->data.binding = var->data.binding;
1005 existing->data.explicit_binding = true;
1006 }
1007
1008 if (var->type->contains_atomic() &&
1009 var->data.offset != existing->data.offset) {
1010 linker_error(prog, "offset specifications for %s "
1011 "`%s' have differing values\n",
1012 mode_string(var), var->name);
1013 return;
1014 }
1015
1016 /* Validate layout qualifiers for gl_FragDepth.
1017 *
1018 * From the AMD/ARB_conservative_depth specs:
1019 *
1020 * "If gl_FragDepth is redeclared in any fragment shader in a
1021 * program, it must be redeclared in all fragment shaders in
1022 * that program that have static assignments to
1023 * gl_FragDepth. All redeclarations of gl_FragDepth in all
1024 * fragment shaders in a single program must have the same set
1025 * of qualifiers."
1026 */
1027 if (strcmp(var->name, "gl_FragDepth") == 0) {
1028 bool layout_declared = var->data.depth_layout != ir_depth_layout_none;
1029 bool layout_differs =
1030 var->data.depth_layout != existing->data.depth_layout;
1031
1032 if (layout_declared && layout_differs) {
1033 linker_error(prog,
1034 "All redeclarations of gl_FragDepth in all "
1035 "fragment shaders in a single program must have "
1036 "the same set of qualifiers.\n");
1037 }
1038
1039 if (var->data.used && layout_differs) {
1040 linker_error(prog,
1041 "If gl_FragDepth is redeclared with a layout "
1042 "qualifier in any fragment shader, it must be "
1043 "redeclared with the same layout qualifier in "
1044 "all fragment shaders that have assignments to "
1045 "gl_FragDepth\n");
1046 }
1047 }
1048
1049 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1050 *
1051 * "If a shared global has multiple initializers, the
1052 * initializers must all be constant expressions, and they
1053 * must all have the same value. Otherwise, a link error will
1054 * result. (A shared global having only one initializer does
1055 * not require that initializer to be a constant expression.)"
1056 *
1057 * Previous to 4.20 the GLSL spec simply said that initializers
1058 * must have the same value. In this case of non-constant
1059 * initializers, this was impossible to determine. As a result,
1060 * no vendor actually implemented that behavior. The 4.20
1061 * behavior matches the implemented behavior of at least one other
1062 * vendor, so we'll implement that for all GLSL versions.
1063 */
1064 if (var->constant_initializer != NULL) {
1065 if (existing->constant_initializer != NULL) {
1066 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
1067 linker_error(prog, "initializers for %s "
1068 "`%s' have differing values\n",
1069 mode_string(var), var->name);
1070 return;
1071 }
1072 } else {
1073 /* If the first-seen instance of a particular uniform did
1074 * not have an initializer but a later instance does,
1075 * replace the former with the later.
1076 */
1077 variables->replace_variable(existing->name, var);
1078 }
1079 }
1080
1081 if (var->data.has_initializer) {
1082 if (existing->data.has_initializer
1083 && (var->constant_initializer == NULL
1084 || existing->constant_initializer == NULL)) {
1085 linker_error(prog,
1086 "shared global variable `%s' has multiple "
1087 "non-constant initializers.\n",
1088 var->name);
1089 return;
1090 }
1091 }
1092
1093 if (existing->data.invariant != var->data.invariant) {
1094 linker_error(prog, "declarations for %s `%s' have "
1095 "mismatching invariant qualifiers\n",
1096 mode_string(var), var->name);
1097 return;
1098 }
1099 if (existing->data.centroid != var->data.centroid) {
1100 linker_error(prog, "declarations for %s `%s' have "
1101 "mismatching centroid qualifiers\n",
1102 mode_string(var), var->name);
1103 return;
1104 }
1105 if (existing->data.sample != var->data.sample) {
1106 linker_error(prog, "declarations for %s `%s` have "
1107 "mismatching sample qualifiers\n",
1108 mode_string(var), var->name);
1109 return;
1110 }
1111 if (existing->data.image_format != var->data.image_format) {
1112 linker_error(prog, "declarations for %s `%s` have "
1113 "mismatching image format qualifiers\n",
1114 mode_string(var), var->name);
1115 return;
1116 }
1117
1118 /* Check the precision qualifier matches for uniform variables on
1119 * GLSL ES.
1120 */
1121 if (!ctx->Const.AllowGLSLRelaxedES &&
1122 prog->IsES && !var->get_interface_type() &&
1123 existing->data.precision != var->data.precision) {
1124 if ((existing->data.used && var->data.used) || prog->data->Version >= 300) {
1125 linker_error(prog, "declarations for %s `%s` have "
1126 "mismatching precision qualifiers\n",
1127 mode_string(var), var->name);
1128 return;
1129 } else {
1130 linker_warning(prog, "declarations for %s `%s` have "
1131 "mismatching precision qualifiers\n",
1132 mode_string(var), var->name);
1133 }
1134 }
1135
1136 /* In OpenGL GLSL 3.20 spec, section 4.3.9:
1137 *
1138 * "It is a link-time error if any particular shader interface
1139 * contains:
1140 *
1141 * - two different blocks, each having no instance name, and each
1142 * having a member of the same name, or
1143 *
1144 * - a variable outside a block, and a block with no instance name,
1145 * where the variable has the same name as a member in the block."
1146 */
1147 const glsl_type *var_itype = var->get_interface_type();
1148 const glsl_type *existing_itype = existing->get_interface_type();
1149 if (var_itype != existing_itype) {
1150 if (!var_itype || !existing_itype) {
1151 linker_error(prog, "declarations for %s `%s` are inside block "
1152 "`%s` and outside a block",
1153 mode_string(var), var->name,
1154 var_itype ? var_itype->name : existing_itype->name);
1155 return;
1156 } else if (strcmp(var_itype->name, existing_itype->name) != 0) {
1157 linker_error(prog, "declarations for %s `%s` are inside blocks "
1158 "`%s` and `%s`",
1159 mode_string(var), var->name,
1160 existing_itype->name,
1161 var_itype->name);
1162 return;
1163 }
1164 }
1165 } else
1166 variables->add_variable(var);
1167 }
1168 }
1169
1170
1171 /**
1172 * Perform validation of uniforms used across multiple shader stages
1173 */
1174 static void
1175 cross_validate_uniforms(struct gl_context *ctx,
1176 struct gl_shader_program *prog)
1177 {
1178 glsl_symbol_table variables;
1179 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1180 if (prog->_LinkedShaders[i] == NULL)
1181 continue;
1182
1183 cross_validate_globals(ctx, prog, prog->_LinkedShaders[i]->ir,
1184 &variables, true);
1185 }
1186 }
1187
1188 /**
1189 * Accumulates the array of buffer blocks and checks that all definitions of
1190 * blocks agree on their contents.
1191 */
1192 static bool
1193 interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog,
1194 bool validate_ssbo)
1195 {
1196 int *InterfaceBlockStageIndex[MESA_SHADER_STAGES];
1197 struct gl_uniform_block *blks = NULL;
1198 unsigned *num_blks = validate_ssbo ? &prog->data->NumShaderStorageBlocks :
1199 &prog->data->NumUniformBlocks;
1200
1201 unsigned max_num_buffer_blocks = 0;
1202 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1203 if (prog->_LinkedShaders[i]) {
1204 if (validate_ssbo) {
1205 max_num_buffer_blocks +=
1206 prog->_LinkedShaders[i]->Program->info.num_ssbos;
1207 } else {
1208 max_num_buffer_blocks +=
1209 prog->_LinkedShaders[i]->Program->info.num_ubos;
1210 }
1211 }
1212 }
1213
1214 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1215 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
1216
1217 InterfaceBlockStageIndex[i] = new int[max_num_buffer_blocks];
1218 for (unsigned int j = 0; j < max_num_buffer_blocks; j++)
1219 InterfaceBlockStageIndex[i][j] = -1;
1220
1221 if (sh == NULL)
1222 continue;
1223
1224 unsigned sh_num_blocks;
1225 struct gl_uniform_block **sh_blks;
1226 if (validate_ssbo) {
1227 sh_num_blocks = prog->_LinkedShaders[i]->Program->info.num_ssbos;
1228 sh_blks = sh->Program->sh.ShaderStorageBlocks;
1229 } else {
1230 sh_num_blocks = prog->_LinkedShaders[i]->Program->info.num_ubos;
1231 sh_blks = sh->Program->sh.UniformBlocks;
1232 }
1233
1234 for (unsigned int j = 0; j < sh_num_blocks; j++) {
1235 int index = link_cross_validate_uniform_block(prog->data, &blks,
1236 num_blks, sh_blks[j]);
1237
1238 if (index == -1) {
1239 linker_error(prog, "buffer block `%s' has mismatching "
1240 "definitions\n", sh_blks[j]->Name);
1241
1242 for (unsigned k = 0; k <= i; k++) {
1243 delete[] InterfaceBlockStageIndex[k];
1244 }
1245
1246 /* Reset the block count. This will help avoid various segfaults
1247 * from api calls that assume the array exists due to the count
1248 * being non-zero.
1249 */
1250 *num_blks = 0;
1251 return false;
1252 }
1253
1254 InterfaceBlockStageIndex[i][index] = j;
1255 }
1256 }
1257
1258 /* Update per stage block pointers to point to the program list.
1259 * FIXME: We should be able to free the per stage blocks here.
1260 */
1261 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1262 for (unsigned j = 0; j < *num_blks; j++) {
1263 int stage_index = InterfaceBlockStageIndex[i][j];
1264
1265 if (stage_index != -1) {
1266 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
1267
1268 struct gl_uniform_block **sh_blks = validate_ssbo ?
1269 sh->Program->sh.ShaderStorageBlocks :
1270 sh->Program->sh.UniformBlocks;
1271
1272 blks[j].stageref |= sh_blks[stage_index]->stageref;
1273 sh_blks[stage_index] = &blks[j];
1274 }
1275 }
1276 }
1277
1278 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1279 delete[] InterfaceBlockStageIndex[i];
1280 }
1281
1282 if (validate_ssbo)
1283 prog->data->ShaderStorageBlocks = blks;
1284 else
1285 prog->data->UniformBlocks = blks;
1286
1287 return true;
1288 }
1289
1290 /**
1291 * Verifies the invariance of built-in special variables.
1292 */
1293 static bool
1294 validate_invariant_builtins(struct gl_shader_program *prog,
1295 const gl_linked_shader *vert,
1296 const gl_linked_shader *frag)
1297 {
1298 const ir_variable *var_vert;
1299 const ir_variable *var_frag;
1300
1301 if (!vert || !frag)
1302 return true;
1303
1304 /*
1305 * From OpenGL ES Shading Language 1.0 specification
1306 * (4.6.4 Invariance and Linkage):
1307 * "The invariance of varyings that are declared in both the vertex and
1308 * fragment shaders must match. For the built-in special variables,
1309 * gl_FragCoord can only be declared invariant if and only if
1310 * gl_Position is declared invariant. Similarly gl_PointCoord can only
1311 * be declared invariant if and only if gl_PointSize is declared
1312 * invariant. It is an error to declare gl_FrontFacing as invariant.
1313 * The invariance of gl_FrontFacing is the same as the invariance of
1314 * gl_Position."
1315 */
1316 var_frag = frag->symbols->get_variable("gl_FragCoord");
1317 if (var_frag && var_frag->data.invariant) {
1318 var_vert = vert->symbols->get_variable("gl_Position");
1319 if (var_vert && !var_vert->data.invariant) {
1320 linker_error(prog,
1321 "fragment shader built-in `%s' has invariant qualifier, "
1322 "but vertex shader built-in `%s' lacks invariant qualifier\n",
1323 var_frag->name, var_vert->name);
1324 return false;
1325 }
1326 }
1327
1328 var_frag = frag->symbols->get_variable("gl_PointCoord");
1329 if (var_frag && var_frag->data.invariant) {
1330 var_vert = vert->symbols->get_variable("gl_PointSize");
1331 if (var_vert && !var_vert->data.invariant) {
1332 linker_error(prog,
1333 "fragment shader built-in `%s' has invariant qualifier, "
1334 "but vertex shader built-in `%s' lacks invariant qualifier\n",
1335 var_frag->name, var_vert->name);
1336 return false;
1337 }
1338 }
1339
1340 var_frag = frag->symbols->get_variable("gl_FrontFacing");
1341 if (var_frag && var_frag->data.invariant) {
1342 linker_error(prog,
1343 "fragment shader built-in `%s' can not be declared as invariant\n",
1344 var_frag->name);
1345 return false;
1346 }
1347
1348 return true;
1349 }
1350
1351 /**
1352 * Populates a shaders symbol table with all global declarations
1353 */
1354 static void
1355 populate_symbol_table(gl_linked_shader *sh, glsl_symbol_table *symbols)
1356 {
1357 sh->symbols = new(sh) glsl_symbol_table;
1358
1359 _mesa_glsl_copy_symbols_from_table(sh->ir, symbols, sh->symbols);
1360 }
1361
1362
1363 /**
1364 * Remap variables referenced in an instruction tree
1365 *
1366 * This is used when instruction trees are cloned from one shader and placed in
1367 * another. These trees will contain references to \c ir_variable nodes that
1368 * do not exist in the target shader. This function finds these \c ir_variable
1369 * references and replaces the references with matching variables in the target
1370 * shader.
1371 *
1372 * If there is no matching variable in the target shader, a clone of the
1373 * \c ir_variable is made and added to the target shader. The new variable is
1374 * added to \b both the instruction stream and the symbol table.
1375 *
1376 * \param inst IR tree that is to be processed.
1377 * \param symbols Symbol table containing global scope symbols in the
1378 * linked shader.
1379 * \param instructions Instruction stream where new variable declarations
1380 * should be added.
1381 */
1382 static void
1383 remap_variables(ir_instruction *inst, struct gl_linked_shader *target,
1384 hash_table *temps)
1385 {
1386 class remap_visitor : public ir_hierarchical_visitor {
1387 public:
1388 remap_visitor(struct gl_linked_shader *target, hash_table *temps)
1389 {
1390 this->target = target;
1391 this->symbols = target->symbols;
1392 this->instructions = target->ir;
1393 this->temps = temps;
1394 }
1395
1396 virtual ir_visitor_status visit(ir_dereference_variable *ir)
1397 {
1398 if (ir->var->data.mode == ir_var_temporary) {
1399 hash_entry *entry = _mesa_hash_table_search(temps, ir->var);
1400 ir_variable *var = entry ? (ir_variable *) entry->data : NULL;
1401
1402 assert(var != NULL);
1403 ir->var = var;
1404 return visit_continue;
1405 }
1406
1407 ir_variable *const existing =
1408 this->symbols->get_variable(ir->var->name);
1409 if (existing != NULL)
1410 ir->var = existing;
1411 else {
1412 ir_variable *copy = ir->var->clone(this->target, NULL);
1413
1414 this->symbols->add_variable(copy);
1415 this->instructions->push_head(copy);
1416 ir->var = copy;
1417 }
1418
1419 return visit_continue;
1420 }
1421
1422 private:
1423 struct gl_linked_shader *target;
1424 glsl_symbol_table *symbols;
1425 exec_list *instructions;
1426 hash_table *temps;
1427 };
1428
1429 remap_visitor v(target, temps);
1430
1431 inst->accept(&v);
1432 }
1433
1434
1435 /**
1436 * Move non-declarations from one instruction stream to another
1437 *
1438 * The intended usage pattern of this function is to pass the pointer to the
1439 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
1440 * pointer) for \c last and \c false for \c make_copies on the first
1441 * call. Successive calls pass the return value of the previous call for
1442 * \c last and \c true for \c make_copies.
1443 *
1444 * \param instructions Source instruction stream
1445 * \param last Instruction after which new instructions should be
1446 * inserted in the target instruction stream
1447 * \param make_copies Flag selecting whether instructions in \c instructions
1448 * should be copied (via \c ir_instruction::clone) into the
1449 * target list or moved.
1450 *
1451 * \return
1452 * The new "last" instruction in the target instruction stream. This pointer
1453 * is suitable for use as the \c last parameter of a later call to this
1454 * function.
1455 */
1456 static exec_node *
1457 move_non_declarations(exec_list *instructions, exec_node *last,
1458 bool make_copies, gl_linked_shader *target)
1459 {
1460 hash_table *temps = NULL;
1461
1462 if (make_copies)
1463 temps = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1464 _mesa_key_pointer_equal);
1465
1466 foreach_in_list_safe(ir_instruction, inst, instructions) {
1467 if (inst->as_function())
1468 continue;
1469
1470 ir_variable *var = inst->as_variable();
1471 if ((var != NULL) && (var->data.mode != ir_var_temporary))
1472 continue;
1473
1474 assert(inst->as_assignment()
1475 || inst->as_call()
1476 || inst->as_if() /* for initializers with the ?: operator */
1477 || ((var != NULL) && (var->data.mode == ir_var_temporary)));
1478
1479 if (make_copies) {
1480 inst = inst->clone(target, NULL);
1481
1482 if (var != NULL)
1483 _mesa_hash_table_insert(temps, var, inst);
1484 else
1485 remap_variables(inst, target, temps);
1486 } else {
1487 inst->remove();
1488 }
1489
1490 last->insert_after(inst);
1491 last = inst;
1492 }
1493
1494 if (make_copies)
1495 _mesa_hash_table_destroy(temps, NULL);
1496
1497 return last;
1498 }
1499
1500
1501 /**
1502 * This class is only used in link_intrastage_shaders() below but declaring
1503 * it inside that function leads to compiler warnings with some versions of
1504 * gcc.
1505 */
1506 class array_sizing_visitor : public deref_type_updater {
1507 public:
1508 array_sizing_visitor()
1509 : mem_ctx(ralloc_context(NULL)),
1510 unnamed_interfaces(_mesa_hash_table_create(NULL, _mesa_hash_pointer,
1511 _mesa_key_pointer_equal))
1512 {
1513 }
1514
1515 ~array_sizing_visitor()
1516 {
1517 _mesa_hash_table_destroy(this->unnamed_interfaces, NULL);
1518 ralloc_free(this->mem_ctx);
1519 }
1520
1521 virtual ir_visitor_status visit(ir_variable *var)
1522 {
1523 const glsl_type *type_without_array;
1524 bool implicit_sized_array = var->data.implicit_sized_array;
1525 fixup_type(&var->type, var->data.max_array_access,
1526 var->data.from_ssbo_unsized_array,
1527 &implicit_sized_array);
1528 var->data.implicit_sized_array = implicit_sized_array;
1529 type_without_array = var->type->without_array();
1530 if (var->type->is_interface()) {
1531 if (interface_contains_unsized_arrays(var->type)) {
1532 const glsl_type *new_type =
1533 resize_interface_members(var->type,
1534 var->get_max_ifc_array_access(),
1535 var->is_in_shader_storage_block());
1536 var->type = new_type;
1537 var->change_interface_type(new_type);
1538 }
1539 } else if (type_without_array->is_interface()) {
1540 if (interface_contains_unsized_arrays(type_without_array)) {
1541 const glsl_type *new_type =
1542 resize_interface_members(type_without_array,
1543 var->get_max_ifc_array_access(),
1544 var->is_in_shader_storage_block());
1545 var->change_interface_type(new_type);
1546 var->type = update_interface_members_array(var->type, new_type);
1547 }
1548 } else if (const glsl_type *ifc_type = var->get_interface_type()) {
1549 /* Store a pointer to the variable in the unnamed_interfaces
1550 * hashtable.
1551 */
1552 hash_entry *entry =
1553 _mesa_hash_table_search(this->unnamed_interfaces,
1554 ifc_type);
1555
1556 ir_variable **interface_vars = entry ? (ir_variable **) entry->data : NULL;
1557
1558 if (interface_vars == NULL) {
1559 interface_vars = rzalloc_array(mem_ctx, ir_variable *,
1560 ifc_type->length);
1561 _mesa_hash_table_insert(this->unnamed_interfaces, ifc_type,
1562 interface_vars);
1563 }
1564 unsigned index = ifc_type->field_index(var->name);
1565 assert(index < ifc_type->length);
1566 assert(interface_vars[index] == NULL);
1567 interface_vars[index] = var;
1568 }
1569 return visit_continue;
1570 }
1571
1572 /**
1573 * For each unnamed interface block that was discovered while running the
1574 * visitor, adjust the interface type to reflect the newly assigned array
1575 * sizes, and fix up the ir_variable nodes to point to the new interface
1576 * type.
1577 */
1578 void fixup_unnamed_interface_types()
1579 {
1580 hash_table_call_foreach(this->unnamed_interfaces,
1581 fixup_unnamed_interface_type, NULL);
1582 }
1583
1584 private:
1585 /**
1586 * If the type pointed to by \c type represents an unsized array, replace
1587 * it with a sized array whose size is determined by max_array_access.
1588 */
1589 static void fixup_type(const glsl_type **type, unsigned max_array_access,
1590 bool from_ssbo_unsized_array, bool *implicit_sized)
1591 {
1592 if (!from_ssbo_unsized_array && (*type)->is_unsized_array()) {
1593 *type = glsl_type::get_array_instance((*type)->fields.array,
1594 max_array_access + 1);
1595 *implicit_sized = true;
1596 assert(*type != NULL);
1597 }
1598 }
1599
1600 static const glsl_type *
1601 update_interface_members_array(const glsl_type *type,
1602 const glsl_type *new_interface_type)
1603 {
1604 const glsl_type *element_type = type->fields.array;
1605 if (element_type->is_array()) {
1606 const glsl_type *new_array_type =
1607 update_interface_members_array(element_type, new_interface_type);
1608 return glsl_type::get_array_instance(new_array_type, type->length);
1609 } else {
1610 return glsl_type::get_array_instance(new_interface_type,
1611 type->length);
1612 }
1613 }
1614
1615 /**
1616 * Determine whether the given interface type contains unsized arrays (if
1617 * it doesn't, array_sizing_visitor doesn't need to process it).
1618 */
1619 static bool interface_contains_unsized_arrays(const glsl_type *type)
1620 {
1621 for (unsigned i = 0; i < type->length; i++) {
1622 const glsl_type *elem_type = type->fields.structure[i].type;
1623 if (elem_type->is_unsized_array())
1624 return true;
1625 }
1626 return false;
1627 }
1628
1629 /**
1630 * Create a new interface type based on the given type, with unsized arrays
1631 * replaced by sized arrays whose size is determined by
1632 * max_ifc_array_access.
1633 */
1634 static const glsl_type *
1635 resize_interface_members(const glsl_type *type,
1636 const int *max_ifc_array_access,
1637 bool is_ssbo)
1638 {
1639 unsigned num_fields = type->length;
1640 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1641 memcpy(fields, type->fields.structure,
1642 num_fields * sizeof(*fields));
1643 for (unsigned i = 0; i < num_fields; i++) {
1644 bool implicit_sized_array = fields[i].implicit_sized_array;
1645 /* If SSBO last member is unsized array, we don't replace it by a sized
1646 * array.
1647 */
1648 if (is_ssbo && i == (num_fields - 1))
1649 fixup_type(&fields[i].type, max_ifc_array_access[i],
1650 true, &implicit_sized_array);
1651 else
1652 fixup_type(&fields[i].type, max_ifc_array_access[i],
1653 false, &implicit_sized_array);
1654 fields[i].implicit_sized_array = implicit_sized_array;
1655 }
1656 glsl_interface_packing packing =
1657 (glsl_interface_packing) type->interface_packing;
1658 bool row_major = (bool) type->interface_row_major;
1659 const glsl_type *new_ifc_type =
1660 glsl_type::get_interface_instance(fields, num_fields,
1661 packing, row_major, type->name);
1662 delete [] fields;
1663 return new_ifc_type;
1664 }
1665
1666 static void fixup_unnamed_interface_type(const void *key, void *data,
1667 void *)
1668 {
1669 const glsl_type *ifc_type = (const glsl_type *) key;
1670 ir_variable **interface_vars = (ir_variable **) data;
1671 unsigned num_fields = ifc_type->length;
1672 glsl_struct_field *fields = new glsl_struct_field[num_fields];
1673 memcpy(fields, ifc_type->fields.structure,
1674 num_fields * sizeof(*fields));
1675 bool interface_type_changed = false;
1676 for (unsigned i = 0; i < num_fields; i++) {
1677 if (interface_vars[i] != NULL &&
1678 fields[i].type != interface_vars[i]->type) {
1679 fields[i].type = interface_vars[i]->type;
1680 interface_type_changed = true;
1681 }
1682 }
1683 if (!interface_type_changed) {
1684 delete [] fields;
1685 return;
1686 }
1687 glsl_interface_packing packing =
1688 (glsl_interface_packing) ifc_type->interface_packing;
1689 bool row_major = (bool) ifc_type->interface_row_major;
1690 const glsl_type *new_ifc_type =
1691 glsl_type::get_interface_instance(fields, num_fields, packing,
1692 row_major, ifc_type->name);
1693 delete [] fields;
1694 for (unsigned i = 0; i < num_fields; i++) {
1695 if (interface_vars[i] != NULL)
1696 interface_vars[i]->change_interface_type(new_ifc_type);
1697 }
1698 }
1699
1700 /**
1701 * Memory context used to allocate the data in \c unnamed_interfaces.
1702 */
1703 void *mem_ctx;
1704
1705 /**
1706 * Hash table from const glsl_type * to an array of ir_variable *'s
1707 * pointing to the ir_variables constituting each unnamed interface block.
1708 */
1709 hash_table *unnamed_interfaces;
1710 };
1711
1712 static bool
1713 validate_xfb_buffer_stride(struct gl_context *ctx, unsigned idx,
1714 struct gl_shader_program *prog)
1715 {
1716 /* We will validate doubles at a later stage */
1717 if (prog->TransformFeedback.BufferStride[idx] % 4) {
1718 linker_error(prog, "invalid qualifier xfb_stride=%d must be a "
1719 "multiple of 4 or if its applied to a type that is "
1720 "or contains a double a multiple of 8.",
1721 prog->TransformFeedback.BufferStride[idx]);
1722 return false;
1723 }
1724
1725 if (prog->TransformFeedback.BufferStride[idx] / 4 >
1726 ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1727 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1728 "limit has been exceeded.");
1729 return false;
1730 }
1731
1732 return true;
1733 }
1734
1735 /**
1736 * Check for conflicting xfb_stride default qualifiers and store buffer stride
1737 * for later use.
1738 */
1739 static void
1740 link_xfb_stride_layout_qualifiers(struct gl_context *ctx,
1741 struct gl_shader_program *prog,
1742 struct gl_shader **shader_list,
1743 unsigned num_shaders)
1744 {
1745 for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {
1746 prog->TransformFeedback.BufferStride[i] = 0;
1747 }
1748
1749 for (unsigned i = 0; i < num_shaders; i++) {
1750 struct gl_shader *shader = shader_list[i];
1751
1752 for (unsigned j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1753 if (shader->TransformFeedbackBufferStride[j]) {
1754 if (prog->TransformFeedback.BufferStride[j] == 0) {
1755 prog->TransformFeedback.BufferStride[j] =
1756 shader->TransformFeedbackBufferStride[j];
1757 if (!validate_xfb_buffer_stride(ctx, j, prog))
1758 return;
1759 } else if (prog->TransformFeedback.BufferStride[j] !=
1760 shader->TransformFeedbackBufferStride[j]){
1761 linker_error(prog,
1762 "intrastage shaders defined with conflicting "
1763 "xfb_stride for buffer %d (%d and %d)\n", j,
1764 prog->TransformFeedback.BufferStride[j],
1765 shader->TransformFeedbackBufferStride[j]);
1766 return;
1767 }
1768 }
1769 }
1770 }
1771 }
1772
1773 /**
1774 * Check for conflicting bindless/bound sampler/image layout qualifiers at
1775 * global scope.
1776 */
1777 static void
1778 link_bindless_layout_qualifiers(struct gl_shader_program *prog,
1779 struct gl_shader **shader_list,
1780 unsigned num_shaders)
1781 {
1782 bool bindless_sampler, bindless_image;
1783 bool bound_sampler, bound_image;
1784
1785 bindless_sampler = bindless_image = false;
1786 bound_sampler = bound_image = false;
1787
1788 for (unsigned i = 0; i < num_shaders; i++) {
1789 struct gl_shader *shader = shader_list[i];
1790
1791 if (shader->bindless_sampler)
1792 bindless_sampler = true;
1793 if (shader->bindless_image)
1794 bindless_image = true;
1795 if (shader->bound_sampler)
1796 bound_sampler = true;
1797 if (shader->bound_image)
1798 bound_image = true;
1799
1800 if ((bindless_sampler && bound_sampler) ||
1801 (bindless_image && bound_image)) {
1802 /* From section 4.4.6 of the ARB_bindless_texture spec:
1803 *
1804 * "If both bindless_sampler and bound_sampler, or bindless_image
1805 * and bound_image, are declared at global scope in any
1806 * compilation unit, a link- time error will be generated."
1807 */
1808 linker_error(prog, "both bindless_sampler and bound_sampler, or "
1809 "bindless_image and bound_image, can't be declared at "
1810 "global scope");
1811 }
1812 }
1813 }
1814
1815 /**
1816 * Performs the cross-validation of tessellation control shader vertices and
1817 * layout qualifiers for the attached tessellation control shaders,
1818 * and propagates them to the linked TCS and linked shader program.
1819 */
1820 static void
1821 link_tcs_out_layout_qualifiers(struct gl_shader_program *prog,
1822 struct gl_program *gl_prog,
1823 struct gl_shader **shader_list,
1824 unsigned num_shaders)
1825 {
1826 if (gl_prog->info.stage != MESA_SHADER_TESS_CTRL)
1827 return;
1828
1829 gl_prog->info.tess.tcs_vertices_out = 0;
1830
1831 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1832 *
1833 * "All tessellation control shader layout declarations in a program
1834 * must specify the same output patch vertex count. There must be at
1835 * least one layout qualifier specifying an output patch vertex count
1836 * in any program containing tessellation control shaders; however,
1837 * such a declaration is not required in all tessellation control
1838 * shaders."
1839 */
1840
1841 for (unsigned i = 0; i < num_shaders; i++) {
1842 struct gl_shader *shader = shader_list[i];
1843
1844 if (shader->info.TessCtrl.VerticesOut != 0) {
1845 if (gl_prog->info.tess.tcs_vertices_out != 0 &&
1846 gl_prog->info.tess.tcs_vertices_out !=
1847 (unsigned) shader->info.TessCtrl.VerticesOut) {
1848 linker_error(prog, "tessellation control shader defined with "
1849 "conflicting output vertex count (%d and %d)\n",
1850 gl_prog->info.tess.tcs_vertices_out,
1851 shader->info.TessCtrl.VerticesOut);
1852 return;
1853 }
1854 gl_prog->info.tess.tcs_vertices_out =
1855 shader->info.TessCtrl.VerticesOut;
1856 }
1857 }
1858
1859 /* Just do the intrastage -> interstage propagation right now,
1860 * since we already know we're in the right type of shader program
1861 * for doing it.
1862 */
1863 if (gl_prog->info.tess.tcs_vertices_out == 0) {
1864 linker_error(prog, "tessellation control shader didn't declare "
1865 "vertices out layout qualifier\n");
1866 return;
1867 }
1868 }
1869
1870
1871 /**
1872 * Performs the cross-validation of tessellation evaluation shader
1873 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1874 * for the attached tessellation evaluation shaders, and propagates them
1875 * to the linked TES and linked shader program.
1876 */
1877 static void
1878 link_tes_in_layout_qualifiers(struct gl_shader_program *prog,
1879 struct gl_program *gl_prog,
1880 struct gl_shader **shader_list,
1881 unsigned num_shaders)
1882 {
1883 if (gl_prog->info.stage != MESA_SHADER_TESS_EVAL)
1884 return;
1885
1886 int point_mode = -1;
1887 unsigned vertex_order = 0;
1888
1889 gl_prog->info.tess.primitive_mode = PRIM_UNKNOWN;
1890 gl_prog->info.tess.spacing = TESS_SPACING_UNSPECIFIED;
1891
1892 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1893 *
1894 * "At least one tessellation evaluation shader (compilation unit) in
1895 * a program must declare a primitive mode in its input layout.
1896 * Declaration vertex spacing, ordering, and point mode identifiers is
1897 * optional. It is not required that all tessellation evaluation
1898 * shaders in a program declare a primitive mode. If spacing or
1899 * vertex ordering declarations are omitted, the tessellation
1900 * primitive generator will use equal spacing or counter-clockwise
1901 * vertex ordering, respectively. If a point mode declaration is
1902 * omitted, the tessellation primitive generator will produce lines or
1903 * triangles according to the primitive mode."
1904 */
1905
1906 for (unsigned i = 0; i < num_shaders; i++) {
1907 struct gl_shader *shader = shader_list[i];
1908
1909 if (shader->info.TessEval.PrimitiveMode != PRIM_UNKNOWN) {
1910 if (gl_prog->info.tess.primitive_mode != PRIM_UNKNOWN &&
1911 gl_prog->info.tess.primitive_mode !=
1912 shader->info.TessEval.PrimitiveMode) {
1913 linker_error(prog, "tessellation evaluation shader defined with "
1914 "conflicting input primitive modes.\n");
1915 return;
1916 }
1917 gl_prog->info.tess.primitive_mode =
1918 shader->info.TessEval.PrimitiveMode;
1919 }
1920
1921 if (shader->info.TessEval.Spacing != 0) {
1922 if (gl_prog->info.tess.spacing != 0 && gl_prog->info.tess.spacing !=
1923 shader->info.TessEval.Spacing) {
1924 linker_error(prog, "tessellation evaluation shader defined with "
1925 "conflicting vertex spacing.\n");
1926 return;
1927 }
1928 gl_prog->info.tess.spacing = shader->info.TessEval.Spacing;
1929 }
1930
1931 if (shader->info.TessEval.VertexOrder != 0) {
1932 if (vertex_order != 0 &&
1933 vertex_order != shader->info.TessEval.VertexOrder) {
1934 linker_error(prog, "tessellation evaluation shader defined with "
1935 "conflicting ordering.\n");
1936 return;
1937 }
1938 vertex_order = shader->info.TessEval.VertexOrder;
1939 }
1940
1941 if (shader->info.TessEval.PointMode != -1) {
1942 if (point_mode != -1 &&
1943 point_mode != shader->info.TessEval.PointMode) {
1944 linker_error(prog, "tessellation evaluation shader defined with "
1945 "conflicting point modes.\n");
1946 return;
1947 }
1948 point_mode = shader->info.TessEval.PointMode;
1949 }
1950
1951 }
1952
1953 /* Just do the intrastage -> interstage propagation right now,
1954 * since we already know we're in the right type of shader program
1955 * for doing it.
1956 */
1957 if (gl_prog->info.tess.primitive_mode == PRIM_UNKNOWN) {
1958 linker_error(prog,
1959 "tessellation evaluation shader didn't declare input "
1960 "primitive modes.\n");
1961 return;
1962 }
1963
1964 if (gl_prog->info.tess.spacing == TESS_SPACING_UNSPECIFIED)
1965 gl_prog->info.tess.spacing = TESS_SPACING_EQUAL;
1966
1967 if (vertex_order == 0 || vertex_order == GL_CCW)
1968 gl_prog->info.tess.ccw = true;
1969 else
1970 gl_prog->info.tess.ccw = false;
1971
1972
1973 if (point_mode == -1 || point_mode == GL_FALSE)
1974 gl_prog->info.tess.point_mode = false;
1975 else
1976 gl_prog->info.tess.point_mode = true;
1977 }
1978
1979
1980 /**
1981 * Performs the cross-validation of layout qualifiers specified in
1982 * redeclaration of gl_FragCoord for the attached fragment shaders,
1983 * and propagates them to the linked FS and linked shader program.
1984 */
1985 static void
1986 link_fs_inout_layout_qualifiers(struct gl_shader_program *prog,
1987 struct gl_linked_shader *linked_shader,
1988 struct gl_shader **shader_list,
1989 unsigned num_shaders)
1990 {
1991 bool redeclares_gl_fragcoord = false;
1992 bool uses_gl_fragcoord = false;
1993 bool origin_upper_left = false;
1994 bool pixel_center_integer = false;
1995
1996 if (linked_shader->Stage != MESA_SHADER_FRAGMENT ||
1997 (prog->data->Version < 150 &&
1998 !prog->ARB_fragment_coord_conventions_enable))
1999 return;
2000
2001 for (unsigned i = 0; i < num_shaders; i++) {
2002 struct gl_shader *shader = shader_list[i];
2003 /* From the GLSL 1.50 spec, page 39:
2004 *
2005 * "If gl_FragCoord is redeclared in any fragment shader in a program,
2006 * it must be redeclared in all the fragment shaders in that program
2007 * that have a static use gl_FragCoord."
2008 */
2009 if ((redeclares_gl_fragcoord && !shader->redeclares_gl_fragcoord &&
2010 shader->uses_gl_fragcoord)
2011 || (shader->redeclares_gl_fragcoord && !redeclares_gl_fragcoord &&
2012 uses_gl_fragcoord)) {
2013 linker_error(prog, "fragment shader defined with conflicting "
2014 "layout qualifiers for gl_FragCoord\n");
2015 }
2016
2017 /* From the GLSL 1.50 spec, page 39:
2018 *
2019 * "All redeclarations of gl_FragCoord in all fragment shaders in a
2020 * single program must have the same set of qualifiers."
2021 */
2022 if (redeclares_gl_fragcoord && shader->redeclares_gl_fragcoord &&
2023 (shader->origin_upper_left != origin_upper_left ||
2024 shader->pixel_center_integer != pixel_center_integer)) {
2025 linker_error(prog, "fragment shader defined with conflicting "
2026 "layout qualifiers for gl_FragCoord\n");
2027 }
2028
2029 /* Update the linked shader state. Note that uses_gl_fragcoord should
2030 * accumulate the results. The other values should replace. If there
2031 * are multiple redeclarations, all the fields except uses_gl_fragcoord
2032 * are already known to be the same.
2033 */
2034 if (shader->redeclares_gl_fragcoord || shader->uses_gl_fragcoord) {
2035 redeclares_gl_fragcoord = shader->redeclares_gl_fragcoord;
2036 uses_gl_fragcoord |= shader->uses_gl_fragcoord;
2037 origin_upper_left = shader->origin_upper_left;
2038 pixel_center_integer = shader->pixel_center_integer;
2039 }
2040
2041 linked_shader->Program->info.fs.early_fragment_tests |=
2042 shader->EarlyFragmentTests || shader->PostDepthCoverage;
2043 linked_shader->Program->info.fs.inner_coverage |= shader->InnerCoverage;
2044 linked_shader->Program->info.fs.post_depth_coverage |=
2045 shader->PostDepthCoverage;
2046 linked_shader->Program->info.fs.pixel_interlock_ordered |=
2047 shader->PixelInterlockOrdered;
2048 linked_shader->Program->info.fs.pixel_interlock_unordered |=
2049 shader->PixelInterlockUnordered;
2050 linked_shader->Program->info.fs.sample_interlock_ordered |=
2051 shader->SampleInterlockOrdered;
2052 linked_shader->Program->info.fs.sample_interlock_unordered |=
2053 shader->SampleInterlockUnordered;
2054
2055 linked_shader->Program->sh.fs.BlendSupport |= shader->BlendSupport;
2056 }
2057 }
2058
2059 /**
2060 * Performs the cross-validation of geometry shader max_vertices and
2061 * primitive type layout qualifiers for the attached geometry shaders,
2062 * and propagates them to the linked GS and linked shader program.
2063 */
2064 static void
2065 link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
2066 struct gl_program *gl_prog,
2067 struct gl_shader **shader_list,
2068 unsigned num_shaders)
2069 {
2070 /* No in/out qualifiers defined for anything but GLSL 1.50+
2071 * geometry shaders so far.
2072 */
2073 if (gl_prog->info.stage != MESA_SHADER_GEOMETRY ||
2074 prog->data->Version < 150)
2075 return;
2076
2077 int vertices_out = -1;
2078
2079 gl_prog->info.gs.invocations = 0;
2080 gl_prog->info.gs.input_primitive = PRIM_UNKNOWN;
2081 gl_prog->info.gs.output_primitive = PRIM_UNKNOWN;
2082
2083 /* From the GLSL 1.50 spec, page 46:
2084 *
2085 * "All geometry shader output layout declarations in a program
2086 * must declare the same layout and same value for
2087 * max_vertices. There must be at least one geometry output
2088 * layout declaration somewhere in a program, but not all
2089 * geometry shaders (compilation units) are required to
2090 * declare it."
2091 */
2092
2093 for (unsigned i = 0; i < num_shaders; i++) {
2094 struct gl_shader *shader = shader_list[i];
2095
2096 if (shader->info.Geom.InputType != PRIM_UNKNOWN) {
2097 if (gl_prog->info.gs.input_primitive != PRIM_UNKNOWN &&
2098 gl_prog->info.gs.input_primitive !=
2099 shader->info.Geom.InputType) {
2100 linker_error(prog, "geometry shader defined with conflicting "
2101 "input types\n");
2102 return;
2103 }
2104 gl_prog->info.gs.input_primitive = shader->info.Geom.InputType;
2105 }
2106
2107 if (shader->info.Geom.OutputType != PRIM_UNKNOWN) {
2108 if (gl_prog->info.gs.output_primitive != PRIM_UNKNOWN &&
2109 gl_prog->info.gs.output_primitive !=
2110 shader->info.Geom.OutputType) {
2111 linker_error(prog, "geometry shader defined with conflicting "
2112 "output types\n");
2113 return;
2114 }
2115 gl_prog->info.gs.output_primitive = shader->info.Geom.OutputType;
2116 }
2117
2118 if (shader->info.Geom.VerticesOut != -1) {
2119 if (vertices_out != -1 &&
2120 vertices_out != shader->info.Geom.VerticesOut) {
2121 linker_error(prog, "geometry shader defined with conflicting "
2122 "output vertex count (%d and %d)\n",
2123 vertices_out, shader->info.Geom.VerticesOut);
2124 return;
2125 }
2126 vertices_out = shader->info.Geom.VerticesOut;
2127 }
2128
2129 if (shader->info.Geom.Invocations != 0) {
2130 if (gl_prog->info.gs.invocations != 0 &&
2131 gl_prog->info.gs.invocations !=
2132 (unsigned) shader->info.Geom.Invocations) {
2133 linker_error(prog, "geometry shader defined with conflicting "
2134 "invocation count (%d and %d)\n",
2135 gl_prog->info.gs.invocations,
2136 shader->info.Geom.Invocations);
2137 return;
2138 }
2139 gl_prog->info.gs.invocations = shader->info.Geom.Invocations;
2140 }
2141 }
2142
2143 /* Just do the intrastage -> interstage propagation right now,
2144 * since we already know we're in the right type of shader program
2145 * for doing it.
2146 */
2147 if (gl_prog->info.gs.input_primitive == PRIM_UNKNOWN) {
2148 linker_error(prog,
2149 "geometry shader didn't declare primitive input type\n");
2150 return;
2151 }
2152
2153 if (gl_prog->info.gs.output_primitive == PRIM_UNKNOWN) {
2154 linker_error(prog,
2155 "geometry shader didn't declare primitive output type\n");
2156 return;
2157 }
2158
2159 if (vertices_out == -1) {
2160 linker_error(prog,
2161 "geometry shader didn't declare max_vertices\n");
2162 return;
2163 } else {
2164 gl_prog->info.gs.vertices_out = vertices_out;
2165 }
2166
2167 if (gl_prog->info.gs.invocations == 0)
2168 gl_prog->info.gs.invocations = 1;
2169 }
2170
2171
2172 /**
2173 * Perform cross-validation of compute shader local_size_{x,y,z} layout
2174 * qualifiers for the attached compute shaders, and propagate them to the
2175 * linked CS and linked shader program.
2176 */
2177 static void
2178 link_cs_input_layout_qualifiers(struct gl_shader_program *prog,
2179 struct gl_program *gl_prog,
2180 struct gl_shader **shader_list,
2181 unsigned num_shaders)
2182 {
2183 /* This function is called for all shader stages, but it only has an effect
2184 * for compute shaders.
2185 */
2186 if (gl_prog->info.stage != MESA_SHADER_COMPUTE)
2187 return;
2188
2189 for (int i = 0; i < 3; i++)
2190 gl_prog->info.cs.local_size[i] = 0;
2191
2192 gl_prog->info.cs.local_size_variable = false;
2193
2194 /* From the ARB_compute_shader spec, in the section describing local size
2195 * declarations:
2196 *
2197 * If multiple compute shaders attached to a single program object
2198 * declare local work-group size, the declarations must be identical;
2199 * otherwise a link-time error results. Furthermore, if a program
2200 * object contains any compute shaders, at least one must contain an
2201 * input layout qualifier specifying the local work sizes of the
2202 * program, or a link-time error will occur.
2203 */
2204 for (unsigned sh = 0; sh < num_shaders; sh++) {
2205 struct gl_shader *shader = shader_list[sh];
2206
2207 if (shader->info.Comp.LocalSize[0] != 0) {
2208 if (gl_prog->info.cs.local_size[0] != 0) {
2209 for (int i = 0; i < 3; i++) {
2210 if (gl_prog->info.cs.local_size[i] !=
2211 shader->info.Comp.LocalSize[i]) {
2212 linker_error(prog, "compute shader defined with conflicting "
2213 "local sizes\n");
2214 return;
2215 }
2216 }
2217 }
2218 for (int i = 0; i < 3; i++) {
2219 gl_prog->info.cs.local_size[i] =
2220 shader->info.Comp.LocalSize[i];
2221 }
2222 } else if (shader->info.Comp.LocalSizeVariable) {
2223 if (gl_prog->info.cs.local_size[0] != 0) {
2224 /* The ARB_compute_variable_group_size spec says:
2225 *
2226 * If one compute shader attached to a program declares a
2227 * variable local group size and a second compute shader
2228 * attached to the same program declares a fixed local group
2229 * size, a link-time error results.
2230 */
2231 linker_error(prog, "compute shader defined with both fixed and "
2232 "variable local group size\n");
2233 return;
2234 }
2235 gl_prog->info.cs.local_size_variable = true;
2236 }
2237 }
2238
2239 /* Just do the intrastage -> interstage propagation right now,
2240 * since we already know we're in the right type of shader program
2241 * for doing it.
2242 */
2243 if (gl_prog->info.cs.local_size[0] == 0 &&
2244 !gl_prog->info.cs.local_size_variable) {
2245 linker_error(prog, "compute shader must contain a fixed or a variable "
2246 "local group size\n");
2247 return;
2248 }
2249 }
2250
2251 /**
2252 * Link all out variables on a single stage which are not
2253 * directly used in a shader with the main function.
2254 */
2255 static void
2256 link_output_variables(struct gl_linked_shader *linked_shader,
2257 struct gl_shader **shader_list,
2258 unsigned num_shaders)
2259 {
2260 struct glsl_symbol_table *symbols = linked_shader->symbols;
2261
2262 for (unsigned i = 0; i < num_shaders; i++) {
2263
2264 /* Skip shader object with main function */
2265 if (shader_list[i]->symbols->get_function("main"))
2266 continue;
2267
2268 foreach_in_list(ir_instruction, ir, shader_list[i]->ir) {
2269 if (ir->ir_type != ir_type_variable)
2270 continue;
2271
2272 ir_variable *const var = (ir_variable *) ir;
2273
2274 if (var->data.mode == ir_var_shader_out &&
2275 !symbols->get_variable(var->name)) {
2276 symbols->add_variable(var);
2277 linked_shader->ir->push_head(var);
2278 }
2279 }
2280 }
2281
2282 return;
2283 }
2284
2285
2286 /**
2287 * Combine a group of shaders for a single stage to generate a linked shader
2288 *
2289 * \note
2290 * If this function is supplied a single shader, it is cloned, and the new
2291 * shader is returned.
2292 */
2293 struct gl_linked_shader *
2294 link_intrastage_shaders(void *mem_ctx,
2295 struct gl_context *ctx,
2296 struct gl_shader_program *prog,
2297 struct gl_shader **shader_list,
2298 unsigned num_shaders,
2299 bool allow_missing_main)
2300 {
2301 struct gl_uniform_block *ubo_blocks = NULL;
2302 struct gl_uniform_block *ssbo_blocks = NULL;
2303 unsigned num_ubo_blocks = 0;
2304 unsigned num_ssbo_blocks = 0;
2305
2306 /* Check that global variables defined in multiple shaders are consistent.
2307 */
2308 glsl_symbol_table variables;
2309 for (unsigned i = 0; i < num_shaders; i++) {
2310 if (shader_list[i] == NULL)
2311 continue;
2312 cross_validate_globals(ctx, prog, shader_list[i]->ir, &variables,
2313 false);
2314 }
2315
2316 if (!prog->data->LinkStatus)
2317 return NULL;
2318
2319 /* Check that interface blocks defined in multiple shaders are consistent.
2320 */
2321 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
2322 num_shaders);
2323 if (!prog->data->LinkStatus)
2324 return NULL;
2325
2326 /* Check that there is only a single definition of each function signature
2327 * across all shaders.
2328 */
2329 for (unsigned i = 0; i < (num_shaders - 1); i++) {
2330 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
2331 ir_function *const f = node->as_function();
2332
2333 if (f == NULL)
2334 continue;
2335
2336 for (unsigned j = i + 1; j < num_shaders; j++) {
2337 ir_function *const other =
2338 shader_list[j]->symbols->get_function(f->name);
2339
2340 /* If the other shader has no function (and therefore no function
2341 * signatures) with the same name, skip to the next shader.
2342 */
2343 if (other == NULL)
2344 continue;
2345
2346 foreach_in_list(ir_function_signature, sig, &f->signatures) {
2347 if (!sig->is_defined)
2348 continue;
2349
2350 ir_function_signature *other_sig =
2351 other->exact_matching_signature(NULL, &sig->parameters);
2352
2353 if (other_sig != NULL && other_sig->is_defined) {
2354 linker_error(prog, "function `%s' is multiply defined\n",
2355 f->name);
2356 return NULL;
2357 }
2358 }
2359 }
2360 }
2361 }
2362
2363 /* Find the shader that defines main, and make a clone of it.
2364 *
2365 * Starting with the clone, search for undefined references. If one is
2366 * found, find the shader that defines it. Clone the reference and add
2367 * it to the shader. Repeat until there are no undefined references or
2368 * until a reference cannot be resolved.
2369 */
2370 gl_shader *main = NULL;
2371 for (unsigned i = 0; i < num_shaders; i++) {
2372 if (_mesa_get_main_function_signature(shader_list[i]->symbols)) {
2373 main = shader_list[i];
2374 break;
2375 }
2376 }
2377
2378 if (main == NULL && allow_missing_main)
2379 main = shader_list[0];
2380
2381 if (main == NULL) {
2382 linker_error(prog, "%s shader lacks `main'\n",
2383 _mesa_shader_stage_to_string(shader_list[0]->Stage));
2384 return NULL;
2385 }
2386
2387 gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
2388 linked->Stage = shader_list[0]->Stage;
2389
2390 /* Create program and attach it to the linked shader */
2391 struct gl_program *gl_prog =
2392 ctx->Driver.NewProgram(ctx,
2393 _mesa_shader_stage_to_program(shader_list[0]->Stage),
2394 prog->Name, false);
2395 if (!gl_prog) {
2396 prog->data->LinkStatus = LINKING_FAILURE;
2397 _mesa_delete_linked_shader(ctx, linked);
2398 return NULL;
2399 }
2400
2401 _mesa_reference_shader_program_data(ctx, &gl_prog->sh.data, prog->data);
2402
2403 /* Don't use _mesa_reference_program() just take ownership */
2404 linked->Program = gl_prog;
2405
2406 linked->ir = new(linked) exec_list;
2407 clone_ir_list(mem_ctx, linked->ir, main->ir);
2408
2409 link_fs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
2410 link_tcs_out_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
2411 link_tes_in_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
2412 link_gs_inout_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
2413 link_cs_input_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
2414
2415 if (linked->Stage != MESA_SHADER_FRAGMENT)
2416 link_xfb_stride_layout_qualifiers(ctx, prog, shader_list, num_shaders);
2417
2418 link_bindless_layout_qualifiers(prog, shader_list, num_shaders);
2419
2420 populate_symbol_table(linked, shader_list[0]->symbols);
2421
2422 /* The pointer to the main function in the final linked shader (i.e., the
2423 * copy of the original shader that contained the main function).
2424 */
2425 ir_function_signature *const main_sig =
2426 _mesa_get_main_function_signature(linked->symbols);
2427
2428 /* Move any instructions other than variable declarations or function
2429 * declarations into main.
2430 */
2431 if (main_sig != NULL) {
2432 exec_node *insertion_point =
2433 move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
2434 linked);
2435
2436 for (unsigned i = 0; i < num_shaders; i++) {
2437 if (shader_list[i] == main)
2438 continue;
2439
2440 insertion_point = move_non_declarations(shader_list[i]->ir,
2441 insertion_point, true, linked);
2442 }
2443 }
2444
2445 if (!link_function_calls(prog, linked, shader_list, num_shaders)) {
2446 _mesa_delete_linked_shader(ctx, linked);
2447 return NULL;
2448 }
2449
2450 if (linked->Stage != MESA_SHADER_FRAGMENT)
2451 link_output_variables(linked, shader_list, num_shaders);
2452
2453 /* Make a pass over all variable declarations to ensure that arrays with
2454 * unspecified sizes have a size specified. The size is inferred from the
2455 * max_array_access field.
2456 */
2457 array_sizing_visitor v;
2458 v.run(linked->ir);
2459 v.fixup_unnamed_interface_types();
2460
2461 /* Link up uniform blocks defined within this stage. */
2462 link_uniform_blocks(mem_ctx, ctx, prog, linked, &ubo_blocks,
2463 &num_ubo_blocks, &ssbo_blocks, &num_ssbo_blocks);
2464
2465 if (!prog->data->LinkStatus) {
2466 _mesa_delete_linked_shader(ctx, linked);
2467 return NULL;
2468 }
2469
2470 /* Copy ubo blocks to linked shader list */
2471 linked->Program->sh.UniformBlocks =
2472 ralloc_array(linked, gl_uniform_block *, num_ubo_blocks);
2473 ralloc_steal(linked, ubo_blocks);
2474 for (unsigned i = 0; i < num_ubo_blocks; i++) {
2475 linked->Program->sh.UniformBlocks[i] = &ubo_blocks[i];
2476 }
2477 linked->Program->info.num_ubos = num_ubo_blocks;
2478
2479 /* Copy ssbo blocks to linked shader list */
2480 linked->Program->sh.ShaderStorageBlocks =
2481 ralloc_array(linked, gl_uniform_block *, num_ssbo_blocks);
2482 ralloc_steal(linked, ssbo_blocks);
2483 for (unsigned i = 0; i < num_ssbo_blocks; i++) {
2484 linked->Program->sh.ShaderStorageBlocks[i] = &ssbo_blocks[i];
2485 }
2486 linked->Program->info.num_ssbos = num_ssbo_blocks;
2487
2488 /* At this point linked should contain all of the linked IR, so
2489 * validate it to make sure nothing went wrong.
2490 */
2491 validate_ir_tree(linked->ir);
2492
2493 /* Set the size of geometry shader input arrays */
2494 if (linked->Stage == MESA_SHADER_GEOMETRY) {
2495 unsigned num_vertices =
2496 vertices_per_prim(gl_prog->info.gs.input_primitive);
2497 array_resize_visitor input_resize_visitor(num_vertices, prog,
2498 MESA_SHADER_GEOMETRY);
2499 foreach_in_list(ir_instruction, ir, linked->ir) {
2500 ir->accept(&input_resize_visitor);
2501 }
2502 }
2503
2504 if (ctx->Const.VertexID_is_zero_based)
2505 lower_vertex_id(linked);
2506
2507 if (ctx->Const.LowerCsDerivedVariables)
2508 lower_cs_derived(linked);
2509
2510 #ifdef DEBUG
2511 /* Compute the source checksum. */
2512 linked->SourceChecksum = 0;
2513 for (unsigned i = 0; i < num_shaders; i++) {
2514 if (shader_list[i] == NULL)
2515 continue;
2516 linked->SourceChecksum ^= shader_list[i]->SourceChecksum;
2517 }
2518 #endif
2519
2520 return linked;
2521 }
2522
2523 /**
2524 * Update the sizes of linked shader uniform arrays to the maximum
2525 * array index used.
2526 *
2527 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
2528 *
2529 * If one or more elements of an array are active,
2530 * GetActiveUniform will return the name of the array in name,
2531 * subject to the restrictions listed above. The type of the array
2532 * is returned in type. The size parameter contains the highest
2533 * array element index used, plus one. The compiler or linker
2534 * determines the highest index used. There will be only one
2535 * active uniform reported by the GL per uniform array.
2536
2537 */
2538 static void
2539 update_array_sizes(struct gl_shader_program *prog)
2540 {
2541 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2542 if (prog->_LinkedShaders[i] == NULL)
2543 continue;
2544
2545 bool types_were_updated = false;
2546
2547 foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) {
2548 ir_variable *const var = node->as_variable();
2549
2550 if ((var == NULL) || (var->data.mode != ir_var_uniform) ||
2551 !var->type->is_array())
2552 continue;
2553
2554 /* GL_ARB_uniform_buffer_object says that std140 uniforms
2555 * will not be eliminated. Since we always do std140, just
2556 * don't resize arrays in UBOs.
2557 *
2558 * Atomic counters are supposed to get deterministic
2559 * locations assigned based on the declaration ordering and
2560 * sizes, array compaction would mess that up.
2561 *
2562 * Subroutine uniforms are not removed.
2563 */
2564 if (var->is_in_buffer_block() || var->type->contains_atomic() ||
2565 var->type->contains_subroutine() || var->constant_initializer)
2566 continue;
2567
2568 int size = var->data.max_array_access;
2569 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
2570 if (prog->_LinkedShaders[j] == NULL)
2571 continue;
2572
2573 foreach_in_list(ir_instruction, node2, prog->_LinkedShaders[j]->ir) {
2574 ir_variable *other_var = node2->as_variable();
2575 if (!other_var)
2576 continue;
2577
2578 if (strcmp(var->name, other_var->name) == 0 &&
2579 other_var->data.max_array_access > size) {
2580 size = other_var->data.max_array_access;
2581 }
2582 }
2583 }
2584
2585 if (size + 1 != (int)var->type->length) {
2586 /* If this is a built-in uniform (i.e., it's backed by some
2587 * fixed-function state), adjust the number of state slots to
2588 * match the new array size. The number of slots per array entry
2589 * is not known. It seems safe to assume that the total number of
2590 * slots is an integer multiple of the number of array elements.
2591 * Determine the number of slots per array element by dividing by
2592 * the old (total) size.
2593 */
2594 const unsigned num_slots = var->get_num_state_slots();
2595 if (num_slots > 0) {
2596 var->set_num_state_slots((size + 1)
2597 * (num_slots / var->type->length));
2598 }
2599
2600 var->type = glsl_type::get_array_instance(var->type->fields.array,
2601 size + 1);
2602 types_were_updated = true;
2603 }
2604 }
2605
2606 /* Update the types of dereferences in case we changed any. */
2607 if (types_were_updated) {
2608 deref_type_updater v;
2609 v.run(prog->_LinkedShaders[i]->ir);
2610 }
2611 }
2612 }
2613
2614 /**
2615 * Resize tessellation evaluation per-vertex inputs to the size of
2616 * tessellation control per-vertex outputs.
2617 */
2618 static void
2619 resize_tes_inputs(struct gl_context *ctx,
2620 struct gl_shader_program *prog)
2621 {
2622 if (prog->_LinkedShaders[MESA_SHADER_TESS_EVAL] == NULL)
2623 return;
2624
2625 gl_linked_shader *const tcs = prog->_LinkedShaders[MESA_SHADER_TESS_CTRL];
2626 gl_linked_shader *const tes = prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
2627
2628 /* If no control shader is present, then the TES inputs are statically
2629 * sized to MaxPatchVertices; the actual size of the arrays won't be
2630 * known until draw time.
2631 */
2632 const int num_vertices = tcs
2633 ? tcs->Program->info.tess.tcs_vertices_out
2634 : ctx->Const.MaxPatchVertices;
2635
2636 array_resize_visitor input_resize_visitor(num_vertices, prog,
2637 MESA_SHADER_TESS_EVAL);
2638 foreach_in_list(ir_instruction, ir, tes->ir) {
2639 ir->accept(&input_resize_visitor);
2640 }
2641
2642 if (tcs) {
2643 /* Convert the gl_PatchVerticesIn system value into a constant, since
2644 * the value is known at this point.
2645 */
2646 foreach_in_list(ir_instruction, ir, tes->ir) {
2647 ir_variable *var = ir->as_variable();
2648 if (var && var->data.mode == ir_var_system_value &&
2649 var->data.location == SYSTEM_VALUE_VERTICES_IN) {
2650 void *mem_ctx = ralloc_parent(var);
2651 var->data.location = 0;
2652 var->data.explicit_location = false;
2653 var->data.mode = ir_var_auto;
2654 var->constant_value = new(mem_ctx) ir_constant(num_vertices);
2655 }
2656 }
2657 }
2658 }
2659
2660 /**
2661 * Find a contiguous set of available bits in a bitmask.
2662 *
2663 * \param used_mask Bits representing used (1) and unused (0) locations
2664 * \param needed_count Number of contiguous bits needed.
2665 *
2666 * \return
2667 * Base location of the available bits on success or -1 on failure.
2668 */
2669 static int
2670 find_available_slots(unsigned used_mask, unsigned needed_count)
2671 {
2672 unsigned needed_mask = (1 << needed_count) - 1;
2673 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
2674
2675 /* The comparison to 32 is redundant, but without it GCC emits "warning:
2676 * cannot optimize possibly infinite loops" for the loop below.
2677 */
2678 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
2679 return -1;
2680
2681 for (int i = 0; i <= max_bit_to_test; i++) {
2682 if ((needed_mask & ~used_mask) == needed_mask)
2683 return i;
2684
2685 needed_mask <<= 1;
2686 }
2687
2688 return -1;
2689 }
2690
2691
2692 #define SAFE_MASK_FROM_INDEX(i) (((i) >= 32) ? ~0 : ((1 << (i)) - 1))
2693
2694 /**
2695 * Assign locations for either VS inputs or FS outputs
2696 *
2697 * \param mem_ctx Temporary ralloc context used for linking
2698 * \param prog Shader program whose variables need locations assigned
2699 * \param constants Driver specific constant values for the program.
2700 * \param target_index Selector for the program target to receive location
2701 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
2702 * \c MESA_SHADER_FRAGMENT.
2703 *
2704 * \return
2705 * If locations are successfully assigned, true is returned. Otherwise an
2706 * error is emitted to the shader link log and false is returned.
2707 */
2708 static bool
2709 assign_attribute_or_color_locations(void *mem_ctx,
2710 gl_shader_program *prog,
2711 struct gl_constants *constants,
2712 unsigned target_index)
2713 {
2714 /* Maximum number of generic locations. This corresponds to either the
2715 * maximum number of draw buffers or the maximum number of generic
2716 * attributes.
2717 */
2718 unsigned max_index = (target_index == MESA_SHADER_VERTEX) ?
2719 constants->Program[target_index].MaxAttribs :
2720 MAX2(constants->MaxDrawBuffers, constants->MaxDualSourceDrawBuffers);
2721
2722 /* Mark invalid locations as being used.
2723 */
2724 unsigned used_locations = ~SAFE_MASK_FROM_INDEX(max_index);
2725 unsigned double_storage_locations = 0;
2726
2727 assert((target_index == MESA_SHADER_VERTEX)
2728 || (target_index == MESA_SHADER_FRAGMENT));
2729
2730 gl_linked_shader *const sh = prog->_LinkedShaders[target_index];
2731 if (sh == NULL)
2732 return true;
2733
2734 /* Operate in a total of four passes.
2735 *
2736 * 1. Invalidate the location assignments for all vertex shader inputs.
2737 *
2738 * 2. Assign locations for inputs that have user-defined (via
2739 * glBindVertexAttribLocation) locations and outputs that have
2740 * user-defined locations (via glBindFragDataLocation).
2741 *
2742 * 3. Sort the attributes without assigned locations by number of slots
2743 * required in decreasing order. Fragmentation caused by attribute
2744 * locations assigned by the application may prevent large attributes
2745 * from having enough contiguous space.
2746 *
2747 * 4. Assign locations to any inputs without assigned locations.
2748 */
2749
2750 const int generic_base = (target_index == MESA_SHADER_VERTEX)
2751 ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
2752
2753 const enum ir_variable_mode direction =
2754 (target_index == MESA_SHADER_VERTEX)
2755 ? ir_var_shader_in : ir_var_shader_out;
2756
2757
2758 /* Temporary storage for the set of attributes that need locations assigned.
2759 */
2760 struct temp_attr {
2761 unsigned slots;
2762 ir_variable *var;
2763
2764 /* Used below in the call to qsort. */
2765 static int compare(const void *a, const void *b)
2766 {
2767 const temp_attr *const l = (const temp_attr *) a;
2768 const temp_attr *const r = (const temp_attr *) b;
2769
2770 /* Reversed because we want a descending order sort below. */
2771 return r->slots - l->slots;
2772 }
2773 } to_assign[32];
2774 assert(max_index <= 32);
2775
2776 /* Temporary array for the set of attributes that have locations assigned,
2777 * for the purpose of checking overlapping slots/components of (non-ES)
2778 * fragment shader outputs.
2779 */
2780 ir_variable *assigned[12 * 4]; /* (max # of FS outputs) * # components */
2781 unsigned assigned_attr = 0;
2782
2783 unsigned num_attr = 0;
2784
2785 foreach_in_list(ir_instruction, node, sh->ir) {
2786 ir_variable *const var = node->as_variable();
2787
2788 if ((var == NULL) || (var->data.mode != (unsigned) direction))
2789 continue;
2790
2791 if (var->data.explicit_location) {
2792 var->data.is_unmatched_generic_inout = 0;
2793 if ((var->data.location >= (int)(max_index + generic_base))
2794 || (var->data.location < 0)) {
2795 linker_error(prog,
2796 "invalid explicit location %d specified for `%s'\n",
2797 (var->data.location < 0)
2798 ? var->data.location
2799 : var->data.location - generic_base,
2800 var->name);
2801 return false;
2802 }
2803 } else if (target_index == MESA_SHADER_VERTEX) {
2804 unsigned binding;
2805
2806 if (prog->AttributeBindings->get(binding, var->name)) {
2807 assert(binding >= VERT_ATTRIB_GENERIC0);
2808 var->data.location = binding;
2809 var->data.is_unmatched_generic_inout = 0;
2810 }
2811 } else if (target_index == MESA_SHADER_FRAGMENT) {
2812 unsigned binding;
2813 unsigned index;
2814 const char *name = var->name;
2815 const glsl_type *type = var->type;
2816
2817 while (type) {
2818 /* Check if there's a binding for the variable name */
2819 if (prog->FragDataBindings->get(binding, name)) {
2820 assert(binding >= FRAG_RESULT_DATA0);
2821 var->data.location = binding;
2822 var->data.is_unmatched_generic_inout = 0;
2823
2824 if (prog->FragDataIndexBindings->get(index, name)) {
2825 var->data.index = index;
2826 }
2827 break;
2828 }
2829
2830 /* If not, but it's an array type, look for name[0] */
2831 if (type->is_array()) {
2832 name = ralloc_asprintf(mem_ctx, "%s[0]", name);
2833 type = type->fields.array;
2834 continue;
2835 }
2836
2837 break;
2838 }
2839 }
2840
2841 if (strcmp(var->name, "gl_LastFragData") == 0)
2842 continue;
2843
2844 /* From GL4.5 core spec, section 15.2 (Shader Execution):
2845 *
2846 * "Output binding assignments will cause LinkProgram to fail:
2847 * ...
2848 * If the program has an active output assigned to a location greater
2849 * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
2850 * an active output assigned an index greater than or equal to one;"
2851 */
2852 if (target_index == MESA_SHADER_FRAGMENT && var->data.index >= 1 &&
2853 var->data.location - generic_base >=
2854 (int) constants->MaxDualSourceDrawBuffers) {
2855 linker_error(prog,
2856 "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
2857 "with index %u for %s\n",
2858 var->data.location - generic_base, var->data.index,
2859 var->name);
2860 return false;
2861 }
2862
2863 const unsigned slots = var->type->count_attribute_slots(target_index == MESA_SHADER_VERTEX);
2864
2865 /* If the variable is not a built-in and has a location statically
2866 * assigned in the shader (presumably via a layout qualifier), make sure
2867 * that it doesn't collide with other assigned locations. Otherwise,
2868 * add it to the list of variables that need linker-assigned locations.
2869 */
2870 if (var->data.location != -1) {
2871 if (var->data.location >= generic_base && var->data.index < 1) {
2872 /* From page 61 of the OpenGL 4.0 spec:
2873 *
2874 * "LinkProgram will fail if the attribute bindings assigned
2875 * by BindAttribLocation do not leave not enough space to
2876 * assign a location for an active matrix attribute or an
2877 * active attribute array, both of which require multiple
2878 * contiguous generic attributes."
2879 *
2880 * I think above text prohibits the aliasing of explicit and
2881 * automatic assignments. But, aliasing is allowed in manual
2882 * assignments of attribute locations. See below comments for
2883 * the details.
2884 *
2885 * From OpenGL 4.0 spec, page 61:
2886 *
2887 * "It is possible for an application to bind more than one
2888 * attribute name to the same location. This is referred to as
2889 * aliasing. This will only work if only one of the aliased
2890 * attributes is active in the executable program, or if no
2891 * path through the shader consumes more than one attribute of
2892 * a set of attributes aliased to the same location. A link
2893 * error can occur if the linker determines that every path
2894 * through the shader consumes multiple aliased attributes,
2895 * but implementations are not required to generate an error
2896 * in this case."
2897 *
2898 * From GLSL 4.30 spec, page 54:
2899 *
2900 * "A program will fail to link if any two non-vertex shader
2901 * input variables are assigned to the same location. For
2902 * vertex shaders, multiple input variables may be assigned
2903 * to the same location using either layout qualifiers or via
2904 * the OpenGL API. However, such aliasing is intended only to
2905 * support vertex shaders where each execution path accesses
2906 * at most one input per each location. Implementations are
2907 * permitted, but not required, to generate link-time errors
2908 * if they detect that every path through the vertex shader
2909 * executable accesses multiple inputs assigned to any single
2910 * location. For all shader types, a program will fail to link
2911 * if explicit location assignments leave the linker unable
2912 * to find space for other variables without explicit
2913 * assignments."
2914 *
2915 * From OpenGL ES 3.0 spec, page 56:
2916 *
2917 * "Binding more than one attribute name to the same location
2918 * is referred to as aliasing, and is not permitted in OpenGL
2919 * ES Shading Language 3.00 vertex shaders. LinkProgram will
2920 * fail when this condition exists. However, aliasing is
2921 * possible in OpenGL ES Shading Language 1.00 vertex shaders.
2922 * This will only work if only one of the aliased attributes
2923 * is active in the executable program, or if no path through
2924 * the shader consumes more than one attribute of a set of
2925 * attributes aliased to the same location. A link error can
2926 * occur if the linker determines that every path through the
2927 * shader consumes multiple aliased attributes, but implemen-
2928 * tations are not required to generate an error in this case."
2929 *
2930 * After looking at above references from OpenGL, OpenGL ES and
2931 * GLSL specifications, we allow aliasing of vertex input variables
2932 * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
2933 *
2934 * NOTE: This is not required by the spec but its worth mentioning
2935 * here that we're not doing anything to make sure that no path
2936 * through the vertex shader executable accesses multiple inputs
2937 * assigned to any single location.
2938 */
2939
2940 /* Mask representing the contiguous slots that will be used by
2941 * this attribute.
2942 */
2943 const unsigned attr = var->data.location - generic_base;
2944 const unsigned use_mask = (1 << slots) - 1;
2945 const char *const string = (target_index == MESA_SHADER_VERTEX)
2946 ? "vertex shader input" : "fragment shader output";
2947
2948 /* Generate a link error if the requested locations for this
2949 * attribute exceed the maximum allowed attribute location.
2950 */
2951 if (attr + slots > max_index) {
2952 linker_error(prog,
2953 "insufficient contiguous locations "
2954 "available for %s `%s' %d %d %d\n", string,
2955 var->name, used_locations, use_mask, attr);
2956 return false;
2957 }
2958
2959 /* Generate a link error if the set of bits requested for this
2960 * attribute overlaps any previously allocated bits.
2961 */
2962 if ((~(use_mask << attr) & used_locations) != used_locations) {
2963 if (target_index == MESA_SHADER_FRAGMENT && !prog->IsES) {
2964 /* From section 4.4.2 (Output Layout Qualifiers) of the GLSL
2965 * 4.40 spec:
2966 *
2967 * "Additionally, for fragment shader outputs, if two
2968 * variables are placed within the same location, they
2969 * must have the same underlying type (floating-point or
2970 * integer). No component aliasing of output variables or
2971 * members is allowed.
2972 */
2973 for (unsigned i = 0; i < assigned_attr; i++) {
2974 unsigned assigned_slots =
2975 assigned[i]->type->count_attribute_slots(false);
2976 unsigned assig_attr =
2977 assigned[i]->data.location - generic_base;
2978 unsigned assigned_use_mask = (1 << assigned_slots) - 1;
2979
2980 if ((assigned_use_mask << assig_attr) &
2981 (use_mask << attr)) {
2982
2983 const glsl_type *assigned_type =
2984 assigned[i]->type->without_array();
2985 const glsl_type *type = var->type->without_array();
2986 if (assigned_type->base_type != type->base_type) {
2987 linker_error(prog, "types do not match for aliased"
2988 " %ss %s and %s\n", string,
2989 assigned[i]->name, var->name);
2990 return false;
2991 }
2992
2993 unsigned assigned_component_mask =
2994 ((1 << assigned_type->vector_elements) - 1) <<
2995 assigned[i]->data.location_frac;
2996 unsigned component_mask =
2997 ((1 << type->vector_elements) - 1) <<
2998 var->data.location_frac;
2999 if (assigned_component_mask & component_mask) {
3000 linker_error(prog, "overlapping component is "
3001 "assigned to %ss %s and %s "
3002 "(component=%d)\n",
3003 string, assigned[i]->name, var->name,
3004 var->data.location_frac);
3005 return false;
3006 }
3007 }
3008 }
3009 } else if (target_index == MESA_SHADER_FRAGMENT ||
3010 (prog->IsES && prog->data->Version >= 300)) {
3011 linker_error(prog, "overlapping location is assigned "
3012 "to %s `%s' %d %d %d\n", string, var->name,
3013 used_locations, use_mask, attr);
3014 return false;
3015 } else {
3016 linker_warning(prog, "overlapping location is assigned "
3017 "to %s `%s' %d %d %d\n", string, var->name,
3018 used_locations, use_mask, attr);
3019 }
3020 }
3021
3022 if (target_index == MESA_SHADER_FRAGMENT && !prog->IsES) {
3023 /* Only track assigned variables for non-ES fragment shaders
3024 * to avoid overflowing the array.
3025 *
3026 * At most one variable per fragment output component should
3027 * reach this.
3028 */
3029 assert(assigned_attr < ARRAY_SIZE(assigned));
3030 assigned[assigned_attr] = var;
3031 assigned_attr++;
3032 }
3033
3034 used_locations |= (use_mask << attr);
3035
3036 /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
3037 *
3038 * "A program with more than the value of MAX_VERTEX_ATTRIBS
3039 * active attribute variables may fail to link, unless
3040 * device-dependent optimizations are able to make the program
3041 * fit within available hardware resources. For the purposes
3042 * of this test, attribute variables of the type dvec3, dvec4,
3043 * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
3044 * count as consuming twice as many attributes as equivalent
3045 * single-precision types. While these types use the same number
3046 * of generic attributes as their single-precision equivalents,
3047 * implementations are permitted to consume two single-precision
3048 * vectors of internal storage for each three- or four-component
3049 * double-precision vector."
3050 *
3051 * Mark this attribute slot as taking up twice as much space
3052 * so we can count it properly against limits. According to
3053 * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
3054 * is optional behavior, but it seems preferable.
3055 */
3056 if (var->type->without_array()->is_dual_slot())
3057 double_storage_locations |= (use_mask << attr);
3058 }
3059
3060 continue;
3061 }
3062
3063 if (num_attr >= max_index) {
3064 linker_error(prog, "too many %s (max %u)",
3065 target_index == MESA_SHADER_VERTEX ?
3066 "vertex shader inputs" : "fragment shader outputs",
3067 max_index);
3068 return false;
3069 }
3070 to_assign[num_attr].slots = slots;
3071 to_assign[num_attr].var = var;
3072 num_attr++;
3073 }
3074
3075 if (target_index == MESA_SHADER_VERTEX) {
3076 unsigned total_attribs_size =
3077 util_bitcount(used_locations & SAFE_MASK_FROM_INDEX(max_index)) +
3078 util_bitcount(double_storage_locations);
3079 if (total_attribs_size > max_index) {
3080 linker_error(prog,
3081 "attempt to use %d vertex attribute slots only %d available ",
3082 total_attribs_size, max_index);
3083 return false;
3084 }
3085 }
3086
3087 /* If all of the attributes were assigned locations by the application (or
3088 * are built-in attributes with fixed locations), return early. This should
3089 * be the common case.
3090 */
3091 if (num_attr == 0)
3092 return true;
3093
3094 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
3095
3096 if (target_index == MESA_SHADER_VERTEX) {
3097 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
3098 * only be explicitly assigned by via glBindAttribLocation. Mark it as
3099 * reserved to prevent it from being automatically allocated below.
3100 */
3101 find_deref_visitor find("gl_Vertex");
3102 find.run(sh->ir);
3103 if (find.variable_found())
3104 used_locations |= (1 << 0);
3105 }
3106
3107 for (unsigned i = 0; i < num_attr; i++) {
3108 /* Mask representing the contiguous slots that will be used by this
3109 * attribute.
3110 */
3111 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
3112
3113 int location = find_available_slots(used_locations, to_assign[i].slots);
3114
3115 if (location < 0) {
3116 const char *const string = (target_index == MESA_SHADER_VERTEX)
3117 ? "vertex shader input" : "fragment shader output";
3118
3119 linker_error(prog,
3120 "insufficient contiguous locations "
3121 "available for %s `%s'\n",
3122 string, to_assign[i].var->name);
3123 return false;
3124 }
3125
3126 to_assign[i].var->data.location = generic_base + location;
3127 to_assign[i].var->data.is_unmatched_generic_inout = 0;
3128 used_locations |= (use_mask << location);
3129
3130 if (to_assign[i].var->type->without_array()->is_dual_slot())
3131 double_storage_locations |= (use_mask << location);
3132 }
3133
3134 /* Now that we have all the locations, from the GL 4.5 core spec, section
3135 * 11.1.1 (Vertex Attributes), dvec3, dvec4, dmat2x3, dmat2x4, dmat3,
3136 * dmat3x4, dmat4x3, and dmat4 count as consuming twice as many attributes
3137 * as equivalent single-precision types.
3138 */
3139 if (target_index == MESA_SHADER_VERTEX) {
3140 unsigned total_attribs_size =
3141 util_bitcount(used_locations & SAFE_MASK_FROM_INDEX(max_index)) +
3142 util_bitcount(double_storage_locations);
3143 if (total_attribs_size > max_index) {
3144 linker_error(prog,
3145 "attempt to use %d vertex attribute slots only %d available ",
3146 total_attribs_size, max_index);
3147 return false;
3148 }
3149 }
3150
3151 return true;
3152 }
3153
3154 /**
3155 * Match explicit locations of outputs to inputs and deactivate the
3156 * unmatch flag if found so we don't optimise them away.
3157 */
3158 static void
3159 match_explicit_outputs_to_inputs(gl_linked_shader *producer,
3160 gl_linked_shader *consumer)
3161 {
3162 glsl_symbol_table parameters;
3163 ir_variable *explicit_locations[MAX_VARYINGS_INCL_PATCH][4] =
3164 { {NULL, NULL} };
3165
3166 /* Find all shader outputs in the "producer" stage.
3167 */
3168 foreach_in_list(ir_instruction, node, producer->ir) {
3169 ir_variable *const var = node->as_variable();
3170
3171 if ((var == NULL) || (var->data.mode != ir_var_shader_out))
3172 continue;
3173
3174 if (var->data.explicit_location &&
3175 var->data.location >= VARYING_SLOT_VAR0) {
3176 const unsigned idx = var->data.location - VARYING_SLOT_VAR0;
3177 if (explicit_locations[idx][var->data.location_frac] == NULL)
3178 explicit_locations[idx][var->data.location_frac] = var;
3179 }
3180 }
3181
3182 /* Match inputs to outputs */
3183 foreach_in_list(ir_instruction, node, consumer->ir) {
3184 ir_variable *const input = node->as_variable();
3185
3186 if ((input == NULL) || (input->data.mode != ir_var_shader_in))
3187 continue;
3188
3189 ir_variable *output = NULL;
3190 if (input->data.explicit_location
3191 && input->data.location >= VARYING_SLOT_VAR0) {
3192 output = explicit_locations[input->data.location - VARYING_SLOT_VAR0]
3193 [input->data.location_frac];
3194
3195 if (output != NULL){
3196 input->data.is_unmatched_generic_inout = 0;
3197 output->data.is_unmatched_generic_inout = 0;
3198 }
3199 }
3200 }
3201 }
3202
3203 /**
3204 * Store the gl_FragDepth layout in the gl_shader_program struct.
3205 */
3206 static void
3207 store_fragdepth_layout(struct gl_shader_program *prog)
3208 {
3209 if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
3210 return;
3211 }
3212
3213 struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
3214
3215 /* We don't look up the gl_FragDepth symbol directly because if
3216 * gl_FragDepth is not used in the shader, it's removed from the IR.
3217 * However, the symbol won't be removed from the symbol table.
3218 *
3219 * We're only interested in the cases where the variable is NOT removed
3220 * from the IR.
3221 */
3222 foreach_in_list(ir_instruction, node, ir) {
3223 ir_variable *const var = node->as_variable();
3224
3225 if (var == NULL || var->data.mode != ir_var_shader_out) {
3226 continue;
3227 }
3228
3229 if (strcmp(var->name, "gl_FragDepth") == 0) {
3230 switch (var->data.depth_layout) {
3231 case ir_depth_layout_none:
3232 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
3233 return;
3234 case ir_depth_layout_any:
3235 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
3236 return;
3237 case ir_depth_layout_greater:
3238 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
3239 return;
3240 case ir_depth_layout_less:
3241 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
3242 return;
3243 case ir_depth_layout_unchanged:
3244 prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
3245 return;
3246 default:
3247 assert(0);
3248 return;
3249 }
3250 }
3251 }
3252 }
3253
3254 /**
3255 * Validate the resources used by a program versus the implementation limits
3256 */
3257 static void
3258 check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
3259 {
3260 unsigned total_uniform_blocks = 0;
3261 unsigned total_shader_storage_blocks = 0;
3262
3263 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3264 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
3265
3266 if (sh == NULL)
3267 continue;
3268
3269 if (sh->Program->info.num_textures >
3270 ctx->Const.Program[i].MaxTextureImageUnits) {
3271 linker_error(prog, "Too many %s shader texture samplers\n",
3272 _mesa_shader_stage_to_string(i));
3273 }
3274
3275 if (sh->num_uniform_components >
3276 ctx->Const.Program[i].MaxUniformComponents) {
3277 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
3278 linker_warning(prog, "Too many %s shader default uniform block "
3279 "components, but the driver will try to optimize "
3280 "them out; this is non-portable out-of-spec "
3281 "behavior\n",
3282 _mesa_shader_stage_to_string(i));
3283 } else {
3284 linker_error(prog, "Too many %s shader default uniform block "
3285 "components\n",
3286 _mesa_shader_stage_to_string(i));
3287 }
3288 }
3289
3290 if (sh->num_combined_uniform_components >
3291 ctx->Const.Program[i].MaxCombinedUniformComponents) {
3292 if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
3293 linker_warning(prog, "Too many %s shader uniform components, "
3294 "but the driver will try to optimize them out; "
3295 "this is non-portable out-of-spec behavior\n",
3296 _mesa_shader_stage_to_string(i));
3297 } else {
3298 linker_error(prog, "Too many %s shader uniform components\n",
3299 _mesa_shader_stage_to_string(i));
3300 }
3301 }
3302
3303 total_shader_storage_blocks += sh->Program->info.num_ssbos;
3304 total_uniform_blocks += sh->Program->info.num_ubos;
3305
3306 const unsigned max_uniform_blocks =
3307 ctx->Const.Program[i].MaxUniformBlocks;
3308 if (max_uniform_blocks < sh->Program->info.num_ubos) {
3309 linker_error(prog, "Too many %s uniform blocks (%d/%d)\n",
3310 _mesa_shader_stage_to_string(i),
3311 sh->Program->info.num_ubos, max_uniform_blocks);
3312 }
3313
3314 const unsigned max_shader_storage_blocks =
3315 ctx->Const.Program[i].MaxShaderStorageBlocks;
3316 if (max_shader_storage_blocks < sh->Program->info.num_ssbos) {
3317 linker_error(prog, "Too many %s shader storage blocks (%d/%d)\n",
3318 _mesa_shader_stage_to_string(i),
3319 sh->Program->info.num_ssbos, max_shader_storage_blocks);
3320 }
3321 }
3322
3323 if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
3324 linker_error(prog, "Too many combined uniform blocks (%d/%d)\n",
3325 total_uniform_blocks, ctx->Const.MaxCombinedUniformBlocks);
3326 }
3327
3328 if (total_shader_storage_blocks > ctx->Const.MaxCombinedShaderStorageBlocks) {
3329 linker_error(prog, "Too many combined shader storage blocks (%d/%d)\n",
3330 total_shader_storage_blocks,
3331 ctx->Const.MaxCombinedShaderStorageBlocks);
3332 }
3333
3334 for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
3335 if (prog->data->UniformBlocks[i].UniformBufferSize >
3336 ctx->Const.MaxUniformBlockSize) {
3337 linker_error(prog, "Uniform block %s too big (%d/%d)\n",
3338 prog->data->UniformBlocks[i].Name,
3339 prog->data->UniformBlocks[i].UniformBufferSize,
3340 ctx->Const.MaxUniformBlockSize);
3341 }
3342 }
3343
3344 for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
3345 if (prog->data->ShaderStorageBlocks[i].UniformBufferSize >
3346 ctx->Const.MaxShaderStorageBlockSize) {
3347 linker_error(prog, "Shader storage block %s too big (%d/%d)\n",
3348 prog->data->ShaderStorageBlocks[i].Name,
3349 prog->data->ShaderStorageBlocks[i].UniformBufferSize,
3350 ctx->Const.MaxShaderStorageBlockSize);
3351 }
3352 }
3353 }
3354
3355 static void
3356 link_calculate_subroutine_compat(struct gl_shader_program *prog)
3357 {
3358 unsigned mask = prog->data->linked_stages;
3359 while (mask) {
3360 const int i = u_bit_scan(&mask);
3361 struct gl_program *p = prog->_LinkedShaders[i]->Program;
3362
3363 for (unsigned j = 0; j < p->sh.NumSubroutineUniformRemapTable; j++) {
3364 if (p->sh.SubroutineUniformRemapTable[j] == INACTIVE_UNIFORM_EXPLICIT_LOCATION)
3365 continue;
3366
3367 struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[j];
3368
3369 if (!uni)
3370 continue;
3371
3372 int count = 0;
3373 if (p->sh.NumSubroutineFunctions == 0) {
3374 linker_error(prog, "subroutine uniform %s defined but no valid functions found\n", uni->type->name);
3375 continue;
3376 }
3377 for (unsigned f = 0; f < p->sh.NumSubroutineFunctions; f++) {
3378 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[f];
3379 for (int k = 0; k < fn->num_compat_types; k++) {
3380 if (fn->types[k] == uni->type) {
3381 count++;
3382 break;
3383 }
3384 }
3385 }
3386 uni->num_compatible_subroutines = count;
3387 }
3388 }
3389 }
3390
3391 static void
3392 check_subroutine_resources(struct gl_shader_program *prog)
3393 {
3394 unsigned mask = prog->data->linked_stages;
3395 while (mask) {
3396 const int i = u_bit_scan(&mask);
3397 struct gl_program *p = prog->_LinkedShaders[i]->Program;
3398
3399 if (p->sh.NumSubroutineUniformRemapTable > MAX_SUBROUTINE_UNIFORM_LOCATIONS) {
3400 linker_error(prog, "Too many %s shader subroutine uniforms\n",
3401 _mesa_shader_stage_to_string(i));
3402 }
3403 }
3404 }
3405 /**
3406 * Validate shader image resources.
3407 */
3408 static void
3409 check_image_resources(struct gl_context *ctx, struct gl_shader_program *prog)
3410 {
3411 unsigned total_image_units = 0;
3412 unsigned fragment_outputs = 0;
3413 unsigned total_shader_storage_blocks = 0;
3414
3415 if (!ctx->Extensions.ARB_shader_image_load_store)
3416 return;
3417
3418 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3419 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
3420
3421 if (sh) {
3422 if (sh->Program->info.num_images > ctx->Const.Program[i].MaxImageUniforms)
3423 linker_error(prog, "Too many %s shader image uniforms (%u > %u)\n",
3424 _mesa_shader_stage_to_string(i),
3425 sh->Program->info.num_images,
3426 ctx->Const.Program[i].MaxImageUniforms);
3427
3428 total_image_units += sh->Program->info.num_images;
3429 total_shader_storage_blocks += sh->Program->info.num_ssbos;
3430
3431 if (i == MESA_SHADER_FRAGMENT) {
3432 foreach_in_list(ir_instruction, node, sh->ir) {
3433 ir_variable *var = node->as_variable();
3434 if (var && var->data.mode == ir_var_shader_out)
3435 /* since there are no double fs outputs - pass false */
3436 fragment_outputs += var->type->count_attribute_slots(false);
3437 }
3438 }
3439 }
3440 }
3441
3442 if (total_image_units > ctx->Const.MaxCombinedImageUniforms)
3443 linker_error(prog, "Too many combined image uniforms\n");
3444
3445 if (total_image_units + fragment_outputs + total_shader_storage_blocks >
3446 ctx->Const.MaxCombinedShaderOutputResources)
3447 linker_error(prog, "Too many combined image uniforms, shader storage "
3448 " buffers and fragment outputs\n");
3449 }
3450
3451
3452 /**
3453 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
3454 * for a variable, checks for overlaps between other uniforms using explicit
3455 * locations.
3456 */
3457 static int
3458 reserve_explicit_locations(struct gl_shader_program *prog,
3459 string_to_uint_map *map, ir_variable *var)
3460 {
3461 unsigned slots = var->type->uniform_locations();
3462 unsigned max_loc = var->data.location + slots - 1;
3463 unsigned return_value = slots;
3464
3465 /* Resize remap table if locations do not fit in the current one. */
3466 if (max_loc + 1 > prog->NumUniformRemapTable) {
3467 prog->UniformRemapTable =
3468 reralloc(prog, prog->UniformRemapTable,
3469 gl_uniform_storage *,
3470 max_loc + 1);
3471
3472 if (!prog->UniformRemapTable) {
3473 linker_error(prog, "Out of memory during linking.\n");
3474 return -1;
3475 }
3476
3477 /* Initialize allocated space. */
3478 for (unsigned i = prog->NumUniformRemapTable; i < max_loc + 1; i++)
3479 prog->UniformRemapTable[i] = NULL;
3480
3481 prog->NumUniformRemapTable = max_loc + 1;
3482 }
3483
3484 for (unsigned i = 0; i < slots; i++) {
3485 unsigned loc = var->data.location + i;
3486
3487 /* Check if location is already used. */
3488 if (prog->UniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
3489
3490 /* Possibly same uniform from a different stage, this is ok. */
3491 unsigned hash_loc;
3492 if (map->get(hash_loc, var->name) && hash_loc == loc - i) {
3493 return_value = 0;
3494 continue;
3495 }
3496
3497 /* ARB_explicit_uniform_location specification states:
3498 *
3499 * "No two default-block uniform variables in the program can have
3500 * the same location, even if they are unused, otherwise a compiler
3501 * or linker error will be generated."
3502 */
3503 linker_error(prog,
3504 "location qualifier for uniform %s overlaps "
3505 "previously used location\n",
3506 var->name);
3507 return -1;
3508 }
3509
3510 /* Initialize location as inactive before optimization
3511 * rounds and location assignment.
3512 */
3513 prog->UniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
3514 }
3515
3516 /* Note, base location used for arrays. */
3517 map->put(var->data.location, var->name);
3518
3519 return return_value;
3520 }
3521
3522 static bool
3523 reserve_subroutine_explicit_locations(struct gl_shader_program *prog,
3524 struct gl_program *p,
3525 ir_variable *var)
3526 {
3527 unsigned slots = var->type->uniform_locations();
3528 unsigned max_loc = var->data.location + slots - 1;
3529
3530 /* Resize remap table if locations do not fit in the current one. */
3531 if (max_loc + 1 > p->sh.NumSubroutineUniformRemapTable) {
3532 p->sh.SubroutineUniformRemapTable =
3533 reralloc(p, p->sh.SubroutineUniformRemapTable,
3534 gl_uniform_storage *,
3535 max_loc + 1);
3536
3537 if (!p->sh.SubroutineUniformRemapTable) {
3538 linker_error(prog, "Out of memory during linking.\n");
3539 return false;
3540 }
3541
3542 /* Initialize allocated space. */
3543 for (unsigned i = p->sh.NumSubroutineUniformRemapTable; i < max_loc + 1; i++)
3544 p->sh.SubroutineUniformRemapTable[i] = NULL;
3545
3546 p->sh.NumSubroutineUniformRemapTable = max_loc + 1;
3547 }
3548
3549 for (unsigned i = 0; i < slots; i++) {
3550 unsigned loc = var->data.location + i;
3551
3552 /* Check if location is already used. */
3553 if (p->sh.SubroutineUniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
3554
3555 /* ARB_explicit_uniform_location specification states:
3556 * "No two subroutine uniform variables can have the same location
3557 * in the same shader stage, otherwise a compiler or linker error
3558 * will be generated."
3559 */
3560 linker_error(prog,
3561 "location qualifier for uniform %s overlaps "
3562 "previously used location\n",
3563 var->name);
3564 return false;
3565 }
3566
3567 /* Initialize location as inactive before optimization
3568 * rounds and location assignment.
3569 */
3570 p->sh.SubroutineUniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
3571 }
3572
3573 return true;
3574 }
3575 /**
3576 * Check and reserve all explicit uniform locations, called before
3577 * any optimizations happen to handle also inactive uniforms and
3578 * inactive array elements that may get trimmed away.
3579 */
3580 static void
3581 check_explicit_uniform_locations(struct gl_context *ctx,
3582 struct gl_shader_program *prog)
3583 {
3584 prog->NumExplicitUniformLocations = 0;
3585
3586 if (!ctx->Extensions.ARB_explicit_uniform_location)
3587 return;
3588
3589 /* This map is used to detect if overlapping explicit locations
3590 * occur with the same uniform (from different stage) or a different one.
3591 */
3592 string_to_uint_map *uniform_map = new string_to_uint_map;
3593
3594 if (!uniform_map) {
3595 linker_error(prog, "Out of memory during linking.\n");
3596 return;
3597 }
3598
3599 unsigned entries_total = 0;
3600 unsigned mask = prog->data->linked_stages;
3601 while (mask) {
3602 const int i = u_bit_scan(&mask);
3603 struct gl_program *p = prog->_LinkedShaders[i]->Program;
3604
3605 foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) {
3606 ir_variable *var = node->as_variable();
3607 if (!var || var->data.mode != ir_var_uniform)
3608 continue;
3609
3610 if (var->data.explicit_location) {
3611 bool ret = false;
3612 if (var->type->without_array()->is_subroutine())
3613 ret = reserve_subroutine_explicit_locations(prog, p, var);
3614 else {
3615 int slots = reserve_explicit_locations(prog, uniform_map,
3616 var);
3617 if (slots != -1) {
3618 ret = true;
3619 entries_total += slots;
3620 }
3621 }
3622 if (!ret) {
3623 delete uniform_map;
3624 return;
3625 }
3626 }
3627 }
3628 }
3629
3630 link_util_update_empty_uniform_locations(prog);
3631
3632 delete uniform_map;
3633 prog->NumExplicitUniformLocations = entries_total;
3634 }
3635
3636 static bool
3637 should_add_buffer_variable(struct gl_shader_program *shProg,
3638 GLenum type, const char *name)
3639 {
3640 bool found_interface = false;
3641 unsigned block_name_len = 0;
3642 const char *block_name_dot = strchr(name, '.');
3643
3644 /* These rules only apply to buffer variables. So we return
3645 * true for the rest of types.
3646 */
3647 if (type != GL_BUFFER_VARIABLE)
3648 return true;
3649
3650 for (unsigned i = 0; i < shProg->data->NumShaderStorageBlocks; i++) {
3651 const char *block_name = shProg->data->ShaderStorageBlocks[i].Name;
3652 block_name_len = strlen(block_name);
3653
3654 const char *block_square_bracket = strchr(block_name, '[');
3655 if (block_square_bracket) {
3656 /* The block is part of an array of named interfaces,
3657 * for the name comparison we ignore the "[x]" part.
3658 */
3659 block_name_len -= strlen(block_square_bracket);
3660 }
3661
3662 if (block_name_dot) {
3663 /* Check if the variable name starts with the interface
3664 * name. The interface name (if present) should have the
3665 * length than the interface block name we are comparing to.
3666 */
3667 unsigned len = strlen(name) - strlen(block_name_dot);
3668 if (len != block_name_len)
3669 continue;
3670 }
3671
3672 if (strncmp(block_name, name, block_name_len) == 0) {
3673 found_interface = true;
3674 break;
3675 }
3676 }
3677
3678 /* We remove the interface name from the buffer variable name,
3679 * including the dot that follows it.
3680 */
3681 if (found_interface)
3682 name = name + block_name_len + 1;
3683
3684 /* The ARB_program_interface_query spec says:
3685 *
3686 * "For an active shader storage block member declared as an array, an
3687 * entry will be generated only for the first array element, regardless
3688 * of its type. For arrays of aggregate types, the enumeration rules
3689 * are applied recursively for the single enumerated array element."
3690 */
3691 const char *struct_first_dot = strchr(name, '.');
3692 const char *first_square_bracket = strchr(name, '[');
3693
3694 /* The buffer variable is on top level and it is not an array */
3695 if (!first_square_bracket) {
3696 return true;
3697 /* The shader storage block member is a struct, then generate the entry */
3698 } else if (struct_first_dot && struct_first_dot < first_square_bracket) {
3699 return true;
3700 } else {
3701 /* Shader storage block member is an array, only generate an entry for the
3702 * first array element.
3703 */
3704 if (strncmp(first_square_bracket, "[0]", 3) == 0)
3705 return true;
3706 }
3707
3708 return false;
3709 }
3710
3711 /* Function checks if a variable var is a packed varying and
3712 * if given name is part of packed varying's list.
3713 *
3714 * If a variable is a packed varying, it has a name like
3715 * 'packed:a,b,c' where a, b and c are separate variables.
3716 */
3717 static bool
3718 included_in_packed_varying(ir_variable *var, const char *name)
3719 {
3720 if (strncmp(var->name, "packed:", 7) != 0)
3721 return false;
3722
3723 char *list = strdup(var->name + 7);
3724 assert(list);
3725
3726 bool found = false;
3727 char *saveptr;
3728 char *token = strtok_r(list, ",", &saveptr);
3729 while (token) {
3730 if (strcmp(token, name) == 0) {
3731 found = true;
3732 break;
3733 }
3734 token = strtok_r(NULL, ",", &saveptr);
3735 }
3736 free(list);
3737 return found;
3738 }
3739
3740 /**
3741 * Function builds a stage reference bitmask from variable name.
3742 */
3743 static uint8_t
3744 build_stageref(struct gl_shader_program *shProg, const char *name,
3745 unsigned mode)
3746 {
3747 uint8_t stages = 0;
3748
3749 /* Note, that we assume MAX 8 stages, if there will be more stages, type
3750 * used for reference mask in gl_program_resource will need to be changed.
3751 */
3752 assert(MESA_SHADER_STAGES < 8);
3753
3754 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3755 struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
3756 if (!sh)
3757 continue;
3758
3759 /* Shader symbol table may contain variables that have
3760 * been optimized away. Search IR for the variable instead.
3761 */
3762 foreach_in_list(ir_instruction, node, sh->ir) {
3763 ir_variable *var = node->as_variable();
3764 if (var) {
3765 unsigned baselen = strlen(var->name);
3766
3767 if (included_in_packed_varying(var, name)) {
3768 stages |= (1 << i);
3769 break;
3770 }
3771
3772 /* Type needs to match if specified, otherwise we might
3773 * pick a variable with same name but different interface.
3774 */
3775 if (var->data.mode != mode)
3776 continue;
3777
3778 if (strncmp(var->name, name, baselen) == 0) {
3779 /* Check for exact name matches but also check for arrays and
3780 * structs.
3781 */
3782 if (name[baselen] == '\0' ||
3783 name[baselen] == '[' ||
3784 name[baselen] == '.') {
3785 stages |= (1 << i);
3786 break;
3787 }
3788 }
3789 }
3790 }
3791 }
3792 return stages;
3793 }
3794
3795 /**
3796 * Create gl_shader_variable from ir_variable class.
3797 */
3798 static gl_shader_variable *
3799 create_shader_variable(struct gl_shader_program *shProg,
3800 const ir_variable *in,
3801 const char *name, const glsl_type *type,
3802 const glsl_type *interface_type,
3803 bool use_implicit_location, int location,
3804 const glsl_type *outermost_struct_type)
3805 {
3806 /* Allocate zero-initialized memory to ensure that bitfield padding
3807 * is zero.
3808 */
3809 gl_shader_variable *out = rzalloc(shProg, struct gl_shader_variable);
3810 if (!out)
3811 return NULL;
3812
3813 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
3814 * expect to see gl_VertexID in the program resource list. Pretend.
3815 */
3816 if (in->data.mode == ir_var_system_value &&
3817 in->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
3818 out->name = ralloc_strdup(shProg, "gl_VertexID");
3819 } else if ((in->data.mode == ir_var_shader_out &&
3820 in->data.location == VARYING_SLOT_TESS_LEVEL_OUTER) ||
3821 (in->data.mode == ir_var_system_value &&
3822 in->data.location == SYSTEM_VALUE_TESS_LEVEL_OUTER)) {
3823 out->name = ralloc_strdup(shProg, "gl_TessLevelOuter");
3824 type = glsl_type::get_array_instance(glsl_type::float_type, 4);
3825 } else if ((in->data.mode == ir_var_shader_out &&
3826 in->data.location == VARYING_SLOT_TESS_LEVEL_INNER) ||
3827 (in->data.mode == ir_var_system_value &&
3828 in->data.location == SYSTEM_VALUE_TESS_LEVEL_INNER)) {
3829 out->name = ralloc_strdup(shProg, "gl_TessLevelInner");
3830 type = glsl_type::get_array_instance(glsl_type::float_type, 2);
3831 } else {
3832 out->name = ralloc_strdup(shProg, name);
3833 }
3834
3835 if (!out->name)
3836 return NULL;
3837
3838 /* The ARB_program_interface_query spec says:
3839 *
3840 * "Not all active variables are assigned valid locations; the
3841 * following variables will have an effective location of -1:
3842 *
3843 * * uniforms declared as atomic counters;
3844 *
3845 * * members of a uniform block;
3846 *
3847 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
3848 *
3849 * * inputs or outputs not declared with a "location" layout
3850 * qualifier, except for vertex shader inputs and fragment shader
3851 * outputs."
3852 */
3853 if (in->type->is_atomic_uint() || is_gl_identifier(in->name) ||
3854 !(in->data.explicit_location || use_implicit_location)) {
3855 out->location = -1;
3856 } else {
3857 out->location = location;
3858 }
3859
3860 out->type = type;
3861 out->outermost_struct_type = outermost_struct_type;
3862 out->interface_type = interface_type;
3863 out->component = in->data.location_frac;
3864 out->index = in->data.index;
3865 out->patch = in->data.patch;
3866 out->mode = in->data.mode;
3867 out->interpolation = in->data.interpolation;
3868 out->explicit_location = in->data.explicit_location;
3869 out->precision = in->data.precision;
3870
3871 return out;
3872 }
3873
3874 static bool
3875 add_shader_variable(const struct gl_context *ctx,
3876 struct gl_shader_program *shProg,
3877 struct set *resource_set,
3878 unsigned stage_mask,
3879 GLenum programInterface, ir_variable *var,
3880 const char *name, const glsl_type *type,
3881 bool use_implicit_location, int location,
3882 bool inouts_share_location,
3883 const glsl_type *outermost_struct_type = NULL)
3884 {
3885 const glsl_type *interface_type = var->get_interface_type();
3886
3887 if (outermost_struct_type == NULL) {
3888 if (var->data.from_named_ifc_block) {
3889 const char *interface_name = interface_type->name;
3890
3891 if (interface_type->is_array()) {
3892 /* Issue #16 of the ARB_program_interface_query spec says:
3893 *
3894 * "* If a variable is a member of an interface block without an
3895 * instance name, it is enumerated using just the variable name.
3896 *
3897 * * If a variable is a member of an interface block with an
3898 * instance name, it is enumerated as "BlockName.Member", where
3899 * "BlockName" is the name of the interface block (not the
3900 * instance name) and "Member" is the name of the variable."
3901 *
3902 * In particular, it indicates that it should be "BlockName",
3903 * not "BlockName[array length]". The conformance suite and
3904 * dEQP both require this behavior.
3905 *
3906 * Here, we unwrap the extra array level added by named interface
3907 * block array lowering so we have the correct variable type. We
3908 * also unwrap the interface type when constructing the name.
3909 *
3910 * We leave interface_type the same so that ES 3.x SSO pipeline
3911 * validation can enforce the rules requiring array length to
3912 * match on interface blocks.
3913 */
3914 type = type->fields.array;
3915
3916 interface_name = interface_type->fields.array->name;
3917 }
3918
3919 name = ralloc_asprintf(shProg, "%s.%s", interface_name, name);
3920 }
3921 }
3922
3923 switch (type->base_type) {
3924 case GLSL_TYPE_STRUCT: {
3925 /* The ARB_program_interface_query spec says:
3926 *
3927 * "For an active variable declared as a structure, a separate entry
3928 * will be generated for each active structure member. The name of
3929 * each entry is formed by concatenating the name of the structure,
3930 * the "." character, and the name of the structure member. If a
3931 * structure member to enumerate is itself a structure or array,
3932 * these enumeration rules are applied recursively."
3933 */
3934 if (outermost_struct_type == NULL)
3935 outermost_struct_type = type;
3936
3937 unsigned field_location = location;
3938 for (unsigned i = 0; i < type->length; i++) {
3939 const struct glsl_struct_field *field = &type->fields.structure[i];
3940 char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name);
3941 if (!add_shader_variable(ctx, shProg, resource_set,
3942 stage_mask, programInterface,
3943 var, field_name, field->type,
3944 use_implicit_location, field_location,
3945 false, outermost_struct_type))
3946 return false;
3947
3948 field_location += field->type->count_attribute_slots(false);
3949 }
3950 return true;
3951 }
3952
3953 case GLSL_TYPE_ARRAY: {
3954 /* The ARB_program_interface_query spec says:
3955 *
3956 * "For an active variable declared as an array of basic types, a
3957 * single entry will be generated, with its name string formed by
3958 * concatenating the name of the array and the string "[0]"."
3959 *
3960 * "For an active variable declared as an array of an aggregate data
3961 * type (structures or arrays), a separate entry will be generated
3962 * for each active array element, unless noted immediately below.
3963 * The name of each entry is formed by concatenating the name of
3964 * the array, the "[" character, an integer identifying the element
3965 * number, and the "]" character. These enumeration rules are
3966 * applied recursively, treating each enumerated array element as a
3967 * separate active variable."
3968 */
3969 const struct glsl_type *array_type = type->fields.array;
3970 if (array_type->base_type == GLSL_TYPE_STRUCT ||
3971 array_type->base_type == GLSL_TYPE_ARRAY) {
3972 unsigned elem_location = location;
3973 unsigned stride = inouts_share_location ? 0 :
3974 array_type->count_attribute_slots(false);
3975 for (unsigned i = 0; i < type->length; i++) {
3976 char *elem = ralloc_asprintf(shProg, "%s[%d]", name, i);
3977 if (!add_shader_variable(ctx, shProg, resource_set,
3978 stage_mask, programInterface,
3979 var, elem, array_type,
3980 use_implicit_location, elem_location,
3981 false, outermost_struct_type))
3982 return false;
3983 elem_location += stride;
3984 }
3985 return true;
3986 }
3987 /* fallthrough */
3988 }
3989
3990 default: {
3991 /* The ARB_program_interface_query spec says:
3992 *
3993 * "For an active variable declared as a single instance of a basic
3994 * type, a single entry will be generated, using the variable name
3995 * from the shader source."
3996 */
3997 gl_shader_variable *sha_v =
3998 create_shader_variable(shProg, var, name, type, interface_type,
3999 use_implicit_location, location,
4000 outermost_struct_type);
4001 if (!sha_v)
4002 return false;
4003
4004 return link_util_add_program_resource(shProg, resource_set,
4005 programInterface, sha_v, stage_mask);
4006 }
4007 }
4008 }
4009
4010 static bool
4011 inout_has_same_location(const ir_variable *var, unsigned stage)
4012 {
4013 if (!var->data.patch &&
4014 ((var->data.mode == ir_var_shader_out &&
4015 stage == MESA_SHADER_TESS_CTRL) ||
4016 (var->data.mode == ir_var_shader_in &&
4017 (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL ||
4018 stage == MESA_SHADER_GEOMETRY))))
4019 return true;
4020 else
4021 return false;
4022 }
4023
4024 static bool
4025 add_interface_variables(const struct gl_context *ctx,
4026 struct gl_shader_program *shProg,
4027 struct set *resource_set,
4028 unsigned stage, GLenum programInterface)
4029 {
4030 exec_list *ir = shProg->_LinkedShaders[stage]->ir;
4031
4032 foreach_in_list(ir_instruction, node, ir) {
4033 ir_variable *var = node->as_variable();
4034
4035 if (!var || var->data.how_declared == ir_var_hidden)
4036 continue;
4037
4038 int loc_bias;
4039
4040 switch (var->data.mode) {
4041 case ir_var_system_value:
4042 case ir_var_shader_in:
4043 if (programInterface != GL_PROGRAM_INPUT)
4044 continue;
4045 loc_bias = (stage == MESA_SHADER_VERTEX) ? int(VERT_ATTRIB_GENERIC0)
4046 : int(VARYING_SLOT_VAR0);
4047 break;
4048 case ir_var_shader_out:
4049 if (programInterface != GL_PROGRAM_OUTPUT)
4050 continue;
4051 loc_bias = (stage == MESA_SHADER_FRAGMENT) ? int(FRAG_RESULT_DATA0)
4052 : int(VARYING_SLOT_VAR0);
4053 break;
4054 default:
4055 continue;
4056 };
4057
4058 if (var->data.patch)
4059 loc_bias = int(VARYING_SLOT_PATCH0);
4060
4061 /* Skip packed varyings, packed varyings are handled separately
4062 * by add_packed_varyings.
4063 */
4064 if (strncmp(var->name, "packed:", 7) == 0)
4065 continue;
4066
4067 /* Skip fragdata arrays, these are handled separately
4068 * by add_fragdata_arrays.
4069 */
4070 if (strncmp(var->name, "gl_out_FragData", 15) == 0)
4071 continue;
4072
4073 const bool vs_input_or_fs_output =
4074 (stage == MESA_SHADER_VERTEX && var->data.mode == ir_var_shader_in) ||
4075 (stage == MESA_SHADER_FRAGMENT && var->data.mode == ir_var_shader_out);
4076
4077 if (!add_shader_variable(ctx, shProg, resource_set,
4078 1 << stage, programInterface,
4079 var, var->name, var->type, vs_input_or_fs_output,
4080 var->data.location - loc_bias,
4081 inout_has_same_location(var, stage)))
4082 return false;
4083 }
4084 return true;
4085 }
4086
4087 static bool
4088 add_packed_varyings(const struct gl_context *ctx,
4089 struct gl_shader_program *shProg,
4090 struct set *resource_set,
4091 int stage, GLenum type)
4092 {
4093 struct gl_linked_shader *sh = shProg->_LinkedShaders[stage];
4094 GLenum iface;
4095
4096 if (!sh || !sh->packed_varyings)
4097 return true;
4098
4099 foreach_in_list(ir_instruction, node, sh->packed_varyings) {
4100 ir_variable *var = node->as_variable();
4101 if (var) {
4102 switch (var->data.mode) {
4103 case ir_var_shader_in:
4104 iface = GL_PROGRAM_INPUT;
4105 break;
4106 case ir_var_shader_out:
4107 iface = GL_PROGRAM_OUTPUT;
4108 break;
4109 default:
4110 unreachable("unexpected type");
4111 }
4112
4113 if (type == iface) {
4114 const int stage_mask =
4115 build_stageref(shProg, var->name, var->data.mode);
4116 if (!add_shader_variable(ctx, shProg, resource_set,
4117 stage_mask,
4118 iface, var, var->name, var->type, false,
4119 var->data.location - VARYING_SLOT_VAR0,
4120 inout_has_same_location(var, stage)))
4121 return false;
4122 }
4123 }
4124 }
4125 return true;
4126 }
4127
4128 static bool
4129 add_fragdata_arrays(const struct gl_context *ctx,
4130 struct gl_shader_program *shProg,
4131 struct set *resource_set)
4132 {
4133 struct gl_linked_shader *sh = shProg->_LinkedShaders[MESA_SHADER_FRAGMENT];
4134
4135 if (!sh || !sh->fragdata_arrays)
4136 return true;
4137
4138 foreach_in_list(ir_instruction, node, sh->fragdata_arrays) {
4139 ir_variable *var = node->as_variable();
4140 if (var) {
4141 assert(var->data.mode == ir_var_shader_out);
4142
4143 if (!add_shader_variable(ctx, shProg, resource_set,
4144 1 << MESA_SHADER_FRAGMENT,
4145 GL_PROGRAM_OUTPUT, var, var->name, var->type,
4146 true, var->data.location - FRAG_RESULT_DATA0,
4147 false))
4148 return false;
4149 }
4150 }
4151 return true;
4152 }
4153
4154 static char*
4155 get_top_level_name(const char *name)
4156 {
4157 const char *first_dot = strchr(name, '.');
4158 const char *first_square_bracket = strchr(name, '[');
4159 int name_size = 0;
4160
4161 /* The ARB_program_interface_query spec says:
4162 *
4163 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying
4164 * the number of active array elements of the top-level shader storage
4165 * block member containing to the active variable is written to
4166 * <params>. If the top-level block member is not declared as an
4167 * array, the value one is written to <params>. If the top-level block
4168 * member is an array with no declared size, the value zero is written
4169 * to <params>."
4170 */
4171
4172 /* The buffer variable is on top level.*/
4173 if (!first_square_bracket && !first_dot)
4174 name_size = strlen(name);
4175 else if ((!first_square_bracket ||
4176 (first_dot && first_dot < first_square_bracket)))
4177 name_size = first_dot - name;
4178 else
4179 name_size = first_square_bracket - name;
4180
4181 return strndup(name, name_size);
4182 }
4183
4184 static char*
4185 get_var_name(const char *name)
4186 {
4187 const char *first_dot = strchr(name, '.');
4188
4189 if (!first_dot)
4190 return strdup(name);
4191
4192 return strndup(first_dot+1, strlen(first_dot) - 1);
4193 }
4194
4195 static bool
4196 is_top_level_shader_storage_block_member(const char* name,
4197 const char* interface_name,
4198 const char* field_name)
4199 {
4200 bool result = false;
4201
4202 /* If the given variable is already a top-level shader storage
4203 * block member, then return array_size = 1.
4204 * We could have two possibilities: if we have an instanced
4205 * shader storage block or not instanced.
4206 *
4207 * For the first, we check create a name as it was in top level and
4208 * compare it with the real name. If they are the same, then
4209 * the variable is already at top-level.
4210 *
4211 * Full instanced name is: interface name + '.' + var name +
4212 * NULL character
4213 */
4214 int name_length = strlen(interface_name) + 1 + strlen(field_name) + 1;
4215 char *full_instanced_name = (char *) calloc(name_length, sizeof(char));
4216 if (!full_instanced_name) {
4217 fprintf(stderr, "%s: Cannot allocate space for name\n", __func__);
4218 return false;
4219 }
4220
4221 util_snprintf(full_instanced_name, name_length, "%s.%s",
4222 interface_name, field_name);
4223
4224 /* Check if its top-level shader storage block member of an
4225 * instanced interface block, or of a unnamed interface block.
4226 */
4227 if (strcmp(name, full_instanced_name) == 0 ||
4228 strcmp(name, field_name) == 0)
4229 result = true;
4230
4231 free(full_instanced_name);
4232 return result;
4233 }
4234
4235 static int
4236 get_array_size(struct gl_uniform_storage *uni, const glsl_struct_field *field,
4237 char *interface_name, char *var_name)
4238 {
4239 /* The ARB_program_interface_query spec says:
4240 *
4241 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying
4242 * the number of active array elements of the top-level shader storage
4243 * block member containing to the active variable is written to
4244 * <params>. If the top-level block member is not declared as an
4245 * array, the value one is written to <params>. If the top-level block
4246 * member is an array with no declared size, the value zero is written
4247 * to <params>."
4248 */
4249 if (is_top_level_shader_storage_block_member(uni->name,
4250 interface_name,
4251 var_name))
4252 return 1;
4253 else if (field->type->is_unsized_array())
4254 return 0;
4255 else if (field->type->is_array())
4256 return field->type->length;
4257
4258 return 1;
4259 }
4260
4261 static int
4262 get_array_stride(struct gl_context *ctx, struct gl_uniform_storage *uni,
4263 const glsl_type *iface, const glsl_struct_field *field,
4264 char *interface_name, char *var_name)
4265 {
4266 /* The ARB_program_interface_query spec says:
4267 *
4268 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
4269 * identifying the stride between array elements of the top-level
4270 * shader storage block member containing the active variable is
4271 * written to <params>. For top-level block members declared as
4272 * arrays, the value written is the difference, in basic machine units,
4273 * between the offsets of the active variable for consecutive elements
4274 * in the top-level array. For top-level block members not declared as
4275 * an array, zero is written to <params>."
4276 */
4277 if (field->type->is_array()) {
4278 const enum glsl_matrix_layout matrix_layout =
4279 glsl_matrix_layout(field->matrix_layout);
4280 bool row_major = matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
4281 const glsl_type *array_type = field->type->fields.array;
4282
4283 if (is_top_level_shader_storage_block_member(uni->name,
4284 interface_name,
4285 var_name))
4286 return 0;
4287
4288 if (GLSL_INTERFACE_PACKING_STD140 ==
4289 iface->
4290 get_internal_ifc_packing(ctx->Const.UseSTD430AsDefaultPacking)) {
4291 if (array_type->is_record() || array_type->is_array())
4292 return glsl_align(array_type->std140_size(row_major), 16);
4293 else
4294 return MAX2(array_type->std140_base_alignment(row_major), 16);
4295 } else {
4296 return array_type->std430_array_stride(row_major);
4297 }
4298 }
4299 return 0;
4300 }
4301
4302 static void
4303 calculate_array_size_and_stride(struct gl_context *ctx,
4304 struct gl_shader_program *shProg,
4305 struct gl_uniform_storage *uni)
4306 {
4307 int block_index = uni->block_index;
4308 int array_size = -1;
4309 int array_stride = -1;
4310 char *var_name = get_top_level_name(uni->name);
4311 char *interface_name =
4312 get_top_level_name(uni->is_shader_storage ?
4313 shProg->data->ShaderStorageBlocks[block_index].Name :
4314 shProg->data->UniformBlocks[block_index].Name);
4315
4316 if (strcmp(var_name, interface_name) == 0) {
4317 /* Deal with instanced array of SSBOs */
4318 char *temp_name = get_var_name(uni->name);
4319 if (!temp_name) {
4320 linker_error(shProg, "Out of memory during linking.\n");
4321 goto write_top_level_array_size_and_stride;
4322 }
4323 free(var_name);
4324 var_name = get_top_level_name(temp_name);
4325 free(temp_name);
4326 if (!var_name) {
4327 linker_error(shProg, "Out of memory during linking.\n");
4328 goto write_top_level_array_size_and_stride;
4329 }
4330 }
4331
4332 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
4333 const gl_linked_shader *sh = shProg->_LinkedShaders[i];
4334 if (sh == NULL)
4335 continue;
4336
4337 foreach_in_list(ir_instruction, node, sh->ir) {
4338 ir_variable *var = node->as_variable();
4339 if (!var || !var->get_interface_type() ||
4340 var->data.mode != ir_var_shader_storage)
4341 continue;
4342
4343 const glsl_type *iface = var->get_interface_type();
4344
4345 if (strcmp(interface_name, iface->name) != 0)
4346 continue;
4347
4348 for (unsigned i = 0; i < iface->length; i++) {
4349 const glsl_struct_field *field = &iface->fields.structure[i];
4350 if (strcmp(field->name, var_name) != 0)
4351 continue;
4352
4353 array_stride = get_array_stride(ctx, uni, iface, field,
4354 interface_name, var_name);
4355 array_size = get_array_size(uni, field, interface_name, var_name);
4356 goto write_top_level_array_size_and_stride;
4357 }
4358 }
4359 }
4360 write_top_level_array_size_and_stride:
4361 free(interface_name);
4362 free(var_name);
4363 uni->top_level_array_stride = array_stride;
4364 uni->top_level_array_size = array_size;
4365 }
4366
4367 /**
4368 * Builds up a list of program resources that point to existing
4369 * resource data.
4370 */
4371 void
4372 build_program_resource_list(struct gl_context *ctx,
4373 struct gl_shader_program *shProg)
4374 {
4375 /* Rebuild resource list. */
4376 if (shProg->data->ProgramResourceList) {
4377 ralloc_free(shProg->data->ProgramResourceList);
4378 shProg->data->ProgramResourceList = NULL;
4379 shProg->data->NumProgramResourceList = 0;
4380 }
4381
4382 int input_stage = MESA_SHADER_STAGES, output_stage = 0;
4383
4384 /* Determine first input and final output stage. These are used to
4385 * detect which variables should be enumerated in the resource list
4386 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
4387 */
4388 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
4389 if (!shProg->_LinkedShaders[i])
4390 continue;
4391 if (input_stage == MESA_SHADER_STAGES)
4392 input_stage = i;
4393 output_stage = i;
4394 }
4395
4396 /* Empty shader, no resources. */
4397 if (input_stage == MESA_SHADER_STAGES && output_stage == 0)
4398 return;
4399
4400 struct set *resource_set = _mesa_set_create(NULL,
4401 _mesa_hash_pointer,
4402 _mesa_key_pointer_equal);
4403
4404 /* Program interface needs to expose varyings in case of SSO. */
4405 if (shProg->SeparateShader) {
4406 if (!add_packed_varyings(ctx, shProg, resource_set,
4407 input_stage, GL_PROGRAM_INPUT))
4408 return;
4409
4410 if (!add_packed_varyings(ctx, shProg, resource_set,
4411 output_stage, GL_PROGRAM_OUTPUT))
4412 return;
4413 }
4414
4415 if (!add_fragdata_arrays(ctx, shProg, resource_set))
4416 return;
4417
4418 /* Add inputs and outputs to the resource list. */
4419 if (!add_interface_variables(ctx, shProg, resource_set,
4420 input_stage, GL_PROGRAM_INPUT))
4421 return;
4422
4423 if (!add_interface_variables(ctx, shProg, resource_set,
4424 output_stage, GL_PROGRAM_OUTPUT))
4425 return;
4426
4427 if (shProg->last_vert_prog) {
4428 struct gl_transform_feedback_info *linked_xfb =
4429 shProg->last_vert_prog->sh.LinkedTransformFeedback;
4430
4431 /* Add transform feedback varyings. */
4432 if (linked_xfb->NumVarying > 0) {
4433 for (int i = 0; i < linked_xfb->NumVarying; i++) {
4434 if (!link_util_add_program_resource(shProg, resource_set,
4435 GL_TRANSFORM_FEEDBACK_VARYING,
4436 &linked_xfb->Varyings[i], 0))
4437 return;
4438 }
4439 }
4440
4441 /* Add transform feedback buffers. */
4442 for (unsigned i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
4443 if ((linked_xfb->ActiveBuffers >> i) & 1) {
4444 linked_xfb->Buffers[i].Binding = i;
4445 if (!link_util_add_program_resource(shProg, resource_set,
4446 GL_TRANSFORM_FEEDBACK_BUFFER,
4447 &linked_xfb->Buffers[i], 0))
4448 return;
4449 }
4450 }
4451 }
4452
4453 /* Add uniforms from uniform storage. */
4454 for (unsigned i = 0; i < shProg->data->NumUniformStorage; i++) {
4455 /* Do not add uniforms internally used by Mesa. */
4456 if (shProg->data->UniformStorage[i].hidden)
4457 continue;
4458
4459 uint8_t stageref =
4460 build_stageref(shProg, shProg->data->UniformStorage[i].name,
4461 ir_var_uniform);
4462
4463 /* Add stagereferences for uniforms in a uniform block. */
4464 bool is_shader_storage =
4465 shProg->data->UniformStorage[i].is_shader_storage;
4466 int block_index = shProg->data->UniformStorage[i].block_index;
4467 if (block_index != -1) {
4468 stageref |= is_shader_storage ?
4469 shProg->data->ShaderStorageBlocks[block_index].stageref :
4470 shProg->data->UniformBlocks[block_index].stageref;
4471 }
4472
4473 GLenum type = is_shader_storage ? GL_BUFFER_VARIABLE : GL_UNIFORM;
4474 if (!should_add_buffer_variable(shProg, type,
4475 shProg->data->UniformStorage[i].name))
4476 continue;
4477
4478 if (is_shader_storage) {
4479 calculate_array_size_and_stride(ctx, shProg,
4480 &shProg->data->UniformStorage[i]);
4481 }
4482
4483 if (!link_util_add_program_resource(shProg, resource_set, type,
4484 &shProg->data->UniformStorage[i], stageref))
4485 return;
4486 }
4487
4488 /* Add program uniform blocks. */
4489 for (unsigned i = 0; i < shProg->data->NumUniformBlocks; i++) {
4490 if (!link_util_add_program_resource(shProg, resource_set, GL_UNIFORM_BLOCK,
4491 &shProg->data->UniformBlocks[i], 0))
4492 return;
4493 }
4494
4495 /* Add program shader storage blocks. */
4496 for (unsigned i = 0; i < shProg->data->NumShaderStorageBlocks; i++) {
4497 if (!link_util_add_program_resource(shProg, resource_set, GL_SHADER_STORAGE_BLOCK,
4498 &shProg->data->ShaderStorageBlocks[i], 0))
4499 return;
4500 }
4501
4502 /* Add atomic counter buffers. */
4503 for (unsigned i = 0; i < shProg->data->NumAtomicBuffers; i++) {
4504 if (!link_util_add_program_resource(shProg, resource_set, GL_ATOMIC_COUNTER_BUFFER,
4505 &shProg->data->AtomicBuffers[i], 0))
4506 return;
4507 }
4508
4509 for (unsigned i = 0; i < shProg->data->NumUniformStorage; i++) {
4510 GLenum type;
4511 if (!shProg->data->UniformStorage[i].hidden)
4512 continue;
4513
4514 for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) {
4515 if (!shProg->data->UniformStorage[i].opaque[j].active ||
4516 !shProg->data->UniformStorage[i].type->is_subroutine())
4517 continue;
4518
4519 type = _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j);
4520 /* add shader subroutines */
4521 if (!link_util_add_program_resource(shProg, resource_set,
4522 type, &shProg->data->UniformStorage[i], 0))
4523 return;
4524 }
4525 }
4526
4527 unsigned mask = shProg->data->linked_stages;
4528 while (mask) {
4529 const int i = u_bit_scan(&mask);
4530 struct gl_program *p = shProg->_LinkedShaders[i]->Program;
4531
4532 GLuint type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i);
4533 for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
4534 if (!link_util_add_program_resource(shProg, resource_set,
4535 type, &p->sh.SubroutineFunctions[j], 0))
4536 return;
4537 }
4538 }
4539
4540 _mesa_set_destroy(resource_set, NULL);
4541 }
4542
4543 /**
4544 * This check is done to make sure we allow only constant expression
4545 * indexing and "constant-index-expression" (indexing with an expression
4546 * that includes loop induction variable).
4547 */
4548 static bool
4549 validate_sampler_array_indexing(struct gl_context *ctx,
4550 struct gl_shader_program *prog)
4551 {
4552 dynamic_sampler_array_indexing_visitor v;
4553 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
4554 if (prog->_LinkedShaders[i] == NULL)
4555 continue;
4556
4557 bool no_dynamic_indexing =
4558 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler;
4559
4560 /* Search for array derefs in shader. */
4561 v.run(prog->_LinkedShaders[i]->ir);
4562 if (v.uses_dynamic_sampler_array_indexing()) {
4563 const char *msg = "sampler arrays indexed with non-constant "
4564 "expressions is forbidden in GLSL %s %u";
4565 /* Backend has indicated that it has no dynamic indexing support. */
4566 if (no_dynamic_indexing) {
4567 linker_error(prog, msg, prog->IsES ? "ES" : "",
4568 prog->data->Version);
4569 return false;
4570 } else {
4571 linker_warning(prog, msg, prog->IsES ? "ES" : "",
4572 prog->data->Version);
4573 }
4574 }
4575 }
4576 return true;
4577 }
4578
4579 static void
4580 link_assign_subroutine_types(struct gl_shader_program *prog)
4581 {
4582 unsigned mask = prog->data->linked_stages;
4583 while (mask) {
4584 const int i = u_bit_scan(&mask);
4585 gl_program *p = prog->_LinkedShaders[i]->Program;
4586
4587 p->sh.MaxSubroutineFunctionIndex = 0;
4588 foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) {
4589 ir_function *fn = node->as_function();
4590 if (!fn)
4591 continue;
4592
4593 if (fn->is_subroutine)
4594 p->sh.NumSubroutineUniformTypes++;
4595
4596 if (!fn->num_subroutine_types)
4597 continue;
4598
4599 /* these should have been calculated earlier. */
4600 assert(fn->subroutine_index != -1);
4601 if (p->sh.NumSubroutineFunctions + 1 > MAX_SUBROUTINES) {
4602 linker_error(prog, "Too many subroutine functions declared.\n");
4603 return;
4604 }
4605 p->sh.SubroutineFunctions = reralloc(p, p->sh.SubroutineFunctions,
4606 struct gl_subroutine_function,
4607 p->sh.NumSubroutineFunctions + 1);
4608 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].name = ralloc_strdup(p, fn->name);
4609 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].num_compat_types = fn->num_subroutine_types;
4610 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types =
4611 ralloc_array(p, const struct glsl_type *,
4612 fn->num_subroutine_types);
4613
4614 /* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the
4615 * GLSL 4.5 spec:
4616 *
4617 * "Each subroutine with an index qualifier in the shader must be
4618 * given a unique index, otherwise a compile or link error will be
4619 * generated."
4620 */
4621 for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
4622 if (p->sh.SubroutineFunctions[j].index != -1 &&
4623 p->sh.SubroutineFunctions[j].index == fn->subroutine_index) {
4624 linker_error(prog, "each subroutine index qualifier in the "
4625 "shader must be unique\n");
4626 return;
4627 }
4628 }
4629 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].index =
4630 fn->subroutine_index;
4631
4632 if (fn->subroutine_index > (int)p->sh.MaxSubroutineFunctionIndex)
4633 p->sh.MaxSubroutineFunctionIndex = fn->subroutine_index;
4634
4635 for (int j = 0; j < fn->num_subroutine_types; j++)
4636 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types[j] = fn->subroutine_types[j];
4637 p->sh.NumSubroutineFunctions++;
4638 }
4639 }
4640 }
4641
4642 static void
4643 set_always_active_io(exec_list *ir, ir_variable_mode io_mode)
4644 {
4645 assert(io_mode == ir_var_shader_in || io_mode == ir_var_shader_out);
4646
4647 foreach_in_list(ir_instruction, node, ir) {
4648 ir_variable *const var = node->as_variable();
4649
4650 if (var == NULL || var->data.mode != io_mode)
4651 continue;
4652
4653 /* Don't set always active on builtins that haven't been redeclared */
4654 if (var->data.how_declared == ir_var_declared_implicitly)
4655 continue;
4656
4657 var->data.always_active_io = true;
4658 }
4659 }
4660
4661 /**
4662 * When separate shader programs are enabled, only input/outputs between
4663 * the stages of a multi-stage separate program can be safely removed
4664 * from the shader interface. Other inputs/outputs must remain active.
4665 */
4666 static void
4667 disable_varying_optimizations_for_sso(struct gl_shader_program *prog)
4668 {
4669 unsigned first, last;
4670 assert(prog->SeparateShader);
4671
4672 first = MESA_SHADER_STAGES;
4673 last = 0;
4674
4675 /* Determine first and last stage. Excluding the compute stage */
4676 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
4677 if (!prog->_LinkedShaders[i])
4678 continue;
4679 if (first == MESA_SHADER_STAGES)
4680 first = i;
4681 last = i;
4682 }
4683
4684 if (first == MESA_SHADER_STAGES)
4685 return;
4686
4687 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
4688 gl_linked_shader *sh = prog->_LinkedShaders[stage];
4689 if (!sh)
4690 continue;
4691
4692 /* Prevent the removal of inputs to the first and outputs from the last
4693 * stage, unless they are the initial pipeline inputs or final pipeline
4694 * outputs, respectively.
4695 *
4696 * The removal of IO between shaders in the same program is always
4697 * allowed.
4698 */
4699 if (stage == first && stage != MESA_SHADER_VERTEX)
4700 set_always_active_io(sh->ir, ir_var_shader_in);
4701 if (stage == last && stage != MESA_SHADER_FRAGMENT)
4702 set_always_active_io(sh->ir, ir_var_shader_out);
4703 }
4704 }
4705
4706 static void
4707 link_and_validate_uniforms(struct gl_context *ctx,
4708 struct gl_shader_program *prog)
4709 {
4710 update_array_sizes(prog);
4711 link_assign_uniform_locations(prog, ctx);
4712
4713 link_assign_atomic_counter_resources(ctx, prog);
4714 link_calculate_subroutine_compat(prog);
4715 check_resources(ctx, prog);
4716 check_subroutine_resources(prog);
4717 check_image_resources(ctx, prog);
4718 link_check_atomic_counter_resources(ctx, prog);
4719 }
4720
4721 static bool
4722 link_varyings_and_uniforms(unsigned first, unsigned last,
4723 struct gl_context *ctx,
4724 struct gl_shader_program *prog, void *mem_ctx)
4725 {
4726 /* Mark all generic shader inputs and outputs as unpaired. */
4727 for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) {
4728 if (prog->_LinkedShaders[i] != NULL) {
4729 link_invalidate_variable_locations(prog->_LinkedShaders[i]->ir);
4730 }
4731 }
4732
4733 unsigned prev = first;
4734 for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) {
4735 if (prog->_LinkedShaders[i] == NULL)
4736 continue;
4737
4738 match_explicit_outputs_to_inputs(prog->_LinkedShaders[prev],
4739 prog->_LinkedShaders[i]);
4740 prev = i;
4741 }
4742
4743 if (!assign_attribute_or_color_locations(mem_ctx, prog, &ctx->Const,
4744 MESA_SHADER_VERTEX)) {
4745 return false;
4746 }
4747
4748 if (!assign_attribute_or_color_locations(mem_ctx, prog, &ctx->Const,
4749 MESA_SHADER_FRAGMENT)) {
4750 return false;
4751 }
4752
4753 prog->last_vert_prog = NULL;
4754 for (int i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
4755 if (prog->_LinkedShaders[i] == NULL)
4756 continue;
4757
4758 prog->last_vert_prog = prog->_LinkedShaders[i]->Program;
4759 break;
4760 }
4761
4762 if (!link_varyings(prog, first, last, ctx, mem_ctx))
4763 return false;
4764
4765 link_and_validate_uniforms(ctx, prog);
4766
4767 if (!prog->data->LinkStatus)
4768 return false;
4769
4770 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
4771 if (prog->_LinkedShaders[i] == NULL)
4772 continue;
4773
4774 const struct gl_shader_compiler_options *options =
4775 &ctx->Const.ShaderCompilerOptions[i];
4776
4777 if (options->LowerBufferInterfaceBlocks)
4778 lower_ubo_reference(prog->_LinkedShaders[i],
4779 options->ClampBlockIndicesToArrayBounds,
4780 ctx->Const.UseSTD430AsDefaultPacking);
4781
4782 if (i == MESA_SHADER_COMPUTE)
4783 lower_shared_reference(ctx, prog, prog->_LinkedShaders[i]);
4784
4785 lower_vector_derefs(prog->_LinkedShaders[i]);
4786 do_vec_index_to_swizzle(prog->_LinkedShaders[i]->ir);
4787 }
4788
4789 return true;
4790 }
4791
4792 static void
4793 linker_optimisation_loop(struct gl_context *ctx, exec_list *ir,
4794 unsigned stage)
4795 {
4796 if (ctx->Const.GLSLOptimizeConservatively) {
4797 /* Run it just once. */
4798 do_common_optimization(ir, true, false,
4799 &ctx->Const.ShaderCompilerOptions[stage],
4800 ctx->Const.NativeIntegers);
4801 } else {
4802 /* Repeat it until it stops making changes. */
4803 while (do_common_optimization(ir, true, false,
4804 &ctx->Const.ShaderCompilerOptions[stage],
4805 ctx->Const.NativeIntegers))
4806 ;
4807 }
4808 }
4809
4810 void
4811 link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
4812 {
4813 prog->data->LinkStatus = LINKING_SUCCESS; /* All error paths will set this to false */
4814 prog->data->Validated = false;
4815
4816 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec says:
4817 *
4818 * "Linking can fail for a variety of reasons as specified in the
4819 * OpenGL Shading Language Specification, as well as any of the
4820 * following reasons:
4821 *
4822 * - No shader objects are attached to program."
4823 *
4824 * The Compatibility Profile specification does not list the error. In
4825 * Compatibility Profile missing shader stages are replaced by
4826 * fixed-function. This applies to the case where all stages are
4827 * missing.
4828 */
4829 if (prog->NumShaders == 0) {
4830 if (ctx->API != API_OPENGL_COMPAT)
4831 linker_error(prog, "no shaders attached to the program\n");
4832 return;
4833 }
4834
4835 #ifdef ENABLE_SHADER_CACHE
4836 if (shader_cache_read_program_metadata(ctx, prog))
4837 return;
4838 #endif
4839
4840 void *mem_ctx = ralloc_context(NULL); // temporary linker context
4841
4842 prog->ARB_fragment_coord_conventions_enable = false;
4843
4844 /* Separate the shaders into groups based on their type.
4845 */
4846 struct gl_shader **shader_list[MESA_SHADER_STAGES];
4847 unsigned num_shaders[MESA_SHADER_STAGES];
4848
4849 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
4850 shader_list[i] = (struct gl_shader **)
4851 calloc(prog->NumShaders, sizeof(struct gl_shader *));
4852 num_shaders[i] = 0;
4853 }
4854
4855 unsigned min_version = UINT_MAX;
4856 unsigned max_version = 0;
4857 for (unsigned i = 0; i < prog->NumShaders; i++) {
4858 min_version = MIN2(min_version, prog->Shaders[i]->Version);
4859 max_version = MAX2(max_version, prog->Shaders[i]->Version);
4860
4861 if (!ctx->Const.AllowGLSLRelaxedES &&
4862 prog->Shaders[i]->IsES != prog->Shaders[0]->IsES) {
4863 linker_error(prog, "all shaders must use same shading "
4864 "language version\n");
4865 goto done;
4866 }
4867
4868 if (prog->Shaders[i]->ARB_fragment_coord_conventions_enable) {
4869 prog->ARB_fragment_coord_conventions_enable = true;
4870 }
4871
4872 gl_shader_stage shader_type = prog->Shaders[i]->Stage;
4873 shader_list[shader_type][num_shaders[shader_type]] = prog->Shaders[i];
4874 num_shaders[shader_type]++;
4875 }
4876
4877 /* In desktop GLSL, different shader versions may be linked together. In
4878 * GLSL ES, all shader versions must be the same.
4879 */
4880 if (!ctx->Const.AllowGLSLRelaxedES && prog->Shaders[0]->IsES &&
4881 min_version != max_version) {
4882 linker_error(prog, "all shaders must use same shading "
4883 "language version\n");
4884 goto done;
4885 }
4886
4887 prog->data->Version = max_version;
4888 prog->IsES = prog->Shaders[0]->IsES;
4889
4890 /* Some shaders have to be linked with some other shaders present.
4891 */
4892 if (!prog->SeparateShader) {
4893 if (num_shaders[MESA_SHADER_GEOMETRY] > 0 &&
4894 num_shaders[MESA_SHADER_VERTEX] == 0) {
4895 linker_error(prog, "Geometry shader must be linked with "
4896 "vertex shader\n");
4897 goto done;
4898 }
4899 if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 &&
4900 num_shaders[MESA_SHADER_VERTEX] == 0) {
4901 linker_error(prog, "Tessellation evaluation shader must be linked "
4902 "with vertex shader\n");
4903 goto done;
4904 }
4905 if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
4906 num_shaders[MESA_SHADER_VERTEX] == 0) {
4907 linker_error(prog, "Tessellation control shader must be linked with "
4908 "vertex shader\n");
4909 goto done;
4910 }
4911
4912 /* Section 7.3 of the OpenGL ES 3.2 specification says:
4913 *
4914 * "Linking can fail for [...] any of the following reasons:
4915 *
4916 * * program contains an object to form a tessellation control
4917 * shader [...] and [...] the program is not separable and
4918 * contains no object to form a tessellation evaluation shader"
4919 *
4920 * The OpenGL spec is contradictory. It allows linking without a tess
4921 * eval shader, but that can only be used with transform feedback and
4922 * rasterization disabled. However, transform feedback isn't allowed
4923 * with GL_PATCHES, so it can't be used.
4924 *
4925 * More investigation showed that the idea of transform feedback after
4926 * a tess control shader was dropped, because some hw vendors couldn't
4927 * support tessellation without a tess eval shader, but the linker
4928 * section wasn't updated to reflect that.
4929 *
4930 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
4931 * spec bug.
4932 *
4933 * Do what's reasonable and always require a tess eval shader if a tess
4934 * control shader is present.
4935 */
4936 if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
4937 num_shaders[MESA_SHADER_TESS_EVAL] == 0) {
4938 linker_error(prog, "Tessellation control shader must be linked with "
4939 "tessellation evaluation shader\n");
4940 goto done;
4941 }
4942
4943 if (prog->IsES) {
4944 if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 &&
4945 num_shaders[MESA_SHADER_TESS_CTRL] == 0) {
4946 linker_error(prog, "GLSL ES requires non-separable programs "
4947 "containing a tessellation evaluation shader to also "
4948 "be linked with a tessellation control shader\n");
4949 goto done;
4950 }
4951 }
4952 }
4953
4954 /* Compute shaders have additional restrictions. */
4955 if (num_shaders[MESA_SHADER_COMPUTE] > 0 &&
4956 num_shaders[MESA_SHADER_COMPUTE] != prog->NumShaders) {
4957 linker_error(prog, "Compute shaders may not be linked with any other "
4958 "type of shader\n");
4959 }
4960
4961 /* Link all shaders for a particular stage and validate the result.
4962 */
4963 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
4964 if (num_shaders[stage] > 0) {
4965 gl_linked_shader *const sh =
4966 link_intrastage_shaders(mem_ctx, ctx, prog, shader_list[stage],
4967 num_shaders[stage], false);
4968
4969 if (!prog->data->LinkStatus) {
4970 if (sh)
4971 _mesa_delete_linked_shader(ctx, sh);
4972 goto done;
4973 }
4974
4975 switch (stage) {
4976 case MESA_SHADER_VERTEX:
4977 validate_vertex_shader_executable(prog, sh, ctx);
4978 break;
4979 case MESA_SHADER_TESS_CTRL:
4980 /* nothing to be done */
4981 break;
4982 case MESA_SHADER_TESS_EVAL:
4983 validate_tess_eval_shader_executable(prog, sh, ctx);
4984 break;
4985 case MESA_SHADER_GEOMETRY:
4986 validate_geometry_shader_executable(prog, sh, ctx);
4987 break;
4988 case MESA_SHADER_FRAGMENT:
4989 validate_fragment_shader_executable(prog, sh);
4990 break;
4991 }
4992 if (!prog->data->LinkStatus) {
4993 if (sh)
4994 _mesa_delete_linked_shader(ctx, sh);
4995 goto done;
4996 }
4997
4998 prog->_LinkedShaders[stage] = sh;
4999 prog->data->linked_stages |= 1 << stage;
5000 }
5001 }
5002
5003 /* Here begins the inter-stage linking phase. Some initial validation is
5004 * performed, then locations are assigned for uniforms, attributes, and
5005 * varyings.
5006 */
5007 cross_validate_uniforms(ctx, prog);
5008 if (!prog->data->LinkStatus)
5009 goto done;
5010
5011 unsigned first, last, prev;
5012
5013 first = MESA_SHADER_STAGES;
5014 last = 0;
5015
5016 /* Determine first and last stage. */
5017 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
5018 if (!prog->_LinkedShaders[i])
5019 continue;
5020 if (first == MESA_SHADER_STAGES)
5021 first = i;
5022 last = i;
5023 }
5024
5025 check_explicit_uniform_locations(ctx, prog);
5026 link_assign_subroutine_types(prog);
5027
5028 if (!prog->data->LinkStatus)
5029 goto done;
5030
5031 resize_tes_inputs(ctx, prog);
5032
5033 /* Validate the inputs of each stage with the output of the preceding
5034 * stage.
5035 */
5036 prev = first;
5037 for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) {
5038 if (prog->_LinkedShaders[i] == NULL)
5039 continue;
5040
5041 validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev],
5042 prog->_LinkedShaders[i]);
5043 if (!prog->data->LinkStatus)
5044 goto done;
5045
5046 cross_validate_outputs_to_inputs(ctx, prog,
5047 prog->_LinkedShaders[prev],
5048 prog->_LinkedShaders[i]);
5049 if (!prog->data->LinkStatus)
5050 goto done;
5051
5052 prev = i;
5053 }
5054
5055 /* The cross validation of outputs/inputs above validates explicit locations
5056 * but for SSO programs we need to do this also for the inputs in the
5057 * first stage and outputs of the last stage included in the program, since
5058 * there is no cross validation for these.
5059 */
5060 if (prog->SeparateShader)
5061 validate_sso_explicit_locations(ctx, prog,
5062 (gl_shader_stage) first,
5063 (gl_shader_stage) last);
5064
5065 /* Cross-validate uniform blocks between shader stages */
5066 validate_interstage_uniform_blocks(prog, prog->_LinkedShaders);
5067 if (!prog->data->LinkStatus)
5068 goto done;
5069
5070 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
5071 if (prog->_LinkedShaders[i] != NULL)
5072 lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
5073 }
5074
5075 if (prog->IsES && prog->data->Version == 100)
5076 if (!validate_invariant_builtins(prog,
5077 prog->_LinkedShaders[MESA_SHADER_VERTEX],
5078 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]))
5079 goto done;
5080
5081 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
5082 * it before optimization because we want most of the checks to get
5083 * dropped thanks to constant propagation.
5084 *
5085 * This rule also applies to GLSL ES 3.00.
5086 */
5087 if (max_version >= (prog->IsES ? 300 : 130)) {
5088 struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
5089 if (sh) {
5090 lower_discard_flow(sh->ir);
5091 }
5092 }
5093
5094 if (prog->SeparateShader)
5095 disable_varying_optimizations_for_sso(prog);
5096
5097 /* Process UBOs */
5098 if (!interstage_cross_validate_uniform_blocks(prog, false))
5099 goto done;
5100
5101 /* Process SSBOs */
5102 if (!interstage_cross_validate_uniform_blocks(prog, true))
5103 goto done;
5104
5105 /* Do common optimization before assigning storage for attributes,
5106 * uniforms, and varyings. Later optimization could possibly make
5107 * some of that unused.
5108 */
5109 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
5110 if (prog->_LinkedShaders[i] == NULL)
5111 continue;
5112
5113 detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
5114 if (!prog->data->LinkStatus)
5115 goto done;
5116
5117 if (ctx->Const.ShaderCompilerOptions[i].LowerCombinedClipCullDistance) {
5118 lower_clip_cull_distance(prog, prog->_LinkedShaders[i]);
5119 }
5120
5121 if (ctx->Const.LowerTessLevel) {
5122 lower_tess_level(prog->_LinkedShaders[i]);
5123 }
5124
5125 /* Call opts before lowering const arrays to uniforms so we can const
5126 * propagate any elements accessed directly.
5127 */
5128 linker_optimisation_loop(ctx, prog->_LinkedShaders[i]->ir, i);
5129
5130 /* Call opts after lowering const arrays to copy propagate things. */
5131 if (lower_const_arrays_to_uniforms(prog->_LinkedShaders[i]->ir, i))
5132 linker_optimisation_loop(ctx, prog->_LinkedShaders[i]->ir, i);
5133
5134 propagate_invariance(prog->_LinkedShaders[i]->ir);
5135 }
5136
5137 /* Validation for special cases where we allow sampler array indexing
5138 * with loop induction variable. This check emits a warning or error
5139 * depending if backend can handle dynamic indexing.
5140 */
5141 if ((!prog->IsES && prog->data->Version < 130) ||
5142 (prog->IsES && prog->data->Version < 300)) {
5143 if (!validate_sampler_array_indexing(ctx, prog))
5144 goto done;
5145 }
5146
5147 /* Check and validate stream emissions in geometry shaders */
5148 validate_geometry_shader_emissions(ctx, prog);
5149
5150 store_fragdepth_layout(prog);
5151
5152 if(!link_varyings_and_uniforms(first, last, ctx, prog, mem_ctx))
5153 goto done;
5154
5155 /* Linking varyings can cause some extra, useless swizzles to be generated
5156 * due to packing and unpacking.
5157 */
5158 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
5159 if (prog->_LinkedShaders[i] == NULL)
5160 continue;
5161
5162 optimize_swizzles(prog->_LinkedShaders[i]->ir);
5163 }
5164
5165 /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both
5166 * be present in a linked program. GL_ARB_ES2_compatibility doesn't say
5167 * anything about shader linking when one of the shaders (vertex or
5168 * fragment shader) is absent. So, the extension shouldn't change the
5169 * behavior specified in GLSL specification.
5170 *
5171 * From OpenGL ES 3.1 specification (7.3 Program Objects):
5172 * "Linking can fail for a variety of reasons as specified in the
5173 * OpenGL ES Shading Language Specification, as well as any of the
5174 * following reasons:
5175 *
5176 * ...
5177 *
5178 * * program contains objects to form either a vertex shader or
5179 * fragment shader, and program is not separable, and does not
5180 * contain objects to form both a vertex shader and fragment
5181 * shader."
5182 *
5183 * However, the only scenario in 3.1+ where we don't require them both is
5184 * when we have a compute shader. For example:
5185 *
5186 * - No shaders is a link error.
5187 * - Geom or Tess without a Vertex shader is a link error which means we
5188 * always require a Vertex shader and hence a Fragment shader.
5189 * - Finally a Compute shader linked with any other stage is a link error.
5190 */
5191 if (!prog->SeparateShader && ctx->API == API_OPENGLES2 &&
5192 num_shaders[MESA_SHADER_COMPUTE] == 0) {
5193 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
5194 linker_error(prog, "program lacks a vertex shader\n");
5195 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
5196 linker_error(prog, "program lacks a fragment shader\n");
5197 }
5198 }
5199
5200 done:
5201 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
5202 free(shader_list[i]);
5203 if (prog->_LinkedShaders[i] == NULL)
5204 continue;
5205
5206 /* Do a final validation step to make sure that the IR wasn't
5207 * invalidated by any modifications performed after intrastage linking.
5208 */
5209 validate_ir_tree(prog->_LinkedShaders[i]->ir);
5210
5211 /* Retain any live IR, but trash the rest. */
5212 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
5213
5214 /* The symbol table in the linked shaders may contain references to
5215 * variables that were removed (e.g., unused uniforms). Since it may
5216 * contain junk, there is no possible valid use. Delete it and set the
5217 * pointer to NULL.
5218 */
5219 delete prog->_LinkedShaders[i]->symbols;
5220 prog->_LinkedShaders[i]->symbols = NULL;
5221 }
5222
5223 ralloc_free(mem_ctx);
5224 }