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/mtypes.h"
33 #include "glsl_symbol_table.h"
34 #include "ir_optimization.h"
36 #include "link_varyings.h"
37 #include "main/macros.h"
38 #include "program/hash_table.h"
43 * Validate that outputs from one stage match inputs of another
46 cross_validate_outputs_to_inputs(struct gl_shader_program
*prog
,
47 gl_shader
*producer
, gl_shader
*consumer
)
49 glsl_symbol_table parameters
;
50 /* FINISHME: Figure these out dynamically. */
51 const char *const producer_stage
= "vertex";
52 const char *const consumer_stage
= "fragment";
54 /* Find all shader outputs in the "producer" stage.
56 foreach_list(node
, producer
->ir
) {
57 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
59 if ((var
== NULL
) || (var
->mode
!= ir_var_shader_out
))
62 parameters
.add_variable(var
);
66 /* Find all shader inputs in the "consumer" stage. Any variables that have
67 * matching outputs already in the symbol table must have the same type and
70 foreach_list(node
, consumer
->ir
) {
71 ir_variable
*const input
= ((ir_instruction
*) node
)->as_variable();
73 if ((input
== NULL
) || (input
->mode
!= ir_var_shader_in
))
76 ir_variable
*const output
= parameters
.get_variable(input
->name
);
78 /* Check that the types match between stages.
80 if (input
->type
!= output
->type
) {
81 /* There is a bit of a special case for gl_TexCoord. This
82 * built-in is unsized by default. Applications that variable
83 * access it must redeclare it with a size. There is some
84 * language in the GLSL spec that implies the fragment shader
85 * and vertex shader do not have to agree on this size. Other
86 * driver behave this way, and one or two applications seem to
89 * Neither declaration needs to be modified here because the array
90 * sizes are fixed later when update_array_sizes is called.
92 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
94 * "Unlike user-defined varying variables, the built-in
95 * varying variables don't have a strict one-to-one
96 * correspondence between the vertex language and the
99 if (!output
->type
->is_array()
100 || (strncmp("gl_", output
->name
, 3) != 0)) {
102 "%s shader output `%s' declared as type `%s', "
103 "but %s shader input declared as type `%s'\n",
104 producer_stage
, output
->name
,
106 consumer_stage
, input
->type
->name
);
111 /* Check that all of the qualifiers match between stages.
113 if (input
->centroid
!= output
->centroid
) {
115 "%s shader output `%s' %s centroid qualifier, "
116 "but %s shader input %s centroid qualifier\n",
119 (output
->centroid
) ? "has" : "lacks",
121 (input
->centroid
) ? "has" : "lacks");
125 if (input
->invariant
!= output
->invariant
) {
127 "%s shader output `%s' %s invariant qualifier, "
128 "but %s shader input %s invariant qualifier\n",
131 (output
->invariant
) ? "has" : "lacks",
133 (input
->invariant
) ? "has" : "lacks");
137 if (input
->interpolation
!= output
->interpolation
) {
139 "%s shader output `%s' specifies %s "
140 "interpolation qualifier, "
141 "but %s shader input specifies %s "
142 "interpolation qualifier\n",
145 output
->interpolation_string(),
147 input
->interpolation_string());
158 * Initialize this object based on a string that was passed to
159 * glTransformFeedbackVaryings.
161 * If the input is mal-formed, this call still succeeds, but it sets
162 * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var()
163 * will fail to find any matching variable.
166 tfeedback_decl::init(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
167 const void *mem_ctx
, const char *input
)
169 /* We don't have to be pedantic about what is a valid GLSL variable name,
170 * because any variable with an invalid name can't exist in the IR anyway.
174 this->orig_name
= input
;
175 this->is_clip_distance_mesa
= false;
176 this->skip_components
= 0;
177 this->next_buffer_separator
= false;
178 this->matched_candidate
= NULL
;
180 if (ctx
->Extensions
.ARB_transform_feedback3
) {
181 /* Parse gl_NextBuffer. */
182 if (strcmp(input
, "gl_NextBuffer") == 0) {
183 this->next_buffer_separator
= true;
187 /* Parse gl_SkipComponents. */
188 if (strcmp(input
, "gl_SkipComponents1") == 0)
189 this->skip_components
= 1;
190 else if (strcmp(input
, "gl_SkipComponents2") == 0)
191 this->skip_components
= 2;
192 else if (strcmp(input
, "gl_SkipComponents3") == 0)
193 this->skip_components
= 3;
194 else if (strcmp(input
, "gl_SkipComponents4") == 0)
195 this->skip_components
= 4;
197 if (this->skip_components
)
201 /* Parse a declaration. */
202 const char *base_name_end
;
203 long subscript
= parse_program_resource_name(input
, &base_name_end
);
204 this->var_name
= ralloc_strndup(mem_ctx
, input
, base_name_end
- input
);
205 if (subscript
>= 0) {
206 this->array_subscript
= subscript
;
207 this->is_subscripted
= true;
209 this->is_subscripted
= false;
212 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
213 * class must behave specially to account for the fact that gl_ClipDistance
214 * is converted from a float[8] to a vec4[2].
216 if (ctx
->ShaderCompilerOptions
[MESA_SHADER_VERTEX
].LowerClipDistance
&&
217 strcmp(this->var_name
, "gl_ClipDistance") == 0) {
218 this->is_clip_distance_mesa
= true;
224 * Determine whether two tfeedback_decl objects refer to the same variable and
225 * array index (if applicable).
228 tfeedback_decl::is_same(const tfeedback_decl
&x
, const tfeedback_decl
&y
)
230 assert(x
.is_varying() && y
.is_varying());
232 if (strcmp(x
.var_name
, y
.var_name
) != 0)
234 if (x
.is_subscripted
!= y
.is_subscripted
)
236 if (x
.is_subscripted
&& x
.array_subscript
!= y
.array_subscript
)
243 * Assign a location for this tfeedback_decl object based on the transform
244 * feedback candidate found by find_candidate.
246 * If an error occurs, the error is reported through linker_error() and false
250 tfeedback_decl::assign_location(struct gl_context
*ctx
,
251 struct gl_shader_program
*prog
)
253 assert(this->is_varying());
255 unsigned fine_location
256 = this->matched_candidate
->toplevel_var
->location
* 4
257 + this->matched_candidate
->toplevel_var
->location_frac
258 + this->matched_candidate
->offset
;
260 if (this->matched_candidate
->type
->is_array()) {
262 const unsigned matrix_cols
=
263 this->matched_candidate
->type
->fields
.array
->matrix_columns
;
264 const unsigned vector_elements
=
265 this->matched_candidate
->type
->fields
.array
->vector_elements
;
266 unsigned actual_array_size
= this->is_clip_distance_mesa
?
267 prog
->Vert
.ClipDistanceArraySize
:
268 this->matched_candidate
->type
->array_size();
270 if (this->is_subscripted
) {
271 /* Check array bounds. */
272 if (this->array_subscript
>= actual_array_size
) {
273 linker_error(prog
, "Transform feedback varying %s has index "
274 "%i, but the array size is %u.",
275 this->orig_name
, this->array_subscript
,
279 unsigned array_elem_size
= this->is_clip_distance_mesa
?
280 1 : vector_elements
* matrix_cols
;
281 fine_location
+= array_elem_size
* this->array_subscript
;
284 this->size
= actual_array_size
;
286 this->vector_elements
= vector_elements
;
287 this->matrix_columns
= matrix_cols
;
288 if (this->is_clip_distance_mesa
)
289 this->type
= GL_FLOAT
;
291 this->type
= this->matched_candidate
->type
->fields
.array
->gl_type
;
293 /* Regular variable (scalar, vector, or matrix) */
294 if (this->is_subscripted
) {
295 linker_error(prog
, "Transform feedback varying %s requested, "
296 "but %s is not an array.",
297 this->orig_name
, this->var_name
);
301 this->vector_elements
= this->matched_candidate
->type
->vector_elements
;
302 this->matrix_columns
= this->matched_candidate
->type
->matrix_columns
;
303 this->type
= this->matched_candidate
->type
->gl_type
;
305 this->location
= fine_location
/ 4;
306 this->location_frac
= fine_location
% 4;
308 /* From GL_EXT_transform_feedback:
309 * A program will fail to link if:
311 * * the total number of components to capture in any varying
312 * variable in <varyings> is greater than the constant
313 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
314 * buffer mode is SEPARATE_ATTRIBS_EXT;
316 if (prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
&&
317 this->num_components() >
318 ctx
->Const
.MaxTransformFeedbackSeparateComponents
) {
319 linker_error(prog
, "Transform feedback varying %s exceeds "
320 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
330 tfeedback_decl::get_num_outputs() const
332 if (!this->is_varying()) {
336 return (this->num_components() + this->location_frac
+ 3)/4;
341 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
343 * If an error occurs, the error is reported through linker_error() and false
347 tfeedback_decl::store(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
348 struct gl_transform_feedback_info
*info
,
349 unsigned buffer
, const unsigned max_outputs
) const
351 assert(!this->next_buffer_separator
);
353 /* Handle gl_SkipComponents. */
354 if (this->skip_components
) {
355 info
->BufferStride
[buffer
] += this->skip_components
;
359 /* From GL_EXT_transform_feedback:
360 * A program will fail to link if:
362 * * the total number of components to capture is greater than
363 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
364 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
366 if (prog
->TransformFeedback
.BufferMode
== GL_INTERLEAVED_ATTRIBS
&&
367 info
->BufferStride
[buffer
] + this->num_components() >
368 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
369 linker_error(prog
, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
370 "limit has been exceeded.");
374 unsigned location
= this->location
;
375 unsigned location_frac
= this->location_frac
;
376 unsigned num_components
= this->num_components();
377 while (num_components
> 0) {
378 unsigned output_size
= MIN2(num_components
, 4 - location_frac
);
379 assert(info
->NumOutputs
< max_outputs
);
380 info
->Outputs
[info
->NumOutputs
].ComponentOffset
= location_frac
;
381 info
->Outputs
[info
->NumOutputs
].OutputRegister
= location
;
382 info
->Outputs
[info
->NumOutputs
].NumComponents
= output_size
;
383 info
->Outputs
[info
->NumOutputs
].OutputBuffer
= buffer
;
384 info
->Outputs
[info
->NumOutputs
].DstOffset
= info
->BufferStride
[buffer
];
386 info
->BufferStride
[buffer
] += output_size
;
387 num_components
-= output_size
;
392 info
->Varyings
[info
->NumVarying
].Name
= ralloc_strdup(prog
, this->orig_name
);
393 info
->Varyings
[info
->NumVarying
].Type
= this->type
;
394 info
->Varyings
[info
->NumVarying
].Size
= this->size
;
401 const tfeedback_candidate
*
402 tfeedback_decl::find_candidate(gl_shader_program
*prog
,
403 hash_table
*tfeedback_candidates
)
405 const char *name
= this->is_clip_distance_mesa
406 ? "gl_ClipDistanceMESA" : this->var_name
;
407 this->matched_candidate
= (const tfeedback_candidate
*)
408 hash_table_find(tfeedback_candidates
, name
);
409 if (!this->matched_candidate
) {
410 /* From GL_EXT_transform_feedback:
411 * A program will fail to link if:
413 * * any variable name specified in the <varyings> array is not
414 * declared as an output in the geometry shader (if present) or
415 * the vertex shader (if no geometry shader is present);
417 linker_error(prog
, "Transform feedback varying %s undeclared.",
420 return this->matched_candidate
;
425 * Parse all the transform feedback declarations that were passed to
426 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
428 * If an error occurs, the error is reported through linker_error() and false
432 parse_tfeedback_decls(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
433 const void *mem_ctx
, unsigned num_names
,
434 char **varying_names
, tfeedback_decl
*decls
)
436 for (unsigned i
= 0; i
< num_names
; ++i
) {
437 decls
[i
].init(ctx
, prog
, mem_ctx
, varying_names
[i
]);
439 if (!decls
[i
].is_varying())
442 /* From GL_EXT_transform_feedback:
443 * A program will fail to link if:
445 * * any two entries in the <varyings> array specify the same varying
448 * We interpret this to mean "any two entries in the <varyings> array
449 * specify the same varying variable and array index", since transform
450 * feedback of arrays would be useless otherwise.
452 for (unsigned j
= 0; j
< i
; ++j
) {
453 if (!decls
[j
].is_varying())
456 if (tfeedback_decl::is_same(decls
[i
], decls
[j
])) {
457 linker_error(prog
, "Transform feedback varying %s specified "
458 "more than once.", varying_names
[i
]);
468 * Store transform feedback location assignments into
469 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
471 * If an error occurs, the error is reported through linker_error() and false
475 store_tfeedback_info(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
476 unsigned num_tfeedback_decls
,
477 tfeedback_decl
*tfeedback_decls
)
479 bool separate_attribs_mode
=
480 prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
;
482 ralloc_free(prog
->LinkedTransformFeedback
.Varyings
);
483 ralloc_free(prog
->LinkedTransformFeedback
.Outputs
);
485 memset(&prog
->LinkedTransformFeedback
, 0,
486 sizeof(prog
->LinkedTransformFeedback
));
488 prog
->LinkedTransformFeedback
.Varyings
=
490 struct gl_transform_feedback_varying_info
,
491 num_tfeedback_decls
);
493 unsigned num_outputs
= 0;
494 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
)
495 num_outputs
+= tfeedback_decls
[i
].get_num_outputs();
497 prog
->LinkedTransformFeedback
.Outputs
=
499 struct gl_transform_feedback_output
,
502 unsigned num_buffers
= 0;
504 if (separate_attribs_mode
) {
505 /* GL_SEPARATE_ATTRIBS */
506 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
507 if (!tfeedback_decls
[i
].store(ctx
, prog
, &prog
->LinkedTransformFeedback
,
508 num_buffers
, num_outputs
))
515 /* GL_INVERLEAVED_ATTRIBS */
516 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
517 if (tfeedback_decls
[i
].is_next_buffer_separator()) {
522 if (!tfeedback_decls
[i
].store(ctx
, prog
,
523 &prog
->LinkedTransformFeedback
,
524 num_buffers
, num_outputs
))
530 assert(prog
->LinkedTransformFeedback
.NumOutputs
== num_outputs
);
532 prog
->LinkedTransformFeedback
.NumBuffers
= num_buffers
;
538 * Data structure recording the relationship between outputs of one shader
539 * stage (the "producer") and inputs of another (the "consumer").
541 class varying_matches
544 varying_matches(bool disable_varying_packing
);
546 void record(ir_variable
*producer_var
, ir_variable
*consumer_var
);
547 unsigned assign_locations();
548 void store_locations(unsigned producer_base
, unsigned consumer_base
) const;
552 * If true, this driver disables varying packing, so all varyings need to
553 * be aligned on slot boundaries, and take up a number of slots equal to
554 * their number of matrix columns times their array size.
556 const bool disable_varying_packing
;
559 * Enum representing the order in which varyings are packed within a
562 * Currently we pack vec4's first, then vec2's, then scalar values, then
563 * vec3's. This order ensures that the only vectors that are at risk of
564 * having to be "double parked" (split between two adjacent varying slots)
567 enum packing_order_enum
{
570 PACKING_ORDER_SCALAR
,
574 static unsigned compute_packing_class(ir_variable
*var
);
575 static packing_order_enum
compute_packing_order(ir_variable
*var
);
576 static int match_comparator(const void *x_generic
, const void *y_generic
);
579 * Structure recording the relationship between a single producer output
580 * and a single consumer input.
584 * Packing class for this varying, computed by compute_packing_class().
586 unsigned packing_class
;
589 * Packing order for this varying, computed by compute_packing_order().
591 packing_order_enum packing_order
;
592 unsigned num_components
;
595 * The output variable in the producer stage.
597 ir_variable
*producer_var
;
600 * The input variable in the consumer stage.
602 ir_variable
*consumer_var
;
605 * The location which has been assigned for this varying. This is
606 * expressed in multiples of a float, with the first generic varying
607 * (i.e. the one referred to by VARYING_SLOT_VAR0) represented by the
610 unsigned generic_location
;
614 * The number of elements in the \c matches array that are currently in
617 unsigned num_matches
;
620 * The number of elements that were set aside for the \c matches array when
623 unsigned matches_capacity
;
627 varying_matches::varying_matches(bool disable_varying_packing
)
628 : disable_varying_packing(disable_varying_packing
)
630 /* Note: this initial capacity is rather arbitrarily chosen to be large
631 * enough for many cases without wasting an unreasonable amount of space.
632 * varying_matches::record() will resize the array if there are more than
633 * this number of varyings.
635 this->matches_capacity
= 8;
636 this->matches
= (match
*)
637 malloc(sizeof(*this->matches
) * this->matches_capacity
);
638 this->num_matches
= 0;
642 varying_matches::~varying_matches()
649 * Record the given producer/consumer variable pair in the list of variables
650 * that should later be assigned locations.
652 * It is permissible for \c consumer_var to be NULL (this happens if a
653 * variable is output by the producer and consumed by transform feedback, but
654 * not consumed by the consumer).
656 * If \c producer_var has already been paired up with a consumer_var, or
657 * producer_var is part of fixed pipeline functionality (and hence already has
658 * a location assigned), this function has no effect.
661 varying_matches::record(ir_variable
*producer_var
, ir_variable
*consumer_var
)
663 if (!producer_var
->is_unmatched_generic_inout
) {
664 /* Either a location already exists for this variable (since it is part
665 * of fixed functionality), or it has already been recorded as part of a
671 if (this->num_matches
== this->matches_capacity
) {
672 this->matches_capacity
*= 2;
673 this->matches
= (match
*)
674 realloc(this->matches
,
675 sizeof(*this->matches
) * this->matches_capacity
);
677 this->matches
[this->num_matches
].packing_class
678 = this->compute_packing_class(producer_var
);
679 this->matches
[this->num_matches
].packing_order
680 = this->compute_packing_order(producer_var
);
681 if (this->disable_varying_packing
) {
682 unsigned slots
= producer_var
->type
->is_array()
683 ? (producer_var
->type
->length
684 * producer_var
->type
->fields
.array
->matrix_columns
)
685 : producer_var
->type
->matrix_columns
;
686 this->matches
[this->num_matches
].num_components
= 4 * slots
;
688 this->matches
[this->num_matches
].num_components
689 = producer_var
->type
->component_slots();
691 this->matches
[this->num_matches
].producer_var
= producer_var
;
692 this->matches
[this->num_matches
].consumer_var
= consumer_var
;
694 producer_var
->is_unmatched_generic_inout
= 0;
696 consumer_var
->is_unmatched_generic_inout
= 0;
701 * Choose locations for all of the variable matches that were previously
702 * passed to varying_matches::record().
705 varying_matches::assign_locations()
707 /* Sort varying matches into an order that makes them easy to pack. */
708 qsort(this->matches
, this->num_matches
, sizeof(*this->matches
),
709 &varying_matches::match_comparator
);
711 unsigned generic_location
= 0;
713 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
714 /* Advance to the next slot if this varying has a different packing
715 * class than the previous one, and we're not already on a slot
719 this->matches
[i
- 1].packing_class
720 != this->matches
[i
].packing_class
) {
721 generic_location
= ALIGN(generic_location
, 4);
724 this->matches
[i
].generic_location
= generic_location
;
726 generic_location
+= this->matches
[i
].num_components
;
729 return (generic_location
+ 3) / 4;
734 * Update the producer and consumer shaders to reflect the locations
735 * assignments that were made by varying_matches::assign_locations().
738 varying_matches::store_locations(unsigned producer_base
,
739 unsigned consumer_base
) const
741 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
742 ir_variable
*producer_var
= this->matches
[i
].producer_var
;
743 ir_variable
*consumer_var
= this->matches
[i
].consumer_var
;
744 unsigned generic_location
= this->matches
[i
].generic_location
;
745 unsigned slot
= generic_location
/ 4;
746 unsigned offset
= generic_location
% 4;
748 producer_var
->location
= producer_base
+ slot
;
749 producer_var
->location_frac
= offset
;
751 assert(consumer_var
->location
== -1);
752 consumer_var
->location
= consumer_base
+ slot
;
753 consumer_var
->location_frac
= offset
;
760 * Compute the "packing class" of the given varying. This is an unsigned
761 * integer with the property that two variables in the same packing class can
762 * be safely backed into the same vec4.
765 varying_matches::compute_packing_class(ir_variable
*var
)
767 /* Without help from the back-end, there is no way to pack together
768 * variables with different interpolation types, because
769 * lower_packed_varyings must choose exactly one interpolation type for
770 * each packed varying it creates.
772 * However, we can safely pack together floats, ints, and uints, because:
774 * - varyings of base type "int" and "uint" must use the "flat"
775 * interpolation type, which can only occur in GLSL 1.30 and above.
777 * - On platforms that support GLSL 1.30 and above, lower_packed_varyings
778 * can store flat floats as ints without losing any information (using
779 * the ir_unop_bitcast_* opcodes).
781 * Therefore, the packing class depends only on the interpolation type.
783 unsigned packing_class
= var
->centroid
? 1 : 0;
785 packing_class
+= var
->interpolation
;
786 return packing_class
;
791 * Compute the "packing order" of the given varying. This is a sort key we
792 * use to determine when to attempt to pack the given varying relative to
793 * other varyings in the same packing class.
795 varying_matches::packing_order_enum
796 varying_matches::compute_packing_order(ir_variable
*var
)
798 const glsl_type
*element_type
= var
->type
;
800 while (element_type
->base_type
== GLSL_TYPE_ARRAY
) {
801 element_type
= element_type
->fields
.array
;
804 switch (element_type
->component_slots() % 4) {
805 case 1: return PACKING_ORDER_SCALAR
;
806 case 2: return PACKING_ORDER_VEC2
;
807 case 3: return PACKING_ORDER_VEC3
;
808 case 0: return PACKING_ORDER_VEC4
;
810 assert(!"Unexpected value of vector_elements");
811 return PACKING_ORDER_VEC4
;
817 * Comparison function passed to qsort() to sort varyings by packing_class and
818 * then by packing_order.
821 varying_matches::match_comparator(const void *x_generic
, const void *y_generic
)
823 const match
*x
= (const match
*) x_generic
;
824 const match
*y
= (const match
*) y_generic
;
826 if (x
->packing_class
!= y
->packing_class
)
827 return x
->packing_class
- y
->packing_class
;
828 return x
->packing_order
- y
->packing_order
;
833 * Is the given variable a varying variable to be counted against the
834 * limit in ctx->Const.MaxVarying?
835 * This includes variables such as texcoords, colors and generic
836 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
839 is_varying_var(GLenum shaderType
, const ir_variable
*var
)
841 /* Only fragment shaders will take a varying variable as an input */
842 if (shaderType
== GL_FRAGMENT_SHADER
&&
843 var
->mode
== ir_var_shader_in
) {
844 switch (var
->location
) {
845 case VARYING_SLOT_POS
:
846 case VARYING_SLOT_FACE
:
847 case VARYING_SLOT_PNTC
:
858 * Visitor class that generates tfeedback_candidate structs describing all
859 * possible targets of transform feedback.
861 * tfeedback_candidate structs are stored in the hash table
862 * tfeedback_candidates, which is passed to the constructor. This hash table
863 * maps varying names to instances of the tfeedback_candidate struct.
865 class tfeedback_candidate_generator
: public program_resource_visitor
868 tfeedback_candidate_generator(void *mem_ctx
,
869 hash_table
*tfeedback_candidates
)
871 tfeedback_candidates(tfeedback_candidates
),
877 void process(ir_variable
*var
)
879 this->toplevel_var
= var
;
880 this->varying_floats
= 0;
881 if (var
->is_interface_instance())
882 program_resource_visitor::process(var
->interface_type
,
883 var
->interface_type
->name
);
885 program_resource_visitor::process(var
);
889 virtual void visit_field(const glsl_type
*type
, const char *name
,
892 assert(!type
->is_record());
893 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
894 assert(!type
->is_interface());
895 assert(!(type
->is_array() && type
->fields
.array
->is_interface()));
899 tfeedback_candidate
*candidate
900 = rzalloc(this->mem_ctx
, tfeedback_candidate
);
901 candidate
->toplevel_var
= this->toplevel_var
;
902 candidate
->type
= type
;
903 candidate
->offset
= this->varying_floats
;
904 hash_table_insert(this->tfeedback_candidates
, candidate
,
905 ralloc_strdup(this->mem_ctx
, name
));
906 this->varying_floats
+= type
->component_slots();
910 * Memory context used to allocate hash table keys and values.
912 void * const mem_ctx
;
915 * Hash table in which tfeedback_candidate objects should be stored.
917 hash_table
* const tfeedback_candidates
;
920 * Pointer to the toplevel variable that is being traversed.
922 ir_variable
*toplevel_var
;
925 * Total number of varying floats that have been visited so far. This is
926 * used to determine the offset to each varying within the toplevel
929 unsigned varying_floats
;
934 * Assign locations for all variables that are produced in one pipeline stage
935 * (the "producer") and consumed in the next stage (the "consumer").
937 * Variables produced by the producer may also be consumed by transform
940 * \param num_tfeedback_decls is the number of declarations indicating
941 * variables that may be consumed by transform feedback.
943 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
944 * representing the result of parsing the strings passed to
945 * glTransformFeedbackVaryings(). assign_location() will be called for
946 * each of these objects that matches one of the outputs of the
949 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
950 * be NULL. In this case, varying locations are assigned solely based on the
951 * requirements of transform feedback.
954 assign_varying_locations(struct gl_context
*ctx
,
956 struct gl_shader_program
*prog
,
957 gl_shader
*producer
, gl_shader
*consumer
,
958 unsigned num_tfeedback_decls
,
959 tfeedback_decl
*tfeedback_decls
)
961 const unsigned producer_base
= VARYING_SLOT_VAR0
;
962 const unsigned consumer_base
= VARYING_SLOT_VAR0
;
963 varying_matches
matches(ctx
->Const
.DisableVaryingPacking
);
964 hash_table
*tfeedback_candidates
965 = hash_table_ctor(0, hash_table_string_hash
, hash_table_string_compare
);
966 hash_table
*consumer_inputs
967 = hash_table_ctor(0, hash_table_string_hash
, hash_table_string_compare
);
969 /* Operate in a total of three passes.
971 * 1. Assign locations for any matching inputs and outputs.
973 * 2. Mark output variables in the producer that do not have locations as
974 * not being outputs. This lets the optimizer eliminate them.
976 * 3. Mark input variables in the consumer that do not have locations as
977 * not being inputs. This lets the optimizer eliminate them.
981 foreach_list(node
, consumer
->ir
) {
982 ir_variable
*const input_var
=
983 ((ir_instruction
*) node
)->as_variable();
985 if ((input_var
!= NULL
) && (input_var
->mode
== ir_var_shader_in
)) {
986 hash_table_insert(consumer_inputs
, input_var
,
987 ralloc_strdup(mem_ctx
, input_var
->name
));
992 foreach_list(node
, producer
->ir
) {
993 ir_variable
*const output_var
= ((ir_instruction
*) node
)->as_variable();
995 if ((output_var
== NULL
) || (output_var
->mode
!= ir_var_shader_out
))
998 tfeedback_candidate_generator
g(mem_ctx
, tfeedback_candidates
);
999 g
.process(output_var
);
1001 ir_variable
*input_var
=
1002 (ir_variable
*) hash_table_find(consumer_inputs
, output_var
->name
);
1004 if (input_var
&& input_var
->mode
!= ir_var_shader_in
)
1008 matches
.record(output_var
, input_var
);
1012 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1013 if (!tfeedback_decls
[i
].is_varying())
1016 const tfeedback_candidate
*matched_candidate
1017 = tfeedback_decls
[i
].find_candidate(prog
, tfeedback_candidates
);
1019 if (matched_candidate
== NULL
) {
1020 hash_table_dtor(tfeedback_candidates
);
1021 hash_table_dtor(consumer_inputs
);
1025 if (matched_candidate
->toplevel_var
->is_unmatched_generic_inout
)
1026 matches
.record(matched_candidate
->toplevel_var
, NULL
);
1029 const unsigned slots_used
= matches
.assign_locations();
1030 matches
.store_locations(producer_base
, consumer_base
);
1032 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1033 if (!tfeedback_decls
[i
].is_varying())
1036 if (!tfeedback_decls
[i
].assign_location(ctx
, prog
)) {
1037 hash_table_dtor(tfeedback_candidates
);
1038 hash_table_dtor(consumer_inputs
);
1043 hash_table_dtor(tfeedback_candidates
);
1044 hash_table_dtor(consumer_inputs
);
1046 if (ctx
->Const
.DisableVaryingPacking
) {
1047 /* Transform feedback code assumes varyings are packed, so if the driver
1048 * has disabled varying packing, make sure it does not support transform
1051 assert(!ctx
->Extensions
.EXT_transform_feedback
);
1053 lower_packed_varyings(mem_ctx
, producer_base
, slots_used
,
1054 ir_var_shader_out
, producer
);
1056 lower_packed_varyings(mem_ctx
, consumer_base
, slots_used
,
1057 ir_var_shader_in
, consumer
);
1061 unsigned varying_vectors
= 0;
1064 foreach_list(node
, consumer
->ir
) {
1065 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
1067 if ((var
== NULL
) || (var
->mode
!= ir_var_shader_in
))
1070 if (var
->is_unmatched_generic_inout
) {
1071 if (prog
->Version
<= 120) {
1072 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1074 * Only those varying variables used (i.e. read) in
1075 * the fragment shader executable must be written to
1076 * by the vertex shader executable; declaring
1077 * superfluous varying variables in a vertex shader is
1080 * We interpret this text as meaning that the VS must
1081 * write the variable for the FS to read it. See
1082 * "glsl1-varying read but not written" in piglit.
1085 linker_error(prog
, "fragment shader varying %s not written "
1086 "by vertex shader\n.", var
->name
);
1089 /* An 'in' variable is only really a shader input if its
1090 * value is written by the previous stage.
1092 var
->mode
= ir_var_auto
;
1093 } else if (is_varying_var(consumer
->Type
, var
)) {
1094 /* The packing rules are used for vertex shader inputs are also
1095 * used for fragment shader inputs.
1097 varying_vectors
+= count_attribute_slots(var
->type
);
1102 if (ctx
->API
== API_OPENGLES2
|| prog
->IsES
) {
1103 if (varying_vectors
> ctx
->Const
.MaxVarying
) {
1104 if (ctx
->Const
.GLSLSkipStrictMaxVaryingLimitCheck
) {
1105 linker_warning(prog
, "shader uses too many varying vectors "
1106 "(%u > %u), but the driver will try to optimize "
1107 "them out; this is non-portable out-of-spec "
1109 varying_vectors
, ctx
->Const
.MaxVarying
);
1111 linker_error(prog
, "shader uses too many varying vectors "
1113 varying_vectors
, ctx
->Const
.MaxVarying
);
1118 const unsigned float_components
= varying_vectors
* 4;
1119 if (float_components
> ctx
->Const
.MaxVarying
* 4) {
1120 if (ctx
->Const
.GLSLSkipStrictMaxVaryingLimitCheck
) {
1121 linker_warning(prog
, "shader uses too many varying components "
1122 "(%u > %u), but the driver will try to optimize "
1123 "them out; this is non-portable out-of-spec "
1125 float_components
, ctx
->Const
.MaxVarying
* 4);
1127 linker_error(prog
, "shader uses too many varying components "
1129 float_components
, ctx
->Const
.MaxVarying
* 4);