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