pan/bi: Route through clause header
[mesa.git] / src / panfrost / bifrost / bi_pack.c
index 2637f427cbb2ccb71c31ccd97e4a4dbbd7ebb831..2f961650d3ed8ca2ded37a359d1507ee0e68eaee 100644 (file)
  * bits on the wire (as well as fixup branches) */
 
 static uint64_t
-bi_pack_header(bi_clause *clause, bi_clause *next)
+bi_pack_header(bi_clause *clause, bi_clause *next, bool is_fragment)
 {
         struct bifrost_header header = {
-                /* stub */
+                .back_to_back = clause->back_to_back,
                 .no_end_of_shader = (next != NULL),
+                .elide_writes = is_fragment,
+                .branch_cond = clause->branch_conditional,
+                .datareg_writebarrier = clause->data_register_write_barrier,
+                .datareg = clause->data_register,
+                .scoreboard_deps = clause->dependencies,
+                .scoreboard_index = clause->scoreboard_id,
+                .clause_type = clause->clause_type,
+                .next_clause_type = next ? next->clause_type : 0,
         };
 
         uint64_t u = 0;
@@ -110,15 +118,28 @@ bi_assign_ports(bi_bundle now, bi_bundle prev)
 {
         struct bi_registers regs = { 0 };
 
+        /* We assign ports for the main register mechanism. Special ops
+         * use the data registers, which has its own mechanism entirely
+         * and thus gets skipped over here. */
+
+        unsigned read_dreg = now.add &&
+                bi_class_props[now.add->type] & BI_DATA_REG_SRC;
+
+        unsigned write_dreg = prev.add &&
+                bi_class_props[prev.add->type] & BI_DATA_REG_DEST;
+
         /* First, assign reads */
 
         if (now.fma)
                 bi_foreach_src(now.fma, src)
                         bi_assign_port_read(&regs, now.fma->src[src]);
 
-        if (now.add)
-                bi_foreach_src(now.add, src)
-                        bi_assign_port_read(&regs, now.add->src[src]);
+        if (now.add) {
+                bi_foreach_src(now.add, src) {
+                        if (!(src == 0 && read_dreg))
+                                bi_assign_port_read(&regs, now.add->src[src]);
+                }
+        }
 
         /* Next, assign writes */
 
@@ -127,7 +148,7 @@ bi_assign_ports(bi_bundle now, bi_bundle prev)
                 regs.write_fma = true;
         }
 
-        if (prev.add && prev.add->dest & BIR_INDEX_REGISTER) {
+        if (prev.add && prev.add->dest & BIR_INDEX_REGISTER && !write_dreg) {
                 unsigned r = prev.add->dest & ~BIR_INDEX_REGISTER;
 
                 if (regs.write_fma) {
@@ -370,6 +391,50 @@ bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
         }
 }
 
+static unsigned
+bi_pack_add_ld_vary(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
+{
+        unsigned size = nir_alu_type_get_type_size(ins->dest_type);
+        assert(size == 32 || size == 16);
+
+        unsigned op = (size == 32) ?
+                BIFROST_ADD_OP_LD_VAR_32 :
+                BIFROST_ADD_OP_LD_VAR_16;
+
+        unsigned cmask = bi_from_bytemask(ins->writemask, size / 8);
+        unsigned channels = util_bitcount(cmask);
+        assert(cmask == ((1 << channels) - 1));
+
+        unsigned packed_addr = 0;
+
+        if (ins->src[0] & BIR_INDEX_CONSTANT) {
+                /* Direct uses address field directly */
+                packed_addr = ins->src[0] & ~BIR_INDEX_CONSTANT;
+                assert(packed_addr < 0b1000);
+        } else {
+                /* Indirect gets an extra source */
+                packed_addr = bi_get_src(ins, regs, 0, false) | 0b11000;
+        }
+
+        /* The destination is thrown in the data register */
+        assert(ins->dest & BIR_INDEX_REGISTER);
+        clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
+
+        assert(channels >= 1 && channels <= 4);
+
+        struct bifrost_ld_var pack = {
+                .src0 = bi_get_src(ins, regs, 1, false),
+                .addr = packed_addr,
+                .channels = MALI_POSITIVE(channels),
+                .interp_mode = ins->load_vary.interp_mode,
+                .reuse = ins->load_vary.reuse,
+                .flat = ins->load_vary.flat,
+                .op = op
+        };
+
+        RETURN_PACKED(pack);
+}
+
 static unsigned
 bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
 {
@@ -390,7 +455,9 @@ bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
         case BI_LOAD:
         case BI_LOAD_UNIFORM:
         case BI_LOAD_ATTR:
+                return BIFROST_ADD_NOP;
         case BI_LOAD_VAR:
+                return bi_pack_add_ld_vary(clause, bundle.add, regs);
         case BI_LOAD_VAR_ADDRESS:
         case BI_MINMAX:
         case BI_MOV:
@@ -437,9 +504,12 @@ bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
         struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0], clause->bundles[0], true);
         assert(clause->bundle_count == 1);
 
+        /* Used to decide if we elide writes */
+        bool is_fragment = ctx->stage == MESA_SHADER_FRAGMENT;
+
         struct bifrost_fmt1 quad_1 = {
                 .tag = BIFROST_FMT1_FINAL,
-                .header = bi_pack_header(clause, next),
+                .header = bi_pack_header(clause, next, is_fragment),
                 .ins_1 = ins_1.lo,
                 .ins_2 = ins_1.hi & ((1 << 11) - 1),
                 .ins_0 = (ins_1.hi >> 11) & 0b111,