pan/bi: Document constant related errata(?)
[mesa.git] / src / panfrost / bifrost / bi_pack.c
index 65218b2301079529e192f8ed2b3a0e0e7b1432c2..d22b8696648e3db1b94b46aa54f8fc6cbf0ec006 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;
@@ -307,6 +315,8 @@ bi_get_src(bi_instruction *ins, struct bi_registers *regs, unsigned s, bool is_f
                 return bi_get_src_const(regs, 0);
         else if (src & BIR_INDEX_PASS)
                 return src & ~BIR_INDEX_PASS;
+        else if (src & BIR_INDEX_CONSTANT)
+                return bi_get_src_const(regs, 0); /*TODO ins->constant.u64 */
         else
                 unreachable("Unknown src");
 }
@@ -354,6 +364,17 @@ bi_pack_fma_add(bi_instruction *ins, struct bi_registers *regs)
         RETURN_PACKED(pack);
 }
 
+static unsigned
+bi_pack_fma_1src(bi_instruction *ins, struct bi_registers *regs, unsigned op)
+{
+        struct bifrost_fma_inst pack = {
+                .src0 = bi_get_src(ins, regs, 0, true),
+                .op = op
+        };
+
+        RETURN_PACKED(pack);
+}
+
 static unsigned
 bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
 {
@@ -373,7 +394,10 @@ bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
         case BI_FREXP:
         case BI_ISUB:
         case BI_MINMAX:
+                return BIFROST_FMA_NOP;
         case BI_MOV:
+                return bi_pack_fma_1src(bundle.fma, regs, BIFROST_FMA_OP_MOV);
+        case BI_FMOV:
         case BI_SHIFT:
         case BI_SWIZZLE:
         case BI_ROUND:
@@ -384,7 +408,7 @@ bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
 }
 
 static unsigned
-bi_pack_add_ld_vary(bi_instruction *ins, struct bi_registers *regs)
+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);
@@ -408,6 +432,10 @@ bi_pack_add_ld_vary(bi_instruction *ins, struct bi_registers *regs)
                 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 = {
@@ -423,6 +451,40 @@ bi_pack_add_ld_vary(bi_instruction *ins, struct bi_registers *regs)
         RETURN_PACKED(pack);
 }
 
+static unsigned
+bi_pack_add_atest(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
+{
+        /* TODO: fp16 */
+        assert(ins->src_types[1] == nir_type_float32);
+
+        struct bifrost_add_atest pack = {
+                .src0 = bi_get_src(ins, regs, 0, false),
+                .src1 = bi_get_src(ins, regs, 1, false),
+                .component = 1, /* Set for fp32 */
+                .op = BIFROST_ADD_OP_ATEST,
+        };
+
+        /* Despite *also* writing with the usual mechanism... quirky and
+         * perhaps unnecessary, but let's match the blob */
+        clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
+
+        RETURN_PACKED(pack);
+}
+
+static unsigned
+bi_pack_add_blend(bi_instruction *ins, struct bi_registers *regs)
+{
+        struct bifrost_add_inst pack = {
+                .src0 = bi_get_src(ins, regs, 0, false),
+                .op = BIFROST_ADD_OP_BLEND
+        };
+
+        /* TODO: Pack location in uniform_const */
+        assert(ins->blend_location == 0);
+
+        RETURN_PACKED(pack);
+}
+
 static unsigned
 bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
 {
@@ -431,10 +493,14 @@ bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
 
         switch (bundle.add->type) {
         case BI_ADD:
+                return BIFROST_ADD_NOP;
         case BI_ATEST:
+                return bi_pack_add_atest(clause, bundle.add, regs);
         case BI_BRANCH:
         case BI_CMP:
+                return BIFROST_ADD_NOP;
         case BI_BLEND:
+                return bi_pack_add_blend(bundle.add, regs);
         case BI_BITWISE:
         case BI_CONVERT:
         case BI_DISCARD:
@@ -445,10 +511,11 @@ bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
         case BI_LOAD_ATTR:
                 return BIFROST_ADD_NOP;
         case BI_LOAD_VAR:
-                return bi_pack_add_ld_vary(bundle.add, regs);
+                return bi_pack_add_ld_vary(clause, bundle.add, regs);
         case BI_LOAD_VAR_ADDRESS:
         case BI_MINMAX:
         case BI_MOV:
+        case BI_FMOV:
         case BI_SHIFT:
         case BI_STORE:
         case BI_STORE_VAR:
@@ -485,6 +552,38 @@ bi_pack_bundle(bi_clause *clause, bi_bundle bundle, bi_bundle prev, bool first_b
         return packed;
 }
 
+/* Packs the next two constants as a dedicated constant quadword at the end of
+ * the clause, returning the number packed. */
+
+static unsigned
+bi_pack_constants(bi_context *ctx, bi_clause *clause,
+                unsigned index,
+                struct util_dynarray *emission)
+{
+        /* After these two, are we done? Determines tag */
+        bool done = clause->constant_count <= (index + 2);
+        bool only = clause->constant_count <= (index + 1);
+
+        /* TODO: Pos */
+        assert(index == 0 && clause->bundle_count == 1);
+
+        struct bifrost_fmt_constant quad = {
+                .pos = 0, /* TODO */
+                .tag = done ? BIFROST_FMTC_FINAL : BIFROST_FMTC_CONSTANTS,
+                .imm_1 = clause->constants[index + 0] >> 4,
+                .imm_2 = only ? 0 : clause->constants[index + 1] >> 4
+        };
+
+        /* XXX: On G71, Connor observed that the difference of the top 4 bits
+         * of the second constant with the first must be less than 8, otherwise
+         * we have to swap them. I am not able to reproduce this on G52,
+         * further investigation needed. Possibly an errata. XXX */
+
+        util_dynarray_append(emission, struct bifrost_fmt_constant, quad);
+
+        return 2;
+}
+
 static void
 bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
                 struct util_dynarray *emission)
@@ -492,15 +591,28 @@ 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;
+
+        /* State for packing constants throughout */
+        unsigned constant_index = 0;
+
         struct bifrost_fmt1 quad_1 = {
-                .tag = BIFROST_FMT1_FINAL,
-                .header = bi_pack_header(clause, next),
+                .tag = clause->constant_count ? BIFROST_FMT1_CONSTANTS : BIFROST_FMT1_FINAL,
+                .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,
         };
 
         util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
+
+        /* Pack the remaining constants */
+
+        while (constant_index < clause->constant_count) {
+                constant_index += bi_pack_constants(ctx, clause,
+                                constant_index, emission);
+        }
 }
 
 static bi_clause *