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"
75 #include "program/prog_instruction.h"
78 #include "link_varyings.h"
79 #include "ir_optimization.h"
80 #include "ir_rvalue_visitor.h"
81 #include "ir_uniform.h"
83 #include "main/shaderobj.h"
84 #include "main/enums.h"
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 array_resize_visitor
: public ir_hierarchical_visitor
{
186 unsigned num_vertices
;
187 gl_shader_program
*prog
;
188 gl_shader_stage stage
;
190 array_resize_visitor(unsigned num_vertices
,
191 gl_shader_program
*prog
,
192 gl_shader_stage stage
)
194 this->num_vertices
= num_vertices
;
199 virtual ~array_resize_visitor()
204 virtual ir_visitor_status
visit(ir_variable
*var
)
206 if (!var
->type
->is_array() || var
->data
.mode
!= ir_var_shader_in
||
208 return visit_continue
;
210 unsigned size
= var
->type
->length
;
212 if (stage
== MESA_SHADER_GEOMETRY
) {
213 /* Generate a link error if the shader has declared this array with
216 if (!var
->data
.implicit_sized_array
&&
217 size
&& size
!= this->num_vertices
) {
218 linker_error(this->prog
, "size of array %s declared as %u, "
219 "but number of input vertices is %u\n",
220 var
->name
, size
, this->num_vertices
);
221 return visit_continue
;
224 /* Generate a link error if the shader attempts to access an input
225 * array using an index too large for its actual size assigned at
228 if (var
->data
.max_array_access
>= (int)this->num_vertices
) {
229 linker_error(this->prog
, "%s shader accesses element %i of "
230 "%s, but only %i input vertices\n",
231 _mesa_shader_stage_to_string(this->stage
),
232 var
->data
.max_array_access
, var
->name
, this->num_vertices
);
233 return visit_continue
;
237 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
239 var
->data
.max_array_access
= this->num_vertices
- 1;
241 return visit_continue
;
244 /* Dereferences of input variables need to be updated so that their type
245 * matches the newly assigned type of the variable they are accessing. */
246 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
248 ir
->type
= ir
->var
->type
;
249 return visit_continue
;
252 /* Dereferences of 2D input arrays need to be updated so that their type
253 * matches the newly assigned type of the array they are accessing. */
254 virtual ir_visitor_status
visit_leave(ir_dereference_array
*ir
)
256 const glsl_type
*const vt
= ir
->array
->type
;
258 ir
->type
= vt
->fields
.array
;
259 return visit_continue
;
263 class barrier_use_visitor
: public ir_hierarchical_visitor
{
265 barrier_use_visitor(gl_shader_program
*prog
)
266 : prog(prog
), in_main(false), after_return(false), control_flow(0)
270 virtual ~barrier_use_visitor()
275 virtual ir_visitor_status
visit_enter(ir_function
*ir
)
277 if (strcmp(ir
->name
, "main") == 0)
280 return visit_continue
;
283 virtual ir_visitor_status
visit_leave(ir_function
*)
286 after_return
= false;
287 return visit_continue
;
290 virtual ir_visitor_status
visit_leave(ir_return
*)
293 return visit_continue
;
296 virtual ir_visitor_status
visit_enter(ir_if
*)
299 return visit_continue
;
302 virtual ir_visitor_status
visit_leave(ir_if
*)
305 return visit_continue
;
308 virtual ir_visitor_status
visit_enter(ir_loop
*)
311 return visit_continue
;
314 virtual ir_visitor_status
visit_leave(ir_loop
*)
317 return visit_continue
;
320 /* FINISHME: `switch` is not expressed at the IR level -- it's already
321 * been lowered to a mess of `if`s. We'll correctly disallow any use of
322 * barrier() in a conditional path within the switch, but not in a path
323 * which is always hit.
326 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
328 if (ir
->use_builtin
&& strcmp(ir
->callee_name(), "barrier") == 0) {
329 /* Use of barrier(); determine if it is legal: */
331 linker_error(prog
, "Builtin barrier() may only be used in main");
336 linker_error(prog
, "Builtin barrier() may not be used after return");
340 if (control_flow
!= 0) {
341 linker_error(prog
, "Builtin barrier() may not be used inside control flow");
345 return visit_continue
;
349 gl_shader_program
*prog
;
350 bool in_main
, after_return
;
355 * Visitor that determines the highest stream id to which a (geometry) shader
356 * emits vertices. It also checks whether End{Stream}Primitive is ever called.
358 class find_emit_vertex_visitor
: public ir_hierarchical_visitor
{
360 find_emit_vertex_visitor(int max_allowed
)
361 : max_stream_allowed(max_allowed
),
362 invalid_stream_id(0),
363 invalid_stream_id_from_emit_vertex(false),
364 end_primitive_found(false),
365 uses_non_zero_stream(false)
370 virtual ir_visitor_status
visit_leave(ir_emit_vertex
*ir
)
372 int stream_id
= ir
->stream_id();
375 invalid_stream_id
= stream_id
;
376 invalid_stream_id_from_emit_vertex
= true;
380 if (stream_id
> max_stream_allowed
) {
381 invalid_stream_id
= stream_id
;
382 invalid_stream_id_from_emit_vertex
= true;
387 uses_non_zero_stream
= true;
389 return visit_continue
;
392 virtual ir_visitor_status
visit_leave(ir_end_primitive
*ir
)
394 end_primitive_found
= true;
396 int stream_id
= ir
->stream_id();
399 invalid_stream_id
= stream_id
;
400 invalid_stream_id_from_emit_vertex
= false;
404 if (stream_id
> max_stream_allowed
) {
405 invalid_stream_id
= stream_id
;
406 invalid_stream_id_from_emit_vertex
= false;
411 uses_non_zero_stream
= true;
413 return visit_continue
;
418 return invalid_stream_id
!= 0;
421 const char *error_func()
423 return invalid_stream_id_from_emit_vertex
?
424 "EmitStreamVertex" : "EndStreamPrimitive";
429 return invalid_stream_id
;
434 return uses_non_zero_stream
;
437 bool uses_end_primitive()
439 return end_primitive_found
;
443 int max_stream_allowed
;
444 int invalid_stream_id
;
445 bool invalid_stream_id_from_emit_vertex
;
446 bool end_primitive_found
;
447 bool uses_non_zero_stream
;
450 /* Class that finds array derefs and check if indexes are dynamic. */
451 class dynamic_sampler_array_indexing_visitor
: public ir_hierarchical_visitor
454 dynamic_sampler_array_indexing_visitor() :
455 dynamic_sampler_array_indexing(false)
459 ir_visitor_status
visit_enter(ir_dereference_array
*ir
)
461 if (!ir
->variable_referenced())
462 return visit_continue
;
464 if (!ir
->variable_referenced()->type
->contains_sampler())
465 return visit_continue
;
467 if (!ir
->array_index
->constant_expression_value()) {
468 dynamic_sampler_array_indexing
= true;
471 return visit_continue
;
474 bool uses_dynamic_sampler_array_indexing()
476 return dynamic_sampler_array_indexing
;
480 bool dynamic_sampler_array_indexing
;
483 } /* anonymous namespace */
486 linker_error(gl_shader_program
*prog
, const char *fmt
, ...)
490 ralloc_strcat(&prog
->InfoLog
, "error: ");
492 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
495 prog
->LinkStatus
= false;
500 linker_warning(gl_shader_program
*prog
, const char *fmt
, ...)
504 ralloc_strcat(&prog
->InfoLog
, "warning: ");
506 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
513 * Given a string identifying a program resource, break it into a base name
514 * and an optional array index in square brackets.
516 * If an array index is present, \c out_base_name_end is set to point to the
517 * "[" that precedes the array index, and the array index itself is returned
520 * If no array index is present (or if the array index is negative or
521 * mal-formed), \c out_base_name_end, is set to point to the null terminator
522 * at the end of the input string, and -1 is returned.
524 * Only the final array index is parsed; if the string contains other array
525 * indices (or structure field accesses), they are left in the base name.
527 * No attempt is made to check that the base name is properly formed;
528 * typically the caller will look up the base name in a hash table, so
529 * ill-formed base names simply turn into hash table lookup failures.
532 parse_program_resource_name(const GLchar
*name
,
533 const GLchar
**out_base_name_end
)
535 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
537 * "When an integer array element or block instance number is part of
538 * the name string, it will be specified in decimal form without a "+"
539 * or "-" sign or any extra leading zeroes. Additionally, the name
540 * string will not include white space anywhere in the string."
543 const size_t len
= strlen(name
);
544 *out_base_name_end
= name
+ len
;
546 if (len
== 0 || name
[len
-1] != ']')
549 /* Walk backwards over the string looking for a non-digit character. This
550 * had better be the opening bracket for an array index.
552 * Initially, i specifies the location of the ']'. Since the string may
553 * contain only the ']' charcater, walk backwards very carefully.
556 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
559 if ((i
== 0) || name
[i
-1] != '[')
562 long array_index
= strtol(&name
[i
], NULL
, 10);
566 /* Check for leading zero */
567 if (name
[i
] == '0' && name
[i
+1] != ']')
570 *out_base_name_end
= name
+ (i
- 1);
576 link_invalidate_variable_locations(exec_list
*ir
)
578 foreach_in_list(ir_instruction
, node
, ir
) {
579 ir_variable
*const var
= node
->as_variable();
584 /* Only assign locations for variables that lack an explicit location.
585 * Explicit locations are set for all built-in variables, generic vertex
586 * shader inputs (via layout(location=...)), and generic fragment shader
587 * outputs (also via layout(location=...)).
589 if (!var
->data
.explicit_location
) {
590 var
->data
.location
= -1;
591 var
->data
.location_frac
= 0;
594 /* ir_variable::is_unmatched_generic_inout is used by the linker while
595 * connecting outputs from one stage to inputs of the next stage.
597 if (var
->data
.explicit_location
&&
598 var
->data
.location
< VARYING_SLOT_VAR0
) {
599 var
->data
.is_unmatched_generic_inout
= 0;
601 var
->data
.is_unmatched_generic_inout
= 1;
608 * Set clip_distance_array_size based and cull_distance_array_size on the given
611 * Also check for errors based on incorrect usage of gl_ClipVertex and
612 * gl_ClipDistance and gl_CullDistance.
613 * Additionally test whether the arrays gl_ClipDistance and gl_CullDistance
614 * exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances.
616 * Return false if an error was reported.
619 analyze_clip_cull_usage(struct gl_shader_program
*prog
,
620 struct gl_linked_shader
*shader
,
621 struct gl_context
*ctx
,
622 GLuint
*clip_distance_array_size
,
623 GLuint
*cull_distance_array_size
)
625 *clip_distance_array_size
= 0;
626 *cull_distance_array_size
= 0;
628 if (prog
->Version
>= (prog
->IsES
? 300 : 130)) {
629 /* From section 7.1 (Vertex Shader Special Variables) of the
632 * "It is an error for a shader to statically write both
633 * gl_ClipVertex and gl_ClipDistance."
635 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
636 * gl_ClipVertex nor gl_ClipDistance. However with
637 * GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0.
639 find_assignment_visitor
clip_distance("gl_ClipDistance");
640 find_assignment_visitor
cull_distance("gl_CullDistance");
642 clip_distance
.run(shader
->ir
);
643 cull_distance
.run(shader
->ir
);
645 /* From the ARB_cull_distance spec:
647 * It is a compile-time or link-time error for the set of shaders forming
648 * a program to statically read or write both gl_ClipVertex and either
649 * gl_ClipDistance or gl_CullDistance.
651 * This does not apply to GLSL ES shaders, since GLSL ES doesn't define
655 find_assignment_visitor
clip_vertex("gl_ClipVertex");
657 clip_vertex
.run(shader
->ir
);
659 if (clip_vertex
.variable_found() && clip_distance
.variable_found()) {
660 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
661 "and `gl_ClipDistance'\n",
662 _mesa_shader_stage_to_string(shader
->Stage
));
665 if (clip_vertex
.variable_found() && cull_distance
.variable_found()) {
666 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
667 "and `gl_CullDistance'\n",
668 _mesa_shader_stage_to_string(shader
->Stage
));
673 if (clip_distance
.variable_found()) {
674 ir_variable
*clip_distance_var
=
675 shader
->symbols
->get_variable("gl_ClipDistance");
676 assert(clip_distance_var
);
677 *clip_distance_array_size
= clip_distance_var
->type
->length
;
679 if (cull_distance
.variable_found()) {
680 ir_variable
*cull_distance_var
=
681 shader
->symbols
->get_variable("gl_CullDistance");
682 assert(cull_distance_var
);
683 *cull_distance_array_size
= cull_distance_var
->type
->length
;
685 /* From the ARB_cull_distance spec:
687 * It is a compile-time or link-time error for the set of shaders forming
688 * a program to have the sum of the sizes of the gl_ClipDistance and
689 * gl_CullDistance arrays to be larger than
690 * gl_MaxCombinedClipAndCullDistances.
692 if ((*clip_distance_array_size
+ *cull_distance_array_size
) >
693 ctx
->Const
.MaxClipPlanes
) {
694 linker_error(prog
, "%s shader: the combined size of "
695 "'gl_ClipDistance' and 'gl_CullDistance' size cannot "
697 "gl_MaxCombinedClipAndCullDistances (%u)",
698 _mesa_shader_stage_to_string(shader
->Stage
),
699 ctx
->Const
.MaxClipPlanes
);
706 * Verify that a vertex shader executable meets all semantic requirements.
708 * Also sets prog->Vert.ClipDistanceArraySize and
709 * prog->Vert.CullDistanceArraySize as a side effect.
711 * \param shader Vertex shader executable to be verified
714 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
715 struct gl_linked_shader
*shader
,
716 struct gl_context
*ctx
)
721 /* From the GLSL 1.10 spec, page 48:
723 * "The variable gl_Position is available only in the vertex
724 * language and is intended for writing the homogeneous vertex
725 * position. All executions of a well-formed vertex shader
726 * executable must write a value into this variable. [...] The
727 * variable gl_Position is available only in the vertex
728 * language and is intended for writing the homogeneous vertex
729 * position. All executions of a well-formed vertex shader
730 * executable must write a value into this variable."
732 * while in GLSL 1.40 this text is changed to:
734 * "The variable gl_Position is available only in the vertex
735 * language and is intended for writing the homogeneous vertex
736 * position. It can be written at any time during shader
737 * execution. It may also be read back by a vertex shader
738 * after being written. This value will be used by primitive
739 * assembly, clipping, culling, and other fixed functionality
740 * operations, if present, that operate on primitives after
741 * vertex processing has occurred. Its value is undefined if
742 * the vertex shader executable does not write gl_Position."
744 * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
745 * gl_Position is not an error.
747 if (prog
->Version
< (prog
->IsES
? 300 : 140)) {
748 find_assignment_visitor
find("gl_Position");
749 find
.run(shader
->ir
);
750 if (!find
.variable_found()) {
753 "vertex shader does not write to `gl_Position'."
754 "It's value is undefined. \n");
757 "vertex shader does not write to `gl_Position'. \n");
763 analyze_clip_cull_usage(prog
, shader
, ctx
,
764 &prog
->Vert
.ClipDistanceArraySize
,
765 &prog
->Vert
.CullDistanceArraySize
);
769 validate_tess_eval_shader_executable(struct gl_shader_program
*prog
,
770 struct gl_linked_shader
*shader
,
771 struct gl_context
*ctx
)
776 analyze_clip_cull_usage(prog
, shader
, ctx
,
777 &prog
->TessEval
.ClipDistanceArraySize
,
778 &prog
->TessEval
.CullDistanceArraySize
);
783 * Verify that a fragment shader executable meets all semantic requirements
785 * \param shader Fragment shader executable to be verified
788 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
789 struct gl_linked_shader
*shader
)
794 find_assignment_visitor
frag_color("gl_FragColor");
795 find_assignment_visitor
frag_data("gl_FragData");
797 frag_color
.run(shader
->ir
);
798 frag_data
.run(shader
->ir
);
800 if (frag_color
.variable_found() && frag_data
.variable_found()) {
801 linker_error(prog
, "fragment shader writes to both "
802 "`gl_FragColor' and `gl_FragData'\n");
807 * Verify that a geometry shader executable meets all semantic requirements
809 * Also sets prog->Geom.VerticesIn, and prog->Geom.ClipDistanceArraySize and
810 * prog->Geom.CullDistanceArraySize as a side effect.
812 * \param shader Geometry shader executable to be verified
815 validate_geometry_shader_executable(struct gl_shader_program
*prog
,
816 struct gl_linked_shader
*shader
,
817 struct gl_context
*ctx
)
822 unsigned num_vertices
= vertices_per_prim(shader
->info
.Geom
.InputType
);
823 prog
->Geom
.VerticesIn
= num_vertices
;
825 analyze_clip_cull_usage(prog
, shader
, ctx
,
826 &prog
->Geom
.ClipDistanceArraySize
,
827 &prog
->Geom
.CullDistanceArraySize
);
831 * Check if geometry shaders emit to non-zero streams and do corresponding
835 validate_geometry_shader_emissions(struct gl_context
*ctx
,
836 struct gl_shader_program
*prog
)
838 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
841 find_emit_vertex_visitor
emit_vertex(ctx
->Const
.MaxVertexStreams
- 1);
842 emit_vertex
.run(sh
->ir
);
843 if (emit_vertex
.error()) {
844 linker_error(prog
, "Invalid call %s(%d). Accepted values for the "
845 "stream parameter are in the range [0, %d].\n",
846 emit_vertex
.error_func(),
847 emit_vertex
.error_stream(),
848 ctx
->Const
.MaxVertexStreams
- 1);
850 prog
->Geom
.UsesStreams
= emit_vertex
.uses_streams();
851 prog
->Geom
.UsesEndPrimitive
= emit_vertex
.uses_end_primitive();
853 /* From the ARB_gpu_shader5 spec:
855 * "Multiple vertex streams are supported only if the output primitive
856 * type is declared to be "points". A program will fail to link if it
857 * contains a geometry shader calling EmitStreamVertex() or
858 * EndStreamPrimitive() if its output primitive type is not "points".
860 * However, in the same spec:
862 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
863 * with <stream> set to zero."
867 * "The function EndPrimitive() is equivalent to calling
868 * EndStreamPrimitive() with <stream> set to zero."
870 * Since we can call EmitVertex() and EndPrimitive() when we output
871 * primitives other than points, calling EmitStreamVertex(0) or
872 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
873 * does. Currently we only set prog->Geom.UsesStreams to TRUE when
874 * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
877 if (prog
->Geom
.UsesStreams
&& sh
->info
.Geom
.OutputType
!= GL_POINTS
) {
878 linker_error(prog
, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
879 "with n>0 requires point output\n");
885 validate_intrastage_arrays(struct gl_shader_program
*prog
,
886 ir_variable
*const var
,
887 ir_variable
*const existing
)
889 /* Consider the types to be "the same" if both types are arrays
890 * of the same type and one of the arrays is implicitly sized.
891 * In addition, set the type of the linked variable to the
892 * explicitly sized array.
894 if (var
->type
->is_array() && existing
->type
->is_array()) {
895 if ((var
->type
->fields
.array
== existing
->type
->fields
.array
) &&
896 ((var
->type
->length
== 0)|| (existing
->type
->length
== 0))) {
897 if (var
->type
->length
!= 0) {
898 if ((int)var
->type
->length
<= existing
->data
.max_array_access
) {
899 linker_error(prog
, "%s `%s' declared as type "
900 "`%s' but outermost dimension has an index"
903 var
->name
, var
->type
->name
,
904 existing
->data
.max_array_access
);
906 existing
->type
= var
->type
;
908 } else if (existing
->type
->length
!= 0) {
909 if((int)existing
->type
->length
<= var
->data
.max_array_access
&&
910 !existing
->data
.from_ssbo_unsized_array
) {
911 linker_error(prog
, "%s `%s' declared as type "
912 "`%s' but outermost dimension has an index"
915 var
->name
, existing
->type
->name
,
916 var
->data
.max_array_access
);
921 /* The arrays of structs could have different glsl_type pointers but
922 * they are actually the same type. Use record_compare() to check that.
924 if (existing
->type
->fields
.array
->is_record() &&
925 var
->type
->fields
.array
->is_record() &&
926 existing
->type
->fields
.array
->record_compare(var
->type
->fields
.array
))
935 * Perform validation of global variables used across multiple shaders
938 cross_validate_globals(struct gl_shader_program
*prog
,
939 struct exec_list
*ir
, glsl_symbol_table
*variables
,
942 foreach_in_list(ir_instruction
, node
, ir
) {
943 ir_variable
*const var
= node
->as_variable();
948 if (uniforms_only
&& (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
))
951 /* don't cross validate subroutine uniforms */
952 if (var
->type
->contains_subroutine())
955 /* Don't cross validate temporaries that are at global scope. These
956 * will eventually get pulled into the shaders 'main'.
958 if (var
->data
.mode
== ir_var_temporary
)
961 /* If a global with this name has already been seen, verify that the
962 * new instance has the same type. In addition, if the globals have
963 * initializers, the values of the initializers must be the same.
965 ir_variable
*const existing
= variables
->get_variable(var
->name
);
966 if (existing
!= NULL
) {
967 /* Check if types match. Interface blocks have some special
968 * rules so we handle those elsewhere.
970 if (var
->type
!= existing
->type
&&
971 !var
->is_interface_instance()) {
972 if (!validate_intrastage_arrays(prog
, var
, existing
)) {
973 if (var
->type
->is_record() && existing
->type
->is_record()
974 && existing
->type
->record_compare(var
->type
)) {
975 existing
->type
= var
->type
;
977 /* If it is an unsized array in a Shader Storage Block,
978 * two different shaders can access to different elements.
979 * Because of that, they might be converted to different
980 * sized arrays, then check that they are compatible but
981 * ignore the array size.
983 if (!(var
->data
.mode
== ir_var_shader_storage
&&
984 var
->data
.from_ssbo_unsized_array
&&
985 existing
->data
.mode
== ir_var_shader_storage
&&
986 existing
->data
.from_ssbo_unsized_array
&&
987 var
->type
->gl_type
== existing
->type
->gl_type
)) {
988 linker_error(prog
, "%s `%s' declared as type "
989 "`%s' and type `%s'\n",
991 var
->name
, var
->type
->name
,
992 existing
->type
->name
);
999 if (var
->data
.explicit_location
) {
1000 if (existing
->data
.explicit_location
1001 && (var
->data
.location
!= existing
->data
.location
)) {
1002 linker_error(prog
, "explicit locations for %s "
1003 "`%s' have differing values\n",
1004 mode_string(var
), var
->name
);
1008 if (var
->data
.location_frac
!= existing
->data
.location_frac
) {
1009 linker_error(prog
, "explicit components for %s `%s' have "
1010 "differing values\n", mode_string(var
), var
->name
);
1014 existing
->data
.location
= var
->data
.location
;
1015 existing
->data
.explicit_location
= true;
1017 /* Check if uniform with implicit location was marked explicit
1018 * by earlier shader stage. If so, mark it explicit in this stage
1019 * too to make sure later processing does not treat it as
1022 if (existing
->data
.explicit_location
) {
1023 var
->data
.location
= existing
->data
.location
;
1024 var
->data
.explicit_location
= true;
1028 /* From the GLSL 4.20 specification:
1029 * "A link error will result if two compilation units in a program
1030 * specify different integer-constant bindings for the same
1031 * opaque-uniform name. However, it is not an error to specify a
1032 * binding on some but not all declarations for the same name"
1034 if (var
->data
.explicit_binding
) {
1035 if (existing
->data
.explicit_binding
&&
1036 var
->data
.binding
!= existing
->data
.binding
) {
1037 linker_error(prog
, "explicit bindings for %s "
1038 "`%s' have differing values\n",
1039 mode_string(var
), var
->name
);
1043 existing
->data
.binding
= var
->data
.binding
;
1044 existing
->data
.explicit_binding
= true;
1047 if (var
->type
->contains_atomic() &&
1048 var
->data
.offset
!= existing
->data
.offset
) {
1049 linker_error(prog
, "offset specifications for %s "
1050 "`%s' have differing values\n",
1051 mode_string(var
), var
->name
);
1055 /* Validate layout qualifiers for gl_FragDepth.
1057 * From the AMD/ARB_conservative_depth specs:
1059 * "If gl_FragDepth is redeclared in any fragment shader in a
1060 * program, it must be redeclared in all fragment shaders in
1061 * that program that have static assignments to
1062 * gl_FragDepth. All redeclarations of gl_FragDepth in all
1063 * fragment shaders in a single program must have the same set
1066 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
1067 bool layout_declared
= var
->data
.depth_layout
!= ir_depth_layout_none
;
1068 bool layout_differs
=
1069 var
->data
.depth_layout
!= existing
->data
.depth_layout
;
1071 if (layout_declared
&& layout_differs
) {
1073 "All redeclarations of gl_FragDepth in all "
1074 "fragment shaders in a single program must have "
1075 "the same set of qualifiers.\n");
1078 if (var
->data
.used
&& layout_differs
) {
1080 "If gl_FragDepth is redeclared with a layout "
1081 "qualifier in any fragment shader, it must be "
1082 "redeclared with the same layout qualifier in "
1083 "all fragment shaders that have assignments to "
1088 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1090 * "If a shared global has multiple initializers, the
1091 * initializers must all be constant expressions, and they
1092 * must all have the same value. Otherwise, a link error will
1093 * result. (A shared global having only one initializer does
1094 * not require that initializer to be a constant expression.)"
1096 * Previous to 4.20 the GLSL spec simply said that initializers
1097 * must have the same value. In this case of non-constant
1098 * initializers, this was impossible to determine. As a result,
1099 * no vendor actually implemented that behavior. The 4.20
1100 * behavior matches the implemented behavior of at least one other
1101 * vendor, so we'll implement that for all GLSL versions.
1103 if (var
->constant_initializer
!= NULL
) {
1104 if (existing
->constant_initializer
!= NULL
) {
1105 if (!var
->constant_initializer
->has_value(existing
->constant_initializer
)) {
1106 linker_error(prog
, "initializers for %s "
1107 "`%s' have differing values\n",
1108 mode_string(var
), var
->name
);
1112 /* If the first-seen instance of a particular uniform did
1113 * not have an initializer but a later instance does,
1114 * replace the former with the later.
1116 variables
->replace_variable(existing
->name
, var
);
1120 if (var
->data
.has_initializer
) {
1121 if (existing
->data
.has_initializer
1122 && (var
->constant_initializer
== NULL
1123 || existing
->constant_initializer
== NULL
)) {
1125 "shared global variable `%s' has multiple "
1126 "non-constant initializers.\n",
1132 if (existing
->data
.invariant
!= var
->data
.invariant
) {
1133 linker_error(prog
, "declarations for %s `%s' have "
1134 "mismatching invariant qualifiers\n",
1135 mode_string(var
), var
->name
);
1138 if (existing
->data
.centroid
!= var
->data
.centroid
) {
1139 linker_error(prog
, "declarations for %s `%s' have "
1140 "mismatching centroid qualifiers\n",
1141 mode_string(var
), var
->name
);
1144 if (existing
->data
.sample
!= var
->data
.sample
) {
1145 linker_error(prog
, "declarations for %s `%s` have "
1146 "mismatching sample qualifiers\n",
1147 mode_string(var
), var
->name
);
1150 if (existing
->data
.image_format
!= var
->data
.image_format
) {
1151 linker_error(prog
, "declarations for %s `%s` have "
1152 "mismatching image format qualifiers\n",
1153 mode_string(var
), var
->name
);
1157 variables
->add_variable(var
);
1163 * Perform validation of uniforms used across multiple shader stages
1166 cross_validate_uniforms(struct gl_shader_program
*prog
)
1168 glsl_symbol_table variables
;
1169 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1170 if (prog
->_LinkedShaders
[i
] == NULL
)
1173 cross_validate_globals(prog
, prog
->_LinkedShaders
[i
]->ir
, &variables
,
1179 * Accumulates the array of buffer blocks and checks that all definitions of
1180 * blocks agree on their contents.
1183 interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
,
1186 int *InterfaceBlockStageIndex
[MESA_SHADER_STAGES
];
1187 struct gl_uniform_block
*blks
= NULL
;
1188 unsigned *num_blks
= validate_ssbo
? &prog
->NumShaderStorageBlocks
:
1189 &prog
->NumUniformBlocks
;
1191 unsigned max_num_buffer_blocks
= 0;
1192 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1193 if (prog
->_LinkedShaders
[i
]) {
1194 if (validate_ssbo
) {
1195 max_num_buffer_blocks
+=
1196 prog
->_LinkedShaders
[i
]->NumShaderStorageBlocks
;
1198 max_num_buffer_blocks
+=
1199 prog
->_LinkedShaders
[i
]->NumUniformBlocks
;
1204 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1205 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1207 InterfaceBlockStageIndex
[i
] = new int[max_num_buffer_blocks
];
1208 for (unsigned int j
= 0; j
< max_num_buffer_blocks
; j
++)
1209 InterfaceBlockStageIndex
[i
][j
] = -1;
1214 unsigned sh_num_blocks
;
1215 struct gl_uniform_block
**sh_blks
;
1216 if (validate_ssbo
) {
1217 sh_num_blocks
= prog
->_LinkedShaders
[i
]->NumShaderStorageBlocks
;
1218 sh_blks
= sh
->ShaderStorageBlocks
;
1220 sh_num_blocks
= prog
->_LinkedShaders
[i
]->NumUniformBlocks
;
1221 sh_blks
= sh
->UniformBlocks
;
1224 for (unsigned int j
= 0; j
< sh_num_blocks
; j
++) {
1225 int index
= link_cross_validate_uniform_block(prog
, &blks
, num_blks
,
1229 linker_error(prog
, "buffer block `%s' has mismatching "
1230 "definitions\n", sh_blks
[j
]->Name
);
1232 for (unsigned k
= 0; k
<= i
; k
++) {
1233 delete[] InterfaceBlockStageIndex
[k
];
1238 InterfaceBlockStageIndex
[i
][index
] = j
;
1242 /* Update per stage block pointers to point to the program list.
1243 * FIXME: We should be able to free the per stage blocks here.
1245 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1246 for (unsigned j
= 0; j
< *num_blks
; j
++) {
1247 int stage_index
= InterfaceBlockStageIndex
[i
][j
];
1249 if (stage_index
!= -1) {
1250 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1252 blks
[j
].stageref
|= (1 << i
);
1254 struct gl_uniform_block
**sh_blks
= validate_ssbo
?
1255 sh
->ShaderStorageBlocks
: sh
->UniformBlocks
;
1257 sh_blks
[stage_index
] = &blks
[j
];
1262 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1263 delete[] InterfaceBlockStageIndex
[i
];
1267 prog
->ShaderStorageBlocks
= blks
;
1269 prog
->UniformBlocks
= blks
;
1276 * Populates a shaders symbol table with all global declarations
1279 populate_symbol_table(gl_linked_shader
*sh
)
1281 sh
->symbols
= new(sh
) glsl_symbol_table
;
1283 foreach_in_list(ir_instruction
, inst
, sh
->ir
) {
1287 if ((func
= inst
->as_function()) != NULL
) {
1288 sh
->symbols
->add_function(func
);
1289 } else if ((var
= inst
->as_variable()) != NULL
) {
1290 if (var
->data
.mode
!= ir_var_temporary
)
1291 sh
->symbols
->add_variable(var
);
1298 * Remap variables referenced in an instruction tree
1300 * This is used when instruction trees are cloned from one shader and placed in
1301 * another. These trees will contain references to \c ir_variable nodes that
1302 * do not exist in the target shader. This function finds these \c ir_variable
1303 * references and replaces the references with matching variables in the target
1306 * If there is no matching variable in the target shader, a clone of the
1307 * \c ir_variable is made and added to the target shader. The new variable is
1308 * added to \b both the instruction stream and the symbol table.
1310 * \param inst IR tree that is to be processed.
1311 * \param symbols Symbol table containing global scope symbols in the
1313 * \param instructions Instruction stream where new variable declarations
1317 remap_variables(ir_instruction
*inst
, struct gl_linked_shader
*target
,
1320 class remap_visitor
: public ir_hierarchical_visitor
{
1322 remap_visitor(struct gl_linked_shader
*target
,
1325 this->target
= target
;
1326 this->symbols
= target
->symbols
;
1327 this->instructions
= target
->ir
;
1328 this->temps
= temps
;
1331 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
1333 if (ir
->var
->data
.mode
== ir_var_temporary
) {
1334 ir_variable
*var
= (ir_variable
*) hash_table_find(temps
, ir
->var
);
1336 assert(var
!= NULL
);
1338 return visit_continue
;
1341 ir_variable
*const existing
=
1342 this->symbols
->get_variable(ir
->var
->name
);
1343 if (existing
!= NULL
)
1346 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
1348 this->symbols
->add_variable(copy
);
1349 this->instructions
->push_head(copy
);
1353 return visit_continue
;
1357 struct gl_linked_shader
*target
;
1358 glsl_symbol_table
*symbols
;
1359 exec_list
*instructions
;
1363 remap_visitor
v(target
, temps
);
1370 * Move non-declarations from one instruction stream to another
1372 * The intended usage pattern of this function is to pass the pointer to the
1373 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
1374 * pointer) for \c last and \c false for \c make_copies on the first
1375 * call. Successive calls pass the return value of the previous call for
1376 * \c last and \c true for \c make_copies.
1378 * \param instructions Source instruction stream
1379 * \param last Instruction after which new instructions should be
1380 * inserted in the target instruction stream
1381 * \param make_copies Flag selecting whether instructions in \c instructions
1382 * should be copied (via \c ir_instruction::clone) into the
1383 * target list or moved.
1386 * The new "last" instruction in the target instruction stream. This pointer
1387 * is suitable for use as the \c last parameter of a later call to this
1391 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
1392 bool make_copies
, gl_linked_shader
*target
)
1394 hash_table
*temps
= NULL
;
1397 temps
= hash_table_ctor(0, hash_table_pointer_hash
,
1398 hash_table_pointer_compare
);
1400 foreach_in_list_safe(ir_instruction
, inst
, instructions
) {
1401 if (inst
->as_function())
1404 ir_variable
*var
= inst
->as_variable();
1405 if ((var
!= NULL
) && (var
->data
.mode
!= ir_var_temporary
))
1408 assert(inst
->as_assignment()
1410 || inst
->as_if() /* for initializers with the ?: operator */
1411 || ((var
!= NULL
) && (var
->data
.mode
== ir_var_temporary
)));
1414 inst
= inst
->clone(target
, NULL
);
1417 hash_table_insert(temps
, inst
, var
);
1419 remap_variables(inst
, target
, temps
);
1424 last
->insert_after(inst
);
1429 hash_table_dtor(temps
);
1436 * This class is only used in link_intrastage_shaders() below but declaring
1437 * it inside that function leads to compiler warnings with some versions of
1440 class array_sizing_visitor
: public ir_hierarchical_visitor
{
1442 array_sizing_visitor()
1443 : mem_ctx(ralloc_context(NULL
)),
1444 unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash
,
1445 hash_table_pointer_compare
))
1449 ~array_sizing_visitor()
1451 hash_table_dtor(this->unnamed_interfaces
);
1452 ralloc_free(this->mem_ctx
);
1455 virtual ir_visitor_status
visit(ir_variable
*var
)
1457 const glsl_type
*type_without_array
;
1458 bool implicit_sized_array
= var
->data
.implicit_sized_array
;
1459 fixup_type(&var
->type
, var
->data
.max_array_access
,
1460 var
->data
.from_ssbo_unsized_array
,
1461 &implicit_sized_array
);
1462 var
->data
.implicit_sized_array
= implicit_sized_array
;
1463 type_without_array
= var
->type
->without_array();
1464 if (var
->type
->is_interface()) {
1465 if (interface_contains_unsized_arrays(var
->type
)) {
1466 const glsl_type
*new_type
=
1467 resize_interface_members(var
->type
,
1468 var
->get_max_ifc_array_access(),
1469 var
->is_in_shader_storage_block());
1470 var
->type
= new_type
;
1471 var
->change_interface_type(new_type
);
1473 } else if (type_without_array
->is_interface()) {
1474 if (interface_contains_unsized_arrays(type_without_array
)) {
1475 const glsl_type
*new_type
=
1476 resize_interface_members(type_without_array
,
1477 var
->get_max_ifc_array_access(),
1478 var
->is_in_shader_storage_block());
1479 var
->change_interface_type(new_type
);
1480 var
->type
= update_interface_members_array(var
->type
, new_type
);
1482 } else if (const glsl_type
*ifc_type
= var
->get_interface_type()) {
1483 /* Store a pointer to the variable in the unnamed_interfaces
1486 ir_variable
**interface_vars
= (ir_variable
**)
1487 hash_table_find(this->unnamed_interfaces
, ifc_type
);
1488 if (interface_vars
== NULL
) {
1489 interface_vars
= rzalloc_array(mem_ctx
, ir_variable
*,
1491 hash_table_insert(this->unnamed_interfaces
, interface_vars
,
1494 unsigned index
= ifc_type
->field_index(var
->name
);
1495 assert(index
< ifc_type
->length
);
1496 assert(interface_vars
[index
] == NULL
);
1497 interface_vars
[index
] = var
;
1499 return visit_continue
;
1503 * For each unnamed interface block that was discovered while running the
1504 * visitor, adjust the interface type to reflect the newly assigned array
1505 * sizes, and fix up the ir_variable nodes to point to the new interface
1508 void fixup_unnamed_interface_types()
1510 hash_table_call_foreach(this->unnamed_interfaces
,
1511 fixup_unnamed_interface_type
, NULL
);
1516 * If the type pointed to by \c type represents an unsized array, replace
1517 * it with a sized array whose size is determined by max_array_access.
1519 static void fixup_type(const glsl_type
**type
, unsigned max_array_access
,
1520 bool from_ssbo_unsized_array
, bool *implicit_sized
)
1522 if (!from_ssbo_unsized_array
&& (*type
)->is_unsized_array()) {
1523 *type
= glsl_type::get_array_instance((*type
)->fields
.array
,
1524 max_array_access
+ 1);
1525 *implicit_sized
= true;
1526 assert(*type
!= NULL
);
1530 static const glsl_type
*
1531 update_interface_members_array(const glsl_type
*type
,
1532 const glsl_type
*new_interface_type
)
1534 const glsl_type
*element_type
= type
->fields
.array
;
1535 if (element_type
->is_array()) {
1536 const glsl_type
*new_array_type
=
1537 update_interface_members_array(element_type
, new_interface_type
);
1538 return glsl_type::get_array_instance(new_array_type
, type
->length
);
1540 return glsl_type::get_array_instance(new_interface_type
,
1546 * Determine whether the given interface type contains unsized arrays (if
1547 * it doesn't, array_sizing_visitor doesn't need to process it).
1549 static bool interface_contains_unsized_arrays(const glsl_type
*type
)
1551 for (unsigned i
= 0; i
< type
->length
; i
++) {
1552 const glsl_type
*elem_type
= type
->fields
.structure
[i
].type
;
1553 if (elem_type
->is_unsized_array())
1560 * Create a new interface type based on the given type, with unsized arrays
1561 * replaced by sized arrays whose size is determined by
1562 * max_ifc_array_access.
1564 static const glsl_type
*
1565 resize_interface_members(const glsl_type
*type
,
1566 const int *max_ifc_array_access
,
1569 unsigned num_fields
= type
->length
;
1570 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1571 memcpy(fields
, type
->fields
.structure
,
1572 num_fields
* sizeof(*fields
));
1573 for (unsigned i
= 0; i
< num_fields
; i
++) {
1574 bool implicit_sized_array
= fields
[i
].implicit_sized_array
;
1575 /* If SSBO last member is unsized array, we don't replace it by a sized
1578 if (is_ssbo
&& i
== (num_fields
- 1))
1579 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1580 true, &implicit_sized_array
);
1582 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1583 false, &implicit_sized_array
);
1584 fields
[i
].implicit_sized_array
= implicit_sized_array
;
1586 glsl_interface_packing packing
=
1587 (glsl_interface_packing
) type
->interface_packing
;
1588 const glsl_type
*new_ifc_type
=
1589 glsl_type::get_interface_instance(fields
, num_fields
,
1590 packing
, type
->name
);
1592 return new_ifc_type
;
1595 static void fixup_unnamed_interface_type(const void *key
, void *data
,
1598 const glsl_type
*ifc_type
= (const glsl_type
*) key
;
1599 ir_variable
**interface_vars
= (ir_variable
**) data
;
1600 unsigned num_fields
= ifc_type
->length
;
1601 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1602 memcpy(fields
, ifc_type
->fields
.structure
,
1603 num_fields
* sizeof(*fields
));
1604 bool interface_type_changed
= false;
1605 for (unsigned i
= 0; i
< num_fields
; i
++) {
1606 if (interface_vars
[i
] != NULL
&&
1607 fields
[i
].type
!= interface_vars
[i
]->type
) {
1608 fields
[i
].type
= interface_vars
[i
]->type
;
1609 interface_type_changed
= true;
1612 if (!interface_type_changed
) {
1616 glsl_interface_packing packing
=
1617 (glsl_interface_packing
) ifc_type
->interface_packing
;
1618 const glsl_type
*new_ifc_type
=
1619 glsl_type::get_interface_instance(fields
, num_fields
, packing
,
1622 for (unsigned i
= 0; i
< num_fields
; i
++) {
1623 if (interface_vars
[i
] != NULL
)
1624 interface_vars
[i
]->change_interface_type(new_ifc_type
);
1629 * Memory context used to allocate the data in \c unnamed_interfaces.
1634 * Hash table from const glsl_type * to an array of ir_variable *'s
1635 * pointing to the ir_variables constituting each unnamed interface block.
1637 hash_table
*unnamed_interfaces
;
1641 * Check for conflicting xfb_stride default qualifiers and store buffer stride
1645 link_xfb_stride_layout_qualifiers(struct gl_context
*ctx
,
1646 struct gl_shader_program
*prog
,
1647 struct gl_linked_shader
*linked_shader
,
1648 struct gl_shader
**shader_list
,
1649 unsigned num_shaders
)
1651 for (unsigned i
= 0; i
< MAX_FEEDBACK_BUFFERS
; i
++) {
1652 linked_shader
->info
.TransformFeedback
.BufferStride
[i
] = 0;
1655 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1656 struct gl_shader
*shader
= shader_list
[i
];
1658 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1659 if (shader
->info
.TransformFeedback
.BufferStride
[j
]) {
1660 if (linked_shader
->info
.TransformFeedback
.BufferStride
[j
] != 0 &&
1661 shader
->info
.TransformFeedback
.BufferStride
[j
] != 0 &&
1662 linked_shader
->info
.TransformFeedback
.BufferStride
[j
] !=
1663 shader
->info
.TransformFeedback
.BufferStride
[j
]) {
1665 "intrastage shaders defined with conflicting "
1666 "xfb_stride for buffer %d (%d and %d)\n", j
,
1668 info
.TransformFeedback
.BufferStride
[j
],
1669 shader
->info
.TransformFeedback
.BufferStride
[j
]);
1673 if (shader
->info
.TransformFeedback
.BufferStride
[j
])
1674 linked_shader
->info
.TransformFeedback
.BufferStride
[j
] =
1675 shader
->info
.TransformFeedback
.BufferStride
[j
];
1680 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1681 if (linked_shader
->info
.TransformFeedback
.BufferStride
[j
]) {
1682 prog
->TransformFeedback
.BufferStride
[j
] =
1683 linked_shader
->info
.TransformFeedback
.BufferStride
[j
];
1685 /* We will validate doubles at a later stage */
1686 if (prog
->TransformFeedback
.BufferStride
[j
] % 4) {
1687 linker_error(prog
, "invalid qualifier xfb_stride=%d must be a "
1688 "multiple of 4 or if its applied to a type that is "
1689 "or contains a double a multiple of 8.",
1690 prog
->TransformFeedback
.BufferStride
[j
]);
1694 if (prog
->TransformFeedback
.BufferStride
[j
] / 4 >
1695 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
1697 "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1698 "limit has been exceeded.");
1706 * Performs the cross-validation of tessellation control shader vertices and
1707 * layout qualifiers for the attached tessellation control shaders,
1708 * and propagates them to the linked TCS and linked shader program.
1711 link_tcs_out_layout_qualifiers(struct gl_shader_program
*prog
,
1712 struct gl_linked_shader
*linked_shader
,
1713 struct gl_shader
**shader_list
,
1714 unsigned num_shaders
)
1716 linked_shader
->info
.TessCtrl
.VerticesOut
= 0;
1718 if (linked_shader
->Stage
!= MESA_SHADER_TESS_CTRL
)
1721 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1723 * "All tessellation control shader layout declarations in a program
1724 * must specify the same output patch vertex count. There must be at
1725 * least one layout qualifier specifying an output patch vertex count
1726 * in any program containing tessellation control shaders; however,
1727 * such a declaration is not required in all tessellation control
1731 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1732 struct gl_shader
*shader
= shader_list
[i
];
1734 if (shader
->info
.TessCtrl
.VerticesOut
!= 0) {
1735 if (linked_shader
->info
.TessCtrl
.VerticesOut
!= 0 &&
1736 linked_shader
->info
.TessCtrl
.VerticesOut
!=
1737 shader
->info
.TessCtrl
.VerticesOut
) {
1738 linker_error(prog
, "tessellation control shader defined with "
1739 "conflicting output vertex count (%d and %d)\n",
1740 linked_shader
->info
.TessCtrl
.VerticesOut
,
1741 shader
->info
.TessCtrl
.VerticesOut
);
1744 linked_shader
->info
.TessCtrl
.VerticesOut
=
1745 shader
->info
.TessCtrl
.VerticesOut
;
1749 /* Just do the intrastage -> interstage propagation right now,
1750 * since we already know we're in the right type of shader program
1753 if (linked_shader
->info
.TessCtrl
.VerticesOut
== 0) {
1754 linker_error(prog
, "tessellation control shader didn't declare "
1755 "vertices out layout qualifier\n");
1762 * Performs the cross-validation of tessellation evaluation shader
1763 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1764 * for the attached tessellation evaluation shaders, and propagates them
1765 * to the linked TES and linked shader program.
1768 link_tes_in_layout_qualifiers(struct gl_shader_program
*prog
,
1769 struct gl_linked_shader
*linked_shader
,
1770 struct gl_shader
**shader_list
,
1771 unsigned num_shaders
)
1773 linked_shader
->info
.TessEval
.PrimitiveMode
= PRIM_UNKNOWN
;
1774 linked_shader
->info
.TessEval
.Spacing
= 0;
1775 linked_shader
->info
.TessEval
.VertexOrder
= 0;
1776 linked_shader
->info
.TessEval
.PointMode
= -1;
1778 if (linked_shader
->Stage
!= MESA_SHADER_TESS_EVAL
)
1781 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1783 * "At least one tessellation evaluation shader (compilation unit) in
1784 * a program must declare a primitive mode in its input layout.
1785 * Declaration vertex spacing, ordering, and point mode identifiers is
1786 * optional. It is not required that all tessellation evaluation
1787 * shaders in a program declare a primitive mode. If spacing or
1788 * vertex ordering declarations are omitted, the tessellation
1789 * primitive generator will use equal spacing or counter-clockwise
1790 * vertex ordering, respectively. If a point mode declaration is
1791 * omitted, the tessellation primitive generator will produce lines or
1792 * triangles according to the primitive mode."
1795 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1796 struct gl_shader
*shader
= shader_list
[i
];
1798 if (shader
->info
.TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
) {
1799 if (linked_shader
->info
.TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
&&
1800 linked_shader
->info
.TessEval
.PrimitiveMode
!=
1801 shader
->info
.TessEval
.PrimitiveMode
) {
1802 linker_error(prog
, "tessellation evaluation shader defined with "
1803 "conflicting input primitive modes.\n");
1806 linked_shader
->info
.TessEval
.PrimitiveMode
= shader
->info
.TessEval
.PrimitiveMode
;
1809 if (shader
->info
.TessEval
.Spacing
!= 0) {
1810 if (linked_shader
->info
.TessEval
.Spacing
!= 0 &&
1811 linked_shader
->info
.TessEval
.Spacing
!=
1812 shader
->info
.TessEval
.Spacing
) {
1813 linker_error(prog
, "tessellation evaluation shader defined with "
1814 "conflicting vertex spacing.\n");
1817 linked_shader
->info
.TessEval
.Spacing
= shader
->info
.TessEval
.Spacing
;
1820 if (shader
->info
.TessEval
.VertexOrder
!= 0) {
1821 if (linked_shader
->info
.TessEval
.VertexOrder
!= 0 &&
1822 linked_shader
->info
.TessEval
.VertexOrder
!=
1823 shader
->info
.TessEval
.VertexOrder
) {
1824 linker_error(prog
, "tessellation evaluation shader defined with "
1825 "conflicting ordering.\n");
1828 linked_shader
->info
.TessEval
.VertexOrder
=
1829 shader
->info
.TessEval
.VertexOrder
;
1832 if (shader
->info
.TessEval
.PointMode
!= -1) {
1833 if (linked_shader
->info
.TessEval
.PointMode
!= -1 &&
1834 linked_shader
->info
.TessEval
.PointMode
!=
1835 shader
->info
.TessEval
.PointMode
) {
1836 linker_error(prog
, "tessellation evaluation shader defined with "
1837 "conflicting point modes.\n");
1840 linked_shader
->info
.TessEval
.PointMode
=
1841 shader
->info
.TessEval
.PointMode
;
1846 /* Just do the intrastage -> interstage propagation right now,
1847 * since we already know we're in the right type of shader program
1850 if (linked_shader
->info
.TessEval
.PrimitiveMode
== PRIM_UNKNOWN
) {
1852 "tessellation evaluation shader didn't declare input "
1853 "primitive modes.\n");
1857 if (linked_shader
->info
.TessEval
.Spacing
== 0)
1858 linked_shader
->info
.TessEval
.Spacing
= GL_EQUAL
;
1860 if (linked_shader
->info
.TessEval
.VertexOrder
== 0)
1861 linked_shader
->info
.TessEval
.VertexOrder
= GL_CCW
;
1863 if (linked_shader
->info
.TessEval
.PointMode
== -1)
1864 linked_shader
->info
.TessEval
.PointMode
= GL_FALSE
;
1869 * Performs the cross-validation of layout qualifiers specified in
1870 * redeclaration of gl_FragCoord for the attached fragment shaders,
1871 * and propagates them to the linked FS and linked shader program.
1874 link_fs_input_layout_qualifiers(struct gl_shader_program
*prog
,
1875 struct gl_linked_shader
*linked_shader
,
1876 struct gl_shader
**shader_list
,
1877 unsigned num_shaders
)
1879 linked_shader
->info
.redeclares_gl_fragcoord
= false;
1880 linked_shader
->info
.uses_gl_fragcoord
= false;
1881 linked_shader
->info
.origin_upper_left
= false;
1882 linked_shader
->info
.pixel_center_integer
= false;
1884 if (linked_shader
->Stage
!= MESA_SHADER_FRAGMENT
||
1885 (prog
->Version
< 150 && !prog
->ARB_fragment_coord_conventions_enable
))
1888 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1889 struct gl_shader
*shader
= shader_list
[i
];
1890 /* From the GLSL 1.50 spec, page 39:
1892 * "If gl_FragCoord is redeclared in any fragment shader in a program,
1893 * it must be redeclared in all the fragment shaders in that program
1894 * that have a static use gl_FragCoord."
1896 if ((linked_shader
->info
.redeclares_gl_fragcoord
1897 && !shader
->info
.redeclares_gl_fragcoord
1898 && shader
->info
.uses_gl_fragcoord
)
1899 || (shader
->info
.redeclares_gl_fragcoord
1900 && !linked_shader
->info
.redeclares_gl_fragcoord
1901 && linked_shader
->info
.uses_gl_fragcoord
)) {
1902 linker_error(prog
, "fragment shader defined with conflicting "
1903 "layout qualifiers for gl_FragCoord\n");
1906 /* From the GLSL 1.50 spec, page 39:
1908 * "All redeclarations of gl_FragCoord in all fragment shaders in a
1909 * single program must have the same set of qualifiers."
1911 if (linked_shader
->info
.redeclares_gl_fragcoord
&&
1912 shader
->info
.redeclares_gl_fragcoord
&&
1913 (shader
->info
.origin_upper_left
!=
1914 linked_shader
->info
.origin_upper_left
||
1915 shader
->info
.pixel_center_integer
!=
1916 linked_shader
->info
.pixel_center_integer
)) {
1917 linker_error(prog
, "fragment shader defined with conflicting "
1918 "layout qualifiers for gl_FragCoord\n");
1921 /* Update the linked shader state. Note that uses_gl_fragcoord should
1922 * accumulate the results. The other values should replace. If there
1923 * are multiple redeclarations, all the fields except uses_gl_fragcoord
1924 * are already known to be the same.
1926 if (shader
->info
.redeclares_gl_fragcoord
||
1927 shader
->info
.uses_gl_fragcoord
) {
1928 linked_shader
->info
.redeclares_gl_fragcoord
=
1929 shader
->info
.redeclares_gl_fragcoord
;
1930 linked_shader
->info
.uses_gl_fragcoord
=
1931 linked_shader
->info
.uses_gl_fragcoord
||
1932 shader
->info
.uses_gl_fragcoord
;
1933 linked_shader
->info
.origin_upper_left
=
1934 shader
->info
.origin_upper_left
;
1935 linked_shader
->info
.pixel_center_integer
=
1936 shader
->info
.pixel_center_integer
;
1939 linked_shader
->info
.EarlyFragmentTests
|=
1940 shader
->info
.EarlyFragmentTests
;
1945 * Performs the cross-validation of geometry shader max_vertices and
1946 * primitive type layout qualifiers for the attached geometry shaders,
1947 * and propagates them to the linked GS and linked shader program.
1950 link_gs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
1951 struct gl_linked_shader
*linked_shader
,
1952 struct gl_shader
**shader_list
,
1953 unsigned num_shaders
)
1955 linked_shader
->info
.Geom
.VerticesOut
= -1;
1956 linked_shader
->info
.Geom
.Invocations
= 0;
1957 linked_shader
->info
.Geom
.InputType
= PRIM_UNKNOWN
;
1958 linked_shader
->info
.Geom
.OutputType
= PRIM_UNKNOWN
;
1960 /* No in/out qualifiers defined for anything but GLSL 1.50+
1961 * geometry shaders so far.
1963 if (linked_shader
->Stage
!= MESA_SHADER_GEOMETRY
|| prog
->Version
< 150)
1966 /* From the GLSL 1.50 spec, page 46:
1968 * "All geometry shader output layout declarations in a program
1969 * must declare the same layout and same value for
1970 * max_vertices. There must be at least one geometry output
1971 * layout declaration somewhere in a program, but not all
1972 * geometry shaders (compilation units) are required to
1976 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1977 struct gl_shader
*shader
= shader_list
[i
];
1979 if (shader
->info
.Geom
.InputType
!= PRIM_UNKNOWN
) {
1980 if (linked_shader
->info
.Geom
.InputType
!= PRIM_UNKNOWN
&&
1981 linked_shader
->info
.Geom
.InputType
!=
1982 shader
->info
.Geom
.InputType
) {
1983 linker_error(prog
, "geometry shader defined with conflicting "
1987 linked_shader
->info
.Geom
.InputType
= shader
->info
.Geom
.InputType
;
1990 if (shader
->info
.Geom
.OutputType
!= PRIM_UNKNOWN
) {
1991 if (linked_shader
->info
.Geom
.OutputType
!= PRIM_UNKNOWN
&&
1992 linked_shader
->info
.Geom
.OutputType
!=
1993 shader
->info
.Geom
.OutputType
) {
1994 linker_error(prog
, "geometry shader defined with conflicting "
1998 linked_shader
->info
.Geom
.OutputType
= shader
->info
.Geom
.OutputType
;
2001 if (shader
->info
.Geom
.VerticesOut
!= -1) {
2002 if (linked_shader
->info
.Geom
.VerticesOut
!= -1 &&
2003 linked_shader
->info
.Geom
.VerticesOut
!=
2004 shader
->info
.Geom
.VerticesOut
) {
2005 linker_error(prog
, "geometry shader defined with conflicting "
2006 "output vertex count (%d and %d)\n",
2007 linked_shader
->info
.Geom
.VerticesOut
,
2008 shader
->info
.Geom
.VerticesOut
);
2011 linked_shader
->info
.Geom
.VerticesOut
= shader
->info
.Geom
.VerticesOut
;
2014 if (shader
->info
.Geom
.Invocations
!= 0) {
2015 if (linked_shader
->info
.Geom
.Invocations
!= 0 &&
2016 linked_shader
->info
.Geom
.Invocations
!=
2017 shader
->info
.Geom
.Invocations
) {
2018 linker_error(prog
, "geometry shader defined with conflicting "
2019 "invocation count (%d and %d)\n",
2020 linked_shader
->info
.Geom
.Invocations
,
2021 shader
->info
.Geom
.Invocations
);
2024 linked_shader
->info
.Geom
.Invocations
= shader
->info
.Geom
.Invocations
;
2028 /* Just do the intrastage -> interstage propagation right now,
2029 * since we already know we're in the right type of shader program
2032 if (linked_shader
->info
.Geom
.InputType
== PRIM_UNKNOWN
) {
2034 "geometry shader didn't declare primitive input type\n");
2038 if (linked_shader
->info
.Geom
.OutputType
== PRIM_UNKNOWN
) {
2040 "geometry shader didn't declare primitive output type\n");
2044 if (linked_shader
->info
.Geom
.VerticesOut
== -1) {
2046 "geometry shader didn't declare max_vertices\n");
2050 if (linked_shader
->info
.Geom
.Invocations
== 0)
2051 linked_shader
->info
.Geom
.Invocations
= 1;
2056 * Perform cross-validation of compute shader local_size_{x,y,z} layout
2057 * qualifiers for the attached compute shaders, and propagate them to the
2058 * linked CS and linked shader program.
2061 link_cs_input_layout_qualifiers(struct gl_shader_program
*prog
,
2062 struct gl_linked_shader
*linked_shader
,
2063 struct gl_shader
**shader_list
,
2064 unsigned num_shaders
)
2066 for (int i
= 0; i
< 3; i
++)
2067 linked_shader
->info
.Comp
.LocalSize
[i
] = 0;
2069 /* This function is called for all shader stages, but it only has an effect
2070 * for compute shaders.
2072 if (linked_shader
->Stage
!= MESA_SHADER_COMPUTE
)
2075 /* From the ARB_compute_shader spec, in the section describing local size
2078 * If multiple compute shaders attached to a single program object
2079 * declare local work-group size, the declarations must be identical;
2080 * otherwise a link-time error results. Furthermore, if a program
2081 * object contains any compute shaders, at least one must contain an
2082 * input layout qualifier specifying the local work sizes of the
2083 * program, or a link-time error will occur.
2085 for (unsigned sh
= 0; sh
< num_shaders
; sh
++) {
2086 struct gl_shader
*shader
= shader_list
[sh
];
2088 if (shader
->info
.Comp
.LocalSize
[0] != 0) {
2089 if (linked_shader
->info
.Comp
.LocalSize
[0] != 0) {
2090 for (int i
= 0; i
< 3; i
++) {
2091 if (linked_shader
->info
.Comp
.LocalSize
[i
] !=
2092 shader
->info
.Comp
.LocalSize
[i
]) {
2093 linker_error(prog
, "compute shader defined with conflicting "
2099 for (int i
= 0; i
< 3; i
++) {
2100 linked_shader
->info
.Comp
.LocalSize
[i
] =
2101 shader
->info
.Comp
.LocalSize
[i
];
2106 /* Just do the intrastage -> interstage propagation right now,
2107 * since we already know we're in the right type of shader program
2110 if (linked_shader
->info
.Comp
.LocalSize
[0] == 0) {
2111 linker_error(prog
, "compute shader didn't declare local size\n");
2114 for (int i
= 0; i
< 3; i
++)
2115 prog
->Comp
.LocalSize
[i
] = linked_shader
->info
.Comp
.LocalSize
[i
];
2120 * Combine a group of shaders for a single stage to generate a linked shader
2123 * If this function is supplied a single shader, it is cloned, and the new
2124 * shader is returned.
2126 static struct gl_linked_shader
*
2127 link_intrastage_shaders(void *mem_ctx
,
2128 struct gl_context
*ctx
,
2129 struct gl_shader_program
*prog
,
2130 struct gl_shader
**shader_list
,
2131 unsigned num_shaders
)
2133 struct gl_uniform_block
*ubo_blocks
= NULL
;
2134 struct gl_uniform_block
*ssbo_blocks
= NULL
;
2135 unsigned num_ubo_blocks
= 0;
2136 unsigned num_ssbo_blocks
= 0;
2138 /* Check that global variables defined in multiple shaders are consistent.
2140 glsl_symbol_table variables
;
2141 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2142 if (shader_list
[i
] == NULL
)
2144 cross_validate_globals(prog
, shader_list
[i
]->ir
, &variables
, false);
2147 if (!prog
->LinkStatus
)
2150 /* Check that interface blocks defined in multiple shaders are consistent.
2152 validate_intrastage_interface_blocks(prog
, (const gl_shader
**)shader_list
,
2154 if (!prog
->LinkStatus
)
2157 /* Check that there is only a single definition of each function signature
2158 * across all shaders.
2160 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
2161 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
2162 ir_function
*const f
= node
->as_function();
2167 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
2168 ir_function
*const other
=
2169 shader_list
[j
]->symbols
->get_function(f
->name
);
2171 /* If the other shader has no function (and therefore no function
2172 * signatures) with the same name, skip to the next shader.
2177 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
2178 if (!sig
->is_defined
|| sig
->is_builtin())
2181 ir_function_signature
*other_sig
=
2182 other
->exact_matching_signature(NULL
, &sig
->parameters
);
2184 if ((other_sig
!= NULL
) && other_sig
->is_defined
2185 && !other_sig
->is_builtin()) {
2186 linker_error(prog
, "function `%s' is multiply defined\n",
2195 /* Find the shader that defines main, and make a clone of it.
2197 * Starting with the clone, search for undefined references. If one is
2198 * found, find the shader that defines it. Clone the reference and add
2199 * it to the shader. Repeat until there are no undefined references or
2200 * until a reference cannot be resolved.
2202 gl_shader
*main
= NULL
;
2203 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2204 if (_mesa_get_main_function_signature(shader_list
[i
]->symbols
)) {
2205 main
= shader_list
[i
];
2211 linker_error(prog
, "%s shader lacks `main'\n",
2212 _mesa_shader_stage_to_string(shader_list
[0]->Stage
));
2216 gl_linked_shader
*linked
= ctx
->Driver
.NewShader(shader_list
[0]->Stage
);
2217 linked
->ir
= new(linked
) exec_list
;
2218 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
2220 link_fs_input_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2221 link_tcs_out_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2222 link_tes_in_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2223 link_gs_inout_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2224 link_cs_input_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2225 link_xfb_stride_layout_qualifiers(ctx
, prog
, linked
, shader_list
,
2228 populate_symbol_table(linked
);
2230 /* The pointer to the main function in the final linked shader (i.e., the
2231 * copy of the original shader that contained the main function).
2233 ir_function_signature
*const main_sig
=
2234 _mesa_get_main_function_signature(linked
->symbols
);
2236 /* Move any instructions other than variable declarations or function
2237 * declarations into main.
2239 exec_node
*insertion_point
=
2240 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
2243 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2244 if (shader_list
[i
] == main
)
2247 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
2248 insertion_point
, true, linked
);
2251 /* Check if any shader needs built-in functions. */
2252 bool need_builtins
= false;
2253 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2254 if (shader_list
[i
]->info
.uses_builtin_functions
) {
2255 need_builtins
= true;
2261 if (need_builtins
) {
2262 /* Make a temporary array one larger than shader_list, which will hold
2263 * the built-in function shader as well.
2265 gl_shader
**linking_shaders
= (gl_shader
**)
2266 calloc(num_shaders
+ 1, sizeof(gl_shader
*));
2268 ok
= linking_shaders
!= NULL
;
2271 memcpy(linking_shaders
, shader_list
, num_shaders
* sizeof(gl_shader
*));
2272 _mesa_glsl_initialize_builtin_functions();
2273 linking_shaders
[num_shaders
] = _mesa_glsl_get_builtin_function_shader();
2275 ok
= link_function_calls(prog
, linked
, linking_shaders
, num_shaders
+ 1);
2277 free(linking_shaders
);
2279 _mesa_error_no_memory(__func__
);
2282 ok
= link_function_calls(prog
, linked
, shader_list
, num_shaders
);
2287 _mesa_delete_linked_shader(ctx
, linked
);
2291 /* Make a pass over all variable declarations to ensure that arrays with
2292 * unspecified sizes have a size specified. The size is inferred from the
2293 * max_array_access field.
2295 array_sizing_visitor v
;
2297 v
.fixup_unnamed_interface_types();
2299 /* Link up uniform blocks defined within this stage. */
2300 link_uniform_blocks(mem_ctx
, ctx
, prog
, linked
, &ubo_blocks
,
2301 &num_ubo_blocks
, &ssbo_blocks
, &num_ssbo_blocks
);
2303 if (!prog
->LinkStatus
) {
2304 _mesa_delete_linked_shader(ctx
, linked
);
2308 /* Copy ubo blocks to linked shader list */
2309 linked
->UniformBlocks
=
2310 ralloc_array(linked
, gl_uniform_block
*, num_ubo_blocks
);
2311 ralloc_steal(linked
, ubo_blocks
);
2312 for (unsigned i
= 0; i
< num_ubo_blocks
; i
++) {
2313 linked
->UniformBlocks
[i
] = &ubo_blocks
[i
];
2315 linked
->NumUniformBlocks
= num_ubo_blocks
;
2317 /* Copy ssbo blocks to linked shader list */
2318 linked
->ShaderStorageBlocks
=
2319 ralloc_array(linked
, gl_uniform_block
*, num_ssbo_blocks
);
2320 ralloc_steal(linked
, ssbo_blocks
);
2321 for (unsigned i
= 0; i
< num_ssbo_blocks
; i
++) {
2322 linked
->ShaderStorageBlocks
[i
] = &ssbo_blocks
[i
];
2324 linked
->NumShaderStorageBlocks
= num_ssbo_blocks
;
2326 /* At this point linked should contain all of the linked IR, so
2327 * validate it to make sure nothing went wrong.
2329 validate_ir_tree(linked
->ir
);
2331 /* Set the size of geometry shader input arrays */
2332 if (linked
->Stage
== MESA_SHADER_GEOMETRY
) {
2333 unsigned num_vertices
= vertices_per_prim(linked
->info
.Geom
.InputType
);
2334 array_resize_visitor
input_resize_visitor(num_vertices
, prog
,
2335 MESA_SHADER_GEOMETRY
);
2336 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2337 ir
->accept(&input_resize_visitor
);
2341 if (ctx
->Const
.VertexID_is_zero_based
)
2342 lower_vertex_id(linked
);
2344 /* Validate correct usage of barrier() in the tess control shader */
2345 if (linked
->Stage
== MESA_SHADER_TESS_CTRL
) {
2346 barrier_use_visitor
visitor(prog
);
2347 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2348 ir
->accept(&visitor
);
2356 * Update the sizes of linked shader uniform arrays to the maximum
2359 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
2361 * If one or more elements of an array are active,
2362 * GetActiveUniform will return the name of the array in name,
2363 * subject to the restrictions listed above. The type of the array
2364 * is returned in type. The size parameter contains the highest
2365 * array element index used, plus one. The compiler or linker
2366 * determines the highest index used. There will be only one
2367 * active uniform reported by the GL per uniform array.
2371 update_array_sizes(struct gl_shader_program
*prog
)
2373 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2374 if (prog
->_LinkedShaders
[i
] == NULL
)
2377 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
2378 ir_variable
*const var
= node
->as_variable();
2380 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
) ||
2381 !var
->type
->is_array())
2384 /* GL_ARB_uniform_buffer_object says that std140 uniforms
2385 * will not be eliminated. Since we always do std140, just
2386 * don't resize arrays in UBOs.
2388 * Atomic counters are supposed to get deterministic
2389 * locations assigned based on the declaration ordering and
2390 * sizes, array compaction would mess that up.
2392 * Subroutine uniforms are not removed.
2394 if (var
->is_in_buffer_block() || var
->type
->contains_atomic() ||
2395 var
->type
->contains_subroutine() || var
->constant_initializer
)
2398 int size
= var
->data
.max_array_access
;
2399 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
2400 if (prog
->_LinkedShaders
[j
] == NULL
)
2403 foreach_in_list(ir_instruction
, node2
, prog
->_LinkedShaders
[j
]->ir
) {
2404 ir_variable
*other_var
= node2
->as_variable();
2408 if (strcmp(var
->name
, other_var
->name
) == 0 &&
2409 other_var
->data
.max_array_access
> size
) {
2410 size
= other_var
->data
.max_array_access
;
2415 if (size
+ 1 != (int)var
->type
->length
) {
2416 /* If this is a built-in uniform (i.e., it's backed by some
2417 * fixed-function state), adjust the number of state slots to
2418 * match the new array size. The number of slots per array entry
2419 * is not known. It seems safe to assume that the total number of
2420 * slots is an integer multiple of the number of array elements.
2421 * Determine the number of slots per array element by dividing by
2422 * the old (total) size.
2424 const unsigned num_slots
= var
->get_num_state_slots();
2425 if (num_slots
> 0) {
2426 var
->set_num_state_slots((size
+ 1)
2427 * (num_slots
/ var
->type
->length
));
2430 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2432 /* FINISHME: We should update the types of array
2433 * dereferences of this variable now.
2441 * Resize tessellation evaluation per-vertex inputs to the size of
2442 * tessellation control per-vertex outputs.
2445 resize_tes_inputs(struct gl_context
*ctx
,
2446 struct gl_shader_program
*prog
)
2448 if (prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] == NULL
)
2451 gl_linked_shader
*const tcs
= prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
];
2452 gl_linked_shader
*const tes
= prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
2454 /* If no control shader is present, then the TES inputs are statically
2455 * sized to MaxPatchVertices; the actual size of the arrays won't be
2456 * known until draw time.
2458 const int num_vertices
= tcs
2459 ? tcs
->info
.TessCtrl
.VerticesOut
2460 : ctx
->Const
.MaxPatchVertices
;
2462 array_resize_visitor
input_resize_visitor(num_vertices
, prog
,
2463 MESA_SHADER_TESS_EVAL
);
2464 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2465 ir
->accept(&input_resize_visitor
);
2468 if (tcs
|| ctx
->Const
.LowerTESPatchVerticesIn
) {
2469 /* Convert the gl_PatchVerticesIn system value into a constant, since
2470 * the value is known at this point.
2472 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2473 ir_variable
*var
= ir
->as_variable();
2474 if (var
&& var
->data
.mode
== ir_var_system_value
&&
2475 var
->data
.location
== SYSTEM_VALUE_VERTICES_IN
) {
2476 void *mem_ctx
= ralloc_parent(var
);
2477 var
->data
.location
= 0;
2478 var
->data
.explicit_location
= false;
2480 var
->data
.mode
= ir_var_auto
;
2481 var
->constant_value
= new(mem_ctx
) ir_constant(num_vertices
);
2483 var
->data
.mode
= ir_var_uniform
;
2484 var
->data
.how_declared
= ir_var_hidden
;
2485 var
->allocate_state_slots(1);
2486 ir_state_slot
*slot0
= &var
->get_state_slots()[0];
2487 slot0
->swizzle
= SWIZZLE_XXXX
;
2488 slot0
->tokens
[0] = STATE_INTERNAL
;
2489 slot0
->tokens
[1] = STATE_TES_PATCH_VERTICES_IN
;
2490 for (int i
= 2; i
< STATE_LENGTH
; i
++)
2491 slot0
->tokens
[i
] = 0;
2499 * Find a contiguous set of available bits in a bitmask.
2501 * \param used_mask Bits representing used (1) and unused (0) locations
2502 * \param needed_count Number of contiguous bits needed.
2505 * Base location of the available bits on success or -1 on failure.
2508 find_available_slots(unsigned used_mask
, unsigned needed_count
)
2510 unsigned needed_mask
= (1 << needed_count
) - 1;
2511 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
2513 /* The comparison to 32 is redundant, but without it GCC emits "warning:
2514 * cannot optimize possibly infinite loops" for the loop below.
2516 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
2519 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
2520 if ((needed_mask
& ~used_mask
) == needed_mask
)
2531 * Assign locations for either VS inputs or FS outputs
2533 * \param prog Shader program whose variables need locations assigned
2534 * \param constants Driver specific constant values for the program.
2535 * \param target_index Selector for the program target to receive location
2536 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
2537 * \c MESA_SHADER_FRAGMENT.
2540 * If locations are successfully assigned, true is returned. Otherwise an
2541 * error is emitted to the shader link log and false is returned.
2544 assign_attribute_or_color_locations(gl_shader_program
*prog
,
2545 struct gl_constants
*constants
,
2546 unsigned target_index
)
2548 /* Maximum number of generic locations. This corresponds to either the
2549 * maximum number of draw buffers or the maximum number of generic
2552 unsigned max_index
= (target_index
== MESA_SHADER_VERTEX
) ?
2553 constants
->Program
[target_index
].MaxAttribs
:
2554 MAX2(constants
->MaxDrawBuffers
, constants
->MaxDualSourceDrawBuffers
);
2556 /* Mark invalid locations as being used.
2558 unsigned used_locations
= (max_index
>= 32)
2559 ? ~0 : ~((1 << max_index
) - 1);
2560 unsigned double_storage_locations
= 0;
2562 assert((target_index
== MESA_SHADER_VERTEX
)
2563 || (target_index
== MESA_SHADER_FRAGMENT
));
2565 gl_linked_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
2569 /* Operate in a total of four passes.
2571 * 1. Invalidate the location assignments for all vertex shader inputs.
2573 * 2. Assign locations for inputs that have user-defined (via
2574 * glBindVertexAttribLocation) locations and outputs that have
2575 * user-defined locations (via glBindFragDataLocation).
2577 * 3. Sort the attributes without assigned locations by number of slots
2578 * required in decreasing order. Fragmentation caused by attribute
2579 * locations assigned by the application may prevent large attributes
2580 * from having enough contiguous space.
2582 * 4. Assign locations to any inputs without assigned locations.
2585 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
2586 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
2588 const enum ir_variable_mode direction
=
2589 (target_index
== MESA_SHADER_VERTEX
)
2590 ? ir_var_shader_in
: ir_var_shader_out
;
2593 /* Temporary storage for the set of attributes that need locations assigned.
2599 /* Used below in the call to qsort. */
2600 static int compare(const void *a
, const void *b
)
2602 const temp_attr
*const l
= (const temp_attr
*) a
;
2603 const temp_attr
*const r
= (const temp_attr
*) b
;
2605 /* Reversed because we want a descending order sort below. */
2606 return r
->slots
- l
->slots
;
2609 assert(max_index
<= 32);
2611 /* Temporary array for the set of attributes that have locations assigned.
2613 ir_variable
*assigned
[16];
2615 unsigned num_attr
= 0;
2616 unsigned assigned_attr
= 0;
2618 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2619 ir_variable
*const var
= node
->as_variable();
2621 if ((var
== NULL
) || (var
->data
.mode
!= (unsigned) direction
))
2624 if (var
->data
.explicit_location
) {
2625 var
->data
.is_unmatched_generic_inout
= 0;
2626 if ((var
->data
.location
>= (int)(max_index
+ generic_base
))
2627 || (var
->data
.location
< 0)) {
2629 "invalid explicit location %d specified for `%s'\n",
2630 (var
->data
.location
< 0)
2631 ? var
->data
.location
2632 : var
->data
.location
- generic_base
,
2636 } else if (target_index
== MESA_SHADER_VERTEX
) {
2639 if (prog
->AttributeBindings
->get(binding
, var
->name
)) {
2640 assert(binding
>= VERT_ATTRIB_GENERIC0
);
2641 var
->data
.location
= binding
;
2642 var
->data
.is_unmatched_generic_inout
= 0;
2644 } else if (target_index
== MESA_SHADER_FRAGMENT
) {
2648 if (prog
->FragDataBindings
->get(binding
, var
->name
)) {
2649 assert(binding
>= FRAG_RESULT_DATA0
);
2650 var
->data
.location
= binding
;
2651 var
->data
.is_unmatched_generic_inout
= 0;
2653 if (prog
->FragDataIndexBindings
->get(index
, var
->name
)) {
2654 var
->data
.index
= index
;
2659 /* From GL4.5 core spec, section 15.2 (Shader Execution):
2661 * "Output binding assignments will cause LinkProgram to fail:
2663 * If the program has an active output assigned to a location greater
2664 * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
2665 * an active output assigned an index greater than or equal to one;"
2667 if (target_index
== MESA_SHADER_FRAGMENT
&& var
->data
.index
>= 1 &&
2668 var
->data
.location
- generic_base
>=
2669 (int) constants
->MaxDualSourceDrawBuffers
) {
2671 "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
2672 "with index %u for %s\n",
2673 var
->data
.location
- generic_base
, var
->data
.index
,
2678 const unsigned slots
= var
->type
->count_attribute_slots(target_index
== MESA_SHADER_VERTEX
);
2680 /* If the variable is not a built-in and has a location statically
2681 * assigned in the shader (presumably via a layout qualifier), make sure
2682 * that it doesn't collide with other assigned locations. Otherwise,
2683 * add it to the list of variables that need linker-assigned locations.
2685 if (var
->data
.location
!= -1) {
2686 if (var
->data
.location
>= generic_base
&& var
->data
.index
< 1) {
2687 /* From page 61 of the OpenGL 4.0 spec:
2689 * "LinkProgram will fail if the attribute bindings assigned
2690 * by BindAttribLocation do not leave not enough space to
2691 * assign a location for an active matrix attribute or an
2692 * active attribute array, both of which require multiple
2693 * contiguous generic attributes."
2695 * I think above text prohibits the aliasing of explicit and
2696 * automatic assignments. But, aliasing is allowed in manual
2697 * assignments of attribute locations. See below comments for
2700 * From OpenGL 4.0 spec, page 61:
2702 * "It is possible for an application to bind more than one
2703 * attribute name to the same location. This is referred to as
2704 * aliasing. This will only work if only one of the aliased
2705 * attributes is active in the executable program, or if no
2706 * path through the shader consumes more than one attribute of
2707 * a set of attributes aliased to the same location. A link
2708 * error can occur if the linker determines that every path
2709 * through the shader consumes multiple aliased attributes,
2710 * but implementations are not required to generate an error
2713 * From GLSL 4.30 spec, page 54:
2715 * "A program will fail to link if any two non-vertex shader
2716 * input variables are assigned to the same location. For
2717 * vertex shaders, multiple input variables may be assigned
2718 * to the same location using either layout qualifiers or via
2719 * the OpenGL API. However, such aliasing is intended only to
2720 * support vertex shaders where each execution path accesses
2721 * at most one input per each location. Implementations are
2722 * permitted, but not required, to generate link-time errors
2723 * if they detect that every path through the vertex shader
2724 * executable accesses multiple inputs assigned to any single
2725 * location. For all shader types, a program will fail to link
2726 * if explicit location assignments leave the linker unable
2727 * to find space for other variables without explicit
2730 * From OpenGL ES 3.0 spec, page 56:
2732 * "Binding more than one attribute name to the same location
2733 * is referred to as aliasing, and is not permitted in OpenGL
2734 * ES Shading Language 3.00 vertex shaders. LinkProgram will
2735 * fail when this condition exists. However, aliasing is
2736 * possible in OpenGL ES Shading Language 1.00 vertex shaders.
2737 * This will only work if only one of the aliased attributes
2738 * is active in the executable program, or if no path through
2739 * the shader consumes more than one attribute of a set of
2740 * attributes aliased to the same location. A link error can
2741 * occur if the linker determines that every path through the
2742 * shader consumes multiple aliased attributes, but implemen-
2743 * tations are not required to generate an error in this case."
2745 * After looking at above references from OpenGL, OpenGL ES and
2746 * GLSL specifications, we allow aliasing of vertex input variables
2747 * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
2749 * NOTE: This is not required by the spec but its worth mentioning
2750 * here that we're not doing anything to make sure that no path
2751 * through the vertex shader executable accesses multiple inputs
2752 * assigned to any single location.
2755 /* Mask representing the contiguous slots that will be used by
2758 const unsigned attr
= var
->data
.location
- generic_base
;
2759 const unsigned use_mask
= (1 << slots
) - 1;
2760 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
2761 ? "vertex shader input" : "fragment shader output";
2763 /* Generate a link error if the requested locations for this
2764 * attribute exceed the maximum allowed attribute location.
2766 if (attr
+ slots
> max_index
) {
2768 "insufficient contiguous locations "
2769 "available for %s `%s' %d %d %d\n", string
,
2770 var
->name
, used_locations
, use_mask
, attr
);
2774 /* Generate a link error if the set of bits requested for this
2775 * attribute overlaps any previously allocated bits.
2777 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
2778 if (target_index
== MESA_SHADER_FRAGMENT
&& !prog
->IsES
) {
2779 /* From section 4.4.2 (Output Layout Qualifiers) of the GLSL
2782 * "Additionally, for fragment shader outputs, if two
2783 * variables are placed within the same location, they
2784 * must have the same underlying type (floating-point or
2785 * integer). No component aliasing of output variables or
2786 * members is allowed.
2788 for (unsigned i
= 0; i
< assigned_attr
; i
++) {
2789 unsigned assigned_slots
=
2790 assigned
[i
]->type
->count_attribute_slots(false);
2791 unsigned assig_attr
=
2792 assigned
[i
]->data
.location
- generic_base
;
2793 unsigned assigned_use_mask
= (1 << assigned_slots
) - 1;
2795 if ((assigned_use_mask
<< assig_attr
) &
2796 (use_mask
<< attr
)) {
2798 const glsl_type
*assigned_type
=
2799 assigned
[i
]->type
->without_array();
2800 const glsl_type
*type
= var
->type
->without_array();
2801 if (assigned_type
->base_type
!= type
->base_type
) {
2802 linker_error(prog
, "types do not match for aliased"
2803 " %ss %s and %s\n", string
,
2804 assigned
[i
]->name
, var
->name
);
2808 unsigned assigned_component_mask
=
2809 ((1 << assigned_type
->vector_elements
) - 1) <<
2810 assigned
[i
]->data
.location_frac
;
2811 unsigned component_mask
=
2812 ((1 << type
->vector_elements
) - 1) <<
2813 var
->data
.location_frac
;
2814 if (assigned_component_mask
& component_mask
) {
2815 linker_error(prog
, "overlapping component is "
2816 "assigned to %ss %s and %s "
2818 string
, assigned
[i
]->name
, var
->name
,
2819 var
->data
.location_frac
);
2824 } else if (target_index
== MESA_SHADER_FRAGMENT
||
2825 (prog
->IsES
&& prog
->Version
>= 300)) {
2826 linker_error(prog
, "overlapping location is assigned "
2827 "to %s `%s' %d %d %d\n", string
, var
->name
,
2828 used_locations
, use_mask
, attr
);
2831 linker_warning(prog
, "overlapping location is assigned "
2832 "to %s `%s' %d %d %d\n", string
, var
->name
,
2833 used_locations
, use_mask
, attr
);
2837 used_locations
|= (use_mask
<< attr
);
2839 /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
2841 * "A program with more than the value of MAX_VERTEX_ATTRIBS
2842 * active attribute variables may fail to link, unless
2843 * device-dependent optimizations are able to make the program
2844 * fit within available hardware resources. For the purposes
2845 * of this test, attribute variables of the type dvec3, dvec4,
2846 * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
2847 * count as consuming twice as many attributes as equivalent
2848 * single-precision types. While these types use the same number
2849 * of generic attributes as their single-precision equivalents,
2850 * implementations are permitted to consume two single-precision
2851 * vectors of internal storage for each three- or four-component
2852 * double-precision vector."
2854 * Mark this attribute slot as taking up twice as much space
2855 * so we can count it properly against limits. According to
2856 * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
2857 * is optional behavior, but it seems preferable.
2859 if (var
->type
->without_array()->is_dual_slot())
2860 double_storage_locations
|= (use_mask
<< attr
);
2863 assigned
[assigned_attr
] = var
;
2869 if (num_attr
>= max_index
) {
2870 linker_error(prog
, "too many %s (max %u)",
2871 target_index
== MESA_SHADER_VERTEX
?
2872 "vertex shader inputs" : "fragment shader outputs",
2876 to_assign
[num_attr
].slots
= slots
;
2877 to_assign
[num_attr
].var
= var
;
2881 if (target_index
== MESA_SHADER_VERTEX
) {
2882 unsigned total_attribs_size
=
2883 _mesa_bitcount(used_locations
& ((1 << max_index
) - 1)) +
2884 _mesa_bitcount(double_storage_locations
);
2885 if (total_attribs_size
> max_index
) {
2887 "attempt to use %d vertex attribute slots only %d available ",
2888 total_attribs_size
, max_index
);
2893 /* If all of the attributes were assigned locations by the application (or
2894 * are built-in attributes with fixed locations), return early. This should
2895 * be the common case.
2900 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
2902 if (target_index
== MESA_SHADER_VERTEX
) {
2903 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
2904 * only be explicitly assigned by via glBindAttribLocation. Mark it as
2905 * reserved to prevent it from being automatically allocated below.
2907 find_deref_visitor
find("gl_Vertex");
2909 if (find
.variable_found())
2910 used_locations
|= (1 << 0);
2913 for (unsigned i
= 0; i
< num_attr
; i
++) {
2914 /* Mask representing the contiguous slots that will be used by this
2917 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
2919 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
2922 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
2923 ? "vertex shader input" : "fragment shader output";
2926 "insufficient contiguous locations "
2927 "available for %s `%s'\n",
2928 string
, to_assign
[i
].var
->name
);
2932 to_assign
[i
].var
->data
.location
= generic_base
+ location
;
2933 to_assign
[i
].var
->data
.is_unmatched_generic_inout
= 0;
2934 used_locations
|= (use_mask
<< location
);
2936 if (to_assign
[i
].var
->type
->without_array()->is_dual_slot())
2937 double_storage_locations
|= (use_mask
<< location
);
2940 /* Now that we have all the locations, from the GL 4.5 core spec, section
2941 * 11.1.1 (Vertex Attributes), dvec3, dvec4, dmat2x3, dmat2x4, dmat3,
2942 * dmat3x4, dmat4x3, and dmat4 count as consuming twice as many attributes
2943 * as equivalent single-precision types.
2945 if (target_index
== MESA_SHADER_VERTEX
) {
2946 unsigned total_attribs_size
=
2947 _mesa_bitcount(used_locations
& ((1 << max_index
) - 1)) +
2948 _mesa_bitcount(double_storage_locations
);
2949 if (total_attribs_size
> max_index
) {
2951 "attempt to use %d vertex attribute slots only %d available ",
2952 total_attribs_size
, max_index
);
2961 * Match explicit locations of outputs to inputs and deactivate the
2962 * unmatch flag if found so we don't optimise them away.
2965 match_explicit_outputs_to_inputs(gl_linked_shader
*producer
,
2966 gl_linked_shader
*consumer
)
2968 glsl_symbol_table parameters
;
2969 ir_variable
*explicit_locations
[MAX_VARYINGS_INCL_PATCH
][4] =
2972 /* Find all shader outputs in the "producer" stage.
2974 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
2975 ir_variable
*const var
= node
->as_variable();
2977 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_shader_out
))
2980 if (var
->data
.explicit_location
&&
2981 var
->data
.location
>= VARYING_SLOT_VAR0
) {
2982 const unsigned idx
= var
->data
.location
- VARYING_SLOT_VAR0
;
2983 if (explicit_locations
[idx
][var
->data
.location_frac
] == NULL
)
2984 explicit_locations
[idx
][var
->data
.location_frac
] = var
;
2988 /* Match inputs to outputs */
2989 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
2990 ir_variable
*const input
= node
->as_variable();
2992 if ((input
== NULL
) || (input
->data
.mode
!= ir_var_shader_in
))
2995 ir_variable
*output
= NULL
;
2996 if (input
->data
.explicit_location
2997 && input
->data
.location
>= VARYING_SLOT_VAR0
) {
2998 output
= explicit_locations
[input
->data
.location
- VARYING_SLOT_VAR0
]
2999 [input
->data
.location_frac
];
3001 if (output
!= NULL
){
3002 input
->data
.is_unmatched_generic_inout
= 0;
3003 output
->data
.is_unmatched_generic_inout
= 0;
3010 * Store the gl_FragDepth layout in the gl_shader_program struct.
3013 store_fragdepth_layout(struct gl_shader_program
*prog
)
3015 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
3019 struct exec_list
*ir
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
3021 /* We don't look up the gl_FragDepth symbol directly because if
3022 * gl_FragDepth is not used in the shader, it's removed from the IR.
3023 * However, the symbol won't be removed from the symbol table.
3025 * We're only interested in the cases where the variable is NOT removed
3028 foreach_in_list(ir_instruction
, node
, ir
) {
3029 ir_variable
*const var
= node
->as_variable();
3031 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
) {
3035 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
3036 switch (var
->data
.depth_layout
) {
3037 case ir_depth_layout_none
:
3038 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
3040 case ir_depth_layout_any
:
3041 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
3043 case ir_depth_layout_greater
:
3044 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
3046 case ir_depth_layout_less
:
3047 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
3049 case ir_depth_layout_unchanged
:
3050 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3061 * Validate the resources used by a program versus the implementation limits
3064 check_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3066 unsigned total_uniform_blocks
= 0;
3067 unsigned total_shader_storage_blocks
= 0;
3069 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3070 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3075 if (sh
->num_samplers
> ctx
->Const
.Program
[i
].MaxTextureImageUnits
) {
3076 linker_error(prog
, "Too many %s shader texture samplers\n",
3077 _mesa_shader_stage_to_string(i
));
3080 if (sh
->num_uniform_components
>
3081 ctx
->Const
.Program
[i
].MaxUniformComponents
) {
3082 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
3083 linker_warning(prog
, "Too many %s shader default uniform block "
3084 "components, but the driver will try to optimize "
3085 "them out; this is non-portable out-of-spec "
3087 _mesa_shader_stage_to_string(i
));
3089 linker_error(prog
, "Too many %s shader default uniform block "
3091 _mesa_shader_stage_to_string(i
));
3095 if (sh
->num_combined_uniform_components
>
3096 ctx
->Const
.Program
[i
].MaxCombinedUniformComponents
) {
3097 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
3098 linker_warning(prog
, "Too many %s shader uniform components, "
3099 "but the driver will try to optimize them out; "
3100 "this is non-portable out-of-spec behavior\n",
3101 _mesa_shader_stage_to_string(i
));
3103 linker_error(prog
, "Too many %s shader uniform components\n",
3104 _mesa_shader_stage_to_string(i
));
3108 total_shader_storage_blocks
+= sh
->NumShaderStorageBlocks
;
3109 total_uniform_blocks
+= sh
->NumUniformBlocks
;
3111 const unsigned max_uniform_blocks
=
3112 ctx
->Const
.Program
[i
].MaxUniformBlocks
;
3113 if (max_uniform_blocks
< sh
->NumUniformBlocks
) {
3114 linker_error(prog
, "Too many %s uniform blocks (%d/%d)\n",
3115 _mesa_shader_stage_to_string(i
), sh
->NumUniformBlocks
,
3116 max_uniform_blocks
);
3119 const unsigned max_shader_storage_blocks
=
3120 ctx
->Const
.Program
[i
].MaxShaderStorageBlocks
;
3121 if (max_shader_storage_blocks
< sh
->NumShaderStorageBlocks
) {
3122 linker_error(prog
, "Too many %s shader storage blocks (%d/%d)\n",
3123 _mesa_shader_stage_to_string(i
),
3124 sh
->NumShaderStorageBlocks
, max_shader_storage_blocks
);
3128 if (total_uniform_blocks
> ctx
->Const
.MaxCombinedUniformBlocks
) {
3129 linker_error(prog
, "Too many combined uniform blocks (%d/%d)\n",
3130 total_uniform_blocks
, ctx
->Const
.MaxCombinedUniformBlocks
);
3133 if (total_shader_storage_blocks
> ctx
->Const
.MaxCombinedShaderStorageBlocks
) {
3134 linker_error(prog
, "Too many combined shader storage blocks (%d/%d)\n",
3135 total_shader_storage_blocks
,
3136 ctx
->Const
.MaxCombinedShaderStorageBlocks
);
3139 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
3140 if (prog
->UniformBlocks
[i
].UniformBufferSize
>
3141 ctx
->Const
.MaxUniformBlockSize
) {
3142 linker_error(prog
, "Uniform block %s too big (%d/%d)\n",
3143 prog
->UniformBlocks
[i
].Name
,
3144 prog
->UniformBlocks
[i
].UniformBufferSize
,
3145 ctx
->Const
.MaxUniformBlockSize
);
3149 for (unsigned i
= 0; i
< prog
->NumShaderStorageBlocks
; i
++) {
3150 if (prog
->ShaderStorageBlocks
[i
].UniformBufferSize
>
3151 ctx
->Const
.MaxShaderStorageBlockSize
) {
3152 linker_error(prog
, "Shader storage block %s too big (%d/%d)\n",
3153 prog
->ShaderStorageBlocks
[i
].Name
,
3154 prog
->ShaderStorageBlocks
[i
].UniformBufferSize
,
3155 ctx
->Const
.MaxShaderStorageBlockSize
);
3161 link_calculate_subroutine_compat(struct gl_shader_program
*prog
)
3163 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3164 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3169 for (unsigned j
= 0; j
< sh
->NumSubroutineUniformRemapTable
; j
++) {
3170 if (sh
->SubroutineUniformRemapTable
[j
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
)
3173 struct gl_uniform_storage
*uni
= sh
->SubroutineUniformRemapTable
[j
];
3178 sh
->NumSubroutineUniforms
++;
3180 if (sh
->NumSubroutineFunctions
== 0) {
3181 linker_error(prog
, "subroutine uniform %s defined but no valid functions found\n", uni
->type
->name
);
3184 for (unsigned f
= 0; f
< sh
->NumSubroutineFunctions
; f
++) {
3185 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[f
];
3186 for (int k
= 0; k
< fn
->num_compat_types
; k
++) {
3187 if (fn
->types
[k
] == uni
->type
) {
3193 uni
->num_compatible_subroutines
= count
;
3199 check_subroutine_resources(struct gl_shader_program
*prog
)
3201 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3202 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3205 if (sh
->NumSubroutineUniformRemapTable
> MAX_SUBROUTINE_UNIFORM_LOCATIONS
)
3206 linker_error(prog
, "Too many %s shader subroutine uniforms\n",
3207 _mesa_shader_stage_to_string(i
));
3212 * Validate shader image resources.
3215 check_image_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3217 unsigned total_image_units
= 0;
3218 unsigned fragment_outputs
= 0;
3219 unsigned total_shader_storage_blocks
= 0;
3221 if (!ctx
->Extensions
.ARB_shader_image_load_store
)
3224 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3225 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3228 if (sh
->NumImages
> ctx
->Const
.Program
[i
].MaxImageUniforms
)
3229 linker_error(prog
, "Too many %s shader image uniforms (%u > %u)\n",
3230 _mesa_shader_stage_to_string(i
), sh
->NumImages
,
3231 ctx
->Const
.Program
[i
].MaxImageUniforms
);
3233 total_image_units
+= sh
->NumImages
;
3234 total_shader_storage_blocks
+= sh
->NumShaderStorageBlocks
;
3236 if (i
== MESA_SHADER_FRAGMENT
) {
3237 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3238 ir_variable
*var
= node
->as_variable();
3239 if (var
&& var
->data
.mode
== ir_var_shader_out
)
3240 /* since there are no double fs outputs - pass false */
3241 fragment_outputs
+= var
->type
->count_attribute_slots(false);
3247 if (total_image_units
> ctx
->Const
.MaxCombinedImageUniforms
)
3248 linker_error(prog
, "Too many combined image uniforms\n");
3250 if (total_image_units
+ fragment_outputs
+ total_shader_storage_blocks
>
3251 ctx
->Const
.MaxCombinedShaderOutputResources
)
3252 linker_error(prog
, "Too many combined image uniforms, shader storage "
3253 " buffers and fragment outputs\n");
3258 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
3259 * for a variable, checks for overlaps between other uniforms using explicit
3263 reserve_explicit_locations(struct gl_shader_program
*prog
,
3264 string_to_uint_map
*map
, ir_variable
*var
)
3266 unsigned slots
= var
->type
->uniform_locations();
3267 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3268 unsigned return_value
= slots
;
3270 /* Resize remap table if locations do not fit in the current one. */
3271 if (max_loc
+ 1 > prog
->NumUniformRemapTable
) {
3272 prog
->UniformRemapTable
=
3273 reralloc(prog
, prog
->UniformRemapTable
,
3274 gl_uniform_storage
*,
3277 if (!prog
->UniformRemapTable
) {
3278 linker_error(prog
, "Out of memory during linking.\n");
3282 /* Initialize allocated space. */
3283 for (unsigned i
= prog
->NumUniformRemapTable
; i
< max_loc
+ 1; i
++)
3284 prog
->UniformRemapTable
[i
] = NULL
;
3286 prog
->NumUniformRemapTable
= max_loc
+ 1;
3289 for (unsigned i
= 0; i
< slots
; i
++) {
3290 unsigned loc
= var
->data
.location
+ i
;
3292 /* Check if location is already used. */
3293 if (prog
->UniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3295 /* Possibly same uniform from a different stage, this is ok. */
3297 if (map
->get(hash_loc
, var
->name
) && hash_loc
== loc
- i
) {
3302 /* ARB_explicit_uniform_location specification states:
3304 * "No two default-block uniform variables in the program can have
3305 * the same location, even if they are unused, otherwise a compiler
3306 * or linker error will be generated."
3309 "location qualifier for uniform %s overlaps "
3310 "previously used location\n",
3315 /* Initialize location as inactive before optimization
3316 * rounds and location assignment.
3318 prog
->UniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3321 /* Note, base location used for arrays. */
3322 map
->put(var
->data
.location
, var
->name
);
3324 return return_value
;
3328 reserve_subroutine_explicit_locations(struct gl_shader_program
*prog
,
3329 struct gl_linked_shader
*sh
,
3332 unsigned slots
= var
->type
->uniform_locations();
3333 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3335 /* Resize remap table if locations do not fit in the current one. */
3336 if (max_loc
+ 1 > sh
->NumSubroutineUniformRemapTable
) {
3337 sh
->SubroutineUniformRemapTable
=
3338 reralloc(sh
, sh
->SubroutineUniformRemapTable
,
3339 gl_uniform_storage
*,
3342 if (!sh
->SubroutineUniformRemapTable
) {
3343 linker_error(prog
, "Out of memory during linking.\n");
3347 /* Initialize allocated space. */
3348 for (unsigned i
= sh
->NumSubroutineUniformRemapTable
; i
< max_loc
+ 1; i
++)
3349 sh
->SubroutineUniformRemapTable
[i
] = NULL
;
3351 sh
->NumSubroutineUniformRemapTable
= max_loc
+ 1;
3354 for (unsigned i
= 0; i
< slots
; i
++) {
3355 unsigned loc
= var
->data
.location
+ i
;
3357 /* Check if location is already used. */
3358 if (sh
->SubroutineUniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3360 /* ARB_explicit_uniform_location specification states:
3361 * "No two subroutine uniform variables can have the same location
3362 * in the same shader stage, otherwise a compiler or linker error
3363 * will be generated."
3366 "location qualifier for uniform %s overlaps "
3367 "previously used location\n",
3372 /* Initialize location as inactive before optimization
3373 * rounds and location assignment.
3375 sh
->SubroutineUniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3381 * Check and reserve all explicit uniform locations, called before
3382 * any optimizations happen to handle also inactive uniforms and
3383 * inactive array elements that may get trimmed away.
3386 check_explicit_uniform_locations(struct gl_context
*ctx
,
3387 struct gl_shader_program
*prog
)
3389 if (!ctx
->Extensions
.ARB_explicit_uniform_location
)
3392 /* This map is used to detect if overlapping explicit locations
3393 * occur with the same uniform (from different stage) or a different one.
3395 string_to_uint_map
*uniform_map
= new string_to_uint_map
;
3398 linker_error(prog
, "Out of memory during linking.\n");
3402 unsigned entries_total
= 0;
3403 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3404 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3409 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3410 ir_variable
*var
= node
->as_variable();
3411 if (!var
|| var
->data
.mode
!= ir_var_uniform
)
3414 if (var
->data
.explicit_location
) {
3416 if (var
->type
->without_array()->is_subroutine())
3417 ret
= reserve_subroutine_explicit_locations(prog
, sh
, var
);
3419 int slots
= reserve_explicit_locations(prog
, uniform_map
,
3423 entries_total
+= slots
;
3434 struct empty_uniform_block
*current_block
= NULL
;
3436 for (unsigned i
= 0; i
< prog
->NumUniformRemapTable
; i
++) {
3437 /* We found empty space in UniformRemapTable. */
3438 if (prog
->UniformRemapTable
[i
] == NULL
) {
3439 /* We've found the beginning of a new continous block of empty slots */
3440 if (!current_block
|| current_block
->start
+ current_block
->slots
!= i
) {
3441 current_block
= rzalloc(prog
, struct empty_uniform_block
);
3442 current_block
->start
= i
;
3443 exec_list_push_tail(&prog
->EmptyUniformLocations
,
3444 ¤t_block
->link
);
3447 /* The current block continues, so we simply increment its slots */
3448 current_block
->slots
++;
3453 return entries_total
;
3457 should_add_buffer_variable(struct gl_shader_program
*shProg
,
3458 GLenum type
, const char *name
)
3460 bool found_interface
= false;
3461 unsigned block_name_len
= 0;
3462 const char *block_name_dot
= strchr(name
, '.');
3464 /* These rules only apply to buffer variables. So we return
3465 * true for the rest of types.
3467 if (type
!= GL_BUFFER_VARIABLE
)
3470 for (unsigned i
= 0; i
< shProg
->NumShaderStorageBlocks
; i
++) {
3471 const char *block_name
= shProg
->ShaderStorageBlocks
[i
].Name
;
3472 block_name_len
= strlen(block_name
);
3474 const char *block_square_bracket
= strchr(block_name
, '[');
3475 if (block_square_bracket
) {
3476 /* The block is part of an array of named interfaces,
3477 * for the name comparison we ignore the "[x]" part.
3479 block_name_len
-= strlen(block_square_bracket
);
3482 if (block_name_dot
) {
3483 /* Check if the variable name starts with the interface
3484 * name. The interface name (if present) should have the
3485 * length than the interface block name we are comparing to.
3487 unsigned len
= strlen(name
) - strlen(block_name_dot
);
3488 if (len
!= block_name_len
)
3492 if (strncmp(block_name
, name
, block_name_len
) == 0) {
3493 found_interface
= true;
3498 /* We remove the interface name from the buffer variable name,
3499 * including the dot that follows it.
3501 if (found_interface
)
3502 name
= name
+ block_name_len
+ 1;
3504 /* The ARB_program_interface_query spec says:
3506 * "For an active shader storage block member declared as an array, an
3507 * entry will be generated only for the first array element, regardless
3508 * of its type. For arrays of aggregate types, the enumeration rules
3509 * are applied recursively for the single enumerated array element."
3511 const char *struct_first_dot
= strchr(name
, '.');
3512 const char *first_square_bracket
= strchr(name
, '[');
3514 /* The buffer variable is on top level and it is not an array */
3515 if (!first_square_bracket
) {
3517 /* The shader storage block member is a struct, then generate the entry */
3518 } else if (struct_first_dot
&& struct_first_dot
< first_square_bracket
) {
3521 /* Shader storage block member is an array, only generate an entry for the
3522 * first array element.
3524 if (strncmp(first_square_bracket
, "[0]", 3) == 0)
3532 add_program_resource(struct gl_shader_program
*prog
,
3533 struct set
*resource_set
,
3534 GLenum type
, const void *data
, uint8_t stages
)
3538 /* If resource already exists, do not add it again. */
3539 if (_mesa_set_search(resource_set
, data
))
3542 prog
->ProgramResourceList
=
3544 prog
->ProgramResourceList
,
3545 gl_program_resource
,
3546 prog
->NumProgramResourceList
+ 1);
3548 if (!prog
->ProgramResourceList
) {
3549 linker_error(prog
, "Out of memory during linking.\n");
3553 struct gl_program_resource
*res
=
3554 &prog
->ProgramResourceList
[prog
->NumProgramResourceList
];
3558 res
->StageReferences
= stages
;
3560 prog
->NumProgramResourceList
++;
3562 _mesa_set_add(resource_set
, data
);
3567 /* Function checks if a variable var is a packed varying and
3568 * if given name is part of packed varying's list.
3570 * If a variable is a packed varying, it has a name like
3571 * 'packed:a,b,c' where a, b and c are separate variables.
3574 included_in_packed_varying(ir_variable
*var
, const char *name
)
3576 if (strncmp(var
->name
, "packed:", 7) != 0)
3579 char *list
= strdup(var
->name
+ 7);
3584 char *token
= strtok_r(list
, ",", &saveptr
);
3586 if (strcmp(token
, name
) == 0) {
3590 token
= strtok_r(NULL
, ",", &saveptr
);
3597 * Function builds a stage reference bitmask from variable name.
3600 build_stageref(struct gl_shader_program
*shProg
, const char *name
,
3605 /* Note, that we assume MAX 8 stages, if there will be more stages, type
3606 * used for reference mask in gl_program_resource will need to be changed.
3608 assert(MESA_SHADER_STAGES
< 8);
3610 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3611 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
3615 /* Shader symbol table may contain variables that have
3616 * been optimized away. Search IR for the variable instead.
3618 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3619 ir_variable
*var
= node
->as_variable();
3621 unsigned baselen
= strlen(var
->name
);
3623 if (included_in_packed_varying(var
, name
)) {
3628 /* Type needs to match if specified, otherwise we might
3629 * pick a variable with same name but different interface.
3631 if (var
->data
.mode
!= mode
)
3634 if (strncmp(var
->name
, name
, baselen
) == 0) {
3635 /* Check for exact name matches but also check for arrays and
3638 if (name
[baselen
] == '\0' ||
3639 name
[baselen
] == '[' ||
3640 name
[baselen
] == '.') {
3652 * Create gl_shader_variable from ir_variable class.
3654 static gl_shader_variable
*
3655 create_shader_variable(struct gl_shader_program
*shProg
,
3656 const ir_variable
*in
,
3657 const char *name
, const glsl_type
*type
,
3658 bool use_implicit_location
, int location
,
3659 const glsl_type
*outermost_struct_type
)
3661 gl_shader_variable
*out
= ralloc(shProg
, struct gl_shader_variable
);
3665 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
3666 * expect to see gl_VertexID in the program resource list. Pretend.
3668 if (in
->data
.mode
== ir_var_system_value
&&
3669 in
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
3670 out
->name
= ralloc_strdup(shProg
, "gl_VertexID");
3671 } else if ((in
->data
.mode
== ir_var_shader_out
&&
3672 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
) ||
3673 (in
->data
.mode
== ir_var_system_value
&&
3674 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_OUTER
)) {
3675 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelOuter");
3676 type
= glsl_type::get_array_instance(glsl_type::float_type
, 4);
3677 } else if ((in
->data
.mode
== ir_var_shader_out
&&
3678 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
) ||
3679 (in
->data
.mode
== ir_var_system_value
&&
3680 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_INNER
)) {
3681 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelInner");
3682 type
= glsl_type::get_array_instance(glsl_type::float_type
, 2);
3684 out
->name
= ralloc_strdup(shProg
, name
);
3690 /* The ARB_program_interface_query spec says:
3692 * "Not all active variables are assigned valid locations; the
3693 * following variables will have an effective location of -1:
3695 * * uniforms declared as atomic counters;
3697 * * members of a uniform block;
3699 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
3701 * * inputs or outputs not declared with a "location" layout
3702 * qualifier, except for vertex shader inputs and fragment shader
3705 if (in
->type
->base_type
== GLSL_TYPE_ATOMIC_UINT
||
3706 is_gl_identifier(in
->name
) ||
3707 !(in
->data
.explicit_location
|| use_implicit_location
)) {
3710 out
->location
= location
;
3714 out
->outermost_struct_type
= outermost_struct_type
;
3715 out
->interface_type
= in
->get_interface_type();
3716 out
->component
= in
->data
.location_frac
;
3717 out
->index
= in
->data
.index
;
3718 out
->patch
= in
->data
.patch
;
3719 out
->mode
= in
->data
.mode
;
3720 out
->interpolation
= in
->data
.interpolation
;
3721 out
->explicit_location
= in
->data
.explicit_location
;
3722 out
->precision
= in
->data
.precision
;
3728 add_shader_variable(struct gl_shader_program
*shProg
, struct set
*resource_set
,
3729 unsigned stage_mask
,
3730 GLenum programInterface
, ir_variable
*var
,
3731 const char *name
, const glsl_type
*type
,
3732 bool use_implicit_location
, int location
,
3733 const glsl_type
*outermost_struct_type
= NULL
)
3735 const bool is_vertex_input
=
3736 programInterface
== GL_PROGRAM_INPUT
&&
3737 stage_mask
== MESA_SHADER_VERTEX
;
3739 switch (type
->base_type
) {
3740 case GLSL_TYPE_STRUCT
: {
3741 /* The ARB_program_interface_query spec says:
3743 * "For an active variable declared as a structure, a separate entry
3744 * will be generated for each active structure member. The name of
3745 * each entry is formed by concatenating the name of the structure,
3746 * the "." character, and the name of the structure member. If a
3747 * structure member to enumerate is itself a structure or array,
3748 * these enumeration rules are applied recursively."
3750 if (outermost_struct_type
== NULL
)
3751 outermost_struct_type
= type
;
3753 unsigned field_location
= location
;
3754 for (unsigned i
= 0; i
< type
->length
; i
++) {
3755 const struct glsl_struct_field
*field
= &type
->fields
.structure
[i
];
3756 char *field_name
= ralloc_asprintf(shProg
, "%s.%s", name
, field
->name
);
3757 if (!add_shader_variable(shProg
, resource_set
,
3758 stage_mask
, programInterface
,
3759 var
, field_name
, field
->type
,
3760 use_implicit_location
, field_location
,
3761 outermost_struct_type
))
3765 field
->type
->count_attribute_slots(is_vertex_input
);
3771 /* Issue #16 of the ARB_program_interface_query spec says:
3773 * "* If a variable is a member of an interface block without an
3774 * instance name, it is enumerated using just the variable name.
3776 * * If a variable is a member of an interface block with an instance
3777 * name, it is enumerated as "BlockName.Member", where "BlockName" is
3778 * the name of the interface block (not the instance name) and
3779 * "Member" is the name of the variable."
3781 const char *prefixed_name
= (var
->data
.from_named_ifc_block
&&
3782 !is_gl_identifier(var
->name
))
3783 ? ralloc_asprintf(shProg
, "%s.%s", var
->get_interface_type()->name
,
3787 /* The ARB_program_interface_query spec says:
3789 * "For an active variable declared as a single instance of a basic
3790 * type, a single entry will be generated, using the variable name
3791 * from the shader source."
3793 gl_shader_variable
*sha_v
=
3794 create_shader_variable(shProg
, var
, prefixed_name
, type
,
3795 use_implicit_location
, location
,
3796 outermost_struct_type
);
3800 return add_program_resource(shProg
, resource_set
,
3801 programInterface
, sha_v
, stage_mask
);
3807 add_interface_variables(struct gl_shader_program
*shProg
,
3808 struct set
*resource_set
,
3809 unsigned stage
, GLenum programInterface
)
3811 exec_list
*ir
= shProg
->_LinkedShaders
[stage
]->ir
;
3813 foreach_in_list(ir_instruction
, node
, ir
) {
3814 ir_variable
*var
= node
->as_variable();
3816 if (!var
|| var
->data
.how_declared
== ir_var_hidden
)
3821 switch (var
->data
.mode
) {
3822 case ir_var_system_value
:
3823 case ir_var_shader_in
:
3824 if (programInterface
!= GL_PROGRAM_INPUT
)
3826 loc_bias
= (stage
== MESA_SHADER_VERTEX
) ? int(VERT_ATTRIB_GENERIC0
)
3827 : int(VARYING_SLOT_VAR0
);
3829 case ir_var_shader_out
:
3830 if (programInterface
!= GL_PROGRAM_OUTPUT
)
3832 loc_bias
= (stage
== MESA_SHADER_FRAGMENT
) ? int(FRAG_RESULT_DATA0
)
3833 : int(VARYING_SLOT_VAR0
);
3839 if (var
->data
.patch
)
3840 loc_bias
= int(VARYING_SLOT_PATCH0
);
3842 /* Skip packed varyings, packed varyings are handled separately
3843 * by add_packed_varyings.
3845 if (strncmp(var
->name
, "packed:", 7) == 0)
3848 /* Skip fragdata arrays, these are handled separately
3849 * by add_fragdata_arrays.
3851 if (strncmp(var
->name
, "gl_out_FragData", 15) == 0)
3854 const bool vs_input_or_fs_output
=
3855 (stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
3856 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
);
3858 if (!add_shader_variable(shProg
, resource_set
,
3859 1 << stage
, programInterface
,
3860 var
, var
->name
, var
->type
, vs_input_or_fs_output
,
3861 var
->data
.location
- loc_bias
))
3868 add_packed_varyings(struct gl_shader_program
*shProg
, struct set
*resource_set
,
3869 int stage
, GLenum type
)
3871 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[stage
];
3874 if (!sh
|| !sh
->packed_varyings
)
3877 foreach_in_list(ir_instruction
, node
, sh
->packed_varyings
) {
3878 ir_variable
*var
= node
->as_variable();
3880 switch (var
->data
.mode
) {
3881 case ir_var_shader_in
:
3882 iface
= GL_PROGRAM_INPUT
;
3884 case ir_var_shader_out
:
3885 iface
= GL_PROGRAM_OUTPUT
;
3888 unreachable("unexpected type");
3891 if (type
== iface
) {
3892 const int stage_mask
=
3893 build_stageref(shProg
, var
->name
, var
->data
.mode
);
3894 if (!add_shader_variable(shProg
, resource_set
,
3896 iface
, var
, var
->name
, var
->type
, false,
3897 var
->data
.location
- VARYING_SLOT_VAR0
))
3906 add_fragdata_arrays(struct gl_shader_program
*shProg
, struct set
*resource_set
)
3908 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
3910 if (!sh
|| !sh
->fragdata_arrays
)
3913 foreach_in_list(ir_instruction
, node
, sh
->fragdata_arrays
) {
3914 ir_variable
*var
= node
->as_variable();
3916 assert(var
->data
.mode
== ir_var_shader_out
);
3918 if (!add_shader_variable(shProg
, resource_set
,
3919 1 << MESA_SHADER_FRAGMENT
,
3920 GL_PROGRAM_OUTPUT
, var
, var
->name
, var
->type
,
3921 true, var
->data
.location
- FRAG_RESULT_DATA0
))
3929 get_top_level_name(const char *name
)
3931 const char *first_dot
= strchr(name
, '.');
3932 const char *first_square_bracket
= strchr(name
, '[');
3935 /* The ARB_program_interface_query spec says:
3937 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying
3938 * the number of active array elements of the top-level shader storage
3939 * block member containing to the active variable is written to
3940 * <params>. If the top-level block member is not declared as an
3941 * array, the value one is written to <params>. If the top-level block
3942 * member is an array with no declared size, the value zero is written
3946 /* The buffer variable is on top level.*/
3947 if (!first_square_bracket
&& !first_dot
)
3948 name_size
= strlen(name
);
3949 else if ((!first_square_bracket
||
3950 (first_dot
&& first_dot
< first_square_bracket
)))
3951 name_size
= first_dot
- name
;
3953 name_size
= first_square_bracket
- name
;
3955 return strndup(name
, name_size
);
3959 get_var_name(const char *name
)
3961 const char *first_dot
= strchr(name
, '.');
3964 return strdup(name
);
3966 return strndup(first_dot
+1, strlen(first_dot
) - 1);
3970 is_top_level_shader_storage_block_member(const char* name
,
3971 const char* interface_name
,
3972 const char* field_name
)
3974 bool result
= false;
3976 /* If the given variable is already a top-level shader storage
3977 * block member, then return array_size = 1.
3978 * We could have two possibilities: if we have an instanced
3979 * shader storage block or not instanced.
3981 * For the first, we check create a name as it was in top level and
3982 * compare it with the real name. If they are the same, then
3983 * the variable is already at top-level.
3985 * Full instanced name is: interface name + '.' + var name +
3988 int name_length
= strlen(interface_name
) + 1 + strlen(field_name
) + 1;
3989 char *full_instanced_name
= (char *) calloc(name_length
, sizeof(char));
3990 if (!full_instanced_name
) {
3991 fprintf(stderr
, "%s: Cannot allocate space for name\n", __func__
);
3995 snprintf(full_instanced_name
, name_length
, "%s.%s",
3996 interface_name
, field_name
);
3998 /* Check if its top-level shader storage block member of an
3999 * instanced interface block, or of a unnamed interface block.
4001 if (strcmp(name
, full_instanced_name
) == 0 ||
4002 strcmp(name
, field_name
) == 0)
4005 free(full_instanced_name
);
4010 get_array_size(struct gl_uniform_storage
*uni
, const glsl_struct_field
*field
,
4011 char *interface_name
, char *var_name
)
4013 /* The ARB_program_interface_query spec says:
4015 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying
4016 * the number of active array elements of the top-level shader storage
4017 * block member containing to the active variable is written to
4018 * <params>. If the top-level block member is not declared as an
4019 * array, the value one is written to <params>. If the top-level block
4020 * member is an array with no declared size, the value zero is written
4023 if (is_top_level_shader_storage_block_member(uni
->name
,
4027 else if (field
->type
->is_unsized_array())
4029 else if (field
->type
->is_array())
4030 return field
->type
->length
;
4036 get_array_stride(struct gl_uniform_storage
*uni
, const glsl_type
*interface
,
4037 const glsl_struct_field
*field
, char *interface_name
,
4040 /* The ARB_program_interface_query spec says:
4042 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
4043 * identifying the stride between array elements of the top-level
4044 * shader storage block member containing the active variable is
4045 * written to <params>. For top-level block members declared as
4046 * arrays, the value written is the difference, in basic machine units,
4047 * between the offsets of the active variable for consecutive elements
4048 * in the top-level array. For top-level block members not declared as
4049 * an array, zero is written to <params>."
4051 if (field
->type
->is_array()) {
4052 const enum glsl_matrix_layout matrix_layout
=
4053 glsl_matrix_layout(field
->matrix_layout
);
4054 bool row_major
= matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
4055 const glsl_type
*array_type
= field
->type
->fields
.array
;
4057 if (is_top_level_shader_storage_block_member(uni
->name
,
4062 if (interface
->interface_packing
!= GLSL_INTERFACE_PACKING_STD430
) {
4063 if (array_type
->is_record() || array_type
->is_array())
4064 return glsl_align(array_type
->std140_size(row_major
), 16);
4066 return MAX2(array_type
->std140_base_alignment(row_major
), 16);
4068 return array_type
->std430_array_stride(row_major
);
4075 calculate_array_size_and_stride(struct gl_shader_program
*shProg
,
4076 struct gl_uniform_storage
*uni
)
4078 int block_index
= uni
->block_index
;
4079 int array_size
= -1;
4080 int array_stride
= -1;
4081 char *var_name
= get_top_level_name(uni
->name
);
4082 char *interface_name
=
4083 get_top_level_name(uni
->is_shader_storage
?
4084 shProg
->ShaderStorageBlocks
[block_index
].Name
:
4085 shProg
->UniformBlocks
[block_index
].Name
);
4087 if (strcmp(var_name
, interface_name
) == 0) {
4088 /* Deal with instanced array of SSBOs */
4089 char *temp_name
= get_var_name(uni
->name
);
4091 linker_error(shProg
, "Out of memory during linking.\n");
4092 goto write_top_level_array_size_and_stride
;
4095 var_name
= get_top_level_name(temp_name
);
4098 linker_error(shProg
, "Out of memory during linking.\n");
4099 goto write_top_level_array_size_and_stride
;
4103 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4104 const gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
4108 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
4109 ir_variable
*var
= node
->as_variable();
4110 if (!var
|| !var
->get_interface_type() ||
4111 var
->data
.mode
!= ir_var_shader_storage
)
4114 const glsl_type
*interface
= var
->get_interface_type();
4116 if (strcmp(interface_name
, interface
->name
) != 0)
4119 for (unsigned i
= 0; i
< interface
->length
; i
++) {
4120 const glsl_struct_field
*field
= &interface
->fields
.structure
[i
];
4121 if (strcmp(field
->name
, var_name
) != 0)
4124 array_stride
= get_array_stride(uni
, interface
, field
,
4125 interface_name
, var_name
);
4126 array_size
= get_array_size(uni
, field
, interface_name
, var_name
);
4127 goto write_top_level_array_size_and_stride
;
4131 write_top_level_array_size_and_stride
:
4132 free(interface_name
);
4134 uni
->top_level_array_stride
= array_stride
;
4135 uni
->top_level_array_size
= array_size
;
4139 * Builds up a list of program resources that point to existing
4143 build_program_resource_list(struct gl_context
*ctx
,
4144 struct gl_shader_program
*shProg
)
4146 /* Rebuild resource list. */
4147 if (shProg
->ProgramResourceList
) {
4148 ralloc_free(shProg
->ProgramResourceList
);
4149 shProg
->ProgramResourceList
= NULL
;
4150 shProg
->NumProgramResourceList
= 0;
4153 int input_stage
= MESA_SHADER_STAGES
, output_stage
= 0;
4155 /* Determine first input and final output stage. These are used to
4156 * detect which variables should be enumerated in the resource list
4157 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
4159 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4160 if (!shProg
->_LinkedShaders
[i
])
4162 if (input_stage
== MESA_SHADER_STAGES
)
4167 /* Empty shader, no resources. */
4168 if (input_stage
== MESA_SHADER_STAGES
&& output_stage
== 0)
4171 struct set
*resource_set
= _mesa_set_create(NULL
,
4173 _mesa_key_pointer_equal
);
4175 /* Program interface needs to expose varyings in case of SSO. */
4176 if (shProg
->SeparateShader
) {
4177 if (!add_packed_varyings(shProg
, resource_set
,
4178 input_stage
, GL_PROGRAM_INPUT
))
4181 if (!add_packed_varyings(shProg
, resource_set
,
4182 output_stage
, GL_PROGRAM_OUTPUT
))
4186 if (!add_fragdata_arrays(shProg
, resource_set
))
4189 /* Add inputs and outputs to the resource list. */
4190 if (!add_interface_variables(shProg
, resource_set
,
4191 input_stage
, GL_PROGRAM_INPUT
))
4194 if (!add_interface_variables(shProg
, resource_set
,
4195 output_stage
, GL_PROGRAM_OUTPUT
))
4198 /* Add transform feedback varyings. */
4199 if (shProg
->LinkedTransformFeedback
.NumVarying
> 0) {
4200 for (int i
= 0; i
< shProg
->LinkedTransformFeedback
.NumVarying
; i
++) {
4201 if (!add_program_resource(shProg
, resource_set
,
4202 GL_TRANSFORM_FEEDBACK_VARYING
,
4203 &shProg
->LinkedTransformFeedback
.Varyings
[i
],
4209 /* Add transform feedback buffers. */
4210 for (unsigned i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
4211 if ((shProg
->LinkedTransformFeedback
.ActiveBuffers
>> i
) & 1) {
4212 shProg
->LinkedTransformFeedback
.Buffers
[i
].Binding
= i
;
4213 if (!add_program_resource(shProg
, resource_set
,
4214 GL_TRANSFORM_FEEDBACK_BUFFER
,
4215 &shProg
->LinkedTransformFeedback
.Buffers
[i
],
4221 /* Add uniforms from uniform storage. */
4222 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; i
++) {
4223 /* Do not add uniforms internally used by Mesa. */
4224 if (shProg
->UniformStorage
[i
].hidden
)
4228 build_stageref(shProg
, shProg
->UniformStorage
[i
].name
,
4231 /* Add stagereferences for uniforms in a uniform block. */
4232 bool is_shader_storage
= shProg
->UniformStorage
[i
].is_shader_storage
;
4233 int block_index
= shProg
->UniformStorage
[i
].block_index
;
4234 if (block_index
!= -1) {
4235 stageref
|= is_shader_storage
?
4236 shProg
->ShaderStorageBlocks
[block_index
].stageref
:
4237 shProg
->UniformBlocks
[block_index
].stageref
;
4240 GLenum type
= is_shader_storage
? GL_BUFFER_VARIABLE
: GL_UNIFORM
;
4241 if (!should_add_buffer_variable(shProg
, type
,
4242 shProg
->UniformStorage
[i
].name
))
4245 if (is_shader_storage
) {
4246 calculate_array_size_and_stride(shProg
, &shProg
->UniformStorage
[i
]);
4249 if (!add_program_resource(shProg
, resource_set
, type
,
4250 &shProg
->UniformStorage
[i
], stageref
))
4254 /* Add program uniform blocks. */
4255 for (unsigned i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
4256 if (!add_program_resource(shProg
, resource_set
, GL_UNIFORM_BLOCK
,
4257 &shProg
->UniformBlocks
[i
], 0))
4261 /* Add program shader storage blocks. */
4262 for (unsigned i
= 0; i
< shProg
->NumShaderStorageBlocks
; i
++) {
4263 if (!add_program_resource(shProg
, resource_set
, GL_SHADER_STORAGE_BLOCK
,
4264 &shProg
->ShaderStorageBlocks
[i
], 0))
4268 /* Add atomic counter buffers. */
4269 for (unsigned i
= 0; i
< shProg
->NumAtomicBuffers
; i
++) {
4270 if (!add_program_resource(shProg
, resource_set
, GL_ATOMIC_COUNTER_BUFFER
,
4271 &shProg
->AtomicBuffers
[i
], 0))
4275 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; i
++) {
4277 if (!shProg
->UniformStorage
[i
].hidden
)
4280 for (int j
= MESA_SHADER_VERTEX
; j
< MESA_SHADER_STAGES
; j
++) {
4281 if (!shProg
->UniformStorage
[i
].opaque
[j
].active
||
4282 !shProg
->UniformStorage
[i
].type
->is_subroutine())
4285 type
= _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage
)j
);
4286 /* add shader subroutines */
4287 if (!add_program_resource(shProg
, resource_set
,
4288 type
, &shProg
->UniformStorage
[i
], 0))
4293 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4294 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
4300 type
= _mesa_shader_stage_to_subroutine((gl_shader_stage
)i
);
4301 for (unsigned j
= 0; j
< sh
->NumSubroutineFunctions
; j
++) {
4302 if (!add_program_resource(shProg
, resource_set
,
4303 type
, &sh
->SubroutineFunctions
[j
], 0))
4308 _mesa_set_destroy(resource_set
, NULL
);
4312 * This check is done to make sure we allow only constant expression
4313 * indexing and "constant-index-expression" (indexing with an expression
4314 * that includes loop induction variable).
4317 validate_sampler_array_indexing(struct gl_context
*ctx
,
4318 struct gl_shader_program
*prog
)
4320 dynamic_sampler_array_indexing_visitor v
;
4321 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4322 if (prog
->_LinkedShaders
[i
] == NULL
)
4325 bool no_dynamic_indexing
=
4326 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
;
4328 /* Search for array derefs in shader. */
4329 v
.run(prog
->_LinkedShaders
[i
]->ir
);
4330 if (v
.uses_dynamic_sampler_array_indexing()) {
4331 const char *msg
= "sampler arrays indexed with non-constant "
4332 "expressions is forbidden in GLSL %s %u";
4333 /* Backend has indicated that it has no dynamic indexing support. */
4334 if (no_dynamic_indexing
) {
4335 linker_error(prog
, msg
, prog
->IsES
? "ES" : "", prog
->Version
);
4338 linker_warning(prog
, msg
, prog
->IsES
? "ES" : "", prog
->Version
);
4346 link_assign_subroutine_types(struct gl_shader_program
*prog
)
4348 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4349 gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
4354 sh
->MaxSubroutineFunctionIndex
= 0;
4355 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
4356 ir_function
*fn
= node
->as_function();
4360 if (fn
->is_subroutine
)
4361 sh
->NumSubroutineUniformTypes
++;
4363 if (!fn
->num_subroutine_types
)
4366 /* these should have been calculated earlier. */
4367 assert(fn
->subroutine_index
!= -1);
4368 if (sh
->NumSubroutineFunctions
+ 1 > MAX_SUBROUTINES
) {
4369 linker_error(prog
, "Too many subroutine functions declared.\n");
4372 sh
->SubroutineFunctions
= reralloc(sh
, sh
->SubroutineFunctions
,
4373 struct gl_subroutine_function
,
4374 sh
->NumSubroutineFunctions
+ 1);
4375 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].name
= ralloc_strdup(sh
, fn
->name
);
4376 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].num_compat_types
= fn
->num_subroutine_types
;
4377 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].types
=
4378 ralloc_array(sh
, const struct glsl_type
*,
4379 fn
->num_subroutine_types
);
4381 /* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the
4384 * "Each subroutine with an index qualifier in the shader must be
4385 * given a unique index, otherwise a compile or link error will be
4388 for (unsigned j
= 0; j
< sh
->NumSubroutineFunctions
; j
++) {
4389 if (sh
->SubroutineFunctions
[j
].index
!= -1 &&
4390 sh
->SubroutineFunctions
[j
].index
== fn
->subroutine_index
) {
4391 linker_error(prog
, "each subroutine index qualifier in the "
4392 "shader must be unique\n");
4396 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].index
=
4397 fn
->subroutine_index
;
4399 if (fn
->subroutine_index
> (int)sh
->MaxSubroutineFunctionIndex
)
4400 sh
->MaxSubroutineFunctionIndex
= fn
->subroutine_index
;
4402 for (int j
= 0; j
< fn
->num_subroutine_types
; j
++)
4403 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].types
[j
] = fn
->subroutine_types
[j
];
4404 sh
->NumSubroutineFunctions
++;
4410 set_always_active_io(exec_list
*ir
, ir_variable_mode io_mode
)
4412 assert(io_mode
== ir_var_shader_in
|| io_mode
== ir_var_shader_out
);
4414 foreach_in_list(ir_instruction
, node
, ir
) {
4415 ir_variable
*const var
= node
->as_variable();
4417 if (var
== NULL
|| var
->data
.mode
!= io_mode
)
4420 /* Don't set always active on builtins that haven't been redeclared */
4421 if (var
->data
.how_declared
== ir_var_declared_implicitly
)
4424 var
->data
.always_active_io
= true;
4429 * When separate shader programs are enabled, only input/outputs between
4430 * the stages of a multi-stage separate program can be safely removed
4431 * from the shader interface. Other inputs/outputs must remain active.
4434 disable_varying_optimizations_for_sso(struct gl_shader_program
*prog
)
4436 unsigned first
, last
;
4437 assert(prog
->SeparateShader
);
4439 first
= MESA_SHADER_STAGES
;
4442 /* Determine first and last stage. Excluding the compute stage */
4443 for (unsigned i
= 0; i
< MESA_SHADER_COMPUTE
; i
++) {
4444 if (!prog
->_LinkedShaders
[i
])
4446 if (first
== MESA_SHADER_STAGES
)
4451 if (first
== MESA_SHADER_STAGES
)
4454 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4455 gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
4459 if (first
== last
) {
4460 /* For a single shader program only allow inputs to the vertex shader
4461 * and outputs from the fragment shader to be removed.
4463 if (stage
!= MESA_SHADER_VERTEX
)
4464 set_always_active_io(sh
->ir
, ir_var_shader_in
);
4465 if (stage
!= MESA_SHADER_FRAGMENT
)
4466 set_always_active_io(sh
->ir
, ir_var_shader_out
);
4468 /* For multi-stage separate shader programs only allow inputs and
4469 * outputs between the shader stages to be removed as well as inputs
4470 * to the vertex shader and outputs from the fragment shader.
4472 if (stage
== first
&& stage
!= MESA_SHADER_VERTEX
)
4473 set_always_active_io(sh
->ir
, ir_var_shader_in
);
4474 else if (stage
== last
&& stage
!= MESA_SHADER_FRAGMENT
)
4475 set_always_active_io(sh
->ir
, ir_var_shader_out
);
4481 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4483 prog
->LinkStatus
= true; /* All error paths will set this to false */
4484 prog
->Validated
= false;
4485 prog
->_Used
= false;
4487 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec says:
4489 * "Linking can fail for a variety of reasons as specified in the
4490 * OpenGL Shading Language Specification, as well as any of the
4491 * following reasons:
4493 * - No shader objects are attached to program."
4495 * The Compatibility Profile specification does not list the error. In
4496 * Compatibility Profile missing shader stages are replaced by
4497 * fixed-function. This applies to the case where all stages are
4500 if (prog
->NumShaders
== 0) {
4501 if (ctx
->API
!= API_OPENGL_COMPAT
)
4502 linker_error(prog
, "no shaders attached to the program\n");
4506 unsigned num_tfeedback_decls
= 0;
4507 unsigned int num_explicit_uniform_locs
= 0;
4508 bool has_xfb_qualifiers
= false;
4509 char **varying_names
= NULL
;
4510 tfeedback_decl
*tfeedback_decls
= NULL
;
4512 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
4514 prog
->ARB_fragment_coord_conventions_enable
= false;
4516 /* Separate the shaders into groups based on their type.
4518 struct gl_shader
**shader_list
[MESA_SHADER_STAGES
];
4519 unsigned num_shaders
[MESA_SHADER_STAGES
];
4521 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4522 shader_list
[i
] = (struct gl_shader
**)
4523 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
4527 unsigned min_version
= UINT_MAX
;
4528 unsigned max_version
= 0;
4529 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
4530 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
4531 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
4533 if (prog
->Shaders
[i
]->IsES
!= prog
->Shaders
[0]->IsES
) {
4534 linker_error(prog
, "all shaders must use same shading "
4535 "language version\n");
4539 if (prog
->Shaders
[i
]->info
.ARB_fragment_coord_conventions_enable
) {
4540 prog
->ARB_fragment_coord_conventions_enable
= true;
4543 gl_shader_stage shader_type
= prog
->Shaders
[i
]->Stage
;
4544 shader_list
[shader_type
][num_shaders
[shader_type
]] = prog
->Shaders
[i
];
4545 num_shaders
[shader_type
]++;
4548 /* In desktop GLSL, different shader versions may be linked together. In
4549 * GLSL ES, all shader versions must be the same.
4551 if (prog
->Shaders
[0]->IsES
&& min_version
!= max_version
) {
4552 linker_error(prog
, "all shaders must use same shading "
4553 "language version\n");
4557 prog
->Version
= max_version
;
4558 prog
->IsES
= prog
->Shaders
[0]->IsES
;
4560 /* Some shaders have to be linked with some other shaders present.
4562 if (!prog
->SeparateShader
) {
4563 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0 &&
4564 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4565 linker_error(prog
, "Geometry shader must be linked with "
4569 if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0 &&
4570 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4571 linker_error(prog
, "Tessellation evaluation shader must be linked "
4572 "with vertex shader\n");
4575 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4576 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4577 linker_error(prog
, "Tessellation control shader must be linked with "
4582 /* The spec is self-contradictory here. It allows linking without a tess
4583 * eval shader, but that can only be used with transform feedback and
4584 * rasterization disabled. However, transform feedback isn't allowed
4585 * with GL_PATCHES, so it can't be used.
4587 * More investigation showed that the idea of transform feedback after
4588 * a tess control shader was dropped, because some hw vendors couldn't
4589 * support tessellation without a tess eval shader, but the linker
4590 * section wasn't updated to reflect that.
4592 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
4595 * Do what's reasonable and always require a tess eval shader if a tess
4596 * control shader is present.
4598 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4599 num_shaders
[MESA_SHADER_TESS_EVAL
] == 0) {
4600 linker_error(prog
, "Tessellation control shader must be linked with "
4601 "tessellation evaluation shader\n");
4606 /* Compute shaders have additional restrictions. */
4607 if (num_shaders
[MESA_SHADER_COMPUTE
] > 0 &&
4608 num_shaders
[MESA_SHADER_COMPUTE
] != prog
->NumShaders
) {
4609 linker_error(prog
, "Compute shaders may not be linked with any other "
4610 "type of shader\n");
4613 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4614 if (prog
->_LinkedShaders
[i
] != NULL
) {
4615 _mesa_delete_linked_shader(ctx
, prog
->_LinkedShaders
[i
]);
4618 prog
->_LinkedShaders
[i
] = NULL
;
4621 /* Link all shaders for a particular stage and validate the result.
4623 for (int stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4624 if (num_shaders
[stage
] > 0) {
4625 gl_linked_shader
*const sh
=
4626 link_intrastage_shaders(mem_ctx
, ctx
, prog
, shader_list
[stage
],
4627 num_shaders
[stage
]);
4629 if (!prog
->LinkStatus
) {
4631 _mesa_delete_linked_shader(ctx
, sh
);
4636 case MESA_SHADER_VERTEX
:
4637 validate_vertex_shader_executable(prog
, sh
, ctx
);
4639 case MESA_SHADER_TESS_CTRL
:
4640 /* nothing to be done */
4642 case MESA_SHADER_TESS_EVAL
:
4643 validate_tess_eval_shader_executable(prog
, sh
, ctx
);
4645 case MESA_SHADER_GEOMETRY
:
4646 validate_geometry_shader_executable(prog
, sh
, ctx
);
4648 case MESA_SHADER_FRAGMENT
:
4649 validate_fragment_shader_executable(prog
, sh
);
4652 if (!prog
->LinkStatus
) {
4654 _mesa_delete_linked_shader(ctx
, sh
);
4658 prog
->_LinkedShaders
[stage
] = sh
;
4662 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0) {
4663 prog
->LastClipDistanceArraySize
= prog
->Geom
.ClipDistanceArraySize
;
4664 prog
->LastCullDistanceArraySize
= prog
->Geom
.CullDistanceArraySize
;
4665 } else if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0) {
4666 prog
->LastClipDistanceArraySize
= prog
->TessEval
.ClipDistanceArraySize
;
4667 prog
->LastCullDistanceArraySize
= prog
->TessEval
.CullDistanceArraySize
;
4668 } else if (num_shaders
[MESA_SHADER_VERTEX
] > 0) {
4669 prog
->LastClipDistanceArraySize
= prog
->Vert
.ClipDistanceArraySize
;
4670 prog
->LastCullDistanceArraySize
= prog
->Vert
.CullDistanceArraySize
;
4672 prog
->LastClipDistanceArraySize
= 0; /* Not used */
4673 prog
->LastCullDistanceArraySize
= 0; /* Not used */
4676 /* Here begins the inter-stage linking phase. Some initial validation is
4677 * performed, then locations are assigned for uniforms, attributes, and
4680 cross_validate_uniforms(prog
);
4681 if (!prog
->LinkStatus
)
4684 unsigned first
, last
, prev
;
4686 first
= MESA_SHADER_STAGES
;
4689 /* Determine first and last stage. */
4690 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4691 if (!prog
->_LinkedShaders
[i
])
4693 if (first
== MESA_SHADER_STAGES
)
4698 num_explicit_uniform_locs
= check_explicit_uniform_locations(ctx
, prog
);
4699 link_assign_subroutine_types(prog
);
4701 if (!prog
->LinkStatus
)
4704 resize_tes_inputs(ctx
, prog
);
4706 /* Validate the inputs of each stage with the output of the preceding
4710 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4711 if (prog
->_LinkedShaders
[i
] == NULL
)
4714 validate_interstage_inout_blocks(prog
, prog
->_LinkedShaders
[prev
],
4715 prog
->_LinkedShaders
[i
]);
4716 if (!prog
->LinkStatus
)
4719 cross_validate_outputs_to_inputs(prog
,
4720 prog
->_LinkedShaders
[prev
],
4721 prog
->_LinkedShaders
[i
]);
4722 if (!prog
->LinkStatus
)
4728 /* Cross-validate uniform blocks between shader stages */
4729 validate_interstage_uniform_blocks(prog
, prog
->_LinkedShaders
);
4730 if (!prog
->LinkStatus
)
4733 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4734 if (prog
->_LinkedShaders
[i
] != NULL
)
4735 lower_named_interface_blocks(mem_ctx
, prog
->_LinkedShaders
[i
]);
4738 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
4739 * it before optimization because we want most of the checks to get
4740 * dropped thanks to constant propagation.
4742 * This rule also applies to GLSL ES 3.00.
4744 if (max_version
>= (prog
->IsES
? 300 : 130)) {
4745 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4747 lower_discard_flow(sh
->ir
);
4751 if (prog
->SeparateShader
)
4752 disable_varying_optimizations_for_sso(prog
);
4755 if (!interstage_cross_validate_uniform_blocks(prog
, false))
4759 if (!interstage_cross_validate_uniform_blocks(prog
, true))
4762 /* Do common optimization before assigning storage for attributes,
4763 * uniforms, and varyings. Later optimization could possibly make
4764 * some of that unused.
4766 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4767 if (prog
->_LinkedShaders
[i
] == NULL
)
4770 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
4771 if (!prog
->LinkStatus
)
4774 if (ctx
->Const
.ShaderCompilerOptions
[i
].LowerCombinedClipCullDistance
) {
4775 lower_clip_cull_distance(prog
, prog
->_LinkedShaders
[i
]);
4778 if (ctx
->Const
.LowerTessLevel
) {
4779 lower_tess_level(prog
->_LinkedShaders
[i
]);
4782 while (do_common_optimization(prog
->_LinkedShaders
[i
]->ir
, true, false,
4783 &ctx
->Const
.ShaderCompilerOptions
[i
],
4784 ctx
->Const
.NativeIntegers
))
4787 lower_const_arrays_to_uniforms(prog
->_LinkedShaders
[i
]->ir
);
4788 propagate_invariance(prog
->_LinkedShaders
[i
]->ir
);
4791 /* Validation for special cases where we allow sampler array indexing
4792 * with loop induction variable. This check emits a warning or error
4793 * depending if backend can handle dynamic indexing.
4795 if ((!prog
->IsES
&& prog
->Version
< 130) ||
4796 (prog
->IsES
&& prog
->Version
< 300)) {
4797 if (!validate_sampler_array_indexing(ctx
, prog
))
4801 /* Check and validate stream emissions in geometry shaders */
4802 validate_geometry_shader_emissions(ctx
, prog
);
4804 /* Mark all generic shader inputs and outputs as unpaired. */
4805 for (unsigned i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4806 if (prog
->_LinkedShaders
[i
] != NULL
) {
4807 link_invalidate_variable_locations(prog
->_LinkedShaders
[i
]->ir
);
4812 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4813 if (prog
->_LinkedShaders
[i
] == NULL
)
4816 match_explicit_outputs_to_inputs(prog
->_LinkedShaders
[prev
],
4817 prog
->_LinkedShaders
[i
]);
4821 if (!assign_attribute_or_color_locations(prog
, &ctx
->Const
,
4822 MESA_SHADER_VERTEX
)) {
4826 if (!assign_attribute_or_color_locations(prog
, &ctx
->Const
,
4827 MESA_SHADER_FRAGMENT
)) {
4831 /* From the ARB_enhanced_layouts spec:
4833 * "If the shader used to record output variables for transform feedback
4834 * varyings uses the "xfb_buffer", "xfb_offset", or "xfb_stride" layout
4835 * qualifiers, the values specified by TransformFeedbackVaryings are
4836 * ignored, and the set of variables captured for transform feedback is
4837 * instead derived from the specified layout qualifiers."
4839 for (int i
= MESA_SHADER_FRAGMENT
- 1; i
>= 0; i
--) {
4840 /* Find last stage before fragment shader */
4841 if (prog
->_LinkedShaders
[i
]) {
4842 has_xfb_qualifiers
=
4843 process_xfb_layout_qualifiers(mem_ctx
, prog
->_LinkedShaders
[i
],
4844 &num_tfeedback_decls
,
4850 if (!has_xfb_qualifiers
) {
4851 num_tfeedback_decls
= prog
->TransformFeedback
.NumVarying
;
4852 varying_names
= prog
->TransformFeedback
.VaryingNames
;
4855 if (num_tfeedback_decls
!= 0) {
4856 /* From GL_EXT_transform_feedback:
4857 * A program will fail to link if:
4859 * * the <count> specified by TransformFeedbackVaryingsEXT is
4860 * non-zero, but the program object has no vertex or geometry
4863 if (first
>= MESA_SHADER_FRAGMENT
) {
4864 linker_error(prog
, "Transform feedback varyings specified, but "
4865 "no vertex, tessellation, or geometry shader is "
4870 tfeedback_decls
= ralloc_array(mem_ctx
, tfeedback_decl
,
4871 num_tfeedback_decls
);
4872 if (!parse_tfeedback_decls(ctx
, prog
, mem_ctx
, num_tfeedback_decls
,
4873 varying_names
, tfeedback_decls
))
4877 /* If there is no fragment shader we need to set transform feedback.
4879 * For SSO we also need to assign output locations. We assign them here
4880 * because we need to do it for both single stage programs and multi stage
4883 if (last
< MESA_SHADER_FRAGMENT
&&
4884 (num_tfeedback_decls
!= 0 || prog
->SeparateShader
)) {
4885 const uint64_t reserved_out_slots
=
4886 reserved_varying_slot(prog
->_LinkedShaders
[last
], ir_var_shader_out
);
4887 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4888 prog
->_LinkedShaders
[last
], NULL
,
4889 num_tfeedback_decls
, tfeedback_decls
,
4890 reserved_out_slots
))
4894 if (last
<= MESA_SHADER_FRAGMENT
) {
4895 /* Remove unused varyings from the first/last stage unless SSO */
4896 remove_unused_shader_inputs_and_outputs(prog
->SeparateShader
,
4897 prog
->_LinkedShaders
[first
],
4899 remove_unused_shader_inputs_and_outputs(prog
->SeparateShader
,
4900 prog
->_LinkedShaders
[last
],
4903 /* If the program is made up of only a single stage */
4904 if (first
== last
) {
4906 gl_linked_shader
*const sh
= prog
->_LinkedShaders
[last
];
4907 if (prog
->SeparateShader
) {
4908 const uint64_t reserved_slots
=
4909 reserved_varying_slot(sh
, ir_var_shader_in
);
4911 /* Assign input locations for SSO, output locations are already
4914 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4915 NULL
/* producer */,
4917 0 /* num_tfeedback_decls */,
4918 NULL
/* tfeedback_decls */,
4923 do_dead_builtin_varyings(ctx
, NULL
, sh
, 0, NULL
);
4924 do_dead_builtin_varyings(ctx
, sh
, NULL
, num_tfeedback_decls
,
4927 /* Linking the stages in the opposite order (from fragment to vertex)
4928 * ensures that inter-shader outputs written to in an earlier stage
4929 * are eliminated if they are (transitively) not used in a later
4933 for (int i
= next
- 1; i
>= 0; i
--) {
4934 if (prog
->_LinkedShaders
[i
] == NULL
&& i
!= 0)
4937 gl_linked_shader
*const sh_i
= prog
->_LinkedShaders
[i
];
4938 gl_linked_shader
*const sh_next
= prog
->_LinkedShaders
[next
];
4940 const uint64_t reserved_out_slots
=
4941 reserved_varying_slot(sh_i
, ir_var_shader_out
);
4942 const uint64_t reserved_in_slots
=
4943 reserved_varying_slot(sh_next
, ir_var_shader_in
);
4945 if (!assign_varying_locations(ctx
, mem_ctx
, prog
, sh_i
, sh_next
,
4946 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
4948 reserved_out_slots
| reserved_in_slots
))
4951 do_dead_builtin_varyings(ctx
, sh_i
, sh_next
,
4952 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
4955 /* This must be done after all dead varyings are eliminated. */
4957 unsigned slots_used
= _mesa_bitcount_64(reserved_out_slots
);
4958 if (!check_against_output_limit(ctx
, prog
, sh_i
, slots_used
)) {
4963 unsigned slots_used
= _mesa_bitcount_64(reserved_in_slots
);
4964 if (!check_against_input_limit(ctx
, prog
, sh_next
, slots_used
))
4972 if (!store_tfeedback_info(ctx
, prog
, num_tfeedback_decls
, tfeedback_decls
,
4973 has_xfb_qualifiers
))
4976 update_array_sizes(prog
);
4977 link_assign_uniform_locations(prog
, ctx
->Const
.UniformBooleanTrue
,
4978 num_explicit_uniform_locs
,
4979 ctx
->Const
.MaxUserAssignableUniformLocations
);
4980 link_assign_atomic_counter_resources(ctx
, prog
);
4981 store_fragdepth_layout(prog
);
4983 link_calculate_subroutine_compat(prog
);
4984 check_resources(ctx
, prog
);
4985 check_subroutine_resources(prog
);
4986 check_image_resources(ctx
, prog
);
4987 link_check_atomic_counter_resources(ctx
, prog
);
4989 if (!prog
->LinkStatus
)
4992 /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both
4993 * be present in a linked program. GL_ARB_ES2_compatibility doesn't say
4994 * anything about shader linking when one of the shaders (vertex or
4995 * fragment shader) is absent. So, the extension shouldn't change the
4996 * behavior specified in GLSL specification.
4998 * From OpenGL ES 3.1 specification (7.3 Program Objects):
4999 * "Linking can fail for a variety of reasons as specified in the
5000 * OpenGL ES Shading Language Specification, as well as any of the
5001 * following reasons:
5005 * * program contains objects to form either a vertex shader or
5006 * fragment shader, and program is not separable, and does not
5007 * contain objects to form both a vertex shader and fragment
5010 * However, the only scenario in 3.1+ where we don't require them both is
5011 * when we have a compute shader. For example:
5013 * - No shaders is a link error.
5014 * - Geom or Tess without a Vertex shader is a link error which means we
5015 * always require a Vertex shader and hence a Fragment shader.
5016 * - Finally a Compute shader linked with any other stage is a link error.
5018 if (!prog
->SeparateShader
&& ctx
->API
== API_OPENGLES2
&&
5019 num_shaders
[MESA_SHADER_COMPUTE
] == 0) {
5020 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
5021 linker_error(prog
, "program lacks a vertex shader\n");
5022 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
5023 linker_error(prog
, "program lacks a fragment shader\n");
5027 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
5028 if (prog
->_LinkedShaders
[i
] == NULL
)
5031 const struct gl_shader_compiler_options
*options
=
5032 &ctx
->Const
.ShaderCompilerOptions
[i
];
5034 if (options
->LowerBufferInterfaceBlocks
)
5035 lower_ubo_reference(prog
->_LinkedShaders
[i
],
5036 options
->ClampBlockIndicesToArrayBounds
);
5038 if (options
->LowerShaderSharedVariables
)
5039 lower_shared_reference(prog
->_LinkedShaders
[i
],
5040 &prog
->Comp
.SharedSize
);
5042 lower_vector_derefs(prog
->_LinkedShaders
[i
]);
5043 do_vec_index_to_swizzle(prog
->_LinkedShaders
[i
]->ir
);
5047 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
5048 free(shader_list
[i
]);
5049 if (prog
->_LinkedShaders
[i
] == NULL
)
5052 /* Do a final validation step to make sure that the IR wasn't
5053 * invalidated by any modifications performed after intrastage linking.
5055 validate_ir_tree(prog
->_LinkedShaders
[i
]->ir
);
5057 /* Retain any live IR, but trash the rest. */
5058 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
5060 /* The symbol table in the linked shaders may contain references to
5061 * variables that were removed (e.g., unused uniforms). Since it may
5062 * contain junk, there is no possible valid use. Delete it and set the
5065 delete prog
->_LinkedShaders
[i
]->symbols
;
5066 prog
->_LinkedShaders
[i
]->symbols
= NULL
;
5069 ralloc_free(mem_ctx
);