2 * Copyright (C) 2019 Connor Abbott <cwabbott0@gmail.com>
3 * Copyright (C) 2019 Lyude Paul <thatslyude@gmail.com>
4 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 enum bifrost_clause_type
{
33 BIFROST_CLAUSE_NONE
= 0,
34 BIFROST_CLAUSE_LOAD_VARY
= 1,
35 BIFROST_CLAUSE_UBO
= 2,
36 BIFROST_CLAUSE_TEX
= 3,
37 BIFROST_CLAUSE_SSBO_LOAD
= 5,
38 BIFROST_CLAUSE_SSBO_STORE
= 6,
39 BIFROST_CLAUSE_BLEND
= 9,
40 BIFROST_CLAUSE_ATEST
= 13,
41 BIFROST_CLAUSE_64BIT
= 15
44 struct bifrost_header
{
46 // If true, convert any infinite result of any floating-point operation to
47 // the biggest representable number.
48 unsigned suppress_inf
: 1;
49 // Convert any NaN results to 0.
50 unsigned suppress_nan
: 1;
52 // true if the execution mask of the next clause is the same as the mask of
53 // the current clause.
54 unsigned back_to_back
: 1;
55 unsigned no_end_of_shader
: 1;
57 // Set to true for fragment shaders, to implement this bit of spec text
58 // from section 7.1.5 of the GLSL ES spec:
60 // "Stores to image and buffer variables performed by helper invocations
61 // have no effect on the underlying image or buffer memory."
63 // Helper invocations are threads (invocations) corresponding to pixels in
64 // a quad that aren't actually part of the triangle, but are included to
65 // make derivatives work correctly. They're usually turned on, but they
66 // need to be masked off for GLSL-level stores. This bit seems to be the
67 // only bit that's actually different between fragment shaders and other
68 // shaders, so this is probably what it's doing.
69 unsigned elide_writes
: 1;
70 // If backToBack is off:
71 // - true for conditional branches and fallthrough
72 // - false for unconditional branches
73 // The blob seems to always set it to true if back-to-back is on.
74 unsigned branch_cond
: 1;
75 // This bit is set when the next clause writes to the data register of some
77 unsigned datareg_writebarrier
: 1;
79 unsigned scoreboard_deps
: 8;
80 unsigned scoreboard_index
: 3;
81 enum bifrost_clause_type clause_type
: 4;
82 unsigned unk3
: 1; // part of clauseType?
83 enum bifrost_clause_type next_clause_type
: 4;
84 unsigned unk4
: 1; // part of nextClauseType?
85 } __attribute__((packed
));
87 enum bifrost_packed_src
{
88 BIFROST_SRC_PORT0
= 0,
89 BIFROST_SRC_PORT1
= 1,
90 BIFROST_SRC_PORT3
= 2,
91 BIFROST_SRC_STAGE
= 3,
92 BIFROST_SRC_CONST_LO
= 4,
93 BIFROST_SRC_CONST_HI
= 5,
94 BIFROST_SRC_PASS_FMA
= 6,
95 BIFROST_SRC_PASS_ADD
= 7,
98 #define BIFROST_FMA_EXT (0xe0000)
99 #define BIFROST_FMA_OP_MOV BIFROST_FMA_EXT | (0x32d)
100 #define BIFROST_FMA_OP_FREXPE_LOG BIFROST_FMA_EXT | 0x3c5
101 #define BIFROST_FMA_OP_ADD_FREXPM ((BIFROST_FMA_EXT | 0x1e80) >> 3)
102 #define BIFROST_FMA_SEL_16(swiz) (((BIFROST_FMA_EXT | 0x1e00) >> 3) | (swiz))
104 struct bifrost_fma_inst
{
107 } __attribute__((packed
));
109 struct bifrost_fma_2src
{
113 } __attribute__((packed
));
115 #define BIFROST_FMA_OP_MSCALE (0x50 >> 3)
117 struct bifrost_fma_mscale
{
123 /* If mscale_mode is set - an MSCALE specific mode. If it is not set, a
126 unsigned mscale_mode
: 1;
128 unsigned src0_abs
: 1;
129 unsigned src1_neg
: 1;
130 unsigned src2_neg
: 1;
132 } __attribute__((packed
));
134 #define BIFROST_ADD_OP_BLEND (0x1952c)
135 #define BIFROST_ADD_OP_FRCP_FAST_F32 (0x0cc00)
136 #define BIFROST_ADD_OP_FRCP_FAST_F16_X (0x0ce10)
137 #define BIFROST_ADD_OP_FRCP_FAST_F16_Y (0x0ce30)
138 #define BIFROST_ADD_OP_FRSQ_FAST_F32 (0x0cc20)
139 #define BIFROST_ADD_OP_FRSQ_FAST_F16_X (0x0ce50)
140 #define BIFROST_ADD_OP_FRSQ_FAST_F16_Y (0x0ce70)
141 #define BIFROST_ADD_OP_LOG2_HELP (0x0cc68)
142 #define BIFROST_ADD_OP_FEXP2_FAST (0x0cd58)
144 struct bifrost_add_inst
{
147 } __attribute__((packed
));
149 #define BIFROST_ADD_OP_LD_UBO_1 (0x0c1a0 >> 3)
150 #define BIFROST_ADD_OP_LD_UBO_2 (0x0c1e0 >> 3)
151 #define BIFROST_ADD_OP_LD_UBO_3 (0x0caa0 >> 3)
152 #define BIFROST_ADD_OP_LD_UBO_4 (0x0c220 >> 3)
153 #define BIFROST_ADD_SEL_16(swiz) ((0xea60 >> 3) | (swiz))
155 struct bifrost_add_2src
{
159 } __attribute__((packed
));
161 #define BIFROST_ADD_OP_FMAX32 (0x00)
162 #define BIFROST_ADD_OP_FMIN32 (0x01)
163 #define BIFROST_ADD_OP_FADD32 (0x02)
165 #define BIFROST_ADD_OP_FADD16 (0x0A)
167 struct bifrost_add_faddmin
{
170 unsigned src1_abs
: 1;
171 unsigned src0_neg
: 1;
172 unsigned src1_neg
: 1;
173 unsigned select
: 2; /* swizzle_0 for fp16 */
174 unsigned outmod
: 2; /* swizzle_1 for fp16 */
176 unsigned src0_abs
: 1;
178 } __attribute__((packed
));
180 #define BIFROST_ADD_OP_FMAX16 (0x10)
181 #define BIFROST_ADD_OP_FMIN16 (0x12)
183 struct bifrost_add_fmin16
{
186 /* abs2 inferred as with FMA */
188 unsigned src0_neg
: 1;
189 unsigned src1_neg
: 1;
190 unsigned src0_swizzle
: 2;
191 unsigned src1_swizzle
: 2;
194 } __attribute__((packed
));
196 #define BIFROST_ADD_OP_ST_VAR (0x19300 >> 8)
198 struct bifrost_st_vary
{
202 unsigned channels
: 2;
204 } __attribute__((packed
));
206 #define BIFROST_ADD_OP_ATEST (0xc8f)
208 struct bifrost_add_atest
{
209 /* gl_SampleMask (R60) */
215 /* If half, X/Y select. If !half, always set */
216 unsigned component
: 1;
220 } __attribute__((packed
));
222 enum bifrost_outmod
{
225 BIFROST_SAT_SIGNED
= 0x2,
229 enum bifrost_roundmode
{
230 BIFROST_RTE
= 0x0, /* round to even */
231 BIFROST_RTP
= 0x1, /* round to positive */
232 BIFROST_RTN
= 0x2, /* round to negative */
233 BIFROST_RTZ
= 0x3 /* round to zero */
236 /* NONE: Same as fmax() and fmin() -- return the other
237 * number if any number is NaN. Also always return +0 if
238 * one argument is +0 and the other is -0.
240 * NAN_WINS: Instead of never returning a NaN, always return
241 * one. The "greater"/"lesser" NaN is always returned, first
242 * by checking the sign and then the mantissa bits.
244 * SRC1_WINS: For max, implement src0 > src1 ? src0 : src1.
245 * For min, implement src0 < src1 ? src0 : src1. This
246 * includes handling NaN's and signedness of 0 differently
247 * from above, since +0 and -0 compare equal and comparisons
248 * always return false for NaN's. As a result, this mode is
251 * SRC0_WINS: For max, implement src0 < src1 ? src1 : src0
252 * For min, implement src0 > src1 ? src1 : src0
256 enum bifrost_minmax_mode
{
257 BIFROST_MINMAX_NONE
= 0x0,
258 BIFROST_NAN_WINS
= 0x1,
259 BIFROST_SRC1_WINS
= 0x2,
260 BIFROST_SRC0_WINS
= 0x3,
263 #define BIFROST_FMA_OP_FADD32 (0x58 >> 2)
264 #define BIFROST_FMA_OP_FMAX32 (0x40 >> 2)
265 #define BIFROST_FMA_OP_FMIN32 (0x44 >> 2)
267 struct bifrost_fma_add
{
270 unsigned src1_abs
: 1;
271 unsigned src0_neg
: 1;
272 unsigned src1_neg
: 1;
274 unsigned src0_abs
: 1;
275 enum bifrost_roundmode roundmode
: 2;
276 enum bifrost_outmod outmod
: 2;
278 } __attribute__((packed
));
280 #define BIFROST_FMA_OP_FMAX16 (0xC0 >> 2)
281 #define BIFROST_FMA_OP_FMIN16 (0xCC >> 2)
282 #define BIFROST_FMA_OP_FADD16 (0xD8 >> 2)
284 struct bifrost_fma_add_minmax16
{
287 /* abs2 inferred as (src1 < src0) */
289 unsigned src0_neg
: 1;
290 unsigned src1_neg
: 1;
291 unsigned src0_swizzle
: 2;
292 unsigned src1_swizzle
: 2;
294 enum bifrost_outmod outmod
: 2;
295 /* roundmode for add, min/max mode for min/max */
297 } __attribute__((packed
));
299 #define BIFROST_FMA_OP_FMA (0x00)
301 struct bifrost_fma_fma
{
305 unsigned src_expand
: 3;
306 unsigned src0_abs
: 1;
307 enum bifrost_roundmode roundmode
: 2;
308 enum bifrost_outmod outmod
: 2;
309 unsigned src0_neg
: 1; /* 14 */
310 unsigned src2_neg
: 1;
311 unsigned src1_abs
: 1;
312 unsigned src2_abs
: 1; /* 17 */
314 } __attribute__((packed
));
316 #define BIFROST_FMA_OP_FMA16 (0x2)
318 struct bifrost_fma_fma16
{
322 unsigned swizzle_0
: 2;
323 unsigned swizzle_1
: 2;
324 enum bifrost_roundmode roundmode
: 2;
325 enum bifrost_outmod outmod
: 2;
326 unsigned src0_neg
: 1;
327 unsigned src2_neg
: 1;
328 unsigned swizzle_2
: 2;
330 } __attribute__((packed
));
332 enum bifrost_csel_cond
{
343 #define BIFROST_FMA_OP_CSEL4 (0x5c)
344 #define BIFROST_FMA_OP_CSEL4_V16 (0xdc)
346 struct bifrost_csel4
{
351 enum bifrost_csel_cond cond
: 3;
353 } __attribute__((packed
));
355 struct bifrost_shift_fma
{
359 unsigned half
: 3; /* 000 for i32, 100 for i8, 111 for v2i16 */
360 unsigned unk
: 1; /* always set? */
361 unsigned invert_1
: 1; /* Inverts sources to combining op */
362 /* For XOR, switches RSHIFT to LSHIFT since only one invert needed */
363 unsigned invert_2
: 1;
365 } __attribute__((packed
));
367 struct bifrost_shift_add
{
373 unsigned invert_1
: 1;
374 unsigned invert_2
: 1;
377 } __attribute__((packed
));
379 /* Two sources for vectorization */
380 #define BIFROST_FMA_FLOAT32_TO_16 (0xdd000 >> 3)
381 #define BIFROST_ADD_FLOAT32_TO_16 (0x0EC00 >> 3)
383 enum bifrost_convert_mode
{
384 BIFROST_CONV_UNK0
= 0,
385 BIFROST_CONV_F32_TO_I32
= 1,
386 BIFROST_CONV_F16_TO_I16
= 2,
387 BIFROST_CONV_I32_TO_F32
= 3,
388 BIFROST_CONV_I16_TO_X32
= 4,
389 BIFROST_CONV_F16_TO_F32
= 5,
390 BIFROST_CONV_I16_TO_F16
= 6,
391 BIFROST_CONV_UNK7
= 7
395 #define BIFROST_CONVERT_4(is_unsigned, component, to_float) \
396 ((is_unsigned & 1) | ((component & 1) << 1) | ((to_float & 1) << 2) | \
397 ((0x3) << 3) | ((4) << 5) | 0x100)
400 #define BIFROST_CONVERT_5(component) \
401 ((component & 1) | ((1) << 1) | ((5) << 5) | 0x100)
403 /* Other conversions */
404 #define BIFROST_CONVERT(is_unsigned, roundmode, swizzle, mode) \
405 ((is_unsigned & 1) | ((roundmode & 3) << 1) | ((swizzle & 3) << 3) | ((mode & 7) << 5))
407 #define BIFROST_FMA_CONVERT (0xe0000)
408 #define BIFROST_ADD_CONVERT (0x07800)
410 enum bifrost_ldst_type
{
411 BIFROST_LDST_F16
= 0,
412 BIFROST_LDST_F32
= 1,
413 BIFROST_LDST_I32
= 2,
417 #define BIFROST_ADD_OP_LD_VAR_ADDR (0x18000 >> 10)
419 struct bifrost_ld_var_addr
{
422 unsigned location
: 5;
423 enum bifrost_ldst_type type
: 2;
425 } __attribute__((packed
));
427 #define BIFROST_ADD_OP_LD_ATTR (0x08000 >> 12)
429 struct bifrost_ld_attr
{
432 unsigned location
: 5;
433 unsigned channels
: 2; /* MALI_POSITIVE */
434 enum bifrost_ldst_type type
: 2;
436 } __attribute__((packed
));
438 enum bifrost_interp_mode
{
439 BIFROST_INTERP_PER_FRAG
= 0x0,
440 BIFROST_INTERP_CENTROID
= 0x1,
441 BIFROST_INTERP_DEFAULT
= 0x2,
442 BIFROST_INTERP_EXPLICIT
= 0x3
445 #define BIFROST_ADD_OP_LD_VAR_16 (0x1a << 1)
446 #define BIFROST_ADD_OP_LD_VAR_32 (0x0a << 1)
448 struct bifrost_ld_var
{
451 /* If top two bits set, indirect with src in bottom three */
454 unsigned channels
: 2; /* MALI_POSITIVE */
455 enum bifrost_interp_mode interp_mode
: 2;
459 } __attribute__((packed
));
461 struct bifrost_tex_ctrl
{
462 unsigned sampler_index
: 4; // also used to signal indirects
463 unsigned tex_index
: 7;
464 bool no_merge_index
: 1; // whether to merge (direct) sampler & texture indices
465 bool filter
: 1; // use the usual filtering pipeline (0 for texelFetch & textureGather)
467 bool texel_offset
: 1; // *Offset()
470 unsigned tex_type
: 2; // 2D, 3D, Cube, Buffer
471 bool compute_lod
: 1; // 0 for *Lod()
472 bool not_supply_lod
: 1; // 0 for *Lod() or when a bias is applied
473 bool calc_gradients
: 1; // 0 for *Grad()
475 unsigned result_type
: 4; // integer, unsigned, float TODO: why is this 4 bits?
477 } __attribute__((packed
));
479 struct bifrost_dual_tex_ctrl
{
480 unsigned sampler_index0
: 2;
482 unsigned tex_index0
: 2;
483 unsigned sampler_index1
: 2;
484 unsigned tex_index1
: 2;
486 } __attribute__((packed
));
488 #define BIFROST_ADD_OP_TEX_COMPACT_F32 (0x0b000 >> 10)
489 #define BIFROST_ADD_OP_TEX_COMPACT_F16 (0x1b000 >> 10)
491 struct bifrost_tex_compact
{
494 unsigned tex_index
: 3;
495 unsigned unknown
: 1;
496 unsigned sampler_index
: 3;
498 } __attribute__((packed
));
500 enum branch_bit_size
{
504 // For the above combinations of bitsize and location, an extra bit is
505 // encoded via comparing the sources. The only possible source of ambiguity
506 // would be if the sources were the same, but then the branch condition
507 // would be always true or always false anyways, so we can ignore it. But
508 // this no longer works when comparing the y component to the x component,
509 // since it's valid to compare the y component of a source against its own
510 // x component. Instead, the extra bit is encoded via an extra bitsize.
513 BR_SIZE_32_AND_16X
= 5,
514 BR_SIZE_32_AND_16Y
= 6,
515 // Used for comparisons with zero and always-true, see below. I think this
516 // only works for integer comparisons.
520 enum bifrost_reg_write_unit
{
521 REG_WRITE_NONE
= 0, // don't write
522 REG_WRITE_TWO
, // write using reg2
523 REG_WRITE_THREE
, // write using reg3
526 struct bifrost_regs
{
527 unsigned uniform_const
: 8;
533 } __attribute__((packed
));
535 enum bifrost_branch_cond
{
540 // Equal vs. not-equal determined by src0/src1 comparison
542 // floating-point comparisons
543 // Becomes UNE when you flip the arguments
545 // TODO what happens when you flip the arguments?
550 enum bifrost_branch_code
{
554 struct bifrost_branch
{
557 /* For BR_SIZE_ZERO, upper two bits become ctrl */
560 /* Offset source -- always uniform/const but
561 * theoretically could support indirect jumps? */
564 enum bifrost_branch_cond cond
: 3;
565 enum branch_bit_size size
: 3;
572 #define BIFROST_FMA_NOP (0x701960 | BIFROST_SRC_STAGE)
573 #define BIFROST_ADD_NOP (0x3D960 | BIFROST_SRC_STAGE)
575 struct bifrost_fmt1
{
580 uint64_t header
: 45;
581 } __attribute__((packed
));
583 #define BIFROST_FMT1_INSTRUCTIONS 0b00101
584 #define BIFROST_FMT1_FINAL 0b01001
585 #define BIFROST_FMT1_CONSTANTS 0b00001
587 #define BIFROST_FMTC_CONSTANTS 0b0011
588 #define BIFROST_FMTC_FINAL 0b0111
590 struct bifrost_fmt_constant
{
595 } __attribute__((packed
));
597 enum bifrost_reg_control
{
598 BIFROST_WRITE_FMA_P2
= 1,
599 BIFROST_WRITE_FMA_P2_READ_P3
= 2,
600 BIFROST_FIRST_WRITE_FMA_P2_READ_P3
= 3,
602 BIFROST_WRITE_ADD_P2
= 5,
603 BIFROST_WRITE_ADD_P2_READ_P3
= 6,
604 BIFROST_WRITE_ADD_P2_FMA_P3
= 7,
606 BIFROST_FIRST_NONE
= 8,
607 BIFROST_FIRST_WRITE_FMA_P2
= 9,
608 /* INSTR_INVALID_ENC */
609 BIFROST_REG_NONE
= 11,
610 BIFROST_FIRST_READ_P3
= 12,
611 BIFROST_FIRST_WRITE_ADD_P2
= 13,
612 BIFROST_FIRST_WRITE_ADD_P2_READ_P3
= 14,
613 BIFROST_FIRST_WRITE_ADD_P2_FMA_P3
= 15