2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * GLSL linker implementation
28 * Given a set of shaders that are to be linked to generate a final program,
29 * there are three distinct stages.
31 * In the first stage shaders are partitioned into groups based on the shader
32 * type. All shaders of a particular type (e.g., vertex shaders) are linked
35 * - Undefined references in each shader are resolve to definitions in
37 * - Types and qualifiers of uniforms, outputs, and global variables defined
38 * in multiple shaders with the same name are verified to be the same.
39 * - Initializers for uniforms and global variables defined
40 * in multiple shaders with the same name are verified to be the same.
42 * The result, in the terminology of the GLSL spec, is a set of shader
43 * executables for each processing unit.
45 * After the first stage is complete, a series of semantic checks are performed
46 * on each of the shader executables.
48 * - Each shader executable must define a \c main function.
49 * - Each vertex shader executable must write to \c gl_Position.
50 * - Each fragment shader executable must write to either \c gl_FragData or
53 * In the final stage individual shader executables are linked to create a
54 * complete exectuable.
56 * - Types of uniforms defined in multiple shader stages with the same name
57 * are verified to be the same.
58 * - Initializers for uniforms defined in multiple shader stages with the
59 * same name are verified to be the same.
60 * - Types and qualifiers of outputs defined in one stage are verified to
61 * be the same as the types and qualifiers of inputs defined with the same
62 * name in a later stage.
64 * \author Ian Romanick <ian.d.romanick@intel.com>
68 #include "util/strndup.h"
69 #include "main/core.h"
70 #include "glsl_symbol_table.h"
71 #include "glsl_parser_extras.h"
74 #include "program/prog_instruction.h"
76 #include "util/string_to_uint_map.h"
78 #include "link_varyings.h"
79 #include "ir_optimization.h"
80 #include "ir_rvalue_visitor.h"
81 #include "ir_uniform.h"
83 #include "main/shaderobj.h"
84 #include "main/enums.h"
90 * Visitor that determines whether or not a variable is ever written.
92 class find_assignment_visitor
: public ir_hierarchical_visitor
{
94 find_assignment_visitor(const char *name
)
95 : name(name
), found(false)
100 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
)
102 ir_variable
*const var
= ir
->lhs
->variable_referenced();
104 if (strcmp(name
, var
->name
) == 0) {
109 return visit_continue_with_parent
;
112 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
114 foreach_two_lists(formal_node
, &ir
->callee
->parameters
,
115 actual_node
, &ir
->actual_parameters
) {
116 ir_rvalue
*param_rval
= (ir_rvalue
*) actual_node
;
117 ir_variable
*sig_param
= (ir_variable
*) formal_node
;
119 if (sig_param
->data
.mode
== ir_var_function_out
||
120 sig_param
->data
.mode
== ir_var_function_inout
) {
121 ir_variable
*var
= param_rval
->variable_referenced();
122 if (var
&& strcmp(name
, var
->name
) == 0) {
129 if (ir
->return_deref
!= NULL
) {
130 ir_variable
*const var
= ir
->return_deref
->variable_referenced();
132 if (strcmp(name
, var
->name
) == 0) {
138 return visit_continue_with_parent
;
141 bool variable_found()
147 const char *name
; /**< Find writes to a variable with this name. */
148 bool found
; /**< Was a write to the variable found? */
153 * Visitor that determines whether or not a variable is ever read.
155 class find_deref_visitor
: public ir_hierarchical_visitor
{
157 find_deref_visitor(const char *name
)
158 : name(name
), found(false)
163 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
165 if (strcmp(this->name
, ir
->var
->name
) == 0) {
170 return visit_continue
;
173 bool variable_found() const
179 const char *name
; /**< Find writes to a variable with this name. */
180 bool found
; /**< Was a write to the variable found? */
185 * A visitor helper that provides methods for updating the types of
186 * ir_dereferences. Classes that update variable types (say, updating
187 * array sizes) will want to use this so that dereference types stay in sync.
189 class deref_type_updater
: public ir_hierarchical_visitor
{
191 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
193 ir
->type
= ir
->var
->type
;
194 return visit_continue
;
197 virtual ir_visitor_status
visit_leave(ir_dereference_array
*ir
)
199 const glsl_type
*const vt
= ir
->array
->type
;
201 ir
->type
= vt
->fields
.array
;
202 return visit_continue
;
205 virtual ir_visitor_status
visit_leave(ir_dereference_record
*ir
)
207 for (unsigned i
= 0; i
< ir
->record
->type
->length
; i
++) {
208 const struct glsl_struct_field
*field
=
209 &ir
->record
->type
->fields
.structure
[i
];
210 if (strcmp(field
->name
, ir
->field
) == 0) {
211 ir
->type
= field
->type
;
215 return visit_continue
;
220 class array_resize_visitor
: public deref_type_updater
{
222 unsigned num_vertices
;
223 gl_shader_program
*prog
;
224 gl_shader_stage stage
;
226 array_resize_visitor(unsigned num_vertices
,
227 gl_shader_program
*prog
,
228 gl_shader_stage stage
)
230 this->num_vertices
= num_vertices
;
235 virtual ~array_resize_visitor()
240 virtual ir_visitor_status
visit(ir_variable
*var
)
242 if (!var
->type
->is_array() || var
->data
.mode
!= ir_var_shader_in
||
244 return visit_continue
;
246 unsigned size
= var
->type
->length
;
248 if (stage
== MESA_SHADER_GEOMETRY
) {
249 /* Generate a link error if the shader has declared this array with
252 if (!var
->data
.implicit_sized_array
&&
253 size
&& size
!= this->num_vertices
) {
254 linker_error(this->prog
, "size of array %s declared as %u, "
255 "but number of input vertices is %u\n",
256 var
->name
, size
, this->num_vertices
);
257 return visit_continue
;
260 /* Generate a link error if the shader attempts to access an input
261 * array using an index too large for its actual size assigned at
264 if (var
->data
.max_array_access
>= (int)this->num_vertices
) {
265 linker_error(this->prog
, "%s shader accesses element %i of "
266 "%s, but only %i input vertices\n",
267 _mesa_shader_stage_to_string(this->stage
),
268 var
->data
.max_array_access
, var
->name
, this->num_vertices
);
269 return visit_continue
;
273 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
275 var
->data
.max_array_access
= this->num_vertices
- 1;
277 return visit_continue
;
282 * Visitor that determines the highest stream id to which a (geometry) shader
283 * emits vertices. It also checks whether End{Stream}Primitive is ever called.
285 class find_emit_vertex_visitor
: public ir_hierarchical_visitor
{
287 find_emit_vertex_visitor(int max_allowed
)
288 : max_stream_allowed(max_allowed
),
289 invalid_stream_id(0),
290 invalid_stream_id_from_emit_vertex(false),
291 end_primitive_found(false),
292 uses_non_zero_stream(false)
297 virtual ir_visitor_status
visit_leave(ir_emit_vertex
*ir
)
299 int stream_id
= ir
->stream_id();
302 invalid_stream_id
= stream_id
;
303 invalid_stream_id_from_emit_vertex
= true;
307 if (stream_id
> max_stream_allowed
) {
308 invalid_stream_id
= stream_id
;
309 invalid_stream_id_from_emit_vertex
= true;
314 uses_non_zero_stream
= true;
316 return visit_continue
;
319 virtual ir_visitor_status
visit_leave(ir_end_primitive
*ir
)
321 end_primitive_found
= true;
323 int stream_id
= ir
->stream_id();
326 invalid_stream_id
= stream_id
;
327 invalid_stream_id_from_emit_vertex
= false;
331 if (stream_id
> max_stream_allowed
) {
332 invalid_stream_id
= stream_id
;
333 invalid_stream_id_from_emit_vertex
= false;
338 uses_non_zero_stream
= true;
340 return visit_continue
;
345 return invalid_stream_id
!= 0;
348 const char *error_func()
350 return invalid_stream_id_from_emit_vertex
?
351 "EmitStreamVertex" : "EndStreamPrimitive";
356 return invalid_stream_id
;
361 return uses_non_zero_stream
;
364 bool uses_end_primitive()
366 return end_primitive_found
;
370 int max_stream_allowed
;
371 int invalid_stream_id
;
372 bool invalid_stream_id_from_emit_vertex
;
373 bool end_primitive_found
;
374 bool uses_non_zero_stream
;
377 /* Class that finds array derefs and check if indexes are dynamic. */
378 class dynamic_sampler_array_indexing_visitor
: public ir_hierarchical_visitor
381 dynamic_sampler_array_indexing_visitor() :
382 dynamic_sampler_array_indexing(false)
386 ir_visitor_status
visit_enter(ir_dereference_array
*ir
)
388 if (!ir
->variable_referenced())
389 return visit_continue
;
391 if (!ir
->variable_referenced()->type
->contains_sampler())
392 return visit_continue
;
394 if (!ir
->array_index
->constant_expression_value()) {
395 dynamic_sampler_array_indexing
= true;
398 return visit_continue
;
401 bool uses_dynamic_sampler_array_indexing()
403 return dynamic_sampler_array_indexing
;
407 bool dynamic_sampler_array_indexing
;
410 } /* anonymous namespace */
413 linker_error(gl_shader_program
*prog
, const char *fmt
, ...)
417 ralloc_strcat(&prog
->InfoLog
, "error: ");
419 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
422 prog
->LinkStatus
= false;
427 linker_warning(gl_shader_program
*prog
, const char *fmt
, ...)
431 ralloc_strcat(&prog
->InfoLog
, "warning: ");
433 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, ap
);
440 * Given a string identifying a program resource, break it into a base name
441 * and an optional array index in square brackets.
443 * If an array index is present, \c out_base_name_end is set to point to the
444 * "[" that precedes the array index, and the array index itself is returned
447 * If no array index is present (or if the array index is negative or
448 * mal-formed), \c out_base_name_end, is set to point to the null terminator
449 * at the end of the input string, and -1 is returned.
451 * Only the final array index is parsed; if the string contains other array
452 * indices (or structure field accesses), they are left in the base name.
454 * No attempt is made to check that the base name is properly formed;
455 * typically the caller will look up the base name in a hash table, so
456 * ill-formed base names simply turn into hash table lookup failures.
459 parse_program_resource_name(const GLchar
*name
,
460 const GLchar
**out_base_name_end
)
462 /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
464 * "When an integer array element or block instance number is part of
465 * the name string, it will be specified in decimal form without a "+"
466 * or "-" sign or any extra leading zeroes. Additionally, the name
467 * string will not include white space anywhere in the string."
470 const size_t len
= strlen(name
);
471 *out_base_name_end
= name
+ len
;
473 if (len
== 0 || name
[len
-1] != ']')
476 /* Walk backwards over the string looking for a non-digit character. This
477 * had better be the opening bracket for an array index.
479 * Initially, i specifies the location of the ']'. Since the string may
480 * contain only the ']' charcater, walk backwards very carefully.
483 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
486 if ((i
== 0) || name
[i
-1] != '[')
489 long array_index
= strtol(&name
[i
], NULL
, 10);
493 /* Check for leading zero */
494 if (name
[i
] == '0' && name
[i
+1] != ']')
497 *out_base_name_end
= name
+ (i
- 1);
503 link_invalidate_variable_locations(exec_list
*ir
)
505 foreach_in_list(ir_instruction
, node
, ir
) {
506 ir_variable
*const var
= node
->as_variable();
511 /* Only assign locations for variables that lack an explicit location.
512 * Explicit locations are set for all built-in variables, generic vertex
513 * shader inputs (via layout(location=...)), and generic fragment shader
514 * outputs (also via layout(location=...)).
516 if (!var
->data
.explicit_location
) {
517 var
->data
.location
= -1;
518 var
->data
.location_frac
= 0;
521 /* ir_variable::is_unmatched_generic_inout is used by the linker while
522 * connecting outputs from one stage to inputs of the next stage.
524 if (var
->data
.explicit_location
&&
525 var
->data
.location
< VARYING_SLOT_VAR0
) {
526 var
->data
.is_unmatched_generic_inout
= 0;
528 var
->data
.is_unmatched_generic_inout
= 1;
535 * Set clip_distance_array_size based and cull_distance_array_size on the given
538 * Also check for errors based on incorrect usage of gl_ClipVertex and
539 * gl_ClipDistance and gl_CullDistance.
540 * Additionally test whether the arrays gl_ClipDistance and gl_CullDistance
541 * exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances.
543 * Return false if an error was reported.
546 analyze_clip_cull_usage(struct gl_shader_program
*prog
,
547 struct gl_linked_shader
*shader
,
548 struct gl_context
*ctx
,
549 GLuint
*clip_distance_array_size
,
550 GLuint
*cull_distance_array_size
)
552 *clip_distance_array_size
= 0;
553 *cull_distance_array_size
= 0;
555 if (prog
->Version
>= (prog
->IsES
? 300 : 130)) {
556 /* From section 7.1 (Vertex Shader Special Variables) of the
559 * "It is an error for a shader to statically write both
560 * gl_ClipVertex and gl_ClipDistance."
562 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
563 * gl_ClipVertex nor gl_ClipDistance. However with
564 * GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0.
566 find_assignment_visitor
clip_distance("gl_ClipDistance");
567 find_assignment_visitor
cull_distance("gl_CullDistance");
569 clip_distance
.run(shader
->ir
);
570 cull_distance
.run(shader
->ir
);
572 /* From the ARB_cull_distance spec:
574 * It is a compile-time or link-time error for the set of shaders forming
575 * a program to statically read or write both gl_ClipVertex and either
576 * gl_ClipDistance or gl_CullDistance.
578 * This does not apply to GLSL ES shaders, since GLSL ES doesn't define
582 find_assignment_visitor
clip_vertex("gl_ClipVertex");
584 clip_vertex
.run(shader
->ir
);
586 if (clip_vertex
.variable_found() && clip_distance
.variable_found()) {
587 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
588 "and `gl_ClipDistance'\n",
589 _mesa_shader_stage_to_string(shader
->Stage
));
592 if (clip_vertex
.variable_found() && cull_distance
.variable_found()) {
593 linker_error(prog
, "%s shader writes to both `gl_ClipVertex' "
594 "and `gl_CullDistance'\n",
595 _mesa_shader_stage_to_string(shader
->Stage
));
600 if (clip_distance
.variable_found()) {
601 ir_variable
*clip_distance_var
=
602 shader
->symbols
->get_variable("gl_ClipDistance");
603 assert(clip_distance_var
);
604 *clip_distance_array_size
= clip_distance_var
->type
->length
;
606 if (cull_distance
.variable_found()) {
607 ir_variable
*cull_distance_var
=
608 shader
->symbols
->get_variable("gl_CullDistance");
609 assert(cull_distance_var
);
610 *cull_distance_array_size
= cull_distance_var
->type
->length
;
612 /* From the ARB_cull_distance spec:
614 * It is a compile-time or link-time error for the set of shaders forming
615 * a program to have the sum of the sizes of the gl_ClipDistance and
616 * gl_CullDistance arrays to be larger than
617 * gl_MaxCombinedClipAndCullDistances.
619 if ((*clip_distance_array_size
+ *cull_distance_array_size
) >
620 ctx
->Const
.MaxClipPlanes
) {
621 linker_error(prog
, "%s shader: the combined size of "
622 "'gl_ClipDistance' and 'gl_CullDistance' size cannot "
624 "gl_MaxCombinedClipAndCullDistances (%u)",
625 _mesa_shader_stage_to_string(shader
->Stage
),
626 ctx
->Const
.MaxClipPlanes
);
633 * Verify that a vertex shader executable meets all semantic requirements.
635 * Also sets prog->Vert.ClipDistanceArraySize and
636 * prog->Vert.CullDistanceArraySize as a side effect.
638 * \param shader Vertex shader executable to be verified
641 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
642 struct gl_linked_shader
*shader
,
643 struct gl_context
*ctx
)
648 /* From the GLSL 1.10 spec, page 48:
650 * "The variable gl_Position is available only in the vertex
651 * language and is intended for writing the homogeneous vertex
652 * position. All executions of a well-formed vertex shader
653 * executable must write a value into this variable. [...] The
654 * variable gl_Position is available only in the vertex
655 * language and is intended for writing the homogeneous vertex
656 * position. All executions of a well-formed vertex shader
657 * executable must write a value into this variable."
659 * while in GLSL 1.40 this text is changed to:
661 * "The variable gl_Position is available only in the vertex
662 * language and is intended for writing the homogeneous vertex
663 * position. It can be written at any time during shader
664 * execution. It may also be read back by a vertex shader
665 * after being written. This value will be used by primitive
666 * assembly, clipping, culling, and other fixed functionality
667 * operations, if present, that operate on primitives after
668 * vertex processing has occurred. Its value is undefined if
669 * the vertex shader executable does not write gl_Position."
671 * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
672 * gl_Position is not an error.
674 if (prog
->Version
< (prog
->IsES
? 300 : 140)) {
675 find_assignment_visitor
find("gl_Position");
676 find
.run(shader
->ir
);
677 if (!find
.variable_found()) {
680 "vertex shader does not write to `gl_Position'. "
681 "Its value is undefined. \n");
684 "vertex shader does not write to `gl_Position'. \n");
690 analyze_clip_cull_usage(prog
, shader
, ctx
,
691 &prog
->Vert
.ClipDistanceArraySize
,
692 &prog
->Vert
.CullDistanceArraySize
);
696 validate_tess_eval_shader_executable(struct gl_shader_program
*prog
,
697 struct gl_linked_shader
*shader
,
698 struct gl_context
*ctx
)
703 analyze_clip_cull_usage(prog
, shader
, ctx
,
704 &prog
->TessEval
.ClipDistanceArraySize
,
705 &prog
->TessEval
.CullDistanceArraySize
);
710 * Verify that a fragment shader executable meets all semantic requirements
712 * \param shader Fragment shader executable to be verified
715 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
716 struct gl_linked_shader
*shader
)
721 find_assignment_visitor
frag_color("gl_FragColor");
722 find_assignment_visitor
frag_data("gl_FragData");
724 frag_color
.run(shader
->ir
);
725 frag_data
.run(shader
->ir
);
727 if (frag_color
.variable_found() && frag_data
.variable_found()) {
728 linker_error(prog
, "fragment shader writes to both "
729 "`gl_FragColor' and `gl_FragData'\n");
734 * Verify that a geometry shader executable meets all semantic requirements
736 * Also sets prog->Geom.VerticesIn, and prog->Geom.ClipDistanceArraySize and
737 * prog->Geom.CullDistanceArraySize as a side effect.
739 * \param shader Geometry shader executable to be verified
742 validate_geometry_shader_executable(struct gl_shader_program
*prog
,
743 struct gl_linked_shader
*shader
,
744 struct gl_context
*ctx
)
749 unsigned num_vertices
= vertices_per_prim(shader
->info
.Geom
.InputType
);
750 prog
->Geom
.VerticesIn
= num_vertices
;
752 analyze_clip_cull_usage(prog
, shader
, ctx
,
753 &prog
->Geom
.ClipDistanceArraySize
,
754 &prog
->Geom
.CullDistanceArraySize
);
758 * Check if geometry shaders emit to non-zero streams and do corresponding
762 validate_geometry_shader_emissions(struct gl_context
*ctx
,
763 struct gl_shader_program
*prog
)
765 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
768 find_emit_vertex_visitor
emit_vertex(ctx
->Const
.MaxVertexStreams
- 1);
769 emit_vertex
.run(sh
->ir
);
770 if (emit_vertex
.error()) {
771 linker_error(prog
, "Invalid call %s(%d). Accepted values for the "
772 "stream parameter are in the range [0, %d].\n",
773 emit_vertex
.error_func(),
774 emit_vertex
.error_stream(),
775 ctx
->Const
.MaxVertexStreams
- 1);
777 prog
->Geom
.UsesStreams
= emit_vertex
.uses_streams();
778 prog
->Geom
.UsesEndPrimitive
= emit_vertex
.uses_end_primitive();
780 /* From the ARB_gpu_shader5 spec:
782 * "Multiple vertex streams are supported only if the output primitive
783 * type is declared to be "points". A program will fail to link if it
784 * contains a geometry shader calling EmitStreamVertex() or
785 * EndStreamPrimitive() if its output primitive type is not "points".
787 * However, in the same spec:
789 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
790 * with <stream> set to zero."
794 * "The function EndPrimitive() is equivalent to calling
795 * EndStreamPrimitive() with <stream> set to zero."
797 * Since we can call EmitVertex() and EndPrimitive() when we output
798 * primitives other than points, calling EmitStreamVertex(0) or
799 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
800 * does. Currently we only set prog->Geom.UsesStreams to TRUE when
801 * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
804 if (prog
->Geom
.UsesStreams
&& sh
->info
.Geom
.OutputType
!= GL_POINTS
) {
805 linker_error(prog
, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
806 "with n>0 requires point output\n");
812 validate_intrastage_arrays(struct gl_shader_program
*prog
,
813 ir_variable
*const var
,
814 ir_variable
*const existing
)
816 /* Consider the types to be "the same" if both types are arrays
817 * of the same type and one of the arrays is implicitly sized.
818 * In addition, set the type of the linked variable to the
819 * explicitly sized array.
821 if (var
->type
->is_array() && existing
->type
->is_array()) {
822 if ((var
->type
->fields
.array
== existing
->type
->fields
.array
) &&
823 ((var
->type
->length
== 0)|| (existing
->type
->length
== 0))) {
824 if (var
->type
->length
!= 0) {
825 if ((int)var
->type
->length
<= existing
->data
.max_array_access
) {
826 linker_error(prog
, "%s `%s' declared as type "
827 "`%s' but outermost dimension has an index"
830 var
->name
, var
->type
->name
,
831 existing
->data
.max_array_access
);
833 existing
->type
= var
->type
;
835 } else if (existing
->type
->length
!= 0) {
836 if((int)existing
->type
->length
<= var
->data
.max_array_access
&&
837 !existing
->data
.from_ssbo_unsized_array
) {
838 linker_error(prog
, "%s `%s' declared as type "
839 "`%s' but outermost dimension has an index"
842 var
->name
, existing
->type
->name
,
843 var
->data
.max_array_access
);
848 /* The arrays of structs could have different glsl_type pointers but
849 * they are actually the same type. Use record_compare() to check that.
851 if (existing
->type
->fields
.array
->is_record() &&
852 var
->type
->fields
.array
->is_record() &&
853 existing
->type
->fields
.array
->record_compare(var
->type
->fields
.array
))
862 * Perform validation of global variables used across multiple shaders
865 cross_validate_globals(struct gl_shader_program
*prog
,
866 struct exec_list
*ir
, glsl_symbol_table
*variables
,
869 foreach_in_list(ir_instruction
, node
, ir
) {
870 ir_variable
*const var
= node
->as_variable();
875 if (uniforms_only
&& (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
))
878 /* don't cross validate subroutine uniforms */
879 if (var
->type
->contains_subroutine())
882 /* Don't cross validate temporaries that are at global scope. These
883 * will eventually get pulled into the shaders 'main'.
885 if (var
->data
.mode
== ir_var_temporary
)
888 /* If a global with this name has already been seen, verify that the
889 * new instance has the same type. In addition, if the globals have
890 * initializers, the values of the initializers must be the same.
892 ir_variable
*const existing
= variables
->get_variable(var
->name
);
893 if (existing
!= NULL
) {
894 /* Check if types match. Interface blocks have some special
895 * rules so we handle those elsewhere.
897 if (var
->type
!= existing
->type
&&
898 !var
->is_interface_instance()) {
899 if (!validate_intrastage_arrays(prog
, var
, existing
)) {
900 if (var
->type
->is_record() && existing
->type
->is_record()
901 && existing
->type
->record_compare(var
->type
)) {
902 existing
->type
= var
->type
;
904 /* If it is an unsized array in a Shader Storage Block,
905 * two different shaders can access to different elements.
906 * Because of that, they might be converted to different
907 * sized arrays, then check that they are compatible but
908 * ignore the array size.
910 if (!(var
->data
.mode
== ir_var_shader_storage
&&
911 var
->data
.from_ssbo_unsized_array
&&
912 existing
->data
.mode
== ir_var_shader_storage
&&
913 existing
->data
.from_ssbo_unsized_array
&&
914 var
->type
->gl_type
== existing
->type
->gl_type
)) {
915 linker_error(prog
, "%s `%s' declared as type "
916 "`%s' and type `%s'\n",
918 var
->name
, var
->type
->name
,
919 existing
->type
->name
);
926 if (var
->data
.explicit_location
) {
927 if (existing
->data
.explicit_location
928 && (var
->data
.location
!= existing
->data
.location
)) {
929 linker_error(prog
, "explicit locations for %s "
930 "`%s' have differing values\n",
931 mode_string(var
), var
->name
);
935 if (var
->data
.location_frac
!= existing
->data
.location_frac
) {
936 linker_error(prog
, "explicit components for %s `%s' have "
937 "differing values\n", mode_string(var
), var
->name
);
941 existing
->data
.location
= var
->data
.location
;
942 existing
->data
.explicit_location
= true;
944 /* Check if uniform with implicit location was marked explicit
945 * by earlier shader stage. If so, mark it explicit in this stage
946 * too to make sure later processing does not treat it as
949 if (existing
->data
.explicit_location
) {
950 var
->data
.location
= existing
->data
.location
;
951 var
->data
.explicit_location
= true;
955 /* From the GLSL 4.20 specification:
956 * "A link error will result if two compilation units in a program
957 * specify different integer-constant bindings for the same
958 * opaque-uniform name. However, it is not an error to specify a
959 * binding on some but not all declarations for the same name"
961 if (var
->data
.explicit_binding
) {
962 if (existing
->data
.explicit_binding
&&
963 var
->data
.binding
!= existing
->data
.binding
) {
964 linker_error(prog
, "explicit bindings for %s "
965 "`%s' have differing values\n",
966 mode_string(var
), var
->name
);
970 existing
->data
.binding
= var
->data
.binding
;
971 existing
->data
.explicit_binding
= true;
974 if (var
->type
->contains_atomic() &&
975 var
->data
.offset
!= existing
->data
.offset
) {
976 linker_error(prog
, "offset specifications for %s "
977 "`%s' have differing values\n",
978 mode_string(var
), var
->name
);
982 /* Validate layout qualifiers for gl_FragDepth.
984 * From the AMD/ARB_conservative_depth specs:
986 * "If gl_FragDepth is redeclared in any fragment shader in a
987 * program, it must be redeclared in all fragment shaders in
988 * that program that have static assignments to
989 * gl_FragDepth. All redeclarations of gl_FragDepth in all
990 * fragment shaders in a single program must have the same set
993 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
994 bool layout_declared
= var
->data
.depth_layout
!= ir_depth_layout_none
;
995 bool layout_differs
=
996 var
->data
.depth_layout
!= existing
->data
.depth_layout
;
998 if (layout_declared
&& layout_differs
) {
1000 "All redeclarations of gl_FragDepth in all "
1001 "fragment shaders in a single program must have "
1002 "the same set of qualifiers.\n");
1005 if (var
->data
.used
&& layout_differs
) {
1007 "If gl_FragDepth is redeclared with a layout "
1008 "qualifier in any fragment shader, it must be "
1009 "redeclared with the same layout qualifier in "
1010 "all fragment shaders that have assignments to "
1015 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1017 * "If a shared global has multiple initializers, the
1018 * initializers must all be constant expressions, and they
1019 * must all have the same value. Otherwise, a link error will
1020 * result. (A shared global having only one initializer does
1021 * not require that initializer to be a constant expression.)"
1023 * Previous to 4.20 the GLSL spec simply said that initializers
1024 * must have the same value. In this case of non-constant
1025 * initializers, this was impossible to determine. As a result,
1026 * no vendor actually implemented that behavior. The 4.20
1027 * behavior matches the implemented behavior of at least one other
1028 * vendor, so we'll implement that for all GLSL versions.
1030 if (var
->constant_initializer
!= NULL
) {
1031 if (existing
->constant_initializer
!= NULL
) {
1032 if (!var
->constant_initializer
->has_value(existing
->constant_initializer
)) {
1033 linker_error(prog
, "initializers for %s "
1034 "`%s' have differing values\n",
1035 mode_string(var
), var
->name
);
1039 /* If the first-seen instance of a particular uniform did
1040 * not have an initializer but a later instance does,
1041 * replace the former with the later.
1043 variables
->replace_variable(existing
->name
, var
);
1047 if (var
->data
.has_initializer
) {
1048 if (existing
->data
.has_initializer
1049 && (var
->constant_initializer
== NULL
1050 || existing
->constant_initializer
== NULL
)) {
1052 "shared global variable `%s' has multiple "
1053 "non-constant initializers.\n",
1059 if (existing
->data
.invariant
!= var
->data
.invariant
) {
1060 linker_error(prog
, "declarations for %s `%s' have "
1061 "mismatching invariant qualifiers\n",
1062 mode_string(var
), var
->name
);
1065 if (existing
->data
.centroid
!= var
->data
.centroid
) {
1066 linker_error(prog
, "declarations for %s `%s' have "
1067 "mismatching centroid qualifiers\n",
1068 mode_string(var
), var
->name
);
1071 if (existing
->data
.sample
!= var
->data
.sample
) {
1072 linker_error(prog
, "declarations for %s `%s` have "
1073 "mismatching sample qualifiers\n",
1074 mode_string(var
), var
->name
);
1077 if (existing
->data
.image_format
!= var
->data
.image_format
) {
1078 linker_error(prog
, "declarations for %s `%s` have "
1079 "mismatching image format qualifiers\n",
1080 mode_string(var
), var
->name
);
1084 /* Only in GLSL ES 3.10, the precision qualifier should not match
1085 * between block members defined in matched block names within a
1088 * In GLSL ES 3.00 and ES 3.20, precision qualifier for each block
1089 * member should match.
1091 if (prog
->IsES
&& (prog
->Version
!= 310 || !var
->get_interface_type()) &&
1092 existing
->data
.precision
!= var
->data
.precision
) {
1093 linker_error(prog
, "declarations for %s `%s` have "
1094 "mismatching precision qualifiers\n",
1095 mode_string(var
), var
->name
);
1099 variables
->add_variable(var
);
1105 * Perform validation of uniforms used across multiple shader stages
1108 cross_validate_uniforms(struct gl_shader_program
*prog
)
1110 glsl_symbol_table variables
;
1111 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1112 if (prog
->_LinkedShaders
[i
] == NULL
)
1115 cross_validate_globals(prog
, prog
->_LinkedShaders
[i
]->ir
, &variables
,
1121 * Accumulates the array of buffer blocks and checks that all definitions of
1122 * blocks agree on their contents.
1125 interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
,
1128 int *InterfaceBlockStageIndex
[MESA_SHADER_STAGES
];
1129 struct gl_uniform_block
*blks
= NULL
;
1130 unsigned *num_blks
= validate_ssbo
? &prog
->NumShaderStorageBlocks
:
1131 &prog
->NumUniformBlocks
;
1133 unsigned max_num_buffer_blocks
= 0;
1134 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1135 if (prog
->_LinkedShaders
[i
]) {
1136 if (validate_ssbo
) {
1137 max_num_buffer_blocks
+=
1138 prog
->_LinkedShaders
[i
]->NumShaderStorageBlocks
;
1140 max_num_buffer_blocks
+=
1141 prog
->_LinkedShaders
[i
]->NumUniformBlocks
;
1146 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1147 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1149 InterfaceBlockStageIndex
[i
] = new int[max_num_buffer_blocks
];
1150 for (unsigned int j
= 0; j
< max_num_buffer_blocks
; j
++)
1151 InterfaceBlockStageIndex
[i
][j
] = -1;
1156 unsigned sh_num_blocks
;
1157 struct gl_uniform_block
**sh_blks
;
1158 if (validate_ssbo
) {
1159 sh_num_blocks
= prog
->_LinkedShaders
[i
]->NumShaderStorageBlocks
;
1160 sh_blks
= sh
->ShaderStorageBlocks
;
1162 sh_num_blocks
= prog
->_LinkedShaders
[i
]->NumUniformBlocks
;
1163 sh_blks
= sh
->UniformBlocks
;
1166 for (unsigned int j
= 0; j
< sh_num_blocks
; j
++) {
1167 int index
= link_cross_validate_uniform_block(prog
, &blks
, num_blks
,
1171 linker_error(prog
, "buffer block `%s' has mismatching "
1172 "definitions\n", sh_blks
[j
]->Name
);
1174 for (unsigned k
= 0; k
<= i
; k
++) {
1175 delete[] InterfaceBlockStageIndex
[k
];
1180 InterfaceBlockStageIndex
[i
][index
] = j
;
1184 /* Update per stage block pointers to point to the program list.
1185 * FIXME: We should be able to free the per stage blocks here.
1187 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1188 for (unsigned j
= 0; j
< *num_blks
; j
++) {
1189 int stage_index
= InterfaceBlockStageIndex
[i
][j
];
1191 if (stage_index
!= -1) {
1192 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1194 struct gl_uniform_block
**sh_blks
= validate_ssbo
?
1195 sh
->ShaderStorageBlocks
: sh
->UniformBlocks
;
1197 blks
[j
].stageref
|= sh_blks
[stage_index
]->stageref
;
1198 sh_blks
[stage_index
] = &blks
[j
];
1203 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1204 delete[] InterfaceBlockStageIndex
[i
];
1208 prog
->ShaderStorageBlocks
= blks
;
1210 prog
->UniformBlocks
= blks
;
1217 * Populates a shaders symbol table with all global declarations
1220 populate_symbol_table(gl_linked_shader
*sh
)
1222 sh
->symbols
= new(sh
) glsl_symbol_table
;
1224 foreach_in_list(ir_instruction
, inst
, sh
->ir
) {
1228 if ((func
= inst
->as_function()) != NULL
) {
1229 sh
->symbols
->add_function(func
);
1230 } else if ((var
= inst
->as_variable()) != NULL
) {
1231 if (var
->data
.mode
!= ir_var_temporary
)
1232 sh
->symbols
->add_variable(var
);
1239 * Remap variables referenced in an instruction tree
1241 * This is used when instruction trees are cloned from one shader and placed in
1242 * another. These trees will contain references to \c ir_variable nodes that
1243 * do not exist in the target shader. This function finds these \c ir_variable
1244 * references and replaces the references with matching variables in the target
1247 * If there is no matching variable in the target shader, a clone of the
1248 * \c ir_variable is made and added to the target shader. The new variable is
1249 * added to \b both the instruction stream and the symbol table.
1251 * \param inst IR tree that is to be processed.
1252 * \param symbols Symbol table containing global scope symbols in the
1254 * \param instructions Instruction stream where new variable declarations
1258 remap_variables(ir_instruction
*inst
, struct gl_linked_shader
*target
,
1261 class remap_visitor
: public ir_hierarchical_visitor
{
1263 remap_visitor(struct gl_linked_shader
*target
, hash_table
*temps
)
1265 this->target
= target
;
1266 this->symbols
= target
->symbols
;
1267 this->instructions
= target
->ir
;
1268 this->temps
= temps
;
1271 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
1273 if (ir
->var
->data
.mode
== ir_var_temporary
) {
1274 hash_entry
*entry
= _mesa_hash_table_search(temps
, ir
->var
);
1275 ir_variable
*var
= entry
? (ir_variable
*) entry
->data
: NULL
;
1277 assert(var
!= NULL
);
1279 return visit_continue
;
1282 ir_variable
*const existing
=
1283 this->symbols
->get_variable(ir
->var
->name
);
1284 if (existing
!= NULL
)
1287 ir_variable
*copy
= ir
->var
->clone(this->target
, NULL
);
1289 this->symbols
->add_variable(copy
);
1290 this->instructions
->push_head(copy
);
1294 return visit_continue
;
1298 struct gl_linked_shader
*target
;
1299 glsl_symbol_table
*symbols
;
1300 exec_list
*instructions
;
1304 remap_visitor
v(target
, temps
);
1311 * Move non-declarations from one instruction stream to another
1313 * The intended usage pattern of this function is to pass the pointer to the
1314 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
1315 * pointer) for \c last and \c false for \c make_copies on the first
1316 * call. Successive calls pass the return value of the previous call for
1317 * \c last and \c true for \c make_copies.
1319 * \param instructions Source instruction stream
1320 * \param last Instruction after which new instructions should be
1321 * inserted in the target instruction stream
1322 * \param make_copies Flag selecting whether instructions in \c instructions
1323 * should be copied (via \c ir_instruction::clone) into the
1324 * target list or moved.
1327 * The new "last" instruction in the target instruction stream. This pointer
1328 * is suitable for use as the \c last parameter of a later call to this
1332 move_non_declarations(exec_list
*instructions
, exec_node
*last
,
1333 bool make_copies
, gl_linked_shader
*target
)
1335 hash_table
*temps
= NULL
;
1338 temps
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1339 _mesa_key_pointer_equal
);
1341 foreach_in_list_safe(ir_instruction
, inst
, instructions
) {
1342 if (inst
->as_function())
1345 ir_variable
*var
= inst
->as_variable();
1346 if ((var
!= NULL
) && (var
->data
.mode
!= ir_var_temporary
))
1349 assert(inst
->as_assignment()
1351 || inst
->as_if() /* for initializers with the ?: operator */
1352 || ((var
!= NULL
) && (var
->data
.mode
== ir_var_temporary
)));
1355 inst
= inst
->clone(target
, NULL
);
1358 _mesa_hash_table_insert(temps
, var
, inst
);
1360 remap_variables(inst
, target
, temps
);
1365 last
->insert_after(inst
);
1370 _mesa_hash_table_destroy(temps
, NULL
);
1377 * This class is only used in link_intrastage_shaders() below but declaring
1378 * it inside that function leads to compiler warnings with some versions of
1381 class array_sizing_visitor
: public deref_type_updater
{
1383 array_sizing_visitor()
1384 : mem_ctx(ralloc_context(NULL
)),
1385 unnamed_interfaces(_mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1386 _mesa_key_pointer_equal
))
1390 ~array_sizing_visitor()
1392 _mesa_hash_table_destroy(this->unnamed_interfaces
, NULL
);
1393 ralloc_free(this->mem_ctx
);
1396 virtual ir_visitor_status
visit(ir_variable
*var
)
1398 const glsl_type
*type_without_array
;
1399 bool implicit_sized_array
= var
->data
.implicit_sized_array
;
1400 fixup_type(&var
->type
, var
->data
.max_array_access
,
1401 var
->data
.from_ssbo_unsized_array
,
1402 &implicit_sized_array
);
1403 var
->data
.implicit_sized_array
= implicit_sized_array
;
1404 type_without_array
= var
->type
->without_array();
1405 if (var
->type
->is_interface()) {
1406 if (interface_contains_unsized_arrays(var
->type
)) {
1407 const glsl_type
*new_type
=
1408 resize_interface_members(var
->type
,
1409 var
->get_max_ifc_array_access(),
1410 var
->is_in_shader_storage_block());
1411 var
->type
= new_type
;
1412 var
->change_interface_type(new_type
);
1414 } else if (type_without_array
->is_interface()) {
1415 if (interface_contains_unsized_arrays(type_without_array
)) {
1416 const glsl_type
*new_type
=
1417 resize_interface_members(type_without_array
,
1418 var
->get_max_ifc_array_access(),
1419 var
->is_in_shader_storage_block());
1420 var
->change_interface_type(new_type
);
1421 var
->type
= update_interface_members_array(var
->type
, new_type
);
1423 } else if (const glsl_type
*ifc_type
= var
->get_interface_type()) {
1424 /* Store a pointer to the variable in the unnamed_interfaces
1428 _mesa_hash_table_search(this->unnamed_interfaces
,
1431 ir_variable
**interface_vars
= entry
? (ir_variable
**) entry
->data
: NULL
;
1433 if (interface_vars
== NULL
) {
1434 interface_vars
= rzalloc_array(mem_ctx
, ir_variable
*,
1436 _mesa_hash_table_insert(this->unnamed_interfaces
, ifc_type
,
1439 unsigned index
= ifc_type
->field_index(var
->name
);
1440 assert(index
< ifc_type
->length
);
1441 assert(interface_vars
[index
] == NULL
);
1442 interface_vars
[index
] = var
;
1444 return visit_continue
;
1448 * For each unnamed interface block that was discovered while running the
1449 * visitor, adjust the interface type to reflect the newly assigned array
1450 * sizes, and fix up the ir_variable nodes to point to the new interface
1453 void fixup_unnamed_interface_types()
1455 hash_table_call_foreach(this->unnamed_interfaces
,
1456 fixup_unnamed_interface_type
, NULL
);
1461 * If the type pointed to by \c type represents an unsized array, replace
1462 * it with a sized array whose size is determined by max_array_access.
1464 static void fixup_type(const glsl_type
**type
, unsigned max_array_access
,
1465 bool from_ssbo_unsized_array
, bool *implicit_sized
)
1467 if (!from_ssbo_unsized_array
&& (*type
)->is_unsized_array()) {
1468 *type
= glsl_type::get_array_instance((*type
)->fields
.array
,
1469 max_array_access
+ 1);
1470 *implicit_sized
= true;
1471 assert(*type
!= NULL
);
1475 static const glsl_type
*
1476 update_interface_members_array(const glsl_type
*type
,
1477 const glsl_type
*new_interface_type
)
1479 const glsl_type
*element_type
= type
->fields
.array
;
1480 if (element_type
->is_array()) {
1481 const glsl_type
*new_array_type
=
1482 update_interface_members_array(element_type
, new_interface_type
);
1483 return glsl_type::get_array_instance(new_array_type
, type
->length
);
1485 return glsl_type::get_array_instance(new_interface_type
,
1491 * Determine whether the given interface type contains unsized arrays (if
1492 * it doesn't, array_sizing_visitor doesn't need to process it).
1494 static bool interface_contains_unsized_arrays(const glsl_type
*type
)
1496 for (unsigned i
= 0; i
< type
->length
; i
++) {
1497 const glsl_type
*elem_type
= type
->fields
.structure
[i
].type
;
1498 if (elem_type
->is_unsized_array())
1505 * Create a new interface type based on the given type, with unsized arrays
1506 * replaced by sized arrays whose size is determined by
1507 * max_ifc_array_access.
1509 static const glsl_type
*
1510 resize_interface_members(const glsl_type
*type
,
1511 const int *max_ifc_array_access
,
1514 unsigned num_fields
= type
->length
;
1515 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1516 memcpy(fields
, type
->fields
.structure
,
1517 num_fields
* sizeof(*fields
));
1518 for (unsigned i
= 0; i
< num_fields
; i
++) {
1519 bool implicit_sized_array
= fields
[i
].implicit_sized_array
;
1520 /* If SSBO last member is unsized array, we don't replace it by a sized
1523 if (is_ssbo
&& i
== (num_fields
- 1))
1524 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1525 true, &implicit_sized_array
);
1527 fixup_type(&fields
[i
].type
, max_ifc_array_access
[i
],
1528 false, &implicit_sized_array
);
1529 fields
[i
].implicit_sized_array
= implicit_sized_array
;
1531 glsl_interface_packing packing
=
1532 (glsl_interface_packing
) type
->interface_packing
;
1533 bool row_major
= (bool) type
->interface_row_major
;
1534 const glsl_type
*new_ifc_type
=
1535 glsl_type::get_interface_instance(fields
, num_fields
,
1536 packing
, row_major
, type
->name
);
1538 return new_ifc_type
;
1541 static void fixup_unnamed_interface_type(const void *key
, void *data
,
1544 const glsl_type
*ifc_type
= (const glsl_type
*) key
;
1545 ir_variable
**interface_vars
= (ir_variable
**) data
;
1546 unsigned num_fields
= ifc_type
->length
;
1547 glsl_struct_field
*fields
= new glsl_struct_field
[num_fields
];
1548 memcpy(fields
, ifc_type
->fields
.structure
,
1549 num_fields
* sizeof(*fields
));
1550 bool interface_type_changed
= false;
1551 for (unsigned i
= 0; i
< num_fields
; i
++) {
1552 if (interface_vars
[i
] != NULL
&&
1553 fields
[i
].type
!= interface_vars
[i
]->type
) {
1554 fields
[i
].type
= interface_vars
[i
]->type
;
1555 interface_type_changed
= true;
1558 if (!interface_type_changed
) {
1562 glsl_interface_packing packing
=
1563 (glsl_interface_packing
) ifc_type
->interface_packing
;
1564 bool row_major
= (bool) ifc_type
->interface_row_major
;
1565 const glsl_type
*new_ifc_type
=
1566 glsl_type::get_interface_instance(fields
, num_fields
, packing
,
1567 row_major
, ifc_type
->name
);
1569 for (unsigned i
= 0; i
< num_fields
; i
++) {
1570 if (interface_vars
[i
] != NULL
)
1571 interface_vars
[i
]->change_interface_type(new_ifc_type
);
1576 * Memory context used to allocate the data in \c unnamed_interfaces.
1581 * Hash table from const glsl_type * to an array of ir_variable *'s
1582 * pointing to the ir_variables constituting each unnamed interface block.
1584 hash_table
*unnamed_interfaces
;
1588 * Check for conflicting xfb_stride default qualifiers and store buffer stride
1592 link_xfb_stride_layout_qualifiers(struct gl_context
*ctx
,
1593 struct gl_shader_program
*prog
,
1594 struct gl_linked_shader
*linked_shader
,
1595 struct gl_shader
**shader_list
,
1596 unsigned num_shaders
)
1598 for (unsigned i
= 0; i
< MAX_FEEDBACK_BUFFERS
; i
++) {
1599 linked_shader
->info
.TransformFeedback
.BufferStride
[i
] = 0;
1602 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1603 struct gl_shader
*shader
= shader_list
[i
];
1605 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1606 if (shader
->info
.TransformFeedback
.BufferStride
[j
]) {
1607 if (linked_shader
->info
.TransformFeedback
.BufferStride
[j
] != 0 &&
1608 shader
->info
.TransformFeedback
.BufferStride
[j
] != 0 &&
1609 linked_shader
->info
.TransformFeedback
.BufferStride
[j
] !=
1610 shader
->info
.TransformFeedback
.BufferStride
[j
]) {
1612 "intrastage shaders defined with conflicting "
1613 "xfb_stride for buffer %d (%d and %d)\n", j
,
1615 info
.TransformFeedback
.BufferStride
[j
],
1616 shader
->info
.TransformFeedback
.BufferStride
[j
]);
1620 if (shader
->info
.TransformFeedback
.BufferStride
[j
])
1621 linked_shader
->info
.TransformFeedback
.BufferStride
[j
] =
1622 shader
->info
.TransformFeedback
.BufferStride
[j
];
1627 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1628 if (linked_shader
->info
.TransformFeedback
.BufferStride
[j
]) {
1629 prog
->TransformFeedback
.BufferStride
[j
] =
1630 linked_shader
->info
.TransformFeedback
.BufferStride
[j
];
1632 /* We will validate doubles at a later stage */
1633 if (prog
->TransformFeedback
.BufferStride
[j
] % 4) {
1634 linker_error(prog
, "invalid qualifier xfb_stride=%d must be a "
1635 "multiple of 4 or if its applied to a type that is "
1636 "or contains a double a multiple of 8.",
1637 prog
->TransformFeedback
.BufferStride
[j
]);
1641 if (prog
->TransformFeedback
.BufferStride
[j
] / 4 >
1642 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
1644 "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1645 "limit has been exceeded.");
1653 * Performs the cross-validation of tessellation control shader vertices and
1654 * layout qualifiers for the attached tessellation control shaders,
1655 * and propagates them to the linked TCS and linked shader program.
1658 link_tcs_out_layout_qualifiers(struct gl_shader_program
*prog
,
1659 struct gl_linked_shader
*linked_shader
,
1660 struct gl_shader
**shader_list
,
1661 unsigned num_shaders
)
1663 linked_shader
->info
.TessCtrl
.VerticesOut
= 0;
1665 if (linked_shader
->Stage
!= MESA_SHADER_TESS_CTRL
)
1668 /* From the GLSL 4.0 spec (chapter 4.3.8.2):
1670 * "All tessellation control shader layout declarations in a program
1671 * must specify the same output patch vertex count. There must be at
1672 * least one layout qualifier specifying an output patch vertex count
1673 * in any program containing tessellation control shaders; however,
1674 * such a declaration is not required in all tessellation control
1678 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1679 struct gl_shader
*shader
= shader_list
[i
];
1681 if (shader
->info
.TessCtrl
.VerticesOut
!= 0) {
1682 if (linked_shader
->info
.TessCtrl
.VerticesOut
!= 0 &&
1683 linked_shader
->info
.TessCtrl
.VerticesOut
!=
1684 shader
->info
.TessCtrl
.VerticesOut
) {
1685 linker_error(prog
, "tessellation control shader defined with "
1686 "conflicting output vertex count (%d and %d)\n",
1687 linked_shader
->info
.TessCtrl
.VerticesOut
,
1688 shader
->info
.TessCtrl
.VerticesOut
);
1691 linked_shader
->info
.TessCtrl
.VerticesOut
=
1692 shader
->info
.TessCtrl
.VerticesOut
;
1696 /* Just do the intrastage -> interstage propagation right now,
1697 * since we already know we're in the right type of shader program
1700 if (linked_shader
->info
.TessCtrl
.VerticesOut
== 0) {
1701 linker_error(prog
, "tessellation control shader didn't declare "
1702 "vertices out layout qualifier\n");
1709 * Performs the cross-validation of tessellation evaluation shader
1710 * primitive type, vertex spacing, ordering and point_mode layout qualifiers
1711 * for the attached tessellation evaluation shaders, and propagates them
1712 * to the linked TES and linked shader program.
1715 link_tes_in_layout_qualifiers(struct gl_shader_program
*prog
,
1716 struct gl_linked_shader
*linked_shader
,
1717 struct gl_shader
**shader_list
,
1718 unsigned num_shaders
)
1720 linked_shader
->info
.TessEval
.PrimitiveMode
= PRIM_UNKNOWN
;
1721 linked_shader
->info
.TessEval
.Spacing
= 0;
1722 linked_shader
->info
.TessEval
.VertexOrder
= 0;
1723 linked_shader
->info
.TessEval
.PointMode
= -1;
1725 if (linked_shader
->Stage
!= MESA_SHADER_TESS_EVAL
)
1728 /* From the GLSL 4.0 spec (chapter 4.3.8.1):
1730 * "At least one tessellation evaluation shader (compilation unit) in
1731 * a program must declare a primitive mode in its input layout.
1732 * Declaration vertex spacing, ordering, and point mode identifiers is
1733 * optional. It is not required that all tessellation evaluation
1734 * shaders in a program declare a primitive mode. If spacing or
1735 * vertex ordering declarations are omitted, the tessellation
1736 * primitive generator will use equal spacing or counter-clockwise
1737 * vertex ordering, respectively. If a point mode declaration is
1738 * omitted, the tessellation primitive generator will produce lines or
1739 * triangles according to the primitive mode."
1742 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1743 struct gl_shader
*shader
= shader_list
[i
];
1745 if (shader
->info
.TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
) {
1746 if (linked_shader
->info
.TessEval
.PrimitiveMode
!= PRIM_UNKNOWN
&&
1747 linked_shader
->info
.TessEval
.PrimitiveMode
!=
1748 shader
->info
.TessEval
.PrimitiveMode
) {
1749 linker_error(prog
, "tessellation evaluation shader defined with "
1750 "conflicting input primitive modes.\n");
1753 linked_shader
->info
.TessEval
.PrimitiveMode
= shader
->info
.TessEval
.PrimitiveMode
;
1756 if (shader
->info
.TessEval
.Spacing
!= 0) {
1757 if (linked_shader
->info
.TessEval
.Spacing
!= 0 &&
1758 linked_shader
->info
.TessEval
.Spacing
!=
1759 shader
->info
.TessEval
.Spacing
) {
1760 linker_error(prog
, "tessellation evaluation shader defined with "
1761 "conflicting vertex spacing.\n");
1764 linked_shader
->info
.TessEval
.Spacing
= shader
->info
.TessEval
.Spacing
;
1767 if (shader
->info
.TessEval
.VertexOrder
!= 0) {
1768 if (linked_shader
->info
.TessEval
.VertexOrder
!= 0 &&
1769 linked_shader
->info
.TessEval
.VertexOrder
!=
1770 shader
->info
.TessEval
.VertexOrder
) {
1771 linker_error(prog
, "tessellation evaluation shader defined with "
1772 "conflicting ordering.\n");
1775 linked_shader
->info
.TessEval
.VertexOrder
=
1776 shader
->info
.TessEval
.VertexOrder
;
1779 if (shader
->info
.TessEval
.PointMode
!= -1) {
1780 if (linked_shader
->info
.TessEval
.PointMode
!= -1 &&
1781 linked_shader
->info
.TessEval
.PointMode
!=
1782 shader
->info
.TessEval
.PointMode
) {
1783 linker_error(prog
, "tessellation evaluation shader defined with "
1784 "conflicting point modes.\n");
1787 linked_shader
->info
.TessEval
.PointMode
=
1788 shader
->info
.TessEval
.PointMode
;
1793 /* Just do the intrastage -> interstage propagation right now,
1794 * since we already know we're in the right type of shader program
1797 if (linked_shader
->info
.TessEval
.PrimitiveMode
== PRIM_UNKNOWN
) {
1799 "tessellation evaluation shader didn't declare input "
1800 "primitive modes.\n");
1804 if (linked_shader
->info
.TessEval
.Spacing
== 0)
1805 linked_shader
->info
.TessEval
.Spacing
= GL_EQUAL
;
1807 if (linked_shader
->info
.TessEval
.VertexOrder
== 0)
1808 linked_shader
->info
.TessEval
.VertexOrder
= GL_CCW
;
1810 if (linked_shader
->info
.TessEval
.PointMode
== -1)
1811 linked_shader
->info
.TessEval
.PointMode
= GL_FALSE
;
1816 * Performs the cross-validation of layout qualifiers specified in
1817 * redeclaration of gl_FragCoord for the attached fragment shaders,
1818 * and propagates them to the linked FS and linked shader program.
1821 link_fs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
1822 struct gl_linked_shader
*linked_shader
,
1823 struct gl_shader
**shader_list
,
1824 unsigned num_shaders
)
1826 linked_shader
->info
.redeclares_gl_fragcoord
= false;
1827 linked_shader
->info
.uses_gl_fragcoord
= false;
1828 linked_shader
->info
.origin_upper_left
= false;
1829 linked_shader
->info
.pixel_center_integer
= false;
1830 linked_shader
->info
.BlendSupport
= 0;
1832 if (linked_shader
->Stage
!= MESA_SHADER_FRAGMENT
||
1833 (prog
->Version
< 150 && !prog
->ARB_fragment_coord_conventions_enable
))
1836 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1837 struct gl_shader
*shader
= shader_list
[i
];
1838 /* From the GLSL 1.50 spec, page 39:
1840 * "If gl_FragCoord is redeclared in any fragment shader in a program,
1841 * it must be redeclared in all the fragment shaders in that program
1842 * that have a static use gl_FragCoord."
1844 if ((linked_shader
->info
.redeclares_gl_fragcoord
1845 && !shader
->info
.redeclares_gl_fragcoord
1846 && shader
->info
.uses_gl_fragcoord
)
1847 || (shader
->info
.redeclares_gl_fragcoord
1848 && !linked_shader
->info
.redeclares_gl_fragcoord
1849 && linked_shader
->info
.uses_gl_fragcoord
)) {
1850 linker_error(prog
, "fragment shader defined with conflicting "
1851 "layout qualifiers for gl_FragCoord\n");
1854 /* From the GLSL 1.50 spec, page 39:
1856 * "All redeclarations of gl_FragCoord in all fragment shaders in a
1857 * single program must have the same set of qualifiers."
1859 if (linked_shader
->info
.redeclares_gl_fragcoord
&&
1860 shader
->info
.redeclares_gl_fragcoord
&&
1861 (shader
->info
.origin_upper_left
!=
1862 linked_shader
->info
.origin_upper_left
||
1863 shader
->info
.pixel_center_integer
!=
1864 linked_shader
->info
.pixel_center_integer
)) {
1865 linker_error(prog
, "fragment shader defined with conflicting "
1866 "layout qualifiers for gl_FragCoord\n");
1869 /* Update the linked shader state. Note that uses_gl_fragcoord should
1870 * accumulate the results. The other values should replace. If there
1871 * are multiple redeclarations, all the fields except uses_gl_fragcoord
1872 * are already known to be the same.
1874 if (shader
->info
.redeclares_gl_fragcoord
||
1875 shader
->info
.uses_gl_fragcoord
) {
1876 linked_shader
->info
.redeclares_gl_fragcoord
=
1877 shader
->info
.redeclares_gl_fragcoord
;
1878 linked_shader
->info
.uses_gl_fragcoord
=
1879 linked_shader
->info
.uses_gl_fragcoord
||
1880 shader
->info
.uses_gl_fragcoord
;
1881 linked_shader
->info
.origin_upper_left
=
1882 shader
->info
.origin_upper_left
;
1883 linked_shader
->info
.pixel_center_integer
=
1884 shader
->info
.pixel_center_integer
;
1887 linked_shader
->info
.EarlyFragmentTests
|=
1888 shader
->info
.EarlyFragmentTests
;
1889 linked_shader
->info
.BlendSupport
|= shader
->info
.BlendSupport
;
1894 * Performs the cross-validation of geometry shader max_vertices and
1895 * primitive type layout qualifiers for the attached geometry shaders,
1896 * and propagates them to the linked GS and linked shader program.
1899 link_gs_inout_layout_qualifiers(struct gl_shader_program
*prog
,
1900 struct gl_linked_shader
*linked_shader
,
1901 struct gl_shader
**shader_list
,
1902 unsigned num_shaders
)
1904 linked_shader
->info
.Geom
.VerticesOut
= -1;
1905 linked_shader
->info
.Geom
.Invocations
= 0;
1906 linked_shader
->info
.Geom
.InputType
= PRIM_UNKNOWN
;
1907 linked_shader
->info
.Geom
.OutputType
= PRIM_UNKNOWN
;
1909 /* No in/out qualifiers defined for anything but GLSL 1.50+
1910 * geometry shaders so far.
1912 if (linked_shader
->Stage
!= MESA_SHADER_GEOMETRY
|| prog
->Version
< 150)
1915 /* From the GLSL 1.50 spec, page 46:
1917 * "All geometry shader output layout declarations in a program
1918 * must declare the same layout and same value for
1919 * max_vertices. There must be at least one geometry output
1920 * layout declaration somewhere in a program, but not all
1921 * geometry shaders (compilation units) are required to
1925 for (unsigned i
= 0; i
< num_shaders
; i
++) {
1926 struct gl_shader
*shader
= shader_list
[i
];
1928 if (shader
->info
.Geom
.InputType
!= PRIM_UNKNOWN
) {
1929 if (linked_shader
->info
.Geom
.InputType
!= PRIM_UNKNOWN
&&
1930 linked_shader
->info
.Geom
.InputType
!=
1931 shader
->info
.Geom
.InputType
) {
1932 linker_error(prog
, "geometry shader defined with conflicting "
1936 linked_shader
->info
.Geom
.InputType
= shader
->info
.Geom
.InputType
;
1939 if (shader
->info
.Geom
.OutputType
!= PRIM_UNKNOWN
) {
1940 if (linked_shader
->info
.Geom
.OutputType
!= PRIM_UNKNOWN
&&
1941 linked_shader
->info
.Geom
.OutputType
!=
1942 shader
->info
.Geom
.OutputType
) {
1943 linker_error(prog
, "geometry shader defined with conflicting "
1947 linked_shader
->info
.Geom
.OutputType
= shader
->info
.Geom
.OutputType
;
1950 if (shader
->info
.Geom
.VerticesOut
!= -1) {
1951 if (linked_shader
->info
.Geom
.VerticesOut
!= -1 &&
1952 linked_shader
->info
.Geom
.VerticesOut
!=
1953 shader
->info
.Geom
.VerticesOut
) {
1954 linker_error(prog
, "geometry shader defined with conflicting "
1955 "output vertex count (%d and %d)\n",
1956 linked_shader
->info
.Geom
.VerticesOut
,
1957 shader
->info
.Geom
.VerticesOut
);
1960 linked_shader
->info
.Geom
.VerticesOut
= shader
->info
.Geom
.VerticesOut
;
1963 if (shader
->info
.Geom
.Invocations
!= 0) {
1964 if (linked_shader
->info
.Geom
.Invocations
!= 0 &&
1965 linked_shader
->info
.Geom
.Invocations
!=
1966 shader
->info
.Geom
.Invocations
) {
1967 linker_error(prog
, "geometry shader defined with conflicting "
1968 "invocation count (%d and %d)\n",
1969 linked_shader
->info
.Geom
.Invocations
,
1970 shader
->info
.Geom
.Invocations
);
1973 linked_shader
->info
.Geom
.Invocations
= shader
->info
.Geom
.Invocations
;
1977 /* Just do the intrastage -> interstage propagation right now,
1978 * since we already know we're in the right type of shader program
1981 if (linked_shader
->info
.Geom
.InputType
== PRIM_UNKNOWN
) {
1983 "geometry shader didn't declare primitive input type\n");
1987 if (linked_shader
->info
.Geom
.OutputType
== PRIM_UNKNOWN
) {
1989 "geometry shader didn't declare primitive output type\n");
1993 if (linked_shader
->info
.Geom
.VerticesOut
== -1) {
1995 "geometry shader didn't declare max_vertices\n");
1999 if (linked_shader
->info
.Geom
.Invocations
== 0)
2000 linked_shader
->info
.Geom
.Invocations
= 1;
2005 * Perform cross-validation of compute shader local_size_{x,y,z} layout
2006 * qualifiers for the attached compute shaders, and propagate them to the
2007 * linked CS and linked shader program.
2010 link_cs_input_layout_qualifiers(struct gl_shader_program
*prog
,
2011 struct gl_linked_shader
*linked_shader
,
2012 struct gl_shader
**shader_list
,
2013 unsigned num_shaders
)
2015 for (int i
= 0; i
< 3; i
++)
2016 linked_shader
->info
.Comp
.LocalSize
[i
] = 0;
2018 linked_shader
->info
.Comp
.LocalSizeVariable
= false;
2020 /* This function is called for all shader stages, but it only has an effect
2021 * for compute shaders.
2023 if (linked_shader
->Stage
!= MESA_SHADER_COMPUTE
)
2026 /* From the ARB_compute_shader spec, in the section describing local size
2029 * If multiple compute shaders attached to a single program object
2030 * declare local work-group size, the declarations must be identical;
2031 * otherwise a link-time error results. Furthermore, if a program
2032 * object contains any compute shaders, at least one must contain an
2033 * input layout qualifier specifying the local work sizes of the
2034 * program, or a link-time error will occur.
2036 for (unsigned sh
= 0; sh
< num_shaders
; sh
++) {
2037 struct gl_shader
*shader
= shader_list
[sh
];
2039 if (shader
->info
.Comp
.LocalSize
[0] != 0) {
2040 if (linked_shader
->info
.Comp
.LocalSize
[0] != 0) {
2041 for (int i
= 0; i
< 3; i
++) {
2042 if (linked_shader
->info
.Comp
.LocalSize
[i
] !=
2043 shader
->info
.Comp
.LocalSize
[i
]) {
2044 linker_error(prog
, "compute shader defined with conflicting "
2050 for (int i
= 0; i
< 3; i
++) {
2051 linked_shader
->info
.Comp
.LocalSize
[i
] =
2052 shader
->info
.Comp
.LocalSize
[i
];
2054 } else if (shader
->info
.Comp
.LocalSizeVariable
) {
2055 if (linked_shader
->info
.Comp
.LocalSize
[0] != 0) {
2056 /* The ARB_compute_variable_group_size spec says:
2058 * If one compute shader attached to a program declares a
2059 * variable local group size and a second compute shader
2060 * attached to the same program declares a fixed local group
2061 * size, a link-time error results.
2063 linker_error(prog
, "compute shader defined with both fixed and "
2064 "variable local group size\n");
2067 linked_shader
->info
.Comp
.LocalSizeVariable
= true;
2071 /* Just do the intrastage -> interstage propagation right now,
2072 * since we already know we're in the right type of shader program
2075 if (linked_shader
->info
.Comp
.LocalSize
[0] == 0 &&
2076 !linked_shader
->info
.Comp
.LocalSizeVariable
) {
2077 linker_error(prog
, "compute shader must contain a fixed or a variable "
2078 "local group size\n");
2081 for (int i
= 0; i
< 3; i
++)
2082 prog
->Comp
.LocalSize
[i
] = linked_shader
->info
.Comp
.LocalSize
[i
];
2084 prog
->Comp
.LocalSizeVariable
=
2085 linked_shader
->info
.Comp
.LocalSizeVariable
;
2090 * Combine a group of shaders for a single stage to generate a linked shader
2093 * If this function is supplied a single shader, it is cloned, and the new
2094 * shader is returned.
2096 static struct gl_linked_shader
*
2097 link_intrastage_shaders(void *mem_ctx
,
2098 struct gl_context
*ctx
,
2099 struct gl_shader_program
*prog
,
2100 struct gl_shader
**shader_list
,
2101 unsigned num_shaders
)
2103 struct gl_uniform_block
*ubo_blocks
= NULL
;
2104 struct gl_uniform_block
*ssbo_blocks
= NULL
;
2105 unsigned num_ubo_blocks
= 0;
2106 unsigned num_ssbo_blocks
= 0;
2108 /* Check that global variables defined in multiple shaders are consistent.
2110 glsl_symbol_table variables
;
2111 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2112 if (shader_list
[i
] == NULL
)
2114 cross_validate_globals(prog
, shader_list
[i
]->ir
, &variables
, false);
2117 if (!prog
->LinkStatus
)
2120 /* Check that interface blocks defined in multiple shaders are consistent.
2122 validate_intrastage_interface_blocks(prog
, (const gl_shader
**)shader_list
,
2124 if (!prog
->LinkStatus
)
2127 /* Check that there is only a single definition of each function signature
2128 * across all shaders.
2130 for (unsigned i
= 0; i
< (num_shaders
- 1); i
++) {
2131 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
2132 ir_function
*const f
= node
->as_function();
2137 for (unsigned j
= i
+ 1; j
< num_shaders
; j
++) {
2138 ir_function
*const other
=
2139 shader_list
[j
]->symbols
->get_function(f
->name
);
2141 /* If the other shader has no function (and therefore no function
2142 * signatures) with the same name, skip to the next shader.
2147 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
2148 if (!sig
->is_defined
)
2151 ir_function_signature
*other_sig
=
2152 other
->exact_matching_signature(NULL
, &sig
->parameters
);
2154 if (other_sig
!= NULL
&& other_sig
->is_defined
) {
2155 linker_error(prog
, "function `%s' is multiply defined\n",
2164 /* Find the shader that defines main, and make a clone of it.
2166 * Starting with the clone, search for undefined references. If one is
2167 * found, find the shader that defines it. Clone the reference and add
2168 * it to the shader. Repeat until there are no undefined references or
2169 * until a reference cannot be resolved.
2171 gl_shader
*main
= NULL
;
2172 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2173 if (_mesa_get_main_function_signature(shader_list
[i
]->symbols
)) {
2174 main
= shader_list
[i
];
2180 linker_error(prog
, "%s shader lacks `main'\n",
2181 _mesa_shader_stage_to_string(shader_list
[0]->Stage
));
2185 gl_linked_shader
*linked
= ctx
->Driver
.NewShader(shader_list
[0]->Stage
);
2186 linked
->ir
= new(linked
) exec_list
;
2187 clone_ir_list(mem_ctx
, linked
->ir
, main
->ir
);
2189 link_fs_inout_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2190 link_tcs_out_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2191 link_tes_in_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2192 link_gs_inout_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2193 link_cs_input_layout_qualifiers(prog
, linked
, shader_list
, num_shaders
);
2194 link_xfb_stride_layout_qualifiers(ctx
, prog
, linked
, shader_list
,
2197 populate_symbol_table(linked
);
2199 /* The pointer to the main function in the final linked shader (i.e., the
2200 * copy of the original shader that contained the main function).
2202 ir_function_signature
*const main_sig
=
2203 _mesa_get_main_function_signature(linked
->symbols
);
2205 /* Move any instructions other than variable declarations or function
2206 * declarations into main.
2208 exec_node
*insertion_point
=
2209 move_non_declarations(linked
->ir
, (exec_node
*) &main_sig
->body
, false,
2212 for (unsigned i
= 0; i
< num_shaders
; i
++) {
2213 if (shader_list
[i
] == main
)
2216 insertion_point
= move_non_declarations(shader_list
[i
]->ir
,
2217 insertion_point
, true, linked
);
2220 if (!link_function_calls(prog
, linked
, shader_list
, num_shaders
)) {
2221 _mesa_delete_linked_shader(ctx
, linked
);
2225 /* Make a pass over all variable declarations to ensure that arrays with
2226 * unspecified sizes have a size specified. The size is inferred from the
2227 * max_array_access field.
2229 array_sizing_visitor v
;
2231 v
.fixup_unnamed_interface_types();
2233 /* Link up uniform blocks defined within this stage. */
2234 link_uniform_blocks(mem_ctx
, ctx
, prog
, linked
, &ubo_blocks
,
2235 &num_ubo_blocks
, &ssbo_blocks
, &num_ssbo_blocks
);
2237 if (!prog
->LinkStatus
) {
2238 _mesa_delete_linked_shader(ctx
, linked
);
2242 /* Copy ubo blocks to linked shader list */
2243 linked
->UniformBlocks
=
2244 ralloc_array(linked
, gl_uniform_block
*, num_ubo_blocks
);
2245 ralloc_steal(linked
, ubo_blocks
);
2246 for (unsigned i
= 0; i
< num_ubo_blocks
; i
++) {
2247 linked
->UniformBlocks
[i
] = &ubo_blocks
[i
];
2249 linked
->NumUniformBlocks
= num_ubo_blocks
;
2251 /* Copy ssbo blocks to linked shader list */
2252 linked
->ShaderStorageBlocks
=
2253 ralloc_array(linked
, gl_uniform_block
*, num_ssbo_blocks
);
2254 ralloc_steal(linked
, ssbo_blocks
);
2255 for (unsigned i
= 0; i
< num_ssbo_blocks
; i
++) {
2256 linked
->ShaderStorageBlocks
[i
] = &ssbo_blocks
[i
];
2258 linked
->NumShaderStorageBlocks
= num_ssbo_blocks
;
2260 /* At this point linked should contain all of the linked IR, so
2261 * validate it to make sure nothing went wrong.
2263 validate_ir_tree(linked
->ir
);
2265 /* Set the size of geometry shader input arrays */
2266 if (linked
->Stage
== MESA_SHADER_GEOMETRY
) {
2267 unsigned num_vertices
= vertices_per_prim(linked
->info
.Geom
.InputType
);
2268 array_resize_visitor
input_resize_visitor(num_vertices
, prog
,
2269 MESA_SHADER_GEOMETRY
);
2270 foreach_in_list(ir_instruction
, ir
, linked
->ir
) {
2271 ir
->accept(&input_resize_visitor
);
2275 if (ctx
->Const
.VertexID_is_zero_based
)
2276 lower_vertex_id(linked
);
2282 * Update the sizes of linked shader uniform arrays to the maximum
2285 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
2287 * If one or more elements of an array are active,
2288 * GetActiveUniform will return the name of the array in name,
2289 * subject to the restrictions listed above. The type of the array
2290 * is returned in type. The size parameter contains the highest
2291 * array element index used, plus one. The compiler or linker
2292 * determines the highest index used. There will be only one
2293 * active uniform reported by the GL per uniform array.
2297 update_array_sizes(struct gl_shader_program
*prog
)
2299 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2300 if (prog
->_LinkedShaders
[i
] == NULL
)
2303 bool types_were_updated
= false;
2305 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
2306 ir_variable
*const var
= node
->as_variable();
2308 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
) ||
2309 !var
->type
->is_array())
2312 /* GL_ARB_uniform_buffer_object says that std140 uniforms
2313 * will not be eliminated. Since we always do std140, just
2314 * don't resize arrays in UBOs.
2316 * Atomic counters are supposed to get deterministic
2317 * locations assigned based on the declaration ordering and
2318 * sizes, array compaction would mess that up.
2320 * Subroutine uniforms are not removed.
2322 if (var
->is_in_buffer_block() || var
->type
->contains_atomic() ||
2323 var
->type
->contains_subroutine() || var
->constant_initializer
)
2326 int size
= var
->data
.max_array_access
;
2327 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
2328 if (prog
->_LinkedShaders
[j
] == NULL
)
2331 foreach_in_list(ir_instruction
, node2
, prog
->_LinkedShaders
[j
]->ir
) {
2332 ir_variable
*other_var
= node2
->as_variable();
2336 if (strcmp(var
->name
, other_var
->name
) == 0 &&
2337 other_var
->data
.max_array_access
> size
) {
2338 size
= other_var
->data
.max_array_access
;
2343 if (size
+ 1 != (int)var
->type
->length
) {
2344 /* If this is a built-in uniform (i.e., it's backed by some
2345 * fixed-function state), adjust the number of state slots to
2346 * match the new array size. The number of slots per array entry
2347 * is not known. It seems safe to assume that the total number of
2348 * slots is an integer multiple of the number of array elements.
2349 * Determine the number of slots per array element by dividing by
2350 * the old (total) size.
2352 const unsigned num_slots
= var
->get_num_state_slots();
2353 if (num_slots
> 0) {
2354 var
->set_num_state_slots((size
+ 1)
2355 * (num_slots
/ var
->type
->length
));
2358 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2360 types_were_updated
= true;
2364 /* Update the types of dereferences in case we changed any. */
2365 if (types_were_updated
) {
2366 deref_type_updater v
;
2367 v
.run(prog
->_LinkedShaders
[i
]->ir
);
2373 * Resize tessellation evaluation per-vertex inputs to the size of
2374 * tessellation control per-vertex outputs.
2377 resize_tes_inputs(struct gl_context
*ctx
,
2378 struct gl_shader_program
*prog
)
2380 if (prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] == NULL
)
2383 gl_linked_shader
*const tcs
= prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
];
2384 gl_linked_shader
*const tes
= prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
2386 /* If no control shader is present, then the TES inputs are statically
2387 * sized to MaxPatchVertices; the actual size of the arrays won't be
2388 * known until draw time.
2390 const int num_vertices
= tcs
2391 ? tcs
->info
.TessCtrl
.VerticesOut
2392 : ctx
->Const
.MaxPatchVertices
;
2394 array_resize_visitor
input_resize_visitor(num_vertices
, prog
,
2395 MESA_SHADER_TESS_EVAL
);
2396 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2397 ir
->accept(&input_resize_visitor
);
2400 if (tcs
|| ctx
->Const
.LowerTESPatchVerticesIn
) {
2401 /* Convert the gl_PatchVerticesIn system value into a constant, since
2402 * the value is known at this point.
2404 foreach_in_list(ir_instruction
, ir
, tes
->ir
) {
2405 ir_variable
*var
= ir
->as_variable();
2406 if (var
&& var
->data
.mode
== ir_var_system_value
&&
2407 var
->data
.location
== SYSTEM_VALUE_VERTICES_IN
) {
2408 void *mem_ctx
= ralloc_parent(var
);
2409 var
->data
.location
= 0;
2410 var
->data
.explicit_location
= false;
2412 var
->data
.mode
= ir_var_auto
;
2413 var
->constant_value
= new(mem_ctx
) ir_constant(num_vertices
);
2415 var
->data
.mode
= ir_var_uniform
;
2416 var
->data
.how_declared
= ir_var_hidden
;
2417 var
->allocate_state_slots(1);
2418 ir_state_slot
*slot0
= &var
->get_state_slots()[0];
2419 slot0
->swizzle
= SWIZZLE_XXXX
;
2420 slot0
->tokens
[0] = STATE_INTERNAL
;
2421 slot0
->tokens
[1] = STATE_TES_PATCH_VERTICES_IN
;
2422 for (int i
= 2; i
< STATE_LENGTH
; i
++)
2423 slot0
->tokens
[i
] = 0;
2431 * Find a contiguous set of available bits in a bitmask.
2433 * \param used_mask Bits representing used (1) and unused (0) locations
2434 * \param needed_count Number of contiguous bits needed.
2437 * Base location of the available bits on success or -1 on failure.
2440 find_available_slots(unsigned used_mask
, unsigned needed_count
)
2442 unsigned needed_mask
= (1 << needed_count
) - 1;
2443 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
2445 /* The comparison to 32 is redundant, but without it GCC emits "warning:
2446 * cannot optimize possibly infinite loops" for the loop below.
2448 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
2451 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
2452 if ((needed_mask
& ~used_mask
) == needed_mask
)
2463 * Assign locations for either VS inputs or FS outputs
2465 * \param mem_ctx Temporary ralloc context used for linking
2466 * \param prog Shader program whose variables need locations assigned
2467 * \param constants Driver specific constant values for the program.
2468 * \param target_index Selector for the program target to receive location
2469 * assignmnets. Must be either \c MESA_SHADER_VERTEX or
2470 * \c MESA_SHADER_FRAGMENT.
2473 * If locations are successfully assigned, true is returned. Otherwise an
2474 * error is emitted to the shader link log and false is returned.
2477 assign_attribute_or_color_locations(void *mem_ctx
,
2478 gl_shader_program
*prog
,
2479 struct gl_constants
*constants
,
2480 unsigned target_index
)
2482 /* Maximum number of generic locations. This corresponds to either the
2483 * maximum number of draw buffers or the maximum number of generic
2486 unsigned max_index
= (target_index
== MESA_SHADER_VERTEX
) ?
2487 constants
->Program
[target_index
].MaxAttribs
:
2488 MAX2(constants
->MaxDrawBuffers
, constants
->MaxDualSourceDrawBuffers
);
2490 /* Mark invalid locations as being used.
2492 unsigned used_locations
= (max_index
>= 32)
2493 ? ~0 : ~((1 << max_index
) - 1);
2494 unsigned double_storage_locations
= 0;
2496 assert((target_index
== MESA_SHADER_VERTEX
)
2497 || (target_index
== MESA_SHADER_FRAGMENT
));
2499 gl_linked_shader
*const sh
= prog
->_LinkedShaders
[target_index
];
2503 /* Operate in a total of four passes.
2505 * 1. Invalidate the location assignments for all vertex shader inputs.
2507 * 2. Assign locations for inputs that have user-defined (via
2508 * glBindVertexAttribLocation) locations and outputs that have
2509 * user-defined locations (via glBindFragDataLocation).
2511 * 3. Sort the attributes without assigned locations by number of slots
2512 * required in decreasing order. Fragmentation caused by attribute
2513 * locations assigned by the application may prevent large attributes
2514 * from having enough contiguous space.
2516 * 4. Assign locations to any inputs without assigned locations.
2519 const int generic_base
= (target_index
== MESA_SHADER_VERTEX
)
2520 ? (int) VERT_ATTRIB_GENERIC0
: (int) FRAG_RESULT_DATA0
;
2522 const enum ir_variable_mode direction
=
2523 (target_index
== MESA_SHADER_VERTEX
)
2524 ? ir_var_shader_in
: ir_var_shader_out
;
2527 /* Temporary storage for the set of attributes that need locations assigned.
2533 /* Used below in the call to qsort. */
2534 static int compare(const void *a
, const void *b
)
2536 const temp_attr
*const l
= (const temp_attr
*) a
;
2537 const temp_attr
*const r
= (const temp_attr
*) b
;
2539 /* Reversed because we want a descending order sort below. */
2540 return r
->slots
- l
->slots
;
2543 assert(max_index
<= 32);
2545 /* Temporary array for the set of attributes that have locations assigned.
2547 ir_variable
*assigned
[16];
2549 unsigned num_attr
= 0;
2550 unsigned assigned_attr
= 0;
2552 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
2553 ir_variable
*const var
= node
->as_variable();
2555 if ((var
== NULL
) || (var
->data
.mode
!= (unsigned) direction
))
2558 if (var
->data
.explicit_location
) {
2559 var
->data
.is_unmatched_generic_inout
= 0;
2560 if ((var
->data
.location
>= (int)(max_index
+ generic_base
))
2561 || (var
->data
.location
< 0)) {
2563 "invalid explicit location %d specified for `%s'\n",
2564 (var
->data
.location
< 0)
2565 ? var
->data
.location
2566 : var
->data
.location
- generic_base
,
2570 } else if (target_index
== MESA_SHADER_VERTEX
) {
2573 if (prog
->AttributeBindings
->get(binding
, var
->name
)) {
2574 assert(binding
>= VERT_ATTRIB_GENERIC0
);
2575 var
->data
.location
= binding
;
2576 var
->data
.is_unmatched_generic_inout
= 0;
2578 } else if (target_index
== MESA_SHADER_FRAGMENT
) {
2581 const char *name
= var
->name
;
2582 const glsl_type
*type
= var
->type
;
2585 /* Check if there's a binding for the variable name */
2586 if (prog
->FragDataBindings
->get(binding
, name
)) {
2587 assert(binding
>= FRAG_RESULT_DATA0
);
2588 var
->data
.location
= binding
;
2589 var
->data
.is_unmatched_generic_inout
= 0;
2591 if (prog
->FragDataIndexBindings
->get(index
, name
)) {
2592 var
->data
.index
= index
;
2597 /* If not, but it's an array type, look for name[0] */
2598 if (type
->is_array()) {
2599 name
= ralloc_asprintf(mem_ctx
, "%s[0]", name
);
2600 type
= type
->fields
.array
;
2608 if (strcmp(var
->name
, "gl_LastFragData") == 0)
2611 /* From GL4.5 core spec, section 15.2 (Shader Execution):
2613 * "Output binding assignments will cause LinkProgram to fail:
2615 * If the program has an active output assigned to a location greater
2616 * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
2617 * an active output assigned an index greater than or equal to one;"
2619 if (target_index
== MESA_SHADER_FRAGMENT
&& var
->data
.index
>= 1 &&
2620 var
->data
.location
- generic_base
>=
2621 (int) constants
->MaxDualSourceDrawBuffers
) {
2623 "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
2624 "with index %u for %s\n",
2625 var
->data
.location
- generic_base
, var
->data
.index
,
2630 const unsigned slots
= var
->type
->count_attribute_slots(target_index
== MESA_SHADER_VERTEX
);
2632 /* If the variable is not a built-in and has a location statically
2633 * assigned in the shader (presumably via a layout qualifier), make sure
2634 * that it doesn't collide with other assigned locations. Otherwise,
2635 * add it to the list of variables that need linker-assigned locations.
2637 if (var
->data
.location
!= -1) {
2638 if (var
->data
.location
>= generic_base
&& var
->data
.index
< 1) {
2639 /* From page 61 of the OpenGL 4.0 spec:
2641 * "LinkProgram will fail if the attribute bindings assigned
2642 * by BindAttribLocation do not leave not enough space to
2643 * assign a location for an active matrix attribute or an
2644 * active attribute array, both of which require multiple
2645 * contiguous generic attributes."
2647 * I think above text prohibits the aliasing of explicit and
2648 * automatic assignments. But, aliasing is allowed in manual
2649 * assignments of attribute locations. See below comments for
2652 * From OpenGL 4.0 spec, page 61:
2654 * "It is possible for an application to bind more than one
2655 * attribute name to the same location. This is referred to as
2656 * aliasing. This will only work if only one of the aliased
2657 * attributes is active in the executable program, or if no
2658 * path through the shader consumes more than one attribute of
2659 * a set of attributes aliased to the same location. A link
2660 * error can occur if the linker determines that every path
2661 * through the shader consumes multiple aliased attributes,
2662 * but implementations are not required to generate an error
2665 * From GLSL 4.30 spec, page 54:
2667 * "A program will fail to link if any two non-vertex shader
2668 * input variables are assigned to the same location. For
2669 * vertex shaders, multiple input variables may be assigned
2670 * to the same location using either layout qualifiers or via
2671 * the OpenGL API. However, such aliasing is intended only to
2672 * support vertex shaders where each execution path accesses
2673 * at most one input per each location. Implementations are
2674 * permitted, but not required, to generate link-time errors
2675 * if they detect that every path through the vertex shader
2676 * executable accesses multiple inputs assigned to any single
2677 * location. For all shader types, a program will fail to link
2678 * if explicit location assignments leave the linker unable
2679 * to find space for other variables without explicit
2682 * From OpenGL ES 3.0 spec, page 56:
2684 * "Binding more than one attribute name to the same location
2685 * is referred to as aliasing, and is not permitted in OpenGL
2686 * ES Shading Language 3.00 vertex shaders. LinkProgram will
2687 * fail when this condition exists. However, aliasing is
2688 * possible in OpenGL ES Shading Language 1.00 vertex shaders.
2689 * This will only work if only one of the aliased attributes
2690 * is active in the executable program, or if no path through
2691 * the shader consumes more than one attribute of a set of
2692 * attributes aliased to the same location. A link error can
2693 * occur if the linker determines that every path through the
2694 * shader consumes multiple aliased attributes, but implemen-
2695 * tations are not required to generate an error in this case."
2697 * After looking at above references from OpenGL, OpenGL ES and
2698 * GLSL specifications, we allow aliasing of vertex input variables
2699 * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
2701 * NOTE: This is not required by the spec but its worth mentioning
2702 * here that we're not doing anything to make sure that no path
2703 * through the vertex shader executable accesses multiple inputs
2704 * assigned to any single location.
2707 /* Mask representing the contiguous slots that will be used by
2710 const unsigned attr
= var
->data
.location
- generic_base
;
2711 const unsigned use_mask
= (1 << slots
) - 1;
2712 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
2713 ? "vertex shader input" : "fragment shader output";
2715 /* Generate a link error if the requested locations for this
2716 * attribute exceed the maximum allowed attribute location.
2718 if (attr
+ slots
> max_index
) {
2720 "insufficient contiguous locations "
2721 "available for %s `%s' %d %d %d\n", string
,
2722 var
->name
, used_locations
, use_mask
, attr
);
2726 /* Generate a link error if the set of bits requested for this
2727 * attribute overlaps any previously allocated bits.
2729 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
2730 if (target_index
== MESA_SHADER_FRAGMENT
&& !prog
->IsES
) {
2731 /* From section 4.4.2 (Output Layout Qualifiers) of the GLSL
2734 * "Additionally, for fragment shader outputs, if two
2735 * variables are placed within the same location, they
2736 * must have the same underlying type (floating-point or
2737 * integer). No component aliasing of output variables or
2738 * members is allowed.
2740 for (unsigned i
= 0; i
< assigned_attr
; i
++) {
2741 unsigned assigned_slots
=
2742 assigned
[i
]->type
->count_attribute_slots(false);
2743 unsigned assig_attr
=
2744 assigned
[i
]->data
.location
- generic_base
;
2745 unsigned assigned_use_mask
= (1 << assigned_slots
) - 1;
2747 if ((assigned_use_mask
<< assig_attr
) &
2748 (use_mask
<< attr
)) {
2750 const glsl_type
*assigned_type
=
2751 assigned
[i
]->type
->without_array();
2752 const glsl_type
*type
= var
->type
->without_array();
2753 if (assigned_type
->base_type
!= type
->base_type
) {
2754 linker_error(prog
, "types do not match for aliased"
2755 " %ss %s and %s\n", string
,
2756 assigned
[i
]->name
, var
->name
);
2760 unsigned assigned_component_mask
=
2761 ((1 << assigned_type
->vector_elements
) - 1) <<
2762 assigned
[i
]->data
.location_frac
;
2763 unsigned component_mask
=
2764 ((1 << type
->vector_elements
) - 1) <<
2765 var
->data
.location_frac
;
2766 if (assigned_component_mask
& component_mask
) {
2767 linker_error(prog
, "overlapping component is "
2768 "assigned to %ss %s and %s "
2770 string
, assigned
[i
]->name
, var
->name
,
2771 var
->data
.location_frac
);
2776 } else if (target_index
== MESA_SHADER_FRAGMENT
||
2777 (prog
->IsES
&& prog
->Version
>= 300)) {
2778 linker_error(prog
, "overlapping location is assigned "
2779 "to %s `%s' %d %d %d\n", string
, var
->name
,
2780 used_locations
, use_mask
, attr
);
2783 linker_warning(prog
, "overlapping location is assigned "
2784 "to %s `%s' %d %d %d\n", string
, var
->name
,
2785 used_locations
, use_mask
, attr
);
2789 used_locations
|= (use_mask
<< attr
);
2791 /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
2793 * "A program with more than the value of MAX_VERTEX_ATTRIBS
2794 * active attribute variables may fail to link, unless
2795 * device-dependent optimizations are able to make the program
2796 * fit within available hardware resources. For the purposes
2797 * of this test, attribute variables of the type dvec3, dvec4,
2798 * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
2799 * count as consuming twice as many attributes as equivalent
2800 * single-precision types. While these types use the same number
2801 * of generic attributes as their single-precision equivalents,
2802 * implementations are permitted to consume two single-precision
2803 * vectors of internal storage for each three- or four-component
2804 * double-precision vector."
2806 * Mark this attribute slot as taking up twice as much space
2807 * so we can count it properly against limits. According to
2808 * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
2809 * is optional behavior, but it seems preferable.
2811 if (var
->type
->without_array()->is_dual_slot())
2812 double_storage_locations
|= (use_mask
<< attr
);
2815 assigned
[assigned_attr
] = var
;
2821 if (num_attr
>= max_index
) {
2822 linker_error(prog
, "too many %s (max %u)",
2823 target_index
== MESA_SHADER_VERTEX
?
2824 "vertex shader inputs" : "fragment shader outputs",
2828 to_assign
[num_attr
].slots
= slots
;
2829 to_assign
[num_attr
].var
= var
;
2833 if (target_index
== MESA_SHADER_VERTEX
) {
2834 unsigned total_attribs_size
=
2835 _mesa_bitcount(used_locations
& ((1 << max_index
) - 1)) +
2836 _mesa_bitcount(double_storage_locations
);
2837 if (total_attribs_size
> max_index
) {
2839 "attempt to use %d vertex attribute slots only %d available ",
2840 total_attribs_size
, max_index
);
2845 /* If all of the attributes were assigned locations by the application (or
2846 * are built-in attributes with fixed locations), return early. This should
2847 * be the common case.
2852 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
2854 if (target_index
== MESA_SHADER_VERTEX
) {
2855 /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
2856 * only be explicitly assigned by via glBindAttribLocation. Mark it as
2857 * reserved to prevent it from being automatically allocated below.
2859 find_deref_visitor
find("gl_Vertex");
2861 if (find
.variable_found())
2862 used_locations
|= (1 << 0);
2865 for (unsigned i
= 0; i
< num_attr
; i
++) {
2866 /* Mask representing the contiguous slots that will be used by this
2869 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
2871 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
2874 const char *const string
= (target_index
== MESA_SHADER_VERTEX
)
2875 ? "vertex shader input" : "fragment shader output";
2878 "insufficient contiguous locations "
2879 "available for %s `%s'\n",
2880 string
, to_assign
[i
].var
->name
);
2884 to_assign
[i
].var
->data
.location
= generic_base
+ location
;
2885 to_assign
[i
].var
->data
.is_unmatched_generic_inout
= 0;
2886 used_locations
|= (use_mask
<< location
);
2888 if (to_assign
[i
].var
->type
->without_array()->is_dual_slot())
2889 double_storage_locations
|= (use_mask
<< location
);
2892 /* Now that we have all the locations, from the GL 4.5 core spec, section
2893 * 11.1.1 (Vertex Attributes), dvec3, dvec4, dmat2x3, dmat2x4, dmat3,
2894 * dmat3x4, dmat4x3, and dmat4 count as consuming twice as many attributes
2895 * as equivalent single-precision types.
2897 if (target_index
== MESA_SHADER_VERTEX
) {
2898 unsigned total_attribs_size
=
2899 _mesa_bitcount(used_locations
& ((1 << max_index
) - 1)) +
2900 _mesa_bitcount(double_storage_locations
);
2901 if (total_attribs_size
> max_index
) {
2903 "attempt to use %d vertex attribute slots only %d available ",
2904 total_attribs_size
, max_index
);
2913 * Match explicit locations of outputs to inputs and deactivate the
2914 * unmatch flag if found so we don't optimise them away.
2917 match_explicit_outputs_to_inputs(gl_linked_shader
*producer
,
2918 gl_linked_shader
*consumer
)
2920 glsl_symbol_table parameters
;
2921 ir_variable
*explicit_locations
[MAX_VARYINGS_INCL_PATCH
][4] =
2924 /* Find all shader outputs in the "producer" stage.
2926 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
2927 ir_variable
*const var
= node
->as_variable();
2929 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_shader_out
))
2932 if (var
->data
.explicit_location
&&
2933 var
->data
.location
>= VARYING_SLOT_VAR0
) {
2934 const unsigned idx
= var
->data
.location
- VARYING_SLOT_VAR0
;
2935 if (explicit_locations
[idx
][var
->data
.location_frac
] == NULL
)
2936 explicit_locations
[idx
][var
->data
.location_frac
] = var
;
2940 /* Match inputs to outputs */
2941 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
2942 ir_variable
*const input
= node
->as_variable();
2944 if ((input
== NULL
) || (input
->data
.mode
!= ir_var_shader_in
))
2947 ir_variable
*output
= NULL
;
2948 if (input
->data
.explicit_location
2949 && input
->data
.location
>= VARYING_SLOT_VAR0
) {
2950 output
= explicit_locations
[input
->data
.location
- VARYING_SLOT_VAR0
]
2951 [input
->data
.location_frac
];
2953 if (output
!= NULL
){
2954 input
->data
.is_unmatched_generic_inout
= 0;
2955 output
->data
.is_unmatched_generic_inout
= 0;
2962 * Store the gl_FragDepth layout in the gl_shader_program struct.
2965 store_fragdepth_layout(struct gl_shader_program
*prog
)
2967 if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
2971 struct exec_list
*ir
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
2973 /* We don't look up the gl_FragDepth symbol directly because if
2974 * gl_FragDepth is not used in the shader, it's removed from the IR.
2975 * However, the symbol won't be removed from the symbol table.
2977 * We're only interested in the cases where the variable is NOT removed
2980 foreach_in_list(ir_instruction
, node
, ir
) {
2981 ir_variable
*const var
= node
->as_variable();
2983 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
) {
2987 if (strcmp(var
->name
, "gl_FragDepth") == 0) {
2988 switch (var
->data
.depth_layout
) {
2989 case ir_depth_layout_none
:
2990 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
2992 case ir_depth_layout_any
:
2993 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
2995 case ir_depth_layout_greater
:
2996 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
2998 case ir_depth_layout_less
:
2999 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
3001 case ir_depth_layout_unchanged
:
3002 prog
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3013 * Validate the resources used by a program versus the implementation limits
3016 check_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3018 unsigned total_uniform_blocks
= 0;
3019 unsigned total_shader_storage_blocks
= 0;
3021 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3022 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3027 if (sh
->num_samplers
> ctx
->Const
.Program
[i
].MaxTextureImageUnits
) {
3028 linker_error(prog
, "Too many %s shader texture samplers\n",
3029 _mesa_shader_stage_to_string(i
));
3032 if (sh
->num_uniform_components
>
3033 ctx
->Const
.Program
[i
].MaxUniformComponents
) {
3034 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
3035 linker_warning(prog
, "Too many %s shader default uniform block "
3036 "components, but the driver will try to optimize "
3037 "them out; this is non-portable out-of-spec "
3039 _mesa_shader_stage_to_string(i
));
3041 linker_error(prog
, "Too many %s shader default uniform block "
3043 _mesa_shader_stage_to_string(i
));
3047 if (sh
->num_combined_uniform_components
>
3048 ctx
->Const
.Program
[i
].MaxCombinedUniformComponents
) {
3049 if (ctx
->Const
.GLSLSkipStrictMaxUniformLimitCheck
) {
3050 linker_warning(prog
, "Too many %s shader uniform components, "
3051 "but the driver will try to optimize them out; "
3052 "this is non-portable out-of-spec behavior\n",
3053 _mesa_shader_stage_to_string(i
));
3055 linker_error(prog
, "Too many %s shader uniform components\n",
3056 _mesa_shader_stage_to_string(i
));
3060 total_shader_storage_blocks
+= sh
->NumShaderStorageBlocks
;
3061 total_uniform_blocks
+= sh
->NumUniformBlocks
;
3063 const unsigned max_uniform_blocks
=
3064 ctx
->Const
.Program
[i
].MaxUniformBlocks
;
3065 if (max_uniform_blocks
< sh
->NumUniformBlocks
) {
3066 linker_error(prog
, "Too many %s uniform blocks (%d/%d)\n",
3067 _mesa_shader_stage_to_string(i
), sh
->NumUniformBlocks
,
3068 max_uniform_blocks
);
3071 const unsigned max_shader_storage_blocks
=
3072 ctx
->Const
.Program
[i
].MaxShaderStorageBlocks
;
3073 if (max_shader_storage_blocks
< sh
->NumShaderStorageBlocks
) {
3074 linker_error(prog
, "Too many %s shader storage blocks (%d/%d)\n",
3075 _mesa_shader_stage_to_string(i
),
3076 sh
->NumShaderStorageBlocks
, max_shader_storage_blocks
);
3080 if (total_uniform_blocks
> ctx
->Const
.MaxCombinedUniformBlocks
) {
3081 linker_error(prog
, "Too many combined uniform blocks (%d/%d)\n",
3082 total_uniform_blocks
, ctx
->Const
.MaxCombinedUniformBlocks
);
3085 if (total_shader_storage_blocks
> ctx
->Const
.MaxCombinedShaderStorageBlocks
) {
3086 linker_error(prog
, "Too many combined shader storage blocks (%d/%d)\n",
3087 total_shader_storage_blocks
,
3088 ctx
->Const
.MaxCombinedShaderStorageBlocks
);
3091 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
3092 if (prog
->UniformBlocks
[i
].UniformBufferSize
>
3093 ctx
->Const
.MaxUniformBlockSize
) {
3094 linker_error(prog
, "Uniform block %s too big (%d/%d)\n",
3095 prog
->UniformBlocks
[i
].Name
,
3096 prog
->UniformBlocks
[i
].UniformBufferSize
,
3097 ctx
->Const
.MaxUniformBlockSize
);
3101 for (unsigned i
= 0; i
< prog
->NumShaderStorageBlocks
; i
++) {
3102 if (prog
->ShaderStorageBlocks
[i
].UniformBufferSize
>
3103 ctx
->Const
.MaxShaderStorageBlockSize
) {
3104 linker_error(prog
, "Shader storage block %s too big (%d/%d)\n",
3105 prog
->ShaderStorageBlocks
[i
].Name
,
3106 prog
->ShaderStorageBlocks
[i
].UniformBufferSize
,
3107 ctx
->Const
.MaxShaderStorageBlockSize
);
3113 link_calculate_subroutine_compat(struct gl_shader_program
*prog
)
3115 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3116 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3121 for (unsigned j
= 0; j
< sh
->NumSubroutineUniformRemapTable
; j
++) {
3122 if (sh
->SubroutineUniformRemapTable
[j
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
)
3125 struct gl_uniform_storage
*uni
= sh
->SubroutineUniformRemapTable
[j
];
3130 sh
->NumSubroutineUniforms
++;
3132 if (sh
->NumSubroutineFunctions
== 0) {
3133 linker_error(prog
, "subroutine uniform %s defined but no valid functions found\n", uni
->type
->name
);
3136 for (unsigned f
= 0; f
< sh
->NumSubroutineFunctions
; f
++) {
3137 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[f
];
3138 for (int k
= 0; k
< fn
->num_compat_types
; k
++) {
3139 if (fn
->types
[k
] == uni
->type
) {
3145 uni
->num_compatible_subroutines
= count
;
3151 check_subroutine_resources(struct gl_shader_program
*prog
)
3153 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3154 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3157 if (sh
->NumSubroutineUniformRemapTable
> MAX_SUBROUTINE_UNIFORM_LOCATIONS
)
3158 linker_error(prog
, "Too many %s shader subroutine uniforms\n",
3159 _mesa_shader_stage_to_string(i
));
3164 * Validate shader image resources.
3167 check_image_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3169 unsigned total_image_units
= 0;
3170 unsigned fragment_outputs
= 0;
3171 unsigned total_shader_storage_blocks
= 0;
3173 if (!ctx
->Extensions
.ARB_shader_image_load_store
)
3176 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3177 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3180 if (sh
->NumImages
> ctx
->Const
.Program
[i
].MaxImageUniforms
)
3181 linker_error(prog
, "Too many %s shader image uniforms (%u > %u)\n",
3182 _mesa_shader_stage_to_string(i
), sh
->NumImages
,
3183 ctx
->Const
.Program
[i
].MaxImageUniforms
);
3185 total_image_units
+= sh
->NumImages
;
3186 total_shader_storage_blocks
+= sh
->NumShaderStorageBlocks
;
3188 if (i
== MESA_SHADER_FRAGMENT
) {
3189 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3190 ir_variable
*var
= node
->as_variable();
3191 if (var
&& var
->data
.mode
== ir_var_shader_out
)
3192 /* since there are no double fs outputs - pass false */
3193 fragment_outputs
+= var
->type
->count_attribute_slots(false);
3199 if (total_image_units
> ctx
->Const
.MaxCombinedImageUniforms
)
3200 linker_error(prog
, "Too many combined image uniforms\n");
3202 if (total_image_units
+ fragment_outputs
+ total_shader_storage_blocks
>
3203 ctx
->Const
.MaxCombinedShaderOutputResources
)
3204 linker_error(prog
, "Too many combined image uniforms, shader storage "
3205 " buffers and fragment outputs\n");
3210 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
3211 * for a variable, checks for overlaps between other uniforms using explicit
3215 reserve_explicit_locations(struct gl_shader_program
*prog
,
3216 string_to_uint_map
*map
, ir_variable
*var
)
3218 unsigned slots
= var
->type
->uniform_locations();
3219 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3220 unsigned return_value
= slots
;
3222 /* Resize remap table if locations do not fit in the current one. */
3223 if (max_loc
+ 1 > prog
->NumUniformRemapTable
) {
3224 prog
->UniformRemapTable
=
3225 reralloc(prog
, prog
->UniformRemapTable
,
3226 gl_uniform_storage
*,
3229 if (!prog
->UniformRemapTable
) {
3230 linker_error(prog
, "Out of memory during linking.\n");
3234 /* Initialize allocated space. */
3235 for (unsigned i
= prog
->NumUniformRemapTable
; i
< max_loc
+ 1; i
++)
3236 prog
->UniformRemapTable
[i
] = NULL
;
3238 prog
->NumUniformRemapTable
= max_loc
+ 1;
3241 for (unsigned i
= 0; i
< slots
; i
++) {
3242 unsigned loc
= var
->data
.location
+ i
;
3244 /* Check if location is already used. */
3245 if (prog
->UniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3247 /* Possibly same uniform from a different stage, this is ok. */
3249 if (map
->get(hash_loc
, var
->name
) && hash_loc
== loc
- i
) {
3254 /* ARB_explicit_uniform_location specification states:
3256 * "No two default-block uniform variables in the program can have
3257 * the same location, even if they are unused, otherwise a compiler
3258 * or linker error will be generated."
3261 "location qualifier for uniform %s overlaps "
3262 "previously used location\n",
3267 /* Initialize location as inactive before optimization
3268 * rounds and location assignment.
3270 prog
->UniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3273 /* Note, base location used for arrays. */
3274 map
->put(var
->data
.location
, var
->name
);
3276 return return_value
;
3280 reserve_subroutine_explicit_locations(struct gl_shader_program
*prog
,
3281 struct gl_linked_shader
*sh
,
3284 unsigned slots
= var
->type
->uniform_locations();
3285 unsigned max_loc
= var
->data
.location
+ slots
- 1;
3287 /* Resize remap table if locations do not fit in the current one. */
3288 if (max_loc
+ 1 > sh
->NumSubroutineUniformRemapTable
) {
3289 sh
->SubroutineUniformRemapTable
=
3290 reralloc(sh
, sh
->SubroutineUniformRemapTable
,
3291 gl_uniform_storage
*,
3294 if (!sh
->SubroutineUniformRemapTable
) {
3295 linker_error(prog
, "Out of memory during linking.\n");
3299 /* Initialize allocated space. */
3300 for (unsigned i
= sh
->NumSubroutineUniformRemapTable
; i
< max_loc
+ 1; i
++)
3301 sh
->SubroutineUniformRemapTable
[i
] = NULL
;
3303 sh
->NumSubroutineUniformRemapTable
= max_loc
+ 1;
3306 for (unsigned i
= 0; i
< slots
; i
++) {
3307 unsigned loc
= var
->data
.location
+ i
;
3309 /* Check if location is already used. */
3310 if (sh
->SubroutineUniformRemapTable
[loc
] == INACTIVE_UNIFORM_EXPLICIT_LOCATION
) {
3312 /* ARB_explicit_uniform_location specification states:
3313 * "No two subroutine uniform variables can have the same location
3314 * in the same shader stage, otherwise a compiler or linker error
3315 * will be generated."
3318 "location qualifier for uniform %s overlaps "
3319 "previously used location\n",
3324 /* Initialize location as inactive before optimization
3325 * rounds and location assignment.
3327 sh
->SubroutineUniformRemapTable
[loc
] = INACTIVE_UNIFORM_EXPLICIT_LOCATION
;
3333 * Check and reserve all explicit uniform locations, called before
3334 * any optimizations happen to handle also inactive uniforms and
3335 * inactive array elements that may get trimmed away.
3338 check_explicit_uniform_locations(struct gl_context
*ctx
,
3339 struct gl_shader_program
*prog
)
3341 if (!ctx
->Extensions
.ARB_explicit_uniform_location
)
3344 /* This map is used to detect if overlapping explicit locations
3345 * occur with the same uniform (from different stage) or a different one.
3347 string_to_uint_map
*uniform_map
= new string_to_uint_map
;
3350 linker_error(prog
, "Out of memory during linking.\n");
3354 unsigned entries_total
= 0;
3355 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3356 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
3361 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3362 ir_variable
*var
= node
->as_variable();
3363 if (!var
|| var
->data
.mode
!= ir_var_uniform
)
3366 if (var
->data
.explicit_location
) {
3368 if (var
->type
->without_array()->is_subroutine())
3369 ret
= reserve_subroutine_explicit_locations(prog
, sh
, var
);
3371 int slots
= reserve_explicit_locations(prog
, uniform_map
,
3375 entries_total
+= slots
;
3386 struct empty_uniform_block
*current_block
= NULL
;
3388 for (unsigned i
= 0; i
< prog
->NumUniformRemapTable
; i
++) {
3389 /* We found empty space in UniformRemapTable. */
3390 if (prog
->UniformRemapTable
[i
] == NULL
) {
3391 /* We've found the beginning of a new continous block of empty slots */
3392 if (!current_block
|| current_block
->start
+ current_block
->slots
!= i
) {
3393 current_block
= rzalloc(prog
, struct empty_uniform_block
);
3394 current_block
->start
= i
;
3395 exec_list_push_tail(&prog
->EmptyUniformLocations
,
3396 ¤t_block
->link
);
3399 /* The current block continues, so we simply increment its slots */
3400 current_block
->slots
++;
3405 return entries_total
;
3409 should_add_buffer_variable(struct gl_shader_program
*shProg
,
3410 GLenum type
, const char *name
)
3412 bool found_interface
= false;
3413 unsigned block_name_len
= 0;
3414 const char *block_name_dot
= strchr(name
, '.');
3416 /* These rules only apply to buffer variables. So we return
3417 * true for the rest of types.
3419 if (type
!= GL_BUFFER_VARIABLE
)
3422 for (unsigned i
= 0; i
< shProg
->NumShaderStorageBlocks
; i
++) {
3423 const char *block_name
= shProg
->ShaderStorageBlocks
[i
].Name
;
3424 block_name_len
= strlen(block_name
);
3426 const char *block_square_bracket
= strchr(block_name
, '[');
3427 if (block_square_bracket
) {
3428 /* The block is part of an array of named interfaces,
3429 * for the name comparison we ignore the "[x]" part.
3431 block_name_len
-= strlen(block_square_bracket
);
3434 if (block_name_dot
) {
3435 /* Check if the variable name starts with the interface
3436 * name. The interface name (if present) should have the
3437 * length than the interface block name we are comparing to.
3439 unsigned len
= strlen(name
) - strlen(block_name_dot
);
3440 if (len
!= block_name_len
)
3444 if (strncmp(block_name
, name
, block_name_len
) == 0) {
3445 found_interface
= true;
3450 /* We remove the interface name from the buffer variable name,
3451 * including the dot that follows it.
3453 if (found_interface
)
3454 name
= name
+ block_name_len
+ 1;
3456 /* The ARB_program_interface_query spec says:
3458 * "For an active shader storage block member declared as an array, an
3459 * entry will be generated only for the first array element, regardless
3460 * of its type. For arrays of aggregate types, the enumeration rules
3461 * are applied recursively for the single enumerated array element."
3463 const char *struct_first_dot
= strchr(name
, '.');
3464 const char *first_square_bracket
= strchr(name
, '[');
3466 /* The buffer variable is on top level and it is not an array */
3467 if (!first_square_bracket
) {
3469 /* The shader storage block member is a struct, then generate the entry */
3470 } else if (struct_first_dot
&& struct_first_dot
< first_square_bracket
) {
3473 /* Shader storage block member is an array, only generate an entry for the
3474 * first array element.
3476 if (strncmp(first_square_bracket
, "[0]", 3) == 0)
3484 add_program_resource(struct gl_shader_program
*prog
,
3485 struct set
*resource_set
,
3486 GLenum type
, const void *data
, uint8_t stages
)
3490 /* If resource already exists, do not add it again. */
3491 if (_mesa_set_search(resource_set
, data
))
3494 prog
->ProgramResourceList
=
3496 prog
->ProgramResourceList
,
3497 gl_program_resource
,
3498 prog
->NumProgramResourceList
+ 1);
3500 if (!prog
->ProgramResourceList
) {
3501 linker_error(prog
, "Out of memory during linking.\n");
3505 struct gl_program_resource
*res
=
3506 &prog
->ProgramResourceList
[prog
->NumProgramResourceList
];
3510 res
->StageReferences
= stages
;
3512 prog
->NumProgramResourceList
++;
3514 _mesa_set_add(resource_set
, data
);
3519 /* Function checks if a variable var is a packed varying and
3520 * if given name is part of packed varying's list.
3522 * If a variable is a packed varying, it has a name like
3523 * 'packed:a,b,c' where a, b and c are separate variables.
3526 included_in_packed_varying(ir_variable
*var
, const char *name
)
3528 if (strncmp(var
->name
, "packed:", 7) != 0)
3531 char *list
= strdup(var
->name
+ 7);
3536 char *token
= strtok_r(list
, ",", &saveptr
);
3538 if (strcmp(token
, name
) == 0) {
3542 token
= strtok_r(NULL
, ",", &saveptr
);
3549 * Function builds a stage reference bitmask from variable name.
3552 build_stageref(struct gl_shader_program
*shProg
, const char *name
,
3557 /* Note, that we assume MAX 8 stages, if there will be more stages, type
3558 * used for reference mask in gl_program_resource will need to be changed.
3560 assert(MESA_SHADER_STAGES
< 8);
3562 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3563 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
3567 /* Shader symbol table may contain variables that have
3568 * been optimized away. Search IR for the variable instead.
3570 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
3571 ir_variable
*var
= node
->as_variable();
3573 unsigned baselen
= strlen(var
->name
);
3575 if (included_in_packed_varying(var
, name
)) {
3580 /* Type needs to match if specified, otherwise we might
3581 * pick a variable with same name but different interface.
3583 if (var
->data
.mode
!= mode
)
3586 if (strncmp(var
->name
, name
, baselen
) == 0) {
3587 /* Check for exact name matches but also check for arrays and
3590 if (name
[baselen
] == '\0' ||
3591 name
[baselen
] == '[' ||
3592 name
[baselen
] == '.') {
3604 * Create gl_shader_variable from ir_variable class.
3606 static gl_shader_variable
*
3607 create_shader_variable(struct gl_shader_program
*shProg
,
3608 const ir_variable
*in
,
3609 const char *name
, const glsl_type
*type
,
3610 const glsl_type
*interface_type
,
3611 bool use_implicit_location
, int location
,
3612 const glsl_type
*outermost_struct_type
)
3614 gl_shader_variable
*out
= ralloc(shProg
, struct gl_shader_variable
);
3618 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
3619 * expect to see gl_VertexID in the program resource list. Pretend.
3621 if (in
->data
.mode
== ir_var_system_value
&&
3622 in
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
3623 out
->name
= ralloc_strdup(shProg
, "gl_VertexID");
3624 } else if ((in
->data
.mode
== ir_var_shader_out
&&
3625 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
) ||
3626 (in
->data
.mode
== ir_var_system_value
&&
3627 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_OUTER
)) {
3628 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelOuter");
3629 type
= glsl_type::get_array_instance(glsl_type::float_type
, 4);
3630 } else if ((in
->data
.mode
== ir_var_shader_out
&&
3631 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
) ||
3632 (in
->data
.mode
== ir_var_system_value
&&
3633 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_INNER
)) {
3634 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelInner");
3635 type
= glsl_type::get_array_instance(glsl_type::float_type
, 2);
3637 out
->name
= ralloc_strdup(shProg
, name
);
3643 /* The ARB_program_interface_query spec says:
3645 * "Not all active variables are assigned valid locations; the
3646 * following variables will have an effective location of -1:
3648 * * uniforms declared as atomic counters;
3650 * * members of a uniform block;
3652 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
3654 * * inputs or outputs not declared with a "location" layout
3655 * qualifier, except for vertex shader inputs and fragment shader
3658 if (in
->type
->base_type
== GLSL_TYPE_ATOMIC_UINT
||
3659 is_gl_identifier(in
->name
) ||
3660 !(in
->data
.explicit_location
|| use_implicit_location
)) {
3663 out
->location
= location
;
3667 out
->outermost_struct_type
= outermost_struct_type
;
3668 out
->interface_type
= interface_type
;
3669 out
->component
= in
->data
.location_frac
;
3670 out
->index
= in
->data
.index
;
3671 out
->patch
= in
->data
.patch
;
3672 out
->mode
= in
->data
.mode
;
3673 out
->interpolation
= in
->data
.interpolation
;
3674 out
->explicit_location
= in
->data
.explicit_location
;
3675 out
->precision
= in
->data
.precision
;
3680 static const glsl_type
*
3681 resize_to_max_patch_vertices(const struct gl_context
*ctx
,
3682 const glsl_type
*type
)
3687 return glsl_type::get_array_instance(type
->fields
.array
,
3688 ctx
->Const
.MaxPatchVertices
);
3692 add_shader_variable(const struct gl_context
*ctx
,
3693 struct gl_shader_program
*shProg
,
3694 struct set
*resource_set
,
3695 unsigned stage_mask
,
3696 GLenum programInterface
, ir_variable
*var
,
3697 const char *name
, const glsl_type
*type
,
3698 bool use_implicit_location
, int location
,
3699 const glsl_type
*outermost_struct_type
= NULL
)
3701 const bool is_vertex_input
=
3702 programInterface
== GL_PROGRAM_INPUT
&&
3703 stage_mask
== MESA_SHADER_VERTEX
;
3705 switch (type
->base_type
) {
3706 case GLSL_TYPE_STRUCT
: {
3707 /* The ARB_program_interface_query spec says:
3709 * "For an active variable declared as a structure, a separate entry
3710 * will be generated for each active structure member. The name of
3711 * each entry is formed by concatenating the name of the structure,
3712 * the "." character, and the name of the structure member. If a
3713 * structure member to enumerate is itself a structure or array,
3714 * these enumeration rules are applied recursively."
3716 if (outermost_struct_type
== NULL
)
3717 outermost_struct_type
= type
;
3719 unsigned field_location
= location
;
3720 for (unsigned i
= 0; i
< type
->length
; i
++) {
3721 const struct glsl_struct_field
*field
= &type
->fields
.structure
[i
];
3722 char *field_name
= ralloc_asprintf(shProg
, "%s.%s", name
, field
->name
);
3723 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3724 stage_mask
, programInterface
,
3725 var
, field_name
, field
->type
,
3726 use_implicit_location
, field_location
,
3727 outermost_struct_type
))
3731 field
->type
->count_attribute_slots(is_vertex_input
);
3737 const glsl_type
*interface_type
= var
->get_interface_type();
3739 /* Unsized (non-patch) TCS output/TES input arrays are implicitly
3740 * sized to gl_MaxPatchVertices. Internally, we shrink them to a
3743 * This can cause trouble with SSO programs. Since the TCS declares
3744 * the number of output vertices, we can always shrink TCS output
3745 * arrays. However, the TES might not be linked with a TCS, in
3746 * which case it won't know the size of the patch. In other words,
3747 * the TCS and TES may disagree on the (smaller) array sizes. This
3748 * can result in the resource names differing across stages, causing
3749 * SSO validation failures and other cascading issues.
3751 * Expanding the array size to the full gl_MaxPatchVertices fixes
3752 * these issues. It's also what program interface queries expect,
3753 * as that is the official size of the array.
3755 if (var
->data
.tess_varying_implicit_sized_array
) {
3756 type
= resize_to_max_patch_vertices(ctx
, type
);
3757 interface_type
= resize_to_max_patch_vertices(ctx
, interface_type
);
3760 /* Issue #16 of the ARB_program_interface_query spec says:
3762 * "* If a variable is a member of an interface block without an
3763 * instance name, it is enumerated using just the variable name.
3765 * * If a variable is a member of an interface block with an instance
3766 * name, it is enumerated as "BlockName.Member", where "BlockName" is
3767 * the name of the interface block (not the instance name) and
3768 * "Member" is the name of the variable."
3770 const char *prefixed_name
= (var
->data
.from_named_ifc_block
&&
3771 !is_gl_identifier(var
->name
))
3772 ? ralloc_asprintf(shProg
, "%s.%s", interface_type
->name
, name
)
3775 /* The ARB_program_interface_query spec says:
3777 * "For an active variable declared as a single instance of a basic
3778 * type, a single entry will be generated, using the variable name
3779 * from the shader source."
3781 gl_shader_variable
*sha_v
=
3782 create_shader_variable(shProg
, var
, prefixed_name
, type
,
3784 use_implicit_location
, location
,
3785 outermost_struct_type
);
3789 return add_program_resource(shProg
, resource_set
,
3790 programInterface
, sha_v
, stage_mask
);
3796 add_interface_variables(const struct gl_context
*ctx
,
3797 struct gl_shader_program
*shProg
,
3798 struct set
*resource_set
,
3799 unsigned stage
, GLenum programInterface
)
3801 exec_list
*ir
= shProg
->_LinkedShaders
[stage
]->ir
;
3803 foreach_in_list(ir_instruction
, node
, ir
) {
3804 ir_variable
*var
= node
->as_variable();
3806 if (!var
|| var
->data
.how_declared
== ir_var_hidden
)
3811 switch (var
->data
.mode
) {
3812 case ir_var_system_value
:
3813 case ir_var_shader_in
:
3814 if (programInterface
!= GL_PROGRAM_INPUT
)
3816 loc_bias
= (stage
== MESA_SHADER_VERTEX
) ? int(VERT_ATTRIB_GENERIC0
)
3817 : int(VARYING_SLOT_VAR0
);
3819 case ir_var_shader_out
:
3820 if (programInterface
!= GL_PROGRAM_OUTPUT
)
3822 loc_bias
= (stage
== MESA_SHADER_FRAGMENT
) ? int(FRAG_RESULT_DATA0
)
3823 : int(VARYING_SLOT_VAR0
);
3829 if (var
->data
.patch
)
3830 loc_bias
= int(VARYING_SLOT_PATCH0
);
3832 /* Skip packed varyings, packed varyings are handled separately
3833 * by add_packed_varyings.
3835 if (strncmp(var
->name
, "packed:", 7) == 0)
3838 /* Skip fragdata arrays, these are handled separately
3839 * by add_fragdata_arrays.
3841 if (strncmp(var
->name
, "gl_out_FragData", 15) == 0)
3844 const bool vs_input_or_fs_output
=
3845 (stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
3846 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
);
3848 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3849 1 << stage
, programInterface
,
3850 var
, var
->name
, var
->type
, vs_input_or_fs_output
,
3851 var
->data
.location
- loc_bias
))
3858 add_packed_varyings(const struct gl_context
*ctx
,
3859 struct gl_shader_program
*shProg
,
3860 struct set
*resource_set
,
3861 int stage
, GLenum type
)
3863 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[stage
];
3866 if (!sh
|| !sh
->packed_varyings
)
3869 foreach_in_list(ir_instruction
, node
, sh
->packed_varyings
) {
3870 ir_variable
*var
= node
->as_variable();
3872 switch (var
->data
.mode
) {
3873 case ir_var_shader_in
:
3874 iface
= GL_PROGRAM_INPUT
;
3876 case ir_var_shader_out
:
3877 iface
= GL_PROGRAM_OUTPUT
;
3880 unreachable("unexpected type");
3883 if (type
== iface
) {
3884 const int stage_mask
=
3885 build_stageref(shProg
, var
->name
, var
->data
.mode
);
3886 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3888 iface
, var
, var
->name
, var
->type
, false,
3889 var
->data
.location
- VARYING_SLOT_VAR0
))
3898 add_fragdata_arrays(const struct gl_context
*ctx
,
3899 struct gl_shader_program
*shProg
,
3900 struct set
*resource_set
)
3902 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
3904 if (!sh
|| !sh
->fragdata_arrays
)
3907 foreach_in_list(ir_instruction
, node
, sh
->fragdata_arrays
) {
3908 ir_variable
*var
= node
->as_variable();
3910 assert(var
->data
.mode
== ir_var_shader_out
);
3912 if (!add_shader_variable(ctx
, shProg
, resource_set
,
3913 1 << MESA_SHADER_FRAGMENT
,
3914 GL_PROGRAM_OUTPUT
, var
, var
->name
, var
->type
,
3915 true, var
->data
.location
- FRAG_RESULT_DATA0
))
3923 get_top_level_name(const char *name
)
3925 const char *first_dot
= strchr(name
, '.');
3926 const char *first_square_bracket
= strchr(name
, '[');
3929 /* The ARB_program_interface_query spec says:
3931 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying
3932 * the number of active array elements of the top-level shader storage
3933 * block member containing to the active variable is written to
3934 * <params>. If the top-level block member is not declared as an
3935 * array, the value one is written to <params>. If the top-level block
3936 * member is an array with no declared size, the value zero is written
3940 /* The buffer variable is on top level.*/
3941 if (!first_square_bracket
&& !first_dot
)
3942 name_size
= strlen(name
);
3943 else if ((!first_square_bracket
||
3944 (first_dot
&& first_dot
< first_square_bracket
)))
3945 name_size
= first_dot
- name
;
3947 name_size
= first_square_bracket
- name
;
3949 return strndup(name
, name_size
);
3953 get_var_name(const char *name
)
3955 const char *first_dot
= strchr(name
, '.');
3958 return strdup(name
);
3960 return strndup(first_dot
+1, strlen(first_dot
) - 1);
3964 is_top_level_shader_storage_block_member(const char* name
,
3965 const char* interface_name
,
3966 const char* field_name
)
3968 bool result
= false;
3970 /* If the given variable is already a top-level shader storage
3971 * block member, then return array_size = 1.
3972 * We could have two possibilities: if we have an instanced
3973 * shader storage block or not instanced.
3975 * For the first, we check create a name as it was in top level and
3976 * compare it with the real name. If they are the same, then
3977 * the variable is already at top-level.
3979 * Full instanced name is: interface name + '.' + var name +
3982 int name_length
= strlen(interface_name
) + 1 + strlen(field_name
) + 1;
3983 char *full_instanced_name
= (char *) calloc(name_length
, sizeof(char));
3984 if (!full_instanced_name
) {
3985 fprintf(stderr
, "%s: Cannot allocate space for name\n", __func__
);
3989 snprintf(full_instanced_name
, name_length
, "%s.%s",
3990 interface_name
, field_name
);
3992 /* Check if its top-level shader storage block member of an
3993 * instanced interface block, or of a unnamed interface block.
3995 if (strcmp(name
, full_instanced_name
) == 0 ||
3996 strcmp(name
, field_name
) == 0)
3999 free(full_instanced_name
);
4004 get_array_size(struct gl_uniform_storage
*uni
, const glsl_struct_field
*field
,
4005 char *interface_name
, char *var_name
)
4007 /* The ARB_program_interface_query spec says:
4009 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying
4010 * the number of active array elements of the top-level shader storage
4011 * block member containing to the active variable is written to
4012 * <params>. If the top-level block member is not declared as an
4013 * array, the value one is written to <params>. If the top-level block
4014 * member is an array with no declared size, the value zero is written
4017 if (is_top_level_shader_storage_block_member(uni
->name
,
4021 else if (field
->type
->is_unsized_array())
4023 else if (field
->type
->is_array())
4024 return field
->type
->length
;
4030 get_array_stride(struct gl_uniform_storage
*uni
, const glsl_type
*interface
,
4031 const glsl_struct_field
*field
, char *interface_name
,
4034 /* The ARB_program_interface_query spec says:
4036 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
4037 * identifying the stride between array elements of the top-level
4038 * shader storage block member containing the active variable is
4039 * written to <params>. For top-level block members declared as
4040 * arrays, the value written is the difference, in basic machine units,
4041 * between the offsets of the active variable for consecutive elements
4042 * in the top-level array. For top-level block members not declared as
4043 * an array, zero is written to <params>."
4045 if (field
->type
->is_array()) {
4046 const enum glsl_matrix_layout matrix_layout
=
4047 glsl_matrix_layout(field
->matrix_layout
);
4048 bool row_major
= matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
4049 const glsl_type
*array_type
= field
->type
->fields
.array
;
4051 if (is_top_level_shader_storage_block_member(uni
->name
,
4056 if (interface
->interface_packing
!= GLSL_INTERFACE_PACKING_STD430
) {
4057 if (array_type
->is_record() || array_type
->is_array())
4058 return glsl_align(array_type
->std140_size(row_major
), 16);
4060 return MAX2(array_type
->std140_base_alignment(row_major
), 16);
4062 return array_type
->std430_array_stride(row_major
);
4069 calculate_array_size_and_stride(struct gl_shader_program
*shProg
,
4070 struct gl_uniform_storage
*uni
)
4072 int block_index
= uni
->block_index
;
4073 int array_size
= -1;
4074 int array_stride
= -1;
4075 char *var_name
= get_top_level_name(uni
->name
);
4076 char *interface_name
=
4077 get_top_level_name(uni
->is_shader_storage
?
4078 shProg
->ShaderStorageBlocks
[block_index
].Name
:
4079 shProg
->UniformBlocks
[block_index
].Name
);
4081 if (strcmp(var_name
, interface_name
) == 0) {
4082 /* Deal with instanced array of SSBOs */
4083 char *temp_name
= get_var_name(uni
->name
);
4085 linker_error(shProg
, "Out of memory during linking.\n");
4086 goto write_top_level_array_size_and_stride
;
4089 var_name
= get_top_level_name(temp_name
);
4092 linker_error(shProg
, "Out of memory during linking.\n");
4093 goto write_top_level_array_size_and_stride
;
4097 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4098 const gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
4102 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
4103 ir_variable
*var
= node
->as_variable();
4104 if (!var
|| !var
->get_interface_type() ||
4105 var
->data
.mode
!= ir_var_shader_storage
)
4108 const glsl_type
*interface
= var
->get_interface_type();
4110 if (strcmp(interface_name
, interface
->name
) != 0)
4113 for (unsigned i
= 0; i
< interface
->length
; i
++) {
4114 const glsl_struct_field
*field
= &interface
->fields
.structure
[i
];
4115 if (strcmp(field
->name
, var_name
) != 0)
4118 array_stride
= get_array_stride(uni
, interface
, field
,
4119 interface_name
, var_name
);
4120 array_size
= get_array_size(uni
, field
, interface_name
, var_name
);
4121 goto write_top_level_array_size_and_stride
;
4125 write_top_level_array_size_and_stride
:
4126 free(interface_name
);
4128 uni
->top_level_array_stride
= array_stride
;
4129 uni
->top_level_array_size
= array_size
;
4133 * Builds up a list of program resources that point to existing
4137 build_program_resource_list(struct gl_context
*ctx
,
4138 struct gl_shader_program
*shProg
)
4140 /* Rebuild resource list. */
4141 if (shProg
->ProgramResourceList
) {
4142 ralloc_free(shProg
->ProgramResourceList
);
4143 shProg
->ProgramResourceList
= NULL
;
4144 shProg
->NumProgramResourceList
= 0;
4147 int input_stage
= MESA_SHADER_STAGES
, output_stage
= 0;
4149 /* Determine first input and final output stage. These are used to
4150 * detect which variables should be enumerated in the resource list
4151 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
4153 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4154 if (!shProg
->_LinkedShaders
[i
])
4156 if (input_stage
== MESA_SHADER_STAGES
)
4161 /* Empty shader, no resources. */
4162 if (input_stage
== MESA_SHADER_STAGES
&& output_stage
== 0)
4165 struct set
*resource_set
= _mesa_set_create(NULL
,
4167 _mesa_key_pointer_equal
);
4169 /* Program interface needs to expose varyings in case of SSO. */
4170 if (shProg
->SeparateShader
) {
4171 if (!add_packed_varyings(ctx
, shProg
, resource_set
,
4172 input_stage
, GL_PROGRAM_INPUT
))
4175 if (!add_packed_varyings(ctx
, shProg
, resource_set
,
4176 output_stage
, GL_PROGRAM_OUTPUT
))
4180 if (!add_fragdata_arrays(ctx
, shProg
, resource_set
))
4183 /* Add inputs and outputs to the resource list. */
4184 if (!add_interface_variables(ctx
, shProg
, resource_set
,
4185 input_stage
, GL_PROGRAM_INPUT
))
4188 if (!add_interface_variables(ctx
, shProg
, resource_set
,
4189 output_stage
, GL_PROGRAM_OUTPUT
))
4192 /* Add transform feedback varyings. */
4193 if (shProg
->LinkedTransformFeedback
.NumVarying
> 0) {
4194 for (int i
= 0; i
< shProg
->LinkedTransformFeedback
.NumVarying
; i
++) {
4195 if (!add_program_resource(shProg
, resource_set
,
4196 GL_TRANSFORM_FEEDBACK_VARYING
,
4197 &shProg
->LinkedTransformFeedback
.Varyings
[i
],
4203 /* Add transform feedback buffers. */
4204 for (unsigned i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
4205 if ((shProg
->LinkedTransformFeedback
.ActiveBuffers
>> i
) & 1) {
4206 shProg
->LinkedTransformFeedback
.Buffers
[i
].Binding
= i
;
4207 if (!add_program_resource(shProg
, resource_set
,
4208 GL_TRANSFORM_FEEDBACK_BUFFER
,
4209 &shProg
->LinkedTransformFeedback
.Buffers
[i
],
4215 /* Add uniforms from uniform storage. */
4216 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; i
++) {
4217 /* Do not add uniforms internally used by Mesa. */
4218 if (shProg
->UniformStorage
[i
].hidden
)
4222 build_stageref(shProg
, shProg
->UniformStorage
[i
].name
,
4225 /* Add stagereferences for uniforms in a uniform block. */
4226 bool is_shader_storage
= shProg
->UniformStorage
[i
].is_shader_storage
;
4227 int block_index
= shProg
->UniformStorage
[i
].block_index
;
4228 if (block_index
!= -1) {
4229 stageref
|= is_shader_storage
?
4230 shProg
->ShaderStorageBlocks
[block_index
].stageref
:
4231 shProg
->UniformBlocks
[block_index
].stageref
;
4234 GLenum type
= is_shader_storage
? GL_BUFFER_VARIABLE
: GL_UNIFORM
;
4235 if (!should_add_buffer_variable(shProg
, type
,
4236 shProg
->UniformStorage
[i
].name
))
4239 if (is_shader_storage
) {
4240 calculate_array_size_and_stride(shProg
, &shProg
->UniformStorage
[i
]);
4243 if (!add_program_resource(shProg
, resource_set
, type
,
4244 &shProg
->UniformStorage
[i
], stageref
))
4248 /* Add program uniform blocks. */
4249 for (unsigned i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
4250 if (!add_program_resource(shProg
, resource_set
, GL_UNIFORM_BLOCK
,
4251 &shProg
->UniformBlocks
[i
], 0))
4255 /* Add program shader storage blocks. */
4256 for (unsigned i
= 0; i
< shProg
->NumShaderStorageBlocks
; i
++) {
4257 if (!add_program_resource(shProg
, resource_set
, GL_SHADER_STORAGE_BLOCK
,
4258 &shProg
->ShaderStorageBlocks
[i
], 0))
4262 /* Add atomic counter buffers. */
4263 for (unsigned i
= 0; i
< shProg
->NumAtomicBuffers
; i
++) {
4264 if (!add_program_resource(shProg
, resource_set
, GL_ATOMIC_COUNTER_BUFFER
,
4265 &shProg
->AtomicBuffers
[i
], 0))
4269 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; i
++) {
4271 if (!shProg
->UniformStorage
[i
].hidden
)
4274 for (int j
= MESA_SHADER_VERTEX
; j
< MESA_SHADER_STAGES
; j
++) {
4275 if (!shProg
->UniformStorage
[i
].opaque
[j
].active
||
4276 !shProg
->UniformStorage
[i
].type
->is_subroutine())
4279 type
= _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage
)j
);
4280 /* add shader subroutines */
4281 if (!add_program_resource(shProg
, resource_set
,
4282 type
, &shProg
->UniformStorage
[i
], 0))
4287 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4288 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
4294 type
= _mesa_shader_stage_to_subroutine((gl_shader_stage
)i
);
4295 for (unsigned j
= 0; j
< sh
->NumSubroutineFunctions
; j
++) {
4296 if (!add_program_resource(shProg
, resource_set
,
4297 type
, &sh
->SubroutineFunctions
[j
], 0))
4302 _mesa_set_destroy(resource_set
, NULL
);
4306 * This check is done to make sure we allow only constant expression
4307 * indexing and "constant-index-expression" (indexing with an expression
4308 * that includes loop induction variable).
4311 validate_sampler_array_indexing(struct gl_context
*ctx
,
4312 struct gl_shader_program
*prog
)
4314 dynamic_sampler_array_indexing_visitor v
;
4315 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4316 if (prog
->_LinkedShaders
[i
] == NULL
)
4319 bool no_dynamic_indexing
=
4320 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
;
4322 /* Search for array derefs in shader. */
4323 v
.run(prog
->_LinkedShaders
[i
]->ir
);
4324 if (v
.uses_dynamic_sampler_array_indexing()) {
4325 const char *msg
= "sampler arrays indexed with non-constant "
4326 "expressions is forbidden in GLSL %s %u";
4327 /* Backend has indicated that it has no dynamic indexing support. */
4328 if (no_dynamic_indexing
) {
4329 linker_error(prog
, msg
, prog
->IsES
? "ES" : "", prog
->Version
);
4332 linker_warning(prog
, msg
, prog
->IsES
? "ES" : "", prog
->Version
);
4340 link_assign_subroutine_types(struct gl_shader_program
*prog
)
4342 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4343 gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
4348 sh
->MaxSubroutineFunctionIndex
= 0;
4349 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
4350 ir_function
*fn
= node
->as_function();
4354 if (fn
->is_subroutine
)
4355 sh
->NumSubroutineUniformTypes
++;
4357 if (!fn
->num_subroutine_types
)
4360 /* these should have been calculated earlier. */
4361 assert(fn
->subroutine_index
!= -1);
4362 if (sh
->NumSubroutineFunctions
+ 1 > MAX_SUBROUTINES
) {
4363 linker_error(prog
, "Too many subroutine functions declared.\n");
4366 sh
->SubroutineFunctions
= reralloc(sh
, sh
->SubroutineFunctions
,
4367 struct gl_subroutine_function
,
4368 sh
->NumSubroutineFunctions
+ 1);
4369 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].name
= ralloc_strdup(sh
, fn
->name
);
4370 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].num_compat_types
= fn
->num_subroutine_types
;
4371 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].types
=
4372 ralloc_array(sh
, const struct glsl_type
*,
4373 fn
->num_subroutine_types
);
4375 /* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the
4378 * "Each subroutine with an index qualifier in the shader must be
4379 * given a unique index, otherwise a compile or link error will be
4382 for (unsigned j
= 0; j
< sh
->NumSubroutineFunctions
; j
++) {
4383 if (sh
->SubroutineFunctions
[j
].index
!= -1 &&
4384 sh
->SubroutineFunctions
[j
].index
== fn
->subroutine_index
) {
4385 linker_error(prog
, "each subroutine index qualifier in the "
4386 "shader must be unique\n");
4390 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].index
=
4391 fn
->subroutine_index
;
4393 if (fn
->subroutine_index
> (int)sh
->MaxSubroutineFunctionIndex
)
4394 sh
->MaxSubroutineFunctionIndex
= fn
->subroutine_index
;
4396 for (int j
= 0; j
< fn
->num_subroutine_types
; j
++)
4397 sh
->SubroutineFunctions
[sh
->NumSubroutineFunctions
].types
[j
] = fn
->subroutine_types
[j
];
4398 sh
->NumSubroutineFunctions
++;
4404 set_always_active_io(exec_list
*ir
, ir_variable_mode io_mode
)
4406 assert(io_mode
== ir_var_shader_in
|| io_mode
== ir_var_shader_out
);
4408 foreach_in_list(ir_instruction
, node
, ir
) {
4409 ir_variable
*const var
= node
->as_variable();
4411 if (var
== NULL
|| var
->data
.mode
!= io_mode
)
4414 /* Don't set always active on builtins that haven't been redeclared */
4415 if (var
->data
.how_declared
== ir_var_declared_implicitly
)
4418 var
->data
.always_active_io
= true;
4423 * When separate shader programs are enabled, only input/outputs between
4424 * the stages of a multi-stage separate program can be safely removed
4425 * from the shader interface. Other inputs/outputs must remain active.
4428 disable_varying_optimizations_for_sso(struct gl_shader_program
*prog
)
4430 unsigned first
, last
;
4431 assert(prog
->SeparateShader
);
4433 first
= MESA_SHADER_STAGES
;
4436 /* Determine first and last stage. Excluding the compute stage */
4437 for (unsigned i
= 0; i
< MESA_SHADER_COMPUTE
; i
++) {
4438 if (!prog
->_LinkedShaders
[i
])
4440 if (first
== MESA_SHADER_STAGES
)
4445 if (first
== MESA_SHADER_STAGES
)
4448 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4449 gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
4453 if (first
== last
) {
4454 /* For a single shader program only allow inputs to the vertex shader
4455 * and outputs from the fragment shader to be removed.
4457 if (stage
!= MESA_SHADER_VERTEX
)
4458 set_always_active_io(sh
->ir
, ir_var_shader_in
);
4459 if (stage
!= MESA_SHADER_FRAGMENT
)
4460 set_always_active_io(sh
->ir
, ir_var_shader_out
);
4462 /* For multi-stage separate shader programs only allow inputs and
4463 * outputs between the shader stages to be removed as well as inputs
4464 * to the vertex shader and outputs from the fragment shader.
4466 if (stage
== first
&& stage
!= MESA_SHADER_VERTEX
)
4467 set_always_active_io(sh
->ir
, ir_var_shader_in
);
4468 else if (stage
== last
&& stage
!= MESA_SHADER_FRAGMENT
)
4469 set_always_active_io(sh
->ir
, ir_var_shader_out
);
4475 link_varyings_and_uniforms(unsigned first
, unsigned last
,
4476 unsigned num_explicit_uniform_locs
,
4477 struct gl_context
*ctx
,
4478 struct gl_shader_program
*prog
, void *mem_ctx
)
4480 bool has_xfb_qualifiers
= false;
4481 unsigned num_tfeedback_decls
= 0;
4482 char **varying_names
= NULL
;
4483 tfeedback_decl
*tfeedback_decls
= NULL
;
4485 /* Mark all generic shader inputs and outputs as unpaired. */
4486 for (unsigned i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4487 if (prog
->_LinkedShaders
[i
] != NULL
) {
4488 link_invalidate_variable_locations(prog
->_LinkedShaders
[i
]->ir
);
4492 unsigned prev
= first
;
4493 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4494 if (prog
->_LinkedShaders
[i
] == NULL
)
4497 match_explicit_outputs_to_inputs(prog
->_LinkedShaders
[prev
],
4498 prog
->_LinkedShaders
[i
]);
4502 if (!assign_attribute_or_color_locations(mem_ctx
, prog
, &ctx
->Const
,
4503 MESA_SHADER_VERTEX
)) {
4507 if (!assign_attribute_or_color_locations(mem_ctx
, prog
, &ctx
->Const
,
4508 MESA_SHADER_FRAGMENT
)) {
4512 /* From the ARB_enhanced_layouts spec:
4514 * "If the shader used to record output variables for transform feedback
4515 * varyings uses the "xfb_buffer", "xfb_offset", or "xfb_stride" layout
4516 * qualifiers, the values specified by TransformFeedbackVaryings are
4517 * ignored, and the set of variables captured for transform feedback is
4518 * instead derived from the specified layout qualifiers."
4520 for (int i
= MESA_SHADER_FRAGMENT
- 1; i
>= 0; i
--) {
4521 /* Find last stage before fragment shader */
4522 if (prog
->_LinkedShaders
[i
]) {
4523 has_xfb_qualifiers
=
4524 process_xfb_layout_qualifiers(mem_ctx
, prog
->_LinkedShaders
[i
],
4525 &num_tfeedback_decls
,
4531 if (!has_xfb_qualifiers
) {
4532 num_tfeedback_decls
= prog
->TransformFeedback
.NumVarying
;
4533 varying_names
= prog
->TransformFeedback
.VaryingNames
;
4536 if (num_tfeedback_decls
!= 0) {
4537 /* From GL_EXT_transform_feedback:
4538 * A program will fail to link if:
4540 * * the <count> specified by TransformFeedbackVaryingsEXT is
4541 * non-zero, but the program object has no vertex or geometry
4544 if (first
>= MESA_SHADER_FRAGMENT
) {
4545 linker_error(prog
, "Transform feedback varyings specified, but "
4546 "no vertex, tessellation, or geometry shader is "
4551 tfeedback_decls
= rzalloc_array(mem_ctx
, tfeedback_decl
,
4552 num_tfeedback_decls
);
4553 if (!parse_tfeedback_decls(ctx
, prog
, mem_ctx
, num_tfeedback_decls
,
4554 varying_names
, tfeedback_decls
))
4558 /* If there is no fragment shader we need to set transform feedback.
4560 * For SSO we also need to assign output locations. We assign them here
4561 * because we need to do it for both single stage programs and multi stage
4564 if (last
< MESA_SHADER_FRAGMENT
&&
4565 (num_tfeedback_decls
!= 0 || prog
->SeparateShader
)) {
4566 const uint64_t reserved_out_slots
=
4567 reserved_varying_slot(prog
->_LinkedShaders
[last
], ir_var_shader_out
);
4568 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4569 prog
->_LinkedShaders
[last
], NULL
,
4570 num_tfeedback_decls
, tfeedback_decls
,
4571 reserved_out_slots
))
4575 if (last
<= MESA_SHADER_FRAGMENT
) {
4576 /* Remove unused varyings from the first/last stage unless SSO */
4577 remove_unused_shader_inputs_and_outputs(prog
->SeparateShader
,
4578 prog
->_LinkedShaders
[first
],
4580 remove_unused_shader_inputs_and_outputs(prog
->SeparateShader
,
4581 prog
->_LinkedShaders
[last
],
4584 /* If the program is made up of only a single stage */
4585 if (first
== last
) {
4586 gl_linked_shader
*const sh
= prog
->_LinkedShaders
[last
];
4588 do_dead_builtin_varyings(ctx
, NULL
, sh
, 0, NULL
);
4589 do_dead_builtin_varyings(ctx
, sh
, NULL
, num_tfeedback_decls
,
4592 if (prog
->SeparateShader
) {
4593 const uint64_t reserved_slots
=
4594 reserved_varying_slot(sh
, ir_var_shader_in
);
4596 /* Assign input locations for SSO, output locations are already
4599 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
4600 NULL
/* producer */,
4602 0 /* num_tfeedback_decls */,
4603 NULL
/* tfeedback_decls */,
4608 /* Linking the stages in the opposite order (from fragment to vertex)
4609 * ensures that inter-shader outputs written to in an earlier stage
4610 * are eliminated if they are (transitively) not used in a later
4614 for (int i
= next
- 1; i
>= 0; i
--) {
4615 if (prog
->_LinkedShaders
[i
] == NULL
&& i
!= 0)
4618 gl_linked_shader
*const sh_i
= prog
->_LinkedShaders
[i
];
4619 gl_linked_shader
*const sh_next
= prog
->_LinkedShaders
[next
];
4621 const uint64_t reserved_out_slots
=
4622 reserved_varying_slot(sh_i
, ir_var_shader_out
);
4623 const uint64_t reserved_in_slots
=
4624 reserved_varying_slot(sh_next
, ir_var_shader_in
);
4626 do_dead_builtin_varyings(ctx
, sh_i
, sh_next
,
4627 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
4630 if (!assign_varying_locations(ctx
, mem_ctx
, prog
, sh_i
, sh_next
,
4631 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
4633 reserved_out_slots
| reserved_in_slots
))
4636 /* This must be done after all dead varyings are eliminated. */
4638 unsigned slots_used
= _mesa_bitcount_64(reserved_out_slots
);
4639 if (!check_against_output_limit(ctx
, prog
, sh_i
, slots_used
)) {
4644 unsigned slots_used
= _mesa_bitcount_64(reserved_in_slots
);
4645 if (!check_against_input_limit(ctx
, prog
, sh_next
, slots_used
))
4653 if (!store_tfeedback_info(ctx
, prog
, num_tfeedback_decls
, tfeedback_decls
,
4654 has_xfb_qualifiers
))
4657 update_array_sizes(prog
);
4658 link_assign_uniform_locations(prog
, ctx
, num_explicit_uniform_locs
);
4659 link_assign_atomic_counter_resources(ctx
, prog
);
4661 link_calculate_subroutine_compat(prog
);
4662 check_resources(ctx
, prog
);
4663 check_subroutine_resources(prog
);
4664 check_image_resources(ctx
, prog
);
4665 link_check_atomic_counter_resources(ctx
, prog
);
4667 if (!prog
->LinkStatus
)
4670 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4671 if (prog
->_LinkedShaders
[i
] == NULL
)
4674 const struct gl_shader_compiler_options
*options
=
4675 &ctx
->Const
.ShaderCompilerOptions
[i
];
4677 if (options
->LowerBufferInterfaceBlocks
)
4678 lower_ubo_reference(prog
->_LinkedShaders
[i
],
4679 options
->ClampBlockIndicesToArrayBounds
);
4681 if (i
== MESA_SHADER_COMPUTE
)
4682 lower_shared_reference(prog
->_LinkedShaders
[i
],
4683 &prog
->Comp
.SharedSize
);
4685 lower_vector_derefs(prog
->_LinkedShaders
[i
]);
4686 do_vec_index_to_swizzle(prog
->_LinkedShaders
[i
]->ir
);
4693 link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4695 prog
->LinkStatus
= true; /* All error paths will set this to false */
4696 prog
->Validated
= false;
4697 prog
->_Used
= false;
4699 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec says:
4701 * "Linking can fail for a variety of reasons as specified in the
4702 * OpenGL Shading Language Specification, as well as any of the
4703 * following reasons:
4705 * - No shader objects are attached to program."
4707 * The Compatibility Profile specification does not list the error. In
4708 * Compatibility Profile missing shader stages are replaced by
4709 * fixed-function. This applies to the case where all stages are
4712 if (prog
->NumShaders
== 0) {
4713 if (ctx
->API
!= API_OPENGL_COMPAT
)
4714 linker_error(prog
, "no shaders attached to the program\n");
4718 unsigned int num_explicit_uniform_locs
= 0;
4720 void *mem_ctx
= ralloc_context(NULL
); // temporary linker context
4722 prog
->ARB_fragment_coord_conventions_enable
= false;
4724 /* Separate the shaders into groups based on their type.
4726 struct gl_shader
**shader_list
[MESA_SHADER_STAGES
];
4727 unsigned num_shaders
[MESA_SHADER_STAGES
];
4729 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4730 shader_list
[i
] = (struct gl_shader
**)
4731 calloc(prog
->NumShaders
, sizeof(struct gl_shader
*));
4735 unsigned min_version
= UINT_MAX
;
4736 unsigned max_version
= 0;
4737 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
4738 min_version
= MIN2(min_version
, prog
->Shaders
[i
]->Version
);
4739 max_version
= MAX2(max_version
, prog
->Shaders
[i
]->Version
);
4741 if (prog
->Shaders
[i
]->IsES
!= prog
->Shaders
[0]->IsES
) {
4742 linker_error(prog
, "all shaders must use same shading "
4743 "language version\n");
4747 if (prog
->Shaders
[i
]->info
.ARB_fragment_coord_conventions_enable
) {
4748 prog
->ARB_fragment_coord_conventions_enable
= true;
4751 gl_shader_stage shader_type
= prog
->Shaders
[i
]->Stage
;
4752 shader_list
[shader_type
][num_shaders
[shader_type
]] = prog
->Shaders
[i
];
4753 num_shaders
[shader_type
]++;
4756 /* In desktop GLSL, different shader versions may be linked together. In
4757 * GLSL ES, all shader versions must be the same.
4759 if (prog
->Shaders
[0]->IsES
&& min_version
!= max_version
) {
4760 linker_error(prog
, "all shaders must use same shading "
4761 "language version\n");
4765 prog
->Version
= max_version
;
4766 prog
->IsES
= prog
->Shaders
[0]->IsES
;
4768 /* Some shaders have to be linked with some other shaders present.
4770 if (!prog
->SeparateShader
) {
4771 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0 &&
4772 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4773 linker_error(prog
, "Geometry shader must be linked with "
4777 if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0 &&
4778 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4779 linker_error(prog
, "Tessellation evaluation shader must be linked "
4780 "with vertex shader\n");
4783 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4784 num_shaders
[MESA_SHADER_VERTEX
] == 0) {
4785 linker_error(prog
, "Tessellation control shader must be linked with "
4790 /* The spec is self-contradictory here. It allows linking without a tess
4791 * eval shader, but that can only be used with transform feedback and
4792 * rasterization disabled. However, transform feedback isn't allowed
4793 * with GL_PATCHES, so it can't be used.
4795 * More investigation showed that the idea of transform feedback after
4796 * a tess control shader was dropped, because some hw vendors couldn't
4797 * support tessellation without a tess eval shader, but the linker
4798 * section wasn't updated to reflect that.
4800 * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
4803 * Do what's reasonable and always require a tess eval shader if a tess
4804 * control shader is present.
4806 if (num_shaders
[MESA_SHADER_TESS_CTRL
] > 0 &&
4807 num_shaders
[MESA_SHADER_TESS_EVAL
] == 0) {
4808 linker_error(prog
, "Tessellation control shader must be linked with "
4809 "tessellation evaluation shader\n");
4814 /* Compute shaders have additional restrictions. */
4815 if (num_shaders
[MESA_SHADER_COMPUTE
] > 0 &&
4816 num_shaders
[MESA_SHADER_COMPUTE
] != prog
->NumShaders
) {
4817 linker_error(prog
, "Compute shaders may not be linked with any other "
4818 "type of shader\n");
4821 /* Link all shaders for a particular stage and validate the result.
4823 for (int stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
4824 if (num_shaders
[stage
] > 0) {
4825 gl_linked_shader
*const sh
=
4826 link_intrastage_shaders(mem_ctx
, ctx
, prog
, shader_list
[stage
],
4827 num_shaders
[stage
]);
4829 if (!prog
->LinkStatus
) {
4831 _mesa_delete_linked_shader(ctx
, sh
);
4836 case MESA_SHADER_VERTEX
:
4837 validate_vertex_shader_executable(prog
, sh
, ctx
);
4839 case MESA_SHADER_TESS_CTRL
:
4840 /* nothing to be done */
4842 case MESA_SHADER_TESS_EVAL
:
4843 validate_tess_eval_shader_executable(prog
, sh
, ctx
);
4845 case MESA_SHADER_GEOMETRY
:
4846 validate_geometry_shader_executable(prog
, sh
, ctx
);
4848 case MESA_SHADER_FRAGMENT
:
4849 validate_fragment_shader_executable(prog
, sh
);
4852 if (!prog
->LinkStatus
) {
4854 _mesa_delete_linked_shader(ctx
, sh
);
4858 prog
->_LinkedShaders
[stage
] = sh
;
4862 if (num_shaders
[MESA_SHADER_GEOMETRY
] > 0) {
4863 prog
->LastClipDistanceArraySize
= prog
->Geom
.ClipDistanceArraySize
;
4864 prog
->LastCullDistanceArraySize
= prog
->Geom
.CullDistanceArraySize
;
4865 } else if (num_shaders
[MESA_SHADER_TESS_EVAL
] > 0) {
4866 prog
->LastClipDistanceArraySize
= prog
->TessEval
.ClipDistanceArraySize
;
4867 prog
->LastCullDistanceArraySize
= prog
->TessEval
.CullDistanceArraySize
;
4868 } else if (num_shaders
[MESA_SHADER_VERTEX
] > 0) {
4869 prog
->LastClipDistanceArraySize
= prog
->Vert
.ClipDistanceArraySize
;
4870 prog
->LastCullDistanceArraySize
= prog
->Vert
.CullDistanceArraySize
;
4872 prog
->LastClipDistanceArraySize
= 0; /* Not used */
4873 prog
->LastCullDistanceArraySize
= 0; /* Not used */
4876 /* Here begins the inter-stage linking phase. Some initial validation is
4877 * performed, then locations are assigned for uniforms, attributes, and
4880 cross_validate_uniforms(prog
);
4881 if (!prog
->LinkStatus
)
4884 unsigned first
, last
, prev
;
4886 first
= MESA_SHADER_STAGES
;
4889 /* Determine first and last stage. */
4890 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4891 if (!prog
->_LinkedShaders
[i
])
4893 if (first
== MESA_SHADER_STAGES
)
4898 num_explicit_uniform_locs
= check_explicit_uniform_locations(ctx
, prog
);
4899 link_assign_subroutine_types(prog
);
4901 if (!prog
->LinkStatus
)
4904 resize_tes_inputs(ctx
, prog
);
4906 /* Validate the inputs of each stage with the output of the preceding
4910 for (unsigned i
= prev
+ 1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
4911 if (prog
->_LinkedShaders
[i
] == NULL
)
4914 validate_interstage_inout_blocks(prog
, prog
->_LinkedShaders
[prev
],
4915 prog
->_LinkedShaders
[i
]);
4916 if (!prog
->LinkStatus
)
4919 cross_validate_outputs_to_inputs(prog
,
4920 prog
->_LinkedShaders
[prev
],
4921 prog
->_LinkedShaders
[i
]);
4922 if (!prog
->LinkStatus
)
4928 /* Cross-validate uniform blocks between shader stages */
4929 validate_interstage_uniform_blocks(prog
, prog
->_LinkedShaders
);
4930 if (!prog
->LinkStatus
)
4933 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4934 if (prog
->_LinkedShaders
[i
] != NULL
)
4935 lower_named_interface_blocks(mem_ctx
, prog
->_LinkedShaders
[i
]);
4938 /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
4939 * it before optimization because we want most of the checks to get
4940 * dropped thanks to constant propagation.
4942 * This rule also applies to GLSL ES 3.00.
4944 if (max_version
>= (prog
->IsES
? 300 : 130)) {
4945 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4947 lower_discard_flow(sh
->ir
);
4951 if (prog
->SeparateShader
)
4952 disable_varying_optimizations_for_sso(prog
);
4955 if (!interstage_cross_validate_uniform_blocks(prog
, false))
4959 if (!interstage_cross_validate_uniform_blocks(prog
, true))
4962 /* Do common optimization before assigning storage for attributes,
4963 * uniforms, and varyings. Later optimization could possibly make
4964 * some of that unused.
4966 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4967 if (prog
->_LinkedShaders
[i
] == NULL
)
4970 detect_recursion_linked(prog
, prog
->_LinkedShaders
[i
]->ir
);
4971 if (!prog
->LinkStatus
)
4974 if (ctx
->Const
.ShaderCompilerOptions
[i
].LowerCombinedClipCullDistance
) {
4975 lower_clip_cull_distance(prog
, prog
->_LinkedShaders
[i
]);
4978 if (ctx
->Const
.LowerTessLevel
) {
4979 lower_tess_level(prog
->_LinkedShaders
[i
]);
4982 while (do_common_optimization(prog
->_LinkedShaders
[i
]->ir
, true, false,
4983 &ctx
->Const
.ShaderCompilerOptions
[i
],
4984 ctx
->Const
.NativeIntegers
))
4987 lower_const_arrays_to_uniforms(prog
->_LinkedShaders
[i
]->ir
, i
);
4988 propagate_invariance(prog
->_LinkedShaders
[i
]->ir
);
4991 /* Validation for special cases where we allow sampler array indexing
4992 * with loop induction variable. This check emits a warning or error
4993 * depending if backend can handle dynamic indexing.
4995 if ((!prog
->IsES
&& prog
->Version
< 130) ||
4996 (prog
->IsES
&& prog
->Version
< 300)) {
4997 if (!validate_sampler_array_indexing(ctx
, prog
))
5001 /* Check and validate stream emissions in geometry shaders */
5002 validate_geometry_shader_emissions(ctx
, prog
);
5004 store_fragdepth_layout(prog
);
5006 if(!link_varyings_and_uniforms(first
, last
, num_explicit_uniform_locs
, ctx
,
5010 /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both
5011 * be present in a linked program. GL_ARB_ES2_compatibility doesn't say
5012 * anything about shader linking when one of the shaders (vertex or
5013 * fragment shader) is absent. So, the extension shouldn't change the
5014 * behavior specified in GLSL specification.
5016 * From OpenGL ES 3.1 specification (7.3 Program Objects):
5017 * "Linking can fail for a variety of reasons as specified in the
5018 * OpenGL ES Shading Language Specification, as well as any of the
5019 * following reasons:
5023 * * program contains objects to form either a vertex shader or
5024 * fragment shader, and program is not separable, and does not
5025 * contain objects to form both a vertex shader and fragment
5028 * However, the only scenario in 3.1+ where we don't require them both is
5029 * when we have a compute shader. For example:
5031 * - No shaders is a link error.
5032 * - Geom or Tess without a Vertex shader is a link error which means we
5033 * always require a Vertex shader and hence a Fragment shader.
5034 * - Finally a Compute shader linked with any other stage is a link error.
5036 if (!prog
->SeparateShader
&& ctx
->API
== API_OPENGLES2
&&
5037 num_shaders
[MESA_SHADER_COMPUTE
] == 0) {
5038 if (prog
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
5039 linker_error(prog
, "program lacks a vertex shader\n");
5040 } else if (prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
) {
5041 linker_error(prog
, "program lacks a fragment shader\n");
5046 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
5047 free(shader_list
[i
]);
5048 if (prog
->_LinkedShaders
[i
] == NULL
)
5051 /* Do a final validation step to make sure that the IR wasn't
5052 * invalidated by any modifications performed after intrastage linking.
5054 validate_ir_tree(prog
->_LinkedShaders
[i
]->ir
);
5056 /* Retain any live IR, but trash the rest. */
5057 reparent_ir(prog
->_LinkedShaders
[i
]->ir
, prog
->_LinkedShaders
[i
]->ir
);
5059 /* The symbol table in the linked shaders may contain references to
5060 * variables that were removed (e.g., unused uniforms). Since it may
5061 * contain junk, there is no possible valid use. Delete it and set the
5064 delete prog
->_LinkedShaders
[i
]->symbols
;
5065 prog
->_LinkedShaders
[i
]->symbols
= NULL
;
5068 ralloc_free(mem_ctx
);