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),
335 uses_non_zero_stream(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;
357 uses_non_zero_stream
= true;
359 return visit_continue
;
362 virtual ir_visitor_status
visit_leave(ir_end_primitive
*ir
)
364 end_primitive_found
= true;
366 int stream_id
= ir
->stream_id();
369 invalid_stream_id
= stream_id
;
370 invalid_stream_id_from_emit_vertex
= false;
374 if (stream_id
> max_stream_allowed
) {
375 invalid_stream_id
= stream_id
;
376 invalid_stream_id_from_emit_vertex
= false;
381 uses_non_zero_stream
= true;
383 return visit_continue
;
388 return invalid_stream_id
!= 0;
391 const char *error_func()
393 return invalid_stream_id_from_emit_vertex
?
394 "EmitStreamVertex" : "EndStreamPrimitive";
399 return invalid_stream_id
;
404 return uses_non_zero_stream
;
407 bool uses_end_primitive()
409 return end_primitive_found
;
413 int max_stream_allowed
;
414 int invalid_stream_id
;
415 bool invalid_stream_id_from_emit_vertex
;
416 bool end_primitive_found
;
417 bool uses_non_zero_stream
;
420 /* Class that finds array derefs and check if indexes are dynamic. */
421 class dynamic_sampler_array_indexing_visitor
: public ir_hierarchical_visitor
424 dynamic_sampler_array_indexing_visitor() :
425 dynamic_sampler_array_indexing(false)
429 ir_visitor_status
visit_enter(ir_dereference_array
*ir
)
431 if (!ir
->variable_referenced())
432 return visit_continue
;
434 if (!ir
->variable_referenced()->type
->contains_sampler())
435 return visit_continue
;
437 if (!ir
->array_index
->constant_expression_value(ralloc_parent(ir
))) {
438 dynamic_sampler_array_indexing
= true;
441 return visit_continue
;
444 bool uses_dynamic_sampler_array_indexing()
446 return dynamic_sampler_array_indexing
;
450 bool dynamic_sampler_array_indexing
;
453 } /* anonymous namespace */
456 linker_error(gl_shader_program
*prog
, const char *fmt
, ...)
460 ralloc_strcat(&prog
->data
->InfoLog
, "error: ");
462 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, ap
);
465 prog
->data
->LinkStatus
= LINKING_FAILURE
;
470 linker_warning(gl_shader_program
*prog
, const char *fmt
, ...)
474 ralloc_strcat(&prog
->data
->InfoLog
, "warning: ");
476 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, ap
);
483 * Given a string identifying a program resource, break it into a base name
484 * and an optional array index in square brackets.
486 * If an array index is present, \c out_base_name_end is set to point to the
487 * "[" that precedes the array index, and the array index itself is returned
490 * If no array index is present (or if the array index is negative or
491 * mal-formed), \c out_base_name_end, is set to point to the null terminator
492 * at the end of the input string, and -1 is returned.
494 * Only the final array index is parsed; if the string contains other array
495 * indices (or structure field accesses), they are left in the base name.
497 * No attempt is made to check that the base name is properly formed;
498 * typically the caller will look up the base name in a hash table, so
499 * ill-formed base names simply turn into hash table lookup failures.
502 parse_program_resource_name(const GLchar
*name
,
503 const GLchar
**out_base_name_end
)
505 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
507 * "When an integer array element or block instance number is part of
508 * the name string, it will be specified in decimal form without a "+"
509 * or "-" sign or any extra leading zeroes. Additionally, the name
510 * string will not include white space anywhere in the string."
513 const size_t len
= strlen(name
);
514 *out_base_name_end
= name
+ len
;
516 if (len
== 0 || name
[len
-1] != ']')
519 /* Walk backwards over the string looking for a non-digit character. This
520 * had better be the opening bracket for an array index.
522 * Initially, i specifies the location of the ']'. Since the string may
523 * contain only the ']' charcater, walk backwards very carefully.
526 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
529 if ((i
== 0) || name
[i
-1] != '[')
532 long array_index
= strtol(&name
[i
], NULL
, 10);
536 /* Check for leading zero */
537 if (name
[i
] == '0' && name
[i
+1] != ']')
540 *out_base_name_end
= name
+ (i
- 1);
546 link_invalidate_variable_locations(exec_list
*ir
)
548 foreach_in_list(ir_instruction
, node
, ir
) {
549 ir_variable
*const var
= node
->as_variable();
554 /* Only assign locations for variables that lack an explicit location.
555 * Explicit locations are set for all built-in variables, generic vertex
556 * shader inputs (via layout(location=...)), and generic fragment shader
557 * outputs (also via layout(location=...)).
559 if (!var
->data
.explicit_location
) {
560 var
->data
.location
= -1;
561 var
->data
.location_frac
= 0;
564 /* ir_variable::is_unmatched_generic_inout is used by the linker while
565 * connecting outputs from one stage to inputs of the next stage.
567 if (var
->data
.explicit_location
&&
568 var
->data
.location
< VARYING_SLOT_VAR0
) {
569 var
->data
.is_unmatched_generic_inout
= 0;
571 var
->data
.is_unmatched_generic_inout
= 1;
578 * Set clip_distance_array_size based and cull_distance_array_size on the given
581 * Also check for errors based on incorrect usage of gl_ClipVertex and
582 * gl_ClipDistance and gl_CullDistance.
583 * Additionally test whether the arrays gl_ClipDistance and gl_CullDistance
584 * exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances.
586 * Return false if an error was reported.
589 analyze_clip_cull_usage(struct gl_shader_program
*prog
,
590 struct gl_linked_shader
*shader
,
591 struct gl_context
*ctx
,
592 struct shader_info
*info
)
594 info
->clip_distance_array_size
= 0;
595 info
->cull_distance_array_size
= 0;
597 if (prog
->data
->Version
>= (prog
->IsES
? 300 : 130)) {
598 /* From section 7.1 (Vertex Shader Special Variables) of the
601 * "It is an error for a shader to statically write both
602 * gl_ClipVertex and gl_ClipDistance."
604 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
605 * gl_ClipVertex nor gl_ClipDistance. However with
606 * GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0.
608 find_variable
gl_ClipDistance("gl_ClipDistance");
609 find_variable
gl_CullDistance("gl_CullDistance");
610 find_variable
gl_ClipVertex("gl_ClipVertex");
611 find_variable
* const variables
[] = {
614 !prog
->IsES
? &gl_ClipVertex
: NULL
,
617 find_assignments(shader
->ir
, variables
);
619 /* From the ARB_cull_distance spec:
621 * It is a compile-time or link-time error for the set of shaders forming
622 * a program to statically read or write both gl_ClipVertex and either
623 * gl_ClipDistance or gl_CullDistance.
625 * This does not apply to GLSL ES shaders, since GLSL ES doesn't define
629 if (gl_ClipVertex
.found
&& gl_ClipDistance
.found
) {
630 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
631 "and `gl_ClipDistance'\n",
632 _mesa_shader_stage_to_string(shader
->Stage
));
635 if (gl_ClipVertex
.found
&& gl_CullDistance
.found
) {
636 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
637 "and `gl_CullDistance'\n",
638 _mesa_shader_stage_to_string(shader
->Stage
));
643 if (gl_ClipDistance
.found
) {
644 ir_variable
*clip_distance_var
=
645 shader
->symbols
->get_variable("gl_ClipDistance");
646 assert(clip_distance_var
);
647 info
->clip_distance_array_size
= clip_distance_var
->type
->length
;
649 if (gl_CullDistance
.found
) {
650 ir_variable
*cull_distance_var
=
651 shader
->symbols
->get_variable("gl_CullDistance");
652 assert(cull_distance_var
);
653 info
->cull_distance_array_size
= cull_distance_var
->type
->length
;
655 /* From the ARB_cull_distance spec:
657 * It is a compile-time or link-time error for the set of shaders forming
658 * a program to have the sum of the sizes of the gl_ClipDistance and
659 * gl_CullDistance arrays to be larger than
660 * gl_MaxCombinedClipAndCullDistances.
662 if ((uint32_t)(info
->clip_distance_array_size
+ info
->cull_distance_array_size
) >
663 ctx
->Const
.MaxClipPlanes
) {
664 linker_error(prog
, "%s shader: the combined size of "
665 "'gl_ClipDistance' and 'gl_CullDistance' size cannot "
667 "gl_MaxCombinedClipAndCullDistances (%u)",
668 _mesa_shader_stage_to_string(shader
->Stage
),
669 ctx
->Const
.MaxClipPlanes
);
676 * Verify that a vertex shader executable meets all semantic requirements.
678 * Also sets info.clip_distance_array_size and
679 * info.cull_distance_array_size as a side effect.
681 * \param shader Vertex shader executable to be verified
684 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
685 struct gl_linked_shader
*shader
,
686 struct gl_context
*ctx
)
691 /* From the GLSL 1.10 spec, page 48:
693 * "The variable gl_Position is available only in the vertex
694 * language and is intended for writing the homogeneous vertex
695 * position. All executions of a well-formed vertex shader
696 * executable must write a value into this variable. [...] The
697 * variable gl_Position is available only in the vertex
698 * language and is intended for writing the homogeneous vertex
699 * position. All executions of a well-formed vertex shader
700 * executable must write a value into this variable."
702 * while in GLSL 1.40 this text is changed to:
704 * "The variable gl_Position is available only in the vertex
705 * language and is intended for writing the homogeneous vertex
706 * position. It can be written at any time during shader
707 * execution. It may also be read back by a vertex shader
708 * after being written. This value will be used by primitive
709 * assembly, clipping, culling, and other fixed functionality
710 * operations, if present, that operate on primitives after
711 * vertex processing has occurred. Its value is undefined if
712 * the vertex shader executable does not write gl_Position."
714 * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
715 * gl_Position is not an error.
717 if (prog
->data
->Version
< (prog
->IsES
? 300 : 140)) {
718 find_variable
gl_Position("gl_Position");
719 find_assignments(shader
->ir
, &gl_Position
);
720 if (!gl_Position
.found
) {
723 "vertex shader does not write to `gl_Position'. "
724 "Its value is undefined. \n");
727 "vertex shader does not write to `gl_Position'. \n");
733 analyze_clip_cull_usage(prog
, shader
, ctx
, &shader
->Program
->info
);
737 validate_tess_eval_shader_executable(struct gl_shader_program
*prog
,
738 struct gl_linked_shader
*shader
,
739 struct gl_context
*ctx
)
744 analyze_clip_cull_usage(prog
, shader
, ctx
, &shader
->Program
->info
);
749 * Verify that a fragment shader executable meets all semantic requirements
751 * \param shader Fragment shader executable to be verified
754 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
755 struct gl_linked_shader
*shader
)
760 find_variable
gl_FragColor("gl_FragColor");
761 find_variable
gl_FragData("gl_FragData");
762 find_variable
* const variables
[] = { &gl_FragColor
, &gl_FragData
, NULL
};
763 find_assignments(shader
->ir
, variables
);
765 if (gl_FragColor
.found
&& gl_FragData
.found
) {
766 linker_error(prog
, "fragment shader writes to both "
767 "`gl_FragColor' and `gl_FragData'\n");
772 * Verify that a geometry shader executable meets all semantic requirements
774 * Also sets prog->Geom.VerticesIn, and info.clip_distance_array_sizeand
775 * info.cull_distance_array_size as a side effect.
777 * \param shader Geometry shader executable to be verified
780 validate_geometry_shader_executable(struct gl_shader_program
*prog
,
781 struct gl_linked_shader
*shader
,
782 struct gl_context
*ctx
)
787 unsigned num_vertices
=
788 vertices_per_prim(shader
->Program
->info
.gs
.input_primitive
);
789 prog
->Geom
.VerticesIn
= num_vertices
;
791 analyze_clip_cull_usage(prog
, shader
, ctx
, &shader
->Program
->info
);
795 * Check if geometry shaders emit to non-zero streams and do corresponding
799 validate_geometry_shader_emissions(struct gl_context
*ctx
,
800 struct gl_shader_program
*prog
)
802 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
805 find_emit_vertex_visitor
emit_vertex(ctx
->Const
.MaxVertexStreams
- 1);
806 emit_vertex
.run(sh
->ir
);
807 if (emit_vertex
.error()) {
808 linker_error(prog
, "Invalid call %s(%d). Accepted values for the "
809 "stream parameter are in the range [0, %d].\n",
810 emit_vertex
.error_func(),
811 emit_vertex
.error_stream(),
812 ctx
->Const
.MaxVertexStreams
- 1);
814 prog
->Geom
.UsesStreams
= emit_vertex
.uses_streams();
815 prog
->Geom
.UsesEndPrimitive
= emit_vertex
.uses_end_primitive();
817 /* From the ARB_gpu_shader5 spec:
819 * "Multiple vertex streams are supported only if the output primitive
820 * type is declared to be "points". A program will fail to link if it
821 * contains a geometry shader calling EmitStreamVertex() or
822 * EndStreamPrimitive() if its output primitive type is not "points".
824 * However, in the same spec:
826 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
827 * with <stream> set to zero."
831 * "The function EndPrimitive() is equivalent to calling
832 * EndStreamPrimitive() with <stream> set to zero."
834 * Since we can call EmitVertex() and EndPrimitive() when we output
835 * primitives other than points, calling EmitStreamVertex(0) or
836 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
837 * does. Currently we only set prog->Geom.UsesStreams to TRUE when
838 * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
841 if (prog
->Geom
.UsesStreams
&&
842 sh
->Program
->info
.gs
.output_primitive
!= GL_POINTS
) {
843 linker_error(prog
, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
844 "with n>0 requires point output\n");
850 validate_intrastage_arrays(struct gl_shader_program
*prog
,
851 ir_variable
*const var
,
852 ir_variable
*const existing
,
853 bool match_precision
)
855 /* Consider the types to be "the same" if both types are arrays
856 * of the same type and one of the arrays is implicitly sized.
857 * In addition, set the type of the linked variable to the
858 * explicitly sized array.
860 if (var
->type
->is_array() && existing
->type
->is_array()) {
861 const glsl_type
*no_array_var
= var
->type
->fields
.array
;
862 const glsl_type
*no_array_existing
= existing
->type
->fields
.array
;
865 type_matches
= (match_precision
?
866 no_array_var
== no_array_existing
:
867 no_array_var
->compare_no_precision(no_array_existing
));
870 ((var
->type
->length
== 0)|| (existing
->type
->length
== 0))) {
871 if (var
->type
->length
!= 0) {
872 if ((int)var
->type
->length
<= existing
->data
.max_array_access
) {
873 linker_error(prog
, "%s `%s' declared as type "
874 "`%s' but outermost dimension has an index"
877 var
->name
, var
->type
->name
,
878 existing
->data
.max_array_access
);
880 existing
->type
= var
->type
;
882 } else if (existing
->type
->length
!= 0) {
883 if((int)existing
->type
->length
<= var
->data
.max_array_access
&&
884 !existing
->data
.from_ssbo_unsized_array
) {
885 linker_error(prog
, "%s `%s' declared as type "
886 "`%s' but outermost dimension has an index"
889 var
->name
, existing
->type
->name
,
890 var
->data
.max_array_access
);
901 * Perform validation of global variables used across multiple shaders
904 cross_validate_globals(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
905 struct exec_list
*ir
, glsl_symbol_table
*variables
,
908 foreach_in_list(ir_instruction
, node
, ir
) {
909 ir_variable
*const var
= node
->as_variable();
914 if (uniforms_only
&& (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
))
917 /* don't cross validate subroutine uniforms */
918 if (var
->type
->contains_subroutine())
921 /* Don't cross validate interface instances. These are only relevant
922 * inside a shader. The cross validation is done at the Interface Block
925 if (var
->is_interface_instance())
928 /* Don't cross validate temporaries that are at global scope. These
929 * will eventually get pulled into the shaders 'main'.
931 if (var
->data
.mode
== ir_var_temporary
)
934 /* If a global with this name has already been seen, verify that the
935 * new instance has the same type. In addition, if the globals have
936 * initializers, the values of the initializers must be the same.
938 ir_variable
*const existing
= variables
->get_variable(var
->name
);
939 if (existing
!= NULL
) {
940 /* Check if types match. */
941 if (var
->type
!= existing
->type
) {
942 if (!validate_intrastage_arrays(prog
, var
, existing
)) {
943 /* If it is an unsized array in a Shader Storage Block,
944 * two different shaders can access to different elements.
945 * Because of that, they might be converted to different
946 * sized arrays, then check that they are compatible but
947 * ignore the array size.
949 if (!(var
->data
.mode
== ir_var_shader_storage
&&
950 var
->data
.from_ssbo_unsized_array
&&
951 existing
->data
.mode
== ir_var_shader_storage
&&
952 existing
->data
.from_ssbo_unsized_array
&&
953 var
->type
->gl_type
== existing
->type
->gl_type
)) {
954 linker_error(prog
, "%s `%s' declared as type "
955 "`%s' and type `%s'\n",
957 var
->name
, var
->type
->name
,
958 existing
->type
->name
);
964 if (var
->data
.explicit_location
) {
965 if (existing
->data
.explicit_location
966 && (var
->data
.location
!= existing
->data
.location
)) {
967 linker_error(prog
, "explicit locations for %s "
968 "`%s' have differing values\n",
969 mode_string(var
), var
->name
);
973 if (var
->data
.location_frac
!= existing
->data
.location_frac
) {
974 linker_error(prog
, "explicit components for %s `%s' have "
975 "differing values\n", mode_string(var
), var
->name
);
979 existing
->data
.location
= var
->data
.location
;
980 existing
->data
.explicit_location
= true;
982 /* Check if uniform with implicit location was marked explicit
983 * by earlier shader stage. If so, mark it explicit in this stage
984 * too to make sure later processing does not treat it as
987 if (existing
->data
.explicit_location
) {
988 var
->data
.location
= existing
->data
.location
;
989 var
->data
.explicit_location
= true;
993 /* From the GLSL 4.20 specification:
994 * "A link error will result if two compilation units in a program
995 * specify different integer-constant bindings for the same
996 * opaque-uniform name. However, it is not an error to specify a
997 * binding on some but not all declarations for the same name"
999 if (var
->data
.explicit_binding
) {
1000 if (existing
->data
.explicit_binding
&&
1001 var
->data
.binding
!= existing
->data
.binding
) {
1002 linker_error(prog
, "explicit bindings for %s "
1003 "`%s' have differing values\n",
1004 mode_string(var
), var
->name
);
1008 existing
->data
.binding
= var
->data
.binding
;
1009 existing
->data
.explicit_binding
= true;
1012 if (var
->type
->contains_atomic() &&
1013 var
->data
.offset
!= existing
->data
.offset
) {
1014 linker_error(prog
, "offset specifications for %s "
1015 "`%s' have differing values\n",
1016 mode_string(var
), var
->name
);
1020 /* Validate layout qualifiers for gl_FragDepth.
1022 * From the AMD/ARB_conservative_depth specs:
1024 * "If gl_FragDepth is redeclared in any fragment shader in a
1025 * program, it must be redeclared in all fragment shaders in
1026 * that program that have static assignments to
1027 * gl_FragDepth. All redeclarations of gl_FragDepth in all
1028 * fragment shaders in a single program must have the same set
1031 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
1032 bool layout_declared
= var
->data
.depth_layout
!= ir_depth_layout_none
;
1033 bool layout_differs
=
1034 var
->data
.depth_layout
!= existing
->data
.depth_layout
;
1036 if (layout_declared
&& layout_differs
) {
1038 "All redeclarations of gl_FragDepth in all "
1039 "fragment shaders in a single program must have "
1040 "the same set of qualifiers.\n");
1043 if (var
->data
.used
&& layout_differs
) {
1045 "If gl_FragDepth is redeclared with a layout "
1046 "qualifier in any fragment shader, it must be "
1047 "redeclared with the same layout qualifier in "
1048 "all fragment shaders that have assignments to "
1053 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1055 * "If a shared global has multiple initializers, the
1056 * initializers must all be constant expressions, and they
1057 * must all have the same value. Otherwise, a link error will
1058 * result. (A shared global having only one initializer does
1059 * not require that initializer to be a constant expression.)"
1061 * Previous to 4.20 the GLSL spec simply said that initializers
1062 * must have the same value. In this case of non-constant
1063 * initializers, this was impossible to determine. As a result,
1064 * no vendor actually implemented that behavior. The 4.20
1065 * behavior matches the implemented behavior of at least one other
1066 * vendor, so we'll implement that for all GLSL versions.
1067 * If (at least) one of these constant expressions is implicit,
1068 * because it was added by glsl_zero_init, we skip the verification.
1070 if (var
->constant_initializer
!= NULL
) {
1071 if (existing
->constant_initializer
!= NULL
&&
1072 !existing
->data
.is_implicit_initializer
&&
1073 !var
->data
.is_implicit_initializer
) {
1074 if (!var
->constant_initializer
->has_value(existing
->constant_initializer
)) {
1075 linker_error(prog
, "initializers for %s "
1076 "`%s' have differing values\n",
1077 mode_string(var
), var
->name
);
1081 /* If the first-seen instance of a particular uniform did
1082 * not have an initializer but a later instance does,
1083 * replace the former with the later.
1085 if (!var
->data
.is_implicit_initializer
)
1086 variables
->replace_variable(existing
->name
, var
);
1090 if (var
->data
.has_initializer
) {
1091 if (existing
->data
.has_initializer
1092 && (var
->constant_initializer
== NULL
1093 || existing
->constant_initializer
== NULL
)) {
1095 "shared global variable `%s' has multiple "
1096 "non-constant initializers.\n",
1102 if (existing
->data
.explicit_invariant
!= var
->data
.explicit_invariant
) {
1103 linker_error(prog
, "declarations for %s `%s' have "
1104 "mismatching invariant qualifiers\n",
1105 mode_string(var
), var
->name
);
1108 if (existing
->data
.centroid
!= var
->data
.centroid
) {
1109 linker_error(prog
, "declarations for %s `%s' have "
1110 "mismatching centroid qualifiers\n",
1111 mode_string(var
), var
->name
);
1114 if (existing
->data
.sample
!= var
->data
.sample
) {
1115 linker_error(prog
, "declarations for %s `%s` have "
1116 "mismatching sample qualifiers\n",
1117 mode_string(var
), var
->name
);
1120 if (existing
->data
.image_format
!= var
->data
.image_format
) {
1121 linker_error(prog
, "declarations for %s `%s` have "
1122 "mismatching image format qualifiers\n",
1123 mode_string(var
), var
->name
);
1127 /* Check the precision qualifier matches for uniform variables on
1130 if (!ctx
->Const
.AllowGLSLRelaxedES
&&
1131 prog
->IsES
&& !var
->get_interface_type() &&
1132 existing
->data
.precision
!= var
->data
.precision
) {
1133 if ((existing
->data
.used
&& var
->data
.used
) || prog
->data
->Version
>= 300) {
1134 linker_error(prog
, "declarations for %s `%s` have "
1135 "mismatching precision qualifiers\n",
1136 mode_string(var
), var
->name
);
1139 linker_warning(prog
, "declarations for %s `%s` have "
1140 "mismatching precision qualifiers\n",
1141 mode_string(var
), var
->name
);
1145 /* In OpenGL GLSL 3.20 spec, section 4.3.9:
1147 * "It is a link-time error if any particular shader interface
1150 * - two different blocks, each having no instance name, and each
1151 * having a member of the same name, or
1153 * - a variable outside a block, and a block with no instance name,
1154 * where the variable has the same name as a member in the block."
1156 const glsl_type
*var_itype
= var
->get_interface_type();
1157 const glsl_type
*existing_itype
= existing
->get_interface_type();
1158 if (var_itype
!= existing_itype
) {
1159 if (!var_itype
|| !existing_itype
) {
1160 linker_error(prog
, "declarations for %s `%s` are inside block "
1161 "`%s` and outside a block",
1162 mode_string(var
), var
->name
,
1163 var_itype
? var_itype
->name
: existing_itype
->name
);
1165 } else if (strcmp(var_itype
->name
, existing_itype
->name
) != 0) {
1166 linker_error(prog
, "declarations for %s `%s` are inside blocks "
1168 mode_string(var
), var
->name
,
1169 existing_itype
->name
,
1175 variables
->add_variable(var
);
1181 * Perform validation of uniforms used across multiple shader stages
1184 cross_validate_uniforms(struct gl_context
*ctx
,
1185 struct gl_shader_program
*prog
)
1187 glsl_symbol_table variables
;
1188 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1189 if (prog
->_LinkedShaders
[i
] == NULL
)
1192 cross_validate_globals(ctx
, prog
, prog
->_LinkedShaders
[i
]->ir
,
1198 * Accumulates the array of buffer blocks and checks that all definitions of
1199 * blocks agree on their contents.
1202 interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
,
1205 int *InterfaceBlockStageIndex
[MESA_SHADER_STAGES
];
1206 struct gl_uniform_block
*blks
= NULL
;
1207 unsigned *num_blks
= validate_ssbo
? &prog
->data
->NumShaderStorageBlocks
:
1208 &prog
->data
->NumUniformBlocks
;
1210 unsigned max_num_buffer_blocks
= 0;
1211 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1212 if (prog
->_LinkedShaders
[i
]) {
1213 if (validate_ssbo
) {
1214 max_num_buffer_blocks
+=
1215 prog
->_LinkedShaders
[i
]->Program
->info
.num_ssbos
;
1217 max_num_buffer_blocks
+=
1218 prog
->_LinkedShaders
[i
]->Program
->info
.num_ubos
;
1223 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1224 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1226 InterfaceBlockStageIndex
[i
] = new int[max_num_buffer_blocks
];
1227 for (unsigned int j
= 0; j
< max_num_buffer_blocks
; j
++)
1228 InterfaceBlockStageIndex
[i
][j
] = -1;
1233 unsigned sh_num_blocks
;
1234 struct gl_uniform_block
**sh_blks
;
1235 if (validate_ssbo
) {
1236 sh_num_blocks
= prog
->_LinkedShaders
[i
]->Program
->info
.num_ssbos
;
1237 sh_blks
= sh
->Program
->sh
.ShaderStorageBlocks
;
1239 sh_num_blocks
= prog
->_LinkedShaders
[i
]->Program
->info
.num_ubos
;
1240 sh_blks
= sh
->Program
->sh
.UniformBlocks
;
1243 for (unsigned int j
= 0; j
< sh_num_blocks
; j
++) {
1244 int index
= link_cross_validate_uniform_block(prog
->data
, &blks
,
1245 num_blks
, sh_blks
[j
]);
1248 linker_error(prog
, "buffer block `%s' has mismatching "
1249 "definitions\n", sh_blks
[j
]->Name
);
1251 for (unsigned k
= 0; k
<= i
; k
++) {
1252 delete[] InterfaceBlockStageIndex
[k
];
1255 /* Reset the block count. This will help avoid various segfaults
1256 * from api calls that assume the array exists due to the count
1263 InterfaceBlockStageIndex
[i
][index
] = j
;
1267 /* Update per stage block pointers to point to the program list.
1268 * FIXME: We should be able to free the per stage blocks here.
1270 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1271 for (unsigned j
= 0; j
< *num_blks
; j
++) {
1272 int stage_index
= InterfaceBlockStageIndex
[i
][j
];
1274 if (stage_index
!= -1) {
1275 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1277 struct gl_uniform_block
**sh_blks
= validate_ssbo
?
1278 sh
->Program
->sh
.ShaderStorageBlocks
:
1279 sh
->Program
->sh
.UniformBlocks
;
1281 blks
[j
].stageref
|= sh_blks
[stage_index
]->stageref
;
1282 sh_blks
[stage_index
] = &blks
[j
];
1287 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1288 delete[] InterfaceBlockStageIndex
[i
];
1292 prog
->data
->ShaderStorageBlocks
= blks
;
1294 prog
->data
->UniformBlocks
= blks
;
1300 * Verifies the invariance of built-in special variables.
1303 validate_invariant_builtins(struct gl_shader_program
*prog
,
1304 const gl_linked_shader
*vert
,
1305 const gl_linked_shader
*frag
)
1307 const ir_variable
*var_vert
;
1308 const ir_variable
*var_frag
;
1314 * From OpenGL ES Shading Language 1.0 specification
1315 * (4.6.4 Invariance and Linkage):
1316 * "The invariance of varyings that are declared in both the vertex and
1317 * fragment shaders must match. For the built-in special variables,
1318 * gl_FragCoord can only be declared invariant if and only if
1319 * gl_Position is declared invariant. Similarly gl_PointCoord can only
1320 * be declared invariant if and only if gl_PointSize is declared
1321 * invariant. It is an error to declare gl_FrontFacing as invariant.
1322 * The invariance of gl_FrontFacing is the same as the invariance of
1325 var_frag
= frag
->symbols
->get_variable("gl_FragCoord");
1326 if (var_frag
&& var_frag
->data
.invariant
) {
1327 var_vert
= vert
->symbols
->get_variable("gl_Position");
1328 if (var_vert
&& !var_vert
->data
.invariant
) {
1330 "fragment shader built-in `%s' has invariant qualifier, "
1331 "but vertex shader built-in `%s' lacks invariant qualifier\n",
1332 var_frag
->name
, var_vert
->name
);
1337 var_frag
= frag
->symbols
->get_variable("gl_PointCoord");
1338 if (var_frag
&& var_frag
->data
.invariant
) {
1339 var_vert
= vert
->symbols
->get_variable("gl_PointSize");
1340 if (var_vert
&& !var_vert
->data
.invariant
) {
1342 "fragment shader built-in `%s' has invariant qualifier, "
1343 "but vertex shader built-in `%s' lacks invariant qualifier\n",
1344 var_frag
->name
, var_vert
->name
);
1349 var_frag
= frag
->symbols
->get_variable("gl_FrontFacing");
1350 if (var_frag
&& var_frag
->data
.invariant
) {
1352 "fragment shader built-in `%s' can not be declared as invariant\n",
1361 * Populates a shaders symbol table with all global declarations
1364 populate_symbol_table(gl_linked_shader
*sh
, glsl_symbol_table
*symbols
)
1366 sh
->symbols
= new(sh
) glsl_symbol_table
;
1368 _mesa_glsl_copy_symbols_from_table(sh
->ir
, symbols
, sh
->symbols
);
1373 * Remap variables referenced in an instruction tree
1375 * This is used when instruction trees are cloned from one shader and placed in
1376 * another. These trees will contain references to \c ir_variable nodes that
1377 * do not exist in the target shader. This function finds these \c ir_variable
1378 * references and replaces the references with matching variables in the target
1381 * If there is no matching variable in the target shader, a clone of the
1382 * \c ir_variable is made and added to the target shader. The new variable is
1383 * added to \b both the instruction stream and the symbol table.
1385 * \param inst IR tree that is to be processed.
1386 * \param symbols Symbol table containing global scope symbols in the
1388 * \param instructions Instruction stream where new variable declarations
1392 remap_variables(ir_instruction
*inst
, struct gl_linked_shader
*target
,
1395 class remap_visitor
: public ir_hierarchical_visitor
{
1397 remap_visitor(struct gl_linked_shader
*target
, hash_table
*temps
)
1399 this->target
= target
;
1400 this->symbols
= target
->symbols
;
1401 this->instructions
= target
->ir
;
1402 this->temps
= temps
;
1405 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
1407 if (ir
->var
->data
.mode
== ir_var_temporary
) {
1408 hash_entry
*entry
= _mesa_hash_table_search(temps
, ir
->var
);
1409 ir_variable
*var
= entry
? (ir_variable
*) entry
->data
: NULL
;
1411 assert(var
!= NULL
);
1413 return visit_continue
;
1416 ir_variable
*const existing
=
1417 this->symbols
->get_variable(ir
->var
->name
);
1418 if (existing
!= NULL
)
1421 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
1423 this->symbols
->add_variable(copy
);
1424 this->instructions
->push_head(copy
);
1428 return visit_continue
;
1432 struct gl_linked_shader
*target
;
1433 glsl_symbol_table
*symbols
;
1434 exec_list
*instructions
;
1438 remap_visitor
v(target
, temps
);
1445 * Move non-declarations from one instruction stream to another
1447 * The intended usage pattern of this function is to pass the pointer to the
1448 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
1449 * pointer) for \c last and \c false for \c make_copies on the first
1450 * call. Successive calls pass the return value of the previous call for
1451 * \c last and \c true for \c make_copies.
1453 * \param instructions Source instruction stream
1454 * \param last Instruction after which new instructions should be
1455 * inserted in the target instruction stream
1456 * \param make_copies Flag selecting whether instructions in \c instructions
1457 * should be copied (via \c ir_instruction::clone) into the
1458 * target list or moved.
1461 * The new "last" instruction in the target instruction stream. This pointer
1462 * is suitable for use as the \c last parameter of a later call to this
1466 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
1467 bool make_copies
, gl_linked_shader
*target
)
1469 hash_table
*temps
= NULL
;
1472 temps
= _mesa_pointer_hash_table_create(NULL
);
1474 foreach_in_list_safe(ir_instruction
, inst
, instructions
) {
1475 if (inst
->as_function())
1478 ir_variable
*var
= inst
->as_variable();
1479 if ((var
!= NULL
) && (var
->data
.mode
!= ir_var_temporary
))
1482 assert(inst
->as_assignment()
1484 || inst
->as_if() /* for initializers with the ?: operator */
1485 || ((var
!= NULL
) && (var
->data
.mode
== ir_var_temporary
)));
1488 inst
= inst
->clone(target
, NULL
);
1491 _mesa_hash_table_insert(temps
, var
, inst
);
1493 remap_variables(inst
, target
, temps
);
1498 last
->insert_after(inst
);
1503 _mesa_hash_table_destroy(temps
, NULL
);
1510 * This class is only used in link_intrastage_shaders() below but declaring
1511 * it inside that function leads to compiler warnings with some versions of
1514 class array_sizing_visitor
: public deref_type_updater
{
1516 using deref_type_updater::visit
;
1518 array_sizing_visitor()
1519 : mem_ctx(ralloc_context(NULL
)),
1520 unnamed_interfaces(_mesa_pointer_hash_table_create(NULL
))
1524 ~array_sizing_visitor()
1526 _mesa_hash_table_destroy(this->unnamed_interfaces
, NULL
);
1527 ralloc_free(this->mem_ctx
);
1530 virtual ir_visitor_status
visit(ir_variable
*var
)
1532 const glsl_type
*type_without_array
;
1533 bool implicit_sized_array
= var
->data
.implicit_sized_array
;
1534 fixup_type(&var
->type
, var
->data
.max_array_access
,
1535 var
->data
.from_ssbo_unsized_array
,
1536 &implicit_sized_array
);
1537 var
->data
.implicit_sized_array
= implicit_sized_array
;
1538 type_without_array
= var
->type
->without_array();
1539 if (var
->type
->is_interface()) {
1540 if (interface_contains_unsized_arrays(var
->type
)) {
1541 const glsl_type
*new_type
=
1542 resize_interface_members(var
->type
,
1543 var
->get_max_ifc_array_access(),
1544 var
->is_in_shader_storage_block());
1545 var
->type
= new_type
;
1546 var
->change_interface_type(new_type
);
1548 } else if (type_without_array
->is_interface()) {
1549 if (interface_contains_unsized_arrays(type_without_array
)) {
1550 const glsl_type
*new_type
=
1551 resize_interface_members(type_without_array
,
1552 var
->get_max_ifc_array_access(),
1553 var
->is_in_shader_storage_block());
1554 var
->change_interface_type(new_type
);
1555 var
->type
= update_interface_members_array(var
->type
, new_type
);
1557 } else if (const glsl_type
*ifc_type
= var
->get_interface_type()) {
1558 /* Store a pointer to the variable in the unnamed_interfaces
1562 _mesa_hash_table_search(this->unnamed_interfaces
,
1565 ir_variable
**interface_vars
= entry
? (ir_variable
**) entry
->data
: NULL
;
1567 if (interface_vars
== NULL
) {
1568 interface_vars
= rzalloc_array(mem_ctx
, ir_variable
*,
1570 _mesa_hash_table_insert(this->unnamed_interfaces
, ifc_type
,
1573 unsigned index
= ifc_type
->field_index(var
->name
);
1574 assert(index
< ifc_type
->length
);
1575 assert(interface_vars
[index
] == NULL
);
1576 interface_vars
[index
] = var
;
1578 return visit_continue
;
1582 * For each unnamed interface block that was discovered while running the
1583 * visitor, adjust the interface type to reflect the newly assigned array
1584 * sizes, and fix up the ir_variable nodes to point to the new interface
1587 void fixup_unnamed_interface_types()
1589 hash_table_call_foreach(this->unnamed_interfaces
,
1590 fixup_unnamed_interface_type
, NULL
);
1595 * If the type pointed to by \c type represents an unsized array, replace
1596 * it with a sized array whose size is determined by max_array_access.
1598 static void fixup_type(const glsl_type
**type
, unsigned max_array_access
,
1599 bool from_ssbo_unsized_array
, bool *implicit_sized
)
1601 if (!from_ssbo_unsized_array
&& (*type
)->is_unsized_array()) {
1602 *type
= glsl_type::get_array_instance((*type
)->fields
.array
,
1603 max_array_access
+ 1);
1604 *implicit_sized
= true;
1605 assert(*type
!= NULL
);
1609 static const glsl_type
*
1610 update_interface_members_array(const glsl_type
*type
,
1611 const glsl_type
*new_interface_type
)
1613 const glsl_type
*element_type
= type
->fields
.array
;
1614 if (element_type
->is_array()) {
1615 const glsl_type
*new_array_type
=
1616 update_interface_members_array(element_type
, new_interface_type
);
1617 return glsl_type::get_array_instance(new_array_type
, type
->length
);
1619 return glsl_type::get_array_instance(new_interface_type
,
1625 * Determine whether the given interface type contains unsized arrays (if
1626 * it doesn't, array_sizing_visitor doesn't need to process it).
1628 static bool interface_contains_unsized_arrays(const glsl_type
*type
)
1630 for (unsigned i
= 0; i
< type
->length
; i
++) {
1631 const glsl_type
*elem_type
= type
->fields
.structure
[i
].type
;
1632 if (elem_type
->is_unsized_array())
1639 * Create a new interface type based on the given type, with unsized arrays
1640 * replaced by sized arrays whose size is determined by
1641 * max_ifc_array_access.
1643 static const glsl_type
*
1644 resize_interface_members(const glsl_type
*type
,
1645 const int *max_ifc_array_access
,
1648 unsigned num_fields
= type
->length
;
1649 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1650 memcpy(fields
, type
->fields
.structure
,
1651 num_fields
* sizeof(*fields
));
1652 for (unsigned i
= 0; i
< num_fields
; i
++) {
1653 bool implicit_sized_array
= fields
[i
].implicit_sized_array
;
1654 /* If SSBO last member is unsized array, we don't replace it by a sized
1657 if (is_ssbo
&& i
== (num_fields
- 1))
1658 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1659 true, &implicit_sized_array
);
1661 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1662 false, &implicit_sized_array
);
1663 fields
[i
].implicit_sized_array
= implicit_sized_array
;
1665 glsl_interface_packing packing
=
1666 (glsl_interface_packing
) type
->interface_packing
;
1667 bool row_major
= (bool) type
->interface_row_major
;
1668 const glsl_type
*new_ifc_type
=
1669 glsl_type::get_interface_instance(fields
, num_fields
,
1670 packing
, row_major
, type
->name
);
1672 return new_ifc_type
;
1675 static void fixup_unnamed_interface_type(const void *key
, void *data
,
1678 const glsl_type
*ifc_type
= (const glsl_type
*) key
;
1679 ir_variable
**interface_vars
= (ir_variable
**) data
;
1680 unsigned num_fields
= ifc_type
->length
;
1681 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1682 memcpy(fields
, ifc_type
->fields
.structure
,
1683 num_fields
* sizeof(*fields
));
1684 bool interface_type_changed
= false;
1685 for (unsigned i
= 0; i
< num_fields
; i
++) {
1686 if (interface_vars
[i
] != NULL
&&
1687 fields
[i
].type
!= interface_vars
[i
]->type
) {
1688 fields
[i
].type
= interface_vars
[i
]->type
;
1689 interface_type_changed
= true;
1692 if (!interface_type_changed
) {
1696 glsl_interface_packing packing
=
1697 (glsl_interface_packing
) ifc_type
->interface_packing
;
1698 bool row_major
= (bool) ifc_type
->interface_row_major
;
1699 const glsl_type
*new_ifc_type
=
1700 glsl_type::get_interface_instance(fields
, num_fields
, packing
,
1701 row_major
, ifc_type
->name
);
1703 for (unsigned i
= 0; i
< num_fields
; i
++) {
1704 if (interface_vars
[i
] != NULL
)
1705 interface_vars
[i
]->change_interface_type(new_ifc_type
);
1710 * Memory context used to allocate the data in \c unnamed_interfaces.
1715 * Hash table from const glsl_type * to an array of ir_variable *'s
1716 * pointing to the ir_variables constituting each unnamed interface block.
1718 hash_table
*unnamed_interfaces
;
1722 validate_xfb_buffer_stride(struct gl_context
*ctx
, unsigned idx
,
1723 struct gl_shader_program
*prog
)
1725 /* We will validate doubles at a later stage */
1726 if (prog
->TransformFeedback
.BufferStride
[idx
] % 4) {
1727 linker_error(prog
, "invalid qualifier xfb_stride=%d must be a "
1728 "multiple of 4 or if its applied to a type that is "
1729 "or contains a double a multiple of 8.",
1730 prog
->TransformFeedback
.BufferStride
[idx
]);
1734 if (prog
->TransformFeedback
.BufferStride
[idx
] / 4 >
1735 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
1736 linker_error(prog
, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1737 "limit has been exceeded.");
1745 * Check for conflicting xfb_stride default qualifiers and store buffer stride
1749 link_xfb_stride_layout_qualifiers(struct gl_context
*ctx
,
1750 struct gl_shader_program
*prog
,
1751 struct gl_shader
**shader_list
,
1752 unsigned num_shaders
)
1754 for (unsigned i
= 0; i
< MAX_FEEDBACK_BUFFERS
; i
++) {
1755 prog
->TransformFeedback
.BufferStride
[i
] = 0;
1758 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1759 struct gl_shader
*shader
= shader_list
[i
];
1761 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1762 if (shader
->TransformFeedbackBufferStride
[j
]) {
1763 if (prog
->TransformFeedback
.BufferStride
[j
] == 0) {
1764 prog
->TransformFeedback
.BufferStride
[j
] =
1765 shader
->TransformFeedbackBufferStride
[j
];
1766 if (!validate_xfb_buffer_stride(ctx
, j
, prog
))
1768 } else if (prog
->TransformFeedback
.BufferStride
[j
] !=
1769 shader
->TransformFeedbackBufferStride
[j
]){
1771 "intrastage shaders defined with conflicting "
1772 "xfb_stride for buffer %d (%d and %d)\n", j
,
1773 prog
->TransformFeedback
.BufferStride
[j
],
1774 shader
->TransformFeedbackBufferStride
[j
]);
1783 * Check for conflicting bindless/bound sampler/image layout qualifiers at
1787 link_bindless_layout_qualifiers(struct gl_shader_program
*prog
,
1788 struct gl_shader
**shader_list
,
1789 unsigned num_shaders
)
1791 bool bindless_sampler
, bindless_image
;
1792 bool bound_sampler
, bound_image
;
1794 bindless_sampler
= bindless_image
= false;
1795 bound_sampler
= bound_image
= false;
1797 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1798 struct gl_shader
*shader
= shader_list
[i
];
1800 if (shader
->bindless_sampler
)
1801 bindless_sampler
= true;
1802 if (shader
->bindless_image
)
1803 bindless_image
= true;
1804 if (shader
->bound_sampler
)
1805 bound_sampler
= true;
1806 if (shader
->bound_image
)
1809 if ((bindless_sampler
&& bound_sampler
) ||
1810 (bindless_image
&& bound_image
)) {
1811 /* From section 4.4.6 of the ARB_bindless_texture spec:
1813 * "If both bindless_sampler and bound_sampler, or bindless_image
1814 * and bound_image, are declared at global scope in any
1815 * compilation unit, a link- time error will be generated."
1817 linker_error(prog
, "both bindless_sampler and bound_sampler, or "
1818 "bindless_image and bound_image, can't be declared at "
1825 * Check for conflicting viewport_relative settings across shaders, and sets
1826 * the value for the linked shader.
1829 link_layer_viewport_relative_qualifier(struct gl_shader_program
*prog
,
1830 struct gl_program
*gl_prog
,
1831 struct gl_shader
**shader_list
,
1832 unsigned num_shaders
)
1836 /* Find first shader with explicit layer declaration */
1837 for (i
= 0; i
< num_shaders
; i
++) {
1838 if (shader_list
[i
]->redeclares_gl_layer
) {
1839 gl_prog
->info
.layer_viewport_relative
=
1840 shader_list
[i
]->layer_viewport_relative
;
1845 /* Now make sure that each subsequent shader's explicit layer declaration
1846 * matches the first one's.
1848 for (; i
< num_shaders
; i
++) {
1849 if (shader_list
[i
]->redeclares_gl_layer
&&
1850 shader_list
[i
]->layer_viewport_relative
!=
1851 gl_prog
->info
.layer_viewport_relative
) {
1852 linker_error(prog
, "all gl_Layer redeclarations must have identical "
1853 "viewport_relative settings");
1859 * Performs the cross-validation of tessellation control shader vertices and
1860 * layout qualifiers for the attached tessellation control shaders,
1861 * and propagates them to the linked TCS and linked shader program.
1864 link_tcs_out_layout_qualifiers(struct gl_shader_program
*prog
,
1865 struct gl_program
*gl_prog
,
1866 struct gl_shader
**shader_list
,
1867 unsigned num_shaders
)
1869 if (gl_prog
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
1872 gl_prog
->info
.tess
.tcs_vertices_out
= 0;
1874 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1876 * "All tessellation control shader layout declarations in a program
1877 * must specify the same output patch vertex count. There must be at
1878 * least one layout qualifier specifying an output patch vertex count
1879 * in any program containing tessellation control shaders; however,
1880 * such a declaration is not required in all tessellation control
1884 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1885 struct gl_shader
*shader
= shader_list
[i
];
1887 if (shader
->info
.TessCtrl
.VerticesOut
!= 0) {
1888 if (gl_prog
->info
.tess
.tcs_vertices_out
!= 0 &&
1889 gl_prog
->info
.tess
.tcs_vertices_out
!=
1890 (unsigned) shader
->info
.TessCtrl
.VerticesOut
) {
1891 linker_error(prog
, "tessellation control shader defined with "
1892 "conflicting output vertex count (%d and %d)\n",
1893 gl_prog
->info
.tess
.tcs_vertices_out
,
1894 shader
->info
.TessCtrl
.VerticesOut
);
1897 gl_prog
->info
.tess
.tcs_vertices_out
=
1898 shader
->info
.TessCtrl
.VerticesOut
;
1902 /* Just do the intrastage -> interstage propagation right now,
1903 * since we already know we're in the right type of shader program
1906 if (gl_prog
->info
.tess
.tcs_vertices_out
== 0) {
1907 linker_error(prog
, "tessellation control shader didn't declare "
1908 "vertices out layout qualifier\n");
1915 * Performs the cross-validation of tessellation evaluation shader
1916 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1917 * for the attached tessellation evaluation shaders, and propagates them
1918 * to the linked TES and linked shader program.
1921 link_tes_in_layout_qualifiers(struct gl_shader_program
*prog
,
1922 struct gl_program
*gl_prog
,
1923 struct gl_shader
**shader_list
,
1924 unsigned num_shaders
)
1926 if (gl_prog
->info
.stage
!= MESA_SHADER_TESS_EVAL
)
1929 int point_mode
= -1;
1930 unsigned vertex_order
= 0;
1932 gl_prog
->info
.tess
.primitive_mode
= PRIM_UNKNOWN
;
1933 gl_prog
->info
.tess
.spacing
= TESS_SPACING_UNSPECIFIED
;
1935 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1937 * "At least one tessellation evaluation shader (compilation unit) in
1938 * a program must declare a primitive mode in its input layout.
1939 * Declaration vertex spacing, ordering, and point mode identifiers is
1940 * optional. It is not required that all tessellation evaluation
1941 * shaders in a program declare a primitive mode. If spacing or
1942 * vertex ordering declarations are omitted, the tessellation
1943 * primitive generator will use equal spacing or counter-clockwise
1944 * vertex ordering, respectively. If a point mode declaration is
1945 * omitted, the tessellation primitive generator will produce lines or
1946 * triangles according to the primitive mode."
1949 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1950 struct gl_shader
*shader
= shader_list
[i
];
1952 if (shader
->info
.TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
) {
1953 if (gl_prog
->info
.tess
.primitive_mode
!= PRIM_UNKNOWN
&&
1954 gl_prog
->info
.tess
.primitive_mode
!=
1955 shader
->info
.TessEval
.PrimitiveMode
) {
1956 linker_error(prog
, "tessellation evaluation shader defined with "
1957 "conflicting input primitive modes.\n");
1960 gl_prog
->info
.tess
.primitive_mode
=
1961 shader
->info
.TessEval
.PrimitiveMode
;
1964 if (shader
->info
.TessEval
.Spacing
!= 0) {
1965 if (gl_prog
->info
.tess
.spacing
!= 0 && gl_prog
->info
.tess
.spacing
!=
1966 shader
->info
.TessEval
.Spacing
) {
1967 linker_error(prog
, "tessellation evaluation shader defined with "
1968 "conflicting vertex spacing.\n");
1971 gl_prog
->info
.tess
.spacing
= shader
->info
.TessEval
.Spacing
;
1974 if (shader
->info
.TessEval
.VertexOrder
!= 0) {
1975 if (vertex_order
!= 0 &&
1976 vertex_order
!= shader
->info
.TessEval
.VertexOrder
) {
1977 linker_error(prog
, "tessellation evaluation shader defined with "
1978 "conflicting ordering.\n");
1981 vertex_order
= shader
->info
.TessEval
.VertexOrder
;
1984 if (shader
->info
.TessEval
.PointMode
!= -1) {
1985 if (point_mode
!= -1 &&
1986 point_mode
!= shader
->info
.TessEval
.PointMode
) {
1987 linker_error(prog
, "tessellation evaluation shader defined with "
1988 "conflicting point modes.\n");
1991 point_mode
= shader
->info
.TessEval
.PointMode
;
1996 /* Just do the intrastage -> interstage propagation right now,
1997 * since we already know we're in the right type of shader program
2000 if (gl_prog
->info
.tess
.primitive_mode
== PRIM_UNKNOWN
) {
2002 "tessellation evaluation shader didn't declare input "
2003 "primitive modes.\n");
2007 if (gl_prog
->info
.tess
.spacing
== TESS_SPACING_UNSPECIFIED
)
2008 gl_prog
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
2010 if (vertex_order
== 0 || vertex_order
== GL_CCW
)
2011 gl_prog
->info
.tess
.ccw
= true;
2013 gl_prog
->info
.tess
.ccw
= false;
2016 if (point_mode
== -1 || point_mode
== GL_FALSE
)
2017 gl_prog
->info
.tess
.point_mode
= false;
2019 gl_prog
->info
.tess
.point_mode
= true;
2024 * Performs the cross-validation of layout qualifiers specified in
2025 * redeclaration of gl_FragCoord for the attached fragment shaders,
2026 * and propagates them to the linked FS and linked shader program.
2029 link_fs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
2030 struct gl_linked_shader
*linked_shader
,
2031 struct gl_shader
**shader_list
,
2032 unsigned num_shaders
)
2034 bool redeclares_gl_fragcoord
= false;
2035 bool uses_gl_fragcoord
= false;
2036 bool origin_upper_left
= false;
2037 bool pixel_center_integer
= false;
2039 if (linked_shader
->Stage
!= MESA_SHADER_FRAGMENT
||
2040 (prog
->data
->Version
< 150 &&
2041 !prog
->ARB_fragment_coord_conventions_enable
))
2044 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2045 struct gl_shader
*shader
= shader_list
[i
];
2046 /* From the GLSL 1.50 spec, page 39:
2048 * "If gl_FragCoord is redeclared in any fragment shader in a program,
2049 * it must be redeclared in all the fragment shaders in that program
2050 * that have a static use gl_FragCoord."
2052 if ((redeclares_gl_fragcoord
&& !shader
->redeclares_gl_fragcoord
&&
2053 shader
->uses_gl_fragcoord
)
2054 || (shader
->redeclares_gl_fragcoord
&& !redeclares_gl_fragcoord
&&
2055 uses_gl_fragcoord
)) {
2056 linker_error(prog
, "fragment shader defined with conflicting "
2057 "layout qualifiers for gl_FragCoord\n");
2060 /* From the GLSL 1.50 spec, page 39:
2062 * "All redeclarations of gl_FragCoord in all fragment shaders in a
2063 * single program must have the same set of qualifiers."
2065 if (redeclares_gl_fragcoord
&& shader
->redeclares_gl_fragcoord
&&
2066 (shader
->origin_upper_left
!= origin_upper_left
||
2067 shader
->pixel_center_integer
!= pixel_center_integer
)) {
2068 linker_error(prog
, "fragment shader defined with conflicting "
2069 "layout qualifiers for gl_FragCoord\n");
2072 /* Update the linked shader state. Note that uses_gl_fragcoord should
2073 * accumulate the results. The other values should replace. If there
2074 * are multiple redeclarations, all the fields except uses_gl_fragcoord
2075 * are already known to be the same.
2077 if (shader
->redeclares_gl_fragcoord
|| shader
->uses_gl_fragcoord
) {
2078 redeclares_gl_fragcoord
= shader
->redeclares_gl_fragcoord
;
2079 uses_gl_fragcoord
|= shader
->uses_gl_fragcoord
;
2080 origin_upper_left
= shader
->origin_upper_left
;
2081 pixel_center_integer
= shader
->pixel_center_integer
;
2084 linked_shader
->Program
->info
.fs
.early_fragment_tests
|=
2085 shader
->EarlyFragmentTests
|| shader
->PostDepthCoverage
;
2086 linked_shader
->Program
->info
.fs
.inner_coverage
|= shader
->InnerCoverage
;
2087 linked_shader
->Program
->info
.fs
.post_depth_coverage
|=
2088 shader
->PostDepthCoverage
;
2089 linked_shader
->Program
->info
.fs
.pixel_interlock_ordered
|=
2090 shader
->PixelInterlockOrdered
;
2091 linked_shader
->Program
->info
.fs
.pixel_interlock_unordered
|=
2092 shader
->PixelInterlockUnordered
;
2093 linked_shader
->Program
->info
.fs
.sample_interlock_ordered
|=
2094 shader
->SampleInterlockOrdered
;
2095 linked_shader
->Program
->info
.fs
.sample_interlock_unordered
|=
2096 shader
->SampleInterlockUnordered
;
2097 linked_shader
->Program
->sh
.fs
.BlendSupport
|= shader
->BlendSupport
;
2100 linked_shader
->Program
->info
.fs
.pixel_center_integer
= pixel_center_integer
;
2101 linked_shader
->Program
->info
.fs
.origin_upper_left
= origin_upper_left
;
2105 * Performs the cross-validation of geometry shader max_vertices and
2106 * primitive type layout qualifiers for the attached geometry shaders,
2107 * and propagates them to the linked GS and linked shader program.
2110 link_gs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
2111 struct gl_program
*gl_prog
,
2112 struct gl_shader
**shader_list
,
2113 unsigned num_shaders
)
2115 /* No in/out qualifiers defined for anything but GLSL 1.50+
2116 * geometry shaders so far.
2118 if (gl_prog
->info
.stage
!= MESA_SHADER_GEOMETRY
||
2119 prog
->data
->Version
< 150)
2122 int vertices_out
= -1;
2124 gl_prog
->info
.gs
.invocations
= 0;
2125 gl_prog
->info
.gs
.input_primitive
= PRIM_UNKNOWN
;
2126 gl_prog
->info
.gs
.output_primitive
= PRIM_UNKNOWN
;
2128 /* From the GLSL 1.50 spec, page 46:
2130 * "All geometry shader output layout declarations in a program
2131 * must declare the same layout and same value for
2132 * max_vertices. There must be at least one geometry output
2133 * layout declaration somewhere in a program, but not all
2134 * geometry shaders (compilation units) are required to
2138 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2139 struct gl_shader
*shader
= shader_list
[i
];
2141 if (shader
->info
.Geom
.InputType
!= PRIM_UNKNOWN
) {
2142 if (gl_prog
->info
.gs
.input_primitive
!= PRIM_UNKNOWN
&&
2143 gl_prog
->info
.gs
.input_primitive
!=
2144 shader
->info
.Geom
.InputType
) {
2145 linker_error(prog
, "geometry shader defined with conflicting "
2149 gl_prog
->info
.gs
.input_primitive
= shader
->info
.Geom
.InputType
;
2152 if (shader
->info
.Geom
.OutputType
!= PRIM_UNKNOWN
) {
2153 if (gl_prog
->info
.gs
.output_primitive
!= PRIM_UNKNOWN
&&
2154 gl_prog
->info
.gs
.output_primitive
!=
2155 shader
->info
.Geom
.OutputType
) {
2156 linker_error(prog
, "geometry shader defined with conflicting "
2160 gl_prog
->info
.gs
.output_primitive
= shader
->info
.Geom
.OutputType
;
2163 if (shader
->info
.Geom
.VerticesOut
!= -1) {
2164 if (vertices_out
!= -1 &&
2165 vertices_out
!= shader
->info
.Geom
.VerticesOut
) {
2166 linker_error(prog
, "geometry shader defined with conflicting "
2167 "output vertex count (%d and %d)\n",
2168 vertices_out
, shader
->info
.Geom
.VerticesOut
);
2171 vertices_out
= shader
->info
.Geom
.VerticesOut
;
2174 if (shader
->info
.Geom
.Invocations
!= 0) {
2175 if (gl_prog
->info
.gs
.invocations
!= 0 &&
2176 gl_prog
->info
.gs
.invocations
!=
2177 (unsigned) shader
->info
.Geom
.Invocations
) {
2178 linker_error(prog
, "geometry shader defined with conflicting "
2179 "invocation count (%d and %d)\n",
2180 gl_prog
->info
.gs
.invocations
,
2181 shader
->info
.Geom
.Invocations
);
2184 gl_prog
->info
.gs
.invocations
= shader
->info
.Geom
.Invocations
;
2188 /* Just do the intrastage -> interstage propagation right now,
2189 * since we already know we're in the right type of shader program
2192 if (gl_prog
->info
.gs
.input_primitive
== PRIM_UNKNOWN
) {
2194 "geometry shader didn't declare primitive input type\n");
2198 if (gl_prog
->info
.gs
.output_primitive
== PRIM_UNKNOWN
) {
2200 "geometry shader didn't declare primitive output type\n");
2204 if (vertices_out
== -1) {
2206 "geometry shader didn't declare max_vertices\n");
2209 gl_prog
->info
.gs
.vertices_out
= vertices_out
;
2212 if (gl_prog
->info
.gs
.invocations
== 0)
2213 gl_prog
->info
.gs
.invocations
= 1;
2218 * Perform cross-validation of compute shader local_size_{x,y,z} layout and
2219 * derivative arrangement qualifiers for the attached compute shaders, and
2220 * propagate them to the linked CS and linked shader program.
2223 link_cs_input_layout_qualifiers(struct gl_shader_program
*prog
,
2224 struct gl_program
*gl_prog
,
2225 struct gl_shader
**shader_list
,
2226 unsigned num_shaders
)
2228 /* This function is called for all shader stages, but it only has an effect
2229 * for compute shaders.
2231 if (gl_prog
->info
.stage
!= MESA_SHADER_COMPUTE
)
2234 for (int i
= 0; i
< 3; i
++)
2235 gl_prog
->info
.cs
.local_size
[i
] = 0;
2237 gl_prog
->info
.cs
.local_size_variable
= false;
2239 gl_prog
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_NONE
;
2241 /* From the ARB_compute_shader spec, in the section describing local size
2244 * If multiple compute shaders attached to a single program object
2245 * declare local work-group size, the declarations must be identical;
2246 * otherwise a link-time error results. Furthermore, if a program
2247 * object contains any compute shaders, at least one must contain an
2248 * input layout qualifier specifying the local work sizes of the
2249 * program, or a link-time error will occur.
2251 for (unsigned sh
= 0; sh
< num_shaders
; sh
++) {
2252 struct gl_shader
*shader
= shader_list
[sh
];
2254 if (shader
->info
.Comp
.LocalSize
[0] != 0) {
2255 if (gl_prog
->info
.cs
.local_size
[0] != 0) {
2256 for (int i
= 0; i
< 3; i
++) {
2257 if (gl_prog
->info
.cs
.local_size
[i
] !=
2258 shader
->info
.Comp
.LocalSize
[i
]) {
2259 linker_error(prog
, "compute shader defined with conflicting "
2265 for (int i
= 0; i
< 3; i
++) {
2266 gl_prog
->info
.cs
.local_size
[i
] =
2267 shader
->info
.Comp
.LocalSize
[i
];
2269 } else if (shader
->info
.Comp
.LocalSizeVariable
) {
2270 if (gl_prog
->info
.cs
.local_size
[0] != 0) {
2271 /* The ARB_compute_variable_group_size spec says:
2273 * If one compute shader attached to a program declares a
2274 * variable local group size and a second compute shader
2275 * attached to the same program declares a fixed local group
2276 * size, a link-time error results.
2278 linker_error(prog
, "compute shader defined with both fixed and "
2279 "variable local group size\n");
2282 gl_prog
->info
.cs
.local_size_variable
= true;
2285 enum gl_derivative_group group
= shader
->info
.Comp
.DerivativeGroup
;
2286 if (group
!= DERIVATIVE_GROUP_NONE
) {
2287 if (gl_prog
->info
.cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
&&
2288 gl_prog
->info
.cs
.derivative_group
!= group
) {
2289 linker_error(prog
, "compute shader defined with conflicting "
2290 "derivative groups\n");
2293 gl_prog
->info
.cs
.derivative_group
= group
;
2297 /* Just do the intrastage -> interstage propagation right now,
2298 * since we already know we're in the right type of shader program
2301 if (gl_prog
->info
.cs
.local_size
[0] == 0 &&
2302 !gl_prog
->info
.cs
.local_size_variable
) {
2303 linker_error(prog
, "compute shader must contain a fixed or a variable "
2304 "local group size\n");
2308 if (gl_prog
->info
.cs
.derivative_group
== DERIVATIVE_GROUP_QUADS
) {
2309 if (gl_prog
->info
.cs
.local_size
[0] % 2 != 0) {
2310 linker_error(prog
, "derivative_group_quadsNV must be used with a "
2311 "local group size whose first dimension "
2312 "is a multiple of 2\n");
2315 if (gl_prog
->info
.cs
.local_size
[1] % 2 != 0) {
2316 linker_error(prog
, "derivative_group_quadsNV must be used with a local"
2317 "group size whose second dimension "
2318 "is a multiple of 2\n");
2321 } else if (gl_prog
->info
.cs
.derivative_group
== DERIVATIVE_GROUP_LINEAR
) {
2322 if ((gl_prog
->info
.cs
.local_size
[0] *
2323 gl_prog
->info
.cs
.local_size
[1] *
2324 gl_prog
->info
.cs
.local_size
[2]) % 4 != 0) {
2325 linker_error(prog
, "derivative_group_linearNV must be used with a "
2326 "local group size whose total number of invocations "
2327 "is a multiple of 4\n");
2334 * Link all out variables on a single stage which are not
2335 * directly used in a shader with the main function.
2338 link_output_variables(struct gl_linked_shader
*linked_shader
,
2339 struct gl_shader
**shader_list
,
2340 unsigned num_shaders
)
2342 struct glsl_symbol_table
*symbols
= linked_shader
->symbols
;
2344 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2346 /* Skip shader object with main function */
2347 if (shader_list
[i
]->symbols
->get_function("main"))
2350 foreach_in_list(ir_instruction
, ir
, shader_list
[i
]->ir
) {
2351 if (ir
->ir_type
!= ir_type_variable
)
2354 ir_variable
*var
= (ir_variable
*) ir
;
2356 if (var
->data
.mode
== ir_var_shader_out
&&
2357 !symbols
->get_variable(var
->name
)) {
2358 var
= var
->clone(linked_shader
, NULL
);
2359 symbols
->add_variable(var
);
2360 linked_shader
->ir
->push_head(var
);
2370 * Combine a group of shaders for a single stage to generate a linked shader
2373 * If this function is supplied a single shader, it is cloned, and the new
2374 * shader is returned.
2376 struct gl_linked_shader
*
2377 link_intrastage_shaders(void *mem_ctx
,
2378 struct gl_context
*ctx
,
2379 struct gl_shader_program
*prog
,
2380 struct gl_shader
**shader_list
,
2381 unsigned num_shaders
,
2382 bool allow_missing_main
)
2384 struct gl_uniform_block
*ubo_blocks
= NULL
;
2385 struct gl_uniform_block
*ssbo_blocks
= NULL
;
2386 unsigned num_ubo_blocks
= 0;
2387 unsigned num_ssbo_blocks
= 0;
2389 /* Check that global variables defined in multiple shaders are consistent.
2391 glsl_symbol_table variables
;
2392 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2393 if (shader_list
[i
] == NULL
)
2395 cross_validate_globals(ctx
, prog
, shader_list
[i
]->ir
, &variables
,
2399 if (!prog
->data
->LinkStatus
)
2402 /* Check that interface blocks defined in multiple shaders are consistent.
2404 validate_intrastage_interface_blocks(prog
, (const gl_shader
**)shader_list
,
2406 if (!prog
->data
->LinkStatus
)
2409 /* Check that there is only a single definition of each function signature
2410 * across all shaders.
2412 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
2413 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
2414 ir_function
*const f
= node
->as_function();
2419 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
2420 ir_function
*const other
=
2421 shader_list
[j
]->symbols
->get_function(f
->name
);
2423 /* If the other shader has no function (and therefore no function
2424 * signatures) with the same name, skip to the next shader.
2429 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
2430 if (!sig
->is_defined
)
2433 ir_function_signature
*other_sig
=
2434 other
->exact_matching_signature(NULL
, &sig
->parameters
);
2436 if (other_sig
!= NULL
&& other_sig
->is_defined
) {
2437 linker_error(prog
, "function `%s' is multiply defined\n",
2446 /* Find the shader that defines main, and make a clone of it.
2448 * Starting with the clone, search for undefined references. If one is
2449 * found, find the shader that defines it. Clone the reference and add
2450 * it to the shader. Repeat until there are no undefined references or
2451 * until a reference cannot be resolved.
2453 gl_shader
*main
= NULL
;
2454 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2455 if (_mesa_get_main_function_signature(shader_list
[i
]->symbols
)) {
2456 main
= shader_list
[i
];
2461 if (main
== NULL
&& allow_missing_main
)
2462 main
= shader_list
[0];
2465 linker_error(prog
, "%s shader lacks `main'\n",
2466 _mesa_shader_stage_to_string(shader_list
[0]->Stage
));
2470 gl_linked_shader
*linked
= rzalloc(NULL
, struct gl_linked_shader
);
2471 linked
->Stage
= shader_list
[0]->Stage
;
2473 /* Create program and attach it to the linked shader */
2474 struct gl_program
*gl_prog
=
2475 ctx
->Driver
.NewProgram(ctx
, shader_list
[0]->Stage
, prog
->Name
, false);
2477 prog
->data
->LinkStatus
= LINKING_FAILURE
;
2478 _mesa_delete_linked_shader(ctx
, linked
);
2482 _mesa_reference_shader_program_data(ctx
, &gl_prog
->sh
.data
, prog
->data
);
2484 /* Don't use _mesa_reference_program() just take ownership */
2485 linked
->Program
= gl_prog
;
2487 linked
->ir
= new(linked
) exec_list
;
2488 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
2490 link_fs_inout_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2491 link_tcs_out_layout_qualifiers(prog
, gl_prog
, shader_list
, num_shaders
);
2492 link_tes_in_layout_qualifiers(prog
, gl_prog
, shader_list
, num_shaders
);
2493 link_gs_inout_layout_qualifiers(prog
, gl_prog
, shader_list
, num_shaders
);
2494 link_cs_input_layout_qualifiers(prog
, gl_prog
, shader_list
, num_shaders
);
2496 if (linked
->Stage
!= MESA_SHADER_FRAGMENT
)
2497 link_xfb_stride_layout_qualifiers(ctx
, prog
, shader_list
, num_shaders
);
2499 link_bindless_layout_qualifiers(prog
, shader_list
, num_shaders
);
2501 link_layer_viewport_relative_qualifier(prog
, gl_prog
, shader_list
, num_shaders
);
2503 populate_symbol_table(linked
, shader_list
[0]->symbols
);
2505 /* The pointer to the main function in the final linked shader (i.e., the
2506 * copy of the original shader that contained the main function).
2508 ir_function_signature
*const main_sig
=
2509 _mesa_get_main_function_signature(linked
->symbols
);
2511 /* Move any instructions other than variable declarations or function
2512 * declarations into main.
2514 if (main_sig
!= NULL
) {
2515 exec_node
*insertion_point
=
2516 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
2519 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2520 if (shader_list
[i
] == main
)
2523 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
2524 insertion_point
, true, linked
);
2528 if (!link_function_calls(prog
, linked
, shader_list
, num_shaders
)) {
2529 _mesa_delete_linked_shader(ctx
, linked
);
2533 if (linked
->Stage
!= MESA_SHADER_FRAGMENT
)
2534 link_output_variables(linked
, shader_list
, num_shaders
);
2536 /* Make a pass over all variable declarations to ensure that arrays with
2537 * unspecified sizes have a size specified. The size is inferred from the
2538 * max_array_access field.
2540 array_sizing_visitor v
;
2542 v
.fixup_unnamed_interface_types();
2544 /* Link up uniform blocks defined within this stage. */
2545 link_uniform_blocks(mem_ctx
, ctx
, prog
, linked
, &ubo_blocks
,
2546 &num_ubo_blocks
, &ssbo_blocks
, &num_ssbo_blocks
);
2548 const unsigned max_uniform_blocks
=
2549 ctx
->Const
.Program
[linked
->Stage
].MaxUniformBlocks
;
2550 if (num_ubo_blocks
> max_uniform_blocks
) {
2551 linker_error(prog
, "Too many %s uniform blocks (%d/%d)\n",
2552 _mesa_shader_stage_to_string(linked
->Stage
),
2553 num_ubo_blocks
, max_uniform_blocks
);
2556 const unsigned max_shader_storage_blocks
=
2557 ctx
->Const
.Program
[linked
->Stage
].MaxShaderStorageBlocks
;
2558 if (num_ssbo_blocks
> max_shader_storage_blocks
) {
2559 linker_error(prog
, "Too many %s shader storage blocks (%d/%d)\n",
2560 _mesa_shader_stage_to_string(linked
->Stage
),
2561 num_ssbo_blocks
, max_shader_storage_blocks
);
2564 if (!prog
->data
->LinkStatus
) {
2565 _mesa_delete_linked_shader(ctx
, linked
);
2569 /* Copy ubo blocks to linked shader list */
2570 linked
->Program
->sh
.UniformBlocks
=
2571 ralloc_array(linked
, gl_uniform_block
*, num_ubo_blocks
);
2572 ralloc_steal(linked
, ubo_blocks
);
2573 for (unsigned i
= 0; i
< num_ubo_blocks
; i
++) {
2574 linked
->Program
->sh
.UniformBlocks
[i
] = &ubo_blocks
[i
];
2576 linked
->Program
->sh
.NumUniformBlocks
= num_ubo_blocks
;
2577 linked
->Program
->info
.num_ubos
= num_ubo_blocks
;
2579 /* Copy ssbo blocks to linked shader list */
2580 linked
->Program
->sh
.ShaderStorageBlocks
=
2581 ralloc_array(linked
, gl_uniform_block
*, num_ssbo_blocks
);
2582 ralloc_steal(linked
, ssbo_blocks
);
2583 for (unsigned i
= 0; i
< num_ssbo_blocks
; i
++) {
2584 linked
->Program
->sh
.ShaderStorageBlocks
[i
] = &ssbo_blocks
[i
];
2586 linked
->Program
->info
.num_ssbos
= num_ssbo_blocks
;
2588 /* At this point linked should contain all of the linked IR, so
2589 * validate it to make sure nothing went wrong.
2591 validate_ir_tree(linked
->ir
);
2593 /* Set the size of geometry shader input arrays */
2594 if (linked
->Stage
== MESA_SHADER_GEOMETRY
) {
2595 unsigned num_vertices
=
2596 vertices_per_prim(gl_prog
->info
.gs
.input_primitive
);
2597 array_resize_visitor
input_resize_visitor(num_vertices
, prog
,
2598 MESA_SHADER_GEOMETRY
);
2599 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2600 ir
->accept(&input_resize_visitor
);
2604 if (ctx
->Const
.VertexID_is_zero_based
)
2605 lower_vertex_id(linked
);
2607 if (ctx
->Const
.LowerCsDerivedVariables
)
2608 lower_cs_derived(linked
);
2611 /* Compute the source checksum. */
2612 linked
->SourceChecksum
= 0;
2613 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2614 if (shader_list
[i
] == NULL
)
2616 linked
->SourceChecksum
^= shader_list
[i
]->SourceChecksum
;
2624 * Update the sizes of linked shader uniform arrays to the maximum
2627 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
2629 * If one or more elements of an array are active,
2630 * GetActiveUniform will return the name of the array in name,
2631 * subject to the restrictions listed above. The type of the array
2632 * is returned in type. The size parameter contains the highest
2633 * array element index used, plus one. The compiler or linker
2634 * determines the highest index used. There will be only one
2635 * active uniform reported by the GL per uniform array.
2639 update_array_sizes(struct gl_shader_program
*prog
)
2641 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2642 if (prog
->_LinkedShaders
[i
] == NULL
)
2645 bool types_were_updated
= false;
2647 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
2648 ir_variable
*const var
= node
->as_variable();
2650 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
) ||
2651 !var
->type
->is_array())
2654 /* GL_ARB_uniform_buffer_object says that std140 uniforms
2655 * will not be eliminated. Since we always do std140, just
2656 * don't resize arrays in UBOs.
2658 * Atomic counters are supposed to get deterministic
2659 * locations assigned based on the declaration ordering and
2660 * sizes, array compaction would mess that up.
2662 * Subroutine uniforms are not removed.
2664 if (var
->is_in_buffer_block() || var
->type
->contains_atomic() ||
2665 var
->type
->contains_subroutine() || var
->constant_initializer
)
2668 int size
= var
->data
.max_array_access
;
2669 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
2670 if (prog
->_LinkedShaders
[j
] == NULL
)
2673 foreach_in_list(ir_instruction
, node2
, prog
->_LinkedShaders
[j
]->ir
) {
2674 ir_variable
*other_var
= node2
->as_variable();
2678 if (strcmp(var
->name
, other_var
->name
) == 0 &&
2679 other_var
->data
.max_array_access
> size
) {
2680 size
= other_var
->data
.max_array_access
;
2685 if (size
+ 1 != (int)var
->type
->length
) {
2686 /* If this is a built-in uniform (i.e., it's backed by some
2687 * fixed-function state), adjust the number of state slots to
2688 * match the new array size. The number of slots per array entry
2689 * is not known. It seems safe to assume that the total number of
2690 * slots is an integer multiple of the number of array elements.
2691 * Determine the number of slots per array element by dividing by
2692 * the old (total) size.
2694 const unsigned num_slots
= var
->get_num_state_slots();
2695 if (num_slots
> 0) {
2696 var
->set_num_state_slots((size
+ 1)
2697 * (num_slots
/ var
->type
->length
));
2700 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2702 types_were_updated
= true;
2706 /* Update the types of dereferences in case we changed any. */
2707 if (types_were_updated
) {
2708 deref_type_updater v
;
2709 v
.run(prog
->_LinkedShaders
[i
]->ir
);
2715 * Resize tessellation evaluation per-vertex inputs to the size of
2716 * tessellation control per-vertex outputs.
2719 resize_tes_inputs(struct gl_context
*ctx
,
2720 struct gl_shader_program
*prog
)
2722 if (prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] == NULL
)
2725 gl_linked_shader
*const tcs
= prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
];
2726 gl_linked_shader
*const tes
= prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
2728 /* If no control shader is present, then the TES inputs are statically
2729 * sized to MaxPatchVertices; the actual size of the arrays won't be
2730 * known until draw time.
2732 const int num_vertices
= tcs
2733 ? tcs
->Program
->info
.tess
.tcs_vertices_out
2734 : ctx
->Const
.MaxPatchVertices
;
2736 array_resize_visitor
input_resize_visitor(num_vertices
, prog
,
2737 MESA_SHADER_TESS_EVAL
);
2738 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2739 ir
->accept(&input_resize_visitor
);
2743 /* Convert the gl_PatchVerticesIn system value into a constant, since
2744 * the value is known at this point.
2746 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2747 ir_variable
*var
= ir
->as_variable();
2748 if (var
&& var
->data
.mode
== ir_var_system_value
&&
2749 var
->data
.location
== SYSTEM_VALUE_VERTICES_IN
) {
2750 void *mem_ctx
= ralloc_parent(var
);
2751 var
->data
.location
= 0;
2752 var
->data
.explicit_location
= false;
2753 var
->data
.mode
= ir_var_auto
;
2754 var
->constant_value
= new(mem_ctx
) ir_constant(num_vertices
);
2761 * Find a contiguous set of available bits in a bitmask.
2763 * \param used_mask Bits representing used (1) and unused (0) locations
2764 * \param needed_count Number of contiguous bits needed.
2767 * Base location of the available bits on success or -1 on failure.
2770 find_available_slots(unsigned used_mask
, unsigned needed_count
)
2772 unsigned needed_mask
= (1 << needed_count
) - 1;
2773 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
2775 /* The comparison to 32 is redundant, but without it GCC emits "warning:
2776 * cannot optimize possibly infinite loops" for the loop below.
2778 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
2781 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
2782 if ((needed_mask
& ~used_mask
) == needed_mask
)
2792 #define SAFE_MASK_FROM_INDEX(i) (((i) >= 32) ? ~0 : ((1 << (i)) - 1))
2795 * Assign locations for either VS inputs or FS outputs.
2797 * \param mem_ctx Temporary ralloc context used for linking.
2798 * \param prog Shader program whose variables need locations
2800 * \param constants Driver specific constant values for the program.
2801 * \param target_index Selector for the program target to receive location
2802 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
2803 * \c MESA_SHADER_FRAGMENT.
2804 * \param do_assignment Whether we are actually marking the assignment or we
2805 * are just doing a dry-run checking.
2808 * If locations are (or can be, in case of dry-running) successfully assigned,
2809 * true is returned. Otherwise an error is emitted to the shader link log and
2810 * false is returned.
2813 assign_attribute_or_color_locations(void *mem_ctx
,
2814 gl_shader_program
*prog
,
2815 struct gl_constants
*constants
,
2816 unsigned target_index
,
2819 /* Maximum number of generic locations. This corresponds to either the
2820 * maximum number of draw buffers or the maximum number of generic
2823 unsigned max_index
= (target_index
== MESA_SHADER_VERTEX
) ?
2824 constants
->Program
[target_index
].MaxAttribs
:
2825 MAX2(constants
->MaxDrawBuffers
, constants
->MaxDualSourceDrawBuffers
);
2827 /* Mark invalid locations as being used.
2829 unsigned used_locations
= ~SAFE_MASK_FROM_INDEX(max_index
);
2830 unsigned double_storage_locations
= 0;
2832 assert((target_index
== MESA_SHADER_VERTEX
)
2833 || (target_index
== MESA_SHADER_FRAGMENT
));
2835 gl_linked_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
2839 /* Operate in a total of four passes.
2841 * 1. Invalidate the location assignments for all vertex shader inputs.
2843 * 2. Assign locations for inputs that have user-defined (via
2844 * glBindVertexAttribLocation) locations and outputs that have
2845 * user-defined locations (via glBindFragDataLocation).
2847 * 3. Sort the attributes without assigned locations by number of slots
2848 * required in decreasing order. Fragmentation caused by attribute
2849 * locations assigned by the application may prevent large attributes
2850 * from having enough contiguous space.
2852 * 4. Assign locations to any inputs without assigned locations.
2855 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
2856 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
2858 const enum ir_variable_mode direction
=
2859 (target_index
== MESA_SHADER_VERTEX
)
2860 ? ir_var_shader_in
: ir_var_shader_out
;
2863 /* Temporary storage for the set of attributes that need locations assigned.
2869 /* Used below in the call to qsort. */
2870 static int compare(const void *a
, const void *b
)
2872 const temp_attr
*const l
= (const temp_attr
*) a
;
2873 const temp_attr
*const r
= (const temp_attr
*) b
;
2875 /* Reversed because we want a descending order sort below. */
2876 return r
->slots
- l
->slots
;
2879 assert(max_index
<= 32);
2881 /* Temporary array for the set of attributes that have locations assigned,
2882 * for the purpose of checking overlapping slots/components of (non-ES)
2883 * fragment shader outputs.
2885 ir_variable
*assigned
[12 * 4]; /* (max # of FS outputs) * # components */
2886 unsigned assigned_attr
= 0;
2888 unsigned num_attr
= 0;
2890 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2891 ir_variable
*const var
= node
->as_variable();
2893 if ((var
== NULL
) || (var
->data
.mode
!= (unsigned) direction
))
2896 if (var
->data
.explicit_location
) {
2897 var
->data
.is_unmatched_generic_inout
= 0;
2898 if ((var
->data
.location
>= (int)(max_index
+ generic_base
))
2899 || (var
->data
.location
< 0)) {
2901 "invalid explicit location %d specified for `%s'\n",
2902 (var
->data
.location
< 0)
2903 ? var
->data
.location
2904 : var
->data
.location
- generic_base
,
2908 } else if (target_index
== MESA_SHADER_VERTEX
) {
2911 if (prog
->AttributeBindings
->get(binding
, var
->name
)) {
2912 assert(binding
>= VERT_ATTRIB_GENERIC0
);
2913 var
->data
.location
= binding
;
2914 var
->data
.is_unmatched_generic_inout
= 0;
2916 } else if (target_index
== MESA_SHADER_FRAGMENT
) {
2919 const char *name
= var
->name
;
2920 const glsl_type
*type
= var
->type
;
2923 /* Check if there's a binding for the variable name */
2924 if (prog
->FragDataBindings
->get(binding
, name
)) {
2925 assert(binding
>= FRAG_RESULT_DATA0
);
2926 var
->data
.location
= binding
;
2927 var
->data
.is_unmatched_generic_inout
= 0;
2929 if (prog
->FragDataIndexBindings
->get(index
, name
)) {
2930 var
->data
.index
= index
;
2935 /* If not, but it's an array type, look for name[0] */
2936 if (type
->is_array()) {
2937 name
= ralloc_asprintf(mem_ctx
, "%s[0]", name
);
2938 type
= type
->fields
.array
;
2946 if (strcmp(var
->name
, "gl_LastFragData") == 0)
2949 /* From GL4.5 core spec, section 15.2 (Shader Execution):
2951 * "Output binding assignments will cause LinkProgram to fail:
2953 * If the program has an active output assigned to a location greater
2954 * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
2955 * an active output assigned an index greater than or equal to one;"
2957 if (target_index
== MESA_SHADER_FRAGMENT
&& var
->data
.index
>= 1 &&
2958 var
->data
.location
- generic_base
>=
2959 (int) constants
->MaxDualSourceDrawBuffers
) {
2961 "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
2962 "with index %u for %s\n",
2963 var
->data
.location
- generic_base
, var
->data
.index
,
2968 const unsigned slots
= var
->type
->count_attribute_slots(target_index
== MESA_SHADER_VERTEX
);
2970 /* If the variable is not a built-in and has a location statically
2971 * assigned in the shader (presumably via a layout qualifier), make sure
2972 * that it doesn't collide with other assigned locations. Otherwise,
2973 * add it to the list of variables that need linker-assigned locations.
2975 if (var
->data
.location
!= -1) {
2976 if (var
->data
.location
>= generic_base
&& var
->data
.index
< 1) {
2977 /* From page 61 of the OpenGL 4.0 spec:
2979 * "LinkProgram will fail if the attribute bindings assigned
2980 * by BindAttribLocation do not leave not enough space to
2981 * assign a location for an active matrix attribute or an
2982 * active attribute array, both of which require multiple
2983 * contiguous generic attributes."
2985 * I think above text prohibits the aliasing of explicit and
2986 * automatic assignments. But, aliasing is allowed in manual
2987 * assignments of attribute locations. See below comments for
2990 * From OpenGL 4.0 spec, page 61:
2992 * "It is possible for an application to bind more than one
2993 * attribute name to the same location. This is referred to as
2994 * aliasing. This will only work if only one of the aliased
2995 * attributes is active in the executable program, or if no
2996 * path through the shader consumes more than one attribute of
2997 * a set of attributes aliased to the same location. A link
2998 * error can occur if the linker determines that every path
2999 * through the shader consumes multiple aliased attributes,
3000 * but implementations are not required to generate an error
3003 * From GLSL 4.30 spec, page 54:
3005 * "A program will fail to link if any two non-vertex shader
3006 * input variables are assigned to the same location. For
3007 * vertex shaders, multiple input variables may be assigned
3008 * to the same location using either layout qualifiers or via
3009 * the OpenGL API. However, such aliasing is intended only to
3010 * support vertex shaders where each execution path accesses
3011 * at most one input per each location. Implementations are
3012 * permitted, but not required, to generate link-time errors
3013 * if they detect that every path through the vertex shader
3014 * executable accesses multiple inputs assigned to any single
3015 * location. For all shader types, a program will fail to link
3016 * if explicit location assignments leave the linker unable
3017 * to find space for other variables without explicit
3020 * From OpenGL ES 3.0 spec, page 56:
3022 * "Binding more than one attribute name to the same location
3023 * is referred to as aliasing, and is not permitted in OpenGL
3024 * ES Shading Language 3.00 vertex shaders. LinkProgram will
3025 * fail when this condition exists. However, aliasing is
3026 * possible in OpenGL ES Shading Language 1.00 vertex shaders.
3027 * This will only work if only one of the aliased attributes
3028 * is active in the executable program, or if no path through
3029 * the shader consumes more than one attribute of a set of
3030 * attributes aliased to the same location. A link error can
3031 * occur if the linker determines that every path through the
3032 * shader consumes multiple aliased attributes, but implemen-
3033 * tations are not required to generate an error in this case."
3035 * After looking at above references from OpenGL, OpenGL ES and
3036 * GLSL specifications, we allow aliasing of vertex input variables
3037 * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
3039 * NOTE: This is not required by the spec but its worth mentioning
3040 * here that we're not doing anything to make sure that no path
3041 * through the vertex shader executable accesses multiple inputs
3042 * assigned to any single location.
3045 /* Mask representing the contiguous slots that will be used by
3048 const unsigned attr
= var
->data
.location
- generic_base
;
3049 const unsigned use_mask
= (1 << slots
) - 1;
3050 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
3051 ? "vertex shader input" : "fragment shader output";
3053 /* Generate a link error if the requested locations for this
3054 * attribute exceed the maximum allowed attribute location.
3056 if (attr
+ slots
> max_index
) {
3058 "insufficient contiguous locations "
3059 "available for %s `%s' %d %d %d\n", string
,
3060 var
->name
, used_locations
, use_mask
, attr
);
3064 /* Generate a link error if the set of bits requested for this
3065 * attribute overlaps any previously allocated bits.
3067 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
3068 if (target_index
== MESA_SHADER_FRAGMENT
&& !prog
->IsES
) {
3069 /* From section 4.4.2 (Output Layout Qualifiers) of the GLSL
3072 * "Additionally, for fragment shader outputs, if two
3073 * variables are placed within the same location, they
3074 * must have the same underlying type (floating-point or
3075 * integer). No component aliasing of output variables or
3076 * members is allowed.
3078 for (unsigned i
= 0; i
< assigned_attr
; i
++) {
3079 unsigned assigned_slots
=
3080 assigned
[i
]->type
->count_attribute_slots(false);
3081 unsigned assig_attr
=
3082 assigned
[i
]->data
.location
- generic_base
;
3083 unsigned assigned_use_mask
= (1 << assigned_slots
) - 1;
3085 if ((assigned_use_mask
<< assig_attr
) &
3086 (use_mask
<< attr
)) {
3088 const glsl_type
*assigned_type
=
3089 assigned
[i
]->type
->without_array();
3090 const glsl_type
*type
= var
->type
->without_array();
3091 if (assigned_type
->base_type
!= type
->base_type
) {
3092 linker_error(prog
, "types do not match for aliased"
3093 " %ss %s and %s\n", string
,
3094 assigned
[i
]->name
, var
->name
);
3098 unsigned assigned_component_mask
=
3099 ((1 << assigned_type
->vector_elements
) - 1) <<
3100 assigned
[i
]->data
.location_frac
;
3101 unsigned component_mask
=
3102 ((1 << type
->vector_elements
) - 1) <<
3103 var
->data
.location_frac
;
3104 if (assigned_component_mask
& component_mask
) {
3105 linker_error(prog
, "overlapping component is "
3106 "assigned to %ss %s and %s "
3108 string
, assigned
[i
]->name
, var
->name
,
3109 var
->data
.location_frac
);
3114 } else if (target_index
== MESA_SHADER_FRAGMENT
||
3115 (prog
->IsES
&& prog
->data
->Version
>= 300)) {
3116 linker_error(prog
, "overlapping location is assigned "
3117 "to %s `%s' %d %d %d\n", string
, var
->name
,
3118 used_locations
, use_mask
, attr
);
3121 linker_warning(prog
, "overlapping location is assigned "
3122 "to %s `%s' %d %d %d\n", string
, var
->name
,
3123 used_locations
, use_mask
, attr
);
3127 if (target_index
== MESA_SHADER_FRAGMENT
&& !prog
->IsES
) {
3128 /* Only track assigned variables for non-ES fragment shaders
3129 * to avoid overflowing the array.
3131 * At most one variable per fragment output component should
3134 assert(assigned_attr
< ARRAY_SIZE(assigned
));
3135 assigned
[assigned_attr
] = var
;
3139 used_locations
|= (use_mask
<< attr
);
3141 /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
3143 * "A program with more than the value of MAX_VERTEX_ATTRIBS
3144 * active attribute variables may fail to link, unless
3145 * device-dependent optimizations are able to make the program
3146 * fit within available hardware resources. For the purposes
3147 * of this test, attribute variables of the type dvec3, dvec4,
3148 * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
3149 * count as consuming twice as many attributes as equivalent
3150 * single-precision types. While these types use the same number
3151 * of generic attributes as their single-precision equivalents,
3152 * implementations are permitted to consume two single-precision
3153 * vectors of internal storage for each three- or four-component
3154 * double-precision vector."
3156 * Mark this attribute slot as taking up twice as much space
3157 * so we can count it properly against limits. According to
3158 * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
3159 * is optional behavior, but it seems preferable.
3161 if (var
->type
->without_array()->is_dual_slot())
3162 double_storage_locations
|= (use_mask
<< attr
);
3168 if (num_attr
>= max_index
) {
3169 linker_error(prog
, "too many %s (max %u)",
3170 target_index
== MESA_SHADER_VERTEX
?
3171 "vertex shader inputs" : "fragment shader outputs",
3175 to_assign
[num_attr
].slots
= slots
;
3176 to_assign
[num_attr
].var
= var
;
3183 if (target_index
== MESA_SHADER_VERTEX
) {
3184 unsigned total_attribs_size
=
3185 util_bitcount(used_locations
& SAFE_MASK_FROM_INDEX(max_index
)) +
3186 util_bitcount(double_storage_locations
);
3187 if (total_attribs_size
> max_index
) {
3189 "attempt to use %d vertex attribute slots only %d available ",
3190 total_attribs_size
, max_index
);
3195 /* If all of the attributes were assigned locations by the application (or
3196 * are built-in attributes with fixed locations), return early. This should
3197 * be the common case.
3202 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
3204 if (target_index
== MESA_SHADER_VERTEX
) {
3205 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
3206 * only be explicitly assigned by via glBindAttribLocation. Mark it as
3207 * reserved to prevent it from being automatically allocated below.
3209 find_deref_visitor
find("gl_Vertex");
3211 if (find
.variable_found())
3212 used_locations
|= (1 << 0);
3215 for (unsigned i
= 0; i
< num_attr
; i
++) {
3216 /* Mask representing the contiguous slots that will be used by this
3219 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
3221 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
3224 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
3225 ? "vertex shader input" : "fragment shader output";
3228 "insufficient contiguous locations "
3229 "available for %s `%s'\n",
3230 string
, to_assign
[i
].var
->name
);
3234 to_assign
[i
].var
->data
.location
= generic_base
+ location
;
3235 to_assign
[i
].var
->data
.is_unmatched_generic_inout
= 0;
3236 used_locations
|= (use_mask
<< location
);
3238 if (to_assign
[i
].var
->type
->without_array()->is_dual_slot())
3239 double_storage_locations
|= (use_mask
<< location
);
3242 /* Now that we have all the locations, from the GL 4.5 core spec, section
3243 * 11.1.1 (Vertex Attributes), dvec3, dvec4, dmat2x3, dmat2x4, dmat3,
3244 * dmat3x4, dmat4x3, and dmat4 count as consuming twice as many attributes
3245 * as equivalent single-precision types.
3247 if (target_index
== MESA_SHADER_VERTEX
) {
3248 unsigned total_attribs_size
=
3249 util_bitcount(used_locations
& SAFE_MASK_FROM_INDEX(max_index
)) +
3250 util_bitcount(double_storage_locations
);
3251 if (total_attribs_size
> max_index
) {
3253 "attempt to use %d vertex attribute slots only %d available ",
3254 total_attribs_size
, max_index
);
3263 * Match explicit locations of outputs to inputs and deactivate the
3264 * unmatch flag if found so we don't optimise them away.
3267 match_explicit_outputs_to_inputs(gl_linked_shader
*producer
,
3268 gl_linked_shader
*consumer
)
3270 glsl_symbol_table parameters
;
3271 ir_variable
*explicit_locations
[MAX_VARYINGS_INCL_PATCH
][4] =
3274 /* Find all shader outputs in the "producer" stage.
3276 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
3277 ir_variable
*const var
= node
->as_variable();
3279 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_shader_out
))
3282 if (var
->data
.explicit_location
&&
3283 var
->data
.location
>= VARYING_SLOT_VAR0
) {
3284 const unsigned idx
= var
->data
.location
- VARYING_SLOT_VAR0
;
3285 if (explicit_locations
[idx
][var
->data
.location_frac
] == NULL
)
3286 explicit_locations
[idx
][var
->data
.location_frac
] = var
;
3288 /* Always match TCS outputs. They are shared by all invocations
3289 * within a patch and can be used as shared memory.
3291 if (producer
->Stage
== MESA_SHADER_TESS_CTRL
)
3292 var
->data
.is_unmatched_generic_inout
= 0;
3296 /* Match inputs to outputs */
3297 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
3298 ir_variable
*const input
= node
->as_variable();
3300 if ((input
== NULL
) || (input
->data
.mode
!= ir_var_shader_in
))
3303 ir_variable
*output
= NULL
;
3304 if (input
->data
.explicit_location
3305 && input
->data
.location
>= VARYING_SLOT_VAR0
) {
3306 output
= explicit_locations
[input
->data
.location
- VARYING_SLOT_VAR0
]
3307 [input
->data
.location_frac
];
3309 if (output
!= NULL
){
3310 input
->data
.is_unmatched_generic_inout
= 0;
3311 output
->data
.is_unmatched_generic_inout
= 0;
3318 * Store the gl_FragDepth layout in the gl_shader_program struct.
3321 store_fragdepth_layout(struct gl_shader_program
*prog
)
3323 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
3327 struct exec_list
*ir
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
3329 /* We don't look up the gl_FragDepth symbol directly because if
3330 * gl_FragDepth is not used in the shader, it's removed from the IR.
3331 * However, the symbol won't be removed from the symbol table.
3333 * We're only interested in the cases where the variable is NOT removed
3336 foreach_in_list(ir_instruction
, node
, ir
) {
3337 ir_variable
*const var
= node
->as_variable();
3339 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
) {
3343 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
3344 switch (var
->data
.depth_layout
) {
3345 case ir_depth_layout_none
:
3346 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
3348 case ir_depth_layout_any
:
3349 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
3351 case ir_depth_layout_greater
:
3352 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
3354 case ir_depth_layout_less
:
3355 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
3357 case ir_depth_layout_unchanged
:
3358 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3369 * Validate shader image resources.
3372 check_image_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3374 unsigned total_image_units
= 0;
3375 unsigned fragment_outputs
= 0;
3376 unsigned total_shader_storage_blocks
= 0;
3378 if (!ctx
->Extensions
.ARB_shader_image_load_store
)
3381 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3382 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3385 total_image_units
+= sh
->Program
->info
.num_images
;
3386 total_shader_storage_blocks
+= sh
->Program
->info
.num_ssbos
;
3388 if (i
== MESA_SHADER_FRAGMENT
) {
3389 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3390 ir_variable
*var
= node
->as_variable();
3391 if (var
&& var
->data
.mode
== ir_var_shader_out
)
3392 /* since there are no double fs outputs - pass false */
3393 fragment_outputs
+= var
->type
->count_attribute_slots(false);
3399 if (total_image_units
> ctx
->Const
.MaxCombinedImageUniforms
)
3400 linker_error(prog
, "Too many combined image uniforms\n");
3402 if (total_image_units
+ fragment_outputs
+ total_shader_storage_blocks
>
3403 ctx
->Const
.MaxCombinedShaderOutputResources
)
3404 linker_error(prog
, "Too many combined image uniforms, shader storage "
3405 " buffers and fragment outputs\n");
3410 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
3411 * for a variable, checks for overlaps between other uniforms using explicit
3415 reserve_explicit_locations(struct gl_shader_program
*prog
,
3416 string_to_uint_map
*map
, ir_variable
*var
)
3418 unsigned slots
= var
->type
->uniform_locations();
3419 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3420 unsigned return_value
= slots
;
3422 /* Resize remap table if locations do not fit in the current one. */
3423 if (max_loc
+ 1 > prog
->NumUniformRemapTable
) {
3424 prog
->UniformRemapTable
=
3425 reralloc(prog
, prog
->UniformRemapTable
,
3426 gl_uniform_storage
*,
3429 if (!prog
->UniformRemapTable
) {
3430 linker_error(prog
, "Out of memory during linking.\n");
3434 /* Initialize allocated space. */
3435 for (unsigned i
= prog
->NumUniformRemapTable
; i
< max_loc
+ 1; i
++)
3436 prog
->UniformRemapTable
[i
] = NULL
;
3438 prog
->NumUniformRemapTable
= max_loc
+ 1;
3441 for (unsigned i
= 0; i
< slots
; i
++) {
3442 unsigned loc
= var
->data
.location
+ i
;
3444 /* Check if location is already used. */
3445 if (prog
->UniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3447 /* Possibly same uniform from a different stage, this is ok. */
3449 if (map
->get(hash_loc
, var
->name
) && hash_loc
== loc
- i
) {
3454 /* ARB_explicit_uniform_location specification states:
3456 * "No two default-block uniform variables in the program can have
3457 * the same location, even if they are unused, otherwise a compiler
3458 * or linker error will be generated."
3461 "location qualifier for uniform %s overlaps "
3462 "previously used location\n",
3467 /* Initialize location as inactive before optimization
3468 * rounds and location assignment.
3470 prog
->UniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3473 /* Note, base location used for arrays. */
3474 map
->put(var
->data
.location
, var
->name
);
3476 return return_value
;
3480 reserve_subroutine_explicit_locations(struct gl_shader_program
*prog
,
3481 struct gl_program
*p
,
3484 unsigned slots
= var
->type
->uniform_locations();
3485 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3487 /* Resize remap table if locations do not fit in the current one. */
3488 if (max_loc
+ 1 > p
->sh
.NumSubroutineUniformRemapTable
) {
3489 p
->sh
.SubroutineUniformRemapTable
=
3490 reralloc(p
, p
->sh
.SubroutineUniformRemapTable
,
3491 gl_uniform_storage
*,
3494 if (!p
->sh
.SubroutineUniformRemapTable
) {
3495 linker_error(prog
, "Out of memory during linking.\n");
3499 /* Initialize allocated space. */
3500 for (unsigned i
= p
->sh
.NumSubroutineUniformRemapTable
; i
< max_loc
+ 1; i
++)
3501 p
->sh
.SubroutineUniformRemapTable
[i
] = NULL
;
3503 p
->sh
.NumSubroutineUniformRemapTable
= max_loc
+ 1;
3506 for (unsigned i
= 0; i
< slots
; i
++) {
3507 unsigned loc
= var
->data
.location
+ i
;
3509 /* Check if location is already used. */
3510 if (p
->sh
.SubroutineUniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3512 /* ARB_explicit_uniform_location specification states:
3513 * "No two subroutine uniform variables can have the same location
3514 * in the same shader stage, otherwise a compiler or linker error
3515 * will be generated."
3518 "location qualifier for uniform %s overlaps "
3519 "previously used location\n",
3524 /* Initialize location as inactive before optimization
3525 * rounds and location assignment.
3527 p
->sh
.SubroutineUniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3533 * Check and reserve all explicit uniform locations, called before
3534 * any optimizations happen to handle also inactive uniforms and
3535 * inactive array elements that may get trimmed away.
3538 check_explicit_uniform_locations(struct gl_context
*ctx
,
3539 struct gl_shader_program
*prog
)
3541 prog
->NumExplicitUniformLocations
= 0;
3543 if (!ctx
->Extensions
.ARB_explicit_uniform_location
)
3546 /* This map is used to detect if overlapping explicit locations
3547 * occur with the same uniform (from different stage) or a different one.
3549 string_to_uint_map
*uniform_map
= new string_to_uint_map
;
3552 linker_error(prog
, "Out of memory during linking.\n");
3556 unsigned entries_total
= 0;
3557 unsigned mask
= prog
->data
->linked_stages
;
3559 const int i
= u_bit_scan(&mask
);
3560 struct gl_program
*p
= prog
->_LinkedShaders
[i
]->Program
;
3562 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
3563 ir_variable
*var
= node
->as_variable();
3564 if (!var
|| var
->data
.mode
!= ir_var_uniform
)
3567 if (var
->data
.explicit_location
) {
3569 if (var
->type
->without_array()->is_subroutine())
3570 ret
= reserve_subroutine_explicit_locations(prog
, p
, var
);
3572 int slots
= reserve_explicit_locations(prog
, uniform_map
,
3576 entries_total
+= slots
;
3587 link_util_update_empty_uniform_locations(prog
);
3590 prog
->NumExplicitUniformLocations
= entries_total
;
3593 /* Function checks if a variable var is a packed varying and
3594 * if given name is part of packed varying's list.
3596 * If a variable is a packed varying, it has a name like
3597 * 'packed:a,b,c' where a, b and c are separate variables.
3600 included_in_packed_varying(ir_variable
*var
, const char *name
)
3602 if (strncmp(var
->name
, "packed:", 7) != 0)
3605 char *list
= strdup(var
->name
+ 7);
3610 char *token
= strtok_r(list
, ",", &saveptr
);
3612 if (strcmp(token
, name
) == 0) {
3616 token
= strtok_r(NULL
, ",", &saveptr
);
3623 * Function builds a stage reference bitmask from variable name.
3626 build_stageref(struct gl_shader_program
*shProg
, const char *name
,
3631 /* Note, that we assume MAX 8 stages, if there will be more stages, type
3632 * used for reference mask in gl_program_resource will need to be changed.
3634 assert(MESA_SHADER_STAGES
< 8);
3636 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3637 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
3641 /* Shader symbol table may contain variables that have
3642 * been optimized away. Search IR for the variable instead.
3644 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3645 ir_variable
*var
= node
->as_variable();
3647 unsigned baselen
= strlen(var
->name
);
3649 if (included_in_packed_varying(var
, name
)) {
3654 /* Type needs to match if specified, otherwise we might
3655 * pick a variable with same name but different interface.
3657 if (var
->data
.mode
!= mode
)
3660 if (strncmp(var
->name
, name
, baselen
) == 0) {
3661 /* Check for exact name matches but also check for arrays and
3664 if (name
[baselen
] == '\0' ||
3665 name
[baselen
] == '[' ||
3666 name
[baselen
] == '.') {
3678 * Create gl_shader_variable from ir_variable class.
3680 static gl_shader_variable
*
3681 create_shader_variable(struct gl_shader_program
*shProg
,
3682 const ir_variable
*in
,
3683 const char *name
, const glsl_type
*type
,
3684 const glsl_type
*interface_type
,
3685 bool use_implicit_location
, int location
,
3686 const glsl_type
*outermost_struct_type
)
3688 /* Allocate zero-initialized memory to ensure that bitfield padding
3691 gl_shader_variable
*out
= rzalloc(shProg
, struct gl_shader_variable
);
3695 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
3696 * expect to see gl_VertexID in the program resource list. Pretend.
3698 if (in
->data
.mode
== ir_var_system_value
&&
3699 in
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
3700 out
->name
= ralloc_strdup(shProg
, "gl_VertexID");
3701 } else if ((in
->data
.mode
== ir_var_shader_out
&&
3702 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
) ||
3703 (in
->data
.mode
== ir_var_system_value
&&
3704 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_OUTER
)) {
3705 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelOuter");
3706 type
= glsl_type::get_array_instance(glsl_type::float_type
, 4);
3707 } else if ((in
->data
.mode
== ir_var_shader_out
&&
3708 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
) ||
3709 (in
->data
.mode
== ir_var_system_value
&&
3710 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_INNER
)) {
3711 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelInner");
3712 type
= glsl_type::get_array_instance(glsl_type::float_type
, 2);
3714 out
->name
= ralloc_strdup(shProg
, name
);
3720 /* The ARB_program_interface_query spec says:
3722 * "Not all active variables are assigned valid locations; the
3723 * following variables will have an effective location of -1:
3725 * * uniforms declared as atomic counters;
3727 * * members of a uniform block;
3729 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
3731 * * inputs or outputs not declared with a "location" layout
3732 * qualifier, except for vertex shader inputs and fragment shader
3735 if (in
->type
->is_atomic_uint() || is_gl_identifier(in
->name
) ||
3736 !(in
->data
.explicit_location
|| use_implicit_location
)) {
3739 out
->location
= location
;
3743 out
->outermost_struct_type
= outermost_struct_type
;
3744 out
->interface_type
= interface_type
;
3745 out
->component
= in
->data
.location_frac
;
3746 out
->index
= in
->data
.index
;
3747 out
->patch
= in
->data
.patch
;
3748 out
->mode
= in
->data
.mode
;
3749 out
->interpolation
= in
->data
.interpolation
;
3750 out
->explicit_location
= in
->data
.explicit_location
;
3751 out
->precision
= in
->data
.precision
;
3757 add_shader_variable(const struct gl_context
*ctx
,
3758 struct gl_shader_program
*shProg
,
3759 struct set
*resource_set
,
3760 unsigned stage_mask
,
3761 GLenum programInterface
, ir_variable
*var
,
3762 const char *name
, const glsl_type
*type
,
3763 bool use_implicit_location
, int location
,
3764 bool inouts_share_location
,
3765 const glsl_type
*outermost_struct_type
= NULL
)
3767 const glsl_type
*interface_type
= var
->get_interface_type();
3769 if (outermost_struct_type
== NULL
) {
3770 if (var
->data
.from_named_ifc_block
) {
3771 const char *interface_name
= interface_type
->name
;
3773 if (interface_type
->is_array()) {
3774 /* Issue #16 of the ARB_program_interface_query spec says:
3776 * "* If a variable is a member of an interface block without an
3777 * instance name, it is enumerated using just the variable name.
3779 * * If a variable is a member of an interface block with an
3780 * instance name, it is enumerated as "BlockName.Member", where
3781 * "BlockName" is the name of the interface block (not the
3782 * instance name) and "Member" is the name of the variable."
3784 * In particular, it indicates that it should be "BlockName",
3785 * not "BlockName[array length]". The conformance suite and
3786 * dEQP both require this behavior.
3788 * Here, we unwrap the extra array level added by named interface
3789 * block array lowering so we have the correct variable type. We
3790 * also unwrap the interface type when constructing the name.
3792 * We leave interface_type the same so that ES 3.x SSO pipeline
3793 * validation can enforce the rules requiring array length to
3794 * match on interface blocks.
3796 type
= type
->fields
.array
;
3798 interface_name
= interface_type
->fields
.array
->name
;
3801 name
= ralloc_asprintf(shProg
, "%s.%s", interface_name
, name
);
3805 switch (type
->base_type
) {
3806 case GLSL_TYPE_STRUCT
: {
3807 /* The ARB_program_interface_query spec says:
3809 * "For an active variable declared as a structure, a separate entry
3810 * will be generated for each active structure member. The name of
3811 * each entry is formed by concatenating the name of the structure,
3812 * the "." character, and the name of the structure member. If a
3813 * structure member to enumerate is itself a structure or array,
3814 * these enumeration rules are applied recursively."
3816 if (outermost_struct_type
== NULL
)
3817 outermost_struct_type
= type
;
3819 unsigned field_location
= location
;
3820 for (unsigned i
= 0; i
< type
->length
; i
++) {
3821 const struct glsl_struct_field
*field
= &type
->fields
.structure
[i
];
3822 char *field_name
= ralloc_asprintf(shProg
, "%s.%s", name
, field
->name
);
3823 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3824 stage_mask
, programInterface
,
3825 var
, field_name
, field
->type
,
3826 use_implicit_location
, field_location
,
3827 false, outermost_struct_type
))
3830 field_location
+= field
->type
->count_attribute_slots(false);
3835 case GLSL_TYPE_ARRAY
: {
3836 /* The ARB_program_interface_query spec says:
3838 * "For an active variable declared as an array of basic types, a
3839 * single entry will be generated, with its name string formed by
3840 * concatenating the name of the array and the string "[0]"."
3842 * "For an active variable declared as an array of an aggregate data
3843 * type (structures or arrays), a separate entry will be generated
3844 * for each active array element, unless noted immediately below.
3845 * The name of each entry is formed by concatenating the name of
3846 * the array, the "[" character, an integer identifying the element
3847 * number, and the "]" character. These enumeration rules are
3848 * applied recursively, treating each enumerated array element as a
3849 * separate active variable."
3851 const struct glsl_type
*array_type
= type
->fields
.array
;
3852 if (array_type
->base_type
== GLSL_TYPE_STRUCT
||
3853 array_type
->base_type
== GLSL_TYPE_ARRAY
) {
3854 unsigned elem_location
= location
;
3855 unsigned stride
= inouts_share_location
? 0 :
3856 array_type
->count_attribute_slots(false);
3857 for (unsigned i
= 0; i
< type
->length
; i
++) {
3858 char *elem
= ralloc_asprintf(shProg
, "%s[%d]", name
, i
);
3859 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3860 stage_mask
, programInterface
,
3861 var
, elem
, array_type
,
3862 use_implicit_location
, elem_location
,
3863 false, outermost_struct_type
))
3865 elem_location
+= stride
;
3873 /* The ARB_program_interface_query spec says:
3875 * "For an active variable declared as a single instance of a basic
3876 * type, a single entry will be generated, using the variable name
3877 * from the shader source."
3879 gl_shader_variable
*sha_v
=
3880 create_shader_variable(shProg
, var
, name
, type
, interface_type
,
3881 use_implicit_location
, location
,
3882 outermost_struct_type
);
3886 return link_util_add_program_resource(shProg
, resource_set
,
3887 programInterface
, sha_v
, stage_mask
);
3893 inout_has_same_location(const ir_variable
*var
, unsigned stage
)
3895 if (!var
->data
.patch
&&
3896 ((var
->data
.mode
== ir_var_shader_out
&&
3897 stage
== MESA_SHADER_TESS_CTRL
) ||
3898 (var
->data
.mode
== ir_var_shader_in
&&
3899 (stage
== MESA_SHADER_TESS_CTRL
|| stage
== MESA_SHADER_TESS_EVAL
||
3900 stage
== MESA_SHADER_GEOMETRY
))))
3907 add_interface_variables(const struct gl_context
*ctx
,
3908 struct gl_shader_program
*shProg
,
3909 struct set
*resource_set
,
3910 unsigned stage
, GLenum programInterface
)
3912 exec_list
*ir
= shProg
->_LinkedShaders
[stage
]->ir
;
3914 foreach_in_list(ir_instruction
, node
, ir
) {
3915 ir_variable
*var
= node
->as_variable();
3917 if (!var
|| var
->data
.how_declared
== ir_var_hidden
)
3922 switch (var
->data
.mode
) {
3923 case ir_var_system_value
:
3924 case ir_var_shader_in
:
3925 if (programInterface
!= GL_PROGRAM_INPUT
)
3927 loc_bias
= (stage
== MESA_SHADER_VERTEX
) ? int(VERT_ATTRIB_GENERIC0
)
3928 : int(VARYING_SLOT_VAR0
);
3930 case ir_var_shader_out
:
3931 if (programInterface
!= GL_PROGRAM_OUTPUT
)
3933 loc_bias
= (stage
== MESA_SHADER_FRAGMENT
) ? int(FRAG_RESULT_DATA0
)
3934 : int(VARYING_SLOT_VAR0
);
3940 if (var
->data
.patch
)
3941 loc_bias
= int(VARYING_SLOT_PATCH0
);
3943 /* Skip packed varyings, packed varyings are handled separately
3944 * by add_packed_varyings.
3946 if (strncmp(var
->name
, "packed:", 7) == 0)
3949 /* Skip fragdata arrays, these are handled separately
3950 * by add_fragdata_arrays.
3952 if (strncmp(var
->name
, "gl_out_FragData", 15) == 0)
3955 const bool vs_input_or_fs_output
=
3956 (stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
3957 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
);
3959 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3960 1 << stage
, programInterface
,
3961 var
, var
->name
, var
->type
, vs_input_or_fs_output
,
3962 var
->data
.location
- loc_bias
,
3963 inout_has_same_location(var
, stage
)))
3970 add_packed_varyings(const struct gl_context
*ctx
,
3971 struct gl_shader_program
*shProg
,
3972 struct set
*resource_set
,
3973 int stage
, GLenum type
)
3975 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[stage
];
3978 if (!sh
|| !sh
->packed_varyings
)
3981 foreach_in_list(ir_instruction
, node
, sh
->packed_varyings
) {
3982 ir_variable
*var
= node
->as_variable();
3984 switch (var
->data
.mode
) {
3985 case ir_var_shader_in
:
3986 iface
= GL_PROGRAM_INPUT
;
3988 case ir_var_shader_out
:
3989 iface
= GL_PROGRAM_OUTPUT
;
3992 unreachable("unexpected type");
3995 if (type
== iface
) {
3996 const int stage_mask
=
3997 build_stageref(shProg
, var
->name
, var
->data
.mode
);
3998 if (!add_shader_variable(ctx
, shProg
, resource_set
,
4000 iface
, var
, var
->name
, var
->type
, false,
4001 var
->data
.location
- VARYING_SLOT_VAR0
,
4002 inout_has_same_location(var
, stage
)))
4011 add_fragdata_arrays(const struct gl_context
*ctx
,
4012 struct gl_shader_program
*shProg
,
4013 struct set
*resource_set
)
4015 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4017 if (!sh
|| !sh
->fragdata_arrays
)
4020 foreach_in_list(ir_instruction
, node
, sh
->fragdata_arrays
) {
4021 ir_variable
*var
= node
->as_variable();
4023 assert(var
->data
.mode
== ir_var_shader_out
);
4025 if (!add_shader_variable(ctx
, shProg
, resource_set
,
4026 1 << MESA_SHADER_FRAGMENT
,
4027 GL_PROGRAM_OUTPUT
, var
, var
->name
, var
->type
,
4028 true, var
->data
.location
- FRAG_RESULT_DATA0
,
4037 * Builds up a list of program resources that point to existing
4041 build_program_resource_list(struct gl_context
*ctx
,
4042 struct gl_shader_program
*shProg
,
4043 bool add_packed_varyings_only
)
4045 /* Rebuild resource list. */
4046 if (shProg
->data
->ProgramResourceList
) {
4047 ralloc_free(shProg
->data
->ProgramResourceList
);
4048 shProg
->data
->ProgramResourceList
= NULL
;
4049 shProg
->data
->NumProgramResourceList
= 0;
4052 int input_stage
= MESA_SHADER_STAGES
, output_stage
= 0;
4054 /* Determine first input and final output stage. These are used to
4055 * detect which variables should be enumerated in the resource list
4056 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
4058 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4059 if (!shProg
->_LinkedShaders
[i
])
4061 if (input_stage
== MESA_SHADER_STAGES
)
4066 /* Empty shader, no resources. */
4067 if (input_stage
== MESA_SHADER_STAGES
&& output_stage
== 0)
4070 struct set
*resource_set
= _mesa_pointer_set_create(NULL
);
4072 /* Program interface needs to expose varyings in case of SSO. */
4073 if (shProg
->SeparateShader
) {
4074 if (!add_packed_varyings(ctx
, shProg
, resource_set
,
4075 input_stage
, GL_PROGRAM_INPUT
))
4078 if (!add_packed_varyings(ctx
, shProg
, resource_set
,
4079 output_stage
, GL_PROGRAM_OUTPUT
))
4083 if (add_packed_varyings_only
) {
4084 _mesa_set_destroy(resource_set
, NULL
);
4088 if (!add_fragdata_arrays(ctx
, shProg
, resource_set
))
4091 /* Add inputs and outputs to the resource list. */
4092 if (!add_interface_variables(ctx
, shProg
, resource_set
,
4093 input_stage
, GL_PROGRAM_INPUT
))
4096 if (!add_interface_variables(ctx
, shProg
, resource_set
,
4097 output_stage
, GL_PROGRAM_OUTPUT
))
4100 if (shProg
->last_vert_prog
) {
4101 struct gl_transform_feedback_info
*linked_xfb
=
4102 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
4104 /* Add transform feedback varyings. */
4105 if (linked_xfb
->NumVarying
> 0) {
4106 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
4107 if (!link_util_add_program_resource(shProg
, resource_set
,
4108 GL_TRANSFORM_FEEDBACK_VARYING
,
4109 &linked_xfb
->Varyings
[i
], 0))
4114 /* Add transform feedback buffers. */
4115 for (unsigned i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
4116 if ((linked_xfb
->ActiveBuffers
>> i
) & 1) {
4117 linked_xfb
->Buffers
[i
].Binding
= i
;
4118 if (!link_util_add_program_resource(shProg
, resource_set
,
4119 GL_TRANSFORM_FEEDBACK_BUFFER
,
4120 &linked_xfb
->Buffers
[i
], 0))
4126 int top_level_array_base_offset
= -1;
4127 int top_level_array_size_in_bytes
= -1;
4128 int second_element_offset
= -1;
4129 int buffer_block_index
= -1;
4131 /* Add uniforms from uniform storage. */
4132 for (unsigned i
= 0; i
< shProg
->data
->NumUniformStorage
; i
++) {
4133 /* Do not add uniforms internally used by Mesa. */
4134 if (shProg
->data
->UniformStorage
[i
].hidden
)
4137 bool is_shader_storage
=
4138 shProg
->data
->UniformStorage
[i
].is_shader_storage
;
4139 GLenum type
= is_shader_storage
? GL_BUFFER_VARIABLE
: GL_UNIFORM
;
4140 if (!link_util_should_add_buffer_variable(shProg
,
4141 &shProg
->data
->UniformStorage
[i
],
4142 top_level_array_base_offset
,
4143 top_level_array_size_in_bytes
,
4144 second_element_offset
,
4145 buffer_block_index
))
4148 if (is_shader_storage
) {
4149 /* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources:
4151 * "For an active shader storage block member declared as an array
4152 * of an aggregate type, an entry will be generated only for the
4153 * first array element, regardless of its type. Such block members
4154 * are referred to as top-level arrays. If the block member is an
4155 * aggregate type, the enumeration rules are then applied
4158 * Below we update our tracking values used by
4159 * link_util_should_add_buffer_variable(). We only want to reset the
4160 * offsets once we have moved past the first element.
4162 if (shProg
->data
->UniformStorage
[i
].offset
>= second_element_offset
) {
4163 top_level_array_base_offset
=
4164 shProg
->data
->UniformStorage
[i
].offset
;
4166 top_level_array_size_in_bytes
=
4167 shProg
->data
->UniformStorage
[i
].top_level_array_size
*
4168 shProg
->data
->UniformStorage
[i
].top_level_array_stride
;
4170 /* Set or reset the second element offset. For non arrays this
4171 * will be set to -1.
4173 second_element_offset
= top_level_array_size_in_bytes
?
4174 top_level_array_base_offset
+
4175 shProg
->data
->UniformStorage
[i
].top_level_array_stride
: -1;
4178 buffer_block_index
= shProg
->data
->UniformStorage
[i
].block_index
;
4181 uint8_t stageref
= shProg
->data
->UniformStorage
[i
].active_shader_mask
;
4182 if (!link_util_add_program_resource(shProg
, resource_set
, type
,
4183 &shProg
->data
->UniformStorage
[i
], stageref
))
4187 /* Add program uniform blocks. */
4188 for (unsigned i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
4189 if (!link_util_add_program_resource(shProg
, resource_set
, GL_UNIFORM_BLOCK
,
4190 &shProg
->data
->UniformBlocks
[i
], 0))
4194 /* Add program shader storage blocks. */
4195 for (unsigned i
= 0; i
< shProg
->data
->NumShaderStorageBlocks
; i
++) {
4196 if (!link_util_add_program_resource(shProg
, resource_set
, GL_SHADER_STORAGE_BLOCK
,
4197 &shProg
->data
->ShaderStorageBlocks
[i
], 0))
4201 /* Add atomic counter buffers. */
4202 for (unsigned i
= 0; i
< shProg
->data
->NumAtomicBuffers
; i
++) {
4203 if (!link_util_add_program_resource(shProg
, resource_set
, GL_ATOMIC_COUNTER_BUFFER
,
4204 &shProg
->data
->AtomicBuffers
[i
], 0))
4208 for (unsigned i
= 0; i
< shProg
->data
->NumUniformStorage
; i
++) {
4210 if (!shProg
->data
->UniformStorage
[i
].hidden
)
4213 for (int j
= MESA_SHADER_VERTEX
; j
< MESA_SHADER_STAGES
; j
++) {
4214 if (!shProg
->data
->UniformStorage
[i
].opaque
[j
].active
||
4215 !shProg
->data
->UniformStorage
[i
].type
->is_subroutine())
4218 type
= _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage
)j
);
4219 /* add shader subroutines */
4220 if (!link_util_add_program_resource(shProg
, resource_set
,
4221 type
, &shProg
->data
->UniformStorage
[i
], 0))
4226 unsigned mask
= shProg
->data
->linked_stages
;
4228 const int i
= u_bit_scan(&mask
);
4229 struct gl_program
*p
= shProg
->_LinkedShaders
[i
]->Program
;
4231 GLuint type
= _mesa_shader_stage_to_subroutine((gl_shader_stage
)i
);
4232 for (unsigned j
= 0; j
< p
->sh
.NumSubroutineFunctions
; j
++) {
4233 if (!link_util_add_program_resource(shProg
, resource_set
,
4234 type
, &p
->sh
.SubroutineFunctions
[j
], 0))
4239 _mesa_set_destroy(resource_set
, NULL
);
4243 * This check is done to make sure we allow only constant expression
4244 * indexing and "constant-index-expression" (indexing with an expression
4245 * that includes loop induction variable).
4248 validate_sampler_array_indexing(struct gl_context
*ctx
,
4249 struct gl_shader_program
*prog
)
4251 dynamic_sampler_array_indexing_visitor v
;
4252 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4253 if (prog
->_LinkedShaders
[i
] == NULL
)
4256 bool no_dynamic_indexing
=
4257 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
;
4259 /* Search for array derefs in shader. */
4260 v
.run(prog
->_LinkedShaders
[i
]->ir
);
4261 if (v
.uses_dynamic_sampler_array_indexing()) {
4262 const char *msg
= "sampler arrays indexed with non-constant "
4263 "expressions is forbidden in GLSL %s %u";
4264 /* Backend has indicated that it has no dynamic indexing support. */
4265 if (no_dynamic_indexing
) {
4266 linker_error(prog
, msg
, prog
->IsES
? "ES" : "",
4267 prog
->data
->Version
);
4270 linker_warning(prog
, msg
, prog
->IsES
? "ES" : "",
4271 prog
->data
->Version
);
4279 link_assign_subroutine_types(struct gl_shader_program
*prog
)
4281 unsigned mask
= prog
->data
->linked_stages
;
4283 const int i
= u_bit_scan(&mask
);
4284 gl_program
*p
= prog
->_LinkedShaders
[i
]->Program
;
4286 p
->sh
.MaxSubroutineFunctionIndex
= 0;
4287 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
4288 ir_function
*fn
= node
->as_function();
4292 if (fn
->is_subroutine
)
4293 p
->sh
.NumSubroutineUniformTypes
++;
4295 if (!fn
->num_subroutine_types
)
4298 /* these should have been calculated earlier. */
4299 assert(fn
->subroutine_index
!= -1);
4300 if (p
->sh
.NumSubroutineFunctions
+ 1 > MAX_SUBROUTINES
) {
4301 linker_error(prog
, "Too many subroutine functions declared.\n");
4304 p
->sh
.SubroutineFunctions
= reralloc(p
, p
->sh
.SubroutineFunctions
,
4305 struct gl_subroutine_function
,
4306 p
->sh
.NumSubroutineFunctions
+ 1);
4307 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].name
= ralloc_strdup(p
, fn
->name
);
4308 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].num_compat_types
= fn
->num_subroutine_types
;
4309 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].types
=
4310 ralloc_array(p
, const struct glsl_type
*,
4311 fn
->num_subroutine_types
);
4313 /* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the
4316 * "Each subroutine with an index qualifier in the shader must be
4317 * given a unique index, otherwise a compile or link error will be
4320 for (unsigned j
= 0; j
< p
->sh
.NumSubroutineFunctions
; j
++) {
4321 if (p
->sh
.SubroutineFunctions
[j
].index
!= -1 &&
4322 p
->sh
.SubroutineFunctions
[j
].index
== fn
->subroutine_index
) {
4323 linker_error(prog
, "each subroutine index qualifier in the "
4324 "shader must be unique\n");
4328 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].index
=
4329 fn
->subroutine_index
;
4331 if (fn
->subroutine_index
> (int)p
->sh
.MaxSubroutineFunctionIndex
)
4332 p
->sh
.MaxSubroutineFunctionIndex
= fn
->subroutine_index
;
4334 for (int j
= 0; j
< fn
->num_subroutine_types
; j
++)
4335 p
->sh
.SubroutineFunctions
[p
->sh
.NumSubroutineFunctions
].types
[j
] = fn
->subroutine_types
[j
];
4336 p
->sh
.NumSubroutineFunctions
++;
4342 verify_subroutine_associated_funcs(struct gl_shader_program
*prog
)
4344 unsigned mask
= prog
->data
->linked_stages
;
4346 const int i
= u_bit_scan(&mask
);
4347 gl_program
*p
= prog
->_LinkedShaders
[i
]->Program
;
4348 glsl_symbol_table
*symbols
= prog
->_LinkedShaders
[i
]->symbols
;
4350 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
4352 * "A program will fail to compile or link if any shader
4353 * or stage contains two or more functions with the same
4354 * name if the name is associated with a subroutine type."
4356 for (unsigned j
= 0; j
< p
->sh
.NumSubroutineFunctions
; j
++) {
4357 unsigned definitions
= 0;
4358 char *name
= p
->sh
.SubroutineFunctions
[j
].name
;
4359 ir_function
*fn
= symbols
->get_function(name
);
4361 /* Calculate number of function definitions with the same name */
4362 foreach_in_list(ir_function_signature
, sig
, &fn
->signatures
) {
4363 if (sig
->is_defined
) {
4364 if (++definitions
> 1) {
4365 linker_error(prog
, "%s shader contains two or more function "
4366 "definitions with name `%s', which is "
4367 "associated with a subroutine type.\n",
4368 _mesa_shader_stage_to_string(i
),
4380 set_always_active_io(exec_list
*ir
, ir_variable_mode io_mode
)
4382 assert(io_mode
== ir_var_shader_in
|| io_mode
== ir_var_shader_out
);
4384 foreach_in_list(ir_instruction
, node
, ir
) {
4385 ir_variable
*const var
= node
->as_variable();
4387 if (var
== NULL
|| var
->data
.mode
!= io_mode
)
4390 /* Don't set always active on builtins that haven't been redeclared */
4391 if (var
->data
.how_declared
== ir_var_declared_implicitly
)
4394 var
->data
.always_active_io
= true;
4399 * When separate shader programs are enabled, only input/outputs between
4400 * the stages of a multi-stage separate program can be safely removed
4401 * from the shader interface. Other inputs/outputs must remain active.
4404 disable_varying_optimizations_for_sso(struct gl_shader_program
*prog
)
4406 unsigned first
, last
;
4407 assert(prog
->SeparateShader
);
4409 first
= MESA_SHADER_STAGES
;
4412 /* Determine first and last stage. Excluding the compute stage */
4413 for (unsigned i
= 0; i
< MESA_SHADER_COMPUTE
; i
++) {
4414 if (!prog
->_LinkedShaders
[i
])
4416 if (first
== MESA_SHADER_STAGES
)
4421 if (first
== MESA_SHADER_STAGES
)
4424 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4425 gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
4429 /* Prevent the removal of inputs to the first and outputs from the last
4430 * stage, unless they are the initial pipeline inputs or final pipeline
4431 * outputs, respectively.
4433 * The removal of IO between shaders in the same program is always
4436 if (stage
== first
&& stage
!= MESA_SHADER_VERTEX
)
4437 set_always_active_io(sh
->ir
, ir_var_shader_in
);
4438 if (stage
== last
&& stage
!= MESA_SHADER_FRAGMENT
)
4439 set_always_active_io(sh
->ir
, ir_var_shader_out
);
4444 link_and_validate_uniforms(struct gl_context
*ctx
,
4445 struct gl_shader_program
*prog
)
4447 assert(!ctx
->Const
.UseNIRGLSLLinker
);
4449 update_array_sizes(prog
);
4450 link_assign_uniform_locations(prog
, ctx
);
4452 if (prog
->data
->LinkStatus
== LINKING_FAILURE
)
4455 link_util_calculate_subroutine_compat(prog
);
4456 link_util_check_uniform_resources(ctx
, prog
);
4457 link_util_check_subroutine_resources(prog
);
4458 check_image_resources(ctx
, prog
);
4459 link_assign_atomic_counter_resources(ctx
, prog
);
4460 link_check_atomic_counter_resources(ctx
, prog
);
4464 link_varyings_and_uniforms(unsigned first
, unsigned last
,
4465 struct gl_context
*ctx
,
4466 struct gl_shader_program
*prog
, void *mem_ctx
)
4468 /* Mark all generic shader inputs and outputs as unpaired. */
4469 for (unsigned i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4470 if (prog
->_LinkedShaders
[i
] != NULL
) {
4471 link_invalidate_variable_locations(prog
->_LinkedShaders
[i
]->ir
);
4475 unsigned prev
= first
;
4476 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4477 if (prog
->_LinkedShaders
[i
] == NULL
)
4480 match_explicit_outputs_to_inputs(prog
->_LinkedShaders
[prev
],
4481 prog
->_LinkedShaders
[i
]);
4485 if (!assign_attribute_or_color_locations(mem_ctx
, prog
, &ctx
->Const
,
4486 MESA_SHADER_VERTEX
, true)) {
4490 if (!assign_attribute_or_color_locations(mem_ctx
, prog
, &ctx
->Const
,
4491 MESA_SHADER_FRAGMENT
, true)) {
4495 prog
->last_vert_prog
= NULL
;
4496 for (int i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
4497 if (prog
->_LinkedShaders
[i
] == NULL
)
4500 prog
->last_vert_prog
= prog
->_LinkedShaders
[i
]->Program
;
4504 if (!link_varyings(prog
, first
, last
, ctx
, mem_ctx
))
4507 if (!ctx
->Const
.UseNIRGLSLLinker
)
4508 link_and_validate_uniforms(ctx
, prog
);
4510 if (!prog
->data
->LinkStatus
)
4513 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4514 if (prog
->_LinkedShaders
[i
] == NULL
)
4517 const struct gl_shader_compiler_options
*options
=
4518 &ctx
->Const
.ShaderCompilerOptions
[i
];
4520 if (options
->LowerBufferInterfaceBlocks
)
4521 lower_ubo_reference(prog
->_LinkedShaders
[i
],
4522 options
->ClampBlockIndicesToArrayBounds
,
4523 ctx
->Const
.UseSTD430AsDefaultPacking
);
4525 if (i
== MESA_SHADER_COMPUTE
)
4526 lower_shared_reference(ctx
, prog
, prog
->_LinkedShaders
[i
]);
4528 lower_vector_derefs(prog
->_LinkedShaders
[i
]);
4529 do_vec_index_to_swizzle(prog
->_LinkedShaders
[i
]->ir
);
4536 linker_optimisation_loop(struct gl_context
*ctx
, exec_list
*ir
,
4539 if (ctx
->Const
.GLSLOptimizeConservatively
) {
4540 /* Run it just once. */
4541 do_common_optimization(ir
, true, false,
4542 &ctx
->Const
.ShaderCompilerOptions
[stage
],
4543 ctx
->Const
.NativeIntegers
);
4545 /* Repeat it until it stops making changes. */
4546 while (do_common_optimization(ir
, true, false,
4547 &ctx
->Const
.ShaderCompilerOptions
[stage
],
4548 ctx
->Const
.NativeIntegers
))
4554 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4556 prog
->data
->LinkStatus
= LINKING_SUCCESS
; /* All error paths will set this to false */
4557 prog
->data
->Validated
= false;
4559 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec says:
4561 * "Linking can fail for a variety of reasons as specified in the
4562 * OpenGL Shading Language Specification, as well as any of the
4563 * following reasons:
4565 * - No shader objects are attached to program."
4567 * The Compatibility Profile specification does not list the error. In
4568 * Compatibility Profile missing shader stages are replaced by
4569 * fixed-function. This applies to the case where all stages are
4572 if (prog
->NumShaders
== 0) {
4573 if (ctx
->API
!= API_OPENGL_COMPAT
)
4574 linker_error(prog
, "no shaders attached to the program\n");
4578 #ifdef ENABLE_SHADER_CACHE
4579 if (shader_cache_read_program_metadata(ctx
, prog
))
4583 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
4585 prog
->ARB_fragment_coord_conventions_enable
= false;
4587 /* Separate the shaders into groups based on their type.
4589 struct gl_shader
**shader_list
[MESA_SHADER_STAGES
];
4590 unsigned num_shaders
[MESA_SHADER_STAGES
];
4592 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4593 shader_list
[i
] = (struct gl_shader
**)
4594 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
4598 unsigned min_version
= UINT_MAX
;
4599 unsigned max_version
= 0;
4600 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
4601 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
4602 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
4604 if (!ctx
->Const
.AllowGLSLRelaxedES
&&
4605 prog
->Shaders
[i
]->IsES
!= prog
->Shaders
[0]->IsES
) {
4606 linker_error(prog
, "all shaders must use same shading "
4607 "language version\n");
4611 if (prog
->Shaders
[i
]->ARB_fragment_coord_conventions_enable
) {
4612 prog
->ARB_fragment_coord_conventions_enable
= true;
4615 gl_shader_stage shader_type
= prog
->Shaders
[i
]->Stage
;
4616 shader_list
[shader_type
][num_shaders
[shader_type
]] = prog
->Shaders
[i
];
4617 num_shaders
[shader_type
]++;
4620 /* In desktop GLSL, different shader versions may be linked together. In
4621 * GLSL ES, all shader versions must be the same.
4623 if (!ctx
->Const
.AllowGLSLRelaxedES
&& prog
->Shaders
[0]->IsES
&&
4624 min_version
!= max_version
) {
4625 linker_error(prog
, "all shaders must use same shading "
4626 "language version\n");
4630 prog
->data
->Version
= max_version
;
4631 prog
->IsES
= prog
->Shaders
[0]->IsES
;
4633 /* Some shaders have to be linked with some other shaders present.
4635 if (!prog
->SeparateShader
) {
4636 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0 &&
4637 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4638 linker_error(prog
, "Geometry shader must be linked with "
4642 if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0 &&
4643 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4644 linker_error(prog
, "Tessellation evaluation shader must be linked "
4645 "with vertex shader\n");
4648 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4649 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4650 linker_error(prog
, "Tessellation control shader must be linked with "
4655 /* Section 7.3 of the OpenGL ES 3.2 specification says:
4657 * "Linking can fail for [...] any of the following reasons:
4659 * * program contains an object to form a tessellation control
4660 * shader [...] and [...] the program is not separable and
4661 * contains no object to form a tessellation evaluation shader"
4663 * The OpenGL spec is contradictory. It allows linking without a tess
4664 * eval shader, but that can only be used with transform feedback and
4665 * rasterization disabled. However, transform feedback isn't allowed
4666 * with GL_PATCHES, so it can't be used.
4668 * More investigation showed that the idea of transform feedback after
4669 * a tess control shader was dropped, because some hw vendors couldn't
4670 * support tessellation without a tess eval shader, but the linker
4671 * section wasn't updated to reflect that.
4673 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
4676 * Do what's reasonable and always require a tess eval shader if a tess
4677 * control shader is present.
4679 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4680 num_shaders
[MESA_SHADER_TESS_EVAL
] == 0) {
4681 linker_error(prog
, "Tessellation control shader must be linked with "
4682 "tessellation evaluation shader\n");
4687 if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0 &&
4688 num_shaders
[MESA_SHADER_TESS_CTRL
] == 0) {
4689 linker_error(prog
, "GLSL ES requires non-separable programs "
4690 "containing a tessellation evaluation shader to also "
4691 "be linked with a tessellation control shader\n");
4697 /* Compute shaders have additional restrictions. */
4698 if (num_shaders
[MESA_SHADER_COMPUTE
] > 0 &&
4699 num_shaders
[MESA_SHADER_COMPUTE
] != prog
->NumShaders
) {
4700 linker_error(prog
, "Compute shaders may not be linked with any other "
4701 "type of shader\n");
4704 /* Link all shaders for a particular stage and validate the result.
4706 for (int stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4707 if (num_shaders
[stage
] > 0) {
4708 gl_linked_shader
*const sh
=
4709 link_intrastage_shaders(mem_ctx
, ctx
, prog
, shader_list
[stage
],
4710 num_shaders
[stage
], false);
4712 if (!prog
->data
->LinkStatus
) {
4714 _mesa_delete_linked_shader(ctx
, sh
);
4719 case MESA_SHADER_VERTEX
:
4720 validate_vertex_shader_executable(prog
, sh
, ctx
);
4722 case MESA_SHADER_TESS_CTRL
:
4723 /* nothing to be done */
4725 case MESA_SHADER_TESS_EVAL
:
4726 validate_tess_eval_shader_executable(prog
, sh
, ctx
);
4728 case MESA_SHADER_GEOMETRY
:
4729 validate_geometry_shader_executable(prog
, sh
, ctx
);
4731 case MESA_SHADER_FRAGMENT
:
4732 validate_fragment_shader_executable(prog
, sh
);
4735 if (!prog
->data
->LinkStatus
) {
4737 _mesa_delete_linked_shader(ctx
, sh
);
4741 prog
->_LinkedShaders
[stage
] = sh
;
4742 prog
->data
->linked_stages
|= 1 << stage
;
4746 /* Here begins the inter-stage linking phase. Some initial validation is
4747 * performed, then locations are assigned for uniforms, attributes, and
4750 cross_validate_uniforms(ctx
, prog
);
4751 if (!prog
->data
->LinkStatus
)
4754 unsigned first
, last
, prev
;
4756 first
= MESA_SHADER_STAGES
;
4759 /* Determine first and last stage. */
4760 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4761 if (!prog
->_LinkedShaders
[i
])
4763 if (first
== MESA_SHADER_STAGES
)
4768 check_explicit_uniform_locations(ctx
, prog
);
4769 link_assign_subroutine_types(prog
);
4770 verify_subroutine_associated_funcs(prog
);
4772 if (!prog
->data
->LinkStatus
)
4775 resize_tes_inputs(ctx
, prog
);
4777 /* Validate the inputs of each stage with the output of the preceding
4781 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4782 if (prog
->_LinkedShaders
[i
] == NULL
)
4785 validate_interstage_inout_blocks(prog
, prog
->_LinkedShaders
[prev
],
4786 prog
->_LinkedShaders
[i
]);
4787 if (!prog
->data
->LinkStatus
)
4790 cross_validate_outputs_to_inputs(ctx
, prog
,
4791 prog
->_LinkedShaders
[prev
],
4792 prog
->_LinkedShaders
[i
]);
4793 if (!prog
->data
->LinkStatus
)
4799 /* The cross validation of outputs/inputs above validates interstage
4800 * explicit locations. We need to do this also for the inputs in the first
4801 * stage and outputs of the last stage included in the program, since there
4802 * is no cross validation for these.
4804 validate_first_and_last_interface_explicit_locations(ctx
, prog
,
4805 (gl_shader_stage
) first
,
4806 (gl_shader_stage
) last
);
4808 /* Cross-validate uniform blocks between shader stages */
4809 validate_interstage_uniform_blocks(prog
, prog
->_LinkedShaders
);
4810 if (!prog
->data
->LinkStatus
)
4813 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4814 if (prog
->_LinkedShaders
[i
] != NULL
)
4815 lower_named_interface_blocks(mem_ctx
, prog
->_LinkedShaders
[i
]);
4818 if (prog
->IsES
&& prog
->data
->Version
== 100)
4819 if (!validate_invariant_builtins(prog
,
4820 prog
->_LinkedShaders
[MESA_SHADER_VERTEX
],
4821 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]))
4824 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
4825 * it before optimization because we want most of the checks to get
4826 * dropped thanks to constant propagation.
4828 * This rule also applies to GLSL ES 3.00.
4830 if (max_version
>= (prog
->IsES
? 300 : 130)) {
4831 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4833 lower_discard_flow(sh
->ir
);
4837 if (prog
->SeparateShader
)
4838 disable_varying_optimizations_for_sso(prog
);
4841 if (!interstage_cross_validate_uniform_blocks(prog
, false))
4845 if (!interstage_cross_validate_uniform_blocks(prog
, true))
4848 /* Do common optimization before assigning storage for attributes,
4849 * uniforms, and varyings. Later optimization could possibly make
4850 * some of that unused.
4852 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4853 if (prog
->_LinkedShaders
[i
] == NULL
)
4856 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
4857 if (!prog
->data
->LinkStatus
)
4860 if (ctx
->Const
.ShaderCompilerOptions
[i
].LowerCombinedClipCullDistance
) {
4861 lower_clip_cull_distance(prog
, prog
->_LinkedShaders
[i
]);
4864 if (ctx
->Const
.LowerTessLevel
) {
4865 lower_tess_level(prog
->_LinkedShaders
[i
]);
4868 /* Section 13.46 (Vertex Attribute Aliasing) of the OpenGL ES 3.2
4869 * specification says:
4871 * "In general, the behavior of GLSL ES should not depend on compiler
4872 * optimizations which might be implementation-dependent. Name matching
4873 * rules in most languages, including C++ from which GLSL ES is derived,
4874 * are based on declarations rather than use.
4876 * RESOLUTION: The existence of aliasing is determined by declarations
4877 * present after preprocessing."
4879 * Because of this rule, we do a 'dry-run' of attribute assignment for
4880 * vertex shader inputs here.
4882 if (prog
->IsES
&& i
== MESA_SHADER_VERTEX
) {
4883 if (!assign_attribute_or_color_locations(mem_ctx
, prog
, &ctx
->Const
,
4884 MESA_SHADER_VERTEX
, false)) {
4889 /* Call opts before lowering const arrays to uniforms so we can const
4890 * propagate any elements accessed directly.
4892 linker_optimisation_loop(ctx
, prog
->_LinkedShaders
[i
]->ir
, i
);
4894 /* Call opts after lowering const arrays to copy propagate things. */
4895 if (ctx
->Const
.GLSLLowerConstArrays
&&
4896 lower_const_arrays_to_uniforms(prog
->_LinkedShaders
[i
]->ir
, i
,
4897 ctx
->Const
.Program
[i
].MaxUniformComponents
))
4898 linker_optimisation_loop(ctx
, prog
->_LinkedShaders
[i
]->ir
, i
);
4902 /* Validation for special cases where we allow sampler array indexing
4903 * with loop induction variable. This check emits a warning or error
4904 * depending if backend can handle dynamic indexing.
4906 if ((!prog
->IsES
&& prog
->data
->Version
< 130) ||
4907 (prog
->IsES
&& prog
->data
->Version
< 300)) {
4908 if (!validate_sampler_array_indexing(ctx
, prog
))
4912 /* Check and validate stream emissions in geometry shaders */
4913 validate_geometry_shader_emissions(ctx
, prog
);
4915 store_fragdepth_layout(prog
);
4917 if(!link_varyings_and_uniforms(first
, last
, ctx
, prog
, mem_ctx
))
4920 /* Linking varyings can cause some extra, useless swizzles to be generated
4921 * due to packing and unpacking.
4923 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4924 if (prog
->_LinkedShaders
[i
] == NULL
)
4927 optimize_swizzles(prog
->_LinkedShaders
[i
]->ir
);
4930 /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both
4931 * be present in a linked program. GL_ARB_ES2_compatibility doesn't say
4932 * anything about shader linking when one of the shaders (vertex or
4933 * fragment shader) is absent. So, the extension shouldn't change the
4934 * behavior specified in GLSL specification.
4936 * From OpenGL ES 3.1 specification (7.3 Program Objects):
4937 * "Linking can fail for a variety of reasons as specified in the
4938 * OpenGL ES Shading Language Specification, as well as any of the
4939 * following reasons:
4943 * * program contains objects to form either a vertex shader or
4944 * fragment shader, and program is not separable, and does not
4945 * contain objects to form both a vertex shader and fragment
4948 * However, the only scenario in 3.1+ where we don't require them both is
4949 * when we have a compute shader. For example:
4951 * - No shaders is a link error.
4952 * - Geom or Tess without a Vertex shader is a link error which means we
4953 * always require a Vertex shader and hence a Fragment shader.
4954 * - Finally a Compute shader linked with any other stage is a link error.
4956 if (!prog
->SeparateShader
&& ctx
->API
== API_OPENGLES2
&&
4957 num_shaders
[MESA_SHADER_COMPUTE
] == 0) {
4958 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
4959 linker_error(prog
, "program lacks a vertex shader\n");
4960 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
4961 linker_error(prog
, "program lacks a fragment shader\n");
4966 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4967 free(shader_list
[i
]);
4968 if (prog
->_LinkedShaders
[i
] == NULL
)
4971 /* Do a final validation step to make sure that the IR wasn't
4972 * invalidated by any modifications performed after intrastage linking.
4974 validate_ir_tree(prog
->_LinkedShaders
[i
]->ir
);
4976 /* Retain any live IR, but trash the rest. */
4977 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
4979 /* The symbol table in the linked shaders may contain references to
4980 * variables that were removed (e.g., unused uniforms). Since it may
4981 * contain junk, there is no possible valid use. Delete it and set the
4984 delete prog
->_LinkedShaders
[i
]->symbols
;
4985 prog
->_LinkedShaders
[i
]->symbols
= NULL
;
4988 ralloc_free(mem_ctx
);