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,
61 midgard_alu_op_fmin
= 0x28,
62 midgard_alu_op_fmax
= 0x2C,
64 midgard_alu_op_fmov
= 0x30,
65 midgard_alu_op_froundeven
= 0x34,
66 midgard_alu_op_ftrunc
= 0x35,
67 midgard_alu_op_ffloor
= 0x36,
68 midgard_alu_op_fceil
= 0x37,
69 midgard_alu_op_ffma
= 0x38,
70 midgard_alu_op_fdot3
= 0x3C,
71 midgard_alu_op_fdot3r
= 0x3D,
72 midgard_alu_op_fdot4
= 0x3E,
73 midgard_alu_op_freduce
= 0x3F,
75 midgard_alu_op_iadd
= 0x40,
76 midgard_alu_op_ishladd
= 0x41,
77 midgard_alu_op_isub
= 0x46,
78 midgard_alu_op_iaddsat
= 0x48,
79 midgard_alu_op_uaddsat
= 0x49,
80 midgard_alu_op_isubsat
= 0x4E,
81 midgard_alu_op_usubsat
= 0x4F,
83 midgard_alu_op_imul
= 0x58,
85 midgard_alu_op_imin
= 0x60,
86 midgard_alu_op_umin
= 0x61,
87 midgard_alu_op_imax
= 0x62,
88 midgard_alu_op_umax
= 0x63,
89 midgard_alu_op_ihadd
= 0x64,
90 midgard_alu_op_uhadd
= 0x65,
91 midgard_alu_op_irhadd
= 0x66,
92 midgard_alu_op_urhadd
= 0x67,
93 midgard_alu_op_iasr
= 0x68,
94 midgard_alu_op_ilsr
= 0x69,
95 midgard_alu_op_ishl
= 0x6E,
97 midgard_alu_op_iand
= 0x70,
98 midgard_alu_op_ior
= 0x71,
99 midgard_alu_op_inand
= 0x72, /* ~(a & b), for inot let a = b */
100 midgard_alu_op_inor
= 0x73, /* ~(a | b) */
101 midgard_alu_op_iandnot
= 0x74, /* (a & ~b), used for not/b2f */
102 midgard_alu_op_iornot
= 0x75, /* (a | ~b) */
103 midgard_alu_op_ixor
= 0x76,
104 midgard_alu_op_inxor
= 0x77, /* ~(a & b) */
105 midgard_alu_op_iclz
= 0x78, /* Number of zeroes on left */
106 midgard_alu_op_ibitcount8
= 0x7A, /* Counts bits in 8-bit increments */
107 midgard_alu_op_imov
= 0x7B,
108 midgard_alu_op_iabsdiff
= 0x7C,
109 midgard_alu_op_uabsdiff
= 0x7D,
110 midgard_alu_op_ichoose
= 0x7E, /* vector, component number - dupe for shuffle() */
112 midgard_alu_op_feq
= 0x80,
113 midgard_alu_op_fne
= 0x81,
114 midgard_alu_op_flt
= 0x82,
115 midgard_alu_op_fle
= 0x83,
116 midgard_alu_op_fball_eq
= 0x88,
117 midgard_alu_op_bball_eq
= 0x89,
118 midgard_alu_op_fball_lt
= 0x8A, /* all(lessThan(.., ..)) */
119 midgard_alu_op_fball_lte
= 0x8B, /* all(lessThanEqual(.., ..)) */
121 midgard_alu_op_bbany_neq
= 0x90, /* used for bvec4(1) */
122 midgard_alu_op_fbany_neq
= 0x91, /* bvec4(0) also */
123 midgard_alu_op_fbany_lt
= 0x92, /* any(lessThan(.., ..)) */
124 midgard_alu_op_fbany_lte
= 0x93, /* any(lessThanEqual(.., ..)) */
125 midgard_alu_op_f2i
= 0x99,
126 midgard_alu_op_f2u8
= 0x9C,
127 midgard_alu_op_f2u
= 0x9D,
129 midgard_alu_op_ieq
= 0xA0,
130 midgard_alu_op_ine
= 0xA1,
131 midgard_alu_op_ult
= 0xA2,
132 midgard_alu_op_ule
= 0xA3,
133 midgard_alu_op_ilt
= 0xA4,
134 midgard_alu_op_ile
= 0xA5,
135 midgard_alu_op_iball_eq
= 0xA8,
136 midgard_alu_op_iball_neq
= 0xA9,
137 midgard_alu_op_uball_lt
= 0xAA,
138 midgard_alu_op_uball_lte
= 0xAB,
139 midgard_alu_op_iball_lt
= 0xAC,
140 midgard_alu_op_iball_lte
= 0xAD,
142 midgard_alu_op_ibany_eq
= 0xB0,
143 midgard_alu_op_ibany_neq
= 0xB1,
144 midgard_alu_op_ubany_lt
= 0xB2,
145 midgard_alu_op_ubany_lte
= 0xB3,
146 midgard_alu_op_ibany_lt
= 0xB4, /* any(lessThan(.., ..)) */
147 midgard_alu_op_ibany_lte
= 0xB5, /* any(lessThanEqual(.., ..)) */
148 midgard_alu_op_i2f
= 0xB8,
149 midgard_alu_op_u2f
= 0xBC,
151 midgard_alu_op_icsel_v
= 0xC0, /* condition code r31 */
152 midgard_alu_op_icsel
= 0xC1, /* condition code r31.w */
153 midgard_alu_op_fcsel_v
= 0xC4,
154 midgard_alu_op_fcsel
= 0xC5,
155 midgard_alu_op_fround
= 0xC6,
157 midgard_alu_op_fatan_pt2
= 0xE8,
158 midgard_alu_op_fpow_pt1
= 0xEC,
159 midgard_alu_op_fpown_pt1
= 0xED,
160 midgard_alu_op_fpowr_pt1
= 0xEE,
162 midgard_alu_op_frcp
= 0xF0,
163 midgard_alu_op_frsqrt
= 0xF2,
164 midgard_alu_op_fsqrt
= 0xF3,
165 midgard_alu_op_fexp2
= 0xF4,
166 midgard_alu_op_flog2
= 0xF5,
167 midgard_alu_op_fsin
= 0xF6,
168 midgard_alu_op_fcos
= 0xF7,
169 midgard_alu_op_fatan2_pt1
= 0xF9,
173 midgard_outmod_none
= 0,
174 midgard_outmod_pos
= 1,
176 midgard_outmod_sat
= 3
177 } midgard_outmod_float
;
180 midgard_outmod_int_saturate
= 0,
181 midgard_outmod_uint_saturate
= 1,
182 midgard_outmod_int_wrap
= 2,
183 midgard_outmod_int_high
= 3, /* Overflowed portion */
184 } midgard_outmod_int
;
187 midgard_reg_mode_8
= 0,
188 midgard_reg_mode_16
= 1,
189 midgard_reg_mode_32
= 2,
190 midgard_reg_mode_64
= 3
194 midgard_dest_override_lower
= 0,
195 midgard_dest_override_upper
= 1,
196 midgard_dest_override_none
= 2
197 } midgard_dest_override
;
200 midgard_int_sign_extend
= 0,
201 midgard_int_zero_extend
= 1,
202 midgard_int_normal
= 2,
203 midgard_int_shift
= 3
206 #define MIDGARD_FLOAT_MOD_ABS (1 << 0)
207 #define MIDGARD_FLOAT_MOD_NEG (1 << 1)
210 __attribute__((__packed__
))
212 /* Either midgard_int_mod or from midgard_float_mod_*, depending on the
216 /* replicate lower half if dest = half, or low/high half selection if
220 bool rep_high
: 1; /* unused if dest = full */
221 bool half
: 1; /* only matters if dest = full */
222 unsigned swizzle
: 8;
224 midgard_vector_alu_src
;
227 __attribute__((__packed__
))
229 midgard_alu_op op
: 8;
230 midgard_reg_mode reg_mode
: 2;
233 midgard_dest_override dest_override
: 2;
234 midgard_outmod_float outmod
: 2;
240 __attribute__((__packed__
))
244 bool full
: 1; /* 0 = half, 1 = full */
245 unsigned component
: 3;
247 midgard_scalar_alu_src
;
250 __attribute__((__packed__
))
252 midgard_alu_op op
: 8;
255 unsigned unknown
: 1;
257 bool output_full
: 1;
258 unsigned output_component
: 3;
263 __attribute__((__packed__
))
265 unsigned src1_reg
: 5;
266 unsigned src2_reg
: 5;
267 unsigned out_reg
: 5;
272 /* In addition to conditional branches and jumps (unconditional branches),
273 * Midgard implements a bit of fixed function functionality used in fragment
274 * shaders via specially crafted branches. These have special branch opcodes,
275 * which perform a fixed-function operation and/or use the results of a
276 * fixed-function operation as the branch condition. */
279 /* Regular branches */
280 midgard_jmp_writeout_op_branch_uncond
= 1,
281 midgard_jmp_writeout_op_branch_cond
= 2,
283 /* In a fragment shader, execute a discard_if instruction, with the
284 * corresponding condition code. Terminates the shader, so generally
285 * set the branch target to out of the shader */
286 midgard_jmp_writeout_op_discard
= 4,
288 /* Branch if the tilebuffer is not yet ready. At the beginning of a
289 * fragment shader that reads from the tile buffer, for instance via
290 * ARM_shader_framebuffer_fetch or EXT_pixel_local_storage, this branch
291 * operation should be used as a loop. An instruction like
292 * "br.tilebuffer.always -1" does the trick, corresponding to
293 * "while(!is_tilebuffer_ready) */
294 midgard_jmp_writeout_op_tilebuffer_pending
= 6,
296 /* In a fragment shader, try to write out the value pushed to r0 to the
297 * tilebuffer, subject to unknown state in r1.z and r1.w. If this
298 * succeeds, the shader terminates. If it fails, it branches to the
299 * specified branch target. Generally, this should be used in a loop to
300 * itself, acting as "do { write(r0); } while(!write_successful);" */
301 midgard_jmp_writeout_op_writeout
= 7,
302 } midgard_jmp_writeout_op
;
305 midgard_condition_write0
= 0,
307 /* These condition codes denote a conditional branch on FALSE and on
308 * TRUE respectively */
309 midgard_condition_false
= 1,
310 midgard_condition_true
= 2,
312 /* This condition code always branches. For a pure branch, the
313 * unconditional branch coding should be used instead, but for
314 * fixed-function branch opcodes, this is still useful */
315 midgard_condition_always
= 3,
319 __attribute__((__packed__
))
321 midgard_jmp_writeout_op op
: 3; /* == branch_uncond */
322 unsigned dest_tag
: 4; /* tag of branch destination */
323 unsigned unknown
: 2;
326 midgard_branch_uncond
;
329 __attribute__((__packed__
))
331 midgard_jmp_writeout_op op
: 3; /* == branch_cond */
332 unsigned dest_tag
: 4; /* tag of branch destination */
334 midgard_condition cond
: 2;
339 __attribute__((__packed__
))
341 midgard_jmp_writeout_op op
: 3; /* == branch_cond */
342 unsigned dest_tag
: 4; /* tag of branch destination */
343 unsigned unknown
: 2;
347 midgard_branch_extended
;
350 __attribute__((__packed__
))
352 midgard_jmp_writeout_op op
: 3; /* == writeout */
353 unsigned unknown
: 13;
362 midgard_op_ld_st_noop
= 0x03,
364 /* Unclear why this is on the L/S unit, but (with an address of 0,
365 * appropriate swizzle, magic constant 0x24, and xy mask?) moves fp32 cube
366 * map coordinates in r27 to its cube map texture coordinate
367 * destination (e.g r29). 0x4 magic for lding from fp16 instead */
369 midgard_op_st_cubemap_coords
= 0x0E,
371 /* Used in OpenCL. Probably can ld other things as well */
372 midgard_op_ld_global_id
= 0x10,
374 /* The L/S unit can do perspective division a clock faster than the ALU
375 * if you're lucky. Put the vec4 in r27, and call with 0x24 as the
376 * unknown state; the output will be <x/w, y/w, z/w, 1>. Replace w with
377 * z for the z version */
378 midgard_op_ldst_perspective_division_z
= 0x12,
379 midgard_op_ldst_perspective_division_w
= 0x13,
381 /* val in r27.y, address embedded, outputs result to argument. Invert val for sub. Let val = +-1 for inc/dec. */
382 midgard_op_atomic_add
= 0x40,
383 midgard_op_atomic_and
= 0x44,
384 midgard_op_atomic_or
= 0x48,
385 midgard_op_atomic_xor
= 0x4C,
387 midgard_op_atomic_imin
= 0x50,
388 midgard_op_atomic_umin
= 0x54,
389 midgard_op_atomic_imax
= 0x58,
390 midgard_op_atomic_umax
= 0x5C,
392 midgard_op_atomic_xchg
= 0x60,
394 /* Used for compute shader's __global arguments, __local variables (or
395 * for register spilling) */
397 midgard_op_ld_char
= 0x81,
398 midgard_op_ld_char2
= 0x84,
399 midgard_op_ld_short
= 0x85,
400 midgard_op_ld_char4
= 0x88, /* short2, int, float */
401 midgard_op_ld_short4
= 0x8C, /* int2, float2, long */
402 midgard_op_ld_int4
= 0x90, /* float4, long2 */
404 midgard_op_ld_attr_32
= 0x94,
405 midgard_op_ld_attr_16
= 0x95,
406 midgard_op_ld_attr_32i
= 0x97,
407 midgard_op_ld_vary_32
= 0x98,
408 midgard_op_ld_vary_16
= 0x99,
409 midgard_op_ld_vary_32i
= 0x9B,
410 midgard_op_ld_color_buffer_16
= 0x9D,
412 midgard_op_ld_uniform_16
= 0xAC,
414 midgard_op_ld_uniform_32
= 0xB0,
415 midgard_op_ld_color_buffer_8
= 0xBA,
417 midgard_op_st_char
= 0xC0,
418 midgard_op_st_char2
= 0xC4, /* short */
419 midgard_op_st_char4
= 0xC8, /* short2, int, float */
420 midgard_op_st_short4
= 0xCC, /* int2, float2, long */
421 midgard_op_st_int4
= 0xD0, /* float4, long2 */
423 midgard_op_st_vary_32
= 0xD4,
424 midgard_op_st_vary_16
= 0xD5,
425 midgard_op_st_vary_32i
= 0xD7,
427 /* Value to st in r27, location r26.w as short2 */
428 midgard_op_st_image_f
= 0xD8,
429 midgard_op_st_image_ui
= 0xDA,
430 midgard_op_st_image_i
= 0xDB,
431 } midgard_load_store_op
;
434 midgard_interp_centroid
= 1,
435 midgard_interp_default
= 2
436 } midgard_interpolation
;
439 midgard_varying_mod_none
= 0,
441 /* Other values unknown */
443 /* Take the would-be result and divide all components by its z/w
444 * (perspective division baked in with the load) */
445 midgard_varying_mod_perspective_z
= 2,
446 midgard_varying_mod_perspective_w
= 3,
447 } midgard_varying_modifier
;
450 __attribute__((__packed__
))
452 unsigned zero0
: 1; /* Always zero */
454 midgard_varying_modifier modifier
: 2;
456 unsigned zero1
: 1; /* Always zero */
458 /* Varying qualifiers, zero if not a varying */
460 unsigned is_varying
: 1; /* Always one for varying, but maybe something else? */
461 midgard_interpolation interpolation
: 2;
463 unsigned zero2
: 2; /* Always zero */
465 midgard_varying_parameter
;
468 __attribute__((__packed__
))
470 midgard_load_store_op op
: 8;
473 unsigned swizzle
: 8;
474 unsigned unknown
: 16;
476 unsigned varying_parameters
: 10;
478 unsigned address
: 9;
480 midgard_load_store_word
;
483 __attribute__((__packed__
))
486 unsigned next_type
: 4;
492 /* Texture pipeline results are in r28-r29 */
493 #define REG_TEX_BASE 28
495 /* Texture opcodes... maybe? */
496 #define TEXTURE_OP_NORMAL 0x11 /* texture */
497 #define TEXTURE_OP_LOD 0x12 /* textureLod */
498 #define TEXTURE_OP_TEXEL_FETCH 0x14 /* texelFetch */
500 /* Texture format types, found in format */
501 #define TEXTURE_CUBE 0x00
502 #define TEXTURE_2D 0x02
503 #define TEXTURE_3D 0x03
506 __attribute__((__packed__
))
509 unsigned next_type
: 4;
513 unsigned unknown3
: 1;
515 /* A little obscure, but last is set for the last texture operation in
516 * a shader. cont appears to just be last's opposite (?). Yeah, I know,
517 * kind of funky.. BiOpen thinks it could do with memory hinting, or
525 /* Is a register used to specify the
526 * LOD/bias/offset? If set, use the `bias` field as
527 * a register index. If clear, use the `bias` field
528 * as an immediate. */
529 unsigned lod_register
: 1;
531 /* Is a register used to specify an offset? If set, use the
532 * offset_reg_* fields to encode this, duplicated for each of the
533 * components. If clear, there is implcitly always an immediate offst
534 * specificed in offset_imm_* */
535 unsigned offset_register
: 1;
537 /* Like in Bifrost */
540 unsigned in_reg_select
: 1;
541 unsigned in_reg_upper
: 1;
542 unsigned in_reg_swizzle
: 8;
544 unsigned unknown8
: 2;
546 unsigned out_full
: 1;
548 /* Always 1 afaict... */
549 unsigned unknown7
: 2;
551 unsigned out_reg_select
: 1;
552 unsigned out_upper
: 1;
556 unsigned unknown2
: 2;
558 unsigned swizzle
: 8;
559 unsigned unknown4
: 8;
561 unsigned unknownA
: 4;
563 /* Each offset field is either an immediate (range 0-7) or, in the case of X, a
564 * register full / select / upper triplet to select the offset vector
565 * register in register mode. In register mode, Y=2 and Z=1 for some
566 * reason. The range in register mode is [-8, 7] */
568 unsigned offset_x
: 3;
569 unsigned offset_unknown4
: 1;
570 unsigned offset_y
: 3;
571 unsigned offset_unknown8
: 1;
572 unsigned offset_z
: 3;
574 unsigned unknownB
: 1;
576 /* Texture bias or LOD, depending on whether it is executed in a
577 * fragment/vertex shader respectively. Compute as int(2^8 * biasf).
579 * For texel fetch, this is the LOD as is. */
582 unsigned unknown9
: 8;
584 unsigned texture_handle
: 16;
585 unsigned sampler_handle
: 16;
587 midgard_texture_word
;