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 "glsl_symbol_table.h"
70 #include "glsl_parser_extras.h"
73 #include "program/prog_instruction.h"
74 #include "program/program.h"
75 #include "util/mesa-sha1.h"
77 #include "string_to_uint_map.h"
79 #include "linker_util.h"
80 #include "link_varyings.h"
81 #include "ir_optimization.h"
82 #include "ir_rvalue_visitor.h"
83 #include "ir_uniform.h"
84 #include "builtin_functions.h"
85 #include "shader_cache.h"
86 #include "util/u_string.h"
87 #include "util/u_math.h"
90 #include "main/shaderobj.h"
91 #include "main/enums.h"
92 #include "main/mtypes.h"
97 struct find_variable
{
101 find_variable(const char *name
) : name(name
), found(false) {}
105 * Visitor that determines whether or not a variable is ever written.
107 * Use \ref find_assignments for convenience.
109 class find_assignment_visitor
: public ir_hierarchical_visitor
{
111 find_assignment_visitor(unsigned num_vars
,
112 find_variable
* const *vars
)
113 : num_variables(num_vars
), num_found(0), variables(vars
)
117 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
)
119 ir_variable
*const var
= ir
->lhs
->variable_referenced();
121 return check_variable_name(var
->name
);
124 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
126 foreach_two_lists(formal_node
, &ir
->callee
->parameters
,
127 actual_node
, &ir
->actual_parameters
) {
128 ir_rvalue
*param_rval
= (ir_rvalue
*) actual_node
;
129 ir_variable
*sig_param
= (ir_variable
*) formal_node
;
131 if (sig_param
->data
.mode
== ir_var_function_out
||
132 sig_param
->data
.mode
== ir_var_function_inout
) {
133 ir_variable
*var
= param_rval
->variable_referenced();
134 if (var
&& check_variable_name(var
->name
) == visit_stop
)
139 if (ir
->return_deref
!= NULL
) {
140 ir_variable
*const var
= ir
->return_deref
->variable_referenced();
142 if (check_variable_name(var
->name
) == visit_stop
)
146 return visit_continue_with_parent
;
150 ir_visitor_status
check_variable_name(const char *name
)
152 for (unsigned i
= 0; i
< num_variables
; ++i
) {
153 if (strcmp(variables
[i
]->name
, name
) == 0) {
154 if (!variables
[i
]->found
) {
155 variables
[i
]->found
= true;
157 assert(num_found
< num_variables
);
158 if (++num_found
== num_variables
)
165 return visit_continue_with_parent
;
169 unsigned num_variables
; /**< Number of variables to find */
170 unsigned num_found
; /**< Number of variables already found */
171 find_variable
* const *variables
; /**< Variables to find */
175 * Determine whether or not any of NULL-terminated list of variables is ever
179 find_assignments(exec_list
*ir
, find_variable
* const *vars
)
181 unsigned num_variables
= 0;
183 for (find_variable
* const *v
= vars
; *v
; ++v
)
186 find_assignment_visitor
visitor(num_variables
, vars
);
191 * Determine whether or not the given variable is ever written to.
194 find_assignments(exec_list
*ir
, find_variable
*var
)
196 find_assignment_visitor
visitor(1, &var
);
201 * Visitor that determines whether or not a variable is ever read.
203 class find_deref_visitor
: public ir_hierarchical_visitor
{
205 find_deref_visitor(const char *name
)
206 : name(name
), found(false)
211 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
213 if (strcmp(this->name
, ir
->var
->name
) == 0) {
218 return visit_continue
;
221 bool variable_found() const
227 const char *name
; /**< Find writes to a variable with this name. */
228 bool found
; /**< Was a write to the variable found? */
233 * A visitor helper that provides methods for updating the types of
234 * ir_dereferences. Classes that update variable types (say, updating
235 * array sizes) will want to use this so that dereference types stay in sync.
237 class deref_type_updater
: public ir_hierarchical_visitor
{
239 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
241 ir
->type
= ir
->var
->type
;
242 return visit_continue
;
245 virtual ir_visitor_status
visit_leave(ir_dereference_array
*ir
)
247 const glsl_type
*const vt
= ir
->array
->type
;
249 ir
->type
= vt
->fields
.array
;
250 return visit_continue
;
253 virtual ir_visitor_status
visit_leave(ir_dereference_record
*ir
)
255 ir
->type
= ir
->record
->type
->fields
.structure
[ir
->field_idx
].type
;
256 return visit_continue
;
261 class array_resize_visitor
: public deref_type_updater
{
263 using deref_type_updater::visit
;
265 unsigned num_vertices
;
266 gl_shader_program
*prog
;
267 gl_shader_stage stage
;
269 array_resize_visitor(unsigned num_vertices
,
270 gl_shader_program
*prog
,
271 gl_shader_stage stage
)
273 this->num_vertices
= num_vertices
;
278 virtual ~array_resize_visitor()
283 virtual ir_visitor_status
visit(ir_variable
*var
)
285 if (!var
->type
->is_array() || var
->data
.mode
!= ir_var_shader_in
||
287 return visit_continue
;
289 unsigned size
= var
->type
->length
;
291 if (stage
== MESA_SHADER_GEOMETRY
) {
292 /* Generate a link error if the shader has declared this array with
295 if (!var
->data
.implicit_sized_array
&&
296 size
&& size
!= this->num_vertices
) {
297 linker_error(this->prog
, "size of array %s declared as %u, "
298 "but number of input vertices is %u\n",
299 var
->name
, size
, this->num_vertices
);
300 return visit_continue
;
303 /* Generate a link error if the shader attempts to access an input
304 * array using an index too large for its actual size assigned at
307 if (var
->data
.max_array_access
>= (int)this->num_vertices
) {
308 linker_error(this->prog
, "%s shader accesses element %i of "
309 "%s, but only %i input vertices\n",
310 _mesa_shader_stage_to_string(this->stage
),
311 var
->data
.max_array_access
, var
->name
, this->num_vertices
);
312 return visit_continue
;
316 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
318 var
->data
.max_array_access
= this->num_vertices
- 1;
320 return visit_continue
;
325 * Visitor that determines the highest stream id to which a (geometry) shader
326 * emits vertices. It also checks whether End{Stream}Primitive is ever called.
328 class find_emit_vertex_visitor
: public ir_hierarchical_visitor
{
330 find_emit_vertex_visitor(int max_allowed
)
331 : max_stream_allowed(max_allowed
),
332 invalid_stream_id(0),
333 invalid_stream_id_from_emit_vertex(false),
334 end_primitive_found(false),
340 virtual ir_visitor_status
visit_leave(ir_emit_vertex
*ir
)
342 int stream_id
= ir
->stream_id();
345 invalid_stream_id
= stream_id
;
346 invalid_stream_id_from_emit_vertex
= true;
350 if (stream_id
> max_stream_allowed
) {
351 invalid_stream_id
= stream_id
;
352 invalid_stream_id_from_emit_vertex
= true;
356 used_streams
|= 1 << stream_id
;
358 return visit_continue
;
361 virtual ir_visitor_status
visit_leave(ir_end_primitive
*ir
)
363 end_primitive_found
= true;
365 int stream_id
= ir
->stream_id();
368 invalid_stream_id
= stream_id
;
369 invalid_stream_id_from_emit_vertex
= false;
373 if (stream_id
> max_stream_allowed
) {
374 invalid_stream_id
= stream_id
;
375 invalid_stream_id_from_emit_vertex
= false;
379 used_streams
|= 1 << stream_id
;
381 return visit_continue
;
386 return invalid_stream_id
!= 0;
389 const char *error_func()
391 return invalid_stream_id_from_emit_vertex
?
392 "EmitStreamVertex" : "EndStreamPrimitive";
397 return invalid_stream_id
;
400 unsigned active_stream_mask()
405 bool uses_end_primitive()
407 return end_primitive_found
;
411 int max_stream_allowed
;
412 int invalid_stream_id
;
413 bool invalid_stream_id_from_emit_vertex
;
414 bool end_primitive_found
;
415 unsigned used_streams
;
418 /* Class that finds array derefs and check if indexes are dynamic. */
419 class dynamic_sampler_array_indexing_visitor
: public ir_hierarchical_visitor
422 dynamic_sampler_array_indexing_visitor() :
423 dynamic_sampler_array_indexing(false)
427 ir_visitor_status
visit_enter(ir_dereference_array
*ir
)
429 if (!ir
->variable_referenced())
430 return visit_continue
;
432 if (!ir
->variable_referenced()->type
->contains_sampler())
433 return visit_continue
;
435 if (!ir
->array_index
->constant_expression_value(ralloc_parent(ir
))) {
436 dynamic_sampler_array_indexing
= true;
439 return visit_continue
;
442 bool uses_dynamic_sampler_array_indexing()
444 return dynamic_sampler_array_indexing
;
448 bool dynamic_sampler_array_indexing
;
451 } /* anonymous namespace */
454 linker_error(gl_shader_program
*prog
, const char *fmt
, ...)
458 ralloc_strcat(&prog
->data
->InfoLog
, "error: ");
460 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, ap
);
463 prog
->data
->LinkStatus
= LINKING_FAILURE
;
468 linker_warning(gl_shader_program
*prog
, const char *fmt
, ...)
472 ralloc_strcat(&prog
->data
->InfoLog
, "warning: ");
474 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, ap
);
481 * Given a string identifying a program resource, break it into a base name
482 * and an optional array index in square brackets.
484 * If an array index is present, \c out_base_name_end is set to point to the
485 * "[" that precedes the array index, and the array index itself is returned
488 * If no array index is present (or if the array index is negative or
489 * mal-formed), \c out_base_name_end, is set to point to the null terminator
490 * at the end of the input string, and -1 is returned.
492 * Only the final array index is parsed; if the string contains other array
493 * indices (or structure field accesses), they are left in the base name.
495 * No attempt is made to check that the base name is properly formed;
496 * typically the caller will look up the base name in a hash table, so
497 * ill-formed base names simply turn into hash table lookup failures.
500 parse_program_resource_name(const GLchar
*name
,
501 const GLchar
**out_base_name_end
)
503 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
505 * "When an integer array element or block instance number is part of
506 * the name string, it will be specified in decimal form without a "+"
507 * or "-" sign or any extra leading zeroes. Additionally, the name
508 * string will not include white space anywhere in the string."
511 const size_t len
= strlen(name
);
512 *out_base_name_end
= name
+ len
;
514 if (len
== 0 || name
[len
-1] != ']')
517 /* Walk backwards over the string looking for a non-digit character. This
518 * had better be the opening bracket for an array index.
520 * Initially, i specifies the location of the ']'. Since the string may
521 * contain only the ']' charcater, walk backwards very carefully.
524 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
527 if ((i
== 0) || name
[i
-1] != '[')
530 long array_index
= strtol(&name
[i
], NULL
, 10);
534 /* Check for leading zero */
535 if (name
[i
] == '0' && name
[i
+1] != ']')
538 *out_base_name_end
= name
+ (i
- 1);
544 link_invalidate_variable_locations(exec_list
*ir
)
546 foreach_in_list(ir_instruction
, node
, ir
) {
547 ir_variable
*const var
= node
->as_variable();
552 /* Only assign locations for variables that lack an explicit location.
553 * Explicit locations are set for all built-in variables, generic vertex
554 * shader inputs (via layout(location=...)), and generic fragment shader
555 * outputs (also via layout(location=...)).
557 if (!var
->data
.explicit_location
) {
558 var
->data
.location
= -1;
559 var
->data
.location_frac
= 0;
562 /* ir_variable::is_unmatched_generic_inout is used by the linker while
563 * connecting outputs from one stage to inputs of the next stage.
565 if (var
->data
.explicit_location
&&
566 var
->data
.location
< VARYING_SLOT_VAR0
) {
567 var
->data
.is_unmatched_generic_inout
= 0;
569 var
->data
.is_unmatched_generic_inout
= 1;
576 * Set clip_distance_array_size based and cull_distance_array_size on the given
579 * Also check for errors based on incorrect usage of gl_ClipVertex and
580 * gl_ClipDistance and gl_CullDistance.
581 * Additionally test whether the arrays gl_ClipDistance and gl_CullDistance
582 * exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances.
584 * Return false if an error was reported.
587 analyze_clip_cull_usage(struct gl_shader_program
*prog
,
588 struct gl_linked_shader
*shader
,
589 struct gl_context
*ctx
,
590 struct shader_info
*info
)
592 info
->clip_distance_array_size
= 0;
593 info
->cull_distance_array_size
= 0;
595 if (prog
->data
->Version
>= (prog
->IsES
? 300 : 130)) {
596 /* From section 7.1 (Vertex Shader Special Variables) of the
599 * "It is an error for a shader to statically write both
600 * gl_ClipVertex and gl_ClipDistance."
602 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
603 * gl_ClipVertex nor gl_ClipDistance. However with
604 * GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0.
606 find_variable
gl_ClipDistance("gl_ClipDistance");
607 find_variable
gl_CullDistance("gl_CullDistance");
608 find_variable
gl_ClipVertex("gl_ClipVertex");
609 find_variable
* const variables
[] = {
612 !prog
->IsES
? &gl_ClipVertex
: NULL
,
615 find_assignments(shader
->ir
, variables
);
617 /* From the ARB_cull_distance spec:
619 * It is a compile-time or link-time error for the set of shaders forming
620 * a program to statically read or write both gl_ClipVertex and either
621 * gl_ClipDistance or gl_CullDistance.
623 * This does not apply to GLSL ES shaders, since GLSL ES doesn't define
627 if (gl_ClipVertex
.found
&& gl_ClipDistance
.found
) {
628 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
629 "and `gl_ClipDistance'\n",
630 _mesa_shader_stage_to_string(shader
->Stage
));
633 if (gl_ClipVertex
.found
&& gl_CullDistance
.found
) {
634 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
635 "and `gl_CullDistance'\n",
636 _mesa_shader_stage_to_string(shader
->Stage
));
641 if (gl_ClipDistance
.found
) {
642 ir_variable
*clip_distance_var
=
643 shader
->symbols
->get_variable("gl_ClipDistance");
644 assert(clip_distance_var
);
645 info
->clip_distance_array_size
= clip_distance_var
->type
->length
;
647 if (gl_CullDistance
.found
) {
648 ir_variable
*cull_distance_var
=
649 shader
->symbols
->get_variable("gl_CullDistance");
650 assert(cull_distance_var
);
651 info
->cull_distance_array_size
= cull_distance_var
->type
->length
;
653 /* From the ARB_cull_distance spec:
655 * It is a compile-time or link-time error for the set of shaders forming
656 * a program to have the sum of the sizes of the gl_ClipDistance and
657 * gl_CullDistance arrays to be larger than
658 * gl_MaxCombinedClipAndCullDistances.
660 if ((uint32_t)(info
->clip_distance_array_size
+ info
->cull_distance_array_size
) >
661 ctx
->Const
.MaxClipPlanes
) {
662 linker_error(prog
, "%s shader: the combined size of "
663 "'gl_ClipDistance' and 'gl_CullDistance' size cannot "
665 "gl_MaxCombinedClipAndCullDistances (%u)",
666 _mesa_shader_stage_to_string(shader
->Stage
),
667 ctx
->Const
.MaxClipPlanes
);
674 * Verify that a vertex shader executable meets all semantic requirements.
676 * Also sets info.clip_distance_array_size and
677 * info.cull_distance_array_size as a side effect.
679 * \param shader Vertex shader executable to be verified
682 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
683 struct gl_linked_shader
*shader
,
684 struct gl_context
*ctx
)
689 /* From the GLSL 1.10 spec, page 48:
691 * "The variable gl_Position is available only in the vertex
692 * language and is intended for writing the homogeneous vertex
693 * position. All executions of a well-formed vertex shader
694 * executable must write a value into this variable. [...] The
695 * variable gl_Position is available only in the vertex
696 * language and is intended for writing the homogeneous vertex
697 * position. All executions of a well-formed vertex shader
698 * executable must write a value into this variable."
700 * while in GLSL 1.40 this text is changed to:
702 * "The variable gl_Position is available only in the vertex
703 * language and is intended for writing the homogeneous vertex
704 * position. It can be written at any time during shader
705 * execution. It may also be read back by a vertex shader
706 * after being written. This value will be used by primitive
707 * assembly, clipping, culling, and other fixed functionality
708 * operations, if present, that operate on primitives after
709 * vertex processing has occurred. Its value is undefined if
710 * the vertex shader executable does not write gl_Position."
712 * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
713 * gl_Position is not an error.
715 if (prog
->data
->Version
< (prog
->IsES
? 300 : 140)) {
716 find_variable
gl_Position("gl_Position");
717 find_assignments(shader
->ir
, &gl_Position
);
718 if (!gl_Position
.found
) {
721 "vertex shader does not write to `gl_Position'. "
722 "Its value is undefined. \n");
725 "vertex shader does not write to `gl_Position'. \n");
731 analyze_clip_cull_usage(prog
, shader
, ctx
, &shader
->Program
->info
);
735 validate_tess_eval_shader_executable(struct gl_shader_program
*prog
,
736 struct gl_linked_shader
*shader
,
737 struct gl_context
*ctx
)
742 analyze_clip_cull_usage(prog
, shader
, ctx
, &shader
->Program
->info
);
747 * Verify that a fragment shader executable meets all semantic requirements
749 * \param shader Fragment shader executable to be verified
752 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
753 struct gl_linked_shader
*shader
)
758 find_variable
gl_FragColor("gl_FragColor");
759 find_variable
gl_FragData("gl_FragData");
760 find_variable
* const variables
[] = { &gl_FragColor
, &gl_FragData
, NULL
};
761 find_assignments(shader
->ir
, variables
);
763 if (gl_FragColor
.found
&& gl_FragData
.found
) {
764 linker_error(prog
, "fragment shader writes to both "
765 "`gl_FragColor' and `gl_FragData'\n");
770 * Verify that a geometry shader executable meets all semantic requirements
772 * Also sets prog->Geom.VerticesIn, and info.clip_distance_array_sizeand
773 * info.cull_distance_array_size as a side effect.
775 * \param shader Geometry shader executable to be verified
778 validate_geometry_shader_executable(struct gl_shader_program
*prog
,
779 struct gl_linked_shader
*shader
,
780 struct gl_context
*ctx
)
785 unsigned num_vertices
=
786 vertices_per_prim(shader
->Program
->info
.gs
.input_primitive
);
787 prog
->Geom
.VerticesIn
= num_vertices
;
789 analyze_clip_cull_usage(prog
, shader
, ctx
, &shader
->Program
->info
);
793 * Check if geometry shaders emit to non-zero streams and do corresponding
797 validate_geometry_shader_emissions(struct gl_context
*ctx
,
798 struct gl_shader_program
*prog
)
800 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
803 find_emit_vertex_visitor
emit_vertex(ctx
->Const
.MaxVertexStreams
- 1);
804 emit_vertex
.run(sh
->ir
);
805 if (emit_vertex
.error()) {
806 linker_error(prog
, "Invalid call %s(%d). Accepted values for the "
807 "stream parameter are in the range [0, %d].\n",
808 emit_vertex
.error_func(),
809 emit_vertex
.error_stream(),
810 ctx
->Const
.MaxVertexStreams
- 1);
812 prog
->Geom
.ActiveStreamMask
= emit_vertex
.active_stream_mask();
813 prog
->Geom
.UsesEndPrimitive
= emit_vertex
.uses_end_primitive();
815 /* From the ARB_gpu_shader5 spec:
817 * "Multiple vertex streams are supported only if the output primitive
818 * type is declared to be "points". A program will fail to link if it
819 * contains a geometry shader calling EmitStreamVertex() or
820 * EndStreamPrimitive() if its output primitive type is not "points".
822 * However, in the same spec:
824 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
825 * with <stream> set to zero."
829 * "The function EndPrimitive() is equivalent to calling
830 * EndStreamPrimitive() with <stream> set to zero."
832 * Since we can call EmitVertex() and EndPrimitive() when we output
833 * primitives other than points, calling EmitStreamVertex(0) or
834 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
835 * does. We can use prog->Geom.ActiveStreamMask to check whether only the
836 * first (zero) stream is active.
839 if (prog
->Geom
.ActiveStreamMask
& ~(1 << 0) &&
840 sh
->Program
->info
.gs
.output_primitive
!= GL_POINTS
) {
841 linker_error(prog
, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
842 "with n>0 requires point output\n");
848 validate_intrastage_arrays(struct gl_shader_program
*prog
,
849 ir_variable
*const var
,
850 ir_variable
*const existing
,
851 bool match_precision
)
853 /* Consider the types to be "the same" if both types are arrays
854 * of the same type and one of the arrays is implicitly sized.
855 * In addition, set the type of the linked variable to the
856 * explicitly sized array.
858 if (var
->type
->is_array() && existing
->type
->is_array()) {
859 const glsl_type
*no_array_var
= var
->type
->fields
.array
;
860 const glsl_type
*no_array_existing
= existing
->type
->fields
.array
;
863 type_matches
= (match_precision
?
864 no_array_var
== no_array_existing
:
865 no_array_var
->compare_no_precision(no_array_existing
));
868 ((var
->type
->length
== 0)|| (existing
->type
->length
== 0))) {
869 if (var
->type
->length
!= 0) {
870 if ((int)var
->type
->length
<= existing
->data
.max_array_access
) {
871 linker_error(prog
, "%s `%s' declared as type "
872 "`%s' but outermost dimension has an index"
875 var
->name
, var
->type
->name
,
876 existing
->data
.max_array_access
);
878 existing
->type
= var
->type
;
880 } else if (existing
->type
->length
!= 0) {
881 if((int)existing
->type
->length
<= var
->data
.max_array_access
&&
882 !existing
->data
.from_ssbo_unsized_array
) {
883 linker_error(prog
, "%s `%s' declared as type "
884 "`%s' but outermost dimension has an index"
887 var
->name
, existing
->type
->name
,
888 var
->data
.max_array_access
);
899 * Perform validation of global variables used across multiple shaders
902 cross_validate_globals(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
903 struct exec_list
*ir
, glsl_symbol_table
*variables
,
906 foreach_in_list(ir_instruction
, node
, ir
) {
907 ir_variable
*const var
= node
->as_variable();
912 if (uniforms_only
&& (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
))
915 /* don't cross validate subroutine uniforms */
916 if (var
->type
->contains_subroutine())
919 /* Don't cross validate interface instances. These are only relevant
920 * inside a shader. The cross validation is done at the Interface Block
923 if (var
->is_interface_instance())
926 /* Don't cross validate temporaries that are at global scope. These
927 * will eventually get pulled into the shaders 'main'.
929 if (var
->data
.mode
== ir_var_temporary
)
932 /* If a global with this name has already been seen, verify that the
933 * new instance has the same type. In addition, if the globals have
934 * initializers, the values of the initializers must be the same.
936 ir_variable
*const existing
= variables
->get_variable(var
->name
);
937 if (existing
!= NULL
) {
938 /* Check if types match. */
939 if (var
->type
!= existing
->type
) {
940 if (!validate_intrastage_arrays(prog
, var
, existing
)) {
941 /* If it is an unsized array in a Shader Storage Block,
942 * two different shaders can access to different elements.
943 * Because of that, they might be converted to different
944 * sized arrays, then check that they are compatible but
945 * ignore the array size.
947 if (!(var
->data
.mode
== ir_var_shader_storage
&&
948 var
->data
.from_ssbo_unsized_array
&&
949 existing
->data
.mode
== ir_var_shader_storage
&&
950 existing
->data
.from_ssbo_unsized_array
&&
951 var
->type
->gl_type
== existing
->type
->gl_type
)) {
952 linker_error(prog
, "%s `%s' declared as type "
953 "`%s' and type `%s'\n",
955 var
->name
, var
->type
->name
,
956 existing
->type
->name
);
962 if (var
->data
.explicit_location
) {
963 if (existing
->data
.explicit_location
964 && (var
->data
.location
!= existing
->data
.location
)) {
965 linker_error(prog
, "explicit locations for %s "
966 "`%s' have differing values\n",
967 mode_string(var
), var
->name
);
971 if (var
->data
.location_frac
!= existing
->data
.location_frac
) {
972 linker_error(prog
, "explicit components for %s `%s' have "
973 "differing values\n", mode_string(var
), var
->name
);
977 existing
->data
.location
= var
->data
.location
;
978 existing
->data
.explicit_location
= true;
980 /* Check if uniform with implicit location was marked explicit
981 * by earlier shader stage. If so, mark it explicit in this stage
982 * too to make sure later processing does not treat it as
985 if (existing
->data
.explicit_location
) {
986 var
->data
.location
= existing
->data
.location
;
987 var
->data
.explicit_location
= true;
991 /* From the GLSL 4.20 specification:
992 * "A link error will result if two compilation units in a program
993 * specify different integer-constant bindings for the same
994 * opaque-uniform name. However, it is not an error to specify a
995 * binding on some but not all declarations for the same name"
997 if (var
->data
.explicit_binding
) {
998 if (existing
->data
.explicit_binding
&&
999 var
->data
.binding
!= existing
->data
.binding
) {
1000 linker_error(prog
, "explicit bindings for %s "
1001 "`%s' have differing values\n",
1002 mode_string(var
), var
->name
);
1006 existing
->data
.binding
= var
->data
.binding
;
1007 existing
->data
.explicit_binding
= true;
1010 if (var
->type
->contains_atomic() &&
1011 var
->data
.offset
!= existing
->data
.offset
) {
1012 linker_error(prog
, "offset specifications for %s "
1013 "`%s' have differing values\n",
1014 mode_string(var
), var
->name
);
1018 /* Validate layout qualifiers for gl_FragDepth.
1020 * From the AMD/ARB_conservative_depth specs:
1022 * "If gl_FragDepth is redeclared in any fragment shader in a
1023 * program, it must be redeclared in all fragment shaders in
1024 * that program that have static assignments to
1025 * gl_FragDepth. All redeclarations of gl_FragDepth in all
1026 * fragment shaders in a single program must have the same set
1029 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
1030 bool layout_declared
= var
->data
.depth_layout
!= ir_depth_layout_none
;
1031 bool layout_differs
=
1032 var
->data
.depth_layout
!= existing
->data
.depth_layout
;
1034 if (layout_declared
&& layout_differs
) {
1036 "All redeclarations of gl_FragDepth in all "
1037 "fragment shaders in a single program must have "
1038 "the same set of qualifiers.\n");
1041 if (var
->data
.used
&& layout_differs
) {
1043 "If gl_FragDepth is redeclared with a layout "
1044 "qualifier in any fragment shader, it must be "
1045 "redeclared with the same layout qualifier in "
1046 "all fragment shaders that have assignments to "
1051 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1053 * "If a shared global has multiple initializers, the
1054 * initializers must all be constant expressions, and they
1055 * must all have the same value. Otherwise, a link error will
1056 * result. (A shared global having only one initializer does
1057 * not require that initializer to be a constant expression.)"
1059 * Previous to 4.20 the GLSL spec simply said that initializers
1060 * must have the same value. In this case of non-constant
1061 * initializers, this was impossible to determine. As a result,
1062 * no vendor actually implemented that behavior. The 4.20
1063 * behavior matches the implemented behavior of at least one other
1064 * vendor, so we'll implement that for all GLSL versions.
1065 * If (at least) one of these constant expressions is implicit,
1066 * because it was added by glsl_zero_init, we skip the verification.
1068 if (var
->constant_initializer
!= NULL
) {
1069 if (existing
->constant_initializer
!= NULL
&&
1070 !existing
->data
.is_implicit_initializer
&&
1071 !var
->data
.is_implicit_initializer
) {
1072 if (!var
->constant_initializer
->has_value(existing
->constant_initializer
)) {
1073 linker_error(prog
, "initializers for %s "
1074 "`%s' have differing values\n",
1075 mode_string(var
), var
->name
);
1079 /* If the first-seen instance of a particular uniform did
1080 * not have an initializer but a later instance does,
1081 * replace the former with the later.
1083 if (!var
->data
.is_implicit_initializer
)
1084 variables
->replace_variable(existing
->name
, var
);
1088 if (var
->data
.has_initializer
) {
1089 if (existing
->data
.has_initializer
1090 && (var
->constant_initializer
== NULL
1091 || existing
->constant_initializer
== NULL
)) {
1093 "shared global variable `%s' has multiple "
1094 "non-constant initializers.\n",
1100 if (existing
->data
.explicit_invariant
!= var
->data
.explicit_invariant
) {
1101 linker_error(prog
, "declarations for %s `%s' have "
1102 "mismatching invariant qualifiers\n",
1103 mode_string(var
), var
->name
);
1106 if (existing
->data
.centroid
!= var
->data
.centroid
) {
1107 linker_error(prog
, "declarations for %s `%s' have "
1108 "mismatching centroid qualifiers\n",
1109 mode_string(var
), var
->name
);
1112 if (existing
->data
.sample
!= var
->data
.sample
) {
1113 linker_error(prog
, "declarations for %s `%s` have "
1114 "mismatching sample qualifiers\n",
1115 mode_string(var
), var
->name
);
1118 if (existing
->data
.image_format
!= var
->data
.image_format
) {
1119 linker_error(prog
, "declarations for %s `%s` have "
1120 "mismatching image format qualifiers\n",
1121 mode_string(var
), var
->name
);
1125 /* Check the precision qualifier matches for uniform variables on
1128 if (!ctx
->Const
.AllowGLSLRelaxedES
&&
1129 prog
->IsES
&& !var
->get_interface_type() &&
1130 existing
->data
.precision
!= var
->data
.precision
) {
1131 if ((existing
->data
.used
&& var
->data
.used
) || prog
->data
->Version
>= 300) {
1132 linker_error(prog
, "declarations for %s `%s` have "
1133 "mismatching precision qualifiers\n",
1134 mode_string(var
), var
->name
);
1137 linker_warning(prog
, "declarations for %s `%s` have "
1138 "mismatching precision qualifiers\n",
1139 mode_string(var
), var
->name
);
1143 /* In OpenGL GLSL 3.20 spec, section 4.3.9:
1145 * "It is a link-time error if any particular shader interface
1148 * - two different blocks, each having no instance name, and each
1149 * having a member of the same name, or
1151 * - a variable outside a block, and a block with no instance name,
1152 * where the variable has the same name as a member in the block."
1154 const glsl_type
*var_itype
= var
->get_interface_type();
1155 const glsl_type
*existing_itype
= existing
->get_interface_type();
1156 if (var_itype
!= existing_itype
) {
1157 if (!var_itype
|| !existing_itype
) {
1158 linker_error(prog
, "declarations for %s `%s` are inside block "
1159 "`%s` and outside a block",
1160 mode_string(var
), var
->name
,
1161 var_itype
? var_itype
->name
: existing_itype
->name
);
1163 } else if (strcmp(var_itype
->name
, existing_itype
->name
) != 0) {
1164 linker_error(prog
, "declarations for %s `%s` are inside blocks "
1166 mode_string(var
), var
->name
,
1167 existing_itype
->name
,
1173 variables
->add_variable(var
);
1179 * Perform validation of uniforms used across multiple shader stages
1182 cross_validate_uniforms(struct gl_context
*ctx
,
1183 struct gl_shader_program
*prog
)
1185 glsl_symbol_table variables
;
1186 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1187 if (prog
->_LinkedShaders
[i
] == NULL
)
1190 cross_validate_globals(ctx
, prog
, prog
->_LinkedShaders
[i
]->ir
,
1196 * Accumulates the array of buffer blocks and checks that all definitions of
1197 * blocks agree on their contents.
1200 interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
,
1203 int *InterfaceBlockStageIndex
[MESA_SHADER_STAGES
];
1204 struct gl_uniform_block
*blks
= NULL
;
1205 unsigned *num_blks
= validate_ssbo
? &prog
->data
->NumShaderStorageBlocks
:
1206 &prog
->data
->NumUniformBlocks
;
1208 unsigned max_num_buffer_blocks
= 0;
1209 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1210 if (prog
->_LinkedShaders
[i
]) {
1211 if (validate_ssbo
) {
1212 max_num_buffer_blocks
+=
1213 prog
->_LinkedShaders
[i
]->Program
->info
.num_ssbos
;
1215 max_num_buffer_blocks
+=
1216 prog
->_LinkedShaders
[i
]->Program
->info
.num_ubos
;
1221 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1222 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1224 InterfaceBlockStageIndex
[i
] = new int[max_num_buffer_blocks
];
1225 for (unsigned int j
= 0; j
< max_num_buffer_blocks
; j
++)
1226 InterfaceBlockStageIndex
[i
][j
] = -1;
1231 unsigned sh_num_blocks
;
1232 struct gl_uniform_block
**sh_blks
;
1233 if (validate_ssbo
) {
1234 sh_num_blocks
= prog
->_LinkedShaders
[i
]->Program
->info
.num_ssbos
;
1235 sh_blks
= sh
->Program
->sh
.ShaderStorageBlocks
;
1237 sh_num_blocks
= prog
->_LinkedShaders
[i
]->Program
->info
.num_ubos
;
1238 sh_blks
= sh
->Program
->sh
.UniformBlocks
;
1241 for (unsigned int j
= 0; j
< sh_num_blocks
; j
++) {
1242 int index
= link_cross_validate_uniform_block(prog
->data
, &blks
,
1243 num_blks
, sh_blks
[j
]);
1246 linker_error(prog
, "buffer block `%s' has mismatching "
1247 "definitions\n", sh_blks
[j
]->Name
);
1249 for (unsigned k
= 0; k
<= i
; k
++) {
1250 delete[] InterfaceBlockStageIndex
[k
];
1253 /* Reset the block count. This will help avoid various segfaults
1254 * from api calls that assume the array exists due to the count
1261 InterfaceBlockStageIndex
[i
][index
] = j
;
1265 /* Update per stage block pointers to point to the program list.
1266 * FIXME: We should be able to free the per stage blocks here.
1268 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1269 for (unsigned j
= 0; j
< *num_blks
; j
++) {
1270 int stage_index
= InterfaceBlockStageIndex
[i
][j
];
1272 if (stage_index
!= -1) {
1273 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1275 struct gl_uniform_block
**sh_blks
= validate_ssbo
?
1276 sh
->Program
->sh
.ShaderStorageBlocks
:
1277 sh
->Program
->sh
.UniformBlocks
;
1279 blks
[j
].stageref
|= sh_blks
[stage_index
]->stageref
;
1280 sh_blks
[stage_index
] = &blks
[j
];
1285 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1286 delete[] InterfaceBlockStageIndex
[i
];
1290 prog
->data
->ShaderStorageBlocks
= blks
;
1292 prog
->data
->UniformBlocks
= blks
;
1298 * Verifies the invariance of built-in special variables.
1301 validate_invariant_builtins(struct gl_shader_program
*prog
,
1302 const gl_linked_shader
*vert
,
1303 const gl_linked_shader
*frag
)
1305 const ir_variable
*var_vert
;
1306 const ir_variable
*var_frag
;
1312 * From OpenGL ES Shading Language 1.0 specification
1313 * (4.6.4 Invariance and Linkage):
1314 * "The invariance of varyings that are declared in both the vertex and
1315 * fragment shaders must match. For the built-in special variables,
1316 * gl_FragCoord can only be declared invariant if and only if
1317 * gl_Position is declared invariant. Similarly gl_PointCoord can only
1318 * be declared invariant if and only if gl_PointSize is declared
1319 * invariant. It is an error to declare gl_FrontFacing as invariant.
1320 * The invariance of gl_FrontFacing is the same as the invariance of
1323 var_frag
= frag
->symbols
->get_variable("gl_FragCoord");
1324 if (var_frag
&& var_frag
->data
.invariant
) {
1325 var_vert
= vert
->symbols
->get_variable("gl_Position");
1326 if (var_vert
&& !var_vert
->data
.invariant
) {
1328 "fragment shader built-in `%s' has invariant qualifier, "
1329 "but vertex shader built-in `%s' lacks invariant qualifier\n",
1330 var_frag
->name
, var_vert
->name
);
1335 var_frag
= frag
->symbols
->get_variable("gl_PointCoord");
1336 if (var_frag
&& var_frag
->data
.invariant
) {
1337 var_vert
= vert
->symbols
->get_variable("gl_PointSize");
1338 if (var_vert
&& !var_vert
->data
.invariant
) {
1340 "fragment shader built-in `%s' has invariant qualifier, "
1341 "but vertex shader built-in `%s' lacks invariant qualifier\n",
1342 var_frag
->name
, var_vert
->name
);
1347 var_frag
= frag
->symbols
->get_variable("gl_FrontFacing");
1348 if (var_frag
&& var_frag
->data
.invariant
) {
1350 "fragment shader built-in `%s' can not be declared as invariant\n",
1359 * Populates a shaders symbol table with all global declarations
1362 populate_symbol_table(gl_linked_shader
*sh
, glsl_symbol_table
*symbols
)
1364 sh
->symbols
= new(sh
) glsl_symbol_table
;
1366 _mesa_glsl_copy_symbols_from_table(sh
->ir
, symbols
, sh
->symbols
);
1371 * Remap variables referenced in an instruction tree
1373 * This is used when instruction trees are cloned from one shader and placed in
1374 * another. These trees will contain references to \c ir_variable nodes that
1375 * do not exist in the target shader. This function finds these \c ir_variable
1376 * references and replaces the references with matching variables in the target
1379 * If there is no matching variable in the target shader, a clone of the
1380 * \c ir_variable is made and added to the target shader. The new variable is
1381 * added to \b both the instruction stream and the symbol table.
1383 * \param inst IR tree that is to be processed.
1384 * \param symbols Symbol table containing global scope symbols in the
1386 * \param instructions Instruction stream where new variable declarations
1390 remap_variables(ir_instruction
*inst
, struct gl_linked_shader
*target
,
1393 class remap_visitor
: public ir_hierarchical_visitor
{
1395 remap_visitor(struct gl_linked_shader
*target
, hash_table
*temps
)
1397 this->target
= target
;
1398 this->symbols
= target
->symbols
;
1399 this->instructions
= target
->ir
;
1400 this->temps
= temps
;
1403 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
1405 if (ir
->var
->data
.mode
== ir_var_temporary
) {
1406 hash_entry
*entry
= _mesa_hash_table_search(temps
, ir
->var
);
1407 ir_variable
*var
= entry
? (ir_variable
*) entry
->data
: NULL
;
1409 assert(var
!= NULL
);
1411 return visit_continue
;
1414 ir_variable
*const existing
=
1415 this->symbols
->get_variable(ir
->var
->name
);
1416 if (existing
!= NULL
)
1419 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
1421 this->symbols
->add_variable(copy
);
1422 this->instructions
->push_head(copy
);
1426 return visit_continue
;
1430 struct gl_linked_shader
*target
;
1431 glsl_symbol_table
*symbols
;
1432 exec_list
*instructions
;
1436 remap_visitor
v(target
, temps
);
1443 * Move non-declarations from one instruction stream to another
1445 * The intended usage pattern of this function is to pass the pointer to the
1446 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
1447 * pointer) for \c last and \c false for \c make_copies on the first
1448 * call. Successive calls pass the return value of the previous call for
1449 * \c last and \c true for \c make_copies.
1451 * \param instructions Source instruction stream
1452 * \param last Instruction after which new instructions should be
1453 * inserted in the target instruction stream
1454 * \param make_copies Flag selecting whether instructions in \c instructions
1455 * should be copied (via \c ir_instruction::clone) into the
1456 * target list or moved.
1459 * The new "last" instruction in the target instruction stream. This pointer
1460 * is suitable for use as the \c last parameter of a later call to this
1464 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
1465 bool make_copies
, gl_linked_shader
*target
)
1467 hash_table
*temps
= NULL
;
1470 temps
= _mesa_pointer_hash_table_create(NULL
);
1472 foreach_in_list_safe(ir_instruction
, inst
, instructions
) {
1473 if (inst
->as_function())
1476 ir_variable
*var
= inst
->as_variable();
1477 if ((var
!= NULL
) && (var
->data
.mode
!= ir_var_temporary
))
1480 assert(inst
->as_assignment()
1482 || inst
->as_if() /* for initializers with the ?: operator */
1483 || ((var
!= NULL
) && (var
->data
.mode
== ir_var_temporary
)));
1486 inst
= inst
->clone(target
, NULL
);
1489 _mesa_hash_table_insert(temps
, var
, inst
);
1491 remap_variables(inst
, target
, temps
);
1496 last
->insert_after(inst
);
1501 _mesa_hash_table_destroy(temps
, NULL
);
1508 * This class is only used in link_intrastage_shaders() below but declaring
1509 * it inside that function leads to compiler warnings with some versions of
1512 class array_sizing_visitor
: public deref_type_updater
{
1514 using deref_type_updater::visit
;
1516 array_sizing_visitor()
1517 : mem_ctx(ralloc_context(NULL
)),
1518 unnamed_interfaces(_mesa_pointer_hash_table_create(NULL
))
1522 ~array_sizing_visitor()
1524 _mesa_hash_table_destroy(this->unnamed_interfaces
, NULL
);
1525 ralloc_free(this->mem_ctx
);
1528 virtual ir_visitor_status
visit(ir_variable
*var
)
1530 const glsl_type
*type_without_array
;
1531 bool implicit_sized_array
= var
->data
.implicit_sized_array
;
1532 fixup_type(&var
->type
, var
->data
.max_array_access
,
1533 var
->data
.from_ssbo_unsized_array
,
1534 &implicit_sized_array
);
1535 var
->data
.implicit_sized_array
= implicit_sized_array
;
1536 type_without_array
= var
->type
->without_array();
1537 if (var
->type
->is_interface()) {
1538 if (interface_contains_unsized_arrays(var
->type
)) {
1539 const glsl_type
*new_type
=
1540 resize_interface_members(var
->type
,
1541 var
->get_max_ifc_array_access(),
1542 var
->is_in_shader_storage_block());
1543 var
->type
= new_type
;
1544 var
->change_interface_type(new_type
);
1546 } else if (type_without_array
->is_interface()) {
1547 if (interface_contains_unsized_arrays(type_without_array
)) {
1548 const glsl_type
*new_type
=
1549 resize_interface_members(type_without_array
,
1550 var
->get_max_ifc_array_access(),
1551 var
->is_in_shader_storage_block());
1552 var
->change_interface_type(new_type
);
1553 var
->type
= update_interface_members_array(var
->type
, new_type
);
1555 } else if (const glsl_type
*ifc_type
= var
->get_interface_type()) {
1556 /* Store a pointer to the variable in the unnamed_interfaces
1560 _mesa_hash_table_search(this->unnamed_interfaces
,
1563 ir_variable
**interface_vars
= entry
? (ir_variable
**) entry
->data
: NULL
;
1565 if (interface_vars
== NULL
) {
1566 interface_vars
= rzalloc_array(mem_ctx
, ir_variable
*,
1568 _mesa_hash_table_insert(this->unnamed_interfaces
, ifc_type
,
1571 unsigned index
= ifc_type
->field_index(var
->name
);
1572 assert(index
< ifc_type
->length
);
1573 assert(interface_vars
[index
] == NULL
);
1574 interface_vars
[index
] = var
;
1576 return visit_continue
;
1580 * For each unnamed interface block that was discovered while running the
1581 * visitor, adjust the interface type to reflect the newly assigned array
1582 * sizes, and fix up the ir_variable nodes to point to the new interface
1585 void fixup_unnamed_interface_types()
1587 hash_table_call_foreach(this->unnamed_interfaces
,
1588 fixup_unnamed_interface_type
, NULL
);
1593 * If the type pointed to by \c type represents an unsized array, replace
1594 * it with a sized array whose size is determined by max_array_access.
1596 static void fixup_type(const glsl_type
**type
, unsigned max_array_access
,
1597 bool from_ssbo_unsized_array
, bool *implicit_sized
)
1599 if (!from_ssbo_unsized_array
&& (*type
)->is_unsized_array()) {
1600 *type
= glsl_type::get_array_instance((*type
)->fields
.array
,
1601 max_array_access
+ 1);
1602 *implicit_sized
= true;
1603 assert(*type
!= NULL
);
1607 static const glsl_type
*
1608 update_interface_members_array(const glsl_type
*type
,
1609 const glsl_type
*new_interface_type
)
1611 const glsl_type
*element_type
= type
->fields
.array
;
1612 if (element_type
->is_array()) {
1613 const glsl_type
*new_array_type
=
1614 update_interface_members_array(element_type
, new_interface_type
);
1615 return glsl_type::get_array_instance(new_array_type
, type
->length
);
1617 return glsl_type::get_array_instance(new_interface_type
,
1623 * Determine whether the given interface type contains unsized arrays (if
1624 * it doesn't, array_sizing_visitor doesn't need to process it).
1626 static bool interface_contains_unsized_arrays(const glsl_type
*type
)
1628 for (unsigned i
= 0; i
< type
->length
; i
++) {
1629 const glsl_type
*elem_type
= type
->fields
.structure
[i
].type
;
1630 if (elem_type
->is_unsized_array())
1637 * Create a new interface type based on the given type, with unsized arrays
1638 * replaced by sized arrays whose size is determined by
1639 * max_ifc_array_access.
1641 static const glsl_type
*
1642 resize_interface_members(const glsl_type
*type
,
1643 const int *max_ifc_array_access
,
1646 unsigned num_fields
= type
->length
;
1647 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1648 memcpy(fields
, type
->fields
.structure
,
1649 num_fields
* sizeof(*fields
));
1650 for (unsigned i
= 0; i
< num_fields
; i
++) {
1651 bool implicit_sized_array
= fields
[i
].implicit_sized_array
;
1652 /* If SSBO last member is unsized array, we don't replace it by a sized
1655 if (is_ssbo
&& i
== (num_fields
- 1))
1656 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1657 true, &implicit_sized_array
);
1659 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1660 false, &implicit_sized_array
);
1661 fields
[i
].implicit_sized_array
= implicit_sized_array
;
1663 glsl_interface_packing packing
=
1664 (glsl_interface_packing
) type
->interface_packing
;
1665 bool row_major
= (bool) type
->interface_row_major
;
1666 const glsl_type
*new_ifc_type
=
1667 glsl_type::get_interface_instance(fields
, num_fields
,
1668 packing
, row_major
, type
->name
);
1670 return new_ifc_type
;
1673 static void fixup_unnamed_interface_type(const void *key
, void *data
,
1676 const glsl_type
*ifc_type
= (const glsl_type
*) key
;
1677 ir_variable
**interface_vars
= (ir_variable
**) data
;
1678 unsigned num_fields
= ifc_type
->length
;
1679 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1680 memcpy(fields
, ifc_type
->fields
.structure
,
1681 num_fields
* sizeof(*fields
));
1682 bool interface_type_changed
= false;
1683 for (unsigned i
= 0; i
< num_fields
; i
++) {
1684 if (interface_vars
[i
] != NULL
&&
1685 fields
[i
].type
!= interface_vars
[i
]->type
) {
1686 fields
[i
].type
= interface_vars
[i
]->type
;
1687 interface_type_changed
= true;
1690 if (!interface_type_changed
) {
1694 glsl_interface_packing packing
=
1695 (glsl_interface_packing
) ifc_type
->interface_packing
;
1696 bool row_major
= (bool) ifc_type
->interface_row_major
;
1697 const glsl_type
*new_ifc_type
=
1698 glsl_type::get_interface_instance(fields
, num_fields
, packing
,
1699 row_major
, ifc_type
->name
);
1701 for (unsigned i
= 0; i
< num_fields
; i
++) {
1702 if (interface_vars
[i
] != NULL
)
1703 interface_vars
[i
]->change_interface_type(new_ifc_type
);
1708 * Memory context used to allocate the data in \c unnamed_interfaces.
1713 * Hash table from const glsl_type * to an array of ir_variable *'s
1714 * pointing to the ir_variables constituting each unnamed interface block.
1716 hash_table
*unnamed_interfaces
;
1720 validate_xfb_buffer_stride(struct gl_context
*ctx
, unsigned idx
,
1721 struct gl_shader_program
*prog
)
1723 /* We will validate doubles at a later stage */
1724 if (prog
->TransformFeedback
.BufferStride
[idx
] % 4) {
1725 linker_error(prog
, "invalid qualifier xfb_stride=%d must be a "
1726 "multiple of 4 or if its applied to a type that is "
1727 "or contains a double a multiple of 8.",
1728 prog
->TransformFeedback
.BufferStride
[idx
]);
1732 if (prog
->TransformFeedback
.BufferStride
[idx
] / 4 >
1733 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
1734 linker_error(prog
, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1735 "limit has been exceeded.");
1743 * Check for conflicting xfb_stride default qualifiers and store buffer stride
1747 link_xfb_stride_layout_qualifiers(struct gl_context
*ctx
,
1748 struct gl_shader_program
*prog
,
1749 struct gl_shader
**shader_list
,
1750 unsigned num_shaders
)
1752 for (unsigned i
= 0; i
< MAX_FEEDBACK_BUFFERS
; i
++) {
1753 prog
->TransformFeedback
.BufferStride
[i
] = 0;
1756 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1757 struct gl_shader
*shader
= shader_list
[i
];
1759 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1760 if (shader
->TransformFeedbackBufferStride
[j
]) {
1761 if (prog
->TransformFeedback
.BufferStride
[j
] == 0) {
1762 prog
->TransformFeedback
.BufferStride
[j
] =
1763 shader
->TransformFeedbackBufferStride
[j
];
1764 if (!validate_xfb_buffer_stride(ctx
, j
, prog
))
1766 } else if (prog
->TransformFeedback
.BufferStride
[j
] !=
1767 shader
->TransformFeedbackBufferStride
[j
]){
1769 "intrastage shaders defined with conflicting "
1770 "xfb_stride for buffer %d (%d and %d)\n", j
,
1771 prog
->TransformFeedback
.BufferStride
[j
],
1772 shader
->TransformFeedbackBufferStride
[j
]);
1781 * Check for conflicting bindless/bound sampler/image layout qualifiers at
1785 link_bindless_layout_qualifiers(struct gl_shader_program
*prog
,
1786 struct gl_shader
**shader_list
,
1787 unsigned num_shaders
)
1789 bool bindless_sampler
, bindless_image
;
1790 bool bound_sampler
, bound_image
;
1792 bindless_sampler
= bindless_image
= false;
1793 bound_sampler
= bound_image
= false;
1795 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1796 struct gl_shader
*shader
= shader_list
[i
];
1798 if (shader
->bindless_sampler
)
1799 bindless_sampler
= true;
1800 if (shader
->bindless_image
)
1801 bindless_image
= true;
1802 if (shader
->bound_sampler
)
1803 bound_sampler
= true;
1804 if (shader
->bound_image
)
1807 if ((bindless_sampler
&& bound_sampler
) ||
1808 (bindless_image
&& bound_image
)) {
1809 /* From section 4.4.6 of the ARB_bindless_texture spec:
1811 * "If both bindless_sampler and bound_sampler, or bindless_image
1812 * and bound_image, are declared at global scope in any
1813 * compilation unit, a link- time error will be generated."
1815 linker_error(prog
, "both bindless_sampler and bound_sampler, or "
1816 "bindless_image and bound_image, can't be declared at "
1823 * Check for conflicting viewport_relative settings across shaders, and sets
1824 * the value for the linked shader.
1827 link_layer_viewport_relative_qualifier(struct gl_shader_program
*prog
,
1828 struct gl_program
*gl_prog
,
1829 struct gl_shader
**shader_list
,
1830 unsigned num_shaders
)
1834 /* Find first shader with explicit layer declaration */
1835 for (i
= 0; i
< num_shaders
; i
++) {
1836 if (shader_list
[i
]->redeclares_gl_layer
) {
1837 gl_prog
->info
.layer_viewport_relative
=
1838 shader_list
[i
]->layer_viewport_relative
;
1843 /* Now make sure that each subsequent shader's explicit layer declaration
1844 * matches the first one's.
1846 for (; i
< num_shaders
; i
++) {
1847 if (shader_list
[i
]->redeclares_gl_layer
&&
1848 shader_list
[i
]->layer_viewport_relative
!=
1849 gl_prog
->info
.layer_viewport_relative
) {
1850 linker_error(prog
, "all gl_Layer redeclarations must have identical "
1851 "viewport_relative settings");
1857 * Performs the cross-validation of tessellation control shader vertices and
1858 * layout qualifiers for the attached tessellation control shaders,
1859 * and propagates them to the linked TCS and linked shader program.
1862 link_tcs_out_layout_qualifiers(struct gl_shader_program
*prog
,
1863 struct gl_program
*gl_prog
,
1864 struct gl_shader
**shader_list
,
1865 unsigned num_shaders
)
1867 if (gl_prog
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
1870 gl_prog
->info
.tess
.tcs_vertices_out
= 0;
1872 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1874 * "All tessellation control shader layout declarations in a program
1875 * must specify the same output patch vertex count. There must be at
1876 * least one layout qualifier specifying an output patch vertex count
1877 * in any program containing tessellation control shaders; however,
1878 * such a declaration is not required in all tessellation control
1882 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1883 struct gl_shader
*shader
= shader_list
[i
];
1885 if (shader
->info
.TessCtrl
.VerticesOut
!= 0) {
1886 if (gl_prog
->info
.tess
.tcs_vertices_out
!= 0 &&
1887 gl_prog
->info
.tess
.tcs_vertices_out
!=
1888 (unsigned) shader
->info
.TessCtrl
.VerticesOut
) {
1889 linker_error(prog
, "tessellation control shader defined with "
1890 "conflicting output vertex count (%d and %d)\n",
1891 gl_prog
->info
.tess
.tcs_vertices_out
,
1892 shader
->info
.TessCtrl
.VerticesOut
);
1895 gl_prog
->info
.tess
.tcs_vertices_out
=
1896 shader
->info
.TessCtrl
.VerticesOut
;
1900 /* Just do the intrastage -> interstage propagation right now,
1901 * since we already know we're in the right type of shader program
1904 if (gl_prog
->info
.tess
.tcs_vertices_out
== 0) {
1905 linker_error(prog
, "tessellation control shader didn't declare "
1906 "vertices out layout qualifier\n");
1913 * Performs the cross-validation of tessellation evaluation shader
1914 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1915 * for the attached tessellation evaluation shaders, and propagates them
1916 * to the linked TES and linked shader program.
1919 link_tes_in_layout_qualifiers(struct gl_shader_program
*prog
,
1920 struct gl_program
*gl_prog
,
1921 struct gl_shader
**shader_list
,
1922 unsigned num_shaders
)
1924 if (gl_prog
->info
.stage
!= MESA_SHADER_TESS_EVAL
)
1927 int point_mode
= -1;
1928 unsigned vertex_order
= 0;
1930 gl_prog
->info
.tess
.primitive_mode
= PRIM_UNKNOWN
;
1931 gl_prog
->info
.tess
.spacing
= TESS_SPACING_UNSPECIFIED
;
1933 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1935 * "At least one tessellation evaluation shader (compilation unit) in
1936 * a program must declare a primitive mode in its input layout.
1937 * Declaration vertex spacing, ordering, and point mode identifiers is
1938 * optional. It is not required that all tessellation evaluation
1939 * shaders in a program declare a primitive mode. If spacing or
1940 * vertex ordering declarations are omitted, the tessellation
1941 * primitive generator will use equal spacing or counter-clockwise
1942 * vertex ordering, respectively. If a point mode declaration is
1943 * omitted, the tessellation primitive generator will produce lines or
1944 * triangles according to the primitive mode."
1947 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1948 struct gl_shader
*shader
= shader_list
[i
];
1950 if (shader
->info
.TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
) {
1951 if (gl_prog
->info
.tess
.primitive_mode
!= PRIM_UNKNOWN
&&
1952 gl_prog
->info
.tess
.primitive_mode
!=
1953 shader
->info
.TessEval
.PrimitiveMode
) {
1954 linker_error(prog
, "tessellation evaluation shader defined with "
1955 "conflicting input primitive modes.\n");
1958 gl_prog
->info
.tess
.primitive_mode
=
1959 shader
->info
.TessEval
.PrimitiveMode
;
1962 if (shader
->info
.TessEval
.Spacing
!= 0) {
1963 if (gl_prog
->info
.tess
.spacing
!= 0 && gl_prog
->info
.tess
.spacing
!=
1964 shader
->info
.TessEval
.Spacing
) {
1965 linker_error(prog
, "tessellation evaluation shader defined with "
1966 "conflicting vertex spacing.\n");
1969 gl_prog
->info
.tess
.spacing
= shader
->info
.TessEval
.Spacing
;
1972 if (shader
->info
.TessEval
.VertexOrder
!= 0) {
1973 if (vertex_order
!= 0 &&
1974 vertex_order
!= shader
->info
.TessEval
.VertexOrder
) {
1975 linker_error(prog
, "tessellation evaluation shader defined with "
1976 "conflicting ordering.\n");
1979 vertex_order
= shader
->info
.TessEval
.VertexOrder
;
1982 if (shader
->info
.TessEval
.PointMode
!= -1) {
1983 if (point_mode
!= -1 &&
1984 point_mode
!= shader
->info
.TessEval
.PointMode
) {
1985 linker_error(prog
, "tessellation evaluation shader defined with "
1986 "conflicting point modes.\n");
1989 point_mode
= shader
->info
.TessEval
.PointMode
;
1994 /* Just do the intrastage -> interstage propagation right now,
1995 * since we already know we're in the right type of shader program
1998 if (gl_prog
->info
.tess
.primitive_mode
== PRIM_UNKNOWN
) {
2000 "tessellation evaluation shader didn't declare input "
2001 "primitive modes.\n");
2005 if (gl_prog
->info
.tess
.spacing
== TESS_SPACING_UNSPECIFIED
)
2006 gl_prog
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
2008 if (vertex_order
== 0 || vertex_order
== GL_CCW
)
2009 gl_prog
->info
.tess
.ccw
= true;
2011 gl_prog
->info
.tess
.ccw
= false;
2014 if (point_mode
== -1 || point_mode
== GL_FALSE
)
2015 gl_prog
->info
.tess
.point_mode
= false;
2017 gl_prog
->info
.tess
.point_mode
= true;
2022 * Performs the cross-validation of layout qualifiers specified in
2023 * redeclaration of gl_FragCoord for the attached fragment shaders,
2024 * and propagates them to the linked FS and linked shader program.
2027 link_fs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
2028 struct gl_linked_shader
*linked_shader
,
2029 struct gl_shader
**shader_list
,
2030 unsigned num_shaders
)
2032 bool redeclares_gl_fragcoord
= false;
2033 bool uses_gl_fragcoord
= false;
2034 bool origin_upper_left
= false;
2035 bool pixel_center_integer
= false;
2037 if (linked_shader
->Stage
!= MESA_SHADER_FRAGMENT
||
2038 (prog
->data
->Version
< 150 &&
2039 !prog
->ARB_fragment_coord_conventions_enable
))
2042 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2043 struct gl_shader
*shader
= shader_list
[i
];
2044 /* From the GLSL 1.50 spec, page 39:
2046 * "If gl_FragCoord is redeclared in any fragment shader in a program,
2047 * it must be redeclared in all the fragment shaders in that program
2048 * that have a static use gl_FragCoord."
2050 if ((redeclares_gl_fragcoord
&& !shader
->redeclares_gl_fragcoord
&&
2051 shader
->uses_gl_fragcoord
)
2052 || (shader
->redeclares_gl_fragcoord
&& !redeclares_gl_fragcoord
&&
2053 uses_gl_fragcoord
)) {
2054 linker_error(prog
, "fragment shader defined with conflicting "
2055 "layout qualifiers for gl_FragCoord\n");
2058 /* From the GLSL 1.50 spec, page 39:
2060 * "All redeclarations of gl_FragCoord in all fragment shaders in a
2061 * single program must have the same set of qualifiers."
2063 if (redeclares_gl_fragcoord
&& shader
->redeclares_gl_fragcoord
&&
2064 (shader
->origin_upper_left
!= origin_upper_left
||
2065 shader
->pixel_center_integer
!= pixel_center_integer
)) {
2066 linker_error(prog
, "fragment shader defined with conflicting "
2067 "layout qualifiers for gl_FragCoord\n");
2070 /* Update the linked shader state. Note that uses_gl_fragcoord should
2071 * accumulate the results. The other values should replace. If there
2072 * are multiple redeclarations, all the fields except uses_gl_fragcoord
2073 * are already known to be the same.
2075 if (shader
->redeclares_gl_fragcoord
|| shader
->uses_gl_fragcoord
) {
2076 redeclares_gl_fragcoord
= shader
->redeclares_gl_fragcoord
;
2077 uses_gl_fragcoord
|= shader
->uses_gl_fragcoord
;
2078 origin_upper_left
= shader
->origin_upper_left
;
2079 pixel_center_integer
= shader
->pixel_center_integer
;
2082 linked_shader
->Program
->info
.fs
.early_fragment_tests
|=
2083 shader
->EarlyFragmentTests
|| shader
->PostDepthCoverage
;
2084 linked_shader
->Program
->info
.fs
.inner_coverage
|= shader
->InnerCoverage
;
2085 linked_shader
->Program
->info
.fs
.post_depth_coverage
|=
2086 shader
->PostDepthCoverage
;
2087 linked_shader
->Program
->info
.fs
.pixel_interlock_ordered
|=
2088 shader
->PixelInterlockOrdered
;
2089 linked_shader
->Program
->info
.fs
.pixel_interlock_unordered
|=
2090 shader
->PixelInterlockUnordered
;
2091 linked_shader
->Program
->info
.fs
.sample_interlock_ordered
|=
2092 shader
->SampleInterlockOrdered
;
2093 linked_shader
->Program
->info
.fs
.sample_interlock_unordered
|=
2094 shader
->SampleInterlockUnordered
;
2095 linked_shader
->Program
->sh
.fs
.BlendSupport
|= shader
->BlendSupport
;
2098 linked_shader
->Program
->info
.fs
.pixel_center_integer
= pixel_center_integer
;
2099 linked_shader
->Program
->info
.fs
.origin_upper_left
= origin_upper_left
;
2103 * Performs the cross-validation of geometry shader max_vertices and
2104 * primitive type layout qualifiers for the attached geometry shaders,
2105 * and propagates them to the linked GS and linked shader program.
2108 link_gs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
2109 struct gl_program
*gl_prog
,
2110 struct gl_shader
**shader_list
,
2111 unsigned num_shaders
)
2113 /* No in/out qualifiers defined for anything but GLSL 1.50+
2114 * geometry shaders so far.
2116 if (gl_prog
->info
.stage
!= MESA_SHADER_GEOMETRY
||
2117 prog
->data
->Version
< 150)
2120 int vertices_out
= -1;
2122 gl_prog
->info
.gs
.invocations
= 0;
2123 gl_prog
->info
.gs
.input_primitive
= PRIM_UNKNOWN
;
2124 gl_prog
->info
.gs
.output_primitive
= PRIM_UNKNOWN
;
2126 /* From the GLSL 1.50 spec, page 46:
2128 * "All geometry shader output layout declarations in a program
2129 * must declare the same layout and same value for
2130 * max_vertices. There must be at least one geometry output
2131 * layout declaration somewhere in a program, but not all
2132 * geometry shaders (compilation units) are required to
2136 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2137 struct gl_shader
*shader
= shader_list
[i
];
2139 if (shader
->info
.Geom
.InputType
!= PRIM_UNKNOWN
) {
2140 if (gl_prog
->info
.gs
.input_primitive
!= PRIM_UNKNOWN
&&
2141 gl_prog
->info
.gs
.input_primitive
!=
2142 shader
->info
.Geom
.InputType
) {
2143 linker_error(prog
, "geometry shader defined with conflicting "
2147 gl_prog
->info
.gs
.input_primitive
= shader
->info
.Geom
.InputType
;
2150 if (shader
->info
.Geom
.OutputType
!= PRIM_UNKNOWN
) {
2151 if (gl_prog
->info
.gs
.output_primitive
!= PRIM_UNKNOWN
&&
2152 gl_prog
->info
.gs
.output_primitive
!=
2153 shader
->info
.Geom
.OutputType
) {
2154 linker_error(prog
, "geometry shader defined with conflicting "
2158 gl_prog
->info
.gs
.output_primitive
= shader
->info
.Geom
.OutputType
;
2161 if (shader
->info
.Geom
.VerticesOut
!= -1) {
2162 if (vertices_out
!= -1 &&
2163 vertices_out
!= shader
->info
.Geom
.VerticesOut
) {
2164 linker_error(prog
, "geometry shader defined with conflicting "
2165 "output vertex count (%d and %d)\n",
2166 vertices_out
, shader
->info
.Geom
.VerticesOut
);
2169 vertices_out
= shader
->info
.Geom
.VerticesOut
;
2172 if (shader
->info
.Geom
.Invocations
!= 0) {
2173 if (gl_prog
->info
.gs
.invocations
!= 0 &&
2174 gl_prog
->info
.gs
.invocations
!=
2175 (unsigned) shader
->info
.Geom
.Invocations
) {
2176 linker_error(prog
, "geometry shader defined with conflicting "
2177 "invocation count (%d and %d)\n",
2178 gl_prog
->info
.gs
.invocations
,
2179 shader
->info
.Geom
.Invocations
);
2182 gl_prog
->info
.gs
.invocations
= shader
->info
.Geom
.Invocations
;
2186 /* Just do the intrastage -> interstage propagation right now,
2187 * since we already know we're in the right type of shader program
2190 if (gl_prog
->info
.gs
.input_primitive
== PRIM_UNKNOWN
) {
2192 "geometry shader didn't declare primitive input type\n");
2196 if (gl_prog
->info
.gs
.output_primitive
== PRIM_UNKNOWN
) {
2198 "geometry shader didn't declare primitive output type\n");
2202 if (vertices_out
== -1) {
2204 "geometry shader didn't declare max_vertices\n");
2207 gl_prog
->info
.gs
.vertices_out
= vertices_out
;
2210 if (gl_prog
->info
.gs
.invocations
== 0)
2211 gl_prog
->info
.gs
.invocations
= 1;
2216 * Perform cross-validation of compute shader local_size_{x,y,z} layout and
2217 * derivative arrangement qualifiers for the attached compute shaders, and
2218 * propagate them to the linked CS and linked shader program.
2221 link_cs_input_layout_qualifiers(struct gl_shader_program
*prog
,
2222 struct gl_program
*gl_prog
,
2223 struct gl_shader
**shader_list
,
2224 unsigned num_shaders
)
2226 /* This function is called for all shader stages, but it only has an effect
2227 * for compute shaders.
2229 if (gl_prog
->info
.stage
!= MESA_SHADER_COMPUTE
)
2232 for (int i
= 0; i
< 3; i
++)
2233 gl_prog
->info
.cs
.local_size
[i
] = 0;
2235 gl_prog
->info
.cs
.local_size_variable
= false;
2237 gl_prog
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_NONE
;
2239 /* From the ARB_compute_shader spec, in the section describing local size
2242 * If multiple compute shaders attached to a single program object
2243 * declare local work-group size, the declarations must be identical;
2244 * otherwise a link-time error results. Furthermore, if a program
2245 * object contains any compute shaders, at least one must contain an
2246 * input layout qualifier specifying the local work sizes of the
2247 * program, or a link-time error will occur.
2249 for (unsigned sh
= 0; sh
< num_shaders
; sh
++) {
2250 struct gl_shader
*shader
= shader_list
[sh
];
2252 if (shader
->info
.Comp
.LocalSize
[0] != 0) {
2253 if (gl_prog
->info
.cs
.local_size
[0] != 0) {
2254 for (int i
= 0; i
< 3; i
++) {
2255 if (gl_prog
->info
.cs
.local_size
[i
] !=
2256 shader
->info
.Comp
.LocalSize
[i
]) {
2257 linker_error(prog
, "compute shader defined with conflicting "
2263 for (int i
= 0; i
< 3; i
++) {
2264 gl_prog
->info
.cs
.local_size
[i
] =
2265 shader
->info
.Comp
.LocalSize
[i
];
2267 } else if (shader
->info
.Comp
.LocalSizeVariable
) {
2268 if (gl_prog
->info
.cs
.local_size
[0] != 0) {
2269 /* The ARB_compute_variable_group_size spec says:
2271 * If one compute shader attached to a program declares a
2272 * variable local group size and a second compute shader
2273 * attached to the same program declares a fixed local group
2274 * size, a link-time error results.
2276 linker_error(prog
, "compute shader defined with both fixed and "
2277 "variable local group size\n");
2280 gl_prog
->info
.cs
.local_size_variable
= true;
2283 enum gl_derivative_group group
= shader
->info
.Comp
.DerivativeGroup
;
2284 if (group
!= DERIVATIVE_GROUP_NONE
) {
2285 if (gl_prog
->info
.cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
&&
2286 gl_prog
->info
.cs
.derivative_group
!= group
) {
2287 linker_error(prog
, "compute shader defined with conflicting "
2288 "derivative groups\n");
2291 gl_prog
->info
.cs
.derivative_group
= group
;
2295 /* Just do the intrastage -> interstage propagation right now,
2296 * since we already know we're in the right type of shader program
2299 if (gl_prog
->info
.cs
.local_size
[0] == 0 &&
2300 !gl_prog
->info
.cs
.local_size_variable
) {
2301 linker_error(prog
, "compute shader must contain a fixed or a variable "
2302 "local group size\n");
2306 if (gl_prog
->info
.cs
.derivative_group
== DERIVATIVE_GROUP_QUADS
) {
2307 if (gl_prog
->info
.cs
.local_size
[0] % 2 != 0) {
2308 linker_error(prog
, "derivative_group_quadsNV must be used with a "
2309 "local group size whose first dimension "
2310 "is a multiple of 2\n");
2313 if (gl_prog
->info
.cs
.local_size
[1] % 2 != 0) {
2314 linker_error(prog
, "derivative_group_quadsNV must be used with a local"
2315 "group size whose second dimension "
2316 "is a multiple of 2\n");
2319 } else if (gl_prog
->info
.cs
.derivative_group
== DERIVATIVE_GROUP_LINEAR
) {
2320 if ((gl_prog
->info
.cs
.local_size
[0] *
2321 gl_prog
->info
.cs
.local_size
[1] *
2322 gl_prog
->info
.cs
.local_size
[2]) % 4 != 0) {
2323 linker_error(prog
, "derivative_group_linearNV must be used with a "
2324 "local group size whose total number of invocations "
2325 "is a multiple of 4\n");
2332 * Link all out variables on a single stage which are not
2333 * directly used in a shader with the main function.
2336 link_output_variables(struct gl_linked_shader
*linked_shader
,
2337 struct gl_shader
**shader_list
,
2338 unsigned num_shaders
)
2340 struct glsl_symbol_table
*symbols
= linked_shader
->symbols
;
2342 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2344 /* Skip shader object with main function */
2345 if (shader_list
[i
]->symbols
->get_function("main"))
2348 foreach_in_list(ir_instruction
, ir
, shader_list
[i
]->ir
) {
2349 if (ir
->ir_type
!= ir_type_variable
)
2352 ir_variable
*var
= (ir_variable
*) ir
;
2354 if (var
->data
.mode
== ir_var_shader_out
&&
2355 !symbols
->get_variable(var
->name
)) {
2356 var
= var
->clone(linked_shader
, NULL
);
2357 symbols
->add_variable(var
);
2358 linked_shader
->ir
->push_head(var
);
2368 * Combine a group of shaders for a single stage to generate a linked shader
2371 * If this function is supplied a single shader, it is cloned, and the new
2372 * shader is returned.
2374 struct gl_linked_shader
*
2375 link_intrastage_shaders(void *mem_ctx
,
2376 struct gl_context
*ctx
,
2377 struct gl_shader_program
*prog
,
2378 struct gl_shader
**shader_list
,
2379 unsigned num_shaders
,
2380 bool allow_missing_main
)
2382 struct gl_uniform_block
*ubo_blocks
= NULL
;
2383 struct gl_uniform_block
*ssbo_blocks
= NULL
;
2384 unsigned num_ubo_blocks
= 0;
2385 unsigned num_ssbo_blocks
= 0;
2387 /* Check that global variables defined in multiple shaders are consistent.
2389 glsl_symbol_table variables
;
2390 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2391 if (shader_list
[i
] == NULL
)
2393 cross_validate_globals(ctx
, prog
, shader_list
[i
]->ir
, &variables
,
2397 if (!prog
->data
->LinkStatus
)
2400 /* Check that interface blocks defined in multiple shaders are consistent.
2402 validate_intrastage_interface_blocks(prog
, (const gl_shader
**)shader_list
,
2404 if (!prog
->data
->LinkStatus
)
2407 /* Check that there is only a single definition of each function signature
2408 * across all shaders.
2410 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
2411 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
2412 ir_function
*const f
= node
->as_function();
2417 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
2418 ir_function
*const other
=
2419 shader_list
[j
]->symbols
->get_function(f
->name
);
2421 /* If the other shader has no function (and therefore no function
2422 * signatures) with the same name, skip to the next shader.
2427 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
2428 if (!sig
->is_defined
)
2431 ir_function_signature
*other_sig
=
2432 other
->exact_matching_signature(NULL
, &sig
->parameters
);
2434 if (other_sig
!= NULL
&& other_sig
->is_defined
) {
2435 linker_error(prog
, "function `%s' is multiply defined\n",
2444 /* Find the shader that defines main, and make a clone of it.
2446 * Starting with the clone, search for undefined references. If one is
2447 * found, find the shader that defines it. Clone the reference and add
2448 * it to the shader. Repeat until there are no undefined references or
2449 * until a reference cannot be resolved.
2451 gl_shader
*main
= NULL
;
2452 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2453 if (_mesa_get_main_function_signature(shader_list
[i
]->symbols
)) {
2454 main
= shader_list
[i
];
2459 if (main
== NULL
&& allow_missing_main
)
2460 main
= shader_list
[0];
2463 linker_error(prog
, "%s shader lacks `main'\n",
2464 _mesa_shader_stage_to_string(shader_list
[0]->Stage
));
2468 gl_linked_shader
*linked
= rzalloc(NULL
, struct gl_linked_shader
);
2469 linked
->Stage
= shader_list
[0]->Stage
;
2471 /* Create program and attach it to the linked shader */
2472 struct gl_program
*gl_prog
=
2473 ctx
->Driver
.NewProgram(ctx
, shader_list
[0]->Stage
, prog
->Name
, false);
2475 prog
->data
->LinkStatus
= LINKING_FAILURE
;
2476 _mesa_delete_linked_shader(ctx
, linked
);
2480 _mesa_reference_shader_program_data(ctx
, &gl_prog
->sh
.data
, prog
->data
);
2482 /* Don't use _mesa_reference_program() just take ownership */
2483 linked
->Program
= gl_prog
;
2485 linked
->ir
= new(linked
) exec_list
;
2486 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
2488 link_fs_inout_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2489 link_tcs_out_layout_qualifiers(prog
, gl_prog
, shader_list
, num_shaders
);
2490 link_tes_in_layout_qualifiers(prog
, gl_prog
, shader_list
, num_shaders
);
2491 link_gs_inout_layout_qualifiers(prog
, gl_prog
, shader_list
, num_shaders
);
2492 link_cs_input_layout_qualifiers(prog
, gl_prog
, shader_list
, num_shaders
);
2494 if (linked
->Stage
!= MESA_SHADER_FRAGMENT
)
2495 link_xfb_stride_layout_qualifiers(ctx
, prog
, shader_list
, num_shaders
);
2497 link_bindless_layout_qualifiers(prog
, shader_list
, num_shaders
);
2499 link_layer_viewport_relative_qualifier(prog
, gl_prog
, shader_list
, num_shaders
);
2501 populate_symbol_table(linked
, shader_list
[0]->symbols
);
2503 /* The pointer to the main function in the final linked shader (i.e., the
2504 * copy of the original shader that contained the main function).
2506 ir_function_signature
*const main_sig
=
2507 _mesa_get_main_function_signature(linked
->symbols
);
2509 /* Move any instructions other than variable declarations or function
2510 * declarations into main.
2512 if (main_sig
!= NULL
) {
2513 exec_node
*insertion_point
=
2514 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
2517 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2518 if (shader_list
[i
] == main
)
2521 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
2522 insertion_point
, true, linked
);
2526 if (!link_function_calls(prog
, linked
, shader_list
, num_shaders
)) {
2527 _mesa_delete_linked_shader(ctx
, linked
);
2531 if (linked
->Stage
!= MESA_SHADER_FRAGMENT
)
2532 link_output_variables(linked
, shader_list
, num_shaders
);
2534 /* Make a pass over all variable declarations to ensure that arrays with
2535 * unspecified sizes have a size specified. The size is inferred from the
2536 * max_array_access field.
2538 array_sizing_visitor v
;
2540 v
.fixup_unnamed_interface_types();
2542 /* Link up uniform blocks defined within this stage. */
2543 link_uniform_blocks(mem_ctx
, ctx
, prog
, linked
, &ubo_blocks
,
2544 &num_ubo_blocks
, &ssbo_blocks
, &num_ssbo_blocks
);
2546 const unsigned max_uniform_blocks
=
2547 ctx
->Const
.Program
[linked
->Stage
].MaxUniformBlocks
;
2548 if (num_ubo_blocks
> max_uniform_blocks
) {
2549 linker_error(prog
, "Too many %s uniform blocks (%d/%d)\n",
2550 _mesa_shader_stage_to_string(linked
->Stage
),
2551 num_ubo_blocks
, max_uniform_blocks
);
2554 const unsigned max_shader_storage_blocks
=
2555 ctx
->Const
.Program
[linked
->Stage
].MaxShaderStorageBlocks
;
2556 if (num_ssbo_blocks
> max_shader_storage_blocks
) {
2557 linker_error(prog
, "Too many %s shader storage blocks (%d/%d)\n",
2558 _mesa_shader_stage_to_string(linked
->Stage
),
2559 num_ssbo_blocks
, max_shader_storage_blocks
);
2562 if (!prog
->data
->LinkStatus
) {
2563 _mesa_delete_linked_shader(ctx
, linked
);
2567 /* Copy ubo blocks to linked shader list */
2568 linked
->Program
->sh
.UniformBlocks
=
2569 ralloc_array(linked
, gl_uniform_block
*, num_ubo_blocks
);
2570 ralloc_steal(linked
, ubo_blocks
);
2571 for (unsigned i
= 0; i
< num_ubo_blocks
; i
++) {
2572 linked
->Program
->sh
.UniformBlocks
[i
] = &ubo_blocks
[i
];
2574 linked
->Program
->sh
.NumUniformBlocks
= num_ubo_blocks
;
2575 linked
->Program
->info
.num_ubos
= num_ubo_blocks
;
2577 /* Copy ssbo blocks to linked shader list */
2578 linked
->Program
->sh
.ShaderStorageBlocks
=
2579 ralloc_array(linked
, gl_uniform_block
*, num_ssbo_blocks
);
2580 ralloc_steal(linked
, ssbo_blocks
);
2581 for (unsigned i
= 0; i
< num_ssbo_blocks
; i
++) {
2582 linked
->Program
->sh
.ShaderStorageBlocks
[i
] = &ssbo_blocks
[i
];
2584 linked
->Program
->info
.num_ssbos
= num_ssbo_blocks
;
2586 /* At this point linked should contain all of the linked IR, so
2587 * validate it to make sure nothing went wrong.
2589 validate_ir_tree(linked
->ir
);
2591 /* Set the size of geometry shader input arrays */
2592 if (linked
->Stage
== MESA_SHADER_GEOMETRY
) {
2593 unsigned num_vertices
=
2594 vertices_per_prim(gl_prog
->info
.gs
.input_primitive
);
2595 array_resize_visitor
input_resize_visitor(num_vertices
, prog
,
2596 MESA_SHADER_GEOMETRY
);
2597 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2598 ir
->accept(&input_resize_visitor
);
2602 if (ctx
->Const
.VertexID_is_zero_based
)
2603 lower_vertex_id(linked
);
2605 if (ctx
->Const
.LowerCsDerivedVariables
)
2606 lower_cs_derived(linked
);
2609 /* Compute the source checksum. */
2610 linked
->SourceChecksum
= 0;
2611 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2612 if (shader_list
[i
] == NULL
)
2614 linked
->SourceChecksum
^= shader_list
[i
]->SourceChecksum
;
2622 * Update the sizes of linked shader uniform arrays to the maximum
2625 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
2627 * If one or more elements of an array are active,
2628 * GetActiveUniform will return the name of the array in name,
2629 * subject to the restrictions listed above. The type of the array
2630 * is returned in type. The size parameter contains the highest
2631 * array element index used, plus one. The compiler or linker
2632 * determines the highest index used. There will be only one
2633 * active uniform reported by the GL per uniform array.
2637 update_array_sizes(struct gl_shader_program
*prog
)
2639 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2640 if (prog
->_LinkedShaders
[i
] == NULL
)
2643 bool types_were_updated
= false;
2645 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
2646 ir_variable
*const var
= node
->as_variable();
2648 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
) ||
2649 !var
->type
->is_array())
2652 /* GL_ARB_uniform_buffer_object says that std140 uniforms
2653 * will not be eliminated. Since we always do std140, just
2654 * don't resize arrays in UBOs.
2656 * Atomic counters are supposed to get deterministic
2657 * locations assigned based on the declaration ordering and
2658 * sizes, array compaction would mess that up.
2660 * Subroutine uniforms are not removed.
2662 if (var
->is_in_buffer_block() || var
->type
->contains_atomic() ||
2663 var
->type
->contains_subroutine() || var
->constant_initializer
)
2666 int size
= var
->data
.max_array_access
;
2667 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
2668 if (prog
->_LinkedShaders
[j
] == NULL
)
2671 foreach_in_list(ir_instruction
, node2
, prog
->_LinkedShaders
[j
]->ir
) {
2672 ir_variable
*other_var
= node2
->as_variable();
2676 if (strcmp(var
->name
, other_var
->name
) == 0 &&
2677 other_var
->data
.max_array_access
> size
) {
2678 size
= other_var
->data
.max_array_access
;
2683 if (size
+ 1 != (int)var
->type
->length
) {
2684 /* If this is a built-in uniform (i.e., it's backed by some
2685 * fixed-function state), adjust the number of state slots to
2686 * match the new array size. The number of slots per array entry
2687 * is not known. It seems safe to assume that the total number of
2688 * slots is an integer multiple of the number of array elements.
2689 * Determine the number of slots per array element by dividing by
2690 * the old (total) size.
2692 const unsigned num_slots
= var
->get_num_state_slots();
2693 if (num_slots
> 0) {
2694 var
->set_num_state_slots((size
+ 1)
2695 * (num_slots
/ var
->type
->length
));
2698 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2700 types_were_updated
= true;
2704 /* Update the types of dereferences in case we changed any. */
2705 if (types_were_updated
) {
2706 deref_type_updater v
;
2707 v
.run(prog
->_LinkedShaders
[i
]->ir
);
2713 * Resize tessellation evaluation per-vertex inputs to the size of
2714 * tessellation control per-vertex outputs.
2717 resize_tes_inputs(struct gl_context
*ctx
,
2718 struct gl_shader_program
*prog
)
2720 if (prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] == NULL
)
2723 gl_linked_shader
*const tcs
= prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
];
2724 gl_linked_shader
*const tes
= prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
2726 /* If no control shader is present, then the TES inputs are statically
2727 * sized to MaxPatchVertices; the actual size of the arrays won't be
2728 * known until draw time.
2730 const int num_vertices
= tcs
2731 ? tcs
->Program
->info
.tess
.tcs_vertices_out
2732 : ctx
->Const
.MaxPatchVertices
;
2734 array_resize_visitor
input_resize_visitor(num_vertices
, prog
,
2735 MESA_SHADER_TESS_EVAL
);
2736 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2737 ir
->accept(&input_resize_visitor
);
2741 /* Convert the gl_PatchVerticesIn system value into a constant, since
2742 * the value is known at this point.
2744 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2745 ir_variable
*var
= ir
->as_variable();
2746 if (var
&& var
->data
.mode
== ir_var_system_value
&&
2747 var
->data
.location
== SYSTEM_VALUE_VERTICES_IN
) {
2748 void *mem_ctx
= ralloc_parent(var
);
2749 var
->data
.location
= 0;
2750 var
->data
.explicit_location
= false;
2751 var
->data
.mode
= ir_var_auto
;
2752 var
->constant_value
= new(mem_ctx
) ir_constant(num_vertices
);
2759 * Find a contiguous set of available bits in a bitmask.
2761 * \param used_mask Bits representing used (1) and unused (0) locations
2762 * \param needed_count Number of contiguous bits needed.
2765 * Base location of the available bits on success or -1 on failure.
2768 find_available_slots(unsigned used_mask
, unsigned needed_count
)
2770 unsigned needed_mask
= (1 << needed_count
) - 1;
2771 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
2773 /* The comparison to 32 is redundant, but without it GCC emits "warning:
2774 * cannot optimize possibly infinite loops" for the loop below.
2776 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
2779 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
2780 if ((needed_mask
& ~used_mask
) == needed_mask
)
2790 #define SAFE_MASK_FROM_INDEX(i) (((i) >= 32) ? ~0 : ((1 << (i)) - 1))
2793 * Assign locations for either VS inputs or FS outputs.
2795 * \param mem_ctx Temporary ralloc context used for linking.
2796 * \param prog Shader program whose variables need locations
2798 * \param constants Driver specific constant values for the program.
2799 * \param target_index Selector for the program target to receive location
2800 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
2801 * \c MESA_SHADER_FRAGMENT.
2802 * \param do_assignment Whether we are actually marking the assignment or we
2803 * are just doing a dry-run checking.
2806 * If locations are (or can be, in case of dry-running) successfully assigned,
2807 * true is returned. Otherwise an error is emitted to the shader link log and
2808 * false is returned.
2811 assign_attribute_or_color_locations(void *mem_ctx
,
2812 gl_shader_program
*prog
,
2813 struct gl_constants
*constants
,
2814 unsigned target_index
,
2817 /* Maximum number of generic locations. This corresponds to either the
2818 * maximum number of draw buffers or the maximum number of generic
2821 unsigned max_index
= (target_index
== MESA_SHADER_VERTEX
) ?
2822 constants
->Program
[target_index
].MaxAttribs
:
2823 MAX2(constants
->MaxDrawBuffers
, constants
->MaxDualSourceDrawBuffers
);
2825 /* Mark invalid locations as being used.
2827 unsigned used_locations
= ~SAFE_MASK_FROM_INDEX(max_index
);
2828 unsigned double_storage_locations
= 0;
2830 assert((target_index
== MESA_SHADER_VERTEX
)
2831 || (target_index
== MESA_SHADER_FRAGMENT
));
2833 gl_linked_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
2837 /* Operate in a total of four passes.
2839 * 1. Invalidate the location assignments for all vertex shader inputs.
2841 * 2. Assign locations for inputs that have user-defined (via
2842 * glBindVertexAttribLocation) locations and outputs that have
2843 * user-defined locations (via glBindFragDataLocation).
2845 * 3. Sort the attributes without assigned locations by number of slots
2846 * required in decreasing order. Fragmentation caused by attribute
2847 * locations assigned by the application may prevent large attributes
2848 * from having enough contiguous space.
2850 * 4. Assign locations to any inputs without assigned locations.
2853 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
2854 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
2856 const enum ir_variable_mode direction
=
2857 (target_index
== MESA_SHADER_VERTEX
)
2858 ? ir_var_shader_in
: ir_var_shader_out
;
2861 /* Temporary storage for the set of attributes that need locations assigned.
2867 /* Used below in the call to qsort. */
2868 static int compare(const void *a
, const void *b
)
2870 const temp_attr
*const l
= (const temp_attr
*) a
;
2871 const temp_attr
*const r
= (const temp_attr
*) b
;
2873 /* Reversed because we want a descending order sort below. */
2874 return r
->slots
- l
->slots
;
2877 assert(max_index
<= 32);
2879 /* Temporary array for the set of attributes that have locations assigned,
2880 * for the purpose of checking overlapping slots/components of (non-ES)
2881 * fragment shader outputs.
2883 ir_variable
*assigned
[12 * 4]; /* (max # of FS outputs) * # components */
2884 unsigned assigned_attr
= 0;
2886 unsigned num_attr
= 0;
2888 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2889 ir_variable
*const var
= node
->as_variable();
2891 if ((var
== NULL
) || (var
->data
.mode
!= (unsigned) direction
))
2894 if (var
->data
.explicit_location
) {
2895 var
->data
.is_unmatched_generic_inout
= 0;
2896 if ((var
->data
.location
>= (int)(max_index
+ generic_base
))
2897 || (var
->data
.location
< 0)) {
2899 "invalid explicit location %d specified for `%s'\n",
2900 (var
->data
.location
< 0)
2901 ? var
->data
.location
2902 : var
->data
.location
- generic_base
,
2906 } else if (target_index
== MESA_SHADER_VERTEX
) {
2909 if (prog
->AttributeBindings
->get(binding
, var
->name
)) {
2910 assert(binding
>= VERT_ATTRIB_GENERIC0
);
2911 var
->data
.location
= binding
;
2912 var
->data
.is_unmatched_generic_inout
= 0;
2914 } else if (target_index
== MESA_SHADER_FRAGMENT
) {
2917 const char *name
= var
->name
;
2918 const glsl_type
*type
= var
->type
;
2921 /* Check if there's a binding for the variable name */
2922 if (prog
->FragDataBindings
->get(binding
, name
)) {
2923 assert(binding
>= FRAG_RESULT_DATA0
);
2924 var
->data
.location
= binding
;
2925 var
->data
.is_unmatched_generic_inout
= 0;
2927 if (prog
->FragDataIndexBindings
->get(index
, name
)) {
2928 var
->data
.index
= index
;
2933 /* If not, but it's an array type, look for name[0] */
2934 if (type
->is_array()) {
2935 name
= ralloc_asprintf(mem_ctx
, "%s[0]", name
);
2936 type
= type
->fields
.array
;
2944 if (strcmp(var
->name
, "gl_LastFragData") == 0)
2947 /* From GL4.5 core spec, section 15.2 (Shader Execution):
2949 * "Output binding assignments will cause LinkProgram to fail:
2951 * If the program has an active output assigned to a location greater
2952 * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
2953 * an active output assigned an index greater than or equal to one;"
2955 if (target_index
== MESA_SHADER_FRAGMENT
&& var
->data
.index
>= 1 &&
2956 var
->data
.location
- generic_base
>=
2957 (int) constants
->MaxDualSourceDrawBuffers
) {
2959 "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
2960 "with index %u for %s\n",
2961 var
->data
.location
- generic_base
, var
->data
.index
,
2966 const unsigned slots
= var
->type
->count_attribute_slots(target_index
== MESA_SHADER_VERTEX
);
2968 /* If the variable is not a built-in and has a location statically
2969 * assigned in the shader (presumably via a layout qualifier), make sure
2970 * that it doesn't collide with other assigned locations. Otherwise,
2971 * add it to the list of variables that need linker-assigned locations.
2973 if (var
->data
.location
!= -1) {
2974 if (var
->data
.location
>= generic_base
&& var
->data
.index
< 1) {
2975 /* From page 61 of the OpenGL 4.0 spec:
2977 * "LinkProgram will fail if the attribute bindings assigned
2978 * by BindAttribLocation do not leave not enough space to
2979 * assign a location for an active matrix attribute or an
2980 * active attribute array, both of which require multiple
2981 * contiguous generic attributes."
2983 * I think above text prohibits the aliasing of explicit and
2984 * automatic assignments. But, aliasing is allowed in manual
2985 * assignments of attribute locations. See below comments for
2988 * From OpenGL 4.0 spec, page 61:
2990 * "It is possible for an application to bind more than one
2991 * attribute name to the same location. This is referred to as
2992 * aliasing. This will only work if only one of the aliased
2993 * attributes is active in the executable program, or if no
2994 * path through the shader consumes more than one attribute of
2995 * a set of attributes aliased to the same location. A link
2996 * error can occur if the linker determines that every path
2997 * through the shader consumes multiple aliased attributes,
2998 * but implementations are not required to generate an error
3001 * From GLSL 4.30 spec, page 54:
3003 * "A program will fail to link if any two non-vertex shader
3004 * input variables are assigned to the same location. For
3005 * vertex shaders, multiple input variables may be assigned
3006 * to the same location using either layout qualifiers or via
3007 * the OpenGL API. However, such aliasing is intended only to
3008 * support vertex shaders where each execution path accesses
3009 * at most one input per each location. Implementations are
3010 * permitted, but not required, to generate link-time errors
3011 * if they detect that every path through the vertex shader
3012 * executable accesses multiple inputs assigned to any single
3013 * location. For all shader types, a program will fail to link
3014 * if explicit location assignments leave the linker unable
3015 * to find space for other variables without explicit
3018 * From OpenGL ES 3.0 spec, page 56:
3020 * "Binding more than one attribute name to the same location
3021 * is referred to as aliasing, and is not permitted in OpenGL
3022 * ES Shading Language 3.00 vertex shaders. LinkProgram will
3023 * fail when this condition exists. However, aliasing is
3024 * possible in OpenGL ES Shading Language 1.00 vertex shaders.
3025 * This will only work if only one of the aliased attributes
3026 * is active in the executable program, or if no path through
3027 * the shader consumes more than one attribute of a set of
3028 * attributes aliased to the same location. A link error can
3029 * occur if the linker determines that every path through the
3030 * shader consumes multiple aliased attributes, but implemen-
3031 * tations are not required to generate an error in this case."
3033 * After looking at above references from OpenGL, OpenGL ES and
3034 * GLSL specifications, we allow aliasing of vertex input variables
3035 * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
3037 * NOTE: This is not required by the spec but its worth mentioning
3038 * here that we're not doing anything to make sure that no path
3039 * through the vertex shader executable accesses multiple inputs
3040 * assigned to any single location.
3043 /* Mask representing the contiguous slots that will be used by
3046 const unsigned attr
= var
->data
.location
- generic_base
;
3047 const unsigned use_mask
= (1 << slots
) - 1;
3048 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
3049 ? "vertex shader input" : "fragment shader output";
3051 /* Generate a link error if the requested locations for this
3052 * attribute exceed the maximum allowed attribute location.
3054 if (attr
+ slots
> max_index
) {
3056 "insufficient contiguous locations "
3057 "available for %s `%s' %d %d %d\n", string
,
3058 var
->name
, used_locations
, use_mask
, attr
);
3062 /* Generate a link error if the set of bits requested for this
3063 * attribute overlaps any previously allocated bits.
3065 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
3066 if (target_index
== MESA_SHADER_FRAGMENT
&& !prog
->IsES
) {
3067 /* From section 4.4.2 (Output Layout Qualifiers) of the GLSL
3070 * "Additionally, for fragment shader outputs, if two
3071 * variables are placed within the same location, they
3072 * must have the same underlying type (floating-point or
3073 * integer). No component aliasing of output variables or
3074 * members is allowed.
3076 for (unsigned i
= 0; i
< assigned_attr
; i
++) {
3077 unsigned assigned_slots
=
3078 assigned
[i
]->type
->count_attribute_slots(false);
3079 unsigned assig_attr
=
3080 assigned
[i
]->data
.location
- generic_base
;
3081 unsigned assigned_use_mask
= (1 << assigned_slots
) - 1;
3083 if ((assigned_use_mask
<< assig_attr
) &
3084 (use_mask
<< attr
)) {
3086 const glsl_type
*assigned_type
=
3087 assigned
[i
]->type
->without_array();
3088 const glsl_type
*type
= var
->type
->without_array();
3089 if (assigned_type
->base_type
!= type
->base_type
) {
3090 linker_error(prog
, "types do not match for aliased"
3091 " %ss %s and %s\n", string
,
3092 assigned
[i
]->name
, var
->name
);
3096 unsigned assigned_component_mask
=
3097 ((1 << assigned_type
->vector_elements
) - 1) <<
3098 assigned
[i
]->data
.location_frac
;
3099 unsigned component_mask
=
3100 ((1 << type
->vector_elements
) - 1) <<
3101 var
->data
.location_frac
;
3102 if (assigned_component_mask
& component_mask
) {
3103 linker_error(prog
, "overlapping component is "
3104 "assigned to %ss %s and %s "
3106 string
, assigned
[i
]->name
, var
->name
,
3107 var
->data
.location_frac
);
3112 } else if (target_index
== MESA_SHADER_FRAGMENT
||
3113 (prog
->IsES
&& prog
->data
->Version
>= 300)) {
3114 linker_error(prog
, "overlapping location is assigned "
3115 "to %s `%s' %d %d %d\n", string
, var
->name
,
3116 used_locations
, use_mask
, attr
);
3119 linker_warning(prog
, "overlapping location is assigned "
3120 "to %s `%s' %d %d %d\n", string
, var
->name
,
3121 used_locations
, use_mask
, attr
);
3125 if (target_index
== MESA_SHADER_FRAGMENT
&& !prog
->IsES
) {
3126 /* Only track assigned variables for non-ES fragment shaders
3127 * to avoid overflowing the array.
3129 * At most one variable per fragment output component should
3132 assert(assigned_attr
< ARRAY_SIZE(assigned
));
3133 assigned
[assigned_attr
] = var
;
3137 used_locations
|= (use_mask
<< attr
);
3139 /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
3141 * "A program with more than the value of MAX_VERTEX_ATTRIBS
3142 * active attribute variables may fail to link, unless
3143 * device-dependent optimizations are able to make the program
3144 * fit within available hardware resources. For the purposes
3145 * of this test, attribute variables of the type dvec3, dvec4,
3146 * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
3147 * count as consuming twice as many attributes as equivalent
3148 * single-precision types. While these types use the same number
3149 * of generic attributes as their single-precision equivalents,
3150 * implementations are permitted to consume two single-precision
3151 * vectors of internal storage for each three- or four-component
3152 * double-precision vector."
3154 * Mark this attribute slot as taking up twice as much space
3155 * so we can count it properly against limits. According to
3156 * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
3157 * is optional behavior, but it seems preferable.
3159 if (var
->type
->without_array()->is_dual_slot())
3160 double_storage_locations
|= (use_mask
<< attr
);
3166 if (num_attr
>= max_index
) {
3167 linker_error(prog
, "too many %s (max %u)",
3168 target_index
== MESA_SHADER_VERTEX
?
3169 "vertex shader inputs" : "fragment shader outputs",
3173 to_assign
[num_attr
].slots
= slots
;
3174 to_assign
[num_attr
].var
= var
;
3181 if (target_index
== MESA_SHADER_VERTEX
) {
3182 unsigned total_attribs_size
=
3183 util_bitcount(used_locations
& SAFE_MASK_FROM_INDEX(max_index
)) +
3184 util_bitcount(double_storage_locations
);
3185 if (total_attribs_size
> max_index
) {
3187 "attempt to use %d vertex attribute slots only %d available ",
3188 total_attribs_size
, max_index
);
3193 /* If all of the attributes were assigned locations by the application (or
3194 * are built-in attributes with fixed locations), return early. This should
3195 * be the common case.
3200 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
3202 if (target_index
== MESA_SHADER_VERTEX
) {
3203 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
3204 * only be explicitly assigned by via glBindAttribLocation. Mark it as
3205 * reserved to prevent it from being automatically allocated below.
3207 find_deref_visitor
find("gl_Vertex");
3209 if (find
.variable_found())
3210 used_locations
|= (1 << 0);
3213 for (unsigned i
= 0; i
< num_attr
; i
++) {
3214 /* Mask representing the contiguous slots that will be used by this
3217 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
3219 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
3222 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
3223 ? "vertex shader input" : "fragment shader output";
3226 "insufficient contiguous locations "
3227 "available for %s `%s'\n",
3228 string
, to_assign
[i
].var
->name
);
3232 to_assign
[i
].var
->data
.location
= generic_base
+ location
;
3233 to_assign
[i
].var
->data
.is_unmatched_generic_inout
= 0;
3234 used_locations
|= (use_mask
<< location
);
3236 if (to_assign
[i
].var
->type
->without_array()->is_dual_slot())
3237 double_storage_locations
|= (use_mask
<< location
);
3240 /* Now that we have all the locations, from the GL 4.5 core spec, section
3241 * 11.1.1 (Vertex Attributes), dvec3, dvec4, dmat2x3, dmat2x4, dmat3,
3242 * dmat3x4, dmat4x3, and dmat4 count as consuming twice as many attributes
3243 * as equivalent single-precision types.
3245 if (target_index
== MESA_SHADER_VERTEX
) {
3246 unsigned total_attribs_size
=
3247 util_bitcount(used_locations
& SAFE_MASK_FROM_INDEX(max_index
)) +
3248 util_bitcount(double_storage_locations
);
3249 if (total_attribs_size
> max_index
) {
3251 "attempt to use %d vertex attribute slots only %d available ",
3252 total_attribs_size
, max_index
);
3261 * Match explicit locations of outputs to inputs and deactivate the
3262 * unmatch flag if found so we don't optimise them away.
3265 match_explicit_outputs_to_inputs(gl_linked_shader
*producer
,
3266 gl_linked_shader
*consumer
)
3268 glsl_symbol_table parameters
;
3269 ir_variable
*explicit_locations
[MAX_VARYINGS_INCL_PATCH
][4] =
3272 /* Find all shader outputs in the "producer" stage.
3274 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
3275 ir_variable
*const var
= node
->as_variable();
3277 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_shader_out
))
3280 if (var
->data
.explicit_location
&&
3281 var
->data
.location
>= VARYING_SLOT_VAR0
) {
3282 const unsigned idx
= var
->data
.location
- VARYING_SLOT_VAR0
;
3283 if (explicit_locations
[idx
][var
->data
.location_frac
] == NULL
)
3284 explicit_locations
[idx
][var
->data
.location_frac
] = var
;
3286 /* Always match TCS outputs. They are shared by all invocations
3287 * within a patch and can be used as shared memory.
3289 if (producer
->Stage
== MESA_SHADER_TESS_CTRL
)
3290 var
->data
.is_unmatched_generic_inout
= 0;
3294 /* Match inputs to outputs */
3295 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
3296 ir_variable
*const input
= node
->as_variable();
3298 if ((input
== NULL
) || (input
->data
.mode
!= ir_var_shader_in
))
3301 ir_variable
*output
= NULL
;
3302 if (input
->data
.explicit_location
3303 && input
->data
.location
>= VARYING_SLOT_VAR0
) {
3304 output
= explicit_locations
[input
->data
.location
- VARYING_SLOT_VAR0
]
3305 [input
->data
.location_frac
];
3307 if (output
!= NULL
){
3308 input
->data
.is_unmatched_generic_inout
= 0;
3309 output
->data
.is_unmatched_generic_inout
= 0;
3316 * Store the gl_FragDepth layout in the gl_shader_program struct.
3319 store_fragdepth_layout(struct gl_shader_program
*prog
)
3321 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
3325 struct exec_list
*ir
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
3327 /* We don't look up the gl_FragDepth symbol directly because if
3328 * gl_FragDepth is not used in the shader, it's removed from the IR.
3329 * However, the symbol won't be removed from the symbol table.
3331 * We're only interested in the cases where the variable is NOT removed
3334 foreach_in_list(ir_instruction
, node
, ir
) {
3335 ir_variable
*const var
= node
->as_variable();
3337 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
) {
3341 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
3342 switch (var
->data
.depth_layout
) {
3343 case ir_depth_layout_none
:
3344 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
3346 case ir_depth_layout_any
:
3347 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
3349 case ir_depth_layout_greater
:
3350 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
3352 case ir_depth_layout_less
:
3353 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
3355 case ir_depth_layout_unchanged
:
3356 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3367 * Validate shader image resources.
3370 check_image_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3372 unsigned total_image_units
= 0;
3373 unsigned fragment_outputs
= 0;
3374 unsigned total_shader_storage_blocks
= 0;
3376 if (!ctx
->Extensions
.ARB_shader_image_load_store
)
3379 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3380 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3383 total_image_units
+= sh
->Program
->info
.num_images
;
3384 total_shader_storage_blocks
+= sh
->Program
->info
.num_ssbos
;
3386 if (i
== MESA_SHADER_FRAGMENT
) {
3387 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3388 ir_variable
*var
= node
->as_variable();
3389 if (var
&& var
->data
.mode
== ir_var_shader_out
)
3390 /* since there are no double fs outputs - pass false */
3391 fragment_outputs
+= var
->type
->count_attribute_slots(false);
3397 if (total_image_units
> ctx
->Const
.MaxCombinedImageUniforms
)
3398 linker_error(prog
, "Too many combined image uniforms\n");
3400 if (total_image_units
+ fragment_outputs
+ total_shader_storage_blocks
>
3401 ctx
->Const
.MaxCombinedShaderOutputResources
)
3402 linker_error(prog
, "Too many combined image uniforms, shader storage "
3403 " buffers and fragment outputs\n");
3408 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
3409 * for a variable, checks for overlaps between other uniforms using explicit
3413 reserve_explicit_locations(struct gl_shader_program
*prog
,
3414 string_to_uint_map
*map
, ir_variable
*var
)
3416 unsigned slots
= var
->type
->uniform_locations();
3417 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3418 unsigned return_value
= slots
;
3420 /* Resize remap table if locations do not fit in the current one. */
3421 if (max_loc
+ 1 > prog
->NumUniformRemapTable
) {
3422 prog
->UniformRemapTable
=
3423 reralloc(prog
, prog
->UniformRemapTable
,
3424 gl_uniform_storage
*,
3427 if (!prog
->UniformRemapTable
) {
3428 linker_error(prog
, "Out of memory during linking.\n");
3432 /* Initialize allocated space. */
3433 for (unsigned i
= prog
->NumUniformRemapTable
; i
< max_loc
+ 1; i
++)
3434 prog
->UniformRemapTable
[i
] = NULL
;
3436 prog
->NumUniformRemapTable
= max_loc
+ 1;
3439 for (unsigned i
= 0; i
< slots
; i
++) {
3440 unsigned loc
= var
->data
.location
+ i
;
3442 /* Check if location is already used. */
3443 if (prog
->UniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3445 /* Possibly same uniform from a different stage, this is ok. */
3447 if (map
->get(hash_loc
, var
->name
) && hash_loc
== loc
- i
) {
3452 /* ARB_explicit_uniform_location specification states:
3454 * "No two default-block uniform variables in the program can have
3455 * the same location, even if they are unused, otherwise a compiler
3456 * or linker error will be generated."
3459 "location qualifier for uniform %s overlaps "
3460 "previously used location\n",
3465 /* Initialize location as inactive before optimization
3466 * rounds and location assignment.
3468 prog
->UniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3471 /* Note, base location used for arrays. */
3472 map
->put(var
->data
.location
, var
->name
);
3474 return return_value
;
3478 reserve_subroutine_explicit_locations(struct gl_shader_program
*prog
,
3479 struct gl_program
*p
,
3482 unsigned slots
= var
->type
->uniform_locations();
3483 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3485 /* Resize remap table if locations do not fit in the current one. */
3486 if (max_loc
+ 1 > p
->sh
.NumSubroutineUniformRemapTable
) {
3487 p
->sh
.SubroutineUniformRemapTable
=
3488 reralloc(p
, p
->sh
.SubroutineUniformRemapTable
,
3489 gl_uniform_storage
*,
3492 if (!p
->sh
.SubroutineUniformRemapTable
) {
3493 linker_error(prog
, "Out of memory during linking.\n");
3497 /* Initialize allocated space. */
3498 for (unsigned i
= p
->sh
.NumSubroutineUniformRemapTable
; i
< max_loc
+ 1; i
++)
3499 p
->sh
.SubroutineUniformRemapTable
[i
] = NULL
;
3501 p
->sh
.NumSubroutineUniformRemapTable
= max_loc
+ 1;
3504 for (unsigned i
= 0; i
< slots
; i
++) {
3505 unsigned loc
= var
->data
.location
+ i
;
3507 /* Check if location is already used. */
3508 if (p
->sh
.SubroutineUniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3510 /* ARB_explicit_uniform_location specification states:
3511 * "No two subroutine uniform variables can have the same location
3512 * in the same shader stage, otherwise a compiler or linker error
3513 * will be generated."
3516 "location qualifier for uniform %s overlaps "
3517 "previously used location\n",
3522 /* Initialize location as inactive before optimization
3523 * rounds and location assignment.
3525 p
->sh
.SubroutineUniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3531 * Check and reserve all explicit uniform locations, called before
3532 * any optimizations happen to handle also inactive uniforms and
3533 * inactive array elements that may get trimmed away.
3536 check_explicit_uniform_locations(struct gl_context
*ctx
,
3537 struct gl_shader_program
*prog
)
3539 prog
->NumExplicitUniformLocations
= 0;
3541 if (!ctx
->Extensions
.ARB_explicit_uniform_location
)
3544 /* This map is used to detect if overlapping explicit locations
3545 * occur with the same uniform (from different stage) or a different one.
3547 string_to_uint_map
*uniform_map
= new string_to_uint_map
;
3550 linker_error(prog
, "Out of memory during linking.\n");
3554 unsigned entries_total
= 0;
3555 unsigned mask
= prog
->data
->linked_stages
;
3557 const int i
= u_bit_scan(&mask
);
3558 struct gl_program
*p
= prog
->_LinkedShaders
[i
]->Program
;
3560 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
3561 ir_variable
*var
= node
->as_variable();
3562 if (!var
|| var
->data
.mode
!= ir_var_uniform
)
3565 if (var
->data
.explicit_location
) {
3567 if (var
->type
->without_array()->is_subroutine())
3568 ret
= reserve_subroutine_explicit_locations(prog
, p
, var
);
3570 int slots
= reserve_explicit_locations(prog
, uniform_map
,
3574 entries_total
+= slots
;
3585 link_util_update_empty_uniform_locations(prog
);
3588 prog
->NumExplicitUniformLocations
= entries_total
;
3591 /* Function checks if a variable var is a packed varying and
3592 * if given name is part of packed varying's list.
3594 * If a variable is a packed varying, it has a name like
3595 * 'packed:a,b,c' where a, b and c are separate variables.
3598 included_in_packed_varying(ir_variable
*var
, const char *name
)
3600 if (strncmp(var
->name
, "packed:", 7) != 0)
3603 char *list
= strdup(var
->name
+ 7);
3608 char *token
= strtok_r(list
, ",", &saveptr
);
3610 if (strcmp(token
, name
) == 0) {
3614 token
= strtok_r(NULL
, ",", &saveptr
);
3621 * Function builds a stage reference bitmask from variable name.
3624 build_stageref(struct gl_shader_program
*shProg
, const char *name
,
3629 /* Note, that we assume MAX 8 stages, if there will be more stages, type
3630 * used for reference mask in gl_program_resource will need to be changed.
3632 assert(MESA_SHADER_STAGES
< 8);
3634 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3635 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
3639 /* Shader symbol table may contain variables that have
3640 * been optimized away. Search IR for the variable instead.
3642 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3643 ir_variable
*var
= node
->as_variable();
3645 unsigned baselen
= strlen(var
->name
);
3647 if (included_in_packed_varying(var
, name
)) {
3652 /* Type needs to match if specified, otherwise we might
3653 * pick a variable with same name but different interface.
3655 if (var
->data
.mode
!= mode
)
3658 if (strncmp(var
->name
, name
, baselen
) == 0) {
3659 /* Check for exact name matches but also check for arrays and
3662 if (name
[baselen
] == '\0' ||
3663 name
[baselen
] == '[' ||
3664 name
[baselen
] == '.') {
3676 * Create gl_shader_variable from ir_variable class.
3678 static gl_shader_variable
*
3679 create_shader_variable(struct gl_shader_program
*shProg
,
3680 const ir_variable
*in
,
3681 const char *name
, const glsl_type
*type
,
3682 const glsl_type
*interface_type
,
3683 bool use_implicit_location
, int location
,
3684 const glsl_type
*outermost_struct_type
)
3686 /* Allocate zero-initialized memory to ensure that bitfield padding
3689 gl_shader_variable
*out
= rzalloc(shProg
, struct gl_shader_variable
);
3693 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
3694 * expect to see gl_VertexID in the program resource list. Pretend.
3696 if (in
->data
.mode
== ir_var_system_value
&&
3697 in
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
3698 out
->name
= ralloc_strdup(shProg
, "gl_VertexID");
3699 } else if ((in
->data
.mode
== ir_var_shader_out
&&
3700 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
) ||
3701 (in
->data
.mode
== ir_var_system_value
&&
3702 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_OUTER
)) {
3703 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelOuter");
3704 type
= glsl_type::get_array_instance(glsl_type::float_type
, 4);
3705 } else if ((in
->data
.mode
== ir_var_shader_out
&&
3706 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
) ||
3707 (in
->data
.mode
== ir_var_system_value
&&
3708 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_INNER
)) {
3709 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelInner");
3710 type
= glsl_type::get_array_instance(glsl_type::float_type
, 2);
3712 out
->name
= ralloc_strdup(shProg
, name
);
3718 /* The ARB_program_interface_query spec says:
3720 * "Not all active variables are assigned valid locations; the
3721 * following variables will have an effective location of -1:
3723 * * uniforms declared as atomic counters;
3725 * * members of a uniform block;
3727 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
3729 * * inputs or outputs not declared with a "location" layout
3730 * qualifier, except for vertex shader inputs and fragment shader
3733 if (in
->type
->is_atomic_uint() || is_gl_identifier(in
->name
) ||
3734 !(in
->data
.explicit_location
|| use_implicit_location
)) {
3737 out
->location
= location
;
3741 out
->outermost_struct_type
= outermost_struct_type
;
3742 out
->interface_type
= interface_type
;
3743 out
->component
= in
->data
.location_frac
;
3744 out
->index
= in
->data
.index
;
3745 out
->patch
= in
->data
.patch
;
3746 out
->mode
= in
->data
.mode
;
3747 out
->interpolation
= in
->data
.interpolation
;
3748 out
->explicit_location
= in
->data
.explicit_location
;
3749 out
->precision
= in
->data
.precision
;
3755 add_shader_variable(const struct gl_context
*ctx
,
3756 struct gl_shader_program
*shProg
,
3757 struct set
*resource_set
,
3758 unsigned stage_mask
,
3759 GLenum programInterface
, ir_variable
*var
,
3760 const char *name
, const glsl_type
*type
,
3761 bool use_implicit_location
, int location
,
3762 bool inouts_share_location
,
3763 const glsl_type
*outermost_struct_type
= NULL
)
3765 const glsl_type
*interface_type
= var
->get_interface_type();
3767 if (outermost_struct_type
== NULL
) {
3768 if (var
->data
.from_named_ifc_block
) {
3769 const char *interface_name
= interface_type
->name
;
3771 if (interface_type
->is_array()) {
3772 /* Issue #16 of the ARB_program_interface_query spec says:
3774 * "* If a variable is a member of an interface block without an
3775 * instance name, it is enumerated using just the variable name.
3777 * * If a variable is a member of an interface block with an
3778 * instance name, it is enumerated as "BlockName.Member", where
3779 * "BlockName" is the name of the interface block (not the
3780 * instance name) and "Member" is the name of the variable."
3782 * In particular, it indicates that it should be "BlockName",
3783 * not "BlockName[array length]". The conformance suite and
3784 * dEQP both require this behavior.
3786 * Here, we unwrap the extra array level added by named interface
3787 * block array lowering so we have the correct variable type. We
3788 * also unwrap the interface type when constructing the name.
3790 * We leave interface_type the same so that ES 3.x SSO pipeline
3791 * validation can enforce the rules requiring array length to
3792 * match on interface blocks.
3794 type
= type
->fields
.array
;
3796 interface_name
= interface_type
->fields
.array
->name
;
3799 name
= ralloc_asprintf(shProg
, "%s.%s", interface_name
, name
);
3803 switch (type
->base_type
) {
3804 case GLSL_TYPE_STRUCT
: {
3805 /* The ARB_program_interface_query spec says:
3807 * "For an active variable declared as a structure, a separate entry
3808 * will be generated for each active structure member. The name of
3809 * each entry is formed by concatenating the name of the structure,
3810 * the "." character, and the name of the structure member. If a
3811 * structure member to enumerate is itself a structure or array,
3812 * these enumeration rules are applied recursively."
3814 if (outermost_struct_type
== NULL
)
3815 outermost_struct_type
= type
;
3817 unsigned field_location
= location
;
3818 for (unsigned i
= 0; i
< type
->length
; i
++) {
3819 const struct glsl_struct_field
*field
= &type
->fields
.structure
[i
];
3820 char *field_name
= ralloc_asprintf(shProg
, "%s.%s", name
, field
->name
);
3821 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3822 stage_mask
, programInterface
,
3823 var
, field_name
, field
->type
,
3824 use_implicit_location
, field_location
,
3825 false, outermost_struct_type
))
3828 field_location
+= field
->type
->count_attribute_slots(false);
3833 case GLSL_TYPE_ARRAY
: {
3834 /* The ARB_program_interface_query spec says:
3836 * "For an active variable declared as an array of basic types, a
3837 * single entry will be generated, with its name string formed by
3838 * concatenating the name of the array and the string "[0]"."
3840 * "For an active variable declared as an array of an aggregate data
3841 * type (structures or arrays), a separate entry will be generated
3842 * for each active array element, unless noted immediately below.
3843 * The name of each entry is formed by concatenating the name of
3844 * the array, the "[" character, an integer identifying the element
3845 * number, and the "]" character. These enumeration rules are
3846 * applied recursively, treating each enumerated array element as a
3847 * separate active variable."
3849 const struct glsl_type
*array_type
= type
->fields
.array
;
3850 if (array_type
->base_type
== GLSL_TYPE_STRUCT
||
3851 array_type
->base_type
== GLSL_TYPE_ARRAY
) {
3852 unsigned elem_location
= location
;
3853 unsigned stride
= inouts_share_location
? 0 :
3854 array_type
->count_attribute_slots(false);
3855 for (unsigned i
= 0; i
< type
->length
; i
++) {
3856 char *elem
= ralloc_asprintf(shProg
, "%s[%d]", name
, i
);
3857 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3858 stage_mask
, programInterface
,
3859 var
, elem
, array_type
,
3860 use_implicit_location
, elem_location
,
3861 false, outermost_struct_type
))
3863 elem_location
+= stride
;
3871 /* The ARB_program_interface_query spec says:
3873 * "For an active variable declared as a single instance of a basic
3874 * type, a single entry will be generated, using the variable name
3875 * from the shader source."
3877 gl_shader_variable
*sha_v
=
3878 create_shader_variable(shProg
, var
, name
, type
, interface_type
,
3879 use_implicit_location
, location
,
3880 outermost_struct_type
);
3884 return link_util_add_program_resource(shProg
, resource_set
,
3885 programInterface
, sha_v
, stage_mask
);
3891 inout_has_same_location(const ir_variable
*var
, unsigned stage
)
3893 if (!var
->data
.patch
&&
3894 ((var
->data
.mode
== ir_var_shader_out
&&
3895 stage
== MESA_SHADER_TESS_CTRL
) ||
3896 (var
->data
.mode
== ir_var_shader_in
&&
3897 (stage
== MESA_SHADER_TESS_CTRL
|| stage
== MESA_SHADER_TESS_EVAL
||
3898 stage
== MESA_SHADER_GEOMETRY
))))
3905 add_interface_variables(const struct gl_context
*ctx
,
3906 struct gl_shader_program
*shProg
,
3907 struct set
*resource_set
,
3908 unsigned stage
, GLenum programInterface
)
3910 exec_list
*ir
= shProg
->_LinkedShaders
[stage
]->ir
;
3912 foreach_in_list(ir_instruction
, node
, ir
) {
3913 ir_variable
*var
= node
->as_variable();
3915 if (!var
|| var
->data
.how_declared
== ir_var_hidden
)
3920 switch (var
->data
.mode
) {
3921 case ir_var_system_value
:
3922 case ir_var_shader_in
:
3923 if (programInterface
!= GL_PROGRAM_INPUT
)
3925 loc_bias
= (stage
== MESA_SHADER_VERTEX
) ? int(VERT_ATTRIB_GENERIC0
)
3926 : int(VARYING_SLOT_VAR0
);
3928 case ir_var_shader_out
:
3929 if (programInterface
!= GL_PROGRAM_OUTPUT
)
3931 loc_bias
= (stage
== MESA_SHADER_FRAGMENT
) ? int(FRAG_RESULT_DATA0
)
3932 : int(VARYING_SLOT_VAR0
);
3938 if (var
->data
.patch
)
3939 loc_bias
= int(VARYING_SLOT_PATCH0
);
3941 /* Skip packed varyings, packed varyings are handled separately
3942 * by add_packed_varyings.
3944 if (strncmp(var
->name
, "packed:", 7) == 0)
3947 /* Skip fragdata arrays, these are handled separately
3948 * by add_fragdata_arrays.
3950 if (strncmp(var
->name
, "gl_out_FragData", 15) == 0)
3953 const bool vs_input_or_fs_output
=
3954 (stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
3955 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
);
3957 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3958 1 << stage
, programInterface
,
3959 var
, var
->name
, var
->type
, vs_input_or_fs_output
,
3960 var
->data
.location
- loc_bias
,
3961 inout_has_same_location(var
, stage
)))
3968 add_packed_varyings(const struct gl_context
*ctx
,
3969 struct gl_shader_program
*shProg
,
3970 struct set
*resource_set
,
3971 int stage
, GLenum type
)
3973 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[stage
];
3976 if (!sh
|| !sh
->packed_varyings
)
3979 foreach_in_list(ir_instruction
, node
, sh
->packed_varyings
) {
3980 ir_variable
*var
= node
->as_variable();
3982 switch (var
->data
.mode
) {
3983 case ir_var_shader_in
:
3984 iface
= GL_PROGRAM_INPUT
;
3986 case ir_var_shader_out
:
3987 iface
= GL_PROGRAM_OUTPUT
;
3990 unreachable("unexpected type");
3993 if (type
== iface
) {
3994 const int stage_mask
=
3995 build_stageref(shProg
, var
->name
, var
->data
.mode
);
3996 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3998 iface
, var
, var
->name
, var
->type
, false,
3999 var
->data
.location
- VARYING_SLOT_VAR0
,
4000 inout_has_same_location(var
, stage
)))
4009 add_fragdata_arrays(const struct gl_context
*ctx
,
4010 struct gl_shader_program
*shProg
,
4011 struct set
*resource_set
)
4013 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4015 if (!sh
|| !sh
->fragdata_arrays
)
4018 foreach_in_list(ir_instruction
, node
, sh
->fragdata_arrays
) {
4019 ir_variable
*var
= node
->as_variable();
4021 assert(var
->data
.mode
== ir_var_shader_out
);
4023 if (!add_shader_variable(ctx
, shProg
, resource_set
,
4024 1 << MESA_SHADER_FRAGMENT
,
4025 GL_PROGRAM_OUTPUT
, var
, var
->name
, var
->type
,
4026 true, var
->data
.location
- FRAG_RESULT_DATA0
,
4035 * Builds up a list of program resources that point to existing
4039 build_program_resource_list(struct gl_context
*ctx
,
4040 struct gl_shader_program
*shProg
,
4041 bool add_packed_varyings_only
)
4043 /* Rebuild resource list. */
4044 if (shProg
->data
->ProgramResourceList
) {
4045 ralloc_free(shProg
->data
->ProgramResourceList
);
4046 shProg
->data
->ProgramResourceList
= NULL
;
4047 shProg
->data
->NumProgramResourceList
= 0;
4050 int input_stage
= MESA_SHADER_STAGES
, output_stage
= 0;
4052 /* Determine first input and final output stage. These are used to
4053 * detect which variables should be enumerated in the resource list
4054 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
4056 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4057 if (!shProg
->_LinkedShaders
[i
])
4059 if (input_stage
== MESA_SHADER_STAGES
)
4064 /* Empty shader, no resources. */
4065 if (input_stage
== MESA_SHADER_STAGES
&& output_stage
== 0)
4068 struct set
*resource_set
= _mesa_pointer_set_create(NULL
);
4070 /* Program interface needs to expose varyings in case of SSO. */
4071 if (shProg
->SeparateShader
) {
4072 if (!add_packed_varyings(ctx
, shProg
, resource_set
,
4073 input_stage
, GL_PROGRAM_INPUT
))
4076 if (!add_packed_varyings(ctx
, shProg
, resource_set
,
4077 output_stage
, GL_PROGRAM_OUTPUT
))
4081 if (add_packed_varyings_only
) {
4082 _mesa_set_destroy(resource_set
, NULL
);
4086 if (!add_fragdata_arrays(ctx
, shProg
, resource_set
))
4089 /* Add inputs and outputs to the resource list. */
4090 if (!add_interface_variables(ctx
, shProg
, resource_set
,
4091 input_stage
, GL_PROGRAM_INPUT
))
4094 if (!add_interface_variables(ctx
, shProg
, resource_set
,
4095 output_stage
, GL_PROGRAM_OUTPUT
))
4098 if (shProg
->last_vert_prog
) {
4099 struct gl_transform_feedback_info
*linked_xfb
=
4100 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
4102 /* Add transform feedback varyings. */
4103 if (linked_xfb
->NumVarying
> 0) {
4104 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
4105 if (!link_util_add_program_resource(shProg
, resource_set
,
4106 GL_TRANSFORM_FEEDBACK_VARYING
,
4107 &linked_xfb
->Varyings
[i
], 0))
4112 /* Add transform feedback buffers. */
4113 for (unsigned i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
4114 if ((linked_xfb
->ActiveBuffers
>> i
) & 1) {
4115 linked_xfb
->Buffers
[i
].Binding
= i
;
4116 if (!link_util_add_program_resource(shProg
, resource_set
,
4117 GL_TRANSFORM_FEEDBACK_BUFFER
,
4118 &linked_xfb
->Buffers
[i
], 0))
4124 int top_level_array_base_offset
= -1;
4125 int top_level_array_size_in_bytes
= -1;
4126 int second_element_offset
= -1;
4127 int buffer_block_index
= -1;
4129 /* Add uniforms from uniform storage. */
4130 for (unsigned i
= 0; i
< shProg
->data
->NumUniformStorage
; i
++) {
4131 /* Do not add uniforms internally used by Mesa. */
4132 if (shProg
->data
->UniformStorage
[i
].hidden
)
4135 bool is_shader_storage
=
4136 shProg
->data
->UniformStorage
[i
].is_shader_storage
;
4137 GLenum type
= is_shader_storage
? GL_BUFFER_VARIABLE
: GL_UNIFORM
;
4138 if (!link_util_should_add_buffer_variable(shProg
,
4139 &shProg
->data
->UniformStorage
[i
],
4140 top_level_array_base_offset
,
4141 top_level_array_size_in_bytes
,
4142 second_element_offset
,
4143 buffer_block_index
))
4146 if (is_shader_storage
) {
4147 /* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources:
4149 * "For an active shader storage block member declared as an array
4150 * of an aggregate type, an entry will be generated only for the
4151 * first array element, regardless of its type. Such block members
4152 * are referred to as top-level arrays. If the block member is an
4153 * aggregate type, the enumeration rules are then applied
4156 * Below we update our tracking values used by
4157 * link_util_should_add_buffer_variable(). We only want to reset the
4158 * offsets once we have moved past the first element.
4160 if (shProg
->data
->UniformStorage
[i
].offset
>= second_element_offset
) {
4161 top_level_array_base_offset
=
4162 shProg
->data
->UniformStorage
[i
].offset
;
4164 top_level_array_size_in_bytes
=
4165 shProg
->data
->UniformStorage
[i
].top_level_array_size
*
4166 shProg
->data
->UniformStorage
[i
].top_level_array_stride
;
4168 /* Set or reset the second element offset. For non arrays this
4169 * will be set to -1.
4171 second_element_offset
= top_level_array_size_in_bytes
?
4172 top_level_array_base_offset
+
4173 shProg
->data
->UniformStorage
[i
].top_level_array_stride
: -1;
4176 buffer_block_index
= shProg
->data
->UniformStorage
[i
].block_index
;
4179 uint8_t stageref
= shProg
->data
->UniformStorage
[i
].active_shader_mask
;
4180 if (!link_util_add_program_resource(shProg
, resource_set
, type
,
4181 &shProg
->data
->UniformStorage
[i
], stageref
))
4185 /* Add program uniform blocks. */
4186 for (unsigned i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
4187 if (!link_util_add_program_resource(shProg
, resource_set
, GL_UNIFORM_BLOCK
,
4188 &shProg
->data
->UniformBlocks
[i
], 0))
4192 /* Add program shader storage blocks. */
4193 for (unsigned i
= 0; i
< shProg
->data
->NumShaderStorageBlocks
; i
++) {
4194 if (!link_util_add_program_resource(shProg
, resource_set
, GL_SHADER_STORAGE_BLOCK
,
4195 &shProg
->data
->ShaderStorageBlocks
[i
], 0))
4199 /* Add atomic counter buffers. */
4200 for (unsigned i
= 0; i
< shProg
->data
->NumAtomicBuffers
; i
++) {
4201 if (!link_util_add_program_resource(shProg
, resource_set
, GL_ATOMIC_COUNTER_BUFFER
,
4202 &shProg
->data
->AtomicBuffers
[i
], 0))
4206 for (unsigned i
= 0; i
< shProg
->data
->NumUniformStorage
; i
++) {
4208 if (!shProg
->data
->UniformStorage
[i
].hidden
)
4211 for (int j
= MESA_SHADER_VERTEX
; j
< MESA_SHADER_STAGES
; j
++) {
4212 if (!shProg
->data
->UniformStorage
[i
].opaque
[j
].active
||
4213 !shProg
->data
->UniformStorage
[i
].type
->is_subroutine())
4216 type
= _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage
)j
);
4217 /* add shader subroutines */
4218 if (!link_util_add_program_resource(shProg
, resource_set
,
4219 type
, &shProg
->data
->UniformStorage
[i
], 0))
4224 unsigned mask
= shProg
->data
->linked_stages
;
4226 const int i
= u_bit_scan(&mask
);
4227 struct gl_program
*p
= shProg
->_LinkedShaders
[i
]->Program
;
4229 GLuint type
= _mesa_shader_stage_to_subroutine((gl_shader_stage
)i
);
4230 for (unsigned j
= 0; j
< p
->sh
.NumSubroutineFunctions
; j
++) {
4231 if (!link_util_add_program_resource(shProg
, resource_set
,
4232 type
, &p
->sh
.SubroutineFunctions
[j
], 0))
4237 _mesa_set_destroy(resource_set
, NULL
);
4241 * This check is done to make sure we allow only constant expression
4242 * indexing and "constant-index-expression" (indexing with an expression
4243 * that includes loop induction variable).
4246 validate_sampler_array_indexing(struct gl_context
*ctx
,
4247 struct gl_shader_program
*prog
)
4249 dynamic_sampler_array_indexing_visitor v
;
4250 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4251 if (prog
->_LinkedShaders
[i
] == NULL
)
4254 bool no_dynamic_indexing
=
4255 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
;
4257 /* Search for array derefs in shader. */
4258 v
.run(prog
->_LinkedShaders
[i
]->ir
);
4259 if (v
.uses_dynamic_sampler_array_indexing()) {
4260 const char *msg
= "sampler arrays indexed with non-constant "
4261 "expressions is forbidden in GLSL %s %u";
4262 /* Backend has indicated that it has no dynamic indexing support. */
4263 if (no_dynamic_indexing
) {
4264 linker_error(prog
, msg
, prog
->IsES
? "ES" : "",
4265 prog
->data
->Version
);
4268 linker_warning(prog
, msg
, prog
->IsES
? "ES" : "",
4269 prog
->data
->Version
);
4277 link_assign_subroutine_types(struct gl_shader_program
*prog
)
4279 unsigned mask
= prog
->data
->linked_stages
;
4281 const int i
= u_bit_scan(&mask
);
4282 gl_program
*p
= prog
->_LinkedShaders
[i
]->Program
;
4284 p
->sh
.MaxSubroutineFunctionIndex
= 0;
4285 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
4286 ir_function
*fn
= node
->as_function();
4290 if (fn
->is_subroutine
)
4291 p
->sh
.NumSubroutineUniformTypes
++;
4293 if (!fn
->num_subroutine_types
)
4296 /* these should have been calculated earlier. */
4297 assert(fn
->subroutine_index
!= -1);
4298 if (p
->sh
.NumSubroutineFunctions
+ 1 > MAX_SUBROUTINES
) {
4299 linker_error(prog
, "Too many subroutine functions declared.\n");
4302 p
->sh
.SubroutineFunctions
= reralloc(p
, p
->sh
.SubroutineFunctions
,
4303 struct gl_subroutine_function
,
4304 p
->sh
.NumSubroutineFunctions
+ 1);
4305 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].name
= ralloc_strdup(p
, fn
->name
);
4306 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].num_compat_types
= fn
->num_subroutine_types
;
4307 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].types
=
4308 ralloc_array(p
, const struct glsl_type
*,
4309 fn
->num_subroutine_types
);
4311 /* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the
4314 * "Each subroutine with an index qualifier in the shader must be
4315 * given a unique index, otherwise a compile or link error will be
4318 for (unsigned j
= 0; j
< p
->sh
.NumSubroutineFunctions
; j
++) {
4319 if (p
->sh
.SubroutineFunctions
[j
].index
!= -1 &&
4320 p
->sh
.SubroutineFunctions
[j
].index
== fn
->subroutine_index
) {
4321 linker_error(prog
, "each subroutine index qualifier in the "
4322 "shader must be unique\n");
4326 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].index
=
4327 fn
->subroutine_index
;
4329 if (fn
->subroutine_index
> (int)p
->sh
.MaxSubroutineFunctionIndex
)
4330 p
->sh
.MaxSubroutineFunctionIndex
= fn
->subroutine_index
;
4332 for (int j
= 0; j
< fn
->num_subroutine_types
; j
++)
4333 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].types
[j
] = fn
->subroutine_types
[j
];
4334 p
->sh
.NumSubroutineFunctions
++;
4340 verify_subroutine_associated_funcs(struct gl_shader_program
*prog
)
4342 unsigned mask
= prog
->data
->linked_stages
;
4344 const int i
= u_bit_scan(&mask
);
4345 gl_program
*p
= prog
->_LinkedShaders
[i
]->Program
;
4346 glsl_symbol_table
*symbols
= prog
->_LinkedShaders
[i
]->symbols
;
4348 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
4350 * "A program will fail to compile or link if any shader
4351 * or stage contains two or more functions with the same
4352 * name if the name is associated with a subroutine type."
4354 for (unsigned j
= 0; j
< p
->sh
.NumSubroutineFunctions
; j
++) {
4355 unsigned definitions
= 0;
4356 char *name
= p
->sh
.SubroutineFunctions
[j
].name
;
4357 ir_function
*fn
= symbols
->get_function(name
);
4359 /* Calculate number of function definitions with the same name */
4360 foreach_in_list(ir_function_signature
, sig
, &fn
->signatures
) {
4361 if (sig
->is_defined
) {
4362 if (++definitions
> 1) {
4363 linker_error(prog
, "%s shader contains two or more function "
4364 "definitions with name `%s', which is "
4365 "associated with a subroutine type.\n",
4366 _mesa_shader_stage_to_string(i
),
4378 set_always_active_io(exec_list
*ir
, ir_variable_mode io_mode
)
4380 assert(io_mode
== ir_var_shader_in
|| io_mode
== ir_var_shader_out
);
4382 foreach_in_list(ir_instruction
, node
, ir
) {
4383 ir_variable
*const var
= node
->as_variable();
4385 if (var
== NULL
|| var
->data
.mode
!= io_mode
)
4388 /* Don't set always active on builtins that haven't been redeclared */
4389 if (var
->data
.how_declared
== ir_var_declared_implicitly
)
4392 var
->data
.always_active_io
= true;
4397 * When separate shader programs are enabled, only input/outputs between
4398 * the stages of a multi-stage separate program can be safely removed
4399 * from the shader interface. Other inputs/outputs must remain active.
4402 disable_varying_optimizations_for_sso(struct gl_shader_program
*prog
)
4404 unsigned first
, last
;
4405 assert(prog
->SeparateShader
);
4407 first
= MESA_SHADER_STAGES
;
4410 /* Determine first and last stage. Excluding the compute stage */
4411 for (unsigned i
= 0; i
< MESA_SHADER_COMPUTE
; i
++) {
4412 if (!prog
->_LinkedShaders
[i
])
4414 if (first
== MESA_SHADER_STAGES
)
4419 if (first
== MESA_SHADER_STAGES
)
4422 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4423 gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
4427 /* Prevent the removal of inputs to the first and outputs from the last
4428 * stage, unless they are the initial pipeline inputs or final pipeline
4429 * outputs, respectively.
4431 * The removal of IO between shaders in the same program is always
4434 if (stage
== first
&& stage
!= MESA_SHADER_VERTEX
)
4435 set_always_active_io(sh
->ir
, ir_var_shader_in
);
4436 if (stage
== last
&& stage
!= MESA_SHADER_FRAGMENT
)
4437 set_always_active_io(sh
->ir
, ir_var_shader_out
);
4442 link_and_validate_uniforms(struct gl_context
*ctx
,
4443 struct gl_shader_program
*prog
)
4445 assert(!ctx
->Const
.UseNIRGLSLLinker
);
4447 update_array_sizes(prog
);
4448 link_assign_uniform_locations(prog
, ctx
);
4450 if (prog
->data
->LinkStatus
== LINKING_FAILURE
)
4453 link_util_calculate_subroutine_compat(prog
);
4454 link_util_check_uniform_resources(ctx
, prog
);
4455 link_util_check_subroutine_resources(prog
);
4456 check_image_resources(ctx
, prog
);
4457 link_assign_atomic_counter_resources(ctx
, prog
);
4458 link_check_atomic_counter_resources(ctx
, prog
);
4462 link_varyings_and_uniforms(unsigned first
, unsigned last
,
4463 struct gl_context
*ctx
,
4464 struct gl_shader_program
*prog
, void *mem_ctx
)
4466 /* Mark all generic shader inputs and outputs as unpaired. */
4467 for (unsigned i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4468 if (prog
->_LinkedShaders
[i
] != NULL
) {
4469 link_invalidate_variable_locations(prog
->_LinkedShaders
[i
]->ir
);
4473 unsigned prev
= first
;
4474 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4475 if (prog
->_LinkedShaders
[i
] == NULL
)
4478 match_explicit_outputs_to_inputs(prog
->_LinkedShaders
[prev
],
4479 prog
->_LinkedShaders
[i
]);
4483 if (!assign_attribute_or_color_locations(mem_ctx
, prog
, &ctx
->Const
,
4484 MESA_SHADER_VERTEX
, true)) {
4488 if (!assign_attribute_or_color_locations(mem_ctx
, prog
, &ctx
->Const
,
4489 MESA_SHADER_FRAGMENT
, true)) {
4493 prog
->last_vert_prog
= NULL
;
4494 for (int i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
4495 if (prog
->_LinkedShaders
[i
] == NULL
)
4498 prog
->last_vert_prog
= prog
->_LinkedShaders
[i
]->Program
;
4502 if (!link_varyings(prog
, first
, last
, ctx
, mem_ctx
))
4505 if (!ctx
->Const
.UseNIRGLSLLinker
)
4506 link_and_validate_uniforms(ctx
, prog
);
4508 if (!prog
->data
->LinkStatus
)
4511 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4512 if (prog
->_LinkedShaders
[i
] == NULL
)
4515 const struct gl_shader_compiler_options
*options
=
4516 &ctx
->Const
.ShaderCompilerOptions
[i
];
4518 if (options
->LowerBufferInterfaceBlocks
)
4519 lower_ubo_reference(prog
->_LinkedShaders
[i
],
4520 options
->ClampBlockIndicesToArrayBounds
,
4521 ctx
->Const
.UseSTD430AsDefaultPacking
);
4523 if (i
== MESA_SHADER_COMPUTE
)
4524 lower_shared_reference(ctx
, prog
, prog
->_LinkedShaders
[i
]);
4526 lower_vector_derefs(prog
->_LinkedShaders
[i
]);
4527 do_vec_index_to_swizzle(prog
->_LinkedShaders
[i
]->ir
);
4534 linker_optimisation_loop(struct gl_context
*ctx
, exec_list
*ir
,
4537 if (ctx
->Const
.GLSLOptimizeConservatively
) {
4538 /* Run it just once. */
4539 do_common_optimization(ir
, true, false,
4540 &ctx
->Const
.ShaderCompilerOptions
[stage
],
4541 ctx
->Const
.NativeIntegers
);
4543 /* Repeat it until it stops making changes. */
4544 while (do_common_optimization(ir
, true, false,
4545 &ctx
->Const
.ShaderCompilerOptions
[stage
],
4546 ctx
->Const
.NativeIntegers
))
4552 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4554 prog
->data
->LinkStatus
= LINKING_SUCCESS
; /* All error paths will set this to false */
4555 prog
->data
->Validated
= false;
4557 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec says:
4559 * "Linking can fail for a variety of reasons as specified in the
4560 * OpenGL Shading Language Specification, as well as any of the
4561 * following reasons:
4563 * - No shader objects are attached to program."
4565 * The Compatibility Profile specification does not list the error. In
4566 * Compatibility Profile missing shader stages are replaced by
4567 * fixed-function. This applies to the case where all stages are
4570 if (prog
->NumShaders
== 0) {
4571 if (ctx
->API
!= API_OPENGL_COMPAT
)
4572 linker_error(prog
, "no shaders attached to the program\n");
4576 #ifdef ENABLE_SHADER_CACHE
4577 if (shader_cache_read_program_metadata(ctx
, prog
))
4581 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
4583 prog
->ARB_fragment_coord_conventions_enable
= false;
4585 /* Separate the shaders into groups based on their type.
4587 struct gl_shader
**shader_list
[MESA_SHADER_STAGES
];
4588 unsigned num_shaders
[MESA_SHADER_STAGES
];
4590 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4591 shader_list
[i
] = (struct gl_shader
**)
4592 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
4596 unsigned min_version
= UINT_MAX
;
4597 unsigned max_version
= 0;
4598 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
4599 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
4600 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
4602 if (!ctx
->Const
.AllowGLSLRelaxedES
&&
4603 prog
->Shaders
[i
]->IsES
!= prog
->Shaders
[0]->IsES
) {
4604 linker_error(prog
, "all shaders must use same shading "
4605 "language version\n");
4609 if (prog
->Shaders
[i
]->ARB_fragment_coord_conventions_enable
) {
4610 prog
->ARB_fragment_coord_conventions_enable
= true;
4613 gl_shader_stage shader_type
= prog
->Shaders
[i
]->Stage
;
4614 shader_list
[shader_type
][num_shaders
[shader_type
]] = prog
->Shaders
[i
];
4615 num_shaders
[shader_type
]++;
4618 /* In desktop GLSL, different shader versions may be linked together. In
4619 * GLSL ES, all shader versions must be the same.
4621 if (!ctx
->Const
.AllowGLSLRelaxedES
&& prog
->Shaders
[0]->IsES
&&
4622 min_version
!= max_version
) {
4623 linker_error(prog
, "all shaders must use same shading "
4624 "language version\n");
4628 prog
->data
->Version
= max_version
;
4629 prog
->IsES
= prog
->Shaders
[0]->IsES
;
4631 /* Some shaders have to be linked with some other shaders present.
4633 if (!prog
->SeparateShader
) {
4634 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0 &&
4635 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4636 linker_error(prog
, "Geometry shader must be linked with "
4640 if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0 &&
4641 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4642 linker_error(prog
, "Tessellation evaluation shader must be linked "
4643 "with vertex shader\n");
4646 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4647 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4648 linker_error(prog
, "Tessellation control shader must be linked with "
4653 /* Section 7.3 of the OpenGL ES 3.2 specification says:
4655 * "Linking can fail for [...] any of the following reasons:
4657 * * program contains an object to form a tessellation control
4658 * shader [...] and [...] the program is not separable and
4659 * contains no object to form a tessellation evaluation shader"
4661 * The OpenGL spec is contradictory. It allows linking without a tess
4662 * eval shader, but that can only be used with transform feedback and
4663 * rasterization disabled. However, transform feedback isn't allowed
4664 * with GL_PATCHES, so it can't be used.
4666 * More investigation showed that the idea of transform feedback after
4667 * a tess control shader was dropped, because some hw vendors couldn't
4668 * support tessellation without a tess eval shader, but the linker
4669 * section wasn't updated to reflect that.
4671 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
4674 * Do what's reasonable and always require a tess eval shader if a tess
4675 * control shader is present.
4677 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4678 num_shaders
[MESA_SHADER_TESS_EVAL
] == 0) {
4679 linker_error(prog
, "Tessellation control shader must be linked with "
4680 "tessellation evaluation shader\n");
4685 if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0 &&
4686 num_shaders
[MESA_SHADER_TESS_CTRL
] == 0) {
4687 linker_error(prog
, "GLSL ES requires non-separable programs "
4688 "containing a tessellation evaluation shader to also "
4689 "be linked with a tessellation control shader\n");
4695 /* Compute shaders have additional restrictions. */
4696 if (num_shaders
[MESA_SHADER_COMPUTE
] > 0 &&
4697 num_shaders
[MESA_SHADER_COMPUTE
] != prog
->NumShaders
) {
4698 linker_error(prog
, "Compute shaders may not be linked with any other "
4699 "type of shader\n");
4702 /* Link all shaders for a particular stage and validate the result.
4704 for (int stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4705 if (num_shaders
[stage
] > 0) {
4706 gl_linked_shader
*const sh
=
4707 link_intrastage_shaders(mem_ctx
, ctx
, prog
, shader_list
[stage
],
4708 num_shaders
[stage
], false);
4710 if (!prog
->data
->LinkStatus
) {
4712 _mesa_delete_linked_shader(ctx
, sh
);
4717 case MESA_SHADER_VERTEX
:
4718 validate_vertex_shader_executable(prog
, sh
, ctx
);
4720 case MESA_SHADER_TESS_CTRL
:
4721 /* nothing to be done */
4723 case MESA_SHADER_TESS_EVAL
:
4724 validate_tess_eval_shader_executable(prog
, sh
, ctx
);
4726 case MESA_SHADER_GEOMETRY
:
4727 validate_geometry_shader_executable(prog
, sh
, ctx
);
4729 case MESA_SHADER_FRAGMENT
:
4730 validate_fragment_shader_executable(prog
, sh
);
4733 if (!prog
->data
->LinkStatus
) {
4735 _mesa_delete_linked_shader(ctx
, sh
);
4739 prog
->_LinkedShaders
[stage
] = sh
;
4740 prog
->data
->linked_stages
|= 1 << stage
;
4744 /* Here begins the inter-stage linking phase. Some initial validation is
4745 * performed, then locations are assigned for uniforms, attributes, and
4748 cross_validate_uniforms(ctx
, prog
);
4749 if (!prog
->data
->LinkStatus
)
4752 unsigned first
, last
, prev
;
4754 first
= MESA_SHADER_STAGES
;
4757 /* Determine first and last stage. */
4758 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4759 if (!prog
->_LinkedShaders
[i
])
4761 if (first
== MESA_SHADER_STAGES
)
4766 check_explicit_uniform_locations(ctx
, prog
);
4767 link_assign_subroutine_types(prog
);
4768 verify_subroutine_associated_funcs(prog
);
4770 if (!prog
->data
->LinkStatus
)
4773 resize_tes_inputs(ctx
, prog
);
4775 /* Validate the inputs of each stage with the output of the preceding
4779 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4780 if (prog
->_LinkedShaders
[i
] == NULL
)
4783 validate_interstage_inout_blocks(prog
, prog
->_LinkedShaders
[prev
],
4784 prog
->_LinkedShaders
[i
]);
4785 if (!prog
->data
->LinkStatus
)
4788 cross_validate_outputs_to_inputs(ctx
, prog
,
4789 prog
->_LinkedShaders
[prev
],
4790 prog
->_LinkedShaders
[i
]);
4791 if (!prog
->data
->LinkStatus
)
4797 /* The cross validation of outputs/inputs above validates interstage
4798 * explicit locations. We need to do this also for the inputs in the first
4799 * stage and outputs of the last stage included in the program, since there
4800 * is no cross validation for these.
4802 validate_first_and_last_interface_explicit_locations(ctx
, prog
,
4803 (gl_shader_stage
) first
,
4804 (gl_shader_stage
) last
);
4806 /* Cross-validate uniform blocks between shader stages */
4807 validate_interstage_uniform_blocks(prog
, prog
->_LinkedShaders
);
4808 if (!prog
->data
->LinkStatus
)
4811 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4812 if (prog
->_LinkedShaders
[i
] != NULL
)
4813 lower_named_interface_blocks(mem_ctx
, prog
->_LinkedShaders
[i
]);
4816 if (prog
->IsES
&& prog
->data
->Version
== 100)
4817 if (!validate_invariant_builtins(prog
,
4818 prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
4819 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]))
4822 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
4823 * it before optimization because we want most of the checks to get
4824 * dropped thanks to constant propagation.
4826 * This rule also applies to GLSL ES 3.00.
4828 if (max_version
>= (prog
->IsES
? 300 : 130)) {
4829 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4831 lower_discard_flow(sh
->ir
);
4835 if (prog
->SeparateShader
)
4836 disable_varying_optimizations_for_sso(prog
);
4839 if (!interstage_cross_validate_uniform_blocks(prog
, false))
4843 if (!interstage_cross_validate_uniform_blocks(prog
, true))
4846 /* Do common optimization before assigning storage for attributes,
4847 * uniforms, and varyings. Later optimization could possibly make
4848 * some of that unused.
4850 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4851 if (prog
->_LinkedShaders
[i
] == NULL
)
4854 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
4855 if (!prog
->data
->LinkStatus
)
4858 if (ctx
->Const
.ShaderCompilerOptions
[i
].LowerCombinedClipCullDistance
) {
4859 lower_clip_cull_distance(prog
, prog
->_LinkedShaders
[i
]);
4862 if (ctx
->Const
.LowerTessLevel
) {
4863 lower_tess_level(prog
->_LinkedShaders
[i
]);
4866 /* Section 13.46 (Vertex Attribute Aliasing) of the OpenGL ES 3.2
4867 * specification says:
4869 * "In general, the behavior of GLSL ES should not depend on compiler
4870 * optimizations which might be implementation-dependent. Name matching
4871 * rules in most languages, including C++ from which GLSL ES is derived,
4872 * are based on declarations rather than use.
4874 * RESOLUTION: The existence of aliasing is determined by declarations
4875 * present after preprocessing."
4877 * Because of this rule, we do a 'dry-run' of attribute assignment for
4878 * vertex shader inputs here.
4880 if (prog
->IsES
&& i
== MESA_SHADER_VERTEX
) {
4881 if (!assign_attribute_or_color_locations(mem_ctx
, prog
, &ctx
->Const
,
4882 MESA_SHADER_VERTEX
, false)) {
4887 /* Call opts before lowering const arrays to uniforms so we can const
4888 * propagate any elements accessed directly.
4890 linker_optimisation_loop(ctx
, prog
->_LinkedShaders
[i
]->ir
, i
);
4892 /* Call opts after lowering const arrays to copy propagate things. */
4893 if (ctx
->Const
.GLSLLowerConstArrays
&&
4894 lower_const_arrays_to_uniforms(prog
->_LinkedShaders
[i
]->ir
, i
,
4895 ctx
->Const
.Program
[i
].MaxUniformComponents
))
4896 linker_optimisation_loop(ctx
, prog
->_LinkedShaders
[i
]->ir
, i
);
4900 /* Validation for special cases where we allow sampler array indexing
4901 * with loop induction variable. This check emits a warning or error
4902 * depending if backend can handle dynamic indexing.
4904 if ((!prog
->IsES
&& prog
->data
->Version
< 130) ||
4905 (prog
->IsES
&& prog
->data
->Version
< 300)) {
4906 if (!validate_sampler_array_indexing(ctx
, prog
))
4910 /* Check and validate stream emissions in geometry shaders */
4911 validate_geometry_shader_emissions(ctx
, prog
);
4913 store_fragdepth_layout(prog
);
4915 if(!link_varyings_and_uniforms(first
, last
, ctx
, prog
, mem_ctx
))
4918 /* Linking varyings can cause some extra, useless swizzles to be generated
4919 * due to packing and unpacking.
4921 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4922 if (prog
->_LinkedShaders
[i
] == NULL
)
4925 optimize_swizzles(prog
->_LinkedShaders
[i
]->ir
);
4928 /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both
4929 * be present in a linked program. GL_ARB_ES2_compatibility doesn't say
4930 * anything about shader linking when one of the shaders (vertex or
4931 * fragment shader) is absent. So, the extension shouldn't change the
4932 * behavior specified in GLSL specification.
4934 * From OpenGL ES 3.1 specification (7.3 Program Objects):
4935 * "Linking can fail for a variety of reasons as specified in the
4936 * OpenGL ES Shading Language Specification, as well as any of the
4937 * following reasons:
4941 * * program contains objects to form either a vertex shader or
4942 * fragment shader, and program is not separable, and does not
4943 * contain objects to form both a vertex shader and fragment
4946 * However, the only scenario in 3.1+ where we don't require them both is
4947 * when we have a compute shader. For example:
4949 * - No shaders is a link error.
4950 * - Geom or Tess without a Vertex shader is a link error which means we
4951 * always require a Vertex shader and hence a Fragment shader.
4952 * - Finally a Compute shader linked with any other stage is a link error.
4954 if (!prog
->SeparateShader
&& ctx
->API
== API_OPENGLES2
&&
4955 num_shaders
[MESA_SHADER_COMPUTE
] == 0) {
4956 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
4957 linker_error(prog
, "program lacks a vertex shader\n");
4958 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
4959 linker_error(prog
, "program lacks a fragment shader\n");
4964 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4965 free(shader_list
[i
]);
4966 if (prog
->_LinkedShaders
[i
] == NULL
)
4969 /* Do a final validation step to make sure that the IR wasn't
4970 * invalidated by any modifications performed after intrastage linking.
4972 validate_ir_tree(prog
->_LinkedShaders
[i
]->ir
);
4974 /* Retain any live IR, but trash the rest. */
4975 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
4977 /* The symbol table in the linked shaders may contain references to
4978 * variables that were removed (e.g., unused uniforms). Since it may
4979 * contain junk, there is no possible valid use. Delete it and set the
4982 delete prog
->_LinkedShaders
[i
]->symbols
;
4983 prog
->_LinkedShaders
[i
]->symbols
= NULL
;
4986 ralloc_free(mem_ctx
);