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>
71 #include "main/core.h"
72 #include "glsl_symbol_table.h"
75 #include "program/hash_table.h"
77 #include "ir_optimization.h"
80 #include "main/shaderobj.h"
84 * Visitor that determines whether or not a variable is ever written.
86 class find_assignment_visitor
: public ir_hierarchical_visitor
{
88 find_assignment_visitor(const char *name
)
89 : name(name
), found(false)
94 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
)
96 ir_variable
*const var
= ir
->lhs
->variable_referenced();
98 if (strcmp(name
, var
->name
) == 0) {
103 return visit_continue_with_parent
;
106 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
108 exec_list_iterator sig_iter
= ir
->get_callee()->parameters
.iterator();
109 foreach_iter(exec_list_iterator
, iter
, *ir
) {
110 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
111 ir_variable
*sig_param
= (ir_variable
*)sig_iter
.get();
113 if (sig_param
->mode
== ir_var_out
||
114 sig_param
->mode
== ir_var_inout
) {
115 ir_variable
*var
= param_rval
->variable_referenced();
116 if (var
&& strcmp(name
, var
->name
) == 0) {
124 return visit_continue_with_parent
;
127 bool variable_found()
133 const char *name
; /**< Find writes to a variable with this name. */
134 bool found
; /**< Was a write to the variable found? */
139 * Visitor that determines whether or not a variable is ever read.
141 class find_deref_visitor
: public ir_hierarchical_visitor
{
143 find_deref_visitor(const char *name
)
144 : name(name
), found(false)
149 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
151 if (strcmp(this->name
, ir
->var
->name
) == 0) {
156 return visit_continue
;
159 bool variable_found() const
165 const char *name
; /**< Find writes to a variable with this name. */
166 bool found
; /**< Was a write to the variable found? */
171 linker_error_printf(gl_shader_program
*prog
, const char *fmt
, ...)
175 prog
->InfoLog
= talloc_strdup_append(prog
->InfoLog
, "error: ");
177 prog
->InfoLog
= talloc_vasprintf_append(prog
->InfoLog
, fmt
, ap
);
183 invalidate_variable_locations(gl_shader
*sh
, enum ir_variable_mode mode
,
186 foreach_list(node
, sh
->ir
) {
187 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
189 if ((var
== NULL
) || (var
->mode
!= (unsigned) mode
))
192 /* Only assign locations for generic attributes / varyings / etc.
194 if ((var
->location
>= generic_base
) && !var
->explicit_location
)
201 * Determine the number of attribute slots required for a particular type
203 * This code is here because it implements the language rules of a specific
204 * GLSL version. Since it's a property of the language and not a property of
205 * types in general, it doesn't really belong in glsl_type.
208 count_attribute_slots(const glsl_type
*t
)
210 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
212 * "A scalar input counts the same amount against this limit as a vec4,
213 * so applications may want to consider packing groups of four
214 * unrelated float inputs together into a vector to better utilize the
215 * capabilities of the underlying hardware. A matrix input will use up
216 * multiple locations. The number of locations used will equal the
217 * number of columns in the matrix."
219 * The spec does not explicitly say how arrays are counted. However, it
220 * should be safe to assume the total number of slots consumed by an array
221 * is the number of entries in the array multiplied by the number of slots
222 * consumed by a single element of the array.
226 return t
->array_size() * count_attribute_slots(t
->element_type());
229 return t
->matrix_columns
;
236 * Verify that a vertex shader executable meets all semantic requirements
238 * \param shader Vertex shader executable to be verified
241 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
242 struct gl_shader
*shader
)
247 find_assignment_visitor
find("gl_Position");
248 find
.run(shader
->ir
);
249 if (!find
.variable_found()) {
250 linker_error_printf(prog
,
251 "vertex shader does not write to `gl_Position'\n");
260 * Verify that a fragment shader executable meets all semantic requirements
262 * \param shader Fragment shader executable to be verified
265 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
266 struct gl_shader
*shader
)
271 find_assignment_visitor
frag_color("gl_FragColor");
272 find_assignment_visitor
frag_data("gl_FragData");
274 frag_color
.run(shader
->ir
);
275 frag_data
.run(shader
->ir
);
277 if (frag_color
.variable_found() && frag_data
.variable_found()) {
278 linker_error_printf(prog
, "fragment shader writes to both "
279 "`gl_FragColor' and `gl_FragData'\n");
288 * Generate a string describing the mode of a variable
291 mode_string(const ir_variable
*var
)
295 return (var
->read_only
) ? "global constant" : "global variable";
297 case ir_var_uniform
: return "uniform";
298 case ir_var_in
: return "shader input";
299 case ir_var_out
: return "shader output";
300 case ir_var_inout
: return "shader inout";
302 case ir_var_temporary
:
304 assert(!"Should not get here.");
305 return "invalid variable";
311 * Perform validation of global variables used across multiple shaders
314 cross_validate_globals(struct gl_shader_program
*prog
,
315 struct gl_shader
**shader_list
,
316 unsigned num_shaders
,
319 /* Examine all of the uniforms in all of the shaders and cross validate
322 glsl_symbol_table variables
;
323 for (unsigned i
= 0; i
< num_shaders
; i
++) {
324 if (shader_list
[i
] == NULL
)
327 foreach_list(node
, shader_list
[i
]->ir
) {
328 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
333 if (uniforms_only
&& (var
->mode
!= ir_var_uniform
))
336 /* Don't cross validate temporaries that are at global scope. These
337 * will eventually get pulled into the shaders 'main'.
339 if (var
->mode
== ir_var_temporary
)
342 /* If a global with this name has already been seen, verify that the
343 * new instance has the same type. In addition, if the globals have
344 * initializers, the values of the initializers must be the same.
346 ir_variable
*const existing
= variables
.get_variable(var
->name
);
347 if (existing
!= NULL
) {
348 if (var
->type
!= existing
->type
) {
349 /* Consider the types to be "the same" if both types are arrays
350 * of the same type and one of the arrays is implicitly sized.
351 * In addition, set the type of the linked variable to the
352 * explicitly sized array.
354 if (var
->type
->is_array()
355 && existing
->type
->is_array()
356 && (var
->type
->fields
.array
== existing
->type
->fields
.array
)
357 && ((var
->type
->length
== 0)
358 || (existing
->type
->length
== 0))) {
359 if (existing
->type
->length
== 0) {
360 existing
->type
= var
->type
;
361 existing
->max_array_access
=
362 MAX2(existing
->max_array_access
,
363 var
->max_array_access
);
366 linker_error_printf(prog
, "%s `%s' declared as type "
367 "`%s' and type `%s'\n",
369 var
->name
, var
->type
->name
,
370 existing
->type
->name
);
375 if (var
->explicit_location
) {
376 if (existing
->explicit_location
377 && (var
->location
!= existing
->location
)) {
378 linker_error_printf(prog
, "explicit locations for %s "
379 "`%s' have differing values\n",
380 mode_string(var
), var
->name
);
384 existing
->location
= var
->location
;
385 existing
->explicit_location
= true;
388 /* FINISHME: Handle non-constant initializers.
390 if (var
->constant_value
!= NULL
) {
391 if (existing
->constant_value
!= NULL
) {
392 if (!var
->constant_value
->has_value(existing
->constant_value
)) {
393 linker_error_printf(prog
, "initializers for %s "
394 "`%s' have differing values\n",
395 mode_string(var
), var
->name
);
399 /* If the first-seen instance of a particular uniform did not
400 * have an initializer but a later instance does, copy the
401 * initializer to the version stored in the symbol table.
403 /* FINISHME: This is wrong. The constant_value field should
404 * FINISHME: not be modified! Imagine a case where a shader
405 * FINISHME: without an initializer is linked in two different
406 * FINISHME: programs with shaders that have differing
407 * FINISHME: initializers. Linking with the first will
408 * FINISHME: modify the shader, and linking with the second
409 * FINISHME: will fail.
411 existing
->constant_value
=
412 var
->constant_value
->clone(talloc_parent(existing
), NULL
);
415 if (existing
->invariant
!= var
->invariant
) {
416 linker_error_printf(prog
, "declarations for %s `%s' have "
417 "mismatching invariant qualifiers\n",
418 mode_string(var
), var
->name
);
421 if (existing
->centroid
!= var
->centroid
) {
422 linker_error_printf(prog
, "declarations for %s `%s' have "
423 "mismatching centroid qualifiers\n",
424 mode_string(var
), var
->name
);
428 variables
.add_variable(var
);
437 * Perform validation of uniforms used across multiple shader stages
440 cross_validate_uniforms(struct gl_shader_program
*prog
)
442 return cross_validate_globals(prog
, prog
->_LinkedShaders
,
443 MESA_SHADER_TYPES
, true);
448 * Validate that outputs from one stage match inputs of another
451 cross_validate_outputs_to_inputs(struct gl_shader_program
*prog
,
452 gl_shader
*producer
, gl_shader
*consumer
)
454 glsl_symbol_table parameters
;
455 /* FINISHME: Figure these out dynamically. */
456 const char *const producer_stage
= "vertex";
457 const char *const consumer_stage
= "fragment";
459 /* Find all shader outputs in the "producer" stage.
461 foreach_list(node
, producer
->ir
) {
462 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
464 /* FINISHME: For geometry shaders, this should also look for inout
465 * FINISHME: variables.
467 if ((var
== NULL
) || (var
->mode
!= ir_var_out
))
470 parameters
.add_variable(var
);
474 /* Find all shader inputs in the "consumer" stage. Any variables that have
475 * matching outputs already in the symbol table must have the same type and
478 foreach_list(node
, consumer
->ir
) {
479 ir_variable
*const input
= ((ir_instruction
*) node
)->as_variable();
481 /* FINISHME: For geometry shaders, this should also look for inout
482 * FINISHME: variables.
484 if ((input
== NULL
) || (input
->mode
!= ir_var_in
))
487 ir_variable
*const output
= parameters
.get_variable(input
->name
);
488 if (output
!= NULL
) {
489 /* Check that the types match between stages.
491 if (input
->type
!= output
->type
) {
492 /* There is a bit of a special case for gl_TexCoord. This
493 * built-in is unsized by default. Appliations that variable
494 * access it must redeclare it with a size. There is some
495 * language in the GLSL spec that implies the fragment shader
496 * and vertex shader do not have to agree on this size. Other
497 * driver behave this way, and one or two applications seem to
500 * Neither declaration needs to be modified here because the array
501 * sizes are fixed later when update_array_sizes is called.
503 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
505 * "Unlike user-defined varying variables, the built-in
506 * varying variables don't have a strict one-to-one
507 * correspondence between the vertex language and the
508 * fragment language."
510 if (!output
->type
->is_array()
511 || (strncmp("gl_", output
->name
, 3) != 0)) {
512 linker_error_printf(prog
,
513 "%s shader output `%s' declared as "
514 "type `%s', but %s shader input declared "
516 producer_stage
, output
->name
,
518 consumer_stage
, input
->type
->name
);
523 /* Check that all of the qualifiers match between stages.
525 if (input
->centroid
!= output
->centroid
) {
526 linker_error_printf(prog
,
527 "%s shader output `%s' %s centroid qualifier, "
528 "but %s shader input %s centroid qualifier\n",
531 (output
->centroid
) ? "has" : "lacks",
533 (input
->centroid
) ? "has" : "lacks");
537 if (input
->invariant
!= output
->invariant
) {
538 linker_error_printf(prog
,
539 "%s shader output `%s' %s invariant qualifier, "
540 "but %s shader input %s invariant qualifier\n",
543 (output
->invariant
) ? "has" : "lacks",
545 (input
->invariant
) ? "has" : "lacks");
549 if (input
->interpolation
!= output
->interpolation
) {
550 linker_error_printf(prog
,
551 "%s shader output `%s' specifies %s "
552 "interpolation qualifier, "
553 "but %s shader input specifies %s "
554 "interpolation qualifier\n",
557 output
->interpolation_string(),
559 input
->interpolation_string());
570 * Populates a shaders symbol table with all global declarations
573 populate_symbol_table(gl_shader
*sh
)
575 sh
->symbols
= new(sh
) glsl_symbol_table
;
577 foreach_list(node
, sh
->ir
) {
578 ir_instruction
*const inst
= (ir_instruction
*) node
;
582 if ((func
= inst
->as_function()) != NULL
) {
583 sh
->symbols
->add_function(func
);
584 } else if ((var
= inst
->as_variable()) != NULL
) {
585 sh
->symbols
->add_variable(var
);
592 * Remap variables referenced in an instruction tree
594 * This is used when instruction trees are cloned from one shader and placed in
595 * another. These trees will contain references to \c ir_variable nodes that
596 * do not exist in the target shader. This function finds these \c ir_variable
597 * references and replaces the references with matching variables in the target
600 * If there is no matching variable in the target shader, a clone of the
601 * \c ir_variable is made and added to the target shader. The new variable is
602 * added to \b both the instruction stream and the symbol table.
604 * \param inst IR tree that is to be processed.
605 * \param symbols Symbol table containing global scope symbols in the
607 * \param instructions Instruction stream where new variable declarations
611 remap_variables(ir_instruction
*inst
, struct gl_shader
*target
,
614 class remap_visitor
: public ir_hierarchical_visitor
{
616 remap_visitor(struct gl_shader
*target
,
619 this->target
= target
;
620 this->symbols
= target
->symbols
;
621 this->instructions
= target
->ir
;
625 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
627 if (ir
->var
->mode
== ir_var_temporary
) {
628 ir_variable
*var
= (ir_variable
*) hash_table_find(temps
, ir
->var
);
632 return visit_continue
;
635 ir_variable
*const existing
=
636 this->symbols
->get_variable(ir
->var
->name
);
637 if (existing
!= NULL
)
640 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
642 this->symbols
->add_variable(copy
);
643 this->instructions
->push_head(copy
);
647 return visit_continue
;
651 struct gl_shader
*target
;
652 glsl_symbol_table
*symbols
;
653 exec_list
*instructions
;
657 remap_visitor
v(target
, temps
);
664 * Move non-declarations from one instruction stream to another
666 * The intended usage pattern of this function is to pass the pointer to the
667 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
668 * pointer) for \c last and \c false for \c make_copies on the first
669 * call. Successive calls pass the return value of the previous call for
670 * \c last and \c true for \c make_copies.
672 * \param instructions Source instruction stream
673 * \param last Instruction after which new instructions should be
674 * inserted in the target instruction stream
675 * \param make_copies Flag selecting whether instructions in \c instructions
676 * should be copied (via \c ir_instruction::clone) into the
677 * target list or moved.
680 * The new "last" instruction in the target instruction stream. This pointer
681 * is suitable for use as the \c last parameter of a later call to this
685 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
686 bool make_copies
, gl_shader
*target
)
688 hash_table
*temps
= NULL
;
691 temps
= hash_table_ctor(0, hash_table_pointer_hash
,
692 hash_table_pointer_compare
);
694 foreach_list_safe(node
, instructions
) {
695 ir_instruction
*inst
= (ir_instruction
*) node
;
697 if (inst
->as_function())
700 ir_variable
*var
= inst
->as_variable();
701 if ((var
!= NULL
) && (var
->mode
!= ir_var_temporary
))
704 assert(inst
->as_assignment()
705 || ((var
!= NULL
) && (var
->mode
== ir_var_temporary
)));
708 inst
= inst
->clone(target
, NULL
);
711 hash_table_insert(temps
, inst
, var
);
713 remap_variables(inst
, target
, temps
);
718 last
->insert_after(inst
);
723 hash_table_dtor(temps
);
729 * Get the function signature for main from a shader
731 static ir_function_signature
*
732 get_main_function_signature(gl_shader
*sh
)
734 ir_function
*const f
= sh
->symbols
->get_function("main");
736 exec_list void_parameters
;
738 /* Look for the 'void main()' signature and ensure that it's defined.
739 * This keeps the linker from accidentally pick a shader that just
740 * contains a prototype for main.
742 * We don't have to check for multiple definitions of main (in multiple
743 * shaders) because that would have already been caught above.
745 ir_function_signature
*sig
= f
->matching_signature(&void_parameters
);
746 if ((sig
!= NULL
) && sig
->is_defined
) {
756 * Combine a group of shaders for a single stage to generate a linked shader
759 * If this function is supplied a single shader, it is cloned, and the new
760 * shader is returned.
762 static struct gl_shader
*
763 link_intrastage_shaders(void *mem_ctx
,
764 struct gl_context
*ctx
,
765 struct gl_shader_program
*prog
,
766 struct gl_shader
**shader_list
,
767 unsigned num_shaders
)
769 /* Check that global variables defined in multiple shaders are consistent.
771 if (!cross_validate_globals(prog
, shader_list
, num_shaders
, false))
774 /* Check that there is only a single definition of each function signature
775 * across all shaders.
777 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
778 foreach_list(node
, shader_list
[i
]->ir
) {
779 ir_function
*const f
= ((ir_instruction
*) node
)->as_function();
784 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
785 ir_function
*const other
=
786 shader_list
[j
]->symbols
->get_function(f
->name
);
788 /* If the other shader has no function (and therefore no function
789 * signatures) with the same name, skip to the next shader.
794 foreach_iter (exec_list_iterator
, iter
, *f
) {
795 ir_function_signature
*sig
=
796 (ir_function_signature
*) iter
.get();
798 if (!sig
->is_defined
|| sig
->is_builtin
)
801 ir_function_signature
*other_sig
=
802 other
->exact_matching_signature(& sig
->parameters
);
804 if ((other_sig
!= NULL
) && other_sig
->is_defined
805 && !other_sig
->is_builtin
) {
806 linker_error_printf(prog
,
807 "function `%s' is multiply defined",
816 /* Find the shader that defines main, and make a clone of it.
818 * Starting with the clone, search for undefined references. If one is
819 * found, find the shader that defines it. Clone the reference and add
820 * it to the shader. Repeat until there are no undefined references or
821 * until a reference cannot be resolved.
823 gl_shader
*main
= NULL
;
824 for (unsigned i
= 0; i
< num_shaders
; i
++) {
825 if (get_main_function_signature(shader_list
[i
]) != NULL
) {
826 main
= shader_list
[i
];
832 linker_error_printf(prog
, "%s shader lacks `main'\n",
833 (shader_list
[0]->Type
== GL_VERTEX_SHADER
)
834 ? "vertex" : "fragment");
838 gl_shader
*linked
= ctx
->Driver
.NewShader(NULL
, 0, main
->Type
);
839 linked
->ir
= new(linked
) exec_list
;
840 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
842 populate_symbol_table(linked
);
844 /* The a pointer to the main function in the final linked shader (i.e., the
845 * copy of the original shader that contained the main function).
847 ir_function_signature
*const main_sig
= get_main_function_signature(linked
);
849 /* Move any instructions other than variable declarations or function
850 * declarations into main.
852 exec_node
*insertion_point
=
853 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
856 for (unsigned i
= 0; i
< num_shaders
; i
++) {
857 if (shader_list
[i
] == main
)
860 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
861 insertion_point
, true, linked
);
864 /* Resolve initializers for global variables in the linked shader.
866 unsigned num_linking_shaders
= num_shaders
;
867 for (unsigned i
= 0; i
< num_shaders
; i
++)
868 num_linking_shaders
+= shader_list
[i
]->num_builtins_to_link
;
870 gl_shader
**linking_shaders
=
871 (gl_shader
**) calloc(num_linking_shaders
, sizeof(gl_shader
*));
873 memcpy(linking_shaders
, shader_list
,
874 sizeof(linking_shaders
[0]) * num_shaders
);
876 unsigned idx
= num_shaders
;
877 for (unsigned i
= 0; i
< num_shaders
; i
++) {
878 memcpy(&linking_shaders
[idx
], shader_list
[i
]->builtins_to_link
,
879 sizeof(linking_shaders
[0]) * shader_list
[i
]->num_builtins_to_link
);
880 idx
+= shader_list
[i
]->num_builtins_to_link
;
883 assert(idx
== num_linking_shaders
);
885 if (!link_function_calls(prog
, linked
, linking_shaders
,
886 num_linking_shaders
)) {
887 ctx
->Driver
.DeleteShader(ctx
, linked
);
891 free(linking_shaders
);
893 /* Make a pass over all global variables to ensure that arrays with
894 * unspecified sizes have a size specified. The size is inferred from the
895 * max_array_access field.
897 if (linked
!= NULL
) {
898 foreach_list(node
, linked
->ir
) {
899 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
904 if ((var
->mode
!= ir_var_auto
) && (var
->mode
!= ir_var_temporary
))
907 if (!var
->type
->is_array() || (var
->type
->length
!= 0))
910 const glsl_type
*type
=
911 glsl_type::get_array_instance(var
->type
->fields
.array
,
912 var
->max_array_access
);
914 assert(type
!= NULL
);
923 struct uniform_node
{
925 struct gl_uniform
*u
;
930 * Update the sizes of linked shader uniform arrays to the maximum
933 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
935 * If one or more elements of an array are active,
936 * GetActiveUniform will return the name of the array in name,
937 * subject to the restrictions listed above. The type of the array
938 * is returned in type. The size parameter contains the highest
939 * array element index used, plus one. The compiler or linker
940 * determines the highest index used. There will be only one
941 * active uniform reported by the GL per uniform array.
945 update_array_sizes(struct gl_shader_program
*prog
)
947 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
948 if (prog
->_LinkedShaders
[i
] == NULL
)
951 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
952 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
954 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
&&
955 var
->mode
!= ir_var_in
&&
956 var
->mode
!= ir_var_out
) ||
957 !var
->type
->is_array())
960 unsigned int size
= var
->max_array_access
;
961 for (unsigned j
= 0; j
< MESA_SHADER_TYPES
; j
++) {
962 if (prog
->_LinkedShaders
[j
] == NULL
)
965 foreach_list(node2
, prog
->_LinkedShaders
[j
]->ir
) {
966 ir_variable
*other_var
= ((ir_instruction
*) node2
)->as_variable();
970 if (strcmp(var
->name
, other_var
->name
) == 0 &&
971 other_var
->max_array_access
> size
) {
972 size
= other_var
->max_array_access
;
977 if (size
+ 1 != var
->type
->fields
.array
->length
) {
978 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
980 /* FINISHME: We should update the types of array
981 * dereferences of this variable now.
989 add_uniform(void *mem_ctx
, exec_list
*uniforms
, struct hash_table
*ht
,
990 const char *name
, const glsl_type
*type
, GLenum shader_type
,
991 unsigned *next_shader_pos
, unsigned *total_uniforms
)
993 if (type
->is_record()) {
994 for (unsigned int i
= 0; i
< type
->length
; i
++) {
995 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
996 char *field_name
= talloc_asprintf(mem_ctx
, "%s.%s", name
,
997 type
->fields
.structure
[i
].name
);
999 add_uniform(mem_ctx
, uniforms
, ht
, field_name
, field_type
,
1000 shader_type
, next_shader_pos
, total_uniforms
);
1003 uniform_node
*n
= (uniform_node
*) hash_table_find(ht
, name
);
1004 unsigned int vec4_slots
;
1005 const glsl_type
*array_elem_type
= NULL
;
1007 if (type
->is_array()) {
1008 array_elem_type
= type
->fields
.array
;
1009 /* Array of structures. */
1010 if (array_elem_type
->is_record()) {
1011 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1012 char *elem_name
= talloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
1013 add_uniform(mem_ctx
, uniforms
, ht
, elem_name
, array_elem_type
,
1014 shader_type
, next_shader_pos
, total_uniforms
);
1020 /* Fix the storage size of samplers at 1 vec4 each. Be sure to pad out
1021 * vectors to vec4 slots.
1023 if (type
->is_array()) {
1024 if (array_elem_type
->is_sampler())
1025 vec4_slots
= type
->length
;
1027 vec4_slots
= type
->length
* array_elem_type
->matrix_columns
;
1028 } else if (type
->is_sampler()) {
1031 vec4_slots
= type
->matrix_columns
;
1035 n
= (uniform_node
*) calloc(1, sizeof(struct uniform_node
));
1036 n
->u
= (gl_uniform
*) calloc(1, sizeof(struct gl_uniform
));
1037 n
->slots
= vec4_slots
;
1039 n
->u
->Name
= strdup(name
);
1044 (*total_uniforms
)++;
1046 hash_table_insert(ht
, n
, name
);
1047 uniforms
->push_tail(& n
->link
);
1050 switch (shader_type
) {
1051 case GL_VERTEX_SHADER
:
1052 n
->u
->VertPos
= *next_shader_pos
;
1054 case GL_FRAGMENT_SHADER
:
1055 n
->u
->FragPos
= *next_shader_pos
;
1057 case GL_GEOMETRY_SHADER
:
1058 n
->u
->GeomPos
= *next_shader_pos
;
1062 (*next_shader_pos
) += vec4_slots
;
1067 assign_uniform_locations(struct gl_shader_program
*prog
)
1071 unsigned total_uniforms
= 0;
1072 hash_table
*ht
= hash_table_ctor(32, hash_table_string_hash
,
1073 hash_table_string_compare
);
1074 void *mem_ctx
= talloc_new(NULL
);
1076 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1077 if (prog
->_LinkedShaders
[i
] == NULL
)
1080 unsigned next_position
= 0;
1082 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
1083 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1085 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
1088 if (strncmp(var
->name
, "gl_", 3) == 0) {
1089 /* At the moment, we don't allocate uniform locations for
1090 * builtin uniforms. It's permitted by spec, and we'll
1091 * likely switch to doing that at some point, but not yet.
1096 var
->location
= next_position
;
1097 add_uniform(mem_ctx
, &uniforms
, ht
, var
->name
, var
->type
,
1098 prog
->_LinkedShaders
[i
]->Type
,
1099 &next_position
, &total_uniforms
);
1103 talloc_free(mem_ctx
);
1105 gl_uniform_list
*ul
= (gl_uniform_list
*)
1106 calloc(1, sizeof(gl_uniform_list
));
1108 ul
->Size
= total_uniforms
;
1109 ul
->NumUniforms
= total_uniforms
;
1110 ul
->Uniforms
= (gl_uniform
*) calloc(total_uniforms
, sizeof(gl_uniform
));
1114 for (uniform_node
*node
= (uniform_node
*) uniforms
.head
1115 ; node
->link
.next
!= NULL
1117 next
= (uniform_node
*) node
->link
.next
;
1119 node
->link
.remove();
1120 memcpy(&ul
->Uniforms
[idx
], node
->u
, sizeof(gl_uniform
));
1127 hash_table_dtor(ht
);
1129 prog
->Uniforms
= ul
;
1134 * Find a contiguous set of available bits in a bitmask
1136 * \param used_mask Bits representing used (1) and unused (0) locations
1137 * \param needed_count Number of contiguous bits needed.
1140 * Base location of the available bits on success or -1 on failure.
1143 find_available_slots(unsigned used_mask
, unsigned needed_count
)
1145 unsigned needed_mask
= (1 << needed_count
) - 1;
1146 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
1148 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1149 * cannot optimize possibly infinite loops" for the loop below.
1151 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
1154 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
1155 if ((needed_mask
& ~used_mask
) == needed_mask
)
1166 assign_attribute_locations(gl_shader_program
*prog
, unsigned max_attribute_index
)
1168 /* Mark invalid attribute locations as being used.
1170 unsigned used_locations
= (max_attribute_index
>= 32)
1171 ? ~0 : ~((1 << max_attribute_index
) - 1);
1173 gl_shader
*const sh
= prog
->_LinkedShaders
[0];
1174 assert(sh
->Type
== GL_VERTEX_SHADER
);
1176 /* Operate in a total of four passes.
1178 * 1. Invalidate the location assignments for all vertex shader inputs.
1180 * 2. Assign locations for inputs that have user-defined (via
1181 * glBindVertexAttribLocation) locatoins.
1183 * 3. Sort the attributes without assigned locations by number of slots
1184 * required in decreasing order. Fragmentation caused by attribute
1185 * locations assigned by the application may prevent large attributes
1186 * from having enough contiguous space.
1188 * 4. Assign locations to any inputs without assigned locations.
1191 invalidate_variable_locations(sh
, ir_var_in
, VERT_ATTRIB_GENERIC0
);
1193 if (prog
->Attributes
!= NULL
) {
1194 for (unsigned i
= 0; i
< prog
->Attributes
->NumParameters
; i
++) {
1195 ir_variable
*const var
=
1196 sh
->symbols
->get_variable(prog
->Attributes
->Parameters
[i
].Name
);
1198 /* Note: attributes that occupy multiple slots, such as arrays or
1199 * matrices, may appear in the attrib array multiple times.
1201 if ((var
== NULL
) || (var
->location
!= -1))
1204 /* From page 61 of the OpenGL 4.0 spec:
1206 * "LinkProgram will fail if the attribute bindings assigned by
1207 * BindAttribLocation do not leave not enough space to assign a
1208 * location for an active matrix attribute or an active attribute
1209 * array, both of which require multiple contiguous generic
1212 * Previous versions of the spec contain similar language but omit the
1213 * bit about attribute arrays.
1215 * Page 61 of the OpenGL 4.0 spec also says:
1217 * "It is possible for an application to bind more than one
1218 * attribute name to the same location. This is referred to as
1219 * aliasing. This will only work if only one of the aliased
1220 * attributes is active in the executable program, or if no path
1221 * through the shader consumes more than one attribute of a set
1222 * of attributes aliased to the same location. A link error can
1223 * occur if the linker determines that every path through the
1224 * shader consumes multiple aliased attributes, but
1225 * implementations are not required to generate an error in this
1228 * These two paragraphs are either somewhat contradictory, or I don't
1229 * fully understand one or both of them.
1231 /* FINISHME: The code as currently written does not support attribute
1232 * FINISHME: location aliasing (see comment above).
1234 const int attr
= prog
->Attributes
->Parameters
[i
].StateIndexes
[0];
1235 const unsigned slots
= count_attribute_slots(var
->type
);
1237 /* Mask representing the contiguous slots that will be used by this
1240 const unsigned use_mask
= (1 << slots
) - 1;
1242 /* Generate a link error if the set of bits requested for this
1243 * attribute overlaps any previously allocated bits.
1245 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
1246 linker_error_printf(prog
,
1247 "insufficient contiguous attribute locations "
1248 "available for vertex shader input `%s'",
1253 var
->location
= VERT_ATTRIB_GENERIC0
+ attr
;
1254 used_locations
|= (use_mask
<< attr
);
1258 /* Temporary storage for the set of attributes that need locations assigned.
1264 /* Used below in the call to qsort. */
1265 static int compare(const void *a
, const void *b
)
1267 const temp_attr
*const l
= (const temp_attr
*) a
;
1268 const temp_attr
*const r
= (const temp_attr
*) b
;
1270 /* Reversed because we want a descending order sort below. */
1271 return r
->slots
- l
->slots
;
1275 unsigned num_attr
= 0;
1277 foreach_list(node
, sh
->ir
) {
1278 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1280 if ((var
== NULL
) || (var
->mode
!= ir_var_in
))
1283 if (var
->explicit_location
) {
1284 const unsigned slots
= count_attribute_slots(var
->type
);
1285 const unsigned use_mask
= (1 << slots
) - 1;
1286 const int attr
= var
->location
- VERT_ATTRIB_GENERIC0
;
1288 if ((var
->location
>= (int)(max_attribute_index
+ VERT_ATTRIB_GENERIC0
))
1289 || (var
->location
< 0)) {
1290 linker_error_printf(prog
,
1291 "invalid explicit location %d specified for "
1293 (var
->location
< 0) ? var
->location
: attr
,
1296 } else if (var
->location
>= VERT_ATTRIB_GENERIC0
) {
1297 used_locations
|= (use_mask
<< attr
);
1301 /* The location was explicitly assigned, nothing to do here.
1303 if (var
->location
!= -1)
1306 to_assign
[num_attr
].slots
= count_attribute_slots(var
->type
);
1307 to_assign
[num_attr
].var
= var
;
1311 /* If all of the attributes were assigned locations by the application (or
1312 * are built-in attributes with fixed locations), return early. This should
1313 * be the common case.
1318 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
1320 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
1321 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
1322 * to prevent it from being automatically allocated below.
1324 find_deref_visitor
find("gl_Vertex");
1326 if (find
.variable_found())
1327 used_locations
|= (1 << 0);
1329 for (unsigned i
= 0; i
< num_attr
; i
++) {
1330 /* Mask representing the contiguous slots that will be used by this
1333 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
1335 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
1338 linker_error_printf(prog
,
1339 "insufficient contiguous attribute locations "
1340 "available for vertex shader input `%s'",
1341 to_assign
[i
].var
->name
);
1345 to_assign
[i
].var
->location
= VERT_ATTRIB_GENERIC0
+ location
;
1346 used_locations
|= (use_mask
<< location
);
1354 * Demote shader inputs and outputs that are not used in other stages
1357 demote_shader_inputs_and_outputs(gl_shader
*sh
, enum ir_variable_mode mode
)
1359 foreach_list(node
, sh
->ir
) {
1360 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1362 if ((var
== NULL
) || (var
->mode
!= int(mode
)))
1365 /* A shader 'in' or 'out' variable is only really an input or output if
1366 * its value is used by other shader stages. This will cause the variable
1367 * to have a location assigned.
1369 if (var
->location
== -1) {
1370 var
->mode
= ir_var_auto
;
1377 assign_varying_locations(struct gl_shader_program
*prog
,
1378 gl_shader
*producer
, gl_shader
*consumer
)
1380 /* FINISHME: Set dynamically when geometry shader support is added. */
1381 unsigned output_index
= VERT_RESULT_VAR0
;
1382 unsigned input_index
= FRAG_ATTRIB_VAR0
;
1384 /* Operate in a total of three passes.
1386 * 1. Assign locations for any matching inputs and outputs.
1388 * 2. Mark output variables in the producer that do not have locations as
1389 * not being outputs. This lets the optimizer eliminate them.
1391 * 3. Mark input variables in the consumer that do not have locations as
1392 * not being inputs. This lets the optimizer eliminate them.
1395 invalidate_variable_locations(producer
, ir_var_out
, VERT_RESULT_VAR0
);
1396 invalidate_variable_locations(consumer
, ir_var_in
, FRAG_ATTRIB_VAR0
);
1398 foreach_list(node
, producer
->ir
) {
1399 ir_variable
*const output_var
= ((ir_instruction
*) node
)->as_variable();
1401 if ((output_var
== NULL
) || (output_var
->mode
!= ir_var_out
)
1402 || (output_var
->location
!= -1))
1405 ir_variable
*const input_var
=
1406 consumer
->symbols
->get_variable(output_var
->name
);
1408 if ((input_var
== NULL
) || (input_var
->mode
!= ir_var_in
))
1411 assert(input_var
->location
== -1);
1413 output_var
->location
= output_index
;
1414 input_var
->location
= input_index
;
1416 /* FINISHME: Support for "varying" records in GLSL 1.50. */
1417 assert(!output_var
->type
->is_record());
1419 if (output_var
->type
->is_array()) {
1420 const unsigned slots
= output_var
->type
->length
1421 * output_var
->type
->fields
.array
->matrix_columns
;
1423 output_index
+= slots
;
1424 input_index
+= slots
;
1426 const unsigned slots
= output_var
->type
->matrix_columns
;
1428 output_index
+= slots
;
1429 input_index
+= slots
;
1433 foreach_list(node
, consumer
->ir
) {
1434 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1436 if ((var
== NULL
) || (var
->mode
!= ir_var_in
))
1439 if (var
->location
== -1) {
1440 if (prog
->Version
<= 120) {
1441 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1443 * Only those varying variables used (i.e. read) in
1444 * the fragment shader executable must be written to
1445 * by the vertex shader executable; declaring
1446 * superfluous varying variables in a vertex shader is
1449 * We interpret this text as meaning that the VS must
1450 * write the variable for the FS to read it. See
1451 * "glsl1-varying read but not written" in piglit.
1454 linker_error_printf(prog
, "fragment shader varying %s not written "
1455 "by vertex shader\n.", var
->name
);
1456 prog
->LinkStatus
= false;
1459 /* An 'in' variable is only really a shader input if its
1460 * value is written by the previous stage.
1462 var
->mode
= ir_var_auto
;
1469 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
1471 void *mem_ctx
= talloc_init("temporary linker context");
1473 prog
->LinkStatus
= false;
1474 prog
->Validated
= false;
1475 prog
->_Used
= false;
1477 if (prog
->InfoLog
!= NULL
)
1478 talloc_free(prog
->InfoLog
);
1480 prog
->InfoLog
= talloc_strdup(NULL
, "");
1482 /* Separate the shaders into groups based on their type.
1484 struct gl_shader
**vert_shader_list
;
1485 unsigned num_vert_shaders
= 0;
1486 struct gl_shader
**frag_shader_list
;
1487 unsigned num_frag_shaders
= 0;
1489 vert_shader_list
= (struct gl_shader
**)
1490 calloc(2 * prog
->NumShaders
, sizeof(struct gl_shader
*));
1491 frag_shader_list
= &vert_shader_list
[prog
->NumShaders
];
1493 unsigned min_version
= UINT_MAX
;
1494 unsigned max_version
= 0;
1495 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
1496 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
1497 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
1499 switch (prog
->Shaders
[i
]->Type
) {
1500 case GL_VERTEX_SHADER
:
1501 vert_shader_list
[num_vert_shaders
] = prog
->Shaders
[i
];
1504 case GL_FRAGMENT_SHADER
:
1505 frag_shader_list
[num_frag_shaders
] = prog
->Shaders
[i
];
1508 case GL_GEOMETRY_SHADER
:
1509 /* FINISHME: Support geometry shaders. */
1510 assert(prog
->Shaders
[i
]->Type
!= GL_GEOMETRY_SHADER
);
1515 /* Previous to GLSL version 1.30, different compilation units could mix and
1516 * match shading language versions. With GLSL 1.30 and later, the versions
1517 * of all shaders must match.
1519 assert(min_version
>= 100);
1520 assert(max_version
<= 130);
1521 if ((max_version
>= 130 || min_version
== 100)
1522 && min_version
!= max_version
) {
1523 linker_error_printf(prog
, "all shaders must use same shading "
1524 "language version\n");
1528 prog
->Version
= max_version
;
1530 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1531 if (prog
->_LinkedShaders
[i
] != NULL
)
1532 ctx
->Driver
.DeleteShader(ctx
, prog
->_LinkedShaders
[i
]);
1534 prog
->_LinkedShaders
[i
] = NULL
;
1537 /* Link all shaders for a particular stage and validate the result.
1539 if (num_vert_shaders
> 0) {
1540 gl_shader
*const sh
=
1541 link_intrastage_shaders(mem_ctx
, ctx
, prog
, vert_shader_list
,
1547 if (!validate_vertex_shader_executable(prog
, sh
))
1550 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
1554 if (num_frag_shaders
> 0) {
1555 gl_shader
*const sh
=
1556 link_intrastage_shaders(mem_ctx
, ctx
, prog
, frag_shader_list
,
1562 if (!validate_fragment_shader_executable(prog
, sh
))
1565 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
],
1569 /* Here begins the inter-stage linking phase. Some initial validation is
1570 * performed, then locations are assigned for uniforms, attributes, and
1573 if (cross_validate_uniforms(prog
)) {
1576 for (prev
= 0; prev
< MESA_SHADER_TYPES
; prev
++) {
1577 if (prog
->_LinkedShaders
[prev
] != NULL
)
1581 /* Validate the inputs of each stage with the output of the preceeding
1584 for (unsigned i
= prev
+ 1; i
< MESA_SHADER_TYPES
; i
++) {
1585 if (prog
->_LinkedShaders
[i
] == NULL
)
1588 if (!cross_validate_outputs_to_inputs(prog
,
1589 prog
->_LinkedShaders
[prev
],
1590 prog
->_LinkedShaders
[i
]))
1596 prog
->LinkStatus
= true;
1599 /* Do common optimization before assigning storage for attributes,
1600 * uniforms, and varyings. Later optimization could possibly make
1601 * some of that unused.
1603 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1604 if (prog
->_LinkedShaders
[i
] == NULL
)
1607 while (do_common_optimization(prog
->_LinkedShaders
[i
]->ir
, true, 32))
1611 update_array_sizes(prog
);
1613 assign_uniform_locations(prog
);
1615 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] != NULL
) {
1616 /* FINISHME: The value of the max_attribute_index parameter is
1617 * FINISHME: implementation dependent based on the value of
1618 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1619 * FINISHME: at least 16, so hardcode 16 for now.
1621 if (!assign_attribute_locations(prog
, 16)) {
1622 prog
->LinkStatus
= false;
1628 for (prev
= 0; prev
< MESA_SHADER_TYPES
; prev
++) {
1629 if (prog
->_LinkedShaders
[prev
] != NULL
)
1633 for (unsigned i
= prev
+ 1; i
< MESA_SHADER_TYPES
; i
++) {
1634 if (prog
->_LinkedShaders
[i
] == NULL
)
1637 assign_varying_locations(prog
,
1638 prog
->_LinkedShaders
[prev
],
1639 prog
->_LinkedShaders
[i
]);
1643 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] != NULL
) {
1644 demote_shader_inputs_and_outputs(prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
1648 if (prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
1649 gl_shader
*const sh
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
1651 demote_shader_inputs_and_outputs(sh
, ir_var_in
);
1652 demote_shader_inputs_and_outputs(sh
, ir_var_inout
);
1653 demote_shader_inputs_and_outputs(sh
, ir_var_out
);
1656 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] != NULL
) {
1657 gl_shader
*const sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
1659 demote_shader_inputs_and_outputs(sh
, ir_var_in
);
1662 /* FINISHME: Assign fragment shader output locations. */
1665 free(vert_shader_list
);
1667 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1668 if (prog
->_LinkedShaders
[i
] == NULL
)
1671 /* Retain any live IR, but trash the rest. */
1672 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
1675 talloc_free(mem_ctx
);