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>
68 #include "util/strndup.h"
69 #include "main/core.h"
70 #include "glsl_symbol_table.h"
71 #include "glsl_parser_extras.h"
74 #include "program/hash_table.h"
76 #include "link_varyings.h"
77 #include "ir_optimization.h"
78 #include "ir_rvalue_visitor.h"
79 #include "ir_uniform.h"
81 #include "main/shaderobj.h"
82 #include "main/enums.h"
85 void linker_error(gl_shader_program
*, const char *, ...);
90 * Visitor that determines whether or not a variable is ever written.
92 class find_assignment_visitor
: public ir_hierarchical_visitor
{
94 find_assignment_visitor(const char *name
)
95 : name(name
), found(false)
100 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
)
102 ir_variable
*const var
= ir
->lhs
->variable_referenced();
104 if (strcmp(name
, var
->name
) == 0) {
109 return visit_continue_with_parent
;
112 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
114 foreach_two_lists(formal_node
, &ir
->callee
->parameters
,
115 actual_node
, &ir
->actual_parameters
) {
116 ir_rvalue
*param_rval
= (ir_rvalue
*) actual_node
;
117 ir_variable
*sig_param
= (ir_variable
*) formal_node
;
119 if (sig_param
->data
.mode
== ir_var_function_out
||
120 sig_param
->data
.mode
== ir_var_function_inout
) {
121 ir_variable
*var
= param_rval
->variable_referenced();
122 if (var
&& strcmp(name
, var
->name
) == 0) {
129 if (ir
->return_deref
!= NULL
) {
130 ir_variable
*const var
= ir
->return_deref
->variable_referenced();
132 if (strcmp(name
, var
->name
) == 0) {
138 return visit_continue_with_parent
;
141 bool variable_found()
147 const char *name
; /**< Find writes to a variable with this name. */
148 bool found
; /**< Was a write to the variable found? */
153 * Visitor that determines whether or not a variable is ever read.
155 class find_deref_visitor
: public ir_hierarchical_visitor
{
157 find_deref_visitor(const char *name
)
158 : name(name
), found(false)
163 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
165 if (strcmp(this->name
, ir
->var
->name
) == 0) {
170 return visit_continue
;
173 bool variable_found() const
179 const char *name
; /**< Find writes to a variable with this name. */
180 bool found
; /**< Was a write to the variable found? */
184 class geom_array_resize_visitor
: public ir_hierarchical_visitor
{
186 unsigned num_vertices
;
187 gl_shader_program
*prog
;
189 geom_array_resize_visitor(unsigned num_vertices
, gl_shader_program
*prog
)
191 this->num_vertices
= num_vertices
;
195 virtual ~geom_array_resize_visitor()
200 virtual ir_visitor_status
visit(ir_variable
*var
)
202 if (!var
->type
->is_array() || var
->data
.mode
!= ir_var_shader_in
)
203 return visit_continue
;
205 unsigned size
= var
->type
->length
;
207 /* Generate a link error if the shader has declared this array with an
210 if (size
&& size
!= this->num_vertices
) {
211 linker_error(this->prog
, "size of array %s declared as %u, "
212 "but number of input vertices is %u\n",
213 var
->name
, size
, this->num_vertices
);
214 return visit_continue
;
217 /* Generate a link error if the shader attempts to access an input
218 * array using an index too large for its actual size assigned at link
221 if (var
->data
.max_array_access
>= this->num_vertices
) {
222 linker_error(this->prog
, "geometry shader accesses element %i of "
223 "%s, but only %i input vertices\n",
224 var
->data
.max_array_access
, var
->name
, this->num_vertices
);
225 return visit_continue
;
228 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
230 var
->data
.max_array_access
= this->num_vertices
- 1;
232 return visit_continue
;
235 /* Dereferences of input variables need to be updated so that their type
236 * matches the newly assigned type of the variable they are accessing. */
237 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
239 ir
->type
= ir
->var
->type
;
240 return visit_continue
;
243 /* Dereferences of 2D input arrays need to be updated so that their type
244 * matches the newly assigned type of the array they are accessing. */
245 virtual ir_visitor_status
visit_leave(ir_dereference_array
*ir
)
247 const glsl_type
*const vt
= ir
->array
->type
;
249 ir
->type
= vt
->fields
.array
;
250 return visit_continue
;
254 class tess_eval_array_resize_visitor
: public ir_hierarchical_visitor
{
256 unsigned num_vertices
;
257 gl_shader_program
*prog
;
259 tess_eval_array_resize_visitor(unsigned num_vertices
, gl_shader_program
*prog
)
261 this->num_vertices
= num_vertices
;
265 virtual ~tess_eval_array_resize_visitor()
270 virtual ir_visitor_status
visit(ir_variable
*var
)
272 if (!var
->type
->is_array() || var
->data
.mode
!= ir_var_shader_in
|| var
->data
.patch
)
273 return visit_continue
;
275 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
277 var
->data
.max_array_access
= this->num_vertices
- 1;
279 return visit_continue
;
282 /* Dereferences of input variables need to be updated so that their type
283 * matches the newly assigned type of the variable they are accessing. */
284 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
286 ir
->type
= ir
->var
->type
;
287 return visit_continue
;
290 /* Dereferences of 2D input arrays need to be updated so that their type
291 * matches the newly assigned type of the array they are accessing. */
292 virtual ir_visitor_status
visit_leave(ir_dereference_array
*ir
)
294 const glsl_type
*const vt
= ir
->array
->type
;
296 ir
->type
= vt
->fields
.array
;
297 return visit_continue
;
301 class barrier_use_visitor
: public ir_hierarchical_visitor
{
303 barrier_use_visitor(gl_shader_program
*prog
)
304 : prog(prog
), in_main(false), after_return(false), control_flow(0)
308 virtual ~barrier_use_visitor()
313 virtual ir_visitor_status
visit_enter(ir_function
*ir
)
315 if (strcmp(ir
->name
, "main") == 0)
318 return visit_continue
;
321 virtual ir_visitor_status
visit_leave(ir_function
*)
324 after_return
= false;
325 return visit_continue
;
328 virtual ir_visitor_status
visit_leave(ir_return
*)
331 return visit_continue
;
334 virtual ir_visitor_status
visit_enter(ir_if
*)
337 return visit_continue
;
340 virtual ir_visitor_status
visit_leave(ir_if
*)
343 return visit_continue
;
346 virtual ir_visitor_status
visit_enter(ir_loop
*)
349 return visit_continue
;
352 virtual ir_visitor_status
visit_leave(ir_loop
*)
355 return visit_continue
;
358 /* FINISHME: `switch` is not expressed at the IR level -- it's already
359 * been lowered to a mess of `if`s. We'll correctly disallow any use of
360 * barrier() in a conditional path within the switch, but not in a path
361 * which is always hit.
364 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
366 if (ir
->use_builtin
&& strcmp(ir
->callee_name(), "barrier") == 0) {
367 /* Use of barrier(); determine if it is legal: */
369 linker_error(prog
, "Builtin barrier() may only be used in main");
374 linker_error(prog
, "Builtin barrier() may not be used after return");
378 if (control_flow
!= 0) {
379 linker_error(prog
, "Builtin barrier() may not be used inside control flow");
383 return visit_continue
;
387 gl_shader_program
*prog
;
388 bool in_main
, after_return
;
393 * Visitor that determines the highest stream id to which a (geometry) shader
394 * emits vertices. It also checks whether End{Stream}Primitive is ever called.
396 class find_emit_vertex_visitor
: public ir_hierarchical_visitor
{
398 find_emit_vertex_visitor(int max_allowed
)
399 : max_stream_allowed(max_allowed
),
400 invalid_stream_id(0),
401 invalid_stream_id_from_emit_vertex(false),
402 end_primitive_found(false),
403 uses_non_zero_stream(false)
408 virtual ir_visitor_status
visit_leave(ir_emit_vertex
*ir
)
410 int stream_id
= ir
->stream_id();
413 invalid_stream_id
= stream_id
;
414 invalid_stream_id_from_emit_vertex
= true;
418 if (stream_id
> max_stream_allowed
) {
419 invalid_stream_id
= stream_id
;
420 invalid_stream_id_from_emit_vertex
= true;
425 uses_non_zero_stream
= true;
427 return visit_continue
;
430 virtual ir_visitor_status
visit_leave(ir_end_primitive
*ir
)
432 end_primitive_found
= true;
434 int stream_id
= ir
->stream_id();
437 invalid_stream_id
= stream_id
;
438 invalid_stream_id_from_emit_vertex
= false;
442 if (stream_id
> max_stream_allowed
) {
443 invalid_stream_id
= stream_id
;
444 invalid_stream_id_from_emit_vertex
= false;
449 uses_non_zero_stream
= true;
451 return visit_continue
;
456 return invalid_stream_id
!= 0;
459 const char *error_func()
461 return invalid_stream_id_from_emit_vertex
?
462 "EmitStreamVertex" : "EndStreamPrimitive";
467 return invalid_stream_id
;
472 return uses_non_zero_stream
;
475 bool uses_end_primitive()
477 return end_primitive_found
;
481 int max_stream_allowed
;
482 int invalid_stream_id
;
483 bool invalid_stream_id_from_emit_vertex
;
484 bool end_primitive_found
;
485 bool uses_non_zero_stream
;
488 /* Class that finds array derefs and check if indexes are dynamic. */
489 class dynamic_sampler_array_indexing_visitor
: public ir_hierarchical_visitor
492 dynamic_sampler_array_indexing_visitor() :
493 dynamic_sampler_array_indexing(false)
497 ir_visitor_status
visit_enter(ir_dereference_array
*ir
)
499 if (!ir
->variable_referenced())
500 return visit_continue
;
502 if (!ir
->variable_referenced()->type
->contains_sampler())
503 return visit_continue
;
505 if (!ir
->array_index
->constant_expression_value()) {
506 dynamic_sampler_array_indexing
= true;
509 return visit_continue
;
512 bool uses_dynamic_sampler_array_indexing()
514 return dynamic_sampler_array_indexing
;
518 bool dynamic_sampler_array_indexing
;
521 } /* anonymous namespace */
524 linker_error(gl_shader_program
*prog
, const char *fmt
, ...)
528 ralloc_strcat(&prog
->InfoLog
, "error: ");
530 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
533 prog
->LinkStatus
= false;
538 linker_warning(gl_shader_program
*prog
, const char *fmt
, ...)
542 ralloc_strcat(&prog
->InfoLog
, "warning: ");
544 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
551 * Given a string identifying a program resource, break it into a base name
552 * and an optional array index in square brackets.
554 * If an array index is present, \c out_base_name_end is set to point to the
555 * "[" that precedes the array index, and the array index itself is returned
558 * If no array index is present (or if the array index is negative or
559 * mal-formed), \c out_base_name_end, is set to point to the null terminator
560 * at the end of the input string, and -1 is returned.
562 * Only the final array index is parsed; if the string contains other array
563 * indices (or structure field accesses), they are left in the base name.
565 * No attempt is made to check that the base name is properly formed;
566 * typically the caller will look up the base name in a hash table, so
567 * ill-formed base names simply turn into hash table lookup failures.
570 parse_program_resource_name(const GLchar
*name
,
571 const GLchar
**out_base_name_end
)
573 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
575 * "When an integer array element or block instance number is part of
576 * the name string, it will be specified in decimal form without a "+"
577 * or "-" sign or any extra leading zeroes. Additionally, the name
578 * string will not include white space anywhere in the string."
581 const size_t len
= strlen(name
);
582 *out_base_name_end
= name
+ len
;
584 if (len
== 0 || name
[len
-1] != ']')
587 /* Walk backwards over the string looking for a non-digit character. This
588 * had better be the opening bracket for an array index.
590 * Initially, i specifies the location of the ']'. Since the string may
591 * contain only the ']' charcater, walk backwards very carefully.
594 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
597 if ((i
== 0) || name
[i
-1] != '[')
600 long array_index
= strtol(&name
[i
], NULL
, 10);
604 /* Check for leading zero */
605 if (name
[i
] == '0' && name
[i
+1] != ']')
608 *out_base_name_end
= name
+ (i
- 1);
614 link_invalidate_variable_locations(exec_list
*ir
)
616 foreach_in_list(ir_instruction
, node
, ir
) {
617 ir_variable
*const var
= node
->as_variable();
622 /* Only assign locations for variables that lack an explicit location.
623 * Explicit locations are set for all built-in variables, generic vertex
624 * shader inputs (via layout(location=...)), and generic fragment shader
625 * outputs (also via layout(location=...)).
627 if (!var
->data
.explicit_location
) {
628 var
->data
.location
= -1;
629 var
->data
.location_frac
= 0;
632 /* ir_variable::is_unmatched_generic_inout is used by the linker while
633 * connecting outputs from one stage to inputs of the next stage.
635 * There are two implicit assumptions here. First, we assume that any
636 * built-in variable (i.e., non-generic in or out) will have
637 * explicit_location set. Second, we assume that any generic in or out
638 * will not have explicit_location set.
640 * This second assumption will only be valid until
641 * GL_ARB_separate_shader_objects is supported. When that extension is
642 * implemented, this function will need some modifications.
644 if (!var
->data
.explicit_location
) {
645 var
->data
.is_unmatched_generic_inout
= 1;
647 var
->data
.is_unmatched_generic_inout
= 0;
654 * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
656 * Also check for errors based on incorrect usage of gl_ClipVertex and
659 * Return false if an error was reported.
662 analyze_clip_usage(struct gl_shader_program
*prog
,
663 struct gl_shader
*shader
, GLboolean
*UsesClipDistance
,
664 GLuint
*ClipDistanceArraySize
)
666 *ClipDistanceArraySize
= 0;
668 if (!prog
->IsES
&& prog
->Version
>= 130) {
669 /* From section 7.1 (Vertex Shader Special Variables) of the
672 * "It is an error for a shader to statically write both
673 * gl_ClipVertex and gl_ClipDistance."
675 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
676 * gl_ClipVertex nor gl_ClipDistance.
678 find_assignment_visitor
clip_vertex("gl_ClipVertex");
679 find_assignment_visitor
clip_distance("gl_ClipDistance");
681 clip_vertex
.run(shader
->ir
);
682 clip_distance
.run(shader
->ir
);
683 if (clip_vertex
.variable_found() && clip_distance
.variable_found()) {
684 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
685 "and `gl_ClipDistance'\n",
686 _mesa_shader_stage_to_string(shader
->Stage
));
689 *UsesClipDistance
= clip_distance
.variable_found();
690 ir_variable
*clip_distance_var
=
691 shader
->symbols
->get_variable("gl_ClipDistance");
692 if (clip_distance_var
)
693 *ClipDistanceArraySize
= clip_distance_var
->type
->length
;
695 *UsesClipDistance
= false;
701 * Verify that a vertex shader executable meets all semantic requirements.
703 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
706 * \param shader Vertex shader executable to be verified
709 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
710 struct gl_shader
*shader
)
715 /* From the GLSL 1.10 spec, page 48:
717 * "The variable gl_Position is available only in the vertex
718 * language and is intended for writing the homogeneous vertex
719 * position. All executions of a well-formed vertex shader
720 * executable must write a value into this variable. [...] The
721 * variable gl_Position is available only in the vertex
722 * language and is intended for writing the homogeneous vertex
723 * position. All executions of a well-formed vertex shader
724 * executable must write a value into this variable."
726 * while in GLSL 1.40 this text is changed to:
728 * "The variable gl_Position is available only in the vertex
729 * language and is intended for writing the homogeneous vertex
730 * position. It can be written at any time during shader
731 * execution. It may also be read back by a vertex shader
732 * after being written. This value will be used by primitive
733 * assembly, clipping, culling, and other fixed functionality
734 * operations, if present, that operate on primitives after
735 * vertex processing has occurred. Its value is undefined if
736 * the vertex shader executable does not write gl_Position."
738 * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
739 * gl_Position is not an error.
741 if (prog
->Version
< (prog
->IsES
? 300 : 140)) {
742 find_assignment_visitor
find("gl_Position");
743 find
.run(shader
->ir
);
744 if (!find
.variable_found()) {
747 "vertex shader does not write to `gl_Position'."
748 "It's value is undefined. \n");
751 "vertex shader does not write to `gl_Position'. \n");
757 analyze_clip_usage(prog
, shader
, &prog
->Vert
.UsesClipDistance
,
758 &prog
->Vert
.ClipDistanceArraySize
);
762 validate_tess_eval_shader_executable(struct gl_shader_program
*prog
,
763 struct gl_shader
*shader
)
768 analyze_clip_usage(prog
, shader
, &prog
->TessEval
.UsesClipDistance
,
769 &prog
->TessEval
.ClipDistanceArraySize
);
774 * Verify that a fragment shader executable meets all semantic requirements
776 * \param shader Fragment shader executable to be verified
779 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
780 struct gl_shader
*shader
)
785 find_assignment_visitor
frag_color("gl_FragColor");
786 find_assignment_visitor
frag_data("gl_FragData");
788 frag_color
.run(shader
->ir
);
789 frag_data
.run(shader
->ir
);
791 if (frag_color
.variable_found() && frag_data
.variable_found()) {
792 linker_error(prog
, "fragment shader writes to both "
793 "`gl_FragColor' and `gl_FragData'\n");
798 * Verify that a geometry shader executable meets all semantic requirements
800 * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
801 * prog->Geom.ClipDistanceArraySize as a side effect.
803 * \param shader Geometry shader executable to be verified
806 validate_geometry_shader_executable(struct gl_shader_program
*prog
,
807 struct gl_shader
*shader
)
812 unsigned num_vertices
= vertices_per_prim(prog
->Geom
.InputType
);
813 prog
->Geom
.VerticesIn
= num_vertices
;
815 analyze_clip_usage(prog
, shader
, &prog
->Geom
.UsesClipDistance
,
816 &prog
->Geom
.ClipDistanceArraySize
);
820 * Check if geometry shaders emit to non-zero streams and do corresponding
824 validate_geometry_shader_emissions(struct gl_context
*ctx
,
825 struct gl_shader_program
*prog
)
827 if (prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
828 find_emit_vertex_visitor
emit_vertex(ctx
->Const
.MaxVertexStreams
- 1);
829 emit_vertex
.run(prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->ir
);
830 if (emit_vertex
.error()) {
831 linker_error(prog
, "Invalid call %s(%d). Accepted values for the "
832 "stream parameter are in the range [0, %d].\n",
833 emit_vertex
.error_func(),
834 emit_vertex
.error_stream(),
835 ctx
->Const
.MaxVertexStreams
- 1);
837 prog
->Geom
.UsesStreams
= emit_vertex
.uses_streams();
838 prog
->Geom
.UsesEndPrimitive
= emit_vertex
.uses_end_primitive();
840 /* From the ARB_gpu_shader5 spec:
842 * "Multiple vertex streams are supported only if the output primitive
843 * type is declared to be "points". A program will fail to link if it
844 * contains a geometry shader calling EmitStreamVertex() or
845 * EndStreamPrimitive() if its output primitive type is not "points".
847 * However, in the same spec:
849 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
850 * with <stream> set to zero."
854 * "The function EndPrimitive() is equivalent to calling
855 * EndStreamPrimitive() with <stream> set to zero."
857 * Since we can call EmitVertex() and EndPrimitive() when we output
858 * primitives other than points, calling EmitStreamVertex(0) or
859 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
860 * does. Currently we only set prog->Geom.UsesStreams to TRUE when
861 * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
864 if (prog
->Geom
.UsesStreams
&& prog
->Geom
.OutputType
!= GL_POINTS
) {
865 linker_error(prog
, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
866 "with n>0 requires point output\n");
872 validate_intrastage_arrays(struct gl_shader_program
*prog
,
873 ir_variable
*const var
,
874 ir_variable
*const existing
)
876 /* Consider the types to be "the same" if both types are arrays
877 * of the same type and one of the arrays is implicitly sized.
878 * In addition, set the type of the linked variable to the
879 * explicitly sized array.
881 if (var
->type
->is_array() && existing
->type
->is_array()) {
882 if ((var
->type
->fields
.array
== existing
->type
->fields
.array
) &&
883 ((var
->type
->length
== 0)|| (existing
->type
->length
== 0))) {
884 if (var
->type
->length
!= 0) {
885 if (var
->type
->length
<= existing
->data
.max_array_access
) {
886 linker_error(prog
, "%s `%s' declared as type "
887 "`%s' but outermost dimension has an index"
890 var
->name
, var
->type
->name
,
891 existing
->data
.max_array_access
);
893 existing
->type
= var
->type
;
895 } else if (existing
->type
->length
!= 0) {
896 if(existing
->type
->length
<= var
->data
.max_array_access
&&
897 !existing
->data
.from_ssbo_unsized_array
) {
898 linker_error(prog
, "%s `%s' declared as type "
899 "`%s' but outermost dimension has an index"
902 var
->name
, existing
->type
->name
,
903 var
->data
.max_array_access
);
908 /* The arrays of structs could have different glsl_type pointers but
909 * they are actually the same type. Use record_compare() to check that.
911 if (existing
->type
->fields
.array
->is_record() &&
912 var
->type
->fields
.array
->is_record() &&
913 existing
->type
->fields
.array
->record_compare(var
->type
->fields
.array
))
922 * Perform validation of global variables used across multiple shaders
925 cross_validate_globals(struct gl_shader_program
*prog
,
926 struct gl_shader
**shader_list
,
927 unsigned num_shaders
,
930 /* Examine all of the uniforms in all of the shaders and cross validate
933 glsl_symbol_table variables
;
934 for (unsigned i
= 0; i
< num_shaders
; i
++) {
935 if (shader_list
[i
] == NULL
)
938 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
939 ir_variable
*const var
= node
->as_variable();
944 if (uniforms_only
&& (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
))
947 /* don't cross validate subroutine uniforms */
948 if (var
->type
->contains_subroutine())
951 /* Don't cross validate temporaries that are at global scope. These
952 * will eventually get pulled into the shaders 'main'.
954 if (var
->data
.mode
== ir_var_temporary
)
957 /* If a global with this name has already been seen, verify that the
958 * new instance has the same type. In addition, if the globals have
959 * initializers, the values of the initializers must be the same.
961 ir_variable
*const existing
= variables
.get_variable(var
->name
);
962 if (existing
!= NULL
) {
963 /* Check if types match. Interface blocks have some special
964 * rules so we handle those elsewhere.
966 if (var
->type
!= existing
->type
&&
967 !var
->is_interface_instance()) {
968 if (!validate_intrastage_arrays(prog
, var
, existing
)) {
969 if (var
->type
->is_record() && existing
->type
->is_record()
970 && existing
->type
->record_compare(var
->type
)) {
971 existing
->type
= var
->type
;
973 /* If it is an unsized array in a Shader Storage Block,
974 * two different shaders can access to different elements.
975 * Because of that, they might be converted to different
976 * sized arrays, then check that they are compatible but
977 * ignore the array size.
979 if (!(var
->data
.mode
== ir_var_shader_storage
&&
980 var
->data
.from_ssbo_unsized_array
&&
981 existing
->data
.mode
== ir_var_shader_storage
&&
982 existing
->data
.from_ssbo_unsized_array
&&
983 var
->type
->gl_type
== existing
->type
->gl_type
)) {
984 linker_error(prog
, "%s `%s' declared as type "
985 "`%s' and type `%s'\n",
987 var
->name
, var
->type
->name
,
988 existing
->type
->name
);
995 if (var
->data
.explicit_location
) {
996 if (existing
->data
.explicit_location
997 && (var
->data
.location
!= existing
->data
.location
)) {
998 linker_error(prog
, "explicit locations for %s "
999 "`%s' have differing values\n",
1000 mode_string(var
), var
->name
);
1004 existing
->data
.location
= var
->data
.location
;
1005 existing
->data
.explicit_location
= true;
1008 /* From the GLSL 4.20 specification:
1009 * "A link error will result if two compilation units in a program
1010 * specify different integer-constant bindings for the same
1011 * opaque-uniform name. However, it is not an error to specify a
1012 * binding on some but not all declarations for the same name"
1014 if (var
->data
.explicit_binding
) {
1015 if (existing
->data
.explicit_binding
&&
1016 var
->data
.binding
!= existing
->data
.binding
) {
1017 linker_error(prog
, "explicit bindings for %s "
1018 "`%s' have differing values\n",
1019 mode_string(var
), var
->name
);
1023 existing
->data
.binding
= var
->data
.binding
;
1024 existing
->data
.explicit_binding
= true;
1027 if (var
->type
->contains_atomic() &&
1028 var
->data
.atomic
.offset
!= existing
->data
.atomic
.offset
) {
1029 linker_error(prog
, "offset specifications for %s "
1030 "`%s' have differing values\n",
1031 mode_string(var
), var
->name
);
1035 /* Validate layout qualifiers for gl_FragDepth.
1037 * From the AMD/ARB_conservative_depth specs:
1039 * "If gl_FragDepth is redeclared in any fragment shader in a
1040 * program, it must be redeclared in all fragment shaders in
1041 * that program that have static assignments to
1042 * gl_FragDepth. All redeclarations of gl_FragDepth in all
1043 * fragment shaders in a single program must have the same set
1046 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
1047 bool layout_declared
= var
->data
.depth_layout
!= ir_depth_layout_none
;
1048 bool layout_differs
=
1049 var
->data
.depth_layout
!= existing
->data
.depth_layout
;
1051 if (layout_declared
&& layout_differs
) {
1053 "All redeclarations of gl_FragDepth in all "
1054 "fragment shaders in a single program must have "
1055 "the same set of qualifiers.\n");
1058 if (var
->data
.used
&& layout_differs
) {
1060 "If gl_FragDepth is redeclared with a layout "
1061 "qualifier in any fragment shader, it must be "
1062 "redeclared with the same layout qualifier in "
1063 "all fragment shaders that have assignments to "
1068 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1070 * "If a shared global has multiple initializers, the
1071 * initializers must all be constant expressions, and they
1072 * must all have the same value. Otherwise, a link error will
1073 * result. (A shared global having only one initializer does
1074 * not require that initializer to be a constant expression.)"
1076 * Previous to 4.20 the GLSL spec simply said that initializers
1077 * must have the same value. In this case of non-constant
1078 * initializers, this was impossible to determine. As a result,
1079 * no vendor actually implemented that behavior. The 4.20
1080 * behavior matches the implemented behavior of at least one other
1081 * vendor, so we'll implement that for all GLSL versions.
1083 if (var
->constant_initializer
!= NULL
) {
1084 if (existing
->constant_initializer
!= NULL
) {
1085 if (!var
->constant_initializer
->has_value(existing
->constant_initializer
)) {
1086 linker_error(prog
, "initializers for %s "
1087 "`%s' have differing values\n",
1088 mode_string(var
), var
->name
);
1092 /* If the first-seen instance of a particular uniform did not
1093 * have an initializer but a later instance does, copy the
1094 * initializer to the version stored in the symbol table.
1096 /* FINISHME: This is wrong. The constant_value field should
1097 * FINISHME: not be modified! Imagine a case where a shader
1098 * FINISHME: without an initializer is linked in two different
1099 * FINISHME: programs with shaders that have differing
1100 * FINISHME: initializers. Linking with the first will
1101 * FINISHME: modify the shader, and linking with the second
1102 * FINISHME: will fail.
1104 existing
->constant_initializer
=
1105 var
->constant_initializer
->clone(ralloc_parent(existing
),
1110 if (var
->data
.has_initializer
) {
1111 if (existing
->data
.has_initializer
1112 && (var
->constant_initializer
== NULL
1113 || existing
->constant_initializer
== NULL
)) {
1115 "shared global variable `%s' has multiple "
1116 "non-constant initializers.\n",
1121 /* Some instance had an initializer, so keep track of that. In
1122 * this location, all sorts of initializers (constant or
1123 * otherwise) will propagate the existence to the variable
1124 * stored in the symbol table.
1126 existing
->data
.has_initializer
= true;
1129 if (existing
->data
.invariant
!= var
->data
.invariant
) {
1130 linker_error(prog
, "declarations for %s `%s' have "
1131 "mismatching invariant qualifiers\n",
1132 mode_string(var
), var
->name
);
1135 if (existing
->data
.centroid
!= var
->data
.centroid
) {
1136 linker_error(prog
, "declarations for %s `%s' have "
1137 "mismatching centroid qualifiers\n",
1138 mode_string(var
), var
->name
);
1141 if (existing
->data
.sample
!= var
->data
.sample
) {
1142 linker_error(prog
, "declarations for %s `%s` have "
1143 "mismatching sample qualifiers\n",
1144 mode_string(var
), var
->name
);
1148 variables
.add_variable(var
);
1155 * Perform validation of uniforms used across multiple shader stages
1158 cross_validate_uniforms(struct gl_shader_program
*prog
)
1160 cross_validate_globals(prog
, prog
->_LinkedShaders
,
1161 MESA_SHADER_STAGES
, true);
1165 * Accumulates the array of prog->BufferInterfaceBlocks and checks that all
1166 * definitons of blocks agree on their contents.
1169 interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
)
1171 unsigned max_num_uniform_blocks
= 0;
1172 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1173 if (prog
->_LinkedShaders
[i
])
1174 max_num_uniform_blocks
+= prog
->_LinkedShaders
[i
]->NumBufferInterfaceBlocks
;
1177 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1178 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
1180 prog
->UniformBlockStageIndex
[i
] = ralloc_array(prog
, int,
1181 max_num_uniform_blocks
);
1182 for (unsigned int j
= 0; j
< max_num_uniform_blocks
; j
++)
1183 prog
->UniformBlockStageIndex
[i
][j
] = -1;
1188 for (unsigned int j
= 0; j
< sh
->NumBufferInterfaceBlocks
; j
++) {
1189 int index
= link_cross_validate_uniform_block(prog
,
1190 &prog
->BufferInterfaceBlocks
,
1191 &prog
->NumBufferInterfaceBlocks
,
1192 &sh
->BufferInterfaceBlocks
[j
]);
1195 linker_error(prog
, "uniform block `%s' has mismatching definitions\n",
1196 sh
->BufferInterfaceBlocks
[j
].Name
);
1200 prog
->UniformBlockStageIndex
[i
][index
] = j
;
1209 * Populates a shaders symbol table with all global declarations
1212 populate_symbol_table(gl_shader
*sh
)
1214 sh
->symbols
= new(sh
) glsl_symbol_table
;
1216 foreach_in_list(ir_instruction
, inst
, sh
->ir
) {
1220 if ((func
= inst
->as_function()) != NULL
) {
1221 sh
->symbols
->add_function(func
);
1222 } else if ((var
= inst
->as_variable()) != NULL
) {
1223 if (var
->data
.mode
!= ir_var_temporary
)
1224 sh
->symbols
->add_variable(var
);
1231 * Remap variables referenced in an instruction tree
1233 * This is used when instruction trees are cloned from one shader and placed in
1234 * another. These trees will contain references to \c ir_variable nodes that
1235 * do not exist in the target shader. This function finds these \c ir_variable
1236 * references and replaces the references with matching variables in the target
1239 * If there is no matching variable in the target shader, a clone of the
1240 * \c ir_variable is made and added to the target shader. The new variable is
1241 * added to \b both the instruction stream and the symbol table.
1243 * \param inst IR tree that is to be processed.
1244 * \param symbols Symbol table containing global scope symbols in the
1246 * \param instructions Instruction stream where new variable declarations
1250 remap_variables(ir_instruction
*inst
, struct gl_shader
*target
,
1253 class remap_visitor
: public ir_hierarchical_visitor
{
1255 remap_visitor(struct gl_shader
*target
,
1258 this->target
= target
;
1259 this->symbols
= target
->symbols
;
1260 this->instructions
= target
->ir
;
1261 this->temps
= temps
;
1264 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
1266 if (ir
->var
->data
.mode
== ir_var_temporary
) {
1267 ir_variable
*var
= (ir_variable
*) hash_table_find(temps
, ir
->var
);
1269 assert(var
!= NULL
);
1271 return visit_continue
;
1274 ir_variable
*const existing
=
1275 this->symbols
->get_variable(ir
->var
->name
);
1276 if (existing
!= NULL
)
1279 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
1281 this->symbols
->add_variable(copy
);
1282 this->instructions
->push_head(copy
);
1286 return visit_continue
;
1290 struct gl_shader
*target
;
1291 glsl_symbol_table
*symbols
;
1292 exec_list
*instructions
;
1296 remap_visitor
v(target
, temps
);
1303 * Move non-declarations from one instruction stream to another
1305 * The intended usage pattern of this function is to pass the pointer to the
1306 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
1307 * pointer) for \c last and \c false for \c make_copies on the first
1308 * call. Successive calls pass the return value of the previous call for
1309 * \c last and \c true for \c make_copies.
1311 * \param instructions Source instruction stream
1312 * \param last Instruction after which new instructions should be
1313 * inserted in the target instruction stream
1314 * \param make_copies Flag selecting whether instructions in \c instructions
1315 * should be copied (via \c ir_instruction::clone) into the
1316 * target list or moved.
1319 * The new "last" instruction in the target instruction stream. This pointer
1320 * is suitable for use as the \c last parameter of a later call to this
1324 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
1325 bool make_copies
, gl_shader
*target
)
1327 hash_table
*temps
= NULL
;
1330 temps
= hash_table_ctor(0, hash_table_pointer_hash
,
1331 hash_table_pointer_compare
);
1333 foreach_in_list_safe(ir_instruction
, inst
, instructions
) {
1334 if (inst
->as_function())
1337 ir_variable
*var
= inst
->as_variable();
1338 if ((var
!= NULL
) && (var
->data
.mode
!= ir_var_temporary
))
1341 assert(inst
->as_assignment()
1343 || inst
->as_if() /* for initializers with the ?: operator */
1344 || ((var
!= NULL
) && (var
->data
.mode
== ir_var_temporary
)));
1347 inst
= inst
->clone(target
, NULL
);
1350 hash_table_insert(temps
, inst
, var
);
1352 remap_variables(inst
, target
, temps
);
1357 last
->insert_after(inst
);
1362 hash_table_dtor(temps
);
1369 * This class is only used in link_intrastage_shaders() below but declaring
1370 * it inside that function leads to compiler warnings with some versions of
1373 class array_sizing_visitor
: public ir_hierarchical_visitor
{
1375 array_sizing_visitor()
1376 : mem_ctx(ralloc_context(NULL
)),
1377 unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash
,
1378 hash_table_pointer_compare
))
1382 ~array_sizing_visitor()
1384 hash_table_dtor(this->unnamed_interfaces
);
1385 ralloc_free(this->mem_ctx
);
1388 virtual ir_visitor_status
visit(ir_variable
*var
)
1390 const glsl_type
*type_without_array
;
1391 fixup_type(&var
->type
, var
->data
.max_array_access
,
1392 var
->data
.from_ssbo_unsized_array
);
1393 type_without_array
= var
->type
->without_array();
1394 if (var
->type
->is_interface()) {
1395 if (interface_contains_unsized_arrays(var
->type
)) {
1396 const glsl_type
*new_type
=
1397 resize_interface_members(var
->type
,
1398 var
->get_max_ifc_array_access(),
1399 var
->is_in_shader_storage_block());
1400 var
->type
= new_type
;
1401 var
->change_interface_type(new_type
);
1403 } else if (type_without_array
->is_interface()) {
1404 if (interface_contains_unsized_arrays(type_without_array
)) {
1405 const glsl_type
*new_type
=
1406 resize_interface_members(type_without_array
,
1407 var
->get_max_ifc_array_access(),
1408 var
->is_in_shader_storage_block());
1409 var
->change_interface_type(new_type
);
1410 var
->type
= update_interface_members_array(var
->type
, new_type
);
1412 } else if (const glsl_type
*ifc_type
= var
->get_interface_type()) {
1413 /* Store a pointer to the variable in the unnamed_interfaces
1416 ir_variable
**interface_vars
= (ir_variable
**)
1417 hash_table_find(this->unnamed_interfaces
, ifc_type
);
1418 if (interface_vars
== NULL
) {
1419 interface_vars
= rzalloc_array(mem_ctx
, ir_variable
*,
1421 hash_table_insert(this->unnamed_interfaces
, interface_vars
,
1424 unsigned index
= ifc_type
->field_index(var
->name
);
1425 assert(index
< ifc_type
->length
);
1426 assert(interface_vars
[index
] == NULL
);
1427 interface_vars
[index
] = var
;
1429 return visit_continue
;
1433 * For each unnamed interface block that was discovered while running the
1434 * visitor, adjust the interface type to reflect the newly assigned array
1435 * sizes, and fix up the ir_variable nodes to point to the new interface
1438 void fixup_unnamed_interface_types()
1440 hash_table_call_foreach(this->unnamed_interfaces
,
1441 fixup_unnamed_interface_type
, NULL
);
1446 * If the type pointed to by \c type represents an unsized array, replace
1447 * it with a sized array whose size is determined by max_array_access.
1449 static void fixup_type(const glsl_type
**type
, unsigned max_array_access
,
1450 bool from_ssbo_unsized_array
)
1452 if (!from_ssbo_unsized_array
&& (*type
)->is_unsized_array()) {
1453 *type
= glsl_type::get_array_instance((*type
)->fields
.array
,
1454 max_array_access
+ 1);
1455 assert(*type
!= NULL
);
1459 static const glsl_type
*
1460 update_interface_members_array(const glsl_type
*type
,
1461 const glsl_type
*new_interface_type
)
1463 const glsl_type
*element_type
= type
->fields
.array
;
1464 if (element_type
->is_array()) {
1465 const glsl_type
*new_array_type
=
1466 update_interface_members_array(element_type
, new_interface_type
);
1467 return glsl_type::get_array_instance(new_array_type
, type
->length
);
1469 return glsl_type::get_array_instance(new_interface_type
,
1475 * Determine whether the given interface type contains unsized arrays (if
1476 * it doesn't, array_sizing_visitor doesn't need to process it).
1478 static bool interface_contains_unsized_arrays(const glsl_type
*type
)
1480 for (unsigned i
= 0; i
< type
->length
; i
++) {
1481 const glsl_type
*elem_type
= type
->fields
.structure
[i
].type
;
1482 if (elem_type
->is_unsized_array())
1489 * Create a new interface type based on the given type, with unsized arrays
1490 * replaced by sized arrays whose size is determined by
1491 * max_ifc_array_access.
1493 static const glsl_type
*
1494 resize_interface_members(const glsl_type
*type
,
1495 const unsigned *max_ifc_array_access
,
1498 unsigned num_fields
= type
->length
;
1499 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1500 memcpy(fields
, type
->fields
.structure
,
1501 num_fields
* sizeof(*fields
));
1502 for (unsigned i
= 0; i
< num_fields
; i
++) {
1503 /* If SSBO last member is unsized array, we don't replace it by a sized
1506 if (is_ssbo
&& i
== (num_fields
- 1))
1507 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1510 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1513 glsl_interface_packing packing
=
1514 (glsl_interface_packing
) type
->interface_packing
;
1515 const glsl_type
*new_ifc_type
=
1516 glsl_type::get_interface_instance(fields
, num_fields
,
1517 packing
, type
->name
);
1519 return new_ifc_type
;
1522 static void fixup_unnamed_interface_type(const void *key
, void *data
,
1525 const glsl_type
*ifc_type
= (const glsl_type
*) key
;
1526 ir_variable
**interface_vars
= (ir_variable
**) data
;
1527 unsigned num_fields
= ifc_type
->length
;
1528 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1529 memcpy(fields
, ifc_type
->fields
.structure
,
1530 num_fields
* sizeof(*fields
));
1531 bool interface_type_changed
= false;
1532 for (unsigned i
= 0; i
< num_fields
; i
++) {
1533 if (interface_vars
[i
] != NULL
&&
1534 fields
[i
].type
!= interface_vars
[i
]->type
) {
1535 fields
[i
].type
= interface_vars
[i
]->type
;
1536 interface_type_changed
= true;
1539 if (!interface_type_changed
) {
1543 glsl_interface_packing packing
=
1544 (glsl_interface_packing
) ifc_type
->interface_packing
;
1545 const glsl_type
*new_ifc_type
=
1546 glsl_type::get_interface_instance(fields
, num_fields
, packing
,
1549 for (unsigned i
= 0; i
< num_fields
; i
++) {
1550 if (interface_vars
[i
] != NULL
)
1551 interface_vars
[i
]->change_interface_type(new_ifc_type
);
1556 * Memory context used to allocate the data in \c unnamed_interfaces.
1561 * Hash table from const glsl_type * to an array of ir_variable *'s
1562 * pointing to the ir_variables constituting each unnamed interface block.
1564 hash_table
*unnamed_interfaces
;
1569 * Performs the cross-validation of tessellation control shader vertices and
1570 * layout qualifiers for the attached tessellation control shaders,
1571 * and propagates them to the linked TCS and linked shader program.
1574 link_tcs_out_layout_qualifiers(struct gl_shader_program
*prog
,
1575 struct gl_shader
*linked_shader
,
1576 struct gl_shader
**shader_list
,
1577 unsigned num_shaders
)
1579 linked_shader
->TessCtrl
.VerticesOut
= 0;
1581 if (linked_shader
->Stage
!= MESA_SHADER_TESS_CTRL
)
1584 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1586 * "All tessellation control shader layout declarations in a program
1587 * must specify the same output patch vertex count. There must be at
1588 * least one layout qualifier specifying an output patch vertex count
1589 * in any program containing tessellation control shaders; however,
1590 * such a declaration is not required in all tessellation control
1594 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1595 struct gl_shader
*shader
= shader_list
[i
];
1597 if (shader
->TessCtrl
.VerticesOut
!= 0) {
1598 if (linked_shader
->TessCtrl
.VerticesOut
!= 0 &&
1599 linked_shader
->TessCtrl
.VerticesOut
!= shader
->TessCtrl
.VerticesOut
) {
1600 linker_error(prog
, "tessellation control shader defined with "
1601 "conflicting output vertex count (%d and %d)\n",
1602 linked_shader
->TessCtrl
.VerticesOut
,
1603 shader
->TessCtrl
.VerticesOut
);
1606 linked_shader
->TessCtrl
.VerticesOut
= shader
->TessCtrl
.VerticesOut
;
1610 /* Just do the intrastage -> interstage propagation right now,
1611 * since we already know we're in the right type of shader program
1614 if (linked_shader
->TessCtrl
.VerticesOut
== 0) {
1615 linker_error(prog
, "tessellation control shader didn't declare "
1616 "vertices out layout qualifier\n");
1619 prog
->TessCtrl
.VerticesOut
= linked_shader
->TessCtrl
.VerticesOut
;
1624 * Performs the cross-validation of tessellation evaluation shader
1625 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1626 * for the attached tessellation evaluation shaders, and propagates them
1627 * to the linked TES and linked shader program.
1630 link_tes_in_layout_qualifiers(struct gl_shader_program
*prog
,
1631 struct gl_shader
*linked_shader
,
1632 struct gl_shader
**shader_list
,
1633 unsigned num_shaders
)
1635 linked_shader
->TessEval
.PrimitiveMode
= PRIM_UNKNOWN
;
1636 linked_shader
->TessEval
.Spacing
= 0;
1637 linked_shader
->TessEval
.VertexOrder
= 0;
1638 linked_shader
->TessEval
.PointMode
= -1;
1640 if (linked_shader
->Stage
!= MESA_SHADER_TESS_EVAL
)
1643 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1645 * "At least one tessellation evaluation shader (compilation unit) in
1646 * a program must declare a primitive mode in its input layout.
1647 * Declaration vertex spacing, ordering, and point mode identifiers is
1648 * optional. It is not required that all tessellation evaluation
1649 * shaders in a program declare a primitive mode. If spacing or
1650 * vertex ordering declarations are omitted, the tessellation
1651 * primitive generator will use equal spacing or counter-clockwise
1652 * vertex ordering, respectively. If a point mode declaration is
1653 * omitted, the tessellation primitive generator will produce lines or
1654 * triangles according to the primitive mode."
1657 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1658 struct gl_shader
*shader
= shader_list
[i
];
1660 if (shader
->TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
) {
1661 if (linked_shader
->TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
&&
1662 linked_shader
->TessEval
.PrimitiveMode
!= shader
->TessEval
.PrimitiveMode
) {
1663 linker_error(prog
, "tessellation evaluation shader defined with "
1664 "conflicting input primitive modes.\n");
1667 linked_shader
->TessEval
.PrimitiveMode
= shader
->TessEval
.PrimitiveMode
;
1670 if (shader
->TessEval
.Spacing
!= 0) {
1671 if (linked_shader
->TessEval
.Spacing
!= 0 &&
1672 linked_shader
->TessEval
.Spacing
!= shader
->TessEval
.Spacing
) {
1673 linker_error(prog
, "tessellation evaluation shader defined with "
1674 "conflicting vertex spacing.\n");
1677 linked_shader
->TessEval
.Spacing
= shader
->TessEval
.Spacing
;
1680 if (shader
->TessEval
.VertexOrder
!= 0) {
1681 if (linked_shader
->TessEval
.VertexOrder
!= 0 &&
1682 linked_shader
->TessEval
.VertexOrder
!= shader
->TessEval
.VertexOrder
) {
1683 linker_error(prog
, "tessellation evaluation shader defined with "
1684 "conflicting ordering.\n");
1687 linked_shader
->TessEval
.VertexOrder
= shader
->TessEval
.VertexOrder
;
1690 if (shader
->TessEval
.PointMode
!= -1) {
1691 if (linked_shader
->TessEval
.PointMode
!= -1 &&
1692 linked_shader
->TessEval
.PointMode
!= shader
->TessEval
.PointMode
) {
1693 linker_error(prog
, "tessellation evaluation shader defined with "
1694 "conflicting point modes.\n");
1697 linked_shader
->TessEval
.PointMode
= shader
->TessEval
.PointMode
;
1702 /* Just do the intrastage -> interstage propagation right now,
1703 * since we already know we're in the right type of shader program
1706 if (linked_shader
->TessEval
.PrimitiveMode
== PRIM_UNKNOWN
) {
1708 "tessellation evaluation shader didn't declare input "
1709 "primitive modes.\n");
1712 prog
->TessEval
.PrimitiveMode
= linked_shader
->TessEval
.PrimitiveMode
;
1714 if (linked_shader
->TessEval
.Spacing
== 0)
1715 linked_shader
->TessEval
.Spacing
= GL_EQUAL
;
1716 prog
->TessEval
.Spacing
= linked_shader
->TessEval
.Spacing
;
1718 if (linked_shader
->TessEval
.VertexOrder
== 0)
1719 linked_shader
->TessEval
.VertexOrder
= GL_CCW
;
1720 prog
->TessEval
.VertexOrder
= linked_shader
->TessEval
.VertexOrder
;
1722 if (linked_shader
->TessEval
.PointMode
== -1)
1723 linked_shader
->TessEval
.PointMode
= GL_FALSE
;
1724 prog
->TessEval
.PointMode
= linked_shader
->TessEval
.PointMode
;
1729 * Performs the cross-validation of layout qualifiers specified in
1730 * redeclaration of gl_FragCoord for the attached fragment shaders,
1731 * and propagates them to the linked FS and linked shader program.
1734 link_fs_input_layout_qualifiers(struct gl_shader_program
*prog
,
1735 struct gl_shader
*linked_shader
,
1736 struct gl_shader
**shader_list
,
1737 unsigned num_shaders
)
1739 linked_shader
->redeclares_gl_fragcoord
= false;
1740 linked_shader
->uses_gl_fragcoord
= false;
1741 linked_shader
->origin_upper_left
= false;
1742 linked_shader
->pixel_center_integer
= false;
1744 if (linked_shader
->Stage
!= MESA_SHADER_FRAGMENT
||
1745 (prog
->Version
< 150 && !prog
->ARB_fragment_coord_conventions_enable
))
1748 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1749 struct gl_shader
*shader
= shader_list
[i
];
1750 /* From the GLSL 1.50 spec, page 39:
1752 * "If gl_FragCoord is redeclared in any fragment shader in a program,
1753 * it must be redeclared in all the fragment shaders in that program
1754 * that have a static use gl_FragCoord."
1756 if ((linked_shader
->redeclares_gl_fragcoord
1757 && !shader
->redeclares_gl_fragcoord
1758 && shader
->uses_gl_fragcoord
)
1759 || (shader
->redeclares_gl_fragcoord
1760 && !linked_shader
->redeclares_gl_fragcoord
1761 && linked_shader
->uses_gl_fragcoord
)) {
1762 linker_error(prog
, "fragment shader defined with conflicting "
1763 "layout qualifiers for gl_FragCoord\n");
1766 /* From the GLSL 1.50 spec, page 39:
1768 * "All redeclarations of gl_FragCoord in all fragment shaders in a
1769 * single program must have the same set of qualifiers."
1771 if (linked_shader
->redeclares_gl_fragcoord
&& shader
->redeclares_gl_fragcoord
1772 && (shader
->origin_upper_left
!= linked_shader
->origin_upper_left
1773 || shader
->pixel_center_integer
!= linked_shader
->pixel_center_integer
)) {
1774 linker_error(prog
, "fragment shader defined with conflicting "
1775 "layout qualifiers for gl_FragCoord\n");
1778 /* Update the linked shader state. Note that uses_gl_fragcoord should
1779 * accumulate the results. The other values should replace. If there
1780 * are multiple redeclarations, all the fields except uses_gl_fragcoord
1781 * are already known to be the same.
1783 if (shader
->redeclares_gl_fragcoord
|| shader
->uses_gl_fragcoord
) {
1784 linked_shader
->redeclares_gl_fragcoord
=
1785 shader
->redeclares_gl_fragcoord
;
1786 linked_shader
->uses_gl_fragcoord
= linked_shader
->uses_gl_fragcoord
1787 || shader
->uses_gl_fragcoord
;
1788 linked_shader
->origin_upper_left
= shader
->origin_upper_left
;
1789 linked_shader
->pixel_center_integer
= shader
->pixel_center_integer
;
1792 linked_shader
->EarlyFragmentTests
|= shader
->EarlyFragmentTests
;
1797 * Performs the cross-validation of geometry shader max_vertices and
1798 * primitive type layout qualifiers for the attached geometry shaders,
1799 * and propagates them to the linked GS and linked shader program.
1802 link_gs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
1803 struct gl_shader
*linked_shader
,
1804 struct gl_shader
**shader_list
,
1805 unsigned num_shaders
)
1807 linked_shader
->Geom
.VerticesOut
= 0;
1808 linked_shader
->Geom
.Invocations
= 0;
1809 linked_shader
->Geom
.InputType
= PRIM_UNKNOWN
;
1810 linked_shader
->Geom
.OutputType
= PRIM_UNKNOWN
;
1812 /* No in/out qualifiers defined for anything but GLSL 1.50+
1813 * geometry shaders so far.
1815 if (linked_shader
->Stage
!= MESA_SHADER_GEOMETRY
|| prog
->Version
< 150)
1818 /* From the GLSL 1.50 spec, page 46:
1820 * "All geometry shader output layout declarations in a program
1821 * must declare the same layout and same value for
1822 * max_vertices. There must be at least one geometry output
1823 * layout declaration somewhere in a program, but not all
1824 * geometry shaders (compilation units) are required to
1828 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1829 struct gl_shader
*shader
= shader_list
[i
];
1831 if (shader
->Geom
.InputType
!= PRIM_UNKNOWN
) {
1832 if (linked_shader
->Geom
.InputType
!= PRIM_UNKNOWN
&&
1833 linked_shader
->Geom
.InputType
!= shader
->Geom
.InputType
) {
1834 linker_error(prog
, "geometry shader defined with conflicting "
1838 linked_shader
->Geom
.InputType
= shader
->Geom
.InputType
;
1841 if (shader
->Geom
.OutputType
!= PRIM_UNKNOWN
) {
1842 if (linked_shader
->Geom
.OutputType
!= PRIM_UNKNOWN
&&
1843 linked_shader
->Geom
.OutputType
!= shader
->Geom
.OutputType
) {
1844 linker_error(prog
, "geometry shader defined with conflicting "
1848 linked_shader
->Geom
.OutputType
= shader
->Geom
.OutputType
;
1851 if (shader
->Geom
.VerticesOut
!= 0) {
1852 if (linked_shader
->Geom
.VerticesOut
!= 0 &&
1853 linked_shader
->Geom
.VerticesOut
!= shader
->Geom
.VerticesOut
) {
1854 linker_error(prog
, "geometry shader defined with conflicting "
1855 "output vertex count (%d and %d)\n",
1856 linked_shader
->Geom
.VerticesOut
,
1857 shader
->Geom
.VerticesOut
);
1860 linked_shader
->Geom
.VerticesOut
= shader
->Geom
.VerticesOut
;
1863 if (shader
->Geom
.Invocations
!= 0) {
1864 if (linked_shader
->Geom
.Invocations
!= 0 &&
1865 linked_shader
->Geom
.Invocations
!= shader
->Geom
.Invocations
) {
1866 linker_error(prog
, "geometry shader defined with conflicting "
1867 "invocation count (%d and %d)\n",
1868 linked_shader
->Geom
.Invocations
,
1869 shader
->Geom
.Invocations
);
1872 linked_shader
->Geom
.Invocations
= shader
->Geom
.Invocations
;
1876 /* Just do the intrastage -> interstage propagation right now,
1877 * since we already know we're in the right type of shader program
1880 if (linked_shader
->Geom
.InputType
== PRIM_UNKNOWN
) {
1882 "geometry shader didn't declare primitive input type\n");
1885 prog
->Geom
.InputType
= linked_shader
->Geom
.InputType
;
1887 if (linked_shader
->Geom
.OutputType
== PRIM_UNKNOWN
) {
1889 "geometry shader didn't declare primitive output type\n");
1892 prog
->Geom
.OutputType
= linked_shader
->Geom
.OutputType
;
1894 if (linked_shader
->Geom
.VerticesOut
== 0) {
1896 "geometry shader didn't declare max_vertices\n");
1899 prog
->Geom
.VerticesOut
= linked_shader
->Geom
.VerticesOut
;
1901 if (linked_shader
->Geom
.Invocations
== 0)
1902 linked_shader
->Geom
.Invocations
= 1;
1904 prog
->Geom
.Invocations
= linked_shader
->Geom
.Invocations
;
1909 * Perform cross-validation of compute shader local_size_{x,y,z} layout
1910 * qualifiers for the attached compute shaders, and propagate them to the
1911 * linked CS and linked shader program.
1914 link_cs_input_layout_qualifiers(struct gl_shader_program
*prog
,
1915 struct gl_shader
*linked_shader
,
1916 struct gl_shader
**shader_list
,
1917 unsigned num_shaders
)
1919 for (int i
= 0; i
< 3; i
++)
1920 linked_shader
->Comp
.LocalSize
[i
] = 0;
1922 /* This function is called for all shader stages, but it only has an effect
1923 * for compute shaders.
1925 if (linked_shader
->Stage
!= MESA_SHADER_COMPUTE
)
1928 /* From the ARB_compute_shader spec, in the section describing local size
1931 * If multiple compute shaders attached to a single program object
1932 * declare local work-group size, the declarations must be identical;
1933 * otherwise a link-time error results. Furthermore, if a program
1934 * object contains any compute shaders, at least one must contain an
1935 * input layout qualifier specifying the local work sizes of the
1936 * program, or a link-time error will occur.
1938 for (unsigned sh
= 0; sh
< num_shaders
; sh
++) {
1939 struct gl_shader
*shader
= shader_list
[sh
];
1941 if (shader
->Comp
.LocalSize
[0] != 0) {
1942 if (linked_shader
->Comp
.LocalSize
[0] != 0) {
1943 for (int i
= 0; i
< 3; i
++) {
1944 if (linked_shader
->Comp
.LocalSize
[i
] !=
1945 shader
->Comp
.LocalSize
[i
]) {
1946 linker_error(prog
, "compute shader defined with conflicting "
1952 for (int i
= 0; i
< 3; i
++)
1953 linked_shader
->Comp
.LocalSize
[i
] = shader
->Comp
.LocalSize
[i
];
1957 /* Just do the intrastage -> interstage propagation right now,
1958 * since we already know we're in the right type of shader program
1961 if (linked_shader
->Comp
.LocalSize
[0] == 0) {
1962 linker_error(prog
, "compute shader didn't declare local size\n");
1965 for (int i
= 0; i
< 3; i
++)
1966 prog
->Comp
.LocalSize
[i
] = linked_shader
->Comp
.LocalSize
[i
];
1971 * Combine a group of shaders for a single stage to generate a linked shader
1974 * If this function is supplied a single shader, it is cloned, and the new
1975 * shader is returned.
1977 static struct gl_shader
*
1978 link_intrastage_shaders(void *mem_ctx
,
1979 struct gl_context
*ctx
,
1980 struct gl_shader_program
*prog
,
1981 struct gl_shader
**shader_list
,
1982 unsigned num_shaders
)
1984 struct gl_uniform_block
*uniform_blocks
= NULL
;
1986 /* Check that global variables defined in multiple shaders are consistent.
1988 cross_validate_globals(prog
, shader_list
, num_shaders
, false);
1989 if (!prog
->LinkStatus
)
1992 /* Check that interface blocks defined in multiple shaders are consistent.
1994 validate_intrastage_interface_blocks(prog
, (const gl_shader
**)shader_list
,
1996 if (!prog
->LinkStatus
)
1999 /* Link up uniform blocks defined within this stage. */
2000 const unsigned num_uniform_blocks
=
2001 link_uniform_blocks(mem_ctx
, ctx
, prog
, shader_list
, num_shaders
,
2003 if (!prog
->LinkStatus
)
2006 /* Check that there is only a single definition of each function signature
2007 * across all shaders.
2009 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
2010 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
2011 ir_function
*const f
= node
->as_function();
2016 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
2017 ir_function
*const other
=
2018 shader_list
[j
]->symbols
->get_function(f
->name
);
2020 /* If the other shader has no function (and therefore no function
2021 * signatures) with the same name, skip to the next shader.
2026 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
2027 if (!sig
->is_defined
|| sig
->is_builtin())
2030 ir_function_signature
*other_sig
=
2031 other
->exact_matching_signature(NULL
, &sig
->parameters
);
2033 if ((other_sig
!= NULL
) && other_sig
->is_defined
2034 && !other_sig
->is_builtin()) {
2035 linker_error(prog
, "function `%s' is multiply defined\n",
2044 /* Find the shader that defines main, and make a clone of it.
2046 * Starting with the clone, search for undefined references. If one is
2047 * found, find the shader that defines it. Clone the reference and add
2048 * it to the shader. Repeat until there are no undefined references or
2049 * until a reference cannot be resolved.
2051 gl_shader
*main
= NULL
;
2052 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2053 if (_mesa_get_main_function_signature(shader_list
[i
]) != NULL
) {
2054 main
= shader_list
[i
];
2060 linker_error(prog
, "%s shader lacks `main'\n",
2061 _mesa_shader_stage_to_string(shader_list
[0]->Stage
));
2065 gl_shader
*linked
= ctx
->Driver
.NewShader(NULL
, 0, main
->Type
);
2066 linked
->ir
= new(linked
) exec_list
;
2067 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
2069 linked
->BufferInterfaceBlocks
= uniform_blocks
;
2070 linked
->NumBufferInterfaceBlocks
= num_uniform_blocks
;
2071 ralloc_steal(linked
, linked
->BufferInterfaceBlocks
);
2073 link_fs_input_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2074 link_tcs_out_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2075 link_tes_in_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2076 link_gs_inout_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2077 link_cs_input_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2079 populate_symbol_table(linked
);
2081 /* The pointer to the main function in the final linked shader (i.e., the
2082 * copy of the original shader that contained the main function).
2084 ir_function_signature
*const main_sig
=
2085 _mesa_get_main_function_signature(linked
);
2087 /* Move any instructions other than variable declarations or function
2088 * declarations into main.
2090 exec_node
*insertion_point
=
2091 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
2094 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2095 if (shader_list
[i
] == main
)
2098 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
2099 insertion_point
, true, linked
);
2102 /* Check if any shader needs built-in functions. */
2103 bool need_builtins
= false;
2104 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2105 if (shader_list
[i
]->uses_builtin_functions
) {
2106 need_builtins
= true;
2112 if (need_builtins
) {
2113 /* Make a temporary array one larger than shader_list, which will hold
2114 * the built-in function shader as well.
2116 gl_shader
**linking_shaders
= (gl_shader
**)
2117 calloc(num_shaders
+ 1, sizeof(gl_shader
*));
2119 ok
= linking_shaders
!= NULL
;
2122 memcpy(linking_shaders
, shader_list
, num_shaders
* sizeof(gl_shader
*));
2123 linking_shaders
[num_shaders
] = _mesa_glsl_get_builtin_function_shader();
2125 ok
= link_function_calls(prog
, linked
, linking_shaders
, num_shaders
+ 1);
2127 free(linking_shaders
);
2129 _mesa_error_no_memory(__func__
);
2132 ok
= link_function_calls(prog
, linked
, shader_list
, num_shaders
);
2137 _mesa_delete_shader(ctx
, linked
);
2141 /* At this point linked should contain all of the linked IR, so
2142 * validate it to make sure nothing went wrong.
2144 validate_ir_tree(linked
->ir
);
2146 /* Set the size of geometry shader input arrays */
2147 if (linked
->Stage
== MESA_SHADER_GEOMETRY
) {
2148 unsigned num_vertices
= vertices_per_prim(prog
->Geom
.InputType
);
2149 geom_array_resize_visitor
input_resize_visitor(num_vertices
, prog
);
2150 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2151 ir
->accept(&input_resize_visitor
);
2155 if (ctx
->Const
.VertexID_is_zero_based
)
2156 lower_vertex_id(linked
);
2158 /* Validate correct usage of barrier() in the tess control shader */
2159 if (linked
->Stage
== MESA_SHADER_TESS_CTRL
) {
2160 barrier_use_visitor
visitor(prog
);
2161 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2162 ir
->accept(&visitor
);
2166 /* Make a pass over all variable declarations to ensure that arrays with
2167 * unspecified sizes have a size specified. The size is inferred from the
2168 * max_array_access field.
2170 array_sizing_visitor v
;
2172 v
.fixup_unnamed_interface_types();
2178 * Update the sizes of linked shader uniform arrays to the maximum
2181 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
2183 * If one or more elements of an array are active,
2184 * GetActiveUniform will return the name of the array in name,
2185 * subject to the restrictions listed above. The type of the array
2186 * is returned in type. The size parameter contains the highest
2187 * array element index used, plus one. The compiler or linker
2188 * determines the highest index used. There will be only one
2189 * active uniform reported by the GL per uniform array.
2193 update_array_sizes(struct gl_shader_program
*prog
)
2195 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2196 if (prog
->_LinkedShaders
[i
] == NULL
)
2199 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
2200 ir_variable
*const var
= node
->as_variable();
2202 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
) ||
2203 !var
->type
->is_array())
2206 /* GL_ARB_uniform_buffer_object says that std140 uniforms
2207 * will not be eliminated. Since we always do std140, just
2208 * don't resize arrays in UBOs.
2210 * Atomic counters are supposed to get deterministic
2211 * locations assigned based on the declaration ordering and
2212 * sizes, array compaction would mess that up.
2214 * Subroutine uniforms are not removed.
2216 if (var
->is_in_buffer_block() || var
->type
->contains_atomic() ||
2217 var
->type
->contains_subroutine())
2220 unsigned int size
= var
->data
.max_array_access
;
2221 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
2222 if (prog
->_LinkedShaders
[j
] == NULL
)
2225 foreach_in_list(ir_instruction
, node2
, prog
->_LinkedShaders
[j
]->ir
) {
2226 ir_variable
*other_var
= node2
->as_variable();
2230 if (strcmp(var
->name
, other_var
->name
) == 0 &&
2231 other_var
->data
.max_array_access
> size
) {
2232 size
= other_var
->data
.max_array_access
;
2237 if (size
+ 1 != var
->type
->length
) {
2238 /* If this is a built-in uniform (i.e., it's backed by some
2239 * fixed-function state), adjust the number of state slots to
2240 * match the new array size. The number of slots per array entry
2241 * is not known. It seems safe to assume that the total number of
2242 * slots is an integer multiple of the number of array elements.
2243 * Determine the number of slots per array element by dividing by
2244 * the old (total) size.
2246 const unsigned num_slots
= var
->get_num_state_slots();
2247 if (num_slots
> 0) {
2248 var
->set_num_state_slots((size
+ 1)
2249 * (num_slots
/ var
->type
->length
));
2252 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2254 /* FINISHME: We should update the types of array
2255 * dereferences of this variable now.
2263 * Resize tessellation evaluation per-vertex inputs to the size of
2264 * tessellation control per-vertex outputs.
2267 resize_tes_inputs(struct gl_context
*ctx
,
2268 struct gl_shader_program
*prog
)
2270 if (prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] == NULL
)
2273 gl_shader
*const tcs
= prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
];
2274 gl_shader
*const tes
= prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
2276 /* If no control shader is present, then the TES inputs are statically
2277 * sized to MaxPatchVertices; the actual size of the arrays won't be
2278 * known until draw time.
2280 const int num_vertices
= tcs
2281 ? tcs
->TessCtrl
.VerticesOut
2282 : ctx
->Const
.MaxPatchVertices
;
2284 tess_eval_array_resize_visitor
input_resize_visitor(num_vertices
, prog
);
2285 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2286 ir
->accept(&input_resize_visitor
);
2291 * Find a contiguous set of available bits in a bitmask.
2293 * \param used_mask Bits representing used (1) and unused (0) locations
2294 * \param needed_count Number of contiguous bits needed.
2297 * Base location of the available bits on success or -1 on failure.
2300 find_available_slots(unsigned used_mask
, unsigned needed_count
)
2302 unsigned needed_mask
= (1 << needed_count
) - 1;
2303 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
2305 /* The comparison to 32 is redundant, but without it GCC emits "warning:
2306 * cannot optimize possibly infinite loops" for the loop below.
2308 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
2311 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
2312 if ((needed_mask
& ~used_mask
) == needed_mask
)
2323 * Assign locations for either VS inputs or FS outputs
2325 * \param prog Shader program whose variables need locations assigned
2326 * \param constants Driver specific constant values for the program.
2327 * \param target_index Selector for the program target to receive location
2328 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
2329 * \c MESA_SHADER_FRAGMENT.
2332 * If locations are successfully assigned, true is returned. Otherwise an
2333 * error is emitted to the shader link log and false is returned.
2336 assign_attribute_or_color_locations(gl_shader_program
*prog
,
2337 struct gl_constants
*constants
,
2338 unsigned target_index
)
2340 /* Maximum number of generic locations. This corresponds to either the
2341 * maximum number of draw buffers or the maximum number of generic
2344 unsigned max_index
= (target_index
== MESA_SHADER_VERTEX
) ?
2345 constants
->Program
[target_index
].MaxAttribs
:
2346 MAX2(constants
->MaxDrawBuffers
, constants
->MaxDualSourceDrawBuffers
);
2348 /* Mark invalid locations as being used.
2350 unsigned used_locations
= (max_index
>= 32)
2351 ? ~0 : ~((1 << max_index
) - 1);
2352 unsigned double_storage_locations
= 0;
2354 assert((target_index
== MESA_SHADER_VERTEX
)
2355 || (target_index
== MESA_SHADER_FRAGMENT
));
2357 gl_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
2361 /* Operate in a total of four passes.
2363 * 1. Invalidate the location assignments for all vertex shader inputs.
2365 * 2. Assign locations for inputs that have user-defined (via
2366 * glBindVertexAttribLocation) locations and outputs that have
2367 * user-defined locations (via glBindFragDataLocation).
2369 * 3. Sort the attributes without assigned locations by number of slots
2370 * required in decreasing order. Fragmentation caused by attribute
2371 * locations assigned by the application may prevent large attributes
2372 * from having enough contiguous space.
2374 * 4. Assign locations to any inputs without assigned locations.
2377 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
2378 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
2380 const enum ir_variable_mode direction
=
2381 (target_index
== MESA_SHADER_VERTEX
)
2382 ? ir_var_shader_in
: ir_var_shader_out
;
2385 /* Temporary storage for the set of attributes that need locations assigned.
2391 /* Used below in the call to qsort. */
2392 static int compare(const void *a
, const void *b
)
2394 const temp_attr
*const l
= (const temp_attr
*) a
;
2395 const temp_attr
*const r
= (const temp_attr
*) b
;
2397 /* Reversed because we want a descending order sort below. */
2398 return r
->slots
- l
->slots
;
2402 unsigned num_attr
= 0;
2404 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2405 ir_variable
*const var
= node
->as_variable();
2407 if ((var
== NULL
) || (var
->data
.mode
!= (unsigned) direction
))
2410 if (var
->data
.explicit_location
) {
2411 if ((var
->data
.location
>= (int)(max_index
+ generic_base
))
2412 || (var
->data
.location
< 0)) {
2414 "invalid explicit location %d specified for `%s'\n",
2415 (var
->data
.location
< 0)
2416 ? var
->data
.location
2417 : var
->data
.location
- generic_base
,
2421 } else if (target_index
== MESA_SHADER_VERTEX
) {
2424 if (prog
->AttributeBindings
->get(binding
, var
->name
)) {
2425 assert(binding
>= VERT_ATTRIB_GENERIC0
);
2426 var
->data
.location
= binding
;
2427 var
->data
.is_unmatched_generic_inout
= 0;
2429 } else if (target_index
== MESA_SHADER_FRAGMENT
) {
2433 if (prog
->FragDataBindings
->get(binding
, var
->name
)) {
2434 assert(binding
>= FRAG_RESULT_DATA0
);
2435 var
->data
.location
= binding
;
2436 var
->data
.is_unmatched_generic_inout
= 0;
2438 if (prog
->FragDataIndexBindings
->get(index
, var
->name
)) {
2439 var
->data
.index
= index
;
2444 /* From GL4.5 core spec, section 15.2 (Shader Execution):
2446 * "Output binding assignments will cause LinkProgram to fail:
2448 * If the program has an active output assigned to a location greater
2449 * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
2450 * an active output assigned an index greater than or equal to one;"
2452 if (target_index
== MESA_SHADER_FRAGMENT
&& var
->data
.index
>= 1 &&
2453 var
->data
.location
- generic_base
>=
2454 (int) constants
->MaxDualSourceDrawBuffers
) {
2456 "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
2457 "with index %u for %s\n",
2458 var
->data
.location
- generic_base
, var
->data
.index
,
2463 const unsigned slots
= var
->type
->count_attribute_slots();
2465 /* If the variable is not a built-in and has a location statically
2466 * assigned in the shader (presumably via a layout qualifier), make sure
2467 * that it doesn't collide with other assigned locations. Otherwise,
2468 * add it to the list of variables that need linker-assigned locations.
2470 if (var
->data
.location
!= -1) {
2471 if (var
->data
.location
>= generic_base
&& var
->data
.index
< 1) {
2472 /* From page 61 of the OpenGL 4.0 spec:
2474 * "LinkProgram will fail if the attribute bindings assigned
2475 * by BindAttribLocation do not leave not enough space to
2476 * assign a location for an active matrix attribute or an
2477 * active attribute array, both of which require multiple
2478 * contiguous generic attributes."
2480 * I think above text prohibits the aliasing of explicit and
2481 * automatic assignments. But, aliasing is allowed in manual
2482 * assignments of attribute locations. See below comments for
2485 * From OpenGL 4.0 spec, page 61:
2487 * "It is possible for an application to bind more than one
2488 * attribute name to the same location. This is referred to as
2489 * aliasing. This will only work if only one of the aliased
2490 * attributes is active in the executable program, or if no
2491 * path through the shader consumes more than one attribute of
2492 * a set of attributes aliased to the same location. A link
2493 * error can occur if the linker determines that every path
2494 * through the shader consumes multiple aliased attributes,
2495 * but implementations are not required to generate an error
2498 * From GLSL 4.30 spec, page 54:
2500 * "A program will fail to link if any two non-vertex shader
2501 * input variables are assigned to the same location. For
2502 * vertex shaders, multiple input variables may be assigned
2503 * to the same location using either layout qualifiers or via
2504 * the OpenGL API. However, such aliasing is intended only to
2505 * support vertex shaders where each execution path accesses
2506 * at most one input per each location. Implementations are
2507 * permitted, but not required, to generate link-time errors
2508 * if they detect that every path through the vertex shader
2509 * executable accesses multiple inputs assigned to any single
2510 * location. For all shader types, a program will fail to link
2511 * if explicit location assignments leave the linker unable
2512 * to find space for other variables without explicit
2515 * From OpenGL ES 3.0 spec, page 56:
2517 * "Binding more than one attribute name to the same location
2518 * is referred to as aliasing, and is not permitted in OpenGL
2519 * ES Shading Language 3.00 vertex shaders. LinkProgram will
2520 * fail when this condition exists. However, aliasing is
2521 * possible in OpenGL ES Shading Language 1.00 vertex shaders.
2522 * This will only work if only one of the aliased attributes
2523 * is active in the executable program, or if no path through
2524 * the shader consumes more than one attribute of a set of
2525 * attributes aliased to the same location. A link error can
2526 * occur if the linker determines that every path through the
2527 * shader consumes multiple aliased attributes, but implemen-
2528 * tations are not required to generate an error in this case."
2530 * After looking at above references from OpenGL, OpenGL ES and
2531 * GLSL specifications, we allow aliasing of vertex input variables
2532 * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
2534 * NOTE: This is not required by the spec but its worth mentioning
2535 * here that we're not doing anything to make sure that no path
2536 * through the vertex shader executable accesses multiple inputs
2537 * assigned to any single location.
2540 /* Mask representing the contiguous slots that will be used by
2543 const unsigned attr
= var
->data
.location
- generic_base
;
2544 const unsigned use_mask
= (1 << slots
) - 1;
2545 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
2546 ? "vertex shader input" : "fragment shader output";
2548 /* Generate a link error if the requested locations for this
2549 * attribute exceed the maximum allowed attribute location.
2551 if (attr
+ slots
> max_index
) {
2553 "insufficient contiguous locations "
2554 "available for %s `%s' %d %d %d\n", string
,
2555 var
->name
, used_locations
, use_mask
, attr
);
2559 /* Generate a link error if the set of bits requested for this
2560 * attribute overlaps any previously allocated bits.
2562 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
2563 if (target_index
== MESA_SHADER_FRAGMENT
||
2564 (prog
->IsES
&& prog
->Version
>= 300)) {
2566 "overlapping location is assigned "
2567 "to %s `%s' %d %d %d\n", string
,
2568 var
->name
, used_locations
, use_mask
, attr
);
2571 linker_warning(prog
,
2572 "overlapping location is assigned "
2573 "to %s `%s' %d %d %d\n", string
,
2574 var
->name
, used_locations
, use_mask
, attr
);
2578 used_locations
|= (use_mask
<< attr
);
2580 /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
2582 * "A program with more than the value of MAX_VERTEX_ATTRIBS
2583 * active attribute variables may fail to link, unless
2584 * device-dependent optimizations are able to make the program
2585 * fit within available hardware resources. For the purposes
2586 * of this test, attribute variables of the type dvec3, dvec4,
2587 * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
2588 * count as consuming twice as many attributes as equivalent
2589 * single-precision types. While these types use the same number
2590 * of generic attributes as their single-precision equivalents,
2591 * implementations are permitted to consume two single-precision
2592 * vectors of internal storage for each three- or four-component
2593 * double-precision vector."
2595 * Mark this attribute slot as taking up twice as much space
2596 * so we can count it properly against limits. According to
2597 * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
2598 * is optional behavior, but it seems preferable.
2600 const glsl_type
*type
= var
->type
->without_array();
2601 if (type
== glsl_type::dvec3_type
||
2602 type
== glsl_type::dvec4_type
||
2603 type
== glsl_type::dmat2x3_type
||
2604 type
== glsl_type::dmat2x4_type
||
2605 type
== glsl_type::dmat3_type
||
2606 type
== glsl_type::dmat3x4_type
||
2607 type
== glsl_type::dmat4x3_type
||
2608 type
== glsl_type::dmat4_type
) {
2609 double_storage_locations
|= (use_mask
<< attr
);
2616 to_assign
[num_attr
].slots
= slots
;
2617 to_assign
[num_attr
].var
= var
;
2621 if (target_index
== MESA_SHADER_VERTEX
) {
2622 unsigned total_attribs_size
=
2623 _mesa_bitcount(used_locations
& ((1 << max_index
) - 1)) +
2624 _mesa_bitcount(double_storage_locations
);
2625 if (total_attribs_size
> max_index
) {
2627 "attempt to use %d vertex attribute slots only %d available ",
2628 total_attribs_size
, max_index
);
2633 /* If all of the attributes were assigned locations by the application (or
2634 * are built-in attributes with fixed locations), return early. This should
2635 * be the common case.
2640 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
2642 if (target_index
== MESA_SHADER_VERTEX
) {
2643 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
2644 * only be explicitly assigned by via glBindAttribLocation. Mark it as
2645 * reserved to prevent it from being automatically allocated below.
2647 find_deref_visitor
find("gl_Vertex");
2649 if (find
.variable_found())
2650 used_locations
|= (1 << 0);
2653 for (unsigned i
= 0; i
< num_attr
; i
++) {
2654 /* Mask representing the contiguous slots that will be used by this
2657 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
2659 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
2662 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
2663 ? "vertex shader input" : "fragment shader output";
2666 "insufficient contiguous locations "
2667 "available for %s `%s'\n",
2668 string
, to_assign
[i
].var
->name
);
2672 to_assign
[i
].var
->data
.location
= generic_base
+ location
;
2673 to_assign
[i
].var
->data
.is_unmatched_generic_inout
= 0;
2674 used_locations
|= (use_mask
<< location
);
2682 * Demote shader inputs and outputs that are not used in other stages
2685 demote_shader_inputs_and_outputs(gl_shader
*sh
, enum ir_variable_mode mode
)
2687 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2688 ir_variable
*const var
= node
->as_variable();
2690 if ((var
== NULL
) || (var
->data
.mode
!= int(mode
)))
2693 /* A shader 'in' or 'out' variable is only really an input or output if
2694 * its value is used by other shader stages. This will cause the variable
2695 * to have a location assigned.
2697 if (var
->data
.is_unmatched_generic_inout
) {
2698 assert(var
->data
.mode
!= ir_var_temporary
);
2699 var
->data
.mode
= ir_var_auto
;
2706 * Store the gl_FragDepth layout in the gl_shader_program struct.
2709 store_fragdepth_layout(struct gl_shader_program
*prog
)
2711 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
2715 struct exec_list
*ir
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
2717 /* We don't look up the gl_FragDepth symbol directly because if
2718 * gl_FragDepth is not used in the shader, it's removed from the IR.
2719 * However, the symbol won't be removed from the symbol table.
2721 * We're only interested in the cases where the variable is NOT removed
2724 foreach_in_list(ir_instruction
, node
, ir
) {
2725 ir_variable
*const var
= node
->as_variable();
2727 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
) {
2731 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
2732 switch (var
->data
.depth_layout
) {
2733 case ir_depth_layout_none
:
2734 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
2736 case ir_depth_layout_any
:
2737 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
2739 case ir_depth_layout_greater
:
2740 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
2742 case ir_depth_layout_less
:
2743 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
2745 case ir_depth_layout_unchanged
:
2746 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2757 * Validate the resources used by a program versus the implementation limits
2760 check_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
2762 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2763 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2768 if (sh
->num_samplers
> ctx
->Const
.Program
[i
].MaxTextureImageUnits
) {
2769 linker_error(prog
, "Too many %s shader texture samplers\n",
2770 _mesa_shader_stage_to_string(i
));
2773 if (sh
->num_uniform_components
>
2774 ctx
->Const
.Program
[i
].MaxUniformComponents
) {
2775 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
2776 linker_warning(prog
, "Too many %s shader default uniform block "
2777 "components, but the driver will try to optimize "
2778 "them out; this is non-portable out-of-spec "
2780 _mesa_shader_stage_to_string(i
));
2782 linker_error(prog
, "Too many %s shader default uniform block "
2784 _mesa_shader_stage_to_string(i
));
2788 if (sh
->num_combined_uniform_components
>
2789 ctx
->Const
.Program
[i
].MaxCombinedUniformComponents
) {
2790 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
2791 linker_warning(prog
, "Too many %s shader uniform components, "
2792 "but the driver will try to optimize them out; "
2793 "this is non-portable out-of-spec behavior\n",
2794 _mesa_shader_stage_to_string(i
));
2796 linker_error(prog
, "Too many %s shader uniform components\n",
2797 _mesa_shader_stage_to_string(i
));
2802 unsigned blocks
[MESA_SHADER_STAGES
] = {0};
2803 unsigned total_uniform_blocks
= 0;
2804 unsigned shader_blocks
[MESA_SHADER_STAGES
] = {0};
2805 unsigned total_shader_storage_blocks
= 0;
2807 for (unsigned i
= 0; i
< prog
->NumBufferInterfaceBlocks
; i
++) {
2808 /* Don't check SSBOs for Uniform Block Size */
2809 if (!prog
->BufferInterfaceBlocks
[i
].IsShaderStorage
&&
2810 prog
->BufferInterfaceBlocks
[i
].UniformBufferSize
> ctx
->Const
.MaxUniformBlockSize
) {
2811 linker_error(prog
, "Uniform block %s too big (%d/%d)\n",
2812 prog
->BufferInterfaceBlocks
[i
].Name
,
2813 prog
->BufferInterfaceBlocks
[i
].UniformBufferSize
,
2814 ctx
->Const
.MaxUniformBlockSize
);
2817 if (prog
->BufferInterfaceBlocks
[i
].IsShaderStorage
&&
2818 prog
->BufferInterfaceBlocks
[i
].UniformBufferSize
> ctx
->Const
.MaxShaderStorageBlockSize
) {
2819 linker_error(prog
, "Shader storage block %s too big (%d/%d)\n",
2820 prog
->BufferInterfaceBlocks
[i
].Name
,
2821 prog
->BufferInterfaceBlocks
[i
].UniformBufferSize
,
2822 ctx
->Const
.MaxShaderStorageBlockSize
);
2825 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
2826 if (prog
->UniformBlockStageIndex
[j
][i
] != -1) {
2827 struct gl_shader
*sh
= prog
->_LinkedShaders
[j
];
2828 int stage_index
= prog
->UniformBlockStageIndex
[j
][i
];
2829 if (sh
&& sh
->BufferInterfaceBlocks
[stage_index
].IsShaderStorage
) {
2831 total_shader_storage_blocks
++;
2834 total_uniform_blocks
++;
2839 if (total_uniform_blocks
> ctx
->Const
.MaxCombinedUniformBlocks
) {
2840 linker_error(prog
, "Too many combined uniform blocks (%d/%d)\n",
2841 total_uniform_blocks
,
2842 ctx
->Const
.MaxCombinedUniformBlocks
);
2844 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2845 const unsigned max_uniform_blocks
=
2846 ctx
->Const
.Program
[i
].MaxUniformBlocks
;
2847 if (blocks
[i
] > max_uniform_blocks
) {
2848 linker_error(prog
, "Too many %s uniform blocks (%d/%d)\n",
2849 _mesa_shader_stage_to_string(i
),
2851 max_uniform_blocks
);
2857 if (total_shader_storage_blocks
> ctx
->Const
.MaxCombinedShaderStorageBlocks
) {
2858 linker_error(prog
, "Too many combined shader storage blocks (%d/%d)\n",
2859 total_shader_storage_blocks
,
2860 ctx
->Const
.MaxCombinedShaderStorageBlocks
);
2862 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2863 const unsigned max_shader_storage_blocks
=
2864 ctx
->Const
.Program
[i
].MaxShaderStorageBlocks
;
2865 if (shader_blocks
[i
] > max_shader_storage_blocks
) {
2866 linker_error(prog
, "Too many %s shader storage blocks (%d/%d)\n",
2867 _mesa_shader_stage_to_string(i
),
2869 max_shader_storage_blocks
);
2878 link_calculate_subroutine_compat(struct gl_shader_program
*prog
)
2880 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2881 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2886 for (unsigned j
= 0; j
< sh
->NumSubroutineUniformRemapTable
; j
++) {
2887 struct gl_uniform_storage
*uni
= sh
->SubroutineUniformRemapTable
[j
];
2893 for (unsigned f
= 0; f
< sh
->NumSubroutineFunctions
; f
++) {
2894 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[f
];
2895 for (int k
= 0; k
< fn
->num_compat_types
; k
++) {
2896 if (fn
->types
[k
] == uni
->type
) {
2902 uni
->num_compatible_subroutines
= count
;
2908 check_subroutine_resources(struct gl_shader_program
*prog
)
2910 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2911 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2914 if (sh
->NumSubroutineUniformRemapTable
> MAX_SUBROUTINE_UNIFORM_LOCATIONS
)
2915 linker_error(prog
, "Too many %s shader subroutine uniforms\n",
2916 _mesa_shader_stage_to_string(i
));
2921 * Validate shader image resources.
2924 check_image_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
2926 unsigned total_image_units
= 0;
2927 unsigned fragment_outputs
= 0;
2928 unsigned total_shader_storage_blocks
= 0;
2930 if (!ctx
->Extensions
.ARB_shader_image_load_store
)
2933 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2934 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2937 if (sh
->NumImages
> ctx
->Const
.Program
[i
].MaxImageUniforms
)
2938 linker_error(prog
, "Too many %s shader image uniforms (%u > %u)\n",
2939 _mesa_shader_stage_to_string(i
), sh
->NumImages
,
2940 ctx
->Const
.Program
[i
].MaxImageUniforms
);
2942 total_image_units
+= sh
->NumImages
;
2944 for (unsigned j
= 0; j
< prog
->NumBufferInterfaceBlocks
; j
++) {
2945 int stage_index
= prog
->UniformBlockStageIndex
[i
][j
];
2946 if (stage_index
!= -1 && sh
->BufferInterfaceBlocks
[stage_index
].IsShaderStorage
)
2947 total_shader_storage_blocks
++;
2950 if (i
== MESA_SHADER_FRAGMENT
) {
2951 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2952 ir_variable
*var
= node
->as_variable();
2953 if (var
&& var
->data
.mode
== ir_var_shader_out
)
2954 fragment_outputs
+= var
->type
->count_attribute_slots();
2960 if (total_image_units
> ctx
->Const
.MaxCombinedImageUniforms
)
2961 linker_error(prog
, "Too many combined image uniforms\n");
2963 if (total_image_units
+ fragment_outputs
+ total_shader_storage_blocks
>
2964 ctx
->Const
.MaxCombinedShaderOutputResources
)
2965 linker_error(prog
, "Too many combined image uniforms, shader storage "
2966 " buffers and fragment outputs\n");
2971 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
2972 * for a variable, checks for overlaps between other uniforms using explicit
2976 reserve_explicit_locations(struct gl_shader_program
*prog
,
2977 string_to_uint_map
*map
, ir_variable
*var
)
2979 unsigned slots
= var
->type
->uniform_locations();
2980 unsigned max_loc
= var
->data
.location
+ slots
- 1;
2982 /* Resize remap table if locations do not fit in the current one. */
2983 if (max_loc
+ 1 > prog
->NumUniformRemapTable
) {
2984 prog
->UniformRemapTable
=
2985 reralloc(prog
, prog
->UniformRemapTable
,
2986 gl_uniform_storage
*,
2989 if (!prog
->UniformRemapTable
) {
2990 linker_error(prog
, "Out of memory during linking.\n");
2994 /* Initialize allocated space. */
2995 for (unsigned i
= prog
->NumUniformRemapTable
; i
< max_loc
+ 1; i
++)
2996 prog
->UniformRemapTable
[i
] = NULL
;
2998 prog
->NumUniformRemapTable
= max_loc
+ 1;
3001 for (unsigned i
= 0; i
< slots
; i
++) {
3002 unsigned loc
= var
->data
.location
+ i
;
3004 /* Check if location is already used. */
3005 if (prog
->UniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3007 /* Possibly same uniform from a different stage, this is ok. */
3009 if (map
->get(hash_loc
, var
->name
) && hash_loc
== loc
- i
)
3012 /* ARB_explicit_uniform_location specification states:
3014 * "No two default-block uniform variables in the program can have
3015 * the same location, even if they are unused, otherwise a compiler
3016 * or linker error will be generated."
3019 "location qualifier for uniform %s overlaps "
3020 "previously used location\n",
3025 /* Initialize location as inactive before optimization
3026 * rounds and location assignment.
3028 prog
->UniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3031 /* Note, base location used for arrays. */
3032 map
->put(var
->data
.location
, var
->name
);
3038 reserve_subroutine_explicit_locations(struct gl_shader_program
*prog
,
3039 struct gl_shader
*sh
,
3042 unsigned slots
= var
->type
->uniform_locations();
3043 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3045 /* Resize remap table if locations do not fit in the current one. */
3046 if (max_loc
+ 1 > sh
->NumSubroutineUniformRemapTable
) {
3047 sh
->SubroutineUniformRemapTable
=
3048 reralloc(sh
, sh
->SubroutineUniformRemapTable
,
3049 gl_uniform_storage
*,
3052 if (!sh
->SubroutineUniformRemapTable
) {
3053 linker_error(prog
, "Out of memory during linking.\n");
3057 /* Initialize allocated space. */
3058 for (unsigned i
= sh
->NumSubroutineUniformRemapTable
; i
< max_loc
+ 1; i
++)
3059 sh
->SubroutineUniformRemapTable
[i
] = NULL
;
3061 sh
->NumSubroutineUniformRemapTable
= max_loc
+ 1;
3064 for (unsigned i
= 0; i
< slots
; i
++) {
3065 unsigned loc
= var
->data
.location
+ i
;
3067 /* Check if location is already used. */
3068 if (sh
->SubroutineUniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3070 /* ARB_explicit_uniform_location specification states:
3071 * "No two subroutine uniform variables can have the same location
3072 * in the same shader stage, otherwise a compiler or linker error
3073 * will be generated."
3076 "location qualifier for uniform %s overlaps "
3077 "previously used location\n",
3082 /* Initialize location as inactive before optimization
3083 * rounds and location assignment.
3085 sh
->SubroutineUniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3091 * Check and reserve all explicit uniform locations, called before
3092 * any optimizations happen to handle also inactive uniforms and
3093 * inactive array elements that may get trimmed away.
3096 check_explicit_uniform_locations(struct gl_context
*ctx
,
3097 struct gl_shader_program
*prog
)
3099 if (!ctx
->Extensions
.ARB_explicit_uniform_location
)
3102 /* This map is used to detect if overlapping explicit locations
3103 * occur with the same uniform (from different stage) or a different one.
3105 string_to_uint_map
*uniform_map
= new string_to_uint_map
;
3108 linker_error(prog
, "Out of memory during linking.\n");
3112 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3113 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
3118 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3119 ir_variable
*var
= node
->as_variable();
3120 if (var
&& (var
->data
.mode
== ir_var_uniform
|| var
->data
.mode
== ir_var_shader_storage
) &&
3121 var
->data
.explicit_location
) {
3123 if (var
->type
->is_subroutine())
3124 ret
= reserve_subroutine_explicit_locations(prog
, sh
, var
);
3126 ret
= reserve_explicit_locations(prog
, uniform_map
, var
);
3139 should_add_buffer_variable(struct gl_shader_program
*shProg
,
3140 GLenum type
, const char *name
)
3142 bool found_interface
= false;
3143 const char *block_name
= NULL
;
3145 /* These rules only apply to buffer variables. So we return
3146 * true for the rest of types.
3148 if (type
!= GL_BUFFER_VARIABLE
)
3151 for (unsigned i
= 0; i
< shProg
->NumBufferInterfaceBlocks
; i
++) {
3152 block_name
= shProg
->BufferInterfaceBlocks
[i
].Name
;
3153 if (strncmp(block_name
, name
, strlen(block_name
)) == 0) {
3154 found_interface
= true;
3159 /* We remove the interface name from the buffer variable name,
3160 * including the dot that follows it.
3162 if (found_interface
)
3163 name
= name
+ strlen(block_name
) + 1;
3165 /* From: ARB_program_interface_query extension:
3167 * "For an active shader storage block member declared as an array, an
3168 * entry will be generated only for the first array element, regardless
3169 * of its type. For arrays of aggregate types, the enumeration rules are
3170 * applied recursively for the single enumerated array element.
3172 const char *first_dot
= strchr(name
, '.');
3173 const char *first_square_bracket
= strchr(name
, '[');
3175 /* The buffer variable is on top level and it is not an array */
3176 if (!first_square_bracket
) {
3178 /* The shader storage block member is a struct, then generate the entry */
3179 } else if (first_dot
&& first_dot
< first_square_bracket
) {
3182 /* Shader storage block member is an array, only generate an entry for the
3183 * first array element.
3185 if (strncmp(first_square_bracket
, "[0]", 3) == 0)
3193 add_program_resource(struct gl_shader_program
*prog
, GLenum type
,
3194 const void *data
, uint8_t stages
)
3198 /* If resource already exists, do not add it again. */
3199 for (unsigned i
= 0; i
< prog
->NumProgramResourceList
; i
++)
3200 if (prog
->ProgramResourceList
[i
].Data
== data
)
3203 prog
->ProgramResourceList
=
3205 prog
->ProgramResourceList
,
3206 gl_program_resource
,
3207 prog
->NumProgramResourceList
+ 1);
3209 if (!prog
->ProgramResourceList
) {
3210 linker_error(prog
, "Out of memory during linking.\n");
3214 struct gl_program_resource
*res
=
3215 &prog
->ProgramResourceList
[prog
->NumProgramResourceList
];
3219 res
->StageReferences
= stages
;
3221 prog
->NumProgramResourceList
++;
3226 /* Function checks if a variable var is a packed varying and
3227 * if given name is part of packed varying's list.
3229 * If a variable is a packed varying, it has a name like
3230 * 'packed:a,b,c' where a, b and c are separate variables.
3233 included_in_packed_varying(ir_variable
*var
, const char *name
)
3235 if (strncmp(var
->name
, "packed:", 7) != 0)
3238 char *list
= strdup(var
->name
+ 7);
3243 char *token
= strtok_r(list
, ",", &saveptr
);
3245 if (strcmp(token
, name
) == 0) {
3249 token
= strtok_r(NULL
, ",", &saveptr
);
3256 * Function builds a stage reference bitmask from variable name.
3259 build_stageref(struct gl_shader_program
*shProg
, const char *name
,
3264 /* Note, that we assume MAX 8 stages, if there will be more stages, type
3265 * used for reference mask in gl_program_resource will need to be changed.
3267 assert(MESA_SHADER_STAGES
< 8);
3269 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3270 struct gl_shader
*sh
= shProg
->_LinkedShaders
[i
];
3274 /* Shader symbol table may contain variables that have
3275 * been optimized away. Search IR for the variable instead.
3277 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3278 ir_variable
*var
= node
->as_variable();
3280 unsigned baselen
= strlen(var
->name
);
3282 if (included_in_packed_varying(var
, name
)) {
3287 /* Type needs to match if specified, otherwise we might
3288 * pick a variable with same name but different interface.
3290 if (var
->data
.mode
!= mode
)
3293 if (strncmp(var
->name
, name
, baselen
) == 0) {
3294 /* Check for exact name matches but also check for arrays and
3297 if (name
[baselen
] == '\0' ||
3298 name
[baselen
] == '[' ||
3299 name
[baselen
] == '.') {
3311 add_interface_variables(struct gl_shader_program
*shProg
,
3312 exec_list
*ir
, GLenum programInterface
)
3314 foreach_in_list(ir_instruction
, node
, ir
) {
3315 ir_variable
*var
= node
->as_variable();
3321 switch (var
->data
.mode
) {
3322 /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
3323 * "For GetActiveAttrib, all active vertex shader input variables
3324 * are enumerated, including the special built-in inputs gl_VertexID
3325 * and gl_InstanceID."
3327 case ir_var_system_value
:
3328 if (var
->data
.location
!= SYSTEM_VALUE_VERTEX_ID
&&
3329 var
->data
.location
!= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
&&
3330 var
->data
.location
!= SYSTEM_VALUE_INSTANCE_ID
)
3332 /* Mark special built-in inputs referenced by the vertex stage so
3333 * that they are considered active by the shader queries.
3335 mask
= (1 << (MESA_SHADER_VERTEX
));
3337 case ir_var_shader_in
:
3338 if (programInterface
!= GL_PROGRAM_INPUT
)
3341 case ir_var_shader_out
:
3342 if (programInterface
!= GL_PROGRAM_OUTPUT
)
3349 /* Skip packed varyings, packed varyings are handled separately
3350 * by add_packed_varyings.
3352 if (strncmp(var
->name
, "packed:", 7) == 0)
3355 if (!add_program_resource(shProg
, programInterface
, var
,
3356 build_stageref(shProg
, var
->name
,
3357 var
->data
.mode
) | mask
))
3364 add_packed_varyings(struct gl_shader_program
*shProg
, int stage
)
3366 struct gl_shader
*sh
= shProg
->_LinkedShaders
[stage
];
3369 if (!sh
|| !sh
->packed_varyings
)
3372 foreach_in_list(ir_instruction
, node
, sh
->packed_varyings
) {
3373 ir_variable
*var
= node
->as_variable();
3375 switch (var
->data
.mode
) {
3376 case ir_var_shader_in
:
3377 iface
= GL_PROGRAM_INPUT
;
3379 case ir_var_shader_out
:
3380 iface
= GL_PROGRAM_OUTPUT
;
3383 unreachable("unexpected type");
3385 if (!add_program_resource(shProg
, iface
, var
,
3386 build_stageref(shProg
, var
->name
,
3395 get_top_level_name(const char *name
)
3397 const char *first_dot
= strchr(name
, '.');
3398 const char *first_square_bracket
= strchr(name
, '[');
3400 /* From ARB_program_interface_query spec:
3402 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying the
3403 * number of active array elements of the top-level shader storage block
3404 * member containing to the active variable is written to <params>. If the
3405 * top-level block member is not declared as an array, the value one is
3406 * written to <params>. If the top-level block member is an array with no
3407 * declared size, the value zero is written to <params>.
3410 /* The buffer variable is on top level.*/
3411 if (!first_square_bracket
&& !first_dot
)
3412 name_size
= strlen(name
);
3413 else if ((!first_square_bracket
||
3414 (first_dot
&& first_dot
< first_square_bracket
)))
3415 name_size
= first_dot
- name
;
3417 name_size
= first_square_bracket
- name
;
3419 return strndup(name
, name_size
);
3423 get_var_name(const char *name
)
3425 const char *first_dot
= strchr(name
, '.');
3428 return strdup(name
);
3430 return strndup(first_dot
+1, strlen(first_dot
) - 1);
3434 is_top_level_shader_storage_block_member(const char* name
,
3435 const char* interface_name
,
3436 const char* field_name
)
3438 bool result
= false;
3440 /* If the given variable is already a top-level shader storage
3441 * block member, then return array_size = 1.
3442 * We could have two possibilities: if we have an instanced
3443 * shader storage block or not instanced.
3445 * For the first, we check create a name as it was in top level and
3446 * compare it with the real name. If they are the same, then
3447 * the variable is already at top-level.
3449 * Full instanced name is: interface name + '.' + var name +
3452 int name_length
= strlen(interface_name
) + 1 + strlen(field_name
) + 1;
3453 char *full_instanced_name
= (char *) calloc(name_length
, sizeof(char));
3454 if (!full_instanced_name
) {
3455 fprintf(stderr
, "%s: Cannot allocate space for name\n", __func__
);
3459 snprintf(full_instanced_name
, name_length
, "%s.%s",
3460 interface_name
, field_name
);
3462 /* Check if its top-level shader storage block member of an
3463 * instanced interface block, or of a unnamed interface block.
3465 if (strcmp(name
, full_instanced_name
) == 0 ||
3466 strcmp(name
, field_name
) == 0)
3469 free(full_instanced_name
);
3474 calculate_array_size(struct gl_shader_program
*shProg
,
3475 struct gl_uniform_storage
*uni
)
3477 int block_index
= uni
->block_index
;
3478 int array_size
= -1;
3479 char *var_name
= get_top_level_name(uni
->name
);
3480 char *interface_name
=
3481 get_top_level_name(shProg
->BufferInterfaceBlocks
[block_index
].Name
);
3483 if (strcmp(var_name
, interface_name
) == 0) {
3484 /* Deal with instanced array of SSBOs */
3485 char *temp_name
= get_var_name(uni
->name
);
3487 var_name
= get_top_level_name(temp_name
);
3491 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
3492 if (shProg
->Shaders
[i
] == NULL
)
3495 const gl_shader
*stage
= shProg
->Shaders
[i
];
3496 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
3497 ir_variable
*var
= node
->as_variable();
3498 if (!var
|| !var
->get_interface_type() ||
3499 var
->data
.mode
!= ir_var_shader_storage
)
3502 const glsl_type
*interface
= var
->get_interface_type();
3504 if (strcmp(interface_name
, interface
->name
) != 0)
3507 for (unsigned i
= 0; i
< interface
->length
; i
++) {
3508 const glsl_struct_field
*field
= &interface
->fields
.structure
[i
];
3509 if (strcmp(field
->name
, var_name
) != 0)
3511 /* From GL_ARB_program_interface_query spec:
3513 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer
3514 * identifying the number of active array elements of the top-level
3515 * shader storage block member containing to the active variable is
3516 * written to <params>. If the top-level block member is not
3517 * declared as an array, the value one is written to <params>. If
3518 * the top-level block member is an array with no declared size,
3519 * the value zero is written to <params>.
3521 if (is_top_level_shader_storage_block_member(uni
->name
,
3525 else if (field
->type
->is_unsized_array())
3527 else if (field
->type
->is_array())
3528 array_size
= field
->type
->length
;
3532 goto found_top_level_array_size
;
3536 found_top_level_array_size
:
3537 free(interface_name
);
3539 uni
->top_level_array_size
= array_size
;
3543 calculate_array_stride(struct gl_shader_program
*shProg
,
3544 struct gl_uniform_storage
*uni
)
3546 int block_index
= uni
->block_index
;
3547 int array_stride
= -1;
3548 char *var_name
= get_top_level_name(uni
->name
);
3549 char *interface_name
=
3550 get_top_level_name(shProg
->BufferInterfaceBlocks
[block_index
].Name
);
3552 if (strcmp(var_name
, interface_name
) == 0) {
3553 /* Deal with instanced array of SSBOs */
3554 char *temp_name
= get_var_name(uni
->name
);
3556 var_name
= get_top_level_name(temp_name
);
3560 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
3561 if (shProg
->Shaders
[i
] == NULL
)
3564 const gl_shader
*stage
= shProg
->Shaders
[i
];
3565 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
3566 ir_variable
*var
= node
->as_variable();
3567 if (!var
|| !var
->get_interface_type() ||
3568 var
->data
.mode
!= ir_var_shader_storage
)
3571 const glsl_type
*interface
= var
->get_interface_type();
3573 if (strcmp(interface_name
, interface
->name
) != 0) {
3577 for (unsigned i
= 0; i
< interface
->length
; i
++) {
3578 const glsl_struct_field
*field
= &interface
->fields
.structure
[i
];
3579 if (strcmp(field
->name
, var_name
) != 0)
3581 /* From GL_ARB_program_interface_query:
3583 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
3584 * identifying the stride between array elements of the top-level
3585 * shader storage block member containing the active variable is
3586 * written to <params>. For top-level block members declared as
3587 * arrays, the value written is the difference, in basic machine
3588 * units, between the offsets of the active variable for
3589 * consecutive elements in the top-level array. For top-level
3590 * block members not declared as an array, zero is written to
3593 if (field
->type
->is_array()) {
3594 const enum glsl_matrix_layout matrix_layout
=
3595 glsl_matrix_layout(field
->matrix_layout
);
3596 bool row_major
= matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
3597 const glsl_type
*array_type
= field
->type
->fields
.array
;
3599 if (is_top_level_shader_storage_block_member(uni
->name
,
3603 goto found_top_level_array_stride
;
3605 if (interface
->interface_packing
!= GLSL_INTERFACE_PACKING_STD430
) {
3606 if (array_type
->is_record() || array_type
->is_array()) {
3607 array_stride
= array_type
->std140_size(row_major
);
3608 array_stride
= glsl_align(array_stride
, 16);
3610 unsigned element_base_align
= 0;
3611 element_base_align
= array_type
->std140_base_alignment(row_major
);
3612 array_stride
= MAX2(element_base_align
, 16);
3615 array_stride
= array_type
->std430_array_stride(row_major
);
3620 goto found_top_level_array_stride
;
3624 found_top_level_array_stride
:
3625 free(interface_name
);
3627 uni
->top_level_array_stride
= array_stride
;
3631 * Builds up a list of program resources that point to existing
3635 build_program_resource_list(struct gl_shader_program
*shProg
)
3637 /* Rebuild resource list. */
3638 if (shProg
->ProgramResourceList
) {
3639 ralloc_free(shProg
->ProgramResourceList
);
3640 shProg
->ProgramResourceList
= NULL
;
3641 shProg
->NumProgramResourceList
= 0;
3644 int input_stage
= MESA_SHADER_STAGES
, output_stage
= 0;
3646 /* Determine first input and final output stage. These are used to
3647 * detect which variables should be enumerated in the resource list
3648 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
3650 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3651 if (!shProg
->_LinkedShaders
[i
])
3653 if (input_stage
== MESA_SHADER_STAGES
)
3658 /* Empty shader, no resources. */
3659 if (input_stage
== MESA_SHADER_STAGES
&& output_stage
== 0)
3662 /* Program interface needs to expose varyings in case of SSO. */
3663 if (shProg
->SeparateShader
) {
3664 if (!add_packed_varyings(shProg
, input_stage
))
3666 if (!add_packed_varyings(shProg
, output_stage
))
3670 /* Add inputs and outputs to the resource list. */
3671 if (!add_interface_variables(shProg
, shProg
->_LinkedShaders
[input_stage
]->ir
,
3675 if (!add_interface_variables(shProg
, shProg
->_LinkedShaders
[output_stage
]->ir
,
3679 /* Add transform feedback varyings. */
3680 if (shProg
->LinkedTransformFeedback
.NumVarying
> 0) {
3681 for (int i
= 0; i
< shProg
->LinkedTransformFeedback
.NumVarying
; i
++) {
3682 if (!add_program_resource(shProg
, GL_TRANSFORM_FEEDBACK_VARYING
,
3683 &shProg
->LinkedTransformFeedback
.Varyings
[i
],
3689 /* Add uniforms from uniform storage. */
3690 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; i
++) {
3691 /* Do not add uniforms internally used by Mesa. */
3692 if (shProg
->UniformStorage
[i
].hidden
)
3696 build_stageref(shProg
, shProg
->UniformStorage
[i
].name
,
3699 /* Add stagereferences for uniforms in a uniform block. */
3700 int block_index
= shProg
->UniformStorage
[i
].block_index
;
3701 if (block_index
!= -1) {
3702 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
3703 if (shProg
->UniformBlockStageIndex
[j
][block_index
] != -1)
3704 stageref
|= (1 << j
);
3708 bool is_shader_storage
= shProg
->UniformStorage
[i
].is_shader_storage
;
3709 GLenum type
= is_shader_storage
? GL_BUFFER_VARIABLE
: GL_UNIFORM
;
3710 if (!should_add_buffer_variable(shProg
, type
,
3711 shProg
->UniformStorage
[i
].name
))
3714 if (is_shader_storage
) {
3715 calculate_array_size(shProg
, &shProg
->UniformStorage
[i
]);
3716 calculate_array_stride(shProg
, &shProg
->UniformStorage
[i
]);
3719 if (!add_program_resource(shProg
, type
,
3720 &shProg
->UniformStorage
[i
], stageref
))
3724 /* Add program uniform blocks and shader storage blocks. */
3725 for (unsigned i
= 0; i
< shProg
->NumBufferInterfaceBlocks
; i
++) {
3726 bool is_shader_storage
= shProg
->BufferInterfaceBlocks
[i
].IsShaderStorage
;
3727 GLenum type
= is_shader_storage
? GL_SHADER_STORAGE_BLOCK
: GL_UNIFORM_BLOCK
;
3728 if (!add_program_resource(shProg
, type
,
3729 &shProg
->BufferInterfaceBlocks
[i
], 0))
3733 /* Add atomic counter buffers. */
3734 for (unsigned i
= 0; i
< shProg
->NumAtomicBuffers
; i
++) {
3735 if (!add_program_resource(shProg
, GL_ATOMIC_COUNTER_BUFFER
,
3736 &shProg
->AtomicBuffers
[i
], 0))
3740 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; i
++) {
3742 if (!shProg
->UniformStorage
[i
].hidden
)
3745 for (int j
= MESA_SHADER_VERTEX
; j
< MESA_SHADER_STAGES
; j
++) {
3746 if (!shProg
->UniformStorage
[i
].opaque
[j
].active
)
3749 type
= _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage
)j
);
3750 /* add shader subroutines */
3751 if (!add_program_resource(shProg
, type
, &shProg
->UniformStorage
[i
], 0))
3756 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3757 struct gl_shader
*sh
= shProg
->_LinkedShaders
[i
];
3763 type
= _mesa_shader_stage_to_subroutine((gl_shader_stage
)i
);
3764 for (unsigned j
= 0; j
< sh
->NumSubroutineFunctions
; j
++) {
3765 if (!add_program_resource(shProg
, type
, &sh
->SubroutineFunctions
[j
], 0))
3770 /* TODO - following extensions will require more resource types:
3772 * GL_ARB_shader_storage_buffer_object
3777 * This check is done to make sure we allow only constant expression
3778 * indexing and "constant-index-expression" (indexing with an expression
3779 * that includes loop induction variable).
3782 validate_sampler_array_indexing(struct gl_context
*ctx
,
3783 struct gl_shader_program
*prog
)
3785 dynamic_sampler_array_indexing_visitor v
;
3786 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3787 if (prog
->_LinkedShaders
[i
] == NULL
)
3790 bool no_dynamic_indexing
=
3791 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
;
3793 /* Search for array derefs in shader. */
3794 v
.run(prog
->_LinkedShaders
[i
]->ir
);
3795 if (v
.uses_dynamic_sampler_array_indexing()) {
3796 const char *msg
= "sampler arrays indexed with non-constant "
3797 "expressions is forbidden in GLSL %s %u";
3798 /* Backend has indicated that it has no dynamic indexing support. */
3799 if (no_dynamic_indexing
) {
3800 linker_error(prog
, msg
, prog
->IsES
? "ES" : "", prog
->Version
);
3803 linker_warning(prog
, msg
, prog
->IsES
? "ES" : "", prog
->Version
);
3811 link_assign_subroutine_types(struct gl_shader_program
*prog
)
3813 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3814 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
3819 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3820 ir_function
*fn
= node
->as_function();
3824 if (fn
->is_subroutine
)
3825 sh
->NumSubroutineUniformTypes
++;
3827 if (!fn
->num_subroutine_types
)
3830 sh
->SubroutineFunctions
= reralloc(sh
, sh
->SubroutineFunctions
,
3831 struct gl_subroutine_function
,
3832 sh
->NumSubroutineFunctions
+ 1);
3833 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].name
= ralloc_strdup(sh
, fn
->name
);
3834 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].num_compat_types
= fn
->num_subroutine_types
;
3835 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].types
=
3836 ralloc_array(sh
, const struct glsl_type
*,
3837 fn
->num_subroutine_types
);
3838 for (int j
= 0; j
< fn
->num_subroutine_types
; j
++)
3839 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].types
[j
] = fn
->subroutine_types
[j
];
3840 sh
->NumSubroutineFunctions
++;
3846 split_ubos_and_ssbos(void *mem_ctx
,
3847 struct gl_uniform_block
*blocks
,
3848 unsigned num_blocks
,
3849 struct gl_uniform_block
***ubos
,
3851 struct gl_uniform_block
***ssbos
,
3852 unsigned *num_ssbos
)
3854 unsigned num_ubo_blocks
= 0;
3855 unsigned num_ssbo_blocks
= 0;
3857 for (unsigned i
= 0; i
< num_blocks
; i
++) {
3858 if (blocks
[i
].IsShaderStorage
)
3864 *ubos
= ralloc_array(mem_ctx
, gl_uniform_block
*, num_ubo_blocks
);
3867 *ssbos
= ralloc_array(mem_ctx
, gl_uniform_block
*, num_ssbo_blocks
);
3870 for (unsigned i
= 0; i
< num_blocks
; i
++) {
3871 if (blocks
[i
].IsShaderStorage
) {
3872 (*ssbos
)[(*num_ssbos
)++] = &blocks
[i
];
3874 (*ubos
)[(*num_ubos
)++] = &blocks
[i
];
3878 assert(*num_ubos
+ *num_ssbos
== num_blocks
);
3882 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3884 tfeedback_decl
*tfeedback_decls
= NULL
;
3885 unsigned num_tfeedback_decls
= prog
->TransformFeedback
.NumVarying
;
3887 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
3889 prog
->LinkStatus
= true; /* All error paths will set this to false */
3890 prog
->Validated
= false;
3891 prog
->_Used
= false;
3893 prog
->ARB_fragment_coord_conventions_enable
= false;
3895 /* Separate the shaders into groups based on their type.
3897 struct gl_shader
**shader_list
[MESA_SHADER_STAGES
];
3898 unsigned num_shaders
[MESA_SHADER_STAGES
];
3900 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3901 shader_list
[i
] = (struct gl_shader
**)
3902 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
3906 unsigned min_version
= UINT_MAX
;
3907 unsigned max_version
= 0;
3908 const bool is_es_prog
=
3909 (prog
->NumShaders
> 0 && prog
->Shaders
[0]->IsES
) ? true : false;
3910 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
3911 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
3912 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
3914 if (prog
->Shaders
[i
]->IsES
!= is_es_prog
) {
3915 linker_error(prog
, "all shaders must use same shading "
3916 "language version\n");
3920 if (prog
->Shaders
[i
]->ARB_fragment_coord_conventions_enable
) {
3921 prog
->ARB_fragment_coord_conventions_enable
= true;
3924 gl_shader_stage shader_type
= prog
->Shaders
[i
]->Stage
;
3925 shader_list
[shader_type
][num_shaders
[shader_type
]] = prog
->Shaders
[i
];
3926 num_shaders
[shader_type
]++;
3929 /* In desktop GLSL, different shader versions may be linked together. In
3930 * GLSL ES, all shader versions must be the same.
3932 if (is_es_prog
&& min_version
!= max_version
) {
3933 linker_error(prog
, "all shaders must use same shading "
3934 "language version\n");
3938 prog
->Version
= max_version
;
3939 prog
->IsES
= is_es_prog
;
3941 /* From OpenGL 4.5 Core specification (7.3 Program Objects):
3942 * "Linking can fail for a variety of reasons as specified in the OpenGL
3943 * Shading Language Specification, as well as any of the following
3946 * * No shader objects are attached to program.
3950 * Same rule applies for OpenGL ES >= 3.1.
3953 if (prog
->NumShaders
== 0 &&
3954 ((ctx
->API
== API_OPENGL_CORE
&& ctx
->Version
>= 45) ||
3955 (ctx
->API
== API_OPENGLES2
&& ctx
->Version
>= 31))) {
3956 linker_error(prog
, "No shader objects are attached to program.\n");
3960 /* Some shaders have to be linked with some other shaders present.
3962 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0 &&
3963 num_shaders
[MESA_SHADER_VERTEX
] == 0 &&
3964 !prog
->SeparateShader
) {
3965 linker_error(prog
, "Geometry shader must be linked with "
3969 if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0 &&
3970 num_shaders
[MESA_SHADER_VERTEX
] == 0 &&
3971 !prog
->SeparateShader
) {
3972 linker_error(prog
, "Tessellation evaluation shader must be linked with "
3976 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
3977 num_shaders
[MESA_SHADER_VERTEX
] == 0 &&
3978 !prog
->SeparateShader
) {
3979 linker_error(prog
, "Tessellation control shader must be linked with "
3984 /* The spec is self-contradictory here. It allows linking without a tess
3985 * eval shader, but that can only be used with transform feedback and
3986 * rasterization disabled. However, transform feedback isn't allowed
3987 * with GL_PATCHES, so it can't be used.
3989 * More investigation showed that the idea of transform feedback after
3990 * a tess control shader was dropped, because some hw vendors couldn't
3991 * support tessellation without a tess eval shader, but the linker section
3992 * wasn't updated to reflect that.
3994 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
3997 * Do what's reasonable and always require a tess eval shader if a tess
3998 * control shader is present.
4000 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4001 num_shaders
[MESA_SHADER_TESS_EVAL
] == 0 &&
4002 !prog
->SeparateShader
) {
4003 linker_error(prog
, "Tessellation control shader must be linked with "
4004 "tessellation evaluation shader\n");
4008 /* Compute shaders have additional restrictions. */
4009 if (num_shaders
[MESA_SHADER_COMPUTE
] > 0 &&
4010 num_shaders
[MESA_SHADER_COMPUTE
] != prog
->NumShaders
) {
4011 linker_error(prog
, "Compute shaders may not be linked with any other "
4012 "type of shader\n");
4015 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4016 if (prog
->_LinkedShaders
[i
] != NULL
)
4017 _mesa_delete_shader(ctx
, prog
->_LinkedShaders
[i
]);
4019 prog
->_LinkedShaders
[i
] = NULL
;
4022 /* Link all shaders for a particular stage and validate the result.
4024 for (int stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4025 if (num_shaders
[stage
] > 0) {
4026 gl_shader
*const sh
=
4027 link_intrastage_shaders(mem_ctx
, ctx
, prog
, shader_list
[stage
],
4028 num_shaders
[stage
]);
4030 if (!prog
->LinkStatus
) {
4032 _mesa_delete_shader(ctx
, sh
);
4037 case MESA_SHADER_VERTEX
:
4038 validate_vertex_shader_executable(prog
, sh
);
4040 case MESA_SHADER_TESS_CTRL
:
4041 /* nothing to be done */
4043 case MESA_SHADER_TESS_EVAL
:
4044 validate_tess_eval_shader_executable(prog
, sh
);
4046 case MESA_SHADER_GEOMETRY
:
4047 validate_geometry_shader_executable(prog
, sh
);
4049 case MESA_SHADER_FRAGMENT
:
4050 validate_fragment_shader_executable(prog
, sh
);
4053 if (!prog
->LinkStatus
) {
4055 _mesa_delete_shader(ctx
, sh
);
4059 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[stage
], sh
);
4063 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0)
4064 prog
->LastClipDistanceArraySize
= prog
->Geom
.ClipDistanceArraySize
;
4065 else if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0)
4066 prog
->LastClipDistanceArraySize
= prog
->TessEval
.ClipDistanceArraySize
;
4067 else if (num_shaders
[MESA_SHADER_VERTEX
] > 0)
4068 prog
->LastClipDistanceArraySize
= prog
->Vert
.ClipDistanceArraySize
;
4070 prog
->LastClipDistanceArraySize
= 0; /* Not used */
4072 /* Here begins the inter-stage linking phase. Some initial validation is
4073 * performed, then locations are assigned for uniforms, attributes, and
4076 cross_validate_uniforms(prog
);
4077 if (!prog
->LinkStatus
)
4082 for (prev
= 0; prev
<= MESA_SHADER_FRAGMENT
; prev
++) {
4083 if (prog
->_LinkedShaders
[prev
] != NULL
)
4087 check_explicit_uniform_locations(ctx
, prog
);
4088 link_assign_subroutine_types(prog
);
4090 if (!prog
->LinkStatus
)
4093 resize_tes_inputs(ctx
, prog
);
4095 /* Validate the inputs of each stage with the output of the preceding
4098 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4099 if (prog
->_LinkedShaders
[i
] == NULL
)
4102 validate_interstage_inout_blocks(prog
, prog
->_LinkedShaders
[prev
],
4103 prog
->_LinkedShaders
[i
]);
4104 if (!prog
->LinkStatus
)
4107 cross_validate_outputs_to_inputs(prog
,
4108 prog
->_LinkedShaders
[prev
],
4109 prog
->_LinkedShaders
[i
]);
4110 if (!prog
->LinkStatus
)
4116 /* Cross-validate uniform blocks between shader stages */
4117 validate_interstage_uniform_blocks(prog
, prog
->_LinkedShaders
,
4118 MESA_SHADER_STAGES
);
4119 if (!prog
->LinkStatus
)
4122 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4123 if (prog
->_LinkedShaders
[i
] != NULL
)
4124 lower_named_interface_blocks(mem_ctx
, prog
->_LinkedShaders
[i
]);
4127 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
4128 * it before optimization because we want most of the checks to get
4129 * dropped thanks to constant propagation.
4131 * This rule also applies to GLSL ES 3.00.
4133 if (max_version
>= (is_es_prog
? 300 : 130)) {
4134 struct gl_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4136 lower_discard_flow(sh
->ir
);
4140 if (!interstage_cross_validate_uniform_blocks(prog
))
4143 /* Do common optimization before assigning storage for attributes,
4144 * uniforms, and varyings. Later optimization could possibly make
4145 * some of that unused.
4147 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4148 if (prog
->_LinkedShaders
[i
] == NULL
)
4151 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
4152 if (!prog
->LinkStatus
)
4155 if (ctx
->Const
.ShaderCompilerOptions
[i
].LowerClipDistance
) {
4156 lower_clip_distance(prog
->_LinkedShaders
[i
]);
4159 if (ctx
->Const
.LowerTessLevel
) {
4160 lower_tess_level(prog
->_LinkedShaders
[i
]);
4163 while (do_common_optimization(prog
->_LinkedShaders
[i
]->ir
, true, false,
4164 &ctx
->Const
.ShaderCompilerOptions
[i
],
4165 ctx
->Const
.NativeIntegers
))
4168 lower_const_arrays_to_uniforms(prog
->_LinkedShaders
[i
]->ir
);
4171 /* Validation for special cases where we allow sampler array indexing
4172 * with loop induction variable. This check emits a warning or error
4173 * depending if backend can handle dynamic indexing.
4175 if ((!prog
->IsES
&& prog
->Version
< 130) ||
4176 (prog
->IsES
&& prog
->Version
< 300)) {
4177 if (!validate_sampler_array_indexing(ctx
, prog
))
4181 /* Check and validate stream emissions in geometry shaders */
4182 validate_geometry_shader_emissions(ctx
, prog
);
4184 /* Mark all generic shader inputs and outputs as unpaired. */
4185 for (unsigned i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4186 if (prog
->_LinkedShaders
[i
] != NULL
) {
4187 link_invalidate_variable_locations(prog
->_LinkedShaders
[i
]->ir
);
4191 if (!assign_attribute_or_color_locations(prog
, &ctx
->Const
,
4192 MESA_SHADER_VERTEX
)) {
4196 if (!assign_attribute_or_color_locations(prog
, &ctx
->Const
,
4197 MESA_SHADER_FRAGMENT
)) {
4201 unsigned first
, last
;
4203 first
= MESA_SHADER_STAGES
;
4206 /* Determine first and last stage. */
4207 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4208 if (!prog
->_LinkedShaders
[i
])
4210 if (first
== MESA_SHADER_STAGES
)
4215 if (num_tfeedback_decls
!= 0) {
4216 /* From GL_EXT_transform_feedback:
4217 * A program will fail to link if:
4219 * * the <count> specified by TransformFeedbackVaryingsEXT is
4220 * non-zero, but the program object has no vertex or geometry
4223 if (first
== MESA_SHADER_FRAGMENT
) {
4224 linker_error(prog
, "Transform feedback varyings specified, but "
4225 "no vertex or geometry shader is present.\n");
4229 tfeedback_decls
= ralloc_array(mem_ctx
, tfeedback_decl
,
4230 prog
->TransformFeedback
.NumVarying
);
4231 if (!parse_tfeedback_decls(ctx
, prog
, mem_ctx
, num_tfeedback_decls
,
4232 prog
->TransformFeedback
.VaryingNames
,
4237 /* Linking the stages in the opposite order (from fragment to vertex)
4238 * ensures that inter-shader outputs written to in an earlier stage are
4239 * eliminated if they are (transitively) not used in a later stage.
4243 if (first
< MESA_SHADER_FRAGMENT
) {
4244 gl_shader
*const sh
= prog
->_LinkedShaders
[last
];
4246 if (first
== MESA_SHADER_GEOMETRY
) {
4247 /* There was no vertex shader, but we still have to assign varying
4248 * locations for use by geometry shader inputs in SSO.
4250 * If the shader is not separable (i.e., prog->SeparateShader is
4251 * false), linking will have already failed when first is
4252 * MESA_SHADER_GEOMETRY.
4254 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4255 NULL
, prog
->_LinkedShaders
[first
],
4256 num_tfeedback_decls
, tfeedback_decls
))
4260 if (last
!= MESA_SHADER_FRAGMENT
&&
4261 (num_tfeedback_decls
!= 0 || prog
->SeparateShader
)) {
4262 /* There was no fragment shader, but we still have to assign varying
4263 * locations for use by transform feedback.
4265 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4267 num_tfeedback_decls
, tfeedback_decls
))
4271 do_dead_builtin_varyings(ctx
, sh
, NULL
,
4272 num_tfeedback_decls
, tfeedback_decls
);
4274 if (!prog
->SeparateShader
)
4275 demote_shader_inputs_and_outputs(sh
, ir_var_shader_out
);
4277 /* Eliminate code that is now dead due to unused outputs being demoted.
4279 while (do_dead_code(sh
->ir
, false))
4282 else if (first
== MESA_SHADER_FRAGMENT
) {
4283 /* If the program only contains a fragment shader...
4285 gl_shader
*const sh
= prog
->_LinkedShaders
[first
];
4287 do_dead_builtin_varyings(ctx
, NULL
, sh
,
4288 num_tfeedback_decls
, tfeedback_decls
);
4290 if (prog
->SeparateShader
) {
4291 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4292 NULL
/* producer */,
4294 0 /* num_tfeedback_decls */,
4295 NULL
/* tfeedback_decls */))
4298 demote_shader_inputs_and_outputs(sh
, ir_var_shader_in
);
4300 while (do_dead_code(sh
->ir
, false))
4305 for (int i
= next
- 1; i
>= 0; i
--) {
4306 if (prog
->_LinkedShaders
[i
] == NULL
)
4309 gl_shader
*const sh_i
= prog
->_LinkedShaders
[i
];
4310 gl_shader
*const sh_next
= prog
->_LinkedShaders
[next
];
4312 if (!assign_varying_locations(ctx
, mem_ctx
, prog
, sh_i
, sh_next
,
4313 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
4317 do_dead_builtin_varyings(ctx
, sh_i
, sh_next
,
4318 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
4321 demote_shader_inputs_and_outputs(sh_i
, ir_var_shader_out
);
4322 demote_shader_inputs_and_outputs(sh_next
, ir_var_shader_in
);
4324 /* Eliminate code that is now dead due to unused outputs being demoted.
4326 while (do_dead_code(sh_i
->ir
, false))
4328 while (do_dead_code(sh_next
->ir
, false))
4331 /* This must be done after all dead varyings are eliminated. */
4332 if (!check_against_output_limit(ctx
, prog
, sh_i
))
4334 if (!check_against_input_limit(ctx
, prog
, sh_next
))
4340 if (!store_tfeedback_info(ctx
, prog
, num_tfeedback_decls
, tfeedback_decls
))
4343 update_array_sizes(prog
);
4344 link_assign_uniform_locations(prog
, ctx
->Const
.UniformBooleanTrue
);
4345 link_assign_atomic_counter_resources(ctx
, prog
);
4346 store_fragdepth_layout(prog
);
4348 link_calculate_subroutine_compat(prog
);
4349 check_resources(ctx
, prog
);
4350 check_subroutine_resources(prog
);
4351 check_image_resources(ctx
, prog
);
4352 link_check_atomic_counter_resources(ctx
, prog
);
4354 if (!prog
->LinkStatus
)
4357 /* OpenGL ES requires that a vertex shader and a fragment shader both be
4358 * present in a linked program. GL_ARB_ES2_compatibility doesn't say
4359 * anything about shader linking when one of the shaders (vertex or
4360 * fragment shader) is absent. So, the extension shouldn't change the
4361 * behavior specified in GLSL specification.
4363 if (!prog
->SeparateShader
&& ctx
->API
== API_OPENGLES2
) {
4364 /* With ES < 3.1 one needs to have always vertex + fragment shader. */
4365 if (ctx
->Version
< 31) {
4366 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
4367 linker_error(prog
, "program lacks a vertex shader\n");
4368 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
4369 linker_error(prog
, "program lacks a fragment shader\n");
4372 /* From OpenGL ES 3.1 specification (7.3 Program Objects):
4373 * "Linking can fail for a variety of reasons as specified in the
4374 * OpenGL ES Shading Language Specification, as well as any of the
4375 * following reasons:
4379 * * program contains objects to form either a vertex shader or
4380 * fragment shader, and program is not separable, and does not
4381 * contain objects to form both a vertex shader and fragment
4384 if (!!prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] ^
4385 !!prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
4386 linker_error(prog
, "Program needs to contain both vertex and "
4387 "fragment shaders.\n");
4392 /* Split BufferInterfaceBlocks into UniformBlocks and ShaderStorageBlocks
4393 * for gl_shader_program and gl_shader, so that drivers that need separate
4394 * index spaces for each set can have that.
4396 for (unsigned i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_STAGES
; i
++) {
4397 if (prog
->_LinkedShaders
[i
] != NULL
) {
4398 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
4399 split_ubos_and_ssbos(sh
,
4400 sh
->BufferInterfaceBlocks
,
4401 sh
->NumBufferInterfaceBlocks
,
4403 &sh
->NumUniformBlocks
,
4404 &sh
->ShaderStorageBlocks
,
4405 &sh
->NumShaderStorageBlocks
);
4409 split_ubos_and_ssbos(prog
,
4410 prog
->BufferInterfaceBlocks
,
4411 prog
->NumBufferInterfaceBlocks
,
4412 &prog
->UniformBlocks
,
4413 &prog
->NumUniformBlocks
,
4414 &prog
->ShaderStorageBlocks
,
4415 &prog
->NumShaderStorageBlocks
);
4417 /* FINISHME: Assign fragment shader output locations. */
4420 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4421 free(shader_list
[i
]);
4422 if (prog
->_LinkedShaders
[i
] == NULL
)
4425 /* Do a final validation step to make sure that the IR wasn't
4426 * invalidated by any modifications performed after intrastage linking.
4428 validate_ir_tree(prog
->_LinkedShaders
[i
]->ir
);
4430 /* Retain any live IR, but trash the rest. */
4431 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
4433 /* The symbol table in the linked shaders may contain references to
4434 * variables that were removed (e.g., unused uniforms). Since it may
4435 * contain junk, there is no possible valid use. Delete it and set the
4438 delete prog
->_LinkedShaders
[i
]->symbols
;
4439 prog
->_LinkedShaders
[i
]->symbols
= NULL
;
4442 ralloc_free(mem_ctx
);