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