ir3: Add layer_zero variant bit
authorConnor Abbott <cwabbott0@gmail.com>
Fri, 3 Jul 2020 10:01:17 +0000 (12:01 +0200)
committerMarge Bot <eric+marge@anholt.net>
Tue, 7 Jul 2020 08:10:47 +0000 (08:10 +0000)
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5732>

src/freedreno/ir3/ir3_nir.c
src/freedreno/ir3/ir3_shader.c
src/freedreno/ir3/ir3_shader.h

index 196c99f302435741837dcd08db5d41bc37fc1e81..f37d29947997e5349ada7e5b2ca2c34e7562772a 100644 (file)
@@ -305,6 +305,61 @@ ir3_nir_post_finalize(struct ir3_compiler *compiler, nir_shader *s)
        ir3_optimize_loop(s);
 }
 
+static bool
+ir3_nir_lower_layer_id(nir_shader *nir)
+{
+       unsigned layer_id_loc = ~0;
+       nir_foreach_variable(var, &nir->inputs) {
+               if (var->data.location == VARYING_SLOT_LAYER) {
+                       layer_id_loc = var->data.driver_location;
+                       break;
+               }
+       }
+
+       assert(layer_id_loc != ~0);
+
+       bool progress = false;
+       nir_builder b;
+
+       nir_foreach_function(func, nir) {
+               nir_builder_init(&b, func->impl);
+
+               nir_foreach_block(block, func->impl) {
+                       nir_foreach_instr_safe(instr, block) {
+                               if (instr->type != nir_instr_type_intrinsic)
+                                       continue;
+
+                               nir_intrinsic_instr *intrin =
+                                       nir_instr_as_intrinsic(instr);
+
+                               if (intrin->intrinsic != nir_intrinsic_load_input)
+                                       continue;
+
+                               unsigned base = nir_intrinsic_base(intrin);
+                               if (base != layer_id_loc)
+                                       continue;
+
+                               b.cursor = nir_before_instr(&intrin->instr);
+                               nir_ssa_def *zero = nir_imm_int(&b, 0);
+                               nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
+                                                                                nir_src_for_ssa(zero));
+                               nir_instr_remove(&intrin->instr);
+                               progress = true;
+                       }
+               }
+
+               if (progress) {
+                       nir_metadata_preserve(func->impl,
+                                                                 nir_metadata_block_index |
+                                                                 nir_metadata_dominance);
+               } else {
+                       nir_metadata_preserve(func->impl, nir_metadata_all);
+               }
+       }
+
+       return progress;
+}
+
 void
 ir3_nir_lower_variant(struct ir3_shader_variant *so, nir_shader *s)
 {
@@ -352,6 +407,8 @@ ir3_nir_lower_variant(struct ir3_shader_variant *so, nir_shader *s)
                        progress |= OPT(s, nir_lower_clip_fs, so->key.ucp_enables, false);
                if (so->key.fclamp_color)
                        progress |= OPT(s, nir_lower_clamp_color_outputs);
+               if (so->key.layer_zero && (s->info.inputs_read & VARYING_BIT_LAYER))
+                       progress |= OPT(s, ir3_nir_lower_layer_id);
        }
        if (so->key.color_two_side) {
                OPT_V(s, nir_lower_two_sided_color);
index 984ae57418daf5397af96177a273773713fc682e..9771c66faf6f4136ad4cde45d8cf1de68c94ed9a 100644 (file)
@@ -354,6 +354,10 @@ ir3_setup_used_key(struct ir3_shader *shader)
                        key->color_two_side = true;
                }
 
+               if (info->inputs_read & VARYING_BIT_LAYER) {
+                       key->layer_zero = true;
+               }
+
                if ((info->outputs_written & ~(FRAG_RESULT_DEPTH |
                                                                FRAG_RESULT_STENCIL |
                                                                FRAG_RESULT_SAMPLE_MASK)) != 0) {
index 7430bed03f76531684593a1024271082b47515a1..f581084711204238f0581f3f27a3113a9bf1305b 100644 (file)
@@ -318,6 +318,9 @@ struct ir3_shader_key {
                         * the limit:
                         */
                        unsigned safe_constlen : 1;
+
+                       /* Whether gl_Layer must be forced to 0 because it isn't written. */
+                       unsigned layer_zero : 1;
                };
                uint32_t global;
        };
@@ -385,6 +388,9 @@ ir3_shader_key_changes_fs(struct ir3_shader_key *key, struct ir3_shader_key *las
        if (last_key->rasterflat != key->rasterflat)
                return true;
 
+       if (last_key->layer_zero != key->layer_zero)
+               return true;
+
        if (last_key->ucp_enables != key->ucp_enables)
                return true;