psx_gpu: workaround overflow crash
[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) \
b7569147 772{ \
773 u32 _num_spans = &span_edge_data_element - psx_gpu->span_edge_data; \
774 if (_num_spans > MAX_SPANS) \
775 *(int *)0 = 1; \
776 if (_num_spans < psx_gpu->num_spans) \
c111e8f8 777 { \
778 if(span_edge_data_element.num_blocks > MAX_BLOCKS_PER_ROW) \
779 *(int *)0 = 1; \
780 if(span_edge_data_element.y > 2048) \
781 *(int *)0 = 1; \
782 } \
b7569147 783} \
c111e8f8 784
785#else
786#define setup_spans_debug_check(span_edge_data_element) \
787
788#endif
789
75e28f62
E
790#define setup_spans_prologue_alternate_yes() \
791 vec_2x64s alternate_x; \
792 vec_2x64s alternate_dx_dy; \
793 vec_4x32s alternate_x_32; \
794 vec_2x32s alternate_x_16; \
795 \
796 vec_4x16u alternate_select; \
797 vec_4x16s y_mid_point; \
798 \
799 s32 y_b = v_b->y; \
800 s64 edge_alt; \
801 s32 edge_dx_dy_alt; \
802 u32 edge_shift_alt \
803
804#define setup_spans_prologue_alternate_no() \
805
806#define setup_spans_prologue(alternate_active) \
807 edge_data_struct *span_edge_data; \
808 vec_4x32u *span_uvrg_offset; \
809 u32 *span_b_offset; \
810 \
811 s32 clip; \
812 \
813 vec_2x64s edges_xy; \
814 vec_2x32s edges_dx_dy; \
815 vec_2x32u edge_shifts; \
816 \
817 vec_2x64s left_x, right_x; \
818 vec_2x64s left_dx_dy, right_dx_dy; \
819 vec_4x32s left_x_32, right_x_32; \
820 vec_8x16s left_right_x_16; \
821 vec_4x16s y_x4; \
822 vec_8x16s left_edge; \
823 vec_8x16s right_edge; \
824 vec_4x16u span_shift; \
825 \
826 vec_2x32u c_0x01; \
827 vec_4x16u c_0x04; \
828 vec_4x16u c_0xFFFE; \
829 vec_4x16u c_0x07; \
830 \
831 vec_2x32s x_starts; \
832 vec_2x32s x_ends; \
833 \
834 s32 x_a = v_a->x; \
835 s32 x_b = v_b->x; \
836 s32 x_c = v_c->x; \
837 s32 y_a = v_a->y; \
838 s32 y_c = v_c->y; \
839 \
840 vec_4x32u uvrg = psx_gpu->uvrg; \
841 vec_4x32u uvrg_dy = psx_gpu->uvrg_dy; \
842 u32 b = psx_gpu->b; \
843 u32 b_dy = psx_gpu->b_dy; \
844 \
845 dup_2x32b(c_0x01, 0x01); \
846 setup_spans_prologue_alternate_##alternate_active() \
847
848#define setup_spans_prologue_b() \
849 span_edge_data = psx_gpu->span_edge_data; \
850 span_uvrg_offset = psx_gpu->span_uvrg_offset; \
851 span_b_offset = psx_gpu->span_b_offset; \
852 \
853 vec_8x16u c_0x0001; \
854 \
855 dup_8x16b(c_0x0001, 0x0001); \
856 dup_8x16b(left_edge, psx_gpu->viewport_start_x); \
857 dup_8x16b(right_edge, psx_gpu->viewport_end_x); \
858 add_8x16b(right_edge, right_edge, c_0x0001); \
859 dup_4x16b(c_0x04, 0x04); \
860 dup_4x16b(c_0x07, 0x07); \
861 dup_4x16b(c_0xFFFE, 0xFFFE); \
862
863
864#define compute_edge_delta_x2() \
865{ \
866 vec_2x32s heights; \
867 vec_2x32s height_reciprocals; \
868 vec_2x32s heights_b; \
869 vec_4x32u widths; \
870 \
871 u32 edge_shift = reciprocal_table[height]; \
872 \
873 dup_2x32b(heights, height); \
874 sub_2x32b(widths, x_ends, x_starts); \
875 \
876 dup_2x32b(edge_shifts, edge_shift); \
877 sub_2x32b(heights_b, heights, c_0x01); \
7d5140f5 878 shr_2x32b(height_reciprocals, edge_shifts, 10); \
75e28f62
E
879 \
880 mla_2x32b(heights_b, x_starts, heights); \
881 bic_immediate_4x16b(vector_cast(vec_4x16u, edge_shifts), 0xE0); \
882 mul_2x32b(edges_dx_dy, widths, height_reciprocals); \
883 mul_long_2x32b(edges_xy, heights_b, height_reciprocals); \
884} \
885
886#define compute_edge_delta_x3(start_c, height_a, height_b) \
887{ \
888 vec_2x32s heights; \
889 vec_2x32s height_reciprocals; \
890 vec_2x32s heights_b; \
891 vec_2x32u widths; \
892 \
893 u32 width_alt; \
894 s32 height_b_alt; \
895 u32 height_reciprocal_alt; \
896 \
897 heights.e[0] = height_a; \
898 heights.e[1] = height_b; \
899 \
900 edge_shifts.e[0] = reciprocal_table[height_a]; \
901 edge_shifts.e[1] = reciprocal_table[height_b]; \
902 edge_shift_alt = reciprocal_table[height_minor_b]; \
903 \
904 sub_2x32b(widths, x_ends, x_starts); \
905 width_alt = x_c - start_c; \
906 \
7d5140f5
E
907 shr_2x32b(height_reciprocals, edge_shifts, 10); \
908 height_reciprocal_alt = edge_shift_alt >> 10; \
75e28f62
E
909 \
910 bic_immediate_4x16b(vector_cast(vec_4x16u, edge_shifts), 0xE0); \
911 edge_shift_alt &= 0x1F; \
912 \
913 sub_2x32b(heights_b, heights, c_0x01); \
914 height_b_alt = height_minor_b - 1; \
915 \
916 mla_2x32b(heights_b, x_starts, heights); \
917 height_b_alt += height_minor_b * start_c; \
918 \
919 mul_long_2x32b(edges_xy, heights_b, height_reciprocals); \
920 edge_alt = (s64)height_b_alt * height_reciprocal_alt; \
921 \
922 mul_2x32b(edges_dx_dy, widths, height_reciprocals); \
923 edge_dx_dy_alt = width_alt * height_reciprocal_alt; \
924} \
925
926
927#define setup_spans_adjust_y_up() \
928 sub_4x32b(y_x4, y_x4, c_0x04) \
929
930#define setup_spans_adjust_y_down() \
931 add_4x32b(y_x4, y_x4, c_0x04) \
932
933#define setup_spans_adjust_interpolants_up() \
934 sub_4x32b(uvrg, uvrg, uvrg_dy); \
935 b -= b_dy \
936
937#define setup_spans_adjust_interpolants_down() \
938 add_4x32b(uvrg, uvrg, uvrg_dy); \
939 b += b_dy \
940
941
942#define setup_spans_clip_interpolants_increment() \
943 mla_scalar_4x32b(uvrg, uvrg_dy, clip); \
944 b += b_dy * clip \
945
946#define setup_spans_clip_interpolants_decrement() \
947 mls_scalar_4x32b(uvrg, uvrg_dy, clip); \
948 b -= b_dy * clip \
949
950#define setup_spans_clip_alternate_yes() \
951 edge_alt += edge_dx_dy_alt * (s64)(clip) \
952
953#define setup_spans_clip_alternate_no() \
954
955#define setup_spans_clip(direction, alternate_active) \
956{ \
957 clipped_triangles++; \
958 mla_scalar_long_2x32b(edges_xy, edges_dx_dy, (s64)clip); \
959 setup_spans_clip_alternate_##alternate_active(); \
960 setup_spans_clip_interpolants_##direction(); \
961} \
962
963
964#define setup_spans_adjust_edges_alternate_no(left_index, right_index) \
965{ \
966 vec_2x64u edge_shifts_64; \
967 vec_2x64s edges_dx_dy_64; \
968 \
969 mov_wide_2x32b(edge_shifts_64, edge_shifts); \
970 shl_variable_2x64b(edges_xy, edges_xy, edge_shifts_64); \
971 \
972 mov_wide_2x32b(edges_dx_dy_64, edges_dx_dy); \
973 shl_variable_2x64b(edges_dx_dy_64, edges_dx_dy_64, edge_shifts_64); \
974 \
975 left_x.e[0] = edges_xy.e[left_index]; \
976 right_x.e[0] = edges_xy.e[right_index]; \
977 \
978 left_dx_dy.e[0] = edges_dx_dy_64.e[left_index]; \
979 left_dx_dy.e[1] = edges_dx_dy_64.e[left_index]; \
980 right_dx_dy.e[0] = edges_dx_dy_64.e[right_index]; \
981 right_dx_dy.e[1] = edges_dx_dy_64.e[right_index]; \
982 \
983 add_1x64b(left_x.high, left_x.low, left_dx_dy.low); \
984 add_1x64b(right_x.high, right_x.low, right_dx_dy.low); \
985 \
986 add_2x64b(left_dx_dy, left_dx_dy, left_dx_dy); \
987 add_2x64b(right_dx_dy, right_dx_dy, right_dx_dy); \
988} \
989
990#define setup_spans_adjust_edges_alternate_yes(left_index, right_index) \
991{ \
992 setup_spans_adjust_edges_alternate_no(left_index, right_index); \
993 s64 edge_dx_dy_alt_64; \
994 \
995 dup_4x16b(y_mid_point, y_b); \
996 \
997 edge_alt <<= edge_shift_alt; \
998 edge_dx_dy_alt_64 = (s64)edge_dx_dy_alt << edge_shift_alt; \
999 \
1000 alternate_x.e[0] = edge_alt; \
1001 alternate_dx_dy.e[0] = edge_dx_dy_alt_64; \
1002 alternate_dx_dy.e[1] = edge_dx_dy_alt_64; \
1003 \
1004 add_1x64b(alternate_x.high, alternate_x.low, alternate_dx_dy.low); \
1005 add_2x64b(alternate_dx_dy, alternate_dx_dy, alternate_dx_dy); \
1006} \
1007
1008
1009#define setup_spans_y_select_up() \
1010 cmplt_4x16b(alternate_select, y_x4, y_mid_point) \
1011
1012#define setup_spans_y_select_down() \
1013 cmpgt_4x16b(alternate_select, y_x4, y_mid_point) \
1014
1015#define setup_spans_y_select_alternate_yes(direction) \
1016 setup_spans_y_select_##direction() \
1017
1018#define setup_spans_y_select_alternate_no(direction) \
1019
1020#define setup_spans_alternate_select_left() \
1021 bit_4x16b(left_right_x_16.low, alternate_x_16, alternate_select) \
1022
1023#define setup_spans_alternate_select_right() \
1024 bit_4x16b(left_right_x_16.high, alternate_x_16, alternate_select) \
1025
1026#define setup_spans_alternate_select_none() \
1027
1028#define setup_spans_increment_alternate_yes() \
1029 shr_narrow_2x64b(alternate_x_32.low, alternate_x, 32); \
1030 add_2x64b(alternate_x, alternate_x, alternate_dx_dy); \
1031 shr_narrow_2x64b(alternate_x_32.high, alternate_x, 32); \
1032 add_2x64b(alternate_x, alternate_x, alternate_dx_dy); \
1033 mov_narrow_4x32b(alternate_x_16, alternate_x_32) \
1034
1035#define setup_spans_increment_alternate_no() \
1036
1037#define setup_spans_set_x4(alternate, direction, alternate_active) \
1038{ \
1039 span_uvrg_offset[0] = uvrg; \
1040 span_b_offset[0] = b; \
1041 setup_spans_adjust_interpolants_##direction(); \
1042 \
1043 span_uvrg_offset[1] = uvrg; \
1044 span_b_offset[1] = b; \
1045 setup_spans_adjust_interpolants_##direction(); \
1046 \
1047 span_uvrg_offset[2] = uvrg; \
1048 span_b_offset[2] = b; \
1049 setup_spans_adjust_interpolants_##direction(); \
1050 \
1051 span_uvrg_offset[3] = uvrg; \
1052 span_b_offset[3] = b; \
1053 setup_spans_adjust_interpolants_##direction(); \
1054 \
1055 span_uvrg_offset += 4; \
1056 span_b_offset += 4; \
1057 \
1058 shr_narrow_2x64b(left_x_32.low, left_x, 32); \
1059 shr_narrow_2x64b(right_x_32.low, right_x, 32); \
1060 \
1061 add_2x64b(left_x, left_x, left_dx_dy); \
1062 add_2x64b(right_x, right_x, right_dx_dy); \
1063 \
1064 shr_narrow_2x64b(left_x_32.high, left_x, 32); \
1065 shr_narrow_2x64b(right_x_32.high, right_x, 32); \
1066 \
1067 add_2x64b(left_x, left_x, left_dx_dy); \
1068 add_2x64b(right_x, right_x, right_dx_dy); \
1069 \
1070 mov_narrow_4x32b(left_right_x_16.low, left_x_32); \
1071 mov_narrow_4x32b(left_right_x_16.high, right_x_32); \
1072 \
1073 setup_spans_increment_alternate_##alternate_active(); \
1074 setup_spans_y_select_alternate_##alternate_active(direction); \
1075 setup_spans_alternate_select_##alternate(); \
1076 \
1077 max_8x16b(left_right_x_16, left_right_x_16, left_edge); \
1078 min_8x16b(left_right_x_16, left_right_x_16, right_edge); \
1079 \
1080 sub_4x16b(left_right_x_16.high, left_right_x_16.high, left_right_x_16.low); \
1081 add_4x16b(left_right_x_16.high, left_right_x_16.high, c_0x07); \
1082 and_4x16b(span_shift, left_right_x_16.high, c_0x07); \
1083 shl_variable_4x16b(span_shift, c_0xFFFE, span_shift); \
1084 shr_4x16b(left_right_x_16.high, left_right_x_16.high, 3); \
1085 \
1086 u32 i; \
1087 for(i = 0; i < 4; i++) \
1088 { \
1089 span_edge_data[i].left_x = left_right_x_16.low.e[i]; \
1090 span_edge_data[i].num_blocks = left_right_x_16.high.e[i]; \
1091 span_edge_data[i].right_mask = span_shift.e[i]; \
1092 span_edge_data[i].y = y_x4.e[i]; \
c111e8f8 1093 setup_spans_debug_check(span_edge_data[i]); \
75e28f62
E
1094 } \
1095 \
1096 span_edge_data += 4; \
1097 \
1098 setup_spans_adjust_y_##direction(); \
1099} \
1100
1101
1102#define setup_spans_alternate_adjust_yes() \
1103 edge_alt -= edge_dx_dy_alt * (s64)height_minor_a \
1104
1105#define setup_spans_alternate_adjust_no() \
1106
1107
1108#define setup_spans_down(left_index, right_index, alternate, alternate_active) \
1109 setup_spans_alternate_adjust_##alternate_active(); \
1110 if(y_c > psx_gpu->viewport_end_y) \
1111 height -= y_c - psx_gpu->viewport_end_y - 1; \
1112 \
1113 clip = psx_gpu->viewport_start_y - y_a; \
1114 if(clip > 0) \
1115 { \
1116 height -= clip; \
1117 y_a += clip; \
1118 setup_spans_clip(increment, alternate_active); \
1119 } \
1120 \
1121 setup_spans_prologue_b(); \
1122 \
1123 if(height > 0) \
1124 { \
1125 y_x4.e[0] = y_a; \
1126 y_x4.e[1] = y_a + 1; \
1127 y_x4.e[2] = y_a + 2; \
1128 y_x4.e[3] = y_a + 3; \
1129 setup_spans_adjust_edges_alternate_##alternate_active(left_index, \
1130 right_index); \
1131 \
1132 psx_gpu->num_spans = height; \
1133 do \
1134 { \
1135 setup_spans_set_x4(alternate, down, alternate_active); \
1136 height -= 4; \
1137 } while(height > 0); \
1138 } \
1139
1140
1141#define setup_spans_alternate_pre_increment_yes() \
1142 edge_alt += edge_dx_dy_alt \
1143
1144#define setup_spans_alternate_pre_increment_no() \
1145
1146#define setup_spans_up_decrement_height_yes() \
1147 height-- \
1148
1149#define setup_spans_up_decrement_height_no() \
1150 {} \
1151
1152#define setup_spans_up(left_index, right_index, alternate, alternate_active) \
1153 setup_spans_alternate_adjust_##alternate_active(); \
1154 y_a--; \
1155 \
1156 if(y_c < psx_gpu->viewport_start_y) \
1157 height -= psx_gpu->viewport_start_y - y_c; \
1158 else \
1159 setup_spans_up_decrement_height_##alternate_active(); \
1160 \
1161 clip = y_a - psx_gpu->viewport_end_y; \
1162 if(clip > 0) \
1163 { \
1164 height -= clip; \
1165 y_a -= clip; \
1166 setup_spans_clip(decrement, alternate_active); \
1167 } \
1168 \
1169 setup_spans_prologue_b(); \
1170 \
1171 if(height > 0) \
1172 { \
1173 y_x4.e[0] = y_a; \
1174 y_x4.e[1] = y_a - 1; \
1175 y_x4.e[2] = y_a - 2; \
1176 y_x4.e[3] = y_a - 3; \
1177 add_wide_2x32b(edges_xy, edges_xy, edges_dx_dy); \
1178 setup_spans_alternate_pre_increment_##alternate_active(); \
1179 setup_spans_adjust_edges_alternate_##alternate_active(left_index, \
1180 right_index); \
1181 setup_spans_adjust_interpolants_up(); \
1182 \
1183 psx_gpu->num_spans = height; \
1184 while(height > 0) \
1185 { \
1186 setup_spans_set_x4(alternate, up, alternate_active); \
1187 height -= 4; \
1188 } \
1189 } \
1190
1191#define index_left 0
1192#define index_right 1
1193
1194#define setup_spans_up_up(minor, major) \
1195 setup_spans_prologue(yes); \
1196 s32 height_minor_a = y_a - y_b; \
1197 s32 height_minor_b = y_b - y_c; \
1198 s32 height = y_a - y_c; \
1199 \
1200 dup_2x32b(x_starts, x_a); \
1201 x_ends.e[0] = x_c; \
1202 x_ends.e[1] = x_b; \
1203 \
1204 compute_edge_delta_x3(x_b, height, height_minor_a); \
1205 setup_spans_up(index_##major, index_##minor, minor, yes) \
1206
1207
1208void setup_spans_up_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1209 vertex_struct *v_b, vertex_struct *v_c);
1210void setup_spans_up_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1211 vertex_struct *v_b, vertex_struct *v_c);
1212void setup_spans_down_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1213 vertex_struct *v_b, vertex_struct *v_c);
1214void setup_spans_down_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1215 vertex_struct *v_b, vertex_struct *v_c);
1216void setup_spans_up_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1217 vertex_struct *v_b, vertex_struct *v_c);
1218void setup_spans_up_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1219 vertex_struct *v_b, vertex_struct *v_c);
1220void setup_spans_down_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1221 vertex_struct *v_b, vertex_struct *v_c);
1222void setup_spans_down_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1223 vertex_struct *v_b, vertex_struct *v_c);
1224void setup_spans_up_down(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1225 vertex_struct *v_b, vertex_struct *v_c);
1226
1227
2bbbb7af 1228#ifndef NEON_BUILD
75e28f62
E
1229
1230void setup_spans_up_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1231 vertex_struct *v_b, vertex_struct *v_c)
1232{
1233 setup_spans_up_up(left, right);
1234}
1235
1236void setup_spans_up_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1237 vertex_struct *v_b, vertex_struct *v_c)
1238{
1239 setup_spans_up_up(right, left);
1240}
1241
1242#define setup_spans_down_down(minor, major) \
1243 setup_spans_prologue(yes); \
1244 s32 height_minor_a = y_b - y_a; \
1245 s32 height_minor_b = y_c - y_b; \
1246 s32 height = y_c - y_a; \
1247 \
1248 dup_2x32b(x_starts, x_a); \
1249 x_ends.e[0] = x_c; \
1250 x_ends.e[1] = x_b; \
1251 \
1252 compute_edge_delta_x3(x_b, height, height_minor_a); \
1253 setup_spans_down(index_##major, index_##minor, minor, yes) \
1254
1255void setup_spans_down_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1256 vertex_struct *v_b, vertex_struct *v_c)
1257{
1258 setup_spans_down_down(left, right);
1259}
1260
1261void setup_spans_down_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1262 vertex_struct *v_b, vertex_struct *v_c)
1263{
1264 setup_spans_down_down(right, left);
1265}
1266
1267#define setup_spans_up_flat() \
1268 s32 height = y_a - y_c; \
1269 \
1270 flat_triangles++; \
1271 compute_edge_delta_x2(); \
1272 setup_spans_up(index_left, index_right, none, no) \
1273
1274void setup_spans_up_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1275 vertex_struct *v_b, vertex_struct *v_c)
1276{
1277 setup_spans_prologue(no);
1278 x_starts.e[0] = x_a;
1279 x_starts.e[1] = x_b;
1280 dup_2x32b(x_ends, x_c);
1281
1282 setup_spans_up_flat();
1283}
1284
1285void setup_spans_up_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1286 vertex_struct *v_b, vertex_struct *v_c)
1287{
1288 setup_spans_prologue(no);
1289 dup_2x32b(x_starts, x_a);
1290 x_ends.e[0] = x_b;
1291 x_ends.e[1] = x_c;
1292
1293 setup_spans_up_flat();
1294}
1295
1296#define setup_spans_down_flat() \
1297 s32 height = y_c - y_a; \
1298 \
1299 flat_triangles++; \
1300 compute_edge_delta_x2(); \
1301 setup_spans_down(index_left, index_right, none, no) \
1302
1303void setup_spans_down_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1304 vertex_struct *v_b, vertex_struct *v_c)
1305{
1306 setup_spans_prologue(no);
1307 x_starts.e[0] = x_a;
1308 x_starts.e[1] = x_b;
1309 dup_2x32b(x_ends, x_c);
1310
1311 setup_spans_down_flat();
1312}
1313
1314void setup_spans_down_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1315 vertex_struct *v_b, vertex_struct *v_c)
1316{
1317 setup_spans_prologue(no);
1318 dup_2x32b(x_starts, x_a);
1319 x_ends.e[0] = x_b;
1320 x_ends.e[1] = x_c;
1321
1322 setup_spans_down_flat();
1323}
1324
1325void setup_spans_up_down(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1326 vertex_struct *v_b, vertex_struct *v_c)
1327{
1328 setup_spans_prologue(no);
1329
1330 s32 y_b = v_b->y;
1331 s64 edge_alt;
1332 s32 edge_dx_dy_alt;
1333 u32 edge_shift_alt;
1334
1335 s32 middle_y = y_a;
1336 s32 height_minor_a = y_a - y_b;
1337 s32 height_minor_b = y_c - y_a;
1338 s32 height_major = y_c - y_b;
1339
1340 vec_2x64s edges_xy_b;
1341 vec_2x32s edges_dx_dy_b;
1342 vec_2x32u edge_shifts_b;
1343
1344 vec_2x32s height_increment;
1345
1346 x_starts.e[0] = x_a;
1347 x_starts.e[1] = x_c;
1348 dup_2x32b(x_ends, x_b);
1349
1350 compute_edge_delta_x3(x_a, height_minor_a, height_major);
1351
1352 height_increment.e[0] = 0;
1353 height_increment.e[1] = height_minor_b;
1354
1355 mla_long_2x32b(edges_xy, edges_dx_dy, height_increment);
1356
1357 edges_xy_b.e[0] = edge_alt;
1358 edges_xy_b.e[1] = edges_xy.e[1];
1359
1360 edge_shifts_b = edge_shifts;
1361 edge_shifts_b.e[0] = edge_shift_alt;
1362
1363 neg_2x32b(edges_dx_dy_b, edges_dx_dy);
1364 edges_dx_dy_b.e[0] = edge_dx_dy_alt;
1365
1366 y_a--;
1367
1368 if(y_b < psx_gpu->viewport_start_y)
1369 height_minor_a -= psx_gpu->viewport_start_y - y_b;
1370
1371 clip = y_a - psx_gpu->viewport_end_y;
1372 if(clip > 0)
1373 {
1374 height_minor_a -= clip;
1375 y_a -= clip;
1376 setup_spans_clip(decrement, no);
1377 }
1378
1379 setup_spans_prologue_b();
1380
1381 if(height_minor_a > 0)
1382 {
1383 y_x4.e[0] = y_a;
1384 y_x4.e[1] = y_a - 1;
1385 y_x4.e[2] = y_a - 2;
1386 y_x4.e[3] = y_a - 3;
1387 add_wide_2x32b(edges_xy, edges_xy, edges_dx_dy);
1388 setup_spans_adjust_edges_alternate_no(index_left, index_right);
1389 setup_spans_adjust_interpolants_up();
1390
1391 psx_gpu->num_spans = height_minor_a;
1392 while(height_minor_a > 0)
1393 {
1394 setup_spans_set_x4(none, up, no);
1395 height_minor_a -= 4;
1396 }
1397
1398 span_edge_data += height_minor_a;
1399 span_uvrg_offset += height_minor_a;
1400 span_b_offset += height_minor_a;
1401 }
1402
1403 edges_xy = edges_xy_b;
1404 edges_dx_dy = edges_dx_dy_b;
1405 edge_shifts = edge_shifts_b;
1406
1407 uvrg = psx_gpu->uvrg;
1408 b = psx_gpu->b;
1409
1410 y_a = middle_y;
1411
1412 if(y_c > psx_gpu->viewport_end_y)
1413 height_minor_b -= y_c - psx_gpu->viewport_end_y - 1;
1414
1415 clip = psx_gpu->viewport_start_y - y_a;
1416 if(clip > 0)
1417 {
1418 height_minor_b -= clip;
1419 y_a += clip;
1420 setup_spans_clip(increment, no);
1421 }
1422
1423 if(height_minor_b > 0)
1424 {
1425 y_x4.e[0] = y_a;
1426 y_x4.e[1] = y_a + 1;
1427 y_x4.e[2] = y_a + 2;
1428 y_x4.e[3] = y_a + 3;
1429 setup_spans_adjust_edges_alternate_no(index_left, index_right);
1430
b7569147 1431 // FIXME: overflow corner case
1432 if(psx_gpu->num_spans + height_minor_b == MAX_SPANS)
1433 height_minor_b &= ~3;
1434
75e28f62 1435 psx_gpu->num_spans += height_minor_b;
b7569147 1436 while(height_minor_b > 0)
75e28f62
E
1437 {
1438 setup_spans_set_x4(none, down, no);
1439 height_minor_b -= 4;
b7569147 1440 }
75e28f62
E
1441 }
1442
1443 left_split_triangles++;
1444}
1445
1446#endif
1447
1448
1449#define dither_table_entry_normal(value) \
1450 (value) \
1451
1452
1453#define setup_blocks_load_msb_mask_indirect() \
1454
1455#define setup_blocks_load_msb_mask_direct() \
1456 vec_8x16u msb_mask; \
1457 dup_8x16b(msb_mask, psx_gpu->mask_msb); \
1458
1459
1460#define setup_blocks_variables_shaded_textured(target) \
1461 vec_4x32u u_block; \
1462 vec_4x32u v_block; \
1463 vec_4x32u r_block; \
1464 vec_4x32u g_block; \
1465 vec_4x32u b_block; \
1466 vec_4x32u uvrg_dx = psx_gpu->uvrg_dx; \
1467 vec_4x32u uvrg_dx4; \
1468 vec_4x32u uvrg_dx8; \
1469 vec_4x32u uvrg; \
1470 u32 b_dx = psx_gpu->b_block_span.e[1]; \
1471 u32 b_dx4 = b_dx << 2; \
1472 u32 b_dx8 = b_dx << 3; \
1473 u32 b; \
1474 \
1475 vec_16x8u texture_mask; \
1476 shl_4x32b(uvrg_dx4, uvrg_dx, 2); \
1477 shl_4x32b(uvrg_dx8, uvrg_dx, 3); \
1478 dup_8x8b(texture_mask.low, psx_gpu->texture_mask_width); \
1479 dup_8x8b(texture_mask.high, psx_gpu->texture_mask_height) \
1480
1481#define setup_blocks_variables_shaded_untextured(target) \
1482 vec_4x32u r_block; \
1483 vec_4x32u g_block; \
1484 vec_4x32u b_block; \
1485 vec_4x32u rgb_dx; \
1486 vec_4x32u rgb_dx4; \
1487 vec_4x32u rgb_dx8; \
1488 vec_4x32u rgb; \
1489 \
1490 vec_8x8u d64_0x07; \
1491 vec_8x8u d64_1; \
1492 vec_8x8u d64_4; \
1493 vec_8x8u d64_128; \
1494 \
1495 dup_8x8b(d64_0x07, 0x07); \
1496 dup_8x8b(d64_1, 1); \
1497 dup_8x8b(d64_4, 4); \
1498 dup_8x8b(d64_128, 128); \
1499 \
1500 rgb_dx.low = psx_gpu->uvrg_dx.high; \
1501 rgb_dx.e[2] = psx_gpu->b_block_span.e[1]; \
1502 shl_4x32b(rgb_dx4, rgb_dx, 2); \
1503 shl_4x32b(rgb_dx8, rgb_dx, 3) \
1504
1505#define setup_blocks_variables_unshaded_textured(target) \
1506 vec_4x32u u_block; \
1507 vec_4x32u v_block; \
1508 vec_2x32u uv_dx = psx_gpu->uvrg_dx.low; \
1509 vec_2x32u uv_dx4; \
1510 vec_2x32u uv_dx8; \
1511 vec_2x32u uv = psx_gpu->uvrg.low; \
1512 \
1513 vec_16x8u texture_mask; \
1514 shl_2x32b(uv_dx4, uv_dx, 2); \
1515 shl_2x32b(uv_dx8, uv_dx, 3); \
1516 dup_8x8b(texture_mask.low, psx_gpu->texture_mask_width); \
1517 dup_8x8b(texture_mask.high, psx_gpu->texture_mask_height) \
1518
1519
1520#define setup_blocks_variables_unshaded_untextured_direct() \
1521 or_8x16b(colors, colors, msb_mask) \
1522
1523#define setup_blocks_variables_unshaded_untextured_indirect() \
1524
1525#define setup_blocks_variables_unshaded_untextured(target) \
1526 u32 color = psx_gpu->triangle_color; \
1527 vec_8x16u colors; \
1528 \
1529 u32 color_r = color & 0xFF; \
1530 u32 color_g = (color >> 8) & 0xFF; \
1531 u32 color_b = (color >> 16) & 0xFF; \
1532 \
1533 color = (color_r >> 3) | ((color_g >> 3) << 5) | \
1534 ((color_b >> 3) << 10); \
1535 dup_8x16b(colors, color); \
1536 setup_blocks_variables_unshaded_untextured_##target() \
1537
1538#define setup_blocks_span_initialize_dithered_textured() \
1539 vec_8x16u dither_offsets; \
1540 shl_long_8x8b(dither_offsets, dither_offsets_short, 4) \
1541
1542#define setup_blocks_span_initialize_dithered_untextured() \
1543 vec_8x8u dither_offsets; \
1544 add_8x8b(dither_offsets, dither_offsets_short, d64_4) \
1545
1546#define setup_blocks_span_initialize_dithered(texturing) \
1547 u32 dither_row = psx_gpu->dither_table[y & 0x3]; \
1548 u32 dither_shift = (span_edge_data->left_x & 0x3) * 8; \
1549 vec_8x8s dither_offsets_short; \
1550 \
1551 dither_row = \
1552 (dither_row >> dither_shift) | (dither_row << (32 - dither_shift)); \
1553 dup_2x32b(vector_cast(vec_2x32u, dither_offsets_short), dither_row); \
1554 setup_blocks_span_initialize_dithered_##texturing() \
1555
1556#define setup_blocks_span_initialize_undithered(texturing) \
1557
1558
1559#define setup_blocks_span_initialize_shaded_textured() \
1560{ \
1561 vec_4x32u block_span; \
1562 u32 offset = span_edge_data->left_x; \
1563 \
1564 uvrg = *span_uvrg_offset; \
1565 mla_scalar_4x32b(uvrg, uvrg_dx, offset); \
1566 b = *span_b_offset; \
1567 b += b_dx * offset; \
1568 \
1569 dup_4x32b(u_block, uvrg.e[0]); \
1570 dup_4x32b(v_block, uvrg.e[1]); \
1571 dup_4x32b(r_block, uvrg.e[2]); \
1572 dup_4x32b(g_block, uvrg.e[3]); \
1573 dup_4x32b(b_block, b); \
1574 \
1575 block_span = psx_gpu->u_block_span; \
1576 add_4x32b(u_block, u_block, block_span); \
1577 block_span = psx_gpu->v_block_span; \
1578 add_4x32b(v_block, v_block, block_span); \
1579 block_span = psx_gpu->r_block_span; \
1580 add_4x32b(r_block, r_block, block_span); \
1581 block_span = psx_gpu->g_block_span; \
1582 add_4x32b(g_block, g_block, block_span); \
1583 block_span = psx_gpu->b_block_span; \
1584 add_4x32b(b_block, b_block, block_span); \
1585}
1586
1587#define setup_blocks_span_initialize_shaded_untextured() \
1588{ \
1589 vec_4x32u block_span; \
1590 u32 offset = span_edge_data->left_x; \
1591 \
1592 rgb.low = span_uvrg_offset->high; \
1593 rgb.high.e[0] = *span_b_offset; \
1594 mla_scalar_4x32b(rgb, rgb_dx, offset); \
1595 \
1596 dup_4x32b(r_block, rgb.e[0]); \
1597 dup_4x32b(g_block, rgb.e[1]); \
1598 dup_4x32b(b_block, rgb.e[2]); \
1599 \
1600 block_span = psx_gpu->r_block_span; \
1601 add_4x32b(r_block, r_block, block_span); \
1602 block_span = psx_gpu->g_block_span; \
1603 add_4x32b(g_block, g_block, block_span); \
1604 block_span = psx_gpu->b_block_span; \
1605 add_4x32b(b_block, b_block, block_span); \
1606} \
1607
1608#define setup_blocks_span_initialize_unshaded_textured() \
1609{ \
1610 vec_4x32u block_span; \
1611 u32 offset = span_edge_data->left_x; \
1612 \
1613 uv = span_uvrg_offset->low; \
1614 mla_scalar_2x32b(uv, uv_dx, offset); \
1615 \
1616 dup_4x32b(u_block, uv.e[0]); \
1617 dup_4x32b(v_block, uv.e[1]); \
1618 \
1619 block_span = psx_gpu->u_block_span; \
1620 add_4x32b(u_block, u_block, block_span); \
1621 block_span = psx_gpu->v_block_span; \
1622 add_4x32b(v_block, v_block, block_span); \
1623} \
1624
1625#define setup_blocks_span_initialize_unshaded_untextured() \
1626
1627
1628#define setup_blocks_texture_swizzled() \
1629{ \
1630 vec_8x8u u_saved = u; \
1631 sli_8x8b(u, v, 4); \
1632 sri_8x8b(v, u_saved, 4); \
1633} \
1634
1635#define setup_blocks_texture_unswizzled() \
1636
1637#define setup_blocks_store_shaded_textured(swizzling, dithering, target, \
1638 edge_type) \
1639{ \
1640 vec_8x16u u_whole; \
1641 vec_8x16u v_whole; \
1642 vec_8x16u r_whole; \
1643 vec_8x16u g_whole; \
1644 vec_8x16u b_whole; \
1645 \
1646 vec_8x8u u; \
1647 vec_8x8u v; \
1648 vec_8x8u r; \
1649 vec_8x8u g; \
1650 vec_8x8u b; \
1651 vec_8x16u uv; \
1652 \
1653 vec_4x32u dx4; \
1654 vec_4x32u dx8; \
1655 \
1656 shr_narrow_4x32b(u_whole.low, u_block, 16); \
1657 shr_narrow_4x32b(v_whole.low, v_block, 16); \
1658 shr_narrow_4x32b(r_whole.low, r_block, 16); \
1659 shr_narrow_4x32b(g_whole.low, g_block, 16); \
1660 shr_narrow_4x32b(b_whole.low, b_block, 16); \
1661 \
1662 dup_4x32b(dx4, uvrg_dx4.e[0]); \
1663 add_high_narrow_4x32b(u_whole.high, u_block, dx4); \
1664 dup_4x32b(dx4, uvrg_dx4.e[1]); \
1665 add_high_narrow_4x32b(v_whole.high, v_block, dx4); \
1666 dup_4x32b(dx4, uvrg_dx4.e[2]); \
1667 add_high_narrow_4x32b(r_whole.high, r_block, dx4); \
1668 dup_4x32b(dx4, uvrg_dx4.e[3]); \
1669 add_high_narrow_4x32b(g_whole.high, g_block, dx4); \
1670 dup_4x32b(dx4, b_dx4); \
1671 add_high_narrow_4x32b(b_whole.high, b_block, dx4); \
1672 \
1673 mov_narrow_8x16b(u, u_whole); \
1674 mov_narrow_8x16b(v, v_whole); \
1675 mov_narrow_8x16b(r, r_whole); \
1676 mov_narrow_8x16b(g, g_whole); \
1677 mov_narrow_8x16b(b, b_whole); \
1678 \
1679 dup_4x32b(dx8, uvrg_dx8.e[0]); \
1680 add_4x32b(u_block, u_block, dx8); \
1681 dup_4x32b(dx8, uvrg_dx8.e[1]); \
1682 add_4x32b(v_block, v_block, dx8); \
1683 dup_4x32b(dx8, uvrg_dx8.e[2]); \
1684 add_4x32b(r_block, r_block, dx8); \
1685 dup_4x32b(dx8, uvrg_dx8.e[3]); \
1686 add_4x32b(g_block, g_block, dx8); \
1687 dup_4x32b(dx8, b_dx8); \
1688 add_4x32b(b_block, b_block, dx8); \
1689 \
1690 and_8x8b(u, u, texture_mask.low); \
1691 and_8x8b(v, v, texture_mask.high); \
1692 setup_blocks_texture_##swizzling(); \
1693 \
1694 zip_8x16b(uv, u, v); \
1695 block->uv = uv; \
1696 block->r = r; \
1697 block->g = g; \
1698 block->b = b; \
1699 block->dither_offsets = vector_cast(vec_8x16u, dither_offsets); \
1700 block->fb_ptr = fb_ptr; \
1701} \
1702
1703#define setup_blocks_store_unshaded_textured(swizzling, dithering, target, \
1704 edge_type) \
1705{ \
1706 vec_8x16u u_whole; \
1707 vec_8x16u v_whole; \
1708 \
1709 vec_8x8u u; \
1710 vec_8x8u v; \
1711 vec_8x16u uv; \
1712 \
1713 vec_4x32u dx4; \
1714 vec_4x32u dx8; \
1715 \
1716 shr_narrow_4x32b(u_whole.low, u_block, 16); \
1717 shr_narrow_4x32b(v_whole.low, v_block, 16); \
1718 \
1719 dup_4x32b(dx4, uv_dx4.e[0]); \
1720 add_high_narrow_4x32b(u_whole.high, u_block, dx4); \
1721 dup_4x32b(dx4, uv_dx4.e[1]); \
1722 add_high_narrow_4x32b(v_whole.high, v_block, dx4); \
1723 \
1724 mov_narrow_8x16b(u, u_whole); \
1725 mov_narrow_8x16b(v, v_whole); \
1726 \
1727 dup_4x32b(dx8, uv_dx8.e[0]); \
1728 add_4x32b(u_block, u_block, dx8); \
1729 dup_4x32b(dx8, uv_dx8.e[1]); \
1730 add_4x32b(v_block, v_block, dx8); \
1731 \
1732 and_8x8b(u, u, texture_mask.low); \
1733 and_8x8b(v, v, texture_mask.high); \
1734 setup_blocks_texture_##swizzling(); \
1735 \
1736 zip_8x16b(uv, u, v); \
1737 block->uv = uv; \
1738 block->dither_offsets = vector_cast(vec_8x16u, dither_offsets); \
1739 block->fb_ptr = fb_ptr; \
1740} \
1741
1742#define setup_blocks_store_shaded_untextured_dithered() \
1743 addq_8x8b(r, r, dither_offsets); \
1744 addq_8x8b(g, g, dither_offsets); \
1745 addq_8x8b(b, b, dither_offsets); \
1746 \
1747 subq_8x8b(r, r, d64_4); \
1748 subq_8x8b(g, g, d64_4); \
1749 subq_8x8b(b, b, d64_4) \
1750
1751#define setup_blocks_store_shaded_untextured_undithered() \
1752
1753
1754#define setup_blocks_store_untextured_pixels_indirect_full(_pixels) \
1755 block->pixels = _pixels; \
1756 block->fb_ptr = fb_ptr \
1757
1758#define setup_blocks_store_untextured_pixels_indirect_edge(_pixels) \
1759 block->pixels = _pixels; \
1760 block->fb_ptr = fb_ptr \
1761
1762#define setup_blocks_store_shaded_untextured_seed_pixels_indirect() \
1763 mul_long_8x8b(pixels, r, d64_1) \
1764
1765
1766#define setup_blocks_store_untextured_pixels_direct_full(_pixels) \
1767 store_8x16b(_pixels, fb_ptr) \
1768
1769#define setup_blocks_store_untextured_pixels_direct_edge(_pixels) \
1770{ \
1771 vec_8x16u fb_pixels; \
1772 vec_8x16u draw_mask; \
1773 vec_8x16u test_mask = psx_gpu->test_mask; \
1774 \
1775 load_8x16b(fb_pixels, fb_ptr); \
1776 dup_8x16b(draw_mask, span_edge_data->right_mask); \
1777 tst_8x16b(draw_mask, draw_mask, test_mask); \
1778 bif_8x16b(fb_pixels, _pixels, draw_mask); \
1779 store_8x16b(fb_pixels, fb_ptr); \
1780} \
1781
1782#define setup_blocks_store_shaded_untextured_seed_pixels_direct() \
1783 pixels = msb_mask; \
1784 mla_long_8x8b(pixels, r, d64_1) \
1785
1786
1787#define setup_blocks_store_shaded_untextured(swizzling, dithering, target, \
1788 edge_type) \
1789{ \
1790 vec_8x16u r_whole; \
1791 vec_8x16u g_whole; \
1792 vec_8x16u b_whole; \
1793 \
1794 vec_8x8u r; \
1795 vec_8x8u g; \
1796 vec_8x8u b; \
1797 \
1798 vec_4x32u dx4; \
1799 vec_4x32u dx8; \
1800 \
1801 vec_8x16u pixels; \
1802 \
1803 shr_narrow_4x32b(r_whole.low, r_block, 16); \
1804 shr_narrow_4x32b(g_whole.low, g_block, 16); \
1805 shr_narrow_4x32b(b_whole.low, b_block, 16); \
1806 \
1807 dup_4x32b(dx4, rgb_dx4.e[0]); \
1808 add_high_narrow_4x32b(r_whole.high, r_block, dx4); \
1809 dup_4x32b(dx4, rgb_dx4.e[1]); \
1810 add_high_narrow_4x32b(g_whole.high, g_block, dx4); \
1811 dup_4x32b(dx4, rgb_dx4.e[2]); \
1812 add_high_narrow_4x32b(b_whole.high, b_block, dx4); \
1813 \
1814 mov_narrow_8x16b(r, r_whole); \
1815 mov_narrow_8x16b(g, g_whole); \
1816 mov_narrow_8x16b(b, b_whole); \
1817 \
1818 dup_4x32b(dx8, rgb_dx8.e[0]); \
1819 add_4x32b(r_block, r_block, dx8); \
1820 dup_4x32b(dx8, rgb_dx8.e[1]); \
1821 add_4x32b(g_block, g_block, dx8); \
1822 dup_4x32b(dx8, rgb_dx8.e[2]); \
1823 add_4x32b(b_block, b_block, dx8); \
1824 \
1825 setup_blocks_store_shaded_untextured_##dithering(); \
1826 \
1827 shr_8x8b(r, r, 3); \
1828 bic_8x8b(g, g, d64_0x07); \
1829 bic_8x8b(b, b, d64_0x07); \
1830 \
1831 setup_blocks_store_shaded_untextured_seed_pixels_##target(); \
1832 mla_long_8x8b(pixels, g, d64_4); \
1833 mla_long_8x8b(pixels, b, d64_128) \
1834 \
1835 setup_blocks_store_untextured_pixels_##target##_##edge_type(pixels); \
1836} \
1837
1838#define setup_blocks_store_unshaded_untextured(swizzling, dithering, target, \
1839 edge_type) \
1840 setup_blocks_store_untextured_pixels_##target##_##edge_type(colors) \
1841
1842
1843#define setup_blocks_store_draw_mask_textured_indirect(_block, bits) \
1844 (_block)->draw_mask_bits = bits \
1845
1846#define setup_blocks_store_draw_mask_untextured_indirect(_block, bits) \
1847{ \
1848 vec_8x16u bits_mask; \
1849 vec_8x16u test_mask = psx_gpu->test_mask; \
1850 dup_8x16b(bits_mask, bits); \
1851 tst_8x16b(bits_mask, bits_mask, test_mask); \
1852 (_block)->draw_mask = bits_mask; \
1853} \
1854
1855#define setup_blocks_store_draw_mask_untextured_direct(_block, bits) \
1856
1857
1858#define setup_blocks_add_blocks_indirect() \
1859 num_blocks += span_num_blocks; \
1860 \
1861 if(num_blocks > MAX_BLOCKS) \
1862 { \
1863 psx_gpu->num_blocks = num_blocks - span_num_blocks; \
1864 flush_render_block_buffer(psx_gpu); \
1865 num_blocks = span_num_blocks; \
1866 block = psx_gpu->blocks; \
1867 } \
1868
1869#define setup_blocks_add_blocks_direct() \
3867c6ef
E
1870 texel_blocks_untextured += span_num_blocks; \
1871 span_pixel_blocks += span_num_blocks \
75e28f62
E
1872
1873
1874#define setup_blocks_builder(shading, texturing, dithering, sw, target) \
1875void setup_blocks_##shading##_##texturing##_##dithering##_##sw##_##target( \
1876 psx_gpu_struct *psx_gpu) \
1877{ \
1878 setup_blocks_load_msb_mask_##target(); \
1879 setup_blocks_variables_##shading##_##texturing(target); \
1880 \
1881 edge_data_struct *span_edge_data = psx_gpu->span_edge_data; \
1882 vec_4x32u *span_uvrg_offset = psx_gpu->span_uvrg_offset; \
1883 u32 *span_b_offset = psx_gpu->span_b_offset; \
1884 \
1885 block_struct *block = psx_gpu->blocks + psx_gpu->num_blocks; \
1886 \
1887 u32 num_spans = psx_gpu->num_spans; \
1888 \
1889 u16 *fb_ptr; \
1890 u32 y; \
1891 \
1892 u32 num_blocks = psx_gpu->num_blocks; \
1893 u32 span_num_blocks; \
1894 \
1895 while(num_spans) \
1896 { \
1897 span_num_blocks = span_edge_data->num_blocks; \
1898 if(span_num_blocks) \
1899 { \
1900 y = span_edge_data->y; \
c1817bd9 1901 fb_ptr = psx_gpu->vram_out_ptr + span_edge_data->left_x + (y * 1024); \
75e28f62
E
1902 \
1903 setup_blocks_span_initialize_##shading##_##texturing(); \
1904 setup_blocks_span_initialize_##dithering(texturing); \
1905 \
1906 setup_blocks_add_blocks_##target(); \
1907 \
1908 s32 pixel_span = span_num_blocks * 8; \
1909 pixel_span -= __builtin_popcount(span_edge_data->right_mask & 0xFF); \
1910 span_pixels += pixel_span; \
75e28f62
E
1911 \
1912 span_num_blocks--; \
1913 while(span_num_blocks) \
1914 { \
1915 setup_blocks_store_##shading##_##texturing(sw, dithering, target, \
1916 full); \
1917 setup_blocks_store_draw_mask_##texturing##_##target(block, 0x00); \
1918 \
1919 fb_ptr += 8; \
1920 block++; \
1921 span_num_blocks--; \
1922 } \
1923 \
1924 setup_blocks_store_##shading##_##texturing(sw, dithering, target, edge); \
1925 setup_blocks_store_draw_mask_##texturing##_##target(block, \
1926 span_edge_data->right_mask); \
1927 \
1928 block++; \
1929 } \
1930 else \
1931 { \
1932 zero_block_spans++; \
1933 } \
1934 \
1935 num_spans--; \
1936 span_edge_data++; \
1937 span_uvrg_offset++; \
1938 span_b_offset++; \
1939 } \
1940 \
1941 psx_gpu->num_blocks = num_blocks; \
1942} \
1943
1944void setup_blocks_shaded_textured_dithered_unswizzled_indirect(psx_gpu_struct
1945 *psx_gpu);
1946
1947void setup_blocks_shaded_untextured_dithered_unswizzled_indirect(psx_gpu_struct
1948 *psx_gpu);
1949void setup_blocks_shaded_untextured_undithered_unswizzled_indirect(
1950 psx_gpu_struct *psx_gpu);
1951void setup_blocks_shaded_untextured_dithered_unswizzled_direct(psx_gpu_struct
1952 *psx_gpu);
1953void setup_blocks_shaded_untextured_undithered_unswizzled_direct(
1954 psx_gpu_struct *psx_gpu);
1955
1956void setup_blocks_unshaded_textured_dithered_unswizzled_indirect(psx_gpu_struct
1957 *psx_gpu);
1958void setup_blocks_unshaded_untextured_undithered_unswizzled_indirect(
1959 psx_gpu_struct *psx_gpu);
1960void setup_blocks_unshaded_untextured_undithered_unswizzled_direct(
1961 psx_gpu_struct *psx_gpu);
1962
1963void setup_blocks_shaded_textured_dithered_swizzled_indirect(psx_gpu_struct
1964 *psx_gpu);
1965void setup_blocks_unshaded_textured_dithered_swizzled_indirect(psx_gpu_struct
1966 *psx_gpu);
1967
1968
1969//setup_blocks_builder(unshaded, untextured, undithered, unswizzled, direct);
1970
2bbbb7af 1971#ifndef NEON_BUILD
75e28f62
E
1972
1973setup_blocks_builder(shaded, textured, dithered, swizzled, indirect);
1974setup_blocks_builder(shaded, textured, dithered, unswizzled, indirect);
1975
1976setup_blocks_builder(unshaded, textured, dithered, unswizzled, indirect);
1977setup_blocks_builder(unshaded, textured, dithered, swizzled, indirect);
1978
1979setup_blocks_builder(shaded, untextured, undithered, unswizzled, indirect);
1980setup_blocks_builder(shaded, untextured, dithered, unswizzled, indirect);
1981setup_blocks_builder(shaded, untextured, undithered, unswizzled, direct);
1982setup_blocks_builder(shaded, untextured, dithered, unswizzled, direct);
1983
1984setup_blocks_builder(unshaded, untextured, undithered, unswizzled, indirect);
1985setup_blocks_builder(unshaded, untextured, undithered, unswizzled, direct);
1986
1987#endif
1988
1989void texture_blocks_untextured(psx_gpu_struct *psx_gpu);
1990void texture_blocks_4bpp(psx_gpu_struct *psx_gpu);
1991void texture_blocks_8bpp(psx_gpu_struct *psx_gpu);
1992void texture_blocks_16bpp(psx_gpu_struct *psx_gpu);
1993
2bbbb7af 1994#ifndef NEON_BUILD
75e28f62
E
1995
1996void texture_blocks_untextured(psx_gpu_struct *psx_gpu)
1997{
1998 if(psx_gpu->primitive_type != PRIMITIVE_TYPE_SPRITE)
1999 texel_blocks_untextured += psx_gpu->num_blocks;
2000}
2001
2002void texture_blocks_4bpp(psx_gpu_struct *psx_gpu)
2003{
2004 block_struct *block = psx_gpu->blocks;
2005 u32 num_blocks = psx_gpu->num_blocks;
2006 texel_blocks_4bpp += num_blocks;
2007
2008 vec_8x8u texels_low;
2009 vec_8x8u texels_high;
2010 vec_8x8u texels;
2011 vec_8x16u pixels;
2012
2013 vec_8x16u clut_a;
2014 vec_8x16u clut_b;
2015 vec_16x8u clut_low;
2016 vec_16x8u clut_high;
2017
2018 u8 *texture_ptr_8bpp = psx_gpu->texture_page_ptr;
2019 u16 *clut_ptr = psx_gpu->clut_ptr;
2020
2021 // Can be done with one deinterleaving load on NEON
2022 load_8x16b(clut_a, clut_ptr);
2023 load_8x16b(clut_b, clut_ptr + 8);
2024 unzip_16x8b(clut_low, clut_high, clut_a, clut_b);
2025
2026 if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_4bpp_mask)
2027 update_texture_4bpp_cache(psx_gpu);
2028
2029 while(num_blocks)
2030 {
2031 texels.e[0] = texture_ptr_8bpp[block->uv.e[0]];
2032 texels.e[1] = texture_ptr_8bpp[block->uv.e[1]];
2033 texels.e[2] = texture_ptr_8bpp[block->uv.e[2]];
2034 texels.e[3] = texture_ptr_8bpp[block->uv.e[3]];
2035 texels.e[4] = texture_ptr_8bpp[block->uv.e[4]];
2036 texels.e[5] = texture_ptr_8bpp[block->uv.e[5]];
2037 texels.e[6] = texture_ptr_8bpp[block->uv.e[6]];
2038 texels.e[7] = texture_ptr_8bpp[block->uv.e[7]];
2039
2040 tbl_16(texels_low, texels, clut_low);
2041 tbl_16(texels_high, texels, clut_high);
2042
2043 // Can be done with an interleaving store on NEON
2044 zip_8x16b(pixels, texels_low, texels_high);
2045
2046 block->texels = pixels;
2047
2048 num_blocks--;
2049 block++;
2050 }
2051}
2052
2053void texture_blocks_8bpp(psx_gpu_struct *psx_gpu)
2054{
2055 block_struct *block = psx_gpu->blocks;
2056 u32 num_blocks = psx_gpu->num_blocks;
2057
2058 texel_blocks_8bpp += num_blocks;
2059
2060 if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_8bpp_mask)
2061 update_texture_8bpp_cache(psx_gpu);
2062
2063 vec_8x16u texels;
2064 u8 *texture_ptr_8bpp = psx_gpu->texture_page_ptr;
2065
2066 u32 texel;
2067 u32 offset;
2068 u32 i;
2069
2070 while(num_blocks)
2071 {
2072 for(i = 0; i < 8; i++)
2073 {
2074 offset = block->uv.e[i];
2075
2076 texel = texture_ptr_8bpp[offset];
2077 texels.e[i] = psx_gpu->clut_ptr[texel];
2078 }
2079
2080 block->texels = texels;
2081
2082 num_blocks--;
2083 block++;
2084 }
2085}
2086
2087void texture_blocks_16bpp(psx_gpu_struct *psx_gpu)
2088{
2089 block_struct *block = psx_gpu->blocks;
2090 u32 num_blocks = psx_gpu->num_blocks;
2091
2092 texel_blocks_16bpp += num_blocks;
2093
2094 vec_8x16u texels;
2095
2096 u16 *texture_ptr_16bpp = psx_gpu->texture_page_ptr;
2097 u32 offset;
2098 u32 i;
2099
2100 while(num_blocks)
2101 {
2102 for(i = 0; i < 8; i++)
2103 {
2104 offset = block->uv.e[i];
2105 offset += ((offset & 0xFF00) * 3);
2106
2107 texels.e[i] = texture_ptr_16bpp[offset];
2108 }
2109
2110 block->texels = texels;
2111
2112 num_blocks--;
2113 block++;
2114 }
2115}
2116
2117#endif
2118
2119
2120#define shade_blocks_load_msb_mask_indirect() \
2121
2122#define shade_blocks_load_msb_mask_direct() \
2123 vec_8x16u msb_mask; \
2124 dup_8x16b(msb_mask, psx_gpu->mask_msb); \
2125
2126#define shade_blocks_store_indirect(_draw_mask, _pixels) \
2127 block->draw_mask = _draw_mask; \
2128 block->pixels = _pixels \
2129
2130#define shade_blocks_store_direct(_draw_mask, _pixels) \
2131{ \
2132 vec_8x16u fb_pixels; \
2133 or_8x16b(_pixels, _pixels, msb_mask); \
2134 load_8x16b(fb_pixels, block->fb_ptr); \
2135 bif_8x16b(fb_pixels, _pixels, _draw_mask); \
2136 store_8x16b(fb_pixels, block->fb_ptr); \
2137} \
2138
2139
3867c6ef 2140#define shade_blocks_textured_false_modulated_check_dithered(target) \
b7ed0632
E
2141 if(psx_gpu->triangle_color == 0x808080) \
2142 { \
2143 false_modulated_blocks += num_blocks; \
2144 } \
3867c6ef
E
2145
2146#define shade_blocks_textured_false_modulated_check_undithered(target) \
2147 if(psx_gpu->triangle_color == 0x808080) \
2148 { \
2149 \
2150 shade_blocks_textured_unmodulated_##target(psx_gpu); \
2151 false_modulated_blocks += num_blocks; \
2152 return; \
2153 } \
2154
2155
2156#define shade_blocks_textured_modulated_shaded_primitive_load(dithering, \
2157 target) \
75e28f62 2158
3867c6ef
E
2159#define shade_blocks_textured_modulated_unshaded_primitive_load(dithering, \
2160 target) \
75e28f62
E
2161{ \
2162 u32 color = psx_gpu->triangle_color; \
2163 dup_8x8b(colors_r, color); \
2164 dup_8x8b(colors_g, color >> 8); \
2165 dup_8x8b(colors_b, color >> 16); \
3867c6ef 2166 shade_blocks_textured_false_modulated_check_##dithering(target); \
75e28f62
E
2167} \
2168
2169#define shade_blocks_textured_modulated_shaded_block_load() \
2170 colors_r = block->r; \
2171 colors_g = block->g; \
2172 colors_b = block->b \
2173
2174#define shade_blocks_textured_modulated_unshaded_block_load() \
2175
2176#define shade_blocks_textured_modulate_dithered(component) \
2177 pixels_##component = block->dither_offsets; \
2178 mla_long_8x8b(pixels_##component, texels_##component, colors_##component) \
2179
2180#define shade_blocks_textured_modulate_undithered(component) \
2181 mul_long_8x8b(pixels_##component, texels_##component, colors_##component) \
2182
2183#define shade_blocks_textured_modulated_builder(shading, dithering, target) \
2184void shade_blocks_##shading##_textured_modulated_##dithering##_##target( \
2185 psx_gpu_struct *psx_gpu) \
2186{ \
2187 block_struct *block = psx_gpu->blocks; \
2188 u32 num_blocks = psx_gpu->num_blocks; \
2189 vec_8x16u texels; \
2190 \
2191 vec_8x8u texels_r; \
2192 vec_8x8u texels_g; \
2193 vec_8x8u texels_b; \
2194 \
2195 vec_8x8u colors_r; \
2196 vec_8x8u colors_g; \
2197 vec_8x8u colors_b; \
2198 \
2199 vec_8x8u pixels_r_low; \
2200 vec_8x8u pixels_g_low; \
2201 vec_8x8u pixels_b_low; \
2202 vec_8x16u pixels; \
2203 \
2204 vec_8x16u pixels_r; \
2205 vec_8x16u pixels_g; \
2206 vec_8x16u pixels_b; \
2207 \
2208 vec_8x16u draw_mask; \
2209 vec_8x16u zero_mask; \
2210 \
2211 vec_8x8u d64_0x07; \
2212 vec_8x8u d64_0x1F; \
2213 vec_8x8u d64_1; \
2214 vec_8x8u d64_4; \
2215 vec_8x8u d64_128; \
2216 \
2217 vec_8x16u d128_0x8000; \
2218 \
2219 vec_8x16u test_mask = psx_gpu->test_mask; \
2220 u32 draw_mask_bits; \
2221 shade_blocks_load_msb_mask_##target(); \
2222 \
2223 dup_8x8b(d64_0x07, 0x07); \
2224 dup_8x8b(d64_0x1F, 0x1F); \
2225 dup_8x8b(d64_1, 1); \
2226 dup_8x8b(d64_4, 4); \
2227 dup_8x8b(d64_128, 128); \
2228 \
2229 dup_8x16b(d128_0x8000, 0x8000); \
2230 \
3867c6ef
E
2231 shade_blocks_textured_modulated_##shading##_primitive_load(dithering, \
2232 target); \
75e28f62
E
2233 \
2234 while(num_blocks) \
2235 { \
2236 draw_mask_bits = block->draw_mask_bits; \
2237 dup_8x16b(draw_mask, draw_mask_bits); \
2238 tst_8x16b(draw_mask, draw_mask, test_mask); \
2239 \
2240 shade_blocks_textured_modulated_##shading##_block_load(); \
2241 \
2242 texels = block->texels; \
2243 \
2244 mov_narrow_8x16b(texels_r, texels); \
2245 shr_narrow_8x16b(texels_g, texels, 5); \
2246 shr_narrow_8x16b(texels_b, texels, 7); \
2247 \
2248 and_8x8b(texels_r, texels_r, d64_0x1F); \
2249 and_8x8b(texels_g, texels_g, d64_0x1F); \
2250 shr_8x8b(texels_b, texels_b, 3); \
2251 \
2252 shade_blocks_textured_modulate_##dithering(r); \
2253 shade_blocks_textured_modulate_##dithering(g); \
2254 shade_blocks_textured_modulate_##dithering(b); \
2255 \
2256 cmpeqz_8x16b(zero_mask, texels); \
2257 and_8x16b(pixels, texels, d128_0x8000); \
2258 \
2259 shrq_narrow_signed_8x16b(pixels_r_low, pixels_r, 4); \
2260 shrq_narrow_signed_8x16b(pixels_g_low, pixels_g, 4); \
2261 shrq_narrow_signed_8x16b(pixels_b_low, pixels_b, 4); \
2262 \
2263 or_8x16b(zero_mask, draw_mask, zero_mask); \
2264 \
2265 shr_8x8b(pixels_r_low, pixels_r_low, 3); \
2266 bic_8x8b(pixels_g_low, pixels_g_low, d64_0x07); \
2267 bic_8x8b(pixels_b_low, pixels_b_low, d64_0x07); \
2268 \
2269 mla_long_8x8b(pixels, pixels_r_low, d64_1); \
2270 mla_long_8x8b(pixels, pixels_g_low, d64_4); \
2271 mla_long_8x8b(pixels, pixels_b_low, d64_128); \
2272 \
2273 shade_blocks_store_##target(zero_mask, pixels); \
2274 \
2275 num_blocks--; \
2276 block++; \
2277 } \
2278} \
2279
2280void shade_blocks_shaded_textured_modulated_dithered_direct(psx_gpu_struct
2281 *psx_gpu);
2282void shade_blocks_shaded_textured_modulated_undithered_direct(psx_gpu_struct
2283 *psx_gpu);
2284void shade_blocks_unshaded_textured_modulated_dithered_direct(psx_gpu_struct
2285 *psx_gpu);
2286void shade_blocks_unshaded_textured_modulated_undithered_direct(psx_gpu_struct
2287 *psx_gpu);
2288
2289void shade_blocks_shaded_textured_modulated_dithered_indirect(psx_gpu_struct
2290 *psx_gpu);
2291void shade_blocks_shaded_textured_modulated_undithered_indirect(psx_gpu_struct
2292 *psx_gpu);
2293void shade_blocks_unshaded_textured_modulated_dithered_indirect(psx_gpu_struct
2294 *psx_gpu);
2295void shade_blocks_unshaded_textured_modulated_undithered_indirect(psx_gpu_struct
2296 *psx_gpu);
2297
3867c6ef
E
2298void shade_blocks_textured_unmodulated_indirect(psx_gpu_struct *psx_gpu);
2299void shade_blocks_textured_unmodulated_direct(psx_gpu_struct *psx_gpu);
2300
2bbbb7af 2301#ifndef NEON_BUILD
75e28f62
E
2302
2303shade_blocks_textured_modulated_builder(shaded, dithered, direct);
2304shade_blocks_textured_modulated_builder(shaded, undithered, direct);
2305shade_blocks_textured_modulated_builder(unshaded, dithered, direct);
2306shade_blocks_textured_modulated_builder(unshaded, undithered, direct);
2307
2308shade_blocks_textured_modulated_builder(shaded, dithered, indirect);
2309shade_blocks_textured_modulated_builder(shaded, undithered, indirect);
2310shade_blocks_textured_modulated_builder(unshaded, dithered, indirect);
2311shade_blocks_textured_modulated_builder(unshaded, undithered, indirect);
2312
2313#endif
2314
2315
2316#define shade_blocks_textured_unmodulated_builder(target) \
2317void shade_blocks_textured_unmodulated_##target(psx_gpu_struct *psx_gpu) \
2318{ \
2319 block_struct *block = psx_gpu->blocks; \
2320 u32 num_blocks = psx_gpu->num_blocks; \
2321 vec_8x16u draw_mask; \
2322 vec_8x16u test_mask = psx_gpu->test_mask; \
2323 u32 draw_mask_bits; \
2324 \
2325 vec_8x16u pixels; \
2326 shade_blocks_load_msb_mask_##target(); \
2327 \
2328 while(num_blocks) \
2329 { \
2330 vec_8x16u zero_mask; \
2331 \
2332 draw_mask_bits = block->draw_mask_bits; \
2333 dup_8x16b(draw_mask, draw_mask_bits); \
2334 tst_8x16b(draw_mask, draw_mask, test_mask); \
2335 \
2336 pixels = block->texels; \
2337 \
2338 cmpeqz_8x16b(zero_mask, pixels); \
2339 or_8x16b(zero_mask, draw_mask, zero_mask); \
2340 \
2341 shade_blocks_store_##target(zero_mask, pixels); \
2342 \
2343 num_blocks--; \
2344 block++; \
2345 } \
2346} \
2347
3867c6ef
E
2348#define shade_blocks_textured_unmodulated_dithered_builder(target) \
2349void shade_blocks_textured_unmodulated_dithered_##target(psx_gpu_struct \
2350 *psx_gpu) \
2351{ \
2352 block_struct *block = psx_gpu->blocks; \
2353 u32 num_blocks = psx_gpu->num_blocks; \
2354 vec_8x16u draw_mask; \
2355 vec_8x16u test_mask = psx_gpu->test_mask; \
2356 u32 draw_mask_bits; \
2357 \
2358 vec_8x16u pixels; \
2359 shade_blocks_load_msb_mask_##target(); \
2360 \
2361 while(num_blocks) \
2362 { \
2363 vec_8x16u zero_mask; \
2364 \
2365 draw_mask_bits = block->draw_mask_bits; \
2366 dup_8x16b(draw_mask, draw_mask_bits); \
2367 tst_8x16b(draw_mask, draw_mask, test_mask); \
2368 \
2369 pixels = block->texels; \
2370 \
2371 cmpeqz_8x16b(zero_mask, pixels); \
2372 or_8x16b(zero_mask, draw_mask, zero_mask); \
2373 \
2374 shade_blocks_store_##target(zero_mask, pixels); \
2375 \
2376 num_blocks--; \
2377 block++; \
2378 } \
2379} \
75e28f62 2380
2bbbb7af 2381#ifndef NEON_BUILD
75e28f62
E
2382
2383shade_blocks_textured_unmodulated_builder(indirect)
2384shade_blocks_textured_unmodulated_builder(direct)
2385
2386#endif
2387
2388
2389void shade_blocks_unshaded_untextured_indirect(psx_gpu_struct *psx_gpu);
2390void shade_blocks_unshaded_untextured_direct(psx_gpu_struct *psx_gpu);
2391
2bbbb7af 2392#ifndef NEON_BUILD
f9248bbf 2393
75e28f62
E
2394void shade_blocks_unshaded_untextured_indirect(psx_gpu_struct *psx_gpu)
2395{
2396}
2397
2398void shade_blocks_unshaded_untextured_direct(psx_gpu_struct *psx_gpu)
2399{
2400 block_struct *block = psx_gpu->blocks;
2401 u32 num_blocks = psx_gpu->num_blocks;
2402
2403 vec_8x16u pixels = block->pixels;
2404 shade_blocks_load_msb_mask_direct();
2405
2406 while(num_blocks)
2407 {
2408 shade_blocks_store_direct(block->draw_mask, pixels);
2409
2410 num_blocks--;
2411 block++;
2412 }
2413}
2414
2415#endif
2416
2417void shade_blocks_shaded_untextured(psx_gpu_struct *psx_gpu)
2418{
2419}
2420
2421
2422#define blend_blocks_mask_evaluate_on() \
2423 vec_8x16u mask_pixels; \
2424 cmpltz_8x16b(mask_pixels, framebuffer_pixels); \
2425 or_8x16b(draw_mask, draw_mask, mask_pixels) \
2426
2427#define blend_blocks_mask_evaluate_off() \
2428
2429#define blend_blocks_average() \
2430{ \
2431 vec_8x16u pixels_no_msb; \
2432 vec_8x16u fb_pixels_no_msb; \
2433 \
2434 vec_8x16u d128_0x0421; \
2435 vec_8x16u d128_0x8000; \
2436 \
2437 dup_8x16b(d128_0x0421, 0x0421); \
2438 dup_8x16b(d128_0x8000, 0x8000); \
2439 \
2440 eor_8x16b(blend_pixels, pixels, framebuffer_pixels); \
2441 bic_8x16b(pixels_no_msb, pixels, d128_0x8000); \
2442 and_8x16b(blend_pixels, blend_pixels, d128_0x0421); \
2443 sub_8x16b(blend_pixels, pixels_no_msb, blend_pixels); \
2444 bic_8x16b(fb_pixels_no_msb, framebuffer_pixels, d128_0x8000); \
2445 average_8x16b(blend_pixels, fb_pixels_no_msb, blend_pixels); \
2446} \
2447
2448#define blend_blocks_add() \
2449{ \
2450 vec_8x16u pixels_rb, pixels_g; \
2451 vec_8x16u fb_rb, fb_g; \
2452 \
2453 vec_8x16u d128_0x7C1F; \
2454 vec_8x16u d128_0x03E0; \
2455 \
2456 dup_8x16b(d128_0x7C1F, 0x7C1F); \
2457 dup_8x16b(d128_0x03E0, 0x03E0); \
2458 \
2459 and_8x16b(pixels_rb, pixels, d128_0x7C1F); \
2460 and_8x16b(pixels_g, pixels, d128_0x03E0); \
2461 \
2462 and_8x16b(fb_rb, framebuffer_pixels, d128_0x7C1F); \
2463 and_8x16b(fb_g, framebuffer_pixels, d128_0x03E0); \
2464 \
2465 add_8x16b(fb_rb, fb_rb, pixels_rb); \
2466 add_8x16b(fb_g, fb_g, pixels_g); \
2467 \
2468 min_16x8b(vector_cast(vec_16x8u, fb_rb), vector_cast(vec_16x8u, fb_rb), \
2469 vector_cast(vec_16x8u, d128_0x7C1F)); \
2470 min_8x16b(fb_g, fb_g, d128_0x03E0); \
2471 \
2472 or_8x16b(blend_pixels, fb_rb, fb_g); \
2473} \
2474
2475#define blend_blocks_subtract() \
2476{ \
2477 vec_8x16u pixels_rb, pixels_g; \
2478 vec_8x16u fb_rb, fb_g; \
2479 \
2480 vec_8x16u d128_0x7C1F; \
2481 vec_8x16u d128_0x03E0; \
2482 \
2483 dup_8x16b(d128_0x7C1F, 0x7C1F); \
2484 dup_8x16b(d128_0x03E0, 0x03E0); \
2485 \
2486 and_8x16b(pixels_rb, pixels, d128_0x7C1F); \
2487 and_8x16b(pixels_g, pixels, d128_0x03E0); \
2488 \
2489 and_8x16b(fb_rb, framebuffer_pixels, d128_0x7C1F); \
2490 and_8x16b(fb_g, framebuffer_pixels, d128_0x03E0); \
2491 \
2492 subs_16x8b(vector_cast(vec_16x8u, fb_rb), \
2493 vector_cast(vec_16x8u, fb_rb), vector_cast(vec_16x8u, pixels_rb)); \
2494 subs_8x16b(fb_g, fb_g, pixels_g); \
2495 \
2496 or_8x16b(blend_pixels, fb_rb, fb_g); \
2497} \
2498
2499#define blend_blocks_add_fourth() \
2500{ \
2501 vec_8x16u pixels_rb, pixels_g; \
2502 vec_8x16u pixels_fourth; \
2503 vec_8x16u fb_rb, fb_g; \
2504 \
2505 vec_8x16u d128_0x7C1F; \
2506 vec_8x16u d128_0x1C07; \
2507 vec_8x16u d128_0x03E0; \
2508 vec_8x16u d128_0x00E0; \
2509 \
2510 dup_8x16b(d128_0x7C1F, 0x7C1F); \
2511 dup_8x16b(d128_0x1C07, 0x1C07); \
2512 dup_8x16b(d128_0x03E0, 0x03E0); \
2513 dup_8x16b(d128_0x00E0, 0x00E0); \
2514 \
2515 shr_8x16b(pixels_fourth, vector_cast(vec_8x16s, pixels), 2); \
2516 \
2517 and_8x16b(fb_rb, framebuffer_pixels, d128_0x7C1F); \
2518 and_8x16b(fb_g, framebuffer_pixels, d128_0x03E0); \
2519 \
2520 and_8x16b(pixels_rb, pixels_fourth, d128_0x1C07); \
2521 and_8x16b(pixels_g, pixels_fourth, d128_0x00E0); \
2522 \
2523 add_8x16b(fb_rb, fb_rb, pixels_rb); \
2524 add_8x16b(fb_g, fb_g, pixels_g); \
2525 \
2526 min_16x8b(vector_cast(vec_16x8u, fb_rb), vector_cast(vec_16x8u, fb_rb), \
2527 vector_cast(vec_16x8u, d128_0x7C1F)); \
2528 min_8x16b(fb_g, fb_g, d128_0x03E0); \
2529 \
2530 or_8x16b(blend_pixels, fb_rb, fb_g); \
2531} \
2532
2533#define blend_blocks_blended_combine_textured() \
2534{ \
2535 vec_8x16u blend_mask; \
2536 cmpltz_8x16b(blend_mask, pixels); \
2537 \
2538 or_immediate_8x16b(blend_pixels, blend_pixels, 0x8000); \
2539 bif_8x16b(blend_pixels, pixels, blend_mask); \
2540} \
2541
2542#define blend_blocks_blended_combine_untextured() \
2543
2544
2545#define blend_blocks_body_blend(blend_mode, texturing) \
2546{ \
2547 blend_blocks_##blend_mode(); \
2548 blend_blocks_blended_combine_##texturing(); \
2549} \
2550
2551#define blend_blocks_body_average(texturing) \
2552 blend_blocks_body_blend(average, texturing) \
2553
2554#define blend_blocks_body_add(texturing) \
2555 blend_blocks_body_blend(add, texturing) \
2556
2557#define blend_blocks_body_subtract(texturing) \
2558 blend_blocks_body_blend(subtract, texturing) \
2559
2560#define blend_blocks_body_add_fourth(texturing) \
2561 blend_blocks_body_blend(add_fourth, texturing) \
2562
2563#define blend_blocks_body_unblended(texturing) \
2564 blend_pixels = pixels \
2565
2566
2567#define blend_blocks_builder(texturing, blend_mode, mask_evaluate) \
2568void \
2569 blend_blocks_##texturing##_##blend_mode##_##mask_evaluate(psx_gpu_struct \
2570 *psx_gpu) \
2571{ \
2572 block_struct *block = psx_gpu->blocks; \
2573 u32 num_blocks = psx_gpu->num_blocks; \
2574 vec_8x16u draw_mask; \
2575 vec_8x16u pixels; \
2576 vec_8x16u blend_pixels; \
2577 vec_8x16u framebuffer_pixels; \
2578 vec_8x16u msb_mask; \
2579 \
2580 u16 *fb_ptr; \
2581 \
2582 dup_8x16b(msb_mask, psx_gpu->mask_msb); \
2583 \
2584 while(num_blocks) \
2585 { \
2586 pixels = block->pixels; \
2587 draw_mask = block->draw_mask; \
2588 fb_ptr = block->fb_ptr; \
2589 \
2590 load_8x16b(framebuffer_pixels, fb_ptr); \
2591 \
2592 blend_blocks_mask_evaluate_##mask_evaluate(); \
2593 blend_blocks_body_##blend_mode(texturing); \
2594 \
2595 or_8x16b(blend_pixels, blend_pixels, msb_mask); \
2596 bif_8x16b(framebuffer_pixels, blend_pixels, draw_mask); \
2597 store_8x16b(framebuffer_pixels, fb_ptr); \
2598 \
2599 blend_blocks++; \
2600 num_blocks--; \
2601 block++; \
2602 } \
2603} \
2604
2605void blend_blocks_textured_average_off(psx_gpu_struct *psx_gpu);
2606void blend_blocks_textured_average_on(psx_gpu_struct *psx_gpu);
2607void blend_blocks_textured_add_off(psx_gpu_struct *psx_gpu);
2608void blend_blocks_textured_add_on(psx_gpu_struct *psx_gpu);
2609void blend_blocks_textured_subtract_off(psx_gpu_struct *psx_gpu);
2610void blend_blocks_textured_subtract_on(psx_gpu_struct *psx_gpu);
2611void blend_blocks_textured_add_fourth_off(psx_gpu_struct *psx_gpu);
2612void blend_blocks_textured_add_fourth_on(psx_gpu_struct *psx_gpu);
2613
2614void blend_blocks_untextured_average_off(psx_gpu_struct *psx_gpu);
2615void blend_blocks_untextured_average_on(psx_gpu_struct *psx_gpu);
2616void blend_blocks_untextured_add_off(psx_gpu_struct *psx_gpu);
2617void blend_blocks_untextured_add_on(psx_gpu_struct *psx_gpu);
2618void blend_blocks_untextured_subtract_off(psx_gpu_struct *psx_gpu);
2619void blend_blocks_untextured_subtract_on(psx_gpu_struct *psx_gpu);
2620void blend_blocks_untextured_add_fourth_off(psx_gpu_struct *psx_gpu);
2621void blend_blocks_untextured_add_fourth_on(psx_gpu_struct *psx_gpu);
2622
2623void blend_blocks_textured_unblended_off(psx_gpu_struct *psx_gpu);
2624void blend_blocks_textured_unblended_on(psx_gpu_struct *psx_gpu);
2625
2bbbb7af 2626#ifndef NEON_BUILD
75e28f62
E
2627
2628void blend_blocks_textured_unblended_off(psx_gpu_struct *psx_gpu)
2629{
2630}
2631
2632blend_blocks_builder(textured, average, off);
2633blend_blocks_builder(textured, average, on);
2634blend_blocks_builder(textured, add, off);
2635blend_blocks_builder(textured, add, on);
2636blend_blocks_builder(textured, subtract, off);
2637blend_blocks_builder(textured, subtract, on);
2638blend_blocks_builder(textured, add_fourth, off);
2639blend_blocks_builder(textured, add_fourth, on);
2640
2641blend_blocks_builder(untextured, average, off);
2642blend_blocks_builder(untextured, average, on);
2643blend_blocks_builder(untextured, add, off);
2644blend_blocks_builder(untextured, add, on);
2645blend_blocks_builder(untextured, subtract, off);
2646blend_blocks_builder(untextured, subtract, on);
2647blend_blocks_builder(untextured, add_fourth, off);
2648blend_blocks_builder(untextured, add_fourth, on);
2649
2650blend_blocks_builder(textured, unblended, on);
2651
2652#endif
2653
2654
2655#define vertex_swap(_a, _b) \
2656{ \
2657 vertex_struct *temp_vertex = _a; \
2658 _a = _b; \
2659 _b = temp_vertex; \
2660 triangle_winding ^= 1; \
2661} \
2662
2663
2664// Setup blocks parametric-variables:
2665// SHADE TEXTURE_MAP SWIZZLING
2666// 0 0 x
2667// 0 1 0
2668// 0 1 1
2669// 1 0 x
2670// 1 1 0
2671// 1 1 1
2672// 8 inputs, 6 combinations
2673
2674#define setup_blocks_switch_untextured_unshaded(dithering, target) \
2675 setup_blocks_unshaded_untextured_undithered_unswizzled_##target \
2676
2677#define setup_blocks_switch_untextured_shaded(dithering, target) \
2678 setup_blocks_shaded_untextured_##dithering##_unswizzled_##target \
2679
2680#define setup_blocks_switch_untextured(shading, texture_mode, dithering, \
2681 target) \
2682 setup_blocks_switch_untextured_##shading(dithering, target) \
2683
2684#define setup_blocks_switch_texture_mode_4bpp(shading) \
2685 setup_blocks_##shading##_textured_dithered_swizzled_indirect \
2686
2687#define setup_blocks_switch_texture_mode_8bpp(shading) \
2688 setup_blocks_##shading##_textured_dithered_swizzled_indirect \
2689
2690#define setup_blocks_switch_texture_mode_16bpp(shading) \
2691 setup_blocks_##shading##_textured_dithered_unswizzled_indirect \
2692
2693#define setup_blocks_switch_textured(shading, texture_mode, dithering, target) \
2694 setup_blocks_switch_texture_mode_##texture_mode(shading) \
2695
2696#define setup_blocks_switch_blended(shading, texturing, texture_mode, \
2697 dithering, mask_evaluate) \
2698 setup_blocks_switch_##texturing(shading, texture_mode, dithering, indirect) \
2699
2700#define setup_blocks_switch_unblended_on(shading, texturing, texture_mode, \
2701 dithering) \
2702 setup_blocks_switch_##texturing(shading, texture_mode, dithering, indirect) \
2703
2704#define setup_blocks_switch_unblended_off(shading, texturing, texture_mode, \
2705 dithering) \
2706 setup_blocks_switch_##texturing(shading, texture_mode, dithering, direct) \
2707
2708#define setup_blocks_switch_unblended(shading, texturing, texture_mode, \
2709 dithering, mask_evaluate) \
2710 setup_blocks_switch_unblended_##mask_evaluate(shading, texturing, \
2711 texture_mode, dithering) \
2712
2713#define setup_blocks_switch(shading, texturing, texture_mode, dithering, \
2714 blending, mask_evaluate) \
2715 setup_blocks_switch_##blending(shading, texturing, texture_mode, \
2716 dithering, mask_evaluate) \
2717
2718
2719// Texture blocks:
2720
2721#define texture_blocks_switch_untextured(texture_mode) \
2722 texture_blocks_untextured \
2723
2724#define texture_blocks_switch_textured(texture_mode) \
2725 texture_blocks_##texture_mode \
2726
2727#define texture_blocks_switch(texturing, texture_mode) \
2728 texture_blocks_switch_##texturing(texture_mode) \
2729
2730
2731// Shade blocks parametric-variables:
2732// SHADE TEXTURE_MAP MODULATE_TEXELS dither_mode
2733// 0 0 x x
2734// 0 1 0 0
2735// 0 1 0 1
2736// x 1 1 x
2737// 1 0 x 0
2738// 1 0 x 1
2739// 1 1 0 0
2740// 1 1 0 1
2741// 16 inputs, 8 combinations
2742
2743#define shade_blocks_switch_unshaded_untextured(modulation, dithering, target) \
2744 shade_blocks_unshaded_untextured_##target \
2745
2746#define shade_blocks_switch_unshaded_textured_unmodulated(dithering, target) \
2747 shade_blocks_textured_unmodulated_##target \
2748
2749#define shade_blocks_switch_unshaded_textured_modulated(dithering, target) \
2750 shade_blocks_unshaded_textured_modulated_##dithering##_##target \
2751
2752#define shade_blocks_switch_unshaded_textured(modulation, dithering, target) \
2753 shade_blocks_switch_unshaded_textured_##modulation(dithering, target) \
2754
2755#define shade_blocks_switch_unshaded(texturing, modulation, dithering, target) \
2756 shade_blocks_switch_unshaded_##texturing(modulation, dithering, target) \
2757
2758#define shade_blocks_switch_shaded_untextured(modulation, dithering, target) \
2759 shade_blocks_shaded_untextured \
2760
2761#define shade_blocks_switch_shaded_textured_unmodulated(dithering, target) \
2762 shade_blocks_textured_unmodulated_##target \
2763
2764#define shade_blocks_switch_shaded_textured_modulated(dithering, target) \
2765 shade_blocks_shaded_textured_modulated_##dithering##_##target \
2766
2767#define shade_blocks_switch_shaded_textured(modulation, dithering, target) \
2768 shade_blocks_switch_shaded_textured_##modulation(dithering, target) \
2769
2770#define shade_blocks_switch_shaded(texturing, modulation, dithering, target) \
2771 shade_blocks_switch_shaded_##texturing(modulation, dithering, target) \
2772
2773#define shade_blocks_switch_mask_off(shading, texturing, modulation, \
2774 dithering) \
2775 shade_blocks_switch_##shading(texturing, modulation, dithering, direct) \
2776
2777#define shade_blocks_switch_mask_on(shading, texturing, modulation, \
2778 dithering) \
2779 shade_blocks_switch_##shading(texturing, modulation, dithering, indirect) \
2780
2781#define shade_blocks_switch_blended(shading, texturing, modulation, dithering, \
2782 mask_evaluate) \
2783 shade_blocks_switch_##shading(texturing, modulation, dithering, indirect) \
2784
2785#define shade_blocks_switch_unblended(shading, texturing, modulation, \
2786 dithering, mask_evaluate) \
2787 shade_blocks_switch_mask_##mask_evaluate(shading, texturing, modulation, \
2788 dithering) \
2789
2790#define shade_blocks_switch(shading, texturing, modulation, dithering, \
2791 blending, mask_evaluate) \
2792 shade_blocks_switch_##blending(shading, texturing, modulation, dithering, \
2793 mask_evaluate) \
2794
2795
2796// Blend blocks parametric-variables:
2797// TEXTURE_MAP BLEND BM_A BM_B mask_evaluate
2798// x 0 x x 0
2799// x 0 x x 1
2800// 0 1 0 0 0
2801// 0 1 0 0 1
2802// 0 1 0 1 0
2803// 0 1 0 1 1
2804// 0 1 1 0 0
2805// 0 1 1 0 1
2806// 0 1 1 1 0
2807// 0 1 1 1 1
2808// 1 1 0 0 0
2809// 1 1 0 0 1
2810// 1 1 0 1 0
2811// 1 1 0 1 1
2812// 1 1 1 0 0
2813// 1 1 1 0 1
2814// 1 1 1 1 0
2815// 1 1 1 1 1
2816// 32 inputs, 18 combinations
2817
2818#define blend_blocks_switch_unblended(texturing, blend_mode, mask_evaluate) \
2819 blend_blocks_textured_unblended_##mask_evaluate \
2820
2821#define blend_blocks_switch_blended(texturing, blend_mode, mask_evaluate) \
2822 blend_blocks_##texturing##_##blend_mode##_##mask_evaluate \
2823
2824#define blend_blocks_switch(texturing, blending, blend_mode, mask_evaluate) \
2825 blend_blocks_switch_##blending(texturing, blend_mode, mask_evaluate) \
2826
2827
2828#define render_blocks_switch_block_modulation(texture_mode, blend_mode, \
2829 mask_evaluate, shading, dithering, texturing, blending, modulation) \
2830{ \
2831 setup_blocks_switch(shading, texturing, texture_mode, dithering, blending, \
2832 mask_evaluate), \
2833 texture_blocks_switch(texturing, texture_mode), \
2834 shade_blocks_switch(shading, texturing, modulation, dithering, blending, \
2835 mask_evaluate), \
2836 blend_blocks_switch(texturing, blending, blend_mode, mask_evaluate) \
2837} \
2838
2839#define render_blocks_switch_block_blending(texture_mode, blend_mode, \
2840 mask_evaluate, shading, dithering, texturing, blending) \
2841 render_blocks_switch_block_modulation(texture_mode, blend_mode, \
2842 mask_evaluate, shading, dithering, texturing, blending, modulated), \
2843 render_blocks_switch_block_modulation(texture_mode, blend_mode, \
2844 mask_evaluate, shading, dithering, texturing, blending, unmodulated) \
2845
2846#define render_blocks_switch_block_texturing(texture_mode, blend_mode, \
2847 mask_evaluate, shading, dithering, texturing) \
2848 render_blocks_switch_block_blending(texture_mode, blend_mode, \
2849 mask_evaluate, shading, dithering, texturing, unblended), \
2850 render_blocks_switch_block_blending(texture_mode, blend_mode, \
2851 mask_evaluate, shading, dithering, texturing, blended) \
2852
2853#define render_blocks_switch_block_dithering(texture_mode, blend_mode, \
2854 mask_evaluate, shading, dithering) \
2855 render_blocks_switch_block_texturing(texture_mode, blend_mode, \
2856 mask_evaluate, shading, dithering, untextured), \
2857 render_blocks_switch_block_texturing(texture_mode, blend_mode, \
2858 mask_evaluate, shading, dithering, textured) \
2859
2860#define render_blocks_switch_block_shading(texture_mode, blend_mode, \
2861 mask_evaluate, shading) \
2862 render_blocks_switch_block_dithering(texture_mode, blend_mode, \
2863 mask_evaluate, shading, undithered), \
2864 render_blocks_switch_block_dithering(texture_mode, blend_mode, \
2865 mask_evaluate, shading, dithered) \
2866
2867#define render_blocks_switch_block_mask_evaluate(texture_mode, blend_mode, \
2868 mask_evaluate) \
2869 render_blocks_switch_block_shading(texture_mode, blend_mode, mask_evaluate, \
2870 unshaded), \
2871 render_blocks_switch_block_shading(texture_mode, blend_mode, mask_evaluate, \
2872 shaded) \
2873
2874#define render_blocks_switch_block_blend_mode(texture_mode, blend_mode) \
2875 render_blocks_switch_block_mask_evaluate(texture_mode, blend_mode, off), \
2876 render_blocks_switch_block_mask_evaluate(texture_mode, blend_mode, on) \
2877
2878#define render_blocks_switch_block_texture_mode(texture_mode) \
2879 render_blocks_switch_block_blend_mode(texture_mode, average), \
2880 render_blocks_switch_block_blend_mode(texture_mode, add), \
2881 render_blocks_switch_block_blend_mode(texture_mode, subtract), \
2882 render_blocks_switch_block_blend_mode(texture_mode, add_fourth) \
2883
2884#define render_blocks_switch_block() \
2885 render_blocks_switch_block_texture_mode(4bpp), \
2886 render_blocks_switch_block_texture_mode(8bpp), \
2887 render_blocks_switch_block_texture_mode(16bpp), \
2888 render_blocks_switch_block_texture_mode(4bpp) \
2889
2890
2891render_block_handler_struct render_triangle_block_handlers[] =
2892{
2893 render_blocks_switch_block()
2894};
2895
2896#undef render_blocks_switch_block_modulation
2897
2898#define render_blocks_switch_block_modulation(texture_mode, blend_mode, \
2899 mask_evaluate, shading, dithering, texturing, blending, modulation) \
2900 "render flags:\n" \
2901 "texture mode: " #texture_mode "\n" \
2902 "blend mode: " #blend_mode "\n" \
2903 "mask evaluation: " #mask_evaluate "\n" \
2904 #shading "\n" \
2905 #dithering "\n" \
2906 #texturing "\n" \
2907 #blending "\n" \
2908 #modulation "\n" \
2909
2910char *render_block_flag_strings[] =
2911{
2912 render_blocks_switch_block()
2913};
2914
2915
2916#define triangle_y_direction_up 1
2917#define triangle_y_direction_flat 2
2918#define triangle_y_direction_down 0
2919
2920#define triangle_winding_positive 0
2921#define triangle_winding_negative 1
2922
2923#define triangle_set_direction(direction_variable, value) \
2924 u32 direction_variable = (u32)(value) >> 31; \
2925 if(value == 0) \
2926 direction_variable = 2 \
2927
2928#define triangle_case(direction_a, direction_b, direction_c, winding) \
2929 case (triangle_y_direction_##direction_a | \
2930 (triangle_y_direction_##direction_b << 2) | \
2931 (triangle_y_direction_##direction_c << 4) | \
2932 (triangle_winding_##winding << 6)) \
2933
c1817bd9 2934static int prepare_triangle(psx_gpu_struct *psx_gpu, vertex_struct *vertexes,
2935 vertex_struct *vertexes_out[3])
75e28f62
E
2936{
2937 s32 y_top, y_bottom;
2938 s32 triangle_area;
2939 u32 triangle_winding = 0;
2940
2941 vertex_struct *a = &(vertexes[0]);
2942 vertex_struct *b = &(vertexes[1]);
2943 vertex_struct *c = &(vertexes[2]);
2944
2945 triangle_area = triangle_signed_area_x2(a->x, a->y, b->x, b->y, c->x, c->y);
2946
3867c6ef 2947#ifdef PROFILE
75e28f62 2948 triangles++;
3867c6ef 2949#endif
75e28f62
E
2950
2951 if(triangle_area == 0)
2952 {
3867c6ef 2953#ifdef PROFILE
75e28f62 2954 trivial_rejects++;
3867c6ef 2955#endif
c1817bd9 2956 return 0;
75e28f62
E
2957 }
2958
2959 if(b->y < a->y)
2960 vertex_swap(a, b);
2961
2962 if(c->y < b->y)
2963 {
2964 vertex_swap(b, c);
2965
2966 if(b->y < a->y)
2967 vertex_swap(a, b);
2968 }
2969
2970 y_bottom = c->y;
2971 y_top = a->y;
2972
2973 if((y_bottom - y_top) >= 512)
2974 {
3867c6ef 2975#ifdef PROFILE
75e28f62 2976 trivial_rejects++;
3867c6ef 2977#endif
c1817bd9 2978 return 0;
75e28f62
E
2979 }
2980
2981 if(triangle_area < 0)
2982 {
2983 triangle_area = -triangle_area;
2984 triangle_winding ^= 1;
2985 vertex_swap(a, c);
2986 }
2987
2988 if(b->x < a->x)
2989 vertex_swap(a, b);
2990
2991 if(c->x < b->x)
2992 {
2993 vertex_swap(b, c);
2994
2995 if(b->x < a->x)
2996 vertex_swap(a, b);
2997 }
2998
af044cbf 2999 if((c->x - psx_gpu->offset_x) >= 1024 || (c->x - a->x) >= 1024)
75e28f62 3000 {
3867c6ef 3001#ifdef PROFILE
75e28f62 3002 trivial_rejects++;
3867c6ef 3003#endif
c1817bd9 3004 return 0;
75e28f62
E
3005 }
3006
3007 if(invalidate_texture_cache_region_viewport(psx_gpu, a->x, y_top, c->x,
3008 y_bottom) == 0)
3009 {
3867c6ef 3010#ifdef PROFILE
75e28f62 3011 trivial_rejects++;
3867c6ef 3012#endif
c1817bd9 3013 return 0;
75e28f62
E
3014 }
3015
75e28f62
E
3016 psx_gpu->triangle_area = triangle_area;
3017 psx_gpu->triangle_winding = triangle_winding;
3018
c1817bd9 3019 vertexes_out[0] = a;
3020 vertexes_out[1] = b;
3021 vertexes_out[2] = c;
3022
3023 return 1;
3024}
3025
3026static void render_triangle_p(psx_gpu_struct *psx_gpu,
3027 vertex_struct *vertex_ptrs[3], u32 flags)
3028{
3029 psx_gpu->num_spans = 0;
3030
3031 vertex_struct *a = vertex_ptrs[0];
3032 vertex_struct *b = vertex_ptrs[1];
3033 vertex_struct *c = vertex_ptrs[2];
3034
75e28f62
E
3035 s32 y_delta_a = b->y - a->y;
3036 s32 y_delta_b = c->y - b->y;
3037 s32 y_delta_c = c->y - a->y;
3038
3039 triangle_set_direction(y_direction_a, y_delta_a);
3040 triangle_set_direction(y_direction_b, y_delta_b);
3041 triangle_set_direction(y_direction_c, y_delta_c);
3042
3043 compute_all_gradients(psx_gpu, a, b, c);
3044
3045 switch(y_direction_a | (y_direction_b << 2) | (y_direction_c << 4) |
c1817bd9 3046 (psx_gpu->triangle_winding << 6))
75e28f62
E
3047 {
3048 triangle_case(up, up, up, negative):
3049 triangle_case(up, up, flat, negative):
3050 triangle_case(up, up, down, negative):
3051 setup_spans_up_right(psx_gpu, a, b, c);
3052 break;
3053
3054 triangle_case(flat, up, up, negative):
3055 triangle_case(flat, up, flat, negative):
3056 triangle_case(flat, up, down, negative):
3057 setup_spans_up_a(psx_gpu, a, b, c);
3058 break;
3059
3060 triangle_case(down, up, up, negative):
3061 setup_spans_up_down(psx_gpu, a, c, b);
3062 break;
3063
3064 triangle_case(down, up, flat, negative):
3065 setup_spans_down_a(psx_gpu, a, c, b);
3066 break;
3067
3068 triangle_case(down, up, down, negative):
3069 setup_spans_down_right(psx_gpu, a, c, b);
3070 break;
3071
3072 triangle_case(down, flat, up, negative):
3073 triangle_case(down, flat, flat, negative):
3074 triangle_case(down, flat, down, negative):
3075 setup_spans_down_b(psx_gpu, a, b, c);
3076 break;
3077
3078 triangle_case(down, down, up, negative):
3079 triangle_case(down, down, flat, negative):
3080 triangle_case(down, down, down, negative):
3081 setup_spans_down_left(psx_gpu, a, b, c);
3082 break;
3083
3084 triangle_case(up, up, up, positive):
3085 triangle_case(up, up, flat, positive):
3086 triangle_case(up, up, down, positive):
3087 setup_spans_up_left(psx_gpu, a, b, c);
3088 break;
3089
3090 triangle_case(up, flat, up, positive):
3091 triangle_case(up, flat, flat, positive):
3092 triangle_case(up, flat, down, positive):
3093 setup_spans_up_b(psx_gpu, a, b, c);
3094 break;
3095
3096 triangle_case(up, down, up, positive):
3097 setup_spans_up_right(psx_gpu, a, c, b);
3098 break;
3099
3100 triangle_case(up, down, flat, positive):
3101 setup_spans_up_a(psx_gpu, a, c, b);
3102 break;
3103
3104 triangle_case(up, down, down, positive):
3105 setup_spans_up_down(psx_gpu, a, b, c);
3106 break;
3107
3108 triangle_case(flat, down, up, positive):
3109 triangle_case(flat, down, flat, positive):
3110 triangle_case(flat, down, down, positive):
3111 setup_spans_down_a(psx_gpu, a, b, c);
3112 break;
3113
3114 triangle_case(down, down, up, positive):
3115 triangle_case(down, down, flat, positive):
3116 triangle_case(down, down, down, positive):
3117 setup_spans_down_right(psx_gpu, a, b, c);
3118 break;
3119 }
3120
3867c6ef 3121#ifdef PROFILE
75e28f62 3122 spans += psx_gpu->num_spans;
3867c6ef 3123#endif
75e28f62 3124
f1359c57 3125 if(unlikely(psx_gpu->render_mode & RENDER_INTERLACE_ENABLED))
69b09c0d
E
3126 {
3127 u32 i;
3128
f1359c57 3129 if(psx_gpu->render_mode & RENDER_INTERLACE_ODD)
69b09c0d
E
3130 {
3131 for(i = 0; i < psx_gpu->num_spans; i++)
3132 {
3133 if((psx_gpu->span_edge_data[i].y & 1) == 0)
3134 psx_gpu->span_edge_data[i].num_blocks = 0;
3135 }
3136 }
3137 else
3138 {
3139 for(i = 0; i < psx_gpu->num_spans; i++)
3140 {
3141 if(psx_gpu->span_edge_data[i].y & 1)
3142 psx_gpu->span_edge_data[i].num_blocks = 0;
3143 }
3144 }
3145 }
3146
75e28f62
E
3147 u32 render_state = flags &
3148 (RENDER_FLAGS_MODULATE_TEXELS | RENDER_FLAGS_BLEND |
3149 RENDER_FLAGS_TEXTURE_MAP | RENDER_FLAGS_SHADE);
3150 render_state |= psx_gpu->render_state_base;
3151
3152 if((psx_gpu->render_state != render_state) ||
3153 (psx_gpu->primitive_type != PRIMITIVE_TYPE_TRIANGLE))
3154 {
3155 psx_gpu->render_state = render_state;
3156 flush_render_block_buffer(psx_gpu);
3867c6ef 3157#ifdef PROFILE
75e28f62 3158 state_changes++;
3867c6ef 3159#endif
75e28f62
E
3160 }
3161
3162 psx_gpu->primitive_type = PRIMITIVE_TYPE_TRIANGLE;
3163
3164 psx_gpu->render_block_handler =
3165 &(render_triangle_block_handlers[render_state]);
3166 ((setup_blocks_function_type *)psx_gpu->render_block_handler->setup_blocks)
3167 (psx_gpu);
3168}
3169
c1817bd9 3170void render_triangle(psx_gpu_struct *psx_gpu, vertex_struct *vertexes,
3171 u32 flags)
3172{
3173 vertex_struct *vertex_ptrs[3];
3174 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs))
3175 render_triangle_p(psx_gpu, vertex_ptrs, flags);
3176}
3177
75e28f62
E
3178
3179void texture_sprite_blocks_8bpp(psx_gpu_struct *psx_gpu);
3180
2bbbb7af 3181#ifndef NEON_BUILD
75e28f62
E
3182
3183void texture_sprite_blocks_8bpp(psx_gpu_struct *psx_gpu)
3184{
3185 block_struct *block = psx_gpu->blocks;
3186 u32 num_blocks = psx_gpu->num_blocks;
3187
3188 vec_8x16u texels;
3189 vec_8x8u texel_indexes;
3190
3191 u16 *clut_ptr = psx_gpu->clut_ptr;
3192 u32 i;
3193
3194 while(num_blocks)
3195 {
3196 texel_indexes = block->r;
3197
3198 for(i = 0; i < 8; i++)
3199 {
3200 texels.e[i] = clut_ptr[texel_indexes.e[i]];
3201 }
3202
3203 block->texels = texels;
3204
3205 num_blocks--;
3206 block++;
3207 }
3208}
3209
3210#endif
3211
3212
59d15d23 3213#define setup_sprite_tiled_initialize_4bpp_clut() \
75e28f62
E
3214 u16 *clut_ptr = psx_gpu->clut_ptr; \
3215 vec_8x16u clut_a, clut_b; \
3216 vec_16x8u clut_low, clut_high; \
3217 \
3218 load_8x16b(clut_a, clut_ptr); \
3219 load_8x16b(clut_b, clut_ptr + 8); \
59d15d23 3220 unzip_16x8b(clut_low, clut_high, clut_a, clut_b) \
3221
3222#define setup_sprite_tiled_initialize_4bpp() \
3223 setup_sprite_tiled_initialize_4bpp_clut(); \
75e28f62
E
3224 \
3225 if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_4bpp_mask) \
3226 update_texture_4bpp_cache(psx_gpu) \
3227
3228#define setup_sprite_tiled_initialize_8bpp() \
3229 if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_8bpp_mask) \
3230 update_texture_8bpp_cache(psx_gpu) \
3231
3232
3233#define setup_sprite_tile_fetch_texel_block_8bpp(offset) \
3234 texture_block_ptr = psx_gpu->texture_page_ptr + \
3235 ((texture_offset + offset) & texture_mask); \
3236 \
3237 load_64b(texels, texture_block_ptr) \
3238
3239
75e28f62
E
3240#define setup_sprite_tile_add_blocks(tile_num_blocks) \
3241 num_blocks += tile_num_blocks; \
3242 sprite_blocks += tile_num_blocks; \
3243 \
3244 if(num_blocks > MAX_BLOCKS) \
3245 { \
3246 flush_render_block_buffer(psx_gpu); \
3247 num_blocks = tile_num_blocks; \
3248 block = psx_gpu->blocks; \
3249 } \
3250
3251#define setup_sprite_tile_full_4bpp(edge) \
3252{ \
3253 vec_8x8u texels_low, texels_high; \
3254 vec_8x16u pixels; \
3255 setup_sprite_tile_add_blocks(sub_tile_height * 2); \
3256 \
3257 while(sub_tile_height) \
3258 { \
3259 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3260 tbl_16(texels_low, texels, clut_low); \
3261 tbl_16(texels_high, texels, clut_high); \
3262 zip_8x16b(pixels, texels_low, texels_high); \
3263 \
3264 block->texels = pixels; \
3265 block->draw_mask_bits = left_mask_bits; \
3266 block->fb_ptr = fb_ptr; \
3267 block++; \
3268 \
3269 setup_sprite_tile_fetch_texel_block_8bpp(8); \
3270 tbl_16(texels_low, texels, clut_low); \
3271 tbl_16(texels_high, texels, clut_high); \
3272 zip_8x16b(pixels, texels_low, texels_high); \
3273 \
3274 block->texels = pixels; \
3275 block->draw_mask_bits = right_mask_bits; \
3276 block->fb_ptr = fb_ptr + 8; \
3277 block++; \
3278 \
3279 fb_ptr += 1024; \
3280 texture_offset += 0x10; \
3281 sub_tile_height--; \
3282 } \
3283 texture_offset += 0xF00; \
3284 psx_gpu->num_blocks = num_blocks; \