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 clip_distance_array_size 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
,
664 GLuint
*clip_distance_array_size
)
666 *clip_distance_array_size
= 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
));
690 if (clip_distance
.variable_found()) {
691 ir_variable
*clip_distance_var
=
692 shader
->symbols
->get_variable("gl_ClipDistance");
694 assert(clip_distance_var
);
695 *clip_distance_array_size
= clip_distance_var
->type
->length
;
702 * Verify that a vertex shader executable meets all semantic requirements.
704 * Also sets prog->Vert.ClipDistanceArraySize as a side effect.
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
.ClipDistanceArraySize
);
761 validate_tess_eval_shader_executable(struct gl_shader_program
*prog
,
762 struct gl_shader
*shader
)
767 analyze_clip_usage(prog
, shader
, &prog
->TessEval
.ClipDistanceArraySize
);
772 * Verify that a fragment shader executable meets all semantic requirements
774 * \param shader Fragment shader executable to be verified
777 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
778 struct gl_shader
*shader
)
783 find_assignment_visitor
frag_color("gl_FragColor");
784 find_assignment_visitor
frag_data("gl_FragData");
786 frag_color
.run(shader
->ir
);
787 frag_data
.run(shader
->ir
);
789 if (frag_color
.variable_found() && frag_data
.variable_found()) {
790 linker_error(prog
, "fragment shader writes to both "
791 "`gl_FragColor' and `gl_FragData'\n");
796 * Verify that a geometry shader executable meets all semantic requirements
798 * Also sets prog->Geom.VerticesIn, and prog->Geom.ClipDistanceArraySize as
801 * \param shader Geometry shader executable to be verified
804 validate_geometry_shader_executable(struct gl_shader_program
*prog
,
805 struct gl_shader
*shader
)
810 unsigned num_vertices
= vertices_per_prim(prog
->Geom
.InputType
);
811 prog
->Geom
.VerticesIn
= num_vertices
;
813 analyze_clip_usage(prog
, shader
, &prog
->Geom
.ClipDistanceArraySize
);
817 * Check if geometry shaders emit to non-zero streams and do corresponding
821 validate_geometry_shader_emissions(struct gl_context
*ctx
,
822 struct gl_shader_program
*prog
)
824 if (prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
825 find_emit_vertex_visitor
emit_vertex(ctx
->Const
.MaxVertexStreams
- 1);
826 emit_vertex
.run(prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->ir
);
827 if (emit_vertex
.error()) {
828 linker_error(prog
, "Invalid call %s(%d). Accepted values for the "
829 "stream parameter are in the range [0, %d].\n",
830 emit_vertex
.error_func(),
831 emit_vertex
.error_stream(),
832 ctx
->Const
.MaxVertexStreams
- 1);
834 prog
->Geom
.UsesStreams
= emit_vertex
.uses_streams();
835 prog
->Geom
.UsesEndPrimitive
= emit_vertex
.uses_end_primitive();
837 /* From the ARB_gpu_shader5 spec:
839 * "Multiple vertex streams are supported only if the output primitive
840 * type is declared to be "points". A program will fail to link if it
841 * contains a geometry shader calling EmitStreamVertex() or
842 * EndStreamPrimitive() if its output primitive type is not "points".
844 * However, in the same spec:
846 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
847 * with <stream> set to zero."
851 * "The function EndPrimitive() is equivalent to calling
852 * EndStreamPrimitive() with <stream> set to zero."
854 * Since we can call EmitVertex() and EndPrimitive() when we output
855 * primitives other than points, calling EmitStreamVertex(0) or
856 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
857 * does. Currently we only set prog->Geom.UsesStreams to TRUE when
858 * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
861 if (prog
->Geom
.UsesStreams
&& prog
->Geom
.OutputType
!= GL_POINTS
) {
862 linker_error(prog
, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
863 "with n>0 requires point output\n");
869 validate_intrastage_arrays(struct gl_shader_program
*prog
,
870 ir_variable
*const var
,
871 ir_variable
*const existing
)
873 /* Consider the types to be "the same" if both types are arrays
874 * of the same type and one of the arrays is implicitly sized.
875 * In addition, set the type of the linked variable to the
876 * explicitly sized array.
878 if (var
->type
->is_array() && existing
->type
->is_array()) {
879 if ((var
->type
->fields
.array
== existing
->type
->fields
.array
) &&
880 ((var
->type
->length
== 0)|| (existing
->type
->length
== 0))) {
881 if (var
->type
->length
!= 0) {
882 if (var
->type
->length
<= existing
->data
.max_array_access
) {
883 linker_error(prog
, "%s `%s' declared as type "
884 "`%s' but outermost dimension has an index"
887 var
->name
, var
->type
->name
,
888 existing
->data
.max_array_access
);
890 existing
->type
= var
->type
;
892 } else if (existing
->type
->length
!= 0) {
893 if(existing
->type
->length
<= var
->data
.max_array_access
&&
894 !existing
->data
.from_ssbo_unsized_array
) {
895 linker_error(prog
, "%s `%s' declared as type "
896 "`%s' but outermost dimension has an index"
899 var
->name
, existing
->type
->name
,
900 var
->data
.max_array_access
);
905 /* The arrays of structs could have different glsl_type pointers but
906 * they are actually the same type. Use record_compare() to check that.
908 if (existing
->type
->fields
.array
->is_record() &&
909 var
->type
->fields
.array
->is_record() &&
910 existing
->type
->fields
.array
->record_compare(var
->type
->fields
.array
))
919 * Perform validation of global variables used across multiple shaders
922 cross_validate_globals(struct gl_shader_program
*prog
,
923 struct gl_shader
**shader_list
,
924 unsigned num_shaders
,
927 /* Examine all of the uniforms in all of the shaders and cross validate
930 glsl_symbol_table variables
;
931 for (unsigned i
= 0; i
< num_shaders
; i
++) {
932 if (shader_list
[i
] == NULL
)
935 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
936 ir_variable
*const var
= node
->as_variable();
941 if (uniforms_only
&& (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
))
944 /* don't cross validate subroutine uniforms */
945 if (var
->type
->contains_subroutine())
948 /* Don't cross validate temporaries that are at global scope. These
949 * will eventually get pulled into the shaders 'main'.
951 if (var
->data
.mode
== ir_var_temporary
)
954 /* If a global with this name has already been seen, verify that the
955 * new instance has the same type. In addition, if the globals have
956 * initializers, the values of the initializers must be the same.
958 ir_variable
*const existing
= variables
.get_variable(var
->name
);
959 if (existing
!= NULL
) {
960 /* Check if types match. Interface blocks have some special
961 * rules so we handle those elsewhere.
963 if (var
->type
!= existing
->type
&&
964 !var
->is_interface_instance()) {
965 if (!validate_intrastage_arrays(prog
, var
, existing
)) {
966 if (var
->type
->is_record() && existing
->type
->is_record()
967 && existing
->type
->record_compare(var
->type
)) {
968 existing
->type
= var
->type
;
970 /* If it is an unsized array in a Shader Storage Block,
971 * two different shaders can access to different elements.
972 * Because of that, they might be converted to different
973 * sized arrays, then check that they are compatible but
974 * ignore the array size.
976 if (!(var
->data
.mode
== ir_var_shader_storage
&&
977 var
->data
.from_ssbo_unsized_array
&&
978 existing
->data
.mode
== ir_var_shader_storage
&&
979 existing
->data
.from_ssbo_unsized_array
&&
980 var
->type
->gl_type
== existing
->type
->gl_type
)) {
981 linker_error(prog
, "%s `%s' declared as type "
982 "`%s' and type `%s'\n",
984 var
->name
, var
->type
->name
,
985 existing
->type
->name
);
992 if (var
->data
.explicit_location
) {
993 if (existing
->data
.explicit_location
994 && (var
->data
.location
!= existing
->data
.location
)) {
995 linker_error(prog
, "explicit locations for %s "
996 "`%s' have differing values\n",
997 mode_string(var
), var
->name
);
1001 existing
->data
.location
= var
->data
.location
;
1002 existing
->data
.explicit_location
= true;
1005 /* From the GLSL 4.20 specification:
1006 * "A link error will result if two compilation units in a program
1007 * specify different integer-constant bindings for the same
1008 * opaque-uniform name. However, it is not an error to specify a
1009 * binding on some but not all declarations for the same name"
1011 if (var
->data
.explicit_binding
) {
1012 if (existing
->data
.explicit_binding
&&
1013 var
->data
.binding
!= existing
->data
.binding
) {
1014 linker_error(prog
, "explicit bindings for %s "
1015 "`%s' have differing values\n",
1016 mode_string(var
), var
->name
);
1020 existing
->data
.binding
= var
->data
.binding
;
1021 existing
->data
.explicit_binding
= true;
1024 if (var
->type
->contains_atomic() &&
1025 var
->data
.atomic
.offset
!= existing
->data
.atomic
.offset
) {
1026 linker_error(prog
, "offset specifications for %s "
1027 "`%s' have differing values\n",
1028 mode_string(var
), var
->name
);
1032 /* Validate layout qualifiers for gl_FragDepth.
1034 * From the AMD/ARB_conservative_depth specs:
1036 * "If gl_FragDepth is redeclared in any fragment shader in a
1037 * program, it must be redeclared in all fragment shaders in
1038 * that program that have static assignments to
1039 * gl_FragDepth. All redeclarations of gl_FragDepth in all
1040 * fragment shaders in a single program must have the same set
1043 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
1044 bool layout_declared
= var
->data
.depth_layout
!= ir_depth_layout_none
;
1045 bool layout_differs
=
1046 var
->data
.depth_layout
!= existing
->data
.depth_layout
;
1048 if (layout_declared
&& layout_differs
) {
1050 "All redeclarations of gl_FragDepth in all "
1051 "fragment shaders in a single program must have "
1052 "the same set of qualifiers.\n");
1055 if (var
->data
.used
&& layout_differs
) {
1057 "If gl_FragDepth is redeclared with a layout "
1058 "qualifier in any fragment shader, it must be "
1059 "redeclared with the same layout qualifier in "
1060 "all fragment shaders that have assignments to "
1065 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1067 * "If a shared global has multiple initializers, the
1068 * initializers must all be constant expressions, and they
1069 * must all have the same value. Otherwise, a link error will
1070 * result. (A shared global having only one initializer does
1071 * not require that initializer to be a constant expression.)"
1073 * Previous to 4.20 the GLSL spec simply said that initializers
1074 * must have the same value. In this case of non-constant
1075 * initializers, this was impossible to determine. As a result,
1076 * no vendor actually implemented that behavior. The 4.20
1077 * behavior matches the implemented behavior of at least one other
1078 * vendor, so we'll implement that for all GLSL versions.
1080 if (var
->constant_initializer
!= NULL
) {
1081 if (existing
->constant_initializer
!= NULL
) {
1082 if (!var
->constant_initializer
->has_value(existing
->constant_initializer
)) {
1083 linker_error(prog
, "initializers for %s "
1084 "`%s' have differing values\n",
1085 mode_string(var
), var
->name
);
1089 /* If the first-seen instance of a particular uniform did not
1090 * have an initializer but a later instance does, copy the
1091 * initializer to the version stored in the symbol table.
1093 /* FINISHME: This is wrong. The constant_value field should
1094 * FINISHME: not be modified! Imagine a case where a shader
1095 * FINISHME: without an initializer is linked in two different
1096 * FINISHME: programs with shaders that have differing
1097 * FINISHME: initializers. Linking with the first will
1098 * FINISHME: modify the shader, and linking with the second
1099 * FINISHME: will fail.
1101 existing
->constant_initializer
=
1102 var
->constant_initializer
->clone(ralloc_parent(existing
),
1107 if (var
->data
.has_initializer
) {
1108 if (existing
->data
.has_initializer
1109 && (var
->constant_initializer
== NULL
1110 || existing
->constant_initializer
== NULL
)) {
1112 "shared global variable `%s' has multiple "
1113 "non-constant initializers.\n",
1118 /* Some instance had an initializer, so keep track of that. In
1119 * this location, all sorts of initializers (constant or
1120 * otherwise) will propagate the existence to the variable
1121 * stored in the symbol table.
1123 existing
->data
.has_initializer
= true;
1126 if (existing
->data
.invariant
!= var
->data
.invariant
) {
1127 linker_error(prog
, "declarations for %s `%s' have "
1128 "mismatching invariant qualifiers\n",
1129 mode_string(var
), var
->name
);
1132 if (existing
->data
.centroid
!= var
->data
.centroid
) {
1133 linker_error(prog
, "declarations for %s `%s' have "
1134 "mismatching centroid qualifiers\n",
1135 mode_string(var
), var
->name
);
1138 if (existing
->data
.sample
!= var
->data
.sample
) {
1139 linker_error(prog
, "declarations for %s `%s` have "
1140 "mismatching sample qualifiers\n",
1141 mode_string(var
), var
->name
);
1145 variables
.add_variable(var
);
1152 * Perform validation of uniforms used across multiple shader stages
1155 cross_validate_uniforms(struct gl_shader_program
*prog
)
1157 cross_validate_globals(prog
, prog
->_LinkedShaders
,
1158 MESA_SHADER_STAGES
, true);
1162 * Accumulates the array of prog->BufferInterfaceBlocks and checks that all
1163 * definitons of blocks agree on their contents.
1166 interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
)
1168 unsigned max_num_uniform_blocks
= 0;
1169 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1170 if (prog
->_LinkedShaders
[i
])
1171 max_num_uniform_blocks
+= prog
->_LinkedShaders
[i
]->NumBufferInterfaceBlocks
;
1174 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1175 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
1177 prog
->UniformBlockStageIndex
[i
] = ralloc_array(prog
, int,
1178 max_num_uniform_blocks
);
1179 for (unsigned int j
= 0; j
< max_num_uniform_blocks
; j
++)
1180 prog
->UniformBlockStageIndex
[i
][j
] = -1;
1185 for (unsigned int j
= 0; j
< sh
->NumBufferInterfaceBlocks
; j
++) {
1186 int index
= link_cross_validate_uniform_block(prog
,
1187 &prog
->BufferInterfaceBlocks
,
1188 &prog
->NumBufferInterfaceBlocks
,
1189 &sh
->BufferInterfaceBlocks
[j
]);
1192 linker_error(prog
, "uniform block `%s' has mismatching definitions\n",
1193 sh
->BufferInterfaceBlocks
[j
].Name
);
1197 prog
->UniformBlockStageIndex
[i
][index
] = j
;
1206 * Populates a shaders symbol table with all global declarations
1209 populate_symbol_table(gl_shader
*sh
)
1211 sh
->symbols
= new(sh
) glsl_symbol_table
;
1213 foreach_in_list(ir_instruction
, inst
, sh
->ir
) {
1217 if ((func
= inst
->as_function()) != NULL
) {
1218 sh
->symbols
->add_function(func
);
1219 } else if ((var
= inst
->as_variable()) != NULL
) {
1220 if (var
->data
.mode
!= ir_var_temporary
)
1221 sh
->symbols
->add_variable(var
);
1228 * Remap variables referenced in an instruction tree
1230 * This is used when instruction trees are cloned from one shader and placed in
1231 * another. These trees will contain references to \c ir_variable nodes that
1232 * do not exist in the target shader. This function finds these \c ir_variable
1233 * references and replaces the references with matching variables in the target
1236 * If there is no matching variable in the target shader, a clone of the
1237 * \c ir_variable is made and added to the target shader. The new variable is
1238 * added to \b both the instruction stream and the symbol table.
1240 * \param inst IR tree that is to be processed.
1241 * \param symbols Symbol table containing global scope symbols in the
1243 * \param instructions Instruction stream where new variable declarations
1247 remap_variables(ir_instruction
*inst
, struct gl_shader
*target
,
1250 class remap_visitor
: public ir_hierarchical_visitor
{
1252 remap_visitor(struct gl_shader
*target
,
1255 this->target
= target
;
1256 this->symbols
= target
->symbols
;
1257 this->instructions
= target
->ir
;
1258 this->temps
= temps
;
1261 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
1263 if (ir
->var
->data
.mode
== ir_var_temporary
) {
1264 ir_variable
*var
= (ir_variable
*) hash_table_find(temps
, ir
->var
);
1266 assert(var
!= NULL
);
1268 return visit_continue
;
1271 ir_variable
*const existing
=
1272 this->symbols
->get_variable(ir
->var
->name
);
1273 if (existing
!= NULL
)
1276 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
1278 this->symbols
->add_variable(copy
);
1279 this->instructions
->push_head(copy
);
1283 return visit_continue
;
1287 struct gl_shader
*target
;
1288 glsl_symbol_table
*symbols
;
1289 exec_list
*instructions
;
1293 remap_visitor
v(target
, temps
);
1300 * Move non-declarations from one instruction stream to another
1302 * The intended usage pattern of this function is to pass the pointer to the
1303 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
1304 * pointer) for \c last and \c false for \c make_copies on the first
1305 * call. Successive calls pass the return value of the previous call for
1306 * \c last and \c true for \c make_copies.
1308 * \param instructions Source instruction stream
1309 * \param last Instruction after which new instructions should be
1310 * inserted in the target instruction stream
1311 * \param make_copies Flag selecting whether instructions in \c instructions
1312 * should be copied (via \c ir_instruction::clone) into the
1313 * target list or moved.
1316 * The new "last" instruction in the target instruction stream. This pointer
1317 * is suitable for use as the \c last parameter of a later call to this
1321 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
1322 bool make_copies
, gl_shader
*target
)
1324 hash_table
*temps
= NULL
;
1327 temps
= hash_table_ctor(0, hash_table_pointer_hash
,
1328 hash_table_pointer_compare
);
1330 foreach_in_list_safe(ir_instruction
, inst
, instructions
) {
1331 if (inst
->as_function())
1334 ir_variable
*var
= inst
->as_variable();
1335 if ((var
!= NULL
) && (var
->data
.mode
!= ir_var_temporary
))
1338 assert(inst
->as_assignment()
1340 || inst
->as_if() /* for initializers with the ?: operator */
1341 || ((var
!= NULL
) && (var
->data
.mode
== ir_var_temporary
)));
1344 inst
= inst
->clone(target
, NULL
);
1347 hash_table_insert(temps
, inst
, var
);
1349 remap_variables(inst
, target
, temps
);
1354 last
->insert_after(inst
);
1359 hash_table_dtor(temps
);
1366 * This class is only used in link_intrastage_shaders() below but declaring
1367 * it inside that function leads to compiler warnings with some versions of
1370 class array_sizing_visitor
: public ir_hierarchical_visitor
{
1372 array_sizing_visitor()
1373 : mem_ctx(ralloc_context(NULL
)),
1374 unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash
,
1375 hash_table_pointer_compare
))
1379 ~array_sizing_visitor()
1381 hash_table_dtor(this->unnamed_interfaces
);
1382 ralloc_free(this->mem_ctx
);
1385 virtual ir_visitor_status
visit(ir_variable
*var
)
1387 const glsl_type
*type_without_array
;
1388 fixup_type(&var
->type
, var
->data
.max_array_access
,
1389 var
->data
.from_ssbo_unsized_array
);
1390 type_without_array
= var
->type
->without_array();
1391 if (var
->type
->is_interface()) {
1392 if (interface_contains_unsized_arrays(var
->type
)) {
1393 const glsl_type
*new_type
=
1394 resize_interface_members(var
->type
,
1395 var
->get_max_ifc_array_access(),
1396 var
->is_in_shader_storage_block());
1397 var
->type
= new_type
;
1398 var
->change_interface_type(new_type
);
1400 } else if (type_without_array
->is_interface()) {
1401 if (interface_contains_unsized_arrays(type_without_array
)) {
1402 const glsl_type
*new_type
=
1403 resize_interface_members(type_without_array
,
1404 var
->get_max_ifc_array_access(),
1405 var
->is_in_shader_storage_block());
1406 var
->change_interface_type(new_type
);
1407 var
->type
= update_interface_members_array(var
->type
, new_type
);
1409 } else if (const glsl_type
*ifc_type
= var
->get_interface_type()) {
1410 /* Store a pointer to the variable in the unnamed_interfaces
1413 ir_variable
**interface_vars
= (ir_variable
**)
1414 hash_table_find(this->unnamed_interfaces
, ifc_type
);
1415 if (interface_vars
== NULL
) {
1416 interface_vars
= rzalloc_array(mem_ctx
, ir_variable
*,
1418 hash_table_insert(this->unnamed_interfaces
, interface_vars
,
1421 unsigned index
= ifc_type
->field_index(var
->name
);
1422 assert(index
< ifc_type
->length
);
1423 assert(interface_vars
[index
] == NULL
);
1424 interface_vars
[index
] = var
;
1426 return visit_continue
;
1430 * For each unnamed interface block that was discovered while running the
1431 * visitor, adjust the interface type to reflect the newly assigned array
1432 * sizes, and fix up the ir_variable nodes to point to the new interface
1435 void fixup_unnamed_interface_types()
1437 hash_table_call_foreach(this->unnamed_interfaces
,
1438 fixup_unnamed_interface_type
, NULL
);
1443 * If the type pointed to by \c type represents an unsized array, replace
1444 * it with a sized array whose size is determined by max_array_access.
1446 static void fixup_type(const glsl_type
**type
, unsigned max_array_access
,
1447 bool from_ssbo_unsized_array
)
1449 if (!from_ssbo_unsized_array
&& (*type
)->is_unsized_array()) {
1450 *type
= glsl_type::get_array_instance((*type
)->fields
.array
,
1451 max_array_access
+ 1);
1452 assert(*type
!= NULL
);
1456 static const glsl_type
*
1457 update_interface_members_array(const glsl_type
*type
,
1458 const glsl_type
*new_interface_type
)
1460 const glsl_type
*element_type
= type
->fields
.array
;
1461 if (element_type
->is_array()) {
1462 const glsl_type
*new_array_type
=
1463 update_interface_members_array(element_type
, new_interface_type
);
1464 return glsl_type::get_array_instance(new_array_type
, type
->length
);
1466 return glsl_type::get_array_instance(new_interface_type
,
1472 * Determine whether the given interface type contains unsized arrays (if
1473 * it doesn't, array_sizing_visitor doesn't need to process it).
1475 static bool interface_contains_unsized_arrays(const glsl_type
*type
)
1477 for (unsigned i
= 0; i
< type
->length
; i
++) {
1478 const glsl_type
*elem_type
= type
->fields
.structure
[i
].type
;
1479 if (elem_type
->is_unsized_array())
1486 * Create a new interface type based on the given type, with unsized arrays
1487 * replaced by sized arrays whose size is determined by
1488 * max_ifc_array_access.
1490 static const glsl_type
*
1491 resize_interface_members(const glsl_type
*type
,
1492 const unsigned *max_ifc_array_access
,
1495 unsigned num_fields
= type
->length
;
1496 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1497 memcpy(fields
, type
->fields
.structure
,
1498 num_fields
* sizeof(*fields
));
1499 for (unsigned i
= 0; i
< num_fields
; i
++) {
1500 /* If SSBO last member is unsized array, we don't replace it by a sized
1503 if (is_ssbo
&& i
== (num_fields
- 1))
1504 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1507 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1510 glsl_interface_packing packing
=
1511 (glsl_interface_packing
) type
->interface_packing
;
1512 const glsl_type
*new_ifc_type
=
1513 glsl_type::get_interface_instance(fields
, num_fields
,
1514 packing
, type
->name
);
1516 return new_ifc_type
;
1519 static void fixup_unnamed_interface_type(const void *key
, void *data
,
1522 const glsl_type
*ifc_type
= (const glsl_type
*) key
;
1523 ir_variable
**interface_vars
= (ir_variable
**) data
;
1524 unsigned num_fields
= ifc_type
->length
;
1525 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1526 memcpy(fields
, ifc_type
->fields
.structure
,
1527 num_fields
* sizeof(*fields
));
1528 bool interface_type_changed
= false;
1529 for (unsigned i
= 0; i
< num_fields
; i
++) {
1530 if (interface_vars
[i
] != NULL
&&
1531 fields
[i
].type
!= interface_vars
[i
]->type
) {
1532 fields
[i
].type
= interface_vars
[i
]->type
;
1533 interface_type_changed
= true;
1536 if (!interface_type_changed
) {
1540 glsl_interface_packing packing
=
1541 (glsl_interface_packing
) ifc_type
->interface_packing
;
1542 const glsl_type
*new_ifc_type
=
1543 glsl_type::get_interface_instance(fields
, num_fields
, packing
,
1546 for (unsigned i
= 0; i
< num_fields
; i
++) {
1547 if (interface_vars
[i
] != NULL
)
1548 interface_vars
[i
]->change_interface_type(new_ifc_type
);
1553 * Memory context used to allocate the data in \c unnamed_interfaces.
1558 * Hash table from const glsl_type * to an array of ir_variable *'s
1559 * pointing to the ir_variables constituting each unnamed interface block.
1561 hash_table
*unnamed_interfaces
;
1566 * Performs the cross-validation of tessellation control shader vertices and
1567 * layout qualifiers for the attached tessellation control shaders,
1568 * and propagates them to the linked TCS and linked shader program.
1571 link_tcs_out_layout_qualifiers(struct gl_shader_program
*prog
,
1572 struct gl_shader
*linked_shader
,
1573 struct gl_shader
**shader_list
,
1574 unsigned num_shaders
)
1576 linked_shader
->TessCtrl
.VerticesOut
= 0;
1578 if (linked_shader
->Stage
!= MESA_SHADER_TESS_CTRL
)
1581 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1583 * "All tessellation control shader layout declarations in a program
1584 * must specify the same output patch vertex count. There must be at
1585 * least one layout qualifier specifying an output patch vertex count
1586 * in any program containing tessellation control shaders; however,
1587 * such a declaration is not required in all tessellation control
1591 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1592 struct gl_shader
*shader
= shader_list
[i
];
1594 if (shader
->TessCtrl
.VerticesOut
!= 0) {
1595 if (linked_shader
->TessCtrl
.VerticesOut
!= 0 &&
1596 linked_shader
->TessCtrl
.VerticesOut
!= shader
->TessCtrl
.VerticesOut
) {
1597 linker_error(prog
, "tessellation control shader defined with "
1598 "conflicting output vertex count (%d and %d)\n",
1599 linked_shader
->TessCtrl
.VerticesOut
,
1600 shader
->TessCtrl
.VerticesOut
);
1603 linked_shader
->TessCtrl
.VerticesOut
= shader
->TessCtrl
.VerticesOut
;
1607 /* Just do the intrastage -> interstage propagation right now,
1608 * since we already know we're in the right type of shader program
1611 if (linked_shader
->TessCtrl
.VerticesOut
== 0) {
1612 linker_error(prog
, "tessellation control shader didn't declare "
1613 "vertices out layout qualifier\n");
1616 prog
->TessCtrl
.VerticesOut
= linked_shader
->TessCtrl
.VerticesOut
;
1621 * Performs the cross-validation of tessellation evaluation shader
1622 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1623 * for the attached tessellation evaluation shaders, and propagates them
1624 * to the linked TES and linked shader program.
1627 link_tes_in_layout_qualifiers(struct gl_shader_program
*prog
,
1628 struct gl_shader
*linked_shader
,
1629 struct gl_shader
**shader_list
,
1630 unsigned num_shaders
)
1632 linked_shader
->TessEval
.PrimitiveMode
= PRIM_UNKNOWN
;
1633 linked_shader
->TessEval
.Spacing
= 0;
1634 linked_shader
->TessEval
.VertexOrder
= 0;
1635 linked_shader
->TessEval
.PointMode
= -1;
1637 if (linked_shader
->Stage
!= MESA_SHADER_TESS_EVAL
)
1640 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1642 * "At least one tessellation evaluation shader (compilation unit) in
1643 * a program must declare a primitive mode in its input layout.
1644 * Declaration vertex spacing, ordering, and point mode identifiers is
1645 * optional. It is not required that all tessellation evaluation
1646 * shaders in a program declare a primitive mode. If spacing or
1647 * vertex ordering declarations are omitted, the tessellation
1648 * primitive generator will use equal spacing or counter-clockwise
1649 * vertex ordering, respectively. If a point mode declaration is
1650 * omitted, the tessellation primitive generator will produce lines or
1651 * triangles according to the primitive mode."
1654 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1655 struct gl_shader
*shader
= shader_list
[i
];
1657 if (shader
->TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
) {
1658 if (linked_shader
->TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
&&
1659 linked_shader
->TessEval
.PrimitiveMode
!= shader
->TessEval
.PrimitiveMode
) {
1660 linker_error(prog
, "tessellation evaluation shader defined with "
1661 "conflicting input primitive modes.\n");
1664 linked_shader
->TessEval
.PrimitiveMode
= shader
->TessEval
.PrimitiveMode
;
1667 if (shader
->TessEval
.Spacing
!= 0) {
1668 if (linked_shader
->TessEval
.Spacing
!= 0 &&
1669 linked_shader
->TessEval
.Spacing
!= shader
->TessEval
.Spacing
) {
1670 linker_error(prog
, "tessellation evaluation shader defined with "
1671 "conflicting vertex spacing.\n");
1674 linked_shader
->TessEval
.Spacing
= shader
->TessEval
.Spacing
;
1677 if (shader
->TessEval
.VertexOrder
!= 0) {
1678 if (linked_shader
->TessEval
.VertexOrder
!= 0 &&
1679 linked_shader
->TessEval
.VertexOrder
!= shader
->TessEval
.VertexOrder
) {
1680 linker_error(prog
, "tessellation evaluation shader defined with "
1681 "conflicting ordering.\n");
1684 linked_shader
->TessEval
.VertexOrder
= shader
->TessEval
.VertexOrder
;
1687 if (shader
->TessEval
.PointMode
!= -1) {
1688 if (linked_shader
->TessEval
.PointMode
!= -1 &&
1689 linked_shader
->TessEval
.PointMode
!= shader
->TessEval
.PointMode
) {
1690 linker_error(prog
, "tessellation evaluation shader defined with "
1691 "conflicting point modes.\n");
1694 linked_shader
->TessEval
.PointMode
= shader
->TessEval
.PointMode
;
1699 /* Just do the intrastage -> interstage propagation right now,
1700 * since we already know we're in the right type of shader program
1703 if (linked_shader
->TessEval
.PrimitiveMode
== PRIM_UNKNOWN
) {
1705 "tessellation evaluation shader didn't declare input "
1706 "primitive modes.\n");
1709 prog
->TessEval
.PrimitiveMode
= linked_shader
->TessEval
.PrimitiveMode
;
1711 if (linked_shader
->TessEval
.Spacing
== 0)
1712 linked_shader
->TessEval
.Spacing
= GL_EQUAL
;
1713 prog
->TessEval
.Spacing
= linked_shader
->TessEval
.Spacing
;
1715 if (linked_shader
->TessEval
.VertexOrder
== 0)
1716 linked_shader
->TessEval
.VertexOrder
= GL_CCW
;
1717 prog
->TessEval
.VertexOrder
= linked_shader
->TessEval
.VertexOrder
;
1719 if (linked_shader
->TessEval
.PointMode
== -1)
1720 linked_shader
->TessEval
.PointMode
= GL_FALSE
;
1721 prog
->TessEval
.PointMode
= linked_shader
->TessEval
.PointMode
;
1726 * Performs the cross-validation of layout qualifiers specified in
1727 * redeclaration of gl_FragCoord for the attached fragment shaders,
1728 * and propagates them to the linked FS and linked shader program.
1731 link_fs_input_layout_qualifiers(struct gl_shader_program
*prog
,
1732 struct gl_shader
*linked_shader
,
1733 struct gl_shader
**shader_list
,
1734 unsigned num_shaders
)
1736 linked_shader
->redeclares_gl_fragcoord
= false;
1737 linked_shader
->uses_gl_fragcoord
= false;
1738 linked_shader
->origin_upper_left
= false;
1739 linked_shader
->pixel_center_integer
= false;
1741 if (linked_shader
->Stage
!= MESA_SHADER_FRAGMENT
||
1742 (prog
->Version
< 150 && !prog
->ARB_fragment_coord_conventions_enable
))
1745 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1746 struct gl_shader
*shader
= shader_list
[i
];
1747 /* From the GLSL 1.50 spec, page 39:
1749 * "If gl_FragCoord is redeclared in any fragment shader in a program,
1750 * it must be redeclared in all the fragment shaders in that program
1751 * that have a static use gl_FragCoord."
1753 if ((linked_shader
->redeclares_gl_fragcoord
1754 && !shader
->redeclares_gl_fragcoord
1755 && shader
->uses_gl_fragcoord
)
1756 || (shader
->redeclares_gl_fragcoord
1757 && !linked_shader
->redeclares_gl_fragcoord
1758 && linked_shader
->uses_gl_fragcoord
)) {
1759 linker_error(prog
, "fragment shader defined with conflicting "
1760 "layout qualifiers for gl_FragCoord\n");
1763 /* From the GLSL 1.50 spec, page 39:
1765 * "All redeclarations of gl_FragCoord in all fragment shaders in a
1766 * single program must have the same set of qualifiers."
1768 if (linked_shader
->redeclares_gl_fragcoord
&& shader
->redeclares_gl_fragcoord
1769 && (shader
->origin_upper_left
!= linked_shader
->origin_upper_left
1770 || shader
->pixel_center_integer
!= linked_shader
->pixel_center_integer
)) {
1771 linker_error(prog
, "fragment shader defined with conflicting "
1772 "layout qualifiers for gl_FragCoord\n");
1775 /* Update the linked shader state. Note that uses_gl_fragcoord should
1776 * accumulate the results. The other values should replace. If there
1777 * are multiple redeclarations, all the fields except uses_gl_fragcoord
1778 * are already known to be the same.
1780 if (shader
->redeclares_gl_fragcoord
|| shader
->uses_gl_fragcoord
) {
1781 linked_shader
->redeclares_gl_fragcoord
=
1782 shader
->redeclares_gl_fragcoord
;
1783 linked_shader
->uses_gl_fragcoord
= linked_shader
->uses_gl_fragcoord
1784 || shader
->uses_gl_fragcoord
;
1785 linked_shader
->origin_upper_left
= shader
->origin_upper_left
;
1786 linked_shader
->pixel_center_integer
= shader
->pixel_center_integer
;
1789 linked_shader
->EarlyFragmentTests
|= shader
->EarlyFragmentTests
;
1794 * Performs the cross-validation of geometry shader max_vertices and
1795 * primitive type layout qualifiers for the attached geometry shaders,
1796 * and propagates them to the linked GS and linked shader program.
1799 link_gs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
1800 struct gl_shader
*linked_shader
,
1801 struct gl_shader
**shader_list
,
1802 unsigned num_shaders
)
1804 linked_shader
->Geom
.VerticesOut
= 0;
1805 linked_shader
->Geom
.Invocations
= 0;
1806 linked_shader
->Geom
.InputType
= PRIM_UNKNOWN
;
1807 linked_shader
->Geom
.OutputType
= PRIM_UNKNOWN
;
1809 /* No in/out qualifiers defined for anything but GLSL 1.50+
1810 * geometry shaders so far.
1812 if (linked_shader
->Stage
!= MESA_SHADER_GEOMETRY
|| prog
->Version
< 150)
1815 /* From the GLSL 1.50 spec, page 46:
1817 * "All geometry shader output layout declarations in a program
1818 * must declare the same layout and same value for
1819 * max_vertices. There must be at least one geometry output
1820 * layout declaration somewhere in a program, but not all
1821 * geometry shaders (compilation units) are required to
1825 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1826 struct gl_shader
*shader
= shader_list
[i
];
1828 if (shader
->Geom
.InputType
!= PRIM_UNKNOWN
) {
1829 if (linked_shader
->Geom
.InputType
!= PRIM_UNKNOWN
&&
1830 linked_shader
->Geom
.InputType
!= shader
->Geom
.InputType
) {
1831 linker_error(prog
, "geometry shader defined with conflicting "
1835 linked_shader
->Geom
.InputType
= shader
->Geom
.InputType
;
1838 if (shader
->Geom
.OutputType
!= PRIM_UNKNOWN
) {
1839 if (linked_shader
->Geom
.OutputType
!= PRIM_UNKNOWN
&&
1840 linked_shader
->Geom
.OutputType
!= shader
->Geom
.OutputType
) {
1841 linker_error(prog
, "geometry shader defined with conflicting "
1845 linked_shader
->Geom
.OutputType
= shader
->Geom
.OutputType
;
1848 if (shader
->Geom
.VerticesOut
!= 0) {
1849 if (linked_shader
->Geom
.VerticesOut
!= 0 &&
1850 linked_shader
->Geom
.VerticesOut
!= shader
->Geom
.VerticesOut
) {
1851 linker_error(prog
, "geometry shader defined with conflicting "
1852 "output vertex count (%d and %d)\n",
1853 linked_shader
->Geom
.VerticesOut
,
1854 shader
->Geom
.VerticesOut
);
1857 linked_shader
->Geom
.VerticesOut
= shader
->Geom
.VerticesOut
;
1860 if (shader
->Geom
.Invocations
!= 0) {
1861 if (linked_shader
->Geom
.Invocations
!= 0 &&
1862 linked_shader
->Geom
.Invocations
!= shader
->Geom
.Invocations
) {
1863 linker_error(prog
, "geometry shader defined with conflicting "
1864 "invocation count (%d and %d)\n",
1865 linked_shader
->Geom
.Invocations
,
1866 shader
->Geom
.Invocations
);
1869 linked_shader
->Geom
.Invocations
= shader
->Geom
.Invocations
;
1873 /* Just do the intrastage -> interstage propagation right now,
1874 * since we already know we're in the right type of shader program
1877 if (linked_shader
->Geom
.InputType
== PRIM_UNKNOWN
) {
1879 "geometry shader didn't declare primitive input type\n");
1882 prog
->Geom
.InputType
= linked_shader
->Geom
.InputType
;
1884 if (linked_shader
->Geom
.OutputType
== PRIM_UNKNOWN
) {
1886 "geometry shader didn't declare primitive output type\n");
1889 prog
->Geom
.OutputType
= linked_shader
->Geom
.OutputType
;
1891 if (linked_shader
->Geom
.VerticesOut
== 0) {
1893 "geometry shader didn't declare max_vertices\n");
1896 prog
->Geom
.VerticesOut
= linked_shader
->Geom
.VerticesOut
;
1898 if (linked_shader
->Geom
.Invocations
== 0)
1899 linked_shader
->Geom
.Invocations
= 1;
1901 prog
->Geom
.Invocations
= linked_shader
->Geom
.Invocations
;
1906 * Perform cross-validation of compute shader local_size_{x,y,z} layout
1907 * qualifiers for the attached compute shaders, and propagate them to the
1908 * linked CS and linked shader program.
1911 link_cs_input_layout_qualifiers(struct gl_shader_program
*prog
,
1912 struct gl_shader
*linked_shader
,
1913 struct gl_shader
**shader_list
,
1914 unsigned num_shaders
)
1916 for (int i
= 0; i
< 3; i
++)
1917 linked_shader
->Comp
.LocalSize
[i
] = 0;
1919 /* This function is called for all shader stages, but it only has an effect
1920 * for compute shaders.
1922 if (linked_shader
->Stage
!= MESA_SHADER_COMPUTE
)
1925 /* From the ARB_compute_shader spec, in the section describing local size
1928 * If multiple compute shaders attached to a single program object
1929 * declare local work-group size, the declarations must be identical;
1930 * otherwise a link-time error results. Furthermore, if a program
1931 * object contains any compute shaders, at least one must contain an
1932 * input layout qualifier specifying the local work sizes of the
1933 * program, or a link-time error will occur.
1935 for (unsigned sh
= 0; sh
< num_shaders
; sh
++) {
1936 struct gl_shader
*shader
= shader_list
[sh
];
1938 if (shader
->Comp
.LocalSize
[0] != 0) {
1939 if (linked_shader
->Comp
.LocalSize
[0] != 0) {
1940 for (int i
= 0; i
< 3; i
++) {
1941 if (linked_shader
->Comp
.LocalSize
[i
] !=
1942 shader
->Comp
.LocalSize
[i
]) {
1943 linker_error(prog
, "compute shader defined with conflicting "
1949 for (int i
= 0; i
< 3; i
++)
1950 linked_shader
->Comp
.LocalSize
[i
] = shader
->Comp
.LocalSize
[i
];
1954 /* Just do the intrastage -> interstage propagation right now,
1955 * since we already know we're in the right type of shader program
1958 if (linked_shader
->Comp
.LocalSize
[0] == 0) {
1959 linker_error(prog
, "compute shader didn't declare local size\n");
1962 for (int i
= 0; i
< 3; i
++)
1963 prog
->Comp
.LocalSize
[i
] = linked_shader
->Comp
.LocalSize
[i
];
1968 * Combine a group of shaders for a single stage to generate a linked shader
1971 * If this function is supplied a single shader, it is cloned, and the new
1972 * shader is returned.
1974 static struct gl_shader
*
1975 link_intrastage_shaders(void *mem_ctx
,
1976 struct gl_context
*ctx
,
1977 struct gl_shader_program
*prog
,
1978 struct gl_shader
**shader_list
,
1979 unsigned num_shaders
)
1981 struct gl_uniform_block
*uniform_blocks
= NULL
;
1983 /* Check that global variables defined in multiple shaders are consistent.
1985 cross_validate_globals(prog
, shader_list
, num_shaders
, false);
1986 if (!prog
->LinkStatus
)
1989 /* Check that interface blocks defined in multiple shaders are consistent.
1991 validate_intrastage_interface_blocks(prog
, (const gl_shader
**)shader_list
,
1993 if (!prog
->LinkStatus
)
1996 /* Link up uniform blocks defined within this stage. */
1997 const unsigned num_uniform_blocks
=
1998 link_uniform_blocks(mem_ctx
, ctx
, prog
, shader_list
, num_shaders
,
2000 if (!prog
->LinkStatus
)
2003 /* Check that there is only a single definition of each function signature
2004 * across all shaders.
2006 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
2007 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
2008 ir_function
*const f
= node
->as_function();
2013 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
2014 ir_function
*const other
=
2015 shader_list
[j
]->symbols
->get_function(f
->name
);
2017 /* If the other shader has no function (and therefore no function
2018 * signatures) with the same name, skip to the next shader.
2023 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
2024 if (!sig
->is_defined
|| sig
->is_builtin())
2027 ir_function_signature
*other_sig
=
2028 other
->exact_matching_signature(NULL
, &sig
->parameters
);
2030 if ((other_sig
!= NULL
) && other_sig
->is_defined
2031 && !other_sig
->is_builtin()) {
2032 linker_error(prog
, "function `%s' is multiply defined\n",
2041 /* Find the shader that defines main, and make a clone of it.
2043 * Starting with the clone, search for undefined references. If one is
2044 * found, find the shader that defines it. Clone the reference and add
2045 * it to the shader. Repeat until there are no undefined references or
2046 * until a reference cannot be resolved.
2048 gl_shader
*main
= NULL
;
2049 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2050 if (_mesa_get_main_function_signature(shader_list
[i
]) != NULL
) {
2051 main
= shader_list
[i
];
2057 linker_error(prog
, "%s shader lacks `main'\n",
2058 _mesa_shader_stage_to_string(shader_list
[0]->Stage
));
2062 gl_shader
*linked
= ctx
->Driver
.NewShader(NULL
, 0, main
->Type
);
2063 linked
->ir
= new(linked
) exec_list
;
2064 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
2066 linked
->BufferInterfaceBlocks
= uniform_blocks
;
2067 linked
->NumBufferInterfaceBlocks
= num_uniform_blocks
;
2068 ralloc_steal(linked
, linked
->BufferInterfaceBlocks
);
2070 link_fs_input_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2071 link_tcs_out_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2072 link_tes_in_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2073 link_gs_inout_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2074 link_cs_input_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2076 populate_symbol_table(linked
);
2078 /* The pointer to the main function in the final linked shader (i.e., the
2079 * copy of the original shader that contained the main function).
2081 ir_function_signature
*const main_sig
=
2082 _mesa_get_main_function_signature(linked
);
2084 /* Move any instructions other than variable declarations or function
2085 * declarations into main.
2087 exec_node
*insertion_point
=
2088 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
2091 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2092 if (shader_list
[i
] == main
)
2095 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
2096 insertion_point
, true, linked
);
2099 /* Check if any shader needs built-in functions. */
2100 bool need_builtins
= false;
2101 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2102 if (shader_list
[i
]->uses_builtin_functions
) {
2103 need_builtins
= true;
2109 if (need_builtins
) {
2110 /* Make a temporary array one larger than shader_list, which will hold
2111 * the built-in function shader as well.
2113 gl_shader
**linking_shaders
= (gl_shader
**)
2114 calloc(num_shaders
+ 1, sizeof(gl_shader
*));
2116 ok
= linking_shaders
!= NULL
;
2119 memcpy(linking_shaders
, shader_list
, num_shaders
* sizeof(gl_shader
*));
2120 linking_shaders
[num_shaders
] = _mesa_glsl_get_builtin_function_shader();
2122 ok
= link_function_calls(prog
, linked
, linking_shaders
, num_shaders
+ 1);
2124 free(linking_shaders
);
2126 _mesa_error_no_memory(__func__
);
2129 ok
= link_function_calls(prog
, linked
, shader_list
, num_shaders
);
2134 _mesa_delete_shader(ctx
, linked
);
2138 /* At this point linked should contain all of the linked IR, so
2139 * validate it to make sure nothing went wrong.
2141 validate_ir_tree(linked
->ir
);
2143 /* Set the size of geometry shader input arrays */
2144 if (linked
->Stage
== MESA_SHADER_GEOMETRY
) {
2145 unsigned num_vertices
= vertices_per_prim(prog
->Geom
.InputType
);
2146 geom_array_resize_visitor
input_resize_visitor(num_vertices
, prog
);
2147 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2148 ir
->accept(&input_resize_visitor
);
2152 if (ctx
->Const
.VertexID_is_zero_based
)
2153 lower_vertex_id(linked
);
2155 /* Validate correct usage of barrier() in the tess control shader */
2156 if (linked
->Stage
== MESA_SHADER_TESS_CTRL
) {
2157 barrier_use_visitor
visitor(prog
);
2158 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2159 ir
->accept(&visitor
);
2163 /* Make a pass over all variable declarations to ensure that arrays with
2164 * unspecified sizes have a size specified. The size is inferred from the
2165 * max_array_access field.
2167 array_sizing_visitor v
;
2169 v
.fixup_unnamed_interface_types();
2175 * Update the sizes of linked shader uniform arrays to the maximum
2178 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
2180 * If one or more elements of an array are active,
2181 * GetActiveUniform will return the name of the array in name,
2182 * subject to the restrictions listed above. The type of the array
2183 * is returned in type. The size parameter contains the highest
2184 * array element index used, plus one. The compiler or linker
2185 * determines the highest index used. There will be only one
2186 * active uniform reported by the GL per uniform array.
2190 update_array_sizes(struct gl_shader_program
*prog
)
2192 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2193 if (prog
->_LinkedShaders
[i
] == NULL
)
2196 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
2197 ir_variable
*const var
= node
->as_variable();
2199 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
) ||
2200 !var
->type
->is_array())
2203 /* GL_ARB_uniform_buffer_object says that std140 uniforms
2204 * will not be eliminated. Since we always do std140, just
2205 * don't resize arrays in UBOs.
2207 * Atomic counters are supposed to get deterministic
2208 * locations assigned based on the declaration ordering and
2209 * sizes, array compaction would mess that up.
2211 * Subroutine uniforms are not removed.
2213 if (var
->is_in_buffer_block() || var
->type
->contains_atomic() ||
2214 var
->type
->contains_subroutine())
2217 unsigned int size
= var
->data
.max_array_access
;
2218 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
2219 if (prog
->_LinkedShaders
[j
] == NULL
)
2222 foreach_in_list(ir_instruction
, node2
, prog
->_LinkedShaders
[j
]->ir
) {
2223 ir_variable
*other_var
= node2
->as_variable();
2227 if (strcmp(var
->name
, other_var
->name
) == 0 &&
2228 other_var
->data
.max_array_access
> size
) {
2229 size
= other_var
->data
.max_array_access
;
2234 if (size
+ 1 != var
->type
->length
) {
2235 /* If this is a built-in uniform (i.e., it's backed by some
2236 * fixed-function state), adjust the number of state slots to
2237 * match the new array size. The number of slots per array entry
2238 * is not known. It seems safe to assume that the total number of
2239 * slots is an integer multiple of the number of array elements.
2240 * Determine the number of slots per array element by dividing by
2241 * the old (total) size.
2243 const unsigned num_slots
= var
->get_num_state_slots();
2244 if (num_slots
> 0) {
2245 var
->set_num_state_slots((size
+ 1)
2246 * (num_slots
/ var
->type
->length
));
2249 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2251 /* FINISHME: We should update the types of array
2252 * dereferences of this variable now.
2260 * Resize tessellation evaluation per-vertex inputs to the size of
2261 * tessellation control per-vertex outputs.
2264 resize_tes_inputs(struct gl_context
*ctx
,
2265 struct gl_shader_program
*prog
)
2267 if (prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] == NULL
)
2270 gl_shader
*const tcs
= prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
];
2271 gl_shader
*const tes
= prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
2273 /* If no control shader is present, then the TES inputs are statically
2274 * sized to MaxPatchVertices; the actual size of the arrays won't be
2275 * known until draw time.
2277 const int num_vertices
= tcs
2278 ? tcs
->TessCtrl
.VerticesOut
2279 : ctx
->Const
.MaxPatchVertices
;
2281 tess_eval_array_resize_visitor
input_resize_visitor(num_vertices
, prog
);
2282 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2283 ir
->accept(&input_resize_visitor
);
2288 * Find a contiguous set of available bits in a bitmask.
2290 * \param used_mask Bits representing used (1) and unused (0) locations
2291 * \param needed_count Number of contiguous bits needed.
2294 * Base location of the available bits on success or -1 on failure.
2297 find_available_slots(unsigned used_mask
, unsigned needed_count
)
2299 unsigned needed_mask
= (1 << needed_count
) - 1;
2300 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
2302 /* The comparison to 32 is redundant, but without it GCC emits "warning:
2303 * cannot optimize possibly infinite loops" for the loop below.
2305 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
2308 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
2309 if ((needed_mask
& ~used_mask
) == needed_mask
)
2320 * Assign locations for either VS inputs or FS outputs
2322 * \param prog Shader program whose variables need locations assigned
2323 * \param constants Driver specific constant values for the program.
2324 * \param target_index Selector for the program target to receive location
2325 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
2326 * \c MESA_SHADER_FRAGMENT.
2329 * If locations are successfully assigned, true is returned. Otherwise an
2330 * error is emitted to the shader link log and false is returned.
2333 assign_attribute_or_color_locations(gl_shader_program
*prog
,
2334 struct gl_constants
*constants
,
2335 unsigned target_index
)
2337 /* Maximum number of generic locations. This corresponds to either the
2338 * maximum number of draw buffers or the maximum number of generic
2341 unsigned max_index
= (target_index
== MESA_SHADER_VERTEX
) ?
2342 constants
->Program
[target_index
].MaxAttribs
:
2343 MAX2(constants
->MaxDrawBuffers
, constants
->MaxDualSourceDrawBuffers
);
2345 /* Mark invalid locations as being used.
2347 unsigned used_locations
= (max_index
>= 32)
2348 ? ~0 : ~((1 << max_index
) - 1);
2349 unsigned double_storage_locations
= 0;
2351 assert((target_index
== MESA_SHADER_VERTEX
)
2352 || (target_index
== MESA_SHADER_FRAGMENT
));
2354 gl_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
2358 /* Operate in a total of four passes.
2360 * 1. Invalidate the location assignments for all vertex shader inputs.
2362 * 2. Assign locations for inputs that have user-defined (via
2363 * glBindVertexAttribLocation) locations and outputs that have
2364 * user-defined locations (via glBindFragDataLocation).
2366 * 3. Sort the attributes without assigned locations by number of slots
2367 * required in decreasing order. Fragmentation caused by attribute
2368 * locations assigned by the application may prevent large attributes
2369 * from having enough contiguous space.
2371 * 4. Assign locations to any inputs without assigned locations.
2374 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
2375 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
2377 const enum ir_variable_mode direction
=
2378 (target_index
== MESA_SHADER_VERTEX
)
2379 ? ir_var_shader_in
: ir_var_shader_out
;
2382 /* Temporary storage for the set of attributes that need locations assigned.
2388 /* Used below in the call to qsort. */
2389 static int compare(const void *a
, const void *b
)
2391 const temp_attr
*const l
= (const temp_attr
*) a
;
2392 const temp_attr
*const r
= (const temp_attr
*) b
;
2394 /* Reversed because we want a descending order sort below. */
2395 return r
->slots
- l
->slots
;
2399 unsigned num_attr
= 0;
2401 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2402 ir_variable
*const var
= node
->as_variable();
2404 if ((var
== NULL
) || (var
->data
.mode
!= (unsigned) direction
))
2407 if (var
->data
.explicit_location
) {
2408 if ((var
->data
.location
>= (int)(max_index
+ generic_base
))
2409 || (var
->data
.location
< 0)) {
2411 "invalid explicit location %d specified for `%s'\n",
2412 (var
->data
.location
< 0)
2413 ? var
->data
.location
2414 : var
->data
.location
- generic_base
,
2418 } else if (target_index
== MESA_SHADER_VERTEX
) {
2421 if (prog
->AttributeBindings
->get(binding
, var
->name
)) {
2422 assert(binding
>= VERT_ATTRIB_GENERIC0
);
2423 var
->data
.location
= binding
;
2424 var
->data
.is_unmatched_generic_inout
= 0;
2426 } else if (target_index
== MESA_SHADER_FRAGMENT
) {
2430 if (prog
->FragDataBindings
->get(binding
, var
->name
)) {
2431 assert(binding
>= FRAG_RESULT_DATA0
);
2432 var
->data
.location
= binding
;
2433 var
->data
.is_unmatched_generic_inout
= 0;
2435 if (prog
->FragDataIndexBindings
->get(index
, var
->name
)) {
2436 var
->data
.index
= index
;
2441 /* From GL4.5 core spec, section 15.2 (Shader Execution):
2443 * "Output binding assignments will cause LinkProgram to fail:
2445 * If the program has an active output assigned to a location greater
2446 * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
2447 * an active output assigned an index greater than or equal to one;"
2449 if (target_index
== MESA_SHADER_FRAGMENT
&& var
->data
.index
>= 1 &&
2450 var
->data
.location
- generic_base
>=
2451 (int) constants
->MaxDualSourceDrawBuffers
) {
2453 "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
2454 "with index %u for %s\n",
2455 var
->data
.location
- generic_base
, var
->data
.index
,
2460 const unsigned slots
= var
->type
->count_attribute_slots();
2462 /* If the variable is not a built-in and has a location statically
2463 * assigned in the shader (presumably via a layout qualifier), make sure
2464 * that it doesn't collide with other assigned locations. Otherwise,
2465 * add it to the list of variables that need linker-assigned locations.
2467 if (var
->data
.location
!= -1) {
2468 if (var
->data
.location
>= generic_base
&& var
->data
.index
< 1) {
2469 /* From page 61 of the OpenGL 4.0 spec:
2471 * "LinkProgram will fail if the attribute bindings assigned
2472 * by BindAttribLocation do not leave not enough space to
2473 * assign a location for an active matrix attribute or an
2474 * active attribute array, both of which require multiple
2475 * contiguous generic attributes."
2477 * I think above text prohibits the aliasing of explicit and
2478 * automatic assignments. But, aliasing is allowed in manual
2479 * assignments of attribute locations. See below comments for
2482 * From OpenGL 4.0 spec, page 61:
2484 * "It is possible for an application to bind more than one
2485 * attribute name to the same location. This is referred to as
2486 * aliasing. This will only work if only one of the aliased
2487 * attributes is active in the executable program, or if no
2488 * path through the shader consumes more than one attribute of
2489 * a set of attributes aliased to the same location. A link
2490 * error can occur if the linker determines that every path
2491 * through the shader consumes multiple aliased attributes,
2492 * but implementations are not required to generate an error
2495 * From GLSL 4.30 spec, page 54:
2497 * "A program will fail to link if any two non-vertex shader
2498 * input variables are assigned to the same location. For
2499 * vertex shaders, multiple input variables may be assigned
2500 * to the same location using either layout qualifiers or via
2501 * the OpenGL API. However, such aliasing is intended only to
2502 * support vertex shaders where each execution path accesses
2503 * at most one input per each location. Implementations are
2504 * permitted, but not required, to generate link-time errors
2505 * if they detect that every path through the vertex shader
2506 * executable accesses multiple inputs assigned to any single
2507 * location. For all shader types, a program will fail to link
2508 * if explicit location assignments leave the linker unable
2509 * to find space for other variables without explicit
2512 * From OpenGL ES 3.0 spec, page 56:
2514 * "Binding more than one attribute name to the same location
2515 * is referred to as aliasing, and is not permitted in OpenGL
2516 * ES Shading Language 3.00 vertex shaders. LinkProgram will
2517 * fail when this condition exists. However, aliasing is
2518 * possible in OpenGL ES Shading Language 1.00 vertex shaders.
2519 * This will only work if only one of the aliased attributes
2520 * is active in the executable program, or if no path through
2521 * the shader consumes more than one attribute of a set of
2522 * attributes aliased to the same location. A link error can
2523 * occur if the linker determines that every path through the
2524 * shader consumes multiple aliased attributes, but implemen-
2525 * tations are not required to generate an error in this case."
2527 * After looking at above references from OpenGL, OpenGL ES and
2528 * GLSL specifications, we allow aliasing of vertex input variables
2529 * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
2531 * NOTE: This is not required by the spec but its worth mentioning
2532 * here that we're not doing anything to make sure that no path
2533 * through the vertex shader executable accesses multiple inputs
2534 * assigned to any single location.
2537 /* Mask representing the contiguous slots that will be used by
2540 const unsigned attr
= var
->data
.location
- generic_base
;
2541 const unsigned use_mask
= (1 << slots
) - 1;
2542 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
2543 ? "vertex shader input" : "fragment shader output";
2545 /* Generate a link error if the requested locations for this
2546 * attribute exceed the maximum allowed attribute location.
2548 if (attr
+ slots
> max_index
) {
2550 "insufficient contiguous locations "
2551 "available for %s `%s' %d %d %d\n", string
,
2552 var
->name
, used_locations
, use_mask
, attr
);
2556 /* Generate a link error if the set of bits requested for this
2557 * attribute overlaps any previously allocated bits.
2559 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
2560 if (target_index
== MESA_SHADER_FRAGMENT
||
2561 (prog
->IsES
&& prog
->Version
>= 300)) {
2563 "overlapping location is assigned "
2564 "to %s `%s' %d %d %d\n", string
,
2565 var
->name
, used_locations
, use_mask
, attr
);
2568 linker_warning(prog
,
2569 "overlapping location is assigned "
2570 "to %s `%s' %d %d %d\n", string
,
2571 var
->name
, used_locations
, use_mask
, attr
);
2575 used_locations
|= (use_mask
<< attr
);
2577 /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
2579 * "A program with more than the value of MAX_VERTEX_ATTRIBS
2580 * active attribute variables may fail to link, unless
2581 * device-dependent optimizations are able to make the program
2582 * fit within available hardware resources. For the purposes
2583 * of this test, attribute variables of the type dvec3, dvec4,
2584 * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
2585 * count as consuming twice as many attributes as equivalent
2586 * single-precision types. While these types use the same number
2587 * of generic attributes as their single-precision equivalents,
2588 * implementations are permitted to consume two single-precision
2589 * vectors of internal storage for each three- or four-component
2590 * double-precision vector."
2592 * Mark this attribute slot as taking up twice as much space
2593 * so we can count it properly against limits. According to
2594 * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
2595 * is optional behavior, but it seems preferable.
2597 const glsl_type
*type
= var
->type
->without_array();
2598 if (type
== glsl_type::dvec3_type
||
2599 type
== glsl_type::dvec4_type
||
2600 type
== glsl_type::dmat2x3_type
||
2601 type
== glsl_type::dmat2x4_type
||
2602 type
== glsl_type::dmat3_type
||
2603 type
== glsl_type::dmat3x4_type
||
2604 type
== glsl_type::dmat4x3_type
||
2605 type
== glsl_type::dmat4_type
) {
2606 double_storage_locations
|= (use_mask
<< attr
);
2613 to_assign
[num_attr
].slots
= slots
;
2614 to_assign
[num_attr
].var
= var
;
2618 if (target_index
== MESA_SHADER_VERTEX
) {
2619 unsigned total_attribs_size
=
2620 _mesa_bitcount(used_locations
& ((1 << max_index
) - 1)) +
2621 _mesa_bitcount(double_storage_locations
);
2622 if (total_attribs_size
> max_index
) {
2624 "attempt to use %d vertex attribute slots only %d available ",
2625 total_attribs_size
, max_index
);
2630 /* If all of the attributes were assigned locations by the application (or
2631 * are built-in attributes with fixed locations), return early. This should
2632 * be the common case.
2637 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
2639 if (target_index
== MESA_SHADER_VERTEX
) {
2640 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
2641 * only be explicitly assigned by via glBindAttribLocation. Mark it as
2642 * reserved to prevent it from being automatically allocated below.
2644 find_deref_visitor
find("gl_Vertex");
2646 if (find
.variable_found())
2647 used_locations
|= (1 << 0);
2650 for (unsigned i
= 0; i
< num_attr
; i
++) {
2651 /* Mask representing the contiguous slots that will be used by this
2654 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
2656 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
2659 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
2660 ? "vertex shader input" : "fragment shader output";
2663 "insufficient contiguous locations "
2664 "available for %s `%s'\n",
2665 string
, to_assign
[i
].var
->name
);
2669 to_assign
[i
].var
->data
.location
= generic_base
+ location
;
2670 to_assign
[i
].var
->data
.is_unmatched_generic_inout
= 0;
2671 used_locations
|= (use_mask
<< location
);
2679 * Demote shader inputs and outputs that are not used in other stages
2682 demote_shader_inputs_and_outputs(gl_shader
*sh
, enum ir_variable_mode mode
)
2684 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2685 ir_variable
*const var
= node
->as_variable();
2687 if ((var
== NULL
) || (var
->data
.mode
!= int(mode
)))
2690 /* A shader 'in' or 'out' variable is only really an input or output if
2691 * its value is used by other shader stages. This will cause the variable
2692 * to have a location assigned.
2694 if (var
->data
.is_unmatched_generic_inout
) {
2695 assert(var
->data
.mode
!= ir_var_temporary
);
2696 var
->data
.mode
= ir_var_auto
;
2703 * Store the gl_FragDepth layout in the gl_shader_program struct.
2706 store_fragdepth_layout(struct gl_shader_program
*prog
)
2708 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
2712 struct exec_list
*ir
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
2714 /* We don't look up the gl_FragDepth symbol directly because if
2715 * gl_FragDepth is not used in the shader, it's removed from the IR.
2716 * However, the symbol won't be removed from the symbol table.
2718 * We're only interested in the cases where the variable is NOT removed
2721 foreach_in_list(ir_instruction
, node
, ir
) {
2722 ir_variable
*const var
= node
->as_variable();
2724 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
) {
2728 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
2729 switch (var
->data
.depth_layout
) {
2730 case ir_depth_layout_none
:
2731 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
2733 case ir_depth_layout_any
:
2734 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
2736 case ir_depth_layout_greater
:
2737 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
2739 case ir_depth_layout_less
:
2740 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
2742 case ir_depth_layout_unchanged
:
2743 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2754 * Validate the resources used by a program versus the implementation limits
2757 check_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
2759 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2760 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2765 if (sh
->num_samplers
> ctx
->Const
.Program
[i
].MaxTextureImageUnits
) {
2766 linker_error(prog
, "Too many %s shader texture samplers\n",
2767 _mesa_shader_stage_to_string(i
));
2770 if (sh
->num_uniform_components
>
2771 ctx
->Const
.Program
[i
].MaxUniformComponents
) {
2772 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
2773 linker_warning(prog
, "Too many %s shader default uniform block "
2774 "components, but the driver will try to optimize "
2775 "them out; this is non-portable out-of-spec "
2777 _mesa_shader_stage_to_string(i
));
2779 linker_error(prog
, "Too many %s shader default uniform block "
2781 _mesa_shader_stage_to_string(i
));
2785 if (sh
->num_combined_uniform_components
>
2786 ctx
->Const
.Program
[i
].MaxCombinedUniformComponents
) {
2787 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
2788 linker_warning(prog
, "Too many %s shader uniform components, "
2789 "but the driver will try to optimize them out; "
2790 "this is non-portable out-of-spec behavior\n",
2791 _mesa_shader_stage_to_string(i
));
2793 linker_error(prog
, "Too many %s shader uniform components\n",
2794 _mesa_shader_stage_to_string(i
));
2799 unsigned blocks
[MESA_SHADER_STAGES
] = {0};
2800 unsigned total_uniform_blocks
= 0;
2801 unsigned shader_blocks
[MESA_SHADER_STAGES
] = {0};
2802 unsigned total_shader_storage_blocks
= 0;
2804 for (unsigned i
= 0; i
< prog
->NumBufferInterfaceBlocks
; i
++) {
2805 /* Don't check SSBOs for Uniform Block Size */
2806 if (!prog
->BufferInterfaceBlocks
[i
].IsShaderStorage
&&
2807 prog
->BufferInterfaceBlocks
[i
].UniformBufferSize
> ctx
->Const
.MaxUniformBlockSize
) {
2808 linker_error(prog
, "Uniform block %s too big (%d/%d)\n",
2809 prog
->BufferInterfaceBlocks
[i
].Name
,
2810 prog
->BufferInterfaceBlocks
[i
].UniformBufferSize
,
2811 ctx
->Const
.MaxUniformBlockSize
);
2814 if (prog
->BufferInterfaceBlocks
[i
].IsShaderStorage
&&
2815 prog
->BufferInterfaceBlocks
[i
].UniformBufferSize
> ctx
->Const
.MaxShaderStorageBlockSize
) {
2816 linker_error(prog
, "Shader storage block %s too big (%d/%d)\n",
2817 prog
->BufferInterfaceBlocks
[i
].Name
,
2818 prog
->BufferInterfaceBlocks
[i
].UniformBufferSize
,
2819 ctx
->Const
.MaxShaderStorageBlockSize
);
2822 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
2823 if (prog
->UniformBlockStageIndex
[j
][i
] != -1) {
2824 struct gl_shader
*sh
= prog
->_LinkedShaders
[j
];
2825 int stage_index
= prog
->UniformBlockStageIndex
[j
][i
];
2826 if (sh
&& sh
->BufferInterfaceBlocks
[stage_index
].IsShaderStorage
) {
2828 total_shader_storage_blocks
++;
2831 total_uniform_blocks
++;
2836 if (total_uniform_blocks
> ctx
->Const
.MaxCombinedUniformBlocks
) {
2837 linker_error(prog
, "Too many combined uniform blocks (%d/%d)\n",
2838 total_uniform_blocks
,
2839 ctx
->Const
.MaxCombinedUniformBlocks
);
2841 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2842 const unsigned max_uniform_blocks
=
2843 ctx
->Const
.Program
[i
].MaxUniformBlocks
;
2844 if (blocks
[i
] > max_uniform_blocks
) {
2845 linker_error(prog
, "Too many %s uniform blocks (%d/%d)\n",
2846 _mesa_shader_stage_to_string(i
),
2848 max_uniform_blocks
);
2854 if (total_shader_storage_blocks
> ctx
->Const
.MaxCombinedShaderStorageBlocks
) {
2855 linker_error(prog
, "Too many combined shader storage blocks (%d/%d)\n",
2856 total_shader_storage_blocks
,
2857 ctx
->Const
.MaxCombinedShaderStorageBlocks
);
2859 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2860 const unsigned max_shader_storage_blocks
=
2861 ctx
->Const
.Program
[i
].MaxShaderStorageBlocks
;
2862 if (shader_blocks
[i
] > max_shader_storage_blocks
) {
2863 linker_error(prog
, "Too many %s shader storage blocks (%d/%d)\n",
2864 _mesa_shader_stage_to_string(i
),
2866 max_shader_storage_blocks
);
2875 link_calculate_subroutine_compat(struct gl_shader_program
*prog
)
2877 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2878 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2883 for (unsigned j
= 0; j
< sh
->NumSubroutineUniformRemapTable
; j
++) {
2884 struct gl_uniform_storage
*uni
= sh
->SubroutineUniformRemapTable
[j
];
2890 for (unsigned f
= 0; f
< sh
->NumSubroutineFunctions
; f
++) {
2891 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[f
];
2892 for (int k
= 0; k
< fn
->num_compat_types
; k
++) {
2893 if (fn
->types
[k
] == uni
->type
) {
2899 uni
->num_compatible_subroutines
= count
;
2905 check_subroutine_resources(struct gl_shader_program
*prog
)
2907 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2908 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2911 if (sh
->NumSubroutineUniformRemapTable
> MAX_SUBROUTINE_UNIFORM_LOCATIONS
)
2912 linker_error(prog
, "Too many %s shader subroutine uniforms\n",
2913 _mesa_shader_stage_to_string(i
));
2918 * Validate shader image resources.
2921 check_image_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
2923 unsigned total_image_units
= 0;
2924 unsigned fragment_outputs
= 0;
2925 unsigned total_shader_storage_blocks
= 0;
2927 if (!ctx
->Extensions
.ARB_shader_image_load_store
)
2930 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2931 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
2934 if (sh
->NumImages
> ctx
->Const
.Program
[i
].MaxImageUniforms
)
2935 linker_error(prog
, "Too many %s shader image uniforms (%u > %u)\n",
2936 _mesa_shader_stage_to_string(i
), sh
->NumImages
,
2937 ctx
->Const
.Program
[i
].MaxImageUniforms
);
2939 total_image_units
+= sh
->NumImages
;
2941 for (unsigned j
= 0; j
< prog
->NumBufferInterfaceBlocks
; j
++) {
2942 int stage_index
= prog
->UniformBlockStageIndex
[i
][j
];
2943 if (stage_index
!= -1 && sh
->BufferInterfaceBlocks
[stage_index
].IsShaderStorage
)
2944 total_shader_storage_blocks
++;
2947 if (i
== MESA_SHADER_FRAGMENT
) {
2948 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2949 ir_variable
*var
= node
->as_variable();
2950 if (var
&& var
->data
.mode
== ir_var_shader_out
)
2951 fragment_outputs
+= var
->type
->count_attribute_slots();
2957 if (total_image_units
> ctx
->Const
.MaxCombinedImageUniforms
)
2958 linker_error(prog
, "Too many combined image uniforms\n");
2960 if (total_image_units
+ fragment_outputs
+ total_shader_storage_blocks
>
2961 ctx
->Const
.MaxCombinedShaderOutputResources
)
2962 linker_error(prog
, "Too many combined image uniforms, shader storage "
2963 " buffers and fragment outputs\n");
2968 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
2969 * for a variable, checks for overlaps between other uniforms using explicit
2973 reserve_explicit_locations(struct gl_shader_program
*prog
,
2974 string_to_uint_map
*map
, ir_variable
*var
)
2976 unsigned slots
= var
->type
->uniform_locations();
2977 unsigned max_loc
= var
->data
.location
+ slots
- 1;
2979 /* Resize remap table if locations do not fit in the current one. */
2980 if (max_loc
+ 1 > prog
->NumUniformRemapTable
) {
2981 prog
->UniformRemapTable
=
2982 reralloc(prog
, prog
->UniformRemapTable
,
2983 gl_uniform_storage
*,
2986 if (!prog
->UniformRemapTable
) {
2987 linker_error(prog
, "Out of memory during linking.\n");
2991 /* Initialize allocated space. */
2992 for (unsigned i
= prog
->NumUniformRemapTable
; i
< max_loc
+ 1; i
++)
2993 prog
->UniformRemapTable
[i
] = NULL
;
2995 prog
->NumUniformRemapTable
= max_loc
+ 1;
2998 for (unsigned i
= 0; i
< slots
; i
++) {
2999 unsigned loc
= var
->data
.location
+ i
;
3001 /* Check if location is already used. */
3002 if (prog
->UniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3004 /* Possibly same uniform from a different stage, this is ok. */
3006 if (map
->get(hash_loc
, var
->name
) && hash_loc
== loc
- i
)
3009 /* ARB_explicit_uniform_location specification states:
3011 * "No two default-block uniform variables in the program can have
3012 * the same location, even if they are unused, otherwise a compiler
3013 * or linker error will be generated."
3016 "location qualifier for uniform %s overlaps "
3017 "previously used location\n",
3022 /* Initialize location as inactive before optimization
3023 * rounds and location assignment.
3025 prog
->UniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3028 /* Note, base location used for arrays. */
3029 map
->put(var
->data
.location
, var
->name
);
3035 reserve_subroutine_explicit_locations(struct gl_shader_program
*prog
,
3036 struct gl_shader
*sh
,
3039 unsigned slots
= var
->type
->uniform_locations();
3040 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3042 /* Resize remap table if locations do not fit in the current one. */
3043 if (max_loc
+ 1 > sh
->NumSubroutineUniformRemapTable
) {
3044 sh
->SubroutineUniformRemapTable
=
3045 reralloc(sh
, sh
->SubroutineUniformRemapTable
,
3046 gl_uniform_storage
*,
3049 if (!sh
->SubroutineUniformRemapTable
) {
3050 linker_error(prog
, "Out of memory during linking.\n");
3054 /* Initialize allocated space. */
3055 for (unsigned i
= sh
->NumSubroutineUniformRemapTable
; i
< max_loc
+ 1; i
++)
3056 sh
->SubroutineUniformRemapTable
[i
] = NULL
;
3058 sh
->NumSubroutineUniformRemapTable
= max_loc
+ 1;
3061 for (unsigned i
= 0; i
< slots
; i
++) {
3062 unsigned loc
= var
->data
.location
+ i
;
3064 /* Check if location is already used. */
3065 if (sh
->SubroutineUniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3067 /* ARB_explicit_uniform_location specification states:
3068 * "No two subroutine uniform variables can have the same location
3069 * in the same shader stage, otherwise a compiler or linker error
3070 * will be generated."
3073 "location qualifier for uniform %s overlaps "
3074 "previously used location\n",
3079 /* Initialize location as inactive before optimization
3080 * rounds and location assignment.
3082 sh
->SubroutineUniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3088 * Check and reserve all explicit uniform locations, called before
3089 * any optimizations happen to handle also inactive uniforms and
3090 * inactive array elements that may get trimmed away.
3093 check_explicit_uniform_locations(struct gl_context
*ctx
,
3094 struct gl_shader_program
*prog
)
3096 if (!ctx
->Extensions
.ARB_explicit_uniform_location
)
3099 /* This map is used to detect if overlapping explicit locations
3100 * occur with the same uniform (from different stage) or a different one.
3102 string_to_uint_map
*uniform_map
= new string_to_uint_map
;
3105 linker_error(prog
, "Out of memory during linking.\n");
3109 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3110 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
3115 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3116 ir_variable
*var
= node
->as_variable();
3117 if (var
&& (var
->data
.mode
== ir_var_uniform
&&
3118 var
->data
.explicit_location
)) {
3120 if (var
->type
->is_subroutine())
3121 ret
= reserve_subroutine_explicit_locations(prog
, sh
, var
);
3123 ret
= reserve_explicit_locations(prog
, uniform_map
, var
);
3136 should_add_buffer_variable(struct gl_shader_program
*shProg
,
3137 GLenum type
, const char *name
)
3139 bool found_interface
= false;
3140 const char *block_name
= NULL
;
3142 /* These rules only apply to buffer variables. So we return
3143 * true for the rest of types.
3145 if (type
!= GL_BUFFER_VARIABLE
)
3148 for (unsigned i
= 0; i
< shProg
->NumBufferInterfaceBlocks
; i
++) {
3149 block_name
= shProg
->BufferInterfaceBlocks
[i
].Name
;
3150 if (strncmp(block_name
, name
, strlen(block_name
)) == 0) {
3151 found_interface
= true;
3156 /* We remove the interface name from the buffer variable name,
3157 * including the dot that follows it.
3159 if (found_interface
)
3160 name
= name
+ strlen(block_name
) + 1;
3162 /* From: ARB_program_interface_query extension:
3164 * "For an active shader storage block member declared as an array, an
3165 * entry will be generated only for the first array element, regardless
3166 * of its type. For arrays of aggregate types, the enumeration rules are
3167 * applied recursively for the single enumerated array element.
3169 const char *first_dot
= strchr(name
, '.');
3170 const char *first_square_bracket
= strchr(name
, '[');
3172 /* The buffer variable is on top level and it is not an array */
3173 if (!first_square_bracket
) {
3175 /* The shader storage block member is a struct, then generate the entry */
3176 } else if (first_dot
&& first_dot
< first_square_bracket
) {
3179 /* Shader storage block member is an array, only generate an entry for the
3180 * first array element.
3182 if (strncmp(first_square_bracket
, "[0]", 3) == 0)
3190 add_program_resource(struct gl_shader_program
*prog
, GLenum type
,
3191 const void *data
, uint8_t stages
)
3195 /* If resource already exists, do not add it again. */
3196 for (unsigned i
= 0; i
< prog
->NumProgramResourceList
; i
++)
3197 if (prog
->ProgramResourceList
[i
].Data
== data
)
3200 prog
->ProgramResourceList
=
3202 prog
->ProgramResourceList
,
3203 gl_program_resource
,
3204 prog
->NumProgramResourceList
+ 1);
3206 if (!prog
->ProgramResourceList
) {
3207 linker_error(prog
, "Out of memory during linking.\n");
3211 struct gl_program_resource
*res
=
3212 &prog
->ProgramResourceList
[prog
->NumProgramResourceList
];
3216 res
->StageReferences
= stages
;
3218 prog
->NumProgramResourceList
++;
3223 /* Function checks if a variable var is a packed varying and
3224 * if given name is part of packed varying's list.
3226 * If a variable is a packed varying, it has a name like
3227 * 'packed:a,b,c' where a, b and c are separate variables.
3230 included_in_packed_varying(ir_variable
*var
, const char *name
)
3232 if (strncmp(var
->name
, "packed:", 7) != 0)
3235 char *list
= strdup(var
->name
+ 7);
3240 char *token
= strtok_r(list
, ",", &saveptr
);
3242 if (strcmp(token
, name
) == 0) {
3246 token
= strtok_r(NULL
, ",", &saveptr
);
3253 * Function builds a stage reference bitmask from variable name.
3256 build_stageref(struct gl_shader_program
*shProg
, const char *name
,
3261 /* Note, that we assume MAX 8 stages, if there will be more stages, type
3262 * used for reference mask in gl_program_resource will need to be changed.
3264 assert(MESA_SHADER_STAGES
< 8);
3266 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3267 struct gl_shader
*sh
= shProg
->_LinkedShaders
[i
];
3271 /* Shader symbol table may contain variables that have
3272 * been optimized away. Search IR for the variable instead.
3274 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3275 ir_variable
*var
= node
->as_variable();
3277 unsigned baselen
= strlen(var
->name
);
3279 if (included_in_packed_varying(var
, name
)) {
3284 /* Type needs to match if specified, otherwise we might
3285 * pick a variable with same name but different interface.
3287 if (var
->data
.mode
!= mode
)
3290 if (strncmp(var
->name
, name
, baselen
) == 0) {
3291 /* Check for exact name matches but also check for arrays and
3294 if (name
[baselen
] == '\0' ||
3295 name
[baselen
] == '[' ||
3296 name
[baselen
] == '.') {
3308 add_interface_variables(struct gl_shader_program
*shProg
,
3309 exec_list
*ir
, GLenum programInterface
)
3311 foreach_in_list(ir_instruction
, node
, ir
) {
3312 ir_variable
*var
= node
->as_variable();
3318 switch (var
->data
.mode
) {
3319 /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
3320 * "For GetActiveAttrib, all active vertex shader input variables
3321 * are enumerated, including the special built-in inputs gl_VertexID
3322 * and gl_InstanceID."
3324 case ir_var_system_value
:
3325 if (var
->data
.location
!= SYSTEM_VALUE_VERTEX_ID
&&
3326 var
->data
.location
!= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
&&
3327 var
->data
.location
!= SYSTEM_VALUE_INSTANCE_ID
)
3329 /* Mark special built-in inputs referenced by the vertex stage so
3330 * that they are considered active by the shader queries.
3332 mask
= (1 << (MESA_SHADER_VERTEX
));
3334 case ir_var_shader_in
:
3335 if (programInterface
!= GL_PROGRAM_INPUT
)
3338 case ir_var_shader_out
:
3339 if (programInterface
!= GL_PROGRAM_OUTPUT
)
3346 /* Skip packed varyings, packed varyings are handled separately
3347 * by add_packed_varyings.
3349 if (strncmp(var
->name
, "packed:", 7) == 0)
3352 if (!add_program_resource(shProg
, programInterface
, var
,
3353 build_stageref(shProg
, var
->name
,
3354 var
->data
.mode
) | mask
))
3361 add_packed_varyings(struct gl_shader_program
*shProg
, int stage
)
3363 struct gl_shader
*sh
= shProg
->_LinkedShaders
[stage
];
3366 if (!sh
|| !sh
->packed_varyings
)
3369 foreach_in_list(ir_instruction
, node
, sh
->packed_varyings
) {
3370 ir_variable
*var
= node
->as_variable();
3372 switch (var
->data
.mode
) {
3373 case ir_var_shader_in
:
3374 iface
= GL_PROGRAM_INPUT
;
3376 case ir_var_shader_out
:
3377 iface
= GL_PROGRAM_OUTPUT
;
3380 unreachable("unexpected type");
3382 if (!add_program_resource(shProg
, iface
, var
,
3383 build_stageref(shProg
, var
->name
,
3392 get_top_level_name(const char *name
)
3394 const char *first_dot
= strchr(name
, '.');
3395 const char *first_square_bracket
= strchr(name
, '[');
3397 /* From ARB_program_interface_query spec:
3399 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying the
3400 * number of active array elements of the top-level shader storage block
3401 * member containing to the active variable is written to <params>. If the
3402 * top-level block member is not declared as an array, the value one is
3403 * written to <params>. If the top-level block member is an array with no
3404 * declared size, the value zero is written to <params>.
3407 /* The buffer variable is on top level.*/
3408 if (!first_square_bracket
&& !first_dot
)
3409 name_size
= strlen(name
);
3410 else if ((!first_square_bracket
||
3411 (first_dot
&& first_dot
< first_square_bracket
)))
3412 name_size
= first_dot
- name
;
3414 name_size
= first_square_bracket
- name
;
3416 return strndup(name
, name_size
);
3420 get_var_name(const char *name
)
3422 const char *first_dot
= strchr(name
, '.');
3425 return strdup(name
);
3427 return strndup(first_dot
+1, strlen(first_dot
) - 1);
3431 is_top_level_shader_storage_block_member(const char* name
,
3432 const char* interface_name
,
3433 const char* field_name
)
3435 bool result
= false;
3437 /* If the given variable is already a top-level shader storage
3438 * block member, then return array_size = 1.
3439 * We could have two possibilities: if we have an instanced
3440 * shader storage block or not instanced.
3442 * For the first, we check create a name as it was in top level and
3443 * compare it with the real name. If they are the same, then
3444 * the variable is already at top-level.
3446 * Full instanced name is: interface name + '.' + var name +
3449 int name_length
= strlen(interface_name
) + 1 + strlen(field_name
) + 1;
3450 char *full_instanced_name
= (char *) calloc(name_length
, sizeof(char));
3451 if (!full_instanced_name
) {
3452 fprintf(stderr
, "%s: Cannot allocate space for name\n", __func__
);
3456 snprintf(full_instanced_name
, name_length
, "%s.%s",
3457 interface_name
, field_name
);
3459 /* Check if its top-level shader storage block member of an
3460 * instanced interface block, or of a unnamed interface block.
3462 if (strcmp(name
, full_instanced_name
) == 0 ||
3463 strcmp(name
, field_name
) == 0)
3466 free(full_instanced_name
);
3471 calculate_array_size(struct gl_shader_program
*shProg
,
3472 struct gl_uniform_storage
*uni
)
3474 int block_index
= uni
->block_index
;
3475 int array_size
= -1;
3476 char *var_name
= get_top_level_name(uni
->name
);
3477 char *interface_name
=
3478 get_top_level_name(shProg
->BufferInterfaceBlocks
[block_index
].Name
);
3480 if (strcmp(var_name
, interface_name
) == 0) {
3481 /* Deal with instanced array of SSBOs */
3482 char *temp_name
= get_var_name(uni
->name
);
3484 var_name
= get_top_level_name(temp_name
);
3488 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
3489 if (shProg
->Shaders
[i
] == NULL
)
3492 const gl_shader
*stage
= shProg
->Shaders
[i
];
3493 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
3494 ir_variable
*var
= node
->as_variable();
3495 if (!var
|| !var
->get_interface_type() ||
3496 var
->data
.mode
!= ir_var_shader_storage
)
3499 const glsl_type
*interface
= var
->get_interface_type();
3501 if (strcmp(interface_name
, interface
->name
) != 0)
3504 for (unsigned i
= 0; i
< interface
->length
; i
++) {
3505 const glsl_struct_field
*field
= &interface
->fields
.structure
[i
];
3506 if (strcmp(field
->name
, var_name
) != 0)
3508 /* From GL_ARB_program_interface_query spec:
3510 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer
3511 * identifying the number of active array elements of the top-level
3512 * shader storage block member containing to the active variable is
3513 * written to <params>. If the top-level block member is not
3514 * declared as an array, the value one is written to <params>. If
3515 * the top-level block member is an array with no declared size,
3516 * the value zero is written to <params>.
3518 if (is_top_level_shader_storage_block_member(uni
->name
,
3522 else if (field
->type
->is_unsized_array())
3524 else if (field
->type
->is_array())
3525 array_size
= field
->type
->length
;
3529 goto found_top_level_array_size
;
3533 found_top_level_array_size
:
3534 free(interface_name
);
3536 uni
->top_level_array_size
= array_size
;
3540 calculate_array_stride(struct gl_shader_program
*shProg
,
3541 struct gl_uniform_storage
*uni
)
3543 int block_index
= uni
->block_index
;
3544 int array_stride
= -1;
3545 char *var_name
= get_top_level_name(uni
->name
);
3546 char *interface_name
=
3547 get_top_level_name(shProg
->BufferInterfaceBlocks
[block_index
].Name
);
3549 if (strcmp(var_name
, interface_name
) == 0) {
3550 /* Deal with instanced array of SSBOs */
3551 char *temp_name
= get_var_name(uni
->name
);
3553 var_name
= get_top_level_name(temp_name
);
3557 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
3558 if (shProg
->Shaders
[i
] == NULL
)
3561 const gl_shader
*stage
= shProg
->Shaders
[i
];
3562 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
3563 ir_variable
*var
= node
->as_variable();
3564 if (!var
|| !var
->get_interface_type() ||
3565 var
->data
.mode
!= ir_var_shader_storage
)
3568 const glsl_type
*interface
= var
->get_interface_type();
3570 if (strcmp(interface_name
, interface
->name
) != 0) {
3574 for (unsigned i
= 0; i
< interface
->length
; i
++) {
3575 const glsl_struct_field
*field
= &interface
->fields
.structure
[i
];
3576 if (strcmp(field
->name
, var_name
) != 0)
3578 /* From GL_ARB_program_interface_query:
3580 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
3581 * identifying the stride between array elements of the top-level
3582 * shader storage block member containing the active variable is
3583 * written to <params>. For top-level block members declared as
3584 * arrays, the value written is the difference, in basic machine
3585 * units, between the offsets of the active variable for
3586 * consecutive elements in the top-level array. For top-level
3587 * block members not declared as an array, zero is written to
3590 if (field
->type
->is_array()) {
3591 const enum glsl_matrix_layout matrix_layout
=
3592 glsl_matrix_layout(field
->matrix_layout
);
3593 bool row_major
= matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
3594 const glsl_type
*array_type
= field
->type
->fields
.array
;
3596 if (is_top_level_shader_storage_block_member(uni
->name
,
3600 goto found_top_level_array_stride
;
3602 if (interface
->interface_packing
!= GLSL_INTERFACE_PACKING_STD430
) {
3603 if (array_type
->is_record() || array_type
->is_array()) {
3604 array_stride
= array_type
->std140_size(row_major
);
3605 array_stride
= glsl_align(array_stride
, 16);
3607 unsigned element_base_align
= 0;
3608 element_base_align
= array_type
->std140_base_alignment(row_major
);
3609 array_stride
= MAX2(element_base_align
, 16);
3612 array_stride
= array_type
->std430_array_stride(row_major
);
3617 goto found_top_level_array_stride
;
3621 found_top_level_array_stride
:
3622 free(interface_name
);
3624 uni
->top_level_array_stride
= array_stride
;
3628 * Builds up a list of program resources that point to existing
3632 build_program_resource_list(struct gl_shader_program
*shProg
)
3634 /* Rebuild resource list. */
3635 if (shProg
->ProgramResourceList
) {
3636 ralloc_free(shProg
->ProgramResourceList
);
3637 shProg
->ProgramResourceList
= NULL
;
3638 shProg
->NumProgramResourceList
= 0;
3641 int input_stage
= MESA_SHADER_STAGES
, output_stage
= 0;
3643 /* Determine first input and final output stage. These are used to
3644 * detect which variables should be enumerated in the resource list
3645 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
3647 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3648 if (!shProg
->_LinkedShaders
[i
])
3650 if (input_stage
== MESA_SHADER_STAGES
)
3655 /* Empty shader, no resources. */
3656 if (input_stage
== MESA_SHADER_STAGES
&& output_stage
== 0)
3659 /* Program interface needs to expose varyings in case of SSO. */
3660 if (shProg
->SeparateShader
) {
3661 if (!add_packed_varyings(shProg
, input_stage
))
3663 if (!add_packed_varyings(shProg
, output_stage
))
3667 /* Add inputs and outputs to the resource list. */
3668 if (!add_interface_variables(shProg
, shProg
->_LinkedShaders
[input_stage
]->ir
,
3672 if (!add_interface_variables(shProg
, shProg
->_LinkedShaders
[output_stage
]->ir
,
3676 /* Add transform feedback varyings. */
3677 if (shProg
->LinkedTransformFeedback
.NumVarying
> 0) {
3678 for (int i
= 0; i
< shProg
->LinkedTransformFeedback
.NumVarying
; i
++) {
3679 if (!add_program_resource(shProg
, GL_TRANSFORM_FEEDBACK_VARYING
,
3680 &shProg
->LinkedTransformFeedback
.Varyings
[i
],
3686 /* Add uniforms from uniform storage. */
3687 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; i
++) {
3688 /* Do not add uniforms internally used by Mesa. */
3689 if (shProg
->UniformStorage
[i
].hidden
)
3693 build_stageref(shProg
, shProg
->UniformStorage
[i
].name
,
3696 /* Add stagereferences for uniforms in a uniform block. */
3697 int block_index
= shProg
->UniformStorage
[i
].block_index
;
3698 if (block_index
!= -1) {
3699 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
3700 if (shProg
->UniformBlockStageIndex
[j
][block_index
] != -1)
3701 stageref
|= (1 << j
);
3705 bool is_shader_storage
= shProg
->UniformStorage
[i
].is_shader_storage
;
3706 GLenum type
= is_shader_storage
? GL_BUFFER_VARIABLE
: GL_UNIFORM
;
3707 if (!should_add_buffer_variable(shProg
, type
,
3708 shProg
->UniformStorage
[i
].name
))
3711 if (is_shader_storage
) {
3712 calculate_array_size(shProg
, &shProg
->UniformStorage
[i
]);
3713 calculate_array_stride(shProg
, &shProg
->UniformStorage
[i
]);
3716 if (!add_program_resource(shProg
, type
,
3717 &shProg
->UniformStorage
[i
], stageref
))
3721 /* Add program uniform blocks and shader storage blocks. */
3722 for (unsigned i
= 0; i
< shProg
->NumBufferInterfaceBlocks
; i
++) {
3723 bool is_shader_storage
= shProg
->BufferInterfaceBlocks
[i
].IsShaderStorage
;
3724 GLenum type
= is_shader_storage
? GL_SHADER_STORAGE_BLOCK
: GL_UNIFORM_BLOCK
;
3725 if (!add_program_resource(shProg
, type
,
3726 &shProg
->BufferInterfaceBlocks
[i
], 0))
3730 /* Add atomic counter buffers. */
3731 for (unsigned i
= 0; i
< shProg
->NumAtomicBuffers
; i
++) {
3732 if (!add_program_resource(shProg
, GL_ATOMIC_COUNTER_BUFFER
,
3733 &shProg
->AtomicBuffers
[i
], 0))
3737 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; i
++) {
3739 if (!shProg
->UniformStorage
[i
].hidden
)
3742 for (int j
= MESA_SHADER_VERTEX
; j
< MESA_SHADER_STAGES
; j
++) {
3743 if (!shProg
->UniformStorage
[i
].opaque
[j
].active
)
3746 type
= _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage
)j
);
3747 /* add shader subroutines */
3748 if (!add_program_resource(shProg
, type
, &shProg
->UniformStorage
[i
], 0))
3753 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3754 struct gl_shader
*sh
= shProg
->_LinkedShaders
[i
];
3760 type
= _mesa_shader_stage_to_subroutine((gl_shader_stage
)i
);
3761 for (unsigned j
= 0; j
< sh
->NumSubroutineFunctions
; j
++) {
3762 if (!add_program_resource(shProg
, type
, &sh
->SubroutineFunctions
[j
], 0))
3767 /* TODO - following extensions will require more resource types:
3769 * GL_ARB_shader_storage_buffer_object
3774 * This check is done to make sure we allow only constant expression
3775 * indexing and "constant-index-expression" (indexing with an expression
3776 * that includes loop induction variable).
3779 validate_sampler_array_indexing(struct gl_context
*ctx
,
3780 struct gl_shader_program
*prog
)
3782 dynamic_sampler_array_indexing_visitor v
;
3783 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3784 if (prog
->_LinkedShaders
[i
] == NULL
)
3787 bool no_dynamic_indexing
=
3788 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
;
3790 /* Search for array derefs in shader. */
3791 v
.run(prog
->_LinkedShaders
[i
]->ir
);
3792 if (v
.uses_dynamic_sampler_array_indexing()) {
3793 const char *msg
= "sampler arrays indexed with non-constant "
3794 "expressions is forbidden in GLSL %s %u";
3795 /* Backend has indicated that it has no dynamic indexing support. */
3796 if (no_dynamic_indexing
) {
3797 linker_error(prog
, msg
, prog
->IsES
? "ES" : "", prog
->Version
);
3800 linker_warning(prog
, msg
, prog
->IsES
? "ES" : "", prog
->Version
);
3808 link_assign_subroutine_types(struct gl_shader_program
*prog
)
3810 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3811 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
3816 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3817 ir_function
*fn
= node
->as_function();
3821 if (fn
->is_subroutine
)
3822 sh
->NumSubroutineUniformTypes
++;
3824 if (!fn
->num_subroutine_types
)
3827 sh
->SubroutineFunctions
= reralloc(sh
, sh
->SubroutineFunctions
,
3828 struct gl_subroutine_function
,
3829 sh
->NumSubroutineFunctions
+ 1);
3830 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].name
= ralloc_strdup(sh
, fn
->name
);
3831 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].num_compat_types
= fn
->num_subroutine_types
;
3832 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].types
=
3833 ralloc_array(sh
, const struct glsl_type
*,
3834 fn
->num_subroutine_types
);
3835 for (int j
= 0; j
< fn
->num_subroutine_types
; j
++)
3836 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].types
[j
] = fn
->subroutine_types
[j
];
3837 sh
->NumSubroutineFunctions
++;
3843 split_ubos_and_ssbos(void *mem_ctx
,
3844 struct gl_uniform_block
*blocks
,
3845 unsigned num_blocks
,
3846 struct gl_uniform_block
***ubos
,
3848 struct gl_uniform_block
***ssbos
,
3849 unsigned *num_ssbos
)
3851 unsigned num_ubo_blocks
= 0;
3852 unsigned num_ssbo_blocks
= 0;
3854 for (unsigned i
= 0; i
< num_blocks
; i
++) {
3855 if (blocks
[i
].IsShaderStorage
)
3861 *ubos
= ralloc_array(mem_ctx
, gl_uniform_block
*, num_ubo_blocks
);
3864 *ssbos
= ralloc_array(mem_ctx
, gl_uniform_block
*, num_ssbo_blocks
);
3867 for (unsigned i
= 0; i
< num_blocks
; i
++) {
3868 if (blocks
[i
].IsShaderStorage
) {
3869 (*ssbos
)[(*num_ssbos
)++] = &blocks
[i
];
3871 (*ubos
)[(*num_ubos
)++] = &blocks
[i
];
3875 assert(*num_ubos
+ *num_ssbos
== num_blocks
);
3879 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3881 tfeedback_decl
*tfeedback_decls
= NULL
;
3882 unsigned num_tfeedback_decls
= prog
->TransformFeedback
.NumVarying
;
3884 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
3886 prog
->LinkStatus
= true; /* All error paths will set this to false */
3887 prog
->Validated
= false;
3888 prog
->_Used
= false;
3890 prog
->ARB_fragment_coord_conventions_enable
= false;
3892 /* Separate the shaders into groups based on their type.
3894 struct gl_shader
**shader_list
[MESA_SHADER_STAGES
];
3895 unsigned num_shaders
[MESA_SHADER_STAGES
];
3897 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3898 shader_list
[i
] = (struct gl_shader
**)
3899 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
3903 unsigned min_version
= UINT_MAX
;
3904 unsigned max_version
= 0;
3905 const bool is_es_prog
=
3906 (prog
->NumShaders
> 0 && prog
->Shaders
[0]->IsES
) ? true : false;
3907 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
3908 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
3909 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
3911 if (prog
->Shaders
[i
]->IsES
!= is_es_prog
) {
3912 linker_error(prog
, "all shaders must use same shading "
3913 "language version\n");
3917 if (prog
->Shaders
[i
]->ARB_fragment_coord_conventions_enable
) {
3918 prog
->ARB_fragment_coord_conventions_enable
= true;
3921 gl_shader_stage shader_type
= prog
->Shaders
[i
]->Stage
;
3922 shader_list
[shader_type
][num_shaders
[shader_type
]] = prog
->Shaders
[i
];
3923 num_shaders
[shader_type
]++;
3926 /* In desktop GLSL, different shader versions may be linked together. In
3927 * GLSL ES, all shader versions must be the same.
3929 if (is_es_prog
&& min_version
!= max_version
) {
3930 linker_error(prog
, "all shaders must use same shading "
3931 "language version\n");
3935 prog
->Version
= max_version
;
3936 prog
->IsES
= is_es_prog
;
3938 /* From OpenGL 4.5 Core specification (7.3 Program Objects):
3939 * "Linking can fail for a variety of reasons as specified in the OpenGL
3940 * Shading Language Specification, as well as any of the following
3943 * * No shader objects are attached to program.
3947 * Same rule applies for OpenGL ES >= 3.1.
3950 if (prog
->NumShaders
== 0 &&
3951 ((ctx
->API
== API_OPENGL_CORE
&& ctx
->Version
>= 45) ||
3952 (ctx
->API
== API_OPENGLES2
&& ctx
->Version
>= 31))) {
3953 linker_error(prog
, "No shader objects are attached to program.\n");
3957 /* Some shaders have to be linked with some other shaders present.
3959 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0 &&
3960 num_shaders
[MESA_SHADER_VERTEX
] == 0 &&
3961 !prog
->SeparateShader
) {
3962 linker_error(prog
, "Geometry shader must be linked with "
3966 if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0 &&
3967 num_shaders
[MESA_SHADER_VERTEX
] == 0 &&
3968 !prog
->SeparateShader
) {
3969 linker_error(prog
, "Tessellation evaluation shader must be linked with "
3973 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
3974 num_shaders
[MESA_SHADER_VERTEX
] == 0 &&
3975 !prog
->SeparateShader
) {
3976 linker_error(prog
, "Tessellation control shader must be linked with "
3981 /* The spec is self-contradictory here. It allows linking without a tess
3982 * eval shader, but that can only be used with transform feedback and
3983 * rasterization disabled. However, transform feedback isn't allowed
3984 * with GL_PATCHES, so it can't be used.
3986 * More investigation showed that the idea of transform feedback after
3987 * a tess control shader was dropped, because some hw vendors couldn't
3988 * support tessellation without a tess eval shader, but the linker section
3989 * wasn't updated to reflect that.
3991 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
3994 * Do what's reasonable and always require a tess eval shader if a tess
3995 * control shader is present.
3997 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
3998 num_shaders
[MESA_SHADER_TESS_EVAL
] == 0 &&
3999 !prog
->SeparateShader
) {
4000 linker_error(prog
, "Tessellation control shader must be linked with "
4001 "tessellation evaluation shader\n");
4005 /* Compute shaders have additional restrictions. */
4006 if (num_shaders
[MESA_SHADER_COMPUTE
] > 0 &&
4007 num_shaders
[MESA_SHADER_COMPUTE
] != prog
->NumShaders
) {
4008 linker_error(prog
, "Compute shaders may not be linked with any other "
4009 "type of shader\n");
4012 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4013 if (prog
->_LinkedShaders
[i
] != NULL
)
4014 _mesa_delete_shader(ctx
, prog
->_LinkedShaders
[i
]);
4016 prog
->_LinkedShaders
[i
] = NULL
;
4019 /* Link all shaders for a particular stage and validate the result.
4021 for (int stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4022 if (num_shaders
[stage
] > 0) {
4023 gl_shader
*const sh
=
4024 link_intrastage_shaders(mem_ctx
, ctx
, prog
, shader_list
[stage
],
4025 num_shaders
[stage
]);
4027 if (!prog
->LinkStatus
) {
4029 _mesa_delete_shader(ctx
, sh
);
4034 case MESA_SHADER_VERTEX
:
4035 validate_vertex_shader_executable(prog
, sh
);
4037 case MESA_SHADER_TESS_CTRL
:
4038 /* nothing to be done */
4040 case MESA_SHADER_TESS_EVAL
:
4041 validate_tess_eval_shader_executable(prog
, sh
);
4043 case MESA_SHADER_GEOMETRY
:
4044 validate_geometry_shader_executable(prog
, sh
);
4046 case MESA_SHADER_FRAGMENT
:
4047 validate_fragment_shader_executable(prog
, sh
);
4050 if (!prog
->LinkStatus
) {
4052 _mesa_delete_shader(ctx
, sh
);
4056 _mesa_reference_shader(ctx
, &prog
->_LinkedShaders
[stage
], sh
);
4060 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0)
4061 prog
->LastClipDistanceArraySize
= prog
->Geom
.ClipDistanceArraySize
;
4062 else if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0)
4063 prog
->LastClipDistanceArraySize
= prog
->TessEval
.ClipDistanceArraySize
;
4064 else if (num_shaders
[MESA_SHADER_VERTEX
] > 0)
4065 prog
->LastClipDistanceArraySize
= prog
->Vert
.ClipDistanceArraySize
;
4067 prog
->LastClipDistanceArraySize
= 0; /* Not used */
4069 /* Here begins the inter-stage linking phase. Some initial validation is
4070 * performed, then locations are assigned for uniforms, attributes, and
4073 cross_validate_uniforms(prog
);
4074 if (!prog
->LinkStatus
)
4079 for (prev
= 0; prev
<= MESA_SHADER_FRAGMENT
; prev
++) {
4080 if (prog
->_LinkedShaders
[prev
] != NULL
)
4084 check_explicit_uniform_locations(ctx
, prog
);
4085 link_assign_subroutine_types(prog
);
4087 if (!prog
->LinkStatus
)
4090 resize_tes_inputs(ctx
, prog
);
4092 /* Validate the inputs of each stage with the output of the preceding
4095 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4096 if (prog
->_LinkedShaders
[i
] == NULL
)
4099 validate_interstage_inout_blocks(prog
, prog
->_LinkedShaders
[prev
],
4100 prog
->_LinkedShaders
[i
]);
4101 if (!prog
->LinkStatus
)
4104 cross_validate_outputs_to_inputs(prog
,
4105 prog
->_LinkedShaders
[prev
],
4106 prog
->_LinkedShaders
[i
]);
4107 if (!prog
->LinkStatus
)
4113 /* Cross-validate uniform blocks between shader stages */
4114 validate_interstage_uniform_blocks(prog
, prog
->_LinkedShaders
,
4115 MESA_SHADER_STAGES
);
4116 if (!prog
->LinkStatus
)
4119 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4120 if (prog
->_LinkedShaders
[i
] != NULL
)
4121 lower_named_interface_blocks(mem_ctx
, prog
->_LinkedShaders
[i
]);
4124 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
4125 * it before optimization because we want most of the checks to get
4126 * dropped thanks to constant propagation.
4128 * This rule also applies to GLSL ES 3.00.
4130 if (max_version
>= (is_es_prog
? 300 : 130)) {
4131 struct gl_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4133 lower_discard_flow(sh
->ir
);
4137 if (!interstage_cross_validate_uniform_blocks(prog
))
4140 /* Do common optimization before assigning storage for attributes,
4141 * uniforms, and varyings. Later optimization could possibly make
4142 * some of that unused.
4144 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4145 if (prog
->_LinkedShaders
[i
] == NULL
)
4148 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
4149 if (!prog
->LinkStatus
)
4152 if (ctx
->Const
.ShaderCompilerOptions
[i
].LowerClipDistance
) {
4153 lower_clip_distance(prog
->_LinkedShaders
[i
]);
4156 if (ctx
->Const
.LowerTessLevel
) {
4157 lower_tess_level(prog
->_LinkedShaders
[i
]);
4160 while (do_common_optimization(prog
->_LinkedShaders
[i
]->ir
, true, false,
4161 &ctx
->Const
.ShaderCompilerOptions
[i
],
4162 ctx
->Const
.NativeIntegers
))
4165 lower_const_arrays_to_uniforms(prog
->_LinkedShaders
[i
]->ir
);
4168 /* Validation for special cases where we allow sampler array indexing
4169 * with loop induction variable. This check emits a warning or error
4170 * depending if backend can handle dynamic indexing.
4172 if ((!prog
->IsES
&& prog
->Version
< 130) ||
4173 (prog
->IsES
&& prog
->Version
< 300)) {
4174 if (!validate_sampler_array_indexing(ctx
, prog
))
4178 /* Check and validate stream emissions in geometry shaders */
4179 validate_geometry_shader_emissions(ctx
, prog
);
4181 /* Mark all generic shader inputs and outputs as unpaired. */
4182 for (unsigned i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4183 if (prog
->_LinkedShaders
[i
] != NULL
) {
4184 link_invalidate_variable_locations(prog
->_LinkedShaders
[i
]->ir
);
4188 if (!assign_attribute_or_color_locations(prog
, &ctx
->Const
,
4189 MESA_SHADER_VERTEX
)) {
4193 if (!assign_attribute_or_color_locations(prog
, &ctx
->Const
,
4194 MESA_SHADER_FRAGMENT
)) {
4198 unsigned first
, last
;
4200 first
= MESA_SHADER_STAGES
;
4203 /* Determine first and last stage. */
4204 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4205 if (!prog
->_LinkedShaders
[i
])
4207 if (first
== MESA_SHADER_STAGES
)
4212 if (num_tfeedback_decls
!= 0) {
4213 /* From GL_EXT_transform_feedback:
4214 * A program will fail to link if:
4216 * * the <count> specified by TransformFeedbackVaryingsEXT is
4217 * non-zero, but the program object has no vertex or geometry
4220 if (first
== MESA_SHADER_FRAGMENT
) {
4221 linker_error(prog
, "Transform feedback varyings specified, but "
4222 "no vertex or geometry shader is present.\n");
4226 tfeedback_decls
= ralloc_array(mem_ctx
, tfeedback_decl
,
4227 prog
->TransformFeedback
.NumVarying
);
4228 if (!parse_tfeedback_decls(ctx
, prog
, mem_ctx
, num_tfeedback_decls
,
4229 prog
->TransformFeedback
.VaryingNames
,
4234 /* Linking the stages in the opposite order (from fragment to vertex)
4235 * ensures that inter-shader outputs written to in an earlier stage are
4236 * eliminated if they are (transitively) not used in a later stage.
4240 if (first
< MESA_SHADER_FRAGMENT
) {
4241 gl_shader
*const sh
= prog
->_LinkedShaders
[last
];
4243 if (first
== MESA_SHADER_GEOMETRY
) {
4244 /* There was no vertex shader, but we still have to assign varying
4245 * locations for use by geometry shader inputs in SSO.
4247 * If the shader is not separable (i.e., prog->SeparateShader is
4248 * false), linking will have already failed when first is
4249 * MESA_SHADER_GEOMETRY.
4251 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4252 NULL
, prog
->_LinkedShaders
[first
],
4253 num_tfeedback_decls
, tfeedback_decls
))
4257 if (last
!= MESA_SHADER_FRAGMENT
&&
4258 (num_tfeedback_decls
!= 0 || prog
->SeparateShader
)) {
4259 /* There was no fragment shader, but we still have to assign varying
4260 * locations for use by transform feedback.
4262 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4264 num_tfeedback_decls
, tfeedback_decls
))
4268 do_dead_builtin_varyings(ctx
, sh
, NULL
,
4269 num_tfeedback_decls
, tfeedback_decls
);
4271 if (!prog
->SeparateShader
)
4272 demote_shader_inputs_and_outputs(sh
, ir_var_shader_out
);
4274 /* Eliminate code that is now dead due to unused outputs being demoted.
4276 while (do_dead_code(sh
->ir
, false))
4279 else if (first
== MESA_SHADER_FRAGMENT
) {
4280 /* If the program only contains a fragment shader...
4282 gl_shader
*const sh
= prog
->_LinkedShaders
[first
];
4284 do_dead_builtin_varyings(ctx
, NULL
, sh
,
4285 num_tfeedback_decls
, tfeedback_decls
);
4287 if (prog
->SeparateShader
) {
4288 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4289 NULL
/* producer */,
4291 0 /* num_tfeedback_decls */,
4292 NULL
/* tfeedback_decls */))
4295 demote_shader_inputs_and_outputs(sh
, ir_var_shader_in
);
4297 while (do_dead_code(sh
->ir
, false))
4302 for (int i
= next
- 1; i
>= 0; i
--) {
4303 if (prog
->_LinkedShaders
[i
] == NULL
)
4306 gl_shader
*const sh_i
= prog
->_LinkedShaders
[i
];
4307 gl_shader
*const sh_next
= prog
->_LinkedShaders
[next
];
4309 if (!assign_varying_locations(ctx
, mem_ctx
, prog
, sh_i
, sh_next
,
4310 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
4314 do_dead_builtin_varyings(ctx
, sh_i
, sh_next
,
4315 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
4318 demote_shader_inputs_and_outputs(sh_i
, ir_var_shader_out
);
4319 demote_shader_inputs_and_outputs(sh_next
, ir_var_shader_in
);
4321 /* Eliminate code that is now dead due to unused outputs being demoted.
4323 while (do_dead_code(sh_i
->ir
, false))
4325 while (do_dead_code(sh_next
->ir
, false))
4328 /* This must be done after all dead varyings are eliminated. */
4329 if (!check_against_output_limit(ctx
, prog
, sh_i
))
4331 if (!check_against_input_limit(ctx
, prog
, sh_next
))
4337 if (!store_tfeedback_info(ctx
, prog
, num_tfeedback_decls
, tfeedback_decls
))
4340 update_array_sizes(prog
);
4341 link_assign_uniform_locations(prog
, ctx
->Const
.UniformBooleanTrue
);
4342 link_assign_atomic_counter_resources(ctx
, prog
);
4343 store_fragdepth_layout(prog
);
4345 link_calculate_subroutine_compat(prog
);
4346 check_resources(ctx
, prog
);
4347 check_subroutine_resources(prog
);
4348 check_image_resources(ctx
, prog
);
4349 link_check_atomic_counter_resources(ctx
, prog
);
4351 if (!prog
->LinkStatus
)
4354 /* OpenGL ES requires that a vertex shader and a fragment shader both be
4355 * present in a linked program. GL_ARB_ES2_compatibility doesn't say
4356 * anything about shader linking when one of the shaders (vertex or
4357 * fragment shader) is absent. So, the extension shouldn't change the
4358 * behavior specified in GLSL specification.
4360 if (!prog
->SeparateShader
&& ctx
->API
== API_OPENGLES2
) {
4361 /* With ES < 3.1 one needs to have always vertex + fragment shader. */
4362 if (ctx
->Version
< 31) {
4363 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
4364 linker_error(prog
, "program lacks a vertex shader\n");
4365 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
4366 linker_error(prog
, "program lacks a fragment shader\n");
4369 /* From OpenGL ES 3.1 specification (7.3 Program Objects):
4370 * "Linking can fail for a variety of reasons as specified in the
4371 * OpenGL ES Shading Language Specification, as well as any of the
4372 * following reasons:
4376 * * program contains objects to form either a vertex shader or
4377 * fragment shader, and program is not separable, and does not
4378 * contain objects to form both a vertex shader and fragment
4381 if (!!prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] ^
4382 !!prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
4383 linker_error(prog
, "Program needs to contain both vertex and "
4384 "fragment shaders.\n");
4389 /* Split BufferInterfaceBlocks into UniformBlocks and ShaderStorageBlocks
4390 * for gl_shader_program and gl_shader, so that drivers that need separate
4391 * index spaces for each set can have that.
4393 for (unsigned i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_STAGES
; i
++) {
4394 if (prog
->_LinkedShaders
[i
] != NULL
) {
4395 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
4396 split_ubos_and_ssbos(sh
,
4397 sh
->BufferInterfaceBlocks
,
4398 sh
->NumBufferInterfaceBlocks
,
4400 &sh
->NumUniformBlocks
,
4401 &sh
->ShaderStorageBlocks
,
4402 &sh
->NumShaderStorageBlocks
);
4406 split_ubos_and_ssbos(prog
,
4407 prog
->BufferInterfaceBlocks
,
4408 prog
->NumBufferInterfaceBlocks
,
4409 &prog
->UniformBlocks
,
4410 &prog
->NumUniformBlocks
,
4411 &prog
->ShaderStorageBlocks
,
4412 &prog
->NumShaderStorageBlocks
);
4414 /* FINISHME: Assign fragment shader output locations. */
4417 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4418 free(shader_list
[i
]);
4419 if (prog
->_LinkedShaders
[i
] == NULL
)
4422 /* Do a final validation step to make sure that the IR wasn't
4423 * invalidated by any modifications performed after intrastage linking.
4425 validate_ir_tree(prog
->_LinkedShaders
[i
]->ir
);
4427 /* Retain any live IR, but trash the rest. */
4428 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
4430 /* The symbol table in the linked shaders may contain references to
4431 * variables that were removed (e.g., unused uniforms). Since it may
4432 * contain junk, there is no possible valid use. Delete it and set the
4435 delete prog
->_LinkedShaders
[i
]->symbols
;
4436 prog
->_LinkedShaders
[i
]->symbols
= NULL
;
4439 ralloc_free(mem_ctx
);