2 * Copyright © 2010 Intel Corporation
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:
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
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.
26 * GLSL linker implementation
28 * Given a set of shaders that are to be linked to generate a final program,
29 * there are three distinct stages.
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
35 * - Undefined references in each shader are resolve to definitions in
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.
42 * The result, in the terminology of the GLSL spec, is a set of shader
43 * executables for each processing unit.
45 * After the first stage is complete, a series of semantic checks are performed
46 * on each of the shader executables.
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
53 * In the final stage individual shader executables are linked to create a
54 * complete exectuable.
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.
64 * \author Ian Romanick <ian.d.romanick@intel.com>
67 #include "main/core.h"
68 #include "glsl_symbol_table.h"
69 #include "glsl_parser_extras.h"
72 #include "program/hash_table.h"
74 #include "link_varyings.h"
75 #include "ir_optimization.h"
76 #include "ir_rvalue_visitor.h"
79 #include "main/shaderobj.h"
80 #include "main/enums.h"
83 void linker_error(gl_shader_program
*, const char *, ...);
88 * Visitor that determines whether or not a variable is ever written.
90 class find_assignment_visitor
: public ir_hierarchical_visitor
{
92 find_assignment_visitor(const char *name
)
93 : name(name
), found(false)
98 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
)
100 ir_variable
*const var
= ir
->lhs
->variable_referenced();
102 if (strcmp(name
, var
->name
) == 0) {
107 return visit_continue_with_parent
;
110 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
112 exec_list_iterator sig_iter
= ir
->callee
->parameters
.iterator();
113 foreach_iter(exec_list_iterator
, iter
, *ir
) {
114 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
115 ir_variable
*sig_param
= (ir_variable
*)sig_iter
.get();
117 if (sig_param
->data
.mode
== ir_var_function_out
||
118 sig_param
->data
.mode
== ir_var_function_inout
) {
119 ir_variable
*var
= param_rval
->variable_referenced();
120 if (var
&& strcmp(name
, var
->name
) == 0) {
128 if (ir
->return_deref
!= NULL
) {
129 ir_variable
*const var
= ir
->return_deref
->variable_referenced();
131 if (strcmp(name
, var
->name
) == 0) {
137 return visit_continue_with_parent
;
140 bool variable_found()
146 const char *name
; /**< Find writes to a variable with this name. */
147 bool found
; /**< Was a write to the variable found? */
152 * Visitor that determines whether or not a variable is ever read.
154 class find_deref_visitor
: public ir_hierarchical_visitor
{
156 find_deref_visitor(const char *name
)
157 : name(name
), found(false)
162 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
164 if (strcmp(this->name
, ir
->var
->name
) == 0) {
169 return visit_continue
;
172 bool variable_found() const
178 const char *name
; /**< Find writes to a variable with this name. */
179 bool found
; /**< Was a write to the variable found? */
183 class geom_array_resize_visitor
: public ir_hierarchical_visitor
{
185 unsigned num_vertices
;
186 gl_shader_program
*prog
;
188 geom_array_resize_visitor(unsigned num_vertices
, gl_shader_program
*prog
)
190 this->num_vertices
= num_vertices
;
194 virtual ~geom_array_resize_visitor()
199 virtual ir_visitor_status
visit(ir_variable
*var
)
201 if (!var
->type
->is_array() || var
->data
.mode
!= ir_var_shader_in
)
202 return visit_continue
;
204 unsigned size
= var
->type
->length
;
206 /* Generate a link error if the shader has declared this array with an
209 if (size
&& size
!= this->num_vertices
) {
210 linker_error(this->prog
, "size of array %s declared as %u, "
211 "but number of input vertices is %u\n",
212 var
->name
, size
, this->num_vertices
);
213 return visit_continue
;
216 /* Generate a link error if the shader attempts to access an input
217 * array using an index too large for its actual size assigned at link
220 if (var
->data
.max_array_access
>= this->num_vertices
) {
221 linker_error(this->prog
, "geometry shader accesses element %i of "
222 "%s, but only %i input vertices\n",
223 var
->data
.max_array_access
, var
->name
, this->num_vertices
);
224 return visit_continue
;
227 var
->type
= glsl_type::get_array_instance(var
->type
->element_type(),
229 var
->data
.max_array_access
= this->num_vertices
- 1;
231 return visit_continue
;
234 /* Dereferences of input variables need to be updated so that their type
235 * matches the newly assigned type of the variable they are accessing. */
236 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
238 ir
->type
= ir
->var
->type
;
239 return visit_continue
;
242 /* Dereferences of 2D input arrays need to be updated so that their type
243 * matches the newly assigned type of the array they are accessing. */
244 virtual ir_visitor_status
visit_leave(ir_dereference_array
*ir
)
246 const glsl_type
*const vt
= ir
->array
->type
;
248 ir
->type
= vt
->element_type();
249 return visit_continue
;
255 * Visitor that determines whether or not a shader uses ir_end_primitive.
257 class find_end_primitive_visitor
: public ir_hierarchical_visitor
{
259 find_end_primitive_visitor()
265 virtual ir_visitor_status
visit(ir_end_primitive
*)
271 bool end_primitive_found()
280 } /* anonymous namespace */
283 linker_error(gl_shader_program
*prog
, const char *fmt
, ...)
287 ralloc_strcat(&prog
->InfoLog
, "error: ");
289 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
292 prog
->LinkStatus
= false;
297 linker_warning(gl_shader_program
*prog
, const char *fmt
, ...)
301 ralloc_strcat(&prog
->InfoLog
, "error: ");
303 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
310 * Given a string identifying a program resource, break it into a base name
311 * and an optional array index in square brackets.
313 * If an array index is present, \c out_base_name_end is set to point to the
314 * "[" that precedes the array index, and the array index itself is returned
317 * If no array index is present (or if the array index is negative or
318 * mal-formed), \c out_base_name_end, is set to point to the null terminator
319 * at the end of the input string, and -1 is returned.
321 * Only the final array index is parsed; if the string contains other array
322 * indices (or structure field accesses), they are left in the base name.
324 * No attempt is made to check that the base name is properly formed;
325 * typically the caller will look up the base name in a hash table, so
326 * ill-formed base names simply turn into hash table lookup failures.
329 parse_program_resource_name(const GLchar
*name
,
330 const GLchar
**out_base_name_end
)
332 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
334 * "When an integer array element or block instance number is part of
335 * the name string, it will be specified in decimal form without a "+"
336 * or "-" sign or any extra leading zeroes. Additionally, the name
337 * string will not include white space anywhere in the string."
340 const size_t len
= strlen(name
);
341 *out_base_name_end
= name
+ len
;
343 if (len
== 0 || name
[len
-1] != ']')
346 /* Walk backwards over the string looking for a non-digit character. This
347 * had better be the opening bracket for an array index.
349 * Initially, i specifies the location of the ']'. Since the string may
350 * contain only the ']' charcater, walk backwards very carefully.
353 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
356 if ((i
== 0) || name
[i
-1] != '[')
359 long array_index
= strtol(&name
[i
], NULL
, 10);
363 *out_base_name_end
= name
+ (i
- 1);
369 link_invalidate_variable_locations(exec_list
*ir
)
371 foreach_list(node
, ir
) {
372 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
377 /* Only assign locations for variables that lack an explicit location.
378 * Explicit locations are set for all built-in variables, generic vertex
379 * shader inputs (via layout(location=...)), and generic fragment shader
380 * outputs (also via layout(location=...)).
382 if (!var
->data
.explicit_location
) {
383 var
->data
.location
= -1;
384 var
->data
.location_frac
= 0;
387 /* ir_variable::is_unmatched_generic_inout is used by the linker while
388 * connecting outputs from one stage to inputs of the next stage.
390 * There are two implicit assumptions here. First, we assume that any
391 * built-in variable (i.e., non-generic in or out) will have
392 * explicit_location set. Second, we assume that any generic in or out
393 * will not have explicit_location set.
395 * This second assumption will only be valid until
396 * GL_ARB_separate_shader_objects is supported. When that extension is
397 * implemented, this function will need some modifications.
399 if (!var
->data
.explicit_location
) {
400 var
->data
.is_unmatched_generic_inout
= 1;
402 var
->data
.is_unmatched_generic_inout
= 0;
409 * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
411 * Also check for errors based on incorrect usage of gl_ClipVertex and
414 * Return false if an error was reported.
417 analyze_clip_usage(struct gl_shader_program
*prog
,
418 struct gl_shader
*shader
, GLboolean
*UsesClipDistance
,
419 GLuint
*ClipDistanceArraySize
)
421 *ClipDistanceArraySize
= 0;
423 if (!prog
->IsES
&& prog
->Version
>= 130) {
424 /* From section 7.1 (Vertex Shader Special Variables) of the
427 * "It is an error for a shader to statically write both
428 * gl_ClipVertex and gl_ClipDistance."
430 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
431 * gl_ClipVertex nor gl_ClipDistance.
433 find_assignment_visitor
clip_vertex("gl_ClipVertex");
434 find_assignment_visitor
clip_distance("gl_ClipDistance");
436 clip_vertex
.run(shader
->ir
);
437 clip_distance
.run(shader
->ir
);
438 if (clip_vertex
.variable_found() && clip_distance
.variable_found()) {
439 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
440 "and `gl_ClipDistance'\n",
441 _mesa_shader_stage_to_string(shader
->Stage
));
444 *UsesClipDistance
= clip_distance
.variable_found();
445 ir_variable
*clip_distance_var
=
446 shader
->symbols
->get_variable("gl_ClipDistance");
447 if (clip_distance_var
)
448 *ClipDistanceArraySize
= clip_distance_var
->type
->length
;
450 *UsesClipDistance
= false;
456 * Verify that a vertex shader executable meets all semantic requirements.
458 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
461 * \param shader Vertex shader executable to be verified
464 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
465 struct gl_shader
*shader
)
470 /* From the GLSL 1.10 spec, page 48:
472 * "The variable gl_Position is available only in the vertex
473 * language and is intended for writing the homogeneous vertex
474 * position. All executions of a well-formed vertex shader
475 * executable must write a value into this variable. [...] The
476 * variable gl_Position is available only in the vertex
477 * language and is intended for writing the homogeneous vertex
478 * position. All executions of a well-formed vertex shader
479 * executable must write a value into this variable."
481 * while in GLSL 1.40 this text is changed to:
483 * "The variable gl_Position is available only in the vertex
484 * language and is intended for writing the homogeneous vertex
485 * position. It can be written at any time during shader
486 * execution. It may also be read back by a vertex shader
487 * after being written. This value will be used by primitive
488 * assembly, clipping, culling, and other fixed functionality
489 * operations, if present, that operate on primitives after
490 * vertex processing has occurred. Its value is undefined if
491 * the vertex shader executable does not write gl_Position."
493 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
496 if (prog
->Version
< (prog
->IsES
? 300 : 140)) {
497 find_assignment_visitor
find("gl_Position");
498 find
.run(shader
->ir
);
499 if (!find
.variable_found()) {
500 linker_error(prog
, "vertex shader does not write to `gl_Position'\n");
505 analyze_clip_usage(prog
, shader
, &prog
->Vert
.UsesClipDistance
,
506 &prog
->Vert
.ClipDistanceArraySize
);
511 * Verify that a fragment shader executable meets all semantic requirements
513 * \param shader Fragment shader executable to be verified
516 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
517 struct gl_shader
*shader
)
522 find_assignment_visitor
frag_color("gl_FragColor");
523 find_assignment_visitor
frag_data("gl_FragData");
525 frag_color
.run(shader
->ir
);
526 frag_data
.run(shader
->ir
);
528 if (frag_color
.variable_found() && frag_data
.variable_found()) {
529 linker_error(prog
, "fragment shader writes to both "
530 "`gl_FragColor' and `gl_FragData'\n");
535 * Verify that a geometry shader executable meets all semantic requirements
537 * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
538 * prog->Geom.ClipDistanceArraySize as a side effect.
540 * \param shader Geometry shader executable to be verified
543 validate_geometry_shader_executable(struct gl_shader_program
*prog
,
544 struct gl_shader
*shader
)
549 unsigned num_vertices
= vertices_per_prim(prog
->Geom
.InputType
);
550 prog
->Geom
.VerticesIn
= num_vertices
;
552 analyze_clip_usage(prog
, shader
, &prog
->Geom
.UsesClipDistance
,
553 &prog
->Geom
.ClipDistanceArraySize
);
555 find_end_primitive_visitor end_primitive
;
556 end_primitive
.run(shader
->ir
);
557 prog
->Geom
.UsesEndPrimitive
= end_primitive
.end_primitive_found();
562 * Perform validation of global variables used across multiple shaders
565 cross_validate_globals(struct gl_shader_program
*prog
,
566 struct gl_shader
**shader_list
,
567 unsigned num_shaders
,
570 /* Examine all of the uniforms in all of the shaders and cross validate
573 glsl_symbol_table variables
;
574 for (unsigned i
= 0; i
< num_shaders
; i
++) {
575 if (shader_list
[i
] == NULL
)
578 foreach_list(node
, shader_list
[i
]->ir
) {
579 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
584 if (uniforms_only
&& (var
->data
.mode
!= ir_var_uniform
))
587 /* Don't cross validate temporaries that are at global scope. These
588 * will eventually get pulled into the shaders 'main'.
590 if (var
->data
.mode
== ir_var_temporary
)
593 /* If a global with this name has already been seen, verify that the
594 * new instance has the same type. In addition, if the globals have
595 * initializers, the values of the initializers must be the same.
597 ir_variable
*const existing
= variables
.get_variable(var
->name
);
598 if (existing
!= NULL
) {
599 if (var
->type
!= existing
->type
) {
600 /* Consider the types to be "the same" if both types are arrays
601 * of the same type and one of the arrays is implicitly sized.
602 * In addition, set the type of the linked variable to the
603 * explicitly sized array.
605 if (var
->type
->is_array()
606 && existing
->type
->is_array()
607 && (var
->type
->fields
.array
== existing
->type
->fields
.array
)
608 && ((var
->type
->length
== 0)
609 || (existing
->type
->length
== 0))) {
610 if (var
->type
->length
!= 0) {
611 existing
->type
= var
->type
;
614 linker_error(prog
, "%s `%s' declared as type "
615 "`%s' and type `%s'\n",
617 var
->name
, var
->type
->name
,
618 existing
->type
->name
);
623 if (var
->data
.explicit_location
) {
624 if (existing
->data
.explicit_location
625 && (var
->data
.location
!= existing
->data
.location
)) {
626 linker_error(prog
, "explicit locations for %s "
627 "`%s' have differing values\n",
628 mode_string(var
), var
->name
);
632 existing
->data
.location
= var
->data
.location
;
633 existing
->data
.explicit_location
= true;
636 /* From the GLSL 4.20 specification:
637 * "A link error will result if two compilation units in a program
638 * specify different integer-constant bindings for the same
639 * opaque-uniform name. However, it is not an error to specify a
640 * binding on some but not all declarations for the same name"
642 if (var
->data
.explicit_binding
) {
643 if (existing
->data
.explicit_binding
&&
644 var
->data
.binding
!= existing
->data
.binding
) {
645 linker_error(prog
, "explicit bindings for %s "
646 "`%s' have differing values\n",
647 mode_string(var
), var
->name
);
651 existing
->data
.binding
= var
->data
.binding
;
652 existing
->data
.explicit_binding
= true;
655 if (var
->type
->contains_atomic() &&
656 var
->data
.atomic
.offset
!= existing
->data
.atomic
.offset
) {
657 linker_error(prog
, "offset specifications for %s "
658 "`%s' have differing values\n",
659 mode_string(var
), var
->name
);
663 /* Validate layout qualifiers for gl_FragDepth.
665 * From the AMD/ARB_conservative_depth specs:
667 * "If gl_FragDepth is redeclared in any fragment shader in a
668 * program, it must be redeclared in all fragment shaders in
669 * that program that have static assignments to
670 * gl_FragDepth. All redeclarations of gl_FragDepth in all
671 * fragment shaders in a single program must have the same set
674 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
675 bool layout_declared
= var
->data
.depth_layout
!= ir_depth_layout_none
;
676 bool layout_differs
=
677 var
->data
.depth_layout
!= existing
->data
.depth_layout
;
679 if (layout_declared
&& layout_differs
) {
681 "All redeclarations of gl_FragDepth in all "
682 "fragment shaders in a single program must have "
683 "the same set of qualifiers.");
686 if (var
->data
.used
&& layout_differs
) {
688 "If gl_FragDepth is redeclared with a layout "
689 "qualifier in any fragment shader, it must be "
690 "redeclared with the same layout qualifier in "
691 "all fragment shaders that have assignments to "
696 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
698 * "If a shared global has multiple initializers, the
699 * initializers must all be constant expressions, and they
700 * must all have the same value. Otherwise, a link error will
701 * result. (A shared global having only one initializer does
702 * not require that initializer to be a constant expression.)"
704 * Previous to 4.20 the GLSL spec simply said that initializers
705 * must have the same value. In this case of non-constant
706 * initializers, this was impossible to determine. As a result,
707 * no vendor actually implemented that behavior. The 4.20
708 * behavior matches the implemented behavior of at least one other
709 * vendor, so we'll implement that for all GLSL versions.
711 if (var
->constant_initializer
!= NULL
) {
712 if (existing
->constant_initializer
!= NULL
) {
713 if (!var
->constant_initializer
->has_value(existing
->constant_initializer
)) {
714 linker_error(prog
, "initializers for %s "
715 "`%s' have differing values\n",
716 mode_string(var
), var
->name
);
720 /* If the first-seen instance of a particular uniform did not
721 * have an initializer but a later instance does, copy the
722 * initializer to the version stored in the symbol table.
724 /* FINISHME: This is wrong. The constant_value field should
725 * FINISHME: not be modified! Imagine a case where a shader
726 * FINISHME: without an initializer is linked in two different
727 * FINISHME: programs with shaders that have differing
728 * FINISHME: initializers. Linking with the first will
729 * FINISHME: modify the shader, and linking with the second
730 * FINISHME: will fail.
732 existing
->constant_initializer
=
733 var
->constant_initializer
->clone(ralloc_parent(existing
),
738 if (var
->data
.has_initializer
) {
739 if (existing
->data
.has_initializer
740 && (var
->constant_initializer
== NULL
741 || existing
->constant_initializer
== NULL
)) {
743 "shared global variable `%s' has multiple "
744 "non-constant initializers.\n",
749 /* Some instance had an initializer, so keep track of that. In
750 * this location, all sorts of initializers (constant or
751 * otherwise) will propagate the existence to the variable
752 * stored in the symbol table.
754 existing
->data
.has_initializer
= true;
757 if (existing
->data
.invariant
!= var
->data
.invariant
) {
758 linker_error(prog
, "declarations for %s `%s' have "
759 "mismatching invariant qualifiers\n",
760 mode_string(var
), var
->name
);
763 if (existing
->data
.centroid
!= var
->data
.centroid
) {
764 linker_error(prog
, "declarations for %s `%s' have "
765 "mismatching centroid qualifiers\n",
766 mode_string(var
), var
->name
);
769 if (existing
->data
.sample
!= var
->data
.sample
) {
770 linker_error(prog
, "declarations for %s `%s` have "
771 "mismatching sample qualifiers\n",
772 mode_string(var
), var
->name
);
776 variables
.add_variable(var
);
783 * Perform validation of uniforms used across multiple shader stages
786 cross_validate_uniforms(struct gl_shader_program
*prog
)
788 cross_validate_globals(prog
, prog
->_LinkedShaders
,
789 MESA_SHADER_STAGES
, true);
793 * Accumulates the array of prog->UniformBlocks and checks that all
794 * definitons of blocks agree on their contents.
797 interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
)
799 unsigned max_num_uniform_blocks
= 0;
800 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
801 if (prog
->_LinkedShaders
[i
])
802 max_num_uniform_blocks
+= prog
->_LinkedShaders
[i
]->NumUniformBlocks
;
805 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
806 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
808 prog
->UniformBlockStageIndex
[i
] = ralloc_array(prog
, int,
809 max_num_uniform_blocks
);
810 for (unsigned int j
= 0; j
< max_num_uniform_blocks
; j
++)
811 prog
->UniformBlockStageIndex
[i
][j
] = -1;
816 for (unsigned int j
= 0; j
< sh
->NumUniformBlocks
; j
++) {
817 int index
= link_cross_validate_uniform_block(prog
,
818 &prog
->UniformBlocks
,
819 &prog
->NumUniformBlocks
,
820 &sh
->UniformBlocks
[j
]);
823 linker_error(prog
, "uniform block `%s' has mismatching definitions",
824 sh
->UniformBlocks
[j
].Name
);
828 prog
->UniformBlockStageIndex
[i
][index
] = j
;
837 * Populates a shaders symbol table with all global declarations
840 populate_symbol_table(gl_shader
*sh
)
842 sh
->symbols
= new(sh
) glsl_symbol_table
;
844 foreach_list(node
, sh
->ir
) {
845 ir_instruction
*const inst
= (ir_instruction
*) node
;
849 if ((func
= inst
->as_function()) != NULL
) {
850 sh
->symbols
->add_function(func
);
851 } else if ((var
= inst
->as_variable()) != NULL
) {
852 sh
->symbols
->add_variable(var
);
859 * Remap variables referenced in an instruction tree
861 * This is used when instruction trees are cloned from one shader and placed in
862 * another. These trees will contain references to \c ir_variable nodes that
863 * do not exist in the target shader. This function finds these \c ir_variable
864 * references and replaces the references with matching variables in the target
867 * If there is no matching variable in the target shader, a clone of the
868 * \c ir_variable is made and added to the target shader. The new variable is
869 * added to \b both the instruction stream and the symbol table.
871 * \param inst IR tree that is to be processed.
872 * \param symbols Symbol table containing global scope symbols in the
874 * \param instructions Instruction stream where new variable declarations
878 remap_variables(ir_instruction
*inst
, struct gl_shader
*target
,
881 class remap_visitor
: public ir_hierarchical_visitor
{
883 remap_visitor(struct gl_shader
*target
,
886 this->target
= target
;
887 this->symbols
= target
->symbols
;
888 this->instructions
= target
->ir
;
892 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
894 if (ir
->var
->data
.mode
== ir_var_temporary
) {
895 ir_variable
*var
= (ir_variable
*) hash_table_find(temps
, ir
->var
);
899 return visit_continue
;
902 ir_variable
*const existing
=
903 this->symbols
->get_variable(ir
->var
->name
);
904 if (existing
!= NULL
)
907 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
909 this->symbols
->add_variable(copy
);
910 this->instructions
->push_head(copy
);
914 return visit_continue
;
918 struct gl_shader
*target
;
919 glsl_symbol_table
*symbols
;
920 exec_list
*instructions
;
924 remap_visitor
v(target
, temps
);
931 * Move non-declarations from one instruction stream to another
933 * The intended usage pattern of this function is to pass the pointer to the
934 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
935 * pointer) for \c last and \c false for \c make_copies on the first
936 * call. Successive calls pass the return value of the previous call for
937 * \c last and \c true for \c make_copies.
939 * \param instructions Source instruction stream
940 * \param last Instruction after which new instructions should be
941 * inserted in the target instruction stream
942 * \param make_copies Flag selecting whether instructions in \c instructions
943 * should be copied (via \c ir_instruction::clone) into the
944 * target list or moved.
947 * The new "last" instruction in the target instruction stream. This pointer
948 * is suitable for use as the \c last parameter of a later call to this
952 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
953 bool make_copies
, gl_shader
*target
)
955 hash_table
*temps
= NULL
;
958 temps
= hash_table_ctor(0, hash_table_pointer_hash
,
959 hash_table_pointer_compare
);
961 foreach_list_safe(node
, instructions
) {
962 ir_instruction
*inst
= (ir_instruction
*) node
;
964 if (inst
->as_function())
967 ir_variable
*var
= inst
->as_variable();
968 if ((var
!= NULL
) && (var
->data
.mode
!= ir_var_temporary
))
971 assert(inst
->as_assignment()
973 || inst
->as_if() /* for initializers with the ?: operator */
974 || ((var
!= NULL
) && (var
->data
.mode
== ir_var_temporary
)));
977 inst
= inst
->clone(target
, NULL
);
980 hash_table_insert(temps
, inst
, var
);
982 remap_variables(inst
, target
, temps
);
987 last
->insert_after(inst
);
992 hash_table_dtor(temps
);
998 * Get the function signature for main from a shader
1000 static ir_function_signature
*
1001 get_main_function_signature(gl_shader
*sh
)
1003 ir_function
*const f
= sh
->symbols
->get_function("main");
1005 exec_list void_parameters
;
1007 /* Look for the 'void main()' signature and ensure that it's defined.
1008 * This keeps the linker from accidentally pick a shader that just
1009 * contains a prototype for main.
1011 * We don't have to check for multiple definitions of main (in multiple
1012 * shaders) because that would have already been caught above.
1014 ir_function_signature
*sig
= f
->matching_signature(NULL
, &void_parameters
);
1015 if ((sig
!= NULL
) && sig
->is_defined
) {
1025 * This class is only used in link_intrastage_shaders() below but declaring
1026 * it inside that function leads to compiler warnings with some versions of
1029 class array_sizing_visitor
: public ir_hierarchical_visitor
{
1031 array_sizing_visitor()
1032 : mem_ctx(ralloc_context(NULL
)),
1033 unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash
,
1034 hash_table_pointer_compare
))
1038 ~array_sizing_visitor()
1040 hash_table_dtor(this->unnamed_interfaces
);
1041 ralloc_free(this->mem_ctx
);
1044 virtual ir_visitor_status
visit(ir_variable
*var
)
1046 fixup_type(&var
->type
, var
->data
.max_array_access
);
1047 if (var
->type
->is_interface()) {
1048 if (interface_contains_unsized_arrays(var
->type
)) {
1049 const glsl_type
*new_type
=
1050 resize_interface_members(var
->type
, var
->max_ifc_array_access
);
1051 var
->type
= new_type
;
1052 var
->change_interface_type(new_type
);
1054 } else if (var
->type
->is_array() &&
1055 var
->type
->fields
.array
->is_interface()) {
1056 if (interface_contains_unsized_arrays(var
->type
->fields
.array
)) {
1057 const glsl_type
*new_type
=
1058 resize_interface_members(var
->type
->fields
.array
,
1059 var
->max_ifc_array_access
);
1060 var
->change_interface_type(new_type
);
1062 glsl_type::get_array_instance(new_type
, var
->type
->length
);
1064 } else if (const glsl_type
*ifc_type
= var
->get_interface_type()) {
1065 /* Store a pointer to the variable in the unnamed_interfaces
1068 ir_variable
**interface_vars
= (ir_variable
**)
1069 hash_table_find(this->unnamed_interfaces
, ifc_type
);
1070 if (interface_vars
== NULL
) {
1071 interface_vars
= rzalloc_array(mem_ctx
, ir_variable
*,
1073 hash_table_insert(this->unnamed_interfaces
, interface_vars
,
1076 unsigned index
= ifc_type
->field_index(var
->name
);
1077 assert(index
< ifc_type
->length
);
1078 assert(interface_vars
[index
] == NULL
);
1079 interface_vars
[index
] = var
;
1081 return visit_continue
;
1085 * For each unnamed interface block that was discovered while running the
1086 * visitor, adjust the interface type to reflect the newly assigned array
1087 * sizes, and fix up the ir_variable nodes to point to the new interface
1090 void fixup_unnamed_interface_types()
1092 hash_table_call_foreach(this->unnamed_interfaces
,
1093 fixup_unnamed_interface_type
, NULL
);
1098 * If the type pointed to by \c type represents an unsized array, replace
1099 * it with a sized array whose size is determined by max_array_access.
1101 static void fixup_type(const glsl_type
**type
, unsigned max_array_access
)
1103 if ((*type
)->is_unsized_array()) {
1104 *type
= glsl_type::get_array_instance((*type
)->fields
.array
,
1105 max_array_access
+ 1);
1106 assert(*type
!= NULL
);
1111 * Determine whether the given interface type contains unsized arrays (if
1112 * it doesn't, array_sizing_visitor doesn't need to process it).
1114 static bool interface_contains_unsized_arrays(const glsl_type
*type
)
1116 for (unsigned i
= 0; i
< type
->length
; i
++) {
1117 const glsl_type
*elem_type
= type
->fields
.structure
[i
].type
;
1118 if (elem_type
->is_unsized_array())
1125 * Create a new interface type based on the given type, with unsized arrays
1126 * replaced by sized arrays whose size is determined by
1127 * max_ifc_array_access.
1129 static const glsl_type
*
1130 resize_interface_members(const glsl_type
*type
,
1131 const unsigned *max_ifc_array_access
)
1133 unsigned num_fields
= type
->length
;
1134 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1135 memcpy(fields
, type
->fields
.structure
,
1136 num_fields
* sizeof(*fields
));
1137 for (unsigned i
= 0; i
< num_fields
; i
++) {
1138 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
]);
1140 glsl_interface_packing packing
=
1141 (glsl_interface_packing
) type
->interface_packing
;
1142 const glsl_type
*new_ifc_type
=
1143 glsl_type::get_interface_instance(fields
, num_fields
,
1144 packing
, type
->name
);
1146 return new_ifc_type
;
1149 static void fixup_unnamed_interface_type(const void *key
, void *data
,
1152 const glsl_type
*ifc_type
= (const glsl_type
*) key
;
1153 ir_variable
**interface_vars
= (ir_variable
**) data
;
1154 unsigned num_fields
= ifc_type
->length
;
1155 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1156 memcpy(fields
, ifc_type
->fields
.structure
,
1157 num_fields
* sizeof(*fields
));
1158 bool interface_type_changed
= false;
1159 for (unsigned i
= 0; i
< num_fields
; i
++) {
1160 if (interface_vars
[i
] != NULL
&&
1161 fields
[i
].type
!= interface_vars
[i
]->type
) {
1162 fields
[i
].type
= interface_vars
[i
]->type
;
1163 interface_type_changed
= true;
1166 if (!interface_type_changed
) {
1170 glsl_interface_packing packing
=
1171 (glsl_interface_packing
) ifc_type
->interface_packing
;
1172 const glsl_type
*new_ifc_type
=
1173 glsl_type::get_interface_instance(fields
, num_fields
, packing
,
1176 for (unsigned i
= 0; i
< num_fields
; i
++) {
1177 if (interface_vars
[i
] != NULL
)
1178 interface_vars
[i
]->change_interface_type(new_ifc_type
);
1183 * Memory context used to allocate the data in \c unnamed_interfaces.
1188 * Hash table from const glsl_type * to an array of ir_variable *'s
1189 * pointing to the ir_variables constituting each unnamed interface block.
1191 hash_table
*unnamed_interfaces
;
1195 * Performs the cross-validation of geometry shader max_vertices and
1196 * primitive type layout qualifiers for the attached geometry shaders,
1197 * and propagates them to the linked GS and linked shader program.
1200 link_gs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
1201 struct gl_shader
*linked_shader
,
1202 struct gl_shader
**shader_list
,
1203 unsigned num_shaders
)
1205 linked_shader
->Geom
.VerticesOut
= 0;
1206 linked_shader
->Geom
.InputType
= PRIM_UNKNOWN
;
1207 linked_shader
->Geom
.OutputType
= PRIM_UNKNOWN
;
1209 /* No in/out qualifiers defined for anything but GLSL 1.50+
1210 * geometry shaders so far.
1212 if (linked_shader
->Stage
!= MESA_SHADER_GEOMETRY
|| prog
->Version
< 150)
1215 /* From the GLSL 1.50 spec, page 46:
1217 * "All geometry shader output layout declarations in a program
1218 * must declare the same layout and same value for
1219 * max_vertices. There must be at least one geometry output
1220 * layout declaration somewhere in a program, but not all
1221 * geometry shaders (compilation units) are required to
1225 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1226 struct gl_shader
*shader
= shader_list
[i
];
1228 if (shader
->Geom
.InputType
!= PRIM_UNKNOWN
) {
1229 if (linked_shader
->Geom
.InputType
!= PRIM_UNKNOWN
&&
1230 linked_shader
->Geom
.InputType
!= shader
->Geom
.InputType
) {
1231 linker_error(prog
, "geometry shader defined with conflicting "
1235 linked_shader
->Geom
.InputType
= shader
->Geom
.InputType
;
1238 if (shader
->Geom
.OutputType
!= PRIM_UNKNOWN
) {
1239 if (linked_shader
->Geom
.OutputType
!= PRIM_UNKNOWN
&&
1240 linked_shader
->Geom
.OutputType
!= shader
->Geom
.OutputType
) {
1241 linker_error(prog
, "geometry shader defined with conflicting "
1245 linked_shader
->Geom
.OutputType
= shader
->Geom
.OutputType
;
1248 if (shader
->Geom
.VerticesOut
!= 0) {
1249 if (linked_shader
->Geom
.VerticesOut
!= 0 &&
1250 linked_shader
->Geom
.VerticesOut
!= shader
->Geom
.VerticesOut
) {
1251 linker_error(prog
, "geometry shader defined with conflicting "
1252 "output vertex count (%d and %d)\n",
1253 linked_shader
->Geom
.VerticesOut
,
1254 shader
->Geom
.VerticesOut
);
1257 linked_shader
->Geom
.VerticesOut
= shader
->Geom
.VerticesOut
;
1261 /* Just do the intrastage -> interstage propagation right now,
1262 * since we already know we're in the right type of shader program
1265 if (linked_shader
->Geom
.InputType
== PRIM_UNKNOWN
) {
1267 "geometry shader didn't declare primitive input type\n");
1270 prog
->Geom
.InputType
= linked_shader
->Geom
.InputType
;
1272 if (linked_shader
->Geom
.OutputType
== PRIM_UNKNOWN
) {
1274 "geometry shader didn't declare primitive output type\n");
1277 prog
->Geom
.OutputType
= linked_shader
->Geom
.OutputType
;
1279 if (linked_shader
->Geom
.VerticesOut
== 0) {
1281 "geometry shader didn't declare max_vertices\n");
1284 prog
->Geom
.VerticesOut
= linked_shader
->Geom
.VerticesOut
;
1288 * Combine a group of shaders for a single stage to generate a linked shader
1291 * If this function is supplied a single shader, it is cloned, and the new
1292 * shader is returned.
1294 static struct gl_shader
*
1295 link_intrastage_shaders(void *mem_ctx
,
1296 struct gl_context
*ctx
,
1297 struct gl_shader_program
*prog
,
1298 struct gl_shader
**shader_list
,
1299 unsigned num_shaders
)
1301 struct gl_uniform_block
*uniform_blocks
= NULL
;
1303 /* Check that global variables defined in multiple shaders are consistent.
1305 cross_validate_globals(prog
, shader_list
, num_shaders
, false);
1306 if (!prog
->LinkStatus
)
1309 /* Check that interface blocks defined in multiple shaders are consistent.
1311 validate_intrastage_interface_blocks(prog
, (const gl_shader
**)shader_list
,
1313 if (!prog
->LinkStatus
)
1316 /* Link up uniform blocks defined within this stage. */
1317 const unsigned num_uniform_blocks
=
1318 link_uniform_blocks(mem_ctx
, prog
, shader_list
, num_shaders
,
1321 /* Check that there is only a single definition of each function signature
1322 * across all shaders.
1324 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
1325 foreach_list(node
, shader_list
[i
]->ir
) {
1326 ir_function
*const f
= ((ir_instruction
*) node
)->as_function();
1331 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
1332 ir_function
*const other
=
1333 shader_list
[j
]->symbols
->get_function(f
->name
);
1335 /* If the other shader has no function (and therefore no function
1336 * signatures) with the same name, skip to the next shader.
1341 foreach_list(n
, &f
->signatures
) {
1342 ir_function_signature
*sig
= (ir_function_signature
*) n
;
1344 if (!sig
->is_defined
|| sig
->is_builtin())
1347 ir_function_signature
*other_sig
=
1348 other
->exact_matching_signature(NULL
, &sig
->parameters
);
1350 if ((other_sig
!= NULL
) && other_sig
->is_defined
1351 && !other_sig
->is_builtin()) {
1352 linker_error(prog
, "function `%s' is multiply defined",
1361 /* Find the shader that defines main, and make a clone of it.
1363 * Starting with the clone, search for undefined references. If one is
1364 * found, find the shader that defines it. Clone the reference and add
1365 * it to the shader. Repeat until there are no undefined references or
1366 * until a reference cannot be resolved.
1368 gl_shader
*main
= NULL
;
1369 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1370 if (get_main_function_signature(shader_list
[i
]) != NULL
) {
1371 main
= shader_list
[i
];
1377 linker_error(prog
, "%s shader lacks `main'\n",
1378 _mesa_shader_stage_to_string(shader_list
[0]->Stage
));
1382 gl_shader
*linked
= ctx
->Driver
.NewShader(NULL
, 0, main
->Type
);
1383 linked
->ir
= new(linked
) exec_list
;
1384 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
1386 linked
->UniformBlocks
= uniform_blocks
;
1387 linked
->NumUniformBlocks
= num_uniform_blocks
;
1388 ralloc_steal(linked
, linked
->UniformBlocks
);
1390 link_gs_inout_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
1392 populate_symbol_table(linked
);
1394 /* The a pointer to the main function in the final linked shader (i.e., the
1395 * copy of the original shader that contained the main function).
1397 ir_function_signature
*const main_sig
= get_main_function_signature(linked
);
1399 /* Move any instructions other than variable declarations or function
1400 * declarations into main.
1402 exec_node
*insertion_point
=
1403 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
1406 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1407 if (shader_list
[i
] == main
)
1410 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
1411 insertion_point
, true, linked
);
1414 /* Check if any shader needs built-in functions. */
1415 bool need_builtins
= false;
1416 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1417 if (shader_list
[i
]->uses_builtin_functions
) {
1418 need_builtins
= true;
1424 if (need_builtins
) {
1425 /* Make a temporary array one larger than shader_list, which will hold
1426 * the built-in function shader as well.
1428 gl_shader
**linking_shaders
= (gl_shader
**)
1429 calloc(num_shaders
+ 1, sizeof(gl_shader
*));
1430 memcpy(linking_shaders
, shader_list
, num_shaders
* sizeof(gl_shader
*));
1431 linking_shaders
[num_shaders
] = _mesa_glsl_get_builtin_function_shader();
1433 ok
= link_function_calls(prog
, linked
, linking_shaders
, num_shaders
+ 1);
1435 free(linking_shaders
);
1437 ok
= link_function_calls(prog
, linked
, shader_list
, num_shaders
);
1442 ctx
->Driver
.DeleteShader(ctx
, linked
);
1446 /* At this point linked should contain all of the linked IR, so
1447 * validate it to make sure nothing went wrong.
1449 validate_ir_tree(linked
->ir
);
1451 /* Set the size of geometry shader input arrays */
1452 if (linked
->Stage
== MESA_SHADER_GEOMETRY
) {
1453 unsigned num_vertices
= vertices_per_prim(prog
->Geom
.InputType
);
1454 geom_array_resize_visitor
input_resize_visitor(num_vertices
, prog
);
1455 foreach_list(n
, linked
->ir
) {
1456 ir_instruction
*ir
= (ir_instruction
*) n
;
1457 ir
->accept(&input_resize_visitor
);
1461 /* Make a pass over all variable declarations to ensure that arrays with
1462 * unspecified sizes have a size specified. The size is inferred from the
1463 * max_array_access field.
1465 array_sizing_visitor v
;
1467 v
.fixup_unnamed_interface_types();
1473 * Update the sizes of linked shader uniform arrays to the maximum
1476 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1478 * If one or more elements of an array are active,
1479 * GetActiveUniform will return the name of the array in name,
1480 * subject to the restrictions listed above. The type of the array
1481 * is returned in type. The size parameter contains the highest
1482 * array element index used, plus one. The compiler or linker
1483 * determines the highest index used. There will be only one
1484 * active uniform reported by the GL per uniform array.
1488 update_array_sizes(struct gl_shader_program
*prog
)
1490 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1491 if (prog
->_LinkedShaders
[i
] == NULL
)
1494 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
1495 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1497 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
) ||
1498 !var
->type
->is_array())
1501 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1502 * will not be eliminated. Since we always do std140, just
1503 * don't resize arrays in UBOs.
1505 * Atomic counters are supposed to get deterministic
1506 * locations assigned based on the declaration ordering and
1507 * sizes, array compaction would mess that up.
1509 if (var
->is_in_uniform_block() || var
->type
->contains_atomic())
1512 unsigned int size
= var
->data
.max_array_access
;
1513 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1514 if (prog
->_LinkedShaders
[j
] == NULL
)
1517 foreach_list(node2
, prog
->_LinkedShaders
[j
]->ir
) {
1518 ir_variable
*other_var
= ((ir_instruction
*) node2
)->as_variable();
1522 if (strcmp(var
->name
, other_var
->name
) == 0 &&
1523 other_var
->data
.max_array_access
> size
) {
1524 size
= other_var
->data
.max_array_access
;
1529 if (size
+ 1 != var
->type
->length
) {
1530 /* If this is a built-in uniform (i.e., it's backed by some
1531 * fixed-function state), adjust the number of state slots to
1532 * match the new array size. The number of slots per array entry
1533 * is not known. It seems safe to assume that the total number of
1534 * slots is an integer multiple of the number of array elements.
1535 * Determine the number of slots per array element by dividing by
1536 * the old (total) size.
1538 if (var
->num_state_slots
> 0) {
1539 var
->num_state_slots
= (size
+ 1)
1540 * (var
->num_state_slots
/ var
->type
->length
);
1543 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
1545 /* FINISHME: We should update the types of array
1546 * dereferences of this variable now.
1554 * Find a contiguous set of available bits in a bitmask.
1556 * \param used_mask Bits representing used (1) and unused (0) locations
1557 * \param needed_count Number of contiguous bits needed.
1560 * Base location of the available bits on success or -1 on failure.
1563 find_available_slots(unsigned used_mask
, unsigned needed_count
)
1565 unsigned needed_mask
= (1 << needed_count
) - 1;
1566 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
1568 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1569 * cannot optimize possibly infinite loops" for the loop below.
1571 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
1574 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
1575 if ((needed_mask
& ~used_mask
) == needed_mask
)
1586 * Assign locations for either VS inputs for FS outputs
1588 * \param prog Shader program whose variables need locations assigned
1589 * \param target_index Selector for the program target to receive location
1590 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1591 * \c MESA_SHADER_FRAGMENT.
1592 * \param max_index Maximum number of generic locations. This corresponds
1593 * to either the maximum number of draw buffers or the
1594 * maximum number of generic attributes.
1597 * If locations are successfully assigned, true is returned. Otherwise an
1598 * error is emitted to the shader link log and false is returned.
1601 assign_attribute_or_color_locations(gl_shader_program
*prog
,
1602 unsigned target_index
,
1605 /* Mark invalid locations as being used.
1607 unsigned used_locations
= (max_index
>= 32)
1608 ? ~0 : ~((1 << max_index
) - 1);
1610 assert((target_index
== MESA_SHADER_VERTEX
)
1611 || (target_index
== MESA_SHADER_FRAGMENT
));
1613 gl_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
1617 /* Operate in a total of four passes.
1619 * 1. Invalidate the location assignments for all vertex shader inputs.
1621 * 2. Assign locations for inputs that have user-defined (via
1622 * glBindVertexAttribLocation) locations and outputs that have
1623 * user-defined locations (via glBindFragDataLocation).
1625 * 3. Sort the attributes without assigned locations by number of slots
1626 * required in decreasing order. Fragmentation caused by attribute
1627 * locations assigned by the application may prevent large attributes
1628 * from having enough contiguous space.
1630 * 4. Assign locations to any inputs without assigned locations.
1633 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
1634 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
1636 const enum ir_variable_mode direction
=
1637 (target_index
== MESA_SHADER_VERTEX
)
1638 ? ir_var_shader_in
: ir_var_shader_out
;
1641 /* Temporary storage for the set of attributes that need locations assigned.
1647 /* Used below in the call to qsort. */
1648 static int compare(const void *a
, const void *b
)
1650 const temp_attr
*const l
= (const temp_attr
*) a
;
1651 const temp_attr
*const r
= (const temp_attr
*) b
;
1653 /* Reversed because we want a descending order sort below. */
1654 return r
->slots
- l
->slots
;
1658 unsigned num_attr
= 0;
1660 foreach_list(node
, sh
->ir
) {
1661 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1663 if ((var
== NULL
) || (var
->data
.mode
!= (unsigned) direction
))
1666 if (var
->data
.explicit_location
) {
1667 if ((var
->data
.location
>= (int)(max_index
+ generic_base
))
1668 || (var
->data
.location
< 0)) {
1670 "invalid explicit location %d specified for `%s'\n",
1671 (var
->data
.location
< 0)
1672 ? var
->data
.location
1673 : var
->data
.location
- generic_base
,
1677 } else if (target_index
== MESA_SHADER_VERTEX
) {
1680 if (prog
->AttributeBindings
->get(binding
, var
->name
)) {
1681 assert(binding
>= VERT_ATTRIB_GENERIC0
);
1682 var
->data
.location
= binding
;
1683 var
->data
.is_unmatched_generic_inout
= 0;
1685 } else if (target_index
== MESA_SHADER_FRAGMENT
) {
1689 if (prog
->FragDataBindings
->get(binding
, var
->name
)) {
1690 assert(binding
>= FRAG_RESULT_DATA0
);
1691 var
->data
.location
= binding
;
1692 var
->data
.is_unmatched_generic_inout
= 0;
1694 if (prog
->FragDataIndexBindings
->get(index
, var
->name
)) {
1695 var
->data
.index
= index
;
1700 /* If the variable is not a built-in and has a location statically
1701 * assigned in the shader (presumably via a layout qualifier), make sure
1702 * that it doesn't collide with other assigned locations. Otherwise,
1703 * add it to the list of variables that need linker-assigned locations.
1705 const unsigned slots
= var
->type
->count_attribute_slots();
1706 if (var
->data
.location
!= -1) {
1707 if (var
->data
.location
>= generic_base
&& var
->data
.index
< 1) {
1708 /* From page 61 of the OpenGL 4.0 spec:
1710 * "LinkProgram will fail if the attribute bindings assigned
1711 * by BindAttribLocation do not leave not enough space to
1712 * assign a location for an active matrix attribute or an
1713 * active attribute array, both of which require multiple
1714 * contiguous generic attributes."
1716 * Previous versions of the spec contain similar language but omit
1717 * the bit about attribute arrays.
1719 * Page 61 of the OpenGL 4.0 spec also says:
1721 * "It is possible for an application to bind more than one
1722 * attribute name to the same location. This is referred to as
1723 * aliasing. This will only work if only one of the aliased
1724 * attributes is active in the executable program, or if no
1725 * path through the shader consumes more than one attribute of
1726 * a set of attributes aliased to the same location. A link
1727 * error can occur if the linker determines that every path
1728 * through the shader consumes multiple aliased attributes,
1729 * but implementations are not required to generate an error
1732 * These two paragraphs are either somewhat contradictory, or I
1733 * don't fully understand one or both of them.
1735 /* FINISHME: The code as currently written does not support
1736 * FINISHME: attribute location aliasing (see comment above).
1738 /* Mask representing the contiguous slots that will be used by
1741 const unsigned attr
= var
->data
.location
- generic_base
;
1742 const unsigned use_mask
= (1 << slots
) - 1;
1744 /* Generate a link error if the set of bits requested for this
1745 * attribute overlaps any previously allocated bits.
1747 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
1748 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
1749 ? "vertex shader input" : "fragment shader output";
1751 "insufficient contiguous locations "
1752 "available for %s `%s' %d %d %d", string
,
1753 var
->name
, used_locations
, use_mask
, attr
);
1757 used_locations
|= (use_mask
<< attr
);
1763 to_assign
[num_attr
].slots
= slots
;
1764 to_assign
[num_attr
].var
= var
;
1768 /* If all of the attributes were assigned locations by the application (or
1769 * are built-in attributes with fixed locations), return early. This should
1770 * be the common case.
1775 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
1777 if (target_index
== MESA_SHADER_VERTEX
) {
1778 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1779 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1780 * reserved to prevent it from being automatically allocated below.
1782 find_deref_visitor
find("gl_Vertex");
1784 if (find
.variable_found())
1785 used_locations
|= (1 << 0);
1788 for (unsigned i
= 0; i
< num_attr
; i
++) {
1789 /* Mask representing the contiguous slots that will be used by this
1792 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
1794 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
1797 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
1798 ? "vertex shader input" : "fragment shader output";
1801 "insufficient contiguous locations "
1802 "available for %s `%s'",
1803 string
, to_assign
[i
].var
->name
);
1807 to_assign
[i
].var
->data
.location
= generic_base
+ location
;
1808 to_assign
[i
].var
->data
.is_unmatched_generic_inout
= 0;
1809 used_locations
|= (use_mask
<< location
);
1817 * Demote shader inputs and outputs that are not used in other stages
1820 demote_shader_inputs_and_outputs(gl_shader
*sh
, enum ir_variable_mode mode
)
1822 foreach_list(node
, sh
->ir
) {
1823 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1825 if ((var
== NULL
) || (var
->data
.mode
!= int(mode
)))
1828 /* A shader 'in' or 'out' variable is only really an input or output if
1829 * its value is used by other shader stages. This will cause the variable
1830 * to have a location assigned.
1832 if (var
->data
.is_unmatched_generic_inout
) {
1833 var
->data
.mode
= ir_var_auto
;
1840 * Store the gl_FragDepth layout in the gl_shader_program struct.
1843 store_fragdepth_layout(struct gl_shader_program
*prog
)
1845 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
1849 struct exec_list
*ir
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
1851 /* We don't look up the gl_FragDepth symbol directly because if
1852 * gl_FragDepth is not used in the shader, it's removed from the IR.
1853 * However, the symbol won't be removed from the symbol table.
1855 * We're only interested in the cases where the variable is NOT removed
1858 foreach_list(node
, ir
) {
1859 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1861 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
) {
1865 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
1866 switch (var
->data
.depth_layout
) {
1867 case ir_depth_layout_none
:
1868 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
1870 case ir_depth_layout_any
:
1871 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
1873 case ir_depth_layout_greater
:
1874 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
1876 case ir_depth_layout_less
:
1877 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
1879 case ir_depth_layout_unchanged
:
1880 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
1891 * Validate the resources used by a program versus the implementation limits
1894 check_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
1896 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1897 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
1902 if (sh
->num_samplers
> ctx
->Const
.Program
[i
].MaxTextureImageUnits
) {
1903 linker_error(prog
, "Too many %s shader texture samplers",
1904 _mesa_shader_stage_to_string(i
));
1907 if (sh
->num_uniform_components
>
1908 ctx
->Const
.Program
[i
].MaxUniformComponents
) {
1909 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
1910 linker_warning(prog
, "Too many %s shader default uniform block "
1911 "components, but the driver will try to optimize "
1912 "them out; this is non-portable out-of-spec "
1914 _mesa_shader_stage_to_string(i
));
1916 linker_error(prog
, "Too many %s shader default uniform block "
1918 _mesa_shader_stage_to_string(i
));
1922 if (sh
->num_combined_uniform_components
>
1923 ctx
->Const
.Program
[i
].MaxCombinedUniformComponents
) {
1924 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
1925 linker_warning(prog
, "Too many %s shader uniform components, "
1926 "but the driver will try to optimize them out; "
1927 "this is non-portable out-of-spec behavior\n",
1928 _mesa_shader_stage_to_string(i
));
1930 linker_error(prog
, "Too many %s shader uniform components",
1931 _mesa_shader_stage_to_string(i
));
1936 unsigned blocks
[MESA_SHADER_STAGES
] = {0};
1937 unsigned total_uniform_blocks
= 0;
1939 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
1940 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1941 if (prog
->UniformBlockStageIndex
[j
][i
] != -1) {
1943 total_uniform_blocks
++;
1947 if (total_uniform_blocks
> ctx
->Const
.MaxCombinedUniformBlocks
) {
1948 linker_error(prog
, "Too many combined uniform blocks (%d/%d)",
1949 prog
->NumUniformBlocks
,
1950 ctx
->Const
.MaxCombinedUniformBlocks
);
1952 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1953 const unsigned max_uniform_blocks
=
1954 ctx
->Const
.Program
[i
].MaxUniformBlocks
;
1955 if (blocks
[i
] > max_uniform_blocks
) {
1956 linker_error(prog
, "Too many %s uniform blocks (%d/%d)",
1957 _mesa_shader_stage_to_string(i
),
1959 max_uniform_blocks
);
1968 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
1970 tfeedback_decl
*tfeedback_decls
= NULL
;
1971 unsigned num_tfeedback_decls
= prog
->TransformFeedback
.NumVarying
;
1973 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
1975 prog
->LinkStatus
= true; /* All error paths will set this to false */
1976 prog
->Validated
= false;
1977 prog
->_Used
= false;
1979 ralloc_free(prog
->InfoLog
);
1980 prog
->InfoLog
= ralloc_strdup(NULL
, "");
1982 ralloc_free(prog
->UniformBlocks
);
1983 prog
->UniformBlocks
= NULL
;
1984 prog
->NumUniformBlocks
= 0;
1985 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1986 ralloc_free(prog
->UniformBlockStageIndex
[i
]);
1987 prog
->UniformBlockStageIndex
[i
] = NULL
;
1990 ralloc_free(prog
->AtomicBuffers
);
1991 prog
->AtomicBuffers
= NULL
;
1992 prog
->NumAtomicBuffers
= 0;
1994 /* Separate the shaders into groups based on their type.
1996 struct gl_shader
**vert_shader_list
;
1997 unsigned num_vert_shaders
= 0;
1998 struct gl_shader
**frag_shader_list
;
1999 unsigned num_frag_shaders
= 0;
2000 struct gl_shader
**geom_shader_list
;
2001 unsigned num_geom_shaders
= 0;
2003 vert_shader_list
= (struct gl_shader
**)
2004 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
2005 frag_shader_list
= (struct gl_shader
**)
2006 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
2007 geom_shader_list
= (struct gl_shader
**)
2008 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
2010 unsigned min_version
= UINT_MAX
;
2011 unsigned max_version
= 0;
2012 const bool is_es_prog
=
2013 (prog
->NumShaders
> 0 && prog
->Shaders
[0]->IsES
) ? true : false;
2014 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
2015 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
2016 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
2018 if (prog
->Shaders
[i
]->IsES
!= is_es_prog
) {
2019 linker_error(prog
, "all shaders must use same shading "
2020 "language version\n");
2024 switch (prog
->Shaders
[i
]->Stage
) {
2025 case MESA_SHADER_VERTEX
:
2026 vert_shader_list
[num_vert_shaders
] = prog
->Shaders
[i
];
2029 case MESA_SHADER_FRAGMENT
:
2030 frag_shader_list
[num_frag_shaders
] = prog
->Shaders
[i
];
2033 case MESA_SHADER_GEOMETRY
:
2034 geom_shader_list
[num_geom_shaders
] = prog
->Shaders
[i
];
2040 /* In desktop GLSL, different shader versions may be linked together. In
2041 * GLSL ES, all shader versions must be the same.
2043 if (is_es_prog
&& min_version
!= max_version
) {
2044 linker_error(prog
, "all shaders must use same shading "
2045 "language version\n");
2049 prog
->Version
= max_version
;
2050 prog
->IsES
= is_es_prog
;
2052 /* Geometry shaders have to be linked with vertex shaders.
2054 if (num_geom_shaders
> 0 && num_vert_shaders
== 0) {
2055 linker_error(prog
, "Geometry shader must be linked with "
2060 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2061 if (prog
->_LinkedShaders
[i
] != NULL
)
2062 ctx
->Driver
.DeleteShader(ctx
, prog
->_LinkedShaders
[i
]);
2064 prog
->_LinkedShaders
[i
] = NULL
;
2067 /* Link all shaders for a particular stage and validate the result.
2069 if (num_vert_shaders
> 0) {
2070 gl_shader
*const sh
=
2071 link_intrastage_shaders(mem_ctx
, ctx
, prog
, vert_shader_list
,
2074 if (!prog
->LinkStatus
)
2077 validate_vertex_shader_executable(prog
, sh
);
2078 if (!prog
->LinkStatus
)
2080 prog
->LastClipDistanceArraySize
= prog
->Vert
.ClipDistanceArraySize
;
2082 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
2086 if (num_frag_shaders
> 0) {
2087 gl_shader
*const sh
=
2088 link_intrastage_shaders(mem_ctx
, ctx
, prog
, frag_shader_list
,
2091 if (!prog
->LinkStatus
)
2094 validate_fragment_shader_executable(prog
, sh
);
2095 if (!prog
->LinkStatus
)
2098 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
],
2102 if (num_geom_shaders
> 0) {
2103 gl_shader
*const sh
=
2104 link_intrastage_shaders(mem_ctx
, ctx
, prog
, geom_shader_list
,
2107 if (!prog
->LinkStatus
)
2110 validate_geometry_shader_executable(prog
, sh
);
2111 if (!prog
->LinkStatus
)
2113 prog
->LastClipDistanceArraySize
= prog
->Geom
.ClipDistanceArraySize
;
2115 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
],
2119 /* Here begins the inter-stage linking phase. Some initial validation is
2120 * performed, then locations are assigned for uniforms, attributes, and
2123 cross_validate_uniforms(prog
);
2124 if (!prog
->LinkStatus
)
2129 for (prev
= 0; prev
< MESA_SHADER_STAGES
; prev
++) {
2130 if (prog
->_LinkedShaders
[prev
] != NULL
)
2134 /* Validate the inputs of each stage with the output of the preceding
2137 for (unsigned i
= prev
+ 1; i
< MESA_SHADER_STAGES
; i
++) {
2138 if (prog
->_LinkedShaders
[i
] == NULL
)
2141 validate_interstage_inout_blocks(prog
, prog
->_LinkedShaders
[prev
],
2142 prog
->_LinkedShaders
[i
]);
2143 if (!prog
->LinkStatus
)
2146 cross_validate_outputs_to_inputs(prog
,
2147 prog
->_LinkedShaders
[prev
],
2148 prog
->_LinkedShaders
[i
]);
2149 if (!prog
->LinkStatus
)
2155 /* Cross-validate uniform blocks between shader stages */
2156 validate_interstage_uniform_blocks(prog
, prog
->_LinkedShaders
,
2157 MESA_SHADER_STAGES
);
2158 if (!prog
->LinkStatus
)
2161 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2162 if (prog
->_LinkedShaders
[i
] != NULL
)
2163 lower_named_interface_blocks(mem_ctx
, prog
->_LinkedShaders
[i
]);
2166 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2167 * it before optimization because we want most of the checks to get
2168 * dropped thanks to constant propagation.
2170 * This rule also applies to GLSL ES 3.00.
2172 if (max_version
>= (is_es_prog
? 300 : 130)) {
2173 struct gl_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
2175 lower_discard_flow(sh
->ir
);
2179 if (!interstage_cross_validate_uniform_blocks(prog
))
2182 /* Do common optimization before assigning storage for attributes,
2183 * uniforms, and varyings. Later optimization could possibly make
2184 * some of that unused.
2186 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2187 if (prog
->_LinkedShaders
[i
] == NULL
)
2190 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
2191 if (!prog
->LinkStatus
)
2194 if (ctx
->ShaderCompilerOptions
[i
].LowerClipDistance
) {
2195 lower_clip_distance(prog
->_LinkedShaders
[i
]);
2198 unsigned max_unroll
= ctx
->ShaderCompilerOptions
[i
].MaxUnrollIterations
;
2200 while (do_common_optimization(prog
->_LinkedShaders
[i
]->ir
, true, false, max_unroll
, &ctx
->ShaderCompilerOptions
[i
]))
2204 /* Mark all generic shader inputs and outputs as unpaired. */
2205 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] != NULL
) {
2206 link_invalidate_variable_locations(
2207 prog
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
);
2209 if (prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
2210 link_invalidate_variable_locations(
2211 prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->ir
);
2213 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] != NULL
) {
2214 link_invalidate_variable_locations(
2215 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
);
2218 /* FINISHME: The value of the max_attribute_index parameter is
2219 * FINISHME: implementation dependent based on the value of
2220 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2221 * FINISHME: at least 16, so hardcode 16 for now.
2223 if (!assign_attribute_or_color_locations(prog
, MESA_SHADER_VERTEX
, 16)) {
2227 if (!assign_attribute_or_color_locations(prog
, MESA_SHADER_FRAGMENT
, MAX2(ctx
->Const
.MaxDrawBuffers
, ctx
->Const
.MaxDualSourceDrawBuffers
))) {
2232 for (first
= 0; first
< MESA_SHADER_STAGES
; first
++) {
2233 if (prog
->_LinkedShaders
[first
] != NULL
)
2237 if (num_tfeedback_decls
!= 0) {
2238 /* From GL_EXT_transform_feedback:
2239 * A program will fail to link if:
2241 * * the <count> specified by TransformFeedbackVaryingsEXT is
2242 * non-zero, but the program object has no vertex or geometry
2245 if (first
== MESA_SHADER_FRAGMENT
) {
2246 linker_error(prog
, "Transform feedback varyings specified, but "
2247 "no vertex or geometry shader is present.");
2251 tfeedback_decls
= ralloc_array(mem_ctx
, tfeedback_decl
,
2252 prog
->TransformFeedback
.NumVarying
);
2253 if (!parse_tfeedback_decls(ctx
, prog
, mem_ctx
, num_tfeedback_decls
,
2254 prog
->TransformFeedback
.VaryingNames
,
2259 /* Linking the stages in the opposite order (from fragment to vertex)
2260 * ensures that inter-shader outputs written to in an earlier stage are
2261 * eliminated if they are (transitively) not used in a later stage.
2264 for (last
= MESA_SHADER_STAGES
-1; last
>= 0; last
--) {
2265 if (prog
->_LinkedShaders
[last
] != NULL
)
2269 if (last
>= 0 && last
< MESA_SHADER_FRAGMENT
) {
2270 gl_shader
*const sh
= prog
->_LinkedShaders
[last
];
2272 if (num_tfeedback_decls
!= 0) {
2273 /* There was no fragment shader, but we still have to assign varying
2274 * locations for use by transform feedback.
2276 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
2278 num_tfeedback_decls
, tfeedback_decls
,
2283 do_dead_builtin_varyings(ctx
, sh
, NULL
,
2284 num_tfeedback_decls
, tfeedback_decls
);
2286 demote_shader_inputs_and_outputs(sh
, ir_var_shader_out
);
2288 /* Eliminate code that is now dead due to unused outputs being demoted.
2290 while (do_dead_code(sh
->ir
, false))
2293 else if (first
== MESA_SHADER_FRAGMENT
) {
2294 /* If the program only contains a fragment shader...
2296 gl_shader
*const sh
= prog
->_LinkedShaders
[first
];
2298 do_dead_builtin_varyings(ctx
, NULL
, sh
,
2299 num_tfeedback_decls
, tfeedback_decls
);
2301 demote_shader_inputs_and_outputs(sh
, ir_var_shader_in
);
2303 while (do_dead_code(sh
->ir
, false))
2308 for (int i
= next
- 1; i
>= 0; i
--) {
2309 if (prog
->_LinkedShaders
[i
] == NULL
)
2312 gl_shader
*const sh_i
= prog
->_LinkedShaders
[i
];
2313 gl_shader
*const sh_next
= prog
->_LinkedShaders
[next
];
2314 unsigned gs_input_vertices
=
2315 next
== MESA_SHADER_GEOMETRY
? prog
->Geom
.VerticesIn
: 0;
2317 if (!assign_varying_locations(ctx
, mem_ctx
, prog
, sh_i
, sh_next
,
2318 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
2319 tfeedback_decls
, gs_input_vertices
))
2322 do_dead_builtin_varyings(ctx
, sh_i
, sh_next
,
2323 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
2326 demote_shader_inputs_and_outputs(sh_i
, ir_var_shader_out
);
2327 demote_shader_inputs_and_outputs(sh_next
, ir_var_shader_in
);
2329 /* Eliminate code that is now dead due to unused outputs being demoted.
2331 while (do_dead_code(sh_i
->ir
, false))
2333 while (do_dead_code(sh_next
->ir
, false))
2336 /* This must be done after all dead varyings are eliminated. */
2337 if (!check_against_output_limit(ctx
, prog
, sh_i
))
2339 if (!check_against_input_limit(ctx
, prog
, sh_next
))
2345 if (!store_tfeedback_info(ctx
, prog
, num_tfeedback_decls
, tfeedback_decls
))
2348 update_array_sizes(prog
);
2349 link_assign_uniform_locations(prog
);
2350 link_assign_atomic_counter_resources(ctx
, prog
);
2351 store_fragdepth_layout(prog
);
2353 check_resources(ctx
, prog
);
2354 link_check_atomic_counter_resources(ctx
, prog
);
2356 if (!prog
->LinkStatus
)
2359 /* OpenGL ES requires that a vertex shader and a fragment shader both be
2360 * present in a linked program. By checking prog->IsES, we also
2361 * catch the GL_ARB_ES2_compatibility case.
2363 if (!prog
->InternalSeparateShader
&&
2364 (ctx
->API
== API_OPENGLES2
|| prog
->IsES
)) {
2365 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
2366 linker_error(prog
, "program lacks a vertex shader\n");
2367 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
2368 linker_error(prog
, "program lacks a fragment shader\n");
2372 /* FINISHME: Assign fragment shader output locations. */
2375 free(vert_shader_list
);
2376 free(frag_shader_list
);
2377 free(geom_shader_list
);
2379 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2380 if (prog
->_LinkedShaders
[i
] == NULL
)
2383 /* Do a final validation step to make sure that the IR wasn't
2384 * invalidated by any modifications performed after intrastage linking.
2386 validate_ir_tree(prog
->_LinkedShaders
[i
]->ir
);
2388 /* Retain any live IR, but trash the rest. */
2389 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
2391 /* The symbol table in the linked shaders may contain references to
2392 * variables that were removed (e.g., unused uniforms). Since it may
2393 * contain junk, there is no possible valid use. Delete it and set the
2396 delete prog
->_LinkedShaders
[i
]->symbols
;
2397 prog
->_LinkedShaders
[i
]->symbols
= NULL
;
2400 ralloc_free(mem_ctx
);