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