2 * Copyright © 2012 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.
25 * \file link_varyings.cpp
27 * Linker functions related specifically to linking varyings between shader
32 #include "main/errors.h"
33 #include "main/mtypes.h"
34 #include "glsl_symbol_table.h"
35 #include "glsl_parser_extras.h"
36 #include "ir_optimization.h"
38 #include "link_varyings.h"
39 #include "main/macros.h"
40 #include "util/hash_table.h"
41 #include "util/u_math.h"
46 * Get the varying type stripped of the outermost array if we're processing
47 * a stage whose varyings are arrays indexed by a vertex number (such as
48 * geometry shader inputs).
50 static const glsl_type
*
51 get_varying_type(const ir_variable
*var
, gl_shader_stage stage
)
53 const glsl_type
*type
= var
->type
;
55 if (!var
->data
.patch
&&
56 ((var
->data
.mode
== ir_var_shader_out
&&
57 stage
== MESA_SHADER_TESS_CTRL
) ||
58 (var
->data
.mode
== ir_var_shader_in
&&
59 (stage
== MESA_SHADER_TESS_CTRL
|| stage
== MESA_SHADER_TESS_EVAL
||
60 stage
== MESA_SHADER_GEOMETRY
)))) {
61 assert(type
->is_array());
62 type
= type
->fields
.array
;
69 create_xfb_varying_names(void *mem_ctx
, const glsl_type
*t
, char **name
,
70 size_t name_length
, unsigned *count
,
71 const char *ifc_member_name
,
72 const glsl_type
*ifc_member_t
, char ***varying_names
)
74 if (t
->is_interface()) {
75 size_t new_length
= name_length
;
77 assert(ifc_member_name
&& ifc_member_t
);
78 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", ifc_member_name
);
80 create_xfb_varying_names(mem_ctx
, ifc_member_t
, name
, new_length
, count
,
81 NULL
, NULL
, varying_names
);
82 } else if (t
->is_struct()) {
83 for (unsigned i
= 0; i
< t
->length
; i
++) {
84 const char *field
= t
->fields
.structure
[i
].name
;
85 size_t new_length
= name_length
;
87 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
89 create_xfb_varying_names(mem_ctx
, t
->fields
.structure
[i
].type
, name
,
90 new_length
, count
, NULL
, NULL
,
93 } else if (t
->without_array()->is_struct() ||
94 t
->without_array()->is_interface() ||
95 (t
->is_array() && t
->fields
.array
->is_array())) {
96 for (unsigned i
= 0; i
< t
->length
; i
++) {
97 size_t new_length
= name_length
;
99 /* Append the subscript to the current variable name */
100 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
102 create_xfb_varying_names(mem_ctx
, t
->fields
.array
, name
, new_length
,
103 count
, ifc_member_name
, ifc_member_t
,
107 (*varying_names
)[(*count
)++] = ralloc_strdup(mem_ctx
, *name
);
112 process_xfb_layout_qualifiers(void *mem_ctx
, const gl_linked_shader
*sh
,
113 struct gl_shader_program
*prog
,
114 unsigned *num_tfeedback_decls
,
115 char ***varying_names
)
117 bool has_xfb_qualifiers
= false;
119 /* We still need to enable transform feedback mode even if xfb_stride is
120 * only applied to a global out. Also we don't bother to propagate
121 * xfb_stride to interface block members so this will catch that case also.
123 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
124 if (prog
->TransformFeedback
.BufferStride
[j
]) {
125 has_xfb_qualifiers
= true;
130 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
131 ir_variable
*var
= node
->as_variable();
132 if (!var
|| var
->data
.mode
!= ir_var_shader_out
)
135 /* From the ARB_enhanced_layouts spec:
137 * "Any shader making any static use (after preprocessing) of any of
138 * these *xfb_* qualifiers will cause the shader to be in a
139 * transform feedback capturing mode and hence responsible for
140 * describing the transform feedback setup. This mode will capture
141 * any output selected by *xfb_offset*, directly or indirectly, to
142 * a transform feedback buffer."
144 if (var
->data
.explicit_xfb_buffer
|| var
->data
.explicit_xfb_stride
) {
145 has_xfb_qualifiers
= true;
148 if (var
->data
.explicit_xfb_offset
) {
149 *num_tfeedback_decls
+= var
->type
->varying_count();
150 has_xfb_qualifiers
= true;
154 if (*num_tfeedback_decls
== 0)
155 return has_xfb_qualifiers
;
158 *varying_names
= ralloc_array(mem_ctx
, char *, *num_tfeedback_decls
);
159 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
160 ir_variable
*var
= node
->as_variable();
161 if (!var
|| var
->data
.mode
!= ir_var_shader_out
)
164 if (var
->data
.explicit_xfb_offset
) {
166 const glsl_type
*type
, *member_type
;
168 if (var
->data
.from_named_ifc_block
) {
169 type
= var
->get_interface_type();
171 /* Find the member type before it was altered by lowering */
172 const glsl_type
*type_wa
= type
->without_array();
174 type_wa
->fields
.structure
[type_wa
->field_index(var
->name
)].type
;
175 name
= ralloc_strdup(NULL
, type_wa
->name
);
179 name
= ralloc_strdup(NULL
, var
->name
);
181 create_xfb_varying_names(mem_ctx
, type
, &name
, strlen(name
), &i
,
182 var
->name
, member_type
, varying_names
);
187 assert(i
== *num_tfeedback_decls
);
188 return has_xfb_qualifiers
;
192 * Validate the types and qualifiers of an output from one stage against the
193 * matching input to another stage.
196 cross_validate_types_and_qualifiers(struct gl_context
*ctx
,
197 struct gl_shader_program
*prog
,
198 const ir_variable
*input
,
199 const ir_variable
*output
,
200 gl_shader_stage consumer_stage
,
201 gl_shader_stage producer_stage
)
203 /* Check that the types match between stages.
205 const glsl_type
*type_to_match
= input
->type
;
207 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
208 const bool extra_array_level
= (producer_stage
== MESA_SHADER_VERTEX
&&
209 consumer_stage
!= MESA_SHADER_FRAGMENT
) ||
210 consumer_stage
== MESA_SHADER_GEOMETRY
;
211 if (extra_array_level
) {
212 assert(type_to_match
->is_array());
213 type_to_match
= type_to_match
->fields
.array
;
216 if (type_to_match
!= output
->type
) {
217 if (output
->type
->is_struct()) {
218 /* Structures across shader stages can have different name
219 * and considered to match in type if and only if structure
220 * members match in name, type, qualification, and declaration
221 * order. The precision doesn’t need to match.
223 if (!output
->type
->record_compare(type_to_match
,
224 false, /* match_name */
225 true, /* match_locations */
226 false /* match_precision */)) {
228 "%s shader output `%s' declared as struct `%s', "
229 "doesn't match in type with %s shader input "
230 "declared as struct `%s'\n",
231 _mesa_shader_stage_to_string(producer_stage
),
234 _mesa_shader_stage_to_string(consumer_stage
),
237 } else if (!output
->type
->is_array() || !is_gl_identifier(output
->name
)) {
238 /* There is a bit of a special case for gl_TexCoord. This
239 * built-in is unsized by default. Applications that variable
240 * access it must redeclare it with a size. There is some
241 * language in the GLSL spec that implies the fragment shader
242 * and vertex shader do not have to agree on this size. Other
243 * driver behave this way, and one or two applications seem to
246 * Neither declaration needs to be modified here because the array
247 * sizes are fixed later when update_array_sizes is called.
249 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
251 * "Unlike user-defined varying variables, the built-in
252 * varying variables don't have a strict one-to-one
253 * correspondence between the vertex language and the
254 * fragment language."
257 "%s shader output `%s' declared as type `%s', "
258 "but %s shader input declared as type `%s'\n",
259 _mesa_shader_stage_to_string(producer_stage
),
262 _mesa_shader_stage_to_string(consumer_stage
),
268 /* Check that all of the qualifiers match between stages.
271 /* According to the OpenGL and OpenGLES GLSL specs, the centroid qualifier
272 * should match until OpenGL 4.3 and OpenGLES 3.1. The OpenGLES 3.0
273 * conformance test suite does not verify that the qualifiers must match.
274 * The deqp test suite expects the opposite (OpenGLES 3.1) behavior for
275 * OpenGLES 3.0 drivers, so we relax the checking in all cases.
277 if (false /* always skip the centroid check */ &&
278 prog
->data
->Version
< (prog
->IsES
? 310 : 430) &&
279 input
->data
.centroid
!= output
->data
.centroid
) {
281 "%s shader output `%s' %s centroid qualifier, "
282 "but %s shader input %s centroid qualifier\n",
283 _mesa_shader_stage_to_string(producer_stage
),
285 (output
->data
.centroid
) ? "has" : "lacks",
286 _mesa_shader_stage_to_string(consumer_stage
),
287 (input
->data
.centroid
) ? "has" : "lacks");
291 if (input
->data
.sample
!= output
->data
.sample
) {
293 "%s shader output `%s' %s sample qualifier, "
294 "but %s shader input %s sample qualifier\n",
295 _mesa_shader_stage_to_string(producer_stage
),
297 (output
->data
.sample
) ? "has" : "lacks",
298 _mesa_shader_stage_to_string(consumer_stage
),
299 (input
->data
.sample
) ? "has" : "lacks");
303 if (input
->data
.patch
!= output
->data
.patch
) {
305 "%s shader output `%s' %s patch qualifier, "
306 "but %s shader input %s patch qualifier\n",
307 _mesa_shader_stage_to_string(producer_stage
),
309 (output
->data
.patch
) ? "has" : "lacks",
310 _mesa_shader_stage_to_string(consumer_stage
),
311 (input
->data
.patch
) ? "has" : "lacks");
315 /* The GLSL 4.30 and GLSL ES 3.00 specifications say:
317 * "As only outputs need be declared with invariant, an output from
318 * one shader stage will still match an input of a subsequent stage
319 * without the input being declared as invariant."
321 * while GLSL 4.20 says:
323 * "For variables leaving one shader and coming into another shader,
324 * the invariant keyword has to be used in both shaders, or a link
325 * error will result."
327 * and GLSL ES 1.00 section 4.6.4 "Invariance and Linking" says:
329 * "The invariance of varyings that are declared in both the vertex
330 * and fragment shaders must match."
332 if (input
->data
.explicit_invariant
!= output
->data
.explicit_invariant
&&
333 prog
->data
->Version
< (prog
->IsES
? 300 : 430)) {
335 "%s shader output `%s' %s invariant qualifier, "
336 "but %s shader input %s invariant qualifier\n",
337 _mesa_shader_stage_to_string(producer_stage
),
339 (output
->data
.explicit_invariant
) ? "has" : "lacks",
340 _mesa_shader_stage_to_string(consumer_stage
),
341 (input
->data
.explicit_invariant
) ? "has" : "lacks");
345 /* GLSL >= 4.40 removes text requiring interpolation qualifiers
346 * to match cross stage, they must only match within the same stage.
348 * From page 84 (page 90 of the PDF) of the GLSL 4.40 spec:
350 * "It is a link-time error if, within the same stage, the interpolation
351 * qualifiers of variables of the same name do not match.
353 * Section 4.3.9 (Interpolation) of the GLSL ES 3.00 spec says:
355 * "When no interpolation qualifier is present, smooth interpolation
358 * So we match variables where one is smooth and the other has no explicit
361 unsigned input_interpolation
= input
->data
.interpolation
;
362 unsigned output_interpolation
= output
->data
.interpolation
;
364 if (input_interpolation
== INTERP_MODE_NONE
)
365 input_interpolation
= INTERP_MODE_SMOOTH
;
366 if (output_interpolation
== INTERP_MODE_NONE
)
367 output_interpolation
= INTERP_MODE_SMOOTH
;
369 if (input_interpolation
!= output_interpolation
&&
370 prog
->data
->Version
< 440) {
371 if (!ctx
->Const
.AllowGLSLCrossStageInterpolationMismatch
) {
373 "%s shader output `%s' specifies %s "
374 "interpolation qualifier, "
375 "but %s shader input specifies %s "
376 "interpolation qualifier\n",
377 _mesa_shader_stage_to_string(producer_stage
),
379 interpolation_string(output
->data
.interpolation
),
380 _mesa_shader_stage_to_string(consumer_stage
),
381 interpolation_string(input
->data
.interpolation
));
385 "%s shader output `%s' specifies %s "
386 "interpolation qualifier, "
387 "but %s shader input specifies %s "
388 "interpolation qualifier\n",
389 _mesa_shader_stage_to_string(producer_stage
),
391 interpolation_string(output
->data
.interpolation
),
392 _mesa_shader_stage_to_string(consumer_stage
),
393 interpolation_string(input
->data
.interpolation
));
399 * Validate front and back color outputs against single color input
402 cross_validate_front_and_back_color(struct gl_context
*ctx
,
403 struct gl_shader_program
*prog
,
404 const ir_variable
*input
,
405 const ir_variable
*front_color
,
406 const ir_variable
*back_color
,
407 gl_shader_stage consumer_stage
,
408 gl_shader_stage producer_stage
)
410 if (front_color
!= NULL
&& front_color
->data
.assigned
)
411 cross_validate_types_and_qualifiers(ctx
, prog
, input
, front_color
,
412 consumer_stage
, producer_stage
);
414 if (back_color
!= NULL
&& back_color
->data
.assigned
)
415 cross_validate_types_and_qualifiers(ctx
, prog
, input
, back_color
,
416 consumer_stage
, producer_stage
);
420 compute_variable_location_slot(ir_variable
*var
, gl_shader_stage stage
)
422 unsigned location_start
= VARYING_SLOT_VAR0
;
425 case MESA_SHADER_VERTEX
:
426 if (var
->data
.mode
== ir_var_shader_in
)
427 location_start
= VERT_ATTRIB_GENERIC0
;
429 case MESA_SHADER_TESS_CTRL
:
430 case MESA_SHADER_TESS_EVAL
:
432 location_start
= VARYING_SLOT_PATCH0
;
434 case MESA_SHADER_FRAGMENT
:
435 if (var
->data
.mode
== ir_var_shader_out
)
436 location_start
= FRAG_RESULT_DATA0
;
442 return var
->data
.location
- location_start
;
445 struct explicit_location_info
{
447 bool base_type_is_integer
;
448 unsigned base_type_bit_size
;
449 unsigned interpolation
;
456 check_location_aliasing(struct explicit_location_info explicit_locations
[][4],
460 unsigned location_limit
,
461 const glsl_type
*type
,
462 unsigned interpolation
,
466 gl_shader_program
*prog
,
467 gl_shader_stage stage
)
470 unsigned base_type_bit_size
;
471 const glsl_type
*type_without_array
= type
->without_array();
472 const bool base_type_is_integer
=
473 glsl_base_type_is_integer(type_without_array
->base_type
);
474 const bool is_struct
= type_without_array
->is_struct();
476 /* structs don't have a defined underlying base type so just treat all
477 * component slots as used and set the bit size to 0. If there is
478 * location aliasing, we'll fail anyway later.
481 base_type_bit_size
= 0;
483 unsigned dmul
= type_without_array
->is_64bit() ? 2 : 1;
484 last_comp
= component
+ type_without_array
->vector_elements
* dmul
;
486 glsl_base_type_get_bit_size(type_without_array
->base_type
);
489 while (location
< location_limit
) {
492 struct explicit_location_info
*info
=
493 &explicit_locations
[location
][comp
];
496 if (info
->var
->type
->without_array()->is_struct() || is_struct
) {
497 /* Structs cannot share location since they are incompatible
498 * with any other underlying numerical type.
501 "%s shader has multiple %sputs sharing the "
502 "same location that don't have the same "
503 "underlying numerical type. Struct variable '%s', "
505 _mesa_shader_stage_to_string(stage
),
506 var
->data
.mode
== ir_var_shader_in
? "in" : "out",
507 is_struct
? var
->name
: info
->var
->name
,
510 } else if (comp
>= component
&& comp
< last_comp
) {
511 /* Component aliasing is not allowed */
513 "%s shader has multiple %sputs explicitly "
514 "assigned to location %d and component %d\n",
515 _mesa_shader_stage_to_string(stage
),
516 var
->data
.mode
== ir_var_shader_in
? "in" : "out",
520 /* From the OpenGL 4.60.5 spec, section 4.4.1 Input Layout
521 * Qualifiers, Page 67, (Location aliasing):
523 * " Further, when location aliasing, the aliases sharing the
524 * location must have the same underlying numerical type
525 * and bit width (floating-point or integer, 32-bit versus
526 * 64-bit, etc.) and the same auxiliary storage and
527 * interpolation qualification."
530 /* If the underlying numerical type isn't integer, implicitly
531 * it will be float or else we would have failed by now.
533 if (info
->base_type_is_integer
!= base_type_is_integer
) {
535 "%s shader has multiple %sputs sharing the "
536 "same location that don't have the same "
537 "underlying numerical type. Location %u "
539 _mesa_shader_stage_to_string(stage
),
540 var
->data
.mode
== ir_var_shader_in
?
541 "in" : "out", location
, comp
);
545 if (info
->base_type_bit_size
!= base_type_bit_size
) {
547 "%s shader has multiple %sputs sharing the "
548 "same location that don't have the same "
549 "underlying numerical bit size. Location %u "
551 _mesa_shader_stage_to_string(stage
),
552 var
->data
.mode
== ir_var_shader_in
?
553 "in" : "out", location
, comp
);
557 if (info
->interpolation
!= interpolation
) {
559 "%s shader has multiple %sputs sharing the "
560 "same location that don't have the same "
561 "interpolation qualification. Location %u "
563 _mesa_shader_stage_to_string(stage
),
564 var
->data
.mode
== ir_var_shader_in
?
565 "in" : "out", location
, comp
);
569 if (info
->centroid
!= centroid
||
570 info
->sample
!= sample
||
571 info
->patch
!= patch
) {
573 "%s shader has multiple %sputs sharing the "
574 "same location that don't have the same "
575 "auxiliary storage qualification. Location %u "
577 _mesa_shader_stage_to_string(stage
),
578 var
->data
.mode
== ir_var_shader_in
?
579 "in" : "out", location
, comp
);
583 } else if (comp
>= component
&& comp
< last_comp
) {
585 info
->base_type_is_integer
= base_type_is_integer
;
586 info
->base_type_bit_size
= base_type_bit_size
;
587 info
->interpolation
= interpolation
;
588 info
->centroid
= centroid
;
589 info
->sample
= sample
;
595 /* We need to do some special handling for doubles as dvec3 and
596 * dvec4 consume two consecutive locations. We don't need to
597 * worry about components beginning at anything other than 0 as
598 * the spec does not allow this for dvec3 and dvec4.
600 if (comp
== 4 && last_comp
> 4) {
601 last_comp
= last_comp
- 4;
602 /* Bump location index and reset the component index */
616 validate_explicit_variable_location(struct gl_context
*ctx
,
617 struct explicit_location_info explicit_locations
[][4],
619 gl_shader_program
*prog
,
620 gl_linked_shader
*sh
)
622 const glsl_type
*type
= get_varying_type(var
, sh
->Stage
);
623 unsigned num_elements
= type
->count_attribute_slots(false);
624 unsigned idx
= compute_variable_location_slot(var
, sh
->Stage
);
625 unsigned slot_limit
= idx
+ num_elements
;
627 /* Vertex shader inputs and fragment shader outputs are validated in
628 * assign_attribute_or_color_locations() so we should not attempt to
629 * validate them again here.
632 if (var
->data
.mode
== ir_var_shader_out
) {
633 assert(sh
->Stage
!= MESA_SHADER_FRAGMENT
);
635 ctx
->Const
.Program
[sh
->Stage
].MaxOutputComponents
/ 4;
637 assert(var
->data
.mode
== ir_var_shader_in
);
638 assert(sh
->Stage
!= MESA_SHADER_VERTEX
);
640 ctx
->Const
.Program
[sh
->Stage
].MaxInputComponents
/ 4;
643 if (slot_limit
> slot_max
) {
645 "Invalid location %u in %s shader\n",
646 idx
, _mesa_shader_stage_to_string(sh
->Stage
));
650 const glsl_type
*type_without_array
= type
->without_array();
651 if (type_without_array
->is_interface()) {
652 for (unsigned i
= 0; i
< type_without_array
->length
; i
++) {
653 glsl_struct_field
*field
= &type_without_array
->fields
.structure
[i
];
654 unsigned field_location
= field
->location
-
655 (field
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
);
656 if (!check_location_aliasing(explicit_locations
, var
,
658 0, field_location
+ 1,
660 field
->interpolation
,
668 } else if (!check_location_aliasing(explicit_locations
, var
,
669 idx
, var
->data
.location_frac
,
671 var
->data
.interpolation
,
683 * Validate explicit locations for the inputs to the first stage and the
684 * outputs of the last stage in a program, if those are not the VS and FS
688 validate_first_and_last_interface_explicit_locations(struct gl_context
*ctx
,
689 struct gl_shader_program
*prog
,
690 gl_shader_stage first_stage
,
691 gl_shader_stage last_stage
)
693 /* VS inputs and FS outputs are validated in
694 * assign_attribute_or_color_locations()
696 bool validate_first_stage
= first_stage
!= MESA_SHADER_VERTEX
;
697 bool validate_last_stage
= last_stage
!= MESA_SHADER_FRAGMENT
;
698 if (!validate_first_stage
&& !validate_last_stage
)
701 struct explicit_location_info explicit_locations
[MAX_VARYING
][4];
703 gl_shader_stage stages
[2] = { first_stage
, last_stage
};
704 bool validate_stage
[2] = { validate_first_stage
, validate_last_stage
};
705 ir_variable_mode var_direction
[2] = { ir_var_shader_in
, ir_var_shader_out
};
707 for (unsigned i
= 0; i
< 2; i
++) {
708 if (!validate_stage
[i
])
711 gl_shader_stage stage
= stages
[i
];
713 gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
716 memset(explicit_locations
, 0, sizeof(explicit_locations
));
718 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
719 ir_variable
*const var
= node
->as_variable();
722 !var
->data
.explicit_location
||
723 var
->data
.location
< VARYING_SLOT_VAR0
||
724 var
->data
.mode
!= var_direction
[i
])
727 if (!validate_explicit_variable_location(
728 ctx
, explicit_locations
, var
, prog
, sh
)) {
736 * Validate that outputs from one stage match inputs of another
739 cross_validate_outputs_to_inputs(struct gl_context
*ctx
,
740 struct gl_shader_program
*prog
,
741 gl_linked_shader
*producer
,
742 gl_linked_shader
*consumer
)
744 glsl_symbol_table parameters
;
745 struct explicit_location_info output_explicit_locations
[MAX_VARYING
][4] = {};
746 struct explicit_location_info input_explicit_locations
[MAX_VARYING
][4] = {};
748 /* Find all shader outputs in the "producer" stage.
750 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
751 ir_variable
*const var
= node
->as_variable();
753 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
756 if (!var
->data
.explicit_location
757 || var
->data
.location
< VARYING_SLOT_VAR0
)
758 parameters
.add_variable(var
);
760 /* User-defined varyings with explicit locations are handled
761 * differently because they do not need to have matching names.
763 if (!validate_explicit_variable_location(ctx
,
764 output_explicit_locations
,
765 var
, prog
, producer
)) {
772 /* Find all shader inputs in the "consumer" stage. Any variables that have
773 * matching outputs already in the symbol table must have the same type and
776 * Exception: if the consumer is the geometry shader, then the inputs
777 * should be arrays and the type of the array element should match the type
778 * of the corresponding producer output.
780 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
781 ir_variable
*const input
= node
->as_variable();
783 if (input
== NULL
|| input
->data
.mode
!= ir_var_shader_in
)
786 if (strcmp(input
->name
, "gl_Color") == 0 && input
->data
.used
) {
787 const ir_variable
*const front_color
=
788 parameters
.get_variable("gl_FrontColor");
790 const ir_variable
*const back_color
=
791 parameters
.get_variable("gl_BackColor");
793 cross_validate_front_and_back_color(ctx
, prog
, input
,
794 front_color
, back_color
,
795 consumer
->Stage
, producer
->Stage
);
796 } else if (strcmp(input
->name
, "gl_SecondaryColor") == 0 && input
->data
.used
) {
797 const ir_variable
*const front_color
=
798 parameters
.get_variable("gl_FrontSecondaryColor");
800 const ir_variable
*const back_color
=
801 parameters
.get_variable("gl_BackSecondaryColor");
803 cross_validate_front_and_back_color(ctx
, prog
, input
,
804 front_color
, back_color
,
805 consumer
->Stage
, producer
->Stage
);
807 /* The rules for connecting inputs and outputs change in the presence
808 * of explicit locations. In this case, we no longer care about the
809 * names of the variables. Instead, we care only about the
810 * explicitly assigned location.
812 ir_variable
*output
= NULL
;
813 if (input
->data
.explicit_location
814 && input
->data
.location
>= VARYING_SLOT_VAR0
) {
816 const glsl_type
*type
= get_varying_type(input
, consumer
->Stage
);
817 unsigned num_elements
= type
->count_attribute_slots(false);
819 compute_variable_location_slot(input
, consumer
->Stage
);
820 unsigned slot_limit
= idx
+ num_elements
;
822 if (!validate_explicit_variable_location(ctx
,
823 input_explicit_locations
,
824 input
, prog
, consumer
)) {
828 while (idx
< slot_limit
) {
829 if (idx
>= MAX_VARYING
) {
831 "Invalid location %u in %s shader\n", idx
,
832 _mesa_shader_stage_to_string(consumer
->Stage
));
836 output
= output_explicit_locations
[idx
][input
->data
.location_frac
].var
;
838 if (output
== NULL
) {
839 /* A linker failure should only happen when there is no
840 * output declaration and there is Static Use of the
843 if (input
->data
.used
) {
845 "%s shader input `%s' with explicit location "
846 "has no matching output\n",
847 _mesa_shader_stage_to_string(consumer
->Stage
),
851 } else if (input
->data
.location
!= output
->data
.location
) {
853 "%s shader input `%s' with explicit location "
854 "has no matching output\n",
855 _mesa_shader_stage_to_string(consumer
->Stage
),
862 output
= parameters
.get_variable(input
->name
);
865 if (output
!= NULL
) {
866 /* Interface blocks have their own validation elsewhere so don't
867 * try validating them here.
869 if (!(input
->get_interface_type() &&
870 output
->get_interface_type()))
871 cross_validate_types_and_qualifiers(ctx
, prog
, input
, output
,
875 /* Check for input vars with unmatched output vars in prev stage
876 * taking into account that interface blocks could have a matching
877 * output but with different name, so we ignore them.
879 assert(!input
->data
.assigned
);
880 if (input
->data
.used
&& !input
->get_interface_type() &&
881 !input
->data
.explicit_location
)
883 "%s shader input `%s' "
884 "has no matching output in the previous stage\n",
885 _mesa_shader_stage_to_string(consumer
->Stage
),
893 * Demote shader inputs and outputs that are not used in other stages, and
894 * remove them via dead code elimination.
897 remove_unused_shader_inputs_and_outputs(bool is_separate_shader_object
,
898 gl_linked_shader
*sh
,
899 enum ir_variable_mode mode
)
901 if (is_separate_shader_object
)
904 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
905 ir_variable
*const var
= node
->as_variable();
907 if (var
== NULL
|| var
->data
.mode
!= int(mode
))
910 /* A shader 'in' or 'out' variable is only really an input or output if
911 * its value is used by other shader stages. This will cause the
912 * variable to have a location assigned.
914 if (var
->data
.is_unmatched_generic_inout
&& !var
->data
.is_xfb_only
) {
915 assert(var
->data
.mode
!= ir_var_temporary
);
917 /* Assign zeros to demoted inputs to allow more optimizations. */
918 if (var
->data
.mode
== ir_var_shader_in
&& !var
->constant_value
)
919 var
->constant_value
= ir_constant::zero(var
, var
->type
);
921 var
->data
.mode
= ir_var_auto
;
925 /* Eliminate code that is now dead due to unused inputs/outputs being
928 while (do_dead_code(sh
->ir
, false))
934 * Initialize this object based on a string that was passed to
935 * glTransformFeedbackVaryings.
937 * If the input is mal-formed, this call still succeeds, but it sets
938 * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var()
939 * will fail to find any matching variable.
942 tfeedback_decl::init(struct gl_context
*ctx
, const void *mem_ctx
,
945 /* We don't have to be pedantic about what is a valid GLSL variable name,
946 * because any variable with an invalid name can't exist in the IR anyway.
950 this->orig_name
= input
;
951 this->lowered_builtin_array_variable
= none
;
952 this->skip_components
= 0;
953 this->next_buffer_separator
= false;
954 this->matched_candidate
= NULL
;
959 if (ctx
->Extensions
.ARB_transform_feedback3
) {
960 /* Parse gl_NextBuffer. */
961 if (strcmp(input
, "gl_NextBuffer") == 0) {
962 this->next_buffer_separator
= true;
966 /* Parse gl_SkipComponents. */
967 if (strcmp(input
, "gl_SkipComponents1") == 0)
968 this->skip_components
= 1;
969 else if (strcmp(input
, "gl_SkipComponents2") == 0)
970 this->skip_components
= 2;
971 else if (strcmp(input
, "gl_SkipComponents3") == 0)
972 this->skip_components
= 3;
973 else if (strcmp(input
, "gl_SkipComponents4") == 0)
974 this->skip_components
= 4;
976 if (this->skip_components
)
980 /* Parse a declaration. */
981 const char *base_name_end
;
982 long subscript
= parse_program_resource_name(input
, &base_name_end
);
983 this->var_name
= ralloc_strndup(mem_ctx
, input
, base_name_end
- input
);
984 if (this->var_name
== NULL
) {
985 _mesa_error_no_memory(__func__
);
989 if (subscript
>= 0) {
990 this->array_subscript
= subscript
;
991 this->is_subscripted
= true;
993 this->is_subscripted
= false;
996 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
997 * class must behave specially to account for the fact that gl_ClipDistance
998 * is converted from a float[8] to a vec4[2].
1000 if (ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].LowerCombinedClipCullDistance
&&
1001 strcmp(this->var_name
, "gl_ClipDistance") == 0) {
1002 this->lowered_builtin_array_variable
= clip_distance
;
1004 if (ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].LowerCombinedClipCullDistance
&&
1005 strcmp(this->var_name
, "gl_CullDistance") == 0) {
1006 this->lowered_builtin_array_variable
= cull_distance
;
1009 if (ctx
->Const
.LowerTessLevel
&&
1010 (strcmp(this->var_name
, "gl_TessLevelOuter") == 0))
1011 this->lowered_builtin_array_variable
= tess_level_outer
;
1012 if (ctx
->Const
.LowerTessLevel
&&
1013 (strcmp(this->var_name
, "gl_TessLevelInner") == 0))
1014 this->lowered_builtin_array_variable
= tess_level_inner
;
1019 * Determine whether two tfeedback_decl objects refer to the same variable and
1020 * array index (if applicable).
1023 tfeedback_decl::is_same(const tfeedback_decl
&x
, const tfeedback_decl
&y
)
1025 assert(x
.is_varying() && y
.is_varying());
1027 if (strcmp(x
.var_name
, y
.var_name
) != 0)
1029 if (x
.is_subscripted
!= y
.is_subscripted
)
1031 if (x
.is_subscripted
&& x
.array_subscript
!= y
.array_subscript
)
1038 * Assign a location and stream ID for this tfeedback_decl object based on the
1039 * transform feedback candidate found by find_candidate.
1041 * If an error occurs, the error is reported through linker_error() and false
1045 tfeedback_decl::assign_location(struct gl_context
*ctx
,
1046 struct gl_shader_program
*prog
)
1048 assert(this->is_varying());
1050 unsigned fine_location
1051 = this->matched_candidate
->toplevel_var
->data
.location
* 4
1052 + this->matched_candidate
->toplevel_var
->data
.location_frac
1053 + this->matched_candidate
->offset
;
1054 const unsigned dmul
=
1055 this->matched_candidate
->type
->without_array()->is_64bit() ? 2 : 1;
1057 if (this->matched_candidate
->type
->is_array()) {
1058 /* Array variable */
1059 const unsigned matrix_cols
=
1060 this->matched_candidate
->type
->fields
.array
->matrix_columns
;
1061 const unsigned vector_elements
=
1062 this->matched_candidate
->type
->fields
.array
->vector_elements
;
1063 unsigned actual_array_size
;
1064 switch (this->lowered_builtin_array_variable
) {
1066 actual_array_size
= prog
->last_vert_prog
?
1067 prog
->last_vert_prog
->info
.clip_distance_array_size
: 0;
1070 actual_array_size
= prog
->last_vert_prog
?
1071 prog
->last_vert_prog
->info
.cull_distance_array_size
: 0;
1073 case tess_level_outer
:
1074 actual_array_size
= 4;
1076 case tess_level_inner
:
1077 actual_array_size
= 2;
1081 actual_array_size
= this->matched_candidate
->type
->array_size();
1085 if (this->is_subscripted
) {
1086 /* Check array bounds. */
1087 if (this->array_subscript
>= actual_array_size
) {
1088 linker_error(prog
, "Transform feedback varying %s has index "
1089 "%i, but the array size is %u.",
1090 this->orig_name
, this->array_subscript
,
1094 unsigned array_elem_size
= this->lowered_builtin_array_variable
?
1095 1 : vector_elements
* matrix_cols
* dmul
;
1096 fine_location
+= array_elem_size
* this->array_subscript
;
1099 this->size
= actual_array_size
;
1101 this->vector_elements
= vector_elements
;
1102 this->matrix_columns
= matrix_cols
;
1103 if (this->lowered_builtin_array_variable
)
1104 this->type
= GL_FLOAT
;
1106 this->type
= this->matched_candidate
->type
->fields
.array
->gl_type
;
1108 /* Regular variable (scalar, vector, or matrix) */
1109 if (this->is_subscripted
) {
1110 linker_error(prog
, "Transform feedback varying %s requested, "
1111 "but %s is not an array.",
1112 this->orig_name
, this->var_name
);
1116 this->vector_elements
= this->matched_candidate
->type
->vector_elements
;
1117 this->matrix_columns
= this->matched_candidate
->type
->matrix_columns
;
1118 this->type
= this->matched_candidate
->type
->gl_type
;
1120 this->location
= fine_location
/ 4;
1121 this->location_frac
= fine_location
% 4;
1123 /* From GL_EXT_transform_feedback:
1124 * A program will fail to link if:
1126 * * the total number of components to capture in any varying
1127 * variable in <varyings> is greater than the constant
1128 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1129 * buffer mode is SEPARATE_ATTRIBS_EXT;
1131 if (prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
&&
1132 this->num_components() >
1133 ctx
->Const
.MaxTransformFeedbackSeparateComponents
) {
1134 linker_error(prog
, "Transform feedback varying %s exceeds "
1135 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1140 /* Only transform feedback varyings can be assigned to non-zero streams,
1141 * so assign the stream id here.
1143 this->stream_id
= this->matched_candidate
->toplevel_var
->data
.stream
;
1145 unsigned array_offset
= this->array_subscript
* 4 * dmul
;
1146 unsigned struct_offset
= this->matched_candidate
->offset
* 4 * dmul
;
1147 this->buffer
= this->matched_candidate
->toplevel_var
->data
.xfb_buffer
;
1148 this->offset
= this->matched_candidate
->toplevel_var
->data
.offset
+
1149 array_offset
+ struct_offset
;
1156 tfeedback_decl::get_num_outputs() const
1158 if (!this->is_varying()) {
1161 return (this->num_components() + this->location_frac
+ 3)/4;
1166 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1168 * If an error occurs, the error is reported through linker_error() and false
1172 tfeedback_decl::store(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1173 struct gl_transform_feedback_info
*info
,
1174 unsigned buffer
, unsigned buffer_index
,
1175 const unsigned max_outputs
,
1176 BITSET_WORD
*used_components
[MAX_FEEDBACK_BUFFERS
],
1177 bool *explicit_stride
, bool has_xfb_qualifiers
,
1178 const void* mem_ctx
) const
1180 unsigned xfb_offset
= 0;
1181 unsigned size
= this->size
;
1182 /* Handle gl_SkipComponents. */
1183 if (this->skip_components
) {
1184 info
->Buffers
[buffer
].Stride
+= this->skip_components
;
1185 size
= this->skip_components
;
1189 if (this->next_buffer_separator
) {
1194 if (has_xfb_qualifiers
) {
1195 xfb_offset
= this->offset
/ 4;
1197 xfb_offset
= info
->Buffers
[buffer
].Stride
;
1199 info
->Varyings
[info
->NumVarying
].Offset
= xfb_offset
* 4;
1202 unsigned location
= this->location
;
1203 unsigned location_frac
= this->location_frac
;
1204 unsigned num_components
= this->num_components();
1206 /* From GL_EXT_transform_feedback:
1208 * " A program will fail to link if:
1210 * * the total number of components to capture is greater than the
1211 * constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1212 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT."
1214 * From GL_ARB_enhanced_layouts:
1216 * " The resulting stride (implicit or explicit) must be less than or
1217 * equal to the implementation-dependent constant
1218 * gl_MaxTransformFeedbackInterleavedComponents."
1220 if ((prog
->TransformFeedback
.BufferMode
== GL_INTERLEAVED_ATTRIBS
||
1221 has_xfb_qualifiers
) &&
1222 xfb_offset
+ num_components
>
1223 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
1225 "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1226 "limit has been exceeded.");
1230 /* From the OpenGL 4.60.5 spec, section 4.4.2. Output Layout Qualifiers,
1231 * Page 76, (Transform Feedback Layout Qualifiers):
1233 * " No aliasing in output buffers is allowed: It is a compile-time or
1234 * link-time error to specify variables with overlapping transform
1235 * feedback offsets."
1237 const unsigned max_components
=
1238 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
;
1239 const unsigned first_component
= xfb_offset
;
1240 const unsigned last_component
= xfb_offset
+ num_components
- 1;
1241 const unsigned start_word
= BITSET_BITWORD(first_component
);
1242 const unsigned end_word
= BITSET_BITWORD(last_component
);
1244 assert(last_component
< max_components
);
1246 if (!used_components
[buffer
]) {
1247 used_components
[buffer
] =
1248 rzalloc_array(mem_ctx
, BITSET_WORD
, BITSET_WORDS(max_components
));
1250 used
= used_components
[buffer
];
1252 for (unsigned word
= start_word
; word
<= end_word
; word
++) {
1253 unsigned start_range
= 0;
1254 unsigned end_range
= BITSET_WORDBITS
- 1;
1256 if (word
== start_word
)
1257 start_range
= first_component
% BITSET_WORDBITS
;
1259 if (word
== end_word
)
1260 end_range
= last_component
% BITSET_WORDBITS
;
1262 if (used
[word
] & BITSET_RANGE(start_range
, end_range
)) {
1264 "variable '%s', xfb_offset (%d) is causing aliasing.",
1265 this->orig_name
, xfb_offset
* 4);
1268 used
[word
] |= BITSET_RANGE(start_range
, end_range
);
1271 while (num_components
> 0) {
1272 unsigned output_size
= MIN2(num_components
, 4 - location_frac
);
1273 assert((info
->NumOutputs
== 0 && max_outputs
== 0) ||
1274 info
->NumOutputs
< max_outputs
);
1276 /* From the ARB_enhanced_layouts spec:
1278 * "If such a block member or variable is not written during a shader
1279 * invocation, the buffer contents at the assigned offset will be
1280 * undefined. Even if there are no static writes to a variable or
1281 * member that is assigned a transform feedback offset, the space is
1282 * still allocated in the buffer and still affects the stride."
1284 if (this->is_varying_written()) {
1285 info
->Outputs
[info
->NumOutputs
].ComponentOffset
= location_frac
;
1286 info
->Outputs
[info
->NumOutputs
].OutputRegister
= location
;
1287 info
->Outputs
[info
->NumOutputs
].NumComponents
= output_size
;
1288 info
->Outputs
[info
->NumOutputs
].StreamId
= stream_id
;
1289 info
->Outputs
[info
->NumOutputs
].OutputBuffer
= buffer
;
1290 info
->Outputs
[info
->NumOutputs
].DstOffset
= xfb_offset
;
1293 info
->Buffers
[buffer
].Stream
= this->stream_id
;
1294 xfb_offset
+= output_size
;
1296 num_components
-= output_size
;
1302 if (explicit_stride
&& explicit_stride
[buffer
]) {
1303 if (this->is_64bit() && info
->Buffers
[buffer
].Stride
% 2) {
1304 linker_error(prog
, "invalid qualifier xfb_stride=%d must be a "
1305 "multiple of 8 as its applied to a type that is or "
1306 "contains a double.",
1307 info
->Buffers
[buffer
].Stride
* 4);
1311 if (xfb_offset
> info
->Buffers
[buffer
].Stride
) {
1312 linker_error(prog
, "xfb_offset (%d) overflows xfb_stride (%d) for "
1313 "buffer (%d)", xfb_offset
* 4,
1314 info
->Buffers
[buffer
].Stride
* 4, buffer
);
1318 info
->Buffers
[buffer
].Stride
= xfb_offset
;
1322 info
->Varyings
[info
->NumVarying
].Name
= ralloc_strdup(prog
,
1324 info
->Varyings
[info
->NumVarying
].Type
= this->type
;
1325 info
->Varyings
[info
->NumVarying
].Size
= size
;
1326 info
->Varyings
[info
->NumVarying
].BufferIndex
= buffer_index
;
1328 info
->Buffers
[buffer
].NumVaryings
++;
1334 const tfeedback_candidate
*
1335 tfeedback_decl::find_candidate(gl_shader_program
*prog
,
1336 hash_table
*tfeedback_candidates
)
1338 const char *name
= this->var_name
;
1339 switch (this->lowered_builtin_array_variable
) {
1341 name
= this->var_name
;
1344 name
= "gl_ClipDistanceMESA";
1347 name
= "gl_CullDistanceMESA";
1349 case tess_level_outer
:
1350 name
= "gl_TessLevelOuterMESA";
1352 case tess_level_inner
:
1353 name
= "gl_TessLevelInnerMESA";
1356 hash_entry
*entry
= _mesa_hash_table_search(tfeedback_candidates
, name
);
1358 this->matched_candidate
= entry
?
1359 (const tfeedback_candidate
*) entry
->data
: NULL
;
1361 if (!this->matched_candidate
) {
1362 /* From GL_EXT_transform_feedback:
1363 * A program will fail to link if:
1365 * * any variable name specified in the <varyings> array is not
1366 * declared as an output in the geometry shader (if present) or
1367 * the vertex shader (if no geometry shader is present);
1369 linker_error(prog
, "Transform feedback varying %s undeclared.",
1373 return this->matched_candidate
;
1378 * Parse all the transform feedback declarations that were passed to
1379 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1381 * If an error occurs, the error is reported through linker_error() and false
1385 parse_tfeedback_decls(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1386 const void *mem_ctx
, unsigned num_names
,
1387 char **varying_names
, tfeedback_decl
*decls
)
1389 for (unsigned i
= 0; i
< num_names
; ++i
) {
1390 decls
[i
].init(ctx
, mem_ctx
, varying_names
[i
]);
1392 if (!decls
[i
].is_varying())
1395 /* From GL_EXT_transform_feedback:
1396 * A program will fail to link if:
1398 * * any two entries in the <varyings> array specify the same varying
1401 * We interpret this to mean "any two entries in the <varyings> array
1402 * specify the same varying variable and array index", since transform
1403 * feedback of arrays would be useless otherwise.
1405 for (unsigned j
= 0; j
< i
; ++j
) {
1406 if (decls
[j
].is_varying()) {
1407 if (tfeedback_decl::is_same(decls
[i
], decls
[j
])) {
1408 linker_error(prog
, "Transform feedback varying %s specified "
1409 "more than once.", varying_names
[i
]);
1420 cmp_xfb_offset(const void * x_generic
, const void * y_generic
)
1422 tfeedback_decl
*x
= (tfeedback_decl
*) x_generic
;
1423 tfeedback_decl
*y
= (tfeedback_decl
*) y_generic
;
1425 if (x
->get_buffer() != y
->get_buffer())
1426 return x
->get_buffer() - y
->get_buffer();
1427 return x
->get_offset() - y
->get_offset();
1431 * Store transform feedback location assignments into
1432 * prog->sh.LinkedTransformFeedback based on the data stored in
1435 * If an error occurs, the error is reported through linker_error() and false
1439 store_tfeedback_info(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1440 unsigned num_tfeedback_decls
,
1441 tfeedback_decl
*tfeedback_decls
, bool has_xfb_qualifiers
,
1442 const void *mem_ctx
)
1444 if (!prog
->last_vert_prog
)
1447 /* Make sure MaxTransformFeedbackBuffers is less than 32 so the bitmask for
1448 * tracking the number of buffers doesn't overflow.
1450 assert(ctx
->Const
.MaxTransformFeedbackBuffers
< 32);
1452 bool separate_attribs_mode
=
1453 prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
;
1455 struct gl_program
*xfb_prog
= prog
->last_vert_prog
;
1456 xfb_prog
->sh
.LinkedTransformFeedback
=
1457 rzalloc(xfb_prog
, struct gl_transform_feedback_info
);
1459 /* The xfb_offset qualifier does not have to be used in increasing order
1460 * however some drivers expect to receive the list of transform feedback
1461 * declarations in order so sort it now for convenience.
1463 if (has_xfb_qualifiers
) {
1464 qsort(tfeedback_decls
, num_tfeedback_decls
, sizeof(*tfeedback_decls
),
1468 xfb_prog
->sh
.LinkedTransformFeedback
->Varyings
=
1469 rzalloc_array(xfb_prog
, struct gl_transform_feedback_varying_info
,
1470 num_tfeedback_decls
);
1472 unsigned num_outputs
= 0;
1473 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1474 if (tfeedback_decls
[i
].is_varying_written())
1475 num_outputs
+= tfeedback_decls
[i
].get_num_outputs();
1478 xfb_prog
->sh
.LinkedTransformFeedback
->Outputs
=
1479 rzalloc_array(xfb_prog
, struct gl_transform_feedback_output
,
1482 unsigned num_buffers
= 0;
1483 unsigned buffers
= 0;
1484 BITSET_WORD
*used_components
[MAX_FEEDBACK_BUFFERS
] = {};
1486 if (!has_xfb_qualifiers
&& separate_attribs_mode
) {
1487 /* GL_SEPARATE_ATTRIBS */
1488 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1489 if (!tfeedback_decls
[i
].store(ctx
, prog
,
1490 xfb_prog
->sh
.LinkedTransformFeedback
,
1491 num_buffers
, num_buffers
, num_outputs
,
1492 used_components
, NULL
,
1493 has_xfb_qualifiers
, mem_ctx
))
1496 buffers
|= 1 << num_buffers
;
1501 /* GL_INVERLEAVED_ATTRIBS */
1502 int buffer_stream_id
= -1;
1504 num_tfeedback_decls
? tfeedback_decls
[0].get_buffer() : 0;
1505 bool explicit_stride
[MAX_FEEDBACK_BUFFERS
] = { false };
1507 /* Apply any xfb_stride global qualifiers */
1508 if (has_xfb_qualifiers
) {
1509 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1510 if (prog
->TransformFeedback
.BufferStride
[j
]) {
1511 explicit_stride
[j
] = true;
1512 xfb_prog
->sh
.LinkedTransformFeedback
->Buffers
[j
].Stride
=
1513 prog
->TransformFeedback
.BufferStride
[j
] / 4;
1518 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1519 if (has_xfb_qualifiers
&&
1520 buffer
!= tfeedback_decls
[i
].get_buffer()) {
1521 /* we have moved to the next buffer so reset stream id */
1522 buffer_stream_id
= -1;
1526 if (tfeedback_decls
[i
].is_next_buffer_separator()) {
1527 if (!tfeedback_decls
[i
].store(ctx
, prog
,
1528 xfb_prog
->sh
.LinkedTransformFeedback
,
1529 buffer
, num_buffers
, num_outputs
,
1530 used_components
, explicit_stride
,
1531 has_xfb_qualifiers
, mem_ctx
))
1534 buffer_stream_id
= -1;
1538 if (has_xfb_qualifiers
) {
1539 buffer
= tfeedback_decls
[i
].get_buffer();
1541 buffer
= num_buffers
;
1544 if (tfeedback_decls
[i
].is_varying()) {
1545 if (buffer_stream_id
== -1) {
1546 /* First varying writing to this buffer: remember its stream */
1547 buffer_stream_id
= (int) tfeedback_decls
[i
].get_stream_id();
1549 /* Only mark a buffer as active when there is a varying
1550 * attached to it. This behaviour is based on a revised version
1551 * of section 13.2.2 of the GL 4.6 spec.
1553 buffers
|= 1 << buffer
;
1554 } else if (buffer_stream_id
!=
1555 (int) tfeedback_decls
[i
].get_stream_id()) {
1556 /* Varying writes to the same buffer from a different stream */
1558 "Transform feedback can't capture varyings belonging "
1559 "to different vertex streams in a single buffer. "
1560 "Varying %s writes to buffer from stream %u, other "
1561 "varyings in the same buffer write from stream %u.",
1562 tfeedback_decls
[i
].name(),
1563 tfeedback_decls
[i
].get_stream_id(),
1569 if (!tfeedback_decls
[i
].store(ctx
, prog
,
1570 xfb_prog
->sh
.LinkedTransformFeedback
,
1571 buffer
, num_buffers
, num_outputs
,
1572 used_components
, explicit_stride
,
1573 has_xfb_qualifiers
, mem_ctx
))
1578 assert(xfb_prog
->sh
.LinkedTransformFeedback
->NumOutputs
== num_outputs
);
1580 xfb_prog
->sh
.LinkedTransformFeedback
->ActiveBuffers
= buffers
;
1587 * Data structure recording the relationship between outputs of one shader
1588 * stage (the "producer") and inputs of another (the "consumer").
1590 class varying_matches
1593 varying_matches(bool disable_varying_packing
,
1594 bool disable_xfb_packing
,
1596 bool enhanced_layouts_enabled
,
1597 gl_shader_stage producer_stage
,
1598 gl_shader_stage consumer_stage
);
1600 void record(ir_variable
*producer_var
, ir_variable
*consumer_var
);
1601 unsigned assign_locations(struct gl_shader_program
*prog
,
1602 uint8_t components
[],
1603 uint64_t reserved_slots
);
1604 void store_locations() const;
1607 bool is_varying_packing_safe(const glsl_type
*type
,
1608 const ir_variable
*var
) const;
1611 * If true, this driver disables varying packing, so all varyings need to
1612 * be aligned on slot boundaries, and take up a number of slots equal to
1613 * their number of matrix columns times their array size.
1615 * Packing may also be disabled because our current packing method is not
1616 * safe in SSO or versions of OpenGL where interpolation qualifiers are not
1617 * guaranteed to match across stages.
1619 const bool disable_varying_packing
;
1622 * If true, this driver disables packing for varyings used by transform
1625 const bool disable_xfb_packing
;
1628 * If true, this driver has transform feedback enabled. The transform
1629 * feedback code usually requires at least some packing be done even
1630 * when varying packing is disabled, fortunately where transform feedback
1631 * requires packing it's safe to override the disabled setting. See
1632 * is_varying_packing_safe().
1634 const bool xfb_enabled
;
1636 const bool enhanced_layouts_enabled
;
1639 * Enum representing the order in which varyings are packed within a
1642 * Currently we pack vec4's first, then vec2's, then scalar values, then
1643 * vec3's. This order ensures that the only vectors that are at risk of
1644 * having to be "double parked" (split between two adjacent varying slots)
1647 enum packing_order_enum
{
1650 PACKING_ORDER_SCALAR
,
1654 static unsigned compute_packing_class(const ir_variable
*var
);
1655 static packing_order_enum
compute_packing_order(const ir_variable
*var
);
1656 static int match_comparator(const void *x_generic
, const void *y_generic
);
1657 static int xfb_comparator(const void *x_generic
, const void *y_generic
);
1658 static int not_xfb_comparator(const void *x_generic
, const void *y_generic
);
1661 * Structure recording the relationship between a single producer output
1662 * and a single consumer input.
1666 * Packing class for this varying, computed by compute_packing_class().
1668 unsigned packing_class
;
1671 * Packing order for this varying, computed by compute_packing_order().
1673 packing_order_enum packing_order
;
1674 unsigned num_components
;
1677 * The output variable in the producer stage.
1679 ir_variable
*producer_var
;
1682 * The input variable in the consumer stage.
1684 ir_variable
*consumer_var
;
1687 * The location which has been assigned for this varying. This is
1688 * expressed in multiples of a float, with the first generic varying
1689 * (i.e. the one referred to by VARYING_SLOT_VAR0) represented by the
1692 unsigned generic_location
;
1696 * The number of elements in the \c matches array that are currently in
1699 unsigned num_matches
;
1702 * The number of elements that were set aside for the \c matches array when
1705 unsigned matches_capacity
;
1707 gl_shader_stage producer_stage
;
1708 gl_shader_stage consumer_stage
;
1711 } /* anonymous namespace */
1713 varying_matches::varying_matches(bool disable_varying_packing
,
1714 bool disable_xfb_packing
,
1716 bool enhanced_layouts_enabled
,
1717 gl_shader_stage producer_stage
,
1718 gl_shader_stage consumer_stage
)
1719 : disable_varying_packing(disable_varying_packing
),
1720 disable_xfb_packing(disable_xfb_packing
),
1721 xfb_enabled(xfb_enabled
),
1722 enhanced_layouts_enabled(enhanced_layouts_enabled
),
1723 producer_stage(producer_stage
),
1724 consumer_stage(consumer_stage
)
1726 /* Note: this initial capacity is rather arbitrarily chosen to be large
1727 * enough for many cases without wasting an unreasonable amount of space.
1728 * varying_matches::record() will resize the array if there are more than
1729 * this number of varyings.
1731 this->matches_capacity
= 8;
1732 this->matches
= (match
*)
1733 malloc(sizeof(*this->matches
) * this->matches_capacity
);
1734 this->num_matches
= 0;
1738 varying_matches::~varying_matches()
1740 free(this->matches
);
1745 * Packing is always safe on individual arrays, structures, and matrices. It
1746 * is also safe if the varying is only used for transform feedback.
1749 varying_matches::is_varying_packing_safe(const glsl_type
*type
,
1750 const ir_variable
*var
) const
1752 if (consumer_stage
== MESA_SHADER_TESS_EVAL
||
1753 consumer_stage
== MESA_SHADER_TESS_CTRL
||
1754 producer_stage
== MESA_SHADER_TESS_CTRL
)
1757 return xfb_enabled
&& (type
->is_array() || type
->is_struct() ||
1758 type
->is_matrix() || var
->data
.is_xfb_only
);
1763 * Record the given producer/consumer variable pair in the list of variables
1764 * that should later be assigned locations.
1766 * It is permissible for \c consumer_var to be NULL (this happens if a
1767 * variable is output by the producer and consumed by transform feedback, but
1768 * not consumed by the consumer).
1770 * If \c producer_var has already been paired up with a consumer_var, or
1771 * producer_var is part of fixed pipeline functionality (and hence already has
1772 * a location assigned), this function has no effect.
1774 * Note: as a side effect this function may change the interpolation type of
1775 * \c producer_var, but only when the change couldn't possibly affect
1779 varying_matches::record(ir_variable
*producer_var
, ir_variable
*consumer_var
)
1781 assert(producer_var
!= NULL
|| consumer_var
!= NULL
);
1783 if ((producer_var
&& (!producer_var
->data
.is_unmatched_generic_inout
||
1784 producer_var
->data
.explicit_location
)) ||
1785 (consumer_var
&& (!consumer_var
->data
.is_unmatched_generic_inout
||
1786 consumer_var
->data
.explicit_location
))) {
1787 /* Either a location already exists for this variable (since it is part
1788 * of fixed functionality), or it has already been recorded as part of a
1794 bool needs_flat_qualifier
= consumer_var
== NULL
&&
1795 (producer_var
->type
->contains_integer() ||
1796 producer_var
->type
->contains_double());
1798 if (!disable_varying_packing
&&
1799 (!disable_xfb_packing
|| producer_var
== NULL
|| !producer_var
->data
.is_xfb
) &&
1800 (needs_flat_qualifier
||
1801 (consumer_stage
!= MESA_SHADER_NONE
&& consumer_stage
!= MESA_SHADER_FRAGMENT
))) {
1802 /* Since this varying is not being consumed by the fragment shader, its
1803 * interpolation type varying cannot possibly affect rendering.
1804 * Also, this variable is non-flat and is (or contains) an integer
1806 * If the consumer stage is unknown, don't modify the interpolation
1807 * type as it could affect rendering later with separate shaders.
1809 * lower_packed_varyings requires all integer varyings to flat,
1810 * regardless of where they appear. We can trivially satisfy that
1811 * requirement by changing the interpolation type to flat here.
1814 producer_var
->data
.centroid
= false;
1815 producer_var
->data
.sample
= false;
1816 producer_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1820 consumer_var
->data
.centroid
= false;
1821 consumer_var
->data
.sample
= false;
1822 consumer_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1826 if (this->num_matches
== this->matches_capacity
) {
1827 this->matches_capacity
*= 2;
1828 this->matches
= (match
*)
1829 realloc(this->matches
,
1830 sizeof(*this->matches
) * this->matches_capacity
);
1833 /* We must use the consumer to compute the packing class because in GL4.4+
1834 * there is no guarantee interpolation qualifiers will match across stages.
1836 * From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.30 spec:
1838 * "The type and presence of interpolation qualifiers of variables with
1839 * the same name declared in all linked shaders for the same cross-stage
1840 * interface must match, otherwise the link command will fail.
1842 * When comparing an output from one stage to an input of a subsequent
1843 * stage, the input and output don't match if their interpolation
1844 * qualifiers (or lack thereof) are not the same."
1846 * This text was also in at least revison 7 of the 4.40 spec but is no
1847 * longer in revision 9 and not in the 4.50 spec.
1849 const ir_variable
*const var
= (consumer_var
!= NULL
)
1850 ? consumer_var
: producer_var
;
1851 const gl_shader_stage stage
= (consumer_var
!= NULL
)
1852 ? consumer_stage
: producer_stage
;
1853 const glsl_type
*type
= get_varying_type(var
, stage
);
1855 if (producer_var
&& consumer_var
&&
1856 consumer_var
->data
.must_be_shader_input
) {
1857 producer_var
->data
.must_be_shader_input
= 1;
1860 this->matches
[this->num_matches
].packing_class
1861 = this->compute_packing_class(var
);
1862 this->matches
[this->num_matches
].packing_order
1863 = this->compute_packing_order(var
);
1864 if ((this->disable_varying_packing
&& !is_varying_packing_safe(type
, var
)) ||
1865 (this->disable_xfb_packing
&& var
->data
.is_xfb
) ||
1866 var
->data
.must_be_shader_input
) {
1867 unsigned slots
= type
->count_attribute_slots(false);
1868 this->matches
[this->num_matches
].num_components
= slots
* 4;
1870 this->matches
[this->num_matches
].num_components
1871 = type
->component_slots();
1874 this->matches
[this->num_matches
].producer_var
= producer_var
;
1875 this->matches
[this->num_matches
].consumer_var
= consumer_var
;
1876 this->num_matches
++;
1878 producer_var
->data
.is_unmatched_generic_inout
= 0;
1880 consumer_var
->data
.is_unmatched_generic_inout
= 0;
1885 * Choose locations for all of the variable matches that were previously
1886 * passed to varying_matches::record().
1887 * \param components returns array[slot] of number of components used
1888 * per slot (1, 2, 3 or 4)
1889 * \param reserved_slots bitmask indicating which varying slots are already
1891 * \return number of slots (4-element vectors) allocated
1894 varying_matches::assign_locations(struct gl_shader_program
*prog
,
1895 uint8_t components
[],
1896 uint64_t reserved_slots
)
1898 /* If packing has been disabled then we cannot safely sort the varyings by
1899 * class as it may mean we are using a version of OpenGL where
1900 * interpolation qualifiers are not guaranteed to be matching across
1901 * shaders, sorting in this case could result in mismatching shader
1903 * When packing is disabled the sort orders varyings used by transform
1904 * feedback first, but also depends on *undefined behaviour* of qsort to
1905 * reverse the order of the varyings. See: xfb_comparator().
1907 * If packing is only disabled for xfb varyings (mutually exclusive with
1908 * disable_varying_packing), we then group varyings depending on if they
1909 * are captured for transform feedback. The same *undefined behaviour* is
1910 * taken advantage of.
1912 if (this->disable_varying_packing
) {
1913 /* Only sort varyings that are only used by transform feedback. */
1914 qsort(this->matches
, this->num_matches
, sizeof(*this->matches
),
1915 &varying_matches::xfb_comparator
);
1916 } else if (this->disable_xfb_packing
) {
1917 /* Only sort varyings that are NOT used by transform feedback. */
1918 qsort(this->matches
, this->num_matches
, sizeof(*this->matches
),
1919 &varying_matches::not_xfb_comparator
);
1921 /* Sort varying matches into an order that makes them easy to pack. */
1922 qsort(this->matches
, this->num_matches
, sizeof(*this->matches
),
1923 &varying_matches::match_comparator
);
1926 unsigned generic_location
= 0;
1927 unsigned generic_patch_location
= MAX_VARYING
*4;
1928 bool previous_var_xfb
= false;
1929 bool previous_var_xfb_only
= false;
1930 unsigned previous_packing_class
= ~0u;
1932 /* For tranform feedback separate mode, we know the number of attributes
1933 * is <= the number of buffers. So packing isn't critical. In fact,
1934 * packing vec3 attributes can cause trouble because splitting a vec3
1935 * effectively creates an additional transform feedback output. The
1936 * extra TFB output may exceed device driver limits.
1938 const bool dont_pack_vec3
=
1939 (prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
&&
1940 prog
->TransformFeedback
.NumVarying
> 0);
1942 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
1943 unsigned *location
= &generic_location
;
1944 const ir_variable
*var
;
1945 const glsl_type
*type
;
1946 bool is_vertex_input
= false;
1948 if (matches
[i
].consumer_var
) {
1949 var
= matches
[i
].consumer_var
;
1950 type
= get_varying_type(var
, consumer_stage
);
1951 if (consumer_stage
== MESA_SHADER_VERTEX
)
1952 is_vertex_input
= true;
1954 var
= matches
[i
].producer_var
;
1955 type
= get_varying_type(var
, producer_stage
);
1958 if (var
->data
.patch
)
1959 location
= &generic_patch_location
;
1961 /* Advance to the next slot if this varying has a different packing
1962 * class than the previous one, and we're not already on a slot
1965 * Also advance if varying packing is disabled for transform feedback,
1966 * and previous or current varying is used for transform feedback.
1968 * Also advance to the next slot if packing is disabled. This makes sure
1969 * we don't assign varyings the same locations which is possible
1970 * because we still pack individual arrays, records and matrices even
1971 * when packing is disabled. Note we don't advance to the next slot if
1972 * we can pack varyings together that are only used for transform
1975 if (var
->data
.must_be_shader_input
||
1976 (this->disable_xfb_packing
&&
1977 (previous_var_xfb
|| var
->data
.is_xfb
)) ||
1978 (this->disable_varying_packing
&&
1979 !(previous_var_xfb_only
&& var
->data
.is_xfb_only
)) ||
1980 (previous_packing_class
!= this->matches
[i
].packing_class
) ||
1981 (this->matches
[i
].packing_order
== PACKING_ORDER_VEC3
&&
1983 *location
= ALIGN(*location
, 4);
1986 previous_var_xfb
= var
->data
.is_xfb
;
1987 previous_var_xfb_only
= var
->data
.is_xfb_only
;
1988 previous_packing_class
= this->matches
[i
].packing_class
;
1990 /* The number of components taken up by this variable. For vertex shader
1991 * inputs, we use the number of slots * 4, as they have different
1994 unsigned num_components
= is_vertex_input
?
1995 type
->count_attribute_slots(is_vertex_input
) * 4 :
1996 this->matches
[i
].num_components
;
1998 /* The last slot for this variable, inclusive. */
1999 unsigned slot_end
= *location
+ num_components
- 1;
2001 /* FIXME: We could be smarter in the below code and loop back over
2002 * trying to fill any locations that we skipped because we couldn't pack
2003 * the varying between an explicit location. For now just let the user
2004 * hit the linking error if we run out of room and suggest they use
2005 * explicit locations.
2007 while (slot_end
< MAX_VARYING
* 4u) {
2008 const unsigned slots
= (slot_end
/ 4u) - (*location
/ 4u) + 1;
2009 const uint64_t slot_mask
= ((1ull << slots
) - 1) << (*location
/ 4u);
2013 if ((reserved_slots
& slot_mask
) == 0) {
2017 *location
= ALIGN(*location
+ 1, 4);
2018 slot_end
= *location
+ num_components
- 1;
2021 if (!var
->data
.patch
&& slot_end
>= MAX_VARYING
* 4u) {
2022 linker_error(prog
, "insufficient contiguous locations available for "
2023 "%s it is possible an array or struct could not be "
2024 "packed between varyings with explicit locations. Try "
2025 "using an explicit location for arrays and structs.",
2029 if (slot_end
< MAX_VARYINGS_INCL_PATCH
* 4u) {
2030 for (unsigned j
= *location
/ 4u; j
< slot_end
/ 4u; j
++)
2032 components
[slot_end
/ 4u] = (slot_end
& 3) + 1;
2035 this->matches
[i
].generic_location
= *location
;
2037 *location
= slot_end
+ 1;
2040 return (generic_location
+ 3) / 4;
2045 * Update the producer and consumer shaders to reflect the locations
2046 * assignments that were made by varying_matches::assign_locations().
2049 varying_matches::store_locations() const
2051 /* Check is location needs to be packed with lower_packed_varyings() or if
2052 * we can just use ARB_enhanced_layouts packing.
2054 bool pack_loc
[MAX_VARYINGS_INCL_PATCH
] = { 0 };
2055 const glsl_type
*loc_type
[MAX_VARYINGS_INCL_PATCH
][4] = { {NULL
, NULL
} };
2057 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
2058 ir_variable
*producer_var
= this->matches
[i
].producer_var
;
2059 ir_variable
*consumer_var
= this->matches
[i
].consumer_var
;
2060 unsigned generic_location
= this->matches
[i
].generic_location
;
2061 unsigned slot
= generic_location
/ 4;
2062 unsigned offset
= generic_location
% 4;
2065 producer_var
->data
.location
= VARYING_SLOT_VAR0
+ slot
;
2066 producer_var
->data
.location_frac
= offset
;
2070 assert(consumer_var
->data
.location
== -1);
2071 consumer_var
->data
.location
= VARYING_SLOT_VAR0
+ slot
;
2072 consumer_var
->data
.location_frac
= offset
;
2075 /* Find locations suitable for native packing via
2076 * ARB_enhanced_layouts.
2078 if (producer_var
&& consumer_var
) {
2079 if (enhanced_layouts_enabled
) {
2080 const glsl_type
*type
=
2081 get_varying_type(producer_var
, producer_stage
);
2082 if (type
->is_array() || type
->is_matrix() || type
->is_struct() ||
2083 type
->is_double()) {
2084 unsigned comp_slots
= type
->component_slots() + offset
;
2085 unsigned slots
= comp_slots
/ 4;
2089 for (unsigned j
= 0; j
< slots
; j
++) {
2090 pack_loc
[slot
+ j
] = true;
2092 } else if (offset
+ type
->vector_elements
> 4) {
2093 pack_loc
[slot
] = true;
2094 pack_loc
[slot
+ 1] = true;
2096 loc_type
[slot
][offset
] = type
;
2102 /* Attempt to use ARB_enhanced_layouts for more efficient packing if
2105 if (enhanced_layouts_enabled
) {
2106 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
2107 ir_variable
*producer_var
= this->matches
[i
].producer_var
;
2108 ir_variable
*consumer_var
= this->matches
[i
].consumer_var
;
2109 unsigned generic_location
= this->matches
[i
].generic_location
;
2110 unsigned slot
= generic_location
/ 4;
2112 if (pack_loc
[slot
] || !producer_var
|| !consumer_var
)
2115 const glsl_type
*type
=
2116 get_varying_type(producer_var
, producer_stage
);
2117 bool type_match
= true;
2118 for (unsigned j
= 0; j
< 4; j
++) {
2119 if (loc_type
[slot
][j
]) {
2120 if (type
->base_type
!= loc_type
[slot
][j
]->base_type
)
2126 producer_var
->data
.explicit_location
= 1;
2127 consumer_var
->data
.explicit_location
= 1;
2128 producer_var
->data
.explicit_component
= 1;
2129 consumer_var
->data
.explicit_component
= 1;
2137 * Compute the "packing class" of the given varying. This is an unsigned
2138 * integer with the property that two variables in the same packing class can
2139 * be safely backed into the same vec4.
2142 varying_matches::compute_packing_class(const ir_variable
*var
)
2144 /* Without help from the back-end, there is no way to pack together
2145 * variables with different interpolation types, because
2146 * lower_packed_varyings must choose exactly one interpolation type for
2147 * each packed varying it creates.
2149 * However, we can safely pack together floats, ints, and uints, because:
2151 * - varyings of base type "int" and "uint" must use the "flat"
2152 * interpolation type, which can only occur in GLSL 1.30 and above.
2154 * - On platforms that support GLSL 1.30 and above, lower_packed_varyings
2155 * can store flat floats as ints without losing any information (using
2156 * the ir_unop_bitcast_* opcodes).
2158 * Therefore, the packing class depends only on the interpolation type.
2160 const unsigned interp
= var
->is_interpolation_flat()
2161 ? unsigned(INTERP_MODE_FLAT
) : var
->data
.interpolation
;
2163 assert(interp
< (1 << 3));
2165 const unsigned packing_class
= (interp
<< 0) |
2166 (var
->data
.centroid
<< 3) |
2167 (var
->data
.sample
<< 4) |
2168 (var
->data
.patch
<< 5) |
2169 (var
->data
.must_be_shader_input
<< 6);
2171 return packing_class
;
2176 * Compute the "packing order" of the given varying. This is a sort key we
2177 * use to determine when to attempt to pack the given varying relative to
2178 * other varyings in the same packing class.
2180 varying_matches::packing_order_enum
2181 varying_matches::compute_packing_order(const ir_variable
*var
)
2183 const glsl_type
*element_type
= var
->type
;
2185 while (element_type
->is_array()) {
2186 element_type
= element_type
->fields
.array
;
2189 switch (element_type
->component_slots() % 4) {
2190 case 1: return PACKING_ORDER_SCALAR
;
2191 case 2: return PACKING_ORDER_VEC2
;
2192 case 3: return PACKING_ORDER_VEC3
;
2193 case 0: return PACKING_ORDER_VEC4
;
2195 assert(!"Unexpected value of vector_elements");
2196 return PACKING_ORDER_VEC4
;
2202 * Comparison function passed to qsort() to sort varyings by packing_class and
2203 * then by packing_order.
2206 varying_matches::match_comparator(const void *x_generic
, const void *y_generic
)
2208 const match
*x
= (const match
*) x_generic
;
2209 const match
*y
= (const match
*) y_generic
;
2211 if (x
->packing_class
!= y
->packing_class
)
2212 return x
->packing_class
- y
->packing_class
;
2213 return x
->packing_order
- y
->packing_order
;
2218 * Comparison function passed to qsort() to sort varyings used only by
2219 * transform feedback when packing of other varyings is disabled.
2222 varying_matches::xfb_comparator(const void *x_generic
, const void *y_generic
)
2224 const match
*x
= (const match
*) x_generic
;
2226 if (x
->producer_var
!= NULL
&& x
->producer_var
->data
.is_xfb_only
)
2227 return match_comparator(x_generic
, y_generic
);
2229 /* FIXME: When the comparator returns 0 it means the elements being
2230 * compared are equivalent. However the qsort documentation says:
2232 * "The order of equivalent elements is undefined."
2234 * In practice the sort ends up reversing the order of the varyings which
2235 * means locations are also assigned in this reversed order and happens to
2236 * be what we want. This is also whats happening in
2237 * varying_matches::match_comparator().
2244 * Comparison function passed to qsort() to sort varyings NOT used by
2245 * transform feedback when packing of xfb varyings is disabled.
2248 varying_matches::not_xfb_comparator(const void *x_generic
, const void *y_generic
)
2250 const match
*x
= (const match
*) x_generic
;
2252 if (x
->producer_var
!= NULL
&& !x
->producer_var
->data
.is_xfb
)
2253 return match_comparator(x_generic
, y_generic
);
2255 /* FIXME: When the comparator returns 0 it means the elements being
2256 * compared are equivalent. However the qsort documentation says:
2258 * "The order of equivalent elements is undefined."
2260 * In practice the sort ends up reversing the order of the varyings which
2261 * means locations are also assigned in this reversed order and happens to
2262 * be what we want. This is also whats happening in
2263 * varying_matches::match_comparator().
2270 * Is the given variable a varying variable to be counted against the
2271 * limit in ctx->Const.MaxVarying?
2272 * This includes variables such as texcoords, colors and generic
2273 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2276 var_counts_against_varying_limit(gl_shader_stage stage
, const ir_variable
*var
)
2278 /* Only fragment shaders will take a varying variable as an input */
2279 if (stage
== MESA_SHADER_FRAGMENT
&&
2280 var
->data
.mode
== ir_var_shader_in
) {
2281 switch (var
->data
.location
) {
2282 case VARYING_SLOT_POS
:
2283 case VARYING_SLOT_FACE
:
2284 case VARYING_SLOT_PNTC
:
2295 * Visitor class that generates tfeedback_candidate structs describing all
2296 * possible targets of transform feedback.
2298 * tfeedback_candidate structs are stored in the hash table
2299 * tfeedback_candidates, which is passed to the constructor. This hash table
2300 * maps varying names to instances of the tfeedback_candidate struct.
2302 class tfeedback_candidate_generator
: public program_resource_visitor
2305 tfeedback_candidate_generator(void *mem_ctx
,
2306 hash_table
*tfeedback_candidates
,
2307 gl_shader_stage stage
)
2309 tfeedback_candidates(tfeedback_candidates
),
2316 void process(ir_variable
*var
)
2318 /* All named varying interface blocks should be flattened by now */
2319 assert(!var
->is_interface_instance());
2320 assert(var
->data
.mode
== ir_var_shader_out
);
2322 this->toplevel_var
= var
;
2323 this->varying_floats
= 0;
2324 const glsl_type
*t
=
2325 var
->data
.from_named_ifc_block
? var
->get_interface_type() : var
->type
;
2326 if (!var
->data
.patch
&& stage
== MESA_SHADER_TESS_CTRL
) {
2327 assert(t
->is_array());
2328 t
= t
->fields
.array
;
2330 program_resource_visitor::process(var
, t
, false);
2334 virtual void visit_field(const glsl_type
*type
, const char *name
,
2335 bool /* row_major */,
2336 const glsl_type
* /* record_type */,
2337 const enum glsl_interface_packing
,
2338 bool /* last_field */)
2340 assert(!type
->without_array()->is_struct());
2341 assert(!type
->without_array()->is_interface());
2343 tfeedback_candidate
*candidate
2344 = rzalloc(this->mem_ctx
, tfeedback_candidate
);
2345 candidate
->toplevel_var
= this->toplevel_var
;
2346 candidate
->type
= type
;
2347 candidate
->offset
= this->varying_floats
;
2348 _mesa_hash_table_insert(this->tfeedback_candidates
,
2349 ralloc_strdup(this->mem_ctx
, name
),
2351 this->varying_floats
+= type
->component_slots();
2355 * Memory context used to allocate hash table keys and values.
2357 void * const mem_ctx
;
2360 * Hash table in which tfeedback_candidate objects should be stored.
2362 hash_table
* const tfeedback_candidates
;
2364 gl_shader_stage stage
;
2367 * Pointer to the toplevel variable that is being traversed.
2369 ir_variable
*toplevel_var
;
2372 * Total number of varying floats that have been visited so far. This is
2373 * used to determine the offset to each varying within the toplevel
2376 unsigned varying_floats
;
2383 populate_consumer_input_sets(void *mem_ctx
, exec_list
*ir
,
2384 hash_table
*consumer_inputs
,
2385 hash_table
*consumer_interface_inputs
,
2386 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_TESS_MAX
])
2388 memset(consumer_inputs_with_locations
,
2390 sizeof(consumer_inputs_with_locations
[0]) * VARYING_SLOT_TESS_MAX
);
2392 foreach_in_list(ir_instruction
, node
, ir
) {
2393 ir_variable
*const input_var
= node
->as_variable();
2395 if (input_var
!= NULL
&& input_var
->data
.mode
== ir_var_shader_in
) {
2396 /* All interface blocks should have been lowered by this point */
2397 assert(!input_var
->type
->is_interface());
2399 if (input_var
->data
.explicit_location
) {
2400 /* assign_varying_locations only cares about finding the
2401 * ir_variable at the start of a contiguous location block.
2403 * - For !producer, consumer_inputs_with_locations isn't used.
2405 * - For !consumer, consumer_inputs_with_locations is empty.
2407 * For consumer && producer, if you were trying to set some
2408 * ir_variable to the middle of a location block on the other side
2409 * of producer/consumer, cross_validate_outputs_to_inputs() should
2410 * be link-erroring due to either type mismatch or location
2411 * overlaps. If the variables do match up, then they've got a
2412 * matching data.location and you only looked at
2413 * consumer_inputs_with_locations[var->data.location], not any
2414 * following entries for the array/structure.
2416 consumer_inputs_with_locations
[input_var
->data
.location
] =
2418 } else if (input_var
->get_interface_type() != NULL
) {
2419 char *const iface_field_name
=
2420 ralloc_asprintf(mem_ctx
, "%s.%s",
2421 input_var
->get_interface_type()->without_array()->name
,
2423 _mesa_hash_table_insert(consumer_interface_inputs
,
2424 iface_field_name
, input_var
);
2426 _mesa_hash_table_insert(consumer_inputs
,
2427 ralloc_strdup(mem_ctx
, input_var
->name
),
2435 * Find a variable from the consumer that "matches" the specified variable
2437 * This function only finds inputs with names that match. There is no
2438 * validation (here) that the types, etc. are compatible.
2441 get_matching_input(void *mem_ctx
,
2442 const ir_variable
*output_var
,
2443 hash_table
*consumer_inputs
,
2444 hash_table
*consumer_interface_inputs
,
2445 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_TESS_MAX
])
2447 ir_variable
*input_var
;
2449 if (output_var
->data
.explicit_location
) {
2450 input_var
= consumer_inputs_with_locations
[output_var
->data
.location
];
2451 } else if (output_var
->get_interface_type() != NULL
) {
2452 char *const iface_field_name
=
2453 ralloc_asprintf(mem_ctx
, "%s.%s",
2454 output_var
->get_interface_type()->without_array()->name
,
2456 hash_entry
*entry
= _mesa_hash_table_search(consumer_interface_inputs
, iface_field_name
);
2457 input_var
= entry
? (ir_variable
*) entry
->data
: NULL
;
2459 hash_entry
*entry
= _mesa_hash_table_search(consumer_inputs
, output_var
->name
);
2460 input_var
= entry
? (ir_variable
*) entry
->data
: NULL
;
2463 return (input_var
== NULL
|| input_var
->data
.mode
!= ir_var_shader_in
)
2470 io_variable_cmp(const void *_a
, const void *_b
)
2472 const ir_variable
*const a
= *(const ir_variable
**) _a
;
2473 const ir_variable
*const b
= *(const ir_variable
**) _b
;
2475 if (a
->data
.explicit_location
&& b
->data
.explicit_location
)
2476 return b
->data
.location
- a
->data
.location
;
2478 if (a
->data
.explicit_location
&& !b
->data
.explicit_location
)
2481 if (!a
->data
.explicit_location
&& b
->data
.explicit_location
)
2484 return -strcmp(a
->name
, b
->name
);
2488 * Sort the shader IO variables into canonical order
2491 canonicalize_shader_io(exec_list
*ir
, enum ir_variable_mode io_mode
)
2493 ir_variable
*var_table
[MAX_PROGRAM_OUTPUTS
* 4];
2494 unsigned num_variables
= 0;
2496 foreach_in_list(ir_instruction
, node
, ir
) {
2497 ir_variable
*const var
= node
->as_variable();
2499 if (var
== NULL
|| var
->data
.mode
!= io_mode
)
2502 /* If we have already encountered more I/O variables that could
2503 * successfully link, bail.
2505 if (num_variables
== ARRAY_SIZE(var_table
))
2508 var_table
[num_variables
++] = var
;
2511 if (num_variables
== 0)
2514 /* Sort the list in reverse order (io_variable_cmp handles this). Later
2515 * we're going to push the variables on to the IR list as a stack, so we
2516 * want the last variable (in canonical order) to be first in the list.
2518 qsort(var_table
, num_variables
, sizeof(var_table
[0]), io_variable_cmp
);
2520 /* Remove the variable from it's current location in the IR, and put it at
2523 for (unsigned i
= 0; i
< num_variables
; i
++) {
2524 var_table
[i
]->remove();
2525 ir
->push_head(var_table
[i
]);
2530 * Generate a bitfield map of the explicit locations for shader varyings.
2532 * Note: For Tessellation shaders we are sitting right on the limits of the
2533 * 64 bit map. Per-vertex and per-patch both have separate location domains
2534 * with a max of MAX_VARYING.
2537 reserved_varying_slot(struct gl_linked_shader
*stage
,
2538 ir_variable_mode io_mode
)
2540 assert(io_mode
== ir_var_shader_in
|| io_mode
== ir_var_shader_out
);
2541 /* Avoid an overflow of the returned value */
2542 assert(MAX_VARYINGS_INCL_PATCH
<= 64);
2550 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
2551 ir_variable
*const var
= node
->as_variable();
2553 if (var
== NULL
|| var
->data
.mode
!= io_mode
||
2554 !var
->data
.explicit_location
||
2555 var
->data
.location
< VARYING_SLOT_VAR0
)
2558 var_slot
= var
->data
.location
- VARYING_SLOT_VAR0
;
2560 unsigned num_elements
= get_varying_type(var
, stage
->Stage
)
2561 ->count_attribute_slots(io_mode
== ir_var_shader_in
&&
2562 stage
->Stage
== MESA_SHADER_VERTEX
);
2563 for (unsigned i
= 0; i
< num_elements
; i
++) {
2564 if (var_slot
>= 0 && var_slot
< MAX_VARYINGS_INCL_PATCH
)
2565 slots
|= UINT64_C(1) << var_slot
;
2575 * Assign locations for all variables that are produced in one pipeline stage
2576 * (the "producer") and consumed in the next stage (the "consumer").
2578 * Variables produced by the producer may also be consumed by transform
2581 * \param num_tfeedback_decls is the number of declarations indicating
2582 * variables that may be consumed by transform feedback.
2584 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2585 * representing the result of parsing the strings passed to
2586 * glTransformFeedbackVaryings(). assign_location() will be called for
2587 * each of these objects that matches one of the outputs of the
2590 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2591 * be NULL. In this case, varying locations are assigned solely based on the
2592 * requirements of transform feedback.
2595 assign_varying_locations(struct gl_context
*ctx
,
2597 struct gl_shader_program
*prog
,
2598 gl_linked_shader
*producer
,
2599 gl_linked_shader
*consumer
,
2600 unsigned num_tfeedback_decls
,
2601 tfeedback_decl
*tfeedback_decls
,
2602 const uint64_t reserved_slots
)
2604 /* Tessellation shaders treat inputs and outputs as shared memory and can
2605 * access inputs and outputs of other invocations.
2606 * Therefore, they can't be lowered to temps easily (and definitely not
2609 bool unpackable_tess
=
2610 (consumer
&& consumer
->Stage
== MESA_SHADER_TESS_EVAL
) ||
2611 (consumer
&& consumer
->Stage
== MESA_SHADER_TESS_CTRL
) ||
2612 (producer
&& producer
->Stage
== MESA_SHADER_TESS_CTRL
);
2614 /* Transform feedback code assumes varying arrays are packed, so if the
2615 * driver has disabled varying packing, make sure to at least enable
2616 * packing required by transform feedback. See below for exception.
2619 ctx
->Extensions
.EXT_transform_feedback
&& !unpackable_tess
;
2621 /* Some drivers actually requires packing to be explicitly disabled
2622 * for varyings used by transform feedback.
2624 bool disable_xfb_packing
=
2625 ctx
->Const
.DisableTransformFeedbackPacking
;
2627 /* Disable packing on outward facing interfaces for SSO because in ES we
2628 * need to retain the unpacked varying information for draw time
2631 * Packing is still enabled on individual arrays, structs, and matrices as
2632 * these are required by the transform feedback code and it is still safe
2633 * to do so. We also enable packing when a varying is only used for
2634 * transform feedback and its not a SSO.
2636 bool disable_varying_packing
=
2637 ctx
->Const
.DisableVaryingPacking
|| unpackable_tess
;
2638 if (prog
->SeparateShader
&& (producer
== NULL
|| consumer
== NULL
))
2639 disable_varying_packing
= true;
2641 varying_matches
matches(disable_varying_packing
,
2642 disable_xfb_packing
,
2644 ctx
->Extensions
.ARB_enhanced_layouts
,
2645 producer
? producer
->Stage
: MESA_SHADER_NONE
,
2646 consumer
? consumer
->Stage
: MESA_SHADER_NONE
);
2647 void *hash_table_ctx
= ralloc_context(NULL
);
2648 hash_table
*tfeedback_candidates
=
2649 _mesa_hash_table_create(hash_table_ctx
, _mesa_hash_string
,
2650 _mesa_key_string_equal
);
2651 hash_table
*consumer_inputs
=
2652 _mesa_hash_table_create(hash_table_ctx
, _mesa_hash_string
,
2653 _mesa_key_string_equal
);
2654 hash_table
*consumer_interface_inputs
=
2655 _mesa_hash_table_create(hash_table_ctx
, _mesa_hash_string
,
2656 _mesa_key_string_equal
);
2657 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_TESS_MAX
] = {
2661 unsigned consumer_vertices
= 0;
2662 if (consumer
&& consumer
->Stage
== MESA_SHADER_GEOMETRY
)
2663 consumer_vertices
= prog
->Geom
.VerticesIn
;
2665 /* Operate in a total of four passes.
2667 * 1. Sort inputs / outputs into a canonical order. This is necessary so
2668 * that inputs / outputs of separable shaders will be assigned
2669 * predictable locations regardless of the order in which declarations
2670 * appeared in the shader source.
2672 * 2. Assign locations for any matching inputs and outputs.
2674 * 3. Mark output variables in the producer that do not have locations as
2675 * not being outputs. This lets the optimizer eliminate them.
2677 * 4. Mark input variables in the consumer that do not have locations as
2678 * not being inputs. This lets the optimizer eliminate them.
2681 canonicalize_shader_io(consumer
->ir
, ir_var_shader_in
);
2684 canonicalize_shader_io(producer
->ir
, ir_var_shader_out
);
2687 linker::populate_consumer_input_sets(mem_ctx
, consumer
->ir
,
2689 consumer_interface_inputs
,
2690 consumer_inputs_with_locations
);
2693 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
2694 ir_variable
*const output_var
= node
->as_variable();
2696 if (output_var
== NULL
|| output_var
->data
.mode
!= ir_var_shader_out
)
2699 /* Only geometry shaders can use non-zero streams */
2700 assert(output_var
->data
.stream
== 0 ||
2701 (output_var
->data
.stream
< MAX_VERTEX_STREAMS
&&
2702 producer
->Stage
== MESA_SHADER_GEOMETRY
));
2704 if (num_tfeedback_decls
> 0) {
2705 tfeedback_candidate_generator
g(mem_ctx
, tfeedback_candidates
, producer
->Stage
);
2706 /* From OpenGL 4.6 (Core Profile) spec, section 11.1.2.1
2707 * ("Vertex Shader Variables / Output Variables")
2709 * "Each program object can specify a set of output variables from
2710 * one shader to be recorded in transform feedback mode (see
2711 * section 13.3). The variables that can be recorded are those
2712 * emitted by the first active shader, in order, from the
2716 * * tessellation evaluation shader
2717 * * tessellation control shader
2720 * But on OpenGL ES 3.2, section 11.1.2.1 ("Vertex Shader
2721 * Variables / Output Variables") tessellation control shader is
2722 * not included in the stages list.
2724 if (!prog
->IsES
|| producer
->Stage
!= MESA_SHADER_TESS_CTRL
) {
2725 g
.process(output_var
);
2729 ir_variable
*const input_var
=
2730 linker::get_matching_input(mem_ctx
, output_var
, consumer_inputs
,
2731 consumer_interface_inputs
,
2732 consumer_inputs_with_locations
);
2734 /* If a matching input variable was found, add this output (and the
2735 * input) to the set. If this is a separable program and there is no
2736 * consumer stage, add the output.
2738 * Always add TCS outputs. They are shared by all invocations
2739 * within a patch and can be used as shared memory.
2741 if (input_var
|| (prog
->SeparateShader
&& consumer
== NULL
) ||
2742 producer
->Stage
== MESA_SHADER_TESS_CTRL
) {
2743 matches
.record(output_var
, input_var
);
2746 /* Only stream 0 outputs can be consumed in the next stage */
2747 if (input_var
&& output_var
->data
.stream
!= 0) {
2748 linker_error(prog
, "output %s is assigned to stream=%d but "
2749 "is linked to an input, which requires stream=0",
2750 output_var
->name
, output_var
->data
.stream
);
2751 ralloc_free(hash_table_ctx
);
2756 /* If there's no producer stage, then this must be a separable program.
2757 * For example, we may have a program that has just a fragment shader.
2758 * Later this program will be used with some arbitrary vertex (or
2759 * geometry) shader program. This means that locations must be assigned
2760 * for all the inputs.
2762 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
2763 ir_variable
*const input_var
= node
->as_variable();
2764 if (input_var
&& input_var
->data
.mode
== ir_var_shader_in
) {
2765 matches
.record(NULL
, input_var
);
2770 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
2771 if (!tfeedback_decls
[i
].is_varying())
2774 const tfeedback_candidate
*matched_candidate
2775 = tfeedback_decls
[i
].find_candidate(prog
, tfeedback_candidates
);
2777 if (matched_candidate
== NULL
) {
2778 ralloc_free(hash_table_ctx
);
2782 /* Mark xfb varyings as always active */
2783 matched_candidate
->toplevel_var
->data
.always_active_io
= 1;
2785 /* Mark any corresponding inputs as always active also. We must do this
2786 * because we have a NIR pass that lowers vectors to scalars and another
2787 * that removes unused varyings.
2788 * We don't split varyings marked as always active because there is no
2789 * point in doing so. This means we need to mark both sides of the
2790 * interface as always active otherwise we will have a mismatch and
2791 * start removing things we shouldn't.
2793 ir_variable
*const input_var
=
2794 linker::get_matching_input(mem_ctx
, matched_candidate
->toplevel_var
,
2796 consumer_interface_inputs
,
2797 consumer_inputs_with_locations
);
2799 input_var
->data
.is_xfb
= 1;
2800 input_var
->data
.always_active_io
= 1;
2803 if (matched_candidate
->toplevel_var
->data
.is_unmatched_generic_inout
) {
2804 matched_candidate
->toplevel_var
->data
.is_xfb_only
= 1;
2805 matches
.record(matched_candidate
->toplevel_var
, NULL
);
2809 uint8_t components
[MAX_VARYINGS_INCL_PATCH
] = {0};
2810 const unsigned slots_used
= matches
.assign_locations(
2811 prog
, components
, reserved_slots
);
2812 matches
.store_locations();
2814 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
2815 if (tfeedback_decls
[i
].is_varying()) {
2816 if (!tfeedback_decls
[i
].assign_location(ctx
, prog
)) {
2817 ralloc_free(hash_table_ctx
);
2822 ralloc_free(hash_table_ctx
);
2824 if (consumer
&& producer
) {
2825 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
2826 ir_variable
*const var
= node
->as_variable();
2828 if (var
&& var
->data
.mode
== ir_var_shader_in
&&
2829 var
->data
.is_unmatched_generic_inout
) {
2830 if (!prog
->IsES
&& prog
->data
->Version
<= 120) {
2831 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2833 * Only those varying variables used (i.e. read) in
2834 * the fragment shader executable must be written to
2835 * by the vertex shader executable; declaring
2836 * superfluous varying variables in a vertex shader is
2839 * We interpret this text as meaning that the VS must
2840 * write the variable for the FS to read it. See
2841 * "glsl1-varying read but not written" in piglit.
2843 linker_error(prog
, "%s shader varying %s not written "
2845 _mesa_shader_stage_to_string(consumer
->Stage
),
2847 _mesa_shader_stage_to_string(producer
->Stage
));
2849 linker_warning(prog
, "%s shader varying %s not written "
2851 _mesa_shader_stage_to_string(consumer
->Stage
),
2853 _mesa_shader_stage_to_string(producer
->Stage
));
2858 /* Now that validation is done its safe to remove unused varyings. As
2859 * we have both a producer and consumer its safe to remove unused
2860 * varyings even if the program is a SSO because the stages are being
2861 * linked together i.e. we have a multi-stage SSO.
2863 remove_unused_shader_inputs_and_outputs(false, producer
,
2865 remove_unused_shader_inputs_and_outputs(false, consumer
,
2870 lower_packed_varyings(mem_ctx
, slots_used
, components
, ir_var_shader_out
,
2871 0, producer
, disable_varying_packing
,
2872 disable_xfb_packing
, xfb_enabled
);
2876 lower_packed_varyings(mem_ctx
, slots_used
, components
, ir_var_shader_in
,
2877 consumer_vertices
, consumer
, disable_varying_packing
,
2878 disable_xfb_packing
, xfb_enabled
);
2885 check_against_output_limit(struct gl_context
*ctx
,
2886 struct gl_shader_program
*prog
,
2887 gl_linked_shader
*producer
,
2888 unsigned num_explicit_locations
)
2890 unsigned output_vectors
= num_explicit_locations
;
2892 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
2893 ir_variable
*const var
= node
->as_variable();
2895 if (var
&& !var
->data
.explicit_location
&&
2896 var
->data
.mode
== ir_var_shader_out
&&
2897 var_counts_against_varying_limit(producer
->Stage
, var
)) {
2898 /* outputs for fragment shader can't be doubles */
2899 output_vectors
+= var
->type
->count_attribute_slots(false);
2903 assert(producer
->Stage
!= MESA_SHADER_FRAGMENT
);
2904 unsigned max_output_components
=
2905 ctx
->Const
.Program
[producer
->Stage
].MaxOutputComponents
;
2907 const unsigned output_components
= output_vectors
* 4;
2908 if (output_components
> max_output_components
) {
2909 if (ctx
->API
== API_OPENGLES2
|| prog
->IsES
)
2910 linker_error(prog
, "%s shader uses too many output vectors "
2912 _mesa_shader_stage_to_string(producer
->Stage
),
2914 max_output_components
/ 4);
2916 linker_error(prog
, "%s shader uses too many output components "
2918 _mesa_shader_stage_to_string(producer
->Stage
),
2920 max_output_components
);
2929 check_against_input_limit(struct gl_context
*ctx
,
2930 struct gl_shader_program
*prog
,
2931 gl_linked_shader
*consumer
,
2932 unsigned num_explicit_locations
)
2934 unsigned input_vectors
= num_explicit_locations
;
2936 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
2937 ir_variable
*const var
= node
->as_variable();
2939 if (var
&& !var
->data
.explicit_location
&&
2940 var
->data
.mode
== ir_var_shader_in
&&
2941 var_counts_against_varying_limit(consumer
->Stage
, var
)) {
2942 /* vertex inputs aren't varying counted */
2943 input_vectors
+= var
->type
->count_attribute_slots(false);
2947 assert(consumer
->Stage
!= MESA_SHADER_VERTEX
);
2948 unsigned max_input_components
=
2949 ctx
->Const
.Program
[consumer
->Stage
].MaxInputComponents
;
2951 const unsigned input_components
= input_vectors
* 4;
2952 if (input_components
> max_input_components
) {
2953 if (ctx
->API
== API_OPENGLES2
|| prog
->IsES
)
2954 linker_error(prog
, "%s shader uses too many input vectors "
2956 _mesa_shader_stage_to_string(consumer
->Stage
),
2958 max_input_components
/ 4);
2960 linker_error(prog
, "%s shader uses too many input components "
2962 _mesa_shader_stage_to_string(consumer
->Stage
),
2964 max_input_components
);
2973 link_varyings(struct gl_shader_program
*prog
, unsigned first
, unsigned last
,
2974 struct gl_context
*ctx
, void *mem_ctx
)
2976 bool has_xfb_qualifiers
= false;
2977 unsigned num_tfeedback_decls
= 0;
2978 char **varying_names
= NULL
;
2979 tfeedback_decl
*tfeedback_decls
= NULL
;
2981 /* From the ARB_enhanced_layouts spec:
2983 * "If the shader used to record output variables for transform feedback
2984 * varyings uses the "xfb_buffer", "xfb_offset", or "xfb_stride" layout
2985 * qualifiers, the values specified by TransformFeedbackVaryings are
2986 * ignored, and the set of variables captured for transform feedback is
2987 * instead derived from the specified layout qualifiers."
2989 for (int i
= MESA_SHADER_FRAGMENT
- 1; i
>= 0; i
--) {
2990 /* Find last stage before fragment shader */
2991 if (prog
->_LinkedShaders
[i
]) {
2992 has_xfb_qualifiers
=
2993 process_xfb_layout_qualifiers(mem_ctx
, prog
->_LinkedShaders
[i
],
2994 prog
, &num_tfeedback_decls
,
3000 if (!has_xfb_qualifiers
) {
3001 num_tfeedback_decls
= prog
->TransformFeedback
.NumVarying
;
3002 varying_names
= prog
->TransformFeedback
.VaryingNames
;
3005 if (num_tfeedback_decls
!= 0) {
3006 /* From GL_EXT_transform_feedback:
3007 * A program will fail to link if:
3009 * * the <count> specified by TransformFeedbackVaryingsEXT is
3010 * non-zero, but the program object has no vertex or geometry
3013 if (first
>= MESA_SHADER_FRAGMENT
) {
3014 linker_error(prog
, "Transform feedback varyings specified, but "
3015 "no vertex, tessellation, or geometry shader is "
3020 tfeedback_decls
= rzalloc_array(mem_ctx
, tfeedback_decl
,
3021 num_tfeedback_decls
);
3022 if (!parse_tfeedback_decls(ctx
, prog
, mem_ctx
, num_tfeedback_decls
,
3023 varying_names
, tfeedback_decls
))
3027 /* If there is no fragment shader we need to set transform feedback.
3029 * For SSO we also need to assign output locations. We assign them here
3030 * because we need to do it for both single stage programs and multi stage
3033 if (last
< MESA_SHADER_FRAGMENT
&&
3034 (num_tfeedback_decls
!= 0 || prog
->SeparateShader
)) {
3035 const uint64_t reserved_out_slots
=
3036 reserved_varying_slot(prog
->_LinkedShaders
[last
], ir_var_shader_out
);
3037 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
3038 prog
->_LinkedShaders
[last
], NULL
,
3039 num_tfeedback_decls
, tfeedback_decls
,
3040 reserved_out_slots
))
3044 if (last
<= MESA_SHADER_FRAGMENT
) {
3045 /* Remove unused varyings from the first/last stage unless SSO */
3046 remove_unused_shader_inputs_and_outputs(prog
->SeparateShader
,
3047 prog
->_LinkedShaders
[first
],
3049 remove_unused_shader_inputs_and_outputs(prog
->SeparateShader
,
3050 prog
->_LinkedShaders
[last
],
3053 /* If the program is made up of only a single stage */
3054 if (first
== last
) {
3055 gl_linked_shader
*const sh
= prog
->_LinkedShaders
[last
];
3057 do_dead_builtin_varyings(ctx
, NULL
, sh
, 0, NULL
);
3058 do_dead_builtin_varyings(ctx
, sh
, NULL
, num_tfeedback_decls
,
3061 if (prog
->SeparateShader
) {
3062 const uint64_t reserved_slots
=
3063 reserved_varying_slot(sh
, ir_var_shader_in
);
3065 /* Assign input locations for SSO, output locations are already
3068 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
3069 NULL
/* producer */,
3071 0 /* num_tfeedback_decls */,
3072 NULL
/* tfeedback_decls */,
3077 /* Linking the stages in the opposite order (from fragment to vertex)
3078 * ensures that inter-shader outputs written to in an earlier stage
3079 * are eliminated if they are (transitively) not used in a later
3083 for (int i
= next
- 1; i
>= 0; i
--) {
3084 if (prog
->_LinkedShaders
[i
] == NULL
&& i
!= 0)
3087 gl_linked_shader
*const sh_i
= prog
->_LinkedShaders
[i
];
3088 gl_linked_shader
*const sh_next
= prog
->_LinkedShaders
[next
];
3090 const uint64_t reserved_out_slots
=
3091 reserved_varying_slot(sh_i
, ir_var_shader_out
);
3092 const uint64_t reserved_in_slots
=
3093 reserved_varying_slot(sh_next
, ir_var_shader_in
);
3095 do_dead_builtin_varyings(ctx
, sh_i
, sh_next
,
3096 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
3099 if (!assign_varying_locations(ctx
, mem_ctx
, prog
, sh_i
, sh_next
,
3100 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
3102 reserved_out_slots
| reserved_in_slots
))
3105 /* This must be done after all dead varyings are eliminated. */
3107 unsigned slots_used
= util_bitcount64(reserved_out_slots
);
3108 if (!check_against_output_limit(ctx
, prog
, sh_i
, slots_used
)) {
3113 unsigned slots_used
= util_bitcount64(reserved_in_slots
);
3114 if (!check_against_input_limit(ctx
, prog
, sh_next
, slots_used
))
3122 if (!store_tfeedback_info(ctx
, prog
, num_tfeedback_decls
, tfeedback_decls
,
3123 has_xfb_qualifiers
, mem_ctx
))