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