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 "glsl_parser_extras.h"
35 #include "ir_optimization.h"
37 #include "link_varyings.h"
38 #include "main/macros.h"
39 #include "program/hash_table.h"
44 * Validate the types and qualifiers of an output from one stage against the
45 * matching input to another stage.
48 cross_validate_types_and_qualifiers(struct gl_shader_program
*prog
,
49 const ir_variable
*input
,
50 const ir_variable
*output
,
51 gl_shader_stage consumer_stage
,
52 gl_shader_stage producer_stage
)
54 /* Check that the types match between stages.
56 const glsl_type
*type_to_match
= input
->type
;
58 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
59 const bool extra_array_level
= (producer_stage
== MESA_SHADER_VERTEX
&&
60 consumer_stage
!= MESA_SHADER_FRAGMENT
) ||
61 consumer_stage
== MESA_SHADER_GEOMETRY
;
62 if (extra_array_level
) {
63 assert(type_to_match
->is_array());
64 type_to_match
= type_to_match
->fields
.array
;
67 if (type_to_match
!= output
->type
) {
68 /* There is a bit of a special case for gl_TexCoord. This
69 * built-in is unsized by default. Applications that variable
70 * access it must redeclare it with a size. There is some
71 * language in the GLSL spec that implies the fragment shader
72 * and vertex shader do not have to agree on this size. Other
73 * driver behave this way, and one or two applications seem to
76 * Neither declaration needs to be modified here because the array
77 * sizes are fixed later when update_array_sizes is called.
79 * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
81 * "Unlike user-defined varying variables, the built-in
82 * varying variables don't have a strict one-to-one
83 * correspondence between the vertex language and the
86 if (!output
->type
->is_array() || !is_gl_identifier(output
->name
)) {
88 "%s shader output `%s' declared as type `%s', "
89 "but %s shader input declared as type `%s'\n",
90 _mesa_shader_stage_to_string(producer_stage
),
93 _mesa_shader_stage_to_string(consumer_stage
),
99 /* Check that all of the qualifiers match between stages.
101 if (input
->data
.centroid
!= output
->data
.centroid
) {
103 "%s shader output `%s' %s centroid qualifier, "
104 "but %s shader input %s centroid qualifier\n",
105 _mesa_shader_stage_to_string(producer_stage
),
107 (output
->data
.centroid
) ? "has" : "lacks",
108 _mesa_shader_stage_to_string(consumer_stage
),
109 (input
->data
.centroid
) ? "has" : "lacks");
113 if (input
->data
.sample
!= output
->data
.sample
) {
115 "%s shader output `%s' %s sample qualifier, "
116 "but %s shader input %s sample qualifier\n",
117 _mesa_shader_stage_to_string(producer_stage
),
119 (output
->data
.sample
) ? "has" : "lacks",
120 _mesa_shader_stage_to_string(consumer_stage
),
121 (input
->data
.sample
) ? "has" : "lacks");
125 if (input
->data
.patch
!= output
->data
.patch
) {
127 "%s shader output `%s' %s patch qualifier, "
128 "but %s shader input %s patch qualifier\n",
129 _mesa_shader_stage_to_string(producer_stage
),
131 (output
->data
.patch
) ? "has" : "lacks",
132 _mesa_shader_stage_to_string(consumer_stage
),
133 (input
->data
.patch
) ? "has" : "lacks");
137 if (!prog
->IsES
&& input
->data
.invariant
!= output
->data
.invariant
) {
139 "%s shader output `%s' %s invariant qualifier, "
140 "but %s shader input %s invariant qualifier\n",
141 _mesa_shader_stage_to_string(producer_stage
),
143 (output
->data
.invariant
) ? "has" : "lacks",
144 _mesa_shader_stage_to_string(consumer_stage
),
145 (input
->data
.invariant
) ? "has" : "lacks");
149 /* GLSL >= 4.40 removes text requiring interpolation qualifiers
150 * to match cross stage, they must only match within the same stage.
152 * From page 84 (page 90 of the PDF) of the GLSL 4.40 spec:
154 * "It is a link-time error if, within the same stage, the interpolation
155 * qualifiers of variables of the same name do not match.
158 if (input
->data
.interpolation
!= output
->data
.interpolation
&&
159 prog
->Version
< 440) {
161 "%s shader output `%s' specifies %s "
162 "interpolation qualifier, "
163 "but %s shader input specifies %s "
164 "interpolation qualifier\n",
165 _mesa_shader_stage_to_string(producer_stage
),
167 interpolation_string(output
->data
.interpolation
),
168 _mesa_shader_stage_to_string(consumer_stage
),
169 interpolation_string(input
->data
.interpolation
));
175 * Validate front and back color outputs against single color input
178 cross_validate_front_and_back_color(struct gl_shader_program
*prog
,
179 const ir_variable
*input
,
180 const ir_variable
*front_color
,
181 const ir_variable
*back_color
,
182 gl_shader_stage consumer_stage
,
183 gl_shader_stage producer_stage
)
185 if (front_color
!= NULL
&& front_color
->data
.assigned
)
186 cross_validate_types_and_qualifiers(prog
, input
, front_color
,
187 consumer_stage
, producer_stage
);
189 if (back_color
!= NULL
&& back_color
->data
.assigned
)
190 cross_validate_types_and_qualifiers(prog
, input
, back_color
,
191 consumer_stage
, producer_stage
);
195 * Validate that outputs from one stage match inputs of another
198 cross_validate_outputs_to_inputs(struct gl_shader_program
*prog
,
199 gl_shader
*producer
, gl_shader
*consumer
)
201 glsl_symbol_table parameters
;
202 ir_variable
*explicit_locations
[MAX_VARYING
] = { NULL
, };
204 /* Find all shader outputs in the "producer" stage.
206 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
207 ir_variable
*const var
= node
->as_variable();
209 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_shader_out
))
212 if (!var
->data
.explicit_location
213 || var
->data
.location
< VARYING_SLOT_VAR0
)
214 parameters
.add_variable(var
);
216 /* User-defined varyings with explicit locations are handled
217 * differently because they do not need to have matching names.
219 const unsigned idx
= var
->data
.location
- VARYING_SLOT_VAR0
;
221 if (explicit_locations
[idx
] != NULL
) {
223 "%s shader has multiple outputs explicitly "
224 "assigned to location %d\n",
225 _mesa_shader_stage_to_string(producer
->Stage
),
230 explicit_locations
[idx
] = var
;
235 /* Find all shader inputs in the "consumer" stage. Any variables that have
236 * matching outputs already in the symbol table must have the same type and
239 * Exception: if the consumer is the geometry shader, then the inputs
240 * should be arrays and the type of the array element should match the type
241 * of the corresponding producer output.
243 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
244 ir_variable
*const input
= node
->as_variable();
246 if ((input
== NULL
) || (input
->data
.mode
!= ir_var_shader_in
))
249 if (strcmp(input
->name
, "gl_Color") == 0 && input
->data
.used
) {
250 const ir_variable
*const front_color
=
251 parameters
.get_variable("gl_FrontColor");
253 const ir_variable
*const back_color
=
254 parameters
.get_variable("gl_BackColor");
256 cross_validate_front_and_back_color(prog
, input
,
257 front_color
, back_color
,
258 consumer
->Stage
, producer
->Stage
);
259 } else if (strcmp(input
->name
, "gl_SecondaryColor") == 0 && input
->data
.used
) {
260 const ir_variable
*const front_color
=
261 parameters
.get_variable("gl_FrontSecondaryColor");
263 const ir_variable
*const back_color
=
264 parameters
.get_variable("gl_BackSecondaryColor");
266 cross_validate_front_and_back_color(prog
, input
,
267 front_color
, back_color
,
268 consumer
->Stage
, producer
->Stage
);
270 /* The rules for connecting inputs and outputs change in the presence
271 * of explicit locations. In this case, we no longer care about the
272 * names of the variables. Instead, we care only about the
273 * explicitly assigned location.
275 ir_variable
*output
= NULL
;
276 if (input
->data
.explicit_location
277 && input
->data
.location
>= VARYING_SLOT_VAR0
) {
278 output
= explicit_locations
[input
->data
.location
- VARYING_SLOT_VAR0
];
280 if (output
== NULL
) {
282 "%s shader input `%s' with explicit location "
283 "has no matching output\n",
284 _mesa_shader_stage_to_string(consumer
->Stage
),
288 output
= parameters
.get_variable(input
->name
);
291 if (output
!= NULL
) {
292 cross_validate_types_and_qualifiers(prog
, input
, output
,
293 consumer
->Stage
, producer
->Stage
);
295 /* Check for input vars with unmatched output vars in prev stage
296 * taking into account that interface blocks could have a matching
297 * output but with different name, so we ignore them.
299 assert(!input
->data
.assigned
);
300 if (input
->data
.used
&& !input
->get_interface_type() &&
301 !input
->data
.explicit_location
&& !prog
->SeparateShader
)
303 "%s shader input `%s' "
304 "has no matching output in the previous stage\n",
305 _mesa_shader_stage_to_string(consumer
->Stage
),
314 * Initialize this object based on a string that was passed to
315 * glTransformFeedbackVaryings.
317 * If the input is mal-formed, this call still succeeds, but it sets
318 * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var()
319 * will fail to find any matching variable.
322 tfeedback_decl::init(struct gl_context
*ctx
, const void *mem_ctx
,
325 /* We don't have to be pedantic about what is a valid GLSL variable name,
326 * because any variable with an invalid name can't exist in the IR anyway.
330 this->orig_name
= input
;
331 this->lowered_builtin_array_variable
= none
;
332 this->skip_components
= 0;
333 this->next_buffer_separator
= false;
334 this->matched_candidate
= NULL
;
337 if (ctx
->Extensions
.ARB_transform_feedback3
) {
338 /* Parse gl_NextBuffer. */
339 if (strcmp(input
, "gl_NextBuffer") == 0) {
340 this->next_buffer_separator
= true;
344 /* Parse gl_SkipComponents. */
345 if (strcmp(input
, "gl_SkipComponents1") == 0)
346 this->skip_components
= 1;
347 else if (strcmp(input
, "gl_SkipComponents2") == 0)
348 this->skip_components
= 2;
349 else if (strcmp(input
, "gl_SkipComponents3") == 0)
350 this->skip_components
= 3;
351 else if (strcmp(input
, "gl_SkipComponents4") == 0)
352 this->skip_components
= 4;
354 if (this->skip_components
)
358 /* Parse a declaration. */
359 const char *base_name_end
;
360 long subscript
= parse_program_resource_name(input
, &base_name_end
);
361 this->var_name
= ralloc_strndup(mem_ctx
, input
, base_name_end
- input
);
362 if (this->var_name
== NULL
) {
363 _mesa_error_no_memory(__func__
);
367 if (subscript
>= 0) {
368 this->array_subscript
= subscript
;
369 this->is_subscripted
= true;
371 this->is_subscripted
= false;
374 /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
375 * class must behave specially to account for the fact that gl_ClipDistance
376 * is converted from a float[8] to a vec4[2].
378 if (ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].LowerClipDistance
&&
379 strcmp(this->var_name
, "gl_ClipDistance") == 0) {
380 this->lowered_builtin_array_variable
= clip_distance
;
383 if (ctx
->Const
.LowerTessLevel
&&
384 (strcmp(this->var_name
, "gl_TessLevelOuter") == 0))
385 this->lowered_builtin_array_variable
= tess_level_outer
;
386 if (ctx
->Const
.LowerTessLevel
&&
387 (strcmp(this->var_name
, "gl_TessLevelInner") == 0))
388 this->lowered_builtin_array_variable
= tess_level_inner
;
393 * Determine whether two tfeedback_decl objects refer to the same variable and
394 * array index (if applicable).
397 tfeedback_decl::is_same(const tfeedback_decl
&x
, const tfeedback_decl
&y
)
399 assert(x
.is_varying() && y
.is_varying());
401 if (strcmp(x
.var_name
, y
.var_name
) != 0)
403 if (x
.is_subscripted
!= y
.is_subscripted
)
405 if (x
.is_subscripted
&& x
.array_subscript
!= y
.array_subscript
)
412 * Assign a location and stream ID for this tfeedback_decl object based on the
413 * transform feedback candidate found by find_candidate.
415 * If an error occurs, the error is reported through linker_error() and false
419 tfeedback_decl::assign_location(struct gl_context
*ctx
,
420 struct gl_shader_program
*prog
)
422 assert(this->is_varying());
424 unsigned fine_location
425 = this->matched_candidate
->toplevel_var
->data
.location
* 4
426 + this->matched_candidate
->toplevel_var
->data
.location_frac
427 + this->matched_candidate
->offset
;
429 if (this->matched_candidate
->type
->is_array()) {
431 const unsigned matrix_cols
=
432 this->matched_candidate
->type
->fields
.array
->matrix_columns
;
433 const unsigned vector_elements
=
434 this->matched_candidate
->type
->fields
.array
->vector_elements
;
435 unsigned actual_array_size
;
436 switch (this->lowered_builtin_array_variable
) {
438 actual_array_size
= prog
->LastClipDistanceArraySize
;
440 case tess_level_outer
:
441 actual_array_size
= 4;
443 case tess_level_inner
:
444 actual_array_size
= 2;
448 actual_array_size
= this->matched_candidate
->type
->array_size();
452 if (this->is_subscripted
) {
453 /* Check array bounds. */
454 if (this->array_subscript
>= actual_array_size
) {
455 linker_error(prog
, "Transform feedback varying %s has index "
456 "%i, but the array size is %u.",
457 this->orig_name
, this->array_subscript
,
461 unsigned array_elem_size
= this->lowered_builtin_array_variable
?
462 1 : vector_elements
* matrix_cols
;
463 fine_location
+= array_elem_size
* this->array_subscript
;
466 this->size
= actual_array_size
;
468 this->vector_elements
= vector_elements
;
469 this->matrix_columns
= matrix_cols
;
470 if (this->lowered_builtin_array_variable
)
471 this->type
= GL_FLOAT
;
473 this->type
= this->matched_candidate
->type
->fields
.array
->gl_type
;
475 /* Regular variable (scalar, vector, or matrix) */
476 if (this->is_subscripted
) {
477 linker_error(prog
, "Transform feedback varying %s requested, "
478 "but %s is not an array.",
479 this->orig_name
, this->var_name
);
483 this->vector_elements
= this->matched_candidate
->type
->vector_elements
;
484 this->matrix_columns
= this->matched_candidate
->type
->matrix_columns
;
485 this->type
= this->matched_candidate
->type
->gl_type
;
487 this->location
= fine_location
/ 4;
488 this->location_frac
= fine_location
% 4;
490 /* From GL_EXT_transform_feedback:
491 * A program will fail to link if:
493 * * the total number of components to capture in any varying
494 * variable in <varyings> is greater than the constant
495 * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
496 * buffer mode is SEPARATE_ATTRIBS_EXT;
498 if (prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
&&
499 this->num_components() >
500 ctx
->Const
.MaxTransformFeedbackSeparateComponents
) {
501 linker_error(prog
, "Transform feedback varying %s exceeds "
502 "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
507 /* Only transform feedback varyings can be assigned to non-zero streams,
508 * so assign the stream id here.
510 this->stream_id
= this->matched_candidate
->toplevel_var
->data
.stream
;
517 tfeedback_decl::get_num_outputs() const
519 if (!this->is_varying()) {
523 return (this->num_components() + this->location_frac
+ 3)/4;
528 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
530 * If an error occurs, the error is reported through linker_error() and false
534 tfeedback_decl::store(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
535 struct gl_transform_feedback_info
*info
,
536 unsigned buffer
, const unsigned max_outputs
) const
538 assert(!this->next_buffer_separator
);
540 /* Handle gl_SkipComponents. */
541 if (this->skip_components
) {
542 info
->BufferStride
[buffer
] += this->skip_components
;
546 /* From GL_EXT_transform_feedback:
547 * A program will fail to link if:
549 * * the total number of components to capture is greater than
550 * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
551 * and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
553 if (prog
->TransformFeedback
.BufferMode
== GL_INTERLEAVED_ATTRIBS
&&
554 info
->BufferStride
[buffer
] + this->num_components() >
555 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
) {
556 linker_error(prog
, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
557 "limit has been exceeded.");
561 unsigned location
= this->location
;
562 unsigned location_frac
= this->location_frac
;
563 unsigned num_components
= this->num_components();
564 while (num_components
> 0) {
565 unsigned output_size
= MIN2(num_components
, 4 - location_frac
);
566 assert(info
->NumOutputs
< max_outputs
);
567 info
->Outputs
[info
->NumOutputs
].ComponentOffset
= location_frac
;
568 info
->Outputs
[info
->NumOutputs
].OutputRegister
= location
;
569 info
->Outputs
[info
->NumOutputs
].NumComponents
= output_size
;
570 info
->Outputs
[info
->NumOutputs
].StreamId
= stream_id
;
571 info
->Outputs
[info
->NumOutputs
].OutputBuffer
= buffer
;
572 info
->Outputs
[info
->NumOutputs
].DstOffset
= info
->BufferStride
[buffer
];
574 info
->BufferStride
[buffer
] += output_size
;
575 num_components
-= output_size
;
580 info
->Varyings
[info
->NumVarying
].Name
= ralloc_strdup(prog
, this->orig_name
);
581 info
->Varyings
[info
->NumVarying
].Type
= this->type
;
582 info
->Varyings
[info
->NumVarying
].Size
= this->size
;
589 const tfeedback_candidate
*
590 tfeedback_decl::find_candidate(gl_shader_program
*prog
,
591 hash_table
*tfeedback_candidates
)
593 const char *name
= this->var_name
;
594 switch (this->lowered_builtin_array_variable
) {
596 name
= this->var_name
;
599 name
= "gl_ClipDistanceMESA";
601 case tess_level_outer
:
602 name
= "gl_TessLevelOuterMESA";
604 case tess_level_inner
:
605 name
= "gl_TessLevelInnerMESA";
608 this->matched_candidate
= (const tfeedback_candidate
*)
609 hash_table_find(tfeedback_candidates
, name
);
610 if (!this->matched_candidate
) {
611 /* From GL_EXT_transform_feedback:
612 * A program will fail to link if:
614 * * any variable name specified in the <varyings> array is not
615 * declared as an output in the geometry shader (if present) or
616 * the vertex shader (if no geometry shader is present);
618 linker_error(prog
, "Transform feedback varying %s undeclared.",
621 return this->matched_candidate
;
626 * Parse all the transform feedback declarations that were passed to
627 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
629 * If an error occurs, the error is reported through linker_error() and false
633 parse_tfeedback_decls(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
634 const void *mem_ctx
, unsigned num_names
,
635 char **varying_names
, tfeedback_decl
*decls
)
637 for (unsigned i
= 0; i
< num_names
; ++i
) {
638 decls
[i
].init(ctx
, mem_ctx
, varying_names
[i
]);
640 if (!decls
[i
].is_varying())
643 /* From GL_EXT_transform_feedback:
644 * A program will fail to link if:
646 * * any two entries in the <varyings> array specify the same varying
649 * We interpret this to mean "any two entries in the <varyings> array
650 * specify the same varying variable and array index", since transform
651 * feedback of arrays would be useless otherwise.
653 for (unsigned j
= 0; j
< i
; ++j
) {
654 if (!decls
[j
].is_varying())
657 if (tfeedback_decl::is_same(decls
[i
], decls
[j
])) {
658 linker_error(prog
, "Transform feedback varying %s specified "
659 "more than once.", varying_names
[i
]);
669 * Store transform feedback location assignments into
670 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
672 * If an error occurs, the error is reported through linker_error() and false
676 store_tfeedback_info(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
677 unsigned num_tfeedback_decls
,
678 tfeedback_decl
*tfeedback_decls
)
680 bool separate_attribs_mode
=
681 prog
->TransformFeedback
.BufferMode
== GL_SEPARATE_ATTRIBS
;
683 ralloc_free(prog
->LinkedTransformFeedback
.Varyings
);
684 ralloc_free(prog
->LinkedTransformFeedback
.Outputs
);
686 memset(&prog
->LinkedTransformFeedback
, 0,
687 sizeof(prog
->LinkedTransformFeedback
));
689 prog
->LinkedTransformFeedback
.Varyings
=
691 struct gl_transform_feedback_varying_info
,
692 num_tfeedback_decls
);
694 unsigned num_outputs
= 0;
695 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
)
696 num_outputs
+= tfeedback_decls
[i
].get_num_outputs();
698 prog
->LinkedTransformFeedback
.Outputs
=
700 struct gl_transform_feedback_output
,
703 unsigned num_buffers
= 0;
705 if (separate_attribs_mode
) {
706 /* GL_SEPARATE_ATTRIBS */
707 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
708 if (!tfeedback_decls
[i
].store(ctx
, prog
, &prog
->LinkedTransformFeedback
,
709 num_buffers
, num_outputs
))
716 /* GL_INVERLEAVED_ATTRIBS */
717 int buffer_stream_id
= -1;
718 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
719 if (tfeedback_decls
[i
].is_next_buffer_separator()) {
721 buffer_stream_id
= -1;
723 } else if (buffer_stream_id
== -1) {
724 /* First varying writing to this buffer: remember its stream */
725 buffer_stream_id
= (int) tfeedback_decls
[i
].get_stream_id();
726 } else if (buffer_stream_id
!=
727 (int) tfeedback_decls
[i
].get_stream_id()) {
728 /* Varying writes to the same buffer from a different stream */
730 "Transform feedback can't capture varyings belonging "
731 "to different vertex streams in a single buffer. "
732 "Varying %s writes to buffer from stream %u, other "
733 "varyings in the same buffer write from stream %u.",
734 tfeedback_decls
[i
].name(),
735 tfeedback_decls
[i
].get_stream_id(),
740 if (!tfeedback_decls
[i
].store(ctx
, prog
,
741 &prog
->LinkedTransformFeedback
,
742 num_buffers
, num_outputs
))
748 assert(prog
->LinkedTransformFeedback
.NumOutputs
== num_outputs
);
750 prog
->LinkedTransformFeedback
.NumBuffers
= num_buffers
;
757 * Data structure recording the relationship between outputs of one shader
758 * stage (the "producer") and inputs of another (the "consumer").
760 class varying_matches
763 varying_matches(bool disable_varying_packing
, bool consumer_is_fs
);
765 void record(ir_variable
*producer_var
, ir_variable
*consumer_var
);
766 unsigned assign_locations();
767 void store_locations() const;
771 * If true, this driver disables varying packing, so all varyings need to
772 * be aligned on slot boundaries, and take up a number of slots equal to
773 * their number of matrix columns times their array size.
775 const bool disable_varying_packing
;
778 * Enum representing the order in which varyings are packed within a
781 * Currently we pack vec4's first, then vec2's, then scalar values, then
782 * vec3's. This order ensures that the only vectors that are at risk of
783 * having to be "double parked" (split between two adjacent varying slots)
786 enum packing_order_enum
{
789 PACKING_ORDER_SCALAR
,
793 static unsigned compute_packing_class(const ir_variable
*var
);
794 static packing_order_enum
compute_packing_order(const ir_variable
*var
);
795 static int match_comparator(const void *x_generic
, const void *y_generic
);
798 * Structure recording the relationship between a single producer output
799 * and a single consumer input.
803 * Packing class for this varying, computed by compute_packing_class().
805 unsigned packing_class
;
808 * Packing order for this varying, computed by compute_packing_order().
810 packing_order_enum packing_order
;
811 unsigned num_components
;
814 * The output variable in the producer stage.
816 ir_variable
*producer_var
;
819 * The input variable in the consumer stage.
821 ir_variable
*consumer_var
;
824 * The location which has been assigned for this varying. This is
825 * expressed in multiples of a float, with the first generic varying
826 * (i.e. the one referred to by VARYING_SLOT_VAR0) represented by the
829 unsigned generic_location
;
833 * The number of elements in the \c matches array that are currently in
836 unsigned num_matches
;
839 * The number of elements that were set aside for the \c matches array when
842 unsigned matches_capacity
;
844 const bool consumer_is_fs
;
847 } /* anonymous namespace */
849 varying_matches::varying_matches(bool disable_varying_packing
,
851 : disable_varying_packing(disable_varying_packing
),
852 consumer_is_fs(consumer_is_fs
)
854 /* Note: this initial capacity is rather arbitrarily chosen to be large
855 * enough for many cases without wasting an unreasonable amount of space.
856 * varying_matches::record() will resize the array if there are more than
857 * this number of varyings.
859 this->matches_capacity
= 8;
860 this->matches
= (match
*)
861 malloc(sizeof(*this->matches
) * this->matches_capacity
);
862 this->num_matches
= 0;
866 varying_matches::~varying_matches()
873 * Record the given producer/consumer variable pair in the list of variables
874 * that should later be assigned locations.
876 * It is permissible for \c consumer_var to be NULL (this happens if a
877 * variable is output by the producer and consumed by transform feedback, but
878 * not consumed by the consumer).
880 * If \c producer_var has already been paired up with a consumer_var, or
881 * producer_var is part of fixed pipeline functionality (and hence already has
882 * a location assigned), this function has no effect.
884 * Note: as a side effect this function may change the interpolation type of
885 * \c producer_var, but only when the change couldn't possibly affect
889 varying_matches::record(ir_variable
*producer_var
, ir_variable
*consumer_var
)
891 assert(producer_var
!= NULL
|| consumer_var
!= NULL
);
893 if ((producer_var
&& !producer_var
->data
.is_unmatched_generic_inout
)
894 || (consumer_var
&& !consumer_var
->data
.is_unmatched_generic_inout
)) {
895 /* Either a location already exists for this variable (since it is part
896 * of fixed functionality), or it has already been recorded as part of a
902 if ((consumer_var
== NULL
&& producer_var
->type
->contains_integer()) ||
904 /* Since this varying is not being consumed by the fragment shader, its
905 * interpolation type varying cannot possibly affect rendering. Also,
906 * this variable is non-flat and is (or contains) an integer.
908 * lower_packed_varyings requires all integer varyings to flat,
909 * regardless of where they appear. We can trivially satisfy that
910 * requirement by changing the interpolation type to flat here.
913 producer_var
->data
.centroid
= false;
914 producer_var
->data
.sample
= false;
915 producer_var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
919 consumer_var
->data
.centroid
= false;
920 consumer_var
->data
.sample
= false;
921 consumer_var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
925 if (this->num_matches
== this->matches_capacity
) {
926 this->matches_capacity
*= 2;
927 this->matches
= (match
*)
928 realloc(this->matches
,
929 sizeof(*this->matches
) * this->matches_capacity
);
932 const ir_variable
*const var
= (producer_var
!= NULL
)
933 ? producer_var
: consumer_var
;
935 this->matches
[this->num_matches
].packing_class
936 = this->compute_packing_class(var
);
937 this->matches
[this->num_matches
].packing_order
938 = this->compute_packing_order(var
);
939 if (this->disable_varying_packing
) {
940 unsigned slots
= var
->type
->is_array()
941 ? (var
->type
->length
* var
->type
->fields
.array
->matrix_columns
)
942 : var
->type
->matrix_columns
;
943 this->matches
[this->num_matches
].num_components
= 4 * slots
;
945 this->matches
[this->num_matches
].num_components
946 = var
->type
->component_slots();
948 this->matches
[this->num_matches
].producer_var
= producer_var
;
949 this->matches
[this->num_matches
].consumer_var
= consumer_var
;
952 producer_var
->data
.is_unmatched_generic_inout
= 0;
954 consumer_var
->data
.is_unmatched_generic_inout
= 0;
959 * Choose locations for all of the variable matches that were previously
960 * passed to varying_matches::record().
963 varying_matches::assign_locations()
965 /* Sort varying matches into an order that makes them easy to pack. */
966 qsort(this->matches
, this->num_matches
, sizeof(*this->matches
),
967 &varying_matches::match_comparator
);
969 unsigned generic_location
= 0;
971 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
972 /* Advance to the next slot if this varying has a different packing
973 * class than the previous one, and we're not already on a slot
977 this->matches
[i
- 1].packing_class
978 != this->matches
[i
].packing_class
) {
979 generic_location
= ALIGN(generic_location
, 4);
982 this->matches
[i
].generic_location
= generic_location
;
984 generic_location
+= this->matches
[i
].num_components
;
987 return (generic_location
+ 3) / 4;
992 * Update the producer and consumer shaders to reflect the locations
993 * assignments that were made by varying_matches::assign_locations().
996 varying_matches::store_locations() const
998 for (unsigned i
= 0; i
< this->num_matches
; i
++) {
999 ir_variable
*producer_var
= this->matches
[i
].producer_var
;
1000 ir_variable
*consumer_var
= this->matches
[i
].consumer_var
;
1001 unsigned generic_location
= this->matches
[i
].generic_location
;
1002 unsigned slot
= generic_location
/ 4;
1003 unsigned offset
= generic_location
% 4;
1006 producer_var
->data
.location
= VARYING_SLOT_VAR0
+ slot
;
1007 producer_var
->data
.location_frac
= offset
;
1011 assert(consumer_var
->data
.location
== -1);
1012 consumer_var
->data
.location
= VARYING_SLOT_VAR0
+ slot
;
1013 consumer_var
->data
.location_frac
= offset
;
1020 * Compute the "packing class" of the given varying. This is an unsigned
1021 * integer with the property that two variables in the same packing class can
1022 * be safely backed into the same vec4.
1025 varying_matches::compute_packing_class(const ir_variable
*var
)
1027 /* Without help from the back-end, there is no way to pack together
1028 * variables with different interpolation types, because
1029 * lower_packed_varyings must choose exactly one interpolation type for
1030 * each packed varying it creates.
1032 * However, we can safely pack together floats, ints, and uints, because:
1034 * - varyings of base type "int" and "uint" must use the "flat"
1035 * interpolation type, which can only occur in GLSL 1.30 and above.
1037 * - On platforms that support GLSL 1.30 and above, lower_packed_varyings
1038 * can store flat floats as ints without losing any information (using
1039 * the ir_unop_bitcast_* opcodes).
1041 * Therefore, the packing class depends only on the interpolation type.
1043 unsigned packing_class
= var
->data
.centroid
| (var
->data
.sample
<< 1) |
1044 (var
->data
.patch
<< 2);
1046 packing_class
+= var
->data
.interpolation
;
1047 return packing_class
;
1052 * Compute the "packing order" of the given varying. This is a sort key we
1053 * use to determine when to attempt to pack the given varying relative to
1054 * other varyings in the same packing class.
1056 varying_matches::packing_order_enum
1057 varying_matches::compute_packing_order(const ir_variable
*var
)
1059 const glsl_type
*element_type
= var
->type
;
1061 while (element_type
->base_type
== GLSL_TYPE_ARRAY
) {
1062 element_type
= element_type
->fields
.array
;
1065 switch (element_type
->component_slots() % 4) {
1066 case 1: return PACKING_ORDER_SCALAR
;
1067 case 2: return PACKING_ORDER_VEC2
;
1068 case 3: return PACKING_ORDER_VEC3
;
1069 case 0: return PACKING_ORDER_VEC4
;
1071 assert(!"Unexpected value of vector_elements");
1072 return PACKING_ORDER_VEC4
;
1078 * Comparison function passed to qsort() to sort varyings by packing_class and
1079 * then by packing_order.
1082 varying_matches::match_comparator(const void *x_generic
, const void *y_generic
)
1084 const match
*x
= (const match
*) x_generic
;
1085 const match
*y
= (const match
*) y_generic
;
1087 if (x
->packing_class
!= y
->packing_class
)
1088 return x
->packing_class
- y
->packing_class
;
1089 return x
->packing_order
- y
->packing_order
;
1094 * Is the given variable a varying variable to be counted against the
1095 * limit in ctx->Const.MaxVarying?
1096 * This includes variables such as texcoords, colors and generic
1097 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
1100 var_counts_against_varying_limit(gl_shader_stage stage
, const ir_variable
*var
)
1102 /* Only fragment shaders will take a varying variable as an input */
1103 if (stage
== MESA_SHADER_FRAGMENT
&&
1104 var
->data
.mode
== ir_var_shader_in
) {
1105 switch (var
->data
.location
) {
1106 case VARYING_SLOT_POS
:
1107 case VARYING_SLOT_FACE
:
1108 case VARYING_SLOT_PNTC
:
1119 * Visitor class that generates tfeedback_candidate structs describing all
1120 * possible targets of transform feedback.
1122 * tfeedback_candidate structs are stored in the hash table
1123 * tfeedback_candidates, which is passed to the constructor. This hash table
1124 * maps varying names to instances of the tfeedback_candidate struct.
1126 class tfeedback_candidate_generator
: public program_resource_visitor
1129 tfeedback_candidate_generator(void *mem_ctx
,
1130 hash_table
*tfeedback_candidates
)
1132 tfeedback_candidates(tfeedback_candidates
),
1138 void process(ir_variable
*var
)
1140 this->toplevel_var
= var
;
1141 this->varying_floats
= 0;
1142 if (var
->is_interface_instance())
1143 program_resource_visitor::process(var
->get_interface_type(),
1144 var
->get_interface_type()->name
);
1146 program_resource_visitor::process(var
);
1150 virtual void visit_field(const glsl_type
*type
, const char *name
,
1153 assert(!type
->without_array()->is_record());
1154 assert(!type
->without_array()->is_interface());
1158 tfeedback_candidate
*candidate
1159 = rzalloc(this->mem_ctx
, tfeedback_candidate
);
1160 candidate
->toplevel_var
= this->toplevel_var
;
1161 candidate
->type
= type
;
1162 candidate
->offset
= this->varying_floats
;
1163 hash_table_insert(this->tfeedback_candidates
, candidate
,
1164 ralloc_strdup(this->mem_ctx
, name
));
1165 this->varying_floats
+= type
->component_slots();
1169 * Memory context used to allocate hash table keys and values.
1171 void * const mem_ctx
;
1174 * Hash table in which tfeedback_candidate objects should be stored.
1176 hash_table
* const tfeedback_candidates
;
1179 * Pointer to the toplevel variable that is being traversed.
1181 ir_variable
*toplevel_var
;
1184 * Total number of varying floats that have been visited so far. This is
1185 * used to determine the offset to each varying within the toplevel
1188 unsigned varying_floats
;
1195 populate_consumer_input_sets(void *mem_ctx
, exec_list
*ir
,
1196 hash_table
*consumer_inputs
,
1197 hash_table
*consumer_interface_inputs
,
1198 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_MAX
])
1200 memset(consumer_inputs_with_locations
,
1202 sizeof(consumer_inputs_with_locations
[0]) * VARYING_SLOT_MAX
);
1204 foreach_in_list(ir_instruction
, node
, ir
) {
1205 ir_variable
*const input_var
= node
->as_variable();
1207 if ((input_var
!= NULL
) && (input_var
->data
.mode
== ir_var_shader_in
)) {
1208 if (input_var
->type
->is_interface())
1211 if (input_var
->data
.explicit_location
) {
1212 /* assign_varying_locations only cares about finding the
1213 * ir_variable at the start of a contiguous location block.
1215 * - For !producer, consumer_inputs_with_locations isn't used.
1217 * - For !consumer, consumer_inputs_with_locations is empty.
1219 * For consumer && producer, if you were trying to set some
1220 * ir_variable to the middle of a location block on the other side
1221 * of producer/consumer, cross_validate_outputs_to_inputs() should
1222 * be link-erroring due to either type mismatch or location
1223 * overlaps. If the variables do match up, then they've got a
1224 * matching data.location and you only looked at
1225 * consumer_inputs_with_locations[var->data.location], not any
1226 * following entries for the array/structure.
1228 consumer_inputs_with_locations
[input_var
->data
.location
] =
1230 } else if (input_var
->get_interface_type() != NULL
) {
1231 char *const iface_field_name
=
1232 ralloc_asprintf(mem_ctx
, "%s.%s",
1233 input_var
->get_interface_type()->name
,
1235 hash_table_insert(consumer_interface_inputs
, input_var
,
1238 hash_table_insert(consumer_inputs
, input_var
,
1239 ralloc_strdup(mem_ctx
, input_var
->name
));
1248 * Find a variable from the consumer that "matches" the specified variable
1250 * This function only finds inputs with names that match. There is no
1251 * validation (here) that the types, etc. are compatible.
1254 get_matching_input(void *mem_ctx
,
1255 const ir_variable
*output_var
,
1256 hash_table
*consumer_inputs
,
1257 hash_table
*consumer_interface_inputs
,
1258 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_MAX
])
1260 ir_variable
*input_var
;
1262 if (output_var
->data
.explicit_location
) {
1263 input_var
= consumer_inputs_with_locations
[output_var
->data
.location
];
1264 } else if (output_var
->get_interface_type() != NULL
) {
1265 char *const iface_field_name
=
1266 ralloc_asprintf(mem_ctx
, "%s.%s",
1267 output_var
->get_interface_type()->name
,
1270 (ir_variable
*) hash_table_find(consumer_interface_inputs
,
1274 (ir_variable
*) hash_table_find(consumer_inputs
, output_var
->name
);
1277 return (input_var
== NULL
|| input_var
->data
.mode
!= ir_var_shader_in
)
1284 io_variable_cmp(const void *_a
, const void *_b
)
1286 const ir_variable
*const a
= *(const ir_variable
**) _a
;
1287 const ir_variable
*const b
= *(const ir_variable
**) _b
;
1289 if (a
->data
.explicit_location
&& b
->data
.explicit_location
)
1290 return b
->data
.location
- a
->data
.location
;
1292 if (a
->data
.explicit_location
&& !b
->data
.explicit_location
)
1295 if (!a
->data
.explicit_location
&& b
->data
.explicit_location
)
1298 return -strcmp(a
->name
, b
->name
);
1302 * Sort the shader IO variables into canonical order
1305 canonicalize_shader_io(exec_list
*ir
, enum ir_variable_mode io_mode
)
1307 ir_variable
*var_table
[MAX_PROGRAM_OUTPUTS
* 4];
1308 unsigned num_variables
= 0;
1310 foreach_in_list(ir_instruction
, node
, ir
) {
1311 ir_variable
*const var
= node
->as_variable();
1313 if (var
== NULL
|| var
->data
.mode
!= io_mode
)
1316 /* If we have already encountered more I/O variables that could
1317 * successfully link, bail.
1319 if (num_variables
== ARRAY_SIZE(var_table
))
1322 var_table
[num_variables
++] = var
;
1325 if (num_variables
== 0)
1328 /* Sort the list in reverse order (io_variable_cmp handles this). Later
1329 * we're going to push the variables on to the IR list as a stack, so we
1330 * want the last variable (in canonical order) to be first in the list.
1332 qsort(var_table
, num_variables
, sizeof(var_table
[0]), io_variable_cmp
);
1334 /* Remove the variable from it's current location in the IR, and put it at
1337 for (unsigned i
= 0; i
< num_variables
; i
++) {
1338 var_table
[i
]->remove();
1339 ir
->push_head(var_table
[i
]);
1344 * Assign locations for all variables that are produced in one pipeline stage
1345 * (the "producer") and consumed in the next stage (the "consumer").
1347 * Variables produced by the producer may also be consumed by transform
1350 * \param num_tfeedback_decls is the number of declarations indicating
1351 * variables that may be consumed by transform feedback.
1353 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
1354 * representing the result of parsing the strings passed to
1355 * glTransformFeedbackVaryings(). assign_location() will be called for
1356 * each of these objects that matches one of the outputs of the
1359 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
1360 * be NULL. In this case, varying locations are assigned solely based on the
1361 * requirements of transform feedback.
1364 assign_varying_locations(struct gl_context
*ctx
,
1366 struct gl_shader_program
*prog
,
1367 gl_shader
*producer
, gl_shader
*consumer
,
1368 unsigned num_tfeedback_decls
,
1369 tfeedback_decl
*tfeedback_decls
)
1371 if (ctx
->Const
.DisableVaryingPacking
) {
1372 /* Transform feedback code assumes varyings are packed, so if the driver
1373 * has disabled varying packing, make sure it does not support transform
1376 assert(!ctx
->Extensions
.EXT_transform_feedback
);
1379 /* Tessellation shaders treat inputs and outputs as shared memory and can
1380 * access inputs and outputs of other invocations.
1381 * Therefore, they can't be lowered to temps easily (and definitely not
1384 bool disable_varying_packing
=
1385 ctx
->Const
.DisableVaryingPacking
||
1386 (consumer
&& consumer
->Stage
== MESA_SHADER_TESS_EVAL
) ||
1387 (consumer
&& consumer
->Stage
== MESA_SHADER_TESS_CTRL
) ||
1388 (producer
&& producer
->Stage
== MESA_SHADER_TESS_CTRL
);
1390 varying_matches
matches(disable_varying_packing
,
1391 consumer
&& consumer
->Stage
== MESA_SHADER_FRAGMENT
);
1392 hash_table
*tfeedback_candidates
1393 = hash_table_ctor(0, hash_table_string_hash
, hash_table_string_compare
);
1394 hash_table
*consumer_inputs
1395 = hash_table_ctor(0, hash_table_string_hash
, hash_table_string_compare
);
1396 hash_table
*consumer_interface_inputs
1397 = hash_table_ctor(0, hash_table_string_hash
, hash_table_string_compare
);
1398 ir_variable
*consumer_inputs_with_locations
[VARYING_SLOT_MAX
] = {
1402 unsigned consumer_vertices
= 0;
1403 if (consumer
&& consumer
->Stage
== MESA_SHADER_GEOMETRY
)
1404 consumer_vertices
= prog
->Geom
.VerticesIn
;
1406 /* Operate in a total of four passes.
1408 * 1. Sort inputs / outputs into a canonical order. This is necessary so
1409 * that inputs / outputs of separable shaders will be assigned
1410 * predictable locations regardless of the order in which declarations
1411 * appeared in the shader source.
1413 * 2. Assign locations for any matching inputs and outputs.
1415 * 3. Mark output variables in the producer that do not have locations as
1416 * not being outputs. This lets the optimizer eliminate them.
1418 * 4. Mark input variables in the consumer that do not have locations as
1419 * not being inputs. This lets the optimizer eliminate them.
1422 canonicalize_shader_io(consumer
->ir
, ir_var_shader_in
);
1425 canonicalize_shader_io(producer
->ir
, ir_var_shader_out
);
1428 && !linker::populate_consumer_input_sets(mem_ctx
,
1431 consumer_interface_inputs
,
1432 consumer_inputs_with_locations
)) {
1433 assert(!"populate_consumer_input_sets failed");
1434 hash_table_dtor(tfeedback_candidates
);
1435 hash_table_dtor(consumer_inputs
);
1436 hash_table_dtor(consumer_interface_inputs
);
1441 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
1442 ir_variable
*const output_var
= node
->as_variable();
1444 if ((output_var
== NULL
) ||
1445 (output_var
->data
.mode
!= ir_var_shader_out
))
1448 /* Only geometry shaders can use non-zero streams */
1449 assert(output_var
->data
.stream
== 0 ||
1450 (output_var
->data
.stream
< MAX_VERTEX_STREAMS
&&
1451 producer
->Stage
== MESA_SHADER_GEOMETRY
));
1453 tfeedback_candidate_generator
g(mem_ctx
, tfeedback_candidates
);
1454 g
.process(output_var
);
1456 ir_variable
*const input_var
=
1457 linker::get_matching_input(mem_ctx
, output_var
, consumer_inputs
,
1458 consumer_interface_inputs
,
1459 consumer_inputs_with_locations
);
1461 /* If a matching input variable was found, add this ouptut (and the
1462 * input) to the set. If this is a separable program and there is no
1463 * consumer stage, add the output.
1465 * Always add TCS outputs. They are shared by all invocations
1466 * within a patch and can be used as shared memory.
1468 if (input_var
|| (prog
->SeparateShader
&& consumer
== NULL
) ||
1469 producer
->Type
== GL_TESS_CONTROL_SHADER
) {
1470 matches
.record(output_var
, input_var
);
1473 /* Only stream 0 outputs can be consumed in the next stage */
1474 if (input_var
&& output_var
->data
.stream
!= 0) {
1475 linker_error(prog
, "output %s is assigned to stream=%d but "
1476 "is linked to an input, which requires stream=0",
1477 output_var
->name
, output_var
->data
.stream
);
1482 /* If there's no producer stage, then this must be a separable program.
1483 * For example, we may have a program that has just a fragment shader.
1484 * Later this program will be used with some arbitrary vertex (or
1485 * geometry) shader program. This means that locations must be assigned
1486 * for all the inputs.
1488 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
1489 ir_variable
*const input_var
= node
->as_variable();
1491 if ((input_var
== NULL
) ||
1492 (input_var
->data
.mode
!= ir_var_shader_in
))
1495 matches
.record(NULL
, input_var
);
1499 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1500 if (!tfeedback_decls
[i
].is_varying())
1503 const tfeedback_candidate
*matched_candidate
1504 = tfeedback_decls
[i
].find_candidate(prog
, tfeedback_candidates
);
1506 if (matched_candidate
== NULL
) {
1507 hash_table_dtor(tfeedback_candidates
);
1508 hash_table_dtor(consumer_inputs
);
1509 hash_table_dtor(consumer_interface_inputs
);
1513 if (matched_candidate
->toplevel_var
->data
.is_unmatched_generic_inout
)
1514 matches
.record(matched_candidate
->toplevel_var
, NULL
);
1517 const unsigned slots_used
= matches
.assign_locations();
1518 matches
.store_locations();
1520 for (unsigned i
= 0; i
< num_tfeedback_decls
; ++i
) {
1521 if (!tfeedback_decls
[i
].is_varying())
1524 if (!tfeedback_decls
[i
].assign_location(ctx
, prog
)) {
1525 hash_table_dtor(tfeedback_candidates
);
1526 hash_table_dtor(consumer_inputs
);
1527 hash_table_dtor(consumer_interface_inputs
);
1532 hash_table_dtor(tfeedback_candidates
);
1533 hash_table_dtor(consumer_inputs
);
1534 hash_table_dtor(consumer_interface_inputs
);
1536 if (!disable_varying_packing
) {
1538 lower_packed_varyings(mem_ctx
, slots_used
, ir_var_shader_out
,
1542 lower_packed_varyings(mem_ctx
, slots_used
, ir_var_shader_in
,
1543 consumer_vertices
, consumer
);
1547 if (consumer
&& producer
) {
1548 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
1549 ir_variable
*const var
= node
->as_variable();
1551 if (var
&& var
->data
.mode
== ir_var_shader_in
&&
1552 var
->data
.is_unmatched_generic_inout
) {
1555 * On Page 91 (Page 97 of the PDF) of the GLSL ES 1.0 spec:
1557 * If the vertex shader declares but doesn't write to a
1558 * varying and the fragment shader declares and reads it,
1563 linker_warning(prog
, "%s shader varying %s not written "
1565 _mesa_shader_stage_to_string(consumer
->Stage
),
1567 _mesa_shader_stage_to_string(producer
->Stage
));
1568 } else if (prog
->Version
<= 120) {
1569 /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1571 * Only those varying variables used (i.e. read) in
1572 * the fragment shader executable must be written to
1573 * by the vertex shader executable; declaring
1574 * superfluous varying variables in a vertex shader is
1577 * We interpret this text as meaning that the VS must
1578 * write the variable for the FS to read it. See
1579 * "glsl1-varying read but not written" in piglit.
1581 linker_error(prog
, "%s shader varying %s not written "
1583 _mesa_shader_stage_to_string(consumer
->Stage
),
1585 _mesa_shader_stage_to_string(producer
->Stage
));
1588 /* An 'in' variable is only really a shader input if its
1589 * value is written by the previous stage.
1591 var
->data
.mode
= ir_var_auto
;
1600 check_against_output_limit(struct gl_context
*ctx
,
1601 struct gl_shader_program
*prog
,
1602 gl_shader
*producer
)
1604 unsigned output_vectors
= 0;
1606 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
1607 ir_variable
*const var
= node
->as_variable();
1609 if (var
&& var
->data
.mode
== ir_var_shader_out
&&
1610 var_counts_against_varying_limit(producer
->Stage
, var
)) {
1611 output_vectors
+= var
->type
->count_attribute_slots();
1615 assert(producer
->Stage
!= MESA_SHADER_FRAGMENT
);
1616 unsigned max_output_components
=
1617 ctx
->Const
.Program
[producer
->Stage
].MaxOutputComponents
;
1619 const unsigned output_components
= output_vectors
* 4;
1620 if (output_components
> max_output_components
) {
1621 if (ctx
->API
== API_OPENGLES2
|| prog
->IsES
)
1622 linker_error(prog
, "%s shader uses too many output vectors "
1624 _mesa_shader_stage_to_string(producer
->Stage
),
1626 max_output_components
/ 4);
1628 linker_error(prog
, "%s shader uses too many output components "
1630 _mesa_shader_stage_to_string(producer
->Stage
),
1632 max_output_components
);
1641 check_against_input_limit(struct gl_context
*ctx
,
1642 struct gl_shader_program
*prog
,
1643 gl_shader
*consumer
)
1645 unsigned input_vectors
= 0;
1647 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
1648 ir_variable
*const var
= node
->as_variable();
1650 if (var
&& var
->data
.mode
== ir_var_shader_in
&&
1651 var_counts_against_varying_limit(consumer
->Stage
, var
)) {
1652 input_vectors
+= var
->type
->count_attribute_slots();
1656 assert(consumer
->Stage
!= MESA_SHADER_VERTEX
);
1657 unsigned max_input_components
=
1658 ctx
->Const
.Program
[consumer
->Stage
].MaxInputComponents
;
1660 const unsigned input_components
= input_vectors
* 4;
1661 if (input_components
> max_input_components
) {
1662 if (ctx
->API
== API_OPENGLES2
|| prog
->IsES
)
1663 linker_error(prog
, "%s shader uses too many input vectors "
1665 _mesa_shader_stage_to_string(consumer
->Stage
),
1667 max_input_components
/ 4);
1669 linker_error(prog
, "%s shader uses too many input components "
1671 _mesa_shader_stage_to_string(consumer
->Stage
),
1673 max_input_components
);