psx_gpu: fix lines
[pcsx_rearmed.git] / plugins / gpu_neon / psx_gpu / psx_gpu.c
1 /*
2  * Copyright (C) 2011 Gilead Kutnick "Exophase" <exophase@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "common.h"
20
21 u32 span_pixels = 0;
22 u32 span_pixel_blocks = 0;
23 u32 spans = 0;
24 u32 triangles = 0;
25 u32 sprites = 0;
26 u32 sprites_4bpp = 0;
27 u32 sprites_8bpp = 0;
28 u32 sprites_16bpp = 0;
29 u32 sprite_blocks = 0;
30 u32 sprites_untextured = 0;
31 u32 lines = 0;
32 u32 trivial_rejects = 0;
33 u32 texels_4bpp = 0;
34 u32 texels_8bpp = 0;
35 u32 texels_16bpp = 0;
36 u32 texel_blocks_4bpp = 0;
37 u32 texel_blocks_8bpp = 0;
38 u32 texel_blocks_16bpp = 0;
39 u32 texel_blocks_untextured = 0;
40 u32 blend_blocks = 0;
41 u32 render_buffer_flushes = 0;
42 u32 state_changes = 0;
43 u32 left_split_triangles = 0;
44 u32 flat_triangles = 0;
45 u32 clipped_triangles = 0;
46 u32 zero_block_spans = 0;
47 u32 texture_cache_loads = 0;
48 u32 false_modulated_blocks = 0;
49
50 u32 reciprocal_table[512];
51
52
53 typedef s32 fixed_type;
54
55 #define EDGE_STEP_BITS 32
56 #define FIXED_BITS     12
57
58 #define fixed_center(value)                                                    \
59   ((((fixed_type)(value)) << FIXED_BITS) + (1 << (FIXED_BITS - 1)))            \
60
61 #define int_to_fixed(value)                                                    \
62   (((fixed_type)(value)) << FIXED_BITS)                                        \
63
64 #define fixed_to_int(value)                                                    \
65   ((value) >> FIXED_BITS)                                                      \
66
67 #define fixed_to_double(value)                                                 \
68   ((value) / (double)(1 << FIXED_BITS))                                        \
69
70 #define double_to_fixed(value)                                                 \
71   (fixed_type)(((value) * (double)(1 << FIXED_BITS)))                          \
72
73 typedef void (setup_blocks_function_type)(psx_gpu_struct *psx_gpu);
74 typedef void (texture_blocks_function_type)(psx_gpu_struct *psx_gpu);
75 typedef void (shade_blocks_function_type)(psx_gpu_struct *psx_gpu);
76 typedef void (blend_blocks_function_type)(psx_gpu_struct *psx_gpu);
77
78 typedef void (setup_sprite_function_type)(psx_gpu_struct *psx_gpu, s32 x,
79  s32 y, s32 u, s32 v, s32 width, s32 height, u32 color);
80
81 struct render_block_handler_struct
82 {
83   void *setup_blocks;
84   texture_blocks_function_type *texture_blocks;
85   shade_blocks_function_type *shade_blocks;
86   blend_blocks_function_type *blend_blocks; 
87 };
88
89 #ifndef NEON_BUILD
90
91 u32 fixed_reciprocal(u32 denominator, u32 *_shift)
92 {
93   u32 shift = __builtin_clz(denominator);
94   u32 denominator_normalized = denominator << shift;
95
96   double numerator = (1ULL << 62) + denominator_normalized;
97   double numerator_b;
98
99   double denominator_normalized_dp_b;
100   u64 denominator_normalized_dp_u64;
101
102   u32 reciprocal;
103   double reciprocal_dp;
104
105   u64 numerator_u64 = (denominator_normalized >> 10) |
106    ((u64)(62 + 1023) << 52);
107   *((u64 *)(&numerator_b)) = numerator_u64;
108
109   denominator_normalized_dp_u64 =
110    (u64)(denominator_normalized << 21) |
111    ((u64)((denominator_normalized >> 11) + ((1022 + 31) << 20)) << 32);
112   *((u64 *)(&denominator_normalized_dp_b)) = denominator_normalized_dp_u64;
113
114   // Implement with a DP divide
115   reciprocal_dp = numerator / denominator_normalized_dp_b;
116   reciprocal = reciprocal_dp;
117
118   if(reciprocal == 0x80000001)
119     reciprocal = 0x80000000;
120
121   *_shift = 62 - shift;
122   return reciprocal;
123 }
124
125 double reciprocal_estimate(double a)
126 {
127   int q, s;
128   double r;
129
130   q = (int)(a * 512.0);
131   /* a in units of 1/512 rounded down */
132   r = 1.0 / (((double)q + 0.5) / 512.0); /* reciprocal r */
133   s = (int)(256.0 * r + 0.5);
134
135   /* r in units of 1/256 rounded to nearest */
136   
137   return (double)s / 256.0;
138 }
139
140 u32 reciprocal_estimate_u32(u32 value)
141 {
142   u64 dp_value_u64;
143   volatile double dp_value;
144   volatile u64 *dp_value_ptr = (volatile u64 *)&dp_value;
145
146   if((value >> 31) == 0)
147     return 0xFFFFFFFF;
148
149   dp_value_u64 = (0x3FEULL << (31 + 21)) | ((u64)(value & 0x7FFFFFFF) << 21);
150
151   *dp_value_ptr = dp_value_u64;
152
153   dp_value = reciprocal_estimate(dp_value);
154   dp_value_u64 = *dp_value_ptr;
155
156   return (0x80000000 | ((dp_value_u64 >> 21) & 0x7FFFFFFF));
157 }
158
159 u32 fixed_reciprocal_nr(u32 value, u32 *_shift)
160 {
161   u32 shift = __builtin_clz(value);
162   u32 value_normalized = value << shift;
163
164   *_shift = 62 - shift;
165
166   value_normalized -= 2;
167
168   u32 reciprocal_normalized = reciprocal_estimate_u32(value_normalized) >> 1;
169
170   u32 temp = -(((u64)value_normalized * (u32)reciprocal_normalized) >> 31);
171   reciprocal_normalized = (((u64)reciprocal_normalized * temp) >> 31);
172   temp = -(((u64)value_normalized * (u32)reciprocal_normalized) >> 31);
173   reciprocal_normalized = (((u64)reciprocal_normalized * temp) >> 31);
174   temp = -(((u64)value_normalized * (u32)reciprocal_normalized) >> 31);
175   reciprocal_normalized = (((u64)reciprocal_normalized * temp) >> 31);
176
177   return reciprocal_normalized;
178 }
179
180 #endif
181
182
183 s32 triangle_signed_area_x2(s32 x0, s32 y0, s32 x1, s32 y1, s32 x2, s32 y2)
184 {
185         return ((x1 - x0) * (y2 - y1)) - ((x2 - x1) * (y1 - y0));
186 }
187
188 u32 texture_region_mask(s32 x1, s32 y1, s32 x2, s32 y2)
189 {
190   s32 coverage_x, coverage_y;
191
192   u32 mask_up_left;
193   u32 mask_down_right;
194
195   coverage_x = x2 >> 6;
196   coverage_y = y2 >> 8;
197
198   if(coverage_x < 0)
199     coverage_x = 0;
200
201   if(coverage_x > 31)
202     coverage_x = 31;
203
204   mask_down_right = ~(0xFFFFFFFF << (coverage_x + 1)) & 0xFFFF;
205
206   if(coverage_y >= 1)
207     mask_down_right |= mask_down_right << 16;
208
209   coverage_x = x1 >> 6;
210
211   mask_up_left = 0xFFFF0000 << coverage_x;
212   if(coverage_x < 0)
213     mask_up_left = 0xFFFF0000;
214
215   coverage_y = y1 >> 8;
216   if(coverage_y <= 0)
217     mask_up_left |= mask_up_left >> 16;
218
219   return mask_up_left & mask_down_right;
220 }
221
222 u32 invalidate_texture_cache_region(psx_gpu_struct *psx_gpu, u32 x1, u32 y1,
223  u32 x2, u32 y2)
224 {
225   u32 mask = texture_region_mask(x1, y1, x2, y2);
226
227   psx_gpu->dirty_textures_4bpp_mask |= mask;
228   psx_gpu->dirty_textures_8bpp_mask |= mask;
229   psx_gpu->dirty_textures_8bpp_alternate_mask |= mask;
230
231   return mask;
232 }
233
234 u32 invalidate_texture_cache_region_viewport(psx_gpu_struct *psx_gpu, u32 x1,
235  u32 y1, u32 x2, u32 y2)
236 {
237   u32 mask = texture_region_mask(x1, y1, x2, y2) &
238    psx_gpu->viewport_mask;
239
240   psx_gpu->dirty_textures_4bpp_mask |= mask;
241   psx_gpu->dirty_textures_8bpp_mask |= mask;
242   psx_gpu->dirty_textures_8bpp_alternate_mask |= mask;
243
244   return mask;
245 }
246
247
248 void update_texture_8bpp_cache_slice(psx_gpu_struct *psx_gpu,
249  u32 texture_page);
250
251 #ifndef NEON_BUILD
252
253 void update_texture_4bpp_cache(psx_gpu_struct *psx_gpu)
254 {
255   u32 current_texture_page = psx_gpu->current_texture_page;
256   u8 *texture_page_ptr = psx_gpu->texture_page_base;
257   u16 *vram_ptr = psx_gpu->vram_ptr;
258
259   u32 texel_block;
260   u32 tile_x, tile_y;
261   u32 sub_x, sub_y;
262
263   vram_ptr += (current_texture_page >> 4) * 256 * 1024;
264   vram_ptr += (current_texture_page & 0xF) * 64;
265
266   texture_cache_loads++;
267
268   tile_y = 16;
269   tile_x = 16;
270   sub_x = 4;
271   sub_y = 16;
272
273   psx_gpu->dirty_textures_4bpp_mask &= ~(psx_gpu->current_texture_mask);
274
275   while(tile_y)
276   {
277     while(tile_x)
278     {
279       while(sub_y)
280       {
281         while(sub_x)
282         {
283           texel_block = *vram_ptr;
284
285           texture_page_ptr[0] = texel_block & 0xF;
286           texture_page_ptr[1] = (texel_block >> 4) & 0xF;
287           texture_page_ptr[2] = (texel_block >> 8) & 0xF;
288           texture_page_ptr[3] = texel_block >> 12;
289           
290           vram_ptr++;
291           texture_page_ptr += 4;
292
293           sub_x--;          
294         }
295
296         vram_ptr -= 4;
297         sub_x = 4;
298
299         sub_y--;
300         vram_ptr += 1024;
301       }
302
303       sub_y = 16;
304
305       vram_ptr -= (1024 * 16) - 4;
306       tile_x--;
307     }
308
309     tile_x = 16;
310
311     vram_ptr += (16 * 1024) - (4 * 16);
312     tile_y--;
313   }
314 }
315
316 void update_texture_8bpp_cache_slice(psx_gpu_struct *psx_gpu,
317  u32 texture_page)
318 {
319   u16 *texture_page_ptr = psx_gpu->texture_page_base;
320   u16 *vram_ptr = psx_gpu->vram_ptr;
321
322   u32 tile_x, tile_y;
323   u32 sub_y;
324
325   vec_8x16u texels;
326
327   texture_cache_loads++;
328
329   vram_ptr += (texture_page >> 4) * 256 * 1024;
330   vram_ptr += (texture_page & 0xF) * 64;
331
332   if((texture_page ^ psx_gpu->current_texture_page) & 0x1)
333     texture_page_ptr += (8 * 16) * 8;
334
335   tile_x = 8;
336   tile_y = 16;
337
338   sub_y = 16;
339
340   while(tile_y)
341   {
342     while(tile_x)
343     {
344       while(sub_y)
345       {
346         load_128b(texels, vram_ptr);
347         store_128b(texels, texture_page_ptr);
348
349         texture_page_ptr += 8;
350         vram_ptr += 1024;
351
352         sub_y--;
353       }
354
355       sub_y = 16;
356
357       vram_ptr -= (1024 * 16);
358       vram_ptr += 8;
359
360       tile_x--;
361     }
362
363     tile_x = 8;
364
365     vram_ptr -= (8 * 8);
366     vram_ptr += (16 * 1024);
367
368     texture_page_ptr += (8 * 16) * 8;
369     tile_y--;
370   }
371 }
372
373 #endif
374
375
376 void update_texture_8bpp_cache(psx_gpu_struct *psx_gpu)
377 {
378   u32 current_texture_page = psx_gpu->current_texture_page;
379   u32 update_textures =
380    psx_gpu->dirty_textures_8bpp_mask & psx_gpu->current_texture_mask;
381
382   psx_gpu->dirty_textures_8bpp_mask &= ~update_textures;
383
384   if(update_textures & (1 << current_texture_page))
385   {
386     update_texture_8bpp_cache_slice(psx_gpu, current_texture_page);
387     update_textures &= ~(1 << current_texture_page);
388   }
389
390   if(update_textures)
391   {
392     u32 adjacent_texture_page = ((current_texture_page + 1) & 0xF) |
393      (current_texture_page & 0x10);
394
395     update_texture_8bpp_cache_slice(psx_gpu, adjacent_texture_page);
396   }
397 }
398
399 void setup_blocks_shaded_untextured_undithered_unswizzled_indirect(
400  psx_gpu_struct *psx_gpu);
401
402 void flush_render_block_buffer(psx_gpu_struct *psx_gpu)
403 {
404   if(psx_gpu->num_blocks)
405   {
406     render_block_handler_struct *render_block_handler =
407      psx_gpu->render_block_handler;
408
409     render_block_handler->texture_blocks(psx_gpu);
410     render_block_handler->shade_blocks(psx_gpu);
411     render_block_handler->blend_blocks(psx_gpu);
412
413 #ifdef PROFILE
414     span_pixel_blocks += psx_gpu->num_blocks;
415     render_buffer_flushes++;
416 #endif
417
418     psx_gpu->num_blocks = 0;
419   }
420 }
421
422
423 void compute_all_gradients(psx_gpu_struct *psx_gpu, vertex_struct *a,
424  vertex_struct *b, vertex_struct *c);
425
426 #ifndef NEON_BUILD
427
428 #define setup_gradient_calculation_input(set, vertex)                          \
429   /* First type is:  uvrg bxxx xxxx                                          */\
430   /* Second type is: yyyy ybyy uvrg                                          */\
431   /* Since x_a and y_c are the same the same variable is used for both.      */\
432   x##set##_a_y##set##_c.e[0] = vertex->u;                                      \
433   x##set##_a_y##set##_c.e[1] = vertex->v;                                      \
434   x##set##_a_y##set##_c.e[2] = vertex->r;                                      \
435   x##set##_a_y##set##_c.e[3] = vertex->g;                                      \
436   dup_4x16b(x##set##_b, vertex->x);                                            \
437   dup_4x16b(x##set##_c, vertex->x);                                            \
438   dup_4x16b(y##set##_a, vertex->y);                                            \
439   dup_4x16b(y##set##_b, vertex->y);                                            \
440   x##set##_b.e[0] = vertex->b;                                                 \
441   y##set##_b.e[1] = vertex->b                                                  \
442   
443
444 void compute_all_gradients(psx_gpu_struct *psx_gpu, vertex_struct *a,
445  vertex_struct *b, vertex_struct *c)
446 {
447   u32 triangle_area = psx_gpu->triangle_area;
448   u32 winding_mask_scalar;
449
450   u32 triangle_area_shift;
451   u64 triangle_area_reciprocal =
452    fixed_reciprocal(triangle_area, &triangle_area_shift);
453   triangle_area_shift = -(triangle_area_shift - FIXED_BITS);
454
455   // ((x1 - x0) * (y2 - y1)) - ((x2 - x1) * (y1 - y0)) =
456   // ( d0       *  d1      ) - ( d2       *  d3      ) =
457   // ( m0                  ) - ( m1                  ) = gradient
458
459   // This is split to do 12 elements at a time over three sets: a, b, and c.
460   // Technically we only need to do 10 elements (uvrgb_x and uvrgb_y), so
461   // two of the slots are unused.
462
463   // Inputs are all 16-bit signed. The m0/m1 results are 32-bit signed, as
464   // is g.
465
466   vec_4x16s x0_a_y0_c, x0_b, x0_c;
467   vec_4x16s y0_a, y0_b;
468   vec_4x16s x1_a_y1_c, x1_b, x1_c;
469   vec_4x16s y1_a, y1_b;
470   vec_4x16s x2_a_y2_c, x2_b, x2_c;
471   vec_4x16s y2_a, y2_b;
472
473   vec_4x32u uvrg_base;
474   vec_4x32u b_base;
475   vec_4x32u const_0x8000;
476
477   vec_4x16s d0_a_d3_c, d0_b, d0_c;
478   vec_4x16s d1_a, d1_b, d1_c_d2_a;
479   vec_4x16s d2_b, d2_c;
480   vec_4x16s d3_a, d3_b;
481
482   vec_4x32s m0_a, m0_b, m0_c;
483   vec_4x32s m1_a, m1_b, m1_c;
484
485   vec_4x32u gradient_area_a, gradient_area_c;
486   vec_2x32u gradient_area_b;  
487
488   vec_4x32u gradient_area_sign_a, gradient_area_sign_c;
489   vec_2x32u gradient_area_sign_b;
490   vec_4x32u winding_mask;
491
492   vec_2x64u gradient_wide_a0, gradient_wide_a1;
493   vec_2x64u gradient_wide_c0, gradient_wide_c1;
494   vec_2x64u gradient_wide_b;
495
496   vec_4x32u gradient_a, gradient_c;
497   vec_2x32u gradient_b;
498   vec_16x8s gradient_shift;
499
500   setup_gradient_calculation_input(0, a);
501   setup_gradient_calculation_input(1, b);
502   setup_gradient_calculation_input(2, c);
503
504   dup_4x32b(const_0x8000, 0x8000);
505   shl_long_4x16b(uvrg_base, x0_a_y0_c, 16);
506   shl_long_4x16b(b_base, x0_b, 16);
507
508   add_4x32b(uvrg_base, uvrg_base, const_0x8000);
509   add_4x32b(b_base, b_base, const_0x8000);
510
511   // Can probably pair these, but it'll require careful register allocation
512   sub_4x16b(d0_a_d3_c, x1_a_y1_c, x0_a_y0_c);
513   sub_4x16b(d1_c_d2_a, x2_a_y2_c, x1_a_y1_c);
514
515   sub_4x16b(d0_b, x1_b, x0_b);
516   sub_4x16b(d0_c, x1_c, x0_c);
517
518   sub_4x16b(d1_a, y2_a, y1_a);
519   sub_4x16b(d1_b, y2_b, y1_b);
520
521   sub_4x16b(d2_b, x2_b, x1_b);
522   sub_4x16b(d2_c, x2_c, x1_c);
523
524   sub_4x16b(d3_a, y1_a, y0_a);
525   sub_4x16b(d3_b, y1_b, y0_b);
526
527   mul_long_4x16b(m0_a, d0_a_d3_c, d1_a);
528   mul_long_4x16b(m0_b, d0_b, d1_b);
529   mul_long_4x16b(m0_c, d0_c, d1_c_d2_a);
530
531   mul_long_4x16b(m1_a, d1_c_d2_a, d3_a);
532   mul_long_4x16b(m1_b, d2_b, d3_b);
533   mul_long_4x16b(m1_c, d2_c, d0_a_d3_c);
534
535   sub_4x32b(gradient_area_a, m0_a, m1_a);
536   sub_2x32b(gradient_area_b, m0_b.low, m1_b.low);
537   sub_4x32b(gradient_area_c, m0_c, m1_c);
538
539   cmpltz_4x32b(gradient_area_sign_a, gradient_area_a);
540   cmpltz_2x32b(gradient_area_sign_b, gradient_area_b);
541   cmpltz_4x32b(gradient_area_sign_c, gradient_area_c);
542
543   abs_4x32b(gradient_area_a, gradient_area_a);
544   abs_2x32b(gradient_area_b, gradient_area_b);
545   abs_4x32b(gradient_area_c, gradient_area_c);
546
547   winding_mask_scalar = -psx_gpu->triangle_winding;
548
549   dup_4x32b(winding_mask, winding_mask_scalar);
550   eor_4x32b(gradient_area_sign_a, gradient_area_sign_a, winding_mask);
551   eor_2x32b(gradient_area_sign_b, gradient_area_sign_b, winding_mask);
552   eor_4x32b(gradient_area_sign_c, gradient_area_sign_c, winding_mask);
553
554   mul_scalar_long_2x32b(gradient_wide_a0, 
555    vector_cast(vec_2x32s, gradient_area_a.low), 
556    (s64)triangle_area_reciprocal);
557   mul_scalar_long_2x32b(gradient_wide_a1,
558    vector_cast(vec_2x32s, gradient_area_a.high),
559    (s64)triangle_area_reciprocal);
560   mul_scalar_long_2x32b(gradient_wide_b, 
561    vector_cast(vec_2x32s, gradient_area_b),
562    (s64)triangle_area_reciprocal);
563   mul_scalar_long_2x32b(gradient_wide_c0, 
564    vector_cast(vec_2x32s, gradient_area_c.low),
565    (s64)triangle_area_reciprocal);
566   mul_scalar_long_2x32b(gradient_wide_c1, 
567    vector_cast(vec_2x32s, gradient_area_c.high),
568    (s64)triangle_area_reciprocal);
569
570   dup_16x8b(gradient_shift, triangle_area_shift);
571   shl_reg_2x64b(gradient_wide_a0, gradient_wide_a0,
572    vector_cast(vec_2x64u, gradient_shift));
573   shl_reg_2x64b(gradient_wide_a1, gradient_wide_a1,
574    vector_cast(vec_2x64u, gradient_shift));
575   shl_reg_2x64b(gradient_wide_b, gradient_wide_b,
576    vector_cast(vec_2x64u, gradient_shift));
577   shl_reg_2x64b(gradient_wide_c0, gradient_wide_c0,
578    vector_cast(vec_2x64u, gradient_shift));
579   shl_reg_2x64b(gradient_wide_c1, gradient_wide_c1,
580    vector_cast(vec_2x64u, gradient_shift));
581
582   mov_narrow_2x64b(gradient_a.low, gradient_wide_a0);
583   mov_narrow_2x64b(gradient_a.high, gradient_wide_a1);
584   mov_narrow_2x64b(gradient_b, gradient_wide_b);
585   mov_narrow_2x64b(gradient_c.low, gradient_wide_c0);
586   mov_narrow_2x64b(gradient_c.high, gradient_wide_c1);
587
588   shl_4x32b(gradient_a, gradient_a, 4);
589   shl_2x32b(gradient_b, gradient_b, 4);
590   shl_4x32b(gradient_c, gradient_c, 4);
591
592   eor_4x32b(gradient_a, gradient_a, gradient_area_sign_a);
593   eor_2x32b(gradient_b, gradient_b, gradient_area_sign_b);
594   eor_4x32b(gradient_c, gradient_c, gradient_area_sign_c);
595
596   sub_4x32b(gradient_a, gradient_a, gradient_area_sign_a);
597   sub_2x32b(gradient_b, gradient_b, gradient_area_sign_b);
598   sub_4x32b(gradient_c, gradient_c, gradient_area_sign_c);
599
600   u32 left_adjust = a->x;
601   mls_scalar_4x32b(uvrg_base, gradient_a, left_adjust);
602   mls_scalar_2x32b(b_base.low, gradient_b, left_adjust);
603
604   vec_4x32u uvrg_dx2;
605   vec_2x32u b_dx2;
606
607   vec_4x32u uvrg_dx3;
608   vec_2x32u b_dx3;
609
610   vec_4x32u zero;
611
612   eor_4x32b(zero, zero, zero);
613   add_4x32b(uvrg_dx2, gradient_a, gradient_a);
614   add_2x32b(b_dx2, gradient_b, gradient_b);
615   add_4x32b(uvrg_dx3, gradient_a, uvrg_dx2);
616   add_2x32b(b_dx3, gradient_b, b_dx2);
617
618   // Can be done with vst4, assuming that the zero, dx, dx2, and dx3 are
619   // lined up properly
620   psx_gpu->u_block_span.e[0] = zero.e[0];
621   psx_gpu->u_block_span.e[1] = gradient_a.e[0];
622   psx_gpu->u_block_span.e[2] = uvrg_dx2.e[0];
623   psx_gpu->u_block_span.e[3] = uvrg_dx3.e[0];
624
625   psx_gpu->v_block_span.e[0] = zero.e[1];
626   psx_gpu->v_block_span.e[1] = gradient_a.e[1];
627   psx_gpu->v_block_span.e[2] = uvrg_dx2.e[1];
628   psx_gpu->v_block_span.e[3] = uvrg_dx3.e[1];
629
630   psx_gpu->r_block_span.e[0] = zero.e[2];
631   psx_gpu->r_block_span.e[1] = gradient_a.e[2];
632   psx_gpu->r_block_span.e[2] = uvrg_dx2.e[2];
633   psx_gpu->r_block_span.e[3] = uvrg_dx3.e[2];
634
635   psx_gpu->g_block_span.e[0] = zero.e[3];
636   psx_gpu->g_block_span.e[1] = gradient_a.e[3];
637   psx_gpu->g_block_span.e[2] = uvrg_dx2.e[3];
638   psx_gpu->g_block_span.e[3] = uvrg_dx3.e[3];
639
640   psx_gpu->b_block_span.e[0] = zero.e[0];
641   psx_gpu->b_block_span.e[1] = gradient_b.e[0];
642   psx_gpu->b_block_span.e[2] = b_dx2.e[0];
643   psx_gpu->b_block_span.e[3] = b_dx3.e[0];
644
645   psx_gpu->uvrg = uvrg_base;
646   psx_gpu->b = b_base.e[0];
647
648   psx_gpu->uvrg_dx = gradient_a;
649   psx_gpu->uvrg_dy = gradient_c;
650   psx_gpu->b_dy = gradient_b.e[1];
651 }
652 #endif
653
654 #define vector_check(_a, _b)                                                   \
655   if(memcmp(&_a, &_b, sizeof(_b)))                                             \
656   {                                                                            \
657     if(sizeof(_b) == 8)                                                        \
658     {                                                                          \
659       printf("mismatch on %s vs %s: (%x %x) vs (%x %x)\n",                     \
660        #_a, #_b, _a.e[0], _a.e[1], _b.e[0], _b.e[1]);                          \
661     }                                                                          \
662     else                                                                       \
663     {                                                                          \
664       printf("mismatch on %s vs %s: (%x %x %x %x) vs (%x %x %x %x)\n",         \
665        #_a, #_b, _a.e[0], _a.e[1], _a.e[2], _a.e[3], _b.e[0], _b.e[1],         \
666        _b.e[2], _b.e[3]);                                                      \
667     }                                                                          \
668   }                                                                            \
669
670 #define scalar_check(_a, _b)                                                   \
671   if(_a != _b)                                                                 \
672     printf("mismatch on %s %s: %x vs %x\n", #_a, #_b, _a, _b)                  \
673
674
675 #define setup_spans_prologue_alternate_yes()                                   \
676   vec_2x64s alternate_x;                                                       \
677   vec_2x64s alternate_dx_dy;                                                   \
678   vec_4x32s alternate_x_32;                                                    \
679   vec_2x32s alternate_x_16;                                                    \
680                                                                                \
681   vec_4x16u alternate_select;                                                  \
682   vec_4x16s y_mid_point;                                                       \
683                                                                                \
684   s32 y_b = v_b->y;                                                            \
685   s64 edge_alt;                                                                \
686   s32 edge_dx_dy_alt;                                                          \
687   u32 edge_shift_alt                                                           \
688
689 #define setup_spans_prologue_alternate_no()                                    \
690
691 #define setup_spans_prologue(alternate_active)                                 \
692   edge_data_struct *span_edge_data;                                            \
693   vec_4x32u *span_uvrg_offset;                                                 \
694   u32 *span_b_offset;                                                          \
695                                                                                \
696   s32 clip;                                                                    \
697                                                                                \
698   vec_2x64s edges_xy;                                                          \
699   vec_2x32s edges_dx_dy;                                                       \
700   vec_2x32u edge_shifts;                                                       \
701                                                                                \
702   vec_2x64s left_x, right_x;                                                   \
703   vec_2x64s left_dx_dy, right_dx_dy;                                           \
704   vec_4x32s left_x_32, right_x_32;                                             \
705   vec_8x16s left_right_x_16;                                                   \
706   vec_4x16s y_x4;                                                              \
707   vec_8x16s left_edge;                                                         \
708   vec_8x16s right_edge;                                                        \
709   vec_4x16u span_shift;                                                        \
710                                                                                \
711   vec_2x32u c_0x01;                                                            \
712   vec_4x16u c_0x04;                                                            \
713   vec_4x16u c_0xFFFE;                                                          \
714   vec_4x16u c_0x07;                                                            \
715                                                                                \
716   vec_2x32s x_starts;                                                          \
717   vec_2x32s x_ends;                                                            \
718                                                                                \
719   s32 x_a = v_a->x;                                                            \
720   s32 x_b = v_b->x;                                                            \
721   s32 x_c = v_c->x;                                                            \
722   s32 y_a = v_a->y;                                                            \
723   s32 y_c = v_c->y;                                                            \
724                                                                                \
725   vec_4x32u uvrg = psx_gpu->uvrg;                                              \
726   vec_4x32u uvrg_dy = psx_gpu->uvrg_dy;                                        \
727   u32 b = psx_gpu->b;                                                          \
728   u32 b_dy = psx_gpu->b_dy;                                                    \
729                                                                                \
730   dup_2x32b(c_0x01, 0x01);                                                     \
731   setup_spans_prologue_alternate_##alternate_active()                          \
732
733 #define setup_spans_prologue_b()                                               \
734   span_edge_data = psx_gpu->span_edge_data;                                    \
735   span_uvrg_offset = psx_gpu->span_uvrg_offset;                                \
736   span_b_offset = psx_gpu->span_b_offset;                                      \
737                                                                                \
738   vec_8x16u c_0x0001;                                                          \
739                                                                                \
740   dup_8x16b(c_0x0001, 0x0001);                                                 \
741   dup_8x16b(left_edge, psx_gpu->viewport_start_x);                             \
742   dup_8x16b(right_edge, psx_gpu->viewport_end_x);                              \
743   add_8x16b(right_edge, right_edge, c_0x0001);                                 \
744   dup_4x16b(c_0x04, 0x04);                                                     \
745   dup_4x16b(c_0x07, 0x07);                                                     \
746   dup_4x16b(c_0xFFFE, 0xFFFE);                                                 \
747
748
749 #define compute_edge_delta_x2()                                                \
750 {                                                                              \
751   vec_2x32s heights;                                                           \
752   vec_2x32s height_reciprocals;                                                \
753   vec_2x32s heights_b;                                                         \
754   vec_4x32u widths;                                                            \
755                                                                                \
756   u32 edge_shift = reciprocal_table[height];                                   \
757                                                                                \
758   dup_2x32b(heights, height);                                                  \
759   sub_2x32b(widths, x_ends, x_starts);                                         \
760                                                                                \
761   dup_2x32b(edge_shifts, edge_shift);                                          \
762   sub_2x32b(heights_b, heights, c_0x01);                                       \
763   shr_2x32b(height_reciprocals, edge_shifts, 12);                              \
764                                                                                \
765   mla_2x32b(heights_b, x_starts, heights);                                     \
766   bic_immediate_4x16b(vector_cast(vec_4x16u, edge_shifts), 0xE0);              \
767   mul_2x32b(edges_dx_dy, widths, height_reciprocals);                          \
768   mul_long_2x32b(edges_xy, heights_b, height_reciprocals);                     \
769 }                                                                              \
770
771 #define compute_edge_delta_x3(start_c, height_a, height_b)                     \
772 {                                                                              \
773   vec_2x32s heights;                                                           \
774   vec_2x32s height_reciprocals;                                                \
775   vec_2x32s heights_b;                                                         \
776   vec_2x32u widths;                                                            \
777                                                                                \
778   u32 width_alt;                                                               \
779   s32 height_b_alt;                                                            \
780   u32 height_reciprocal_alt;                                                   \
781                                                                                \
782   heights.e[0] = height_a;                                                     \
783   heights.e[1] = height_b;                                                     \
784                                                                                \
785   edge_shifts.e[0] = reciprocal_table[height_a];                               \
786   edge_shifts.e[1] = reciprocal_table[height_b];                               \
787   edge_shift_alt = reciprocal_table[height_minor_b];                           \
788                                                                                \
789   sub_2x32b(widths, x_ends, x_starts);                                         \
790   width_alt = x_c - start_c;                                                   \
791                                                                                \
792   shr_2x32b(height_reciprocals, edge_shifts, 12);                              \
793   height_reciprocal_alt = edge_shift_alt >> 12;                                \
794                                                                                \
795   bic_immediate_4x16b(vector_cast(vec_4x16u, edge_shifts), 0xE0);              \
796   edge_shift_alt &= 0x1F;                                                      \
797                                                                                \
798   sub_2x32b(heights_b, heights, c_0x01);                                       \
799   height_b_alt = height_minor_b - 1;                                           \
800                                                                                \
801   mla_2x32b(heights_b, x_starts, heights);                                     \
802   height_b_alt += height_minor_b * start_c;                                    \
803                                                                                \
804   mul_long_2x32b(edges_xy, heights_b, height_reciprocals);                     \
805   edge_alt = (s64)height_b_alt * height_reciprocal_alt;                        \
806                                                                                \
807   mul_2x32b(edges_dx_dy, widths, height_reciprocals);                          \
808   edge_dx_dy_alt = width_alt * height_reciprocal_alt;                          \
809 }                                                                              \
810
811
812 #define setup_spans_adjust_y_up()                                              \
813   sub_4x32b(y_x4, y_x4, c_0x04)                                                \
814
815 #define setup_spans_adjust_y_down()                                            \
816   add_4x32b(y_x4, y_x4, c_0x04)                                                \
817
818 #define setup_spans_adjust_interpolants_up()                                   \
819   sub_4x32b(uvrg, uvrg, uvrg_dy);                                              \
820   b -= b_dy                                                                    \
821
822 #define setup_spans_adjust_interpolants_down()                                 \
823   add_4x32b(uvrg, uvrg, uvrg_dy);                                              \
824   b += b_dy                                                                    \
825
826
827 #define setup_spans_clip_interpolants_increment()                              \
828   mla_scalar_4x32b(uvrg, uvrg_dy, clip);                                       \
829   b += b_dy * clip                                                             \
830
831 #define setup_spans_clip_interpolants_decrement()                              \
832   mls_scalar_4x32b(uvrg, uvrg_dy, clip);                                       \
833   b -= b_dy * clip                                                             \
834
835 #define setup_spans_clip_alternate_yes()                                       \
836   edge_alt += edge_dx_dy_alt * (s64)(clip)                                     \
837
838 #define setup_spans_clip_alternate_no()                                        \
839
840 #define setup_spans_clip(direction, alternate_active)                          \
841 {                                                                              \
842   clipped_triangles++;                                                         \
843   mla_scalar_long_2x32b(edges_xy, edges_dx_dy, (s64)clip);                     \
844   setup_spans_clip_alternate_##alternate_active();                             \
845   setup_spans_clip_interpolants_##direction();                                 \
846 }                                                                              \
847
848
849 #define setup_spans_adjust_edges_alternate_no(left_index, right_index)         \
850 {                                                                              \
851   vec_2x64u edge_shifts_64;                                                    \
852   vec_2x64s edges_dx_dy_64;                                                    \
853                                                                                \
854   mov_wide_2x32b(edge_shifts_64, edge_shifts);                                 \
855   shl_variable_2x64b(edges_xy, edges_xy, edge_shifts_64);                      \
856                                                                                \
857   mov_wide_2x32b(edges_dx_dy_64, edges_dx_dy);                                 \
858   shl_variable_2x64b(edges_dx_dy_64, edges_dx_dy_64, edge_shifts_64);          \
859                                                                                \
860   left_x.e[0] = edges_xy.e[left_index];                                        \
861   right_x.e[0] = edges_xy.e[right_index];                                      \
862                                                                                \
863   left_dx_dy.e[0] = edges_dx_dy_64.e[left_index];                              \
864   left_dx_dy.e[1] = edges_dx_dy_64.e[left_index];                              \
865   right_dx_dy.e[0] = edges_dx_dy_64.e[right_index];                            \
866   right_dx_dy.e[1] = edges_dx_dy_64.e[right_index];                            \
867                                                                                \
868   add_1x64b(left_x.high, left_x.low, left_dx_dy.low);                          \
869   add_1x64b(right_x.high, right_x.low, right_dx_dy.low);                       \
870                                                                                \
871   add_2x64b(left_dx_dy, left_dx_dy, left_dx_dy);                               \
872   add_2x64b(right_dx_dy, right_dx_dy, right_dx_dy);                            \
873 }                                                                              \
874
875 #define setup_spans_adjust_edges_alternate_yes(left_index, right_index)        \
876 {                                                                              \
877   setup_spans_adjust_edges_alternate_no(left_index, right_index);              \
878   s64 edge_dx_dy_alt_64;                                                       \
879                                                                                \
880   dup_4x16b(y_mid_point, y_b);                                                 \
881                                                                                \
882   edge_alt <<= edge_shift_alt;                                                 \
883   edge_dx_dy_alt_64 = (s64)edge_dx_dy_alt << edge_shift_alt;                   \
884                                                                                \
885   alternate_x.e[0] = edge_alt;                                                 \
886   alternate_dx_dy.e[0] = edge_dx_dy_alt_64;                                    \
887   alternate_dx_dy.e[1] = edge_dx_dy_alt_64;                                    \
888                                                                                \
889   add_1x64b(alternate_x.high, alternate_x.low, alternate_dx_dy.low);           \
890   add_2x64b(alternate_dx_dy, alternate_dx_dy, alternate_dx_dy);                \
891 }                                                                              \
892
893
894 #define setup_spans_y_select_up()                                              \
895   cmplt_4x16b(alternate_select, y_x4, y_mid_point)                             \
896
897 #define setup_spans_y_select_down()                                            \
898   cmpgt_4x16b(alternate_select, y_x4, y_mid_point)                             \
899
900 #define setup_spans_y_select_alternate_yes(direction)                          \
901   setup_spans_y_select_##direction()                                           \
902
903 #define setup_spans_y_select_alternate_no(direction)                           \
904
905 #define setup_spans_alternate_select_left()                                    \
906   bit_4x16b(left_right_x_16.low, alternate_x_16, alternate_select)             \
907
908 #define setup_spans_alternate_select_right()                                   \
909   bit_4x16b(left_right_x_16.high, alternate_x_16, alternate_select)            \
910
911 #define setup_spans_alternate_select_none()                                    \
912
913 #define setup_spans_increment_alternate_yes()                                  \
914   shr_narrow_2x64b(alternate_x_32.low, alternate_x, 32);                       \
915   add_2x64b(alternate_x, alternate_x, alternate_dx_dy);                        \
916   shr_narrow_2x64b(alternate_x_32.high, alternate_x, 32);                      \
917   add_2x64b(alternate_x, alternate_x, alternate_dx_dy);                        \
918   mov_narrow_4x32b(alternate_x_16, alternate_x_32)                             \
919
920 #define setup_spans_increment_alternate_no()                                   \
921
922 #define setup_spans_set_x4(alternate, direction, alternate_active)             \
923 {                                                                              \
924   span_uvrg_offset[0] = uvrg;                                                  \
925   span_b_offset[0] = b;                                                        \
926   setup_spans_adjust_interpolants_##direction();                               \
927                                                                                \
928   span_uvrg_offset[1] = uvrg;                                                  \
929   span_b_offset[1] = b;                                                        \
930   setup_spans_adjust_interpolants_##direction();                               \
931                                                                                \
932   span_uvrg_offset[2] = uvrg;                                                  \
933   span_b_offset[2] = b;                                                        \
934   setup_spans_adjust_interpolants_##direction();                               \
935                                                                                \
936   span_uvrg_offset[3] = uvrg;                                                  \
937   span_b_offset[3] = b;                                                        \
938   setup_spans_adjust_interpolants_##direction();                               \
939                                                                                \
940   span_uvrg_offset += 4;                                                       \
941   span_b_offset += 4;                                                          \
942                                                                                \
943   shr_narrow_2x64b(left_x_32.low, left_x, 32);                                 \
944   shr_narrow_2x64b(right_x_32.low, right_x, 32);                               \
945                                                                                \
946   add_2x64b(left_x, left_x, left_dx_dy);                                       \
947   add_2x64b(right_x, right_x, right_dx_dy);                                    \
948                                                                                \
949   shr_narrow_2x64b(left_x_32.high, left_x, 32);                                \
950   shr_narrow_2x64b(right_x_32.high, right_x, 32);                              \
951                                                                                \
952   add_2x64b(left_x, left_x, left_dx_dy);                                       \
953   add_2x64b(right_x, right_x, right_dx_dy);                                    \
954                                                                                \
955   mov_narrow_4x32b(left_right_x_16.low, left_x_32);                            \
956   mov_narrow_4x32b(left_right_x_16.high, right_x_32);                          \
957                                                                                \
958   setup_spans_increment_alternate_##alternate_active();                        \
959   setup_spans_y_select_alternate_##alternate_active(direction);                \
960   setup_spans_alternate_select_##alternate();                                  \
961                                                                                \
962   max_8x16b(left_right_x_16, left_right_x_16, left_edge);                      \
963   min_8x16b(left_right_x_16, left_right_x_16, right_edge);                     \
964                                                                                \
965   sub_4x16b(left_right_x_16.high, left_right_x_16.high, left_right_x_16.low);  \
966   add_4x16b(left_right_x_16.high, left_right_x_16.high, c_0x07);               \
967   and_4x16b(span_shift, left_right_x_16.high, c_0x07);                         \
968   shl_variable_4x16b(span_shift, c_0xFFFE, span_shift);                        \
969   shr_4x16b(left_right_x_16.high, left_right_x_16.high, 3);                    \
970                                                                                \
971   u32 i;                                                                       \
972   for(i = 0; i < 4; i++)                                                       \
973   {                                                                            \
974     span_edge_data[i].left_x = left_right_x_16.low.e[i];                       \
975     span_edge_data[i].num_blocks = left_right_x_16.high.e[i];                  \
976     span_edge_data[i].right_mask = span_shift.e[i];                            \
977     span_edge_data[i].y = y_x4.e[i];                                           \
978   }                                                                            \
979                                                                                \
980   span_edge_data += 4;                                                         \
981                                                                                \
982   setup_spans_adjust_y_##direction();                                          \
983 }                                                                              \
984
985
986 #define setup_spans_alternate_adjust_yes()                                     \
987   edge_alt -= edge_dx_dy_alt * (s64)height_minor_a                             \
988
989 #define setup_spans_alternate_adjust_no()                                      \
990
991
992 #define setup_spans_down(left_index, right_index, alternate, alternate_active) \
993   setup_spans_alternate_adjust_##alternate_active();                           \
994   if(y_c > psx_gpu->viewport_end_y)                                            \
995     height -= y_c - psx_gpu->viewport_end_y - 1;                               \
996                                                                                \
997   clip = psx_gpu->viewport_start_y - y_a;                                      \
998   if(clip > 0)                                                                 \
999   {                                                                            \
1000     height -= clip;                                                            \
1001     y_a += clip;                                                               \
1002     setup_spans_clip(increment, alternate_active);                             \
1003   }                                                                            \
1004                                                                                \
1005   setup_spans_prologue_b();                                                    \
1006                                                                                \
1007   if(height > 0)                                                               \
1008   {                                                                            \
1009     y_x4.e[0] = y_a;                                                           \
1010     y_x4.e[1] = y_a + 1;                                                       \
1011     y_x4.e[2] = y_a + 2;                                                       \
1012     y_x4.e[3] = y_a + 3;                                                       \
1013     setup_spans_adjust_edges_alternate_##alternate_active(left_index,          \
1014      right_index);                                                             \
1015                                                                                \
1016     psx_gpu->num_spans = height;                                               \
1017     do                                                                         \
1018     {                                                                          \
1019       setup_spans_set_x4(alternate, down, alternate_active);                   \
1020       height -= 4;                                                             \
1021     } while(height > 0);                                                       \
1022   }                                                                            \
1023
1024
1025 #define setup_spans_alternate_pre_increment_yes()                              \
1026   edge_alt += edge_dx_dy_alt                                                   \
1027
1028 #define setup_spans_alternate_pre_increment_no()                               \
1029
1030 #define setup_spans_up_decrement_height_yes()                                  \
1031   height--                                                                     \
1032
1033 #define setup_spans_up_decrement_height_no()                                   \
1034   {}                                                                           \
1035
1036 #define setup_spans_up(left_index, right_index, alternate, alternate_active)   \
1037   setup_spans_alternate_adjust_##alternate_active();                           \
1038   y_a--;                                                                       \
1039                                                                                \
1040   if(y_c < psx_gpu->viewport_start_y)                                          \
1041     height -= psx_gpu->viewport_start_y - y_c;                                 \
1042   else                                                                         \
1043     setup_spans_up_decrement_height_##alternate_active();                      \
1044                                                                                \
1045   clip = y_a - psx_gpu->viewport_end_y;                                        \
1046   if(clip > 0)                                                                 \
1047   {                                                                            \
1048     height -= clip;                                                            \
1049     y_a -= clip;                                                               \
1050     setup_spans_clip(decrement, alternate_active);                             \
1051   }                                                                            \
1052                                                                                \
1053   setup_spans_prologue_b();                                                    \
1054                                                                                \
1055   if(height > 0)                                                               \
1056   {                                                                            \
1057     y_x4.e[0] = y_a;                                                           \
1058     y_x4.e[1] = y_a - 1;                                                       \
1059     y_x4.e[2] = y_a - 2;                                                       \
1060     y_x4.e[3] = y_a - 3;                                                       \
1061     add_wide_2x32b(edges_xy, edges_xy, edges_dx_dy);                           \
1062     setup_spans_alternate_pre_increment_##alternate_active();                  \
1063     setup_spans_adjust_edges_alternate_##alternate_active(left_index,          \
1064      right_index);                                                             \
1065     setup_spans_adjust_interpolants_up();                                      \
1066                                                                                \
1067     psx_gpu->num_spans = height;                                               \
1068     while(height > 0)                                                          \
1069     {                                                                          \
1070       setup_spans_set_x4(alternate, up, alternate_active);                     \
1071       height -= 4;                                                             \
1072     }                                                                          \
1073   }                                                                            \
1074
1075 #define index_left  0
1076 #define index_right 1
1077
1078 #define setup_spans_up_up(minor, major)                                        \
1079   setup_spans_prologue(yes);                                                   \
1080   s32 height_minor_a = y_a - y_b;                                              \
1081   s32 height_minor_b = y_b - y_c;                                              \
1082   s32 height = y_a - y_c;                                                      \
1083                                                                                \
1084   dup_2x32b(x_starts, x_a);                                                    \
1085   x_ends.e[0] = x_c;                                                           \
1086   x_ends.e[1] = x_b;                                                           \
1087                                                                                \
1088   compute_edge_delta_x3(x_b, height, height_minor_a);                          \
1089   setup_spans_up(index_##major, index_##minor, minor, yes)                     \
1090
1091
1092 void setup_spans_up_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1093  vertex_struct *v_b, vertex_struct *v_c);
1094 void setup_spans_up_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1095  vertex_struct *v_b, vertex_struct *v_c);
1096 void setup_spans_down_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1097  vertex_struct *v_b, vertex_struct *v_c);
1098 void setup_spans_down_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1099  vertex_struct *v_b, vertex_struct *v_c);
1100 void setup_spans_up_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1101  vertex_struct *v_b, vertex_struct *v_c);
1102 void setup_spans_up_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1103  vertex_struct *v_b, vertex_struct *v_c);
1104 void setup_spans_down_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1105  vertex_struct *v_b, vertex_struct *v_c);
1106 void setup_spans_down_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1107  vertex_struct *v_b, vertex_struct *v_c);
1108 void setup_spans_up_down(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1109  vertex_struct *v_b, vertex_struct *v_c);
1110
1111
1112 #ifndef NEON_BUILD
1113
1114 void setup_spans_up_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1115  vertex_struct *v_b, vertex_struct *v_c)
1116 {
1117   setup_spans_up_up(left, right);
1118 }
1119
1120 void setup_spans_up_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1121  vertex_struct *v_b, vertex_struct *v_c)
1122 {
1123   setup_spans_up_up(right, left);
1124 }
1125
1126 #define setup_spans_down_down(minor, major)                                    \
1127   setup_spans_prologue(yes);                                                   \
1128   s32 height_minor_a = y_b - y_a;                                              \
1129   s32 height_minor_b = y_c - y_b;                                              \
1130   s32 height = y_c - y_a;                                                      \
1131                                                                                \
1132   dup_2x32b(x_starts, x_a);                                                    \
1133   x_ends.e[0] = x_c;                                                           \
1134   x_ends.e[1] = x_b;                                                           \
1135                                                                                \
1136   compute_edge_delta_x3(x_b, height, height_minor_a);                          \
1137   setup_spans_down(index_##major, index_##minor, minor, yes)                   \
1138
1139 void setup_spans_down_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1140  vertex_struct *v_b, vertex_struct *v_c)
1141 {
1142   setup_spans_down_down(left, right);
1143 }
1144
1145 void setup_spans_down_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1146  vertex_struct *v_b, vertex_struct *v_c)
1147 {
1148   setup_spans_down_down(right, left);
1149 }
1150
1151 #define setup_spans_up_flat()                                                  \
1152   s32 height = y_a - y_c;                                                      \
1153                                                                                \
1154   flat_triangles++;                                                            \
1155   compute_edge_delta_x2();                                                     \
1156   setup_spans_up(index_left, index_right, none, no)                            \
1157
1158 void setup_spans_up_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1159  vertex_struct *v_b, vertex_struct *v_c)
1160 {
1161   setup_spans_prologue(no);
1162   x_starts.e[0] = x_a;
1163   x_starts.e[1] = x_b;
1164   dup_2x32b(x_ends, x_c);
1165
1166   setup_spans_up_flat();
1167 }
1168
1169 void setup_spans_up_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1170  vertex_struct *v_b, vertex_struct *v_c)
1171 {
1172   setup_spans_prologue(no);
1173   dup_2x32b(x_starts, x_a);
1174   x_ends.e[0] = x_b;
1175   x_ends.e[1] = x_c;
1176
1177   setup_spans_up_flat();
1178 }
1179
1180 #define setup_spans_down_flat()                                                \
1181   s32 height = y_c - y_a;                                                      \
1182                                                                                \
1183   flat_triangles++;                                                            \
1184   compute_edge_delta_x2();                                                     \
1185   setup_spans_down(index_left, index_right, none, no)                          \
1186
1187 void setup_spans_down_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1188  vertex_struct *v_b, vertex_struct *v_c)
1189 {
1190   setup_spans_prologue(no);
1191   x_starts.e[0] = x_a;
1192   x_starts.e[1] = x_b;
1193   dup_2x32b(x_ends, x_c);
1194
1195   setup_spans_down_flat();
1196 }
1197
1198 void setup_spans_down_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1199  vertex_struct *v_b, vertex_struct *v_c)
1200 {
1201   setup_spans_prologue(no);
1202   dup_2x32b(x_starts, x_a);
1203   x_ends.e[0] = x_b;
1204   x_ends.e[1] = x_c;
1205
1206   setup_spans_down_flat();
1207 }
1208
1209 void setup_spans_up_down(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1210  vertex_struct *v_b, vertex_struct *v_c)
1211 {
1212   setup_spans_prologue(no);
1213
1214   s32 y_b = v_b->y;
1215   s64 edge_alt;
1216   s32 edge_dx_dy_alt;
1217   u32 edge_shift_alt;
1218
1219   s32 middle_y = y_a;
1220   s32 height_minor_a = y_a - y_b;
1221   s32 height_minor_b = y_c - y_a;
1222   s32 height_major = y_c - y_b;
1223
1224   vec_2x64s edges_xy_b;
1225   vec_2x32s edges_dx_dy_b;
1226   vec_2x32u edge_shifts_b;
1227
1228   vec_2x32s height_increment;
1229
1230   x_starts.e[0] = x_a;
1231   x_starts.e[1] = x_c;
1232   dup_2x32b(x_ends, x_b);
1233
1234   compute_edge_delta_x3(x_a, height_minor_a, height_major);
1235
1236   height_increment.e[0] = 0;
1237   height_increment.e[1] = height_minor_b;
1238
1239   mla_long_2x32b(edges_xy, edges_dx_dy, height_increment);
1240
1241   edges_xy_b.e[0] = edge_alt;
1242   edges_xy_b.e[1] = edges_xy.e[1];
1243
1244   edge_shifts_b = edge_shifts;
1245   edge_shifts_b.e[0] = edge_shift_alt;
1246
1247   neg_2x32b(edges_dx_dy_b, edges_dx_dy);
1248   edges_dx_dy_b.e[0] = edge_dx_dy_alt;
1249   
1250   y_a--;
1251
1252   if(y_b < psx_gpu->viewport_start_y)
1253     height_minor_a -= psx_gpu->viewport_start_y - y_b;
1254
1255   clip = y_a - psx_gpu->viewport_end_y;
1256   if(clip > 0)
1257   {
1258     height_minor_a -= clip;
1259     y_a -= clip;
1260     setup_spans_clip(decrement, no);
1261   }
1262
1263   setup_spans_prologue_b();
1264
1265   if(height_minor_a > 0)
1266   {
1267     y_x4.e[0] = y_a;
1268     y_x4.e[1] = y_a - 1;
1269     y_x4.e[2] = y_a - 2;
1270     y_x4.e[3] = y_a - 3;
1271     add_wide_2x32b(edges_xy, edges_xy, edges_dx_dy);
1272     setup_spans_adjust_edges_alternate_no(index_left, index_right);
1273     setup_spans_adjust_interpolants_up();
1274
1275     psx_gpu->num_spans = height_minor_a;
1276     while(height_minor_a > 0)
1277     {
1278       setup_spans_set_x4(none, up, no);
1279       height_minor_a -= 4;
1280     }
1281
1282     span_edge_data += height_minor_a;
1283     span_uvrg_offset += height_minor_a;
1284     span_b_offset += height_minor_a;
1285   }
1286   
1287   edges_xy = edges_xy_b;
1288   edges_dx_dy = edges_dx_dy_b;
1289   edge_shifts = edge_shifts_b;
1290
1291   uvrg = psx_gpu->uvrg;
1292   b = psx_gpu->b;
1293
1294   y_a = middle_y;
1295
1296   if(y_c > psx_gpu->viewport_end_y)
1297     height_minor_b -= y_c - psx_gpu->viewport_end_y - 1;
1298
1299   clip = psx_gpu->viewport_start_y - y_a;
1300   if(clip > 0)
1301   {
1302     height_minor_b -= clip;
1303     y_a += clip;
1304     setup_spans_clip(increment, no);
1305   }
1306
1307   if(height_minor_b > 0)
1308   {
1309     y_x4.e[0] = y_a;
1310     y_x4.e[1] = y_a + 1;
1311     y_x4.e[2] = y_a + 2;
1312     y_x4.e[3] = y_a + 3;
1313     setup_spans_adjust_edges_alternate_no(index_left, index_right);
1314
1315     psx_gpu->num_spans += height_minor_b;
1316     do
1317     {
1318       setup_spans_set_x4(none, down, no);
1319       height_minor_b -= 4;
1320     } while(height_minor_b > 0);
1321   }
1322
1323   left_split_triangles++;
1324 }
1325
1326 #endif
1327
1328
1329 #define dither_table_entry_normal(value)                                       \
1330   (value)                                                                      \
1331
1332
1333 #define setup_blocks_load_msb_mask_indirect()                                  \
1334
1335 #define setup_blocks_load_msb_mask_direct()                                    \
1336   vec_8x16u msb_mask;                                                          \
1337   dup_8x16b(msb_mask, psx_gpu->mask_msb);                                      \
1338
1339
1340 #define setup_blocks_variables_shaded_textured(target)                         \
1341   vec_4x32u u_block;                                                           \
1342   vec_4x32u v_block;                                                           \
1343   vec_4x32u r_block;                                                           \
1344   vec_4x32u g_block;                                                           \
1345   vec_4x32u b_block;                                                           \
1346   vec_4x32u uvrg_dx = psx_gpu->uvrg_dx;                                        \
1347   vec_4x32u uvrg_dx4;                                                          \
1348   vec_4x32u uvrg_dx8;                                                          \
1349   vec_4x32u uvrg;                                                              \
1350   u32 b_dx = psx_gpu->b_block_span.e[1];                                       \
1351   u32 b_dx4 = b_dx << 2;                                                       \
1352   u32 b_dx8 = b_dx << 3;                                                       \
1353   u32 b;                                                                       \
1354                                                                                \
1355   vec_16x8u texture_mask;                                                      \
1356   shl_4x32b(uvrg_dx4, uvrg_dx, 2);                                             \
1357   shl_4x32b(uvrg_dx8, uvrg_dx, 3);                                             \
1358   dup_8x8b(texture_mask.low, psx_gpu->texture_mask_width);                     \
1359   dup_8x8b(texture_mask.high, psx_gpu->texture_mask_height)                    \
1360
1361 #define setup_blocks_variables_shaded_untextured(target)                       \
1362   vec_4x32u r_block;                                                           \
1363   vec_4x32u g_block;                                                           \
1364   vec_4x32u b_block;                                                           \
1365   vec_4x32u rgb_dx;                                                            \
1366   vec_4x32u rgb_dx4;                                                           \
1367   vec_4x32u rgb_dx8;                                                           \
1368   vec_4x32u rgb;                                                               \
1369                                                                                \
1370   vec_8x8u d64_0x07;                                                           \
1371   vec_8x8u d64_1;                                                              \
1372   vec_8x8u d64_4;                                                              \
1373   vec_8x8u d64_128;                                                            \
1374                                                                                \
1375   dup_8x8b(d64_0x07, 0x07);                                                    \
1376   dup_8x8b(d64_1, 1);                                                          \
1377   dup_8x8b(d64_4, 4);                                                          \
1378   dup_8x8b(d64_128, 128);                                                      \
1379                                                                                \
1380   rgb_dx.low = psx_gpu->uvrg_dx.high;                                          \
1381   rgb_dx.e[2] = psx_gpu->b_block_span.e[1];                                    \
1382   shl_4x32b(rgb_dx4, rgb_dx, 2);                                               \
1383   shl_4x32b(rgb_dx8, rgb_dx, 3)                                                \
1384
1385 #define setup_blocks_variables_unshaded_textured(target)                       \
1386   vec_4x32u u_block;                                                           \
1387   vec_4x32u v_block;                                                           \
1388   vec_2x32u uv_dx = psx_gpu->uvrg_dx.low;                                      \
1389   vec_2x32u uv_dx4;                                                            \
1390   vec_2x32u uv_dx8;                                                            \
1391   vec_2x32u uv = psx_gpu->uvrg.low;                                            \
1392                                                                                \
1393   vec_16x8u texture_mask;                                                      \
1394   shl_2x32b(uv_dx4, uv_dx, 2);                                                 \
1395   shl_2x32b(uv_dx8, uv_dx, 3);                                                 \
1396   dup_8x8b(texture_mask.low, psx_gpu->texture_mask_width);                     \
1397   dup_8x8b(texture_mask.high, psx_gpu->texture_mask_height)                    \
1398
1399
1400 #define setup_blocks_variables_unshaded_untextured_direct()                    \
1401   or_8x16b(colors, colors, msb_mask)                                           \
1402
1403 #define setup_blocks_variables_unshaded_untextured_indirect()                  \
1404
1405 #define setup_blocks_variables_unshaded_untextured(target)                     \
1406   u32 color = psx_gpu->triangle_color;                                         \
1407   vec_8x16u colors;                                                            \
1408                                                                                \
1409   u32 color_r = color & 0xFF;                                                  \
1410   u32 color_g = (color >> 8) & 0xFF;                                           \
1411   u32 color_b = (color >> 16) & 0xFF;                                          \
1412                                                                                \
1413   color = (color_r >> 3) | ((color_g >> 3) << 5) |                             \
1414    ((color_b >> 3) << 10);                                                     \
1415   dup_8x16b(colors, color);                                                    \
1416   setup_blocks_variables_unshaded_untextured_##target()                        \
1417
1418 #define setup_blocks_span_initialize_dithered_textured()                       \
1419   vec_8x16u dither_offsets;                                                    \
1420   shl_long_8x8b(dither_offsets, dither_offsets_short, 4)                       \
1421
1422 #define setup_blocks_span_initialize_dithered_untextured()                     \
1423   vec_8x8u dither_offsets;                                                     \
1424   add_8x8b(dither_offsets, dither_offsets_short, d64_4)                        \
1425
1426 #define setup_blocks_span_initialize_dithered(texturing)                       \
1427   u32 dither_row = psx_gpu->dither_table[y & 0x3];                             \
1428   u32 dither_shift = (span_edge_data->left_x & 0x3) * 8;                       \
1429   vec_8x8s dither_offsets_short;                                               \
1430                                                                                \
1431   dither_row =                                                                 \
1432    (dither_row >> dither_shift) | (dither_row << (32 - dither_shift));         \
1433   dup_2x32b(vector_cast(vec_2x32u, dither_offsets_short), dither_row);         \
1434   setup_blocks_span_initialize_dithered_##texturing()                          \
1435
1436 #define setup_blocks_span_initialize_undithered(texturing)                     \
1437
1438
1439 #define setup_blocks_span_initialize_shaded_textured()                         \
1440 {                                                                              \
1441   vec_4x32u block_span;                                                        \
1442   u32 offset = span_edge_data->left_x;                                         \
1443                                                                                \
1444   uvrg = *span_uvrg_offset;                                                    \
1445   mla_scalar_4x32b(uvrg, uvrg_dx, offset);                                     \
1446   b = *span_b_offset;                                                          \
1447   b += b_dx * offset;                                                          \
1448                                                                                \
1449   dup_4x32b(u_block, uvrg.e[0]);                                               \
1450   dup_4x32b(v_block, uvrg.e[1]);                                               \
1451   dup_4x32b(r_block, uvrg.e[2]);                                               \
1452   dup_4x32b(g_block, uvrg.e[3]);                                               \
1453   dup_4x32b(b_block, b);                                                       \
1454                                                                                \
1455   block_span = psx_gpu->u_block_span;                                          \
1456   add_4x32b(u_block, u_block, block_span);                                     \
1457   block_span = psx_gpu->v_block_span;                                          \
1458   add_4x32b(v_block, v_block, block_span);                                     \
1459   block_span = psx_gpu->r_block_span;                                          \
1460   add_4x32b(r_block, r_block, block_span);                                     \
1461   block_span = psx_gpu->g_block_span;                                          \
1462   add_4x32b(g_block, g_block, block_span);                                     \
1463   block_span = psx_gpu->b_block_span;                                          \
1464   add_4x32b(b_block, b_block, block_span);                                     \
1465 }
1466   
1467 #define setup_blocks_span_initialize_shaded_untextured()                       \
1468 {                                                                              \
1469   vec_4x32u block_span;                                                        \
1470   u32 offset = span_edge_data->left_x;                                         \
1471                                                                                \
1472   rgb.low = span_uvrg_offset->high;                                            \
1473   rgb.high.e[0] = *span_b_offset;                                              \
1474   mla_scalar_4x32b(rgb, rgb_dx, offset);                                       \
1475                                                                                \
1476   dup_4x32b(r_block, rgb.e[0]);                                                \
1477   dup_4x32b(g_block, rgb.e[1]);                                                \
1478   dup_4x32b(b_block, rgb.e[2]);                                                \
1479                                                                                \
1480   block_span = psx_gpu->r_block_span;                                          \
1481   add_4x32b(r_block, r_block, block_span);                                     \
1482   block_span = psx_gpu->g_block_span;                                          \
1483   add_4x32b(g_block, g_block, block_span);                                     \
1484   block_span = psx_gpu->b_block_span;                                          \
1485   add_4x32b(b_block, b_block, block_span);                                     \
1486 }                                                                              \
1487   
1488 #define setup_blocks_span_initialize_unshaded_textured()                       \
1489 {                                                                              \
1490   vec_4x32u block_span;                                                        \
1491   u32 offset = span_edge_data->left_x;                                         \
1492                                                                                \
1493   uv = span_uvrg_offset->low;                                                  \
1494   mla_scalar_2x32b(uv, uv_dx, offset);                                         \
1495                                                                                \
1496   dup_4x32b(u_block, uv.e[0]);                                                 \
1497   dup_4x32b(v_block, uv.e[1]);                                                 \
1498                                                                                \
1499   block_span = psx_gpu->u_block_span;                                          \
1500   add_4x32b(u_block, u_block, block_span);                                     \
1501   block_span = psx_gpu->v_block_span;                                          \
1502   add_4x32b(v_block, v_block, block_span);                                     \
1503 }                                                                              \
1504
1505 #define setup_blocks_span_initialize_unshaded_untextured()                     \
1506
1507
1508 #define setup_blocks_texture_swizzled()                                        \
1509 {                                                                              \
1510   vec_8x8u u_saved = u;                                                        \
1511   sli_8x8b(u, v, 4);                                                           \
1512   sri_8x8b(v, u_saved, 4);                                                     \
1513 }                                                                              \
1514
1515 #define setup_blocks_texture_unswizzled()                                      \
1516
1517 #define setup_blocks_store_shaded_textured(swizzling, dithering, target,       \
1518  edge_type)                                                                    \
1519 {                                                                              \
1520   vec_8x16u u_whole;                                                           \
1521   vec_8x16u v_whole;                                                           \
1522   vec_8x16u r_whole;                                                           \
1523   vec_8x16u g_whole;                                                           \
1524   vec_8x16u b_whole;                                                           \
1525                                                                                \
1526   vec_8x8u u;                                                                  \
1527   vec_8x8u v;                                                                  \
1528   vec_8x8u r;                                                                  \
1529   vec_8x8u g;                                                                  \
1530   vec_8x8u b;                                                                  \
1531   vec_8x16u uv;                                                                \
1532                                                                                \
1533   vec_4x32u dx4;                                                               \
1534   vec_4x32u dx8;                                                               \
1535                                                                                \
1536   shr_narrow_4x32b(u_whole.low, u_block, 16);                                  \
1537   shr_narrow_4x32b(v_whole.low, v_block, 16);                                  \
1538   shr_narrow_4x32b(r_whole.low, r_block, 16);                                  \
1539   shr_narrow_4x32b(g_whole.low, g_block, 16);                                  \
1540   shr_narrow_4x32b(b_whole.low, b_block, 16);                                  \
1541                                                                                \
1542   dup_4x32b(dx4, uvrg_dx4.e[0]);                                               \
1543   add_high_narrow_4x32b(u_whole.high, u_block, dx4);                           \
1544   dup_4x32b(dx4, uvrg_dx4.e[1]);                                               \
1545   add_high_narrow_4x32b(v_whole.high, v_block, dx4);                           \
1546   dup_4x32b(dx4, uvrg_dx4.e[2]);                                               \
1547   add_high_narrow_4x32b(r_whole.high, r_block, dx4);                           \
1548   dup_4x32b(dx4, uvrg_dx4.e[3]);                                               \
1549   add_high_narrow_4x32b(g_whole.high, g_block, dx4);                           \
1550   dup_4x32b(dx4, b_dx4);                                                       \
1551   add_high_narrow_4x32b(b_whole.high, b_block, dx4);                           \
1552                                                                                \
1553   mov_narrow_8x16b(u, u_whole);                                                \
1554   mov_narrow_8x16b(v, v_whole);                                                \
1555   mov_narrow_8x16b(r, r_whole);                                                \
1556   mov_narrow_8x16b(g, g_whole);                                                \
1557   mov_narrow_8x16b(b, b_whole);                                                \
1558                                                                                \
1559   dup_4x32b(dx8, uvrg_dx8.e[0]);                                               \
1560   add_4x32b(u_block, u_block, dx8);                                            \
1561   dup_4x32b(dx8, uvrg_dx8.e[1]);                                               \
1562   add_4x32b(v_block, v_block, dx8);                                            \
1563   dup_4x32b(dx8, uvrg_dx8.e[2]);                                               \
1564   add_4x32b(r_block, r_block, dx8);                                            \
1565   dup_4x32b(dx8, uvrg_dx8.e[3]);                                               \
1566   add_4x32b(g_block, g_block, dx8);                                            \
1567   dup_4x32b(dx8, b_dx8);                                                       \
1568   add_4x32b(b_block, b_block, dx8);                                            \
1569                                                                                \
1570   and_8x8b(u, u, texture_mask.low);                                            \
1571   and_8x8b(v, v, texture_mask.high);                                           \
1572   setup_blocks_texture_##swizzling();                                          \
1573                                                                                \
1574   zip_8x16b(uv, u, v);                                                         \
1575   block->uv = uv;                                                              \
1576   block->r = r;                                                                \
1577   block->g = g;                                                                \
1578   block->b = b;                                                                \
1579   block->dither_offsets = vector_cast(vec_8x16u, dither_offsets);              \
1580   block->fb_ptr = fb_ptr;                                                      \
1581 }                                                                              \
1582
1583 #define setup_blocks_store_unshaded_textured(swizzling, dithering, target,     \
1584  edge_type)                                                                    \
1585 {                                                                              \
1586   vec_8x16u u_whole;                                                           \
1587   vec_8x16u v_whole;                                                           \
1588                                                                                \
1589   vec_8x8u u;                                                                  \
1590   vec_8x8u v;                                                                  \
1591   vec_8x16u uv;                                                                \
1592                                                                                \
1593   vec_4x32u dx4;                                                               \
1594   vec_4x32u dx8;                                                               \
1595                                                                                \
1596   shr_narrow_4x32b(u_whole.low, u_block, 16);                                  \
1597   shr_narrow_4x32b(v_whole.low, v_block, 16);                                  \
1598                                                                                \
1599   dup_4x32b(dx4, uv_dx4.e[0]);                                                 \
1600   add_high_narrow_4x32b(u_whole.high, u_block, dx4);                           \
1601   dup_4x32b(dx4, uv_dx4.e[1]);                                                 \
1602   add_high_narrow_4x32b(v_whole.high, v_block, dx4);                           \
1603                                                                                \
1604   mov_narrow_8x16b(u, u_whole);                                                \
1605   mov_narrow_8x16b(v, v_whole);                                                \
1606                                                                                \
1607   dup_4x32b(dx8, uv_dx8.e[0]);                                                 \
1608   add_4x32b(u_block, u_block, dx8);                                            \
1609   dup_4x32b(dx8, uv_dx8.e[1]);                                                 \
1610   add_4x32b(v_block, v_block, dx8);                                            \
1611                                                                                \
1612   and_8x8b(u, u, texture_mask.low);                                            \
1613   and_8x8b(v, v, texture_mask.high);                                           \
1614   setup_blocks_texture_##swizzling();                                          \
1615                                                                                \
1616   zip_8x16b(uv, u, v);                                                         \
1617   block->uv = uv;                                                              \
1618   block->dither_offsets = vector_cast(vec_8x16u, dither_offsets);              \
1619   block->fb_ptr = fb_ptr;                                                      \
1620 }                                                                              \
1621
1622 #define setup_blocks_store_shaded_untextured_dithered()                        \
1623   addq_8x8b(r, r, dither_offsets);                                             \
1624   addq_8x8b(g, g, dither_offsets);                                             \
1625   addq_8x8b(b, b, dither_offsets);                                             \
1626                                                                                \
1627   subq_8x8b(r, r, d64_4);                                                      \
1628   subq_8x8b(g, g, d64_4);                                                      \
1629   subq_8x8b(b, b, d64_4)                                                       \
1630
1631 #define setup_blocks_store_shaded_untextured_undithered()                      \
1632   
1633
1634 #define setup_blocks_store_untextured_pixels_indirect_full(_pixels)            \
1635   block->pixels = _pixels;                                                     \
1636   block->fb_ptr = fb_ptr                                                       \
1637
1638 #define setup_blocks_store_untextured_pixels_indirect_edge(_pixels)            \
1639   block->pixels = _pixels;                                                     \
1640   block->fb_ptr = fb_ptr                                                       \
1641
1642 #define setup_blocks_store_shaded_untextured_seed_pixels_indirect()            \
1643   mul_long_8x8b(pixels, r, d64_1)                                              \
1644
1645
1646 #define setup_blocks_store_untextured_pixels_direct_full(_pixels)              \
1647   store_8x16b(_pixels, fb_ptr)                                                 \
1648
1649 #define setup_blocks_store_untextured_pixels_direct_edge(_pixels)              \
1650 {                                                                              \
1651   vec_8x16u fb_pixels;                                                         \
1652   vec_8x16u draw_mask;                                                         \
1653   vec_8x16u test_mask = psx_gpu->test_mask;                                    \
1654                                                                                \
1655   load_8x16b(fb_pixels, fb_ptr);                                               \
1656   dup_8x16b(draw_mask, span_edge_data->right_mask);                            \
1657   tst_8x16b(draw_mask, draw_mask, test_mask);                                  \
1658   bif_8x16b(fb_pixels, _pixels, draw_mask);                                    \
1659   store_8x16b(fb_pixels, fb_ptr);                                              \
1660 }                                                                              \
1661
1662 #define setup_blocks_store_shaded_untextured_seed_pixels_direct()              \
1663   pixels = msb_mask;                                                           \
1664   mla_long_8x8b(pixels, r, d64_1)                                              \
1665
1666
1667 #define setup_blocks_store_shaded_untextured(swizzling, dithering, target,     \
1668  edge_type)                                                                    \
1669 {                                                                              \
1670   vec_8x16u r_whole;                                                           \
1671   vec_8x16u g_whole;                                                           \
1672   vec_8x16u b_whole;                                                           \
1673                                                                                \
1674   vec_8x8u r;                                                                  \
1675   vec_8x8u g;                                                                  \
1676   vec_8x8u b;                                                                  \
1677                                                                                \
1678   vec_4x32u dx4;                                                               \
1679   vec_4x32u dx8;                                                               \
1680                                                                                \
1681   vec_8x16u pixels;                                                            \
1682                                                                                \
1683   shr_narrow_4x32b(r_whole.low, r_block, 16);                                  \
1684   shr_narrow_4x32b(g_whole.low, g_block, 16);                                  \
1685   shr_narrow_4x32b(b_whole.low, b_block, 16);                                  \
1686                                                                                \
1687   dup_4x32b(dx4, rgb_dx4.e[0]);                                                \
1688   add_high_narrow_4x32b(r_whole.high, r_block, dx4);                           \
1689   dup_4x32b(dx4, rgb_dx4.e[1]);                                                \
1690   add_high_narrow_4x32b(g_whole.high, g_block, dx4);                           \
1691   dup_4x32b(dx4, rgb_dx4.e[2]);                                                \
1692   add_high_narrow_4x32b(b_whole.high, b_block, dx4);                           \
1693                                                                                \
1694   mov_narrow_8x16b(r, r_whole);                                                \
1695   mov_narrow_8x16b(g, g_whole);                                                \
1696   mov_narrow_8x16b(b, b_whole);                                                \
1697                                                                                \
1698   dup_4x32b(dx8, rgb_dx8.e[0]);                                                \
1699   add_4x32b(r_block, r_block, dx8);                                            \
1700   dup_4x32b(dx8, rgb_dx8.e[1]);                                                \
1701   add_4x32b(g_block, g_block, dx8);                                            \
1702   dup_4x32b(dx8, rgb_dx8.e[2]);                                                \
1703   add_4x32b(b_block, b_block, dx8);                                            \
1704                                                                                \
1705   setup_blocks_store_shaded_untextured_##dithering();                          \
1706                                                                                \
1707   shr_8x8b(r, r, 3);                                                           \
1708   bic_8x8b(g, g, d64_0x07);                                                    \
1709   bic_8x8b(b, b, d64_0x07);                                                    \
1710                                                                                \
1711   setup_blocks_store_shaded_untextured_seed_pixels_##target();                 \
1712   mla_long_8x8b(pixels, g, d64_4);                                             \
1713   mla_long_8x8b(pixels, b, d64_128)                                            \
1714                                                                                \
1715   setup_blocks_store_untextured_pixels_##target##_##edge_type(pixels);         \
1716 }                                                                              \
1717
1718 #define setup_blocks_store_unshaded_untextured(swizzling, dithering, target,   \
1719  edge_type)                                                                    \
1720   setup_blocks_store_untextured_pixels_##target##_##edge_type(colors)          \
1721
1722
1723 #define setup_blocks_store_draw_mask_textured_indirect(_block, bits)           \
1724   (_block)->draw_mask_bits = bits                                              \
1725
1726 #define setup_blocks_store_draw_mask_untextured_indirect(_block, bits)         \
1727 {                                                                              \
1728   vec_8x16u bits_mask;                                                         \
1729   vec_8x16u test_mask = psx_gpu->test_mask;                                    \
1730   dup_8x16b(bits_mask, bits);                                                  \
1731   tst_8x16b(bits_mask, bits_mask, test_mask);                                  \
1732   (_block)->draw_mask = bits_mask;                                             \
1733 }                                                                              \
1734
1735 #define setup_blocks_store_draw_mask_untextured_direct(_block, bits)           \
1736
1737
1738 #define setup_blocks_add_blocks_indirect()                                     \
1739   num_blocks += span_num_blocks;                                               \
1740                                                                                \
1741   if(num_blocks > MAX_BLOCKS)                                                  \
1742   {                                                                            \
1743     psx_gpu->num_blocks = num_blocks - span_num_blocks;                        \
1744     flush_render_block_buffer(psx_gpu);                                        \
1745     num_blocks = span_num_blocks;                                              \
1746     block = psx_gpu->blocks;                                                   \
1747   }                                                                            \
1748
1749 #define setup_blocks_add_blocks_direct()                                       \
1750   texel_blocks_untextured += span_num_blocks;                                  \
1751   span_pixel_blocks += span_num_blocks                                         \
1752
1753
1754 #define setup_blocks_builder(shading, texturing, dithering, sw, target)        \
1755 void setup_blocks_##shading##_##texturing##_##dithering##_##sw##_##target(     \
1756  psx_gpu_struct *psx_gpu)                                                      \
1757 {                                                                              \
1758   setup_blocks_load_msb_mask_##target();                                       \
1759   setup_blocks_variables_##shading##_##texturing(target);                      \
1760                                                                                \
1761   edge_data_struct *span_edge_data = psx_gpu->span_edge_data;                  \
1762   vec_4x32u *span_uvrg_offset = psx_gpu->span_uvrg_offset;                     \
1763   u32 *span_b_offset = psx_gpu->span_b_offset;                                 \
1764                                                                                \
1765   block_struct *block = psx_gpu->blocks + psx_gpu->num_blocks;                 \
1766                                                                                \
1767   u32 num_spans = psx_gpu->num_spans;                                          \
1768                                                                                \
1769   u16 *fb_ptr;                                                                 \
1770   u32 y;                                                                       \
1771                                                                                \
1772   u32 num_blocks = psx_gpu->num_blocks;                                        \
1773   u32 span_num_blocks;                                                         \
1774                                                                                \
1775   while(num_spans)                                                             \
1776   {                                                                            \
1777     span_num_blocks = span_edge_data->num_blocks;                              \
1778     if(span_num_blocks)                                                        \
1779     {                                                                          \
1780       y = span_edge_data->y;                                                   \
1781       fb_ptr = psx_gpu->vram_ptr + span_edge_data->left_x + (y * 1024);        \
1782                                                                                \
1783       setup_blocks_span_initialize_##shading##_##texturing();                  \
1784       setup_blocks_span_initialize_##dithering(texturing);                     \
1785                                                                                \
1786       setup_blocks_add_blocks_##target();                                      \
1787                                                                                \
1788       s32 pixel_span = span_num_blocks * 8;                                    \
1789       pixel_span -= __builtin_popcount(span_edge_data->right_mask & 0xFF);     \
1790       span_pixels += pixel_span;                                               \
1791                                                                                \
1792       span_num_blocks--;                                                       \
1793       while(span_num_blocks)                                                   \
1794       {                                                                        \
1795         setup_blocks_store_##shading##_##texturing(sw, dithering, target,      \
1796          full);                                                                \
1797         setup_blocks_store_draw_mask_##texturing##_##target(block, 0x00);      \
1798                                                                                \
1799         fb_ptr += 8;                                                           \
1800         block++;                                                               \
1801         span_num_blocks--;                                                     \
1802       }                                                                        \
1803                                                                                \
1804       setup_blocks_store_##shading##_##texturing(sw, dithering, target, edge); \
1805       setup_blocks_store_draw_mask_##texturing##_##target(block,               \
1806        span_edge_data->right_mask);                                            \
1807                                                                                \
1808       block++;                                                                 \
1809     }                                                                          \
1810     else                                                                       \
1811     {                                                                          \
1812       zero_block_spans++;                                                      \
1813     }                                                                          \
1814                                                                                \
1815     num_spans--;                                                               \
1816     span_edge_data++;                                                          \
1817     span_uvrg_offset++;                                                        \
1818     span_b_offset++;                                                           \
1819   }                                                                            \
1820                                                                                \
1821   psx_gpu->num_blocks = num_blocks;                                            \
1822 }                                                                              \
1823
1824 void setup_blocks_shaded_textured_dithered_unswizzled_indirect(psx_gpu_struct
1825  *psx_gpu);
1826
1827 void setup_blocks_shaded_untextured_dithered_unswizzled_indirect(psx_gpu_struct
1828  *psx_gpu);
1829 void setup_blocks_shaded_untextured_undithered_unswizzled_indirect(
1830  psx_gpu_struct *psx_gpu);
1831 void setup_blocks_shaded_untextured_dithered_unswizzled_direct(psx_gpu_struct
1832  *psx_gpu);
1833 void setup_blocks_shaded_untextured_undithered_unswizzled_direct(
1834  psx_gpu_struct *psx_gpu);
1835
1836 void setup_blocks_unshaded_textured_dithered_unswizzled_indirect(psx_gpu_struct
1837  *psx_gpu);
1838 void setup_blocks_unshaded_untextured_undithered_unswizzled_indirect(
1839  psx_gpu_struct *psx_gpu);
1840 void setup_blocks_unshaded_untextured_undithered_unswizzled_direct(
1841  psx_gpu_struct *psx_gpu);
1842
1843 void setup_blocks_shaded_textured_dithered_swizzled_indirect(psx_gpu_struct
1844  *psx_gpu);
1845 void setup_blocks_unshaded_textured_dithered_swizzled_indirect(psx_gpu_struct
1846  *psx_gpu);
1847
1848
1849 //setup_blocks_builder(unshaded, untextured, undithered, unswizzled, direct);
1850
1851 #ifndef NEON_BUILD
1852
1853 setup_blocks_builder(shaded, textured, dithered, swizzled, indirect);
1854 setup_blocks_builder(shaded, textured, dithered, unswizzled, indirect);
1855
1856 setup_blocks_builder(unshaded, textured, dithered, unswizzled, indirect);
1857 setup_blocks_builder(unshaded, textured, dithered, swizzled, indirect);
1858
1859 setup_blocks_builder(shaded, untextured, undithered, unswizzled, indirect);
1860 setup_blocks_builder(shaded, untextured, dithered, unswizzled, indirect);
1861 setup_blocks_builder(shaded, untextured, undithered, unswizzled, direct);
1862 setup_blocks_builder(shaded, untextured, dithered, unswizzled, direct);
1863
1864 setup_blocks_builder(unshaded, untextured, undithered, unswizzled, indirect);
1865 setup_blocks_builder(unshaded, untextured, undithered, unswizzled, direct);
1866
1867 #endif
1868
1869 void texture_blocks_untextured(psx_gpu_struct *psx_gpu);
1870 void texture_blocks_4bpp(psx_gpu_struct *psx_gpu);
1871 void texture_blocks_8bpp(psx_gpu_struct *psx_gpu);
1872 void texture_blocks_16bpp(psx_gpu_struct *psx_gpu);
1873
1874 #ifndef NEON_BUILD
1875
1876 void texture_blocks_untextured(psx_gpu_struct *psx_gpu)
1877 {
1878   if(psx_gpu->primitive_type != PRIMITIVE_TYPE_SPRITE)
1879     texel_blocks_untextured += psx_gpu->num_blocks;
1880 }
1881
1882 void texture_blocks_4bpp(psx_gpu_struct *psx_gpu)
1883 {
1884   block_struct *block = psx_gpu->blocks;
1885   u32 num_blocks = psx_gpu->num_blocks;
1886   texel_blocks_4bpp += num_blocks;
1887
1888   vec_8x8u texels_low;
1889   vec_8x8u texels_high;
1890   vec_8x8u texels;
1891   vec_8x16u pixels;
1892
1893   vec_8x16u clut_a;
1894   vec_8x16u clut_b;
1895   vec_16x8u clut_low;
1896   vec_16x8u clut_high;
1897
1898   u8 *texture_ptr_8bpp = psx_gpu->texture_page_ptr;
1899   u16 *clut_ptr = psx_gpu->clut_ptr;
1900
1901   // Can be done with one deinterleaving load on NEON
1902   load_8x16b(clut_a, clut_ptr);
1903   load_8x16b(clut_b, clut_ptr + 8);
1904   unzip_16x8b(clut_low, clut_high, clut_a, clut_b);
1905
1906   if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_4bpp_mask)
1907     update_texture_4bpp_cache(psx_gpu);
1908
1909   while(num_blocks)
1910   {
1911     texels.e[0] = texture_ptr_8bpp[block->uv.e[0]];
1912     texels.e[1] = texture_ptr_8bpp[block->uv.e[1]];
1913     texels.e[2] = texture_ptr_8bpp[block->uv.e[2]];
1914     texels.e[3] = texture_ptr_8bpp[block->uv.e[3]];
1915     texels.e[4] = texture_ptr_8bpp[block->uv.e[4]];
1916     texels.e[5] = texture_ptr_8bpp[block->uv.e[5]];
1917     texels.e[6] = texture_ptr_8bpp[block->uv.e[6]];
1918     texels.e[7] = texture_ptr_8bpp[block->uv.e[7]];
1919
1920     tbl_16(texels_low, texels, clut_low);
1921     tbl_16(texels_high, texels, clut_high);
1922
1923     // Can be done with an interleaving store on NEON
1924     zip_8x16b(pixels, texels_low, texels_high);
1925
1926     block->texels = pixels;
1927
1928     num_blocks--;
1929     block++;
1930   }
1931 }
1932
1933 void texture_blocks_8bpp(psx_gpu_struct *psx_gpu)
1934 {
1935   block_struct *block = psx_gpu->blocks;
1936   u32 num_blocks = psx_gpu->num_blocks;
1937
1938   texel_blocks_8bpp += num_blocks;
1939
1940   if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_8bpp_mask)
1941     update_texture_8bpp_cache(psx_gpu);
1942
1943   vec_8x16u texels;
1944   u8 *texture_ptr_8bpp = psx_gpu->texture_page_ptr;
1945
1946   u32 texel;
1947   u32 offset;
1948   u32 i;
1949
1950   while(num_blocks)
1951   {
1952     for(i = 0; i < 8; i++)
1953     {
1954       offset = block->uv.e[i];
1955
1956       texel = texture_ptr_8bpp[offset];
1957       texels.e[i] = psx_gpu->clut_ptr[texel];
1958     }
1959
1960     block->texels = texels;
1961
1962     num_blocks--;
1963     block++;
1964   }
1965 }
1966
1967 void texture_blocks_16bpp(psx_gpu_struct *psx_gpu)
1968 {
1969   block_struct *block = psx_gpu->blocks;
1970   u32 num_blocks = psx_gpu->num_blocks;
1971
1972   texel_blocks_16bpp += num_blocks;
1973
1974   vec_8x16u texels;
1975
1976   u16 *texture_ptr_16bpp = psx_gpu->texture_page_ptr;
1977   u32 offset;
1978   u32 i;
1979
1980   while(num_blocks)
1981   {
1982     for(i = 0; i < 8; i++)
1983     {
1984       offset = block->uv.e[i];
1985       offset += ((offset & 0xFF00) * 3);
1986
1987       texels.e[i] = texture_ptr_16bpp[offset];
1988     }
1989
1990     block->texels = texels;
1991
1992     num_blocks--;
1993     block++;
1994   }
1995 }
1996
1997 #endif
1998
1999
2000 #define shade_blocks_load_msb_mask_indirect()                                  \
2001
2002 #define shade_blocks_load_msb_mask_direct()                                    \
2003   vec_8x16u msb_mask;                                                          \
2004   dup_8x16b(msb_mask, psx_gpu->mask_msb);                                      \
2005
2006 #define shade_blocks_store_indirect(_draw_mask, _pixels)                       \
2007   block->draw_mask = _draw_mask;                                               \
2008   block->pixels = _pixels                                                      \
2009
2010 #define shade_blocks_store_direct(_draw_mask, _pixels)                         \
2011 {                                                                              \
2012   vec_8x16u fb_pixels;                                                         \
2013   or_8x16b(_pixels, _pixels, msb_mask);                                        \
2014   load_8x16b(fb_pixels, block->fb_ptr);                                        \
2015   bif_8x16b(fb_pixels, _pixels, _draw_mask);                                   \
2016   store_8x16b(fb_pixels, block->fb_ptr);                                       \
2017 }                                                                              \
2018
2019
2020 #define shade_blocks_textured_false_modulated_check_dithered(target)           \
2021   if(psx_gpu->triangle_color == 0x808080)                                      \
2022   {                                                                            \
2023     false_modulated_blocks += num_blocks;                                      \
2024   }                                                                            \
2025
2026 #define shade_blocks_textured_false_modulated_check_undithered(target)         \
2027   if(psx_gpu->triangle_color == 0x808080)                                      \
2028   {                                                                            \
2029                                                                                \
2030     shade_blocks_textured_unmodulated_##target(psx_gpu);                       \
2031     false_modulated_blocks += num_blocks;                                      \
2032     return;                                                                    \
2033   }                                                                            \
2034
2035
2036 #define shade_blocks_textured_modulated_shaded_primitive_load(dithering,       \
2037  target)                                                                       \
2038
2039 #define shade_blocks_textured_modulated_unshaded_primitive_load(dithering,     \
2040  target)                                                                       \
2041 {                                                                              \
2042   u32 color = psx_gpu->triangle_color;                                         \
2043   dup_8x8b(colors_r, color);                                                   \
2044   dup_8x8b(colors_g, color >> 8);                                              \
2045   dup_8x8b(colors_b, color >> 16);                                             \
2046   shade_blocks_textured_false_modulated_check_##dithering(target);             \
2047 }                                                                              \
2048
2049 #define shade_blocks_textured_modulated_shaded_block_load()                    \
2050   colors_r = block->r;                                                         \
2051   colors_g = block->g;                                                         \
2052   colors_b = block->b                                                          \
2053
2054 #define shade_blocks_textured_modulated_unshaded_block_load()                  \
2055
2056 #define shade_blocks_textured_modulate_dithered(component)                     \
2057   pixels_##component = block->dither_offsets;                                  \
2058   mla_long_8x8b(pixels_##component, texels_##component, colors_##component)    \
2059
2060 #define shade_blocks_textured_modulate_undithered(component)                   \
2061   mul_long_8x8b(pixels_##component, texels_##component, colors_##component)    \
2062
2063 #define shade_blocks_textured_modulated_builder(shading, dithering, target)    \
2064 void shade_blocks_##shading##_textured_modulated_##dithering##_##target(       \
2065  psx_gpu_struct *psx_gpu)                                                      \
2066 {                                                                              \
2067   block_struct *block = psx_gpu->blocks;                                       \
2068   u32 num_blocks = psx_gpu->num_blocks;                                        \
2069   vec_8x16u texels;                                                            \
2070                                                                                \
2071   vec_8x8u texels_r;                                                           \
2072   vec_8x8u texels_g;                                                           \
2073   vec_8x8u texels_b;                                                           \
2074                                                                                \
2075   vec_8x8u colors_r;                                                           \
2076   vec_8x8u colors_g;                                                           \
2077   vec_8x8u colors_b;                                                           \
2078                                                                                \
2079   vec_8x8u pixels_r_low;                                                       \
2080   vec_8x8u pixels_g_low;                                                       \
2081   vec_8x8u pixels_b_low;                                                       \
2082   vec_8x16u pixels;                                                            \
2083                                                                                \
2084   vec_8x16u pixels_r;                                                          \
2085   vec_8x16u pixels_g;                                                          \
2086   vec_8x16u pixels_b;                                                          \
2087                                                                                \
2088   vec_8x16u draw_mask;                                                         \
2089   vec_8x16u zero_mask;                                                         \
2090                                                                                \
2091   vec_8x8u d64_0x07;                                                           \
2092   vec_8x8u d64_0x1F;                                                           \
2093   vec_8x8u d64_1;                                                              \
2094   vec_8x8u d64_4;                                                              \
2095   vec_8x8u d64_128;                                                            \
2096                                                                                \
2097   vec_8x16u d128_0x8000;                                                       \
2098                                                                                \
2099   vec_8x16u test_mask = psx_gpu->test_mask;                                    \
2100   u32 draw_mask_bits;                                                          \
2101   shade_blocks_load_msb_mask_##target();                                       \
2102                                                                                \
2103   dup_8x8b(d64_0x07, 0x07);                                                    \
2104   dup_8x8b(d64_0x1F, 0x1F);                                                    \
2105   dup_8x8b(d64_1, 1);                                                          \
2106   dup_8x8b(d64_4, 4);                                                          \
2107   dup_8x8b(d64_128, 128);                                                      \
2108                                                                                \
2109   dup_8x16b(d128_0x8000, 0x8000);                                              \
2110                                                                                \
2111   shade_blocks_textured_modulated_##shading##_primitive_load(dithering,        \
2112    target);                                                                    \
2113                                                                                \
2114   while(num_blocks)                                                            \
2115   {                                                                            \
2116     draw_mask_bits = block->draw_mask_bits;                                    \
2117     dup_8x16b(draw_mask, draw_mask_bits);                                      \
2118     tst_8x16b(draw_mask, draw_mask, test_mask);                                \
2119                                                                                \
2120     shade_blocks_textured_modulated_##shading##_block_load();                  \
2121                                                                                \
2122     texels = block->texels;                                                    \
2123                                                                                \
2124     mov_narrow_8x16b(texels_r, texels);                                        \
2125     shr_narrow_8x16b(texels_g, texels, 5);                                     \
2126     shr_narrow_8x16b(texels_b, texels, 7);                                     \
2127                                                                                \
2128     and_8x8b(texels_r, texels_r, d64_0x1F);                                    \
2129     and_8x8b(texels_g, texels_g, d64_0x1F);                                    \
2130     shr_8x8b(texels_b, texels_b, 3);                                           \
2131                                                                                \
2132     shade_blocks_textured_modulate_##dithering(r);                             \
2133     shade_blocks_textured_modulate_##dithering(g);                             \
2134     shade_blocks_textured_modulate_##dithering(b);                             \
2135                                                                                \
2136     cmpeqz_8x16b(zero_mask, texels);                                           \
2137     and_8x16b(pixels, texels, d128_0x8000);                                    \
2138                                                                                \
2139     shrq_narrow_signed_8x16b(pixels_r_low, pixels_r, 4);                       \
2140     shrq_narrow_signed_8x16b(pixels_g_low, pixels_g, 4);                       \
2141     shrq_narrow_signed_8x16b(pixels_b_low, pixels_b, 4);                       \
2142                                                                                \
2143     or_8x16b(zero_mask, draw_mask, zero_mask);                                 \
2144                                                                                \
2145     shr_8x8b(pixels_r_low, pixels_r_low, 3);                                   \
2146     bic_8x8b(pixels_g_low, pixels_g_low, d64_0x07);                            \
2147     bic_8x8b(pixels_b_low, pixels_b_low, d64_0x07);                            \
2148                                                                                \
2149     mla_long_8x8b(pixels, pixels_r_low, d64_1);                                \
2150     mla_long_8x8b(pixels, pixels_g_low, d64_4);                                \
2151     mla_long_8x8b(pixels, pixels_b_low, d64_128);                              \
2152                                                                                \
2153     shade_blocks_store_##target(zero_mask, pixels);                            \
2154                                                                                \
2155     num_blocks--;                                                              \
2156     block++;                                                                   \
2157   }                                                                            \
2158 }                                                                              \
2159
2160 void shade_blocks_shaded_textured_modulated_dithered_direct(psx_gpu_struct
2161  *psx_gpu);
2162 void shade_blocks_shaded_textured_modulated_undithered_direct(psx_gpu_struct
2163  *psx_gpu);
2164 void shade_blocks_unshaded_textured_modulated_dithered_direct(psx_gpu_struct
2165  *psx_gpu);
2166 void shade_blocks_unshaded_textured_modulated_undithered_direct(psx_gpu_struct
2167  *psx_gpu);
2168
2169 void shade_blocks_shaded_textured_modulated_dithered_indirect(psx_gpu_struct
2170  *psx_gpu);
2171 void shade_blocks_shaded_textured_modulated_undithered_indirect(psx_gpu_struct
2172  *psx_gpu);
2173 void shade_blocks_unshaded_textured_modulated_dithered_indirect(psx_gpu_struct
2174  *psx_gpu);
2175 void shade_blocks_unshaded_textured_modulated_undithered_indirect(psx_gpu_struct
2176  *psx_gpu);
2177
2178 void shade_blocks_textured_unmodulated_indirect(psx_gpu_struct *psx_gpu);
2179 void shade_blocks_textured_unmodulated_direct(psx_gpu_struct *psx_gpu);
2180
2181 #ifndef NEON_BUILD
2182
2183 shade_blocks_textured_modulated_builder(shaded, dithered, direct);
2184 shade_blocks_textured_modulated_builder(shaded, undithered, direct);
2185 shade_blocks_textured_modulated_builder(unshaded, dithered, direct);
2186 shade_blocks_textured_modulated_builder(unshaded, undithered, direct);
2187
2188 shade_blocks_textured_modulated_builder(shaded, dithered, indirect);
2189 shade_blocks_textured_modulated_builder(shaded, undithered, indirect);
2190 shade_blocks_textured_modulated_builder(unshaded, dithered, indirect);
2191 shade_blocks_textured_modulated_builder(unshaded, undithered, indirect);
2192
2193 #endif
2194
2195
2196 #define shade_blocks_textured_unmodulated_builder(target)                      \
2197 void shade_blocks_textured_unmodulated_##target(psx_gpu_struct *psx_gpu)       \
2198 {                                                                              \
2199   block_struct *block = psx_gpu->blocks;                                       \
2200   u32 num_blocks = psx_gpu->num_blocks;                                        \
2201   vec_8x16u draw_mask;                                                         \
2202   vec_8x16u test_mask = psx_gpu->test_mask;                                    \
2203   u32 draw_mask_bits;                                                          \
2204                                                                                \
2205   vec_8x16u pixels;                                                            \
2206   shade_blocks_load_msb_mask_##target();                                       \
2207                                                                                \
2208   while(num_blocks)                                                            \
2209   {                                                                            \
2210     vec_8x16u zero_mask;                                                       \
2211                                                                                \
2212     draw_mask_bits = block->draw_mask_bits;                                    \
2213     dup_8x16b(draw_mask, draw_mask_bits);                                      \
2214     tst_8x16b(draw_mask, draw_mask, test_mask);                                \
2215                                                                                \
2216     pixels = block->texels;                                                    \
2217                                                                                \
2218     cmpeqz_8x16b(zero_mask, pixels);                                           \
2219     or_8x16b(zero_mask, draw_mask, zero_mask);                                 \
2220                                                                                \
2221     shade_blocks_store_##target(zero_mask, pixels);                            \
2222                                                                                \
2223     num_blocks--;                                                              \
2224     block++;                                                                   \
2225   }                                                                            \
2226 }                                                                              \
2227
2228 #define shade_blocks_textured_unmodulated_dithered_builder(target)             \
2229 void shade_blocks_textured_unmodulated_dithered_##target(psx_gpu_struct        \
2230  *psx_gpu)                                                                     \
2231 {                                                                              \
2232   block_struct *block = psx_gpu->blocks;                                       \
2233   u32 num_blocks = psx_gpu->num_blocks;                                        \
2234   vec_8x16u draw_mask;                                                         \
2235   vec_8x16u test_mask = psx_gpu->test_mask;                                    \
2236   u32 draw_mask_bits;                                                          \
2237                                                                                \
2238   vec_8x16u pixels;                                                            \
2239   shade_blocks_load_msb_mask_##target();                                       \
2240                                                                                \
2241   while(num_blocks)                                                            \
2242   {                                                                            \
2243     vec_8x16u zero_mask;                                                       \
2244                                                                                \
2245     draw_mask_bits = block->draw_mask_bits;                                    \
2246     dup_8x16b(draw_mask, draw_mask_bits);                                      \
2247     tst_8x16b(draw_mask, draw_mask, test_mask);                                \
2248                                                                                \
2249     pixels = block->texels;                                                    \
2250                                                                                \
2251     cmpeqz_8x16b(zero_mask, pixels);                                           \
2252     or_8x16b(zero_mask, draw_mask, zero_mask);                                 \
2253                                                                                \
2254     shade_blocks_store_##target(zero_mask, pixels);                            \
2255                                                                                \
2256     num_blocks--;                                                              \
2257     block++;                                                                   \
2258   }                                                                            \
2259 }                                                                              \
2260
2261 #ifndef NEON_BUILD
2262
2263 shade_blocks_textured_unmodulated_builder(indirect)
2264 shade_blocks_textured_unmodulated_builder(direct)
2265
2266 #endif
2267
2268
2269 void shade_blocks_unshaded_untextured_indirect(psx_gpu_struct *psx_gpu);
2270 void shade_blocks_unshaded_untextured_direct(psx_gpu_struct *psx_gpu);
2271
2272 #ifndef NEON_BUILD
2273                                                                                
2274 void shade_blocks_unshaded_untextured_indirect(psx_gpu_struct *psx_gpu)
2275 {
2276 }
2277
2278 void shade_blocks_unshaded_untextured_direct(psx_gpu_struct *psx_gpu)
2279 {
2280   block_struct *block = psx_gpu->blocks;
2281   u32 num_blocks = psx_gpu->num_blocks;
2282
2283   vec_8x16u pixels = block->pixels;
2284   shade_blocks_load_msb_mask_direct();
2285
2286   while(num_blocks)
2287   {
2288     shade_blocks_store_direct(block->draw_mask, pixels);
2289
2290     num_blocks--;
2291     block++;
2292   }
2293 }
2294
2295 #endif
2296
2297 void shade_blocks_shaded_untextured(psx_gpu_struct *psx_gpu)
2298 {
2299 }
2300
2301
2302 #define blend_blocks_mask_evaluate_on()                                        \
2303   vec_8x16u mask_pixels;                                                       \
2304   cmpltz_8x16b(mask_pixels, framebuffer_pixels);                               \
2305   or_8x16b(draw_mask, draw_mask, mask_pixels)                                  \
2306
2307 #define blend_blocks_mask_evaluate_off()                                       \
2308
2309 #define blend_blocks_average()                                                 \
2310 {                                                                              \
2311   vec_8x16u pixels_no_msb;                                                     \
2312   vec_8x16u fb_pixels_no_msb;                                                  \
2313                                                                                \
2314   vec_8x16u d128_0x0421;                                                       \
2315   vec_8x16u d128_0x8000;                                                       \
2316                                                                                \
2317   dup_8x16b(d128_0x0421, 0x0421);                                              \
2318   dup_8x16b(d128_0x8000, 0x8000);                                              \
2319                                                                                \
2320   eor_8x16b(blend_pixels, pixels, framebuffer_pixels);                         \
2321   bic_8x16b(pixels_no_msb, pixels, d128_0x8000);                               \
2322   and_8x16b(blend_pixels, blend_pixels, d128_0x0421);                          \
2323   sub_8x16b(blend_pixels, pixels_no_msb, blend_pixels);                        \
2324   bic_8x16b(fb_pixels_no_msb, framebuffer_pixels, d128_0x8000);                \
2325   average_8x16b(blend_pixels, fb_pixels_no_msb, blend_pixels);                 \
2326 }                                                                              \
2327
2328 #define blend_blocks_add()                                                     \
2329 {                                                                              \
2330   vec_8x16u pixels_rb, pixels_g;                                               \
2331   vec_8x16u fb_rb, fb_g;                                                       \
2332                                                                                \
2333   vec_8x16u d128_0x7C1F;                                                       \
2334   vec_8x16u d128_0x03E0;                                                       \
2335                                                                                \
2336   dup_8x16b(d128_0x7C1F, 0x7C1F);                                              \
2337   dup_8x16b(d128_0x03E0, 0x03E0);                                              \
2338                                                                                \
2339   and_8x16b(pixels_rb, pixels, d128_0x7C1F);                                   \
2340   and_8x16b(pixels_g, pixels, d128_0x03E0);                                    \
2341                                                                                \
2342   and_8x16b(fb_rb, framebuffer_pixels, d128_0x7C1F);                           \
2343   and_8x16b(fb_g, framebuffer_pixels, d128_0x03E0);                            \
2344                                                                                \
2345   add_8x16b(fb_rb, fb_rb, pixels_rb);                                          \
2346   add_8x16b(fb_g, fb_g, pixels_g);                                             \
2347                                                                                \
2348   min_16x8b(vector_cast(vec_16x8u, fb_rb), vector_cast(vec_16x8u, fb_rb),      \
2349    vector_cast(vec_16x8u, d128_0x7C1F));                                       \
2350   min_8x16b(fb_g, fb_g, d128_0x03E0);                                          \
2351                                                                                \
2352   or_8x16b(blend_pixels, fb_rb, fb_g);                                         \
2353 }                                                                              \
2354
2355 #define blend_blocks_subtract()                                                \
2356 {                                                                              \
2357   vec_8x16u pixels_rb, pixels_g;                                               \
2358   vec_8x16u fb_rb, fb_g;                                                       \
2359                                                                                \
2360   vec_8x16u d128_0x7C1F;                                                       \
2361   vec_8x16u d128_0x03E0;                                                       \
2362                                                                                \
2363   dup_8x16b(d128_0x7C1F, 0x7C1F);                                              \
2364   dup_8x16b(d128_0x03E0, 0x03E0);                                              \
2365                                                                                \
2366   and_8x16b(pixels_rb, pixels, d128_0x7C1F);                                   \
2367   and_8x16b(pixels_g, pixels, d128_0x03E0);                                    \
2368                                                                                \
2369   and_8x16b(fb_rb, framebuffer_pixels, d128_0x7C1F);                           \
2370   and_8x16b(fb_g, framebuffer_pixels, d128_0x03E0);                            \
2371                                                                                \
2372   subs_16x8b(vector_cast(vec_16x8u, fb_rb),                                    \
2373    vector_cast(vec_16x8u, fb_rb), vector_cast(vec_16x8u, pixels_rb));          \
2374   subs_8x16b(fb_g, fb_g, pixels_g);                                            \
2375                                                                                \
2376   or_8x16b(blend_pixels, fb_rb, fb_g);                                         \
2377 }                                                                              \
2378
2379 #define blend_blocks_add_fourth()                                              \
2380 {                                                                              \
2381   vec_8x16u pixels_rb, pixels_g;                                               \
2382   vec_8x16u pixels_fourth;                                                     \
2383   vec_8x16u fb_rb, fb_g;                                                       \
2384                                                                                \
2385   vec_8x16u d128_0x7C1F;                                                       \
2386   vec_8x16u d128_0x1C07;                                                       \
2387   vec_8x16u d128_0x03E0;                                                       \
2388   vec_8x16u d128_0x00E0;                                                       \
2389                                                                                \
2390   dup_8x16b(d128_0x7C1F, 0x7C1F);                                              \
2391   dup_8x16b(d128_0x1C07, 0x1C07);                                              \
2392   dup_8x16b(d128_0x03E0, 0x03E0);                                              \
2393   dup_8x16b(d128_0x00E0, 0x00E0);                                              \
2394                                                                                \
2395   shr_8x16b(pixels_fourth, vector_cast(vec_8x16s, pixels), 2);                 \
2396                                                                                \