2 * Copyright © 2013 Marek Olšák <maraeo@gmail.com>
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 opt_dead_builtin_varyings.cpp
27 * This eliminates the built-in shader outputs which are either not written
28 * at all or not used by the next stage. It also eliminates unused elements
29 * of gl_TexCoord inputs, which reduces the overall varying usage.
30 * The varyings handled here are the primary and secondary color, the fog,
31 * and the texture coordinates (gl_TexCoord).
33 * This pass is necessary, because the Mesa GLSL linker cannot eliminate
34 * built-in varyings like it eliminates user-defined varyings, because
35 * the built-in varyings have pre-assigned locations. Also, the elimination
36 * of unused gl_TexCoord elements requires its own lowering pass anyway.
38 * It's implemented by replacing all occurrences of dead varyings with
39 * temporary variables, which creates dead code. It is recommended to run
40 * a dead-code elimination pass after this.
42 * If any texture coordinate slots can be eliminated, the gl_TexCoord array is
43 * broken down into separate vec4 variables with locations equal to
44 * VARYING_SLOT_TEX0 + i.
46 * The same is done for the gl_FragData fragment shader output.
49 #include "main/core.h" /* for snprintf and ARRAY_SIZE */
51 #include "ir_rvalue_visitor.h"
52 #include "ir_optimization.h"
53 #include "ir_print_visitor.h"
54 #include "glsl_types.h"
55 #include "link_varyings.h"
60 * This obtains detailed information about built-in varyings from shader code.
62 class varying_info_visitor
: public ir_hierarchical_visitor
{
64 /* "mode" can be either ir_var_shader_in or ir_var_shader_out */
65 varying_info_visitor(ir_variable_mode mode
, bool find_frag_outputs
= false)
66 : lower_texcoord_array(true),
69 find_frag_outputs(find_frag_outputs
),
70 lower_fragdata_array(true),
74 tfeedback_color_usage(0),
77 tfeedback_has_fog(false),
80 memset(color
, 0, sizeof(color
));
81 memset(backcolor
, 0, sizeof(backcolor
));
84 virtual ir_visitor_status
visit_enter(ir_dereference_array
*ir
)
86 ir_variable
*var
= ir
->variable_referenced();
88 if (!var
|| var
->data
.mode
!= this->mode
)
89 return visit_continue
;
91 if (this->find_frag_outputs
&& var
->data
.location
== FRAG_RESULT_DATA0
) {
92 this->fragdata_array
= var
;
94 ir_constant
*index
= ir
->array_index
->as_constant();
96 /* This is variable indexing. */
97 this->fragdata_usage
|= (1 << var
->type
->array_size()) - 1;
98 this->lower_fragdata_array
= false;
101 this->fragdata_usage
|= 1 << index
->get_uint_component(0);
102 /* Don't lower fragdata array if the output variable
103 * is not a float variable (or float vector) because it will
104 * generate wrong register assignments because of different
107 if (var
->type
->gl_type
!= GL_FLOAT
&&
108 var
->type
->gl_type
!= GL_FLOAT_VEC2
&&
109 var
->type
->gl_type
!= GL_FLOAT_VEC3
&&
110 var
->type
->gl_type
!= GL_FLOAT_VEC4
)
111 this->lower_fragdata_array
= false;
114 /* Don't visit the leaves of ir_dereference_array. */
115 return visit_continue_with_parent
;
118 if (!this->find_frag_outputs
&& var
->data
.location
== VARYING_SLOT_TEX0
) {
119 this->texcoord_array
= var
;
121 ir_constant
*index
= ir
->array_index
->as_constant();
123 /* There is variable indexing, we can't lower the texcoord array.
125 this->texcoord_usage
|= (1 << var
->type
->array_size()) - 1;
126 this->lower_texcoord_array
= false;
129 this->texcoord_usage
|= 1 << index
->get_uint_component(0);
132 /* Don't visit the leaves of ir_dereference_array. */
133 return visit_continue_with_parent
;
136 return visit_continue
;
139 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
141 ir_variable
*var
= ir
->variable_referenced();
143 if (var
->data
.mode
!= this->mode
|| !var
->type
->is_array())
144 return visit_continue
;
146 if (this->find_frag_outputs
&& var
->data
.location
== FRAG_RESULT_DATA0
) {
147 /* This is a whole array dereference. */
148 this->fragdata_usage
|= (1 << var
->type
->array_size()) - 1;
149 this->lower_fragdata_array
= false;
150 return visit_continue
;
153 if (!this->find_frag_outputs
&& var
->data
.location
== VARYING_SLOT_TEX0
) {
154 /* This is a whole array dereference like "gl_TexCoord = x;",
155 * there's probably no point in lowering that.
157 this->texcoord_usage
|= (1 << var
->type
->array_size()) - 1;
158 this->lower_texcoord_array
= false;
160 return visit_continue
;
163 virtual ir_visitor_status
visit(ir_variable
*var
)
165 if (var
->data
.mode
!= this->mode
)
166 return visit_continue
;
168 /* Nothing to do here for fragment outputs. */
169 if (this->find_frag_outputs
)
170 return visit_continue
;
172 /* Handle colors and fog. */
173 switch (var
->data
.location
) {
174 case VARYING_SLOT_COL0
:
175 this->color
[0] = var
;
176 this->color_usage
|= 1;
178 case VARYING_SLOT_COL1
:
179 this->color
[1] = var
;
180 this->color_usage
|= 2;
182 case VARYING_SLOT_BFC0
:
183 this->backcolor
[0] = var
;
184 this->color_usage
|= 1;
186 case VARYING_SLOT_BFC1
:
187 this->backcolor
[1] = var
;
188 this->color_usage
|= 2;
190 case VARYING_SLOT_FOGC
:
192 this->has_fog
= true;
196 return visit_continue
;
199 void get(exec_list
*ir
,
200 unsigned num_tfeedback_decls
,
201 tfeedback_decl
*tfeedback_decls
)
203 /* Handle the transform feedback varyings. */
204 for (unsigned i
= 0; i
< num_tfeedback_decls
; i
++) {
205 if (!tfeedback_decls
[i
].is_varying())
208 unsigned location
= tfeedback_decls
[i
].get_location();
211 case VARYING_SLOT_COL0
:
212 case VARYING_SLOT_BFC0
:
213 this->tfeedback_color_usage
|= 1;
215 case VARYING_SLOT_COL1
:
216 case VARYING_SLOT_BFC1
:
217 this->tfeedback_color_usage
|= 2;
219 case VARYING_SLOT_FOGC
:
220 this->tfeedback_has_fog
= true;
223 if (location
>= VARYING_SLOT_TEX0
&&
224 location
<= VARYING_SLOT_TEX7
) {
225 this->lower_texcoord_array
= false;
230 /* Process the shader. */
231 visit_list_elements(this, ir
);
233 if (!this->texcoord_array
) {
234 this->lower_texcoord_array
= false;
236 if (!this->fragdata_array
) {
237 this->lower_fragdata_array
= false;
241 bool lower_texcoord_array
;
242 ir_variable
*texcoord_array
;
243 unsigned texcoord_usage
; /* bitmask */
245 bool find_frag_outputs
; /* false if it's looking for varyings */
246 bool lower_fragdata_array
;
247 ir_variable
*fragdata_array
;
248 unsigned fragdata_usage
; /* bitmask */
250 ir_variable
*color
[2];
251 ir_variable
*backcolor
[2];
252 unsigned color_usage
; /* bitmask */
253 unsigned tfeedback_color_usage
; /* bitmask */
257 bool tfeedback_has_fog
;
259 ir_variable_mode mode
;
264 * This replaces unused varyings with temporary variables.
266 * If "ir" is the producer, the "external" usage should come from
267 * the consumer. It also works the other way around. If either one is
268 * missing, set the "external" usage to a full mask.
270 class replace_varyings_visitor
: public ir_rvalue_visitor
{
272 replace_varyings_visitor(struct gl_shader
*sha
,
273 const varying_info_visitor
*info
,
274 unsigned external_texcoord_usage
,
275 unsigned external_color_usage
,
276 bool external_has_fog
)
277 : shader(sha
), info(info
), new_fog(NULL
)
279 void *const ctx
= shader
->ir
;
281 memset(this->new_fragdata
, 0, sizeof(this->new_fragdata
));
282 memset(this->new_texcoord
, 0, sizeof(this->new_texcoord
));
283 memset(this->new_color
, 0, sizeof(this->new_color
));
284 memset(this->new_backcolor
, 0, sizeof(this->new_backcolor
));
286 const char *mode_str
=
287 info
->mode
== ir_var_shader_in
? "in" : "out";
289 /* Handle texcoord outputs.
291 * We're going to break down the gl_TexCoord array into separate
292 * variables. First, add declarations of the new variables all
293 * occurrences of gl_TexCoord will be replaced with.
295 if (info
->lower_texcoord_array
) {
296 prepare_array(shader
->ir
, this->new_texcoord
,
297 ARRAY_SIZE(this->new_texcoord
),
298 VARYING_SLOT_TEX0
, "TexCoord", mode_str
,
299 info
->texcoord_usage
, external_texcoord_usage
);
302 /* Handle gl_FragData in the same way like gl_TexCoord. */
303 if (info
->lower_fragdata_array
) {
304 prepare_array(shader
->ir
, this->new_fragdata
,
305 ARRAY_SIZE(this->new_fragdata
),
306 FRAG_RESULT_DATA0
, "FragData", mode_str
,
307 info
->fragdata_usage
, (1 << MAX_DRAW_BUFFERS
) - 1);
310 /* Create dummy variables which will replace set-but-unused color and
313 external_color_usage
|= info
->tfeedback_color_usage
;
315 for (int i
= 0; i
< 2; i
++) {
318 if (!(external_color_usage
& (1 << i
))) {
319 if (info
->color
[i
]) {
320 snprintf(name
, 32, "gl_%s_FrontColor%i_dummy", mode_str
, i
);
322 new (ctx
) ir_variable(glsl_type::vec4_type
, name
,
326 if (info
->backcolor
[i
]) {
327 snprintf(name
, 32, "gl_%s_BackColor%i_dummy", mode_str
, i
);
328 this->new_backcolor
[i
] =
329 new (ctx
) ir_variable(glsl_type::vec4_type
, name
,
335 if (!external_has_fog
&& !info
->tfeedback_has_fog
&&
339 snprintf(name
, 32, "gl_%s_FogFragCoord_dummy", mode_str
);
340 this->new_fog
= new (ctx
) ir_variable(glsl_type::float_type
, name
,
344 /* Now do the replacing. */
345 visit_list_elements(this, shader
->ir
);
348 void prepare_array(exec_list
*ir
,
349 ir_variable
**new_var
,
350 int max_elements
, unsigned start_location
,
351 const char *var_name
, const char *mode_str
,
352 unsigned usage
, unsigned external_usage
)
354 void *const ctx
= ir
;
356 for (int i
= max_elements
-1; i
>= 0; i
--) {
357 if (usage
& (1 << i
)) {
360 if (!(external_usage
& (1 << i
))) {
361 /* This varying is unused in the next stage. Declare
362 * a temporary instead of an output. */
363 snprintf(name
, 32, "gl_%s_%s%i_dummy", mode_str
, var_name
, i
);
365 new (ctx
) ir_variable(glsl_type::vec4_type
, name
,
369 snprintf(name
, 32, "gl_%s_%s%i", mode_str
, var_name
, i
);
371 new(ctx
) ir_variable(glsl_type::vec4_type
, name
,
373 new_var
[i
]->data
.location
= start_location
+ i
;
374 new_var
[i
]->data
.explicit_location
= true;
375 new_var
[i
]->data
.explicit_index
= 0;
378 ir
->head
->insert_before(new_var
[i
]);
383 virtual ir_visitor_status
visit(ir_variable
*var
)
385 /* Remove the gl_TexCoord array. */
386 if (this->info
->lower_texcoord_array
&&
387 var
== this->info
->texcoord_array
) {
391 /* Remove the gl_FragData array. */
392 if (this->info
->lower_fragdata_array
&&
393 var
== this->info
->fragdata_array
) {
395 /* Clone variable for program resource list before it is removed. */
396 if (!shader
->fragdata_arrays
)
397 shader
->fragdata_arrays
= new (shader
) exec_list
;
399 shader
->fragdata_arrays
->push_tail(var
->clone(shader
, NULL
));
404 /* Replace set-but-unused color and fog outputs with dummy variables. */
405 for (int i
= 0; i
< 2; i
++) {
406 if (var
== this->info
->color
[i
] && this->new_color
[i
]) {
407 var
->replace_with(this->new_color
[i
]);
409 if (var
== this->info
->backcolor
[i
] &&
410 this->new_backcolor
[i
]) {
411 var
->replace_with(this->new_backcolor
[i
]);
415 if (var
== this->info
->fog
&& this->new_fog
) {
416 var
->replace_with(this->new_fog
);
419 return visit_continue
;
422 virtual void handle_rvalue(ir_rvalue
**rvalue
)
427 void *ctx
= ralloc_parent(*rvalue
);
429 /* Replace an array dereference gl_TexCoord[i] with a single
430 * variable dereference representing gl_TexCoord[i].
432 if (this->info
->lower_texcoord_array
) {
433 /* gl_TexCoord[i] occurrence */
434 ir_dereference_array
*const da
= (*rvalue
)->as_dereference_array();
436 if (da
&& da
->variable_referenced() ==
437 this->info
->texcoord_array
) {
438 unsigned i
= da
->array_index
->as_constant()->get_uint_component(0);
440 *rvalue
= new(ctx
) ir_dereference_variable(this->new_texcoord
[i
]);
445 /* Same for gl_FragData. */
446 if (this->info
->lower_fragdata_array
) {
447 /* gl_FragData[i] occurrence */
448 ir_dereference_array
*const da
= (*rvalue
)->as_dereference_array();
450 if (da
&& da
->variable_referenced() == this->info
->fragdata_array
) {
451 unsigned i
= da
->array_index
->as_constant()->get_uint_component(0);
453 *rvalue
= new(ctx
) ir_dereference_variable(this->new_fragdata
[i
]);
458 /* Replace set-but-unused color and fog outputs with dummy variables. */
459 ir_dereference_variable
*const dv
= (*rvalue
)->as_dereference_variable();
463 ir_variable
*var
= dv
->variable_referenced();
465 for (int i
= 0; i
< 2; i
++) {
466 if (var
== this->info
->color
[i
] && this->new_color
[i
]) {
467 *rvalue
= new(ctx
) ir_dereference_variable(this->new_color
[i
]);
470 if (var
== this->info
->backcolor
[i
] &&
471 this->new_backcolor
[i
]) {
472 *rvalue
= new(ctx
) ir_dereference_variable(this->new_backcolor
[i
]);
477 if (var
== this->info
->fog
&& this->new_fog
) {
478 *rvalue
= new(ctx
) ir_dereference_variable(this->new_fog
);
482 virtual ir_visitor_status
visit_leave(ir_assignment
*ir
)
484 handle_rvalue(&ir
->rhs
);
485 handle_rvalue(&ir
->condition
);
487 /* We have to use set_lhs when changing the LHS of an assignment. */
488 ir_rvalue
*lhs
= ir
->lhs
;
491 if (lhs
!= ir
->lhs
) {
495 return visit_continue
;
499 struct gl_shader
*shader
;
500 const varying_info_visitor
*info
;
501 ir_variable
*new_fragdata
[MAX_DRAW_BUFFERS
];
502 ir_variable
*new_texcoord
[MAX_TEXTURE_COORD_UNITS
];
503 ir_variable
*new_color
[2];
504 ir_variable
*new_backcolor
[2];
505 ir_variable
*new_fog
;
508 } /* anonymous namespace */
511 lower_texcoord_array(struct gl_shader
*shader
, const varying_info_visitor
*info
)
513 replace_varyings_visitor(shader
, info
,
514 (1 << MAX_TEXTURE_COORD_UNITS
) - 1,
519 lower_fragdata_array(struct gl_shader
*shader
)
521 varying_info_visitor
info(ir_var_shader_out
, true);
522 info
.get(shader
->ir
, 0, NULL
);
524 replace_varyings_visitor(shader
, &info
, 0, 0, 0);
529 do_dead_builtin_varyings(struct gl_context
*ctx
,
530 gl_shader
*producer
, gl_shader
*consumer
,
531 unsigned num_tfeedback_decls
,
532 tfeedback_decl
*tfeedback_decls
)
534 /* Lower the gl_FragData array to separate variables. */
535 if (consumer
&& consumer
->Stage
== MESA_SHADER_FRAGMENT
) {
536 lower_fragdata_array(consumer
);
539 /* Lowering of built-in varyings has no effect with the core context and
540 * GLES2, because they are not available there.
542 if (ctx
->API
== API_OPENGL_CORE
||
543 ctx
->API
== API_OPENGLES2
) {
547 /* Information about built-in varyings. */
548 varying_info_visitor
producer_info(ir_var_shader_out
);
549 varying_info_visitor
consumer_info(ir_var_shader_in
);
552 producer_info
.get(producer
->ir
, num_tfeedback_decls
, tfeedback_decls
);
555 /* At least eliminate unused gl_TexCoord elements. */
556 if (producer_info
.lower_texcoord_array
) {
557 lower_texcoord_array(producer
, &producer_info
);
564 consumer_info
.get(consumer
->ir
, 0, NULL
);
567 /* At least eliminate unused gl_TexCoord elements. */
568 if (consumer_info
.lower_texcoord_array
) {
569 lower_texcoord_array(consumer
, &consumer_info
);
575 /* Eliminate the outputs unused by the consumer. */
576 if (producer_info
.lower_texcoord_array
||
577 producer_info
.color_usage
||
578 producer_info
.has_fog
) {
579 replace_varyings_visitor(producer
,
581 consumer_info
.texcoord_usage
,
582 consumer_info
.color_usage
,
583 consumer_info
.has_fog
);
586 /* The gl_TexCoord fragment shader inputs can be initialized
587 * by GL_COORD_REPLACE, so we can't eliminate them.
589 * This doesn't prevent elimination of the gl_TexCoord elements which
590 * are not read by the fragment shader. We want to eliminate those anyway.
592 if (consumer
->Stage
== MESA_SHADER_FRAGMENT
) {
593 producer_info
.texcoord_usage
= (1 << MAX_TEXTURE_COORD_UNITS
) - 1;
596 /* Eliminate the inputs uninitialized by the producer. */
597 if (consumer_info
.lower_texcoord_array
||
598 consumer_info
.color_usage
||
599 consumer_info
.has_fog
) {
600 replace_varyings_visitor(consumer
,
602 producer_info
.texcoord_usage
,
603 producer_info
.color_usage
,
604 producer_info
.has_fog
);