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 <[email protected]>
65 */
66
67 #include <ctype.h>
68 #include "util/strndup.h"
69 #include "glsl_symbol_table.h"
70 #include "glsl_parser_extras.h"
71 #include "ir.h"
72 #include "nir.h"
73 #include "program.h"
74 #include "program/prog_instruction.h"
75 #include "program/program.h"
76 #include "util/mesa-sha1.h"
77 #include "util/set.h"
78 #include "string_to_uint_map.h"
79 #include "linker.h"
80 #include "linker_util.h"
81 #include "ir_optimization.h"
82 #include "ir_rvalue_visitor.h"
83 #include "ir_uniform.h"
84 #include "builtin_functions.h"
85 #include "shader_cache.h"
86 #include "util/u_string.h"
87 #include "util/u_math.h"
88
89
90 #include "main/shaderobj.h"
91 #include "main/enums.h"
92 #include "main/mtypes.h"
93 #include "main/context.h"
94
95
96 namespace {
97
98 /**
99 * A visitor helper that provides methods for updating the types of
100 * ir_dereferences. Classes that update variable types (say, updating
101 * array sizes) will want to use this so that dereference types stay in sync.
102 */
103 class deref_type_updater : public ir_hierarchical_visitor {
104 public:
visit(ir_dereference_variable * ir)105 virtual ir_visitor_status visit(ir_dereference_variable *ir)
106 {
107 ir->type = ir->var->type;
108 return visit_continue;
109 }
110
visit_leave(ir_dereference_array * ir)111 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
112 {
113 const glsl_type *const vt = ir->array->type;
114 if (glsl_type_is_array(vt))
115 ir->type = vt->fields.array;
116 return visit_continue;
117 }
118
visit_leave(ir_dereference_record * ir)119 virtual ir_visitor_status visit_leave(ir_dereference_record *ir)
120 {
121 ir->type = ir->record->type->fields.structure[ir->field_idx].type;
122 return visit_continue;
123 }
124 };
125
126
127 class array_length_to_const_visitor : public ir_rvalue_visitor {
128 public:
array_length_to_const_visitor()129 array_length_to_const_visitor()
130 {
131 this->progress = false;
132 }
133
~array_length_to_const_visitor()134 virtual ~array_length_to_const_visitor()
135 {
136 /* empty */
137 }
138
139 bool progress;
140
handle_rvalue(ir_rvalue ** rvalue)141 virtual void handle_rvalue(ir_rvalue **rvalue)
142 {
143 if (*rvalue == NULL || (*rvalue)->ir_type != ir_type_expression)
144 return;
145
146 ir_expression *expr = (*rvalue)->as_expression();
147 if (expr) {
148 if (expr->operation == ir_unop_implicitly_sized_array_length) {
149 assert(!glsl_type_is_unsized_array(expr->operands[0]->type));
150 ir_constant *constant = new(expr)
151 ir_constant(glsl_array_size(expr->operands[0]->type));
152 if (constant) {
153 *rvalue = constant;
154 }
155 }
156 }
157 }
158 };
159
160 } /* anonymous namespace */
161
162 void
linker_error(gl_shader_program * prog,const char * fmt,...)163 linker_error(gl_shader_program *prog, const char *fmt, ...)
164 {
165 va_list ap;
166
167 ralloc_strcat(&prog->data->InfoLog, "error: ");
168 va_start(ap, fmt);
169 ralloc_vasprintf_append(&prog->data->InfoLog, fmt, ap);
170 va_end(ap);
171
172 prog->data->LinkStatus = LINKING_FAILURE;
173 }
174
175
176 void
linker_warning(gl_shader_program * prog,const char * fmt,...)177 linker_warning(gl_shader_program *prog, const char *fmt, ...)
178 {
179 va_list ap;
180
181 ralloc_strcat(&prog->data->InfoLog, "warning: ");
182 va_start(ap, fmt);
183 ralloc_vasprintf_append(&prog->data->InfoLog, fmt, ap);
184 va_end(ap);
185
186 }
187
188 bool
validate_intrastage_arrays(struct gl_shader_program * prog,ir_variable * const var,ir_variable * const existing,bool match_precision)189 validate_intrastage_arrays(struct gl_shader_program *prog,
190 ir_variable *const var,
191 ir_variable *const existing,
192 bool match_precision)
193 {
194 /* Consider the types to be "the same" if both types are arrays
195 * of the same type and one of the arrays is implicitly sized.
196 * In addition, set the type of the linked variable to the
197 * explicitly sized array.
198 */
199 if (glsl_type_is_array(var->type) && glsl_type_is_array(existing->type)) {
200 const glsl_type *no_array_var = var->type->fields.array;
201 const glsl_type *no_array_existing = existing->type->fields.array;
202 bool type_matches;
203
204 type_matches = (match_precision ?
205 no_array_var == no_array_existing :
206 glsl_type_compare_no_precision(no_array_var, no_array_existing));
207
208 if (type_matches &&
209 ((var->type->length == 0)|| (existing->type->length == 0))) {
210 if (var->type->length != 0) {
211 if ((int)var->type->length <= existing->data.max_array_access) {
212 linker_error(prog, "%s `%s' declared as type "
213 "`%s' but outermost dimension has an index"
214 " of `%i'\n",
215 mode_string(var),
216 var->name, glsl_get_type_name(var->type),
217 existing->data.max_array_access);
218 }
219 existing->type = var->type;
220 return true;
221 } else if (existing->type->length != 0) {
222 if((int)existing->type->length <= var->data.max_array_access &&
223 !existing->data.from_ssbo_unsized_array) {
224 linker_error(prog, "%s `%s' declared as type "
225 "`%s' but outermost dimension has an index"
226 " of `%i'\n",
227 mode_string(var),
228 var->name, glsl_get_type_name(existing->type),
229 var->data.max_array_access);
230 }
231 return true;
232 }
233 }
234 }
235 return false;
236 }
237
238
239 /**
240 * Perform validation of global variables used across multiple shaders
241 */
242 static void
cross_validate_globals(const struct gl_constants * consts,struct gl_shader_program * prog,struct exec_list * ir,glsl_symbol_table * variables,bool uniforms_only)243 cross_validate_globals(const struct gl_constants *consts,
244 struct gl_shader_program *prog,
245 struct exec_list *ir, glsl_symbol_table *variables,
246 bool uniforms_only)
247 {
248 foreach_in_list(ir_instruction, node, ir) {
249 ir_variable *const var = node->as_variable();
250
251 if (var == NULL)
252 continue;
253
254 if (uniforms_only && (var->data.mode != ir_var_uniform && var->data.mode != ir_var_shader_storage))
255 continue;
256
257 /* don't cross validate subroutine uniforms */
258 if (glsl_contains_subroutine(var->type))
259 continue;
260
261 /* Don't cross validate interface instances. These are only relevant
262 * inside a shader. The cross validation is done at the Interface Block
263 * name level.
264 */
265 if (var->is_interface_instance())
266 continue;
267
268 /* Don't cross validate temporaries that are at global scope. These
269 * will eventually get pulled into the shaders 'main'.
270 */
271 if (var->data.mode == ir_var_temporary)
272 continue;
273
274 /* If a global with this name has already been seen, verify that the
275 * new instance has the same type. In addition, if the globals have
276 * initializers, the values of the initializers must be the same.
277 */
278 ir_variable *const existing = variables->get_variable(var->name);
279 if (existing != NULL) {
280 /* Check if types match. */
281 if (var->type != existing->type) {
282 if (!validate_intrastage_arrays(prog, var, existing)) {
283 /* If it is an unsized array in a Shader Storage Block,
284 * two different shaders can access to different elements.
285 * Because of that, they might be converted to different
286 * sized arrays, then check that they are compatible but
287 * ignore the array size.
288 */
289 if (!(var->data.mode == ir_var_shader_storage &&
290 var->data.from_ssbo_unsized_array &&
291 existing->data.mode == ir_var_shader_storage &&
292 existing->data.from_ssbo_unsized_array &&
293 var->type->gl_type == existing->type->gl_type)) {
294 linker_error(prog, "%s `%s' declared as type "
295 "`%s' and type `%s'\n",
296 mode_string(var),
297 var->name, glsl_get_type_name(var->type),
298 glsl_get_type_name(existing->type));
299 return;
300 }
301 }
302 }
303
304 if (var->data.explicit_location) {
305 if (existing->data.explicit_location
306 && (var->data.location != existing->data.location)) {
307 linker_error(prog, "explicit locations for %s "
308 "`%s' have differing values\n",
309 mode_string(var), var->name);
310 return;
311 }
312
313 if (var->data.location_frac != existing->data.location_frac) {
314 linker_error(prog, "explicit components for %s `%s' have "
315 "differing values\n", mode_string(var), var->name);
316 return;
317 }
318
319 existing->data.location = var->data.location;
320 existing->data.explicit_location = true;
321 } else {
322 /* Check if uniform with implicit location was marked explicit
323 * by earlier shader stage. If so, mark it explicit in this stage
324 * too to make sure later processing does not treat it as
325 * implicit one.
326 */
327 if (existing->data.explicit_location) {
328 var->data.location = existing->data.location;
329 var->data.explicit_location = true;
330 }
331 }
332
333 /* From the GLSL 4.20 specification:
334 * "A link error will result if two compilation units in a program
335 * specify different integer-constant bindings for the same
336 * opaque-uniform name. However, it is not an error to specify a
337 * binding on some but not all declarations for the same name"
338 */
339 if (var->data.explicit_binding) {
340 if (existing->data.explicit_binding &&
341 var->data.binding != existing->data.binding) {
342 linker_error(prog, "explicit bindings for %s "
343 "`%s' have differing values\n",
344 mode_string(var), var->name);
345 return;
346 }
347
348 existing->data.binding = var->data.binding;
349 existing->data.explicit_binding = true;
350 }
351
352 if (glsl_contains_atomic(var->type) &&
353 var->data.offset != existing->data.offset) {
354 linker_error(prog, "offset specifications for %s "
355 "`%s' have differing values\n",
356 mode_string(var), var->name);
357 return;
358 }
359
360 /* Validate layout qualifiers for gl_FragDepth.
361 *
362 * From the AMD/ARB_conservative_depth specs:
363 *
364 * "If gl_FragDepth is redeclared in any fragment shader in a
365 * program, it must be redeclared in all fragment shaders in
366 * that program that have static assignments to
367 * gl_FragDepth. All redeclarations of gl_FragDepth in all
368 * fragment shaders in a single program must have the same set
369 * of qualifiers."
370 */
371 if (strcmp(var->name, "gl_FragDepth") == 0) {
372 bool layout_declared = var->data.depth_layout != ir_depth_layout_none;
373 bool layout_differs =
374 var->data.depth_layout != existing->data.depth_layout;
375
376 if (layout_declared && layout_differs) {
377 linker_error(prog,
378 "All redeclarations of gl_FragDepth in all "
379 "fragment shaders in a single program must have "
380 "the same set of qualifiers.\n");
381 }
382
383 if (var->data.used && layout_differs) {
384 linker_error(prog,
385 "If gl_FragDepth is redeclared with a layout "
386 "qualifier in any fragment shader, it must be "
387 "redeclared with the same layout qualifier in "
388 "all fragment shaders that have assignments to "
389 "gl_FragDepth\n");
390 }
391 }
392
393 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
394 *
395 * "If a shared global has multiple initializers, the
396 * initializers must all be constant expressions, and they
397 * must all have the same value. Otherwise, a link error will
398 * result. (A shared global having only one initializer does
399 * not require that initializer to be a constant expression.)"
400 *
401 * Previous to 4.20 the GLSL spec simply said that initializers
402 * must have the same value. In this case of non-constant
403 * initializers, this was impossible to determine. As a result,
404 * no vendor actually implemented that behavior. The 4.20
405 * behavior matches the implemented behavior of at least one other
406 * vendor, so we'll implement that for all GLSL versions.
407 * If (at least) one of these constant expressions is implicit,
408 * because it was added by glsl_zero_init, we skip the verification.
409 */
410 if (var->constant_initializer != NULL) {
411 if (existing->constant_initializer != NULL &&
412 !existing->data.is_implicit_initializer &&
413 !var->data.is_implicit_initializer) {
414 if (!var->constant_initializer->has_value(existing->constant_initializer)) {
415 linker_error(prog, "initializers for %s "
416 "`%s' have differing values\n",
417 mode_string(var), var->name);
418 return;
419 }
420 } else {
421 /* If the first-seen instance of a particular uniform did
422 * not have an initializer but a later instance does,
423 * replace the former with the later.
424 */
425 if (!var->data.is_implicit_initializer)
426 variables->replace_variable(existing->name, var);
427 }
428 }
429
430 if (var->data.has_initializer) {
431 if (existing->data.has_initializer
432 && (var->constant_initializer == NULL
433 || existing->constant_initializer == NULL)) {
434 linker_error(prog,
435 "shared global variable `%s' has multiple "
436 "non-constant initializers.\n",
437 var->name);
438 return;
439 }
440 }
441
442 if (existing->data.explicit_invariant != var->data.explicit_invariant) {
443 linker_error(prog, "declarations for %s `%s' have "
444 "mismatching invariant qualifiers\n",
445 mode_string(var), var->name);
446 return;
447 }
448 if (existing->data.centroid != var->data.centroid) {
449 linker_error(prog, "declarations for %s `%s' have "
450 "mismatching centroid qualifiers\n",
451 mode_string(var), var->name);
452 return;
453 }
454 if (existing->data.sample != var->data.sample) {
455 linker_error(prog, "declarations for %s `%s` have "
456 "mismatching sample qualifiers\n",
457 mode_string(var), var->name);
458 return;
459 }
460 if (existing->data.image_format != var->data.image_format) {
461 linker_error(prog, "declarations for %s `%s` have "
462 "mismatching image format qualifiers\n",
463 mode_string(var), var->name);
464 return;
465 }
466
467 /* Check the precision qualifier matches for uniform variables on
468 * GLSL ES.
469 */
470 if (!consts->AllowGLSLRelaxedES &&
471 prog->IsES && !var->get_interface_type() &&
472 existing->data.precision != var->data.precision) {
473 if ((existing->data.used && var->data.used) ||
474 prog->GLSL_Version >= 300) {
475 linker_error(prog, "declarations for %s `%s` have "
476 "mismatching precision qualifiers\n",
477 mode_string(var), var->name);
478 return;
479 } else {
480 linker_warning(prog, "declarations for %s `%s` have "
481 "mismatching precision qualifiers\n",
482 mode_string(var), var->name);
483 }
484 }
485
486 /* In OpenGL GLSL 3.20 spec, section 4.3.9:
487 *
488 * "It is a link-time error if any particular shader interface
489 * contains:
490 *
491 * - two different blocks, each having no instance name, and each
492 * having a member of the same name, or
493 *
494 * - a variable outside a block, and a block with no instance name,
495 * where the variable has the same name as a member in the block."
496 */
497 const glsl_type *var_itype = var->get_interface_type();
498 const glsl_type *existing_itype = existing->get_interface_type();
499 if (var_itype != existing_itype) {
500 if (!var_itype || !existing_itype) {
501 linker_error(prog, "declarations for %s `%s` are inside block "
502 "`%s` and outside a block",
503 mode_string(var), var->name,
504 glsl_get_type_name(var_itype ? var_itype : existing_itype));
505 return;
506 } else if (strcmp(glsl_get_type_name(var_itype), glsl_get_type_name(existing_itype)) != 0) {
507 linker_error(prog, "declarations for %s `%s` are inside blocks "
508 "`%s` and `%s`",
509 mode_string(var), var->name,
510 glsl_get_type_name(existing_itype),
511 glsl_get_type_name(var_itype));
512 return;
513 }
514 }
515 } else
516 variables->add_variable(var);
517 }
518 }
519
520 /**
521 * Populates a shaders symbol table with all global declarations
522 */
523 static void
populate_symbol_table(gl_linked_shader * sh,glsl_symbol_table * symbols)524 populate_symbol_table(gl_linked_shader *sh, glsl_symbol_table *symbols)
525 {
526 sh->symbols = new(sh) glsl_symbol_table;
527
528 _mesa_glsl_copy_symbols_from_table(sh->ir, symbols, sh->symbols);
529 }
530
531
532 /**
533 * Remap variables referenced in an instruction tree
534 *
535 * This is used when instruction trees are cloned from one shader and placed in
536 * another. These trees will contain references to \c ir_variable nodes that
537 * do not exist in the target shader. This function finds these \c ir_variable
538 * references and replaces the references with matching variables in the target
539 * shader.
540 *
541 * If there is no matching variable in the target shader, a clone of the
542 * \c ir_variable is made and added to the target shader. The new variable is
543 * added to \b both the instruction stream and the symbol table.
544 *
545 * \param inst IR tree that is to be processed.
546 * \param symbols Symbol table containing global scope symbols in the
547 * linked shader.
548 * \param instructions Instruction stream where new variable declarations
549 * should be added.
550 */
551 static void
remap_variables(ir_instruction * inst,struct gl_linked_shader * target,hash_table * temps)552 remap_variables(ir_instruction *inst, struct gl_linked_shader *target,
553 hash_table *temps)
554 {
555 class remap_visitor : public ir_hierarchical_visitor {
556 public:
557 remap_visitor(struct gl_linked_shader *target, hash_table *temps)
558 {
559 this->target = target;
560 this->symbols = target->symbols;
561 this->instructions = target->ir;
562 this->temps = temps;
563 }
564
565 virtual ir_visitor_status visit(ir_dereference_variable *ir)
566 {
567 if (ir->var->data.mode == ir_var_temporary) {
568 hash_entry *entry = _mesa_hash_table_search(temps, ir->var);
569 ir_variable *var = entry ? (ir_variable *) entry->data : NULL;
570
571 assert(var != NULL);
572 ir->var = var;
573 return visit_continue;
574 }
575
576 ir_variable *const existing =
577 this->symbols->get_variable(ir->var->name);
578 if (existing != NULL)
579 ir->var = existing;
580 else {
581 ir_variable *copy = ir->var->clone(this->target, NULL);
582
583 this->symbols->add_variable(copy);
584 this->instructions->push_head(copy);
585 ir->var = copy;
586 }
587
588 return visit_continue;
589 }
590
591 private:
592 struct gl_linked_shader *target;
593 glsl_symbol_table *symbols;
594 exec_list *instructions;
595 hash_table *temps;
596 };
597
598 remap_visitor v(target, temps);
599
600 inst->accept(&v);
601 }
602
603
604 /**
605 * Move non-declarations from one instruction stream to another
606 *
607 * The intended usage pattern of this function is to pass the pointer to the
608 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
609 * pointer) for \c last and \c false for \c make_copies on the first
610 * call. Successive calls pass the return value of the previous call for
611 * \c last and \c true for \c make_copies.
612 *
613 * \param instructions Source instruction stream
614 * \param last Instruction after which new instructions should be
615 * inserted in the target instruction stream
616 * \param make_copies Flag selecting whether instructions in \c instructions
617 * should be copied (via \c ir_instruction::clone) into the
618 * target list or moved.
619 *
620 * \return
621 * The new "last" instruction in the target instruction stream. This pointer
622 * is suitable for use as the \c last parameter of a later call to this
623 * function.
624 */
625 static exec_node *
move_non_declarations(exec_list * instructions,exec_node * last,bool make_copies,gl_linked_shader * target)626 move_non_declarations(exec_list *instructions, exec_node *last,
627 bool make_copies, gl_linked_shader *target)
628 {
629 hash_table *temps = NULL;
630
631 if (make_copies)
632 temps = _mesa_pointer_hash_table_create(NULL);
633
634 foreach_in_list_safe(ir_instruction, inst, instructions) {
635 if (inst->as_function())
636 continue;
637
638 ir_variable *var = inst->as_variable();
639 if ((var != NULL) && (var->data.mode != ir_var_temporary))
640 continue;
641
642 assert(inst->as_assignment()
643 || inst->as_call()
644 || inst->as_if() /* for initializers with the ?: operator */
645 || ((var != NULL) && (var->data.mode == ir_var_temporary)));
646
647 if (make_copies) {
648 inst = inst->clone(target, NULL);
649
650 if (var != NULL)
651 _mesa_hash_table_insert(temps, var, inst);
652 else
653 remap_variables(inst, target, temps);
654 } else {
655 inst->remove();
656 }
657
658 last->insert_after(inst);
659 last = inst;
660 }
661
662 if (make_copies)
663 _mesa_hash_table_destroy(temps, NULL);
664
665 return last;
666 }
667
668
669 /**
670 * This class is only used in link_intrastage_shaders() below but declaring
671 * it inside that function leads to compiler warnings with some versions of
672 * gcc.
673 */
674 class array_sizing_visitor : public deref_type_updater {
675 public:
676 using deref_type_updater::visit;
677
array_sizing_visitor()678 array_sizing_visitor()
679 : mem_ctx(ralloc_context(NULL)),
680 unnamed_interfaces(_mesa_pointer_hash_table_create(NULL))
681 {
682 }
683
~array_sizing_visitor()684 ~array_sizing_visitor()
685 {
686 _mesa_hash_table_destroy(this->unnamed_interfaces, NULL);
687 ralloc_free(this->mem_ctx);
688 }
689
690 array_sizing_visitor(const array_sizing_visitor &) = delete;
691 array_sizing_visitor & operator=(const array_sizing_visitor &) = delete;
692
visit(ir_variable * var)693 virtual ir_visitor_status visit(ir_variable *var)
694 {
695 const glsl_type *type_without_array;
696 bool implicit_sized_array = var->data.implicit_sized_array;
697 fixup_type(&var->type, var->data.max_array_access,
698 var->data.from_ssbo_unsized_array,
699 &implicit_sized_array);
700 var->data.implicit_sized_array = implicit_sized_array;
701 type_without_array = glsl_without_array(var->type);
702 if (glsl_type_is_interface(var->type)) {
703 if (interface_contains_unsized_arrays(var->type)) {
704 const glsl_type *new_type =
705 resize_interface_members(var->type,
706 var->get_max_ifc_array_access(),
707 var->is_in_shader_storage_block());
708 var->type = new_type;
709 var->change_interface_type(new_type);
710 }
711 } else if (glsl_type_is_interface(type_without_array)) {
712 if (interface_contains_unsized_arrays(type_without_array)) {
713 const glsl_type *new_type =
714 resize_interface_members(type_without_array,
715 var->get_max_ifc_array_access(),
716 var->is_in_shader_storage_block());
717 var->change_interface_type(new_type);
718 var->type = update_interface_members_array(var->type, new_type);
719 }
720 } else if (const glsl_type *ifc_type = var->get_interface_type()) {
721 /* Store a pointer to the variable in the unnamed_interfaces
722 * hashtable.
723 */
724 hash_entry *entry =
725 _mesa_hash_table_search(this->unnamed_interfaces,
726 ifc_type);
727
728 ir_variable **interface_vars = entry ? (ir_variable **) entry->data : NULL;
729
730 if (interface_vars == NULL) {
731 interface_vars = rzalloc_array(mem_ctx, ir_variable *,
732 ifc_type->length);
733 _mesa_hash_table_insert(this->unnamed_interfaces, ifc_type,
734 interface_vars);
735 }
736 unsigned index = glsl_get_field_index(ifc_type, var->name);
737 assert(index < ifc_type->length);
738 assert(interface_vars[index] == NULL);
739 interface_vars[index] = var;
740 }
741 return visit_continue;
742 }
743
744 /**
745 * For each unnamed interface block that was discovered while running the
746 * visitor, adjust the interface type to reflect the newly assigned array
747 * sizes, and fix up the ir_variable nodes to point to the new interface
748 * type.
749 */
fixup_unnamed_interface_types()750 void fixup_unnamed_interface_types()
751 {
752 hash_table_call_foreach(this->unnamed_interfaces,
753 fixup_unnamed_interface_type, NULL);
754 }
755
756 private:
757 /**
758 * If the type pointed to by \c type represents an unsized array, replace
759 * it with a sized array whose size is determined by max_array_access.
760 */
fixup_type(const glsl_type ** type,unsigned max_array_access,bool from_ssbo_unsized_array,bool * implicit_sized)761 static void fixup_type(const glsl_type **type, unsigned max_array_access,
762 bool from_ssbo_unsized_array, bool *implicit_sized)
763 {
764 if (!from_ssbo_unsized_array && glsl_type_is_unsized_array(*type)) {
765 *type = glsl_array_type((*type)->fields.array,
766 max_array_access + 1, 0);
767 *implicit_sized = true;
768 assert(*type != NULL);
769 }
770 }
771
772 static const glsl_type *
update_interface_members_array(const glsl_type * type,const glsl_type * new_interface_type)773 update_interface_members_array(const glsl_type *type,
774 const glsl_type *new_interface_type)
775 {
776 const glsl_type *element_type = type->fields.array;
777 if (glsl_type_is_array(element_type)) {
778 const glsl_type *new_array_type =
779 update_interface_members_array(element_type, new_interface_type);
780 return glsl_array_type(new_array_type, type->length, 0);
781 } else {
782 return glsl_array_type(new_interface_type, type->length, 0);
783 }
784 }
785
786 /**
787 * Determine whether the given interface type contains unsized arrays (if
788 * it doesn't, array_sizing_visitor doesn't need to process it).
789 */
interface_contains_unsized_arrays(const glsl_type * type)790 static bool interface_contains_unsized_arrays(const glsl_type *type)
791 {
792 for (unsigned i = 0; i < type->length; i++) {
793 const glsl_type *elem_type = type->fields.structure[i].type;
794 if (glsl_type_is_unsized_array(elem_type))
795 return true;
796 }
797 return false;
798 }
799
800 /**
801 * Create a new interface type based on the given type, with unsized arrays
802 * replaced by sized arrays whose size is determined by
803 * max_ifc_array_access.
804 */
805 static const glsl_type *
resize_interface_members(const glsl_type * type,const int * max_ifc_array_access,bool is_ssbo)806 resize_interface_members(const glsl_type *type,
807 const int *max_ifc_array_access,
808 bool is_ssbo)
809 {
810 unsigned num_fields = type->length;
811 glsl_struct_field *fields = new glsl_struct_field[num_fields];
812 memcpy(fields, type->fields.structure,
813 num_fields * sizeof(*fields));
814 for (unsigned i = 0; i < num_fields; i++) {
815 bool implicit_sized_array = fields[i].implicit_sized_array;
816 /* If SSBO last member is unsized array, we don't replace it by a sized
817 * array.
818 */
819 if (is_ssbo && i == (num_fields - 1))
820 fixup_type(&fields[i].type, max_ifc_array_access[i],
821 true, &implicit_sized_array);
822 else
823 fixup_type(&fields[i].type, max_ifc_array_access[i],
824 false, &implicit_sized_array);
825 fields[i].implicit_sized_array = implicit_sized_array;
826 }
827 glsl_interface_packing packing =
828 (glsl_interface_packing) type->interface_packing;
829 bool row_major = (bool) type->interface_row_major;
830 const glsl_type *new_ifc_type =
831 glsl_interface_type(fields, num_fields,
832 packing, row_major, glsl_get_type_name(type));
833 delete [] fields;
834 return new_ifc_type;
835 }
836
fixup_unnamed_interface_type(const void * key,void * data,void *)837 static void fixup_unnamed_interface_type(const void *key, void *data,
838 void *)
839 {
840 const glsl_type *ifc_type = (const glsl_type *) key;
841 ir_variable **interface_vars = (ir_variable **) data;
842 unsigned num_fields = ifc_type->length;
843 glsl_struct_field *fields = new glsl_struct_field[num_fields];
844 memcpy(fields, ifc_type->fields.structure,
845 num_fields * sizeof(*fields));
846 bool interface_type_changed = false;
847 for (unsigned i = 0; i < num_fields; i++) {
848 if (interface_vars[i] != NULL &&
849 fields[i].type != interface_vars[i]->type) {
850 fields[i].type = interface_vars[i]->type;
851 interface_type_changed = true;
852 }
853 }
854 if (!interface_type_changed) {
855 delete [] fields;
856 return;
857 }
858 glsl_interface_packing packing =
859 (glsl_interface_packing) ifc_type->interface_packing;
860 bool row_major = (bool) ifc_type->interface_row_major;
861 const glsl_type *new_ifc_type =
862 glsl_interface_type(fields, num_fields, packing,
863 row_major, glsl_get_type_name(ifc_type));
864 delete [] fields;
865 for (unsigned i = 0; i < num_fields; i++) {
866 if (interface_vars[i] != NULL)
867 interface_vars[i]->change_interface_type(new_ifc_type);
868 }
869 }
870
871 /**
872 * Memory context used to allocate the data in \c unnamed_interfaces.
873 */
874 void *mem_ctx;
875
876 /**
877 * Hash table from const glsl_type * to an array of ir_variable *'s
878 * pointing to the ir_variables constituting each unnamed interface block.
879 */
880 hash_table *unnamed_interfaces;
881 };
882
883 static bool
validate_xfb_buffer_stride(const struct gl_constants * consts,unsigned idx,struct gl_shader_program * prog)884 validate_xfb_buffer_stride(const struct gl_constants *consts, unsigned idx,
885 struct gl_shader_program *prog)
886 {
887 /* We will validate doubles at a later stage */
888 if (prog->TransformFeedback.BufferStride[idx] % 4) {
889 linker_error(prog, "invalid qualifier xfb_stride=%d must be a "
890 "multiple of 4 or if its applied to a type that is "
891 "or contains a double a multiple of 8.",
892 prog->TransformFeedback.BufferStride[idx]);
893 return false;
894 }
895
896 if (prog->TransformFeedback.BufferStride[idx] / 4 >
897 consts->MaxTransformFeedbackInterleavedComponents) {
898 linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
899 "limit has been exceeded.");
900 return false;
901 }
902
903 return true;
904 }
905
906 /**
907 * Check for conflicting xfb_stride default qualifiers and store buffer stride
908 * for later use.
909 */
910 static void
link_xfb_stride_layout_qualifiers(const struct gl_constants * consts,struct gl_shader_program * prog,struct gl_shader ** shader_list,unsigned num_shaders)911 link_xfb_stride_layout_qualifiers(const struct gl_constants *consts,
912 struct gl_shader_program *prog,
913 struct gl_shader **shader_list,
914 unsigned num_shaders)
915 {
916 for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {
917 prog->TransformFeedback.BufferStride[i] = 0;
918 }
919
920 for (unsigned i = 0; i < num_shaders; i++) {
921 struct gl_shader *shader = shader_list[i];
922
923 for (unsigned j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
924 if (shader->TransformFeedbackBufferStride[j]) {
925 if (prog->TransformFeedback.BufferStride[j] == 0) {
926 prog->TransformFeedback.BufferStride[j] =
927 shader->TransformFeedbackBufferStride[j];
928 if (!validate_xfb_buffer_stride(consts, j, prog))
929 return;
930 } else if (prog->TransformFeedback.BufferStride[j] !=
931 shader->TransformFeedbackBufferStride[j]){
932 linker_error(prog,
933 "intrastage shaders defined with conflicting "
934 "xfb_stride for buffer %d (%d and %d)\n", j,
935 prog->TransformFeedback.BufferStride[j],
936 shader->TransformFeedbackBufferStride[j]);
937 return;
938 }
939 }
940 }
941 }
942 }
943
944 /**
945 * Check for conflicting bindless/bound sampler/image layout qualifiers at
946 * global scope.
947 */
948 static void
link_bindless_layout_qualifiers(struct gl_shader_program * prog,struct gl_shader ** shader_list,unsigned num_shaders)949 link_bindless_layout_qualifiers(struct gl_shader_program *prog,
950 struct gl_shader **shader_list,
951 unsigned num_shaders)
952 {
953 bool bindless_sampler, bindless_image;
954 bool bound_sampler, bound_image;
955
956 bindless_sampler = bindless_image = false;
957 bound_sampler = bound_image = false;
958
959 for (unsigned i = 0; i < num_shaders; i++) {
960 struct gl_shader *shader = shader_list[i];
961
962 if (shader->bindless_sampler)
963 bindless_sampler = true;
964 if (shader->bindless_image)
965 bindless_image = true;
966 if (shader->bound_sampler)
967 bound_sampler = true;
968 if (shader->bound_image)
969 bound_image = true;
970
971 if ((bindless_sampler && bound_sampler) ||
972 (bindless_image && bound_image)) {
973 /* From section 4.4.6 of the ARB_bindless_texture spec:
974 *
975 * "If both bindless_sampler and bound_sampler, or bindless_image
976 * and bound_image, are declared at global scope in any
977 * compilation unit, a link- time error will be generated."
978 */
979 linker_error(prog, "both bindless_sampler and bound_sampler, or "
980 "bindless_image and bound_image, can't be declared at "
981 "global scope");
982 }
983 }
984 }
985
986 /**
987 * Check for conflicting viewport_relative settings across shaders, and sets
988 * the value for the linked shader.
989 */
990 static void
link_layer_viewport_relative_qualifier(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)991 link_layer_viewport_relative_qualifier(struct gl_shader_program *prog,
992 struct gl_program *gl_prog,
993 struct gl_shader **shader_list,
994 unsigned num_shaders)
995 {
996 unsigned i;
997
998 /* Find first shader with explicit layer declaration */
999 for (i = 0; i < num_shaders; i++) {
1000 if (shader_list[i]->redeclares_gl_layer) {
1001 gl_prog->info.layer_viewport_relative =
1002 shader_list[i]->layer_viewport_relative;
1003 break;
1004 }
1005 }
1006
1007 /* Now make sure that each subsequent shader's explicit layer declaration
1008 * matches the first one's.
1009 */
1010 for (; i < num_shaders; i++) {
1011 if (shader_list[i]->redeclares_gl_layer &&
1012 shader_list[i]->layer_viewport_relative !=
1013 gl_prog->info.layer_viewport_relative) {
1014 linker_error(prog, "all gl_Layer redeclarations must have identical "
1015 "viewport_relative settings");
1016 }
1017 }
1018 }
1019
1020 /**
1021 * Performs the cross-validation of tessellation control shader vertices and
1022 * layout qualifiers for the attached tessellation control shaders,
1023 * and propagates them to the linked TCS and linked shader program.
1024 */
1025 static void
link_tcs_out_layout_qualifiers(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)1026 link_tcs_out_layout_qualifiers(struct gl_shader_program *prog,
1027 struct gl_program *gl_prog,
1028 struct gl_shader **shader_list,
1029 unsigned num_shaders)
1030 {
1031 if (gl_prog->info.stage != MESA_SHADER_TESS_CTRL)
1032 return;
1033
1034 gl_prog->info.tess.tcs_vertices_out = 0;
1035
1036 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1037 *
1038 * "All tessellation control shader layout declarations in a program
1039 * must specify the same output patch vertex count. There must be at
1040 * least one layout qualifier specifying an output patch vertex count
1041 * in any program containing tessellation control shaders; however,
1042 * such a declaration is not required in all tessellation control
1043 * shaders."
1044 */
1045
1046 for (unsigned i = 0; i < num_shaders; i++) {
1047 struct gl_shader *shader = shader_list[i];
1048
1049 if (shader->info.TessCtrl.VerticesOut != 0) {
1050 if (gl_prog->info.tess.tcs_vertices_out != 0 &&
1051 gl_prog->info.tess.tcs_vertices_out !=
1052 (unsigned) shader->info.TessCtrl.VerticesOut) {
1053 linker_error(prog, "tessellation control shader defined with "
1054 "conflicting output vertex count (%d and %d)\n",
1055 gl_prog->info.tess.tcs_vertices_out,
1056 shader->info.TessCtrl.VerticesOut);
1057 return;
1058 }
1059 gl_prog->info.tess.tcs_vertices_out =
1060 shader->info.TessCtrl.VerticesOut;
1061 }
1062 }
1063
1064 /* Just do the intrastage -> interstage propagation right now,
1065 * since we already know we're in the right type of shader program
1066 * for doing it.
1067 */
1068 if (gl_prog->info.tess.tcs_vertices_out == 0) {
1069 linker_error(prog, "tessellation control shader didn't declare "
1070 "vertices out layout qualifier\n");
1071 return;
1072 }
1073 }
1074
1075
1076 /**
1077 * Performs the cross-validation of tessellation evaluation shader
1078 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1079 * for the attached tessellation evaluation shaders, and propagates them
1080 * to the linked TES and linked shader program.
1081 */
1082 static void
link_tes_in_layout_qualifiers(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)1083 link_tes_in_layout_qualifiers(struct gl_shader_program *prog,
1084 struct gl_program *gl_prog,
1085 struct gl_shader **shader_list,
1086 unsigned num_shaders)
1087 {
1088 if (gl_prog->info.stage != MESA_SHADER_TESS_EVAL)
1089 return;
1090
1091 int point_mode = -1;
1092 unsigned vertex_order = 0;
1093
1094 gl_prog->info.tess._primitive_mode = TESS_PRIMITIVE_UNSPECIFIED;
1095 gl_prog->info.tess.spacing = TESS_SPACING_UNSPECIFIED;
1096
1097 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1098 *
1099 * "At least one tessellation evaluation shader (compilation unit) in
1100 * a program must declare a primitive mode in its input layout.
1101 * Declaration vertex spacing, ordering, and point mode identifiers is
1102 * optional. It is not required that all tessellation evaluation
1103 * shaders in a program declare a primitive mode. If spacing or
1104 * vertex ordering declarations are omitted, the tessellation
1105 * primitive generator will use equal spacing or counter-clockwise
1106 * vertex ordering, respectively. If a point mode declaration is
1107 * omitted, the tessellation primitive generator will produce lines or
1108 * triangles according to the primitive mode."
1109 */
1110
1111 for (unsigned i = 0; i < num_shaders; i++) {
1112 struct gl_shader *shader = shader_list[i];
1113
1114 if (shader->info.TessEval._PrimitiveMode != TESS_PRIMITIVE_UNSPECIFIED) {
1115 if (gl_prog->info.tess._primitive_mode != TESS_PRIMITIVE_UNSPECIFIED &&
1116 gl_prog->info.tess._primitive_mode !=
1117 shader->info.TessEval._PrimitiveMode) {
1118 linker_error(prog, "tessellation evaluation shader defined with "
1119 "conflicting input primitive modes.\n");
1120 return;
1121 }
1122 gl_prog->info.tess._primitive_mode =
1123 shader->info.TessEval._PrimitiveMode;
1124 }
1125
1126 if (shader->info.TessEval.Spacing != 0) {
1127 if (gl_prog->info.tess.spacing != 0 && gl_prog->info.tess.spacing !=
1128 shader->info.TessEval.Spacing) {
1129 linker_error(prog, "tessellation evaluation shader defined with "
1130 "conflicting vertex spacing.\n");
1131 return;
1132 }
1133 gl_prog->info.tess.spacing = shader->info.TessEval.Spacing;
1134 }
1135
1136 if (shader->info.TessEval.VertexOrder != 0) {
1137 if (vertex_order != 0 &&
1138 vertex_order != shader->info.TessEval.VertexOrder) {
1139 linker_error(prog, "tessellation evaluation shader defined with "
1140 "conflicting ordering.\n");
1141 return;
1142 }
1143 vertex_order = shader->info.TessEval.VertexOrder;
1144 }
1145
1146 if (shader->info.TessEval.PointMode != -1) {
1147 if (point_mode != -1 &&
1148 point_mode != shader->info.TessEval.PointMode) {
1149 linker_error(prog, "tessellation evaluation shader defined with "
1150 "conflicting point modes.\n");
1151 return;
1152 }
1153 point_mode = shader->info.TessEval.PointMode;
1154 }
1155
1156 }
1157
1158 /* Just do the intrastage -> interstage propagation right now,
1159 * since we already know we're in the right type of shader program
1160 * for doing it.
1161 */
1162 if (gl_prog->info.tess._primitive_mode == TESS_PRIMITIVE_UNSPECIFIED) {
1163 linker_error(prog,
1164 "tessellation evaluation shader didn't declare input "
1165 "primitive modes.\n");
1166 return;
1167 }
1168
1169 if (gl_prog->info.tess.spacing == TESS_SPACING_UNSPECIFIED)
1170 gl_prog->info.tess.spacing = TESS_SPACING_EQUAL;
1171
1172 if (vertex_order == 0 || vertex_order == GL_CCW)
1173 gl_prog->info.tess.ccw = true;
1174 else
1175 gl_prog->info.tess.ccw = false;
1176
1177
1178 if (point_mode == -1 || point_mode == GL_FALSE)
1179 gl_prog->info.tess.point_mode = false;
1180 else
1181 gl_prog->info.tess.point_mode = true;
1182 }
1183
1184
1185 /**
1186 * Performs the cross-validation of layout qualifiers specified in
1187 * redeclaration of gl_FragCoord for the attached fragment shaders,
1188 * and propagates them to the linked FS and linked shader program.
1189 */
1190 static void
link_fs_inout_layout_qualifiers(struct gl_shader_program * prog,struct gl_linked_shader * linked_shader,struct gl_shader ** shader_list,unsigned num_shaders,bool arb_fragment_coord_conventions_enable)1191 link_fs_inout_layout_qualifiers(struct gl_shader_program *prog,
1192 struct gl_linked_shader *linked_shader,
1193 struct gl_shader **shader_list,
1194 unsigned num_shaders,
1195 bool arb_fragment_coord_conventions_enable)
1196 {
1197 bool redeclares_gl_fragcoord = false;
1198 bool uses_gl_fragcoord = false;
1199 bool origin_upper_left = false;
1200 bool pixel_center_integer = false;
1201
1202 if (linked_shader->Stage != MESA_SHADER_FRAGMENT ||
1203 (prog->GLSL_Version < 150 && !arb_fragment_coord_conventions_enable))
1204 return;
1205
1206 for (unsigned i = 0; i < num_shaders; i++) {
1207 struct gl_shader *shader = shader_list[i];
1208 /* From the GLSL 1.50 spec, page 39:
1209 *
1210 * "If gl_FragCoord is redeclared in any fragment shader in a program,
1211 * it must be redeclared in all the fragment shaders in that program
1212 * that have a static use gl_FragCoord."
1213 */
1214 if ((redeclares_gl_fragcoord && !shader->redeclares_gl_fragcoord &&
1215 shader->uses_gl_fragcoord)
1216 || (shader->redeclares_gl_fragcoord && !redeclares_gl_fragcoord &&
1217 uses_gl_fragcoord)) {
1218 linker_error(prog, "fragment shader defined with conflicting "
1219 "layout qualifiers for gl_FragCoord\n");
1220 }
1221
1222 /* From the GLSL 1.50 spec, page 39:
1223 *
1224 * "All redeclarations of gl_FragCoord in all fragment shaders in a
1225 * single program must have the same set of qualifiers."
1226 */
1227 if (redeclares_gl_fragcoord && shader->redeclares_gl_fragcoord &&
1228 (shader->origin_upper_left != origin_upper_left ||
1229 shader->pixel_center_integer != pixel_center_integer)) {
1230 linker_error(prog, "fragment shader defined with conflicting "
1231 "layout qualifiers for gl_FragCoord\n");
1232 }
1233
1234 /* Update the linked shader state. Note that uses_gl_fragcoord should
1235 * accumulate the results. The other values should replace. If there
1236 * are multiple redeclarations, all the fields except uses_gl_fragcoord
1237 * are already known to be the same.
1238 */
1239 if (shader->redeclares_gl_fragcoord || shader->uses_gl_fragcoord) {
1240 redeclares_gl_fragcoord = shader->redeclares_gl_fragcoord;
1241 uses_gl_fragcoord |= shader->uses_gl_fragcoord;
1242 origin_upper_left = shader->origin_upper_left;
1243 pixel_center_integer = shader->pixel_center_integer;
1244 }
1245
1246 linked_shader->Program->info.fs.early_fragment_tests |=
1247 shader->EarlyFragmentTests || shader->PostDepthCoverage;
1248 linked_shader->Program->info.fs.inner_coverage |= shader->InnerCoverage;
1249 linked_shader->Program->info.fs.post_depth_coverage |=
1250 shader->PostDepthCoverage;
1251 linked_shader->Program->info.fs.pixel_interlock_ordered |=
1252 shader->PixelInterlockOrdered;
1253 linked_shader->Program->info.fs.pixel_interlock_unordered |=
1254 shader->PixelInterlockUnordered;
1255 linked_shader->Program->info.fs.sample_interlock_ordered |=
1256 shader->SampleInterlockOrdered;
1257 linked_shader->Program->info.fs.sample_interlock_unordered |=
1258 shader->SampleInterlockUnordered;
1259 linked_shader->Program->info.fs.advanced_blend_modes |= shader->BlendSupport;
1260 }
1261
1262 linked_shader->Program->info.fs.pixel_center_integer = pixel_center_integer;
1263 linked_shader->Program->info.fs.origin_upper_left = origin_upper_left;
1264 }
1265
1266 /**
1267 * Performs the cross-validation of geometry shader max_vertices and
1268 * primitive type layout qualifiers for the attached geometry shaders,
1269 * and propagates them to the linked GS and linked shader program.
1270 */
1271 static void
link_gs_inout_layout_qualifiers(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)1272 link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
1273 struct gl_program *gl_prog,
1274 struct gl_shader **shader_list,
1275 unsigned num_shaders)
1276 {
1277 /* No in/out qualifiers defined for anything but GLSL 1.50+
1278 * geometry shaders so far.
1279 */
1280 if (gl_prog->info.stage != MESA_SHADER_GEOMETRY || prog->GLSL_Version < 150)
1281 return;
1282
1283 int vertices_out = -1;
1284
1285 gl_prog->info.gs.invocations = 0;
1286 gl_prog->info.gs.input_primitive = MESA_PRIM_UNKNOWN;
1287 gl_prog->info.gs.output_primitive = MESA_PRIM_UNKNOWN;
1288
1289 /* From the GLSL 1.50 spec, page 46:
1290 *
1291 * "All geometry shader output layout declarations in a program
1292 * must declare the same layout and same value for
1293 * max_vertices. There must be at least one geometry output
1294 * layout declaration somewhere in a program, but not all
1295 * geometry shaders (compilation units) are required to
1296 * declare it."
1297 */
1298
1299 for (unsigned i = 0; i < num_shaders; i++) {
1300 struct gl_shader *shader = shader_list[i];
1301
1302 if (shader->info.Geom.InputType != MESA_PRIM_UNKNOWN) {
1303 if (gl_prog->info.gs.input_primitive != MESA_PRIM_UNKNOWN &&
1304 gl_prog->info.gs.input_primitive !=
1305 shader->info.Geom.InputType) {
1306 linker_error(prog, "geometry shader defined with conflicting "
1307 "input types\n");
1308 return;
1309 }
1310 gl_prog->info.gs.input_primitive = (enum mesa_prim)shader->info.Geom.InputType;
1311 }
1312
1313 if (shader->info.Geom.OutputType != MESA_PRIM_UNKNOWN) {
1314 if (gl_prog->info.gs.output_primitive != MESA_PRIM_UNKNOWN &&
1315 gl_prog->info.gs.output_primitive !=
1316 shader->info.Geom.OutputType) {
1317 linker_error(prog, "geometry shader defined with conflicting "
1318 "output types\n");
1319 return;
1320 }
1321 gl_prog->info.gs.output_primitive = (enum mesa_prim)shader->info.Geom.OutputType;
1322 }
1323
1324 if (shader->info.Geom.VerticesOut != -1) {
1325 if (vertices_out != -1 &&
1326 vertices_out != shader->info.Geom.VerticesOut) {
1327 linker_error(prog, "geometry shader defined with conflicting "
1328 "output vertex count (%d and %d)\n",
1329 vertices_out, shader->info.Geom.VerticesOut);
1330 return;
1331 }
1332 vertices_out = shader->info.Geom.VerticesOut;
1333 }
1334
1335 if (shader->info.Geom.Invocations != 0) {
1336 if (gl_prog->info.gs.invocations != 0 &&
1337 gl_prog->info.gs.invocations !=
1338 (unsigned) shader->info.Geom.Invocations) {
1339 linker_error(prog, "geometry shader defined with conflicting "
1340 "invocation count (%d and %d)\n",
1341 gl_prog->info.gs.invocations,
1342 shader->info.Geom.Invocations);
1343 return;
1344 }
1345 gl_prog->info.gs.invocations = shader->info.Geom.Invocations;
1346 }
1347 }
1348
1349 /* Just do the intrastage -> interstage propagation right now,
1350 * since we already know we're in the right type of shader program
1351 * for doing it.
1352 */
1353 if (gl_prog->info.gs.input_primitive == MESA_PRIM_UNKNOWN) {
1354 linker_error(prog,
1355 "geometry shader didn't declare primitive input type\n");
1356 return;
1357 }
1358
1359 if (gl_prog->info.gs.output_primitive == MESA_PRIM_UNKNOWN) {
1360 linker_error(prog,
1361 "geometry shader didn't declare primitive output type\n");
1362 return;
1363 }
1364
1365 if (vertices_out == -1) {
1366 linker_error(prog,
1367 "geometry shader didn't declare max_vertices\n");
1368 return;
1369 } else {
1370 gl_prog->info.gs.vertices_out = vertices_out;
1371 }
1372
1373 if (gl_prog->info.gs.invocations == 0)
1374 gl_prog->info.gs.invocations = 1;
1375 }
1376
1377
1378 /**
1379 * Perform cross-validation of compute shader local_size_{x,y,z} layout and
1380 * derivative arrangement qualifiers for the attached compute shaders, and
1381 * propagate them to the linked CS and linked shader program.
1382 */
1383 static void
link_cs_input_layout_qualifiers(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)1384 link_cs_input_layout_qualifiers(struct gl_shader_program *prog,
1385 struct gl_program *gl_prog,
1386 struct gl_shader **shader_list,
1387 unsigned num_shaders)
1388 {
1389 /* This function is called for all shader stages, but it only has an effect
1390 * for compute shaders.
1391 */
1392 if (gl_prog->info.stage != MESA_SHADER_COMPUTE)
1393 return;
1394
1395 for (int i = 0; i < 3; i++)
1396 gl_prog->info.workgroup_size[i] = 0;
1397
1398 gl_prog->info.workgroup_size_variable = false;
1399
1400 gl_prog->info.derivative_group = DERIVATIVE_GROUP_NONE;
1401
1402 /* From the ARB_compute_shader spec, in the section describing local size
1403 * declarations:
1404 *
1405 * If multiple compute shaders attached to a single program object
1406 * declare local work-group size, the declarations must be identical;
1407 * otherwise a link-time error results. Furthermore, if a program
1408 * object contains any compute shaders, at least one must contain an
1409 * input layout qualifier specifying the local work sizes of the
1410 * program, or a link-time error will occur.
1411 */
1412 for (unsigned sh = 0; sh < num_shaders; sh++) {
1413 struct gl_shader *shader = shader_list[sh];
1414
1415 if (shader->info.Comp.LocalSize[0] != 0) {
1416 if (gl_prog->info.workgroup_size[0] != 0) {
1417 for (int i = 0; i < 3; i++) {
1418 if (gl_prog->info.workgroup_size[i] !=
1419 shader->info.Comp.LocalSize[i]) {
1420 linker_error(prog, "compute shader defined with conflicting "
1421 "local sizes\n");
1422 return;
1423 }
1424 }
1425 }
1426 for (int i = 0; i < 3; i++) {
1427 gl_prog->info.workgroup_size[i] =
1428 shader->info.Comp.LocalSize[i];
1429 }
1430 } else if (shader->info.Comp.LocalSizeVariable) {
1431 if (gl_prog->info.workgroup_size[0] != 0) {
1432 /* The ARB_compute_variable_group_size spec says:
1433 *
1434 * If one compute shader attached to a program declares a
1435 * variable local group size and a second compute shader
1436 * attached to the same program declares a fixed local group
1437 * size, a link-time error results.
1438 */
1439 linker_error(prog, "compute shader defined with both fixed and "
1440 "variable local group size\n");
1441 return;
1442 }
1443 gl_prog->info.workgroup_size_variable = true;
1444 }
1445
1446 enum gl_derivative_group group = shader->info.Comp.DerivativeGroup;
1447 if (group != DERIVATIVE_GROUP_NONE) {
1448 if (gl_prog->info.derivative_group != DERIVATIVE_GROUP_NONE &&
1449 gl_prog->info.derivative_group != group) {
1450 linker_error(prog, "compute shader defined with conflicting "
1451 "derivative groups\n");
1452 return;
1453 }
1454 gl_prog->info.derivative_group = group;
1455 }
1456 }
1457
1458 /* Just do the intrastage -> interstage propagation right now,
1459 * since we already know we're in the right type of shader program
1460 * for doing it.
1461 */
1462 if (gl_prog->info.workgroup_size[0] == 0 &&
1463 !gl_prog->info.workgroup_size_variable) {
1464 linker_error(prog, "compute shader must contain a fixed or a variable "
1465 "local group size\n");
1466 return;
1467 }
1468
1469 if (gl_prog->info.derivative_group == DERIVATIVE_GROUP_QUADS) {
1470 if (gl_prog->info.workgroup_size[0] % 2 != 0) {
1471 linker_error(prog, "derivative_group_quadsNV must be used with a "
1472 "local group size whose first dimension "
1473 "is a multiple of 2\n");
1474 return;
1475 }
1476 if (gl_prog->info.workgroup_size[1] % 2 != 0) {
1477 linker_error(prog, "derivative_group_quadsNV must be used with a local"
1478 "group size whose second dimension "
1479 "is a multiple of 2\n");
1480 return;
1481 }
1482 } else if (gl_prog->info.derivative_group == DERIVATIVE_GROUP_LINEAR) {
1483 if ((gl_prog->info.workgroup_size[0] *
1484 gl_prog->info.workgroup_size[1] *
1485 gl_prog->info.workgroup_size[2]) % 4 != 0) {
1486 linker_error(prog, "derivative_group_linearNV must be used with a "
1487 "local group size whose total number of invocations "
1488 "is a multiple of 4\n");
1489 return;
1490 }
1491 }
1492 }
1493
1494 /**
1495 * Link all out variables on a single stage which are not
1496 * directly used in a shader with the main function.
1497 */
1498 static void
link_output_variables(struct gl_linked_shader * linked_shader,struct gl_shader ** shader_list,unsigned num_shaders)1499 link_output_variables(struct gl_linked_shader *linked_shader,
1500 struct gl_shader **shader_list,
1501 unsigned num_shaders)
1502 {
1503 struct glsl_symbol_table *symbols = linked_shader->symbols;
1504
1505 for (unsigned i = 0; i < num_shaders; i++) {
1506
1507 /* Skip shader object with main function */
1508 if (shader_list[i]->symbols->get_function("main"))
1509 continue;
1510
1511 foreach_in_list(ir_instruction, ir, shader_list[i]->ir) {
1512 if (ir->ir_type != ir_type_variable)
1513 continue;
1514
1515 ir_variable *var = (ir_variable *) ir;
1516
1517 if (var->data.mode == ir_var_shader_out &&
1518 !symbols->get_variable(var->name)) {
1519 var = var->clone(linked_shader, NULL);
1520 symbols->add_variable(var);
1521 linked_shader->ir->push_head(var);
1522 }
1523 }
1524 }
1525
1526 return;
1527 }
1528
1529
1530 /**
1531 * Combine a group of shaders for a single stage to generate a linked shader
1532 *
1533 * \note
1534 * If this function is supplied a single shader, it is cloned, and the new
1535 * shader is returned.
1536 */
1537 struct gl_linked_shader *
link_intrastage_shaders(void * mem_ctx,struct gl_context * ctx,struct gl_shader_program * prog,struct gl_shader ** shader_list,unsigned num_shaders,bool allow_missing_main)1538 link_intrastage_shaders(void *mem_ctx,
1539 struct gl_context *ctx,
1540 struct gl_shader_program *prog,
1541 struct gl_shader **shader_list,
1542 unsigned num_shaders,
1543 bool allow_missing_main)
1544 {
1545 bool arb_fragment_coord_conventions_enable = false;
1546 bool KHR_shader_subgroup_basic_enable = false;
1547
1548 /* Check that global variables defined in multiple shaders are consistent.
1549 */
1550 glsl_symbol_table variables;
1551 for (unsigned i = 0; i < num_shaders; i++) {
1552 if (shader_list[i] == NULL)
1553 continue;
1554 cross_validate_globals(&ctx->Const, prog, shader_list[i]->ir, &variables,
1555 false);
1556 if (shader_list[i]->ARB_fragment_coord_conventions_enable)
1557 arb_fragment_coord_conventions_enable = true;
1558 if (shader_list[i]->KHR_shader_subgroup_basic_enable)
1559 KHR_shader_subgroup_basic_enable = true;
1560 }
1561
1562 if (!prog->data->LinkStatus)
1563 return NULL;
1564
1565 /* Check that interface blocks defined in multiple shaders are consistent.
1566 */
1567 validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
1568 num_shaders);
1569 if (!prog->data->LinkStatus)
1570 return NULL;
1571
1572 /* Check that there is only a single definition of each function signature
1573 * across all shaders.
1574 */
1575 for (unsigned i = 0; i < (num_shaders - 1); i++) {
1576 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
1577 ir_function *const f = node->as_function();
1578
1579 if (f == NULL)
1580 continue;
1581
1582 for (unsigned j = i + 1; j < num_shaders; j++) {
1583 ir_function *const other =
1584 shader_list[j]->symbols->get_function(f->name);
1585
1586 /* If the other shader has no function (and therefore no function
1587 * signatures) with the same name, skip to the next shader.
1588 */
1589 if (other == NULL)
1590 continue;
1591
1592 foreach_in_list(ir_function_signature, sig, &f->signatures) {
1593 if (!sig->is_defined)
1594 continue;
1595
1596 ir_function_signature *other_sig =
1597 other->exact_matching_signature(NULL, &sig->parameters);
1598
1599 if (other_sig != NULL && other_sig->is_defined) {
1600 linker_error(prog, "function `%s' is multiply defined\n",
1601 f->name);
1602 return NULL;
1603 }
1604 }
1605 }
1606 }
1607 }
1608
1609 /* Find the shader that defines main, and make a clone of it.
1610 *
1611 * Starting with the clone, search for undefined references. If one is
1612 * found, find the shader that defines it. Clone the reference and add
1613 * it to the shader. Repeat until there are no undefined references or
1614 * until a reference cannot be resolved.
1615 */
1616 gl_shader *main = NULL;
1617 for (unsigned i = 0; i < num_shaders; i++) {
1618 if (_mesa_get_main_function_signature(shader_list[i]->symbols)) {
1619 main = shader_list[i];
1620 break;
1621 }
1622 }
1623
1624 if (main == NULL && allow_missing_main)
1625 main = shader_list[0];
1626
1627 if (main == NULL) {
1628 linker_error(prog, "%s shader lacks `main'\n",
1629 _mesa_shader_stage_to_string(shader_list[0]->Stage));
1630 return NULL;
1631 }
1632
1633 gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
1634 linked->Stage = shader_list[0]->Stage;
1635
1636 /* Create program and attach it to the linked shader */
1637 struct gl_program *gl_prog =
1638 ctx->Driver.NewProgram(ctx, shader_list[0]->Stage, prog->Name, false);
1639 if (!gl_prog) {
1640 prog->data->LinkStatus = LINKING_FAILURE;
1641 _mesa_delete_linked_shader(ctx, linked);
1642 return NULL;
1643 }
1644
1645 _mesa_reference_shader_program_data(&gl_prog->sh.data, prog->data);
1646
1647 /* Don't use _mesa_reference_program() just take ownership */
1648 linked->Program = gl_prog;
1649
1650 linked->ir = new(linked) exec_list;
1651 clone_ir_list(mem_ctx, linked->ir, main->ir);
1652
1653 link_fs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders,
1654 arb_fragment_coord_conventions_enable);
1655 link_tcs_out_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
1656 link_tes_in_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
1657 link_gs_inout_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
1658 link_cs_input_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
1659
1660 if (linked->Stage != MESA_SHADER_FRAGMENT)
1661 link_xfb_stride_layout_qualifiers(&ctx->Const, prog, shader_list, num_shaders);
1662
1663 link_bindless_layout_qualifiers(prog, shader_list, num_shaders);
1664
1665 link_layer_viewport_relative_qualifier(prog, gl_prog, shader_list, num_shaders);
1666
1667 populate_symbol_table(linked, shader_list[0]->symbols);
1668
1669 gl_prog->info.subgroup_size = KHR_shader_subgroup_basic_enable ?
1670 SUBGROUP_SIZE_API_CONSTANT : SUBGROUP_SIZE_UNIFORM;
1671
1672 /* The pointer to the main function in the final linked shader (i.e., the
1673 * copy of the original shader that contained the main function).
1674 */
1675 ir_function_signature *const main_sig =
1676 _mesa_get_main_function_signature(linked->symbols);
1677
1678 /* Move any instructions other than variable declarations or function
1679 * declarations into main.
1680 */
1681 if (main_sig != NULL) {
1682 exec_node *insertion_point =
1683 move_non_declarations(linked->ir, &main_sig->body.head_sentinel, false,
1684 linked);
1685
1686 for (unsigned i = 0; i < num_shaders; i++) {
1687 if (shader_list[i] == main)
1688 continue;
1689
1690 insertion_point = move_non_declarations(shader_list[i]->ir,
1691 insertion_point, true, linked);
1692 }
1693 }
1694
1695 if (!link_function_calls(prog, linked, main, shader_list, num_shaders)) {
1696 _mesa_delete_linked_shader(ctx, linked);
1697 return NULL;
1698 }
1699
1700 if (linked->Stage != MESA_SHADER_FRAGMENT)
1701 link_output_variables(linked, shader_list, num_shaders);
1702
1703 /* Make a pass over all variable declarations to ensure that arrays with
1704 * unspecified sizes have a size specified. The size is inferred from the
1705 * max_array_access field.
1706 */
1707 array_sizing_visitor v;
1708 v.run(linked->ir);
1709 v.fixup_unnamed_interface_types();
1710
1711 /* Now that we know the sizes of all the arrays, we can replace .length()
1712 * calls with a constant expression.
1713 */
1714 array_length_to_const_visitor len_v;
1715 len_v.run(linked->ir);
1716
1717 if (!prog->data->LinkStatus) {
1718 _mesa_delete_linked_shader(ctx, linked);
1719 return NULL;
1720 }
1721
1722 /* At this point linked should contain all of the linked IR, so
1723 * validate it to make sure nothing went wrong.
1724 */
1725 validate_ir_tree(linked->ir);
1726
1727 /* Set the linked source BLAKE3. */
1728 if (num_shaders == 1) {
1729 memcpy(linked->linked_source_blake3, shader_list[0]->compiled_source_blake3,
1730 BLAKE3_OUT_LEN);
1731 } else {
1732 struct mesa_blake3 blake3_ctx;
1733 _mesa_blake3_init(&blake3_ctx);
1734
1735 for (unsigned i = 0; i < num_shaders; i++) {
1736 if (shader_list[i] == NULL)
1737 continue;
1738
1739 _mesa_blake3_update(&blake3_ctx, shader_list[i]->compiled_source_blake3,
1740 BLAKE3_OUT_LEN);
1741 }
1742 _mesa_blake3_final(&blake3_ctx, linked->linked_source_blake3);
1743 }
1744
1745 return linked;
1746 }
1747
1748 void
link_shaders(struct gl_context * ctx,struct gl_shader_program * prog)1749 link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
1750 {
1751 const struct gl_constants *consts = &ctx->Const;
1752 prog->data->LinkStatus = LINKING_SUCCESS; /* All error paths will set this to false */
1753 prog->data->Validated = false;
1754
1755 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec says:
1756 *
1757 * "Linking can fail for a variety of reasons as specified in the
1758 * OpenGL Shading Language Specification, as well as any of the
1759 * following reasons:
1760 *
1761 * - No shader objects are attached to program."
1762 *
1763 * The Compatibility Profile specification does not list the error. In
1764 * Compatibility Profile missing shader stages are replaced by
1765 * fixed-function. This applies to the case where all stages are
1766 * missing.
1767 */
1768 if (prog->NumShaders == 0) {
1769 if (ctx->API != API_OPENGL_COMPAT)
1770 linker_error(prog, "no shaders attached to the program\n");
1771 return;
1772 }
1773
1774 #ifdef ENABLE_SHADER_CACHE
1775 if (shader_cache_read_program_metadata(ctx, prog))
1776 return;
1777 #endif
1778
1779 void *mem_ctx = ralloc_context(NULL); // temporary linker context
1780
1781 /* Separate the shaders into groups based on their type.
1782 */
1783 struct gl_shader **shader_list[MESA_SHADER_STAGES];
1784 unsigned num_shaders[MESA_SHADER_STAGES];
1785
1786 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1787 shader_list[i] = (struct gl_shader **)
1788 calloc(prog->NumShaders, sizeof(struct gl_shader *));
1789 num_shaders[i] = 0;
1790 }
1791
1792 unsigned min_version = UINT_MAX;
1793 unsigned max_version = 0;
1794 for (unsigned i = 0; i < prog->NumShaders; i++) {
1795 min_version = MIN2(min_version, prog->Shaders[i]->Version);
1796 max_version = MAX2(max_version, prog->Shaders[i]->Version);
1797
1798 if (!consts->AllowGLSLRelaxedES &&
1799 prog->Shaders[i]->IsES != prog->Shaders[0]->IsES) {
1800 linker_error(prog, "all shaders must use same shading "
1801 "language version\n");
1802 goto done;
1803 }
1804
1805 gl_shader_stage shader_type = prog->Shaders[i]->Stage;
1806 shader_list[shader_type][num_shaders[shader_type]] = prog->Shaders[i];
1807 num_shaders[shader_type]++;
1808 }
1809
1810 /* In desktop GLSL, different shader versions may be linked together. In
1811 * GLSL ES, all shader versions must be the same.
1812 */
1813 if (!consts->AllowGLSLRelaxedES && prog->Shaders[0]->IsES &&
1814 min_version != max_version) {
1815 linker_error(prog, "all shaders must use same shading "
1816 "language version\n");
1817 goto done;
1818 }
1819
1820 prog->GLSL_Version = max_version;
1821 prog->IsES = prog->Shaders[0]->IsES;
1822
1823 /* Some shaders have to be linked with some other shaders present.
1824 */
1825 if (!prog->SeparateShader) {
1826 if (num_shaders[MESA_SHADER_GEOMETRY] > 0 &&
1827 num_shaders[MESA_SHADER_VERTEX] == 0) {
1828 linker_error(prog, "Geometry shader must be linked with "
1829 "vertex shader\n");
1830 goto done;
1831 }
1832 if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 &&
1833 num_shaders[MESA_SHADER_VERTEX] == 0) {
1834 linker_error(prog, "Tessellation evaluation shader must be linked "
1835 "with vertex shader\n");
1836 goto done;
1837 }
1838 if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
1839 num_shaders[MESA_SHADER_VERTEX] == 0) {
1840 linker_error(prog, "Tessellation control shader must be linked with "
1841 "vertex shader\n");
1842 goto done;
1843 }
1844
1845 /* Section 7.3 of the OpenGL ES 3.2 specification says:
1846 *
1847 * "Linking can fail for [...] any of the following reasons:
1848 *
1849 * * program contains an object to form a tessellation control
1850 * shader [...] and [...] the program is not separable and
1851 * contains no object to form a tessellation evaluation shader"
1852 *
1853 * The OpenGL spec is contradictory. It allows linking without a tess
1854 * eval shader, but that can only be used with transform feedback and
1855 * rasterization disabled. However, transform feedback isn't allowed
1856 * with GL_PATCHES, so it can't be used.
1857 *
1858 * More investigation showed that the idea of transform feedback after
1859 * a tess control shader was dropped, because some hw vendors couldn't
1860 * support tessellation without a tess eval shader, but the linker
1861 * section wasn't updated to reflect that.
1862 *
1863 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
1864 * spec bug.
1865 *
1866 * Do what's reasonable and always require a tess eval shader if a tess
1867 * control shader is present.
1868 */
1869 if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
1870 num_shaders[MESA_SHADER_TESS_EVAL] == 0) {
1871 linker_error(prog, "Tessellation control shader must be linked with "
1872 "tessellation evaluation shader\n");
1873 goto done;
1874 }
1875
1876 if (prog->IsES) {
1877 if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 &&
1878 num_shaders[MESA_SHADER_TESS_CTRL] == 0) {
1879 linker_error(prog, "GLSL ES requires non-separable programs "
1880 "containing a tessellation evaluation shader to also "
1881 "be linked with a tessellation control shader\n");
1882 goto done;
1883 }
1884 }
1885 }
1886
1887 /* Compute shaders have additional restrictions. */
1888 if (num_shaders[MESA_SHADER_COMPUTE] > 0 &&
1889 num_shaders[MESA_SHADER_COMPUTE] != prog->NumShaders) {
1890 linker_error(prog, "Compute shaders may not be linked with any other "
1891 "type of shader\n");
1892 }
1893
1894 /* Link all shaders for a particular stage and validate the result.
1895 */
1896 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1897 if (num_shaders[stage] > 0) {
1898 gl_linked_shader *const sh =
1899 link_intrastage_shaders(mem_ctx, ctx, prog, shader_list[stage],
1900 num_shaders[stage], false);
1901
1902 if (!prog->data->LinkStatus) {
1903 if (sh)
1904 _mesa_delete_linked_shader(ctx, sh);
1905 goto done;
1906 }
1907
1908 prog->_LinkedShaders[stage] = sh;
1909 prog->data->linked_stages |= 1 << stage;
1910 }
1911 }
1912
1913 done:
1914 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1915 free(shader_list[i]);
1916 if (prog->_LinkedShaders[i] == NULL)
1917 continue;
1918
1919 /* Do a final validation step to make sure that the IR wasn't
1920 * invalidated by any modifications performed after intrastage linking.
1921 */
1922 validate_ir_tree(prog->_LinkedShaders[i]->ir);
1923
1924 /* Retain any live IR, but trash the rest. */
1925 reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
1926
1927 /* The symbol table in the linked shaders may contain references to
1928 * variables that were removed (e.g., unused uniforms). Since it may
1929 * contain junk, there is no possible valid use. Delete it and set the
1930 * pointer to NULL.
1931 */
1932 delete prog->_LinkedShaders[i]->symbols;
1933 prog->_LinkedShaders[i]->symbols = NULL;
1934 }
1935
1936 ralloc_free(mem_ctx);
1937 }
1938
1939 void
resource_name_updated(struct gl_resource_name * name)1940 resource_name_updated(struct gl_resource_name *name)
1941 {
1942 if (name->string) {
1943 name->length = strlen(name->string);
1944
1945 const char *last_square_bracket = strrchr(name->string, '[');
1946 if (last_square_bracket) {
1947 name->last_square_bracket = last_square_bracket - name->string;
1948 name->suffix_is_zero_square_bracketed =
1949 strcmp(last_square_bracket, "[0]") == 0;
1950 } else {
1951 name->last_square_bracket = -1;
1952 name->suffix_is_zero_square_bracketed = false;
1953 }
1954 } else {
1955 name->length = 0;
1956 name->last_square_bracket = -1;
1957 name->suffix_is_zero_square_bracketed = false;
1958 }
1959 }
1960