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