psx_gpu: consolidate C code, implement exnhancement asm
[pcsx_rearmed.git] / plugins / gpu_neon / psx_gpu / psx_gpu_arm_neon.S
1 /*
2  * Copyright (C) 2011 Gilead Kutnick "Exophase" <exophase@gmail.com>
3  * Copyright (C) 2012 GraÅžvydas Ignotas "notaz" <notasas@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  */
15
16 #define MAX_SPANS                                         512
17 #define MAX_BLOCKS                                        64
18 #define MAX_BLOCKS_PER_ROW                                128
19
20 #include "psx_gpu_offsets.h"
21
22 #define psx_gpu_b_dx_offset (psx_gpu_b_block_span_offset + 4)
23
24 #define edge_data_left_x_offset                           0
25 #define edge_data_num_blocks_offset                       2
26 #define edge_data_right_mask_offset                       4
27 #define edge_data_y_offset                                6
28
29
30 #define psx_gpu                                           r0
31 #define v_a                                               r1
32 #define v_b                                               r2
33 #define v_c                                               r3
34
35 #define x0                                                r4
36 #define x1                                                r5
37 #define x2                                                r6
38 #define x0_x1                                             r5
39 #define x1_x2                                             r6
40 #define y0                                                r7
41 #define y1                                                r8
42 #define y2                                                r9
43 #define y0_y1                                             r7
44 #define y1_y2                                             r8
45 #define b0                                                r9
46 #define b1                                                r10
47 #define b2                                                r11
48 #define b0_b1                                             r10
49 #define b1_b2                                             r11
50
51
52 #define area_r_s                                          r5
53
54 #define g_bx0                                             r2
55 #define g_bx                                              r3
56 #define g_bx2                                             r4
57 #define g_bx3                                             r5
58 #define b_base                                            r6
59 #define g_by                                              r8
60
61 #define gs_bx                                             r7
62 #define gs_by                                             r10
63
64 #define ga_bx                                             g_bx
65 #define ga_by                                             g_by
66
67 #define gw_bx_h                                           g_bx
68 #define gw_by_h                                           g_by
69
70 #define gw_bx_l                                           r11
71 #define gw_by_l                                           gw_bx_l
72
73 #define store_a                                           r0
74 #define store_b                                           r1
75 #define store_inc                                         r5
76
77
78 #define v0                                                q0
79 #define uvrgb0                                            d0
80 #define x0_y0                                             d1
81
82 #define v1                                                q1
83 #define uvrgb1                                            d2
84 #define x1_y1                                             d3
85
86 #define v2                                                q2
87 #define uvrgb2                                            d4
88 #define x2_y2                                             d5
89
90 #define x0_ab                                             q3
91 #define uvrg_xxxx0                                        q3
92 #define uvrg0                                             d6
93 #define xxxx0                                             d7
94
95 #define x1_ab                                             q4
96 #define uvrg_xxxx1                                        q4
97 #define uvrg1                                             d8
98 #define xxxx1                                             d9
99
100 #define x2_ab                                             q5
101 #define uvrg_xxxx2                                        q5
102 #define uvrg2                                             d10
103 #define xxxx2                                             d11
104
105 #define y0_ab                                             q6
106 #define yyyy_uvrg0                                        q6
107 #define yyyy0                                             d12
108 #define uvrg0b                                            d13
109
110 #define y1_ab                                             q7
111 #define yyyy_uvrg1                                        q7
112 #define yyyy1                                             d14
113 #define uvrg1b                                            d15
114
115 #define y2_ab                                             q8
116 #define yyyy_uvrg2                                        q8
117 #define yyyy2                                             d16
118 #define uvrg2b                                            d17
119
120 #define d0_ab                                             q9
121 #define d0_a                                              d18
122 #define d0_b                                              d19
123
124 #define d1_ab                                             q10
125 #define d1_a                                              d20
126 #define d1_b                                              d21
127
128 #define d2_ab                                             q11
129 #define d2_a                                              d22
130 #define d2_b                                              d23
131
132 #define d3_ab                                             q12
133 #define d3_a                                              d24
134 #define d3_b                                              d25
135
136 #define ga_uvrg_x                                         q1
137 #define ga_uvrg_y                                         q4
138
139 #define dx                                                x0_x1
140 #define dy                                                y0_y1
141 #define db                                                b0_b1
142
143 #define uvrg_base                                         q11
144
145 #define gs_uvrg_x                                         q5
146 #define gs_uvrg_y                                         q6
147
148 #define g_uvrg_x                                          q1
149 #define ga_uv_x                                           d2
150 #define g_uv_x                                            d2
151 #define ga_rg_x                                           d3
152 #define g_rg_x                                            d3
153
154 #define g_uvrg_y                                          q4
155 #define ga_uv_y                                           d8
156 #define g_uv_y                                            d8
157 #define ga_rg_y                                           d9
158 #define g_rg_y                                            d9
159
160 #define gw_uv_x                                           q1
161 #define gw_rg_x                                           q2
162 #define gw_uv_y                                           q4
163 #define gw_rg_y                                           q3
164
165 #define w_mask                                            q9
166 #define w_mask_l                                          d18
167
168 #define r_shift                                           q10
169
170 #define uvrg_dx0                                          q0
171 #define uvrg_dx0l                                         d0
172 #define uvrg_dx0h                                         d1
173
174 #define uvrg_dx1                                          q1
175 #define uvrg_dx1l                                         d2
176 #define uvrg_dx1h                                         d3
177
178 #define uvrg_dx2                                          q2
179 #define uvrg_dx2l                                         d4
180 #define uvrg_dx2h                                         d5
181
182 #define uvrg_dx3                                          q3
183 #define uvrg_dx3l                                         d6
184 #define uvrg_dx3h                                         d7
185
186 #define uvrgb_phase                                       q13
187
188 .align 4
189
190 /* FIXME: users of this should be in psx_gpu instead */
191 #ifndef __PIC__
192 #define load_pointer(register, pointer)                                        \
193   movw register, :lower16:pointer;                                             \
194   movt register, :upper16:pointer;                                             \
195
196 #else
197 #define load_pointer(register, pointer)                                        \
198   ldr  register, =pointer                                                      \
199
200 #endif
201
202 #define function(name)                                                         \
203   .global name;                                                                \
204   name:                                                                        \
205
206 @ r0: psx_gpu
207 @ r1: v_a
208 @ r2: v_b
209 @ r3: v_c
210
211 function(compute_all_gradients)
212   // First compute the triangle area reciprocal and shift. The division will
213   // happen concurrently with much of the work which follows.
214   @ r12 = psx_gpu->triangle_area
215   ldr r12, [ psx_gpu, #psx_gpu_triangle_area_offset ]
216   stmdb sp!, { r4 - r11, lr }
217
218   @ load exponent of 62 into upper half of double
219   movw r4, #0
220   clz r14, r12                       @ r14 = shift
221
222   movt r4, #((62 + 1023) << 4)
223   mov r12, r12, lsl r14              @ r12 = triangle_area_normalized
224
225   @ load area normalized into lower half of double
226   mov r5, r12, lsr #10
227   vmov.f64 d30, r5, r4               @ d30 = (1 << 62) + ta_n
228
229   movt r4, #((1022 + 31) << 4)
230   mov r5, r12, lsl #20
231
232   add r4, r4, r12, lsr #11
233   vmov.f64 d31, r5, r4
234
235   vdiv.f64 d30, d30, d31             @ d30 = ((1 << 62) + ta_n) / ta_n
236
237   // ((x1 - x0) * (y2 - y1)) - ((x2 - x1) * (y1 - y0)) =
238   // ( d0       *  d1      ) - ( d2       *  d3      ) =
239   // ( m0                  ) - ( m1                  ) = gradient
240
241   // This is split to do 12 elements at a time over three sets: a, b, and c.
242   // Technically we only need to do 10 elements (uvrgb_x and uvrgb_y), so
243   // two of the slots are unused.
244
245   // Inputs are all 16-bit signed. The m0/m1 results are 32-bit signed, as
246   // is g.
247
248   // First type is:  uvrg bxxx xxxx 
249   // Second type is: yyyy ybyy uvrg 
250   // Since x_a and y_c are the same the same variable is used for both. 
251
252   vld1.u32 { v0 }, [ v_a, : 128 ]    @ v0 = { uvrg0, b0, x0, y0 }
253   ldrsh x0, [ v_a, #8 ]              @ load x0
254
255   vld1.u32 { v1 }, [ v_b, : 128 ]    @ v1 = { uvrg1, b1, x1, y1}
256   ldrh x1, [ v_b, #8 ]               @ load x1
257
258   vld1.u32 { v2 }, [ v_c, : 128 ]    @ v2 = { uvrg2, b2, x2, y2 }
259   ldrh x2, [ v_c, #8 ]               @ load x2
260
261   vmovl.u8 uvrg_xxxx0, uvrgb0        @ uvrg_xxxx0 = { uv0, rg0, b0-, -- }
262   ldrh y0, [ v_a, #10 ]              @ load y0
263
264   vmovl.u8 uvrg_xxxx1, uvrgb1        @ uvrg_xxxx1 = { uv1, rg1, b1-, -- }
265   ldrh y1, [ v_b, #10 ]              @ load y1
266
267   vmovl.u8 uvrg_xxxx2, uvrgb2        @ uvrg_xxxx2 = { uv2, rg2, b2-, -- }
268   ldrh y2, [ v_c, #10 ]              @ load y2
269
270   vmov.u8 uvrg0b, uvrg0              @ uvrg0b = { uv0, rg0 }
271   vdup.u16 xxxx0, x0_y0[0]           @ xxxx0 = { xx0, xx0 }
272
273   orr x1_x2, x1, x2, lsl #16         @ x1_x2 = { x1, x2 }
274   pkhbt x0_x1, x0, x1, lsl #16       @ x0_x1 = { x0, x1 }
275
276   vmov.u8 uvrg1b, uvrg1              @ uvrg1b = { uv1, rg1 }
277   vdup.u16 xxxx1, x1_y1[0]           @ xxxx1 = { xx1, xx1 }
278
279   vmov.u8 uvrg2b, uvrg2              @ uvrg2b = { uv2, rg2 }
280   vdup.u16 xxxx2, x2_y2[0]           @ xxxx2 = { xx2, xx2 }
281
282   ldrb b2, [ v_c, #4 ]               @ load b2
283   orr y0_y1, y0, y1, lsl #16         @ y0_y1 = { y0, y1 }
284
285   ldrb b1, [ v_b, #4 ]               @ load b1
286   orr y1_y2, y1, y2, lsl #16         @ y1_y2 = { y1, y2 }
287
288   vdup.u16 yyyy0, x0_y0[1]           @ yyyy0 = { yy0, yy0 }
289   vsub.s16 d0_ab, x1_ab, x0_ab
290
291   ldrb b0, [ v_a, #4 ]               @ load b0
292   orr b1_b2, b1, b2, lsl #16         @ b1_b2 = { b1, b2 }
293
294   vdup.u16 yyyy1, x1_y1[1]           @ yyyy1 = { yy1, yy1 }
295   vsub.s16 d2_ab, x2_ab, x1_ab
296
297   vdup.u16 yyyy2, x2_y2[1]           @ yyyy2 = { yy2, yy2 }
298   vsub.s16 d1_ab, y2_ab, y1_ab
299
300   orr b0_b1, b0, b1, lsl #16         @ b1_b2 = { b1, b2 }
301   ssub16 dx, x1_x2, x0_x1            @ dx = { x1 - x0, x2 - x1 }
302
303   ssub16 dy, y1_y2, y0_y1            @ dy = { y1 - y0, y2 - y1 }
304   ssub16 db, b1_b2, b0_b1            @ db = { b1 - b0, b2 - b1 }
305
306   vsub.s16 d3_ab, y1_ab, y0_ab
307   smusdx ga_by, dx, db               @ ga_by = ((x1 - x0) * (b2 - b1)) -
308                                      @         ((x2 - X1) * (b1 - b0)) 
309   vmull.s16 ga_uvrg_x, d0_a, d1_a
310   smusdx ga_bx, db, dy               @ ga_bx = ((b1 - b0) * (y2 - y1)) -
311                                      @         ((b2 - b1) * (y1 - y0))
312   vmlsl.s16 ga_uvrg_x, d2_a, d3_a
313   movs gs_bx, ga_bx, asr #31
314
315   vmull.s16 ga_uvrg_y, d0_b, d1_b
316   rsbmi ga_bx, ga_bx, #0
317
318   @ r12 = psx_gpu->uvrgb_phase
319   ldr r12, [ psx_gpu, #psx_gpu_uvrgb_phase_offset ]
320
321   vmlsl.s16 ga_uvrg_y, d2_b, d3_b
322   movs gs_by, ga_by, asr #31
323
324   vshr.u64 d0, d30, #22
325   add b_base, r12, b0, lsl #16
326
327   vdup.u32 uvrgb_phase, r12
328
329   rsbmi ga_by, ga_by, #0
330   vclt.s32 gs_uvrg_x, ga_uvrg_x, #0  @ gs_uvrg_x = ga_uvrg_x < 0
331
332   @ r12 = psx_gpu->triangle_winding_offset
333   ldrb r12, [ psx_gpu, #psx_gpu_triangle_winding_offset ]
334   vclt.s32 gs_uvrg_y, ga_uvrg_y, #0  @ gs_uvrg_y = ga_uvrg_y < 0
335
336   rsb r12, r12, #0                   @ r12 = -(triangle->winding)
337
338   vdup.u32 w_mask, r12               @ w_mask = { -w, -w, -w, -w }
339   sub r14, r14, #(62 - 12)           @ r14 = shift - (62 - FIXED_BITS)
340
341   vshll.u16 uvrg_base, uvrg0, #16    @ uvrg_base = uvrg0 << 16
342   vdup.u32 r_shift, r14              @ r_shift = { shift, shift, shift, shift }
343
344   vadd.u32 uvrg_base, uvrgb_phase
345   vabs.s32 ga_uvrg_x, ga_uvrg_x      @ ga_uvrg_x = abs(ga_uvrg_x)
346
347   vmov area_r_s, s0                  @ area_r_s = triangle_reciprocal
348   vabs.s32 ga_uvrg_y, ga_uvrg_y      @ ga_uvrg_y = abs(ga_uvrg_y)
349
350   vmull.u32 gw_rg_x, ga_rg_x, d0[0]
351   vmull.u32 gw_uv_x, ga_uv_x, d0[0]
352   vmull.u32 gw_rg_y, ga_rg_y, d0[0]
353   vmull.u32 gw_uv_y, ga_uv_y, d0[0]
354
355   vshl.u64 gw_rg_x, gw_rg_x, r_shift
356   vshl.u64 gw_uv_x, gw_uv_x, r_shift
357   vshl.u64 gw_rg_y, gw_rg_y, r_shift
358   vshl.u64 gw_uv_y, gw_uv_y, r_shift
359
360   veor.u32 gs_uvrg_x, gs_uvrg_x, w_mask
361   vmovn.u64 g_uv_x, gw_uv_x
362
363   veor.u32 gs_uvrg_y, gs_uvrg_y, w_mask
364   vmovn.u64 g_rg_x, gw_rg_x
365
366   veor.u32 g_uvrg_x, g_uvrg_x, gs_uvrg_x
367   vmovn.u64 g_uv_y, gw_uv_y
368
369   vsub.u32 g_uvrg_x, g_uvrg_x, gs_uvrg_x
370   vmovn.u64 g_rg_y, gw_rg_y
371
372   veor.u32 g_uvrg_y, g_uvrg_y, gs_uvrg_y
373   mov ga_bx, ga_bx, lsl #13
374
375   vsub.u32 g_uvrg_y, g_uvrg_y, gs_uvrg_y
376   mov ga_by, ga_by, lsl #13
377
378   vdup.u32 x0_y0, x0
379   umull gw_bx_l, gw_bx_h, ga_bx, area_r_s
380
381   vshl.u32 g_uvrg_x, g_uvrg_x, #4
382   vshl.u32 g_uvrg_y, g_uvrg_y, #4
383
384   umull gw_by_l, gw_by_h, ga_by, area_r_s
385   vmls.s32 uvrg_base, ga_uvrg_x, x0_y0[0]
386
387   eor gs_bx, gs_bx, r12
388   vadd.u32 uvrg_dx2, uvrg_dx1, uvrg_dx1
389
390   veor.u32 uvrg_dx0, uvrg_dx0, uvrg_dx0
391   eor gs_by, gs_by, r12
392
393   rsb r11, r14, #0                   @ r11 = negative shift for scalar lsr
394   add store_a, psx_gpu, #psx_gpu_uvrg_offset
395
396   sub r11, r11, #(32 - 13)
397
398   add store_b, store_a, #16
399   mov store_inc, #32
400
401   vadd.u32 uvrg_dx3, uvrg_dx2, uvrg_dx1
402   vst1.u32 { uvrg_base }, [ store_a, : 128 ], store_inc
403
404   vst1.u32 { uvrg_dx1 }, [ store_b, : 128 ], store_inc
405   mov g_bx, gw_bx_h, lsr r11
406
407   vst1.u32 { g_uvrg_y }, [ store_a, : 128 ], store_inc
408   mov g_by, gw_by_h, lsr r11
409
410   vst4.u32 { uvrg_dx0l, uvrg_dx1l, uvrg_dx2l, uvrg_dx3l },                     \
411    [ store_b, : 128 ], store_inc
412   eor g_bx, g_bx, gs_bx
413
414   vst4.u32 { uvrg_dx0h, uvrg_dx1h, uvrg_dx2h, uvrg_dx3h },                     \
415    [ store_b, : 128 ], store_inc
416   sub g_bx, g_bx, gs_bx
417
418   lsl g_bx, g_bx, #4  
419   eor g_by, g_by, gs_by
420
421   mls b_base, g_bx, x0, b_base
422   sub g_by, g_by, gs_by
423
424   lsl g_by, g_by, #4
425   mov g_bx0, #0
426
427   add g_bx2, g_bx, g_bx
428   add g_bx3, g_bx, g_bx2
429
430   stmia store_b, { g_bx0, g_bx, g_bx2, g_bx3, b_base, g_by }
431
432   ldmia sp!, { r4 - r11, pc }
433
434
435 #define psx_gpu                                  r0
436 #define v_a                                      r1
437 #define v_b                                      r2
438 #define v_c                                      r3
439
440 #define temp                                     r14
441
442 #define x_a                                      r4
443 #define x_b                                      r5
444 #define x_c                                      r6
445 #define y_a                                      r1
446 #define y_b                                      r2
447 #define y_c                                      r3
448
449 #define height_minor_a                           r7
450 #define height_minor_b                           r8
451 #define height_major                             r9
452 #define height                                   r9
453
454 #define reciprocal_table_ptr                     r10
455
456 #define edge_alt_low                             r4
457 #define edge_alt_high                            r5
458 #define edge_dx_dy_alt                           r6
459 #define edge_shift_alt                           r10
460
461 #define edge_dx_dy_alt_low                       r4
462 #define edge_dx_dy_alt_high                      r5
463
464 #define span_edge_data                           r4
465 #define span_uvrg_offset                         r5
466 #define span_b_offset                            r6
467
468 #define clip                                     r14
469
470 #define b                                        r11
471 #define b_dy                                     r12
472
473
474 #define alternate_x                              q0
475 #define alternate_dx_dy                          q1
476 #define alternate_x_32                           q2
477
478 #define alternate_x_low                          d0
479 #define alternate_x_high                         d1
480 #define alternate_dx_dy_low                      d2
481 #define alternate_dx_dy_high                     d3
482 #define alternate_x_32_low                       d4
483 #define alternate_x_32_high                      d5
484
485 #define left_x                                   q3
486 #define right_x                                  q4
487 #define left_dx_dy                               q5
488 #define right_dx_dy                              q6
489 #define left_edge                                q7
490 #define right_edge                               q8
491
492 #define left_x_low                               d6
493 #define left_x_high                              d7
494 #define right_x_low                              d8
495 #define right_x_high                             d9
496 #define left_dx_dy_low                           d10
497 #define left_dx_dy_high                          d11
498 #define right_dx_dy_low                          d12
499 #define right_dx_dy_high                         d13
500 #define left_edge_low                            d14
501 #define left_edge_high                           d15
502 #define right_edge_low                           d16
503 #define right_edge_high                          d17
504
505 #define y_mid_point                              d18
506 #define c_0x0004                                 d19
507
508 #define left_right_x_16                          q11
509 #define span_shifts_y                            q12
510 #define c_0x0001                                 q13
511
512 #define span_shifts                              d24
513 #define y_x4                                     d25
514 #define c_0xFFFE                                 d26
515 #define c_0x0007                                 d27
516
517 #define left_right_x_16_low                      d22
518 #define left_right_x_16_high                     d23
519
520 #define uvrg                                     q14
521 #define uvrg_dy                                  q15
522
523 #define alternate_x_16                           d4
524
525 #define v_clip                                   q3
526 #define v_clip_low                               d6
527
528 #define right_x_32                               q10
529 #define left_x_32                                q11
530 #define alternate_select                         d24
531
532 #define right_x_32_low                           d20
533 #define right_x_32_high                          d21
534 #define left_x_32_low                            d22
535 #define left_x_32_high                           d23
536
537 #define edges_xy                                 q0
538 #define edges_dx_dy                              d2
539 #define edge_shifts                              d3
540 #define edge_shifts_64                           q2
541
542 #define edges_xy_left                            d0
543 #define edges_xy_right                           d1
544
545 #define height_reciprocals                       d6
546 #define heights                                  d7
547
548 #define widths                                   d8
549 #define c_0x01                                   d9
550 #define x_starts                                 d10
551 #define x_ends                                   d11
552
553 #define heights_b                                d12
554 #define edges_dx_dy_64                           q10
555
556 #define edges_dx_dy_64_left                      d20
557 #define edges_dx_dy_64_right                     d21
558
559
560 #define setup_spans_prologue()                                                 \
561   stmdb sp!, { r4 - r11, lr };                                                 \
562                                                                                \
563   ldrsh x_a, [ v_a, #8 ];                                                      \
564   ldrsh x_b, [ v_b, #8 ];                                                      \
565   ldrsh x_c, [ v_c, #8 ];                                                      \
566   ldrsh y_a, [ v_a, #10 ];                                                     \
567   ldrsh y_b, [ v_b, #10 ];                                                     \
568   ldrsh y_c, [ v_c, #10 ];                                                     \
569                                                                                \
570   add temp, psx_gpu, #psx_gpu_uvrg_offset;                                     \
571   vld1.32 { uvrg }, [ temp ];                                                  \
572   add temp, psx_gpu, #psx_gpu_uvrg_dy_offset;                                  \
573   vld1.32 { uvrg_dy }, [ temp ];                                               \
574   load_pointer(reciprocal_table_ptr, reciprocal_table);                        \
575                                                                                \
576   vmov.u32 c_0x01, #0x01                                                       \
577
578 #define setup_spans_load_b()                                                   \
579   ldr b, [ psx_gpu, #psx_gpu_b_offset ];                                       \
580   ldr b_dy, [ psx_gpu, #psx_gpu_b_dy_offset ]                                  \
581
582 #define setup_spans_prologue_b()                                               \
583   add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
584   add temp, psx_gpu, #psx_gpu_viewport_start_x_offset;                         \
585                                                                                \
586   add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
587   vmov.u16 c_0x0004, #0x0004;                                                  \
588                                                                                \
589   add span_b_offset, psx_gpu, #psx_gpu_span_b_offset_offset;                   \
590   vmov.u16 c_0x0001, #0x0001;                                                  \
591                                                                                \
592   vld1.u16 { left_edge_low[], left_edge_high[] }, [ temp ];                    \
593   add temp, psx_gpu, #psx_gpu_viewport_end_x_offset;                           \
594                                                                                \
595   vld1.u16 { right_edge_low[], right_edge_high[] }, [ temp ];                  \
596   vadd.u16 right_edge, right_edge, c_0x0001;                                   \
597                                                                                \
598   vmov.u16 c_0x0007, #0x0007;                                                  \
599   vmvn.u16 c_0xFFFE, #0x0001                                                   \
600
601
602 #define compute_edge_delta_x2()                                                \
603   ldr temp, [ reciprocal_table_ptr, height, lsl #2 ];                          \
604                                                                                \
605   vdup.u32 heights, height;                                                    \
606   vsub.u32 widths, x_ends, x_starts;                                           \
607                                                                                \
608   vdup.u32 edge_shifts, temp;                                                  \
609   vsub.u32 heights_b, heights, c_0x01;                                         \
610   vshr.u32 height_reciprocals, edge_shifts, #10;                               \
611                                                                                \
612   vmla.s32 heights_b, x_starts, heights;                                       \
613   vbic.u16 edge_shifts, #0xE0;                                                 \
614   vmul.s32 edges_dx_dy, widths, height_reciprocals;                            \
615   vmull.s32 edges_xy, heights_b, height_reciprocals                            \
616
617 #define width_alt                 r6
618 #define height_reciprocal_alt     r11
619 #define height_b_alt              r12
620
621 #define compute_edge_delta_x3(start_c, height_a, height_b)                     \
622   vmov.u32 heights, height_a, height_b;                                        \
623   ldr temp, [ reciprocal_table_ptr, height_a, lsl #2 ];                        \
624   vmov.u32 edge_shifts[0], temp;                                               \
625   ldr temp, [ reciprocal_table_ptr, height_b, lsl #2 ];                        \
626   vmov.u32 edge_shifts[1], temp;                                               \
627   ldr edge_shift_alt, [ reciprocal_table_ptr, height_minor_b, lsl #2 ];        \
628                                                                                \
629   vsub.u32 widths, x_ends, x_starts;                                           \
630   sub width_alt, x_c, start_c;                                                 \
631                                                                                \
632   vsub.u32 heights_b, heights, c_0x01;                                         \
633   sub height_b_alt, height_minor_b, #1;                                        \
634                                                                                \
635   vshr.u32 height_reciprocals, edge_shifts, #10;                               \
636   lsr height_reciprocal_alt, edge_shift_alt, #10;                              \
637                                                                                \
638   vmla.s32 heights_b, x_starts, heights;                                       \
639   mla height_b_alt, height_minor_b, start_c, height_b_alt;                     \
640                                                                                \
641   vbic.u16 edge_shifts, #0xE0;                                                 \
642   and edge_shift_alt, edge_shift_alt, #0x1F;                                   \
643                                                                                \
644   vmul.s32 edges_dx_dy, widths, height_reciprocals;                            \
645   mul edge_dx_dy_alt, width_alt, height_reciprocal_alt;                        \
646                                                                                \
647   vmull.s32 edges_xy, heights_b, height_reciprocals;                           \
648   smull edge_alt_low, edge_alt_high, height_b_alt, height_reciprocal_alt       \
649
650
651 #define setup_spans_adjust_y_up()                                              \
652   vsub.u32 y_x4, y_x4, c_0x0004                                                \
653
654 #define setup_spans_adjust_y_down()                                            \
655   vadd.u32 y_x4, y_x4, c_0x0004                                                \
656
657 #define setup_spans_adjust_interpolants_up()                                   \
658   vsub.u32 uvrg, uvrg, uvrg_dy;                                                \
659   sub b, b, b_dy                                                               \
660
661 #define setup_spans_adjust_interpolants_down()                                 \
662   vadd.u32 uvrg, uvrg, uvrg_dy;                                                \
663   add b, b, b_dy                                                               \
664
665
666 #define setup_spans_clip_interpolants_increment()                              \
667   mla b, b_dy, clip, b;                                                        \
668   vmla.s32 uvrg, uvrg_dy, v_clip                                               \
669
670 #define setup_spans_clip_interpolants_decrement()                              \
671   mls b, b_dy, clip, b;                                                        \
672   vmls.s32 uvrg, uvrg_dy, v_clip                                               \
673
674 #define setup_spans_clip_alternate_yes()                                       \
675   smlal edge_alt_low, edge_alt_high, edge_dx_dy_alt, clip                      \
676
677 #define setup_spans_clip_alternate_no()                                        \
678
679 #define setup_spans_clip(direction, alternate_active)                          \
680   vdup.u32 v_clip, clip;                                                       \
681   setup_spans_clip_alternate_##alternate_active();                             \
682   setup_spans_clip_interpolants_##direction();                                 \
683   vmlal.s32 edges_xy, edges_dx_dy, v_clip_low                                  \
684
685
686 #define setup_spans_adjust_edges_alternate_no(left_index, right_index)         \
687   vmovl.s32 edge_shifts_64, edge_shifts;                                       \
688   vmovl.s32 edges_dx_dy_64, edges_dx_dy;                                       \
689                                                                                \
690   vshl.s64 edges_xy, edges_xy, edge_shifts_64;                                 \
691   vshl.s64 edges_dx_dy_64, edges_dx_dy_64, edge_shifts_64;                     \
692                                                                                \
693   vmov left_x_low, edges_xy_##left_index;                                      \
694   vmov right_x_low, edges_xy_##right_index;                                    \
695                                                                                \
696   vmov left_dx_dy_low, edges_dx_dy_64_##left_index;                            \
697   vmov left_dx_dy_high, edges_dx_dy_64_##left_index;                           \
698   vmov right_dx_dy_low, edges_dx_dy_64_##right_index;                          \
699   vmov right_dx_dy_high, edges_dx_dy_64_##right_index;                         \
700                                                                                \
701   vadd.u64 left_x_high, left_x_low, left_dx_dy_low;                            \
702   vadd.u64 right_x_high, right_x_low, right_dx_dy_low;                         \
703                                                                                \
704   vadd.u64 left_dx_dy, left_dx_dy, left_dx_dy;                                 \
705   vadd.u64 right_dx_dy, right_dx_dy, right_dx_dy                               \
706
707
708 #define setup_spans_adjust_edges_alternate_yes(left_index, right_index)        \
709   setup_spans_adjust_edges_alternate_no(left_index, right_index);              \
710                                                                                \
711   vdup.u16 y_mid_point, y_b;                                                   \
712   rsb temp, edge_shift_alt, #32;                                               \
713                                                                                \
714   lsl edge_alt_high, edge_alt_high, edge_shift_alt;                            \
715   orr edge_alt_high, edge_alt_high, edge_alt_low, lsr temp;                    \
716   lsl edge_alt_low, edge_alt_low, edge_shift_alt;                              \
717   vmov alternate_x_low, edge_alt_low, edge_alt_high;                           \
718                                                                                \
719   asr edge_dx_dy_alt_high, edge_dx_dy_alt, temp;                               \
720   lsl edge_dx_dy_alt_low, edge_dx_dy_alt, edge_shift_alt;                      \
721   vmov alternate_dx_dy_low, edge_dx_dy_alt_low, edge_dx_dy_alt_high;           \
722   vmov alternate_dx_dy_high, alternate_dx_dy_low;                              \
723                                                                                \
724   vadd.u64 alternate_x_high, alternate_x_low, alternate_dx_dy_low;             \
725   vadd.u64 alternate_dx_dy, alternate_dx_dy, alternate_dx_dy                   \
726
727
728 #define setup_spans_y_select_up()                                              \
729   vclt.s16 alternate_select, y_x4, y_mid_point                                 \
730
731 #define setup_spans_y_select_down()                                            \
732   vcgt.s16 alternate_select, y_x4, y_mid_point                                 \
733
734
735 #define setup_spans_alternate_select_left()                                    \
736   vbit.u16 left_right_x_16_low, alternate_x_16, alternate_select               \
737
738 #define setup_spans_alternate_select_right()                                   \
739   vbit.u16 left_right_x_16_high, alternate_x_16, alternate_select              \
740
741
742 #define setup_spans_set_x4_alternate_yes(alternate, direction)                 \
743   vshrn.s64 alternate_x_32_low, alternate_x, #32;                              \
744   vshrn.s64 left_x_32_low, left_x, #32;                                        \
745   vshrn.s64 right_x_32_low, right_x, #32;                                      \
746                                                                                \
747   vadd.u64 alternate_x, alternate_x, alternate_dx_dy;                          \
748   vadd.u64 left_x, left_x, left_dx_dy;                                         \
749   vadd.u64 right_x, right_x, right_dx_dy;                                      \
750                                                                                \
751   vshrn.s64 alternate_x_32_high, alternate_x, #32;                             \
752   vshrn.s64 left_x_32_high, left_x, #32;                                       \
753   vshrn.s64 right_x_32_high, right_x, #32;                                     \
754                                                                                \
755   vadd.u64 alternate_x, alternate_x, alternate_dx_dy;                          \
756   vadd.u64 left_x, left_x, left_dx_dy;                                         \
757   vadd.u64 right_x, right_x, right_dx_dy;                                      \
758                                                                                \
759   vmovn.u32 alternate_x_16, alternate_x_32;                                    \
760   setup_spans_y_select_##direction();                                          \
761   vmovn.u32 left_right_x_16_low, left_x_32;                                    \
762                                                                                \
763   vmovn.u32 left_right_x_16_high, right_x_32;                                  \
764   setup_spans_alternate_select_##alternate();                                  \
765                                                                                \
766   vst1.u32 { uvrg }, [ span_uvrg_offset, :128 ]!;                              \
767   str b, [ span_b_offset ], #4;                                                \
768   setup_spans_adjust_interpolants_##direction();                               \
769                                                                                \
770   vmax.s16 left_right_x_16, left_right_x_16, left_edge;                        \
771                                                                                \
772   vst1.u32 { uvrg }, [ span_uvrg_offset, :128 ]!;                              \
773   str b, [ span_b_offset ], #4;                                                \
774   setup_spans_adjust_interpolants_##direction();                               \
775                                                                                \
776   vmin.s16 left_right_x_16, left_right_x_16, right_edge;                       \
777                                                                                \
778   vst1.u32 { uvrg }, [ span_uvrg_offset, :128 ]!;                              \
779   str b, [ span_b_offset ], #4;                                                \
780   setup_spans_adjust_interpolants_##direction();                               \
781                                                                                \
782   vsub.u16 left_right_x_16_high, left_right_x_16_high, left_right_x_16_low;    \
783   vadd.u16 left_right_x_16_high, left_right_x_16_high, c_0x0007;               \
784   vand.u16 span_shifts, left_right_x_16_high, c_0x0007;                        \
785                                                                                \
786   vst1.u32 { uvrg }, [ span_uvrg_offset, :128 ]!;                              \
787   str b, [ span_b_offset ], #4;                                                \
788   setup_spans_adjust_interpolants_##direction();                               \
789                                                                                \
790   vshr.u16 left_right_x_16_high, left_right_x_16_high, #3;                     \
791   vshl.u16 span_shifts, c_0xFFFE, span_shifts;                                 \
792                                                                                \
793   vst4.u16 { left_right_x_16, span_shifts_y }, [ span_edge_data ]!;            \
794                                                                                \
795   setup_spans_adjust_y_##direction()                                           \
796
797
798 #define setup_spans_set_x4_alternate_no(alternate, direction)                  \
799   vshrn.s64 left_x_32_low, left_x, #32;                                        \
800   vshrn.s64 right_x_32_low, right_x, #32;                                      \
801                                                                                \
802   vadd.u64 left_x, left_x, left_dx_dy;                                         \
803   vadd.u64 right_x, right_x, right_dx_dy;                                      \
804                                                                                \
805   vshrn.s64 left_x_32_high, left_x, #32;                                       \
806   vshrn.s64 right_x_32_high, right_x, #32;                                     \
807                                                                                \
808   vadd.u64 left_x, left_x, left_dx_dy;                                         \
809   vadd.u64 right_x, right_x, right_dx_dy;                                      \
810                                                                                \
811   vmovn.u32 left_right_x_16_low, left_x_32;                                    \
812   vmovn.u32 left_right_x_16_high, right_x_32;                                  \
813                                                                                \
814   vst1.u32 { uvrg }, [ span_uvrg_offset, :128 ]!;                              \
815   str b, [ span_b_offset ], #4;                                                \
816   setup_spans_adjust_interpolants_##direction();                               \
817                                                                                \
818   vmax.s16 left_right_x_16, left_right_x_16, left_edge;                        \
819                                                                                \
820   vst1.u32 { uvrg }, [ span_uvrg_offset, :128 ]!;                              \
821   str b, [ span_b_offset ], #4;                                                \
822   setup_spans_adjust_interpolants_##direction();                               \
823                                                                                \
824   vmin.s16 left_right_x_16, left_right_x_16, right_edge;                       \
825                                                                                \
826   vst1.u32 { uvrg }, [ span_uvrg_offset, :128 ]!;                              \
827   str b, [ span_b_offset ], #4;                                                \
828   setup_spans_adjust_interpolants_##direction();                               \
829                                                                                \
830   vsub.u16 left_right_x_16_high, left_right_x_16_high, left_right_x_16_low;    \
831   vadd.u16 left_right_x_16_high, left_right_x_16_high, c_0x0007;               \
832   vand.u16 span_shifts, left_right_x_16_high, c_0x0007;                        \
833                                                                                \
834   vst1.u32 { uvrg }, [ span_uvrg_offset, :128 ]!;                              \
835   str b, [ span_b_offset ], #4;                                                \
836   setup_spans_adjust_interpolants_##direction();                               \
837                                                                                \
838   vshl.u16 span_shifts, c_0xFFFE, span_shifts;                                 \
839   vshr.u16 left_right_x_16_high, left_right_x_16_high, #3;                     \
840                                                                                \
841   vst4.u16 { left_right_x_16, span_shifts_y }, [ span_edge_data ]!;            \
842                                                                                \
843   setup_spans_adjust_y_##direction()                                           \
844
845
846 #define edge_adjust_low           r11
847 #define edge_adjust_high          r12
848
849 #define setup_spans_alternate_adjust_yes()                                     \
850   smull edge_adjust_low, edge_adjust_high, edge_dx_dy_alt, height_minor_a;     \
851   subs edge_alt_low, edge_alt_low, edge_adjust_low;                            \
852   sbc edge_alt_high, edge_alt_high, edge_adjust_high                           \
853
854 #define setup_spans_alternate_adjust_no()                                      \
855
856
857 #define setup_spans_down(left_index, right_index, alternate, alternate_active) \
858   setup_spans_alternate_adjust_##alternate_active();                           \
859   setup_spans_load_b();                                                        \
860                                                                                \
861   ldrsh temp, [ psx_gpu, #psx_gpu_viewport_end_y_offset ];                     \
862   subs y_c, y_c, temp;                                                         \
863   subgt height, height, y_c;                                                   \
864   addgt height, height, #1;                                                    \
865                                                                                \
866   ldrsh temp, [ psx_gpu, #psx_gpu_viewport_start_y_offset ];                   \
867   subs clip, temp, y_a;                                                        \
868   ble 0f;                                                                      \
869                                                                                \
870   sub height, height, clip;                                                    \
871   add y_a, y_a, clip;                                                          \
872   setup_spans_clip(increment, alternate_active);                               \
873                                                                                \
874  0:                                                                            \
875   cmp height, #0;                                                              \
876   ble 1f;                                                                      \
877                                                                                \
878   orr temp, y_a, y_a, lsl #16;                                                 \
879   add temp, temp, #(1 << 16);                                                  \
880   add y_a, temp, #2;                                                           \
881   add y_a, y_a, #(2 << 16);                                                    \
882   vmov.u32 y_x4, temp, y_a;                                                    \
883                                                                                \
884   setup_spans_adjust_edges_alternate_##alternate_active(left_index,            \
885    right_index);                                                               \
886   setup_spans_prologue_b();                                                    \
887                                                                                \
888   strh height, [ psx_gpu, #psx_gpu_num_spans_offset ];                         \
889                                                                                \
890  2:                                                                            \
891   setup_spans_set_x4_alternate_##alternate_active(alternate, down);            \
892   subs height, height, #4;                                                     \
893   bhi 2b;                                                                      \
894                                                                                \
895  1:                                                                            \
896
897
898 #define setup_spans_alternate_pre_increment_yes()                              \
899   adds edge_alt_low, edge_alt_low, edge_dx_dy_alt;                             \
900   adc edge_alt_high, edge_alt_high, edge_dx_dy_alt, asr #31                    \
901
902 #define setup_spans_alternate_pre_increment_no()                               \
903
904
905 #define setup_spans_up_decrement_yes()                                         \
906   suble height, height, #1                                                     \
907
908 #define setup_spans_up_decrement_no()                                          \
909
910
911 #define setup_spans_up(left_index, right_index, alternate, alternate_active)   \
912   setup_spans_alternate_adjust_##alternate_active();                           \
913   setup_spans_load_b();                                                        \
914   sub y_a, y_a, #1;                                                            \
915                                                                                \
916   ldrh temp, [ psx_gpu, #psx_gpu_viewport_start_y_offset ];                    \
917   subs temp, temp, y_c;                                                        \
918   subgt height, height, temp;                                                  \
919   setup_spans_up_decrement_##alternate_active();                               \
920                                                                                \
921   ldrh temp, [ psx_gpu, #psx_gpu_viewport_end_y_offset ];                      \
922   subs clip, y_a, temp;                                                        \
923   ble 0f;                                                                      \
924                                                                                \
925   sub height, height, clip;                                                    \
926   sub y_a, y_a, clip;                                                          \
927   setup_spans_clip(decrement, alternate_active);                               \
928                                                                                \
929  0:                                                                            \
930   cmp height, #0;                                                              \
931   ble 1f;                                                                      \
932                                                                                \
933   orr temp, y_a, y_a, lsl #16;                                                 \
934   sub temp, temp, #(1 << 16);                                                  \
935   sub y_a, temp, #2;                                                           \
936   sub y_a, y_a, #(2 << 16);                                                    \
937   vmov.u32 y_x4, temp, y_a;                                                    \
938                                                                                \
939   vaddw.s32 edges_xy, edges_xy, edges_dx_dy;                                   \
940                                                                                \
941   setup_spans_alternate_pre_increment_##alternate_active();                    \
942   setup_spans_adjust_edges_alternate_##alternate_active(left_index,            \
943    right_index);                                                               \
944   setup_spans_adjust_interpolants_up();                                        \
945   setup_spans_prologue_b();                                                    \
946                                                                                \
947   strh height, [ psx_gpu, #psx_gpu_num_spans_offset ];                         \
948                                                                                \
949  2:                                                                            \
950   setup_spans_set_x4_alternate_##alternate_active(alternate, up);              \
951   subs height, height, #4;                                                     \
952   bhi 2b;                                                                      \
953                                                                                \
954  1:                                                                            \
955
956
957 #define setup_spans_epilogue()                                                 \
958   ldmia sp!, { r4 - r11, pc }                                                  \
959
960
961 #define setup_spans_up_up(minor, major)                                        \
962   setup_spans_prologue();                                                      \
963   sub height_minor_a, y_a, y_b;                                                \
964   sub height_minor_b, y_b, y_c;                                                \
965   sub height, y_a, y_c;                                                        \
966                                                                                \
967   vdup.u32 x_starts, x_a;                                                      \
968   vmov.u32 x_ends, x_c, x_b;                                                   \
969                                                                                \
970   compute_edge_delta_x3(x_b, height_major, height_minor_a);                    \
971   setup_spans_up(major, minor, minor, yes);                                    \
972   setup_spans_epilogue()                                                       \
973
974 function(setup_spans_up_left)
975   setup_spans_up_up(left, right)
976
977 function(setup_spans_up_right)
978   setup_spans_up_up(right, left)
979
980 .pool
981
982 #define setup_spans_down_down(minor, major)                                    \
983   setup_spans_prologue();                                                      \
984   sub height_minor_a, y_b, y_a;                                                \
985   sub height_minor_b, y_c, y_b;                                                \
986   sub height, y_c, y_a;                                                        \
987                                                                                \
988   vdup.u32 x_starts, x_a;                                                      \
989   vmov.u32 x_ends, x_c, x_b;                                                   \
990                                                                                \
991   compute_edge_delta_x3(x_b, height_major, height_minor_a);                    \
992   setup_spans_down(major, minor, minor, yes);                                  \
993   setup_spans_epilogue()                                                       \
994
995 function(setup_spans_down_left)
996   setup_spans_down_down(left, right)
997
998 function(setup_spans_down_right)
999   setup_spans_down_down(right, left)
1000
1001
1002 #define setup_spans_up_flat()                                                  \
1003   sub height, y_a, y_c;                                                        \
1004                                                                                \
1005   compute_edge_delta_x2();                                                     \
1006   setup_spans_up(left, right, none, no);                                       \
1007   setup_spans_epilogue()                                                       \
1008
1009 function(setup_spans_up_a)
1010   setup_spans_prologue()
1011
1012   vmov.u32 x_starts, x_a, x_b
1013   vdup.u32 x_ends, x_c
1014
1015   setup_spans_up_flat()
1016
1017 function(setup_spans_up_b)
1018   setup_spans_prologue()
1019
1020   vdup.u32 x_starts, x_a
1021   vmov.u32 x_ends, x_b, x_c
1022
1023   setup_spans_up_flat()
1024
1025 #define setup_spans_down_flat()                                                \
1026   sub height, y_c, y_a;                                                        \
1027                                                                                \
1028   compute_edge_delta_x2();                                                     \
1029   setup_spans_down(left, right, none, no);                                     \
1030   setup_spans_epilogue()                                                       \
1031
1032 function(setup_spans_down_a)
1033   setup_spans_prologue()
1034
1035   vmov.u32 x_starts, x_a, x_b
1036   vdup.u32 x_ends, x_c
1037
1038   setup_spans_down_flat()
1039
1040 function(setup_spans_down_b)
1041   setup_spans_prologue()
1042
1043   vdup.u32 x_starts, x_a
1044   vmov.u32 x_ends, x_b, x_c
1045
1046   setup_spans_down_flat()
1047
1048
1049 #define middle_y                                          r9
1050
1051 #define edges_xy_b                                        q11
1052 #define edges_dx_dy_b                                     d26
1053 #define edge_shifts_b                                     d27
1054 #define edges_dx_dy_and_shifts_b                          q13
1055 #define height_increment                                  d20
1056
1057 #define edges_dx_dy_and_shifts                            q1
1058
1059 #define edges_xy_b_left                                   d22
1060 #define edges_xy_b_right                                  d23
1061
1062 #define setup_spans_up_down_load_edge_set_b()                                  \
1063   vmov edges_xy, edges_xy_b;                                                   \
1064   vmov edges_dx_dy_and_shifts, edges_dx_dy_and_shifts_b                        \
1065
1066
1067 function(setup_spans_up_down)
1068   setup_spans_prologue()
1069
1070   // s32 middle_y = y_a;
1071   sub height_minor_a, y_a, y_b
1072   sub height_minor_b, y_c, y_a
1073   sub height_major, y_c, y_b
1074
1075   vmov.u32 x_starts, x_a, x_c
1076   vdup.u32 x_ends, x_b
1077
1078   compute_edge_delta_x3(x_a, height_minor_a, height_major)
1079
1080   mov temp, #0
1081   vmov.u32 height_increment, temp, height_minor_b
1082   vmlal.s32 edges_xy, edges_dx_dy, height_increment
1083
1084   vmov edges_xy_b_left, edge_alt_low, edge_alt_high
1085   vmov edges_xy_b_right, edges_xy_right
1086
1087   vmov edge_shifts_b, edge_shifts
1088   vmov.u32 edge_shifts_b[0], edge_shift_alt
1089
1090   vneg.s32 edges_dx_dy_b, edges_dx_dy
1091   vmov.u32 edges_dx_dy_b[0], edge_dx_dy_alt
1092
1093   mov middle_y, y_a
1094   
1095   setup_spans_load_b()
1096   sub y_a, y_a, #1
1097
1098   ldrh temp, [ psx_gpu, #psx_gpu_viewport_start_y_offset ]
1099   subs temp, temp, y_b
1100   subgt height_minor_a, height_minor_a, temp
1101
1102   ldrh temp, [ psx_gpu, #psx_gpu_viewport_end_y_offset ]
1103   subs clip, y_a, temp
1104   ble 0f
1105
1106   sub height_minor_a, height_minor_a, clip
1107   sub y_a, y_a, clip
1108   setup_spans_clip(decrement, no)
1109
1110  0:                                                                
1111   cmp height_minor_a, #0
1112   ble 3f
1113
1114   orr temp, y_a, y_a, lsl #16
1115   sub temp, temp, #(1 << 16)
1116   sub y_a, temp, #2
1117   sub y_a, y_a, #(2 << 16)
1118   vmov.u32 y_x4, temp, y_a
1119
1120   vaddw.s32 edges_xy, edges_xy, edges_dx_dy
1121
1122   strh height_minor_a, [ psx_gpu, #psx_gpu_num_spans_offset ]
1123
1124   setup_spans_adjust_edges_alternate_no(left, right); 
1125   setup_spans_adjust_interpolants_up()
1126   setup_spans_up_down_load_edge_set_b()
1127
1128   setup_spans_prologue_b()
1129
1130
1131  2: 
1132   setup_spans_set_x4_alternate_no(none, up)
1133   subs height_minor_a, height_minor_a, #4
1134   bhi 2b
1135
1136   add span_edge_data, span_edge_data, height_minor_a, lsl #3
1137   add span_uvrg_offset, span_uvrg_offset, height_minor_a, lsl #4
1138   add span_b_offset, span_b_offset, height_minor_a, lsl #2
1139
1140  4:
1141   add temp, psx_gpu, #psx_gpu_uvrg_offset
1142   vld1.32 { uvrg }, [ temp ]
1143   mov y_a, middle_y
1144   
1145   setup_spans_load_b()
1146
1147   ldrh temp, [ psx_gpu, #psx_gpu_viewport_end_y_offset ]
1148   subs y_c, y_c, temp
1149   subgt height_minor_b, height_minor_b, y_c
1150   addgt height_minor_b, height_minor_b, #1
1151
1152   ldrh temp, [ psx_gpu, #psx_gpu_viewport_start_y_offset ]
1153   subs clip, temp, y_a
1154   ble 0f
1155
1156   sub height_minor_b, height_minor_b, clip
1157   add y_a, y_a, clip
1158   setup_spans_clip(increment, no)
1159
1160  0:
1161   cmp height_minor_b, #0
1162   ble 1f
1163
1164   orr temp, y_a, y_a, lsl #16
1165   add temp, temp, #(1 << 16) 
1166   add y_a, temp, #2
1167   add y_a, y_a, #(2 << 16)
1168   vmov.u32 y_x4, temp, y_a
1169
1170   setup_spans_adjust_edges_alternate_no(left, right)
1171
1172   ldrh temp, [ psx_gpu, #psx_gpu_num_spans_offset ]
1173   add temp, temp, height_minor_b
1174   strh temp, [ psx_gpu, #psx_gpu_num_spans_offset ]
1175
1176  2:                                                     
1177   setup_spans_set_x4_alternate_no(none, down)
1178   subs height_minor_b, height_minor_b, #4
1179   bhi 2b
1180
1181  1:
1182   setup_spans_epilogue()
1183
1184  3:
1185   setup_spans_up_down_load_edge_set_b()
1186   setup_spans_prologue_b()
1187   bal 4b
1188
1189 .pool
1190
1191 #undef span_uvrg_offset
1192 #undef span_edge_data
1193 #undef span_b_offset
1194 #undef left_x
1195 #undef b
1196
1197 #define psx_gpu                                  r0
1198 #define num_spans                                r1
1199 #define span_uvrg_offset                         r2
1200 #define span_edge_data                           r3
1201 #define span_b_offset                            r4
1202 #define b_dx                                     r5
1203 #define span_num_blocks                          r6
1204 #define y                                        r7
1205 #define left_x                                   r8
1206 #define b                                        r9
1207 #define dither_offset_ptr                        r10
1208 #define block_ptr_a                              r11
1209 #define fb_ptr                                   r12
1210 #define num_blocks                               r14
1211
1212 #define uvrg_dx_ptr                              r2
1213 #define texture_mask_ptr                         r3
1214 #define dither_shift                             r8
1215 #define dither_row                               r10
1216
1217 #define c_32                                     r7
1218 #define b_dx4                                    r8
1219 #define b_dx8                                    r9
1220 #define block_ptr_b                              r10
1221
1222 #define block_span_ptr                           r10
1223 #define right_mask                               r8
1224
1225 #define color                                    r2
1226 #define color_r                                  r3
1227 #define color_g                                  r4
1228 #define color_b                                  r5
1229
1230 #undef uvrg
1231
1232 #define u_block                                  q0
1233 #define v_block                                  q1
1234 #define r_block                                  q2
1235 #define g_block                                  q3
1236 #define b_block                                  q4
1237
1238 #define uv_dx4                                   d10
1239 #define rg_dx4                                   d11
1240 #define uv_dx8                                   d12
1241 #define rg_dx8                                   d13
1242 #define b_whole_8                                d14
1243 #define fb_mask_ptrs                             d15
1244
1245 #define uvrg_dx4                                 q5
1246 #define uvrg_dx8                                 q6
1247 #define uv_dx8                                   d12
1248 #define rg_dx8                                   d13
1249
1250 #define u_whole                                  q8
1251 #define v_whole                                  q9
1252 #define r_whole                                  q10
1253 #define g_whole                                  q11
1254 #define b_whole                                  q12
1255
1256 #define u_whole_low                              d16
1257 #define u_whole_high                             d17
1258 #define v_whole_low                              d18
1259 #define v_whole_high                             d19
1260 #define r_whole_low                              d20
1261 #define r_whole_high                             d21
1262 #define g_whole_low                              d22
1263 #define g_whole_high                             d23
1264 #define b_whole_low                              d24
1265 #define b_whole_high                             d25
1266
1267 #define dx4                                      q13
1268 #define dx8                                      q13
1269
1270 #define u_whole_8                                d26
1271 #define v_whole_8                                d27
1272 #define u_whole_8b                               d24
1273 #define r_whole_8                                d24
1274 #define g_whole_8                                d25
1275
1276 #define uv_whole_8                               q13
1277 #define uv_whole_8b                              q14
1278
1279 #define dither_offsets                           q14
1280 #define texture_mask                             q15
1281 #define texture_mask_u                           d30
1282 #define texture_mask_v                           d31
1283
1284 #define dither_offsets_short                     d28
1285
1286 #define v_left_x                                 q8
1287 #define uvrg                                     q9
1288 #define block_span                               q10
1289
1290 #define uv                                       d18
1291 #define rg                                       d19
1292
1293 #define draw_mask                                q1
1294 #define draw_mask_edge                           q13
1295 #define test_mask                                q0
1296
1297 #define uvrg_dx                                  q3
1298
1299 #define colors                                   q2
1300
1301 #define setup_blocks_texture_swizzled()                                        \
1302   vand.u8 u_whole_8b, u_whole_8, texture_mask_u;                               \
1303   vsli.u8 u_whole_8, v_whole_8, #4;                                            \
1304   vsri.u8 v_whole_8, u_whole_8b, #4                                            \
1305
1306 #define setup_blocks_texture_unswizzled()                                      \
1307
1308
1309 #define setup_blocks_shaded_textured_builder(swizzling)                        \
1310 .align 3;                                                                      \
1311                                                                                \
1312 function(setup_blocks_shaded_textured_dithered_##swizzling##_indirect)         \
1313   ldrh num_spans, [ psx_gpu, #psx_gpu_num_spans_offset ];                      \
1314   add uvrg_dx_ptr, psx_gpu, #psx_gpu_uvrg_dx_offset;                           \
1315                                                                                \
1316   vld1.u32 { uvrg_dx }, [ uvrg_dx_ptr, :128 ];                                 \
1317   add texture_mask_ptr, psx_gpu, #psx_gpu_texture_mask_width_offset;           \
1318                                                                                \
1319   cmp num_spans, #0;                                                           \
1320   bxeq lr;                                                                     \
1321                                                                                \
1322   stmdb sp!, { r4 - r11, r14 };                                                \
1323   vshl.u32 uvrg_dx4, uvrg_dx, #2;                                              \
1324                                                                                \
1325   ldr b_dx, [ psx_gpu, #psx_gpu_b_dx_offset ];                                 \
1326   vshl.u32 uvrg_dx8, uvrg_dx, #3;                                              \
1327                                                                                \
1328   vld2.u8 { texture_mask_u[], texture_mask_v[] }, [ texture_mask_ptr, :16 ];   \
1329   add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
1330                                                                                \
1331   ldrh num_blocks, [ psx_gpu, #psx_gpu_num_blocks_offset ];                    \
1332   add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
1333                                                                                \
1334   add span_b_offset, psx_gpu, #psx_gpu_span_b_offset_offset;                   \
1335   add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
1336                                                                                \
1337   add block_ptr_a, block_ptr_a, num_blocks, lsl #6;                            \
1338                                                                                \
1339  0:                                                                            \
1340   vmov.u8 fb_mask_ptrs, #0;                                                    \
1341                                                                                \
1342   ldrh span_num_blocks, [ span_edge_data, #edge_data_num_blocks_offset ];      \
1343   add dither_offset_ptr, psx_gpu, #psx_gpu_dither_table_offset;                \
1344                                                                                \
1345   ldrh y, [ span_edge_data, #edge_data_y_offset ];                             \
1346   ldr fb_ptr, [ psx_gpu, #psx_gpu_vram_out_ptr_offset ];                       \
1347                                                                                \
1348   cmp span_num_blocks, #0;                                                     \
1349   beq 1f;                                                                      \
1350                                                                                \
1351   ldrh left_x, [ span_edge_data, #edge_data_left_x_offset ];                   \
1352   add num_blocks, span_num_blocks, num_blocks;                                 \
1353                                                                                \
1354   cmp num_blocks, #MAX_BLOCKS;                                                 \
1355   bgt 2f;                                                                      \
1356                                                                                \
1357  3:                                                                            \
1358   ldr b, [ span_b_offset ];                                                    \
1359   add fb_ptr, fb_ptr, y, lsl #11;                                              \
1360                                                                                \
1361   vdup.u32 v_left_x, left_x;                                                   \
1362   and y, y, #0x3;                                                              \
1363                                                                                \
1364   ldr dither_row, [ dither_offset_ptr, y, lsl #2 ];                            \
1365   add fb_ptr, fb_ptr, left_x, lsl #1;                                          \
1366                                                                                \
1367   mla b, b_dx, left_x, b;                                                      \
1368   and dither_shift, left_x, #0x03;                                             \
1369                                                                                \
1370   vld1.u32 { uvrg }, [ span_uvrg_offset, :128 ];                               \
1371   vshr.u32 uvrg_dx, uvrg_dx4, #2;                                              \
1372                                                                                \
1373   mov dither_shift, dither_shift, lsl #3;                                      \
1374   vmla.u32 uvrg, uvrg_dx, v_left_x;                                            \
1375                                                                                \
1376   mov c_32, #32;                                                               \
1377   subs span_num_blocks, span_num_blocks, #1;                                   \
1378                                                                                \
1379   mov dither_row, dither_row, ror dither_shift;                                \
1380   mov b_dx4, b_dx, lsl #2;                                                     \
1381                                                                                \
1382   vdup.u32 dither_offsets_short, dither_row;                                   \
1383   add block_span_ptr, psx_gpu, #psx_gpu_u_block_span_offset;                   \
1384                                                                                \
1385   vdup.u32 b_block, b;                                                         \
1386   vshll.s8 dither_offsets, dither_offsets_short, #4;                           \
1387                                                                                \
1388   vdup.u32 u_block, uv[0];                                                     \
1389   mov b_dx8, b_dx, lsl #3;                                                     \
1390                                                                                \
1391   vdup.u32 v_block, uv[1];                                                     \
1392   vdup.u32 r_block, rg[0];                                                     \
1393   vdup.u32 g_block, rg[1];                                                     \
1394                                                                                \
1395   vld1.u32 { block_span }, [ block_span_ptr, :128 ]!;                          \
1396                                                                                \
1397   vadd.u32 u_block, u_block, block_span;                                       \
1398   vld1.u32 { block_span }, [ block_span_ptr, :128 ]!;                          \
1399                                                                                \
1400   vadd.u32 v_block, v_block, block_span;                                       \
1401   vld1.u32 { block_span }, [ block_span_ptr, :128 ]!;                          \
1402                                                                                \
1403   vadd.u32 r_block, r_block, block_span;                                       \
1404   vld1.u32 { block_span }, [ block_span_ptr, :128 ]!;                          \
1405                                                                                \
1406   vadd.u32 g_block, g_block, block_span;                                       \
1407   vld1.u32 { block_span }, [ block_span_ptr, :128 ];                           \
1408                                                                                \
1409   vadd.u32 b_block, b_block, block_span;                                       \
1410   add block_ptr_b, block_ptr_a, #16;                                           \
1411                                                                                \
1412   vshrn.u32 u_whole_low, u_block, #16;                                         \
1413   vshrn.u32 v_whole_low, v_block, #16;                                         \
1414   vshrn.u32 r_whole_low, r_block, #16;                                         \
1415   vshrn.u32 g_whole_low, g_block, #16;                                         \
1416                                                                                \
1417   vdup.u32 dx4, uv_dx4[0];                                                     \
1418   vshrn.u32 b_whole_low, b_block, #16;                                         \
1419                                                                                \
1420   vaddhn.u32 u_whole_high, u_block, dx4;                                       \
1421   vdup.u32 dx4, uv_dx4[1];                                                     \
1422                                                                                \
1423   vaddhn.u32 v_whole_high, v_block, dx4;                                       \
1424   vdup.u32 dx4, rg_dx4[0];                                                     \
1425                                                                                \
1426   vaddhn.u32 r_whole_high, r_block, dx4;                                       \
1427   vdup.u32 dx4, rg_dx4[1];                                                     \
1428                                                                                \
1429   vaddhn.u32 g_whole_high, g_block, dx4;                                       \
1430   vdup.u32 dx4, b_dx4;                                                         \
1431                                                                                \
1432   vaddhn.u32 b_whole_high, b_block, dx4;                                       \
1433   vdup.u32 dx8, uv_dx8[0];                                                     \
1434                                                                                \
1435   vadd.u32 u_block, u_block, dx8;                                              \
1436   vdup.u32 dx8, uv_dx8[1];                                                     \
1437                                                                                \
1438   vadd.u32 v_block, v_block, dx8;                                              \
1439   vdup.u32 dx8, rg_dx8[0];                                                     \
1440                                                                                \
1441   vadd.u32 r_block, r_block, dx8;                                              \
1442   vdup.u32 dx8, rg_dx8[1];                                                     \
1443                                                                                \
1444   vadd.u32 g_block, g_block, dx8;                                              \
1445   vdup.u32 dx8, b_dx8;                                                         \
1446                                                                                \
1447   vadd.u32 b_block, b_block, dx8;                                              \
1448   vmovn.u16 u_whole_8, u_whole;                                                \
1449                                                                                \
1450   vmovn.u16 v_whole_8, v_whole;                                                \
1451                                                                                \
1452   vmovn.u16 b_whole_8, b_whole;                                                \
1453   pld [ fb_ptr ];                                                              \
1454   vmov.u32 fb_mask_ptrs[1], fb_ptr;                                            \
1455                                                                                \
1456   vand.u8 uv_whole_8, uv_whole_8, texture_mask;                                \
1457   setup_blocks_texture_##swizzling();                                          \
1458                                                                                \
1459   vmovn.u16 r_whole_8, r_whole;                                                \
1460   beq 5f;                                                                      \
1461                                                                                \
1462  4:                                                                            \
1463   vmovn.u16 g_whole_8, g_whole;                                                \
1464   vshrn.u32 u_whole_low, u_block, #16;                                         \
1465                                                                                \
1466   vst2.u8 { u_whole_8, v_whole_8 }, [ block_ptr_a, :128 ], c_32;               \
1467   vshrn.u32 v_whole_low, v_block, #16;                                         \
1468                                                                                \
1469   vst1.u32 { r_whole_8, g_whole_8 }, [ block_ptr_b, :128 ], c_32;              \
1470   vshrn.u32 r_whole_low, r_block, #16;                                         \
1471                                                                                \
1472   vst1.u32 { b_whole_8, fb_mask_ptrs }, [ block_ptr_a, :128 ], c_32;           \
1473   vshrn.u32 g_whole_low, g_block, #16;                                         \
1474                                                                                \
1475   vdup.u32 dx4, uv_dx4[0];                                                     \
1476   vshrn.u32 b_whole_low, b_block, #16;                                         \
1477                                                                                \
1478   vaddhn.u32 u_whole_high, u_block, dx4;                                       \
1479   vdup.u32 dx4, uv_dx4[1];                                                     \
1480                                                                                \
1481   vaddhn.u32 v_whole_high, v_block, dx4;                                       \
1482   vdup.u32 dx4, rg_dx4[0];                                                     \
1483                                                                                \
1484   vaddhn.u32 r_whole_high, r_block, dx4;                                       \
1485   vdup.u32 dx4, rg_dx4[1];                                                     \
1486                                                                                \
1487   vaddhn.u32 g_whole_high, g_block, dx4;                                       \
1488   vdup.u32 dx4, b_dx4;                                                         \
1489                                                                                \
1490   vaddhn.u32 b_whole_high, b_block, dx4;                                       \
1491   vdup.u32 dx8, uv_dx8[0];                                                     \
1492                                                                                \
1493   vadd.u32 u_block, u_block, dx8;                                              \
1494   vdup.u32 dx8, uv_dx8[1];                                                     \
1495                                                                                \
1496   vadd.u32 v_block, v_block, dx8;                                              \
1497   vdup.u32 dx8, rg_dx8[0];                                                     \
1498                                                                                \
1499   vadd.u32 r_block, r_block, dx8;                                              \
1500   vdup.u32 dx8, rg_dx8[1];                                                     \
1501                                                                                \
1502   vadd.u32 g_block, g_block, dx8;                                              \
1503   vdup.u32 dx8, b_dx8;                                                         \
1504                                                                                \
1505   vadd.u32 b_block, b_block, dx8;                                              \
1506   vmovn.u16 u_whole_8, u_whole;                                                \
1507                                                                                \
1508   add fb_ptr, fb_ptr, #16;                                                     \
1509   vmovn.u16 v_whole_8, v_whole;                                                \
1510                                                                                \
1511   vst1.u32 { dither_offsets }, [ block_ptr_b, :128 ], c_32;                    \
1512   vmovn.u16 b_whole_8, b_whole;                                                \
1513                                                                                \
1514   pld [ fb_ptr ];                                                              \
1515                                                                                \
1516   vmov.u32 fb_mask_ptrs[1], fb_ptr;                                            \
1517   subs span_num_blocks, span_num_blocks, #1;                                   \
1518                                                                                \
1519   vand.u8 uv_whole_8, uv_whole_8, texture_mask;                                \
1520   setup_blocks_texture_##swizzling();                                          \
1521                                                                                \
1522   vmovn.u16 r_whole_8, r_whole;                                                \
1523   bne 4b;                                                                      \
1524                                                                                \
1525  5:                                                                            \
1526   vmovn.u16 g_whole_8, g_whole;                                                \
1527   ldrh right_mask, [ span_edge_data, #edge_data_right_mask_offset ];           \
1528                                                                                \
1529   vld1.u32 { test_mask }, [ psx_gpu, :128 ];                                   \
1530   vdup.u8 draw_mask, right_mask;                                               \
1531                                                                                \
1532   vmov.u32 fb_mask_ptrs[0], right_mask;                                        \
1533   vtst.u16 draw_mask, draw_mask, test_mask;                                    \
1534   vzip.u8 u_whole_8, v_whole_8;                                                \
1535                                                                                \
1536   vbic.u16 uv_whole_8, uv_whole_8, draw_mask;                                  \
1537   vst1.u32 { r_whole_8, g_whole_8 }, [ block_ptr_b, :128 ], c_32;              \
1538   vst1.u32 { uv_whole_8 }, [ block_ptr_a, :128 ], c_32;                        \
1539   vst1.u32 { dither_offsets }, [ block_ptr_b, :128 ], c_32;                    \
1540   vst1.u32 { b_whole_8, fb_mask_ptrs }, [ block_ptr_a, :128 ], c_32;           \
1541                                                                                \
1542  1:                                                                            \
1543   add span_uvrg_offset, span_uvrg_offset, #16;                                 \
1544   add span_b_offset, span_b_offset, #4;                                        \
1545                                                                                \
1546   add span_edge_data, span_edge_data, #8;                                      \
1547   subs num_spans, num_spans, #1;                                               \
1548                                                                                \
1549   strh num_blocks, [ psx_gpu, #psx_gpu_num_blocks_offset ];                    \
1550   bne 0b;                                                                      \
1551                                                                                \
1552   ldmia sp!, { r4 - r11, pc };                                                 \
1553                                                                                \
1554  2:                                                                            \
1555   /* TODO: Load from psx_gpu instead of saving/restoring these               */\
1556   vpush { texture_mask };                                                      \
1557   vpush { uvrg_dx4 };                                                          \
1558                                                                                \
1559   stmdb sp!, { r0 - r3, r12, r14 };                                            \
1560   bl flush_render_block_buffer;                                                \
1561   ldmia sp!, { r0 - r3, r12, r14 };                                            \
1562                                                                                \
1563   vpop { uvrg_dx4 };                                                           \
1564   vpop { texture_mask };                                                       \
1565                                                                                \
1566   vadd.u32 uvrg_dx8, uvrg_dx4, uvrg_dx4;                                       \
1567   vmov.u8 fb_mask_ptrs, #0;                                                    \
1568                                                                                \
1569   mov num_blocks, span_num_blocks;                                             \
1570   add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
1571   bal 3b                                                                       \
1572
1573
1574 setup_blocks_shaded_textured_builder(swizzled)
1575 setup_blocks_shaded_textured_builder(unswizzled)
1576
1577
1578 #define setup_blocks_unshaded_textured_builder(swizzling)                      \
1579 .align 3;                                                                      \
1580                                                                                \
1581 function(setup_blocks_unshaded_textured_dithered_##swizzling##_indirect)       \
1582   ldrh num_spans, [ psx_gpu, #psx_gpu_num_spans_offset ];                      \
1583   add uvrg_dx_ptr, psx_gpu, #psx_gpu_uvrg_dx_offset;                           \
1584                                                                                \
1585   vld1.u32 { uvrg_dx }, [ uvrg_dx_ptr, :128 ];                                 \
1586   add texture_mask_ptr, psx_gpu, #psx_gpu_texture_mask_width_offset;           \
1587                                                                                \
1588   cmp num_spans, #0;                                                           \
1589   bxeq lr;                                                                     \
1590                                                                                \
1591   stmdb sp!, { r4 - r11, r14 };                                                \
1592   vshl.u32 uvrg_dx4, uvrg_dx, #2;                                              \
1593                                                                                \
1594   vshl.u32 uvrg_dx8, uvrg_dx, #3;                                              \
1595                                                                                \
1596   vld2.u8 { texture_mask_u[], texture_mask_v[] }, [ texture_mask_ptr, :16 ];   \
1597   add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
1598                                                                                \
1599   ldrh num_blocks, [ psx_gpu, #psx_gpu_num_blocks_offset ];                    \
1600   add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
1601                                                                                \
1602   add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
1603                                                                                \
1604   add block_ptr_a, block_ptr_a, num_blocks, lsl #6;                            \
1605                                                                                \
1606  0:                                                                            \
1607   vmov.u8 fb_mask_ptrs, #0;                                                    \
1608                                                                                \
1609   ldrh span_num_blocks, [ span_edge_data, #edge_data_num_blocks_offset ];      \
1610   add dither_offset_ptr, psx_gpu, #psx_gpu_dither_table_offset;                \
1611                                                                                \
1612   ldrh y, [ span_edge_data, #edge_data_y_offset ];                             \
1613   ldr fb_ptr, [ psx_gpu, #psx_gpu_vram_out_ptr_offset ];                       \
1614                                                                                \
1615   cmp span_num_blocks, #0;                                                     \
1616   beq 1f;                                                                      \
1617                                                                                \
1618   ldrh left_x, [ span_edge_data, #edge_data_left_x_offset ];                   \
1619   add num_blocks, span_num_blocks, num_blocks;                                 \
1620                                                                                \
1621   cmp num_blocks, #MAX_BLOCKS;                                                 \
1622   bgt 2f;                                                                      \
1623                                                                                \
1624  3:                                                                            \
1625   add fb_ptr, fb_ptr, y, lsl #11;                                              \
1626                                                                                \
1627   vdup.u32 v_left_x, left_x;                                                   \
1628   and y, y, #0x3;                                                              \
1629                                                                                \
1630   ldr dither_row, [ dither_offset_ptr, y, lsl #2 ];                            \
1631   add fb_ptr, fb_ptr, left_x, lsl #1;                                          \
1632                                                                                \
1633   and dither_shift, left_x, #0x03;                                             \
1634                                                                                \
1635   vld1.u32 { uvrg }, [ span_uvrg_offset, :128 ];                               \
1636   vshr.u32 uvrg_dx, uvrg_dx4, #2;                                              \
1637                                                                                \
1638   mov dither_shift, dither_shift, lsl #3;                                      \
1639   vmla.u32 uvrg, uvrg_dx, v_left_x;                                            \
1640                                                                                \
1641   mov c_32, #32;                                                               \
1642   subs span_num_blocks, span_num_blocks, #1;                                   \
1643                                                                                \
1644   mov dither_row, dither_row, ror dither_shift;                                \
1645                                                                                \
1646   vdup.u32 dither_offsets_short, dither_row;                                   \
1647   add block_span_ptr, psx_gpu, #psx_gpu_u_block_span_offset;                   \
1648                                                                                \
1649   vshll.s8 dither_offsets, dither_offsets_short, #4;                           \
1650                                                                                \
1651   vdup.u32 u_block, uv[0];                                                     \
1652                                                                                \
1653   vdup.u32 v_block, uv[1];                                                     \
1654   vld1.u32 { block_span }, [ block_span_ptr, :128 ]!;                          \
1655                                                                                \
1656   vadd.u32 u_block, u_block, block_span;                                       \
1657   vld1.u32 { block_span }, [ block_span_ptr, :128 ]!;                          \
1658                                                                                \
1659   vadd.u32 v_block, v_block, block_span;                                       \
1660   add block_ptr_b, block_ptr_a, #16;                                           \
1661                                                                                \
1662   vshrn.u32 u_whole_low, u_block, #16;                                         \
1663   vshrn.u32 v_whole_low, v_block, #16;                                         \
1664                                                                                \
1665   vdup.u32 dx4, uv_dx4[0];                                                     \
1666                                                                                \
1667   vaddhn.u32 u_whole_high, u_block, dx4;                                       \
1668   vdup.u32 dx4, uv_dx4[1];                                                     \
1669                                                                                \
1670   vaddhn.u32 v_whole_high, v_block, dx4;                                       \
1671   vdup.u32 dx8, uv_dx8[0];                                                     \
1672                                                                                \
1673   vadd.u32 u_block, u_block, dx8;                                              \
1674   vdup.u32 dx8, uv_dx8[1];                                                     \
1675                                                                                \
1676   vadd.u32 v_block, v_block, dx8;                                              \
1677   vmovn.u16 u_whole_8, u_whole;                                                \
1678                                                                                \
1679   vmovn.u16 v_whole_8, v_whole;                                                \
1680                                                                                \
1681   pld [ fb_ptr ];                                                              \
1682   vmov.u32 fb_mask_ptrs[1], fb_ptr;                                            \
1683                                                                                \
1684   vand.u8 uv_whole_8, uv_whole_8, texture_mask;                                \
1685   setup_blocks_texture_##swizzling();                                          \
1686                                                                                \
1687   beq 5f;                                                                      \
1688                                                                                \
1689  4:                                                                            \
1690   vshrn.u32 u_whole_low, u_block, #16;                                         \
1691                                                                                \
1692   vst2.u8 { u_whole_8, v_whole_8 }, [ block_ptr_a, :128 ], c_32;               \
1693   vshrn.u32 v_whole_low, v_block, #16;                                         \
1694                                                                                \
1695   add block_ptr_b, block_ptr_b, #32;                                           \
1696   vst1.u32 { b_whole_8, fb_mask_ptrs }, [ block_ptr_a, :128 ], c_32;           \
1697                                                                                \
1698   vdup.u32 dx4, uv_dx4[0];                                                     \
1699   vaddhn.u32 u_whole_high, u_block, dx4;                                       \
1700   vdup.u32 dx4, uv_dx4[1];                                                     \
1701                                                                                \
1702   vaddhn.u32 v_whole_high, v_block, dx4;                                       \
1703   vdup.u32 dx8, uv_dx8[0];                                                     \
1704                                                                                \
1705   vadd.u32 u_block, u_block, dx8;                                              \
1706   vdup.u32 dx8, uv_dx8[1];                                                     \
1707                                                                                \
1708   vadd.u32 v_block, v_block, dx8;                                              \
1709   vmovn.u16 u_whole_8, u_whole;                                                \
1710                                                                                \
1711   add fb_ptr, fb_ptr, #16;                                                     \
1712   vmovn.u16 v_whole_8, v_whole;                                                \
1713                                                                                \
1714   vst1.u32 { dither_offsets }, [ block_ptr_b, :128 ], c_32;                    \
1715   pld [ fb_ptr ];                                                              \
1716                                                                                \
1717   vmov.u32 fb_mask_ptrs[1], fb_ptr;                                            \
1718   subs span_num_blocks, span_num_blocks, #1;                                   \
1719                                                                                \
1720   vand.u8 uv_whole_8, uv_whole_8, texture_mask;                                \
1721   setup_blocks_texture_##swizzling();                                          \
1722                                                                                \
1723   bne 4b;                                                                      \
1724                                                                                \
1725  5:                                                                            \
1726   ldrh right_mask, [ span_edge_data, #edge_data_right_mask_offset ];           \
1727                                                                                \
1728   vld1.u32 { test_mask }, [ psx_gpu, :128 ];                                   \
1729   vdup.u8 draw_mask, right_mask;                                               \
1730                                                                                \
1731   vmov.u32 fb_mask_ptrs[0], right_mask;                                        \
1732   vtst.u16 draw_mask, draw_mask, test_mask;                                    \
1733   vzip.u8 u_whole_8, v_whole_8;                                                \
1734                                                                                \
1735   vbic.u16 uv_whole_8, uv_whole_8, draw_mask;                                  \
1736   add block_ptr_b, block_ptr_b, #32;                                           \
1737   vst1.u32 { uv_whole_8 }, [ block_ptr_a, :128 ], c_32;                        \
1738   vst1.u32 { dither_offsets }, [ block_ptr_b, :128 ], c_32;                    \
1739   vst1.u32 { b_whole_8, fb_mask_ptrs }, [ block_ptr_a, :128 ], c_32;           \
1740                                                                                \
1741  1:                                                                            \
1742   add span_uvrg_offset, span_uvrg_offset, #16;                                 \
1743   add span_edge_data, span_edge_data, #8;                                      \
1744   subs num_spans, num_spans, #1;                                               \
1745                                                                                \
1746   strh num_blocks, [ psx_gpu, #psx_gpu_num_blocks_offset ];                    \
1747   bne 0b;                                                                      \
1748                                                                                \
1749   ldmia sp!, { r4 - r11, pc };                                                 \
1750                                                                                \
1751  2:                                                                            \
1752   /* TODO: Load from psx_gpu instead of saving/restoring these               */\
1753   vpush { texture_mask };                                                      \
1754   vpush { uvrg_dx4 };                                                          \
1755                                                                                \
1756   stmdb sp!, { r0 - r3, r12, r14 };                                            \
1757   bl flush_render_block_buffer;                                                \
1758   ldmia sp!, { r0 - r3, r12, r14 };                                            \
1759                                                                                \
1760   vpop { uvrg_dx4 };                                                           \
1761   vpop { texture_mask };                                                       \
1762                                                                                \
1763   vadd.u32 uvrg_dx8, uvrg_dx4, uvrg_dx4;                                       \
1764   vmov.u8 fb_mask_ptrs, #0;                                                    \
1765                                                                                \
1766   mov num_blocks, span_num_blocks;                                             \
1767   add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
1768   bal 3b                                                                       \
1769
1770
1771 setup_blocks_unshaded_textured_builder(swizzled)
1772 setup_blocks_unshaded_textured_builder(unswizzled)
1773
1774
1775 .align 3
1776
1777 function(setup_blocks_unshaded_untextured_undithered_unswizzled_indirect)
1778   ldrh num_spans, [ psx_gpu, #psx_gpu_num_spans_offset ]
1779   veor.u32 draw_mask, draw_mask, draw_mask
1780
1781   cmp num_spans, #0
1782   bxeq lr
1783
1784   stmdb sp!, { r4 - r11, r14 }
1785   vld1.u32 { test_mask }, [ psx_gpu, :128 ]
1786
1787   ldr color, [ psx_gpu, #psx_gpu_triangle_color_offset ]
1788
1789   ubfx color_r, color, #3, #5
1790   ubfx color_g, color, #11, #5
1791   ubfx color_b, color, #19, #5
1792
1793   orr color, color_r, color_b, lsl #10
1794   orr color, color, color_g, lsl #5
1795
1796   vdup.u16 colors, color
1797
1798   ldrh num_blocks, [ psx_gpu, #psx_gpu_num_blocks_offset ]
1799   add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset
1800
1801   add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset
1802   add block_ptr_a, block_ptr_a, num_blocks, lsl #6
1803
1804  0:
1805   ldrh span_num_blocks, [ span_edge_data, #edge_data_num_blocks_offset ]
1806   ldrh y, [ span_edge_data, #edge_data_y_offset ]
1807
1808   ldr fb_ptr, [ psx_gpu, #psx_gpu_vram_out_ptr_offset ]
1809
1810   cmp span_num_blocks, #0
1811   beq 1f
1812
1813   ldrh left_x, [ span_edge_data, #edge_data_left_x_offset ]
1814   add num_blocks, span_num_blocks, num_blocks
1815
1816   cmp num_blocks, #MAX_BLOCKS
1817   bgt 2f
1818
1819  3:
1820   add fb_ptr, fb_ptr, y, lsl #11
1821   and y, y, #0x3
1822
1823   add fb_ptr, fb_ptr, left_x, lsl #1
1824   mov c_32, #32
1825
1826   subs span_num_blocks, span_num_blocks, #1
1827
1828   add block_ptr_b, block_ptr_a, #16
1829   pld [ fb_ptr ]
1830
1831   vmov.u32 fb_mask_ptrs[1], fb_ptr
1832   beq 5f
1833
1834  4:
1835   vst1.u32 { draw_mask }, [ block_ptr_a, :128 ], c_32
1836   vst1.u32 { colors }, [ block_ptr_b, :128 ], c_32
1837   vst1.u32 { b_whole_8, fb_mask_ptrs }, [ block_ptr_a, :128 ], c_32
1838
1839   add fb_ptr, fb_ptr, #16
1840   add block_ptr_b, block_ptr_b, #32
1841
1842   pld [ fb_ptr ]
1843
1844   vmov.u32 fb_mask_ptrs[1], fb_ptr
1845   subs span_num_blocks, span_num_blocks, #1
1846
1847   bne 4b
1848
1849  5:
1850   ldrh right_mask, [ span_edge_data, #edge_data_right_mask_offset ]
1851
1852   vdup.u8 draw_mask_edge, right_mask
1853   vtst.u16 draw_mask_edge, draw_mask_edge, test_mask
1854
1855   vst1.u32 { colors }, [ block_ptr_b, :128 ], c_32
1856   vst1.u32 { draw_mask_edge }, [ block_ptr_a, :128 ], c_32
1857   add block_ptr_b, block_ptr_b, #32
1858   vst1.u32 { b_whole_8, fb_mask_ptrs }, [ block_ptr_a, :128 ], c_32
1859
1860  1:
1861   add span_edge_data, span_edge_data, #8
1862   subs num_spans, num_spans, #1
1863
1864   strh num_blocks, [ psx_gpu, #psx_gpu_num_blocks_offset ]
1865   bne 0b
1866
1867   ldmia sp!, { r4 - r11, pc }
1868                                                                            
1869  2:
1870   vpush { colors }
1871
1872   stmdb sp!, { r0 - r3, r12, r14 }
1873   bl flush_render_block_buffer
1874   ldmia sp!, { r0 - r3, r12, r14 }
1875
1876   vpop { colors }
1877
1878   vld1.u32 { test_mask }, [ psx_gpu, :128 ]
1879   veor.u32 draw_mask, draw_mask, draw_mask
1880
1881   mov num_blocks, span_num_blocks
1882   add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset
1883   bal 3b
1884
1885
1886 #define mask_msb_scalar                                   r14
1887
1888 #define msb_mask                                          q15
1889
1890 #define pixels_low                                        d16
1891
1892 #define msb_mask_low                                      d30
1893 #define msb_mask_high                                     d31
1894
1895
1896 .align 3
1897
1898 function(setup_blocks_unshaded_untextured_undithered_unswizzled_direct)
1899   ldrh num_spans, [ psx_gpu, #psx_gpu_num_spans_offset ]
1900
1901   cmp num_spans, #0
1902   bxeq lr
1903
1904   stmdb sp!, { r4 - r11, r14 }
1905
1906   ldr color, [ psx_gpu, #psx_gpu_triangle_color_offset ]
1907
1908   ubfx color_r, color, #3, #5
1909   ubfx color_g, color, #11, #5
1910
1911   ldrh mask_msb_scalar, [ psx_gpu, #psx_gpu_mask_msb_offset ]
1912   ubfx color_b, color, #19, #5
1913
1914   orr color, color_r, color_b, lsl #10
1915   orr color, color, color_g, lsl #5
1916   orr color, color, mask_msb_scalar
1917
1918   vdup.u16 colors, color
1919
1920   add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset
1921   orr color, color, lsl #16
1922
1923
1924  0:
1925   ldrh span_num_blocks, [ span_edge_data, #edge_data_num_blocks_offset ]
1926   ldrh y, [ span_edge_data, #edge_data_y_offset ]
1927
1928   ldr fb_ptr, [ psx_gpu, #psx_gpu_vram_out_ptr_offset ]
1929
1930   cmp span_num_blocks, #0
1931   beq 1f
1932
1933   ldrh left_x, [ span_edge_data, #edge_data_left_x_offset ]
1934
1935   add fb_ptr, fb_ptr, y, lsl #11
1936   subs span_num_blocks, span_num_blocks, #1
1937
1938   add fb_ptr, fb_ptr, left_x, lsl #1
1939   beq 3f
1940
1941  2:
1942   vst1.u32 { colors }, [ fb_ptr ]!
1943   subs span_num_blocks, span_num_blocks, #1
1944
1945   bne 2b
1946
1947  3:
1948   ldrb right_mask, [ span_edge_data, #edge_data_right_mask_offset ]
1949
1950   cmp right_mask, #0x0
1951   beq 5f
1952
1953   tst right_mask, #0xF
1954   streq color, [ fb_ptr ], #4
1955   moveq right_mask, right_mask, lsr #4
1956   streq color, [ fb_ptr ], #4
1957
1958   tst right_mask, #0x3
1959   streq color, [ fb_ptr ], #4
1960   moveq right_mask, right_mask, lsr #2
1961
1962   tst right_mask, #0x1
1963   streqh color, [ fb_ptr ]
1964
1965  1:
1966   add span_edge_data, span_edge_data, #8
1967   subs num_spans, num_spans, #1
1968   bne 0b
1969
1970   ldmia sp!, { r4 - r11, pc }
1971                                                                            
1972  5:
1973   vst1.u32 { colors }, [ fb_ptr ]
1974   bal 1b
1975
1976
1977 #undef c_64
1978
1979 #define c_64                                              r7
1980 #define rg_dx_ptr                                         r2
1981
1982
1983 #undef r_block
1984 #undef g_block
1985 #undef b_block
1986 #undef r_whole
1987 #undef g_whole
1988 #undef b_whole
1989 #undef r_whole_low
1990 #undef r_whole_high
1991 #undef g_whole_low
1992 #undef g_whole_high
1993 #undef b_whole_low
1994 #undef b_whole_high
1995 #undef r_whole_8
1996 #undef g_whole_8
1997 #undef b_whole_8
1998 #undef dither_offsets
1999 #undef rg_dx4
2000 #undef rg_dx8
2001 #undef dx4
2002 #undef dx8
2003 #undef v_left_x
2004 #undef uvrg
2005 #undef block_span
2006 #undef rg
2007 #undef draw_mask
2008 #undef test_mask
2009
2010 #define r_block                                           q0
2011 #define g_block                                           q1
2012 #define b_block                                           q2
2013
2014 #define r_whole                                           q3
2015 #define g_whole                                           q4
2016 #define b_whole                                           q5
2017
2018 #define r_whole_low                                       d6
2019 #define r_whole_high                                      d7
2020 #define g_whole_low                                       d8
2021 #define g_whole_high                                      d9
2022 #define b_whole_low                                       d10
2023 #define b_whole_high                                      d11
2024
2025 #define gb_whole_8                                        q6
2026
2027 #define g_whole_8                                         d12
2028 #define b_whole_8                                         d13
2029
2030 #define r_whole_8                                         d14
2031
2032 #define pixels                                            q8
2033
2034 #define rg_dx4                                            d18
2035 #define rg_dx8                                            d19
2036
2037 #define dx4                                               q10
2038 #define dx8                                               q10
2039
2040 #define v_left_x                                          d6
2041 #define uvrg                                              q4
2042 #define block_span                                        q5
2043
2044 #define rg                                                d9
2045
2046 #define d64_1                                             d22
2047 #define d64_128                                           d23
2048
2049 #define d128_4                                            q12
2050 #define d128_0x7                                          q13
2051
2052 #define d64_4                                             d24
2053
2054 #define dither_offsets                                    q14
2055 #define draw_mask                                         q15
2056
2057 #define dither_offsets_low                                d28
2058
2059 #define rg_dx                                             d0
2060 #define test_mask                                         q10
2061
2062
2063 #define setup_blocks_shaded_untextured_dither_a_dithered()                     \
2064   vqadd.u8 r_whole_8, r_whole_8, dither_offsets_low;                           \
2065   vqadd.u8 gb_whole_8, gb_whole_8, dither_offsets;                             \
2066
2067 #define setup_blocks_shaded_untextured_dither_b_dithered()                     \
2068   vqsub.u8 r_whole_8, r_whole_8, d64_4;                                        \
2069   vqsub.u8 gb_whole_8, gb_whole_8, d128_4                                      \
2070
2071 #define setup_blocks_shaded_untextured_dither_a_undithered()                   \
2072
2073 #define setup_blocks_shaded_untextured_dither_b_undithered()                   \
2074
2075
2076 #define setup_blocks_shaded_untextured_indirect_builder(dithering)             \
2077 .align 3;                                                                      \
2078                                                                                \
2079 function(setup_blocks_shaded_untextured_##dithering##_unswizzled_indirect)     \
2080   ldrh num_spans, [ psx_gpu, #psx_gpu_num_spans_offset ];                      \
2081   add rg_dx_ptr, psx_gpu, #(psx_gpu_uvrg_dx_offset + 8);                       \
2082                                                                                \
2083   vld1.u32 { rg_dx }, [ rg_dx_ptr, :64 ];                                      \
2084                                                                                \
2085   cmp num_spans, #0;                                                           \
2086   bxeq lr;                                                                     \
2087                                                                                \
2088   stmdb sp!, { r4 - r11, r14 };                                                \
2089   vshl.u32 rg_dx4, rg_dx, #2;                                                  \
2090                                                                                \
2091   ldr b_dx, [ psx_gpu, #psx_gpu_b_dx_offset ];                                 \
2092   vshl.u32 rg_dx8, rg_dx, #3;                                                  \
2093                                                                                \
2094   add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
2095                                                                                \
2096   ldrh num_blocks, [ psx_gpu, #psx_gpu_num_blocks_offset ];                    \
2097   add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
2098                                                                                \
2099   add span_b_offset, psx_gpu, #psx_gpu_span_b_offset_offset;                   \
2100   add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
2101                                                                                \
2102   add block_ptr_a, block_ptr_a, num_blocks, lsl #6;                            \
2103   vmov.u8 d64_1, #1;                                                           \
2104                                                                                \
2105   vmov.u8 d128_4, #4;                                                          \
2106   vmov.u8 d64_128, #128;                                                       \
2107                                                                                \
2108   vmov.u8 d128_0x7, #0x7;                                                      \
2109                                                                                \
2110  0:                                                                            \
2111   ldrh span_num_blocks, [ span_edge_data, #edge_data_num_blocks_offset ];      \
2112   add dither_offset_ptr, psx_gpu, #psx_gpu_dither_table_offset;                \
2113                                                                                \
2114   ldrh y, [ span_edge_data, #edge_data_y_offset ];                             \
2115   ldr fb_ptr, [ psx_gpu, #psx_gpu_vram_out_ptr_offset ];                       \
2116                                                                                \
2117   cmp span_num_blocks, #0;                                                     \
2118   beq 1f;                                                                      \
2119                                                                                \
2120   ldrh left_x, [ span_edge_data, #edge_data_left_x_offset ];                   \
2121   add num_blocks, span_num_blocks, num_blocks;                                 \
2122                                                                                \
2123   cmp num_blocks, #MAX_BLOCKS;                                                 \
2124   bgt 2f;                                                                      \
2125                                                                                \
2126  3:                                                                            \
2127   ldr b, [ span_b_offset ];                                                    \
2128   add fb_ptr, fb_ptr, y, lsl #11;                                              \
2129                                                                                \
2130   vdup.u32 v_left_x, left_x;                                                   \
2131   and y, y, #0x3;                                                              \
2132                                                                                \
2133   ldr dither_row, [ dither_offset_ptr, y, lsl #2 ];                            \
2134   add fb_ptr, fb_ptr, left_x, lsl #1;                                          \
2135                                                                                \
2136   mla b, b_dx, left_x, b;                                                      \
2137   and dither_shift, left_x, #0x03;                                             \
2138                                                                                \
2139   vld1.u32 { uvrg }, [ span_uvrg_offset, :128 ];                               \
2140   vshr.u32 rg_dx, rg_dx4, #2;                                                  \
2141                                                                                \
2142   mov dither_shift, dither_shift, lsl #3;                                      \
2143   vmla.u32 rg, rg_dx, v_left_x;                                                \
2144                                                                                \
2145   mov c_64, #64;                                                               \
2146   subs span_num_blocks, span_num_blocks, #1;                                   \
2147                                                                                \
2148   mov dither_row, dither_row, ror dither_shift;                                \
2149   mov b_dx4, b_dx, lsl #2;                                                     \
2150                                                                                \
2151   vdup.u32 dither_offsets, dither_row;                                         \
2152   add block_span_ptr, psx_gpu, #psx_gpu_r_block_span_offset;                   \
2153                                                                                \
2154   vdup.u32 b_block, b;                                                         \
2155   vadd.u8 dither_offsets, dither_offsets, d128_4;                              \
2156                                                                                \
2157   mov b_dx8, b_dx, lsl #3;                                                     \
2158   vdup.u32 r_block, rg[0];                                                     \
2159   vdup.u32 g_block, rg[1];                                                     \
2160                                                                                \
2161   vld1.u32 { block_span }, [ block_span_ptr, :128 ]!;                          \
2162                                                                                \
2163   vadd.u32 r_block, r_block, block_span;                                       \
2164   vld1.u32 { block_span }, [ block_span_ptr, :128 ]!;                          \
2165                                                                                \
2166   vadd.u32 g_block, g_block, block_span;                                       \
2167   vld1.u32 { block_span }, [ block_span_ptr, :128 ];                           \
2168                                                                                \
2169   vadd.u32 b_block, b_block, block_span;                                       \
2170   add block_ptr_b, block_ptr_a, #16;                                           \
2171                                                                                \
2172   vshrn.u32 r_whole_low, r_block, #16;                                         \
2173   vshrn.u32 g_whole_low, g_block, #16;                                         \
2174   vshrn.u32 b_whole_low, b_block, #16;                                         \
2175   vdup.u32 dx4, rg_dx4[0];                                                     \
2176                                                                                \
2177   vaddhn.u32 r_whole_high, r_block, dx4;                                       \
2178   vdup.u32 dx4, rg_dx4[1];                                                     \
2179                                                                                \
2180   vaddhn.u32 g_whole_high, g_block, dx4;                                       \
2181   vdup.u32 dx4, b_dx4;                                                         \
2182                                                                                \
2183   vaddhn.u32 b_whole_high, b_block, dx4;                                       \
2184   vdup.u32 dx8, rg_dx8[0];                                                     \
2185                                                                                \
2186   vadd.u32 r_block, r_block, dx8;                                              \
2187   vdup.u32 dx8, rg_dx8[1];                                                     \
2188                                                                                \
2189   vadd.u32 g_block, g_block, dx8;                                              \
2190   vdup.u32 dx8, b_dx8;                                                         \
2191                                                                                \
2192   vadd.u32 b_block, b_block, dx8;                                              \
2193                                                                                \
2194   vmovn.u16 r_whole_8, r_whole;                                                \
2195   vmovn.u16 g_whole_8, g_whole;                                                \
2196   vmovn.u16 b_whole_8, b_whole;                                                \
2197                                                                                \
2198   beq 5f;                                                                      \
2199   veor.u32 draw_mask, draw_mask, draw_mask;                                    \
2200                                                                                \
2201  4:                                                                            \
2202   setup_blocks_shaded_untextured_dither_a_##dithering();                       \
2203   vshrn.u32 r_whole_low, r_block, #16;                                         \
2204                                                                                \
2205   setup_blocks_shaded_untextured_dither_b_##dithering();                       \
2206   vshrn.u32 g_whole_low, g_block, #16;                                         \
2207                                                                                \
2208   vshrn.u32 b_whole_low, b_block, #16;                                         \
2209   str fb_ptr, [ block_ptr_a, #44 ];                                            \
2210                                                                                \
2211   vdup.u32 dx4, rg_dx4[0];                                                     \
2212   vshr.u8 r_whole_8, r_whole_8, #3;                                            \
2213   vbic.u8 gb_whole_8, gb_whole_8, d128_0x7;                                    \
2214                                                                                \
2215   vaddhn.u32 r_whole_high, r_block, dx4;                                       \
2216   vdup.u32 dx4, rg_dx4[1];                                                     \
2217                                                                                \
2218   vaddhn.u32 g_whole_high, g_block, dx4;                                       \
2219   vdup.u32 dx4, b_dx4;                                                         \
2220                                                                                \
2221   vaddhn.u32 b_whole_high, b_block, dx4;                                       \
2222   vdup.u32 dx8, rg_dx8[0];                                                     \
2223                                                                                \
2224   vmull.u8 pixels, r_whole_8, d64_1;                                           \
2225   vmlal.u8 pixels, g_whole_8, d64_4;                                           \
2226   vmlal.u8 pixels, b_whole_8, d64_128;                                         \
2227                                                                                \