5 * Copyright (c) 2013 Connor Abbott (connor@abbott.cx)
6 * Copyright (c) 2018 Alyssa Rosenzweig (alyssa@rosenzweig.io)
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #define MIDGARD_DBG_MSGS 0x0001
34 #define MIDGARD_DBG_SHADERS 0x0002
36 extern int midgard_debug
;
39 midgard_word_type_alu
,
40 midgard_word_type_load_store
,
41 midgard_word_type_texture
,
42 midgard_word_type_unknown
58 midgard_alu_op_fadd
= 0x10,
59 midgard_alu_op_fmul
= 0x14,
60 midgard_alu_op_fmin
= 0x28,
61 midgard_alu_op_fmax
= 0x2C,
62 midgard_alu_op_fmov
= 0x30,
63 midgard_alu_op_froundeven
= 0x34,
64 midgard_alu_op_ftrunc
= 0x35,
65 midgard_alu_op_ffloor
= 0x36,
66 midgard_alu_op_fceil
= 0x37,
67 midgard_alu_op_ffma
= 0x38,
68 midgard_alu_op_fdot3
= 0x3C,
69 midgard_alu_op_fdot3r
= 0x3D,
70 midgard_alu_op_fdot4
= 0x3E,
71 midgard_alu_op_freduce
= 0x3F,
72 midgard_alu_op_iadd
= 0x40,
73 midgard_alu_op_ishladd
= 0x41,
74 midgard_alu_op_isub
= 0x46,
75 midgard_alu_op_imul
= 0x58,
76 midgard_alu_op_imin
= 0x60,
77 midgard_alu_op_imax
= 0x62,
78 midgard_alu_op_iasr
= 0x68,
79 midgard_alu_op_ilsr
= 0x69,
80 midgard_alu_op_ishl
= 0x6E,
81 midgard_alu_op_iand
= 0x70,
82 midgard_alu_op_ior
= 0x71,
83 midgard_alu_op_inot
= 0x72,
84 midgard_alu_op_iandnot
= 0x74, /* (a, b) -> a & ~b, used for not/b2f */
85 midgard_alu_op_ixor
= 0x76,
86 midgard_alu_op_imov
= 0x7B,
87 midgard_alu_op_iabs
= 0x7C,
88 midgard_alu_op_feq
= 0x80,
89 midgard_alu_op_fne
= 0x81,
90 midgard_alu_op_flt
= 0x82,
91 midgard_alu_op_fle
= 0x83,
92 midgard_alu_op_fball_eq
= 0x88,
93 midgard_alu_op_bball_eq
= 0x89,
94 midgard_alu_op_fball_lt
= 0x8A, /* all(lessThan(.., ..)) */
95 midgard_alu_op_fball_lte
= 0x8B, /* all(lessThanEqual(.., ..)) */
96 midgard_alu_op_bbany_neq
= 0x90, /* used for bvec4(1) */
97 midgard_alu_op_fbany_neq
= 0x91, /* bvec4(0) also */
98 midgard_alu_op_fbany_lt
= 0x92, /* any(lessThan(.., ..)) */
99 midgard_alu_op_fbany_lte
= 0x93, /* any(lessThanEqual(.., ..)) */
100 midgard_alu_op_f2i
= 0x99,
101 midgard_alu_op_f2u8
= 0x9C,
102 midgard_alu_op_f2u
= 0x9D,
104 midgard_alu_op_ieq
= 0xA0,
105 midgard_alu_op_ine
= 0xA1,
106 midgard_alu_op_ult
= 0xA2,
107 midgard_alu_op_ule
= 0xA3,
108 midgard_alu_op_ilt
= 0xA4,
109 midgard_alu_op_ile
= 0xA5,
110 midgard_alu_op_iball_eq
= 0xA8,
111 midgard_alu_op_ball
= 0xA9,
112 midgard_alu_op_uball_lt
= 0xAA,
113 midgard_alu_op_uball_lte
= 0xAB,
114 midgard_alu_op_iball_lt
= 0xAC,
115 midgard_alu_op_iball_lte
= 0xAD,
116 midgard_alu_op_ibany_eq
= 0xB0,
117 midgard_alu_op_ibany_neq
= 0xB1,
118 midgard_alu_op_ubany_lt
= 0xB2,
119 midgard_alu_op_ubany_lte
= 0xB3,
120 midgard_alu_op_ibany_lt
= 0xB4, /* any(lessThan(.., ..)) */
121 midgard_alu_op_ibany_lte
= 0xB5, /* any(lessThanEqual(.., ..)) */
122 midgard_alu_op_i2f
= 0xB8,
123 midgard_alu_op_u2f
= 0xBC,
124 midgard_alu_op_icsel
= 0xC1,
125 midgard_alu_op_fcsel_i
= 0xC4,
126 midgard_alu_op_fcsel
= 0xC5,
127 midgard_alu_op_fround
= 0xC6,
128 midgard_alu_op_fatan_pt2
= 0xE8,
129 midgard_alu_op_fpow_pt1
= 0xEC,
130 midgard_alu_op_frcp
= 0xF0,
131 midgard_alu_op_frsqrt
= 0xF2,
132 midgard_alu_op_fsqrt
= 0xF3,
133 midgard_alu_op_fexp2
= 0xF4,
134 midgard_alu_op_flog2
= 0xF5,
135 midgard_alu_op_fsin
= 0xF6,
136 midgard_alu_op_fcos
= 0xF7,
137 midgard_alu_op_fatan2_pt1
= 0xF9,
141 midgard_outmod_none
= 0,
142 midgard_outmod_pos
= 1,
143 midgard_outmod_int
= 2,
144 midgard_outmod_sat
= 3
148 midgard_reg_mode_quarter
= 0,
149 midgard_reg_mode_half
= 1,
150 midgard_reg_mode_full
= 2,
151 midgard_reg_mode_double
= 3 /* TODO: verify */
155 midgard_dest_override_lower
= 0,
156 midgard_dest_override_upper
= 1,
157 midgard_dest_override_none
= 2
158 } midgard_dest_override
;
161 __attribute__((__packed__
))
166 /* replicate lower half if dest = half, or low/high half selection if
170 bool rep_high
: 1; /* unused if dest = full */
171 bool half
: 1; /* only matters if dest = full */
172 unsigned swizzle
: 8;
174 midgard_vector_alu_src
;
177 __attribute__((__packed__
))
179 midgard_alu_op op
: 8;
180 midgard_reg_mode reg_mode
: 2;
183 midgard_dest_override dest_override
: 2;
184 midgard_outmod outmod
: 2;
190 __attribute__((__packed__
))
194 bool full
: 1; /* 0 = half, 1 = full */
195 unsigned component
: 3;
197 midgard_scalar_alu_src
;
200 __attribute__((__packed__
))
202 midgard_alu_op op
: 8;
205 unsigned unknown
: 1;
206 midgard_outmod outmod
: 2;
207 bool output_full
: 1;
208 unsigned output_component
: 3;
213 __attribute__((__packed__
))
215 unsigned src1_reg
: 5;
216 unsigned src2_reg
: 5;
217 unsigned out_reg
: 5;
222 /* In addition to conditional branches and jumps (unconditional branches),
223 * Midgard implements a bit of fixed function functionality used in fragment
224 * shaders via specially crafted branches. These have special branch opcodes,
225 * which perform a fixed-function operation and/or use the results of a
226 * fixed-function operation as the branch condition. */
229 /* Regular branches */
230 midgard_jmp_writeout_op_branch_uncond
= 1,
231 midgard_jmp_writeout_op_branch_cond
= 2,
233 /* In a fragment shader, execute a discard_if instruction, with the
234 * corresponding condition code. Terminates the shader, so generally
235 * set the branch target to out of the shader */
236 midgard_jmp_writeout_op_discard
= 4,
238 /* Branch if the tilebuffer is not yet ready. At the beginning of a
239 * fragment shader that reads from the tile buffer, for instance via
240 * ARM_shader_framebuffer_fetch or EXT_pixel_local_storage, this branch
241 * operation should be used as a loop. An instruction like
242 * "br.tilebuffer.always -1" does the trick, corresponding to
243 * "while(!is_tilebuffer_ready) */
244 midgard_jmp_writeout_op_tilebuffer_pending
= 6,
246 /* In a fragment shader, try to write out the value pushed to r0 to the
247 * tilebuffer, subject to unknown state in r1.z and r1.w. If this
248 * succeeds, the shader terminates. If it fails, it branches to the
249 * specified branch target. Generally, this should be used in a loop to
250 * itself, acting as "do { write(r0); } while(!write_successful);" */
251 midgard_jmp_writeout_op_writeout
= 7,
252 } midgard_jmp_writeout_op
;
255 midgard_condition_write0
= 0,
257 /* These condition codes denote a conditional branch on FALSE and on
258 * TRUE respectively */
259 midgard_condition_false
= 1,
260 midgard_condition_true
= 2,
262 /* This condition code always branches. For a pure branch, the
263 * unconditional branch coding should be used instead, but for
264 * fixed-function branch opcodes, this is still useful */
265 midgard_condition_always
= 3,
269 __attribute__((__packed__
))
271 midgard_jmp_writeout_op op
: 3; /* == branch_uncond */
272 unsigned dest_tag
: 4; /* tag of branch destination */
273 unsigned unknown
: 2;
276 midgard_branch_uncond
;
279 __attribute__((__packed__
))
281 midgard_jmp_writeout_op op
: 3; /* == branch_cond */
282 unsigned dest_tag
: 4; /* tag of branch destination */
284 midgard_condition cond
: 2;
289 __attribute__((__packed__
))
291 midgard_jmp_writeout_op op
: 3; /* == branch_cond */
292 unsigned dest_tag
: 4; /* tag of branch destination */
293 unsigned unknown
: 2;
297 midgard_branch_extended
;
300 __attribute__((__packed__
))
302 midgard_jmp_writeout_op op
: 3; /* == writeout */
303 unsigned unknown
: 13;
312 midgard_op_ld_st_noop
= 0x03,
314 /* Unclear why this is on the L/S unit, but (with an address of 0,
315 * appropriate swizzle, magic constant 0x24, and xy mask?) moves fp32 cube
316 * map coordinates in r27 to its cube map texture coordinate
317 * destination (e.g r29). 0x4 magic for loading from fp16 instead */
319 midgard_op_store_cubemap_coords
= 0x0E,
321 midgard_op_load_attr_16
= 0x95,
322 midgard_op_load_attr_32
= 0x94,
323 midgard_op_load_vary_16
= 0x99,
324 midgard_op_load_vary_32
= 0x98,
325 midgard_op_load_color_buffer_16
= 0x9D,
326 midgard_op_load_color_buffer_8
= 0xBA,
327 midgard_op_load_uniform_16
= 0xAC,
328 midgard_op_load_uniform_32
= 0xB0,
329 midgard_op_store_vary_16
= 0xD5,
330 midgard_op_store_vary_32
= 0xD4
331 } midgard_load_store_op
;
334 midgard_interp_centroid
= 1,
335 midgard_interp_default
= 2
336 } midgard_interpolation
;
339 __attribute__((__packed__
))
341 unsigned zero1
: 4; /* Always zero */
343 /* Varying qualifiers, zero if not a varying */
345 unsigned is_varying
: 1; /* Always one for varying, but maybe something else? */
346 midgard_interpolation interpolation
: 2;
348 unsigned zero2
: 2; /* Always zero */
350 midgard_varying_parameter
;
353 __attribute__((__packed__
))
355 midgard_load_store_op op
: 8;
358 unsigned swizzle
: 8;
359 unsigned unknown
: 16;
361 unsigned varying_parameters
: 10;
363 unsigned address
: 9;
365 midgard_load_store_word
;
368 __attribute__((__packed__
))
371 unsigned next_type
: 4;
377 /* Texture pipeline results are in r28-r29 */
378 #define REG_TEX_BASE 28
380 /* Texture opcodes... maybe? */
381 #define TEXTURE_OP_NORMAL 0x11
382 #define TEXTURE_OP_TEXEL_FETCH 0x14
384 /* Texture format types, found in format */
385 #define TEXTURE_CUBE 0x00
386 #define TEXTURE_2D 0x02
387 #define TEXTURE_3D 0x03
390 __attribute__((__packed__
))
393 unsigned next_type
: 4;
397 unsigned unknown3
: 1;
399 /* A little obscure, but last is set for the last texture operation in
400 * a shader. cont appears to just be last's opposite (?). Yeah, I know,
401 * kind of funky.. BiOpen thinks it could do with memory hinting, or
408 unsigned has_offset
: 1;
410 /* Like in Bifrost */
413 unsigned in_reg_select
: 1;
414 unsigned in_reg_upper
: 1;
416 unsigned in_reg_swizzle_left
: 2;
417 unsigned in_reg_swizzle_right
: 2;
419 unsigned unknown1
: 2;
421 unsigned unknown8
: 4;
423 unsigned out_full
: 1;
425 /* Always 1 afaict... */
426 unsigned unknown7
: 2;
428 unsigned out_reg_select
: 1;
429 unsigned out_upper
: 1;
433 unsigned unknown2
: 2;
435 unsigned swizzle
: 8;
436 unsigned unknown4
: 8;
438 unsigned unknownA
: 4;
440 unsigned offset_unknown1
: 1;
441 unsigned offset_reg_select
: 1;
442 unsigned offset_reg_upper
: 1;
443 unsigned offset_unknown4
: 1;
444 unsigned offset_unknown5
: 1;
445 unsigned offset_unknown6
: 1;
446 unsigned offset_unknown7
: 1;
447 unsigned offset_unknown8
: 1;
448 unsigned offset_unknown9
: 1;
450 unsigned unknownB
: 3;
452 /* Texture bias or LOD, depending on whether it is executed in a
453 * fragment/vertex shader respectively. Compute as int(2^8 * biasf).
455 * For texel fetch, this is the LOD as is. */
458 unsigned unknown9
: 8;
460 unsigned texture_handle
: 16;
461 unsigned sampler_handle
: 16;
463 midgard_texture_word
;
465 /* Opcode name table */
467 static char *alu_opcode_names
[256] = {
468 [midgard_alu_op_fadd
] = "fadd",
469 [midgard_alu_op_fmul
] = "fmul",
470 [midgard_alu_op_fmin
] = "fmin",
471 [midgard_alu_op_fmax
] = "fmax",
472 [midgard_alu_op_fmov
] = "fmov",
473 [midgard_alu_op_froundeven
] = "froundeven",
474 [midgard_alu_op_ftrunc
] = "ftrunc",
475 [midgard_alu_op_ffloor
] = "ffloor",
476 [midgard_alu_op_fceil
] = "fceil",
477 [midgard_alu_op_ffma
] = "ffma",
478 [midgard_alu_op_fdot3
] = "fdot3",
479 [midgard_alu_op_fdot3r
] = "fdot3r",
480 [midgard_alu_op_fdot4
] = "fdot4",
481 [midgard_alu_op_freduce
] = "freduce",
482 [midgard_alu_op_imin
] = "imin",
483 [midgard_alu_op_imax
] = "imax",
484 [midgard_alu_op_ishl
] = "ishl",
485 [midgard_alu_op_iasr
] = "iasr",
486 [midgard_alu_op_ilsr
] = "ilsr",
487 [midgard_alu_op_iadd
] = "iadd",
488 [midgard_alu_op_ishladd
] = "ishladd",
489 [midgard_alu_op_isub
] = "isub",
490 [midgard_alu_op_imul
] = "imul",
491 [midgard_alu_op_imov
] = "imov",
492 [midgard_alu_op_iabs
] = "iabs",
493 [midgard_alu_op_iand
] = "iand",
494 [midgard_alu_op_ior
] = "ior",
495 [midgard_alu_op_inot
] = "inot",
496 [midgard_alu_op_iandnot
] = "iandnot",
497 [midgard_alu_op_ixor
] = "ixor",
498 [midgard_alu_op_feq
] = "feq",
499 [midgard_alu_op_fne
] = "fne",
500 [midgard_alu_op_flt
] = "flt",
501 [midgard_alu_op_fle
] = "fle",
502 [midgard_alu_op_fball_eq
] = "fball_eq",
503 [midgard_alu_op_fbany_neq
] = "fbany_neq",
504 [midgard_alu_op_bball_eq
] = "bball_eq",
505 [midgard_alu_op_fball_lt
] = "fball_lt",
506 [midgard_alu_op_fball_lte
] = "fball_lte",
507 [midgard_alu_op_bbany_neq
] = "bbany_neq",
508 [midgard_alu_op_fbany_lt
] = "fbany_lt",
509 [midgard_alu_op_fbany_lte
] = "fbany_lte",
510 [midgard_alu_op_f2i
] = "f2i",
511 [midgard_alu_op_f2u
] = "f2u",
512 [midgard_alu_op_f2u8
] = "f2u8",
513 [midgard_alu_op_ieq
] = "ieq",
514 [midgard_alu_op_ine
] = "ine",
515 [midgard_alu_op_ult
] = "ult",
516 [midgard_alu_op_ule
] = "ule",
517 [midgard_alu_op_ilt
] = "ilt",
518 [midgard_alu_op_ile
] = "ile",
519 [midgard_alu_op_iball_eq
] = "iball_eq",
520 [midgard_alu_op_ball
] = "ball",
521 [midgard_alu_op_uball_lt
] = "uball_lt",
522 [midgard_alu_op_uball_lte
] = "uball_lte",
523 [midgard_alu_op_iball_lt
] = "iball_lt",
524 [midgard_alu_op_iball_lte
] = "iball_lte",
525 [midgard_alu_op_iball_eq
] = "iball_eq",
526 [midgard_alu_op_ibany_neq
] = "ibany_neq",
527 [midgard_alu_op_ubany_lt
] = "ubany_lt",
528 [midgard_alu_op_ubany_lte
] = "ubany_lte",
529 [midgard_alu_op_ibany_lt
] = "ibany_lt",
530 [midgard_alu_op_ibany_lte
] = "ibany_lte",
531 [midgard_alu_op_i2f
] = "i2f",
532 [midgard_alu_op_u2f
] = "u2f",
533 [midgard_alu_op_icsel
] = "icsel",
534 [midgard_alu_op_fcsel_i
] = "fcsel_i",
535 [midgard_alu_op_fcsel
] = "fcsel",
536 [midgard_alu_op_fround
] = "fround",
537 [midgard_alu_op_fatan_pt2
] = "fatan_pt2",
538 [midgard_alu_op_frcp
] = "frcp",
539 [midgard_alu_op_frsqrt
] = "frsqrt",
540 [midgard_alu_op_fsqrt
] = "fsqrt",
541 [midgard_alu_op_fpow_pt1
] = "fpow_pt1",
542 [midgard_alu_op_fexp2
] = "fexp2",
543 [midgard_alu_op_flog2
] = "flog2",
544 [midgard_alu_op_fsin
] = "fsin",
545 [midgard_alu_op_fcos
] = "fcos",
546 [midgard_alu_op_fatan2_pt1
] = "fatan2_pt1"
549 static char *load_store_opcode_names
[256] = {
550 [midgard_op_store_cubemap_coords
] = "st_cubemap_coords",
551 [midgard_op_load_attr_16
] = "ld_attr_16",
552 [midgard_op_load_attr_32
] = "ld_attr_32",
553 [midgard_op_load_vary_16
] = "ld_vary_16",
554 [midgard_op_load_vary_32
] = "ld_vary_32",
555 [midgard_op_load_uniform_16
] = "ld_uniform_16",
556 [midgard_op_load_uniform_32
] = "ld_uniform_32",
557 [midgard_op_load_color_buffer_8
] = "ld_color_buffer_8",
558 [midgard_op_load_color_buffer_16
] = "ld_color_buffer_16",
559 [midgard_op_store_vary_16
] = "st_vary_16",
560 [midgard_op_store_vary_32
] = "st_vary_32"