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
223 if (!output
->type
->record_compare(type_to_match
, false, true)) {
225 "%s shader output `%s' declared as struct `%s', "
226 "doesn't match in type with %s shader input "
227 "declared as struct `%s'\n",
228 _mesa_shader_stage_to_string(producer_stage
),
231 _mesa_shader_stage_to_string(consumer_stage
),
234 } else if (!output
->type
->is_array() || !is_gl_identifier(output
->name
)) {
235 /* There is a bit of a special case for gl_TexCoord. This
236 * built-in is unsized by default. Applications that variable
237 * access it must redeclare it with a size. There is some
238 * language in the GLSL spec that implies the fragment shader
239 * and vertex shader do not have to agree on this size. Other
240 * driver behave this way, and one or two applications seem to
243 * Neither declaration needs to be modified here because the array
244 * sizes are fixed later when update_array_sizes is called.
246 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
248 * "Unlike user-defined varying variables, the built-in
249 * varying variables don't have a strict one-to-one
250 * correspondence between the vertex language and the
251 * fragment language."
254 "%s shader output `%s' declared as type `%s', "
255 "but %s shader input declared as type `%s'\n",
256 _mesa_shader_stage_to_string(producer_stage
),
259 _mesa_shader_stage_to_string(consumer_stage
),
265 /* Check that all of the qualifiers match between stages.
268 /* According to the OpenGL and OpenGLES GLSL specs, the centroid qualifier
269 * should match until OpenGL 4.3 and OpenGLES 3.1. The OpenGLES 3.0
270 * conformance test suite does not verify that the qualifiers must match.
271 * The deqp test suite expects the opposite (OpenGLES 3.1) behavior for
272 * OpenGLES 3.0 drivers, so we relax the checking in all cases.
274 if (false /* always skip the centroid check */ &&
275 prog
->data
->Version
< (prog
->IsES
? 310 : 430) &&
276 input
->data
.centroid
!= output
->data
.centroid
) {
278 "%s shader output `%s' %s centroid qualifier, "
279 "but %s shader input %s centroid qualifier\n",
280 _mesa_shader_stage_to_string(producer_stage
),
282 (output
->data
.centroid
) ? "has" : "lacks",
283 _mesa_shader_stage_to_string(consumer_stage
),
284 (input
->data
.centroid
) ? "has" : "lacks");
288 if (input
->data
.sample
!= output
->data
.sample
) {
290 "%s shader output `%s' %s sample qualifier, "
291 "but %s shader input %s sample qualifier\n",
292 _mesa_shader_stage_to_string(producer_stage
),
294 (output
->data
.sample
) ? "has" : "lacks",
295 _mesa_shader_stage_to_string(consumer_stage
),
296 (input
->data
.sample
) ? "has" : "lacks");
300 if (input
->data
.patch
!= output
->data
.patch
) {
302 "%s shader output `%s' %s patch qualifier, "
303 "but %s shader input %s patch qualifier\n",
304 _mesa_shader_stage_to_string(producer_stage
),
306 (output
->data
.patch
) ? "has" : "lacks",
307 _mesa_shader_stage_to_string(consumer_stage
),
308 (input
->data
.patch
) ? "has" : "lacks");
312 /* The GLSL 4.30 and GLSL ES 3.00 specifications say:
314 * "As only outputs need be declared with invariant, an output from
315 * one shader stage will still match an input of a subsequent stage
316 * without the input being declared as invariant."
318 * while GLSL 4.20 says:
320 * "For variables leaving one shader and coming into another shader,
321 * the invariant keyword has to be used in both shaders, or a link
322 * error will result."
324 * and GLSL ES 1.00 section 4.6.4 "Invariance and Linking" says:
326 * "The invariance of varyings that are declared in both the vertex
327 * and fragment shaders must match."
329 if (input
->data
.explicit_invariant
!= output
->data
.explicit_invariant
&&
330 prog
->data
->Version
< (prog
->IsES
? 300 : 430)) {
332 "%s shader output `%s' %s invariant qualifier, "
333 "but %s shader input %s invariant qualifier\n",
334 _mesa_shader_stage_to_string(producer_stage
),
336 (output
->data
.explicit_invariant
) ? "has" : "lacks",
337 _mesa_shader_stage_to_string(consumer_stage
),
338 (input
->data
.explicit_invariant
) ? "has" : "lacks");
342 /* GLSL >= 4.40 removes text requiring interpolation qualifiers
343 * to match cross stage, they must only match within the same stage.
345 * From page 84 (page 90 of the PDF) of the GLSL 4.40 spec:
347 * "It is a link-time error if, within the same stage, the interpolation
348 * qualifiers of variables of the same name do not match.
350 * Section 4.3.9 (Interpolation) of the GLSL ES 3.00 spec says:
352 * "When no interpolation qualifier is present, smooth interpolation
355 * So we match variables where one is smooth and the other has no explicit
358 unsigned input_interpolation
= input
->data
.interpolation
;
359 unsigned output_interpolation
= output
->data
.interpolation
;
361 if (input_interpolation
== INTERP_MODE_NONE
)
362 input_interpolation
= INTERP_MODE_SMOOTH
;
363 if (output_interpolation
== INTERP_MODE_NONE
)
364 output_interpolation
= INTERP_MODE_SMOOTH
;
366 if (input_interpolation
!= output_interpolation
&&
367 prog
->data
->Version
< 440) {
368 if (!ctx
->Const
.AllowGLSLCrossStageInterpolationMismatch
) {
370 "%s shader output `%s' specifies %s "
371 "interpolation qualifier, "
372 "but %s shader input specifies %s "
373 "interpolation qualifier\n",
374 _mesa_shader_stage_to_string(producer_stage
),
376 interpolation_string(output
->data
.interpolation
),
377 _mesa_shader_stage_to_string(consumer_stage
),
378 interpolation_string(input
->data
.interpolation
));
382 "%s shader output `%s' specifies %s "
383 "interpolation qualifier, "
384 "but %s shader input specifies %s "
385 "interpolation qualifier\n",
386 _mesa_shader_stage_to_string(producer_stage
),
388 interpolation_string(output
->data
.interpolation
),
389 _mesa_shader_stage_to_string(consumer_stage
),
390 interpolation_string(input
->data
.interpolation
));
396 * Validate front and back color outputs against single color input
399 cross_validate_front_and_back_color(struct gl_context
*ctx
,
400 struct gl_shader_program
*prog
,
401 const ir_variable
*input
,
402 const ir_variable
*front_color
,
403 const ir_variable
*back_color
,
404 gl_shader_stage consumer_stage
,
405 gl_shader_stage producer_stage
)
407 if (front_color
!= NULL
&& front_color
->data
.assigned
)
408 cross_validate_types_and_qualifiers(ctx
, prog
, input
, front_color
,
409 consumer_stage
, producer_stage
);
411 if (back_color
!= NULL
&& back_color
->data
.assigned
)
412 cross_validate_types_and_qualifiers(ctx
, prog
, input
, back_color
,
413 consumer_stage
, producer_stage
);
417 compute_variable_location_slot(ir_variable
*var
, gl_shader_stage stage
)
419 unsigned location_start
= VARYING_SLOT_VAR0
;
422 case MESA_SHADER_VERTEX
:
423 if (var
->data
.mode
== ir_var_shader_in
)
424 location_start
= VERT_ATTRIB_GENERIC0
;
426 case MESA_SHADER_TESS_CTRL
:
427 case MESA_SHADER_TESS_EVAL
:
429 location_start
= VARYING_SLOT_PATCH0
;
431 case MESA_SHADER_FRAGMENT
:
432 if (var
->data
.mode
== ir_var_shader_out
)
433 location_start
= FRAG_RESULT_DATA0
;
439 return var
->data
.location
- location_start
;
442 struct explicit_location_info
{
444 bool base_type_is_integer
;
445 unsigned base_type_bit_size
;
446 unsigned interpolation
;
453 check_location_aliasing(struct explicit_location_info explicit_locations
[][4],
457 unsigned location_limit
,
458 const glsl_type
*type
,
459 unsigned interpolation
,
463 gl_shader_program
*prog
,
464 gl_shader_stage stage
)
467 unsigned base_type_bit_size
;
468 const glsl_type
*type_without_array
= type
->without_array();
469 const bool base_type_is_integer
=
470 glsl_base_type_is_integer(type_without_array
->base_type
);
471 const bool is_struct
= type_without_array
->is_struct();
473 /* structs don't have a defined underlying base type so just treat all
474 * component slots as used and set the bit size to 0. If there is
475 * location aliasing, we'll fail anyway later.
478 base_type_bit_size
= 0;
480 unsigned dmul
= type_without_array
->is_64bit() ? 2 : 1;
481 last_comp
= component
+ type_without_array
->vector_elements
* dmul
;
483 glsl_base_type_get_bit_size(type_without_array
->base_type
);
486 while (location
< location_limit
) {
489 struct explicit_location_info
*info
=
490 &explicit_locations
[location
][comp
];
493 if (info
->var
->type
->without_array()->is_struct() || is_struct
) {
494 /* Structs cannot share location since they are incompatible
495 * with any other underlying numerical type.
498 "%s shader has multiple %sputs sharing the "
499 "same location that don't have the same "
500 "underlying numerical type. Struct variable '%s', "
502 _mesa_shader_stage_to_string(stage
),
503 var
->data
.mode
== ir_var_shader_in
? "in" : "out",
504 is_struct
? var
->name
: info
->var
->name
,
507 } else if (comp
>= component
&& comp
< last_comp
) {
508 /* Component aliasing is not allowed */
510 "%s shader has multiple %sputs explicitly "
511 "assigned to location %d and component %d\n",
512 _mesa_shader_stage_to_string(stage
),
513 var
->data
.mode
== ir_var_shader_in
? "in" : "out",
517 /* From the OpenGL 4.60.5 spec, section 4.4.1 Input Layout
518 * Qualifiers, Page 67, (Location aliasing):
520 * " Further, when location aliasing, the aliases sharing the
521 * location must have the same underlying numerical type
522 * and bit width (floating-point or integer, 32-bit versus
523 * 64-bit, etc.) and the same auxiliary storage and
524 * interpolation qualification."
527 /* If the underlying numerical type isn't integer, implicitly
528 * it will be float or else we would have failed by now.
530 if (info
->base_type_is_integer
!= base_type_is_integer
) {
532 "%s shader has multiple %sputs sharing the "
533 "same location that don't have the same "
534 "underlying numerical type. Location %u "
536 _mesa_shader_stage_to_string(stage
),
537 var
->data
.mode
== ir_var_shader_in
?
538 "in" : "out", location
, comp
);
542 if (info
->base_type_bit_size
!= base_type_bit_size
) {
544 "%s shader has multiple %sputs sharing the "
545 "same location that don't have the same "
546 "underlying numerical bit size. Location %u "
548 _mesa_shader_stage_to_string(stage
),
549 var
->data
.mode
== ir_var_shader_in
?
550 "in" : "out", location
, comp
);
554 if (info
->interpolation
!= interpolation
) {
556 "%s shader has multiple %sputs sharing the "
557 "same location that don't have the same "
558 "interpolation qualification. Location %u "
560 _mesa_shader_stage_to_string(stage
),
561 var
->data
.mode
== ir_var_shader_in
?
562 "in" : "out", location
, comp
);
566 if (info
->centroid
!= centroid
||
567 info
->sample
!= sample
||
568 info
->patch
!= patch
) {
570 "%s shader has multiple %sputs sharing the "
571 "same location that don't have the same "
572 "auxiliary storage qualification. Location %u "
574 _mesa_shader_stage_to_string(stage
),
575 var
->data
.mode
== ir_var_shader_in
?
576 "in" : "out", location
, comp
);
580 } else if (comp
>= component
&& comp
< last_comp
) {
582 info
->base_type_is_integer
= base_type_is_integer
;
583 info
->base_type_bit_size
= base_type_bit_size
;
584 info
->interpolation
= interpolation
;
585 info
->centroid
= centroid
;
586 info
->sample
= sample
;
592 /* We need to do some special handling for doubles as dvec3 and
593 * dvec4 consume two consecutive locations. We don't need to
594 * worry about components beginning at anything other than 0 as
595 * the spec does not allow this for dvec3 and dvec4.
597 if (comp
== 4 && last_comp
> 4) {
598 last_comp
= last_comp
- 4;
599 /* Bump location index and reset the component index */
613 validate_explicit_variable_location(struct gl_context
*ctx
,
614 struct explicit_location_info explicit_locations
[][4],
616 gl_shader_program
*prog
,
617 gl_linked_shader
*sh
)
619 const glsl_type
*type
= get_varying_type(var
, sh
->Stage
);
620 unsigned num_elements
= type
->count_attribute_slots(false);
621 unsigned idx
= compute_variable_location_slot(var
, sh
->Stage
);
622 unsigned slot_limit
= idx
+ num_elements
;
624 /* Vertex shader inputs and fragment shader outputs are validated in
625 * assign_attribute_or_color_locations() so we should not attempt to
626 * validate them again here.
629 if (var
->data
.mode
== ir_var_shader_out
) {
630 assert(sh
->Stage
!= MESA_SHADER_FRAGMENT
);
632 ctx
->Const
.Program
[sh
->Stage
].MaxOutputComponents
/ 4;
634 assert(var
->data
.mode
== ir_var_shader_in
);
635 assert(sh
->Stage
!= MESA_SHADER_VERTEX
);
637 ctx
->Const
.Program
[sh
->Stage
].MaxInputComponents
/ 4;
640 if (slot_limit
> slot_max
) {
642 "Invalid location %u in %s shader\n",
643 idx
, _mesa_shader_stage_to_string(sh
->Stage
));
647 const glsl_type
*type_without_array
= type
->without_array();
648 if (type_without_array
->is_interface()) {
649 for (unsigned i
= 0; i
< type_without_array
->length
; i
++) {
650 glsl_struct_field
*field
= &type_without_array
->fields
.structure
[i
];
651 unsigned field_location
= field
->location
-
652 (field
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
);
653 if (!check_location_aliasing(explicit_locations
, var
,
655 0, field_location
+ 1,
657 field
->interpolation
,
665 } else if (!check_location_aliasing(explicit_locations
, var
,
666 idx
, var
->data
.location_frac
,
668 var
->data
.interpolation
,
680 * Validate explicit locations for the inputs to the first stage and the
681 * outputs of the last stage in a program, if those are not the VS and FS
685 validate_first_and_last_interface_explicit_locations(struct gl_context
*ctx
,
686 struct gl_shader_program
*prog
,
687 gl_shader_stage first_stage
,
688 gl_shader_stage last_stage
)
690 /* VS inputs and FS outputs are validated in
691 * assign_attribute_or_color_locations()
693 bool validate_first_stage
= first_stage
!= MESA_SHADER_VERTEX
;
694 bool validate_last_stage
= last_stage
!= MESA_SHADER_FRAGMENT
;
695 if (!validate_first_stage
&& !validate_last_stage
)
698 struct explicit_location_info explicit_locations
[MAX_VARYING
][4];
700 gl_shader_stage stages
[2] = { first_stage
, last_stage
};
701 bool validate_stage
[2] = { validate_first_stage
, validate_last_stage
};
702 ir_variable_mode var_direction
[2] = { ir_var_shader_in
, ir_var_shader_out
};
704 for (unsigned i
= 0; i
< 2; i
++) {
705 if (!validate_stage
[i
])
708 gl_shader_stage stage
= stages
[i
];
710 gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
713 memset(explicit_locations
, 0, sizeof(explicit_locations
));
715 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
716 ir_variable
*const var
= node
->as_variable();
719 !var
->data
.explicit_location
||
720 var
->data
.location
< VARYING_SLOT_VAR0
||
721 var
->data
.mode
!= var_direction
[i
])
724 if (!validate_explicit_variable_location(
725 ctx
, explicit_locations
, var
, prog
, sh
)) {
733 * Validate that outputs from one stage match inputs of another
736 cross_validate_outputs_to_inputs(struct gl_context
*ctx
,
737 struct gl_shader_program
*prog
,
738 gl_linked_shader
*producer
,
739 gl_linked_shader
*consumer
)
741 glsl_symbol_table parameters
;
742 struct explicit_location_info output_explicit_locations
[MAX_VARYING
][4] = { 0 };
743 struct explicit_location_info input_explicit_locations
[MAX_VARYING
][4] = { 0 };
745 /* Find all shader outputs in the "producer" stage.
747 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
748 ir_variable
*const var
= node
->as_variable();
750 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
753 if (!var
->data
.explicit_location
754 || var
->data
.location
< VARYING_SLOT_VAR0
)
755 parameters
.add_variable(var
);
757 /* User-defined varyings with explicit locations are handled
758 * differently because they do not need to have matching names.
760 if (!validate_explicit_variable_location(ctx
,
761 output_explicit_locations
,
762 var
, prog
, producer
)) {
769 /* Find all shader inputs in the "consumer" stage. Any variables that have
770 * matching outputs already in the symbol table must have the same type and
773 * Exception: if the consumer is the geometry shader, then the inputs
774 * should be arrays and the type of the array element should match the type
775 * of the corresponding producer output.
777 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
778 ir_variable
*const input
= node
->as_variable();
780 if (input
== NULL
|| input
->data
.mode
!= ir_var_shader_in
)
783 if (strcmp(input
->name
, "gl_Color") == 0 && input
->data
.used
) {
784 const ir_variable
*const front_color
=
785 parameters
.get_variable("gl_FrontColor");
787 const ir_variable
*const back_color
=
788 parameters
.get_variable("gl_BackColor");
790 cross_validate_front_and_back_color(ctx
, prog
, input
,
791 front_color
, back_color
,
792 consumer
->Stage
, producer
->Stage
);
793 } else if (strcmp(input
->name
, "gl_SecondaryColor") == 0 && input
->data
.used
) {
794 const ir_variable
*const front_color
=
795 parameters
.get_variable("gl_FrontSecondaryColor");
797 const ir_variable
*const back_color
=
798 parameters
.get_variable("gl_BackSecondaryColor");
800 cross_validate_front_and_back_color(ctx
, prog
, input
,
801 front_color
, back_color
,
802 consumer
->Stage
, producer
->Stage
);
804 /* The rules for connecting inputs and outputs change in the presence
805 * of explicit locations. In this case, we no longer care about the
806 * names of the variables. Instead, we care only about the
807 * explicitly assigned location.
809 ir_variable
*output
= NULL
;
810 if (input
->data
.explicit_location
811 && input
->data
.location
>= VARYING_SLOT_VAR0
) {
813 const glsl_type
*type
= get_varying_type(input
, consumer
->Stage
);
814 unsigned num_elements
= type
->count_attribute_slots(false);
816 compute_variable_location_slot(input
, consumer
->Stage
);
817 unsigned slot_limit
= idx
+ num_elements
;
819 if (!validate_explicit_variable_location(ctx
,
820 input_explicit_locations
,
821 input
, prog
, consumer
)) {
825 while (idx
< slot_limit
) {
826 if (idx
>= MAX_VARYING
) {
828 "Invalid location %u in %s shader\n", idx
,
829 _mesa_shader_stage_to_string(consumer
->Stage
));
833 output
= output_explicit_locations
[idx
][input
->data
.location_frac
].var
;
835 if (output
== NULL
) {
836 /* A linker failure should only happen when there is no
837 * output declaration and there is Static Use of the
840 if (input
->data
.used
) {
842 "%s shader input `%s' with explicit location "
843 "has no matching output\n",
844 _mesa_shader_stage_to_string(consumer
->Stage
),
848 } else if (input
->data
.location
!= output
->data
.location
) {
850 "%s shader input `%s' with explicit location "
851 "has no matching output\n",
852 _mesa_shader_stage_to_string(consumer
->Stage
),
859 output
= parameters
.get_variable(input
->name
);
862 if (output
!= NULL
) {
863 /* Interface blocks have their own validation elsewhere so don't
864 * try validating them here.
866 if (!(input
->get_interface_type() &&
867 output
->get_interface_type()))
868 cross_validate_types_and_qualifiers(ctx
, prog
, input
, output
,
872 /* Check for input vars with unmatched output vars in prev stage
873 * taking into account that interface blocks could have a matching
874 * output but with different name, so we ignore them.
876 assert(!input
->data
.assigned
);
877 if (input
->data
.used
&& !input
->get_interface_type() &&
878 !input
->data
.explicit_location
)
880 "%s shader input `%s' "
881 "has no matching output in the previous stage\n",
882 _mesa_shader_stage_to_string(consumer
->Stage
),
890 * Demote shader inputs and outputs that are not used in other stages, and
891 * remove them via dead code elimination.
894 remove_unused_shader_inputs_and_outputs(bool is_separate_shader_object
,
895 gl_linked_shader
*sh
,
896 enum ir_variable_mode mode
)
898 if (is_separate_shader_object
)
901 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
902 ir_variable
*const var
= node
->as_variable();
904 if (var
== NULL
|| var
->data
.mode
!= int(mode
))
907 /* A shader 'in' or 'out' variable is only really an input or output if
908 * its value is used by other shader stages. This will cause the
909 * variable to have a location assigned.
911 if (var
->data
.is_unmatched_generic_inout
&& !var
->data
.is_xfb_only
) {
912 assert(var
->data
.mode
!= ir_var_temporary
);
914 /* Assign zeros to demoted inputs to allow more optimizations. */
915 if (var
->data
.mode
== ir_var_shader_in
&& !var
->constant_value
)
916 var
->constant_value
= ir_constant::zero(var
, var
->type
);
918 var
->data
.mode
= ir_var_auto
;
922 /* Eliminate code that is now dead due to unused inputs/outputs being
925 while (do_dead_code(sh
->ir
, false))
931 * Initialize this object based on a string that was passed to
932 * glTransformFeedbackVaryings.
934 * If the input is mal-formed, this call still succeeds, but it sets
935 * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var()
936 * will fail to find any matching variable.
939 tfeedback_decl::init(struct gl_context
*ctx
, const void *mem_ctx
,
942 /* We don't have to be pedantic about what is a valid GLSL variable name,
943 * because any variable with an invalid name can't exist in the IR anyway.
947 this->orig_name
= input
;
948 this->lowered_builtin_array_variable
= none
;
949 this->skip_components
= 0;
950 this->next_buffer_separator
= false;
951 this->matched_candidate
= NULL
;
956 if (ctx
->Extensions
.ARB_transform_feedback3
) {
957 /* Parse gl_NextBuffer. */
958 if (strcmp(input
, "gl_NextBuffer") == 0) {
959 this->next_buffer_separator
= true;
963 /* Parse gl_SkipComponents. */
964 if (strcmp(input
, "gl_SkipComponents1") == 0)
965 this->skip_components
= 1;
966 else if (strcmp(input
, "gl_SkipComponents2") == 0)
967 this->skip_components
= 2;
968 else if (strcmp(input
, "gl_SkipComponents3") == 0)
969 this->skip_components
= 3;
970 else if (strcmp(input
, "gl_SkipComponents4") == 0)
971 this->skip_components
= 4;
973 if (this->skip_components
)
977 /* Parse a declaration. */
978 const char *base_name_end
;
979 long subscript
= parse_program_resource_name(input
, &base_name_end
);
980 this->var_name
= ralloc_strndup(mem_ctx
, input
, base_name_end
- input
);
981 if (this->var_name
== NULL
) {
982 _mesa_error_no_memory(__func__
);
986 if (subscript
>= 0) {
987 this->array_subscript
= subscript
;
988 this->is_subscripted
= true;
990 this->is_subscripted
= false;
993 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
994 * class must behave specially to account for the fact that gl_ClipDistance
995 * is converted from a float[8] to a vec4[2].
997 if (ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].LowerCombinedClipCullDistance
&&
998 strcmp(this->var_name
, "gl_ClipDistance") == 0) {
999 this->lowered_builtin_array_variable
= clip_distance
;
1001 if (ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].LowerCombinedClipCullDistance
&&
1002 strcmp(this->var_name
, "gl_CullDistance") == 0) {
1003 this->lowered_builtin_array_variable
= cull_distance
;
1006 if (ctx
->Const
.LowerTessLevel
&&
1007 (strcmp(this->var_name
, "gl_TessLevelOuter") == 0))
1008 this->lowered_builtin_array_variable
= tess_level_outer
;
1009 if (ctx
->Const
.LowerTessLevel
&&
1010 (strcmp(this->var_name
, "gl_TessLevelInner") == 0))
1011 this->lowered_builtin_array_variable
= tess_level_inner
;
1016 * Determine whether two tfeedback_decl objects refer to the same variable and
1017 * array index (if applicable).
1020 tfeedback_decl::is_same(const tfeedback_decl
&x
, const tfeedback_decl
&y
)
1022 assert(x
.is_varying() && y
.is_varying());
1024 if (strcmp(x
.var_name
, y
.var_name
) != 0)
1026 if (x
.is_subscripted
!= y
.is_subscripted
)
1028 if (x
.is_subscripted
&& x
.array_subscript
!= y
.array_subscript
)
1035 * Assign a location and stream ID for this tfeedback_decl object based on the
1036 * transform feedback candidate found by find_candidate.
1038 * If an error occurs, the error is reported through linker_error() and false
1042 tfeedback_decl::assign_location(struct gl_context
*ctx
,
1043 struct gl_shader_program
*prog
)
1045 assert(this->is_varying());
1047 unsigned fine_location
1048 = this->matched_candidate
->toplevel_var
->data
.location
* 4
1049 + this->matched_candidate
->toplevel_var
->data
.location_frac
1050 + this->matched_candidate
->offset
;
1051 const unsigned dmul
=
1052 this->matched_candidate
->type
->without_array()->is_64bit() ? 2 : 1;
1054 if (this->matched_candidate
->type
->is_array()) {
1055 /* Array variable */
1056 const unsigned matrix_cols
=
1057 this->matched_candidate
->type
->fields
.array
->matrix_columns
;
1058 const unsigned vector_elements
=
1059 this->matched_candidate
->type
->fields
.array
->vector_elements
;
1060 unsigned actual_array_size
;
1061 switch (this->lowered_builtin_array_variable
) {
1063 actual_array_size
= prog
->last_vert_prog
?
1064 prog
->last_vert_prog
->info
.clip_distance_array_size
: 0;
1067 actual_array_size
= prog
->last_vert_prog
?
1068 prog
->last_vert_prog
->info
.cull_distance_array_size
: 0;
1070 case tess_level_outer
:
1071 actual_array_size
= 4;
1073 case tess_level_inner
:
1074 actual_array_size
= 2;
1078 actual_array_size
= this->matched_candidate
->type
->array_size();
1082 if (this->is_subscripted
) {
1083 /* Check array bounds. */
1084 if (this->array_subscript
>= actual_array_size
) {
1085 linker_error(prog
, "Transform feedback varying %s has index "
1086 "%i, but the array size is %u.",
1087 this->orig_name
, this->array_subscript
,
1091 unsigned array_elem_size
= this->lowered_builtin_array_variable
?
1092 1 : vector_elements
* matrix_cols
* dmul
;
1093 fine_location
+= array_elem_size
* this->array_subscript
;
1096 this->size
= actual_array_size
;
1098 this->vector_elements
= vector_elements
;
1099 this->matrix_columns
= matrix_cols
;
1100 if (this->lowered_builtin_array_variable
)
1101 this->type
= GL_FLOAT
;
1103 this->type
= this->matched_candidate
->type
->fields
.array
->gl_type
;
1105 /* Regular variable (scalar, vector, or matrix) */
1106 if (this->is_subscripted
) {
1107 linker_error(prog
, "Transform feedback varying %s requested, "
1108 "but %s is not an array.",
1109 this->orig_name
, this->var_name
);
1113 this->vector_elements
= this->matched_candidate
->type
->vector_elements
;
1114 this->matrix_columns
= this->matched_candidate
->type
->matrix_columns
;
1115 this->type
= this->matched_candidate
->type
->gl_type
;
1117 this->location
= fine_location
/ 4;
1118 this->location_frac
= fine_location
% 4;
1120 /* From GL_EXT_transform_feedback:
1121 * A program will fail to link if:
1123 * * the total number of components to capture in any varying
1124 * variable in <varyings> is greater than the constant
1125 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1126 * buffer mode is SEPARATE_ATTRIBS_EXT;
1128 if (prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
&&
1129 this->num_components() >
1130 ctx
->Const
.MaxTransformFeedbackSeparateComponents
) {
1131 linker_error(prog
, "Transform feedback varying %s exceeds "
1132 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1137 /* Only transform feedback varyings can be assigned to non-zero streams,
1138 * so assign the stream id here.
1140 this->stream_id
= this->matched_candidate
->toplevel_var
->data
.stream
;
1142 unsigned array_offset
= this->array_subscript
* 4 * dmul
;
1143 unsigned struct_offset
= this->matched_candidate
->offset
* 4 * dmul
;
1144 this->buffer
= this->matched_candidate
->toplevel_var
->data
.xfb_buffer
;
1145 this->offset
= this->matched_candidate
->toplevel_var
->data
.offset
+
1146 array_offset
+ struct_offset
;
1153 tfeedback_decl::get_num_outputs() const
1155 if (!this->is_varying()) {
1158 return (this->num_components() + this->location_frac
+ 3)/4;
1163 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1165 * If an error occurs, the error is reported through linker_error() and false
1169 tfeedback_decl::store(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1170 struct gl_transform_feedback_info
*info
,
1171 unsigned buffer
, unsigned buffer_index
,
1172 const unsigned max_outputs
, bool *explicit_stride
,
1173 bool has_xfb_qualifiers
) const
1175 unsigned xfb_offset
= 0;
1176 unsigned size
= this->size
;
1177 /* Handle gl_SkipComponents. */
1178 if (this->skip_components
) {
1179 info
->Buffers
[buffer
].Stride
+= this->skip_components
;
1180 size
= this->skip_components
;
1184 if (this->next_buffer_separator
) {
1189 if (has_xfb_qualifiers
) {
1190 xfb_offset
= this->offset
/ 4;
1192 xfb_offset
= info
->Buffers
[buffer
].Stride
;
1194 info
->Varyings
[info
->NumVarying
].Offset
= xfb_offset
* 4;
1197 unsigned location
= this->location
;
1198 unsigned location_frac
= this->location_frac
;
1199 unsigned num_components
= this->num_components();
1200 while (num_components
> 0) {
1201 unsigned output_size
= MIN2(num_components
, 4 - location_frac
);
1202 assert((info
->NumOutputs
== 0 && max_outputs
== 0) ||
1203 info
->NumOutputs
< max_outputs
);
1205 /* From the ARB_enhanced_layouts spec:
1207 * "If such a block member or variable is not written during a shader
1208 * invocation, the buffer contents at the assigned offset will be
1209 * undefined. Even if there are no static writes to a variable or
1210 * member that is assigned a transform feedback offset, the space is
1211 * still allocated in the buffer and still affects the stride."
1213 if (this->is_varying_written()) {
1214 info
->Outputs
[info
->NumOutputs
].ComponentOffset
= location_frac
;
1215 info
->Outputs
[info
->NumOutputs
].OutputRegister
= location
;
1216 info
->Outputs
[info
->NumOutputs
].NumComponents
= output_size
;
1217 info
->Outputs
[info
->NumOutputs
].StreamId
= stream_id
;
1218 info
->Outputs
[info
->NumOutputs
].OutputBuffer
= buffer
;
1219 info
->Outputs
[info
->NumOutputs
].DstOffset
= xfb_offset
;
1222 info
->Buffers
[buffer
].Stream
= this->stream_id
;
1223 xfb_offset
+= output_size
;
1225 num_components
-= output_size
;
1231 if (explicit_stride
&& explicit_stride
[buffer
]) {
1232 if (this->is_64bit() && info
->Buffers
[buffer
].Stride
% 2) {
1233 linker_error(prog
, "invalid qualifier xfb_stride=%d must be a "
1234 "multiple of 8 as its applied to a type that is or "
1235 "contains a double.",
1236 info
->Buffers
[buffer
].Stride
* 4);
1240 if (xfb_offset
> info
->Buffers
[buffer
].Stride
) {
1241 linker_error(prog
, "xfb_offset (%d) overflows xfb_stride (%d) for "
1242 "buffer (%d)", xfb_offset
* 4,
1243 info
->Buffers
[buffer
].Stride
* 4, buffer
);
1247 info
->Buffers
[buffer
].Stride
= xfb_offset
;
1250 /* From GL_EXT_transform_feedback:
1251 * A program will fail to link if:
1253 * * the total number of components to capture is greater than
1254 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1255 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1257 * From GL_ARB_enhanced_layouts:
1259 * "The resulting stride (implicit or explicit) must be less than or
1260 * equal to the implementation-dependent constant
1261 * gl_MaxTransformFeedbackInterleavedComponents."
1263 if ((prog
->TransformFeedback
.BufferMode
== GL_INTERLEAVED_ATTRIBS
||
1264 has_xfb_qualifiers
) &&
1265 info
->Buffers
[buffer
].Stride
>
1266 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
1267 linker_error(prog
, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1268 "limit has been exceeded.");
1273 info
->Varyings
[info
->NumVarying
].Name
= ralloc_strdup(prog
,
1275 info
->Varyings
[info
->NumVarying
].Type
= this->type
;
1276 info
->Varyings
[info
->NumVarying
].Size
= size
;
1277 info
->Varyings
[info
->NumVarying
].BufferIndex
= buffer_index
;
1279 info
->Buffers
[buffer
].NumVaryings
++;
1285 const tfeedback_candidate
*
1286 tfeedback_decl::find_candidate(gl_shader_program
*prog
,
1287 hash_table
*tfeedback_candidates
)
1289 const char *name
= this->var_name
;
1290 switch (this->lowered_builtin_array_variable
) {
1292 name
= this->var_name
;
1295 name
= "gl_ClipDistanceMESA";
1298 name
= "gl_CullDistanceMESA";
1300 case tess_level_outer
:
1301 name
= "gl_TessLevelOuterMESA";
1303 case tess_level_inner
:
1304 name
= "gl_TessLevelInnerMESA";
1307 hash_entry
*entry
= _mesa_hash_table_search(tfeedback_candidates
, name
);
1309 this->matched_candidate
= entry
?
1310 (const tfeedback_candidate
*) entry
->data
: NULL
;
1312 if (!this->matched_candidate
) {
1313 /* From GL_EXT_transform_feedback:
1314 * A program will fail to link if:
1316 * * any variable name specified in the <varyings> array is not
1317 * declared as an output in the geometry shader (if present) or
1318 * the vertex shader (if no geometry shader is present);
1320 linker_error(prog
, "Transform feedback varying %s undeclared.",
1324 return this->matched_candidate
;
1329 * Parse all the transform feedback declarations that were passed to
1330 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1332 * If an error occurs, the error is reported through linker_error() and false
1336 parse_tfeedback_decls(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1337 const void *mem_ctx
, unsigned num_names
,
1338 char **varying_names
, tfeedback_decl
*decls
)
1340 for (unsigned i
= 0; i
< num_names
; ++i
) {
1341 decls
[i
].init(ctx
, mem_ctx
, varying_names
[i
]);
1343 if (!decls
[i
].is_varying())
1346 /* From GL_EXT_transform_feedback:
1347 * A program will fail to link if:
1349 * * any two entries in the <varyings> array specify the same varying
1352 * We interpret this to mean "any two entries in the <varyings> array
1353 * specify the same varying variable and array index", since transform
1354 * feedback of arrays would be useless otherwise.
1356 for (unsigned j
= 0; j
< i
; ++j
) {
1357 if (decls
[j
].is_varying()) {
1358 if (tfeedback_decl::is_same(decls
[i
], decls
[j
])) {
1359 linker_error(prog
, "Transform feedback varying %s specified "
1360 "more than once.", varying_names
[i
]);
1371 cmp_xfb_offset(const void * x_generic
, const void * y_generic
)
1373 tfeedback_decl
*x
= (tfeedback_decl
*) x_generic
;
1374 tfeedback_decl
*y
= (tfeedback_decl
*) y_generic
;
1376 if (x
->get_buffer() != y
->get_buffer())
1377 return x
->get_buffer() - y
->get_buffer();
1378 return x
->get_offset() - y
->get_offset();
1382 * Store transform feedback location assignments into
1383 * prog->sh.LinkedTransformFeedback based on the data stored in
1386 * If an error occurs, the error is reported through linker_error() and false
1390 store_tfeedback_info(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
1391 unsigned num_tfeedback_decls
,
1392 tfeedback_decl
*tfeedback_decls
, bool has_xfb_qualifiers
)
1394 if (!prog
->last_vert_prog
)
1397 /* Make sure MaxTransformFeedbackBuffers is less than 32 so the bitmask for
1398 * tracking the number of buffers doesn't overflow.
1400 assert(ctx
->Const
.MaxTransformFeedbackBuffers
< 32);
1402 bool separate_attribs_mode
=
1403 prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
;
1405 struct gl_program
*xfb_prog
= prog
->last_vert_prog
;
1406 xfb_prog
->sh
.LinkedTransformFeedback
=
1407 rzalloc(xfb_prog
, struct gl_transform_feedback_info
);
1409 /* The xfb_offset qualifier does not have to be used in increasing order
1410 * however some drivers expect to receive the list of transform feedback
1411 * declarations in order so sort it now for convenience.
1413 if (has_xfb_qualifiers
) {
1414 qsort(tfeedback_decls
, num_tfeedback_decls
, sizeof(*tfeedback_decls
),
1418 xfb_prog
->sh
.LinkedTransformFeedback
->Varyings
=
1419 rzalloc_array(xfb_prog
, struct gl_transform_feedback_varying_info
,
1420 num_tfeedback_decls
);
1422 unsigned num_outputs
= 0;
1423 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1424 if (tfeedback_decls
[i
].is_varying_written())
1425 num_outputs
+= tfeedback_decls
[i
].get_num_outputs();
1428 xfb_prog
->sh
.LinkedTransformFeedback
->Outputs
=
1429 rzalloc_array(xfb_prog
, struct gl_transform_feedback_output
,
1432 unsigned num_buffers
= 0;
1433 unsigned buffers
= 0;
1435 if (!has_xfb_qualifiers
&& separate_attribs_mode
) {
1436 /* GL_SEPARATE_ATTRIBS */
1437 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1438 if (!tfeedback_decls
[i
].store(ctx
, prog
,
1439 xfb_prog
->sh
.LinkedTransformFeedback
,
1440 num_buffers
, num_buffers
, num_outputs
,
1441 NULL
, has_xfb_qualifiers
))
1444 buffers
|= 1 << num_buffers
;
1449 /* GL_INVERLEAVED_ATTRIBS */
1450 int buffer_stream_id
= -1;
1452 num_tfeedback_decls
? tfeedback_decls
[0].get_buffer() : 0;
1453 bool explicit_stride
[MAX_FEEDBACK_BUFFERS
] = { false };
1455 /* Apply any xfb_stride global qualifiers */
1456 if (has_xfb_qualifiers
) {
1457 for (unsigned j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1458 if (prog
->TransformFeedback
.BufferStride
[j
]) {
1459 explicit_stride
[j
] = true;
1460 xfb_prog
->sh
.LinkedTransformFeedback
->Buffers
[j
].Stride
=
1461 prog
->TransformFeedback
.BufferStride
[j
] / 4;
1466 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1467 if (has_xfb_qualifiers
&&
1468 buffer
!= tfeedback_decls
[i
].get_buffer()) {
1469 /* we have moved to the next buffer so reset stream id */
1470 buffer_stream_id
= -1;
1474 if (tfeedback_decls
[i
].is_next_buffer_separator()) {
1475 if (!tfeedback_decls
[i
].store(ctx
, prog
,
1476 xfb_prog
->sh
.LinkedTransformFeedback
,
1477 buffer
, num_buffers
, num_outputs
,
1478 explicit_stride
, has_xfb_qualifiers
))
1481 buffer_stream_id
= -1;
1485 if (has_xfb_qualifiers
) {
1486 buffer
= tfeedback_decls
[i
].get_buffer();
1488 buffer
= num_buffers
;
1491 if (tfeedback_decls
[i
].is_varying()) {
1492 if (buffer_stream_id
== -1) {
1493 /* First varying writing to this buffer: remember its stream */
1494 buffer_stream_id
= (int) tfeedback_decls
[i
].get_stream_id();
1496 /* Only mark a buffer as active when there is a varying
1497 * attached to it. This behaviour is based on a revised version
1498 * of section 13.2.2 of the GL 4.6 spec.
1500 buffers
|= 1 << buffer
;
1501 } else if (buffer_stream_id
!=
1502 (int) tfeedback_decls
[i
].get_stream_id()) {
1503 /* Varying writes to the same buffer from a different stream */
1505 "Transform feedback can't capture varyings belonging "
1506 "to different vertex streams in a single buffer. "
1507 "Varying %s writes to buffer from stream %u, other "
1508 "varyings in the same buffer write from stream %u.",
1509 tfeedback_decls
[i
].name(),
1510 tfeedback_decls
[i
].get_stream_id(),
1516 if (!tfeedback_decls
[i
].store(ctx
, prog
,
1517 xfb_prog
->sh
.LinkedTransformFeedback
,
1518 buffer
, num_buffers
, num_outputs
,
1519 explicit_stride
, has_xfb_qualifiers
))
1524 assert(xfb_prog
->sh
.LinkedTransformFeedback
->NumOutputs
== num_outputs
);
1526 xfb_prog
->sh
.LinkedTransformFeedback
->ActiveBuffers
= buffers
;
1533 * Data structure recording the relationship between outputs of one shader
1534 * stage (the "producer") and inputs of another (the "consumer").
1536 class varying_matches
1539 varying_matches(bool disable_varying_packing
, bool xfb_enabled
,
1540 bool enhanced_layouts_enabled
,
1541 gl_shader_stage producer_stage
,
1542 gl_shader_stage consumer_stage
);
1544 void record(ir_variable
*producer_var
, ir_variable
*consumer_var
);
1545 unsigned assign_locations(struct gl_shader_program
*prog
,
1546 uint8_t components
[],
1547 uint64_t reserved_slots
);
1548 void store_locations() const;
1551 bool is_varying_packing_safe(const glsl_type
*type
,
1552 const ir_variable
*var
) const;
1555 * If true, this driver disables varying packing, so all varyings need to
1556 * be aligned on slot boundaries, and take up a number of slots equal to
1557 * their number of matrix columns times their array size.
1559 * Packing may also be disabled because our current packing method is not
1560 * safe in SSO or versions of OpenGL where interpolation qualifiers are not
1561 * guaranteed to match across stages.
1563 const bool disable_varying_packing
;
1566 * If true, this driver has transform feedback enabled. The transform
1567 * feedback code requires at least some packing be done even when varying
1568 * packing is disabled, fortunately where transform feedback requires
1569 * packing it's safe to override the disabled setting. See
1570 * is_varying_packing_safe().
1572 const bool xfb_enabled
;
1574 const bool enhanced_layouts_enabled
;
1577 * Enum representing the order in which varyings are packed within a
1580 * Currently we pack vec4's first, then vec2's, then scalar values, then
1581 * vec3's. This order ensures that the only vectors that are at risk of
1582 * having to be "double parked" (split between two adjacent varying slots)
1585 enum packing_order_enum
{
1588 PACKING_ORDER_SCALAR
,
1592 static unsigned compute_packing_class(const ir_variable
*var
);
1593 static packing_order_enum
compute_packing_order(const ir_variable
*var
);
1594 static int match_comparator(const void *x_generic
, const void *y_generic
);
1595 static int xfb_comparator(const void *x_generic
, const void *y_generic
);
1598 * Structure recording the relationship between a single producer output
1599 * and a single consumer input.
1603 * Packing class for this varying, computed by compute_packing_class().
1605 unsigned packing_class
;
1608 * Packing order for this varying, computed by compute_packing_order().
1610 packing_order_enum packing_order
;
1611 unsigned num_components
;
1614 * The output variable in the producer stage.
1616 ir_variable
*producer_var
;
1619 * The input variable in the consumer stage.
1621 ir_variable
*consumer_var
;
1624 * The location which has been assigned for this varying. This is
1625 * expressed in multiples of a float, with the first generic varying
1626 * (i.e. the one referred to by VARYING_SLOT_VAR0) represented by the
1629 unsigned generic_location
;
1633 * The number of elements in the \c matches array that are currently in
1636 unsigned num_matches
;
1639 * The number of elements that were set aside for the \c matches array when
1642 unsigned matches_capacity
;
1644 gl_shader_stage producer_stage
;
1645 gl_shader_stage consumer_stage
;
1648 } /* anonymous namespace */
1650 varying_matches::varying_matches(bool disable_varying_packing
,
1652 bool enhanced_layouts_enabled
,
1653 gl_shader_stage producer_stage
,
1654 gl_shader_stage consumer_stage
)
1655 : disable_varying_packing(disable_varying_packing
),
1656 xfb_enabled(xfb_enabled
),
1657 enhanced_layouts_enabled(enhanced_layouts_enabled
),
1658 producer_stage(producer_stage
),
1659 consumer_stage(consumer_stage
)
1661 /* Note: this initial capacity is rather arbitrarily chosen to be large
1662 * enough for many cases without wasting an unreasonable amount of space.
1663 * varying_matches::record() will resize the array if there are more than
1664 * this number of varyings.
1666 this->matches_capacity
= 8;
1667 this->matches
= (match
*)
1668 malloc(sizeof(*this->matches
) * this->matches_capacity
);
1669 this->num_matches
= 0;
1673 varying_matches::~varying_matches()
1675 free(this->matches
);
1680 * Packing is always safe on individual arrays, structures, and matrices. It
1681 * is also safe if the varying is only used for transform feedback.
1684 varying_matches::is_varying_packing_safe(const glsl_type
*type
,
1685 const ir_variable
*var
) const
1687 if (consumer_stage
== MESA_SHADER_TESS_EVAL
||
1688 consumer_stage
== MESA_SHADER_TESS_CTRL
||
1689 producer_stage
== MESA_SHADER_TESS_CTRL
)
1692 return xfb_enabled
&& (type
->is_array() || type
->is_struct() ||
1693 type
->is_matrix() || var
->data
.is_xfb_only
);
1698 * Record the given producer/consumer variable pair in the list of variables
1699 * that should later be assigned locations.
1701 * It is permissible for \c consumer_var to be NULL (this happens if a
1702 * variable is output by the producer and consumed by transform feedback, but
1703 * not consumed by the consumer).
1705 * If \c producer_var has already been paired up with a consumer_var, or
1706 * producer_var is part of fixed pipeline functionality (and hence already has
1707 * a location assigned), this function has no effect.
1709 * Note: as a side effect this function may change the interpolation type of
1710 * \c producer_var, but only when the change couldn't possibly affect
1714 varying_matches::record(ir_variable
*producer_var
, ir_variable
*consumer_var
)
1716 assert(producer_var
!= NULL
|| consumer_var
!= NULL
);
1718 if ((producer_var
&& (!producer_var
->data
.is_unmatched_generic_inout
||
1719 producer_var
->data
.explicit_location
)) ||
1720 (consumer_var
&& (!consumer_var
->data
.is_unmatched_generic_inout
||
1721 consumer_var
->data
.explicit_location
))) {
1722 /* Either a location already exists for this variable (since it is part
1723 * of fixed functionality), or it has already been recorded as part of a
1729 bool needs_flat_qualifier
= consumer_var
== NULL
&&
1730 (producer_var
->type
->contains_integer() ||
1731 producer_var
->type
->contains_double());
1733 if (!disable_varying_packing
&&
1734 (needs_flat_qualifier
||
1735 (consumer_stage
!= MESA_SHADER_NONE
&& consumer_stage
!= MESA_SHADER_FRAGMENT
))) {
1736 /* Since this varying is not being consumed by the fragment shader, its
1737 * interpolation type varying cannot possibly affect rendering.
1738 * Also, this variable is non-flat and is (or contains) an integer
1740 * If the consumer stage is unknown, don't modify the interpolation
1741 * type as it could affect rendering later with separate shaders.
1743 * lower_packed_varyings requires all integer varyings to flat,
1744 * regardless of where they appear. We can trivially satisfy that
1745 * requirement by changing the interpolation type to flat here.
1748 producer_var
->data
.centroid
= false;
1749 producer_var
->data
.sample
= false;
1750 producer_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1754 consumer_var
->data
.centroid
= false;
1755 consumer_var
->data
.sample
= false;
1756 consumer_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1760 if (this->num_matches
== this->matches_capacity
) {
1761 this->matches_capacity
*= 2;
1762 this->matches
= (match
*)
1763 realloc(this->matches
,
1764 sizeof(*this->matches
) * this->matches_capacity
);
1767 /* We must use the consumer to compute the packing class because in GL4.4+
1768 * there is no guarantee interpolation qualifiers will match across stages.
1770 * From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.30 spec:
1772 * "The type and presence of interpolation qualifiers of variables with
1773 * the same name declared in all linked shaders for the same cross-stage
1774 * interface must match, otherwise the link command will fail.
1776 * When comparing an output from one stage to an input of a subsequent
1777 * stage, the input and output don't match if their interpolation
1778 * qualifiers (or lack thereof) are not the same."
1780 * This text was also in at least revison 7 of the 4.40 spec but is no
1781 * longer in revision 9 and not in the 4.50 spec.
1783 const ir_variable
*const var
= (consumer_var
!= NULL
)
1784 ? consumer_var
: producer_var
;
1785 const gl_shader_stage stage
= (consumer_var
!= NULL
)
1786 ? consumer_stage
: producer_stage
;
1787 const glsl_type
*type
= get_varying_type(var
, stage
);
1789 if (producer_var
&& consumer_var
&&
1790 consumer_var
->data
.must_be_shader_input
) {
1791 producer_var
->data
.must_be_shader_input
= 1;
1794 this->matches
[this->num_matches
].packing_class
1795 = this->compute_packing_class(var
);
1796 this->matches
[this->num_matches
].packing_order
1797 = this->compute_packing_order(var
);
1798 if ((this->disable_varying_packing
&& !is_varying_packing_safe(type
, var
)) ||
1799 var
->data
.must_be_shader_input
) {
1800 unsigned slots
= type
->count_attribute_slots(false);
1801 this->matches
[this->num_matches
].num_components
= slots
* 4;
1803 this->matches
[this->num_matches
].num_components
1804 = type
->component_slots();
1807 this->matches
[this->num_matches
].producer_var
= producer_var
;
1808 this->matches
[this->num_matches
].consumer_var
= consumer_var
;
1809 this->num_matches
++;
1811 producer_var
->data
.is_unmatched_generic_inout
= 0;
1813 consumer_var
->data
.is_unmatched_generic_inout
= 0;
1818 * Choose locations for all of the variable matches that were previously
1819 * passed to varying_matches::record().
1820 * \param components returns array[slot] of number of components used
1821 * per slot (1, 2, 3 or 4)
1822 * \param reserved_slots bitmask indicating which varying slots are already
1824 * \return number of slots (4-element vectors) allocated
1827 varying_matches::assign_locations(struct gl_shader_program
*prog
,
1828 uint8_t components
[],
1829 uint64_t reserved_slots
)
1831 /* If packing has been disabled then we cannot safely sort the varyings by
1832 * class as it may mean we are using a version of OpenGL where
1833 * interpolation qualifiers are not guaranteed to be matching across
1834 * shaders, sorting in this case could result in mismatching shader
1836 * When packing is disabled the sort orders varyings used by transform
1837 * feedback first, but also depends on *undefined behaviour* of qsort to
1838 * reverse the order of the varyings. See: xfb_comparator().
1840 if (!this->disable_varying_packing
) {
1841 /* Sort varying matches into an order that makes them easy to pack. */
1842 qsort(this->matches
, this->num_matches
, sizeof(*this->matches
),
1843 &varying_matches::match_comparator
);
1845 /* Only sort varyings that are only used by transform feedback. */
1846 qsort(this->matches
, this->num_matches
, sizeof(*this->matches
),
1847 &varying_matches::xfb_comparator
);
1850 unsigned generic_location
= 0;
1851 unsigned generic_patch_location
= MAX_VARYING
*4;
1852 bool previous_var_xfb_only
= false;
1853 unsigned previous_packing_class
= ~0u;
1855 /* For tranform feedback separate mode, we know the number of attributes
1856 * is <= the number of buffers. So packing isn't critical. In fact,
1857 * packing vec3 attributes can cause trouble because splitting a vec3
1858 * effectively creates an additional transform feedback output. The
1859 * extra TFB output may exceed device driver limits.
1861 const bool dont_pack_vec3
=
1862 (prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
&&
1863 prog
->TransformFeedback
.NumVarying
> 0);
1865 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
1866 unsigned *location
= &generic_location
;
1867 const ir_variable
*var
;
1868 const glsl_type
*type
;
1869 bool is_vertex_input
= false;
1871 if (matches
[i
].consumer_var
) {
1872 var
= matches
[i
].consumer_var
;
1873 type
= get_varying_type(var
, consumer_stage
);
1874 if (consumer_stage
== MESA_SHADER_VERTEX
)
1875 is_vertex_input
= true;
1877 var
= matches
[i
].producer_var
;
1878 type
= get_varying_type(var
, producer_stage
);
1881 if (var
->data
.patch
)
1882 location
= &generic_patch_location
;
1884 /* Advance to the next slot if this varying has a different packing
1885 * class than the previous one, and we're not already on a slot
1888 * Also advance to the next slot if packing is disabled. This makes sure
1889 * we don't assign varyings the same locations which is possible
1890 * because we still pack individual arrays, records and matrices even
1891 * when packing is disabled. Note we don't advance to the next slot if
1892 * we can pack varyings together that are only used for transform
1895 if (var
->data
.must_be_shader_input
||
1896 (this->disable_varying_packing
&&
1897 !(previous_var_xfb_only
&& var
->data
.is_xfb_only
)) ||
1898 (previous_packing_class
!= this->matches
[i
].packing_class
) ||
1899 (this->matches
[i
].packing_order
== PACKING_ORDER_VEC3
&&
1901 *location
= ALIGN(*location
, 4);
1904 previous_var_xfb_only
= var
->data
.is_xfb_only
;
1905 previous_packing_class
= this->matches
[i
].packing_class
;
1907 /* The number of components taken up by this variable. For vertex shader
1908 * inputs, we use the number of slots * 4, as they have different
1911 unsigned num_components
= is_vertex_input
?
1912 type
->count_attribute_slots(is_vertex_input
) * 4 :
1913 this->matches
[i
].num_components
;
1915 /* The last slot for this variable, inclusive. */
1916 unsigned slot_end
= *location
+ num_components
- 1;
1918 /* FIXME: We could be smarter in the below code and loop back over
1919 * trying to fill any locations that we skipped because we couldn't pack
1920 * the varying between an explicit location. For now just let the user
1921 * hit the linking error if we run out of room and suggest they use
1922 * explicit locations.
1924 while (slot_end
< MAX_VARYING
* 4u) {
1925 const unsigned slots
= (slot_end
/ 4u) - (*location
/ 4u) + 1;
1926 const uint64_t slot_mask
= ((1ull << slots
) - 1) << (*location
/ 4u);
1930 if ((reserved_slots
& slot_mask
) == 0) {
1934 *location
= ALIGN(*location
+ 1, 4);
1935 slot_end
= *location
+ num_components
- 1;
1938 if (!var
->data
.patch
&& slot_end
>= MAX_VARYING
* 4u) {
1939 linker_error(prog
, "insufficient contiguous locations available for "
1940 "%s it is possible an array or struct could not be "
1941 "packed between varyings with explicit locations. Try "
1942 "using an explicit location for arrays and structs.",
1946 if (slot_end
< MAX_VARYINGS_INCL_PATCH
* 4u) {
1947 for (unsigned j
= *location
/ 4u; j
< slot_end
/ 4u; j
++)
1949 components
[slot_end
/ 4u] = (slot_end
& 3) + 1;
1952 this->matches
[i
].generic_location
= *location
;
1954 *location
= slot_end
+ 1;
1957 return (generic_location
+ 3) / 4;
1962 * Update the producer and consumer shaders to reflect the locations
1963 * assignments that were made by varying_matches::assign_locations().
1966 varying_matches::store_locations() const
1968 /* Check is location needs to be packed with lower_packed_varyings() or if
1969 * we can just use ARB_enhanced_layouts packing.
1971 bool pack_loc
[MAX_VARYINGS_INCL_PATCH
] = { 0 };
1972 const glsl_type
*loc_type
[MAX_VARYINGS_INCL_PATCH
][4] = { {NULL
, NULL
} };
1974 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
1975 ir_variable
*producer_var
= this->matches
[i
].producer_var
;
1976 ir_variable
*consumer_var
= this->matches
[i
].consumer_var
;
1977 unsigned generic_location
= this->matches
[i
].generic_location
;
1978 unsigned slot
= generic_location
/ 4;
1979 unsigned offset
= generic_location
% 4;
1982 producer_var
->data
.location
= VARYING_SLOT_VAR0
+ slot
;
1983 producer_var
->data
.location_frac
= offset
;
1987 assert(consumer_var
->data
.location
== -1);
1988 consumer_var
->data
.location
= VARYING_SLOT_VAR0
+ slot
;
1989 consumer_var
->data
.location_frac
= offset
;
1992 /* Find locations suitable for native packing via
1993 * ARB_enhanced_layouts.
1995 if (producer_var
&& consumer_var
) {
1996 if (enhanced_layouts_enabled
) {
1997 const glsl_type
*type
=
1998 get_varying_type(producer_var
, producer_stage
);
1999 if (type
->is_array() || type
->is_matrix() || type
->is_struct() ||
2000 type
->is_double()) {
2001 unsigned comp_slots
= type
->component_slots() + offset
;
2002 unsigned slots
= comp_slots
/ 4;
2006 for (unsigned j
= 0; j
< slots
; j
++) {
2007 pack_loc
[slot
+ j
] = true;
2009 } else if (offset
+ type
->vector_elements
> 4) {
2010 pack_loc
[slot
] = true;
2011 pack_loc
[slot
+ 1] = true;
2013 loc_type
[slot
][offset
] = type
;
2019 /* Attempt to use ARB_enhanced_layouts for more efficient packing if
2022 if (enhanced_layouts_enabled
) {
2023 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
2024 ir_variable
*producer_var
= this->matches
[i
].producer_var
;
2025 ir_variable
*consumer_var
= this->matches
[i
].consumer_var
;
2026 unsigned generic_location
= this->matches
[i
].generic_location
;
2027 unsigned slot
= generic_location
/ 4;
2029 if (pack_loc
[slot
] || !producer_var
|| !consumer_var
)
2032 const glsl_type
*type
=
2033 get_varying_type(producer_var
, producer_stage
);
2034 bool type_match
= true;
2035 for (unsigned j
= 0; j
< 4; j
++) {
2036 if (loc_type
[slot
][j
]) {
2037 if (type
->base_type
!= loc_type
[slot
][j
]->base_type
)
2043 producer_var
->data
.explicit_location
= 1;
2044 consumer_var
->data
.explicit_location
= 1;
2045 producer_var
->data
.explicit_component
= 1;
2046 consumer_var
->data
.explicit_component
= 1;
2054 * Compute the "packing class" of the given varying. This is an unsigned
2055 * integer with the property that two variables in the same packing class can
2056 * be safely backed into the same vec4.
2059 varying_matches::compute_packing_class(const ir_variable
*var
)
2061 /* Without help from the back-end, there is no way to pack together
2062 * variables with different interpolation types, because
2063 * lower_packed_varyings must choose exactly one interpolation type for
2064 * each packed varying it creates.
2066 * However, we can safely pack together floats, ints, and uints, because:
2068 * - varyings of base type "int" and "uint" must use the "flat"
2069 * interpolation type, which can only occur in GLSL 1.30 and above.
2071 * - On platforms that support GLSL 1.30 and above, lower_packed_varyings
2072 * can store flat floats as ints without losing any information (using
2073 * the ir_unop_bitcast_* opcodes).
2075 * Therefore, the packing class depends only on the interpolation type.
2077 const unsigned interp
= var
->is_interpolation_flat()
2078 ? unsigned(INTERP_MODE_FLAT
) : var
->data
.interpolation
;
2080 assert(interp
< (1 << 3));
2082 const unsigned packing_class
= (interp
<< 0) |
2083 (var
->data
.centroid
<< 3) |
2084 (var
->data
.sample
<< 4) |
2085 (var
->data
.patch
<< 5) |
2086 (var
->data
.must_be_shader_input
<< 6);
2088 return packing_class
;
2093 * Compute the "packing order" of the given varying. This is a sort key we
2094 * use to determine when to attempt to pack the given varying relative to
2095 * other varyings in the same packing class.
2097 varying_matches::packing_order_enum
2098 varying_matches::compute_packing_order(const ir_variable
*var
)
2100 const glsl_type
*element_type
= var
->type
;
2102 while (element_type
->is_array()) {
2103 element_type
= element_type
->fields
.array
;
2106 switch (element_type
->component_slots() % 4) {
2107 case 1: return PACKING_ORDER_SCALAR
;
2108 case 2: return PACKING_ORDER_VEC2
;
2109 case 3: return PACKING_ORDER_VEC3
;
2110 case 0: return PACKING_ORDER_VEC4
;
2112 assert(!"Unexpected value of vector_elements");
2113 return PACKING_ORDER_VEC4
;
2119 * Comparison function passed to qsort() to sort varyings by packing_class and
2120 * then by packing_order.
2123 varying_matches::match_comparator(const void *x_generic
, const void *y_generic
)
2125 const match
*x
= (const match
*) x_generic
;
2126 const match
*y
= (const match
*) y_generic
;
2128 if (x
->packing_class
!= y
->packing_class
)
2129 return x
->packing_class
- y
->packing_class
;
2130 return x
->packing_order
- y
->packing_order
;
2135 * Comparison function passed to qsort() to sort varyings used only by
2136 * transform feedback when packing of other varyings is disabled.
2139 varying_matches::xfb_comparator(const void *x_generic
, const void *y_generic
)
2141 const match
*x
= (const match
*) x_generic
;
2143 if (x
->producer_var
!= NULL
&& x
->producer_var
->data
.is_xfb_only
)
2144 return match_comparator(x_generic
, y_generic
);
2146 /* FIXME: When the comparator returns 0 it means the elements being
2147 * compared are equivalent. However the qsort documentation says:
2149 * "The order of equivalent elements is undefined."
2151 * In practice the sort ends up reversing the order of the varyings which
2152 * means locations are also assigned in this reversed order and happens to
2153 * be what we want. This is also whats happening in
2154 * varying_matches::match_comparator().
2161 * Is the given variable a varying variable to be counted against the
2162 * limit in ctx->Const.MaxVarying?
2163 * This includes variables such as texcoords, colors and generic
2164 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2167 var_counts_against_varying_limit(gl_shader_stage stage
, const ir_variable
*var
)
2169 /* Only fragment shaders will take a varying variable as an input */
2170 if (stage
== MESA_SHADER_FRAGMENT
&&
2171 var
->data
.mode
== ir_var_shader_in
) {
2172 switch (var
->data
.location
) {
2173 case VARYING_SLOT_POS
:
2174 case VARYING_SLOT_FACE
:
2175 case VARYING_SLOT_PNTC
:
2186 * Visitor class that generates tfeedback_candidate structs describing all
2187 * possible targets of transform feedback.
2189 * tfeedback_candidate structs are stored in the hash table
2190 * tfeedback_candidates, which is passed to the constructor. This hash table
2191 * maps varying names to instances of the tfeedback_candidate struct.
2193 class tfeedback_candidate_generator
: public program_resource_visitor
2196 tfeedback_candidate_generator(void *mem_ctx
,
2197 hash_table
*tfeedback_candidates
,
2198 gl_shader_stage stage
)
2200 tfeedback_candidates(tfeedback_candidates
),
2207 void process(ir_variable
*var
)
2209 /* All named varying interface blocks should be flattened by now */
2210 assert(!var
->is_interface_instance());
2211 assert(var
->data
.mode
== ir_var_shader_out
);
2213 this->toplevel_var
= var
;
2214 this->varying_floats
= 0;
2215 const glsl_type
*t
=
2216 var
->data
.from_named_ifc_block
? var
->get_interface_type() : var
->type
;
2217 if (!var
->data
.patch
&& stage
== MESA_SHADER_TESS_CTRL
) {
2218 assert(t
->is_array());
2219 t
= t
->fields
.array
;
2221 program_resource_visitor::process(var
, t
, false);
2225 virtual void visit_field(const glsl_type
*type
, const char *name
,
2226 bool /* row_major */,
2227 const glsl_type
* /* record_type */,
2228 const enum glsl_interface_packing
,
2229 bool /* last_field */)
2231 assert(!type
->without_array()->is_struct());
2232 assert(!type
->without_array()->is_interface());
2234 tfeedback_candidate
*candidate
2235 = rzalloc(this->mem_ctx
, tfeedback_candidate
);
2236 candidate
->toplevel_var
= this->toplevel_var
;
2237 candidate
->type
= type
;
2238 candidate
->offset
= this->varying_floats
;
2239 _mesa_hash_table_insert(this->tfeedback_candidates
,
2240 ralloc_strdup(this->mem_ctx
, name
),
2242 this->varying_floats
+= type
->component_slots();
2246 * Memory context used to allocate hash table keys and values.
2248 void * const mem_ctx
;
2251 * Hash table in which tfeedback_candidate objects should be stored.
2253 hash_table
* const tfeedback_candidates
;
2255 gl_shader_stage stage
;
2258 * Pointer to the toplevel variable that is being traversed.
2260 ir_variable
*toplevel_var
;
2263 * Total number of varying floats that have been visited so far. This is
2264 * used to determine the offset to each varying within the toplevel
2267 unsigned varying_floats
;
2274 populate_consumer_input_sets(void *mem_ctx
, exec_list
*ir
,
2275 hash_table
*consumer_inputs
,
2276 hash_table
*consumer_interface_inputs
,
2277 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_TESS_MAX
])
2279 memset(consumer_inputs_with_locations
,
2281 sizeof(consumer_inputs_with_locations
[0]) * VARYING_SLOT_TESS_MAX
);
2283 foreach_in_list(ir_instruction
, node
, ir
) {
2284 ir_variable
*const input_var
= node
->as_variable();
2286 if (input_var
!= NULL
&& input_var
->data
.mode
== ir_var_shader_in
) {
2287 /* All interface blocks should have been lowered by this point */
2288 assert(!input_var
->type
->is_interface());
2290 if (input_var
->data
.explicit_location
) {
2291 /* assign_varying_locations only cares about finding the
2292 * ir_variable at the start of a contiguous location block.
2294 * - For !producer, consumer_inputs_with_locations isn't used.
2296 * - For !consumer, consumer_inputs_with_locations is empty.
2298 * For consumer && producer, if you were trying to set some
2299 * ir_variable to the middle of a location block on the other side
2300 * of producer/consumer, cross_validate_outputs_to_inputs() should
2301 * be link-erroring due to either type mismatch or location
2302 * overlaps. If the variables do match up, then they've got a
2303 * matching data.location and you only looked at
2304 * consumer_inputs_with_locations[var->data.location], not any
2305 * following entries for the array/structure.
2307 consumer_inputs_with_locations
[input_var
->data
.location
] =
2309 } else if (input_var
->get_interface_type() != NULL
) {
2310 char *const iface_field_name
=
2311 ralloc_asprintf(mem_ctx
, "%s.%s",
2312 input_var
->get_interface_type()->without_array()->name
,
2314 _mesa_hash_table_insert(consumer_interface_inputs
,
2315 iface_field_name
, input_var
);
2317 _mesa_hash_table_insert(consumer_inputs
,
2318 ralloc_strdup(mem_ctx
, input_var
->name
),
2326 * Find a variable from the consumer that "matches" the specified variable
2328 * This function only finds inputs with names that match. There is no
2329 * validation (here) that the types, etc. are compatible.
2332 get_matching_input(void *mem_ctx
,
2333 const ir_variable
*output_var
,
2334 hash_table
*consumer_inputs
,
2335 hash_table
*consumer_interface_inputs
,
2336 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_TESS_MAX
])
2338 ir_variable
*input_var
;
2340 if (output_var
->data
.explicit_location
) {
2341 input_var
= consumer_inputs_with_locations
[output_var
->data
.location
];
2342 } else if (output_var
->get_interface_type() != NULL
) {
2343 char *const iface_field_name
=
2344 ralloc_asprintf(mem_ctx
, "%s.%s",
2345 output_var
->get_interface_type()->without_array()->name
,
2347 hash_entry
*entry
= _mesa_hash_table_search(consumer_interface_inputs
, iface_field_name
);
2348 input_var
= entry
? (ir_variable
*) entry
->data
: NULL
;
2350 hash_entry
*entry
= _mesa_hash_table_search(consumer_inputs
, output_var
->name
);
2351 input_var
= entry
? (ir_variable
*) entry
->data
: NULL
;
2354 return (input_var
== NULL
|| input_var
->data
.mode
!= ir_var_shader_in
)
2361 io_variable_cmp(const void *_a
, const void *_b
)
2363 const ir_variable
*const a
= *(const ir_variable
**) _a
;
2364 const ir_variable
*const b
= *(const ir_variable
**) _b
;
2366 if (a
->data
.explicit_location
&& b
->data
.explicit_location
)
2367 return b
->data
.location
- a
->data
.location
;
2369 if (a
->data
.explicit_location
&& !b
->data
.explicit_location
)
2372 if (!a
->data
.explicit_location
&& b
->data
.explicit_location
)
2375 return -strcmp(a
->name
, b
->name
);
2379 * Sort the shader IO variables into canonical order
2382 canonicalize_shader_io(exec_list
*ir
, enum ir_variable_mode io_mode
)
2384 ir_variable
*var_table
[MAX_PROGRAM_OUTPUTS
* 4];
2385 unsigned num_variables
= 0;
2387 foreach_in_list(ir_instruction
, node
, ir
) {
2388 ir_variable
*const var
= node
->as_variable();
2390 if (var
== NULL
|| var
->data
.mode
!= io_mode
)
2393 /* If we have already encountered more I/O variables that could
2394 * successfully link, bail.
2396 if (num_variables
== ARRAY_SIZE(var_table
))
2399 var_table
[num_variables
++] = var
;
2402 if (num_variables
== 0)
2405 /* Sort the list in reverse order (io_variable_cmp handles this). Later
2406 * we're going to push the variables on to the IR list as a stack, so we
2407 * want the last variable (in canonical order) to be first in the list.
2409 qsort(var_table
, num_variables
, sizeof(var_table
[0]), io_variable_cmp
);
2411 /* Remove the variable from it's current location in the IR, and put it at
2414 for (unsigned i
= 0; i
< num_variables
; i
++) {
2415 var_table
[i
]->remove();
2416 ir
->push_head(var_table
[i
]);
2421 * Generate a bitfield map of the explicit locations for shader varyings.
2423 * Note: For Tessellation shaders we are sitting right on the limits of the
2424 * 64 bit map. Per-vertex and per-patch both have separate location domains
2425 * with a max of MAX_VARYING.
2428 reserved_varying_slot(struct gl_linked_shader
*stage
,
2429 ir_variable_mode io_mode
)
2431 assert(io_mode
== ir_var_shader_in
|| io_mode
== ir_var_shader_out
);
2432 /* Avoid an overflow of the returned value */
2433 assert(MAX_VARYINGS_INCL_PATCH
<= 64);
2441 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
2442 ir_variable
*const var
= node
->as_variable();
2444 if (var
== NULL
|| var
->data
.mode
!= io_mode
||
2445 !var
->data
.explicit_location
||
2446 var
->data
.location
< VARYING_SLOT_VAR0
)
2449 var_slot
= var
->data
.location
- VARYING_SLOT_VAR0
;
2451 unsigned num_elements
= get_varying_type(var
, stage
->Stage
)
2452 ->count_attribute_slots(io_mode
== ir_var_shader_in
&&
2453 stage
->Stage
== MESA_SHADER_VERTEX
);
2454 for (unsigned i
= 0; i
< num_elements
; i
++) {
2455 if (var_slot
>= 0 && var_slot
< MAX_VARYINGS_INCL_PATCH
)
2456 slots
|= UINT64_C(1) << var_slot
;
2466 * Assign locations for all variables that are produced in one pipeline stage
2467 * (the "producer") and consumed in the next stage (the "consumer").
2469 * Variables produced by the producer may also be consumed by transform
2472 * \param num_tfeedback_decls is the number of declarations indicating
2473 * variables that may be consumed by transform feedback.
2475 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2476 * representing the result of parsing the strings passed to
2477 * glTransformFeedbackVaryings(). assign_location() will be called for
2478 * each of these objects that matches one of the outputs of the
2481 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2482 * be NULL. In this case, varying locations are assigned solely based on the
2483 * requirements of transform feedback.
2486 assign_varying_locations(struct gl_context
*ctx
,
2488 struct gl_shader_program
*prog
,
2489 gl_linked_shader
*producer
,
2490 gl_linked_shader
*consumer
,
2491 unsigned num_tfeedback_decls
,
2492 tfeedback_decl
*tfeedback_decls
,
2493 const uint64_t reserved_slots
)
2495 /* Tessellation shaders treat inputs and outputs as shared memory and can
2496 * access inputs and outputs of other invocations.
2497 * Therefore, they can't be lowered to temps easily (and definitely not
2500 bool unpackable_tess
=
2501 (consumer
&& consumer
->Stage
== MESA_SHADER_TESS_EVAL
) ||
2502 (consumer
&& consumer
->Stage
== MESA_SHADER_TESS_CTRL
) ||
2503 (producer
&& producer
->Stage
== MESA_SHADER_TESS_CTRL
);
2505 /* Transform feedback code assumes varying arrays are packed, so if the
2506 * driver has disabled varying packing, make sure to at least enable
2507 * packing required by transform feedback.
2510 ctx
->Extensions
.EXT_transform_feedback
&& !unpackable_tess
;
2512 /* Disable packing on outward facing interfaces for SSO because in ES we
2513 * need to retain the unpacked varying information for draw time
2516 * Packing is still enabled on individual arrays, structs, and matrices as
2517 * these are required by the transform feedback code and it is still safe
2518 * to do so. We also enable packing when a varying is only used for
2519 * transform feedback and its not a SSO.
2521 bool disable_varying_packing
=
2522 ctx
->Const
.DisableVaryingPacking
|| unpackable_tess
;
2523 if (prog
->SeparateShader
&& (producer
== NULL
|| consumer
== NULL
))
2524 disable_varying_packing
= true;
2526 varying_matches
matches(disable_varying_packing
, xfb_enabled
,
2527 ctx
->Extensions
.ARB_enhanced_layouts
,
2528 producer
? producer
->Stage
: MESA_SHADER_NONE
,
2529 consumer
? consumer
->Stage
: MESA_SHADER_NONE
);
2530 hash_table
*tfeedback_candidates
=
2531 _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
2532 _mesa_key_string_equal
);
2533 hash_table
*consumer_inputs
=
2534 _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
2535 _mesa_key_string_equal
);
2536 hash_table
*consumer_interface_inputs
=
2537 _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
2538 _mesa_key_string_equal
);
2539 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_TESS_MAX
] = {
2543 unsigned consumer_vertices
= 0;
2544 if (consumer
&& consumer
->Stage
== MESA_SHADER_GEOMETRY
)
2545 consumer_vertices
= prog
->Geom
.VerticesIn
;
2547 /* Operate in a total of four passes.
2549 * 1. Sort inputs / outputs into a canonical order. This is necessary so
2550 * that inputs / outputs of separable shaders will be assigned
2551 * predictable locations regardless of the order in which declarations
2552 * appeared in the shader source.
2554 * 2. Assign locations for any matching inputs and outputs.
2556 * 3. Mark output variables in the producer that do not have locations as
2557 * not being outputs. This lets the optimizer eliminate them.
2559 * 4. Mark input variables in the consumer that do not have locations as
2560 * not being inputs. This lets the optimizer eliminate them.
2563 canonicalize_shader_io(consumer
->ir
, ir_var_shader_in
);
2566 canonicalize_shader_io(producer
->ir
, ir_var_shader_out
);
2569 linker::populate_consumer_input_sets(mem_ctx
, consumer
->ir
,
2571 consumer_interface_inputs
,
2572 consumer_inputs_with_locations
);
2575 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
2576 ir_variable
*const output_var
= node
->as_variable();
2578 if (output_var
== NULL
|| output_var
->data
.mode
!= ir_var_shader_out
)
2581 /* Only geometry shaders can use non-zero streams */
2582 assert(output_var
->data
.stream
== 0 ||
2583 (output_var
->data
.stream
< MAX_VERTEX_STREAMS
&&
2584 producer
->Stage
== MESA_SHADER_GEOMETRY
));
2586 if (num_tfeedback_decls
> 0) {
2587 tfeedback_candidate_generator
g(mem_ctx
, tfeedback_candidates
, producer
->Stage
);
2588 /* From OpenGL 4.6 (Core Profile) spec, section 11.1.2.1
2589 * ("Vertex Shader Variables / Output Variables")
2591 * "Each program object can specify a set of output variables from
2592 * one shader to be recorded in transform feedback mode (see
2593 * section 13.3). The variables that can be recorded are those
2594 * emitted by the first active shader, in order, from the
2598 * * tessellation evaluation shader
2599 * * tessellation control shader
2602 * But on OpenGL ES 3.2, section 11.1.2.1 ("Vertex Shader
2603 * Variables / Output Variables") tessellation control shader is
2604 * not included in the stages list.
2606 if (!prog
->IsES
|| producer
->Stage
!= MESA_SHADER_TESS_CTRL
) {
2607 g
.process(output_var
);
2611 ir_variable
*const input_var
=
2612 linker::get_matching_input(mem_ctx
, output_var
, consumer_inputs
,
2613 consumer_interface_inputs
,
2614 consumer_inputs_with_locations
);
2616 /* If a matching input variable was found, add this output (and the
2617 * input) to the set. If this is a separable program and there is no
2618 * consumer stage, add the output.
2620 * Always add TCS outputs. They are shared by all invocations
2621 * within a patch and can be used as shared memory.
2623 if (input_var
|| (prog
->SeparateShader
&& consumer
== NULL
) ||
2624 producer
->Stage
== MESA_SHADER_TESS_CTRL
) {
2625 matches
.record(output_var
, input_var
);
2628 /* Only stream 0 outputs can be consumed in the next stage */
2629 if (input_var
&& output_var
->data
.stream
!= 0) {
2630 linker_error(prog
, "output %s is assigned to stream=%d but "
2631 "is linked to an input, which requires stream=0",
2632 output_var
->name
, output_var
->data
.stream
);
2637 /* If there's no producer stage, then this must be a separable program.
2638 * For example, we may have a program that has just a fragment shader.
2639 * Later this program will be used with some arbitrary vertex (or
2640 * geometry) shader program. This means that locations must be assigned
2641 * for all the inputs.
2643 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
2644 ir_variable
*const input_var
= node
->as_variable();
2645 if (input_var
&& input_var
->data
.mode
== ir_var_shader_in
) {
2646 matches
.record(NULL
, input_var
);
2651 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
2652 if (!tfeedback_decls
[i
].is_varying())
2655 const tfeedback_candidate
*matched_candidate
2656 = tfeedback_decls
[i
].find_candidate(prog
, tfeedback_candidates
);
2658 if (matched_candidate
== NULL
) {
2659 _mesa_hash_table_destroy(tfeedback_candidates
, NULL
);
2663 /* Mark xfb varyings as always active */
2664 matched_candidate
->toplevel_var
->data
.always_active_io
= 1;
2666 /* Mark any corresponding inputs as always active also. We must do this
2667 * because we have a NIR pass that lowers vectors to scalars and another
2668 * that removes unused varyings.
2669 * We don't split varyings marked as always active because there is no
2670 * point in doing so. This means we need to mark both sides of the
2671 * interface as always active otherwise we will have a mismatch and
2672 * start removing things we shouldn't.
2674 ir_variable
*const input_var
=
2675 linker::get_matching_input(mem_ctx
, matched_candidate
->toplevel_var
,
2677 consumer_interface_inputs
,
2678 consumer_inputs_with_locations
);
2680 input_var
->data
.always_active_io
= 1;
2682 if (matched_candidate
->toplevel_var
->data
.is_unmatched_generic_inout
) {
2683 matched_candidate
->toplevel_var
->data
.is_xfb_only
= 1;
2684 matches
.record(matched_candidate
->toplevel_var
, NULL
);
2688 _mesa_hash_table_destroy(consumer_inputs
, NULL
);
2689 _mesa_hash_table_destroy(consumer_interface_inputs
, NULL
);
2691 uint8_t components
[MAX_VARYINGS_INCL_PATCH
] = {0};
2692 const unsigned slots_used
= matches
.assign_locations(
2693 prog
, components
, reserved_slots
);
2694 matches
.store_locations();
2696 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
2697 if (tfeedback_decls
[i
].is_varying()) {
2698 if (!tfeedback_decls
[i
].assign_location(ctx
, prog
)) {
2699 _mesa_hash_table_destroy(tfeedback_candidates
, NULL
);
2704 _mesa_hash_table_destroy(tfeedback_candidates
, NULL
);
2706 if (consumer
&& producer
) {
2707 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
2708 ir_variable
*const var
= node
->as_variable();
2710 if (var
&& var
->data
.mode
== ir_var_shader_in
&&
2711 var
->data
.is_unmatched_generic_inout
) {
2712 if (!prog
->IsES
&& prog
->data
->Version
<= 120) {
2713 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2715 * Only those varying variables used (i.e. read) in
2716 * the fragment shader executable must be written to
2717 * by the vertex shader executable; declaring
2718 * superfluous varying variables in a vertex shader is
2721 * We interpret this text as meaning that the VS must
2722 * write the variable for the FS to read it. See
2723 * "glsl1-varying read but not written" in piglit.
2725 linker_error(prog
, "%s shader varying %s not written "
2727 _mesa_shader_stage_to_string(consumer
->Stage
),
2729 _mesa_shader_stage_to_string(producer
->Stage
));
2731 linker_warning(prog
, "%s shader varying %s not written "
2733 _mesa_shader_stage_to_string(consumer
->Stage
),
2735 _mesa_shader_stage_to_string(producer
->Stage
));
2740 /* Now that validation is done its safe to remove unused varyings. As
2741 * we have both a producer and consumer its safe to remove unused
2742 * varyings even if the program is a SSO because the stages are being
2743 * linked together i.e. we have a multi-stage SSO.
2745 remove_unused_shader_inputs_and_outputs(false, producer
,
2747 remove_unused_shader_inputs_and_outputs(false, consumer
,
2752 lower_packed_varyings(mem_ctx
, slots_used
, components
, ir_var_shader_out
,
2753 0, producer
, disable_varying_packing
,
2758 lower_packed_varyings(mem_ctx
, slots_used
, components
, ir_var_shader_in
,
2759 consumer_vertices
, consumer
,
2760 disable_varying_packing
, xfb_enabled
);
2767 check_against_output_limit(struct gl_context
*ctx
,
2768 struct gl_shader_program
*prog
,
2769 gl_linked_shader
*producer
,
2770 unsigned num_explicit_locations
)
2772 unsigned output_vectors
= num_explicit_locations
;
2774 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
2775 ir_variable
*const var
= node
->as_variable();
2777 if (var
&& !var
->data
.explicit_location
&&
2778 var
->data
.mode
== ir_var_shader_out
&&
2779 var_counts_against_varying_limit(producer
->Stage
, var
)) {
2780 /* outputs for fragment shader can't be doubles */
2781 output_vectors
+= var
->type
->count_attribute_slots(false);
2785 assert(producer
->Stage
!= MESA_SHADER_FRAGMENT
);
2786 unsigned max_output_components
=
2787 ctx
->Const
.Program
[producer
->Stage
].MaxOutputComponents
;
2789 const unsigned output_components
= output_vectors
* 4;
2790 if (output_components
> max_output_components
) {
2791 if (ctx
->API
== API_OPENGLES2
|| prog
->IsES
)
2792 linker_error(prog
, "%s shader uses too many output vectors "
2794 _mesa_shader_stage_to_string(producer
->Stage
),
2796 max_output_components
/ 4);
2798 linker_error(prog
, "%s shader uses too many output components "
2800 _mesa_shader_stage_to_string(producer
->Stage
),
2802 max_output_components
);
2811 check_against_input_limit(struct gl_context
*ctx
,
2812 struct gl_shader_program
*prog
,
2813 gl_linked_shader
*consumer
,
2814 unsigned num_explicit_locations
)
2816 unsigned input_vectors
= num_explicit_locations
;
2818 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
2819 ir_variable
*const var
= node
->as_variable();
2821 if (var
&& !var
->data
.explicit_location
&&
2822 var
->data
.mode
== ir_var_shader_in
&&
2823 var_counts_against_varying_limit(consumer
->Stage
, var
)) {
2824 /* vertex inputs aren't varying counted */
2825 input_vectors
+= var
->type
->count_attribute_slots(false);
2829 assert(consumer
->Stage
!= MESA_SHADER_VERTEX
);
2830 unsigned max_input_components
=
2831 ctx
->Const
.Program
[consumer
->Stage
].MaxInputComponents
;
2833 const unsigned input_components
= input_vectors
* 4;
2834 if (input_components
> max_input_components
) {
2835 if (ctx
->API
== API_OPENGLES2
|| prog
->IsES
)
2836 linker_error(prog
, "%s shader uses too many input vectors "
2838 _mesa_shader_stage_to_string(consumer
->Stage
),
2840 max_input_components
/ 4);
2842 linker_error(prog
, "%s shader uses too many input components "
2844 _mesa_shader_stage_to_string(consumer
->Stage
),
2846 max_input_components
);
2855 link_varyings(struct gl_shader_program
*prog
, unsigned first
, unsigned last
,
2856 struct gl_context
*ctx
, void *mem_ctx
)
2858 bool has_xfb_qualifiers
= false;
2859 unsigned num_tfeedback_decls
= 0;
2860 char **varying_names
= NULL
;
2861 tfeedback_decl
*tfeedback_decls
= NULL
;
2863 /* From the ARB_enhanced_layouts spec:
2865 * "If the shader used to record output variables for transform feedback
2866 * varyings uses the "xfb_buffer", "xfb_offset", or "xfb_stride" layout
2867 * qualifiers, the values specified by TransformFeedbackVaryings are
2868 * ignored, and the set of variables captured for transform feedback is
2869 * instead derived from the specified layout qualifiers."
2871 for (int i
= MESA_SHADER_FRAGMENT
- 1; i
>= 0; i
--) {
2872 /* Find last stage before fragment shader */
2873 if (prog
->_LinkedShaders
[i
]) {
2874 has_xfb_qualifiers
=
2875 process_xfb_layout_qualifiers(mem_ctx
, prog
->_LinkedShaders
[i
],
2876 prog
, &num_tfeedback_decls
,
2882 if (!has_xfb_qualifiers
) {
2883 num_tfeedback_decls
= prog
->TransformFeedback
.NumVarying
;
2884 varying_names
= prog
->TransformFeedback
.VaryingNames
;
2887 if (num_tfeedback_decls
!= 0) {
2888 /* From GL_EXT_transform_feedback:
2889 * A program will fail to link if:
2891 * * the <count> specified by TransformFeedbackVaryingsEXT is
2892 * non-zero, but the program object has no vertex or geometry
2895 if (first
>= MESA_SHADER_FRAGMENT
) {
2896 linker_error(prog
, "Transform feedback varyings specified, but "
2897 "no vertex, tessellation, or geometry shader is "
2902 tfeedback_decls
= rzalloc_array(mem_ctx
, tfeedback_decl
,
2903 num_tfeedback_decls
);
2904 if (!parse_tfeedback_decls(ctx
, prog
, mem_ctx
, num_tfeedback_decls
,
2905 varying_names
, tfeedback_decls
))
2909 /* If there is no fragment shader we need to set transform feedback.
2911 * For SSO we also need to assign output locations. We assign them here
2912 * because we need to do it for both single stage programs and multi stage
2915 if (last
< MESA_SHADER_FRAGMENT
&&
2916 (num_tfeedback_decls
!= 0 || prog
->SeparateShader
)) {
2917 const uint64_t reserved_out_slots
=
2918 reserved_varying_slot(prog
->_LinkedShaders
[last
], ir_var_shader_out
);
2919 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
2920 prog
->_LinkedShaders
[last
], NULL
,
2921 num_tfeedback_decls
, tfeedback_decls
,
2922 reserved_out_slots
))
2926 if (last
<= MESA_SHADER_FRAGMENT
) {
2927 /* Remove unused varyings from the first/last stage unless SSO */
2928 remove_unused_shader_inputs_and_outputs(prog
->SeparateShader
,
2929 prog
->_LinkedShaders
[first
],
2931 remove_unused_shader_inputs_and_outputs(prog
->SeparateShader
,
2932 prog
->_LinkedShaders
[last
],
2935 /* If the program is made up of only a single stage */
2936 if (first
== last
) {
2937 gl_linked_shader
*const sh
= prog
->_LinkedShaders
[last
];
2939 do_dead_builtin_varyings(ctx
, NULL
, sh
, 0, NULL
);
2940 do_dead_builtin_varyings(ctx
, sh
, NULL
, num_tfeedback_decls
,
2943 if (prog
->SeparateShader
) {
2944 const uint64_t reserved_slots
=
2945 reserved_varying_slot(sh
, ir_var_shader_in
);
2947 /* Assign input locations for SSO, output locations are already
2950 if (!assign_varying_locations(ctx
, mem_ctx
, prog
,
2951 NULL
/* producer */,
2953 0 /* num_tfeedback_decls */,
2954 NULL
/* tfeedback_decls */,
2959 /* Linking the stages in the opposite order (from fragment to vertex)
2960 * ensures that inter-shader outputs written to in an earlier stage
2961 * are eliminated if they are (transitively) not used in a later
2965 for (int i
= next
- 1; i
>= 0; i
--) {
2966 if (prog
->_LinkedShaders
[i
] == NULL
&& i
!= 0)
2969 gl_linked_shader
*const sh_i
= prog
->_LinkedShaders
[i
];
2970 gl_linked_shader
*const sh_next
= prog
->_LinkedShaders
[next
];
2972 const uint64_t reserved_out_slots
=
2973 reserved_varying_slot(sh_i
, ir_var_shader_out
);
2974 const uint64_t reserved_in_slots
=
2975 reserved_varying_slot(sh_next
, ir_var_shader_in
);
2977 do_dead_builtin_varyings(ctx
, sh_i
, sh_next
,
2978 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
2981 if (!assign_varying_locations(ctx
, mem_ctx
, prog
, sh_i
, sh_next
,
2982 next
== MESA_SHADER_FRAGMENT
? num_tfeedback_decls
: 0,
2984 reserved_out_slots
| reserved_in_slots
))
2987 /* This must be done after all dead varyings are eliminated. */
2989 unsigned slots_used
= util_bitcount64(reserved_out_slots
);
2990 if (!check_against_output_limit(ctx
, prog
, sh_i
, slots_used
)) {
2995 unsigned slots_used
= util_bitcount64(reserved_in_slots
);
2996 if (!check_against_input_limit(ctx
, prog
, sh_next
, slots_used
))
3004 if (!store_tfeedback_info(ctx
, prog
, num_tfeedback_decls
, tfeedback_decls
,
3005 has_xfb_qualifiers
))