Merge pull request #392 from justinweiss/ctr-use-floatmath
[pcsx_rearmed.git] / plugins / gpu_unai / gpu_inner_light.h
CommitLineData
86aad47b 1/***************************************************************************
030d1121 2* Copyright (C) 2016 PCSX4ALL Team *
86aad47b 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 _OP_LIGHT_H_
22#define _OP_LIGHT_H_
23
24// GPU color operations for lighting calculations
25
030d1121 26static void SetupLightLUT()
27{
28 // 1024-entry lookup table that modulates 5-bit texture + 5-bit light value.
29 // A light value of 15 does not modify the incoming texture color.
30 // LightLUT[32*32] array is initialized to following values:
31 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
33 // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
34 // 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
35 // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
36 // 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9,
37 // 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,11,11,
38 // 0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9,10,10,10,11,11,12,12,13,13,
39 // 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,
40 // 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8, 9, 9,10,10,11,11,12,12,13,14,14,15,15,16,16,17,
41 // 0, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9,10,10,11,11,12,13,13,14,15,15,16,16,17,18,18,19,
42 // 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,19,20,21,
43 // 0, 0, 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9,10,11,12,12,13,14,15,15,16,17,18,18,19,20,21,21,22,23,
44 // 0, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9,10,11,12,13,13,14,15,16,17,17,18,19,20,21,21,22,23,24,25,
45 // 0, 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,15,16,17,18,19,20,21,21,22,23,24,25,26,27,
46 // 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
47 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
48 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,31,
49 // 0, 1, 2, 3, 4, 5, 6, 7, 9,10,11,12,13,14,15,16,18,19,20,21,22,23,24,25,27,28,29,30,31,31,31,31,
50 // 0, 1, 2, 3, 4, 5, 7, 8, 9,10,11,13,14,15,16,17,19,20,21,22,23,24,26,27,28,29,30,31,31,31,31,31,
51 // 0, 1, 2, 3, 5, 6, 7, 8,10,11,12,13,15,16,17,18,20,21,22,23,25,26,27,28,30,31,31,31,31,31,31,31,
52 // 0, 1, 2, 3, 5, 6, 7, 9,10,11,13,14,15,17,18,19,21,22,23,24,26,27,28,30,31,31,31,31,31,31,31,31,
53 // 0, 1, 2, 4, 5, 6, 8, 9,11,12,13,15,16,17,19,20,22,23,24,26,27,28,30,31,31,31,31,31,31,31,31,31,
54 // 0, 1, 2, 4, 5, 7, 8,10,11,12,14,15,17,18,20,21,23,24,25,27,28,30,31,31,31,31,31,31,31,31,31,31,
55 // 0, 1, 3, 4, 6, 7, 9,10,12,13,15,16,18,19,21,22,24,25,27,28,30,31,31,31,31,31,31,31,31,31,31,31,
56 // 0, 1, 3, 4, 6, 7, 9,10,12,14,15,17,18,20,21,23,25,26,28,29,31,31,31,31,31,31,31,31,31,31,31,31,
57 // 0, 1, 3, 4, 6, 8, 9,11,13,14,16,17,19,21,22,24,26,27,29,30,31,31,31,31,31,31,31,31,31,31,31,31,
58 // 0, 1, 3, 5, 6, 8,10,11,13,15,16,18,20,21,23,25,27,28,30,31,31,31,31,31,31,31,31,31,31,31,31,31,
59 // 0, 1, 3, 5, 7, 8,10,12,14,15,17,19,21,22,24,26,28,29,31,31,31,31,31,31,31,31,31,31,31,31,31,31,
60 // 0, 1, 3, 5, 7, 9,10,12,14,16,18,19,21,23,25,27,29,30,31,31,31,31,31,31,31,31,31,31,31,31,31,31,
61 // 0, 1, 3, 5, 7, 9,11,13,15,16,18,20,22,24,26,28,30,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,
62 // 0, 1, 3, 5, 7, 9,11,13,15,17,19,21,23,25,27,29,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31
63
64 for (int j=0; j < 32; ++j) {
65 for (int i=0; i < 32; ++i) {
66 int val = i * j / 16;
67 if (val > 31) val = 31;
68 gpu_unai.LightLUT[(j*32) + i] = val;
69 }
70 }
71}
72
73
74////////////////////////////////////////////////////////////////////////////////
75// Create packed Gouraud fixed-pt 8.3:8.3:8.2 rgb triplet
76//
77// INPUT:
78// 'r','g','b' are 8.10 fixed-pt color components (r shown here)
79// 'r' input: --------------rrrrrrrrXXXXXXXXXX
80// ^ bit 31
81// RETURNS:
82// u32 output: rrrrrrrrXXXggggggggXXXbbbbbbbbXX
83// ^ bit 31
84// Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and '-' don't care
85////////////////////////////////////////////////////////////////////////////////
86GPU_INLINE u32 gpuPackGouraudCol(u32 r, u32 g, u32 b)
87{
88 return ((u32)(b>> 8)&(0x03ff ))
89 | ((u32)(g<< 3)&(0x07ff<<10))
90 | ((u32)(r<<14)&(0x07ff<<21));
91}
92
93
94////////////////////////////////////////////////////////////////////////////////
95// Create packed increment for Gouraud fixed-pt 8.3:8.3:8.2 rgb triplet
96//
97// INPUT:
98// Sign-extended 8.10 fixed-pt r,g,b color increment values (only dr is shown)
99// 'dr' input: ssssssssssssssrrrrrrrrXXXXXXXXXX
100// ^ bit 31
101// RETURNS:
102// u32 output: rrrrrrrrXXXggggggggXXXbbbbbbbbXX
103// ^ bit 31
104// Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and 's' sign bits
105//
106// NOTE: The correctness of this code/method has not been fully verified,
107// having been merely factored out from original code in
108// poly-drawing functions. Feel free to check/improve it -senquack
109////////////////////////////////////////////////////////////////////////////////
110GPU_INLINE u32 gpuPackGouraudColInc(s32 dr, s32 dg, s32 db)
111{
112 u32 dr_tmp = (u32)(dr << 14)&(0xffffffff<<21); if (dr < 0) dr_tmp += 1<<21;
113 u32 dg_tmp = (u32)(dg << 3)&(0xffffffff<<10); if (dg < 0) dg_tmp += 1<<10;
114 u32 db_tmp = (u32)(db >> 8)&(0xffffffff ); if (db < 0) db_tmp += 1<< 0;
115 return db_tmp + dg_tmp + dr_tmp;
86aad47b 116}
86aad47b 117
030d1121 118
119////////////////////////////////////////////////////////////////////////////////
120// Extract bgr555 color from Gouraud u32 fixed-pt 8.3:8.3:8.2 rgb triplet
121//
122// INPUT:
123// 'gCol' input: rrrrrrrrXXXggggggggXXXbbbbbbbbXX
124// ^ bit 31
125// RETURNS:
126// u16 output: 0bbbbbgggggrrrrr
127// ^ bit 16
128// Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and '0' zero
129////////////////////////////////////////////////////////////////////////////////
130GPU_INLINE u16 gpuLightingRGB(u32 gCol)
131{
132 return ((gCol<< 5)&0x7C00) |
133 ((gCol>>11)&0x03E0) |
134 (gCol>>27);
135}
136
137
138////////////////////////////////////////////////////////////////////////////////
139// Convert packed Gouraud u32 fixed-pt 8.3:8.3:8.2 rgb triplet in 'gCol'
140// to padded u32 5.4:5.4:5.4 bgr fixed-pt triplet, suitable for use
141// with HQ 24-bit lighting/quantization.
142//
143// INPUT:
144// 'gCol' input: rrrrrrrrXXXggggggggXXXbbbbbbbbXX
145// ^ bit 31
146// RETURNS:
147// u32 output: 000bbbbbXXXX0gggggXXXX0rrrrrXXXX
148// ^ bit 31
149// Where 'X' are fixed-pt bits, '0' zero-padding, and '-' is don't care
150////////////////////////////////////////////////////////////////////////////////
151GPU_INLINE u32 gpuLightingRGB24(u32 gCol)
152{
153 return ((gCol<<19) & (0x1FF<<20)) |
154 ((gCol>> 2) & (0x1FF<<10)) |
155 (gCol>>23);
156}
157
158
159////////////////////////////////////////////////////////////////////////////////
160// Apply fast (low-precision) 5-bit lighting to bgr555 texture color:
161//
162// INPUT:
163// 'r5','g5','b5' are unsigned 5-bit color values, value of 15
164// is midpoint that doesn't modify that component of texture
165// 'uSrc' input: -bbbbbgggggrrrrr
166// ^ bit 16
167// RETURNS:
168// u16 output: 0bbbbbgggggrrrrr
169// Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care
170////////////////////////////////////////////////////////////////////////////////
171GPU_INLINE u16 gpuLightingTXT(u16 uSrc, u8 r5, u8 g5, u8 b5)
86aad47b 172{
030d1121 173 return (gpu_unai.LightLUT[((uSrc&0x7C00)>>5) | b5] << 10) |
174 (gpu_unai.LightLUT[ (uSrc&0x03E0) | g5] << 5) |
175 (gpu_unai.LightLUT[((uSrc&0x001F)<<5) | r5] );
176}
177
178
179////////////////////////////////////////////////////////////////////////////////
180// Apply fast (low-precision) 5-bit Gouraud lighting to bgr555 texture color:
181//
182// INPUT:
183// 'gCol' is a packed Gouraud u32 fixed-pt 8.3:8.3:8.2 rgb triplet, value of
184// 15.0 is midpoint that does not modify color of texture
185// gCol input : rrrrrXXXXXXgggggXXXXXXbbbbbXXXXX
186// ^ bit 31
187// 'uSrc' input: -bbbbbgggggrrrrr
188// ^ bit 16
189// RETURNS:
190// u16 output: 0bbbbbgggggrrrrr
191// Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care
192////////////////////////////////////////////////////////////////////////////////
193GPU_INLINE u16 gpuLightingTXTGouraud(u16 uSrc, u32 gCol)
194{
195 return (gpu_unai.LightLUT[((uSrc&0x7C00)>>5) | ((gCol>> 5)&0x1F)]<<10) |
196 (gpu_unai.LightLUT[ (uSrc&0x03E0) | ((gCol>>16)&0x1F)]<< 5) |
197 (gpu_unai.LightLUT[((uSrc&0x001F)<<5) | (gCol>>27) ] );
198}
199
200
201////////////////////////////////////////////////////////////////////////////////
202// Apply high-precision 8-bit lighting to bgr555 texture color,
203// returning a padded u32 5.4:5.4:5.4 bgr fixed-pt triplet
204// suitable for use with HQ 24-bit lighting/quantization.
205//
206// INPUT:
207// 'r8','g8','b8' are unsigned 8-bit color component values, value of
208// 127 is midpoint that doesn't modify that component of texture
209//
210// uSrc input: -bbbbbgggggrrrrr
211// ^ bit 16
212// RETURNS:
213// u32 output: 000bbbbbXXXX0gggggXXXX0rrrrrXXXX
214// ^ bit 31
215// Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care
216////////////////////////////////////////////////////////////////////////////////
217GPU_INLINE u32 gpuLightingTXT24(u16 uSrc, u8 r8, u8 g8, u8 b8)
218{
219 u16 r1 = uSrc&0x001F;
220 u16 g1 = uSrc&0x03E0;
221 u16 b1 = uSrc&0x7C00;
222
223 u16 r2 = r8;
224 u16 g2 = g8;
225 u16 b2 = b8;
226
227 u32 r3 = r1 * r2; if (r3 & 0xFFFFF000) r3 = ~0xFFFFF000;
228 u32 g3 = g1 * g2; if (g3 & 0xFFFE0000) g3 = ~0xFFFE0000;
229 u32 b3 = b1 * b2; if (b3 & 0xFFC00000) b3 = ~0xFFC00000;
230
231 return ((r3>> 3) ) |
232 ((g3>> 8)<<10) |
233 ((b3>>13)<<20);
234}
235
236
237////////////////////////////////////////////////////////////////////////////////
238// Apply high-precision 8-bit lighting to bgr555 texture color in 'uSrc',
239// returning a padded u32 5.4:5.4:5.4 bgr fixed-pt triplet
240// suitable for use with HQ 24-bit lighting/quantization.
241//
242// INPUT:
243// 'uSrc' input: -bbbbbgggggrrrrr
244// ^ bit 16
245// 'gCol' input: rrrrrrrrXXXggggggggXXXbbbbbbbbXX
246// ^ bit 31
247// RETURNS:
248// u32 output: 000bbbbbXXXX0gggggXXXX0rrrrrXXXX
249// ^ bit 31
250// Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care
251////////////////////////////////////////////////////////////////////////////////
252GPU_INLINE u32 gpuLightingTXT24Gouraud(u16 uSrc, u32 gCol)
253{
254 u16 r1 = uSrc&0x001F;
255 u16 g1 = uSrc&0x03E0;
256 u16 b1 = uSrc&0x7C00;
257
258 u16 r2 = (gCol>>24) & 0xFF;
259 u16 g2 = (gCol>>13) & 0xFF;
260 u16 b2 = (gCol>> 2) & 0xFF;
261
262 u32 r3 = r1 * r2; if (r3 & 0xFFFFF000) r3 = ~0xFFFFF000;
263 u32 g3 = g1 * g2; if (g3 & 0xFFFE0000) g3 = ~0xFFFE0000;
264 u32 b3 = b1 * b2; if (b3 & 0xFFC00000) b3 = ~0xFFC00000;
265
266 return ((r3>> 3) ) |
267 ((g3>> 8)<<10) |
268 ((b3>>13)<<20);
86aad47b 269}
270
271#endif //_OP_LIGHT_H_