pan/bi: Pack ld_ubo ops
[mesa.git] / src / panfrost / bifrost / bi_pack.c
1 /*
2 * Copyright (C) 2020 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "compiler.h"
25
26 #define RETURN_PACKED(str) { \
27 uint64_t temp = 0; \
28 memcpy(&temp, &str, sizeof(str)); \
29 return temp; \
30 }
31
32 /* This file contains the final passes of the compiler. Running after
33 * scheduling and RA, the IR is now finalized, so we need to emit it to actual
34 * bits on the wire (as well as fixup branches) */
35
36 static uint64_t
37 bi_pack_header(bi_clause *clause, bi_clause *next, bool is_fragment)
38 {
39 struct bifrost_header header = {
40 .back_to_back = clause->back_to_back,
41 .no_end_of_shader = (next != NULL),
42 .elide_writes = is_fragment,
43 .branch_cond = clause->branch_conditional,
44 .datareg_writebarrier = clause->data_register_write_barrier,
45 .datareg = clause->data_register,
46 .scoreboard_deps = clause->dependencies,
47 .scoreboard_index = clause->scoreboard_id,
48 .clause_type = clause->clause_type,
49 .next_clause_type = next ? next->clause_type : 0,
50 };
51
52 uint64_t u = 0;
53 memcpy(&u, &header, sizeof(header));
54 return u;
55 }
56
57 /* Represents the assignment of ports for a given bundle */
58
59 struct bi_registers {
60 /* Register to assign to each port */
61 unsigned port[4];
62
63 /* Read ports can be disabled */
64 bool enabled[2];
65
66 /* Should we write FMA? what about ADD? If only a single port is
67 * enabled it is in port 2, else ADD/FMA is 2/3 respectively */
68 bool write_fma, write_add;
69
70 /* Should we read with port 3? */
71 bool read_port3;
72
73 /* Packed uniform/constant */
74 uint8_t uniform_constant;
75
76 /* Whether writes are actually for the last instruction */
77 bool first_instruction;
78 };
79
80 /* The uniform/constant slot allows loading a contiguous 64-bit immediate or
81 * pushed uniform per bundle. Figure out which one we need in the bundle (the
82 * scheduler needs to ensure we only have one type per bundle), validate
83 * everything, and rewrite away the register/uniform indices to use 3-bit
84 * sources directly. */
85
86 static unsigned
87 bi_lookup_constant(bi_clause *clause, uint64_t cons)
88 {
89 for (unsigned i = 0; i < clause->constant_count; ++i) {
90 /* Only check top 60-bits since that's what's actually embedded
91 * in the clause, the bottom 4-bits are bundle-inline */
92
93 if ((cons >> 4) == (clause->constants[i] >> 4))
94 return i;
95 }
96
97 unreachable("Invalid constant accessed");
98 }
99
100 static unsigned
101 bi_constant_field(unsigned idx)
102 {
103 assert(idx <= 5);
104
105 const unsigned values[] = {
106 4, 5, 6, 7, 2, 3
107 };
108
109 return values[idx] << 4;
110 }
111
112 static bool
113 bi_assign_uniform_constant_single(
114 struct bi_registers *regs,
115 bi_clause *clause,
116 bi_instruction *ins, bool assigned, bool fast_zero)
117 {
118 if (!ins)
119 return assigned;
120
121 bi_foreach_src(ins, s) {
122 if (ins->src[s] & BIR_INDEX_CONSTANT) {
123 /* TODO: lo/hi matching? */
124 uint64_t cons = ins->constant.u64;
125 unsigned idx = bi_lookup_constant(clause, cons);
126 unsigned f = bi_constant_field(idx) | (cons & 0xF);
127
128 if (assigned && regs->uniform_constant != f)
129 unreachable("Mismatched uniform/const field: imm");
130
131 regs->uniform_constant = f;
132 ins->src[s] = BIR_INDEX_PASS | BIFROST_SRC_CONST_LO;
133 assigned = true;
134 } else if (ins->src[s] & BIR_INDEX_ZERO && (ins->type == BI_LOAD_UNIFORM || ins->type == BI_LOAD_VAR)) {
135 /* XXX: HACK UNTIL WE HAVE HI MATCHING DUE TO OVERFLOW XXX */
136 ins->src[s] = BIR_INDEX_PASS | BIFROST_SRC_CONST_HI;
137 } else if (ins->src[s] & BIR_INDEX_ZERO && !fast_zero) {
138 /* FMAs have a fast zero port, ADD needs to use the
139 * uniform/const port's special 0 mode handled here */
140 unsigned f = 0;
141
142 if (assigned && regs->uniform_constant != f)
143 unreachable("Mismatched uniform/const field: 0");
144
145 regs->uniform_constant = f;
146 ins->src[s] = BIR_INDEX_PASS | BIFROST_SRC_CONST_LO;
147 assigned = true;
148 } else if (s & BIR_INDEX_UNIFORM) {
149 unreachable("Push uniforms not implemented yet");
150 }
151 }
152
153 return assigned;
154 }
155
156 static void
157 bi_assign_uniform_constant(
158 bi_clause *clause,
159 struct bi_registers *regs,
160 bi_bundle bundle)
161 {
162 bool assigned =
163 bi_assign_uniform_constant_single(regs, clause, bundle.fma, false, true);
164
165 bi_assign_uniform_constant_single(regs, clause, bundle.add, assigned, false);
166 }
167
168 /* Assigns a port for reading, before anything is written */
169
170 static void
171 bi_assign_port_read(struct bi_registers *regs, unsigned src)
172 {
173 /* We only assign for registers */
174 if (!(src & BIR_INDEX_REGISTER))
175 return;
176
177 unsigned reg = src & ~BIR_INDEX_REGISTER;
178
179 /* Check if we already assigned the port */
180 for (unsigned i = 0; i <= 1; ++i) {
181 if (regs->port[i] == reg && regs->enabled[i])
182 return;
183 }
184
185 if (regs->port[3] == reg && regs->read_port3)
186 return;
187
188 /* Assign it now */
189
190 for (unsigned i = 0; i <= 1; ++i) {
191 if (!regs->enabled[i]) {
192 regs->port[i] = reg;
193 regs->enabled[i] = true;
194 return;
195 }
196 }
197
198 if (!regs->read_port3) {
199 regs->port[3] = reg;
200 regs->read_port3 = true;
201 }
202 }
203
204 static struct bi_registers
205 bi_assign_ports(bi_bundle now, bi_bundle prev)
206 {
207 struct bi_registers regs = { 0 };
208
209 /* We assign ports for the main register mechanism. Special ops
210 * use the data registers, which has its own mechanism entirely
211 * and thus gets skipped over here. */
212
213 unsigned read_dreg = now.add &&
214 bi_class_props[now.add->type] & BI_DATA_REG_SRC;
215
216 unsigned write_dreg = prev.add &&
217 bi_class_props[prev.add->type] & BI_DATA_REG_DEST;
218
219 /* First, assign reads */
220
221 if (now.fma)
222 bi_foreach_src(now.fma, src)
223 bi_assign_port_read(&regs, now.fma->src[src]);
224
225 if (now.add) {
226 bi_foreach_src(now.add, src) {
227 if (!(src == 0 && read_dreg))
228 bi_assign_port_read(&regs, now.add->src[src]);
229 }
230 }
231
232 /* Next, assign writes */
233
234 if (prev.fma && prev.fma->dest & BIR_INDEX_REGISTER) {
235 regs.port[2] = prev.fma->dest & ~BIR_INDEX_REGISTER;
236 regs.write_fma = true;
237 }
238
239 if (prev.add && prev.add->dest & BIR_INDEX_REGISTER && !write_dreg) {
240 unsigned r = prev.add->dest & ~BIR_INDEX_REGISTER;
241
242 if (regs.write_fma) {
243 /* Scheduler constraint: cannot read 3 and write 2 */
244 assert(!regs.read_port3);
245 regs.port[3] = r;
246 } else {
247 regs.port[2] = r;
248 }
249
250 regs.write_add = true;
251 }
252
253 /* Finally, ensure port 1 > port 0 for the 63-x trick to function */
254
255 if (regs.enabled[0] && regs.enabled[1] && regs.port[1] < regs.port[0]) {
256 unsigned temp = regs.port[0];
257 regs.port[0] = regs.port[1];
258 regs.port[1] = temp;
259 }
260
261 return regs;
262 }
263
264 /* Determines the register control field, ignoring the first? flag */
265
266 static enum bifrost_reg_control
267 bi_pack_register_ctrl_lo(struct bi_registers r)
268 {
269 if (r.write_fma) {
270 if (r.write_add) {
271 assert(!r.read_port3);
272 return BIFROST_WRITE_ADD_P2_FMA_P3;
273 } else {
274 if (r.read_port3)
275 return BIFROST_WRITE_FMA_P2_READ_P3;
276 else
277 return BIFROST_WRITE_FMA_P2;
278 }
279 } else if (r.write_add) {
280 if (r.read_port3)
281 return BIFROST_WRITE_ADD_P2_READ_P3;
282 else
283 return BIFROST_WRITE_ADD_P2;
284 } else if (r.read_port3)
285 return BIFROST_READ_P3;
286 else
287 return BIFROST_REG_NONE;
288 }
289
290 /* Ditto but account for the first? flag this time */
291
292 static enum bifrost_reg_control
293 bi_pack_register_ctrl(struct bi_registers r)
294 {
295 enum bifrost_reg_control ctrl = bi_pack_register_ctrl_lo(r);
296
297 if (r.first_instruction) {
298 if (ctrl == BIFROST_REG_NONE)
299 ctrl = BIFROST_FIRST_NONE;
300 else
301 ctrl |= BIFROST_FIRST_NONE;
302 }
303
304 return ctrl;
305 }
306
307 static uint64_t
308 bi_pack_registers(struct bi_registers regs)
309 {
310 enum bifrost_reg_control ctrl = bi_pack_register_ctrl(regs);
311 struct bifrost_regs s;
312 uint64_t packed = 0;
313
314 if (regs.enabled[1]) {
315 /* Gotta save that bit!~ Required by the 63-x trick */
316 assert(regs.port[1] > regs.port[0]);
317 assert(regs.enabled[0]);
318
319 /* Do the 63-x trick, see docs/disasm */
320 if (regs.port[0] > 31) {
321 regs.port[0] = 63 - regs.port[0];
322 regs.port[1] = 63 - regs.port[1];
323 }
324
325 assert(regs.port[0] <= 31);
326 assert(regs.port[1] <= 63);
327
328 s.ctrl = ctrl;
329 s.reg1 = regs.port[1];
330 s.reg0 = regs.port[0];
331 } else {
332 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
333 s.reg1 = ctrl << 2;
334
335 if (regs.enabled[0]) {
336 /* Bit 0 upper bit of port 0 */
337 s.reg1 |= (regs.port[0] >> 5);
338
339 /* Rest of port 0 in usual spot */
340 s.reg0 = (regs.port[0] & 0b11111);
341 } else {
342 /* Bit 1 set if port 0 also disabled */
343 s.reg1 |= (1 << 1);
344 }
345 }
346
347 s.reg3 = regs.port[3];
348 s.reg2 = regs.port[2];
349 s.uniform_const = regs.uniform_constant;
350
351 memcpy(&packed, &s, sizeof(s));
352 return packed;
353 }
354
355 static void
356 bi_write_data_register(bi_clause *clause, bi_instruction *ins)
357 {
358 assert(ins->dest & BIR_INDEX_REGISTER);
359 unsigned reg = ins->dest & ~BIR_INDEX_REGISTER;
360 assert(reg <= 63);
361 clause->data_register = reg;
362 }
363
364 static enum bifrost_packed_src
365 bi_get_src_reg_port(struct bi_registers *regs, unsigned src)
366 {
367 unsigned reg = src & ~BIR_INDEX_REGISTER;
368
369 if (regs->port[0] == reg && regs->enabled[0])
370 return BIFROST_SRC_PORT0;
371 else if (regs->port[1] == reg && regs->enabled[1])
372 return BIFROST_SRC_PORT1;
373 else if (regs->port[3] == reg && regs->read_port3)
374 return BIFROST_SRC_PORT3;
375 else
376 unreachable("Tried to access register with no port");
377 }
378
379 static enum bifrost_packed_src
380 bi_get_src(bi_instruction *ins, struct bi_registers *regs, unsigned s, bool is_fma)
381 {
382 unsigned src = ins->src[s];
383
384 if (src & BIR_INDEX_REGISTER)
385 return bi_get_src_reg_port(regs, src);
386 else if (src & BIR_INDEX_ZERO && is_fma)
387 return BIFROST_SRC_STAGE;
388 else if (src & BIR_INDEX_PASS)
389 return src & ~BIR_INDEX_PASS;
390 else
391 unreachable("Unknown src");
392 }
393
394 static unsigned
395 bi_pack_fma_fma(bi_instruction *ins, struct bi_registers *regs)
396 {
397 /* (-a)(-b) = ab, so we only need one negate bit */
398 bool negate_mul = ins->src_neg[0] ^ ins->src_neg[1];
399
400 struct bifrost_fma_fma pack = {
401 .src0 = bi_get_src(ins, regs, 0, true),
402 .src1 = bi_get_src(ins, regs, 1, true),
403 .src2 = bi_get_src(ins, regs, 2, true),
404 .src0_abs = ins->src_abs[0],
405 .src1_abs = ins->src_abs[1],
406 .src2_abs = ins->src_abs[2],
407 .src0_neg = negate_mul,
408 .src2_neg = ins->src_neg[2],
409 .op = BIFROST_FMA_OP_FMA
410 };
411
412 RETURN_PACKED(pack);
413 }
414
415 static unsigned
416 bi_pack_fma_add(bi_instruction *ins, struct bi_registers *regs)
417 {
418 /* TODO: fadd16 packing is a bit different */
419 assert(ins->dest_type == nir_type_float32);
420
421 struct bifrost_fma_add pack = {
422 .src0 = bi_get_src(ins, regs, 0, true),
423 .src1 = bi_get_src(ins, regs, 1, true),
424 .src0_abs = ins->src_abs[0],
425 .src1_abs = ins->src_abs[1],
426 .src0_neg = ins->src_neg[0],
427 .src1_neg = ins->src_neg[1],
428 .unk = 0x0,
429 .outmod = ins->outmod,
430 .roundmode = ins->roundmode,
431 .op = BIFROST_FMA_OP_FADD32
432 };
433
434 RETURN_PACKED(pack);
435 }
436
437 static unsigned
438 bi_pack_fma_1src(bi_instruction *ins, struct bi_registers *regs, unsigned op)
439 {
440 struct bifrost_fma_inst pack = {
441 .src0 = bi_get_src(ins, regs, 0, true),
442 .op = op
443 };
444
445 RETURN_PACKED(pack);
446 }
447
448 static unsigned
449 bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
450 {
451 if (!bundle.fma)
452 return BIFROST_FMA_NOP;
453
454 switch (bundle.fma->type) {
455 case BI_ADD:
456 return bi_pack_fma_add(bundle.fma, regs);
457 case BI_CMP:
458 case BI_BITWISE:
459 case BI_CONVERT:
460 case BI_CSEL:
461 return BIFROST_FMA_NOP;
462 case BI_FMA:
463 return bi_pack_fma_fma(bundle.fma, regs);
464 case BI_FREXP:
465 case BI_ISUB:
466 case BI_MINMAX:
467 return BIFROST_FMA_NOP;
468 case BI_MOV:
469 return bi_pack_fma_1src(bundle.fma, regs, BIFROST_FMA_OP_MOV);
470 case BI_FMOV:
471 case BI_SHIFT:
472 case BI_SWIZZLE:
473 case BI_ROUND:
474 return BIFROST_FMA_NOP;
475 default:
476 unreachable("Cannot encode class as FMA");
477 }
478 }
479
480 static unsigned
481 bi_pack_add_ld_vary(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
482 {
483 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
484 assert(size == 32 || size == 16);
485
486 unsigned op = (size == 32) ?
487 BIFROST_ADD_OP_LD_VAR_32 :
488 BIFROST_ADD_OP_LD_VAR_16;
489
490 unsigned cmask = bi_from_bytemask(ins->writemask, size / 8);
491 unsigned channels = util_bitcount(cmask);
492 assert(cmask == ((1 << channels) - 1));
493
494 unsigned packed_addr = 0;
495
496 if (ins->src[0] & BIR_INDEX_CONSTANT) {
497 /* Direct uses address field directly */
498 packed_addr = ins->src[0] & ~BIR_INDEX_CONSTANT;
499 assert(packed_addr < 0b1000);
500 } else {
501 /* Indirect gets an extra source */
502 packed_addr = bi_get_src(ins, regs, 0, false) | 0b11000;
503 }
504
505 /* The destination is thrown in the data register */
506 assert(ins->dest & BIR_INDEX_REGISTER);
507 clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
508
509 assert(channels >= 1 && channels <= 4);
510
511 struct bifrost_ld_var pack = {
512 .src0 = bi_get_src(ins, regs, 1, false),
513 .addr = packed_addr,
514 .channels = MALI_POSITIVE(channels),
515 .interp_mode = ins->load_vary.interp_mode,
516 .reuse = ins->load_vary.reuse,
517 .flat = ins->load_vary.flat,
518 .op = op
519 };
520
521 RETURN_PACKED(pack);
522 }
523
524 static unsigned
525 bi_pack_add_2src(bi_instruction *ins, struct bi_registers *regs, unsigned op)
526 {
527 struct bifrost_add_2src pack = {
528 .src0 = bi_get_src(ins, regs, 0, true),
529 .src1 = bi_get_src(ins, regs, 1, true),
530 .op = op
531 };
532
533 RETURN_PACKED(pack);
534 }
535
536 static unsigned
537 bi_pack_add_ld_ubo(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
538 {
539 unsigned components = bi_load32_components(ins);
540
541 const unsigned ops[4] = {
542 BIFROST_ADD_OP_LD_UBO_1,
543 BIFROST_ADD_OP_LD_UBO_2,
544 BIFROST_ADD_OP_LD_UBO_3,
545 BIFROST_ADD_OP_LD_UBO_4
546 };
547
548 bi_write_data_register(clause, ins);
549 return bi_pack_add_2src(ins, regs, ops[components - 1]);
550 }
551
552 static unsigned
553 bi_pack_add_atest(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
554 {
555 /* TODO: fp16 */
556 assert(ins->src_types[1] == nir_type_float32);
557
558 struct bifrost_add_atest pack = {
559 .src0 = bi_get_src(ins, regs, 0, false),
560 .src1 = bi_get_src(ins, regs, 1, false),
561 .component = 1, /* Set for fp32 */
562 .op = BIFROST_ADD_OP_ATEST,
563 };
564
565 /* Despite *also* writing with the usual mechanism... quirky and
566 * perhaps unnecessary, but let's match the blob */
567 clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
568
569 RETURN_PACKED(pack);
570 }
571
572 static unsigned
573 bi_pack_add_blend(bi_instruction *ins, struct bi_registers *regs)
574 {
575 struct bifrost_add_inst pack = {
576 .src0 = bi_get_src(ins, regs, 0, false),
577 .op = BIFROST_ADD_OP_BLEND
578 };
579
580 /* TODO: Pack location in uniform_const */
581 assert(ins->blend_location == 0);
582
583 RETURN_PACKED(pack);
584 }
585
586 static unsigned
587 bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
588 {
589 if (!bundle.add)
590 return BIFROST_ADD_NOP;
591
592 switch (bundle.add->type) {
593 case BI_ADD:
594 return BIFROST_ADD_NOP;
595 case BI_ATEST:
596 return bi_pack_add_atest(clause, bundle.add, regs);
597 case BI_BRANCH:
598 case BI_CMP:
599 return BIFROST_ADD_NOP;
600 case BI_BLEND:
601 return bi_pack_add_blend(bundle.add, regs);
602 case BI_BITWISE:
603 case BI_CONVERT:
604 case BI_DISCARD:
605 case BI_FREXP:
606 case BI_ISUB:
607 case BI_LOAD:
608 return BIFROST_ADD_NOP;
609 case BI_LOAD_UNIFORM:
610 return bi_pack_add_ld_ubo(clause, bundle.add, regs);
611 case BI_LOAD_ATTR:
612 return BIFROST_ADD_NOP;
613 case BI_LOAD_VAR:
614 return bi_pack_add_ld_vary(clause, bundle.add, regs);
615 case BI_LOAD_VAR_ADDRESS:
616 case BI_MINMAX:
617 case BI_MOV:
618 case BI_FMOV:
619 case BI_SHIFT:
620 case BI_STORE:
621 case BI_STORE_VAR:
622 case BI_SPECIAL:
623 case BI_SWIZZLE:
624 case BI_TEX:
625 case BI_ROUND:
626 return BIFROST_ADD_NOP;
627 default:
628 unreachable("Cannot encode class as ADD");
629 }
630 }
631
632 struct bi_packed_bundle {
633 uint64_t lo;
634 uint64_t hi;
635 };
636
637 static struct bi_packed_bundle
638 bi_pack_bundle(bi_clause *clause, bi_bundle bundle, bi_bundle prev, bool first_bundle)
639 {
640 struct bi_registers regs = bi_assign_ports(bundle, prev);
641 bi_assign_uniform_constant(clause, &regs, bundle);
642 regs.first_instruction = first_bundle;
643
644 uint64_t reg = bi_pack_registers(regs);
645 uint64_t fma = bi_pack_fma(clause, bundle, &regs);
646 uint64_t add = bi_pack_add(clause, bundle, &regs);
647
648 struct bi_packed_bundle packed = {
649 .lo = reg | (fma << 35) | ((add & 0b111111) << 58),
650 .hi = add >> 6
651 };
652
653 return packed;
654 }
655
656 /* Packs the next two constants as a dedicated constant quadword at the end of
657 * the clause, returning the number packed. */
658
659 static unsigned
660 bi_pack_constants(bi_context *ctx, bi_clause *clause,
661 unsigned index,
662 struct util_dynarray *emission)
663 {
664 /* After these two, are we done? Determines tag */
665 bool done = clause->constant_count <= (index + 2);
666 bool only = clause->constant_count <= (index + 1);
667
668 /* TODO: Pos */
669 assert(index == 0 && clause->bundle_count == 1);
670
671 struct bifrost_fmt_constant quad = {
672 .pos = 0, /* TODO */
673 .tag = done ? BIFROST_FMTC_FINAL : BIFROST_FMTC_CONSTANTS,
674 .imm_1 = clause->constants[index + 0] >> 4,
675 .imm_2 = only ? 0 : clause->constants[index + 1] >> 4
676 };
677
678 /* XXX: On G71, Connor observed that the difference of the top 4 bits
679 * of the second constant with the first must be less than 8, otherwise
680 * we have to swap them. I am not able to reproduce this on G52,
681 * further investigation needed. Possibly an errata. XXX */
682
683 util_dynarray_append(emission, struct bifrost_fmt_constant, quad);
684
685 return 2;
686 }
687
688 static void
689 bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
690 struct util_dynarray *emission)
691 {
692 struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0], clause->bundles[0], true);
693 assert(clause->bundle_count == 1);
694
695 /* Used to decide if we elide writes */
696 bool is_fragment = ctx->stage == MESA_SHADER_FRAGMENT;
697
698 /* State for packing constants throughout */
699 unsigned constant_index = 0;
700
701 struct bifrost_fmt1 quad_1 = {
702 .tag = clause->constant_count ? BIFROST_FMT1_CONSTANTS : BIFROST_FMT1_FINAL,
703 .header = bi_pack_header(clause, next, is_fragment),
704 .ins_1 = ins_1.lo,
705 .ins_2 = ins_1.hi & ((1 << 11) - 1),
706 .ins_0 = (ins_1.hi >> 11) & 0b111,
707 };
708
709 util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
710
711 /* Pack the remaining constants */
712
713 while (constant_index < clause->constant_count) {
714 constant_index += bi_pack_constants(ctx, clause,
715 constant_index, emission);
716 }
717 }
718
719 static bi_clause *
720 bi_next_clause(bi_context *ctx, pan_block *block, bi_clause *clause)
721 {
722 /* Try the next clause in this block */
723 if (clause->link.next != &((bi_block *) block)->clauses)
724 return list_first_entry(&(clause->link), bi_clause, link);
725
726 /* Try the next block, or the one after that if it's empty, etc .*/
727 pan_block *next_block = pan_next_block(block);
728
729 bi_foreach_block_from(ctx, next_block, block) {
730 bi_block *blk = (bi_block *) block;
731
732 if (!list_is_empty(&blk->clauses))
733 return list_first_entry(&(blk->clauses), bi_clause, link);
734 }
735
736 return NULL;
737 }
738
739 void
740 bi_pack(bi_context *ctx, struct util_dynarray *emission)
741 {
742 util_dynarray_init(emission, NULL);
743
744 bi_foreach_block(ctx, _block) {
745 bi_block *block = (bi_block *) _block;
746
747 bi_foreach_clause_in_block(block, clause) {
748 bi_clause *next = bi_next_clause(ctx, _block, clause);
749 bi_pack_clause(ctx, clause, next, emission);
750 }
751 }
752 }