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