nir: Add a face_sysval argument to nir_lower_two_sided_color
[mesa.git] / src / freedreno / ir3 / ir3_nir.c
index 196c99f302435741837dcd08db5d41bc37fc1e81..c779a57f7c5176c85161137b5dffb50b15f2aa62 100644 (file)
@@ -194,6 +194,7 @@ ir3_optimize_loop(nir_shader *s)
                        OPT(s, nir_opt_dce);
                }
                progress |= OPT(s, nir_opt_if, false);
+               progress |= OPT(s, nir_opt_loop_unroll, nir_var_all);
                progress |= OPT(s, nir_opt_remove_phis);
                progress |= OPT(s, nir_opt_undef);
        } while (progress);
@@ -305,6 +306,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)
 {
@@ -324,7 +380,7 @@ ir3_nir_lower_variant(struct ir3_shader_variant *so, nir_shader *s)
                        break;
                case MESA_SHADER_TESS_CTRL:
                        NIR_PASS_V(s, ir3_nir_lower_tess_ctrl, so, so->key.tessellation);
-                       NIR_PASS_V(s, ir3_nir_lower_to_explicit_input);
+                       NIR_PASS_V(s, ir3_nir_lower_to_explicit_input, so->shader->compiler);
                        progress = true;
                        break;
                case MESA_SHADER_TESS_EVAL:
@@ -334,7 +390,7 @@ ir3_nir_lower_variant(struct ir3_shader_variant *so, nir_shader *s)
                        progress = true;
                        break;
                case MESA_SHADER_GEOMETRY:
-                       NIR_PASS_V(s, ir3_nir_lower_to_explicit_input);
+                       NIR_PASS_V(s, ir3_nir_lower_to_explicit_input, so->shader->compiler);
                        progress = true;
                        break;
                default:
@@ -352,9 +408,11 @@ 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);
+               OPT_V(s, nir_lower_two_sided_color, true);
                progress = true;
        }