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)
101 struct bifrost_fma_inst
{
104 } __attribute__((packed
));
106 struct bifrost_fma_2src
{
110 } __attribute__((packed
));
112 #define BIFROST_ADD_OP_BLEND (0x1952c)
113 #define BIFROST_ADD_OP_FRCP_FAST_F32 (0x0cc00)
114 #define BIFROST_ADD_OP_FRCP_FAST_F16_X (0x0ce10)
115 #define BIFROST_ADD_OP_FRCP_FAST_F16_Y (0x0ce30)
116 #define BIFROST_ADD_OP_FRSQ_FAST_F32 (0x0cc20)
117 #define BIFROST_ADD_OP_FRSQ_FAST_F16_X (0x0ce50)
118 #define BIFROST_ADD_OP_FRSQ_FAST_F16_Y (0x0ce70)
120 struct bifrost_add_inst
{
123 } __attribute__((packed
));
125 #define BIFROST_ADD_OP_LD_UBO_1 (0x0c1a0 >> 3)
126 #define BIFROST_ADD_OP_LD_UBO_2 (0x0c1e0 >> 3)
127 #define BIFROST_ADD_OP_LD_UBO_3 (0x0caa0 >> 3)
128 #define BIFROST_ADD_OP_LD_UBO_4 (0x0c220 >> 3)
130 struct bifrost_add_2src
{
134 } __attribute__((packed
));
136 #define BIFROST_ADD_OP_ST_VAR (0x19300 >> 8)
138 struct bifrost_st_vary
{
142 unsigned channels
: 2;
144 } __attribute__((packed
));
146 #define BIFROST_ADD_OP_ATEST (0xc8f)
148 struct bifrost_add_atest
{
149 /* gl_SampleMask (R60) */
155 /* If half, X/Y select. If !half, always set */
156 unsigned component
: 1;
160 } __attribute__((packed
));
162 enum bifrost_outmod
{
165 BIFROST_SAT_SIGNED
= 0x2,
169 enum bifrost_roundmode
{
170 BIFROST_RTE
= 0x0, /* round to even */
171 BIFROST_RTP
= 0x1, /* round to positive */
172 BIFROST_RTN
= 0x2, /* round to negative */
173 BIFROST_RTZ
= 0x3 /* round to zero */
176 /* NONE: Same as fmax() and fmin() -- return the other
177 * number if any number is NaN. Also always return +0 if
178 * one argument is +0 and the other is -0.
180 * NAN_WINS: Instead of never returning a NaN, always return
181 * one. The "greater"/"lesser" NaN is always returned, first
182 * by checking the sign and then the mantissa bits.
184 * SRC1_WINS: For max, implement src0 > src1 ? src0 : src1.
185 * For min, implement src0 < src1 ? src0 : src1. This
186 * includes handling NaN's and signedness of 0 differently
187 * from above, since +0 and -0 compare equal and comparisons
188 * always return false for NaN's. As a result, this mode is
191 * SRC0_WINS: For max, implement src0 < src1 ? src1 : src0
192 * For min, implement src0 > src1 ? src1 : src0
196 enum bifrost_minmax_mode
{
197 BIFROST_MINMAX_NONE
= 0x0,
198 BIFROST_NAN_WINS
= 0x1,
199 BIFROST_SRC1_WINS
= 0x2,
200 BIFROST_SRC0_WINS
= 0x3,
203 #define BIFROST_FMA_OP_FADD32 (0x58 >> 2)
204 #define BIFROST_FMA_OP_FMAX32 (0x40 >> 2)
205 #define BIFROST_FMA_OP_FMIN32 (0x44 >> 2)
207 struct bifrost_fma_add
{
210 unsigned src1_abs
: 1;
211 unsigned src0_neg
: 1;
212 unsigned src1_neg
: 1;
214 unsigned src0_abs
: 1;
215 enum bifrost_roundmode roundmode
: 2;
216 enum bifrost_outmod outmod
: 2;
218 } __attribute__((packed
));
220 #define BIFROST_FMA_OP_FMAX16 (0xC0 >> 2)
221 #define BIFROST_FMA_OP_FMIN16 (0xCC >> 2)
222 #define BIFROST_FMA_OP_FADD16 (0xD8 >> 2)
224 struct bifrost_fma_add_minmax16
{
227 /* abs2 inferred as (src1 < src0) */
229 unsigned src0_neg
: 1;
230 unsigned src1_neg
: 1;
231 unsigned src0_swizzle
: 2;
232 unsigned src1_swizzle
: 2;
234 enum bifrost_outmod outmod
: 2;
235 /* roundmode for add, min/max mode for min/max */
237 } __attribute__((packed
));
239 #define BIFROST_FMA_OP_FMA (0x00)
241 struct bifrost_fma_fma
{
245 unsigned src_expand
: 3;
246 unsigned src0_abs
: 1;
247 enum bifrost_roundmode roundmode
: 2;
248 enum bifrost_outmod outmod
: 2;
249 unsigned src0_neg
: 1; /* 14 */
250 unsigned src2_neg
: 1;
251 unsigned src1_abs
: 1;
252 unsigned src2_abs
: 1; /* 17 */
254 } __attribute__((packed
));
256 #define BIFROST_FMA_OP_FMA16 (0x2)
258 struct bifrost_fma_fma16
{
262 unsigned swizzle_0
: 2;
263 unsigned swizzle_1
: 2;
264 enum bifrost_roundmode roundmode
: 2;
265 enum bifrost_outmod outmod
: 2;
266 unsigned src0_neg
: 1;
267 unsigned src2_neg
: 1;
268 unsigned swizzle_2
: 2;
270 } __attribute__((packed
));
272 enum bifrost_csel_cond
{
283 #define BIFROST_FMA_OP_CSEL4 (0x5c)
284 #define BIFROST_FMA_OP_CSEL4_V16 (0xdc)
286 struct bifrost_csel4
{
291 enum bifrost_csel_cond cond
: 3;
293 } __attribute__((packed
));
295 struct bifrost_shift_fma
{
299 unsigned half
: 3; /* 000 for i32, 100 for i8, 111 for v2i16 */
300 unsigned unk
: 1; /* always set? */
301 unsigned invert_1
: 1; /* Inverts sources to combining op */
302 /* For XOR, switches RSHIFT to LSHIFT since only one invert needed */
303 unsigned invert_2
: 1;
305 } __attribute__((packed
));
307 struct bifrost_shift_add
{
313 unsigned invert_1
: 1;
314 unsigned invert_2
: 1;
317 } __attribute__((packed
));
319 #define BIFROST_FMA_INT16_TO_32 (0xe0198 >> 3)
321 struct bifrost_fma_int16_to_32
{
323 unsigned is_unsigned
: 1;
324 unsigned swizzle
: 1;
325 unsigned to_float
: 1;
327 } __attribute__((packed
));
329 /* We could combine but it's easier to just use FMA_ONE_SRC */
330 #define BIFROST_FMA_FLOAT16_TO_32(y) (0xe01a2 | ((y) ? 1 : 0))
332 /* Two sources for vectorization */
333 #define BIFROST_FMA_FLOAT32_TO_16 (0xdd000 >> 3)
335 /* Again we could combine but easier to just ONE_SRC with an
336 * argumnt for unsignedness */
337 #define BIFROST_FMA_FLOAT32_TO_INT(u) (0xe0136 | ((u) ? 1 : 0))
338 #define BIFROST_FMA_INT_TO_FLOAT32(u) (0xe0178 | ((u) ? 1 : 0))
340 /* Used for f2i16 and i2f16 */
341 #define BIFROST_FMA_F2I16 (0xe00)
343 struct bifrost_fma_f2i_i2f16
{
345 unsigned is_unsigned
: 1;
346 unsigned direction
: 2; /* 00 for i2f, 11 for f2i */
347 unsigned swizzle
: 2;
348 unsigned unk
: 2; /* always 10 */
349 unsigned direction_2
: 1; /* 0 for f2i, 1 for i2f */
351 } __attribute__((packed
));
353 enum bifrost_ldst_type
{
354 BIFROST_LDST_F16
= 0,
355 BIFROST_LDST_F32
= 1,
356 BIFROST_LDST_I32
= 2,
360 #define BIFROST_ADD_OP_LD_VAR_ADDR (0x18000 >> 10)
362 struct bifrost_ld_var_addr
{
365 unsigned location
: 5;
366 enum bifrost_ldst_type type
: 2;
368 } __attribute__((packed
));
370 #define BIFROST_ADD_OP_LD_ATTR (0x08000 >> 12)
372 struct bifrost_ld_attr
{
375 unsigned location
: 5;
376 unsigned channels
: 2; /* MALI_POSITIVE */
377 enum bifrost_ldst_type type
: 2;
379 } __attribute__((packed
));
381 enum bifrost_interp_mode
{
382 BIFROST_INTERP_PER_FRAG
= 0x0,
383 BIFROST_INTERP_CENTROID
= 0x1,
384 BIFROST_INTERP_DEFAULT
= 0x2,
385 BIFROST_INTERP_EXPLICIT
= 0x3
388 #define BIFROST_ADD_OP_LD_VAR_16 (0x1a << 1)
389 #define BIFROST_ADD_OP_LD_VAR_32 (0x0a << 1)
391 struct bifrost_ld_var
{
394 /* If top two bits set, indirect with src in bottom three */
397 unsigned channels
: 2; /* MALI_POSITIVE */
398 enum bifrost_interp_mode interp_mode
: 2;
402 } __attribute__((packed
));
404 struct bifrost_tex_ctrl
{
405 unsigned sampler_index
: 4; // also used to signal indirects
406 unsigned tex_index
: 7;
407 bool no_merge_index
: 1; // whether to merge (direct) sampler & texture indices
408 bool filter
: 1; // use the usual filtering pipeline (0 for texelFetch & textureGather)
410 bool texel_offset
: 1; // *Offset()
413 unsigned tex_type
: 2; // 2D, 3D, Cube, Buffer
414 bool compute_lod
: 1; // 0 for *Lod()
415 bool not_supply_lod
: 1; // 0 for *Lod() or when a bias is applied
416 bool calc_gradients
: 1; // 0 for *Grad()
418 unsigned result_type
: 4; // integer, unsigned, float TODO: why is this 4 bits?
420 } __attribute__((packed
));
422 struct bifrost_dual_tex_ctrl
{
423 unsigned sampler_index0
: 2;
425 unsigned tex_index0
: 2;
426 unsigned sampler_index1
: 2;
427 unsigned tex_index1
: 2;
429 } __attribute__((packed
));
431 enum branch_bit_size
{
435 // For the above combinations of bitsize and location, an extra bit is
436 // encoded via comparing the sources. The only possible source of ambiguity
437 // would be if the sources were the same, but then the branch condition
438 // would be always true or always false anyways, so we can ignore it. But
439 // this no longer works when comparing the y component to the x component,
440 // since it's valid to compare the y component of a source against its own
441 // x component. Instead, the extra bit is encoded via an extra bitsize.
444 BR_SIZE_32_AND_16X
= 5,
445 BR_SIZE_32_AND_16Y
= 6,
446 // Used for comparisons with zero and always-true, see below. I think this
447 // only works for integer comparisons.
451 enum bifrost_reg_write_unit
{
452 REG_WRITE_NONE
= 0, // don't write
453 REG_WRITE_TWO
, // write using reg2
454 REG_WRITE_THREE
, // write using reg3
457 struct bifrost_regs
{
458 unsigned uniform_const
: 8;
464 } __attribute__((packed
));
466 enum bifrost_branch_cond
{
471 // Equal vs. not-equal determined by src0/src1 comparison
473 // floating-point comparisons
474 // Becomes UNE when you flip the arguments
476 // TODO what happens when you flip the arguments?
481 enum bifrost_branch_code
{
485 struct bifrost_branch
{
488 /* For BR_SIZE_ZERO, upper two bits become ctrl */
491 /* Offset source -- always uniform/const but
492 * theoretically could support indirect jumps? */
495 enum bifrost_branch_cond cond
: 3;
496 enum branch_bit_size size
: 3;
503 #define BIFROST_FMA_NOP (0x701960 | BIFROST_SRC_STAGE)
504 #define BIFROST_ADD_NOP (0x3D960 | BIFROST_SRC_STAGE)
506 struct bifrost_fmt1
{
511 uint64_t header
: 45;
512 } __attribute__((packed
));
514 #define BIFROST_FMT1_INSTRUCTIONS 0b00101
515 #define BIFROST_FMT1_FINAL 0b01001
516 #define BIFROST_FMT1_CONSTANTS 0b00001
518 #define BIFROST_FMTC_CONSTANTS 0b0011
519 #define BIFROST_FMTC_FINAL 0b0111
521 struct bifrost_fmt_constant
{
526 } __attribute__((packed
));
528 enum bifrost_reg_control
{
529 BIFROST_WRITE_FMA_P2
= 1,
530 BIFROST_WRITE_FMA_P2_READ_P3
= 2,
531 BIFROST_FIRST_WRITE_FMA_P2_READ_P3
= 3,
533 BIFROST_WRITE_ADD_P2
= 5,
534 BIFROST_WRITE_ADD_P2_READ_P3
= 6,
535 BIFROST_WRITE_ADD_P2_FMA_P3
= 7,
537 BIFROST_FIRST_NONE
= 8,
538 BIFROST_FIRST_WRITE_FMA_P2
= 9,
539 /* INSTR_INVALID_ENC */
540 BIFROST_REG_NONE
= 11,
541 BIFROST_FIRST_READ_P3
= 12,
542 BIFROST_FIRST_WRITE_ADD_P2
= 13,
543 BIFROST_FIRST_WRITE_ADD_P2_READ_P3
= 14,
544 BIFROST_FIRST_WRITE_ADD_P2_FMA_P3
= 15