1 /*
2 * Copyright © 2013 Marek Olšák <maraeo@gmail.com>
3 * Copyright © 2022 Valve Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * This eliminates the built-in shader outputs which are either not written
27 * at all or not used by the next stage. It also eliminates unused elements
28 * of gl_TexCoord inputs, which reduces the overall varying usage.
29 * The varyings handled here are the primary and secondary color, the fog,
30 * and the texture coordinates (gl_TexCoord).
31 *
32 * This pass is necessary, because the Mesa GLSL linker cannot eliminate
33 * built-in varyings like it eliminates user-defined varyings, because
34 * the built-in varyings have pre-assigned locations. Also, the elimination
35 * of unused gl_TexCoord elements requires its own lowering pass anyway.
36 *
37 * It's implemented by replacing all occurrences of dead varyings with
38 * temporary variables, which creates dead code. It is recommended to run
39 * a dead-code elimination pass after this.
40 *
41 * If any texture coordinate slots can be eliminated, the gl_TexCoord array is
42 * broken down into separate vec4 variables with locations equal to
43 * VARYING_SLOT_TEX0 + i.
44 */
45
46 #include "gl_nir_link_varyings.h"
47 #include "gl_nir_linker.h"
48 #include "nir_builder.h"
49 #include "nir_gl_types.h"
50 #include "nir_types.h"
51
52 #include "compiler/glsl_types.h"
53 #include "main/consts_exts.h"
54 #include "main/shader_types.h"
55 #include "util/u_string.h"
56
57 struct varying_info {
58 bool lower_texcoord_array;
59 nir_variable *texcoord_array;
60 unsigned texcoord_usage; /* bitmask */
61
62 bool find_frag_outputs; /* false if it's looking for varyings */
63
64 nir_variable *color[2];
65 nir_variable *backcolor[2];
66 unsigned color_usage; /* bitmask */
67 unsigned tfeedback_color_usage; /* bitmask */
68
69 nir_variable *fog;
70 bool has_fog;
71 bool tfeedback_has_fog;
72
73 nir_variable_mode mode;
74 };
75
76 static void
initialise_varying_info(struct varying_info * info,nir_variable_mode mode,bool find_frag_outputs)77 initialise_varying_info(struct varying_info *info, nir_variable_mode mode,
78 bool find_frag_outputs)
79 {
80 info->find_frag_outputs = find_frag_outputs;
81 info->lower_texcoord_array = true;
82 info->texcoord_array = NULL;
83 info->texcoord_usage = 0;
84 info->color_usage = 0;
85 info->tfeedback_color_usage = 0;
86 info->fog = NULL;
87 info->has_fog = false;
88 info->tfeedback_has_fog = false;
89 info->mode = mode;
90
91 memset(info->color, 0, sizeof(info->color));
92 memset(info->backcolor, 0, sizeof(info->backcolor));
93 }
94
95 /**
96 * Built-in / reserved GL variables names start with "gl_"
97 */
98 static bool
is_gl_identifier(const char * s)99 is_gl_identifier(const char *s)
100 {
101 return s && s[0] == 'g' && s[1] == 'l' && s[2] == '_';
102 }
103
104 static void
gather_info_on_varying_deref(struct varying_info * info,nir_deref_instr * deref)105 gather_info_on_varying_deref(struct varying_info *info, nir_deref_instr *deref)
106 {
107 nir_variable *var = nir_deref_instr_get_variable(deref);
108
109 if (!glsl_type_is_array(var->type) || !is_gl_identifier(var->name))
110 return;
111
112 if (!info->find_frag_outputs && var->data.location == VARYING_SLOT_TEX0) {
113 info->texcoord_array = var;
114
115 assert(deref->deref_type == nir_deref_type_array);
116 if (nir_src_is_const(deref->arr.index)) {
117 info->texcoord_usage |= 1 << nir_src_as_uint(deref->arr.index);
118 } else {
119 /* There is variable indexing, we can't lower the texcoord array. */
120 info->texcoord_usage |= (1 << glsl_array_size(var->type)) - 1;
121 info->lower_texcoord_array = false;
122 }
123
124 return;
125 }
126 }
127
128 /**
129 * This obtains detailed information about built-in varyings from shader code.
130 */
131 static void
get_varying_info(struct varying_info * info,nir_shader * shader,unsigned num_tfeedback_decls,struct xfb_decl * tfeedback_decls)132 get_varying_info(struct varying_info *info, nir_shader *shader,
133 unsigned num_tfeedback_decls, struct xfb_decl *tfeedback_decls)
134 {
135 /* Handle the transform feedback varyings. */
136 for (unsigned i = 0; i < num_tfeedback_decls; i++) {
137 if (!xfb_decl_is_varying(&tfeedback_decls[i]))
138 continue;
139
140 unsigned location = tfeedback_decls[i].location;
141
142 switch (location) {
143 case VARYING_SLOT_COL0:
144 case VARYING_SLOT_BFC0:
145 info->tfeedback_color_usage |= 1;
146 break;
147 case VARYING_SLOT_COL1:
148 case VARYING_SLOT_BFC1:
149 info->tfeedback_color_usage |= 2;
150 break;
151 case VARYING_SLOT_FOGC:
152 info->tfeedback_has_fog = true;
153 break;
154 default:
155 if (location >= VARYING_SLOT_TEX0 &&
156 location <= VARYING_SLOT_TEX7) {
157 info->lower_texcoord_array = false;
158 }
159 }
160 }
161
162 /* Process frag shader vars */
163 nir_foreach_variable_with_modes(var, shader, info->mode) {
164 /* Nothing to do here for fragment outputs. */
165 if (info->find_frag_outputs)
166 break;
167
168 /* Handle colors and fog. */
169 switch (var->data.location) {
170 case VARYING_SLOT_COL0:
171 info->color[0] = var;
172 info->color_usage |= 1;
173 break;
174 case VARYING_SLOT_COL1:
175 info->color[1] = var;
176 info->color_usage |= 2;
177 break;
178 case VARYING_SLOT_BFC0:
179 info->backcolor[0] = var;
180 info->color_usage |= 1;
181 break;
182 case VARYING_SLOT_BFC1:
183 info->backcolor[1] = var;
184 info->color_usage |= 2;
185 break;
186 case VARYING_SLOT_FOGC:
187 info->fog = var;
188 info->has_fog = true;
189 break;
190 }
191 }
192
193 /* Process the shader. */
194 assert(shader->info.stage != MESA_SHADER_COMPUTE);
195 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
196
197 /* assert that functions have been inlined before packing is called */
198 nir_foreach_function(f, shader) {
199 assert(f->impl == impl);
200 }
201
202 nir_foreach_block(block, impl) {
203 nir_foreach_instr(instr, block) {
204 if (instr->type != nir_instr_type_intrinsic)
205 continue;
206
207 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
208
209 /* Copies should have been lowered by nir_split_var_copies() before
210 * calling this pass.
211 */
212 assert(intrin->intrinsic != nir_intrinsic_copy_deref);
213
214 if (intrin->intrinsic != nir_intrinsic_load_deref &&
215 intrin->intrinsic != nir_intrinsic_store_deref)
216 continue;
217
218 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
219 if (!nir_deref_mode_is(deref, info->mode))
220 continue;
221
222 gather_info_on_varying_deref(info, deref);
223 }
224 }
225
226 if (!info->texcoord_array) {
227 info->lower_texcoord_array = false;
228 }
229 }
230
231 struct replace_varyings_data {
232 const struct gl_constants *consts;
233
234 struct gl_shader_program *prog;
235 struct gl_linked_shader *shader;
236 const struct varying_info *info;
237
238 nir_variable *new_texcoord[MAX_TEXTURE_COORD_UNITS];
239 nir_variable *new_color[2];
240 nir_variable *new_backcolor[2];
241 nir_variable *new_fog;
242 };
243
244 static nir_variable *
create_new_var(nir_shader * shader,char * name,nir_variable_mode mode,const struct glsl_type * type)245 create_new_var(nir_shader *shader, char *name, nir_variable_mode mode,
246 const struct glsl_type *type)
247 {
248 nir_variable *var = rzalloc(shader, nir_variable);
249 var->name = ralloc_strdup(var, name);
250 var->data.mode = mode;
251 var->type = type;
252
253 nir_shader_add_variable(shader, var);
254 return var;
255 }
256
257 static void
replace_varying(struct replace_varyings_data * rv_data,nir_variable * var)258 replace_varying(struct replace_varyings_data *rv_data, nir_variable *var)
259 {
260 /* Remove the gl_TexCoord array. */
261 if (rv_data->info->lower_texcoord_array &&
262 var == rv_data->info->texcoord_array) {
263 var->data.mode = nir_var_shader_temp;
264 }
265
266 /* Lower set-but-unused color and fog outputs to shader temps. */
267 for (int i = 0; i < 2; i++) {
268 if (var == rv_data->info->color[i] && rv_data->new_color[i]) {
269 var->data.mode = nir_var_shader_temp;
270 }
271
272 if (var == rv_data->info->backcolor[i] && rv_data->new_backcolor[i]) {
273 var->data.mode = nir_var_shader_temp;
274 }
275 }
276
277 if (var == rv_data->info->fog && rv_data->new_fog) {
278 var->data.mode = nir_var_shader_temp;
279 }
280 }
281
282 static void
rewrite_varying_deref(nir_builder * b,struct replace_varyings_data * rv_data,nir_deref_instr * deref)283 rewrite_varying_deref(nir_builder *b, struct replace_varyings_data *rv_data,
284 nir_deref_instr *deref)
285 {
286 if (deref->deref_type != nir_deref_type_array)
287 return;
288
289 nir_variable *var = nir_deref_instr_get_variable(deref);
290 const struct varying_info *info = rv_data->info;
291 b->cursor = nir_before_instr(&deref->instr);
292
293 /* Replace an array dereference gl_TexCoord[i] with a single
294 * variable dereference representing gl_TexCoord[i].
295 */
296 if (info->lower_texcoord_array && info->texcoord_array == var) {
297 /* gl_TexCoord[i] occurrence */
298 unsigned i = nir_src_as_uint(deref->arr.index);
299 nir_deref_instr *new_deref =
300 nir_build_deref_var(b, rv_data->new_texcoord[i]);
301 nir_ssa_def_rewrite_uses(&deref->dest.ssa, &new_deref->dest.ssa);
302 return;
303 }
304 }
305
306 static void
prepare_array(struct replace_varyings_data * rv_data,nir_shader * shader,nir_variable ** new_var,int max_elements,unsigned start_location,const char * var_name,const char * mode_str,unsigned usage,unsigned external_usage)307 prepare_array(struct replace_varyings_data *rv_data,
308 nir_shader *shader, nir_variable **new_var,
309 int max_elements, unsigned start_location,
310 const char *var_name, const char *mode_str,
311 unsigned usage, unsigned external_usage)
312 {
313 for (int i = max_elements - 1; i >= 0; i--) {
314 if (usage & (1 << i)) {
315 char name[32];
316
317 if (!(external_usage & (1 << i))) {
318 /* This varying is unused in the next stage. Declare
319 * a temporary instead of an output. */
320 snprintf(name, 32, "gl_%s_%s%i_dummy", mode_str, var_name, i);
321 new_var[i] = create_new_var(shader, name, nir_var_shader_temp,
322 glsl_vec4_type());
323 } else {
324 snprintf(name, 32, "gl_%s_%s%i", mode_str, var_name, i);
325 new_var[i] = create_new_var(shader, name, rv_data->info->mode,
326 glsl_vec4_type());
327 new_var[i]->data.location = start_location + i;
328 new_var[i]->data.explicit_location = true;
329 }
330 }
331 }
332 }
333
334 /**
335 * This replaces unused varyings with temporary variables.
336 *
337 * If "ir" is the producer, the "external" usage should come from
338 * the consumer. It also works the other way around. If either one is
339 * missing, set the "external" usage to a full mask.
340 */
341 static void
replace_varyings(const struct gl_constants * consts,struct gl_linked_shader * shader,struct gl_shader_program * prog,const struct varying_info * info,unsigned external_texcoord_usage,unsigned external_color_usage,bool external_has_fog)342 replace_varyings(const struct gl_constants *consts,
343 struct gl_linked_shader *shader,
344 struct gl_shader_program *prog,
345 const struct varying_info *info,
346 unsigned external_texcoord_usage,
347 unsigned external_color_usage, bool external_has_fog)
348 {
349 struct replace_varyings_data rv_data;
350 rv_data.shader = shader;
351 rv_data.info = info;
352 rv_data.consts = consts;
353 rv_data.prog = prog;
354
355 memset(rv_data.new_texcoord, 0, sizeof(rv_data.new_texcoord));
356 memset(rv_data.new_color, 0, sizeof(rv_data.new_color));
357 memset(rv_data.new_backcolor, 0, sizeof(rv_data.new_backcolor));
358 rv_data.new_fog = NULL;
359
360 const char *mode_str = info->mode == nir_var_shader_in ? "in" : "out";
361
362 /* Handle texcoord outputs.
363 *
364 * We're going to break down the gl_TexCoord array into separate
365 * variables. First, add declarations of the new variables all
366 * occurrences of gl_TexCoord will be replaced with.
367 */
368 if (info->lower_texcoord_array) {
369 prepare_array(&rv_data, shader->Program->nir, rv_data.new_texcoord,
370 ARRAY_SIZE(rv_data.new_texcoord),
371 VARYING_SLOT_TEX0, "TexCoord", mode_str,
372 info->texcoord_usage, external_texcoord_usage);
373 }
374
375 /* Create dummy variables which will replace set-but-unused color and
376 * fog outputs.
377 */
378 external_color_usage |= info->tfeedback_color_usage;
379
380 for (int i = 0; i < 2; i++) {
381 char name[32];
382
383 if (!(external_color_usage & (1 << i))) {
384 if (info->color[i]) {
385 snprintf(name, 32, "gl_%s_FrontColor%i_dummy", mode_str, i);
386 rv_data.new_color[i] =
387 create_new_var(shader->Program->nir, name, nir_var_shader_temp,
388 glsl_vec4_type());
389 }
390
391 if (info->backcolor[i]) {
392 snprintf(name, 32, "gl_%s_BackColor%i_dummy", mode_str, i);
393 rv_data.new_backcolor[i] =
394 create_new_var(shader->Program->nir, name, nir_var_shader_temp,
395 glsl_vec4_type());
396 }
397 }
398 }
399
400 if (!external_has_fog && !info->tfeedback_has_fog && info->fog) {
401 char name[32];
402
403 snprintf(name, 32, "gl_%s_FogFragCoord_dummy", mode_str);
404 rv_data.new_fog =
405 create_new_var(shader->Program->nir, name, nir_var_shader_temp,
406 glsl_float_type());
407 }
408
409 /* Now do the replacing. */
410 nir_foreach_variable_with_modes_safe(var, shader->Program->nir, info->mode) {
411 replace_varying(&rv_data, var);
412 }
413
414 nir_function_impl *impl = nir_shader_get_entrypoint(shader->Program->nir);
415 nir_builder b;
416 nir_builder_init(&b, impl);
417
418 /* assert that functions have been inlined before packing is called */
419 nir_foreach_function(f, shader->Program->nir) {
420 assert(f->impl == impl);
421 }
422
423 /* Rewrite the derefs to use the new vars */
424 nir_foreach_block(block, impl) {
425 nir_foreach_instr(instr, block) {
426 if (instr->type != nir_instr_type_intrinsic)
427 continue;
428
429 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
430
431 /* Copies should have been lowered by nir_split_var_copies() before
432 * calling this pass.
433 */
434 assert(intrin->intrinsic != nir_intrinsic_copy_deref);
435
436 if (intrin->intrinsic != nir_intrinsic_load_deref &&
437 intrin->intrinsic != nir_intrinsic_store_deref)
438 continue;
439
440 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
441 if (!nir_deref_mode_is(deref, info->mode))
442 continue;
443
444 rewrite_varying_deref(&b, &rv_data, deref);
445 }
446 }
447 }
448
449 static void
lower_texcoord_array(const struct gl_constants * consts,struct gl_linked_shader * shader,struct gl_shader_program * prog,const struct varying_info * info)450 lower_texcoord_array(const struct gl_constants *consts,
451 struct gl_linked_shader *shader,
452 struct gl_shader_program *prog,
453 const struct varying_info *info)
454 {
455 replace_varyings(consts, shader, prog, info,
456 (1 << MAX_TEXTURE_COORD_UNITS) - 1, 1 | 2, true);
457 }
458
459 void
gl_nir_opt_dead_builtin_varyings(const struct gl_constants * consts,gl_api api,struct gl_shader_program * prog,struct gl_linked_shader * producer,struct gl_linked_shader * consumer,unsigned num_tfeedback_decls,struct xfb_decl * tfeedback_decls)460 gl_nir_opt_dead_builtin_varyings(const struct gl_constants *consts, gl_api api,
461 struct gl_shader_program *prog,
462 struct gl_linked_shader *producer,
463 struct gl_linked_shader *consumer,
464 unsigned num_tfeedback_decls,
465 struct xfb_decl *tfeedback_decls)
466 {
467 /* Lowering of built-in varyings has no effect with the core context and
468 * GLES2, because they are not available there.
469 */
470 if (api == API_OPENGL_CORE ||
471 api == API_OPENGLES2) {
472 goto done;
473 }
474
475 /* Information about built-in varyings. */
476 struct varying_info producer_info;
477 struct varying_info consumer_info;
478 initialise_varying_info(&producer_info, nir_var_shader_out, false);
479 initialise_varying_info(&consumer_info, nir_var_shader_in, false);
480
481 if (producer) {
482 get_varying_info(&producer_info, producer->Program->nir,
483 num_tfeedback_decls, tfeedback_decls);
484
485 if (producer->Stage == MESA_SHADER_TESS_CTRL)
486 producer_info.lower_texcoord_array = false;
487
488 if (!consumer) {
489 /* At least eliminate unused gl_TexCoord elements. */
490 if (producer_info.lower_texcoord_array) {
491 lower_texcoord_array(consts, producer, prog, &producer_info);
492 }
493 goto done;
494 }
495 }
496
497 if (consumer) {
498 get_varying_info(&consumer_info, consumer->Program->nir,
499 num_tfeedback_decls, tfeedback_decls);
500
501 if (consumer->Stage != MESA_SHADER_FRAGMENT)
502 consumer_info.lower_texcoord_array = false;
503
504 if (!producer) {
505 /* At least eliminate unused gl_TexCoord elements. */
506 if (consumer_info.lower_texcoord_array) {
507 lower_texcoord_array(consts, consumer, prog, &consumer_info);
508 }
509 goto done;
510 }
511 }
512
513 /* Eliminate the outputs unused by the consumer. */
514 if (producer_info.lower_texcoord_array ||
515 producer_info.color_usage ||
516 producer_info.has_fog) {
517 replace_varyings(consts, producer, prog, &producer_info,
518 consumer_info.texcoord_usage,
519 consumer_info.color_usage,
520 consumer_info.has_fog);
521 }
522
523 /* The gl_TexCoord fragment shader inputs can be initialized
524 * by GL_COORD_REPLACE, so we can't eliminate them.
525 *
526 * This doesn't prevent elimination of the gl_TexCoord elements which
527 * are not read by the fragment shader. We want to eliminate those anyway.
528 */
529 if (consumer->Stage == MESA_SHADER_FRAGMENT) {
530 producer_info.texcoord_usage = (1 << MAX_TEXTURE_COORD_UNITS) - 1;
531 }
532
533 /* Eliminate the inputs uninitialized by the producer. */
534 if (consumer_info.lower_texcoord_array ||
535 consumer_info.color_usage ||
536 consumer_info.has_fog) {
537 replace_varyings(consts, consumer, prog, &consumer_info,
538 producer_info.texcoord_usage,
539 producer_info.color_usage,
540 producer_info.has_fog);
541 }
542
543 done:
544 if (producer)
545 nir_fixup_deref_modes(producer->Program->nir);
546
547 if (consumer)
548 nir_fixup_deref_modes(consumer->Program->nir);
549 }
550