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>
74 #include "main/mtypes.h"
75 #include "glsl_symbol_table.h"
76 #include "glsl_parser_extras.h"
78 #include "ir_optimization.h"
80 #include "hash_table.h"
81 #include "shader_api.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 bool variable_found()
112 const char *name
; /**< Find writes to a variable with this name. */
113 bool found
; /**< Was a write to the variable found? */
118 linker_error_printf(gl_shader_program
*prog
, const char *fmt
, ...)
122 prog
->InfoLog
= talloc_strdup_append(prog
->InfoLog
, "error: ");
124 prog
->InfoLog
= talloc_vasprintf_append(prog
->InfoLog
, fmt
, ap
);
130 invalidate_variable_locations(gl_shader
*sh
, enum ir_variable_mode mode
,
133 foreach_list(node
, sh
->ir
) {
134 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
136 if ((var
== NULL
) || (var
->mode
!= (unsigned) mode
))
139 /* Only assign locations for generic attributes / varyings / etc.
141 if (var
->location
>= generic_base
)
148 * Determine the number of attribute slots required for a particular type
150 * This code is here because it implements the language rules of a specific
151 * GLSL version. Since it's a property of the language and not a property of
152 * types in general, it doesn't really belong in glsl_type.
155 count_attribute_slots(const glsl_type
*t
)
157 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
159 * "A scalar input counts the same amount against this limit as a vec4,
160 * so applications may want to consider packing groups of four
161 * unrelated float inputs together into a vector to better utilize the
162 * capabilities of the underlying hardware. A matrix input will use up
163 * multiple locations. The number of locations used will equal the
164 * number of columns in the matrix."
166 * The spec does not explicitly say how arrays are counted. However, it
167 * should be safe to assume the total number of slots consumed by an array
168 * is the number of entries in the array multiplied by the number of slots
169 * consumed by a single element of the array.
173 return t
->array_size() * count_attribute_slots(t
->element_type());
176 return t
->matrix_columns
;
183 * Verify that a vertex shader executable meets all semantic requirements
185 * \param shader Vertex shader executable to be verified
188 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
189 struct gl_shader
*shader
)
194 if (!shader
->symbols
->get_function("main")) {
195 linker_error_printf(prog
, "vertex shader lacks `main'\n");
199 find_assignment_visitor
find("gl_Position");
200 find
.run(shader
->ir
);
201 if (!find
.variable_found()) {
202 linker_error_printf(prog
,
203 "vertex shader does not write to `gl_Position'\n");
212 * Verify that a fragment shader executable meets all semantic requirements
214 * \param shader Fragment shader executable to be verified
217 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
218 struct gl_shader
*shader
)
223 if (!shader
->symbols
->get_function("main")) {
224 linker_error_printf(prog
, "fragment shader lacks `main'\n");
228 find_assignment_visitor
frag_color("gl_FragColor");
229 find_assignment_visitor
frag_data("gl_FragData");
231 frag_color
.run(shader
->ir
);
232 frag_data
.run(shader
->ir
);
234 if (frag_color
.variable_found() && frag_data
.variable_found()) {
235 linker_error_printf(prog
, "fragment shader writes to both "
236 "`gl_FragColor' and `gl_FragData'\n");
245 * Generate a string describing the mode of a variable
248 mode_string(const ir_variable
*var
)
252 return (var
->read_only
) ? "global constant" : "global variable";
254 case ir_var_uniform
: return "uniform";
255 case ir_var_in
: return "shader input";
256 case ir_var_out
: return "shader output";
257 case ir_var_inout
: return "shader inout";
259 assert(!"Should not get here.");
260 return "invalid variable";
266 * Perform validation of global variables used across multiple shaders
269 cross_validate_globals(struct gl_shader_program
*prog
,
270 struct gl_shader
**shader_list
,
271 unsigned num_shaders
,
274 /* Examine all of the uniforms in all of the shaders and cross validate
277 glsl_symbol_table variables
;
278 for (unsigned i
= 0; i
< num_shaders
; i
++) {
279 foreach_list(node
, shader_list
[i
]->ir
) {
280 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
285 if (uniforms_only
&& (var
->mode
!= ir_var_uniform
))
288 /* If a global with this name has already been seen, verify that the
289 * new instance has the same type. In addition, if the globals have
290 * initializers, the values of the initializers must be the same.
292 ir_variable
*const existing
= variables
.get_variable(var
->name
);
293 if (existing
!= NULL
) {
294 if (var
->type
!= existing
->type
) {
295 linker_error_printf(prog
, "%s `%s' declared as type "
296 "`%s' and type `%s'\n",
298 var
->name
, var
->type
->name
,
299 existing
->type
->name
);
303 /* FINISHME: Handle non-constant initializers.
305 if (var
->constant_value
!= NULL
) {
306 if (existing
->constant_value
!= NULL
) {
307 if (!var
->constant_value
->has_value(existing
->constant_value
)) {
308 linker_error_printf(prog
, "initializers for %s "
309 "`%s' have differing values\n",
310 mode_string(var
), var
->name
);
314 /* If the first-seen instance of a particular uniform did not
315 * have an initializer but a later instance does, copy the
316 * initializer to the version stored in the symbol table.
318 existing
->constant_value
= var
->constant_value
->clone(NULL
);
321 variables
.add_variable(var
->name
, var
);
330 * Perform validation of uniforms used across multiple shader stages
333 cross_validate_uniforms(struct gl_shader_program
*prog
)
335 return cross_validate_globals(prog
, prog
->_LinkedShaders
,
336 prog
->_NumLinkedShaders
, true);
341 * Validate that outputs from one stage match inputs of another
344 cross_validate_outputs_to_inputs(struct gl_shader_program
*prog
,
345 gl_shader
*producer
, gl_shader
*consumer
)
347 glsl_symbol_table parameters
;
348 /* FINISHME: Figure these out dynamically. */
349 const char *const producer_stage
= "vertex";
350 const char *const consumer_stage
= "fragment";
352 /* Find all shader outputs in the "producer" stage.
354 foreach_list(node
, producer
->ir
) {
355 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
357 /* FINISHME: For geometry shaders, this should also look for inout
358 * FINISHME: variables.
360 if ((var
== NULL
) || (var
->mode
!= ir_var_out
))
363 parameters
.add_variable(var
->name
, var
);
367 /* Find all shader inputs in the "consumer" stage. Any variables that have
368 * matching outputs already in the symbol table must have the same type and
371 foreach_list(node
, consumer
->ir
) {
372 ir_variable
*const input
= ((ir_instruction
*) node
)->as_variable();
374 /* FINISHME: For geometry shaders, this should also look for inout
375 * FINISHME: variables.
377 if ((input
== NULL
) || (input
->mode
!= ir_var_in
))
380 ir_variable
*const output
= parameters
.get_variable(input
->name
);
381 if (output
!= NULL
) {
382 /* Check that the types match between stages.
384 if (input
->type
!= output
->type
) {
385 linker_error_printf(prog
,
386 "%s shader output `%s' delcared as "
387 "type `%s', but %s shader input declared "
389 producer_stage
, output
->name
,
391 consumer_stage
, input
->type
->name
);
395 /* Check that all of the qualifiers match between stages.
397 if (input
->centroid
!= output
->centroid
) {
398 linker_error_printf(prog
,
399 "%s shader output `%s' %s centroid qualifier, "
400 "but %s shader input %s centroid qualifier\n",
403 (output
->centroid
) ? "has" : "lacks",
405 (input
->centroid
) ? "has" : "lacks");
409 if (input
->invariant
!= output
->invariant
) {
410 linker_error_printf(prog
,
411 "%s shader output `%s' %s invariant qualifier, "
412 "but %s shader input %s invariant qualifier\n",
415 (output
->invariant
) ? "has" : "lacks",
417 (input
->invariant
) ? "has" : "lacks");
421 if (input
->interpolation
!= output
->interpolation
) {
422 linker_error_printf(prog
,
423 "%s shader output `%s' specifies %s "
424 "interpolation qualifier, "
425 "but %s shader input specifies %s "
426 "interpolation qualifier\n",
429 output
->interpolation_string(),
431 input
->interpolation_string());
442 * Populates a shaders symbol table with all global declarations
445 populate_symbol_table(gl_shader
*sh
)
447 sh
->symbols
= new(sh
) glsl_symbol_table
;
449 foreach_list(node
, sh
->ir
) {
450 ir_instruction
*const inst
= (ir_instruction
*) node
;
454 if ((func
= inst
->as_function()) != NULL
) {
455 sh
->symbols
->add_function(func
->name
, func
);
456 } else if ((var
= inst
->as_variable()) != NULL
) {
457 sh
->symbols
->add_variable(var
->name
, var
);
464 * Remap variables referenced in an instruction tree
466 * This is used when instruction trees are cloned from one shader and placed in
467 * another. These trees will contain references to \c ir_variable nodes that
468 * do not exist in the target shader. This function finds these \c ir_variable
469 * references and replaces the references with matching variables in the target
472 * If there is no matching variable in the target shader, a clone of the
473 * \c ir_variable is made and added to the target shader. The new variable is
474 * added to \b both the instruction stream and the symbol table.
476 * \param inst IR tree that is to be processed.
477 * \param symbols Symbol table containing global scope symbols in the
479 * \param instructions Instruction stream where new variable declarations
483 remap_variables(ir_instruction
*inst
, glsl_symbol_table
*symbols
,
484 exec_list
*instructions
)
486 class remap_visitor
: public ir_hierarchical_visitor
{
488 remap_visitor(glsl_symbol_table
*symbols
, exec_list
*instructions
)
490 this->symbols
= symbols
;
491 this->instructions
= instructions
;
494 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
496 ir_variable
*const existing
=
497 this->symbols
->get_variable(ir
->var
->name
);
498 if (existing
!= NULL
)
501 ir_variable
*copy
= ir
->var
->clone(NULL
);
503 this->symbols
->add_variable(copy
->name
, copy
);
504 this->instructions
->push_head(copy
);
507 return visit_continue
;
511 glsl_symbol_table
*symbols
;
512 exec_list
*instructions
;
515 remap_visitor
v(symbols
, instructions
);
522 * Move non-declarations from one instruction stream to another
524 * The intended usage pattern of this function is to pass the pointer to the
525 * head sentinal of a list (i.e., a pointer to the list cast to an \c exec_node
526 * pointer) for \c last and \c false for \c make_copies on the first
527 * call. Successive calls pass the return value of the previous call for
528 * \c last and \c true for \c make_copies.
530 * \param instructions Source instruction stream
531 * \param last Instruction after which new instructions should be
532 * inserted in the target instruction stream
533 * \param make_copies Flag selecting whether instructions in \c instructions
534 * should be copied (via \c ir_instruction::clone) into the
535 * target list or moved.
538 * The new "last" instruction in the target instruction stream. This pointer
539 * is suitable for use as the \c last parameter of a later call to this
543 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
544 bool make_copies
, gl_shader
*target
)
546 foreach_list_safe(node
, instructions
) {
547 ir_instruction
*inst
= (ir_instruction
*) node
;
549 if (inst
->as_variable() || inst
->as_function())
552 assert(inst
->as_assignment());
555 inst
= inst
->clone(NULL
);
556 remap_variables(inst
, target
->symbols
, target
->ir
);
561 last
->insert_after(inst
);
569 * Get the function signature for main from a shader
571 static ir_function_signature
*
572 get_main_function_signature(gl_shader
*sh
)
574 ir_function
*const f
= sh
->symbols
->get_function("main");
576 exec_list void_parameters
;
578 /* Look for the 'void main()' signature and ensure that it's defined.
579 * This keeps the linker from accidentally pick a shader that just
580 * contains a prototype for main.
582 * We don't have to check for multiple definitions of main (in multiple
583 * shaders) because that would have already been caught above.
585 ir_function_signature
*sig
= f
->matching_signature(&void_parameters
);
586 if ((sig
!= NULL
) && sig
->is_defined
) {
596 * Combine a group of shaders for a single stage to generate a linked shader
599 * If this function is supplied a single shader, it is cloned, and the new
600 * shader is returned.
602 static struct gl_shader
*
603 link_intrastage_shaders(struct gl_shader_program
*prog
,
604 struct gl_shader
**shader_list
,
605 unsigned num_shaders
)
607 /* Check that global variables defined in multiple shaders are consistent.
609 if (!cross_validate_globals(prog
, shader_list
, num_shaders
, false))
612 /* Check that there is only a single definition of each function signature
613 * across all shaders.
615 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
616 foreach_list(node
, shader_list
[i
]->ir
) {
617 ir_function
*const f
= ((ir_instruction
*) node
)->as_function();
622 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
623 ir_function
*const other
=
624 shader_list
[j
]->symbols
->get_function(f
->name
);
626 /* If the other shader has no function (and therefore no function
627 * signatures) with the same name, skip to the next shader.
632 foreach_iter (exec_list_iterator
, iter
, *f
) {
633 ir_function_signature
*sig
=
634 (ir_function_signature
*) iter
.get();
636 if (!sig
->is_defined
|| sig
->is_built_in
)
639 ir_function_signature
*other_sig
=
640 other
->exact_matching_signature(& sig
->parameters
);
642 if ((other_sig
!= NULL
) && other_sig
->is_defined
643 && !other_sig
->is_built_in
) {
644 linker_error_printf(prog
,
645 "function `%s' is multiply defined",
654 /* Find the shader that defines main, and make a clone of it.
656 * Starting with the clone, search for undefined references. If one is
657 * found, find the shader that defines it. Clone the reference and add
658 * it to the shader. Repeat until there are no undefined references or
659 * until a reference cannot be resolved.
661 gl_shader
*main
= NULL
;
662 for (unsigned i
= 0; i
< num_shaders
; i
++) {
663 if (get_main_function_signature(shader_list
[i
]) != NULL
) {
664 main
= shader_list
[i
];
670 linker_error_printf(prog
, "%s shader lacks `main'\n",
671 (shader_list
[0]->Type
== GL_VERTEX_SHADER
)
672 ? "vertex" : "fragment");
676 gl_shader
*const linked
= _mesa_new_shader(NULL
, 0, main
->Type
);
677 linked
->ir
= new(linked
) exec_list
;
678 clone_ir_list(linked
->ir
, main
->ir
);
680 populate_symbol_table(linked
);
682 /* The a pointer to the main function in the final linked shader (i.e., the
683 * copy of the original shader that contained the main function).
685 ir_function_signature
*const main_sig
= get_main_function_signature(linked
);
687 /* Move any instructions other than variable declarations or function
688 * declarations into main.
690 exec_node
*insertion_point
=
691 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
694 for (unsigned i
= 0; i
< num_shaders
; i
++) {
695 if (shader_list
[i
] == main
)
698 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
699 insertion_point
, true, linked
);
702 /* Resolve initializers for global variables in the linked shader.
709 struct uniform_node
{
711 struct gl_uniform
*u
;
716 assign_uniform_locations(struct gl_shader_program
*prog
)
720 unsigned total_uniforms
= 0;
721 hash_table
*ht
= hash_table_ctor(32, hash_table_string_hash
,
722 hash_table_string_compare
);
724 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
725 unsigned next_position
= 0;
727 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
728 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
730 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
733 const unsigned vec4_slots
= (var
->component_slots() + 3) / 4;
734 assert(vec4_slots
!= 0);
736 uniform_node
*n
= (uniform_node
*) hash_table_find(ht
, var
->name
);
738 n
= (uniform_node
*) calloc(1, sizeof(struct uniform_node
));
739 n
->u
= (gl_uniform
*) calloc(vec4_slots
, sizeof(struct gl_uniform
));
740 n
->slots
= vec4_slots
;
742 n
->u
[0].Name
= strdup(var
->name
);
743 for (unsigned j
= 1; j
< vec4_slots
; j
++)
744 n
->u
[j
].Name
= n
->u
[0].Name
;
746 hash_table_insert(ht
, n
, n
->u
[0].Name
);
747 uniforms
.push_tail(& n
->link
);
748 total_uniforms
+= vec4_slots
;
751 if (var
->constant_value
!= NULL
)
752 for (unsigned j
= 0; j
< vec4_slots
; j
++)
753 n
->u
[j
].Initialized
= true;
755 var
->location
= next_position
;
757 for (unsigned j
= 0; j
< vec4_slots
; j
++) {
758 switch (prog
->_LinkedShaders
[i
]->Type
) {
759 case GL_VERTEX_SHADER
:
760 n
->u
[j
].VertPos
= next_position
;
762 case GL_FRAGMENT_SHADER
:
763 n
->u
[j
].FragPos
= next_position
;
765 case GL_GEOMETRY_SHADER
:
766 /* FINISHME: Support geometry shaders. */
767 assert(prog
->_LinkedShaders
[i
]->Type
!= GL_GEOMETRY_SHADER
);
776 gl_uniform_list
*ul
= (gl_uniform_list
*)
777 calloc(1, sizeof(gl_uniform_list
));
779 ul
->Size
= total_uniforms
;
780 ul
->NumUniforms
= total_uniforms
;
781 ul
->Uniforms
= (gl_uniform
*) calloc(total_uniforms
, sizeof(gl_uniform
));
785 for (uniform_node
*node
= (uniform_node
*) uniforms
.head
786 ; node
->link
.next
!= NULL
788 next
= (uniform_node
*) node
->link
.next
;
791 memcpy(&ul
->Uniforms
[idx
], node
->u
, sizeof(gl_uniform
) * node
->slots
);
805 * Find a contiguous set of available bits in a bitmask
807 * \param used_mask Bits representing used (1) and unused (0) locations
808 * \param needed_count Number of contiguous bits needed.
811 * Base location of the available bits on success or -1 on failure.
814 find_available_slots(unsigned used_mask
, unsigned needed_count
)
816 unsigned needed_mask
= (1 << needed_count
) - 1;
817 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
819 /* The comparison to 32 is redundant, but without it GCC emits "warning:
820 * cannot optimize possibly infinite loops" for the loop below.
822 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
825 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
826 if ((needed_mask
& ~used_mask
) == needed_mask
)
837 assign_attribute_locations(gl_shader_program
*prog
, unsigned max_attribute_index
)
839 /* Mark invalid attribute locations as being used.
841 unsigned used_locations
= (max_attribute_index
>= 32)
842 ? ~0 : ~((1 << max_attribute_index
) - 1);
844 gl_shader
*const sh
= prog
->_LinkedShaders
[0];
845 assert(sh
->Type
== GL_VERTEX_SHADER
);
847 /* Operate in a total of four passes.
849 * 1. Invalidate the location assignments for all vertex shader inputs.
851 * 2. Assign locations for inputs that have user-defined (via
852 * glBindVertexAttribLocation) locatoins.
854 * 3. Sort the attributes without assigned locations by number of slots
855 * required in decreasing order. Fragmentation caused by attribute
856 * locations assigned by the application may prevent large attributes
857 * from having enough contiguous space.
859 * 4. Assign locations to any inputs without assigned locations.
862 invalidate_variable_locations(sh
, ir_var_in
, VERT_ATTRIB_GENERIC0
);
864 if (prog
->Attributes
!= NULL
) {
865 for (unsigned i
= 0; i
< prog
->Attributes
->NumParameters
; i
++) {
866 ir_variable
*const var
=
867 sh
->symbols
->get_variable(prog
->Attributes
->Parameters
[i
].Name
);
869 /* Note: attributes that occupy multiple slots, such as arrays or
870 * matrices, may appear in the attrib array multiple times.
872 if ((var
== NULL
) || (var
->location
!= -1))
875 /* From page 61 of the OpenGL 4.0 spec:
877 * "LinkProgram will fail if the attribute bindings assigned by
878 * BindAttribLocation do not leave not enough space to assign a
879 * location for an active matrix attribute or an active attribute
880 * array, both of which require multiple contiguous generic
883 * Previous versions of the spec contain similar language but omit the
884 * bit about attribute arrays.
886 * Page 61 of the OpenGL 4.0 spec also says:
888 * "It is possible for an application to bind more than one
889 * attribute name to the same location. This is referred to as
890 * aliasing. This will only work if only one of the aliased
891 * attributes is active in the executable program, or if no path
892 * through the shader consumes more than one attribute of a set
893 * of attributes aliased to the same location. A link error can
894 * occur if the linker determines that every path through the
895 * shader consumes multiple aliased attributes, but
896 * implementations are not required to generate an error in this
899 * These two paragraphs are either somewhat contradictory, or I don't
900 * fully understand one or both of them.
902 /* FINISHME: The code as currently written does not support attribute
903 * FINISHME: location aliasing (see comment above).
905 const int attr
= prog
->Attributes
->Parameters
[i
].StateIndexes
[0];
906 const unsigned slots
= count_attribute_slots(var
->type
);
908 /* Mask representing the contiguous slots that will be used by this
911 const unsigned use_mask
= (1 << slots
) - 1;
913 /* Generate a link error if the set of bits requested for this
914 * attribute overlaps any previously allocated bits.
916 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
917 linker_error_printf(prog
,
918 "insufficient contiguous attribute locations "
919 "available for vertex shader input `%s'",
924 var
->location
= VERT_ATTRIB_GENERIC0
+ attr
;
925 used_locations
|= (use_mask
<< attr
);
929 /* Temporary storage for the set of attributes that need locations assigned.
935 /* Used below in the call to qsort. */
936 static int compare(const void *a
, const void *b
)
938 const temp_attr
*const l
= (const temp_attr
*) a
;
939 const temp_attr
*const r
= (const temp_attr
*) b
;
941 /* Reversed because we want a descending order sort below. */
942 return r
->slots
- l
->slots
;
946 unsigned num_attr
= 0;
948 foreach_list(node
, sh
->ir
) {
949 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
951 if ((var
== NULL
) || (var
->mode
!= ir_var_in
))
954 /* The location was explicitly assigned, nothing to do here.
956 if (var
->location
!= -1)
959 to_assign
[num_attr
].slots
= count_attribute_slots(var
->type
);
960 to_assign
[num_attr
].var
= var
;
964 /* If all of the attributes were assigned locations by the application (or
965 * are built-in attributes with fixed locations), return early. This should
966 * be the common case.
971 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
973 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
974 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
975 * to prevent it from being automatically allocated below.
977 used_locations
|= (1 << 0);
979 for (unsigned i
= 0; i
< num_attr
; i
++) {
980 /* Mask representing the contiguous slots that will be used by this
983 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
985 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
988 linker_error_printf(prog
,
989 "insufficient contiguous attribute locations "
990 "available for vertex shader input `%s'",
991 to_assign
[i
].var
->name
);
995 to_assign
[i
].var
->location
= VERT_ATTRIB_GENERIC0
+ location
;
996 used_locations
|= (use_mask
<< location
);
1004 assign_varying_locations(gl_shader
*producer
, gl_shader
*consumer
)
1006 /* FINISHME: Set dynamically when geometry shader support is added. */
1007 unsigned output_index
= VERT_RESULT_VAR0
;
1008 unsigned input_index
= FRAG_ATTRIB_VAR0
;
1010 /* Operate in a total of three passes.
1012 * 1. Assign locations for any matching inputs and outputs.
1014 * 2. Mark output variables in the producer that do not have locations as
1015 * not being outputs. This lets the optimizer eliminate them.
1017 * 3. Mark input variables in the consumer that do not have locations as
1018 * not being inputs. This lets the optimizer eliminate them.
1021 invalidate_variable_locations(producer
, ir_var_out
, VERT_RESULT_VAR0
);
1022 invalidate_variable_locations(consumer
, ir_var_in
, FRAG_ATTRIB_VAR0
);
1024 foreach_list(node
, producer
->ir
) {
1025 ir_variable
*const output_var
= ((ir_instruction
*) node
)->as_variable();
1027 if ((output_var
== NULL
) || (output_var
->mode
!= ir_var_out
)
1028 || (output_var
->location
!= -1))
1031 ir_variable
*const input_var
=
1032 consumer
->symbols
->get_variable(output_var
->name
);
1034 if ((input_var
== NULL
) || (input_var
->mode
!= ir_var_in
))
1037 assert(input_var
->location
== -1);
1039 /* FINISHME: Location assignment will need some changes when arrays,
1040 * FINISHME: matrices, and structures are allowed as shader inputs /
1041 * FINISHME: outputs.
1043 output_var
->location
= output_index
;
1044 input_var
->location
= input_index
;
1050 foreach_list(node
, producer
->ir
) {
1051 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1053 if ((var
== NULL
) || (var
->mode
!= ir_var_out
))
1056 /* An 'out' variable is only really a shader output if its value is read
1057 * by the following stage.
1059 if (var
->location
== -1) {
1060 var
->shader_out
= false;
1061 var
->mode
= ir_var_auto
;
1065 foreach_list(node
, consumer
->ir
) {
1066 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1068 if ((var
== NULL
) || (var
->mode
!= ir_var_in
))
1071 /* An 'in' variable is only really a shader input if its value is written
1072 * by the previous stage.
1074 var
->shader_in
= (var
->location
!= -1);
1080 link_shaders(struct gl_shader_program
*prog
)
1082 prog
->LinkStatus
= false;
1083 prog
->Validated
= false;
1084 prog
->_Used
= false;
1086 if (prog
->InfoLog
!= NULL
)
1087 talloc_free(prog
->InfoLog
);
1089 prog
->InfoLog
= talloc_strdup(NULL
, "");
1091 /* Separate the shaders into groups based on their type.
1093 struct gl_shader
**vert_shader_list
;
1094 unsigned num_vert_shaders
= 0;
1095 struct gl_shader
**frag_shader_list
;
1096 unsigned num_frag_shaders
= 0;
1098 vert_shader_list
= (struct gl_shader
**)
1099 calloc(2 * prog
->NumShaders
, sizeof(struct gl_shader
*));
1100 frag_shader_list
= &vert_shader_list
[prog
->NumShaders
];
1102 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
1103 switch (prog
->Shaders
[i
]->Type
) {
1104 case GL_VERTEX_SHADER
:
1105 vert_shader_list
[num_vert_shaders
] = prog
->Shaders
[i
];
1108 case GL_FRAGMENT_SHADER
:
1109 frag_shader_list
[num_frag_shaders
] = prog
->Shaders
[i
];
1112 case GL_GEOMETRY_SHADER
:
1113 /* FINISHME: Support geometry shaders. */
1114 assert(prog
->Shaders
[i
]->Type
!= GL_GEOMETRY_SHADER
);
1119 /* FINISHME: Implement intra-stage linking. */
1120 prog
->_NumLinkedShaders
= 0;
1121 if (num_vert_shaders
> 0) {
1122 gl_shader
*const sh
=
1123 link_intrastage_shaders(prog
, vert_shader_list
, num_vert_shaders
);
1128 if (!validate_vertex_shader_executable(prog
, sh
))
1131 prog
->_LinkedShaders
[prog
->_NumLinkedShaders
] = sh
;
1132 prog
->_NumLinkedShaders
++;
1135 if (num_frag_shaders
> 0) {
1136 gl_shader
*const sh
=
1137 link_intrastage_shaders(prog
, frag_shader_list
, num_frag_shaders
);
1142 if (!validate_fragment_shader_executable(prog
, sh
))
1145 prog
->_LinkedShaders
[prog
->_NumLinkedShaders
] = sh
;
1146 prog
->_NumLinkedShaders
++;
1149 /* Here begins the inter-stage linking phase. Some initial validation is
1150 * performed, then locations are assigned for uniforms, attributes, and
1153 if (cross_validate_uniforms(prog
)) {
1154 /* Validate the inputs of each stage with the output of the preceeding
1157 for (unsigned i
= 1; i
< prog
->_NumLinkedShaders
; i
++) {
1158 if (!cross_validate_outputs_to_inputs(prog
,
1159 prog
->_LinkedShaders
[i
- 1],
1160 prog
->_LinkedShaders
[i
]))
1164 prog
->LinkStatus
= true;
1167 /* FINISHME: Perform whole-program optimization here. */
1169 assign_uniform_locations(prog
);
1171 if (prog
->_LinkedShaders
[0]->Type
== GL_VERTEX_SHADER
)
1172 /* FINISHME: The value of the max_attribute_index parameter is
1173 * FINISHME: implementation dependent based on the value of
1174 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1175 * FINISHME: at least 16, so hardcode 16 for now.
1177 if (!assign_attribute_locations(prog
, 16))
1180 for (unsigned i
= 1; i
< prog
->_NumLinkedShaders
; i
++)
1181 assign_varying_locations(prog
->_LinkedShaders
[i
- 1],
1182 prog
->_LinkedShaders
[i
]);
1184 /* FINISHME: Assign fragment shader output locations. */
1187 free(vert_shader_list
);