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