5c7b67ceb43203c03a78a0373693e84929b273d8
[pcsx_rearmed.git] / plugins / gpu_unai / gpu_raster_sprite.h
1 /***************************************************************************
2 *   Copyright (C) 2010 PCSX4ALL Team                                      *
3 *   Copyright (C) 2010 Unai                                               *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program; if not, write to the                         *
17 *   Free Software Foundation, Inc.,                                       *
18 *   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
19 ***************************************************************************/
20
21 #ifndef __GPU_UNAI_GPU_RASTER_SPRITE_H__
22 #define __GPU_UNAI_GPU_RASTER_SPRITE_H__
23
24 ///////////////////////////////////////////////////////////////////////////////
25 //  GPU internal sprite drawing functions
26
27 void gpuDrawS(PtrUnion packet, const PS gpuSpriteDriver, s32 *w_out, s32 *h_out)
28 {
29         s32 x0, x1, y0, y1;
30         u32 u0, v0;
31
32         //NOTE: Must 11-bit sign-extend the whole sum here, not just packet X/Y,
33         // or sprites in 1st level of SkullMonkeys disappear when walking right.
34         // This now matches behavior of Mednafen and PCSX Rearmed's gpu_neon:
35         x0 = GPU_EXPANDSIGN(le16_to_s16(packet.U2[2]) + gpu_unai.DrawingOffset[0]);
36         y0 = GPU_EXPANDSIGN(le16_to_s16(packet.U2[3]) + gpu_unai.DrawingOffset[1]);
37
38         u32 w = le16_to_u16(packet.U2[6]) & 0x3ff; // Max width is 1023
39         u32 h = le16_to_u16(packet.U2[7]) & 0x1ff; // Max height is 511
40         x1 = x0 + w;
41         y1 = y0 + h;
42
43         s32 xmin, xmax, ymin, ymax;
44         xmin = gpu_unai.DrawingArea[0]; xmax = gpu_unai.DrawingArea[2];
45         ymin = gpu_unai.DrawingArea[1]; ymax = gpu_unai.DrawingArea[3];
46
47         u0 = packet.U1[8];
48         v0 = packet.U1[9];
49
50         s32 temp;
51         temp = ymin - y0;
52         if (temp > 0) { y0 = ymin; v0 += temp; }
53         if (y1 > ymax) y1 = ymax;
54         if (y1 <= y0) return;
55
56         temp = xmin - x0;
57         if (temp > 0) { x0 = xmin; u0 += temp; }
58         if (x1 > xmax) x1 = xmax;
59         x1 -= x0;
60         if (x1 <= 0) return;
61         *w_out = x1;
62         *h_out = y1 - y0;
63
64         le16_t *Pixel = &gpu_unai.vram[FRAME_OFFSET(x0, y0)];
65
66         gpu_unai.inn.r5 = packet.U1[0] >> 3;
67         gpu_unai.inn.g5 = packet.U1[1] >> 3;
68         gpu_unai.inn.b5 = packet.U1[2] >> 3;
69         gpu_unai.inn.u = u0;
70         gpu_unai.inn.v = v0;
71         gpu_unai.inn.y0 = y0;
72         gpu_unai.inn.y1 = y1;
73         gpuSpriteDriver(Pixel, x1, (u8 *)gpu_unai.inn.TBA, gpu_unai.inn);
74 }
75
76 void gpuDrawT(PtrUnion packet, const PT gpuTileDriver, s32 *w_out, s32 *h_out)
77 {
78         s32 x0, x1, y0, y1;
79
80         // This now matches behavior of Mednafen and PCSX Rearmed's gpu_neon:
81         x0 = GPU_EXPANDSIGN(le16_to_s16(packet.U2[2]) + gpu_unai.DrawingOffset[0]);
82         y0 = GPU_EXPANDSIGN(le16_to_s16(packet.U2[3]) + gpu_unai.DrawingOffset[1]);
83
84         u32 w = le16_to_u16(packet.U2[4]) & 0x3ff; // Max width is 1023
85         u32 h = le16_to_u16(packet.U2[5]) & 0x1ff; // Max height is 511
86         x1 = x0 + w;
87         y1 = y0 + h;
88
89         s32 xmin, xmax, ymin, ymax;
90         xmin = gpu_unai.DrawingArea[0]; xmax = gpu_unai.DrawingArea[2];
91         ymin = gpu_unai.DrawingArea[1]; ymax = gpu_unai.DrawingArea[3];
92
93         if (y0 < ymin) y0 = ymin;
94         if (y1 > ymax) y1 = ymax;
95         if (y1 <= y0) return;
96
97         if (x0 < xmin) x0 = xmin;
98         if (x1 > xmax) x1 = xmax;
99         x1 -= x0;
100         if (x1 <= 0) return;
101         *w_out = x1;
102         *h_out = y1 - y0;
103
104         const u16 Data = GPU_RGB16(le32_to_u32(packet.U4[0]));
105         le16_t *Pixel = &gpu_unai.vram[FRAME_OFFSET(x0, y0)];
106
107         gpu_unai.inn.y0 = y0;
108         gpu_unai.inn.y1 = y1;
109         gpuTileDriver(Pixel, Data, x1, gpu_unai.inn);
110 }
111
112 #endif /* __GPU_UNAI_GPU_RASTER_SPRITE_H__ */