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