2 * Copyright (C) 2011 Gilead Kutnick "Exophase" <exophase@gmail.com>
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.
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.
22 u32 span_pixel_blocks = 0;
28 u32 sprites_16bpp = 0;
29 u32 sprite_blocks = 0;
30 u32 sprites_untextured = 0;
32 u32 trivial_rejects = 0;
36 u32 texel_blocks_4bpp = 0;
37 u32 texel_blocks_8bpp = 0;
38 u32 texel_blocks_16bpp = 0;
39 u32 texel_blocks_untextured = 0;
41 u32 render_buffer_flushes = 0;
42 u32 state_changes = 0;
43 u32 left_split_triangles = 0;
44 u32 flat_triangles = 0;
45 u32 clipped_triangles = 0;
46 u32 zero_block_spans = 0;
47 u32 texture_cache_loads = 0;
48 u32 false_modulated_blocks = 0;
50 /* double size for enhancement */
51 u32 reciprocal_table[512 * 2];
54 typedef s32 fixed_type;
56 #define EDGE_STEP_BITS 32
59 #define fixed_center(value) \
60 ((((fixed_type)(value)) << FIXED_BITS) + (1 << (FIXED_BITS - 1))) \
62 #define int_to_fixed(value) \
63 (((fixed_type)(value)) << FIXED_BITS) \
65 #define fixed_to_int(value) \
66 ((value) >> FIXED_BITS) \
68 #define fixed_to_double(value) \
69 ((value) / (double)(1 << FIXED_BITS)) \
71 #define double_to_fixed(value) \
72 (fixed_type)(((value) * (double)(1 << FIXED_BITS))) \
74 typedef void (setup_blocks_function_type)(psx_gpu_struct *psx_gpu);
75 typedef void (texture_blocks_function_type)(psx_gpu_struct *psx_gpu);
76 typedef void (shade_blocks_function_type)(psx_gpu_struct *psx_gpu);
77 typedef void (blend_blocks_function_type)(psx_gpu_struct *psx_gpu);
79 typedef 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);
82 struct render_block_handler_struct
85 texture_blocks_function_type *texture_blocks;
86 shade_blocks_function_type *shade_blocks;
87 blend_blocks_function_type *blend_blocks;
92 u32 fixed_reciprocal(u32 denominator, u32 *_shift)
94 u32 shift = __builtin_clz(denominator);
95 u32 denominator_normalized = denominator << shift;
97 double numerator = (1ULL << 62) + denominator_normalized;
100 double denominator_normalized_dp_b;
101 u64 denominator_normalized_dp_u64;
104 double reciprocal_dp;
106 u64 numerator_u64 = (denominator_normalized >> 10) |
107 ((u64)(62 + 1023) << 52);
108 *((u64 *)(&numerator_b)) = numerator_u64;
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;
115 // Implement with a DP divide
116 reciprocal_dp = numerator / denominator_normalized_dp_b;
117 reciprocal = reciprocal_dp;
119 if(reciprocal == 0x80000001)
120 reciprocal = 0x80000000;
122 *_shift = 62 - shift;
126 double reciprocal_estimate(double a)
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);
136 /* r in units of 1/256 rounded to nearest */
138 return (double)s / 256.0;
141 u32 reciprocal_estimate_u32(u32 value)
144 volatile double dp_value;
145 volatile u64 *dp_value_ptr = (volatile u64 *)&dp_value;
147 if((value >> 31) == 0)
150 dp_value_u64 = (0x3FEULL << (31 + 21)) | ((u64)(value & 0x7FFFFFFF) << 21);
152 *dp_value_ptr = dp_value_u64;
154 dp_value = reciprocal_estimate(dp_value);
155 dp_value_u64 = *dp_value_ptr;
157 return (0x80000000 | ((dp_value_u64 >> 21) & 0x7FFFFFFF));
160 u32 fixed_reciprocal_nr(u32 value, u32 *_shift)
162 u32 shift = __builtin_clz(value);
163 u32 value_normalized = value << shift;
165 *_shift = 62 - shift;
167 value_normalized -= 2;
169 u32 reciprocal_normalized = reciprocal_estimate_u32(value_normalized) >> 1;
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);
178 return reciprocal_normalized;
184 s32 triangle_signed_area_x2(s32 x0, s32 y0, s32 x1, s32 y1, s32 x2, s32 y2)
186 return ((x1 - x0) * (y2 - y1)) - ((x2 - x1) * (y1 - y0));
189 u32 texture_region_mask(s32 x1, s32 y1, s32 x2, s32 y2)
191 s32 coverage_x, coverage_y;
196 coverage_x = x2 >> 6;
197 coverage_y = y2 >> 8;
205 mask_down_right = ~(0xFFFFFFFF << (coverage_x + 1)) & 0xFFFF;
208 mask_down_right |= mask_down_right << 16;
210 coverage_x = x1 >> 6;
212 mask_up_left = 0xFFFF0000 << coverage_x;
214 mask_up_left = 0xFFFF0000;
216 coverage_y = y1 >> 8;
218 mask_up_left |= mask_up_left >> 16;
220 return mask_up_left & mask_down_right;
223 u32 invalidate_texture_cache_region(psx_gpu_struct *psx_gpu, u32 x1, u32 y1,
226 u32 mask = texture_region_mask(x1, y1, x2, y2);
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;
235 u32 invalidate_texture_cache_region_viewport(psx_gpu_struct *psx_gpu, u32 x1,
236 u32 y1, u32 x2, u32 y2)
238 u32 mask = texture_region_mask(x1, y1, x2, y2) &
239 psx_gpu->viewport_mask;
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;
248 void update_texture_cache_region(psx_gpu_struct *psx_gpu, u32 x1, u32 y1,
251 u32 mask = texture_region_mask(x1, y1, x2, y2);
253 u8 *texture_page_ptr;
258 psx_gpu->dirty_textures_8bpp_mask |= mask;
259 psx_gpu->dirty_textures_8bpp_alternate_mask |= mask;
261 if ((psx_gpu->dirty_textures_4bpp_mask & mask) == 0 &&
262 (x1 & 3) == 0 && (y1 & 15) == 0 && x2 - x1 < 4 && y2 - y1 < 16)
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;
275 texel_block = *vram_ptr;
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;
283 texture_page_ptr += 4;
297 psx_gpu->dirty_textures_4bpp_mask |= mask;
301 void update_texture_8bpp_cache_slice(psx_gpu_struct *psx_gpu,
306 void update_texture_4bpp_cache(psx_gpu_struct *psx_gpu)
308 u32 current_texture_page = psx_gpu->current_texture_page;
309 u8 *texture_page_ptr = psx_gpu->texture_page_base;
310 u16 *vram_ptr = psx_gpu->vram_ptr;
316 vram_ptr += (current_texture_page >> 4) * 256 * 1024;
317 vram_ptr += (current_texture_page & 0xF) * 64;
319 texture_cache_loads++;
326 psx_gpu->dirty_textures_4bpp_mask &= ~(psx_gpu->current_texture_mask);
336 texel_block = *vram_ptr;
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;
344 texture_page_ptr += 4;
358 vram_ptr -= (1024 * 16) - 4;
364 vram_ptr += (16 * 1024) - (4 * 16);
369 void update_texture_8bpp_cache_slice(psx_gpu_struct *psx_gpu,
372 u16 *texture_page_ptr = psx_gpu->texture_page_base;
373 u16 *vram_ptr = psx_gpu->vram_ptr;
380 texture_cache_loads++;
382 vram_ptr += (texture_page >> 4) * 256 * 1024;
383 vram_ptr += (texture_page & 0xF) * 64;
385 if((texture_page ^ psx_gpu->current_texture_page) & 0x1)
386 texture_page_ptr += (8 * 16) * 8;
399 load_128b(texels, vram_ptr);
400 store_128b(texels, texture_page_ptr);
402 texture_page_ptr += 8;
410 vram_ptr -= (1024 * 16);
419 vram_ptr += (16 * 1024);
421 texture_page_ptr += (8 * 16) * 8;
429 void update_texture_8bpp_cache(psx_gpu_struct *psx_gpu)
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;
435 psx_gpu->dirty_textures_8bpp_mask &= ~update_textures;
437 if(update_textures & (1 << current_texture_page))
439 update_texture_8bpp_cache_slice(psx_gpu, current_texture_page);
440 update_textures &= ~(1 << current_texture_page);
445 u32 adjacent_texture_page = ((current_texture_page + 1) & 0xF) |
446 (current_texture_page & 0x10);
448 update_texture_8bpp_cache_slice(psx_gpu, adjacent_texture_page);
452 void setup_blocks_shaded_untextured_undithered_unswizzled_indirect(
453 psx_gpu_struct *psx_gpu);
455 void flush_render_block_buffer(psx_gpu_struct *psx_gpu)
457 if((psx_gpu->render_mode & RENDER_INTERLACE_ENABLED) &&
458 (psx_gpu->primitive_type == PRIMITIVE_TYPE_SPRITE))
460 u32 num_blocks_dest = 0;
461 block_struct *block_src = psx_gpu->blocks;
462 block_struct *block_dest = psx_gpu->blocks;
464 u16 *vram_ptr = psx_gpu->vram_ptr;
467 if(psx_gpu->render_mode & RENDER_INTERLACE_ODD)
469 for(i = 0; i < psx_gpu->num_blocks; i++)
471 u32 fb_offset = (u32)((u8 *)block_src->fb_ptr - (u8 *)vram_ptr);
472 if(fb_offset & (1 << 11))
474 *block_dest = *block_src;
483 for(i = 0; i < psx_gpu->num_blocks; i++)
485 u32 fb_offset = (u32)((u8 *)block_src->fb_ptr - (u8 *)vram_ptr);
486 if((fb_offset & (1 << 11)) == 0)
488 *block_dest = *block_src;
496 psx_gpu->num_blocks = num_blocks_dest;
499 if(psx_gpu->num_blocks)
501 render_block_handler_struct *render_block_handler =
502 psx_gpu->render_block_handler;
504 render_block_handler->texture_blocks(psx_gpu);
505 render_block_handler->shade_blocks(psx_gpu);
506 render_block_handler->blend_blocks(psx_gpu);
509 span_pixel_blocks += psx_gpu->num_blocks;
510 render_buffer_flushes++;
513 psx_gpu->num_blocks = 0;
518 void compute_all_gradients(psx_gpu_struct *psx_gpu, vertex_struct *a,
519 vertex_struct *b, vertex_struct *c);
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 \
539 void compute_all_gradients(psx_gpu_struct *psx_gpu, vertex_struct *a,
540 vertex_struct *b, vertex_struct *c)
542 u32 triangle_area = psx_gpu->triangle_area;
543 u32 winding_mask_scalar;
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);
550 // ((x1 - x0) * (y2 - y1)) - ((x2 - x1) * (y1 - y0)) =
551 // ( d0 * d1 ) - ( d2 * d3 ) =
552 // ( m0 ) - ( m1 ) = gradient
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.
558 // Inputs are all 16-bit signed. The m0/m1 results are 32-bit signed, as
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;
570 vec_4x32u uvrgb_phase;
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;
577 vec_4x32s m0_a, m0_b, m0_c;
578 vec_4x32s m1_a, m1_b, m1_c;
580 vec_4x32u gradient_area_a, gradient_area_c;
581 vec_2x32u gradient_area_b;
583 vec_4x32u gradient_area_sign_a, gradient_area_sign_c;
584 vec_2x32u gradient_area_sign_b;
585 vec_4x32u winding_mask;
587 vec_2x64u gradient_wide_a0, gradient_wide_a1;
588 vec_2x64u gradient_wide_c0, gradient_wide_c1;
589 vec_2x64u gradient_wide_b;
591 vec_4x32u gradient_a, gradient_c;
592 vec_2x32u gradient_b;
593 vec_16x8s gradient_shift;
595 setup_gradient_calculation_input(0, a);
596 setup_gradient_calculation_input(1, b);
597 setup_gradient_calculation_input(2, c);
599 dup_4x32b(uvrgb_phase, psx_gpu->uvrgb_phase);
600 shl_long_4x16b(uvrg_base, x0_a_y0_c, 16);
601 shl_long_4x16b(b_base, x0_b, 16);
603 add_4x32b(uvrg_base, uvrg_base, uvrgb_phase);
604 add_4x32b(b_base, b_base, uvrgb_phase);
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);
610 sub_4x16b(d0_b, x1_b, x0_b);
611 sub_4x16b(d0_c, x1_c, x0_c);
613 sub_4x16b(d1_a, y2_a, y1_a);
614 sub_4x16b(d1_b, y2_b, y1_b);
616 sub_4x16b(d2_b, x2_b, x1_b);
617 sub_4x16b(d2_c, x2_c, x1_c);
619 sub_4x16b(d3_a, y1_a, y0_a);
620 sub_4x16b(d3_b, y1_b, y0_b);
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);
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);
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);
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);
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);
642 winding_mask_scalar = -psx_gpu->triangle_winding;
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);
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);
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));
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);
683 shl_4x32b(gradient_a, gradient_a, 4);
684 shl_2x32b(gradient_b, gradient_b, 4);
685 shl_4x32b(gradient_c, gradient_c, 4);
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);
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);
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);
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);
713 // Can be done with vst4, assuming that the zero, dx, dx2, and dx3 are
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];
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];
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];
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];
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];
740 psx_gpu->uvrg = uvrg_base;
741 psx_gpu->b = b_base.e[0];
743 psx_gpu->uvrg_dx = gradient_a;
744 psx_gpu->uvrg_dy = gradient_c;
745 psx_gpu->b_dy = gradient_b.e[1];
749 #define vector_check(_a, _b) \
750 if(memcmp(&_a, &_b, sizeof(_b))) \
752 if(sizeof(_b) == 8) \
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]); \
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], \
765 #define scalar_check(_a, _b) \
767 printf("mismatch on %s %s: %x vs %x\n", #_a, #_b, _a, _b) \
771 #define setup_spans_debug_check(span_edge_data_element) \
772 if (&span_edge_data_element - psx_gpu->span_edge_data < psx_gpu->num_spans) \
774 if(span_edge_data_element.num_blocks > MAX_BLOCKS_PER_ROW) \
776 if(span_edge_data_element.y > 2048) \
781 #define setup_spans_debug_check(span_edge_data_element) \
785 #define setup_spans_prologue_alternate_yes() \
786 vec_2x64s alternate_x; \
787 vec_2x64s alternate_dx_dy; \
788 vec_4x32s alternate_x_32; \
789 vec_2x32s alternate_x_16; \
791 vec_4x16u alternate_select; \
792 vec_4x16s y_mid_point; \
796 s32 edge_dx_dy_alt; \
799 #define setup_spans_prologue_alternate_no() \
801 #define setup_spans_prologue(alternate_active) \
802 edge_data_struct *span_edge_data; \
803 vec_4x32u *span_uvrg_offset; \
804 u32 *span_b_offset; \
808 vec_2x64s edges_xy; \
809 vec_2x32s edges_dx_dy; \
810 vec_2x32u edge_shifts; \
812 vec_2x64s left_x, right_x; \
813 vec_2x64s left_dx_dy, right_dx_dy; \
814 vec_4x32s left_x_32, right_x_32; \
815 vec_8x16s left_right_x_16; \
817 vec_8x16s left_edge; \
818 vec_8x16s right_edge; \
819 vec_4x16u span_shift; \
823 vec_4x16u c_0xFFFE; \
826 vec_2x32s x_starts; \
835 vec_4x32u uvrg = psx_gpu->uvrg; \
836 vec_4x32u uvrg_dy = psx_gpu->uvrg_dy; \
837 u32 b = psx_gpu->b; \
838 u32 b_dy = psx_gpu->b_dy; \
840 dup_2x32b(c_0x01, 0x01); \
841 setup_spans_prologue_alternate_##alternate_active() \
843 #define setup_spans_prologue_b() \
844 span_edge_data = psx_gpu->span_edge_data; \
845 span_uvrg_offset = psx_gpu->span_uvrg_offset; \
846 span_b_offset = psx_gpu->span_b_offset; \
848 vec_8x16u c_0x0001; \
850 dup_8x16b(c_0x0001, 0x0001); \
851 dup_8x16b(left_edge, psx_gpu->viewport_start_x); \
852 dup_8x16b(right_edge, psx_gpu->viewport_end_x); \
853 add_8x16b(right_edge, right_edge, c_0x0001); \
854 dup_4x16b(c_0x04, 0x04); \
855 dup_4x16b(c_0x07, 0x07); \
856 dup_4x16b(c_0xFFFE, 0xFFFE); \
859 #define compute_edge_delta_x2() \
862 vec_2x32s height_reciprocals; \
863 vec_2x32s heights_b; \
866 u32 edge_shift = reciprocal_table[height]; \
868 dup_2x32b(heights, height); \
869 sub_2x32b(widths, x_ends, x_starts); \
871 dup_2x32b(edge_shifts, edge_shift); \
872 sub_2x32b(heights_b, heights, c_0x01); \
873 shr_2x32b(height_reciprocals, edge_shifts, 10); \
875 mla_2x32b(heights_b, x_starts, heights); \
876 bic_immediate_4x16b(vector_cast(vec_4x16u, edge_shifts), 0xE0); \
877 mul_2x32b(edges_dx_dy, widths, height_reciprocals); \
878 mul_long_2x32b(edges_xy, heights_b, height_reciprocals); \
881 #define compute_edge_delta_x3(start_c, height_a, height_b) \
884 vec_2x32s height_reciprocals; \
885 vec_2x32s heights_b; \
890 u32 height_reciprocal_alt; \
892 heights.e[0] = height_a; \
893 heights.e[1] = height_b; \
895 edge_shifts.e[0] = reciprocal_table[height_a]; \
896 edge_shifts.e[1] = reciprocal_table[height_b]; \
897 edge_shift_alt = reciprocal_table[height_minor_b]; \
899 sub_2x32b(widths, x_ends, x_starts); \
900 width_alt = x_c - start_c; \
902 shr_2x32b(height_reciprocals, edge_shifts, 10); \
903 height_reciprocal_alt = edge_shift_alt >> 10; \
905 bic_immediate_4x16b(vector_cast(vec_4x16u, edge_shifts), 0xE0); \
906 edge_shift_alt &= 0x1F; \
908 sub_2x32b(heights_b, heights, c_0x01); \
909 height_b_alt = height_minor_b - 1; \
911 mla_2x32b(heights_b, x_starts, heights); \
912 height_b_alt += height_minor_b * start_c; \
914 mul_long_2x32b(edges_xy, heights_b, height_reciprocals); \
915 edge_alt = (s64)height_b_alt * height_reciprocal_alt; \
917 mul_2x32b(edges_dx_dy, widths, height_reciprocals); \
918 edge_dx_dy_alt = width_alt * height_reciprocal_alt; \
922 #define setup_spans_adjust_y_up() \
923 sub_4x32b(y_x4, y_x4, c_0x04) \
925 #define setup_spans_adjust_y_down() \
926 add_4x32b(y_x4, y_x4, c_0x04) \
928 #define setup_spans_adjust_interpolants_up() \
929 sub_4x32b(uvrg, uvrg, uvrg_dy); \
932 #define setup_spans_adjust_interpolants_down() \
933 add_4x32b(uvrg, uvrg, uvrg_dy); \
937 #define setup_spans_clip_interpolants_increment() \
938 mla_scalar_4x32b(uvrg, uvrg_dy, clip); \
941 #define setup_spans_clip_interpolants_decrement() \
942 mls_scalar_4x32b(uvrg, uvrg_dy, clip); \
945 #define setup_spans_clip_alternate_yes() \
946 edge_alt += edge_dx_dy_alt * (s64)(clip) \
948 #define setup_spans_clip_alternate_no() \
950 #define setup_spans_clip(direction, alternate_active) \
952 clipped_triangles++; \
953 mla_scalar_long_2x32b(edges_xy, edges_dx_dy, (s64)clip); \
954 setup_spans_clip_alternate_##alternate_active(); \
955 setup_spans_clip_interpolants_##direction(); \
959 #define setup_spans_adjust_edges_alternate_no(left_index, right_index) \
961 vec_2x64u edge_shifts_64; \
962 vec_2x64s edges_dx_dy_64; \
964 mov_wide_2x32b(edge_shifts_64, edge_shifts); \
965 shl_variable_2x64b(edges_xy, edges_xy, edge_shifts_64); \
967 mov_wide_2x32b(edges_dx_dy_64, edges_dx_dy); \
968 shl_variable_2x64b(edges_dx_dy_64, edges_dx_dy_64, edge_shifts_64); \
970 left_x.e[0] = edges_xy.e[left_index]; \
971 right_x.e[0] = edges_xy.e[right_index]; \
973 left_dx_dy.e[0] = edges_dx_dy_64.e[left_index]; \
974 left_dx_dy.e[1] = edges_dx_dy_64.e[left_index]; \
975 right_dx_dy.e[0] = edges_dx_dy_64.e[right_index]; \
976 right_dx_dy.e[1] = edges_dx_dy_64.e[right_index]; \
978 add_1x64b(left_x.high, left_x.low, left_dx_dy.low); \
979 add_1x64b(right_x.high, right_x.low, right_dx_dy.low); \
981 add_2x64b(left_dx_dy, left_dx_dy, left_dx_dy); \
982 add_2x64b(right_dx_dy, right_dx_dy, right_dx_dy); \
985 #define setup_spans_adjust_edges_alternate_yes(left_index, right_index) \
987 setup_spans_adjust_edges_alternate_no(left_index, right_index); \
988 s64 edge_dx_dy_alt_64; \
990 dup_4x16b(y_mid_point, y_b); \
992 edge_alt <<= edge_shift_alt; \
993 edge_dx_dy_alt_64 = (s64)edge_dx_dy_alt << edge_shift_alt; \
995 alternate_x.e[0] = edge_alt; \
996 alternate_dx_dy.e[0] = edge_dx_dy_alt_64; \
997 alternate_dx_dy.e[1] = edge_dx_dy_alt_64; \
999 add_1x64b(alternate_x.high, alternate_x.low, alternate_dx_dy.low); \
1000 add_2x64b(alternate_dx_dy, alternate_dx_dy, alternate_dx_dy); \
1004 #define setup_spans_y_select_up() \
1005 cmplt_4x16b(alternate_select, y_x4, y_mid_point) \
1007 #define setup_spans_y_select_down() \
1008 cmpgt_4x16b(alternate_select, y_x4, y_mid_point) \
1010 #define setup_spans_y_select_alternate_yes(direction) \
1011 setup_spans_y_select_##direction() \
1013 #define setup_spans_y_select_alternate_no(direction) \
1015 #define setup_spans_alternate_select_left() \
1016 bit_4x16b(left_right_x_16.low, alternate_x_16, alternate_select) \
1018 #define setup_spans_alternate_select_right() \
1019 bit_4x16b(left_right_x_16.high, alternate_x_16, alternate_select) \
1021 #define setup_spans_alternate_select_none() \
1023 #define setup_spans_increment_alternate_yes() \
1024 shr_narrow_2x64b(alternate_x_32.low, alternate_x, 32); \
1025 add_2x64b(alternate_x, alternate_x, alternate_dx_dy); \
1026 shr_narrow_2x64b(alternate_x_32.high, alternate_x, 32); \
1027 add_2x64b(alternate_x, alternate_x, alternate_dx_dy); \
1028 mov_narrow_4x32b(alternate_x_16, alternate_x_32) \
1030 #define setup_spans_increment_alternate_no() \
1032 #define setup_spans_set_x4(alternate, direction, alternate_active) \
1034 span_uvrg_offset[0] = uvrg; \
1035 span_b_offset[0] = b; \
1036 setup_spans_adjust_interpolants_##direction(); \
1038 span_uvrg_offset[1] = uvrg; \
1039 span_b_offset[1] = b; \
1040 setup_spans_adjust_interpolants_##direction(); \
1042 span_uvrg_offset[2] = uvrg; \
1043 span_b_offset[2] = b; \
1044 setup_spans_adjust_interpolants_##direction(); \
1046 span_uvrg_offset[3] = uvrg; \
1047 span_b_offset[3] = b; \
1048 setup_spans_adjust_interpolants_##direction(); \
1050 span_uvrg_offset += 4; \
1051 span_b_offset += 4; \
1053 shr_narrow_2x64b(left_x_32.low, left_x, 32); \
1054 shr_narrow_2x64b(right_x_32.low, right_x, 32); \
1056 add_2x64b(left_x, left_x, left_dx_dy); \
1057 add_2x64b(right_x, right_x, right_dx_dy); \
1059 shr_narrow_2x64b(left_x_32.high, left_x, 32); \
1060 shr_narrow_2x64b(right_x_32.high, right_x, 32); \
1062 add_2x64b(left_x, left_x, left_dx_dy); \
1063 add_2x64b(right_x, right_x, right_dx_dy); \
1065 mov_narrow_4x32b(left_right_x_16.low, left_x_32); \
1066 mov_narrow_4x32b(left_right_x_16.high, right_x_32); \
1068 setup_spans_increment_alternate_##alternate_active(); \
1069 setup_spans_y_select_alternate_##alternate_active(direction); \
1070 setup_spans_alternate_select_##alternate(); \
1072 max_8x16b(left_right_x_16, left_right_x_16, left_edge); \
1073 min_8x16b(left_right_x_16, left_right_x_16, right_edge); \
1075 sub_4x16b(left_right_x_16.high, left_right_x_16.high, left_right_x_16.low); \
1076 add_4x16b(left_right_x_16.high, left_right_x_16.high, c_0x07); \
1077 and_4x16b(span_shift, left_right_x_16.high, c_0x07); \
1078 shl_variable_4x16b(span_shift, c_0xFFFE, span_shift); \
1079 shr_4x16b(left_right_x_16.high, left_right_x_16.high, 3); \
1082 for(i = 0; i < 4; i++) \
1084 span_edge_data[i].left_x = left_right_x_16.low.e[i]; \
1085 span_edge_data[i].num_blocks = left_right_x_16.high.e[i]; \
1086 span_edge_data[i].right_mask = span_shift.e[i]; \
1087 span_edge_data[i].y = y_x4.e[i]; \
1088 setup_spans_debug_check(span_edge_data[i]); \
1091 span_edge_data += 4; \
1093 setup_spans_adjust_y_##direction(); \
1097 #define setup_spans_alternate_adjust_yes() \
1098 edge_alt -= edge_dx_dy_alt * (s64)height_minor_a \
1100 #define setup_spans_alternate_adjust_no() \
1103 #define setup_spans_down(left_index, right_index, alternate, alternate_active) \
1104 setup_spans_alternate_adjust_##alternate_active(); \
1105 if(y_c > psx_gpu->viewport_end_y) \
1106 height -= y_c - psx_gpu->viewport_end_y - 1; \
1108 clip = psx_gpu->viewport_start_y - y_a; \
1113 setup_spans_clip(increment, alternate_active); \
1116 setup_spans_prologue_b(); \
1121 y_x4.e[1] = y_a + 1; \
1122 y_x4.e[2] = y_a + 2; \
1123 y_x4.e[3] = y_a + 3; \
1124 setup_spans_adjust_edges_alternate_##alternate_active(left_index, \
1127 psx_gpu->num_spans = height; \
1130 setup_spans_set_x4(alternate, down, alternate_active); \
1132 } while(height > 0); \
1136 #define setup_spans_alternate_pre_increment_yes() \
1137 edge_alt += edge_dx_dy_alt \
1139 #define setup_spans_alternate_pre_increment_no() \
1141 #define setup_spans_up_decrement_height_yes() \
1144 #define setup_spans_up_decrement_height_no() \
1147 #define setup_spans_up(left_index, right_index, alternate, alternate_active) \
1148 setup_spans_alternate_adjust_##alternate_active(); \
1151 if(y_c < psx_gpu->viewport_start_y) \
1152 height -= psx_gpu->viewport_start_y - y_c; \
1154 setup_spans_up_decrement_height_##alternate_active(); \
1156 clip = y_a - psx_gpu->viewport_end_y; \
1161 setup_spans_clip(decrement, alternate_active); \
1164 setup_spans_prologue_b(); \
1169 y_x4.e[1] = y_a - 1; \
1170 y_x4.e[2] = y_a - 2; \
1171 y_x4.e[3] = y_a - 3; \
1172 add_wide_2x32b(edges_xy, edges_xy, edges_dx_dy); \
1173 setup_spans_alternate_pre_increment_##alternate_active(); \
1174 setup_spans_adjust_edges_alternate_##alternate_active(left_index, \
1176 setup_spans_adjust_interpolants_up(); \
1178 psx_gpu->num_spans = height; \
1181 setup_spans_set_x4(alternate, up, alternate_active); \
1186 #define index_left 0
1187 #define index_right 1
1189 #define setup_spans_up_up(minor, major) \
1190 setup_spans_prologue(yes); \
1191 s32 height_minor_a = y_a - y_b; \
1192 s32 height_minor_b = y_b - y_c; \
1193 s32 height = y_a - y_c; \
1195 dup_2x32b(x_starts, x_a); \
1196 x_ends.e[0] = x_c; \
1197 x_ends.e[1] = x_b; \
1199 compute_edge_delta_x3(x_b, height, height_minor_a); \
1200 setup_spans_up(index_##major, index_##minor, minor, yes) \
1203 void setup_spans_up_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1204 vertex_struct *v_b, vertex_struct *v_c);
1205 void setup_spans_up_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1206 vertex_struct *v_b, vertex_struct *v_c);
1207 void setup_spans_down_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1208 vertex_struct *v_b, vertex_struct *v_c);
1209 void setup_spans_down_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1210 vertex_struct *v_b, vertex_struct *v_c);
1211 void setup_spans_up_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1212 vertex_struct *v_b, vertex_struct *v_c);
1213 void setup_spans_up_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1214 vertex_struct *v_b, vertex_struct *v_c);
1215 void setup_spans_down_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1216 vertex_struct *v_b, vertex_struct *v_c);
1217 void setup_spans_down_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1218 vertex_struct *v_b, vertex_struct *v_c);
1219 void setup_spans_up_down(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1220 vertex_struct *v_b, vertex_struct *v_c);
1225 void setup_spans_up_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1226 vertex_struct *v_b, vertex_struct *v_c)
1228 setup_spans_up_up(left, right);
1231 void setup_spans_up_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1232 vertex_struct *v_b, vertex_struct *v_c)
1234 setup_spans_up_up(right, left);
1237 #define setup_spans_down_down(minor, major) \
1238 setup_spans_prologue(yes); \
1239 s32 height_minor_a = y_b - y_a; \
1240 s32 height_minor_b = y_c - y_b; \
1241 s32 height = y_c - y_a; \
1243 dup_2x32b(x_starts, x_a); \
1244 x_ends.e[0] = x_c; \
1245 x_ends.e[1] = x_b; \
1247 compute_edge_delta_x3(x_b, height, height_minor_a); \
1248 setup_spans_down(index_##major, index_##minor, minor, yes) \
1250 void setup_spans_down_left(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1251 vertex_struct *v_b, vertex_struct *v_c)
1253 setup_spans_down_down(left, right);
1256 void setup_spans_down_right(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1257 vertex_struct *v_b, vertex_struct *v_c)
1259 setup_spans_down_down(right, left);
1262 #define setup_spans_up_flat() \
1263 s32 height = y_a - y_c; \
1266 compute_edge_delta_x2(); \
1267 setup_spans_up(index_left, index_right, none, no) \
1269 void setup_spans_up_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1270 vertex_struct *v_b, vertex_struct *v_c)
1272 setup_spans_prologue(no);
1273 x_starts.e[0] = x_a;
1274 x_starts.e[1] = x_b;
1275 dup_2x32b(x_ends, x_c);
1277 setup_spans_up_flat();
1280 void setup_spans_up_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1281 vertex_struct *v_b, vertex_struct *v_c)
1283 setup_spans_prologue(no);
1284 dup_2x32b(x_starts, x_a);
1288 setup_spans_up_flat();
1291 #define setup_spans_down_flat() \
1292 s32 height = y_c - y_a; \
1295 compute_edge_delta_x2(); \
1296 setup_spans_down(index_left, index_right, none, no) \
1298 void setup_spans_down_a(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1299 vertex_struct *v_b, vertex_struct *v_c)
1301 setup_spans_prologue(no);
1302 x_starts.e[0] = x_a;
1303 x_starts.e[1] = x_b;
1304 dup_2x32b(x_ends, x_c);
1306 setup_spans_down_flat();
1309 void setup_spans_down_b(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1310 vertex_struct *v_b, vertex_struct *v_c)
1312 setup_spans_prologue(no);
1313 dup_2x32b(x_starts, x_a);
1317 setup_spans_down_flat();
1320 void setup_spans_up_down(psx_gpu_struct *psx_gpu, vertex_struct *v_a,
1321 vertex_struct *v_b, vertex_struct *v_c)
1323 setup_spans_prologue(no);
1331 s32 height_minor_a = y_a - y_b;
1332 s32 height_minor_b = y_c - y_a;
1333 s32 height_major = y_c - y_b;
1335 vec_2x64s edges_xy_b;
1336 vec_2x32s edges_dx_dy_b;
1337 vec_2x32u edge_shifts_b;
1339 vec_2x32s height_increment;
1341 x_starts.e[0] = x_a;
1342 x_starts.e[1] = x_c;
1343 dup_2x32b(x_ends, x_b);
1345 compute_edge_delta_x3(x_a, height_minor_a, height_major);
1347 height_increment.e[0] = 0;
1348 height_increment.e[1] = height_minor_b;
1350 mla_long_2x32b(edges_xy, edges_dx_dy, height_increment);
1352 edges_xy_b.e[0] = edge_alt;
1353 edges_xy_b.e[1] = edges_xy.e[1];
1355 edge_shifts_b = edge_shifts;
1356 edge_shifts_b.e[0] = edge_shift_alt;
1358 neg_2x32b(edges_dx_dy_b, edges_dx_dy);
1359 edges_dx_dy_b.e[0] = edge_dx_dy_alt;
1363 if(y_b < psx_gpu->viewport_start_y)
1364 height_minor_a -= psx_gpu->viewport_start_y - y_b;
1366 clip = y_a - psx_gpu->viewport_end_y;
1369 height_minor_a -= clip;
1371 setup_spans_clip(decrement, no);
1374 setup_spans_prologue_b();
1376 if(height_minor_a > 0)
1379 y_x4.e[1] = y_a - 1;
1380 y_x4.e[2] = y_a - 2;
1381 y_x4.e[3] = y_a - 3;
1382 add_wide_2x32b(edges_xy, edges_xy, edges_dx_dy);
1383 setup_spans_adjust_edges_alternate_no(index_left, index_right);
1384 setup_spans_adjust_interpolants_up();
1386 psx_gpu->num_spans = height_minor_a;
1387 while(height_minor_a > 0)
1389 setup_spans_set_x4(none, up, no);
1390 height_minor_a -= 4;
1393 span_edge_data += height_minor_a;
1394 span_uvrg_offset += height_minor_a;
1395 span_b_offset += height_minor_a;
1398 edges_xy = edges_xy_b;
1399 edges_dx_dy = edges_dx_dy_b;
1400 edge_shifts = edge_shifts_b;
1402 uvrg = psx_gpu->uvrg;
1407 if(y_c > psx_gpu->viewport_end_y)
1408 height_minor_b -= y_c - psx_gpu->viewport_end_y - 1;
1410 clip = psx_gpu->viewport_start_y - y_a;
1413 height_minor_b -= clip;
1415 setup_spans_clip(increment, no);
1418 if(height_minor_b > 0)
1421 y_x4.e[1] = y_a + 1;
1422 y_x4.e[2] = y_a + 2;
1423 y_x4.e[3] = y_a + 3;
1424 setup_spans_adjust_edges_alternate_no(index_left, index_right);
1426 psx_gpu->num_spans += height_minor_b;
1429 setup_spans_set_x4(none, down, no);
1430 height_minor_b -= 4;
1431 } while(height_minor_b > 0);
1434 left_split_triangles++;
1440 #define dither_table_entry_normal(value) \
1444 #define setup_blocks_load_msb_mask_indirect() \
1446 #define setup_blocks_load_msb_mask_direct() \
1447 vec_8x16u msb_mask; \
1448 dup_8x16b(msb_mask, psx_gpu->mask_msb); \
1451 #define setup_blocks_variables_shaded_textured(target) \
1452 vec_4x32u u_block; \
1453 vec_4x32u v_block; \
1454 vec_4x32u r_block; \
1455 vec_4x32u g_block; \
1456 vec_4x32u b_block; \
1457 vec_4x32u uvrg_dx = psx_gpu->uvrg_dx; \
1458 vec_4x32u uvrg_dx4; \
1459 vec_4x32u uvrg_dx8; \
1461 u32 b_dx = psx_gpu->b_block_span.e[1]; \
1462 u32 b_dx4 = b_dx << 2; \
1463 u32 b_dx8 = b_dx << 3; \
1466 vec_16x8u texture_mask; \
1467 shl_4x32b(uvrg_dx4, uvrg_dx, 2); \
1468 shl_4x32b(uvrg_dx8, uvrg_dx, 3); \
1469 dup_8x8b(texture_mask.low, psx_gpu->texture_mask_width); \
1470 dup_8x8b(texture_mask.high, psx_gpu->texture_mask_height) \
1472 #define setup_blocks_variables_shaded_untextured(target) \
1473 vec_4x32u r_block; \
1474 vec_4x32u g_block; \
1475 vec_4x32u b_block; \
1477 vec_4x32u rgb_dx4; \
1478 vec_4x32u rgb_dx8; \
1481 vec_8x8u d64_0x07; \
1486 dup_8x8b(d64_0x07, 0x07); \
1487 dup_8x8b(d64_1, 1); \
1488 dup_8x8b(d64_4, 4); \
1489 dup_8x8b(d64_128, 128); \
1491 rgb_dx.low = psx_gpu->uvrg_dx.high; \
1492 rgb_dx.e[2] = psx_gpu->b_block_span.e[1]; \
1493 shl_4x32b(rgb_dx4, rgb_dx, 2); \
1494 shl_4x32b(rgb_dx8, rgb_dx, 3) \
1496 #define setup_blocks_variables_unshaded_textured(target) \
1497 vec_4x32u u_block; \
1498 vec_4x32u v_block; \
1499 vec_2x32u uv_dx = psx_gpu->uvrg_dx.low; \
1502 vec_2x32u uv = psx_gpu->uvrg.low; \
1504 vec_16x8u texture_mask; \
1505 shl_2x32b(uv_dx4, uv_dx, 2); \
1506 shl_2x32b(uv_dx8, uv_dx, 3); \
1507 dup_8x8b(texture_mask.low, psx_gpu->texture_mask_width); \
1508 dup_8x8b(texture_mask.high, psx_gpu->texture_mask_height) \
1511 #define setup_blocks_variables_unshaded_untextured_direct() \
1512 or_8x16b(colors, colors, msb_mask) \
1514 #define setup_blocks_variables_unshaded_untextured_indirect() \
1516 #define setup_blocks_variables_unshaded_untextured(target) \
1517 u32 color = psx_gpu->triangle_color; \
1520 u32 color_r = color & 0xFF; \
1521 u32 color_g = (color >> 8) & 0xFF; \
1522 u32 color_b = (color >> 16) & 0xFF; \
1524 color = (color_r >> 3) | ((color_g >> 3) << 5) | \
1525 ((color_b >> 3) << 10); \
1526 dup_8x16b(colors, color); \
1527 setup_blocks_variables_unshaded_untextured_##target() \
1529 #define setup_blocks_span_initialize_dithered_textured() \
1530 vec_8x16u dither_offsets; \
1531 shl_long_8x8b(dither_offsets, dither_offsets_short, 4) \
1533 #define setup_blocks_span_initialize_dithered_untextured() \
1534 vec_8x8u dither_offsets; \
1535 add_8x8b(dither_offsets, dither_offsets_short, d64_4) \
1537 #define setup_blocks_span_initialize_dithered(texturing) \
1538 u32 dither_row = psx_gpu->dither_table[y & 0x3]; \
1539 u32 dither_shift = (span_edge_data->left_x & 0x3) * 8; \
1540 vec_8x8s dither_offsets_short; \
1543 (dither_row >> dither_shift) | (dither_row << (32 - dither_shift)); \
1544 dup_2x32b(vector_cast(vec_2x32u, dither_offsets_short), dither_row); \
1545 setup_blocks_span_initialize_dithered_##texturing() \
1547 #define setup_blocks_span_initialize_undithered(texturing) \
1550 #define setup_blocks_span_initialize_shaded_textured() \
1552 vec_4x32u block_span; \
1553 u32 offset = span_edge_data->left_x; \
1555 uvrg = *span_uvrg_offset; \
1556 mla_scalar_4x32b(uvrg, uvrg_dx, offset); \
1557 b = *span_b_offset; \
1558 b += b_dx * offset; \
1560 dup_4x32b(u_block, uvrg.e[0]); \
1561 dup_4x32b(v_block, uvrg.e[1]); \
1562 dup_4x32b(r_block, uvrg.e[2]); \
1563 dup_4x32b(g_block, uvrg.e[3]); \
1564 dup_4x32b(b_block, b); \
1566 block_span = psx_gpu->u_block_span; \
1567 add_4x32b(u_block, u_block, block_span); \
1568 block_span = psx_gpu->v_block_span; \
1569 add_4x32b(v_block, v_block, block_span); \
1570 block_span = psx_gpu->r_block_span; \
1571 add_4x32b(r_block, r_block, block_span); \
1572 block_span = psx_gpu->g_block_span; \
1573 add_4x32b(g_block, g_block, block_span); \
1574 block_span = psx_gpu->b_block_span; \
1575 add_4x32b(b_block, b_block, block_span); \
1578 #define setup_blocks_span_initialize_shaded_untextured() \
1580 vec_4x32u block_span; \
1581 u32 offset = span_edge_data->left_x; \
1583 rgb.low = span_uvrg_offset->high; \
1584 rgb.high.e[0] = *span_b_offset; \
1585 mla_scalar_4x32b(rgb, rgb_dx, offset); \
1587 dup_4x32b(r_block, rgb.e[0]); \
1588 dup_4x32b(g_block, rgb.e[1]); \
1589 dup_4x32b(b_block, rgb.e[2]); \
1591 block_span = psx_gpu->r_block_span; \
1592 add_4x32b(r_block, r_block, block_span); \
1593 block_span = psx_gpu->g_block_span; \
1594 add_4x32b(g_block, g_block, block_span); \
1595 block_span = psx_gpu->b_block_span; \
1596 add_4x32b(b_block, b_block, block_span); \
1599 #define setup_blocks_span_initialize_unshaded_textured() \
1601 vec_4x32u block_span; \
1602 u32 offset = span_edge_data->left_x; \
1604 uv = span_uvrg_offset->low; \
1605 mla_scalar_2x32b(uv, uv_dx, offset); \
1607 dup_4x32b(u_block, uv.e[0]); \
1608 dup_4x32b(v_block, uv.e[1]); \
1610 block_span = psx_gpu->u_block_span; \
1611 add_4x32b(u_block, u_block, block_span); \
1612 block_span = psx_gpu->v_block_span; \
1613 add_4x32b(v_block, v_block, block_span); \
1616 #define setup_blocks_span_initialize_unshaded_untextured() \
1619 #define setup_blocks_texture_swizzled() \
1621 vec_8x8u u_saved = u; \
1622 sli_8x8b(u, v, 4); \
1623 sri_8x8b(v, u_saved, 4); \
1626 #define setup_blocks_texture_unswizzled() \
1628 #define setup_blocks_store_shaded_textured(swizzling, dithering, target, \
1631 vec_8x16u u_whole; \
1632 vec_8x16u v_whole; \
1633 vec_8x16u r_whole; \
1634 vec_8x16u g_whole; \
1635 vec_8x16u b_whole; \
1647 shr_narrow_4x32b(u_whole.low, u_block, 16); \
1648 shr_narrow_4x32b(v_whole.low, v_block, 16); \
1649 shr_narrow_4x32b(r_whole.low, r_block, 16); \
1650 shr_narrow_4x32b(g_whole.low, g_block, 16); \
1651 shr_narrow_4x32b(b_whole.low, b_block, 16); \
1653 dup_4x32b(dx4, uvrg_dx4.e[0]); \
1654 add_high_narrow_4x32b(u_whole.high, u_block, dx4); \
1655 dup_4x32b(dx4, uvrg_dx4.e[1]); \
1656 add_high_narrow_4x32b(v_whole.high, v_block, dx4); \
1657 dup_4x32b(dx4, uvrg_dx4.e[2]); \
1658 add_high_narrow_4x32b(r_whole.high, r_block, dx4); \
1659 dup_4x32b(dx4, uvrg_dx4.e[3]); \
1660 add_high_narrow_4x32b(g_whole.high, g_block, dx4); \
1661 dup_4x32b(dx4, b_dx4); \
1662 add_high_narrow_4x32b(b_whole.high, b_block, dx4); \
1664 mov_narrow_8x16b(u, u_whole); \
1665 mov_narrow_8x16b(v, v_whole); \
1666 mov_narrow_8x16b(r, r_whole); \
1667 mov_narrow_8x16b(g, g_whole); \
1668 mov_narrow_8x16b(b, b_whole); \
1670 dup_4x32b(dx8, uvrg_dx8.e[0]); \
1671 add_4x32b(u_block, u_block, dx8); \
1672 dup_4x32b(dx8, uvrg_dx8.e[1]); \
1673 add_4x32b(v_block, v_block, dx8); \
1674 dup_4x32b(dx8, uvrg_dx8.e[2]); \
1675 add_4x32b(r_block, r_block, dx8); \
1676 dup_4x32b(dx8, uvrg_dx8.e[3]); \
1677 add_4x32b(g_block, g_block, dx8); \
1678 dup_4x32b(dx8, b_dx8); \
1679 add_4x32b(b_block, b_block, dx8); \
1681 and_8x8b(u, u, texture_mask.low); \
1682 and_8x8b(v, v, texture_mask.high); \
1683 setup_blocks_texture_##swizzling(); \
1685 zip_8x16b(uv, u, v); \
1690 block->dither_offsets = vector_cast(vec_8x16u, dither_offsets); \
1691 block->fb_ptr = fb_ptr; \
1694 #define setup_blocks_store_unshaded_textured(swizzling, dithering, target, \
1697 vec_8x16u u_whole; \
1698 vec_8x16u v_whole; \
1707 shr_narrow_4x32b(u_whole.low, u_block, 16); \
1708 shr_narrow_4x32b(v_whole.low, v_block, 16); \
1710 dup_4x32b(dx4, uv_dx4.e[0]); \
1711 add_high_narrow_4x32b(u_whole.high, u_block, dx4); \
1712 dup_4x32b(dx4, uv_dx4.e[1]); \
1713 add_high_narrow_4x32b(v_whole.high, v_block, dx4); \
1715 mov_narrow_8x16b(u, u_whole); \
1716 mov_narrow_8x16b(v, v_whole); \
1718 dup_4x32b(dx8, uv_dx8.e[0]); \
1719 add_4x32b(u_block, u_block, dx8); \
1720 dup_4x32b(dx8, uv_dx8.e[1]); \
1721 add_4x32b(v_block, v_block, dx8); \
1723 and_8x8b(u, u, texture_mask.low); \
1724 and_8x8b(v, v, texture_mask.high); \
1725 setup_blocks_texture_##swizzling(); \
1727 zip_8x16b(uv, u, v); \
1729 block->dither_offsets = vector_cast(vec_8x16u, dither_offsets); \
1730 block->fb_ptr = fb_ptr; \
1733 #define setup_blocks_store_shaded_untextured_dithered() \
1734 addq_8x8b(r, r, dither_offsets); \
1735 addq_8x8b(g, g, dither_offsets); \
1736 addq_8x8b(b, b, dither_offsets); \
1738 subq_8x8b(r, r, d64_4); \
1739 subq_8x8b(g, g, d64_4); \
1740 subq_8x8b(b, b, d64_4) \
1742 #define setup_blocks_store_shaded_untextured_undithered() \
1745 #define setup_blocks_store_untextured_pixels_indirect_full(_pixels) \
1746 block->pixels = _pixels; \
1747 block->fb_ptr = fb_ptr \
1749 #define setup_blocks_store_untextured_pixels_indirect_edge(_pixels) \
1750 block->pixels = _pixels; \
1751 block->fb_ptr = fb_ptr \
1753 #define setup_blocks_store_shaded_untextured_seed_pixels_indirect() \
1754 mul_long_8x8b(pixels, r, d64_1) \
1757 #define setup_blocks_store_untextured_pixels_direct_full(_pixels) \
1758 store_8x16b(_pixels, fb_ptr) \
1760 #define setup_blocks_store_untextured_pixels_direct_edge(_pixels) \
1762 vec_8x16u fb_pixels; \
1763 vec_8x16u draw_mask; \
1764 vec_8x16u test_mask = psx_gpu->test_mask; \
1766 load_8x16b(fb_pixels, fb_ptr); \
1767 dup_8x16b(draw_mask, span_edge_data->right_mask); \
1768 tst_8x16b(draw_mask, draw_mask, test_mask); \
1769 bif_8x16b(fb_pixels, _pixels, draw_mask); \
1770 store_8x16b(fb_pixels, fb_ptr); \
1773 #define setup_blocks_store_shaded_untextured_seed_pixels_direct() \
1774 pixels = msb_mask; \
1775 mla_long_8x8b(pixels, r, d64_1) \
1778 #define setup_blocks_store_shaded_untextured(swizzling, dithering, target, \
1781 vec_8x16u r_whole; \
1782 vec_8x16u g_whole; \
1783 vec_8x16u b_whole; \
1794 shr_narrow_4x32b(r_whole.low, r_block, 16); \
1795 shr_narrow_4x32b(g_whole.low, g_block, 16); \
1796 shr_narrow_4x32b(b_whole.low, b_block, 16); \
1798 dup_4x32b(dx4, rgb_dx4.e[0]); \
1799 add_high_narrow_4x32b(r_whole.high, r_block, dx4); \
1800 dup_4x32b(dx4, rgb_dx4.e[1]); \
1801 add_high_narrow_4x32b(g_whole.high, g_block, dx4); \
1802 dup_4x32b(dx4, rgb_dx4.e[2]); \
1803 add_high_narrow_4x32b(b_whole.high, b_block, dx4); \
1805 mov_narrow_8x16b(r, r_whole); \
1806 mov_narrow_8x16b(g, g_whole); \
1807 mov_narrow_8x16b(b, b_whole); \
1809 dup_4x32b(dx8, rgb_dx8.e[0]); \
1810 add_4x32b(r_block, r_block, dx8); \
1811 dup_4x32b(dx8, rgb_dx8.e[1]); \
1812 add_4x32b(g_block, g_block, dx8); \
1813 dup_4x32b(dx8, rgb_dx8.e[2]); \
1814 add_4x32b(b_block, b_block, dx8); \
1816 setup_blocks_store_shaded_untextured_##dithering(); \
1818 shr_8x8b(r, r, 3); \
1819 bic_8x8b(g, g, d64_0x07); \
1820 bic_8x8b(b, b, d64_0x07); \
1822 setup_blocks_store_shaded_untextured_seed_pixels_##target(); \
1823 mla_long_8x8b(pixels, g, d64_4); \
1824 mla_long_8x8b(pixels, b, d64_128) \
1826 setup_blocks_store_untextured_pixels_##target##_##edge_type(pixels); \
1829 #define setup_blocks_store_unshaded_untextured(swizzling, dithering, target, \
1831 setup_blocks_store_untextured_pixels_##target##_##edge_type(colors) \
1834 #define setup_blocks_store_draw_mask_textured_indirect(_block, bits) \
1835 (_block)->draw_mask_bits = bits \
1837 #define setup_blocks_store_draw_mask_untextured_indirect(_block, bits) \
1839 vec_8x16u bits_mask; \
1840 vec_8x16u test_mask = psx_gpu->test_mask; \
1841 dup_8x16b(bits_mask, bits); \
1842 tst_8x16b(bits_mask, bits_mask, test_mask); \
1843 (_block)->draw_mask = bits_mask; \
1846 #define setup_blocks_store_draw_mask_untextured_direct(_block, bits) \
1849 #define setup_blocks_add_blocks_indirect() \
1850 num_blocks += span_num_blocks; \
1852 if(num_blocks > MAX_BLOCKS) \
1854 psx_gpu->num_blocks = num_blocks - span_num_blocks; \
1855 flush_render_block_buffer(psx_gpu); \
1856 num_blocks = span_num_blocks; \
1857 block = psx_gpu->blocks; \
1860 #define setup_blocks_add_blocks_direct() \
1861 texel_blocks_untextured += span_num_blocks; \
1862 span_pixel_blocks += span_num_blocks \
1865 #define setup_blocks_builder(shading, texturing, dithering, sw, target) \
1866 void setup_blocks_##shading##_##texturing##_##dithering##_##sw##_##target( \
1867 psx_gpu_struct *psx_gpu) \
1869 setup_blocks_load_msb_mask_##target(); \
1870 setup_blocks_variables_##shading##_##texturing(target); \
1872 edge_data_struct *span_edge_data = psx_gpu->span_edge_data; \
1873 vec_4x32u *span_uvrg_offset = psx_gpu->span_uvrg_offset; \
1874 u32 *span_b_offset = psx_gpu->span_b_offset; \
1876 block_struct *block = psx_gpu->blocks + psx_gpu->num_blocks; \
1878 u32 num_spans = psx_gpu->num_spans; \
1883 u32 num_blocks = psx_gpu->num_blocks; \
1884 u32 span_num_blocks; \
1888 span_num_blocks = span_edge_data->num_blocks; \
1889 if(span_num_blocks) \
1891 y = span_edge_data->y; \
1892 fb_ptr = psx_gpu->vram_out_ptr + span_edge_data->left_x + (y * 1024); \
1894 setup_blocks_span_initialize_##shading##_##texturing(); \
1895 setup_blocks_span_initialize_##dithering(texturing); \
1897 setup_blocks_add_blocks_##target(); \
1899 s32 pixel_span = span_num_blocks * 8; \
1900 pixel_span -= __builtin_popcount(span_edge_data->right_mask & 0xFF); \
1901 span_pixels += pixel_span; \
1903 span_num_blocks--; \
1904 while(span_num_blocks) \
1906 setup_blocks_store_##shading##_##texturing(sw, dithering, target, \
1908 setup_blocks_store_draw_mask_##texturing##_##target(block, 0x00); \
1912 span_num_blocks--; \
1915 setup_blocks_store_##shading##_##texturing(sw, dithering, target, edge); \
1916 setup_blocks_store_draw_mask_##texturing##_##target(block, \
1917 span_edge_data->right_mask); \
1923 zero_block_spans++; \
1928 span_uvrg_offset++; \
1932 psx_gpu->num_blocks = num_blocks; \
1935 void setup_blocks_shaded_textured_dithered_unswizzled_indirect(psx_gpu_struct
1938 void setup_blocks_shaded_untextured_dithered_unswizzled_indirect(psx_gpu_struct
1940 void setup_blocks_shaded_untextured_undithered_unswizzled_indirect(
1941 psx_gpu_struct *psx_gpu);
1942 void setup_blocks_shaded_untextured_dithered_unswizzled_direct(psx_gpu_struct
1944 void setup_blocks_shaded_untextured_undithered_unswizzled_direct(
1945 psx_gpu_struct *psx_gpu);
1947 void setup_blocks_unshaded_textured_dithered_unswizzled_indirect(psx_gpu_struct
1949 void setup_blocks_unshaded_untextured_undithered_unswizzled_indirect(
1950 psx_gpu_struct *psx_gpu);
1951 void setup_blocks_unshaded_untextured_undithered_unswizzled_direct(
1952 psx_gpu_struct *psx_gpu);
1954 void setup_blocks_shaded_textured_dithered_swizzled_indirect(psx_gpu_struct
1956 void setup_blocks_unshaded_textured_dithered_swizzled_indirect(psx_gpu_struct
1960 //setup_blocks_builder(unshaded, untextured, undithered, unswizzled, direct);
1964 setup_blocks_builder(shaded, textured, dithered, swizzled, indirect);
1965 setup_blocks_builder(shaded, textured, dithered, unswizzled, indirect);
1967 setup_blocks_builder(unshaded, textured, dithered, unswizzled, indirect);
1968 setup_blocks_builder(unshaded, textured, dithered, swizzled, indirect);
1970 setup_blocks_builder(shaded, untextured, undithered, unswizzled, indirect);
1971 setup_blocks_builder(shaded, untextured, dithered, unswizzled, indirect);
1972 setup_blocks_builder(shaded, untextured, undithered, unswizzled, direct);
1973 setup_blocks_builder(shaded, untextured, dithered, unswizzled, direct);
1975 setup_blocks_builder(unshaded, untextured, undithered, unswizzled, indirect);
1976 setup_blocks_builder(unshaded, untextured, undithered, unswizzled, direct);
1980 void texture_blocks_untextured(psx_gpu_struct *psx_gpu);
1981 void texture_blocks_4bpp(psx_gpu_struct *psx_gpu);
1982 void texture_blocks_8bpp(psx_gpu_struct *psx_gpu);
1983 void texture_blocks_16bpp(psx_gpu_struct *psx_gpu);
1987 void texture_blocks_untextured(psx_gpu_struct *psx_gpu)
1989 if(psx_gpu->primitive_type != PRIMITIVE_TYPE_SPRITE)
1990 texel_blocks_untextured += psx_gpu->num_blocks;
1993 void texture_blocks_4bpp(psx_gpu_struct *psx_gpu)
1995 block_struct *block = psx_gpu->blocks;
1996 u32 num_blocks = psx_gpu->num_blocks;
1997 texel_blocks_4bpp += num_blocks;
1999 vec_8x8u texels_low;
2000 vec_8x8u texels_high;
2007 vec_16x8u clut_high;
2009 u8 *texture_ptr_8bpp = psx_gpu->texture_page_ptr;
2010 u16 *clut_ptr = psx_gpu->clut_ptr;
2012 // Can be done with one deinterleaving load on NEON
2013 load_8x16b(clut_a, clut_ptr);
2014 load_8x16b(clut_b, clut_ptr + 8);
2015 unzip_16x8b(clut_low, clut_high, clut_a, clut_b);
2017 if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_4bpp_mask)
2018 update_texture_4bpp_cache(psx_gpu);
2022 texels.e[0] = texture_ptr_8bpp[block->uv.e[0]];
2023 texels.e[1] = texture_ptr_8bpp[block->uv.e[1]];
2024 texels.e[2] = texture_ptr_8bpp[block->uv.e[2]];
2025 texels.e[3] = texture_ptr_8bpp[block->uv.e[3]];
2026 texels.e[4] = texture_ptr_8bpp[block->uv.e[4]];
2027 texels.e[5] = texture_ptr_8bpp[block->uv.e[5]];
2028 texels.e[6] = texture_ptr_8bpp[block->uv.e[6]];
2029 texels.e[7] = texture_ptr_8bpp[block->uv.e[7]];
2031 tbl_16(texels_low, texels, clut_low);
2032 tbl_16(texels_high, texels, clut_high);
2034 // Can be done with an interleaving store on NEON
2035 zip_8x16b(pixels, texels_low, texels_high);
2037 block->texels = pixels;
2044 void texture_blocks_8bpp(psx_gpu_struct *psx_gpu)
2046 block_struct *block = psx_gpu->blocks;
2047 u32 num_blocks = psx_gpu->num_blocks;
2049 texel_blocks_8bpp += num_blocks;
2051 if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_8bpp_mask)
2052 update_texture_8bpp_cache(psx_gpu);
2055 u8 *texture_ptr_8bpp = psx_gpu->texture_page_ptr;
2063 for(i = 0; i < 8; i++)
2065 offset = block->uv.e[i];
2067 texel = texture_ptr_8bpp[offset];
2068 texels.e[i] = psx_gpu->clut_ptr[texel];
2071 block->texels = texels;
2078 void texture_blocks_16bpp(psx_gpu_struct *psx_gpu)
2080 block_struct *block = psx_gpu->blocks;
2081 u32 num_blocks = psx_gpu->num_blocks;
2083 texel_blocks_16bpp += num_blocks;
2087 u16 *texture_ptr_16bpp = psx_gpu->texture_page_ptr;
2093 for(i = 0; i < 8; i++)
2095 offset = block->uv.e[i];
2096 offset += ((offset & 0xFF00) * 3);
2098 texels.e[i] = texture_ptr_16bpp[offset];
2101 block->texels = texels;
2111 #define shade_blocks_load_msb_mask_indirect() \
2113 #define shade_blocks_load_msb_mask_direct() \
2114 vec_8x16u msb_mask; \
2115 dup_8x16b(msb_mask, psx_gpu->mask_msb); \
2117 #define shade_blocks_store_indirect(_draw_mask, _pixels) \
2118 block->draw_mask = _draw_mask; \
2119 block->pixels = _pixels \
2121 #define shade_blocks_store_direct(_draw_mask, _pixels) \
2123 vec_8x16u fb_pixels; \
2124 or_8x16b(_pixels, _pixels, msb_mask); \
2125 load_8x16b(fb_pixels, block->fb_ptr); \
2126 bif_8x16b(fb_pixels, _pixels, _draw_mask); \
2127 store_8x16b(fb_pixels, block->fb_ptr); \
2131 #define shade_blocks_textured_false_modulated_check_dithered(target) \
2132 if(psx_gpu->triangle_color == 0x808080) \
2134 false_modulated_blocks += num_blocks; \
2137 #define shade_blocks_textured_false_modulated_check_undithered(target) \
2138 if(psx_gpu->triangle_color == 0x808080) \
2141 shade_blocks_textured_unmodulated_##target(psx_gpu); \
2142 false_modulated_blocks += num_blocks; \
2147 #define shade_blocks_textured_modulated_shaded_primitive_load(dithering, \
2150 #define shade_blocks_textured_modulated_unshaded_primitive_load(dithering, \
2153 u32 color = psx_gpu->triangle_color; \
2154 dup_8x8b(colors_r, color); \
2155 dup_8x8b(colors_g, color >> 8); \
2156 dup_8x8b(colors_b, color >> 16); \
2157 shade_blocks_textured_false_modulated_check_##dithering(target); \
2160 #define shade_blocks_textured_modulated_shaded_block_load() \
2161 colors_r = block->r; \
2162 colors_g = block->g; \
2163 colors_b = block->b \
2165 #define shade_blocks_textured_modulated_unshaded_block_load() \
2167 #define shade_blocks_textured_modulate_dithered(component) \
2168 pixels_##component = block->dither_offsets; \
2169 mla_long_8x8b(pixels_##component, texels_##component, colors_##component) \
2171 #define shade_blocks_textured_modulate_undithered(component) \
2172 mul_long_8x8b(pixels_##component, texels_##component, colors_##component) \
2174 #define shade_blocks_textured_modulated_builder(shading, dithering, target) \
2175 void shade_blocks_##shading##_textured_modulated_##dithering##_##target( \
2176 psx_gpu_struct *psx_gpu) \
2178 block_struct *block = psx_gpu->blocks; \
2179 u32 num_blocks = psx_gpu->num_blocks; \
2182 vec_8x8u texels_r; \
2183 vec_8x8u texels_g; \
2184 vec_8x8u texels_b; \
2186 vec_8x8u colors_r; \
2187 vec_8x8u colors_g; \
2188 vec_8x8u colors_b; \
2190 vec_8x8u pixels_r_low; \
2191 vec_8x8u pixels_g_low; \
2192 vec_8x8u pixels_b_low; \
2195 vec_8x16u pixels_r; \
2196 vec_8x16u pixels_g; \
2197 vec_8x16u pixels_b; \
2199 vec_8x16u draw_mask; \
2200 vec_8x16u zero_mask; \
2202 vec_8x8u d64_0x07; \
2203 vec_8x8u d64_0x1F; \
2208 vec_8x16u d128_0x8000; \
2210 vec_8x16u test_mask = psx_gpu->test_mask; \
2211 u32 draw_mask_bits; \
2212 shade_blocks_load_msb_mask_##target(); \
2214 dup_8x8b(d64_0x07, 0x07); \
2215 dup_8x8b(d64_0x1F, 0x1F); \
2216 dup_8x8b(d64_1, 1); \
2217 dup_8x8b(d64_4, 4); \
2218 dup_8x8b(d64_128, 128); \
2220 dup_8x16b(d128_0x8000, 0x8000); \
2222 shade_blocks_textured_modulated_##shading##_primitive_load(dithering, \
2227 draw_mask_bits = block->draw_mask_bits; \
2228 dup_8x16b(draw_mask, draw_mask_bits); \
2229 tst_8x16b(draw_mask, draw_mask, test_mask); \
2231 shade_blocks_textured_modulated_##shading##_block_load(); \
2233 texels = block->texels; \
2235 mov_narrow_8x16b(texels_r, texels); \
2236 shr_narrow_8x16b(texels_g, texels, 5); \
2237 shr_narrow_8x16b(texels_b, texels, 7); \
2239 and_8x8b(texels_r, texels_r, d64_0x1F); \
2240 and_8x8b(texels_g, texels_g, d64_0x1F); \
2241 shr_8x8b(texels_b, texels_b, 3); \
2243 shade_blocks_textured_modulate_##dithering(r); \
2244 shade_blocks_textured_modulate_##dithering(g); \
2245 shade_blocks_textured_modulate_##dithering(b); \
2247 cmpeqz_8x16b(zero_mask, texels); \
2248 and_8x16b(pixels, texels, d128_0x8000); \
2250 shrq_narrow_signed_8x16b(pixels_r_low, pixels_r, 4); \
2251 shrq_narrow_signed_8x16b(pixels_g_low, pixels_g, 4); \
2252 shrq_narrow_signed_8x16b(pixels_b_low, pixels_b, 4); \
2254 or_8x16b(zero_mask, draw_mask, zero_mask); \
2256 shr_8x8b(pixels_r_low, pixels_r_low, 3); \
2257 bic_8x8b(pixels_g_low, pixels_g_low, d64_0x07); \
2258 bic_8x8b(pixels_b_low, pixels_b_low, d64_0x07); \
2260 mla_long_8x8b(pixels, pixels_r_low, d64_1); \
2261 mla_long_8x8b(pixels, pixels_g_low, d64_4); \
2262 mla_long_8x8b(pixels, pixels_b_low, d64_128); \
2264 shade_blocks_store_##target(zero_mask, pixels); \
2271 void shade_blocks_shaded_textured_modulated_dithered_direct(psx_gpu_struct
2273 void shade_blocks_shaded_textured_modulated_undithered_direct(psx_gpu_struct
2275 void shade_blocks_unshaded_textured_modulated_dithered_direct(psx_gpu_struct
2277 void shade_blocks_unshaded_textured_modulated_undithered_direct(psx_gpu_struct
2280 void shade_blocks_shaded_textured_modulated_dithered_indirect(psx_gpu_struct
2282 void shade_blocks_shaded_textured_modulated_undithered_indirect(psx_gpu_struct
2284 void shade_blocks_unshaded_textured_modulated_dithered_indirect(psx_gpu_struct
2286 void shade_blocks_unshaded_textured_modulated_undithered_indirect(psx_gpu_struct
2289 void shade_blocks_textured_unmodulated_indirect(psx_gpu_struct *psx_gpu);
2290 void shade_blocks_textured_unmodulated_direct(psx_gpu_struct *psx_gpu);
2294 shade_blocks_textured_modulated_builder(shaded, dithered, direct);
2295 shade_blocks_textured_modulated_builder(shaded, undithered, direct);
2296 shade_blocks_textured_modulated_builder(unshaded, dithered, direct);
2297 shade_blocks_textured_modulated_builder(unshaded, undithered, direct);
2299 shade_blocks_textured_modulated_builder(shaded, dithered, indirect);
2300 shade_blocks_textured_modulated_builder(shaded, undithered, indirect);
2301 shade_blocks_textured_modulated_builder(unshaded, dithered, indirect);
2302 shade_blocks_textured_modulated_builder(unshaded, undithered, indirect);
2307 #define shade_blocks_textured_unmodulated_builder(target) \
2308 void shade_blocks_textured_unmodulated_##target(psx_gpu_struct *psx_gpu) \
2310 block_struct *block = psx_gpu->blocks; \
2311 u32 num_blocks = psx_gpu->num_blocks; \
2312 vec_8x16u draw_mask; \
2313 vec_8x16u test_mask = psx_gpu->test_mask; \
2314 u32 draw_mask_bits; \
2317 shade_blocks_load_msb_mask_##target(); \
2321 vec_8x16u zero_mask; \
2323 draw_mask_bits = block->draw_mask_bits; \
2324 dup_8x16b(draw_mask, draw_mask_bits); \
2325 tst_8x16b(draw_mask, draw_mask, test_mask); \
2327 pixels = block->texels; \
2329 cmpeqz_8x16b(zero_mask, pixels); \
2330 or_8x16b(zero_mask, draw_mask, zero_mask); \
2332 shade_blocks_store_##target(zero_mask, pixels); \
2339 #define shade_blocks_textured_unmodulated_dithered_builder(target) \
2340 void shade_blocks_textured_unmodulated_dithered_##target(psx_gpu_struct \
2343 block_struct *block = psx_gpu->blocks; \
2344 u32 num_blocks = psx_gpu->num_blocks; \
2345 vec_8x16u draw_mask; \
2346 vec_8x16u test_mask = psx_gpu->test_mask; \
2347 u32 draw_mask_bits; \
2350 shade_blocks_load_msb_mask_##target(); \
2354 vec_8x16u zero_mask; \
2356 draw_mask_bits = block->draw_mask_bits; \
2357 dup_8x16b(draw_mask, draw_mask_bits); \
2358 tst_8x16b(draw_mask, draw_mask, test_mask); \
2360 pixels = block->texels; \
2362 cmpeqz_8x16b(zero_mask, pixels); \
2363 or_8x16b(zero_mask, draw_mask, zero_mask); \
2365 shade_blocks_store_##target(zero_mask, pixels); \
2374 shade_blocks_textured_unmodulated_builder(indirect)
2375 shade_blocks_textured_unmodulated_builder(direct)
2380 void shade_blocks_unshaded_untextured_indirect(psx_gpu_struct *psx_gpu);
2381 void shade_blocks_unshaded_untextured_direct(psx_gpu_struct *psx_gpu);
2385 void shade_blocks_unshaded_untextured_indirect(psx_gpu_struct *psx_gpu)
2389 void shade_blocks_unshaded_untextured_direct(psx_gpu_struct *psx_gpu)
2391 block_struct *block = psx_gpu->blocks;
2392 u32 num_blocks = psx_gpu->num_blocks;
2394 vec_8x16u pixels = block->pixels;
2395 shade_blocks_load_msb_mask_direct();
2399 shade_blocks_store_direct(block->draw_mask, pixels);
2408 void shade_blocks_shaded_untextured(psx_gpu_struct *psx_gpu)
2413 #define blend_blocks_mask_evaluate_on() \
2414 vec_8x16u mask_pixels; \
2415 cmpltz_8x16b(mask_pixels, framebuffer_pixels); \
2416 or_8x16b(draw_mask, draw_mask, mask_pixels) \
2418 #define blend_blocks_mask_evaluate_off() \
2420 #define blend_blocks_average() \
2422 vec_8x16u pixels_no_msb; \
2423 vec_8x16u fb_pixels_no_msb; \
2425 vec_8x16u d128_0x0421; \
2426 vec_8x16u d128_0x8000; \
2428 dup_8x16b(d128_0x0421, 0x0421); \
2429 dup_8x16b(d128_0x8000, 0x8000); \
2431 eor_8x16b(blend_pixels, pixels, framebuffer_pixels); \
2432 bic_8x16b(pixels_no_msb, pixels, d128_0x8000); \
2433 and_8x16b(blend_pixels, blend_pixels, d128_0x0421); \
2434 sub_8x16b(blend_pixels, pixels_no_msb, blend_pixels); \
2435 bic_8x16b(fb_pixels_no_msb, framebuffer_pixels, d128_0x8000); \
2436 average_8x16b(blend_pixels, fb_pixels_no_msb, blend_pixels); \
2439 #define blend_blocks_add() \
2441 vec_8x16u pixels_rb, pixels_g; \
2442 vec_8x16u fb_rb, fb_g; \
2444 vec_8x16u d128_0x7C1F; \
2445 vec_8x16u d128_0x03E0; \
2447 dup_8x16b(d128_0x7C1F, 0x7C1F); \
2448 dup_8x16b(d128_0x03E0, 0x03E0); \
2450 and_8x16b(pixels_rb, pixels, d128_0x7C1F); \
2451 and_8x16b(pixels_g, pixels, d128_0x03E0); \
2453 and_8x16b(fb_rb, framebuffer_pixels, d128_0x7C1F); \
2454 and_8x16b(fb_g, framebuffer_pixels, d128_0x03E0); \
2456 add_8x16b(fb_rb, fb_rb, pixels_rb); \
2457 add_8x16b(fb_g, fb_g, pixels_g); \
2459 min_16x8b(vector_cast(vec_16x8u, fb_rb), vector_cast(vec_16x8u, fb_rb), \
2460 vector_cast(vec_16x8u, d128_0x7C1F)); \
2461 min_8x16b(fb_g, fb_g, d128_0x03E0); \
2463 or_8x16b(blend_pixels, fb_rb, fb_g); \
2466 #define blend_blocks_subtract() \
2468 vec_8x16u pixels_rb, pixels_g; \
2469 vec_8x16u fb_rb, fb_g; \
2471 vec_8x16u d128_0x7C1F; \
2472 vec_8x16u d128_0x03E0; \
2474 dup_8x16b(d128_0x7C1F, 0x7C1F); \
2475 dup_8x16b(d128_0x03E0, 0x03E0); \
2477 and_8x16b(pixels_rb, pixels, d128_0x7C1F); \
2478 and_8x16b(pixels_g, pixels, d128_0x03E0); \
2480 and_8x16b(fb_rb, framebuffer_pixels, d128_0x7C1F); \
2481 and_8x16b(fb_g, framebuffer_pixels, d128_0x03E0); \
2483 subs_16x8b(vector_cast(vec_16x8u, fb_rb), \
2484 vector_cast(vec_16x8u, fb_rb), vector_cast(vec_16x8u, pixels_rb)); \
2485 subs_8x16b(fb_g, fb_g, pixels_g); \
2487 or_8x16b(blend_pixels, fb_rb, fb_g); \
2490 #define blend_blocks_add_fourth() \
2492 vec_8x16u pixels_rb, pixels_g; \
2493 vec_8x16u pixels_fourth; \
2494 vec_8x16u fb_rb, fb_g; \
2496 vec_8x16u d128_0x7C1F; \
2497 vec_8x16u d128_0x1C07; \
2498 vec_8x16u d128_0x03E0; \
2499 vec_8x16u d128_0x00E0; \
2501 dup_8x16b(d128_0x7C1F, 0x7C1F); \
2502 dup_8x16b(d128_0x1C07, 0x1C07); \
2503 dup_8x16b(d128_0x03E0, 0x03E0); \
2504 dup_8x16b(d128_0x00E0, 0x00E0); \
2506 shr_8x16b(pixels_fourth, vector_cast(vec_8x16s, pixels), 2); \
2508 and_8x16b(fb_rb, framebuffer_pixels, d128_0x7C1F); \
2509 and_8x16b(fb_g, framebuffer_pixels, d128_0x03E0); \
2511 and_8x16b(pixels_rb, pixels_fourth, d128_0x1C07); \
2512 and_8x16b(pixels_g, pixels_fourth, d128_0x00E0); \
2514 add_8x16b(fb_rb, fb_rb, pixels_rb); \
2515 add_8x16b(fb_g, fb_g, pixels_g); \
2517 min_16x8b(vector_cast(vec_16x8u, fb_rb), vector_cast(vec_16x8u, fb_rb), \
2518 vector_cast(vec_16x8u, d128_0x7C1F)); \
2519 min_8x16b(fb_g, fb_g, d128_0x03E0); \
2521 or_8x16b(blend_pixels, fb_rb, fb_g); \
2524 #define blend_blocks_blended_combine_textured() \
2526 vec_8x16u blend_mask; \
2527 cmpltz_8x16b(blend_mask, pixels); \
2529 or_immediate_8x16b(blend_pixels, blend_pixels, 0x8000); \
2530 bif_8x16b(blend_pixels, pixels, blend_mask); \
2533 #define blend_blocks_blended_combine_untextured() \
2536 #define blend_blocks_body_blend(blend_mode, texturing) \
2538 blend_blocks_##blend_mode(); \
2539 blend_blocks_blended_combine_##texturing(); \
2542 #define blend_blocks_body_average(texturing) \
2543 blend_blocks_body_blend(average, texturing) \
2545 #define blend_blocks_body_add(texturing) \
2546 blend_blocks_body_blend(add, texturing) \
2548 #define blend_blocks_body_subtract(texturing) \
2549 blend_blocks_body_blend(subtract, texturing) \
2551 #define blend_blocks_body_add_fourth(texturing) \
2552 blend_blocks_body_blend(add_fourth, texturing) \
2554 #define blend_blocks_body_unblended(texturing) \
2555 blend_pixels = pixels \
2558 #define blend_blocks_builder(texturing, blend_mode, mask_evaluate) \
2560 blend_blocks_##texturing##_##blend_mode##_##mask_evaluate(psx_gpu_struct \
2563 block_struct *block = psx_gpu->blocks; \
2564 u32 num_blocks = psx_gpu->num_blocks; \
2565 vec_8x16u draw_mask; \
2567 vec_8x16u blend_pixels; \
2568 vec_8x16u framebuffer_pixels; \
2569 vec_8x16u msb_mask; \
2573 dup_8x16b(msb_mask, psx_gpu->mask_msb); \
2577 pixels = block->pixels; \
2578 draw_mask = block->draw_mask; \
2579 fb_ptr = block->fb_ptr; \
2581 load_8x16b(framebuffer_pixels, fb_ptr); \
2583 blend_blocks_mask_evaluate_##mask_evaluate(); \
2584 blend_blocks_body_##blend_mode(texturing); \
2586 or_8x16b(blend_pixels, blend_pixels, msb_mask); \
2587 bif_8x16b(framebuffer_pixels, blend_pixels, draw_mask); \
2588 store_8x16b(framebuffer_pixels, fb_ptr); \
2596 void blend_blocks_textured_average_off(psx_gpu_struct *psx_gpu);
2597 void blend_blocks_textured_average_on(psx_gpu_struct *psx_gpu);
2598 void blend_blocks_textured_add_off(psx_gpu_struct *psx_gpu);
2599 void blend_blocks_textured_add_on(psx_gpu_struct *psx_gpu);
2600 void blend_blocks_textured_subtract_off(psx_gpu_struct *psx_gpu);
2601 void blend_blocks_textured_subtract_on(psx_gpu_struct *psx_gpu);
2602 void blend_blocks_textured_add_fourth_off(psx_gpu_struct *psx_gpu);
2603 void blend_blocks_textured_add_fourth_on(psx_gpu_struct *psx_gpu);
2605 void blend_blocks_untextured_average_off(psx_gpu_struct *psx_gpu);
2606 void blend_blocks_untextured_average_on(psx_gpu_struct *psx_gpu);
2607 void blend_blocks_untextured_add_off(psx_gpu_struct *psx_gpu);
2608 void blend_blocks_untextured_add_on(psx_gpu_struct *psx_gpu);
2609 void blend_blocks_untextured_subtract_off(psx_gpu_struct *psx_gpu);
2610 void blend_blocks_untextured_subtract_on(psx_gpu_struct *psx_gpu);
2611 void blend_blocks_untextured_add_fourth_off(psx_gpu_struct *psx_gpu);
2612 void blend_blocks_untextured_add_fourth_on(psx_gpu_struct *psx_gpu);
2614 void blend_blocks_textured_unblended_off(psx_gpu_struct *psx_gpu);
2615 void blend_blocks_textured_unblended_on(psx_gpu_struct *psx_gpu);
2619 void blend_blocks_textured_unblended_off(psx_gpu_struct *psx_gpu)
2623 blend_blocks_builder(textured, average, off);
2624 blend_blocks_builder(textured, average, on);
2625 blend_blocks_builder(textured, add, off);
2626 blend_blocks_builder(textured, add, on);
2627 blend_blocks_builder(textured, subtract, off);
2628 blend_blocks_builder(textured, subtract, on);
2629 blend_blocks_builder(textured, add_fourth, off);
2630 blend_blocks_builder(textured, add_fourth, on);
2632 blend_blocks_builder(untextured, average, off);
2633 blend_blocks_builder(untextured, average, on);
2634 blend_blocks_builder(untextured, add, off);
2635 blend_blocks_builder(untextured, add, on);
2636 blend_blocks_builder(untextured, subtract, off);
2637 blend_blocks_builder(untextured, subtract, on);
2638 blend_blocks_builder(untextured, add_fourth, off);
2639 blend_blocks_builder(untextured, add_fourth, on);
2641 blend_blocks_builder(textured, unblended, on);
2646 #define vertex_swap(_a, _b) \
2648 vertex_struct *temp_vertex = _a; \
2651 triangle_winding ^= 1; \
2655 // Setup blocks parametric-variables:
2656 // SHADE TEXTURE_MAP SWIZZLING
2663 // 8 inputs, 6 combinations
2665 #define setup_blocks_switch_untextured_unshaded(dithering, target) \
2666 setup_blocks_unshaded_untextured_undithered_unswizzled_##target \
2668 #define setup_blocks_switch_untextured_shaded(dithering, target) \
2669 setup_blocks_shaded_untextured_##dithering##_unswizzled_##target \
2671 #define setup_blocks_switch_untextured(shading, texture_mode, dithering, \
2673 setup_blocks_switch_untextured_##shading(dithering, target) \
2675 #define setup_blocks_switch_texture_mode_4bpp(shading) \
2676 setup_blocks_##shading##_textured_dithered_swizzled_indirect \
2678 #define setup_blocks_switch_texture_mode_8bpp(shading) \
2679 setup_blocks_##shading##_textured_dithered_swizzled_indirect \
2681 #define setup_blocks_switch_texture_mode_16bpp(shading) \
2682 setup_blocks_##shading##_textured_dithered_unswizzled_indirect \
2684 #define setup_blocks_switch_textured(shading, texture_mode, dithering, target) \
2685 setup_blocks_switch_texture_mode_##texture_mode(shading) \
2687 #define setup_blocks_switch_blended(shading, texturing, texture_mode, \
2688 dithering, mask_evaluate) \
2689 setup_blocks_switch_##texturing(shading, texture_mode, dithering, indirect) \
2691 #define setup_blocks_switch_unblended_on(shading, texturing, texture_mode, \
2693 setup_blocks_switch_##texturing(shading, texture_mode, dithering, indirect) \
2695 #define setup_blocks_switch_unblended_off(shading, texturing, texture_mode, \
2697 setup_blocks_switch_##texturing(shading, texture_mode, dithering, direct) \
2699 #define setup_blocks_switch_unblended(shading, texturing, texture_mode, \
2700 dithering, mask_evaluate) \
2701 setup_blocks_switch_unblended_##mask_evaluate(shading, texturing, \
2702 texture_mode, dithering) \
2704 #define setup_blocks_switch(shading, texturing, texture_mode, dithering, \
2705 blending, mask_evaluate) \
2706 setup_blocks_switch_##blending(shading, texturing, texture_mode, \
2707 dithering, mask_evaluate) \
2712 #define texture_blocks_switch_untextured(texture_mode) \
2713 texture_blocks_untextured \
2715 #define texture_blocks_switch_textured(texture_mode) \
2716 texture_blocks_##texture_mode \
2718 #define texture_blocks_switch(texturing, texture_mode) \
2719 texture_blocks_switch_##texturing(texture_mode) \
2722 // Shade blocks parametric-variables:
2723 // SHADE TEXTURE_MAP MODULATE_TEXELS dither_mode
2732 // 16 inputs, 8 combinations
2734 #define shade_blocks_switch_unshaded_untextured(modulation, dithering, target) \
2735 shade_blocks_unshaded_untextured_##target \
2737 #define shade_blocks_switch_unshaded_textured_unmodulated(dithering, target) \
2738 shade_blocks_textured_unmodulated_##target \
2740 #define shade_blocks_switch_unshaded_textured_modulated(dithering, target) \
2741 shade_blocks_unshaded_textured_modulated_##dithering##_##target \
2743 #define shade_blocks_switch_unshaded_textured(modulation, dithering, target) \
2744 shade_blocks_switch_unshaded_textured_##modulation(dithering, target) \
2746 #define shade_blocks_switch_unshaded(texturing, modulation, dithering, target) \
2747 shade_blocks_switch_unshaded_##texturing(modulation, dithering, target) \
2749 #define shade_blocks_switch_shaded_untextured(modulation, dithering, target) \
2750 shade_blocks_shaded_untextured \
2752 #define shade_blocks_switch_shaded_textured_unmodulated(dithering, target) \
2753 shade_blocks_textured_unmodulated_##target \
2755 #define shade_blocks_switch_shaded_textured_modulated(dithering, target) \
2756 shade_blocks_shaded_textured_modulated_##dithering##_##target \
2758 #define shade_blocks_switch_shaded_textured(modulation, dithering, target) \
2759 shade_blocks_switch_shaded_textured_##modulation(dithering, target) \
2761 #define shade_blocks_switch_shaded(texturing, modulation, dithering, target) \
2762 shade_blocks_switch_shaded_##texturing(modulation, dithering, target) \
2764 #define shade_blocks_switch_mask_off(shading, texturing, modulation, \
2766 shade_blocks_switch_##shading(texturing, modulation, dithering, direct) \
2768 #define shade_blocks_switch_mask_on(shading, texturing, modulation, \
2770 shade_blocks_switch_##shading(texturing, modulation, dithering, indirect) \
2772 #define shade_blocks_switch_blended(shading, texturing, modulation, dithering, \
2774 shade_blocks_switch_##shading(texturing, modulation, dithering, indirect) \
2776 #define shade_blocks_switch_unblended(shading, texturing, modulation, \
2777 dithering, mask_evaluate) \
2778 shade_blocks_switch_mask_##mask_evaluate(shading, texturing, modulation, \
2781 #define shade_blocks_switch(shading, texturing, modulation, dithering, \
2782 blending, mask_evaluate) \
2783 shade_blocks_switch_##blending(shading, texturing, modulation, dithering, \
2787 // Blend blocks parametric-variables:
2788 // TEXTURE_MAP BLEND BM_A BM_B mask_evaluate
2807 // 32 inputs, 18 combinations
2809 #define blend_blocks_switch_unblended(texturing, blend_mode, mask_evaluate) \
2810 blend_blocks_textured_unblended_##mask_evaluate \
2812 #define blend_blocks_switch_blended(texturing, blend_mode, mask_evaluate) \
2813 blend_blocks_##texturing##_##blend_mode##_##mask_evaluate \
2815 #define blend_blocks_switch(texturing, blending, blend_mode, mask_evaluate) \
2816 blend_blocks_switch_##blending(texturing, blend_mode, mask_evaluate) \
2819 #define render_blocks_switch_block_modulation(texture_mode, blend_mode, \
2820 mask_evaluate, shading, dithering, texturing, blending, modulation) \
2822 setup_blocks_switch(shading, texturing, texture_mode, dithering, blending, \
2824 texture_blocks_switch(texturing, texture_mode), \
2825 shade_blocks_switch(shading, texturing, modulation, dithering, blending, \
2827 blend_blocks_switch(texturing, blending, blend_mode, mask_evaluate) \
2830 #define render_blocks_switch_block_blending(texture_mode, blend_mode, \
2831 mask_evaluate, shading, dithering, texturing, blending) \
2832 render_blocks_switch_block_modulation(texture_mode, blend_mode, \
2833 mask_evaluate, shading, dithering, texturing, blending, modulated), \
2834 render_blocks_switch_block_modulation(texture_mode, blend_mode, \
2835 mask_evaluate, shading, dithering, texturing, blending, unmodulated) \
2837 #define render_blocks_switch_block_texturing(texture_mode, blend_mode, \
2838 mask_evaluate, shading, dithering, texturing) \
2839 render_blocks_switch_block_blending(texture_mode, blend_mode, \
2840 mask_evaluate, shading, dithering, texturing, unblended), \
2841 render_blocks_switch_block_blending(texture_mode, blend_mode, \
2842 mask_evaluate, shading, dithering, texturing, blended) \
2844 #define render_blocks_switch_block_dithering(texture_mode, blend_mode, \
2845 mask_evaluate, shading, dithering) \
2846 render_blocks_switch_block_texturing(texture_mode, blend_mode, \
2847 mask_evaluate, shading, dithering, untextured), \
2848 render_blocks_switch_block_texturing(texture_mode, blend_mode, \
2849 mask_evaluate, shading, dithering, textured) \
2851 #define render_blocks_switch_block_shading(texture_mode, blend_mode, \
2852 mask_evaluate, shading) \
2853 render_blocks_switch_block_dithering(texture_mode, blend_mode, \
2854 mask_evaluate, shading, undithered), \
2855 render_blocks_switch_block_dithering(texture_mode, blend_mode, \
2856 mask_evaluate, shading, dithered) \
2858 #define render_blocks_switch_block_mask_evaluate(texture_mode, blend_mode, \
2860 render_blocks_switch_block_shading(texture_mode, blend_mode, mask_evaluate, \
2862 render_blocks_switch_block_shading(texture_mode, blend_mode, mask_evaluate, \
2865 #define render_blocks_switch_block_blend_mode(texture_mode, blend_mode) \
2866 render_blocks_switch_block_mask_evaluate(texture_mode, blend_mode, off), \
2867 render_blocks_switch_block_mask_evaluate(texture_mode, blend_mode, on) \
2869 #define render_blocks_switch_block_texture_mode(texture_mode) \
2870 render_blocks_switch_block_blend_mode(texture_mode, average), \
2871 render_blocks_switch_block_blend_mode(texture_mode, add), \
2872 render_blocks_switch_block_blend_mode(texture_mode, subtract), \
2873 render_blocks_switch_block_blend_mode(texture_mode, add_fourth) \
2875 #define render_blocks_switch_block() \
2876 render_blocks_switch_block_texture_mode(4bpp), \
2877 render_blocks_switch_block_texture_mode(8bpp), \
2878 render_blocks_switch_block_texture_mode(16bpp), \
2879 render_blocks_switch_block_texture_mode(4bpp) \
2882 render_block_handler_struct render_triangle_block_handlers[] =
2884 render_blocks_switch_block()
2887 #undef render_blocks_switch_block_modulation
2889 #define render_blocks_switch_block_modulation(texture_mode, blend_mode, \
2890 mask_evaluate, shading, dithering, texturing, blending, modulation) \
2892 "texture mode: " #texture_mode "\n" \
2893 "blend mode: " #blend_mode "\n" \
2894 "mask evaluation: " #mask_evaluate "\n" \
2901 char *render_block_flag_strings[] =
2903 render_blocks_switch_block()
2907 #define triangle_y_direction_up 1
2908 #define triangle_y_direction_flat 2
2909 #define triangle_y_direction_down 0
2911 #define triangle_winding_positive 0
2912 #define triangle_winding_negative 1
2914 #define triangle_set_direction(direction_variable, value) \
2915 u32 direction_variable = (u32)(value) >> 31; \
2917 direction_variable = 2 \
2919 #define triangle_case(direction_a, direction_b, direction_c, winding) \
2920 case (triangle_y_direction_##direction_a | \
2921 (triangle_y_direction_##direction_b << 2) | \
2922 (triangle_y_direction_##direction_c << 4) | \
2923 (triangle_winding_##winding << 6)) \
2925 static int prepare_triangle(psx_gpu_struct *psx_gpu, vertex_struct *vertexes,
2926 vertex_struct *vertexes_out[3])
2928 s32 y_top, y_bottom;
2930 u32 triangle_winding = 0;
2932 vertex_struct *a = &(vertexes[0]);
2933 vertex_struct *b = &(vertexes[1]);
2934 vertex_struct *c = &(vertexes[2]);
2936 triangle_area = triangle_signed_area_x2(a->x, a->y, b->x, b->y, c->x, c->y);
2942 if(triangle_area == 0)
2964 if((y_bottom - y_top) >= 512)
2972 if(triangle_area < 0)
2974 triangle_area = -triangle_area;
2975 triangle_winding ^= 1;
2990 if((c->x - psx_gpu->offset_x) >= 1024 || (c->x - a->x) >= 1024)
2998 if(invalidate_texture_cache_region_viewport(psx_gpu, a->x, y_top, c->x,
3007 psx_gpu->triangle_area = triangle_area;
3008 psx_gpu->triangle_winding = triangle_winding;
3010 vertexes_out[0] = a;
3011 vertexes_out[1] = b;
3012 vertexes_out[2] = c;
3017 static void render_triangle_p(psx_gpu_struct *psx_gpu,
3018 vertex_struct *vertex_ptrs[3], u32 flags)
3020 psx_gpu->num_spans = 0;
3022 vertex_struct *a = vertex_ptrs[0];
3023 vertex_struct *b = vertex_ptrs[1];
3024 vertex_struct *c = vertex_ptrs[2];
3026 s32 y_delta_a = b->y - a->y;
3027 s32 y_delta_b = c->y - b->y;
3028 s32 y_delta_c = c->y - a->y;
3030 triangle_set_direction(y_direction_a, y_delta_a);
3031 triangle_set_direction(y_direction_b, y_delta_b);
3032 triangle_set_direction(y_direction_c, y_delta_c);
3034 compute_all_gradients(psx_gpu, a, b, c);
3036 switch(y_direction_a | (y_direction_b << 2) | (y_direction_c << 4) |
3037 (psx_gpu->triangle_winding << 6))
3039 triangle_case(up, up, up, negative):
3040 triangle_case(up, up, flat, negative):
3041 triangle_case(up, up, down, negative):
3042 setup_spans_up_right(psx_gpu, a, b, c);
3045 triangle_case(flat, up, up, negative):
3046 triangle_case(flat, up, flat, negative):
3047 triangle_case(flat, up, down, negative):
3048 setup_spans_up_a(psx_gpu, a, b, c);
3051 triangle_case(down, up, up, negative):
3052 setup_spans_up_down(psx_gpu, a, c, b);
3055 triangle_case(down, up, flat, negative):
3056 setup_spans_down_a(psx_gpu, a, c, b);
3059 triangle_case(down, up, down, negative):
3060 setup_spans_down_right(psx_gpu, a, c, b);
3063 triangle_case(down, flat, up, negative):
3064 triangle_case(down, flat, flat, negative):
3065 triangle_case(down, flat, down, negative):
3066 setup_spans_down_b(psx_gpu, a, b, c);
3069 triangle_case(down, down, up, negative):
3070 triangle_case(down, down, flat, negative):
3071 triangle_case(down, down, down, negative):
3072 setup_spans_down_left(psx_gpu, a, b, c);
3075 triangle_case(up, up, up, positive):
3076 triangle_case(up, up, flat, positive):
3077 triangle_case(up, up, down, positive):
3078 setup_spans_up_left(psx_gpu, a, b, c);
3081 triangle_case(up, flat, up, positive):
3082 triangle_case(up, flat, flat, positive):
3083 triangle_case(up, flat, down, positive):
3084 setup_spans_up_b(psx_gpu, a, b, c);
3087 triangle_case(up, down, up, positive):
3088 setup_spans_up_right(psx_gpu, a, c, b);
3091 triangle_case(up, down, flat, positive):
3092 setup_spans_up_a(psx_gpu, a, c, b);
3095 triangle_case(up, down, down, positive):
3096 setup_spans_up_down(psx_gpu, a, b, c);
3099 triangle_case(flat, down, up, positive):
3100 triangle_case(flat, down, flat, positive):
3101 triangle_case(flat, down, down, positive):
3102 setup_spans_down_a(psx_gpu, a, b, c);
3105 triangle_case(down, down, up, positive):
3106 triangle_case(down, down, flat, positive):
3107 triangle_case(down, down, down, positive):
3108 setup_spans_down_right(psx_gpu, a, b, c);
3113 spans += psx_gpu->num_spans;
3116 if(unlikely(psx_gpu->render_mode & RENDER_INTERLACE_ENABLED))
3120 if(psx_gpu->render_mode & RENDER_INTERLACE_ODD)
3122 for(i = 0; i < psx_gpu->num_spans; i++)
3124 if((psx_gpu->span_edge_data[i].y & 1) == 0)
3125 psx_gpu->span_edge_data[i].num_blocks = 0;
3130 for(i = 0; i < psx_gpu->num_spans; i++)
3132 if(psx_gpu->span_edge_data[i].y & 1)
3133 psx_gpu->span_edge_data[i].num_blocks = 0;
3138 u32 render_state = flags &
3139 (RENDER_FLAGS_MODULATE_TEXELS | RENDER_FLAGS_BLEND |
3140 RENDER_FLAGS_TEXTURE_MAP | RENDER_FLAGS_SHADE);
3141 render_state |= psx_gpu->render_state_base;
3143 if((psx_gpu->render_state != render_state) ||
3144 (psx_gpu->primitive_type != PRIMITIVE_TYPE_TRIANGLE))
3146 psx_gpu->render_state = render_state;
3147 flush_render_block_buffer(psx_gpu);
3153 psx_gpu->primitive_type = PRIMITIVE_TYPE_TRIANGLE;
3155 psx_gpu->render_block_handler =
3156 &(render_triangle_block_handlers[render_state]);
3157 ((setup_blocks_function_type *)psx_gpu->render_block_handler->setup_blocks)
3161 void render_triangle(psx_gpu_struct *psx_gpu, vertex_struct *vertexes,
3164 vertex_struct *vertex_ptrs[3];
3165 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs))
3166 render_triangle_p(psx_gpu, vertex_ptrs, flags);
3170 void texture_sprite_blocks_8bpp(psx_gpu_struct *psx_gpu);
3174 void texture_sprite_blocks_8bpp(psx_gpu_struct *psx_gpu)
3176 block_struct *block = psx_gpu->blocks;
3177 u32 num_blocks = psx_gpu->num_blocks;
3180 vec_8x8u texel_indexes;
3182 u16 *clut_ptr = psx_gpu->clut_ptr;
3187 texel_indexes = block->r;
3189 for(i = 0; i < 8; i++)
3191 texels.e[i] = clut_ptr[texel_indexes.e[i]];
3194 block->texels = texels;
3204 #define setup_sprite_tiled_initialize_4bpp_clut() \
3205 u16 *clut_ptr = psx_gpu->clut_ptr; \
3206 vec_8x16u clut_a, clut_b; \
3207 vec_16x8u clut_low, clut_high; \
3209 load_8x16b(clut_a, clut_ptr); \
3210 load_8x16b(clut_b, clut_ptr + 8); \
3211 unzip_16x8b(clut_low, clut_high, clut_a, clut_b) \
3213 #define setup_sprite_tiled_initialize_4bpp() \
3214 setup_sprite_tiled_initialize_4bpp_clut(); \
3216 if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_4bpp_mask) \
3217 update_texture_4bpp_cache(psx_gpu) \
3219 #define setup_sprite_tiled_initialize_8bpp() \
3220 if(psx_gpu->current_texture_mask & psx_gpu->dirty_textures_8bpp_mask) \
3221 update_texture_8bpp_cache(psx_gpu) \
3224 #define setup_sprite_tile_fetch_texel_block_8bpp(offset) \
3225 texture_block_ptr = psx_gpu->texture_page_ptr + \
3226 ((texture_offset + offset) & texture_mask); \
3228 load_64b(texels, texture_block_ptr) \
3231 #define setup_sprite_tile_add_blocks(tile_num_blocks) \
3232 num_blocks += tile_num_blocks; \
3233 sprite_blocks += tile_num_blocks; \
3235 if(num_blocks > MAX_BLOCKS) \
3237 flush_render_block_buffer(psx_gpu); \
3238 num_blocks = tile_num_blocks; \
3239 block = psx_gpu->blocks; \
3242 #define setup_sprite_tile_full_4bpp(edge) \
3244 vec_8x8u texels_low, texels_high; \
3246 setup_sprite_tile_add_blocks(sub_tile_height * 2); \
3248 while(sub_tile_height) \
3250 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3251 tbl_16(texels_low, texels, clut_low); \
3252 tbl_16(texels_high, texels, clut_high); \
3253 zip_8x16b(pixels, texels_low, texels_high); \
3255 block->texels = pixels; \
3256 block->draw_mask_bits = left_mask_bits; \
3257 block->fb_ptr = fb_ptr; \
3260 setup_sprite_tile_fetch_texel_block_8bpp(8); \
3261 tbl_16(texels_low, texels, clut_low); \
3262 tbl_16(texels_high, texels, clut_high); \
3263 zip_8x16b(pixels, texels_low, texels_high); \
3265 block->texels = pixels; \
3266 block->draw_mask_bits = right_mask_bits; \
3267 block->fb_ptr = fb_ptr + 8; \
3271 texture_offset += 0x10; \
3272 sub_tile_height--; \
3274 texture_offset += 0xF00; \
3275 psx_gpu->num_blocks = num_blocks; \
3278 #define setup_sprite_tile_half_4bpp(edge) \
3280 vec_8x8u texels_low, texels_high; \
3282 setup_sprite_tile_add_blocks(sub_tile_height); \
3284 while(sub_tile_height) \
3286 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3287 tbl_16(texels_low, texels, clut_low); \
3288 tbl_16(texels_high, texels, clut_high); \
3289 zip_8x16b(pixels, texels_low, texels_high); \
3291 block->texels = pixels; \
3292 block->draw_mask_bits = edge##_mask_bits; \
3293 block->fb_ptr = fb_ptr; \
3297 texture_offset += 0x10; \
3298 sub_tile_height--; \
3300 texture_offset += 0xF00; \
3301 psx_gpu->num_blocks = num_blocks; \
3305 #define setup_sprite_tile_full_8bpp(edge) \
3307 setup_sprite_tile_add_blocks(sub_tile_height * 2); \
3309 while(sub_tile_height) \
3311 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3312 block->r = texels; \
3313 block->draw_mask_bits = left_mask_bits; \
3314 block->fb_ptr = fb_ptr; \
3317 setup_sprite_tile_fetch_texel_block_8bpp(8); \
3318 block->r = texels; \
3319 block->draw_mask_bits = right_mask_bits; \
3320 block->fb_ptr = fb_ptr + 8; \
3324 texture_offset += 0x10; \
3325 sub_tile_height--; \
3327 texture_offset += 0xF00; \
3328 psx_gpu->num_blocks = num_blocks; \
3331 #define setup_sprite_tile_half_8bpp(edge) \
3333 setup_sprite_tile_add_blocks(sub_tile_height * 2); \
3335 while(sub_tile_height) \
3337 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3338 block->r = texels; \
3339 block->draw_mask_bits = edge##_mask_bits; \
3340 block->fb_ptr = fb_ptr; \
3344 texture_offset += 0x10; \
3345 sub_tile_height--; \
3347 texture_offset += 0xF00; \
3348 psx_gpu->num_blocks = num_blocks; \
3352 #define setup_sprite_tile_column_edge_pre_adjust_half_right() \
3353 texture_offset = texture_offset_base + 8; \
3356 #define setup_sprite_tile_column_edge_pre_adjust_half_left() \
3357 texture_offset = texture_offset_base \
3359 #define setup_sprite_tile_column_edge_pre_adjust_half(edge) \
3360 setup_sprite_tile_column_edge_pre_adjust_half_##edge() \
3362 #define setup_sprite_tile_column_edge_pre_adjust_full(edge) \
3363 texture_offset = texture_offset_base \
3365 #define setup_sprite_tile_column_edge_post_adjust_half_right() \
3368 #define setup_sprite_tile_column_edge_post_adjust_half_left() \
3370 #define setup_sprite_tile_column_edge_post_adjust_half(edge) \
3371 setup_sprite_tile_column_edge_post_adjust_half_##edge() \
3373 #define setup_sprite_tile_column_edge_post_adjust_full(edge) \
3376 #define setup_sprite_tile_column_height_single(edge_mode, edge, texture_mode, \
3380 sub_tile_height = column_data; \
3381 setup_sprite_tile_column_edge_pre_adjust_##edge_mode##x4mode(edge); \
3382 setup_sprite_tile_##edge_mode##_##texture_mode##x4mode(edge); \
3383 setup_sprite_tile_column_edge_post_adjust_##edge_mode##x4mode(edge); \
3386 #define setup_sprite_tile_column_height_multi(edge_mode, edge, texture_mode, \
3390 u32 tiles_remaining = column_data >> 16; \
3391 sub_tile_height = column_data & 0xFF; \
3392 setup_sprite_tile_column_edge_pre_adjust_##edge_mode##x4mode(edge); \
3393 setup_sprite_tile_##edge_mode##_##texture_mode##x4mode(edge); \
3394 tiles_remaining -= 1; \
3396 while(tiles_remaining) \
3398 sub_tile_height = 16; \
3399 setup_sprite_tile_##edge_mode##_##texture_mode##x4mode(edge); \
3400 tiles_remaining--; \
3403 sub_tile_height = (column_data >> 8) & 0xFF; \
3404 setup_sprite_tile_##edge_mode##_##texture_mode##x4mode(edge); \
3405 setup_sprite_tile_column_edge_post_adjust_##edge_mode##x4mode(edge); \
3409 #define setup_sprite_column_data_single() \
3410 column_data = height \
3412 #define setup_sprite_column_data_multi() \
3413 column_data = 16 - offset_v; \
3414 column_data |= ((height_rounded & 0xF) + 1) << 8; \
3415 column_data |= (tile_height - 1) << 16 \
3418 #define RIGHT_MASK_BIT_SHIFT 8
3419 #define RIGHT_MASK_BIT_SHIFT_4x 16
3421 #define setup_sprite_tile_column_width_single(texture_mode, multi_height, \
3422 edge_mode, edge, x4mode) \
3424 setup_sprite_column_data_##multi_height(); \
3425 left_mask_bits = left_block_mask | right_block_mask; \
3426 right_mask_bits = left_mask_bits >> RIGHT_MASK_BIT_SHIFT##x4mode; \
3428 setup_sprite_tile_column_height_##multi_height(edge_mode, edge, \
3429 texture_mode, x4mode); \
3432 #define setup_sprite_tiled_advance_column() \
3433 texture_offset_base += 0x100; \
3434 if((texture_offset_base & 0xF00) == 0) \
3435 texture_offset_base -= (0x100 + 0xF00) \
3437 #define FB_PTR_MULTIPLIER 1
3438 #define FB_PTR_MULTIPLIER_4x 2
3440 #define setup_sprite_tile_column_width_multi(texture_mode, multi_height, \
3441 left_mode, right_mode, x4mode) \
3443 setup_sprite_column_data_##multi_height(); \
3444 s32 fb_ptr_advance_column = (16 - (1024 * height)) \
3445 * FB_PTR_MULTIPLIER##x4mode; \
3448 left_mask_bits = left_block_mask; \
3449 right_mask_bits = left_mask_bits >> RIGHT_MASK_BIT_SHIFT##x4mode; \
3451 setup_sprite_tile_column_height_##multi_height(left_mode, right, \
3452 texture_mode, x4mode); \
3453 fb_ptr += fb_ptr_advance_column; \
3455 left_mask_bits = 0x00; \
3456 right_mask_bits = 0x00; \
3460 setup_sprite_tiled_advance_column(); \
3461 setup_sprite_tile_column_height_##multi_height(full, none, \
3462 texture_mode, x4mode); \
3463 fb_ptr += fb_ptr_advance_column; \
3467 left_mask_bits = right_block_mask; \
3468 right_mask_bits = left_mask_bits >> RIGHT_MASK_BIT_SHIFT##x4mode; \
3470 setup_sprite_tiled_advance_column(); \
3471 setup_sprite_tile_column_height_##multi_height(right_mode, left, \
3472 texture_mode, x4mode); \
3477 #define setup_sprite_tiled_initialize_4bpp_4x() \
3478 setup_sprite_tiled_initialize_4bpp_clut() \
3480 #define setup_sprite_tiled_initialize_8bpp_4x() \
3483 #define setup_sprite_tile_full_4bpp_4x(edge) \
3485 vec_8x8u texels_low, texels_high; \
3486 vec_8x16u pixels, pixels_wide; \
3487 setup_sprite_tile_add_blocks(sub_tile_height * 2 * 4); \
3488 u32 left_mask_bits_a = left_mask_bits & 0xFF; \
3489 u32 left_mask_bits_b = left_mask_bits >> 8; \
3490 u32 right_mask_bits_a = right_mask_bits & 0xFF; \
3491 u32 right_mask_bits_b = right_mask_bits >> 8; \
3493 while(sub_tile_height) \
3495 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3496 tbl_16(texels_low, texels, clut_low); \
3497 tbl_16(texels_high, texels, clut_high); \
3498 zip_8x16b(pixels, texels_low, texels_high); \
3500 zip_4x32b(vector_cast(vec_4x32u, pixels_wide), pixels.low, pixels.low); \
3501 block->texels = pixels_wide; \
3502 block->draw_mask_bits = left_mask_bits_a; \
3503 block->fb_ptr = fb_ptr; \
3506 block->texels = pixels_wide; \
3507 block->draw_mask_bits = left_mask_bits_a; \
3508 block->fb_ptr = fb_ptr + 1024; \
3511 zip_4x32b(vector_cast(vec_4x32u, pixels_wide), pixels.high, pixels.high); \
3512 block->texels = pixels_wide; \
3513 block->draw_mask_bits = left_mask_bits_b; \
3514 block->fb_ptr = fb_ptr + 8; \
3517 block->texels = pixels_wide; \
3518 block->draw_mask_bits = left_mask_bits_b; \
3519 block->fb_ptr = fb_ptr + 1024 + 8; \
3522 setup_sprite_tile_fetch_texel_block_8bpp(8); \
3523 tbl_16(texels_low, texels, clut_low); \
3524 tbl_16(texels_high, texels, clut_high); \
3525 zip_8x16b(pixels, texels_low, texels_high); \
3527 zip_4x32b(vector_cast(vec_4x32u, pixels_wide), pixels.low, pixels.low); \
3528 block->texels = pixels_wide; \
3529 block->draw_mask_bits = right_mask_bits_a; \
3530 block->fb_ptr = fb_ptr + 16; \
3533 block->texels = pixels_wide; \
3534 block->draw_mask_bits = right_mask_bits_a; \
3535 block->fb_ptr = fb_ptr + 1024 + 16; \
3538 zip_4x32b(vector_cast(vec_4x32u, pixels_wide), pixels.high, pixels.high); \
3539 block->texels = pixels_wide; \
3540 block->draw_mask_bits = right_mask_bits_b; \
3541 block->fb_ptr = fb_ptr + 24; \
3544 block->texels = pixels_wide; \
3545 block->draw_mask_bits = right_mask_bits_b; \
3546 block->fb_ptr = fb_ptr + 1024 + 24; \
3550 texture_offset += 0x10; \
3551 sub_tile_height--; \
3553 texture_offset += 0xF00; \
3554 psx_gpu->num_blocks = num_blocks; \
3557 #define setup_sprite_tile_half_4bpp_4x(edge) \
3559 vec_8x8u texels_low, texels_high; \
3560 vec_8x16u pixels, pixels_wide; \
3561 setup_sprite_tile_add_blocks(sub_tile_height * 4); \
3562 u32 edge##_mask_bits_a = edge##_mask_bits & 0xFF; \
3563 u32 edge##_mask_bits_b = edge##_mask_bits >> 8; \
3565 while(sub_tile_height) \
3567 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3568 tbl_16(texels_low, texels, clut_low); \
3569 tbl_16(texels_high, texels, clut_high); \
3570 zip_8x16b(pixels, texels_low, texels_high); \
3572 zip_4x32b(vector_cast(vec_4x32u, pixels_wide), pixels.low, pixels.low); \
3573 block->texels = pixels_wide; \
3574 block->draw_mask_bits = edge##_mask_bits_a; \
3575 block->fb_ptr = fb_ptr; \
3578 block->texels = pixels_wide; \
3579 block->draw_mask_bits = edge##_mask_bits_a; \
3580 block->fb_ptr = fb_ptr + 1024; \
3583 zip_4x32b(vector_cast(vec_4x32u, pixels_wide), pixels.high, pixels.high); \
3584 block->texels = pixels_wide; \
3585 block->draw_mask_bits = edge##_mask_bits_b; \
3586 block->fb_ptr = fb_ptr + 8; \
3589 block->texels = pixels_wide; \
3590 block->draw_mask_bits = edge##_mask_bits_b; \
3591 block->fb_ptr = fb_ptr + 1024 + 8; \
3595 texture_offset += 0x10; \
3596 sub_tile_height--; \
3598 texture_offset += 0xF00; \
3599 psx_gpu->num_blocks = num_blocks; \
3603 #define setup_sprite_tile_full_8bpp_4x(edge) \
3605 setup_sprite_tile_add_blocks(sub_tile_height * 2 * 4); \
3606 vec_16x8u texels_wide; \
3607 u32 left_mask_bits_a = left_mask_bits & 0xFF; \
3608 u32 left_mask_bits_b = left_mask_bits >> 8; \
3609 u32 right_mask_bits_a = right_mask_bits & 0xFF; \
3610 u32 right_mask_bits_b = right_mask_bits >> 8; \
3612 while(sub_tile_height) \
3614 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3615 zip_8x16b(vector_cast(vec_8x16u, texels_wide), texels, texels); \
3616 block->r = texels_wide.low; \
3617 block->draw_mask_bits = left_mask_bits_a; \
3618 block->fb_ptr = fb_ptr; \
3621 block->r = texels_wide.low; \
3622 block->draw_mask_bits = left_mask_bits_a; \
3623 block->fb_ptr = fb_ptr + 1024; \
3626 block->r = texels_wide.high; \
3627 block->draw_mask_bits = left_mask_bits_b; \
3628 block->fb_ptr = fb_ptr + 8; \
3631 block->r = texels_wide.high; \
3632 block->draw_mask_bits = left_mask_bits_b; \
3633 block->fb_ptr = fb_ptr + 1024 + 8; \
3636 setup_sprite_tile_fetch_texel_block_8bpp(8); \
3637 zip_8x16b(vector_cast(vec_8x16u, texels_wide), texels, texels); \
3638 block->r = texels_wide.low; \
3639 block->draw_mask_bits = right_mask_bits_a; \
3640 block->fb_ptr = fb_ptr + 16; \
3643 block->r = texels_wide.low; \
3644 block->draw_mask_bits = right_mask_bits_a; \
3645 block->fb_ptr = fb_ptr + 1024 + 16; \
3648 block->r = texels_wide.high; \
3649 block->draw_mask_bits = right_mask_bits_b; \
3650 block->fb_ptr = fb_ptr + 24; \
3653 block->r = texels_wide.high; \
3654 block->draw_mask_bits = right_mask_bits_b; \
3655 block->fb_ptr = fb_ptr + 24 + 1024; \
3659 texture_offset += 0x10; \
3660 sub_tile_height--; \
3662 texture_offset += 0xF00; \
3663 psx_gpu->num_blocks = num_blocks; \
3666 #define setup_sprite_tile_half_8bpp_4x(edge) \
3668 setup_sprite_tile_add_blocks(sub_tile_height * 4); \
3669 vec_16x8u texels_wide; \
3670 u32 edge##_mask_bits_a = edge##_mask_bits & 0xFF; \
3671 u32 edge##_mask_bits_b = edge##_mask_bits >> 8; \
3673 while(sub_tile_height) \
3675 setup_sprite_tile_fetch_texel_block_8bpp(0); \
3676 zip_8x16b(vector_cast(vec_8x16u, texels_wide), texels, texels); \
3677 block->r = texels_wide.low; \
3678 block->draw_mask_bits = edge##_mask_bits_a; \
3679 block->fb_ptr = fb_ptr; \
3682 block->r = texels_wide.low; \
3683 block->draw_mask_bits = edge##_mask_bits_a; \
3684 block->fb_ptr = fb_ptr + 1024; \
3687 block->r = texels_wide.high; \
3688 block->draw_mask_bits = edge##_mask_bits_b; \
3689 block->fb_ptr = fb_ptr + 8; \
3692 block->r = texels_wide.high; \
3693 block->draw_mask_bits = edge##_mask_bits_b; \
3694 block->fb_ptr = fb_ptr + 8 + 1024; \
3698 texture_offset += 0x10; \
3699 sub_tile_height--; \
3701 texture_offset += 0xF00; \
3702 psx_gpu->num_blocks = num_blocks; \
3706 #define setup_sprite_tile_column_edge_pre_adjust_half_right_4x() \
3707 texture_offset = texture_offset_base + 8; \
3710 #define setup_sprite_tile_column_edge_pre_adjust_half_left_4x() \
3711 texture_offset = texture_offset_base \
3713 #define setup_sprite_tile_column_edge_pre_adjust_half_4x(edge) \
3714 setup_sprite_tile_column_edge_pre_adjust_half_##edge##_4x() \
3716 #define setup_sprite_tile_column_edge_pre_adjust_full_4x(edge) \
3717 texture_offset = texture_offset_base \
3719 #define setup_sprite_tile_column_edge_post_adjust_half_right_4x() \
3722 #define setup_sprite_tile_column_edge_post_adjust_half_left_4x() \
3724 #define setup_sprite_tile_column_edge_post_adjust_half_4x(edge) \
3725 setup_sprite_tile_column_edge_post_adjust_half_##edge##_4x() \
3727 #define setup_sprite_tile_column_edge_post_adjust_full_4x(edge) \
3730 #define setup_sprite_offset_u_adjust() \
3732 #define setup_sprite_comapre_left_block_mask() \
3733 ((left_block_mask & 0xFF) == 0xFF) \
3735 #define setup_sprite_comapre_right_block_mask() \
3736 (((right_block_mask >> 8) & 0xFF) == 0xFF) \
3739 #define setup_sprite_offset_u_adjust_4x() \
3741 offset_u_right = offset_u_right * 2 + 1 \
3743 #define setup_sprite_comapre_left_block_mask_4x() \
3744 ((left_block_mask & 0xFFFF) == 0xFFFF) \
3746 #define setup_sprite_comapre_right_block_mask_4x() \
3747 (((right_block_mask >> 16) & 0xFFFF) == 0xFFFF) \
3750 #define setup_sprite_tiled_builder(texture_mode, x4mode) \
3751 void setup_sprite_##texture_mode##x4mode(psx_gpu_struct *psx_gpu, s32 x, s32 y,\
3752 s32 u, s32 v, s32 width, s32 height, u32 color) \
3754 s32 offset_u = u & 0xF; \
3755 s32 offset_v = v & 0xF; \
3757 s32 width_rounded = offset_u + width + 15; \
3758 s32 height_rounded = offset_v + height + 15; \
3759 s32 tile_height = height_rounded / 16; \
3760 s32 tile_width = width_rounded / 16; \
3761 u32 offset_u_right = width_rounded & 0xF; \
3763 setup_sprite_offset_u_adjust##x4mode(); \
3765 u32 left_block_mask = ~(0xFFFFFFFF << offset_u); \
3766 u32 right_block_mask = 0xFFFFFFFE << offset_u_right; \
3768 u32 left_mask_bits; \
3769 u32 right_mask_bits; \
3771 u32 sub_tile_height; \
3774 u32 texture_mask = (psx_gpu->texture_mask_width & 0xF) | \
3775 ((psx_gpu->texture_mask_height & 0xF) << 4) | \
3776 ((psx_gpu->texture_mask_width >> 4) << 8) | \
3777 ((psx_gpu->texture_mask_height >> 4) << 12); \
3778 u32 texture_offset = ((v & 0xF) << 4) | ((u & 0xF0) << 4) | \
3779 ((v & 0xF0) << 8); \
3780 u32 texture_offset_base = texture_offset; \
3783 u16 *fb_ptr = psx_gpu->vram_out_ptr + (y * 1024) + (x - offset_u); \
3784 u32 num_blocks = psx_gpu->num_blocks; \
3785 block_struct *block = psx_gpu->blocks + num_blocks; \
3787 u16 *texture_block_ptr; \
3790 setup_sprite_tiled_initialize_##texture_mode##x4mode(); \
3792 control_mask = tile_width == 1; \
3793 control_mask |= (tile_height == 1) << 1; \
3794 control_mask |= setup_sprite_comapre_left_block_mask##x4mode() << 2; \
3795 control_mask |= setup_sprite_comapre_right_block_mask##x4mode() << 3; \
3797 sprites_##texture_mode++; \
3799 switch(control_mask) \
3803 setup_sprite_tile_column_width_multi(texture_mode, multi, full, full, \
3808 setup_sprite_tile_column_width_single(texture_mode, multi, full, none, \
3813 setup_sprite_tile_column_width_multi(texture_mode, single, full, full, \
3818 setup_sprite_tile_column_width_single(texture_mode, single, full, none, \
3823 setup_sprite_tile_column_width_multi(texture_mode, multi, half, full, \
3828 setup_sprite_tile_column_width_single(texture_mode, multi, half, right, \
3833 setup_sprite_tile_column_width_multi(texture_mode, single, half, full, \
3838 setup_sprite_tile_column_width_single(texture_mode, single, half, right, \
3843 setup_sprite_tile_column_width_multi(texture_mode, multi, full, half, \
3848 setup_sprite_tile_column_width_single(texture_mode, multi, half, left, \
3853 setup_sprite_tile_column_width_multi(texture_mode, single, full, half, \
3858 setup_sprite_tile_column_width_single(texture_mode, single, half, left, \
3863 setup_sprite_tile_column_width_multi(texture_mode, multi, half, half, \
3868 setup_sprite_tile_column_width_multi(texture_mode, single, half, half, \
3874 void setup_sprite_4bpp(psx_gpu_struct *psx_gpu, s32 x, s32 y, s32 u, s32 v,
3875 s32 width, s32 height, u32 color);
3876 void setup_sprite_8bpp(psx_gpu_struct *psx_gpu, s32 x, s32 y, s32 u, s32 v,
3877 s32 width, s32 height, u32 color);
3878 void setup_sprite_16bpp(psx_gpu_struct *psx_gpu, s32 x, s32 y, s32 u, s32 v,
3879 s32 width, s32 height, u32 color);
3881 void setup_sprite_4bpp_4x(psx_gpu_struct *psx_gpu, s32 x, s32 y, s32 u, s32 v,
3882 s32 width, s32 height, u32 color);
3883 void setup_sprite_8bpp_4x(psx_gpu_struct *psx_gpu, s32 x, s32 y, s32 u, s32 v,
3884 s32 width, s32 height, u32 color);
3885 void setup_sprite_16bpp_4x(psx_gpu_struct *psx_gpu, s32 x, s32 y, s32 u, s32 v,
3886 s32 width, s32 height, u32 color);
3889 setup_sprite_tiled_builder(4bpp,);
3890 setup_sprite_tiled_builder(8bpp,);
3892 setup_sprite_tiled_builder(4bpp,_4x);
3893 setup_sprite_tiled_builder(8bpp,_4x);
3895 void setup_sprite_16bpp(psx_gpu_struct *psx_gpu, s32 x, s32 y, s32 u,
3896 s32 v, s32 width, s32 height, u32 color)
3898 u32 left_offset = u & 0x7;
3899 u32 width_rounded = width + left_offset + 7;
3901 u16 *fb_ptr = psx_gpu->vram_out_ptr + (y * 1024) + (s32)(x - left_offset);
3902 u32 right_width = width_rounded & 0x7;
3903 u32 block_width = width_rounded / 8;
3904 u32 fb_ptr_pitch = (1024 + 8) - (block_width * 8);
3906 u32 left_mask_bits = ~(0xFF << left_offset);
3907 u32 right_mask_bits = 0xFE << right_width;
3909 u32 texture_offset_base = u + (v * 1024);
3911 psx_gpu->texture_mask_width | (psx_gpu->texture_mask_height * 1024);
3913 u32 blocks_remaining;
3914 u32 num_blocks = psx_gpu->num_blocks;
3915 block_struct *block = psx_gpu->blocks + num_blocks;
3917 u16 *texture_page_ptr = psx_gpu->texture_page_ptr;
3918 u16 *texture_block_ptr;
3920 texture_offset_base &= ~0x7;
3924 if(block_width == 1)
3926 u32 mask_bits = left_mask_bits | right_mask_bits;
3933 if(num_blocks > MAX_BLOCKS)
3935 flush_render_block_buffer(psx_gpu);
3937 block = psx_gpu->blocks;
3941 texture_page_ptr + (texture_offset_base & texture_mask);
3943 load_128b(block->texels, texture_block_ptr);
3944 block->draw_mask_bits = mask_bits;
3945 block->fb_ptr = fb_ptr;
3949 texture_offset_base += 1024;
3953 psx_gpu->num_blocks = num_blocks;
3962 blocks_remaining = block_width - 2;
3963 num_blocks += block_width;
3964 sprite_blocks += block_width;
3966 if(num_blocks > MAX_BLOCKS)
3968 flush_render_block_buffer(psx_gpu);
3969 num_blocks = block_width;
3970 block = psx_gpu->blocks;
3973 texture_offset = texture_offset_base;
3974 texture_offset_base += 1024;
3976 texture_block_ptr = texture_page_ptr + (texture_offset & texture_mask);
3977 load_128b(block->texels, texture_block_ptr);
3979 block->draw_mask_bits = left_mask_bits;
3980 block->fb_ptr = fb_ptr;
3982 texture_offset += 8;
3986 while(blocks_remaining)
3988 texture_block_ptr = texture_page_ptr + (texture_offset & texture_mask);
3989 load_128b(block->texels, texture_block_ptr);
3991 block->draw_mask_bits = 0;
3992 block->fb_ptr = fb_ptr;
3994 texture_offset += 8;
4001 texture_block_ptr = texture_page_ptr + (texture_offset & texture_mask);
4002 load_128b(block->texels, texture_block_ptr);
4004 block->draw_mask_bits = right_mask_bits;
4005 block->fb_ptr = fb_ptr;
4007 fb_ptr += fb_ptr_pitch;
4011 psx_gpu->num_blocks = num_blocks;
4018 void setup_sprite_untextured(psx_gpu_struct *psx_gpu, s32 x, s32 y, s32 u,
4019 s32 v, s32 width, s32 height, u32 color)
4021 u32 right_width = ((width - 1) & 0x7) + 1;
4022 u32 right_mask_bits = (0xFF << right_width);
4023 u16 *fb_ptr = psx_gpu->vram_out_ptr + (y * 1024) + x;
4024 u32 block_width = (width + 7) / 8;
4025 u32 fb_ptr_pitch = 1024 - ((block_width - 1) * 8);
4026 u32 blocks_remaining;
4027 u32 num_blocks = psx_gpu->num_blocks;
4028 block_struct *block = psx_gpu->blocks + num_blocks;
4030 u32 color_r = color & 0xFF;
4031 u32 color_g = (color >> 8) & 0xFF;
4032 u32 color_b = (color >> 16) & 0xFF;
4034 vec_8x16u right_mask;
4035 vec_8x16u test_mask = psx_gpu->test_mask;
4036 vec_8x16u zero_mask;
4038 sprites_untextured++;
4040 color = (color_r >> 3) | ((color_g >> 3) << 5) | ((color_b >> 3) << 10);
4042 dup_8x16b(colors, color);
4043 dup_8x16b(zero_mask, 0x00);
4044 dup_8x16b(right_mask, right_mask_bits);
4045 tst_8x16b(right_mask, right_mask, test_mask);
4049 blocks_remaining = block_width - 1;
4050 num_blocks += block_width;
4053 sprite_blocks += block_width;
4056 if(num_blocks > MAX_BLOCKS)
4058 flush_render_block_buffer(psx_gpu);
4059 num_blocks = block_width;
4060 block = psx_gpu->blocks;
4063 while(blocks_remaining)
4065 block->pixels = colors;
4066 block->draw_mask = zero_mask;
4067 block->fb_ptr = fb_ptr;
4074 block->pixels = colors;
4075 block->draw_mask = right_mask;
4076 block->fb_ptr = fb_ptr;
4079 fb_ptr += fb_ptr_pitch;
4082 psx_gpu->num_blocks = num_blocks;
4088 #define setup_sprite_blocks_switch_textured(texture_mode) \
4089 setup_sprite_##texture_mode \
4091 #define setup_sprite_blocks_switch_untextured(texture_mode) \
4092 setup_sprite_untextured \
4094 #define setup_sprite_blocks_switch(texturing, texture_mode) \
4095 setup_sprite_blocks_switch_##texturing(texture_mode) \
4098 #define texture_sprite_blocks_switch_4bpp() \
4099 texture_blocks_untextured \
4101 #define texture_sprite_blocks_switch_8bpp() \
4102 texture_sprite_blocks_8bpp \
4104 #define texture_sprite_blocks_switch_16bpp() \
4105 texture_blocks_untextured \
4107 #define texture_sprite_blocks_switch_untextured(texture_mode) \
4108 texture_blocks_untextured \
4110 #define texture_sprite_blocks_switch_textured(texture_mode) \
4111 texture_sprite_blocks_switch_##texture_mode() \
4113 #define render_sprite_blocks_switch_block_modulation(texture_mode, blend_mode, \
4114 mask_evaluate, shading, dithering, texturing, blending, modulation) \
4116 setup_sprite_blocks_switch(texturing, texture_mode), \
4117 texture_sprite_blocks_switch_##texturing(texture_mode), \
4118 shade_blocks_switch(unshaded, texturing, modulation, undithered, blending, \
4120 blend_blocks_switch(texturing, blending, blend_mode, mask_evaluate) \
4123 #define render_sprite_blocks_switch_block_blending(texture_mode, blend_mode, \
4124 mask_evaluate, shading, dithering, texturing, blending) \
4125 render_sprite_blocks_switch_block_modulation(texture_mode, blend_mode, \
4126 mask_evaluate, shading, dithering, texturing, blending, modulated), \
4127 render_sprite_blocks_switch_block_modulation(texture_mode, blend_mode, \
4128 mask_evaluate, shading, dithering, texturing, blending, unmodulated) \
4130 #define render_sprite_blocks_switch_block_texturing(texture_mode, blend_mode, \
4131 mask_evaluate, shading, dithering, texturing) \
4132 render_sprite_blocks_switch_block_blending(texture_mode, blend_mode, \
4133 mask_evaluate, shading, dithering, texturing, unblended), \
4134 render_sprite_blocks_switch_block_blending(texture_mode, blend_mode, \
4135 mask_evaluate, shading, dithering, texturing, blended) \
4137 #define render_sprite_blocks_switch_block_dithering(texture_mode, blend_mode, \
4138 mask_evaluate, shading, dithering) \
4139 render_sprite_blocks_switch_block_texturing(texture_mode, blend_mode, \
4140 mask_evaluate, shading, dithering, untextured), \
4141 render_sprite_blocks_switch_block_texturing(texture_mode, blend_mode, \
4142 mask_evaluate, shading, dithering, textured) \
4144 #define render_sprite_blocks_switch_block_shading(texture_mode, blend_mode, \
4145 mask_evaluate, shading) \
4146 render_sprite_blocks_switch_block_dithering(texture_mode, blend_mode, \
4147 mask_evaluate, shading, undithered), \
4148 render_sprite_blocks_switch_block_dithering(texture_mode, blend_mode, \
4149 mask_evaluate, shading, dithered) \
4151 #define render_sprite_blocks_switch_block_mask_evaluate(texture_mode, \
4152 blend_mode, mask_evaluate) \
4153 render_sprite_blocks_switch_block_shading(texture_mode, blend_mode, \
4154 mask_evaluate, unshaded), \
4155 render_sprite_blocks_switch_block_shading(texture_mode, blend_mode, \
4156 mask_evaluate, shaded) \
4158 #define render_sprite_blocks_switch_block_blend_mode(texture_mode, blend_mode) \
4159 render_sprite_blocks_switch_block_mask_evaluate(texture_mode, blend_mode, \
4161 render_sprite_blocks_switch_block_mask_evaluate(texture_mode, blend_mode, \
4164 #define render_sprite_blocks_switch_block_texture_mode(texture_mode) \
4165 render_sprite_blocks_switch_block_blend_mode(texture_mode, average), \
4166 render_sprite_blocks_switch_block_blend_mode(texture_mode, add), \
4167 render_sprite_blocks_switch_block_blend_mode(texture_mode, subtract), \
4168 render_sprite_blocks_switch_block_blend_mode(texture_mode, add_fourth) \
4170 #define render_sprite_blocks_switch_block() \
4171 render_sprite_blocks_switch_block_texture_mode(4bpp), \
4172 render_sprite_blocks_switch_block_texture_mode(8bpp), \
4173 render_sprite_blocks_switch_block_texture_mode(16bpp), \
4174 render_sprite_blocks_switch_block_texture_mode(4bpp) \
4177 render_block_handler_struct render_sprite_block_handlers[] =
4179 render_sprite_blocks_switch_block()
4183 void render_sprite(psx_gpu_struct *psx_gpu, s32 x, s32 y, u32 u, u32 v,
4184 s32 width, s32 height, u32 flags, u32 color)
4186 s32 x_right = x + width - 1;
4187 s32 y_bottom = y + height - 1;
4193 if(invalidate_texture_cache_region_viewport(psx_gpu, x, y, x_right,
4199 if(x < psx_gpu->viewport_start_x)
4201 u32 clip = psx_gpu->viewport_start_x - x;
4207 if(y < psx_gpu->viewport_start_y)
4209 s32 clip = psx_gpu->viewport_start_y - y;
4215 if(x_right > psx_gpu->viewport_end_x)
4216 width -= x_right - psx_gpu->viewport_end_x;
4218 if(y_bottom > psx_gpu->viewport_end_y)
4219 height -= y_bottom - psx_gpu->viewport_end_y;
4221 if((width <= 0) || (height <= 0))
4225 span_pixels += width * height;
4229 u32 render_state = flags &
4230 (RENDER_FLAGS_MODULATE_TEXELS | RENDER_FLAGS_BLEND |
4231 RENDER_FLAGS_TEXTURE_MAP);
4233 (psx_gpu->render_state_base & ~RENDER_STATE_DITHER);
4235 if((psx_gpu->render_state != render_state) ||
4236 (psx_gpu->primitive_type != PRIMITIVE_TYPE_SPRITE))
4238 psx_gpu->render_state = render_state;
4239 flush_render_block_buffer(psx_gpu);
4245 psx_gpu->primitive_type = PRIMITIVE_TYPE_SPRITE;
4249 if(psx_gpu->triangle_color != color)
4251 flush_render_block_buffer(psx_gpu);
4252 psx_gpu->triangle_color = color;
4255 if(color == 0x808080)
4256 render_state |= RENDER_FLAGS_MODULATE_TEXELS;
4258 render_block_handler_struct *render_block_handler =
4259 &(render_sprite_block_handlers[render_state]);
4260 psx_gpu->render_block_handler = render_block_handler;
4262 ((setup_sprite_function_type *)render_block_handler->setup_blocks)
4263 (psx_gpu, x, y, u, v, width, height, color);
4266 #define draw_pixel_line_mask_evaluate_yes() \
4267 if(*vram_ptr & 0x8000) \
4269 #define draw_pixel_line_mask_evaluate_no() \
4272 #define draw_pixel_line_shaded() \
4274 color_r = fixed_to_int(current_r); \
4275 color_g = fixed_to_int(current_g); \
4276 color_b = fixed_to_int(current_b); \
4278 current_r += gradient_r; \
4279 current_g += gradient_g; \
4280 current_b += gradient_b; \
4283 #define draw_pixel_line_unshaded() \
4285 color_r = color & 0xFF; \
4286 color_g = (color >> 8) & 0xFF; \
4287 color_b = (color >> 16) & 0xFF; \
4291 #define draw_pixel_line_dithered(_x, _y) \
4293 u32 dither_xor = _x ^ _y; \
4294 s32 dither_offset = (dither_xor >> 1) & 0x1; \
4295 dither_offset |= (_y & 0x1) << 1; \
4296 dither_offset |= (dither_xor & 0x1) << 2; \
4297 dither_offset -= 4; \
4299 color_r += dither_offset; \
4300 color_g += dither_offset; \
4301 color_b += dither_offset; \
4322 #define draw_pixel_line_undithered(_x, _y) \
4325 #define draw_pixel_line_average() \
4326 color_r = (color_r + fb_r) / 2; \
4327 color_g = (color_g + fb_g) / 2; \
4328 color_b = (color_b + fb_b) / 2 \
4330 #define draw_pixel_line_add() \
4345 #define draw_pixel_line_subtract() \
4346 color_r = fb_r - color_r; \
4347 color_g = fb_g - color_g; \
4348 color_b = fb_b - color_b; \
4359 #define draw_pixel_line_add_fourth() \
4360 color_r = fb_r + (color_r / 4); \
4361 color_g = fb_g + (color_g / 4); \
4362 color_b = fb_b + (color_b / 4); \
4374 #define draw_pixel_line_blended(blend_mode) \
4375 s32 fb_pixel = *vram_ptr; \
4376 s32 fb_r = fb_pixel & 0x1F; \
4377 s32 fb_g = (fb_pixel >> 5) & 0x1F; \
4378 s32 fb_b = (fb_pixel >> 10) & 0x1F; \
4380 draw_pixel_line_##blend_mode() \
4382 #define draw_pixel_line_unblended(blend_mode) \
4385 #define draw_pixel_line(_x, _y, shading, blending, dithering, mask_evaluate, \
4387 if((_x >= psx_gpu->viewport_start_x) && (_y >= psx_gpu->viewport_start_y) && \
4388 (_x <= psx_gpu->viewport_end_x) && (_y <= psx_gpu->viewport_end_y)) \
4390 draw_pixel_line_mask_evaluate_##mask_evaluate() \
4392 draw_pixel_line_##shading(); \
4393 draw_pixel_line_##dithering(_x, _y); \
4399 draw_pixel_line_##blending(blend_mode); \
4401 *vram_ptr = color_r | (color_g << 5) | (color_b << 10) | \
4402 psx_gpu->mask_msb; \
4406 #define update_increment(value) \
4409 #define update_decrement(value) \
4412 #define update_vram_row_increment(value) \
4415 #define update_vram_row_decrement(value) \
4418 #define compare_increment(a, b) \
4421 #define compare_decrement(a, b) \
4424 #define set_line_gradients(minor) \
4426 s32 gradient_divisor = delta_##minor; \
4427 if(gradient_divisor != 0) \
4429 gradient_r = int_to_fixed(vertex_b->r - vertex_a->r) / gradient_divisor; \
4430 gradient_g = int_to_fixed(vertex_b->g - vertex_a->g) / gradient_divisor; \
4431 gradient_b = int_to_fixed(vertex_b->b - vertex_a->b) / gradient_divisor; \
4439 current_r = fixed_center(vertex_a->r); \
4440 current_g = fixed_center(vertex_a->g); \
4441 current_b = fixed_center(vertex_a->b); \
4444 #define draw_line_span_horizontal(direction, shading, blending, dithering, \
4445 mask_evaluate, blend_mode) \
4448 error_step = delta_y * 2; \
4449 error_wrap = delta_x * 2; \
4453 set_line_gradients(x); \
4455 for(current_x = x_a; current_x <= x_b; current_x++) \
4457 draw_pixel_line(current_x, current_y, shading, blending, dithering, \
4458 mask_evaluate, blend_mode); \
4459 error += error_step; \
4462 if(error >= error_wrap) \
4464 update_##direction(current_y); \
4465 update_vram_row_##direction(); \
4466 error -= error_wrap; \
4471 #define draw_line_span_vertical(direction, shading, blending, dithering, \
4472 mask_evaluate, blend_mode) \
4475 error_step = delta_x * 2; \
4476 error_wrap = delta_y * 2; \
4480 set_line_gradients(y); \
4482 for(current_y = y_a; compare_##direction(current_y, y_b); \
4483 update_##direction(current_y)) \
4485 draw_pixel_line(current_x, current_y, shading, blending, dithering, \
4486 mask_evaluate, blend_mode); \
4487 error += error_step; \
4488 update_vram_row_##direction(); \
4490 if(error > error_wrap) \
4494 error -= error_wrap; \
4500 #define render_line_body(shading, blending, dithering, mask_evaluate, \
4506 if(delta_x > delta_y) \
4508 draw_line_span_horizontal(decrement, shading, blending, dithering, \
4509 mask_evaluate, blend_mode); \
4513 draw_line_span_vertical(decrement, shading, blending, dithering, \
4514 mask_evaluate, blend_mode); \
4519 if(delta_x > delta_y) \
4521 draw_line_span_horizontal(increment, shading, blending, dithering, \
4522 mask_evaluate, blend_mode); \
4526 draw_line_span_vertical(increment, shading, blending, dithering, \
4527 mask_evaluate, blend_mode); \
4532 void render_line(psx_gpu_struct *psx_gpu, vertex_struct *vertexes, u32 flags,
4533 u32 color, int double_resolution)
4535 s32 color_r, color_g, color_b;
4536 u32 triangle_winding = 0;
4538 fixed_type gradient_r = 0;
4539 fixed_type gradient_g = 0;
4540 fixed_type gradient_b = 0;
4541 fixed_type current_r = 0;
4542 fixed_type current_g = 0;
4543 fixed_type current_b = 0;
4548 s32 delta_x, delta_y;
4559 flush_render_block_buffer(psx_gpu);
4560 psx_gpu->primitive_type = PRIMITIVE_TYPE_LINE;
4562 vertex_struct *vertex_a = &(vertexes[0]);
4563 vertex_struct *vertex_b = &(vertexes[1]);
4571 if(vertex_a->x >= vertex_b->x)
4573 vertex_swap(vertex_a, vertex_b);
4582 delta_x = x_b - x_a;
4583 delta_y = y_b - y_a;
4585 if(delta_x >= 1024 || delta_y >= 512 || delta_y <= -512)
4588 if(double_resolution)
4598 flags &= ~RENDER_FLAGS_TEXTURE_MAP;
4600 vram_ptr = psx_gpu->vram_out_ptr + (y_a * 1024) + x_a;
4604 if(flags & RENDER_FLAGS_SHADE)
4605 control_mask |= 0x1;
4607 if(flags & RENDER_FLAGS_BLEND)
4609 control_mask |= 0x2;
4610 control_mask |= ((psx_gpu->render_state_base >> 6) & 0x3) << 4;
4613 if(psx_gpu->render_state_base & RENDER_STATE_DITHER)
4614 control_mask |= 0x4;
4616 if(psx_gpu->render_state_base & RENDER_STATE_MASK_EVALUATE)
4617 control_mask |= 0x8;
4619 switch(control_mask)
4622 render_line_body(unshaded, unblended, undithered, no, none);
4626 render_line_body(shaded, unblended, undithered, no, none);
4630 render_line_body(unshaded, blended, undithered, no, average);
4634 render_line_body(shaded, blended, undithered, no, average);
4638 render_line_body(unshaded, unblended, dithered, no, none);
4642 render_line_body(shaded, unblended, dithered, no, none);
4646 render_line_body(unshaded, blended, dithered, no, average);
4650 render_line_body(shaded, blended, dithered, no, average);
4654 render_line_body(unshaded, unblended, undithered, yes, none);
4658 render_line_body(shaded, unblended, undithered, yes, none);
4662 render_line_body(unshaded, blended, undithered, yes, average);
4666 render_line_body(shaded, blended, undithered, yes, average);
4670 render_line_body(unshaded, unblended, dithered, yes, none);
4674 render_line_body(shaded, unblended, dithered, yes, none);
4678 render_line_body(unshaded, blended, dithered, yes, average);
4682 render_line_body(shaded, blended, dithered, yes, average);
4686 render_line_body(unshaded, blended, undithered, no, add);
4690 render_line_body(shaded, blended, undithered, no, add);
4694 render_line_body(unshaded, blended, dithered, no, add);
4698 render_line_body(shaded, blended, dithered, no, add);
4702 render_line_body(unshaded, blended, undithered, yes, add);
4706 render_line_body(shaded, blended, undithered, yes, add);
4710 render_line_body(unshaded, blended, dithered, yes, add);
4714 render_line_body(shaded, blended, dithered, yes, add);
4718 render_line_body(unshaded, blended, undithered, no, subtract);
4722 render_line_body(shaded, blended, undithered, no, subtract);
4726 render_line_body(unshaded, blended, dithered, no, subtract);
4730 render_line_body(shaded, blended, dithered, no, subtract);
4734 render_line_body(unshaded, blended, undithered, yes, subtract);
4738 render_line_body(shaded, blended, undithered, yes, subtract);
4742 render_line_body(unshaded, blended, dithered, yes, subtract);
4746 render_line_body(shaded, blended, dithered, yes, subtract);
4750 render_line_body(unshaded, blended, undithered, no, add_fourth);
4754 render_line_body(shaded, blended, undithered, no, add_fourth);
4758 render_line_body(unshaded, blended, dithered, no, add_fourth);
4762 render_line_body(shaded, blended, dithered, no, add_fourth);
4766 render_line_body(unshaded, blended, undithered, yes, add_fourth);
4770 render_line_body(shaded, blended, undithered, yes, add_fourth);
4774 render_line_body(unshaded, blended, dithered, yes, add_fourth);
4778 render_line_body(shaded, blended, dithered, yes, add_fourth);
4784 void render_block_fill(psx_gpu_struct *psx_gpu, u32 color, u32 x, u32 y,
4785 u32 width, u32 height)
4787 if((width == 0) || (height == 0))
4790 invalidate_texture_cache_region(psx_gpu, x, y, x + width - 1, y + height - 1);
4792 u32 r = color & 0xFF;
4793 u32 g = (color >> 8) & 0xFF;
4794 u32 b = (color >> 16) & 0xFF;
4795 u32 color_16bpp = (r >> 3) | ((g >> 3) << 5) | ((b >> 3) << 10) |
4797 u32 color_32bpp = color_16bpp | (color_16bpp << 16);
4799 u32 *vram_ptr = (u32 *)(psx_gpu->vram_out_ptr + x + (y * 1024));
4801 u32 pitch = 512 - (width / 2);
4804 if(psx_gpu->render_mode & RENDER_INTERLACE_ENABLED)
4809 if(psx_gpu->render_mode & RENDER_INTERLACE_ODD)
4818 vram_ptr[0] = color_32bpp;
4819 vram_ptr[1] = color_32bpp;
4820 vram_ptr[2] = color_32bpp;
4821 vram_ptr[3] = color_32bpp;
4822 vram_ptr[4] = color_32bpp;
4823 vram_ptr[5] = color_32bpp;
4824 vram_ptr[6] = color_32bpp;
4825 vram_ptr[7] = color_32bpp;
4836 void render_block_fill_enh(psx_gpu_struct *psx_gpu, u32 color, u32 x, u32 y,
4837 u32 width, u32 height)
4839 if((width == 0) || (height == 0))
4845 u32 r = color & 0xFF;
4846 u32 g = (color >> 8) & 0xFF;
4847 u32 b = (color >> 16) & 0xFF;
4848 u32 color_16bpp = (r >> 3) | ((g >> 3) << 5) | ((b >> 3) << 10) |
4850 u32 color_32bpp = color_16bpp | (color_16bpp << 16);
4852 u32 *vram_ptr = (u32 *)(psx_gpu->vram_out_ptr + x + (y * 1024));
4854 u32 pitch = 1024 / 2 - (width / 2);
4862 vram_ptr[0] = color_32bpp;
4863 vram_ptr[1] = color_32bpp;
4864 vram_ptr[2] = color_32bpp;
4865 vram_ptr[3] = color_32bpp;
4866 vram_ptr[4] = color_32bpp;
4867 vram_ptr[5] = color_32bpp;
4868 vram_ptr[6] = color_32bpp;
4869 vram_ptr[7] = color_32bpp;
4880 void render_block_copy(psx_gpu_struct *psx_gpu, u16 *source, u32 x, u32 y,
4881 u32 width, u32 height, u32 pitch)
4883 u16 *vram_ptr = psx_gpu->vram_ptr + x + (y * 1024);
4885 u32 mask_msb = psx_gpu->mask_msb;
4887 if((width == 0) || (height == 0))
4890 flush_render_block_buffer(psx_gpu);
4891 invalidate_texture_cache_region(psx_gpu, x, y, x + width - 1, y + height - 1);
4893 for(draw_y = 0; draw_y < height; draw_y++)
4895 for(draw_x = 0; draw_x < width; draw_x++)
4897 vram_ptr[draw_x] = source[draw_x] | mask_msb;
4905 void render_block_move(psx_gpu_struct *psx_gpu, u32 source_x, u32 source_y,
4906 u32 dest_x, u32 dest_y, u32 width, u32 height)
4908 render_block_copy(psx_gpu, psx_gpu->vram_ptr + source_x + (source_y * 1024),
4909 dest_x, dest_y, width, height, 1024);
4913 void initialize_reciprocal_table(void)
4916 u32 height_normalized;
4917 u32 height_reciprocal;
4920 for(height = 1; height < sizeof(reciprocal_table)
4921 / sizeof(reciprocal_table[0]); height++)
4923 shift = __builtin_clz(height);
4924 height_normalized = height << shift;
4925 height_reciprocal = ((1ULL << 51) + (height_normalized - 1)) /
4928 shift = 32 - (51 - shift);
4930 reciprocal_table[height] = (height_reciprocal << 10) | shift;
4935 #define dither_table_row(a, b, c, d) \
4936 ((a & 0xFF) | ((b & 0xFF) << 8) | ((c & 0xFF) << 16) | ((d & 0xFF) << 24)) \
4938 void initialize_psx_gpu(psx_gpu_struct *psx_gpu, u16 *vram)
4940 vec_8x16u test_mask =
4941 { { { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 } } };
4943 psx_gpu->test_mask = test_mask;
4945 psx_gpu->dirty_textures_4bpp_mask = 0xFFFFFFFF;
4946 psx_gpu->dirty_textures_8bpp_mask = 0xFFFFFFFF;
4947 psx_gpu->dirty_textures_8bpp_alternate_mask = 0xFFFFFFFF;
4948 psx_gpu->viewport_mask = 0;
4949 psx_gpu->current_texture_page = 0;
4950 psx_gpu->current_texture_mask = 0;
4951 psx_gpu->last_8bpp_texture_page = 0;
4953 psx_gpu->clut_settings = 0;
4954 psx_gpu->texture_settings = 0;
4955 psx_gpu->render_state = 0;
4956 psx_gpu->render_state_base = 0;
4957 psx_gpu->num_blocks = 0;
4958 psx_gpu->uvrgb_phase = 0x8000;
4960 psx_gpu->vram_ptr = vram;
4961 psx_gpu->vram_out_ptr = vram;
4963 psx_gpu->texture_page_base = psx_gpu->vram_ptr;
4964 psx_gpu->texture_page_ptr = psx_gpu->vram_ptr;
4965 psx_gpu->clut_ptr = psx_gpu->vram_ptr;
4967 psx_gpu->mask_msb = 0;
4969 psx_gpu->texture_window_x = 0;
4970 psx_gpu->texture_window_y = 0;
4971 psx_gpu->texture_mask_width = 0xFF;
4972 psx_gpu->texture_mask_height = 0xFF;
4974 psx_gpu->render_mode = 0;
4976 memset(psx_gpu->vram_ptr, 0, sizeof(u16) * 1024 * 512);
4978 initialize_reciprocal_table();
4985 // (minus ones(4) * 4)
4987 // d0: (1 3 5 7): x1 ^ y1
4988 // d1: (2 3 6 7): y0
4989 // d2: (4 5 6 7): x0 ^ y0
4991 psx_gpu->dither_table[0] = dither_table_row(-4, 0, -3, 1);
4992 psx_gpu->dither_table[1] = dither_table_row(2, -2, 3, -1);
4993 psx_gpu->dither_table[2] = dither_table_row(-3, 1, -4, 0);
4994 psx_gpu->dither_table[3] = dither_table_row(3, -1, 2, -2);
4996 psx_gpu->primitive_type = PRIMITIVE_TYPE_UNKNOWN;
4998 psx_gpu->enhancement_x_threshold = 256;
5004 gettimeofday(&tv, NULL);
5006 return (tv.tv_sec * 1000000ULL) + tv.tv_usec;
5014 __asm__ volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(counter));
5019 void init_counter(void)
5022 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(value));
5023 value |= 5; // master enable, ccnt reset
5024 value &= ~8; // ccnt divider 0
5025 asm volatile("mcr p15, 0, %0, c9, c12, 0" :: "r"(value));
5026 // enable cycle counter
5027 asm volatile("mcr p15, 0, %0, c9, c12, 1" :: "r"(1 << 31));
5030 void triangle_benchmark(psx_gpu_struct *psx_gpu)
5037 const u32 iterations = 500000;
5039 psx_gpu->num_blocks = 64;
5040 psx_gpu->clut_ptr = psx_gpu->vram_ptr;
5042 for(i = 0; i < 64; i++)
5044 memset(&(psx_gpu->blocks[i].r), 0, 16);
5049 ticks = get_counter();
5051 for(i = 0; i < iterations; i++)
5053 texture_sprite_blocks_8bpp(psx_gpu);
5056 ticks_elapsed = get_counter() - ticks;
5058 printf("benchmark: %lf cycles\n", (double)ticks_elapsed / (iterations * 64));
5063 #include "psx_gpu_4x.c"