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"
79 #define ALIGN(value, alignment) (((value) + alignment - 1) & ~(alignment - 1))
82 * Visitor that determines whether or not a variable is ever written.
84 class find_assignment_visitor
: public ir_hierarchical_visitor
{
86 find_assignment_visitor(const char *name
)
87 : name(name
), found(false)
92 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
)
94 ir_variable
*const var
= ir
->lhs
->variable_referenced();
96 if (strcmp(name
, var
->name
) == 0) {
101 return visit_continue_with_parent
;
104 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
106 exec_list_iterator sig_iter
= ir
->callee
->parameters
.iterator();
107 foreach_iter(exec_list_iterator
, iter
, *ir
) {
108 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
109 ir_variable
*sig_param
= (ir_variable
*)sig_iter
.get();
111 if (sig_param
->mode
== ir_var_out
||
112 sig_param
->mode
== ir_var_inout
) {
113 ir_variable
*var
= param_rval
->variable_referenced();
114 if (var
&& strcmp(name
, var
->name
) == 0) {
122 if (ir
->return_deref
!= NULL
) {
123 ir_variable
*const var
= ir
->return_deref
->variable_referenced();
125 if (strcmp(name
, var
->name
) == 0) {
131 return visit_continue_with_parent
;
134 bool variable_found()
140 const char *name
; /**< Find writes to a variable with this name. */
141 bool found
; /**< Was a write to the variable found? */
146 * Visitor that determines whether or not a variable is ever read.
148 class find_deref_visitor
: public ir_hierarchical_visitor
{
150 find_deref_visitor(const char *name
)
151 : name(name
), found(false)
156 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
158 if (strcmp(this->name
, ir
->var
->name
) == 0) {
163 return visit_continue
;
166 bool variable_found() const
172 const char *name
; /**< Find writes to a variable with this name. */
173 bool found
; /**< Was a write to the variable found? */
178 linker_error(gl_shader_program
*prog
, const char *fmt
, ...)
182 ralloc_strcat(&prog
->InfoLog
, "error: ");
184 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
187 prog
->LinkStatus
= false;
192 linker_warning(gl_shader_program
*prog
, const char *fmt
, ...)
196 ralloc_strcat(&prog
->InfoLog
, "error: ");
198 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
205 link_invalidate_variable_locations(gl_shader
*sh
, int input_base
,
208 foreach_list(node
, sh
->ir
) {
209 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
226 /* Only assign locations for generic attributes / varyings / etc.
228 if ((var
->location
>= base
) && !var
->explicit_location
)
231 if ((var
->location
== -1) && !var
->explicit_location
) {
232 var
->is_unmatched_generic_inout
= 1;
233 var
->location_frac
= 0;
235 var
->is_unmatched_generic_inout
= 0;
242 * Determine the number of attribute slots required for a particular type
244 * This code is here because it implements the language rules of a specific
245 * GLSL version. Since it's a property of the language and not a property of
246 * types in general, it doesn't really belong in glsl_type.
249 count_attribute_slots(const glsl_type
*t
)
251 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
253 * "A scalar input counts the same amount against this limit as a vec4,
254 * so applications may want to consider packing groups of four
255 * unrelated float inputs together into a vector to better utilize the
256 * capabilities of the underlying hardware. A matrix input will use up
257 * multiple locations. The number of locations used will equal the
258 * number of columns in the matrix."
260 * The spec does not explicitly say how arrays are counted. However, it
261 * should be safe to assume the total number of slots consumed by an array
262 * is the number of entries in the array multiplied by the number of slots
263 * consumed by a single element of the array.
267 return t
->array_size() * count_attribute_slots(t
->element_type());
270 return t
->matrix_columns
;
277 * Verify that a vertex shader executable meets all semantic requirements.
279 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
282 * \param shader Vertex shader executable to be verified
285 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
286 struct gl_shader
*shader
)
291 /* From the GLSL 1.10 spec, page 48:
293 * "The variable gl_Position is available only in the vertex
294 * language and is intended for writing the homogeneous vertex
295 * position. All executions of a well-formed vertex shader
296 * executable must write a value into this variable. [...] The
297 * variable gl_Position is available only in the vertex
298 * language and is intended for writing the homogeneous vertex
299 * position. All executions of a well-formed vertex shader
300 * executable must write a value into this variable."
302 * while in GLSL 1.40 this text is changed to:
304 * "The variable gl_Position is available only in the vertex
305 * language and is intended for writing the homogeneous vertex
306 * position. It can be written at any time during shader
307 * execution. It may also be read back by a vertex shader
308 * after being written. This value will be used by primitive
309 * assembly, clipping, culling, and other fixed functionality
310 * operations, if present, that operate on primitives after
311 * vertex processing has occurred. Its value is undefined if
312 * the vertex shader executable does not write gl_Position."
314 * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
317 if (prog
->Version
< (prog
->IsES
? 300 : 140)) {
318 find_assignment_visitor
find("gl_Position");
319 find
.run(shader
->ir
);
320 if (!find
.variable_found()) {
321 linker_error(prog
, "vertex shader does not write to `gl_Position'\n");
326 prog
->Vert
.ClipDistanceArraySize
= 0;
328 if (!prog
->IsES
&& prog
->Version
>= 130) {
329 /* From section 7.1 (Vertex Shader Special Variables) of the
332 * "It is an error for a shader to statically write both
333 * gl_ClipVertex and gl_ClipDistance."
335 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
336 * gl_ClipVertex nor gl_ClipDistance.
338 find_assignment_visitor
clip_vertex("gl_ClipVertex");
339 find_assignment_visitor
clip_distance("gl_ClipDistance");
341 clip_vertex
.run(shader
->ir
);
342 clip_distance
.run(shader
->ir
);
343 if (clip_vertex
.variable_found() && clip_distance
.variable_found()) {
344 linker_error(prog
, "vertex shader writes to both `gl_ClipVertex' "
345 "and `gl_ClipDistance'\n");
348 prog
->Vert
.UsesClipDistance
= clip_distance
.variable_found();
349 ir_variable
*clip_distance_var
=
350 shader
->symbols
->get_variable("gl_ClipDistance");
351 if (clip_distance_var
)
352 prog
->Vert
.ClipDistanceArraySize
= clip_distance_var
->type
->length
;
360 * Verify that a fragment shader executable meets all semantic requirements
362 * \param shader Fragment shader executable to be verified
365 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
366 struct gl_shader
*shader
)
371 find_assignment_visitor
frag_color("gl_FragColor");
372 find_assignment_visitor
frag_data("gl_FragData");
374 frag_color
.run(shader
->ir
);
375 frag_data
.run(shader
->ir
);
377 if (frag_color
.variable_found() && frag_data
.variable_found()) {
378 linker_error(prog
, "fragment shader writes to both "
379 "`gl_FragColor' and `gl_FragData'\n");
388 * Generate a string describing the mode of a variable
391 mode_string(const ir_variable
*var
)
395 return (var
->read_only
) ? "global constant" : "global variable";
397 case ir_var_uniform
: return "uniform";
398 case ir_var_in
: return "shader input";
399 case ir_var_out
: return "shader output";
400 case ir_var_inout
: return "shader inout";
402 case ir_var_const_in
:
403 case ir_var_temporary
:
405 assert(!"Should not get here.");
406 return "invalid variable";
412 * Perform validation of global variables used across multiple shaders
415 cross_validate_globals(struct gl_shader_program
*prog
,
416 struct gl_shader
**shader_list
,
417 unsigned num_shaders
,
420 /* Examine all of the uniforms in all of the shaders and cross validate
423 glsl_symbol_table variables
;
424 for (unsigned i
= 0; i
< num_shaders
; i
++) {
425 if (shader_list
[i
] == NULL
)
428 foreach_list(node
, shader_list
[i
]->ir
) {
429 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
434 if (uniforms_only
&& (var
->mode
!= ir_var_uniform
))
437 /* Don't cross validate temporaries that are at global scope. These
438 * will eventually get pulled into the shaders 'main'.
440 if (var
->mode
== ir_var_temporary
)
443 /* If a global with this name has already been seen, verify that the
444 * new instance has the same type. In addition, if the globals have
445 * initializers, the values of the initializers must be the same.
447 ir_variable
*const existing
= variables
.get_variable(var
->name
);
448 if (existing
!= NULL
) {
449 if (var
->type
!= existing
->type
) {
450 /* Consider the types to be "the same" if both types are arrays
451 * of the same type and one of the arrays is implicitly sized.
452 * In addition, set the type of the linked variable to the
453 * explicitly sized array.
455 if (var
->type
->is_array()
456 && existing
->type
->is_array()
457 && (var
->type
->fields
.array
== existing
->type
->fields
.array
)
458 && ((var
->type
->length
== 0)
459 || (existing
->type
->length
== 0))) {
460 if (var
->type
->length
!= 0) {
461 existing
->type
= var
->type
;
464 linker_error(prog
, "%s `%s' declared as type "
465 "`%s' and type `%s'\n",
467 var
->name
, var
->type
->name
,
468 existing
->type
->name
);
473 if (var
->explicit_location
) {
474 if (existing
->explicit_location
475 && (var
->location
!= existing
->location
)) {
476 linker_error(prog
, "explicit locations for %s "
477 "`%s' have differing values\n",
478 mode_string(var
), var
->name
);
482 existing
->location
= var
->location
;
483 existing
->explicit_location
= true;
486 /* Validate layout qualifiers for gl_FragDepth.
488 * From the AMD/ARB_conservative_depth specs:
490 * "If gl_FragDepth is redeclared in any fragment shader in a
491 * program, it must be redeclared in all fragment shaders in
492 * that program that have static assignments to
493 * gl_FragDepth. All redeclarations of gl_FragDepth in all
494 * fragment shaders in a single program must have the same set
497 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
498 bool layout_declared
= var
->depth_layout
!= ir_depth_layout_none
;
499 bool layout_differs
=
500 var
->depth_layout
!= existing
->depth_layout
;
502 if (layout_declared
&& layout_differs
) {
504 "All redeclarations of gl_FragDepth in all "
505 "fragment shaders in a single program must have "
506 "the same set of qualifiers.");
509 if (var
->used
&& layout_differs
) {
511 "If gl_FragDepth is redeclared with a layout "
512 "qualifier in any fragment shader, it must be "
513 "redeclared with the same layout qualifier in "
514 "all fragment shaders that have assignments to "
519 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
521 * "If a shared global has multiple initializers, the
522 * initializers must all be constant expressions, and they
523 * must all have the same value. Otherwise, a link error will
524 * result. (A shared global having only one initializer does
525 * not require that initializer to be a constant expression.)"
527 * Previous to 4.20 the GLSL spec simply said that initializers
528 * must have the same value. In this case of non-constant
529 * initializers, this was impossible to determine. As a result,
530 * no vendor actually implemented that behavior. The 4.20
531 * behavior matches the implemented behavior of at least one other
532 * vendor, so we'll implement that for all GLSL versions.
534 if (var
->constant_initializer
!= NULL
) {
535 if (existing
->constant_initializer
!= NULL
) {
536 if (!var
->constant_initializer
->has_value(existing
->constant_initializer
)) {
537 linker_error(prog
, "initializers for %s "
538 "`%s' have differing values\n",
539 mode_string(var
), var
->name
);
543 /* If the first-seen instance of a particular uniform did not
544 * have an initializer but a later instance does, copy the
545 * initializer to the version stored in the symbol table.
547 /* FINISHME: This is wrong. The constant_value field should
548 * FINISHME: not be modified! Imagine a case where a shader
549 * FINISHME: without an initializer is linked in two different
550 * FINISHME: programs with shaders that have differing
551 * FINISHME: initializers. Linking with the first will
552 * FINISHME: modify the shader, and linking with the second
553 * FINISHME: will fail.
555 existing
->constant_initializer
=
556 var
->constant_initializer
->clone(ralloc_parent(existing
),
561 if (var
->has_initializer
) {
562 if (existing
->has_initializer
563 && (var
->constant_initializer
== NULL
564 || existing
->constant_initializer
== NULL
)) {
566 "shared global variable `%s' has multiple "
567 "non-constant initializers.\n",
572 /* Some instance had an initializer, so keep track of that. In
573 * this location, all sorts of initializers (constant or
574 * otherwise) will propagate the existence to the variable
575 * stored in the symbol table.
577 existing
->has_initializer
= true;
580 if (existing
->invariant
!= var
->invariant
) {
581 linker_error(prog
, "declarations for %s `%s' have "
582 "mismatching invariant qualifiers\n",
583 mode_string(var
), var
->name
);
586 if (existing
->centroid
!= var
->centroid
) {
587 linker_error(prog
, "declarations for %s `%s' have "
588 "mismatching centroid qualifiers\n",
589 mode_string(var
), var
->name
);
593 variables
.add_variable(var
);
602 * Perform validation of uniforms used across multiple shader stages
605 cross_validate_uniforms(struct gl_shader_program
*prog
)
607 return cross_validate_globals(prog
, prog
->_LinkedShaders
,
608 MESA_SHADER_TYPES
, true);
612 * Accumulates the array of prog->UniformBlocks and checks that all
613 * definitons of blocks agree on their contents.
616 interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
)
618 unsigned max_num_uniform_blocks
= 0;
619 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
620 if (prog
->_LinkedShaders
[i
])
621 max_num_uniform_blocks
+= prog
->_LinkedShaders
[i
]->NumUniformBlocks
;
624 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
625 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
627 prog
->UniformBlockStageIndex
[i
] = ralloc_array(prog
, int,
628 max_num_uniform_blocks
);
629 for (unsigned int j
= 0; j
< max_num_uniform_blocks
; j
++)
630 prog
->UniformBlockStageIndex
[i
][j
] = -1;
635 for (unsigned int j
= 0; j
< sh
->NumUniformBlocks
; j
++) {
636 int index
= link_cross_validate_uniform_block(prog
,
637 &prog
->UniformBlocks
,
638 &prog
->NumUniformBlocks
,
639 &sh
->UniformBlocks
[j
]);
642 linker_error(prog
, "uniform block `%s' has mismatching definitions",
643 sh
->UniformBlocks
[j
].Name
);
647 prog
->UniformBlockStageIndex
[i
][index
] = j
;
655 * Validate that outputs from one stage match inputs of another
658 cross_validate_outputs_to_inputs(struct gl_shader_program
*prog
,
659 gl_shader
*producer
, gl_shader
*consumer
)
661 glsl_symbol_table parameters
;
662 /* FINISHME: Figure these out dynamically. */
663 const char *const producer_stage
= "vertex";
664 const char *const consumer_stage
= "fragment";
666 /* Find all shader outputs in the "producer" stage.
668 foreach_list(node
, producer
->ir
) {
669 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
671 /* FINISHME: For geometry shaders, this should also look for inout
672 * FINISHME: variables.
674 if ((var
== NULL
) || (var
->mode
!= ir_var_out
))
677 parameters
.add_variable(var
);
681 /* Find all shader inputs in the "consumer" stage. Any variables that have
682 * matching outputs already in the symbol table must have the same type and
685 foreach_list(node
, consumer
->ir
) {
686 ir_variable
*const input
= ((ir_instruction
*) node
)->as_variable();
688 /* FINISHME: For geometry shaders, this should also look for inout
689 * FINISHME: variables.
691 if ((input
== NULL
) || (input
->mode
!= ir_var_in
))
694 ir_variable
*const output
= parameters
.get_variable(input
->name
);
695 if (output
!= NULL
) {
696 /* Check that the types match between stages.
698 if (input
->type
!= output
->type
) {
699 /* There is a bit of a special case for gl_TexCoord. This
700 * built-in is unsized by default. Applications that variable
701 * access it must redeclare it with a size. There is some
702 * language in the GLSL spec that implies the fragment shader
703 * and vertex shader do not have to agree on this size. Other
704 * driver behave this way, and one or two applications seem to
707 * Neither declaration needs to be modified here because the array
708 * sizes are fixed later when update_array_sizes is called.
710 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
712 * "Unlike user-defined varying variables, the built-in
713 * varying variables don't have a strict one-to-one
714 * correspondence between the vertex language and the
715 * fragment language."
717 if (!output
->type
->is_array()
718 || (strncmp("gl_", output
->name
, 3) != 0)) {
720 "%s shader output `%s' declared as type `%s', "
721 "but %s shader input declared as type `%s'\n",
722 producer_stage
, output
->name
,
724 consumer_stage
, input
->type
->name
);
729 /* Check that all of the qualifiers match between stages.
731 if (input
->centroid
!= output
->centroid
) {
733 "%s shader output `%s' %s centroid qualifier, "
734 "but %s shader input %s centroid qualifier\n",
737 (output
->centroid
) ? "has" : "lacks",
739 (input
->centroid
) ? "has" : "lacks");
743 if (input
->invariant
!= output
->invariant
) {
745 "%s shader output `%s' %s invariant qualifier, "
746 "but %s shader input %s invariant qualifier\n",
749 (output
->invariant
) ? "has" : "lacks",
751 (input
->invariant
) ? "has" : "lacks");
755 if (input
->interpolation
!= output
->interpolation
) {
757 "%s shader output `%s' specifies %s "
758 "interpolation qualifier, "
759 "but %s shader input specifies %s "
760 "interpolation qualifier\n",
763 output
->interpolation_string(),
765 input
->interpolation_string());
776 * Populates a shaders symbol table with all global declarations
779 populate_symbol_table(gl_shader
*sh
)
781 sh
->symbols
= new(sh
) glsl_symbol_table
;
783 foreach_list(node
, sh
->ir
) {
784 ir_instruction
*const inst
= (ir_instruction
*) node
;
788 if ((func
= inst
->as_function()) != NULL
) {
789 sh
->symbols
->add_function(func
);
790 } else if ((var
= inst
->as_variable()) != NULL
) {
791 sh
->symbols
->add_variable(var
);
798 * Remap variables referenced in an instruction tree
800 * This is used when instruction trees are cloned from one shader and placed in
801 * another. These trees will contain references to \c ir_variable nodes that
802 * do not exist in the target shader. This function finds these \c ir_variable
803 * references and replaces the references with matching variables in the target
806 * If there is no matching variable in the target shader, a clone of the
807 * \c ir_variable is made and added to the target shader. The new variable is
808 * added to \b both the instruction stream and the symbol table.
810 * \param inst IR tree that is to be processed.
811 * \param symbols Symbol table containing global scope symbols in the
813 * \param instructions Instruction stream where new variable declarations
817 remap_variables(ir_instruction
*inst
, struct gl_shader
*target
,
820 class remap_visitor
: public ir_hierarchical_visitor
{
822 remap_visitor(struct gl_shader
*target
,
825 this->target
= target
;
826 this->symbols
= target
->symbols
;
827 this->instructions
= target
->ir
;
831 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
833 if (ir
->var
->mode
== ir_var_temporary
) {
834 ir_variable
*var
= (ir_variable
*) hash_table_find(temps
, ir
->var
);
838 return visit_continue
;
841 ir_variable
*const existing
=
842 this->symbols
->get_variable(ir
->var
->name
);
843 if (existing
!= NULL
)
846 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
848 this->symbols
->add_variable(copy
);
849 this->instructions
->push_head(copy
);
853 return visit_continue
;
857 struct gl_shader
*target
;
858 glsl_symbol_table
*symbols
;
859 exec_list
*instructions
;
863 remap_visitor
v(target
, temps
);
870 * Move non-declarations from one instruction stream to another
872 * The intended usage pattern of this function is to pass the pointer to the
873 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
874 * pointer) for \c last and \c false for \c make_copies on the first
875 * call. Successive calls pass the return value of the previous call for
876 * \c last and \c true for \c make_copies.
878 * \param instructions Source instruction stream
879 * \param last Instruction after which new instructions should be
880 * inserted in the target instruction stream
881 * \param make_copies Flag selecting whether instructions in \c instructions
882 * should be copied (via \c ir_instruction::clone) into the
883 * target list or moved.
886 * The new "last" instruction in the target instruction stream. This pointer
887 * is suitable for use as the \c last parameter of a later call to this
891 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
892 bool make_copies
, gl_shader
*target
)
894 hash_table
*temps
= NULL
;
897 temps
= hash_table_ctor(0, hash_table_pointer_hash
,
898 hash_table_pointer_compare
);
900 foreach_list_safe(node
, instructions
) {
901 ir_instruction
*inst
= (ir_instruction
*) node
;
903 if (inst
->as_function())
906 ir_variable
*var
= inst
->as_variable();
907 if ((var
!= NULL
) && (var
->mode
!= ir_var_temporary
))
910 assert(inst
->as_assignment()
912 || inst
->as_if() /* for initializers with the ?: operator */
913 || ((var
!= NULL
) && (var
->mode
== ir_var_temporary
)));
916 inst
= inst
->clone(target
, NULL
);
919 hash_table_insert(temps
, inst
, var
);
921 remap_variables(inst
, target
, temps
);
926 last
->insert_after(inst
);
931 hash_table_dtor(temps
);
937 * Get the function signature for main from a shader
939 static ir_function_signature
*
940 get_main_function_signature(gl_shader
*sh
)
942 ir_function
*const f
= sh
->symbols
->get_function("main");
944 exec_list void_parameters
;
946 /* Look for the 'void main()' signature and ensure that it's defined.
947 * This keeps the linker from accidentally pick a shader that just
948 * contains a prototype for main.
950 * We don't have to check for multiple definitions of main (in multiple
951 * shaders) because that would have already been caught above.
953 ir_function_signature
*sig
= f
->matching_signature(&void_parameters
);
954 if ((sig
!= NULL
) && sig
->is_defined
) {
964 * This class is only used in link_intrastage_shaders() below but declaring
965 * it inside that function leads to compiler warnings with some versions of
968 class array_sizing_visitor
: public ir_hierarchical_visitor
{
970 virtual ir_visitor_status
visit(ir_variable
*var
)
972 if (var
->type
->is_array() && (var
->type
->length
== 0)) {
973 const glsl_type
*type
=
974 glsl_type::get_array_instance(var
->type
->fields
.array
,
975 var
->max_array_access
+ 1);
976 assert(type
!= NULL
);
979 return visit_continue
;
984 * Combine a group of shaders for a single stage to generate a linked shader
987 * If this function is supplied a single shader, it is cloned, and the new
988 * shader is returned.
990 static struct gl_shader
*
991 link_intrastage_shaders(void *mem_ctx
,
992 struct gl_context
*ctx
,
993 struct gl_shader_program
*prog
,
994 struct gl_shader
**shader_list
,
995 unsigned num_shaders
)
997 struct gl_uniform_block
*uniform_blocks
= NULL
;
998 unsigned num_uniform_blocks
= 0;
1000 /* Check that global variables defined in multiple shaders are consistent.
1002 if (!cross_validate_globals(prog
, shader_list
, num_shaders
, false))
1005 /* Check that uniform blocks between shaders for a stage agree. */
1006 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1007 struct gl_shader
*sh
= shader_list
[i
];
1009 for (unsigned j
= 0; j
< shader_list
[i
]->NumUniformBlocks
; j
++) {
1010 link_assign_uniform_block_offsets(shader_list
[i
]);
1012 int index
= link_cross_validate_uniform_block(mem_ctx
,
1014 &num_uniform_blocks
,
1015 &sh
->UniformBlocks
[j
]);
1017 linker_error(prog
, "uniform block `%s' has mismatching definitions",
1018 sh
->UniformBlocks
[j
].Name
);
1024 /* Check that there is only a single definition of each function signature
1025 * across all shaders.
1027 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
1028 foreach_list(node
, shader_list
[i
]->ir
) {
1029 ir_function
*const f
= ((ir_instruction
*) node
)->as_function();
1034 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
1035 ir_function
*const other
=
1036 shader_list
[j
]->symbols
->get_function(f
->name
);
1038 /* If the other shader has no function (and therefore no function
1039 * signatures) with the same name, skip to the next shader.
1044 foreach_iter (exec_list_iterator
, iter
, *f
) {
1045 ir_function_signature
*sig
=
1046 (ir_function_signature
*) iter
.get();
1048 if (!sig
->is_defined
|| sig
->is_builtin
)
1051 ir_function_signature
*other_sig
=
1052 other
->exact_matching_signature(& sig
->parameters
);
1054 if ((other_sig
!= NULL
) && other_sig
->is_defined
1055 && !other_sig
->is_builtin
) {
1056 linker_error(prog
, "function `%s' is multiply defined",
1065 /* Find the shader that defines main, and make a clone of it.
1067 * Starting with the clone, search for undefined references. If one is
1068 * found, find the shader that defines it. Clone the reference and add
1069 * it to the shader. Repeat until there are no undefined references or
1070 * until a reference cannot be resolved.
1072 gl_shader
*main
= NULL
;
1073 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1074 if (get_main_function_signature(shader_list
[i
]) != NULL
) {
1075 main
= shader_list
[i
];
1081 linker_error(prog
, "%s shader lacks `main'\n",
1082 (shader_list
[0]->Type
== GL_VERTEX_SHADER
)
1083 ? "vertex" : "fragment");
1087 gl_shader
*linked
= ctx
->Driver
.NewShader(NULL
, 0, main
->Type
);
1088 linked
->ir
= new(linked
) exec_list
;
1089 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
1091 linked
->UniformBlocks
= uniform_blocks
;
1092 linked
->NumUniformBlocks
= num_uniform_blocks
;
1093 ralloc_steal(linked
, linked
->UniformBlocks
);
1095 populate_symbol_table(linked
);
1097 /* The a pointer to the main function in the final linked shader (i.e., the
1098 * copy of the original shader that contained the main function).
1100 ir_function_signature
*const main_sig
= get_main_function_signature(linked
);
1102 /* Move any instructions other than variable declarations or function
1103 * declarations into main.
1105 exec_node
*insertion_point
=
1106 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
1109 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1110 if (shader_list
[i
] == main
)
1113 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
1114 insertion_point
, true, linked
);
1117 /* Resolve initializers for global variables in the linked shader.
1119 unsigned num_linking_shaders
= num_shaders
;
1120 for (unsigned i
= 0; i
< num_shaders
; i
++)
1121 num_linking_shaders
+= shader_list
[i
]->num_builtins_to_link
;
1123 gl_shader
**linking_shaders
=
1124 (gl_shader
**) calloc(num_linking_shaders
, sizeof(gl_shader
*));
1126 memcpy(linking_shaders
, shader_list
,
1127 sizeof(linking_shaders
[0]) * num_shaders
);
1129 unsigned idx
= num_shaders
;
1130 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1131 memcpy(&linking_shaders
[idx
], shader_list
[i
]->builtins_to_link
,
1132 sizeof(linking_shaders
[0]) * shader_list
[i
]->num_builtins_to_link
);
1133 idx
+= shader_list
[i
]->num_builtins_to_link
;
1136 assert(idx
== num_linking_shaders
);
1138 if (!link_function_calls(prog
, linked
, linking_shaders
,
1139 num_linking_shaders
)) {
1140 ctx
->Driver
.DeleteShader(ctx
, linked
);
1144 free(linking_shaders
);
1147 /* At this point linked should contain all of the linked IR, so
1148 * validate it to make sure nothing went wrong.
1151 validate_ir_tree(linked
->ir
);
1154 /* Make a pass over all variable declarations to ensure that arrays with
1155 * unspecified sizes have a size specified. The size is inferred from the
1156 * max_array_access field.
1158 if (linked
!= NULL
) {
1159 array_sizing_visitor v
;
1168 * Update the sizes of linked shader uniform arrays to the maximum
1171 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1173 * If one or more elements of an array are active,
1174 * GetActiveUniform will return the name of the array in name,
1175 * subject to the restrictions listed above. The type of the array
1176 * is returned in type. The size parameter contains the highest
1177 * array element index used, plus one. The compiler or linker
1178 * determines the highest index used. There will be only one
1179 * active uniform reported by the GL per uniform array.
1183 update_array_sizes(struct gl_shader_program
*prog
)
1185 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1186 if (prog
->_LinkedShaders
[i
] == NULL
)
1189 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
1190 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1192 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
&&
1193 var
->mode
!= ir_var_in
&&
1194 var
->mode
!= ir_var_out
) ||
1195 !var
->type
->is_array())
1198 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1199 * will not be eliminated. Since we always do std140, just
1200 * don't resize arrays in UBOs.
1202 if (var
->uniform_block
!= -1)
1205 unsigned int size
= var
->max_array_access
;
1206 for (unsigned j
= 0; j
< MESA_SHADER_TYPES
; j
++) {
1207 if (prog
->_LinkedShaders
[j
] == NULL
)
1210 foreach_list(node2
, prog
->_LinkedShaders
[j
]->ir
) {
1211 ir_variable
*other_var
= ((ir_instruction
*) node2
)->as_variable();
1215 if (strcmp(var
->name
, other_var
->name
) == 0 &&
1216 other_var
->max_array_access
> size
) {
1217 size
= other_var
->max_array_access
;
1222 if (size
+ 1 != var
->type
->fields
.array
->length
) {
1223 /* If this is a built-in uniform (i.e., it's backed by some
1224 * fixed-function state), adjust the number of state slots to
1225 * match the new array size. The number of slots per array entry
1226 * is not known. It seems safe to assume that the total number of
1227 * slots is an integer multiple of the number of array elements.
1228 * Determine the number of slots per array element by dividing by
1229 * the old (total) size.
1231 if (var
->num_state_slots
> 0) {
1232 var
->num_state_slots
= (size
+ 1)
1233 * (var
->num_state_slots
/ var
->type
->length
);
1236 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
1238 /* FINISHME: We should update the types of array
1239 * dereferences of this variable now.
1247 * Find a contiguous set of available bits in a bitmask.
1249 * \param used_mask Bits representing used (1) and unused (0) locations
1250 * \param needed_count Number of contiguous bits needed.
1253 * Base location of the available bits on success or -1 on failure.
1256 find_available_slots(unsigned used_mask
, unsigned needed_count
)
1258 unsigned needed_mask
= (1 << needed_count
) - 1;
1259 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
1261 /* The comparison to 32 is redundant, but without it GCC emits "warning:
1262 * cannot optimize possibly infinite loops" for the loop below.
1264 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
1267 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
1268 if ((needed_mask
& ~used_mask
) == needed_mask
)
1279 * Assign locations for either VS inputs for FS outputs
1281 * \param prog Shader program whose variables need locations assigned
1282 * \param target_index Selector for the program target to receive location
1283 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
1284 * \c MESA_SHADER_FRAGMENT.
1285 * \param max_index Maximum number of generic locations. This corresponds
1286 * to either the maximum number of draw buffers or the
1287 * maximum number of generic attributes.
1290 * If locations are successfully assigned, true is returned. Otherwise an
1291 * error is emitted to the shader link log and false is returned.
1294 assign_attribute_or_color_locations(gl_shader_program
*prog
,
1295 unsigned target_index
,
1298 /* Mark invalid locations as being used.
1300 unsigned used_locations
= (max_index
>= 32)
1301 ? ~0 : ~((1 << max_index
) - 1);
1303 assert((target_index
== MESA_SHADER_VERTEX
)
1304 || (target_index
== MESA_SHADER_FRAGMENT
));
1306 gl_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
1310 /* Operate in a total of four passes.
1312 * 1. Invalidate the location assignments for all vertex shader inputs.
1314 * 2. Assign locations for inputs that have user-defined (via
1315 * glBindVertexAttribLocation) locations and outputs that have
1316 * user-defined locations (via glBindFragDataLocation).
1318 * 3. Sort the attributes without assigned locations by number of slots
1319 * required in decreasing order. Fragmentation caused by attribute
1320 * locations assigned by the application may prevent large attributes
1321 * from having enough contiguous space.
1323 * 4. Assign locations to any inputs without assigned locations.
1326 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
1327 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
1329 const enum ir_variable_mode direction
=
1330 (target_index
== MESA_SHADER_VERTEX
) ? ir_var_in
: ir_var_out
;
1333 /* Temporary storage for the set of attributes that need locations assigned.
1339 /* Used below in the call to qsort. */
1340 static int compare(const void *a
, const void *b
)
1342 const temp_attr
*const l
= (const temp_attr
*) a
;
1343 const temp_attr
*const r
= (const temp_attr
*) b
;
1345 /* Reversed because we want a descending order sort below. */
1346 return r
->slots
- l
->slots
;
1350 unsigned num_attr
= 0;
1352 foreach_list(node
, sh
->ir
) {
1353 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1355 if ((var
== NULL
) || (var
->mode
!= (unsigned) direction
))
1358 if (var
->explicit_location
) {
1359 if ((var
->location
>= (int)(max_index
+ generic_base
))
1360 || (var
->location
< 0)) {
1362 "invalid explicit location %d specified for `%s'\n",
1364 ? var
->location
: var
->location
- generic_base
,
1368 } else if (target_index
== MESA_SHADER_VERTEX
) {
1371 if (prog
->AttributeBindings
->get(binding
, var
->name
)) {
1372 assert(binding
>= VERT_ATTRIB_GENERIC0
);
1373 var
->location
= binding
;
1374 var
->is_unmatched_generic_inout
= 0;
1376 } else if (target_index
== MESA_SHADER_FRAGMENT
) {
1380 if (prog
->FragDataBindings
->get(binding
, var
->name
)) {
1381 assert(binding
>= FRAG_RESULT_DATA0
);
1382 var
->location
= binding
;
1383 var
->is_unmatched_generic_inout
= 0;
1385 if (prog
->FragDataIndexBindings
->get(index
, var
->name
)) {
1391 /* If the variable is not a built-in and has a location statically
1392 * assigned in the shader (presumably via a layout qualifier), make sure
1393 * that it doesn't collide with other assigned locations. Otherwise,
1394 * add it to the list of variables that need linker-assigned locations.
1396 const unsigned slots
= count_attribute_slots(var
->type
);
1397 if (var
->location
!= -1) {
1398 if (var
->location
>= generic_base
&& var
->index
< 1) {
1399 /* From page 61 of the OpenGL 4.0 spec:
1401 * "LinkProgram will fail if the attribute bindings assigned
1402 * by BindAttribLocation do not leave not enough space to
1403 * assign a location for an active matrix attribute or an
1404 * active attribute array, both of which require multiple
1405 * contiguous generic attributes."
1407 * Previous versions of the spec contain similar language but omit
1408 * the bit about attribute arrays.
1410 * Page 61 of the OpenGL 4.0 spec also says:
1412 * "It is possible for an application to bind more than one
1413 * attribute name to the same location. This is referred to as
1414 * aliasing. This will only work if only one of the aliased
1415 * attributes is active in the executable program, or if no
1416 * path through the shader consumes more than one attribute of
1417 * a set of attributes aliased to the same location. A link
1418 * error can occur if the linker determines that every path
1419 * through the shader consumes multiple aliased attributes,
1420 * but implementations are not required to generate an error
1423 * These two paragraphs are either somewhat contradictory, or I
1424 * don't fully understand one or both of them.
1426 /* FINISHME: The code as currently written does not support
1427 * FINISHME: attribute location aliasing (see comment above).
1429 /* Mask representing the contiguous slots that will be used by
1432 const unsigned attr
= var
->location
- generic_base
;
1433 const unsigned use_mask
= (1 << slots
) - 1;
1435 /* Generate a link error if the set of bits requested for this
1436 * attribute overlaps any previously allocated bits.
1438 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
1439 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
1440 ? "vertex shader input" : "fragment shader output";
1442 "insufficient contiguous locations "
1443 "available for %s `%s' %d %d %d", string
,
1444 var
->name
, used_locations
, use_mask
, attr
);
1448 used_locations
|= (use_mask
<< attr
);
1454 to_assign
[num_attr
].slots
= slots
;
1455 to_assign
[num_attr
].var
= var
;
1459 /* If all of the attributes were assigned locations by the application (or
1460 * are built-in attributes with fixed locations), return early. This should
1461 * be the common case.
1466 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
1468 if (target_index
== MESA_SHADER_VERTEX
) {
1469 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
1470 * only be explicitly assigned by via glBindAttribLocation. Mark it as
1471 * reserved to prevent it from being automatically allocated below.
1473 find_deref_visitor
find("gl_Vertex");
1475 if (find
.variable_found())
1476 used_locations
|= (1 << 0);
1479 for (unsigned i
= 0; i
< num_attr
; i
++) {
1480 /* Mask representing the contiguous slots that will be used by this
1483 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
1485 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
1488 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
1489 ? "vertex shader input" : "fragment shader output";
1492 "insufficient contiguous locations "
1493 "available for %s `%s'",
1494 string
, to_assign
[i
].var
->name
);
1498 to_assign
[i
].var
->location
= generic_base
+ location
;
1499 to_assign
[i
].var
->is_unmatched_generic_inout
= 0;
1500 used_locations
|= (use_mask
<< location
);
1508 * Demote shader inputs and outputs that are not used in other stages
1511 demote_shader_inputs_and_outputs(gl_shader
*sh
, enum ir_variable_mode mode
)
1513 foreach_list(node
, sh
->ir
) {
1514 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1516 if ((var
== NULL
) || (var
->mode
!= int(mode
)))
1519 /* A shader 'in' or 'out' variable is only really an input or output if
1520 * its value is used by other shader stages. This will cause the variable
1521 * to have a location assigned.
1523 if (var
->is_unmatched_generic_inout
) {
1524 var
->mode
= ir_var_auto
;
1531 * Data structure tracking information about a transform feedback declaration
1534 class tfeedback_decl
1537 bool init(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1538 const void *mem_ctx
, const char *input
);
1539 static bool is_same(const tfeedback_decl
&x
, const tfeedback_decl
&y
);
1540 bool assign_location(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1541 ir_variable
*output_var
);
1542 unsigned get_num_outputs() const;
1543 bool store(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1544 struct gl_transform_feedback_info
*info
, unsigned buffer
,
1545 const unsigned max_outputs
) const;
1546 ir_variable
*find_output_var(gl_shader_program
*prog
,
1547 gl_shader
*producer
) const;
1549 bool is_next_buffer_separator() const
1551 return this->next_buffer_separator
;
1554 bool is_varying() const
1556 return !this->next_buffer_separator
&& !this->skip_components
;
1560 * The total number of varying components taken up by this variable. Only
1561 * valid if assign_location() has been called.
1563 unsigned num_components() const
1565 if (this->is_clip_distance_mesa
)
1568 return this->vector_elements
* this->matrix_columns
* this->size
;
1573 * The name that was supplied to glTransformFeedbackVaryings. Used for
1574 * error reporting and glGetTransformFeedbackVarying().
1576 const char *orig_name
;
1579 * The name of the variable, parsed from orig_name.
1581 const char *var_name
;
1584 * True if the declaration in orig_name represents an array.
1586 bool is_subscripted
;
1589 * If is_subscripted is true, the subscript that was specified in orig_name.
1591 unsigned array_subscript
;
1594 * True if the variable is gl_ClipDistance and the driver lowers
1595 * gl_ClipDistance to gl_ClipDistanceMESA.
1597 bool is_clip_distance_mesa
;
1600 * The vertex shader output location that the linker assigned for this
1601 * variable. -1 if a location hasn't been assigned yet.
1606 * If non-zero, then this variable may be packed along with other variables
1607 * into a single varying slot, so this offset should be applied when
1608 * accessing components. For example, an offset of 1 means that the x
1609 * component of this variable is actually stored in component y of the
1610 * location specified by \c location.
1612 * Only valid if location != -1.
1614 unsigned location_frac
;
1617 * If location != -1, the number of vector elements in this variable, or 1
1618 * if this variable is a scalar.
1620 unsigned vector_elements
;
1623 * If location != -1, the number of matrix columns in this variable, or 1
1624 * if this variable is not a matrix.
1626 unsigned matrix_columns
;
1628 /** Type of the varying returned by glGetTransformFeedbackVarying() */
1632 * If location != -1, the size that should be returned by
1633 * glGetTransformFeedbackVarying().
1638 * How many components to skip. If non-zero, this is
1639 * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1641 unsigned skip_components
;
1644 * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1646 bool next_buffer_separator
;
1651 * Initialize this object based on a string that was passed to
1652 * glTransformFeedbackVaryings. If there is a parse error, the error is
1653 * reported using linker_error(), and false is returned.
1656 tfeedback_decl::init(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1657 const void *mem_ctx
, const char *input
)
1659 /* We don't have to be pedantic about what is a valid GLSL variable name,
1660 * because any variable with an invalid name can't exist in the IR anyway.
1663 this->location
= -1;
1664 this->orig_name
= input
;
1665 this->is_clip_distance_mesa
= false;
1666 this->skip_components
= 0;
1667 this->next_buffer_separator
= false;
1669 if (ctx
->Extensions
.ARB_transform_feedback3
) {
1670 /* Parse gl_NextBuffer. */
1671 if (strcmp(input
, "gl_NextBuffer") == 0) {
1672 this->next_buffer_separator
= true;
1676 /* Parse gl_SkipComponents. */
1677 if (strcmp(input
, "gl_SkipComponents1") == 0)
1678 this->skip_components
= 1;
1679 else if (strcmp(input
, "gl_SkipComponents2") == 0)
1680 this->skip_components
= 2;
1681 else if (strcmp(input
, "gl_SkipComponents3") == 0)
1682 this->skip_components
= 3;
1683 else if (strcmp(input
, "gl_SkipComponents4") == 0)
1684 this->skip_components
= 4;
1686 if (this->skip_components
)
1690 /* Parse a declaration. */
1691 const char *bracket
= strrchr(input
, '[');
1694 this->var_name
= ralloc_strndup(mem_ctx
, input
, bracket
- input
);
1695 if (sscanf(bracket
, "[%u]", &this->array_subscript
) != 1) {
1696 linker_error(prog
, "Cannot parse transform feedback varying %s", input
);
1699 this->is_subscripted
= true;
1701 this->var_name
= ralloc_strdup(mem_ctx
, input
);
1702 this->is_subscripted
= false;
1705 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1706 * class must behave specially to account for the fact that gl_ClipDistance
1707 * is converted from a float[8] to a vec4[2].
1709 if (ctx
->ShaderCompilerOptions
[MESA_SHADER_VERTEX
].LowerClipDistance
&&
1710 strcmp(this->var_name
, "gl_ClipDistance") == 0) {
1711 this->is_clip_distance_mesa
= true;
1719 * Determine whether two tfeedback_decl objects refer to the same variable and
1720 * array index (if applicable).
1723 tfeedback_decl::is_same(const tfeedback_decl
&x
, const tfeedback_decl
&y
)
1725 assert(x
.is_varying() && y
.is_varying());
1727 if (strcmp(x
.var_name
, y
.var_name
) != 0)
1729 if (x
.is_subscripted
!= y
.is_subscripted
)
1731 if (x
.is_subscripted
&& x
.array_subscript
!= y
.array_subscript
)
1738 * Assign a location for this tfeedback_decl object based on the location
1739 * assignment in output_var.
1741 * If an error occurs, the error is reported through linker_error() and false
1745 tfeedback_decl::assign_location(struct gl_context
*ctx
,
1746 struct gl_shader_program
*prog
,
1747 ir_variable
*output_var
)
1749 assert(this->is_varying());
1751 if (output_var
->type
->is_array()) {
1752 /* Array variable */
1753 const unsigned matrix_cols
=
1754 output_var
->type
->fields
.array
->matrix_columns
;
1755 const unsigned vector_elements
=
1756 output_var
->type
->fields
.array
->vector_elements
;
1757 unsigned actual_array_size
= this->is_clip_distance_mesa
?
1758 prog
->Vert
.ClipDistanceArraySize
: output_var
->type
->array_size();
1760 if (this->is_subscripted
) {
1761 /* Check array bounds. */
1762 if (this->array_subscript
>= actual_array_size
) {
1763 linker_error(prog
, "Transform feedback varying %s has index "
1764 "%i, but the array size is %u.",
1765 this->orig_name
, this->array_subscript
,
1769 if (this->is_clip_distance_mesa
) {
1771 output_var
->location
+ this->array_subscript
/ 4;
1772 this->location_frac
= this->array_subscript
% 4;
1774 unsigned fine_location
1775 = output_var
->location
* 4 + output_var
->location_frac
;
1776 unsigned array_elem_size
= vector_elements
* matrix_cols
;
1777 fine_location
+= array_elem_size
* this->array_subscript
;
1778 this->location
= fine_location
/ 4;
1779 this->location_frac
= fine_location
% 4;
1783 this->location
= output_var
->location
;
1784 this->location_frac
= output_var
->location_frac
;
1785 this->size
= actual_array_size
;
1787 this->vector_elements
= vector_elements
;
1788 this->matrix_columns
= matrix_cols
;
1789 if (this->is_clip_distance_mesa
)
1790 this->type
= GL_FLOAT
;
1792 this->type
= output_var
->type
->fields
.array
->gl_type
;
1794 /* Regular variable (scalar, vector, or matrix) */
1795 if (this->is_subscripted
) {
1796 linker_error(prog
, "Transform feedback varying %s requested, "
1797 "but %s is not an array.",
1798 this->orig_name
, this->var_name
);
1801 this->location
= output_var
->location
;
1802 this->location_frac
= output_var
->location_frac
;
1804 this->vector_elements
= output_var
->type
->vector_elements
;
1805 this->matrix_columns
= output_var
->type
->matrix_columns
;
1806 this->type
= output_var
->type
->gl_type
;
1809 /* From GL_EXT_transform_feedback:
1810 * A program will fail to link if:
1812 * * the total number of components to capture in any varying
1813 * variable in <varyings> is greater than the constant
1814 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1815 * buffer mode is SEPARATE_ATTRIBS_EXT;
1817 if (prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
&&
1818 this->num_components() >
1819 ctx
->Const
.MaxTransformFeedbackSeparateComponents
) {
1820 linker_error(prog
, "Transform feedback varying %s exceeds "
1821 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1831 tfeedback_decl::get_num_outputs() const
1833 if (!this->is_varying()) {
1837 return (this->num_components() + this->location_frac
+ 3)/4;
1842 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1844 * If an error occurs, the error is reported through linker_error() and false
1848 tfeedback_decl::store(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1849 struct gl_transform_feedback_info
*info
,
1850 unsigned buffer
, const unsigned max_outputs
) const
1852 assert(!this->next_buffer_separator
);
1854 /* Handle gl_SkipComponents. */
1855 if (this->skip_components
) {
1856 info
->BufferStride
[buffer
] += this->skip_components
;
1860 /* From GL_EXT_transform_feedback:
1861 * A program will fail to link if:
1863 * * the total number of components to capture is greater than
1864 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1865 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1867 if (prog
->TransformFeedback
.BufferMode
== GL_INTERLEAVED_ATTRIBS
&&
1868 info
->BufferStride
[buffer
] + this->num_components() >
1869 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
1870 linker_error(prog
, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1871 "limit has been exceeded.");
1875 unsigned location
= this->location
;
1876 unsigned location_frac
= this->location_frac
;
1877 unsigned num_components
= this->num_components();
1878 while (num_components
> 0) {
1879 unsigned output_size
= MIN2(num_components
, 4 - location_frac
);
1880 assert(info
->NumOutputs
< max_outputs
);
1881 info
->Outputs
[info
->NumOutputs
].ComponentOffset
= location_frac
;
1882 info
->Outputs
[info
->NumOutputs
].OutputRegister
= location
;
1883 info
->Outputs
[info
->NumOutputs
].NumComponents
= output_size
;
1884 info
->Outputs
[info
->NumOutputs
].OutputBuffer
= buffer
;
1885 info
->Outputs
[info
->NumOutputs
].DstOffset
= info
->BufferStride
[buffer
];
1887 info
->BufferStride
[buffer
] += output_size
;
1888 num_components
-= output_size
;
1893 info
->Varyings
[info
->NumVarying
].Name
= ralloc_strdup(prog
, this->orig_name
);
1894 info
->Varyings
[info
->NumVarying
].Type
= this->type
;
1895 info
->Varyings
[info
->NumVarying
].Size
= this->size
;
1903 tfeedback_decl::find_output_var(gl_shader_program
*prog
,
1904 gl_shader
*producer
) const
1906 const char *name
= this->is_clip_distance_mesa
1907 ? "gl_ClipDistanceMESA" : this->var_name
;
1908 ir_variable
*var
= producer
->symbols
->get_variable(name
);
1909 if (var
&& var
->mode
== ir_var_out
)
1912 /* From GL_EXT_transform_feedback:
1913 * A program will fail to link if:
1915 * * any variable name specified in the <varyings> array is not
1916 * declared as an output in the geometry shader (if present) or
1917 * the vertex shader (if no geometry shader is present);
1919 linker_error(prog
, "Transform feedback varying %s undeclared.",
1926 * Parse all the transform feedback declarations that were passed to
1927 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1929 * If an error occurs, the error is reported through linker_error() and false
1933 parse_tfeedback_decls(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1934 const void *mem_ctx
, unsigned num_names
,
1935 char **varying_names
, tfeedback_decl
*decls
)
1937 for (unsigned i
= 0; i
< num_names
; ++i
) {
1938 if (!decls
[i
].init(ctx
, prog
, mem_ctx
, varying_names
[i
]))
1941 if (!decls
[i
].is_varying())
1944 /* From GL_EXT_transform_feedback:
1945 * A program will fail to link if:
1947 * * any two entries in the <varyings> array specify the same varying
1950 * We interpret this to mean "any two entries in the <varyings> array
1951 * specify the same varying variable and array index", since transform
1952 * feedback of arrays would be useless otherwise.
1954 for (unsigned j
= 0; j
< i
; ++j
) {
1955 if (!decls
[j
].is_varying())
1958 if (tfeedback_decl::is_same(decls
[i
], decls
[j
])) {
1959 linker_error(prog
, "Transform feedback varying %s specified "
1960 "more than once.", varying_names
[i
]);
1970 * Data structure recording the relationship between outputs of one shader
1971 * stage (the "producer") and inputs of another (the "consumer").
1973 class varying_matches
1976 varying_matches(bool disable_varying_packing
);
1978 void record(ir_variable
*producer_var
, ir_variable
*consumer_var
);
1979 unsigned assign_locations();
1980 void store_locations(unsigned producer_base
, unsigned consumer_base
) const;
1984 * If true, this driver disables varying packing, so all varyings need to
1985 * be aligned on slot boundaries, and take up a number of slots equal to
1986 * their number of matrix columns times their array size.
1988 const bool disable_varying_packing
;
1991 * Enum representing the order in which varyings are packed within a
1994 * Currently we pack vec4's first, then vec2's, then scalar values, then
1995 * vec3's. This order ensures that the only vectors that are at risk of
1996 * having to be "double parked" (split between two adjacent varying slots)
1999 enum packing_order_enum
{
2002 PACKING_ORDER_SCALAR
,
2006 static unsigned compute_packing_class(ir_variable
*var
);
2007 static packing_order_enum
compute_packing_order(ir_variable
*var
);
2008 static int match_comparator(const void *x_generic
, const void *y_generic
);
2011 * Structure recording the relationship between a single producer output
2012 * and a single consumer input.
2016 * Packing class for this varying, computed by compute_packing_class().
2018 unsigned packing_class
;
2021 * Packing order for this varying, computed by compute_packing_order().
2023 packing_order_enum packing_order
;
2024 unsigned num_components
;
2027 * The output variable in the producer stage.
2029 ir_variable
*producer_var
;
2032 * The input variable in the consumer stage.
2034 ir_variable
*consumer_var
;
2037 * The location which has been assigned for this varying. This is
2038 * expressed in multiples of a float, with the first generic varying
2039 * (i.e. the one referred to by VERT_RESULT_VAR0 or FRAG_ATTRIB_VAR0)
2040 * represented by the value 0.
2042 unsigned generic_location
;
2046 * The number of elements in the \c matches array that are currently in
2049 unsigned num_matches
;
2052 * The number of elements that were set aside for the \c matches array when
2055 unsigned matches_capacity
;
2059 varying_matches::varying_matches(bool disable_varying_packing
)
2060 : disable_varying_packing(disable_varying_packing
)
2062 /* Note: this initial capacity is rather arbitrarily chosen to be large
2063 * enough for many cases without wasting an unreasonable amount of space.
2064 * varying_matches::record() will resize the array if there are more than
2065 * this number of varyings.
2067 this->matches_capacity
= 8;
2068 this->matches
= (match
*)
2069 malloc(sizeof(*this->matches
) * this->matches_capacity
);
2070 this->num_matches
= 0;
2074 varying_matches::~varying_matches()
2076 free(this->matches
);
2081 * Record the given producer/consumer variable pair in the list of variables
2082 * that should later be assigned locations.
2084 * It is permissible for \c consumer_var to be NULL (this happens if a
2085 * variable is output by the producer and consumed by transform feedback, but
2086 * not consumed by the consumer).
2088 * If \c producer_var has already been paired up with a consumer_var, or
2089 * producer_var is part of fixed pipeline functionality (and hence already has
2090 * a location assigned), this function has no effect.
2093 varying_matches::record(ir_variable
*producer_var
, ir_variable
*consumer_var
)
2095 if (!producer_var
->is_unmatched_generic_inout
) {
2096 /* Either a location already exists for this variable (since it is part
2097 * of fixed functionality), or it has already been recorded as part of a
2103 if (this->num_matches
== this->matches_capacity
) {
2104 this->matches_capacity
*= 2;
2105 this->matches
= (match
*)
2106 realloc(this->matches
,
2107 sizeof(*this->matches
) * this->matches_capacity
);
2109 this->matches
[this->num_matches
].packing_class
2110 = this->compute_packing_class(producer_var
);
2111 this->matches
[this->num_matches
].packing_order
2112 = this->compute_packing_order(producer_var
);
2113 if (this->disable_varying_packing
) {
2114 unsigned slots
= producer_var
->type
->is_array()
2115 ? (producer_var
->type
->length
2116 * producer_var
->type
->fields
.array
->matrix_columns
)
2117 : producer_var
->type
->matrix_columns
;
2118 this->matches
[this->num_matches
].num_components
= 4 * slots
;
2120 this->matches
[this->num_matches
].num_components
2121 = producer_var
->type
->component_slots();
2123 this->matches
[this->num_matches
].producer_var
= producer_var
;
2124 this->matches
[this->num_matches
].consumer_var
= consumer_var
;
2125 this->num_matches
++;
2126 producer_var
->is_unmatched_generic_inout
= 0;
2128 consumer_var
->is_unmatched_generic_inout
= 0;
2133 * Choose locations for all of the variable matches that were previously
2134 * passed to varying_matches::record().
2137 varying_matches::assign_locations()
2139 /* Sort varying matches into an order that makes them easy to pack. */
2140 qsort(this->matches
, this->num_matches
, sizeof(*this->matches
),
2141 &varying_matches::match_comparator
);
2143 unsigned generic_location
= 0;
2145 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
2146 /* Advance to the next slot if this varying has a different packing
2147 * class than the previous one, and we're not already on a slot
2151 this->matches
[i
- 1].packing_class
2152 != this->matches
[i
].packing_class
) {
2153 generic_location
= ALIGN(generic_location
, 4);
2156 this->matches
[i
].generic_location
= generic_location
;
2158 generic_location
+= this->matches
[i
].num_components
;
2161 return (generic_location
+ 3) / 4;
2166 * Update the producer and consumer shaders to reflect the locations
2167 * assignments that were made by varying_matches::assign_locations().
2170 varying_matches::store_locations(unsigned producer_base
,
2171 unsigned consumer_base
) const
2173 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
2174 ir_variable
*producer_var
= this->matches
[i
].producer_var
;
2175 ir_variable
*consumer_var
= this->matches
[i
].consumer_var
;
2176 unsigned generic_location
= this->matches
[i
].generic_location
;
2177 unsigned slot
= generic_location
/ 4;
2178 unsigned offset
= generic_location
% 4;
2180 producer_var
->location
= producer_base
+ slot
;
2181 producer_var
->location_frac
= offset
;
2183 assert(consumer_var
->location
== -1);
2184 consumer_var
->location
= consumer_base
+ slot
;
2185 consumer_var
->location_frac
= offset
;
2192 * Compute the "packing class" of the given varying. This is an unsigned
2193 * integer with the property that two variables in the same packing class can
2194 * be safely backed into the same vec4.
2197 varying_matches::compute_packing_class(ir_variable
*var
)
2199 /* In this initial implementation we conservatively assume that variables
2200 * can only be packed if their base type (float/int/uint/bool) matches and
2201 * their interpolation and centroid qualifiers match.
2203 * TODO: relax these restrictions when the driver back-end permits.
2205 unsigned packing_class
= var
->centroid
? 1 : 0;
2207 packing_class
+= var
->interpolation
;
2208 packing_class
*= GLSL_TYPE_ERROR
;
2209 packing_class
+= var
->type
->get_scalar_type()->base_type
;
2210 return packing_class
;
2215 * Compute the "packing order" of the given varying. This is a sort key we
2216 * use to determine when to attempt to pack the given varying relative to
2217 * other varyings in the same packing class.
2219 varying_matches::packing_order_enum
2220 varying_matches::compute_packing_order(ir_variable
*var
)
2222 const glsl_type
*element_type
= var
->type
;
2224 /* FINISHME: Support for "varying" records in GLSL 1.50. */
2225 while (element_type
->base_type
== GLSL_TYPE_ARRAY
) {
2226 element_type
= element_type
->fields
.array
;
2229 switch (element_type
->vector_elements
) {
2230 case 1: return PACKING_ORDER_SCALAR
;
2231 case 2: return PACKING_ORDER_VEC2
;
2232 case 3: return PACKING_ORDER_VEC3
;
2233 case 4: return PACKING_ORDER_VEC4
;
2235 assert(!"Unexpected value of vector_elements");
2236 return PACKING_ORDER_VEC4
;
2242 * Comparison function passed to qsort() to sort varyings by packing_class and
2243 * then by packing_order.
2246 varying_matches::match_comparator(const void *x_generic
, const void *y_generic
)
2248 const match
*x
= (const match
*) x_generic
;
2249 const match
*y
= (const match
*) y_generic
;
2251 if (x
->packing_class
!= y
->packing_class
)
2252 return x
->packing_class
- y
->packing_class
;
2253 return x
->packing_order
- y
->packing_order
;
2258 * Is the given variable a varying variable to be counted against the
2259 * limit in ctx->Const.MaxVarying?
2260 * This includes variables such as texcoords, colors and generic
2261 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2264 is_varying_var(GLenum shaderType
, const ir_variable
*var
)
2266 /* Only fragment shaders will take a varying variable as an input */
2267 if (shaderType
== GL_FRAGMENT_SHADER
&&
2268 var
->mode
== ir_var_in
) {
2269 switch (var
->location
) {
2270 case FRAG_ATTRIB_WPOS
:
2271 case FRAG_ATTRIB_FACE
:
2272 case FRAG_ATTRIB_PNTC
:
2283 * Assign locations for all variables that are produced in one pipeline stage
2284 * (the "producer") and consumed in the next stage (the "consumer").
2286 * Variables produced by the producer may also be consumed by transform
2289 * \param num_tfeedback_decls is the number of declarations indicating
2290 * variables that may be consumed by transform feedback.
2292 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2293 * representing the result of parsing the strings passed to
2294 * glTransformFeedbackVaryings(). assign_location() will be called for
2295 * each of these objects that matches one of the outputs of the
2298 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2299 * be NULL. In this case, varying locations are assigned solely based on the
2300 * requirements of transform feedback.
2303 assign_varying_locations(struct gl_context
*ctx
,
2304 struct gl_shader_program
*prog
,
2305 gl_shader
*producer
, gl_shader
*consumer
,
2306 unsigned num_tfeedback_decls
,
2307 tfeedback_decl
*tfeedback_decls
)
2309 /* FINISHME: Set dynamically when geometry shader support is added. */
2310 const unsigned producer_base
= VERT_RESULT_VAR0
;
2311 const unsigned consumer_base
= FRAG_ATTRIB_VAR0
;
2312 varying_matches
matches(ctx
->Const
.DisableVaryingPacking
);
2314 /* Operate in a total of three passes.
2316 * 1. Assign locations for any matching inputs and outputs.
2318 * 2. Mark output variables in the producer that do not have locations as
2319 * not being outputs. This lets the optimizer eliminate them.
2321 * 3. Mark input variables in the consumer that do not have locations as
2322 * not being inputs. This lets the optimizer eliminate them.
2325 foreach_list(node
, producer
->ir
) {
2326 ir_variable
*const output_var
= ((ir_instruction
*) node
)->as_variable();
2328 if ((output_var
== NULL
) || (output_var
->mode
!= ir_var_out
))
2331 ir_variable
*input_var
=
2332 consumer
? consumer
->symbols
->get_variable(output_var
->name
) : NULL
;
2334 if (input_var
&& input_var
->mode
!= ir_var_in
)
2338 matches
.record(output_var
, input_var
);
2342 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
2343 if (!tfeedback_decls
[i
].is_varying())
2346 ir_variable
*output_var
2347 = tfeedback_decls
[i
].find_output_var(prog
, producer
);
2349 if (output_var
== NULL
)
2352 if (output_var
->is_unmatched_generic_inout
) {
2353 matches
.record(output_var
, NULL
);
2357 const unsigned slots_used
= matches
.assign_locations();
2358 matches
.store_locations(producer_base
, consumer_base
);
2360 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
2361 if (!tfeedback_decls
[i
].is_varying())
2364 ir_variable
*output_var
2365 = tfeedback_decls
[i
].find_output_var(prog
, producer
);
2367 if (!tfeedback_decls
[i
].assign_location(ctx
, prog
, output_var
))
2371 if (ctx
->Const
.DisableVaryingPacking
) {
2372 /* Transform feedback code assumes varyings are packed, so if the driver
2373 * has disabled varying packing, make sure it does not support transform
2376 assert(!ctx
->Extensions
.EXT_transform_feedback
);
2378 lower_packed_varyings(ctx
, producer_base
, slots_used
, ir_var_out
,
2381 lower_packed_varyings(ctx
, consumer_base
, slots_used
, ir_var_in
,
2386 unsigned varying_vectors
= 0;
2389 foreach_list(node
, consumer
->ir
) {
2390 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
2392 if ((var
== NULL
) || (var
->mode
!= ir_var_in
))
2395 if (var
->is_unmatched_generic_inout
) {
2396 if (prog
->Version
<= 120) {
2397 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2399 * Only those varying variables used (i.e. read) in
2400 * the fragment shader executable must be written to
2401 * by the vertex shader executable; declaring
2402 * superfluous varying variables in a vertex shader is
2405 * We interpret this text as meaning that the VS must
2406 * write the variable for the FS to read it. See
2407 * "glsl1-varying read but not written" in piglit.
2410 linker_error(prog
, "fragment shader varying %s not written "
2411 "by vertex shader\n.", var
->name
);
2414 /* An 'in' variable is only really a shader input if its
2415 * value is written by the previous stage.
2417 var
->mode
= ir_var_auto
;
2418 } else if (is_varying_var(consumer
->Type
, var
)) {
2419 /* The packing rules are used for vertex shader inputs are also
2420 * used for fragment shader inputs.
2422 varying_vectors
+= count_attribute_slots(var
->type
);
2427 if (ctx
->API
== API_OPENGLES2
|| prog
->IsES
) {
2428 if (varying_vectors
> ctx
->Const
.MaxVarying
) {
2429 if (ctx
->Const
.GLSLSkipStrictMaxVaryingLimitCheck
) {
2430 linker_warning(prog
, "shader uses too many varying vectors "
2431 "(%u > %u), but the driver will try to optimize "
2432 "them out; this is non-portable out-of-spec "
2434 varying_vectors
, ctx
->Const
.MaxVarying
);
2436 linker_error(prog
, "shader uses too many varying vectors "
2438 varying_vectors
, ctx
->Const
.MaxVarying
);
2443 const unsigned float_components
= varying_vectors
* 4;
2444 if (float_components
> ctx
->Const
.MaxVarying
* 4) {
2445 if (ctx
->Const
.GLSLSkipStrictMaxVaryingLimitCheck
) {
2446 linker_warning(prog
, "shader uses too many varying components "
2447 "(%u > %u), but the driver will try to optimize "
2448 "them out; this is non-portable out-of-spec "
2450 float_components
, ctx
->Const
.MaxVarying
* 4);
2452 linker_error(prog
, "shader uses too many varying components "
2454 float_components
, ctx
->Const
.MaxVarying
* 4);
2465 * Store transform feedback location assignments into
2466 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2468 * If an error occurs, the error is reported through linker_error() and false
2472 store_tfeedback_info(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
2473 unsigned num_tfeedback_decls
,
2474 tfeedback_decl
*tfeedback_decls
)
2476 bool separate_attribs_mode
=
2477 prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
;
2479 ralloc_free(prog
->LinkedTransformFeedback
.Varyings
);
2480 ralloc_free(prog
->LinkedTransformFeedback
.Outputs
);
2482 memset(&prog
->LinkedTransformFeedback
, 0,
2483 sizeof(prog
->LinkedTransformFeedback
));
2485 prog
->LinkedTransformFeedback
.Varyings
=
2487 struct gl_transform_feedback_varying_info
,
2488 num_tfeedback_decls
);
2490 unsigned num_outputs
= 0;
2491 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
)
2492 num_outputs
+= tfeedback_decls
[i
].get_num_outputs();
2494 prog
->LinkedTransformFeedback
.Outputs
=
2496 struct gl_transform_feedback_output
,
2499 unsigned num_buffers
= 0;
2501 if (separate_attribs_mode
) {
2502 /* GL_SEPARATE_ATTRIBS */
2503 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
2504 if (!tfeedback_decls
[i
].store(ctx
, prog
, &prog
->LinkedTransformFeedback
,
2505 num_buffers
, num_outputs
))
2512 /* GL_INVERLEAVED_ATTRIBS */
2513 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
2514 if (tfeedback_decls
[i
].is_next_buffer_separator()) {
2519 if (!tfeedback_decls
[i
].store(ctx
, prog
,
2520 &prog
->LinkedTransformFeedback
,
2521 num_buffers
, num_outputs
))
2527 assert(prog
->LinkedTransformFeedback
.NumOutputs
== num_outputs
);
2529 prog
->LinkedTransformFeedback
.NumBuffers
= num_buffers
;
2534 * Store the gl_FragDepth layout in the gl_shader_program struct.
2537 store_fragdepth_layout(struct gl_shader_program
*prog
)
2539 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
2543 struct exec_list
*ir
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
2545 /* We don't look up the gl_FragDepth symbol directly because if
2546 * gl_FragDepth is not used in the shader, it's removed from the IR.
2547 * However, the symbol won't be removed from the symbol table.
2549 * We're only interested in the cases where the variable is NOT removed
2552 foreach_list(node
, ir
) {
2553 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
2555 if (var
== NULL
|| var
->mode
!= ir_var_out
) {
2559 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
2560 switch (var
->depth_layout
) {
2561 case ir_depth_layout_none
:
2562 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
2564 case ir_depth_layout_any
:
2565 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
2567 case ir_depth_layout_greater
:
2568 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
2570 case ir_depth_layout_less
:
2571 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
2573 case ir_depth_layout_unchanged
:
2574 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2585 * Validate the resources used by a program versus the implementation limits
2588 check_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
2590 static const char *const shader_names
[MESA_SHADER_TYPES
] = {
2591 "vertex", "fragment", "geometry"
2594 const unsigned max_samplers
[MESA_SHADER_TYPES
] = {
2595 ctx
->Const
.MaxVertexTextureImageUnits
,
2596 ctx
->Const
.MaxTextureImageUnits
,
2597 ctx
->Const
.MaxGeometryTextureImageUnits
2600 const unsigned max_uniform_components
[MESA_SHADER_TYPES
] = {
2601 ctx
->Const
.VertexProgram
.MaxUniformComponents
,
2602 ctx
->Const
.FragmentProgram
.MaxUniformComponents
,
2603 0 /* FINISHME: Geometry shaders. */
2606 const unsigned max_uniform_blocks
[MESA_SHADER_TYPES
] = {
2607 ctx
->Const
.VertexProgram
.MaxUniformBlocks
,
2608 ctx
->Const
.FragmentProgram
.MaxUniformBlocks
,
2609 ctx
->Const
.GeometryProgram
.MaxUniformBlocks
,
2612 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2613 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2618 if (sh
->num_samplers
> max_samplers
[i
]) {
2619 linker_error(prog
, "Too many %s shader texture samplers",
2623 if (sh
->num_uniform_components
> max_uniform_components
[i
]) {
2624 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
2625 linker_warning(prog
, "Too many %s shader uniform components, "
2626 "but the driver will try to optimize them out; "
2627 "this is non-portable out-of-spec behavior\n",
2630 linker_error(prog
, "Too many %s shader uniform components",
2636 unsigned blocks
[MESA_SHADER_TYPES
] = {0};
2637 unsigned total_uniform_blocks
= 0;
2639 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
2640 for (unsigned j
= 0; j
< MESA_SHADER_TYPES
; j
++) {
2641 if (prog
->UniformBlockStageIndex
[j
][i
] != -1) {
2643 total_uniform_blocks
++;
2647 if (total_uniform_blocks
> ctx
->Const
.MaxCombinedUniformBlocks
) {
2648 linker_error(prog
, "Too many combined uniform blocks (%d/%d)",
2649 prog
->NumUniformBlocks
,
2650 ctx
->Const
.MaxCombinedUniformBlocks
);
2652 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2653 if (blocks
[i
] > max_uniform_blocks
[i
]) {
2654 linker_error(prog
, "Too many %s uniform blocks (%d/%d)",
2657 max_uniform_blocks
[i
]);
2664 return prog
->LinkStatus
;
2668 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
2670 tfeedback_decl
*tfeedback_decls
= NULL
;
2671 unsigned num_tfeedback_decls
= prog
->TransformFeedback
.NumVarying
;
2673 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
2675 prog
->LinkStatus
= false;
2676 prog
->Validated
= false;
2677 prog
->_Used
= false;
2679 ralloc_free(prog
->InfoLog
);
2680 prog
->InfoLog
= ralloc_strdup(NULL
, "");
2682 ralloc_free(prog
->UniformBlocks
);
2683 prog
->UniformBlocks
= NULL
;
2684 prog
->NumUniformBlocks
= 0;
2685 for (int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2686 ralloc_free(prog
->UniformBlockStageIndex
[i
]);
2687 prog
->UniformBlockStageIndex
[i
] = NULL
;
2690 /* Separate the shaders into groups based on their type.
2692 struct gl_shader
**vert_shader_list
;
2693 unsigned num_vert_shaders
= 0;
2694 struct gl_shader
**frag_shader_list
;
2695 unsigned num_frag_shaders
= 0;
2697 vert_shader_list
= (struct gl_shader
**)
2698 calloc(2 * prog
->NumShaders
, sizeof(struct gl_shader
*));
2699 frag_shader_list
= &vert_shader_list
[prog
->NumShaders
];
2701 unsigned min_version
= UINT_MAX
;
2702 unsigned max_version
= 0;
2703 const bool is_es_prog
=
2704 (prog
->NumShaders
> 0 && prog
->Shaders
[0]->IsES
) ? true : false;
2705 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
2706 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
2707 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
2709 if (prog
->Shaders
[i
]->IsES
!= is_es_prog
) {
2710 linker_error(prog
, "all shaders must use same shading "
2711 "language version\n");
2715 switch (prog
->Shaders
[i
]->Type
) {
2716 case GL_VERTEX_SHADER
:
2717 vert_shader_list
[num_vert_shaders
] = prog
->Shaders
[i
];
2720 case GL_FRAGMENT_SHADER
:
2721 frag_shader_list
[num_frag_shaders
] = prog
->Shaders
[i
];
2724 case GL_GEOMETRY_SHADER
:
2725 /* FINISHME: Support geometry shaders. */
2726 assert(prog
->Shaders
[i
]->Type
!= GL_GEOMETRY_SHADER
);
2731 /* Previous to GLSL version 1.30, different compilation units could mix and
2732 * match shading language versions. With GLSL 1.30 and later, the versions
2733 * of all shaders must match.
2735 * GLSL ES has never allowed mixing of shading language versions.
2737 if ((is_es_prog
|| max_version
>= 130)
2738 && min_version
!= max_version
) {
2739 linker_error(prog
, "all shaders must use same shading "
2740 "language version\n");
2744 prog
->Version
= max_version
;
2745 prog
->IsES
= is_es_prog
;
2747 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2748 if (prog
->_LinkedShaders
[i
] != NULL
)
2749 ctx
->Driver
.DeleteShader(ctx
, prog
->_LinkedShaders
[i
]);
2751 prog
->_LinkedShaders
[i
] = NULL
;
2754 /* Link all shaders for a particular stage and validate the result.
2756 if (num_vert_shaders
> 0) {
2757 gl_shader
*const sh
=
2758 link_intrastage_shaders(mem_ctx
, ctx
, prog
, vert_shader_list
,
2764 if (!validate_vertex_shader_executable(prog
, sh
))
2767 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
2771 if (num_frag_shaders
> 0) {
2772 gl_shader
*const sh
=
2773 link_intrastage_shaders(mem_ctx
, ctx
, prog
, frag_shader_list
,
2779 if (!validate_fragment_shader_executable(prog
, sh
))
2782 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
],
2786 /* Here begins the inter-stage linking phase. Some initial validation is
2787 * performed, then locations are assigned for uniforms, attributes, and
2790 if (cross_validate_uniforms(prog
)) {
2793 for (prev
= 0; prev
< MESA_SHADER_TYPES
; prev
++) {
2794 if (prog
->_LinkedShaders
[prev
] != NULL
)
2798 /* Validate the inputs of each stage with the output of the preceding
2801 for (unsigned i
= prev
+ 1; i
< MESA_SHADER_TYPES
; i
++) {
2802 if (prog
->_LinkedShaders
[i
] == NULL
)
2805 if (!cross_validate_outputs_to_inputs(prog
,
2806 prog
->_LinkedShaders
[prev
],
2807 prog
->_LinkedShaders
[i
]))
2813 prog
->LinkStatus
= true;
2816 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2817 * it before optimization because we want most of the checks to get
2818 * dropped thanks to constant propagation.
2820 * This rule also applies to GLSL ES 3.00.
2822 if (max_version
>= (is_es_prog
? 300 : 130)) {
2823 struct gl_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
2825 lower_discard_flow(sh
->ir
);
2829 if (!interstage_cross_validate_uniform_blocks(prog
))
2832 /* Do common optimization before assigning storage for attributes,
2833 * uniforms, and varyings. Later optimization could possibly make
2834 * some of that unused.
2836 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2837 if (prog
->_LinkedShaders
[i
] == NULL
)
2840 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
2841 if (!prog
->LinkStatus
)
2844 if (ctx
->ShaderCompilerOptions
[i
].LowerClipDistance
) {
2845 lower_clip_distance(prog
->_LinkedShaders
[i
]);
2848 unsigned max_unroll
= ctx
->ShaderCompilerOptions
[i
].MaxUnrollIterations
;
2850 while (do_common_optimization(prog
->_LinkedShaders
[i
]->ir
, true, false, max_unroll
))
2854 /* Mark all generic shader inputs and outputs as unpaired. */
2855 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] != NULL
) {
2856 link_invalidate_variable_locations(
2857 prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
2858 VERT_ATTRIB_GENERIC0
, VERT_RESULT_VAR0
);
2860 /* FINISHME: Geometry shaders not implemented yet */
2861 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] != NULL
) {
2862 link_invalidate_variable_locations(
2863 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
],
2864 FRAG_ATTRIB_VAR0
, FRAG_RESULT_DATA0
);
2867 /* FINISHME: The value of the max_attribute_index parameter is
2868 * FINISHME: implementation dependent based on the value of
2869 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
2870 * FINISHME: at least 16, so hardcode 16 for now.
2872 if (!assign_attribute_or_color_locations(prog
, MESA_SHADER_VERTEX
, 16)) {
2876 if (!assign_attribute_or_color_locations(prog
, MESA_SHADER_FRAGMENT
, MAX2(ctx
->Const
.MaxDrawBuffers
, ctx
->Const
.MaxDualSourceDrawBuffers
))) {
2881 for (prev
= 0; prev
< MESA_SHADER_TYPES
; prev
++) {
2882 if (prog
->_LinkedShaders
[prev
] != NULL
)
2886 if (num_tfeedback_decls
!= 0) {
2887 /* From GL_EXT_transform_feedback:
2888 * A program will fail to link if:
2890 * * the <count> specified by TransformFeedbackVaryingsEXT is
2891 * non-zero, but the program object has no vertex or geometry
2894 if (prev
>= MESA_SHADER_FRAGMENT
) {
2895 linker_error(prog
, "Transform feedback varyings specified, but "
2896 "no vertex or geometry shader is present.");
2900 tfeedback_decls
= ralloc_array(mem_ctx
, tfeedback_decl
,
2901 prog
->TransformFeedback
.NumVarying
);
2902 if (!parse_tfeedback_decls(ctx
, prog
, mem_ctx
, num_tfeedback_decls
,
2903 prog
->TransformFeedback
.VaryingNames
,
2908 for (unsigned i
= prev
+ 1; i
< MESA_SHADER_TYPES
; i
++) {
2909 if (prog
->_LinkedShaders
[i
] == NULL
)
2912 if (!assign_varying_locations(
2913 ctx
, prog
, prog
->_LinkedShaders
[prev
], prog
->_LinkedShaders
[i
],
2914 i
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
2921 if (prev
!= MESA_SHADER_FRAGMENT
&& num_tfeedback_decls
!= 0) {
2922 /* There was no fragment shader, but we still have to assign varying
2923 * locations for use by transform feedback.
2925 if (!assign_varying_locations(
2926 ctx
, prog
, prog
->_LinkedShaders
[prev
], NULL
, num_tfeedback_decls
,
2931 if (!store_tfeedback_info(ctx
, prog
, num_tfeedback_decls
, tfeedback_decls
))
2934 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] != NULL
) {
2935 demote_shader_inputs_and_outputs(prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
2938 /* Eliminate code that is now dead due to unused vertex outputs being
2941 while (do_dead_code(prog
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
, false))
2945 if (prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
2946 gl_shader
*const sh
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
2948 demote_shader_inputs_and_outputs(sh
, ir_var_in
);
2949 demote_shader_inputs_and_outputs(sh
, ir_var_inout
);
2950 demote_shader_inputs_and_outputs(sh
, ir_var_out
);
2952 /* Eliminate code that is now dead due to unused geometry outputs being
2955 while (do_dead_code(prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->ir
, false))
2959 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] != NULL
) {
2960 gl_shader
*const sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
2962 demote_shader_inputs_and_outputs(sh
, ir_var_in
);
2964 /* Eliminate code that is now dead due to unused fragment inputs being
2965 * demoted. This shouldn't actually do anything other than remove
2966 * declarations of the (now unused) global variables.
2968 while (do_dead_code(prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
, false))
2972 update_array_sizes(prog
);
2973 link_assign_uniform_locations(prog
);
2974 store_fragdepth_layout(prog
);
2976 if (!check_resources(ctx
, prog
))
2979 /* OpenGL ES requires that a vertex shader and a fragment shader both be
2980 * present in a linked program. By checking prog->IsES, we also
2981 * catch the GL_ARB_ES2_compatibility case.
2983 if (!prog
->InternalSeparateShader
&&
2984 (ctx
->API
== API_OPENGLES2
|| prog
->IsES
)) {
2985 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
2986 linker_error(prog
, "program lacks a vertex shader\n");
2987 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
2988 linker_error(prog
, "program lacks a fragment shader\n");
2992 /* FINISHME: Assign fragment shader output locations. */
2995 free(vert_shader_list
);
2997 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2998 if (prog
->_LinkedShaders
[i
] == NULL
)
3001 /* Retain any live IR, but trash the rest. */
3002 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
3004 /* The symbol table in the linked shaders may contain references to
3005 * variables that were removed (e.g., unused uniforms). Since it may
3006 * contain junk, there is no possible valid use. Delete it and set the
3009 delete prog
->_LinkedShaders
[i
]->symbols
;
3010 prog
->_LinkedShaders
[i
]->symbols
= NULL
;
3013 ralloc_free(mem_ctx
);