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"
71 #include "program/hash_table.h"
73 #include "ir_optimization.h"
76 #include "main/shaderobj.h"
80 * Visitor that determines whether or not a variable is ever written.
82 class find_assignment_visitor
: public ir_hierarchical_visitor
{
84 find_assignment_visitor(const char *name
)
85 : name(name
), found(false)
90 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
)
92 ir_variable
*const var
= ir
->lhs
->variable_referenced();
94 if (strcmp(name
, var
->name
) == 0) {
99 return visit_continue_with_parent
;
102 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
104 exec_list_iterator sig_iter
= ir
->get_callee()->parameters
.iterator();
105 foreach_iter(exec_list_iterator
, iter
, *ir
) {
106 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
107 ir_variable
*sig_param
= (ir_variable
*)sig_iter
.get();
109 if (sig_param
->mode
== ir_var_out
||
110 sig_param
->mode
== ir_var_inout
) {
111 ir_variable
*var
= param_rval
->variable_referenced();
112 if (var
&& strcmp(name
, var
->name
) == 0) {
120 return visit_continue_with_parent
;
123 bool variable_found()
129 const char *name
; /**< Find writes to a variable with this name. */
130 bool found
; /**< Was a write to the variable found? */
135 * Visitor that determines whether or not a variable is ever read.
137 class find_deref_visitor
: public ir_hierarchical_visitor
{
139 find_deref_visitor(const char *name
)
140 : name(name
), found(false)
145 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
147 if (strcmp(this->name
, ir
->var
->name
) == 0) {
152 return visit_continue
;
155 bool variable_found() const
161 const char *name
; /**< Find writes to a variable with this name. */
162 bool found
; /**< Was a write to the variable found? */
167 linker_error(gl_shader_program
*prog
, const char *fmt
, ...)
171 ralloc_strcat(&prog
->InfoLog
, "error: ");
173 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
176 prog
->LinkStatus
= false;
181 linker_warning(gl_shader_program
*prog
, const char *fmt
, ...)
185 ralloc_strcat(&prog
->InfoLog
, "error: ");
187 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
194 invalidate_variable_locations(gl_shader
*sh
, enum ir_variable_mode mode
,
197 foreach_list(node
, sh
->ir
) {
198 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
200 if ((var
== NULL
) || (var
->mode
!= (unsigned) mode
))
203 /* Only assign locations for generic attributes / varyings / etc.
205 if ((var
->location
>= generic_base
) && !var
->explicit_location
)
212 * Determine the number of attribute slots required for a particular type
214 * This code is here because it implements the language rules of a specific
215 * GLSL version. Since it's a property of the language and not a property of
216 * types in general, it doesn't really belong in glsl_type.
219 count_attribute_slots(const glsl_type
*t
)
221 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
223 * "A scalar input counts the same amount against this limit as a vec4,
224 * so applications may want to consider packing groups of four
225 * unrelated float inputs together into a vector to better utilize the
226 * capabilities of the underlying hardware. A matrix input will use up
227 * multiple locations. The number of locations used will equal the
228 * number of columns in the matrix."
230 * The spec does not explicitly say how arrays are counted. However, it
231 * should be safe to assume the total number of slots consumed by an array
232 * is the number of entries in the array multiplied by the number of slots
233 * consumed by a single element of the array.
237 return t
->array_size() * count_attribute_slots(t
->element_type());
240 return t
->matrix_columns
;
247 * Verify that a vertex shader executable meets all semantic requirements
249 * \param shader Vertex shader executable to be verified
252 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
253 struct gl_shader
*shader
)
258 find_assignment_visitor
find("gl_Position");
259 find
.run(shader
->ir
);
260 if (!find
.variable_found()) {
261 linker_error(prog
, "vertex shader does not write to `gl_Position'\n");
270 * Verify that a fragment shader executable meets all semantic requirements
272 * \param shader Fragment shader executable to be verified
275 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
276 struct gl_shader
*shader
)
281 find_assignment_visitor
frag_color("gl_FragColor");
282 find_assignment_visitor
frag_data("gl_FragData");
284 frag_color
.run(shader
->ir
);
285 frag_data
.run(shader
->ir
);
287 if (frag_color
.variable_found() && frag_data
.variable_found()) {
288 linker_error(prog
, "fragment shader writes to both "
289 "`gl_FragColor' and `gl_FragData'\n");
298 * Generate a string describing the mode of a variable
301 mode_string(const ir_variable
*var
)
305 return (var
->read_only
) ? "global constant" : "global variable";
307 case ir_var_uniform
: return "uniform";
308 case ir_var_in
: return "shader input";
309 case ir_var_out
: return "shader output";
310 case ir_var_inout
: return "shader inout";
312 case ir_var_const_in
:
313 case ir_var_temporary
:
315 assert(!"Should not get here.");
316 return "invalid variable";
322 * Perform validation of global variables used across multiple shaders
325 cross_validate_globals(struct gl_shader_program
*prog
,
326 struct gl_shader
**shader_list
,
327 unsigned num_shaders
,
330 /* Examine all of the uniforms in all of the shaders and cross validate
333 glsl_symbol_table variables
;
334 for (unsigned i
= 0; i
< num_shaders
; i
++) {
335 if (shader_list
[i
] == NULL
)
338 foreach_list(node
, shader_list
[i
]->ir
) {
339 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
344 if (uniforms_only
&& (var
->mode
!= ir_var_uniform
))
347 /* Don't cross validate temporaries that are at global scope. These
348 * will eventually get pulled into the shaders 'main'.
350 if (var
->mode
== ir_var_temporary
)
353 /* If a global with this name has already been seen, verify that the
354 * new instance has the same type. In addition, if the globals have
355 * initializers, the values of the initializers must be the same.
357 ir_variable
*const existing
= variables
.get_variable(var
->name
);
358 if (existing
!= NULL
) {
359 if (var
->type
!= existing
->type
) {
360 /* Consider the types to be "the same" if both types are arrays
361 * of the same type and one of the arrays is implicitly sized.
362 * In addition, set the type of the linked variable to the
363 * explicitly sized array.
365 if (var
->type
->is_array()
366 && existing
->type
->is_array()
367 && (var
->type
->fields
.array
== existing
->type
->fields
.array
)
368 && ((var
->type
->length
== 0)
369 || (existing
->type
->length
== 0))) {
370 if (var
->type
->length
!= 0) {
371 existing
->type
= var
->type
;
374 linker_error(prog
, "%s `%s' declared as type "
375 "`%s' and type `%s'\n",
377 var
->name
, var
->type
->name
,
378 existing
->type
->name
);
383 if (var
->explicit_location
) {
384 if (existing
->explicit_location
385 && (var
->location
!= existing
->location
)) {
386 linker_error(prog
, "explicit locations for %s "
387 "`%s' have differing values\n",
388 mode_string(var
), var
->name
);
392 existing
->location
= var
->location
;
393 existing
->explicit_location
= true;
396 /* Validate layout qualifiers for gl_FragDepth.
398 * From the AMD_conservative_depth spec:
399 * "If gl_FragDepth is redeclared in any fragment shader in
400 * a program, it must be redeclared in all fragment shaders in that
401 * program that have static assignments to gl_FragDepth. All
402 * redeclarations of gl_FragDepth in all fragment shaders in
403 * a single program must have the same set of qualifiers."
405 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
406 bool layout_declared
= var
->depth_layout
!= ir_depth_layout_none
;
407 bool layout_differs
= var
->depth_layout
!= existing
->depth_layout
;
408 if (layout_declared
&& layout_differs
) {
410 "All redeclarations of gl_FragDepth in all fragment shaders "
411 "in a single program must have the same set of qualifiers.");
413 if (var
->used
&& layout_differs
) {
415 "If gl_FragDepth is redeclared with a layout qualifier in"
416 "any fragment shader, it must be redeclared with the same"
417 "layout qualifier in all fragment shaders that have"
418 "assignments to gl_FragDepth");
422 /* FINISHME: Handle non-constant initializers.
424 if (var
->constant_value
!= NULL
) {
425 if (existing
->constant_value
!= NULL
) {
426 if (!var
->constant_value
->has_value(existing
->constant_value
)) {
427 linker_error(prog
, "initializers for %s "
428 "`%s' have differing values\n",
429 mode_string(var
), var
->name
);
433 /* If the first-seen instance of a particular uniform did not
434 * have an initializer but a later instance does, copy the
435 * initializer to the version stored in the symbol table.
437 /* FINISHME: This is wrong. The constant_value field should
438 * FINISHME: not be modified! Imagine a case where a shader
439 * FINISHME: without an initializer is linked in two different
440 * FINISHME: programs with shaders that have differing
441 * FINISHME: initializers. Linking with the first will
442 * FINISHME: modify the shader, and linking with the second
443 * FINISHME: will fail.
445 existing
->constant_value
=
446 var
->constant_value
->clone(ralloc_parent(existing
), NULL
);
449 if (existing
->invariant
!= var
->invariant
) {
450 linker_error(prog
, "declarations for %s `%s' have "
451 "mismatching invariant qualifiers\n",
452 mode_string(var
), var
->name
);
455 if (existing
->centroid
!= var
->centroid
) {
456 linker_error(prog
, "declarations for %s `%s' have "
457 "mismatching centroid qualifiers\n",
458 mode_string(var
), var
->name
);
462 variables
.add_variable(var
);
471 * Perform validation of uniforms used across multiple shader stages
474 cross_validate_uniforms(struct gl_shader_program
*prog
)
476 return cross_validate_globals(prog
, prog
->_LinkedShaders
,
477 MESA_SHADER_TYPES
, true);
482 * Validate that outputs from one stage match inputs of another
485 cross_validate_outputs_to_inputs(struct gl_shader_program
*prog
,
486 gl_shader
*producer
, gl_shader
*consumer
)
488 glsl_symbol_table parameters
;
489 /* FINISHME: Figure these out dynamically. */
490 const char *const producer_stage
= "vertex";
491 const char *const consumer_stage
= "fragment";
493 /* Find all shader outputs in the "producer" stage.
495 foreach_list(node
, producer
->ir
) {
496 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
498 /* FINISHME: For geometry shaders, this should also look for inout
499 * FINISHME: variables.
501 if ((var
== NULL
) || (var
->mode
!= ir_var_out
))
504 parameters
.add_variable(var
);
508 /* Find all shader inputs in the "consumer" stage. Any variables that have
509 * matching outputs already in the symbol table must have the same type and
512 foreach_list(node
, consumer
->ir
) {
513 ir_variable
*const input
= ((ir_instruction
*) node
)->as_variable();
515 /* FINISHME: For geometry shaders, this should also look for inout
516 * FINISHME: variables.
518 if ((input
== NULL
) || (input
->mode
!= ir_var_in
))
521 ir_variable
*const output
= parameters
.get_variable(input
->name
);
522 if (output
!= NULL
) {
523 /* Check that the types match between stages.
525 if (input
->type
!= output
->type
) {
526 /* There is a bit of a special case for gl_TexCoord. This
527 * built-in is unsized by default. Applications that variable
528 * access it must redeclare it with a size. There is some
529 * language in the GLSL spec that implies the fragment shader
530 * and vertex shader do not have to agree on this size. Other
531 * driver behave this way, and one or two applications seem to
534 * Neither declaration needs to be modified here because the array
535 * sizes are fixed later when update_array_sizes is called.
537 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
539 * "Unlike user-defined varying variables, the built-in
540 * varying variables don't have a strict one-to-one
541 * correspondence between the vertex language and the
542 * fragment language."
544 if (!output
->type
->is_array()
545 || (strncmp("gl_", output
->name
, 3) != 0)) {
547 "%s shader output `%s' declared as type `%s', "
548 "but %s shader input declared as type `%s'\n",
549 producer_stage
, output
->name
,
551 consumer_stage
, input
->type
->name
);
556 /* Check that all of the qualifiers match between stages.
558 if (input
->centroid
!= output
->centroid
) {
560 "%s shader output `%s' %s centroid qualifier, "
561 "but %s shader input %s centroid qualifier\n",
564 (output
->centroid
) ? "has" : "lacks",
566 (input
->centroid
) ? "has" : "lacks");
570 if (input
->invariant
!= output
->invariant
) {
572 "%s shader output `%s' %s invariant qualifier, "
573 "but %s shader input %s invariant qualifier\n",
576 (output
->invariant
) ? "has" : "lacks",
578 (input
->invariant
) ? "has" : "lacks");
582 if (input
->interpolation
!= output
->interpolation
) {
584 "%s shader output `%s' specifies %s "
585 "interpolation qualifier, "
586 "but %s shader input specifies %s "
587 "interpolation qualifier\n",
590 output
->interpolation_string(),
592 input
->interpolation_string());
603 * Populates a shaders symbol table with all global declarations
606 populate_symbol_table(gl_shader
*sh
)
608 sh
->symbols
= new(sh
) glsl_symbol_table
;
610 foreach_list(node
, sh
->ir
) {
611 ir_instruction
*const inst
= (ir_instruction
*) node
;
615 if ((func
= inst
->as_function()) != NULL
) {
616 sh
->symbols
->add_function(func
);
617 } else if ((var
= inst
->as_variable()) != NULL
) {
618 sh
->symbols
->add_variable(var
);
625 * Remap variables referenced in an instruction tree
627 * This is used when instruction trees are cloned from one shader and placed in
628 * another. These trees will contain references to \c ir_variable nodes that
629 * do not exist in the target shader. This function finds these \c ir_variable
630 * references and replaces the references with matching variables in the target
633 * If there is no matching variable in the target shader, a clone of the
634 * \c ir_variable is made and added to the target shader. The new variable is
635 * added to \b both the instruction stream and the symbol table.
637 * \param inst IR tree that is to be processed.
638 * \param symbols Symbol table containing global scope symbols in the
640 * \param instructions Instruction stream where new variable declarations
644 remap_variables(ir_instruction
*inst
, struct gl_shader
*target
,
647 class remap_visitor
: public ir_hierarchical_visitor
{
649 remap_visitor(struct gl_shader
*target
,
652 this->target
= target
;
653 this->symbols
= target
->symbols
;
654 this->instructions
= target
->ir
;
658 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
660 if (ir
->var
->mode
== ir_var_temporary
) {
661 ir_variable
*var
= (ir_variable
*) hash_table_find(temps
, ir
->var
);
665 return visit_continue
;
668 ir_variable
*const existing
=
669 this->symbols
->get_variable(ir
->var
->name
);
670 if (existing
!= NULL
)
673 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
675 this->symbols
->add_variable(copy
);
676 this->instructions
->push_head(copy
);
680 return visit_continue
;
684 struct gl_shader
*target
;
685 glsl_symbol_table
*symbols
;
686 exec_list
*instructions
;
690 remap_visitor
v(target
, temps
);
697 * Move non-declarations from one instruction stream to another
699 * The intended usage pattern of this function is to pass the pointer to the
700 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
701 * pointer) for \c last and \c false for \c make_copies on the first
702 * call. Successive calls pass the return value of the previous call for
703 * \c last and \c true for \c make_copies.
705 * \param instructions Source instruction stream
706 * \param last Instruction after which new instructions should be
707 * inserted in the target instruction stream
708 * \param make_copies Flag selecting whether instructions in \c instructions
709 * should be copied (via \c ir_instruction::clone) into the
710 * target list or moved.
713 * The new "last" instruction in the target instruction stream. This pointer
714 * is suitable for use as the \c last parameter of a later call to this
718 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
719 bool make_copies
, gl_shader
*target
)
721 hash_table
*temps
= NULL
;
724 temps
= hash_table_ctor(0, hash_table_pointer_hash
,
725 hash_table_pointer_compare
);
727 foreach_list_safe(node
, instructions
) {
728 ir_instruction
*inst
= (ir_instruction
*) node
;
730 if (inst
->as_function())
733 ir_variable
*var
= inst
->as_variable();
734 if ((var
!= NULL
) && (var
->mode
!= ir_var_temporary
))
737 assert(inst
->as_assignment()
738 || ((var
!= NULL
) && (var
->mode
== ir_var_temporary
)));
741 inst
= inst
->clone(target
, NULL
);
744 hash_table_insert(temps
, inst
, var
);
746 remap_variables(inst
, target
, temps
);
751 last
->insert_after(inst
);
756 hash_table_dtor(temps
);
762 * Get the function signature for main from a shader
764 static ir_function_signature
*
765 get_main_function_signature(gl_shader
*sh
)
767 ir_function
*const f
= sh
->symbols
->get_function("main");
769 exec_list void_parameters
;
771 /* Look for the 'void main()' signature and ensure that it's defined.
772 * This keeps the linker from accidentally pick a shader that just
773 * contains a prototype for main.
775 * We don't have to check for multiple definitions of main (in multiple
776 * shaders) because that would have already been caught above.
778 ir_function_signature
*sig
= f
->matching_signature(&void_parameters
);
779 if ((sig
!= NULL
) && sig
->is_defined
) {
789 * Combine a group of shaders for a single stage to generate a linked shader
792 * If this function is supplied a single shader, it is cloned, and the new
793 * shader is returned.
795 static struct gl_shader
*
796 link_intrastage_shaders(void *mem_ctx
,
797 struct gl_context
*ctx
,
798 struct gl_shader_program
*prog
,
799 struct gl_shader
**shader_list
,
800 unsigned num_shaders
)
802 /* Check that global variables defined in multiple shaders are consistent.
804 if (!cross_validate_globals(prog
, shader_list
, num_shaders
, false))
807 /* Check that there is only a single definition of each function signature
808 * across all shaders.
810 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
811 foreach_list(node
, shader_list
[i
]->ir
) {
812 ir_function
*const f
= ((ir_instruction
*) node
)->as_function();
817 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
818 ir_function
*const other
=
819 shader_list
[j
]->symbols
->get_function(f
->name
);
821 /* If the other shader has no function (and therefore no function
822 * signatures) with the same name, skip to the next shader.
827 foreach_iter (exec_list_iterator
, iter
, *f
) {
828 ir_function_signature
*sig
=
829 (ir_function_signature
*) iter
.get();
831 if (!sig
->is_defined
|| sig
->is_builtin
)
834 ir_function_signature
*other_sig
=
835 other
->exact_matching_signature(& sig
->parameters
);
837 if ((other_sig
!= NULL
) && other_sig
->is_defined
838 && !other_sig
->is_builtin
) {
839 linker_error(prog
, "function `%s' is multiply defined",
848 /* Find the shader that defines main, and make a clone of it.
850 * Starting with the clone, search for undefined references. If one is
851 * found, find the shader that defines it. Clone the reference and add
852 * it to the shader. Repeat until there are no undefined references or
853 * until a reference cannot be resolved.
855 gl_shader
*main
= NULL
;
856 for (unsigned i
= 0; i
< num_shaders
; i
++) {
857 if (get_main_function_signature(shader_list
[i
]) != NULL
) {
858 main
= shader_list
[i
];
864 linker_error(prog
, "%s shader lacks `main'\n",
865 (shader_list
[0]->Type
== GL_VERTEX_SHADER
)
866 ? "vertex" : "fragment");
870 gl_shader
*linked
= ctx
->Driver
.NewShader(NULL
, 0, main
->Type
);
871 linked
->ir
= new(linked
) exec_list
;
872 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
874 populate_symbol_table(linked
);
876 /* The a pointer to the main function in the final linked shader (i.e., the
877 * copy of the original shader that contained the main function).
879 ir_function_signature
*const main_sig
= get_main_function_signature(linked
);
881 /* Move any instructions other than variable declarations or function
882 * declarations into main.
884 exec_node
*insertion_point
=
885 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
888 for (unsigned i
= 0; i
< num_shaders
; i
++) {
889 if (shader_list
[i
] == main
)
892 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
893 insertion_point
, true, linked
);
896 /* Resolve initializers for global variables in the linked shader.
898 unsigned num_linking_shaders
= num_shaders
;
899 for (unsigned i
= 0; i
< num_shaders
; i
++)
900 num_linking_shaders
+= shader_list
[i
]->num_builtins_to_link
;
902 gl_shader
**linking_shaders
=
903 (gl_shader
**) calloc(num_linking_shaders
, sizeof(gl_shader
*));
905 memcpy(linking_shaders
, shader_list
,
906 sizeof(linking_shaders
[0]) * num_shaders
);
908 unsigned idx
= num_shaders
;
909 for (unsigned i
= 0; i
< num_shaders
; i
++) {
910 memcpy(&linking_shaders
[idx
], shader_list
[i
]->builtins_to_link
,
911 sizeof(linking_shaders
[0]) * shader_list
[i
]->num_builtins_to_link
);
912 idx
+= shader_list
[i
]->num_builtins_to_link
;
915 assert(idx
== num_linking_shaders
);
917 if (!link_function_calls(prog
, linked
, linking_shaders
,
918 num_linking_shaders
)) {
919 ctx
->Driver
.DeleteShader(ctx
, linked
);
923 free(linking_shaders
);
926 /* At this point linked should contain all of the linked IR, so
927 * validate it to make sure nothing went wrong.
930 validate_ir_tree(linked
->ir
);
933 /* Make a pass over all variable declarations to ensure that arrays with
934 * unspecified sizes have a size specified. The size is inferred from the
935 * max_array_access field.
937 if (linked
!= NULL
) {
938 class array_sizing_visitor
: public ir_hierarchical_visitor
{
940 virtual ir_visitor_status
visit(ir_variable
*var
)
942 if (var
->type
->is_array() && (var
->type
->length
== 0)) {
943 const glsl_type
*type
=
944 glsl_type::get_array_instance(var
->type
->fields
.array
,
945 var
->max_array_access
+ 1);
947 assert(type
!= NULL
);
951 return visit_continue
;
962 struct uniform_node
{
964 struct gl_uniform
*u
;
969 * Update the sizes of linked shader uniform arrays to the maximum
972 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
974 * If one or more elements of an array are active,
975 * GetActiveUniform will return the name of the array in name,
976 * subject to the restrictions listed above. The type of the array
977 * is returned in type. The size parameter contains the highest
978 * array element index used, plus one. The compiler or linker
979 * determines the highest index used. There will be only one
980 * active uniform reported by the GL per uniform array.
984 update_array_sizes(struct gl_shader_program
*prog
)
986 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
987 if (prog
->_LinkedShaders
[i
] == NULL
)
990 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
991 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
993 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
&&
994 var
->mode
!= ir_var_in
&&
995 var
->mode
!= ir_var_out
) ||
996 !var
->type
->is_array())
999 unsigned int size
= var
->max_array_access
;
1000 for (unsigned j
= 0; j
< MESA_SHADER_TYPES
; j
++) {
1001 if (prog
->_LinkedShaders
[j
] == NULL
)
1004 foreach_list(node2
, prog
->_LinkedShaders
[j
]->ir
) {
1005 ir_variable
*other_var
= ((ir_instruction
*) node2
)->as_variable();
1009 if (strcmp(var
->name
, other_var
->name
) == 0 &&
1010 other_var
->max_array_access
> size
) {
1011 size
= other_var
->max_array_access
;
1016 if (size
+ 1 != var
->type
->fields
.array
->length
) {
1017 /* If this is a built-in uniform (i.e., it's backed by some
1018 * fixed-function state), adjust the number of state slots to
1019 * match the new array size. The number of slots per array entry
1020 * is not known. It seems safe to assume that the total number of
1021 * slots is an integer multiple of the number of array elements.
1022 * Determine the number of slots per array element by dividing by
1023 * the old (total) size.
1025 if (var
->num_state_slots
> 0) {
1026 var
->num_state_slots
= (size
+ 1)
1027 * (var
->num_state_slots
/ var
->type
->length
);
1030 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
1032 /* FINISHME: We should update the types of array
1033 * dereferences of this variable now.
1041 add_uniform(void *mem_ctx
, exec_list
*uniforms
, struct hash_table
*ht
,
1042 const char *name
, const glsl_type
*type
, GLenum shader_type
,
1043 unsigned *next_shader_pos
, unsigned *total_uniforms
)
1045 if (type
->is_record()) {
1046 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1047 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
1048 char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
1049 type
->fields
.structure
[i
].name
);
1051 add_uniform(mem_ctx
, uniforms
, ht
, field_name
, field_type
,
1052 shader_type
, next_shader_pos
, total_uniforms
);
1055 uniform_node
*n
= (uniform_node
*) hash_table_find(ht
, name
);
1056 unsigned int vec4_slots
;
1057 const glsl_type
*array_elem_type
= NULL
;
1059 if (type
->is_array()) {
1060 array_elem_type
= type
->fields
.array
;
1061 /* Array of structures. */
1062 if (array_elem_type
->is_record()) {
1063 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1064 char *elem_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
1065 add_uniform(mem_ctx
, uniforms
, ht
, elem_name
, array_elem_type
,
1066 shader_type
, next_shader_pos
, total_uniforms
);
1072 /* Fix the storage size of samplers at 1 vec4 each. Be sure to pad out
1073 * vectors to vec4 slots.
1075 if (type
->is_array()) {
1076 if (array_elem_type
->is_sampler())
1077 vec4_slots
= type
->length
;
1079 vec4_slots
= type
->length
* array_elem_type
->matrix_columns
;
1080 } else if (type
->is_sampler()) {
1083 vec4_slots
= type
->matrix_columns
;
1087 n
= (uniform_node
*) calloc(1, sizeof(struct uniform_node
));
1088 n
->u
= (gl_uniform
*) calloc(1, sizeof(struct gl_uniform
));
1089 n
->slots
= vec4_slots
;
1091 n
->u
->Name
= strdup(name
);
1096 (*total_uniforms
)++;
1098 hash_table_insert(ht
, n
, name
);
1099 uniforms
->push_tail(& n
->link
);
1102 switch (shader_type
) {
1103 case GL_VERTEX_SHADER
:
1104 n
->u
->VertPos
= *next_shader_pos
;
1106 case GL_FRAGMENT_SHADER
:
1107 n
->u
->FragPos
= *next_shader_pos
;
1109 case GL_GEOMETRY_SHADER
:
1110 n
->u
->GeomPos
= *next_shader_pos
;
1114 (*next_shader_pos
) += vec4_slots
;
1119 assign_uniform_locations(struct gl_shader_program
*prog
)
1123 unsigned total_uniforms
= 0;
1124 hash_table
*ht
= hash_table_ctor(32, hash_table_string_hash
,
1125 hash_table_string_compare
);
1126 void *mem_ctx
= ralloc_context(NULL
);
1128 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1129 if (prog
->_LinkedShaders
[i
] == NULL
)
1132 unsigned next_position
= 0;
1134 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
1135 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1137 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
1140 if (strncmp(var
->name
, "gl_", 3) == 0) {
1141 /* At the moment, we don't allocate uniform locations for
1142 * builtin uniforms. It's permitted by spec, and we'll
1143 * likely switch to doing that at some point, but not yet.
1148 var
->location
= next_position
;
1149 add_uniform(mem_ctx
, &uniforms
, ht
, var
->name
, var
->type
,
1150 prog
->_LinkedShaders
[i
]->Type
,
1151 &next_position
, &total_uniforms
);
1155 ralloc_free(mem_ctx
);
1157 gl_uniform_list
*ul
= (gl_uniform_list
*)
1158 calloc(1, sizeof(gl_uniform_list
));
1160 ul
->Size
= total_uniforms
;
1161 ul
->NumUniforms
= total_uniforms
;
1162 ul
->Uniforms
= (gl_uniform
*) calloc(total_uniforms
, sizeof(gl_uniform
));
1166 for (uniform_node
*node
= (uniform_node
*) uniforms
.head
1167 ; node
->link
.next
!= NULL
1169 next
= (uniform_node
*) node
->link
.next
;
1171 node
->link
.remove();
1172 memcpy(&ul
->Uniforms
[idx
], node
->u
, sizeof(gl_uniform
));
1179 hash_table_dtor(ht
);
1181 prog
->Uniforms
= ul
;
1186 * Find a contiguous set of available bits in a bitmask.
1188 * \param used_mask Bits representing used (1) and unused (0) locations
1189 * \param needed_count Number of contiguous bits needed.
1192 * Base location of the available bits on success or -1 on failure.
1195 find_available_slots(unsigned used_mask
, unsigned needed_count
)
1197 unsigned needed_mask
= (1 << needed_count
) - 1;
1198 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
1200 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1201 * cannot optimize possibly infinite loops" for the loop below.
1203 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
1206 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
1207 if ((needed_mask
& ~used_mask
) == needed_mask
)
1218 * Assign locations for either VS inputs for FS outputs
1220 * \param prog Shader program whose variables need locations assigned
1221 * \param target_index Selector for the program target to receive location
1222 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1223 * \c MESA_SHADER_FRAGMENT.
1224 * \param max_index Maximum number of generic locations. This corresponds
1225 * to either the maximum number of draw buffers or the
1226 * maximum number of generic attributes.
1229 * If locations are successfully assigned, true is returned. Otherwise an
1230 * error is emitted to the shader link log and false is returned.
1233 * Locations set via \c glBindFragDataLocation are not currently supported.
1234 * Only locations assigned automatically by the linker, explicitly set by a
1235 * layout qualifier, or explicitly set by a built-in variable (e.g., \c
1236 * gl_FragColor) are supported for fragment shaders.
1239 assign_attribute_or_color_locations(gl_shader_program
*prog
,
1240 unsigned target_index
,
1243 /* Mark invalid locations as being used.
1245 unsigned used_locations
= (max_index
>= 32)
1246 ? ~0 : ~((1 << max_index
) - 1);
1248 assert((target_index
== MESA_SHADER_VERTEX
)
1249 || (target_index
== MESA_SHADER_FRAGMENT
));
1251 gl_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
1255 /* Operate in a total of four passes.
1257 * 1. Invalidate the location assignments for all vertex shader inputs.
1259 * 2. Assign locations for inputs that have user-defined (via
1260 * glBindVertexAttribLocation) locations.
1262 * 3. Sort the attributes without assigned locations by number of slots
1263 * required in decreasing order. Fragmentation caused by attribute
1264 * locations assigned by the application may prevent large attributes
1265 * from having enough contiguous space.
1267 * 4. Assign locations to any inputs without assigned locations.
1270 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
1271 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
1273 const enum ir_variable_mode direction
=
1274 (target_index
== MESA_SHADER_VERTEX
) ? ir_var_in
: ir_var_out
;
1277 invalidate_variable_locations(sh
, direction
, generic_base
);
1279 if ((target_index
== MESA_SHADER_VERTEX
) && (prog
->Attributes
!= NULL
)) {
1280 for (unsigned i
= 0; i
< prog
->Attributes
->NumParameters
; i
++) {
1281 ir_variable
*const var
=
1282 sh
->symbols
->get_variable(prog
->Attributes
->Parameters
[i
].Name
);
1284 /* Note: attributes that occupy multiple slots, such as arrays or
1285 * matrices, may appear in the attrib array multiple times.
1287 if ((var
== NULL
) || (var
->location
!= -1))
1290 /* From page 61 of the OpenGL 4.0 spec:
1292 * "LinkProgram will fail if the attribute bindings assigned by
1293 * BindAttribLocation do not leave not enough space to assign a
1294 * location for an active matrix attribute or an active attribute
1295 * array, both of which require multiple contiguous generic
1298 * Previous versions of the spec contain similar language but omit the
1299 * bit about attribute arrays.
1301 * Page 61 of the OpenGL 4.0 spec also says:
1303 * "It is possible for an application to bind more than one
1304 * attribute name to the same location. This is referred to as
1305 * aliasing. This will only work if only one of the aliased
1306 * attributes is active in the executable program, or if no path
1307 * through the shader consumes more than one attribute of a set
1308 * of attributes aliased to the same location. A link error can
1309 * occur if the linker determines that every path through the
1310 * shader consumes multiple aliased attributes, but
1311 * implementations are not required to generate an error in this
1314 * These two paragraphs are either somewhat contradictory, or I don't
1315 * fully understand one or both of them.
1317 /* FINISHME: The code as currently written does not support attribute
1318 * FINISHME: location aliasing (see comment above).
1320 const int attr
= prog
->Attributes
->Parameters
[i
].StateIndexes
[0];
1321 const unsigned slots
= count_attribute_slots(var
->type
);
1323 /* Mask representing the contiguous slots that will be used by this
1326 const unsigned use_mask
= (1 << slots
) - 1;
1328 /* Generate a link error if the set of bits requested for this
1329 * attribute overlaps any previously allocated bits.
1331 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
1333 "insufficient contiguous attribute locations "
1334 "available for vertex shader input `%s'",
1339 var
->location
= VERT_ATTRIB_GENERIC0
+ attr
;
1340 used_locations
|= (use_mask
<< attr
);
1344 /* Temporary storage for the set of attributes that need locations assigned.
1350 /* Used below in the call to qsort. */
1351 static int compare(const void *a
, const void *b
)
1353 const temp_attr
*const l
= (const temp_attr
*) a
;
1354 const temp_attr
*const r
= (const temp_attr
*) b
;
1356 /* Reversed because we want a descending order sort below. */
1357 return r
->slots
- l
->slots
;
1361 unsigned num_attr
= 0;
1363 foreach_list(node
, sh
->ir
) {
1364 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1366 if ((var
== NULL
) || (var
->mode
!= (unsigned) direction
))
1369 if (var
->explicit_location
) {
1370 const unsigned slots
= count_attribute_slots(var
->type
);
1371 const unsigned use_mask
= (1 << slots
) - 1;
1372 const int attr
= var
->location
- generic_base
;
1374 if ((var
->location
>= (int)(max_index
+ generic_base
))
1375 || (var
->location
< 0)) {
1377 "invalid explicit location %d specified for `%s'\n",
1378 (var
->location
< 0) ? var
->location
: attr
,
1381 } else if (var
->location
>= generic_base
) {
1382 used_locations
|= (use_mask
<< attr
);
1386 /* The location was explicitly assigned, nothing to do here.
1388 if (var
->location
!= -1)
1391 to_assign
[num_attr
].slots
= count_attribute_slots(var
->type
);
1392 to_assign
[num_attr
].var
= var
;
1396 /* If all of the attributes were assigned locations by the application (or
1397 * are built-in attributes with fixed locations), return early. This should
1398 * be the common case.
1403 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
1405 if (target_index
== MESA_SHADER_VERTEX
) {
1406 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1407 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1408 * reserved to prevent it from being automatically allocated below.
1410 find_deref_visitor
find("gl_Vertex");
1412 if (find
.variable_found())
1413 used_locations
|= (1 << 0);
1416 for (unsigned i
= 0; i
< num_attr
; i
++) {
1417 /* Mask representing the contiguous slots that will be used by this
1420 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
1422 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
1425 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
1426 ? "vertex shader input" : "fragment shader output";
1429 "insufficient contiguous attribute locations "
1430 "available for %s `%s'",
1431 string
, to_assign
[i
].var
->name
);
1435 to_assign
[i
].var
->location
= generic_base
+ location
;
1436 used_locations
|= (use_mask
<< location
);
1444 * Demote shader inputs and outputs that are not used in other stages
1447 demote_shader_inputs_and_outputs(gl_shader
*sh
, enum ir_variable_mode mode
)
1449 foreach_list(node
, sh
->ir
) {
1450 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1452 if ((var
== NULL
) || (var
->mode
!= int(mode
)))
1455 /* A shader 'in' or 'out' variable is only really an input or output if
1456 * its value is used by other shader stages. This will cause the variable
1457 * to have a location assigned.
1459 if (var
->location
== -1) {
1460 var
->mode
= ir_var_auto
;
1467 assign_varying_locations(struct gl_context
*ctx
,
1468 struct gl_shader_program
*prog
,
1469 gl_shader
*producer
, gl_shader
*consumer
)
1471 /* FINISHME: Set dynamically when geometry shader support is added. */
1472 unsigned output_index
= VERT_RESULT_VAR0
;
1473 unsigned input_index
= FRAG_ATTRIB_VAR0
;
1475 /* Operate in a total of three passes.
1477 * 1. Assign locations for any matching inputs and outputs.
1479 * 2. Mark output variables in the producer that do not have locations as
1480 * not being outputs. This lets the optimizer eliminate them.
1482 * 3. Mark input variables in the consumer that do not have locations as
1483 * not being inputs. This lets the optimizer eliminate them.
1486 invalidate_variable_locations(producer
, ir_var_out
, VERT_RESULT_VAR0
);
1487 invalidate_variable_locations(consumer
, ir_var_in
, FRAG_ATTRIB_VAR0
);
1489 foreach_list(node
, producer
->ir
) {
1490 ir_variable
*const output_var
= ((ir_instruction
*) node
)->as_variable();
1492 if ((output_var
== NULL
) || (output_var
->mode
!= ir_var_out
)
1493 || (output_var
->location
!= -1))
1496 ir_variable
*const input_var
=
1497 consumer
->symbols
->get_variable(output_var
->name
);
1499 if ((input_var
== NULL
) || (input_var
->mode
!= ir_var_in
))
1502 assert(input_var
->location
== -1);
1504 output_var
->location
= output_index
;
1505 input_var
->location
= input_index
;
1507 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1508 assert(!output_var
->type
->is_record());
1510 if (output_var
->type
->is_array()) {
1511 const unsigned slots
= output_var
->type
->length
1512 * output_var
->type
->fields
.array
->matrix_columns
;
1514 output_index
+= slots
;
1515 input_index
+= slots
;
1517 const unsigned slots
= output_var
->type
->matrix_columns
;
1519 output_index
+= slots
;
1520 input_index
+= slots
;
1524 unsigned varying_vectors
= 0;
1526 foreach_list(node
, consumer
->ir
) {
1527 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1529 if ((var
== NULL
) || (var
->mode
!= ir_var_in
))
1532 if (var
->location
== -1) {
1533 if (prog
->Version
<= 120) {
1534 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1536 * Only those varying variables used (i.e. read) in
1537 * the fragment shader executable must be written to
1538 * by the vertex shader executable; declaring
1539 * superfluous varying variables in a vertex shader is
1542 * We interpret this text as meaning that the VS must
1543 * write the variable for the FS to read it. See
1544 * "glsl1-varying read but not written" in piglit.
1547 linker_error(prog
, "fragment shader varying %s not written "
1548 "by vertex shader\n.", var
->name
);
1551 /* An 'in' variable is only really a shader input if its
1552 * value is written by the previous stage.
1554 var
->mode
= ir_var_auto
;
1556 /* The packing rules are used for vertex shader inputs are also used
1557 * for fragment shader inputs.
1559 varying_vectors
+= count_attribute_slots(var
->type
);
1563 if (ctx
->API
== API_OPENGLES2
|| prog
->Version
== 100) {
1564 if (varying_vectors
> ctx
->Const
.MaxVarying
) {
1565 linker_error(prog
, "shader uses too many varying vectors "
1567 varying_vectors
, ctx
->Const
.MaxVarying
);
1571 const unsigned float_components
= varying_vectors
* 4;
1572 if (float_components
> ctx
->Const
.MaxVarying
* 4) {
1573 linker_error(prog
, "shader uses too many varying components "
1575 float_components
, ctx
->Const
.MaxVarying
* 4);
1585 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
1587 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
1589 prog
->LinkStatus
= false;
1590 prog
->Validated
= false;
1591 prog
->_Used
= false;
1593 if (prog
->InfoLog
!= NULL
)
1594 ralloc_free(prog
->InfoLog
);
1596 prog
->InfoLog
= ralloc_strdup(NULL
, "");
1598 /* Separate the shaders into groups based on their type.
1600 struct gl_shader
**vert_shader_list
;
1601 unsigned num_vert_shaders
= 0;
1602 struct gl_shader
**frag_shader_list
;
1603 unsigned num_frag_shaders
= 0;
1605 vert_shader_list
= (struct gl_shader
**)
1606 calloc(2 * prog
->NumShaders
, sizeof(struct gl_shader
*));
1607 frag_shader_list
= &vert_shader_list
[prog
->NumShaders
];
1609 unsigned min_version
= UINT_MAX
;
1610 unsigned max_version
= 0;
1611 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
1612 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
1613 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
1615 switch (prog
->Shaders
[i
]->Type
) {
1616 case GL_VERTEX_SHADER
:
1617 vert_shader_list
[num_vert_shaders
] = prog
->Shaders
[i
];
1620 case GL_FRAGMENT_SHADER
:
1621 frag_shader_list
[num_frag_shaders
] = prog
->Shaders
[i
];
1624 case GL_GEOMETRY_SHADER
:
1625 /* FINISHME: Support geometry shaders. */
1626 assert(prog
->Shaders
[i
]->Type
!= GL_GEOMETRY_SHADER
);
1631 /* Previous to GLSL version 1.30, different compilation units could mix and
1632 * match shading language versions. With GLSL 1.30 and later, the versions
1633 * of all shaders must match.
1635 assert(min_version
>= 100);
1636 assert(max_version
<= 130);
1637 if ((max_version
>= 130 || min_version
== 100)
1638 && min_version
!= max_version
) {
1639 linker_error(prog
, "all shaders must use same shading "
1640 "language version\n");
1644 prog
->Version
= max_version
;
1646 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1647 if (prog
->_LinkedShaders
[i
] != NULL
)
1648 ctx
->Driver
.DeleteShader(ctx
, prog
->_LinkedShaders
[i
]);
1650 prog
->_LinkedShaders
[i
] = NULL
;
1653 /* Link all shaders for a particular stage and validate the result.
1655 if (num_vert_shaders
> 0) {
1656 gl_shader
*const sh
=
1657 link_intrastage_shaders(mem_ctx
, ctx
, prog
, vert_shader_list
,
1663 if (!validate_vertex_shader_executable(prog
, sh
))
1666 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
1670 if (num_frag_shaders
> 0) {
1671 gl_shader
*const sh
=
1672 link_intrastage_shaders(mem_ctx
, ctx
, prog
, frag_shader_list
,
1678 if (!validate_fragment_shader_executable(prog
, sh
))
1681 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
],
1685 /* Here begins the inter-stage linking phase. Some initial validation is
1686 * performed, then locations are assigned for uniforms, attributes, and
1689 if (cross_validate_uniforms(prog
)) {
1692 for (prev
= 0; prev
< MESA_SHADER_TYPES
; prev
++) {
1693 if (prog
->_LinkedShaders
[prev
] != NULL
)
1697 /* Validate the inputs of each stage with the output of the preceding
1700 for (unsigned i
= prev
+ 1; i
< MESA_SHADER_TYPES
; i
++) {
1701 if (prog
->_LinkedShaders
[i
] == NULL
)
1704 if (!cross_validate_outputs_to_inputs(prog
,
1705 prog
->_LinkedShaders
[prev
],
1706 prog
->_LinkedShaders
[i
]))
1712 prog
->LinkStatus
= true;
1715 /* Do common optimization before assigning storage for attributes,
1716 * uniforms, and varyings. Later optimization could possibly make
1717 * some of that unused.
1719 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1720 if (prog
->_LinkedShaders
[i
] == NULL
)
1723 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
1724 if (!prog
->LinkStatus
)
1727 while (do_common_optimization(prog
->_LinkedShaders
[i
]->ir
, true, 32))
1731 update_array_sizes(prog
);
1733 assign_uniform_locations(prog
);
1735 /* FINISHME: The value of the max_attribute_index parameter is
1736 * FINISHME: implementation dependent based on the value of
1737 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1738 * FINISHME: at least 16, so hardcode 16 for now.
1740 if (!assign_attribute_or_color_locations(prog
, MESA_SHADER_VERTEX
, 16)) {
1744 if (!assign_attribute_or_color_locations(prog
, MESA_SHADER_FRAGMENT
, ctx
->Const
.MaxDrawBuffers
)) {
1749 for (prev
= 0; prev
< MESA_SHADER_TYPES
; prev
++) {
1750 if (prog
->_LinkedShaders
[prev
] != NULL
)
1754 for (unsigned i
= prev
+ 1; i
< MESA_SHADER_TYPES
; i
++) {
1755 if (prog
->_LinkedShaders
[i
] == NULL
)
1758 if (!assign_varying_locations(ctx
, prog
,
1759 prog
->_LinkedShaders
[prev
],
1760 prog
->_LinkedShaders
[i
])) {
1767 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] != NULL
) {
1768 demote_shader_inputs_and_outputs(prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
1772 if (prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
1773 gl_shader
*const sh
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
1775 demote_shader_inputs_and_outputs(sh
, ir_var_in
);
1776 demote_shader_inputs_and_outputs(sh
, ir_var_inout
);
1777 demote_shader_inputs_and_outputs(sh
, ir_var_out
);
1780 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] != NULL
) {
1781 gl_shader
*const sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
1783 demote_shader_inputs_and_outputs(sh
, ir_var_in
);
1786 /* OpenGL ES requires that a vertex shader and a fragment shader both be
1787 * present in a linked program. By checking for use of shading language
1788 * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
1790 if (ctx
->API
== API_OPENGLES2
|| prog
->Version
== 100) {
1791 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
1792 linker_error(prog
, "program lacks a vertex shader\n");
1793 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
1794 linker_error(prog
, "program lacks a fragment shader\n");
1798 /* FINISHME: Assign fragment shader output locations. */
1801 free(vert_shader_list
);
1803 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1804 if (prog
->_LinkedShaders
[i
] == NULL
)
1807 /* Retain any live IR, but trash the rest. */
1808 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
1811 ralloc_free(mem_ctx
);