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