Commit | Line | Data |
---|---|---|
86aad47b | 1 | /*************************************************************************** |
2 | * Copyright (C) 2010 PCSX4ALL Team * | |
3 | * Copyright (C) 2010 Unai * | |
030d1121 | 4 | * Copyright (C) 2016 Senquack (dansilsby <AT> gmail <DOT> com) * |
86aad47b | 5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the * | |
18 | * Free Software Foundation, Inc., * | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. * | |
20 | ***************************************************************************/ | |
21 | ||
908e426c | 22 | #ifndef __GPU_UNAI_GPU_INNER_H__ |
23 | #define __GPU_UNAI_GPU_INNER_H__ | |
24 | ||
86aad47b | 25 | /////////////////////////////////////////////////////////////////////////////// |
030d1121 | 26 | // Inner loop driver instantiation file |
86aad47b | 27 | |
28 | /////////////////////////////////////////////////////////////////////////////// | |
030d1121 | 29 | // Option Masks (CF template paramter) |
30 | #define CF_LIGHT ((CF>> 0)&1) // Lighting | |
31 | #define CF_BLEND ((CF>> 1)&1) // Blending | |
32 | #define CF_MASKCHECK ((CF>> 2)&1) // Mask bit check | |
33 | #define CF_BLENDMODE ((CF>> 3)&3) // Blend mode 0..3 | |
34 | #define CF_TEXTMODE ((CF>> 5)&3) // Texture mode 1..3 (0: texturing disabled) | |
35 | #define CF_GOURAUD ((CF>> 7)&1) // Gouraud shading | |
36 | #define CF_MASKSET ((CF>> 8)&1) // Mask bit set | |
37 | #define CF_DITHER ((CF>> 9)&1) // Dithering | |
38 | #define CF_BLITMASK ((CF>>10)&1) // blit_mask check (skip rendering pixels | |
39 | // that wouldn't end up displayed on | |
40 | // low-res screen using simple downscaler) | |
86aad47b | 41 | |
030d1121 | 42 | //#ifdef __arm__ |
43 | //#ifndef ENABLE_GPU_ARMV7 | |
44 | /* ARMv5 */ | |
45 | //#include "gpu_inner_blend_arm5.h" | |
46 | //#else | |
47 | /* ARMv7 optimized */ | |
48 | //#include "gpu_inner_blend_arm7.h" | |
49 | //#endif | |
50 | //#else | |
51 | //#include "gpu_inner_blend.h" | |
52 | //#endif | |
86aad47b | 53 | |
54 | #include "gpu_inner_blend.h" | |
030d1121 | 55 | #include "gpu_inner_quantization.h" |
86aad47b | 56 | #include "gpu_inner_light.h" |
57 | ||
788f5e89 | 58 | #ifdef __arm__ |
92eab56a | 59 | #include "gpu_inner_blend_arm.h" |
788f5e89 | 60 | #include "gpu_inner_light_arm.h" |
92eab56a | 61 | #define gpuBlending gpuBlendingARM |
788f5e89 | 62 | #define gpuLightingTXT gpuLightingTXTARM |
788f5e89 | 63 | #else |
92eab56a | 64 | #define gpuBlending gpuBlendingGeneric |
788f5e89 | 65 | #define gpuLightingTXT gpuLightingTXTGeneric |
788f5e89 JW |
66 | #endif |
67 | ||
0c399195 PC |
68 | // Non-dithering lighting and blending functions preserve uSrc |
69 | // MSB. This saves a few operations and useless load/stores. | |
70 | #define MSB_PRESERVED (!CF_DITHER) | |
788f5e89 | 71 | |
030d1121 | 72 | // If defined, Gouraud colors are fixed-point 5.11, otherwise they are 8.16 |
73 | // This is only for debugging/verification of low-precision colors in C. | |
74 | // Low-precision Gouraud is intended for use by SIMD-optimized inner drivers | |
75 | // which get/use Gouraud colors in SIMD registers. | |
76 | //#define GPU_GOURAUD_LOW_PRECISION | |
77 | ||
78 | // How many bits of fixed-point precision GouraudColor uses | |
79 | #ifdef GPU_GOURAUD_LOW_PRECISION | |
80 | #define GPU_GOURAUD_FIXED_BITS 11 | |
81 | #else | |
82 | #define GPU_GOURAUD_FIXED_BITS 16 | |
83 | #endif | |
84 | ||
85 | // Used to pass Gouraud colors to gpuPixelSpanFn() (lines) | |
86 | struct GouraudColor { | |
87 | #ifdef GPU_GOURAUD_LOW_PRECISION | |
88 | u16 r, g, b; | |
89 | s16 r_incr, g_incr, b_incr; | |
90 | #else | |
91 | u32 r, g, b; | |
92 | s32 r_incr, g_incr, b_incr; | |
93 | #endif | |
94 | }; | |
95 | ||
96 | static inline u16 gpuGouraudColor15bpp(u32 r, u32 g, u32 b) | |
97 | { | |
98 | r >>= GPU_GOURAUD_FIXED_BITS; | |
99 | g >>= GPU_GOURAUD_FIXED_BITS; | |
100 | b >>= GPU_GOURAUD_FIXED_BITS; | |
101 | ||
102 | #ifndef GPU_GOURAUD_LOW_PRECISION | |
103 | // High-precision Gouraud colors are 8-bit + fractional | |
104 | r >>= 3; g >>= 3; b >>= 3; | |
105 | #endif | |
106 | ||
107 | return r | (g << 5) | (b << 10); | |
108 | } | |
109 | ||
86aad47b | 110 | /////////////////////////////////////////////////////////////////////////////// |
030d1121 | 111 | // GPU Pixel span operations generator gpuPixelSpanFn<> |
112 | // Oct 2016: Created/adapted from old gpuPixelFn by senquack: | |
113 | // Original gpuPixelFn was used to draw lines one pixel at a time. I wrote | |
114 | // new line algorithms that draw lines using horizontal/vertical/diagonal | |
115 | // spans of pixels, necessitating new pixel-drawing function that could | |
116 | // not only render spans of pixels, but gouraud-shade them as well. | |
117 | // This speeds up line rendering and would allow tile-rendering (untextured | |
118 | // rectangles) to use the same set of functions. Since tiles are always | |
119 | // monochrome, they simply wouldn't use the extra set of 32 gouraud-shaded | |
120 | // gpuPixelSpanFn functions (TODO?). | |
030d1121 | 121 | template<int CF> |
4949d4ff | 122 | static le16_t* gpuPixelSpanFn(le16_t* pDst, uintptr_t data, ptrdiff_t incr, size_t len) |
86aad47b | 123 | { |
030d1121 | 124 | // Blend func can save an operation if it knows uSrc MSB is |
125 | // unset. For untextured prims, this is always true. | |
126 | const bool skip_uSrc_mask = true; | |
127 | ||
128 | u16 col; | |
129 | struct GouraudColor * gcPtr; | |
130 | u32 r, g, b; | |
131 | s32 r_incr, g_incr, b_incr; | |
132 | ||
4949d4ff PC |
133 | // Caller counts in bytes, we count in pixels |
134 | incr /= 2; | |
135 | ||
030d1121 | 136 | if (CF_GOURAUD) { |
137 | gcPtr = (GouraudColor*)data; | |
138 | r = gcPtr->r; r_incr = gcPtr->r_incr; | |
139 | g = gcPtr->g; g_incr = gcPtr->g_incr; | |
140 | b = gcPtr->b; b_incr = gcPtr->b_incr; | |
141 | } else { | |
142 | col = (u16)data; | |
86aad47b | 143 | } |
030d1121 | 144 | |
145 | do { | |
146 | if (!CF_GOURAUD) | |
147 | { // NO GOURAUD | |
148 | if (!CF_MASKCHECK && !CF_BLEND) { | |
4949d4ff PC |
149 | if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); } |
150 | else { *pDst = u16_to_le16(col); } | |
030d1121 | 151 | } else if (CF_MASKCHECK && !CF_BLEND) { |
4949d4ff PC |
152 | if (!(le16_raw(*pDst) & HTOLE16(0x8000))) { |
153 | if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); } | |
154 | else { *pDst = u16_to_le16(col); } | |
030d1121 | 155 | } |
156 | } else { | |
4949d4ff | 157 | uint_fast16_t uDst = le16_to_u16(*pDst); |
030d1121 | 158 | if (CF_MASKCHECK) { if (uDst & 0x8000) goto endpixel; } |
159 | ||
92eab56a | 160 | uint_fast16_t uSrc = col; |
030d1121 | 161 | |
162 | if (CF_BLEND) | |
163 | uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst); | |
164 | ||
4949d4ff PC |
165 | if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); } |
166 | else { *pDst = u16_to_le16(uSrc); } | |
030d1121 | 167 | } |
168 | ||
169 | } else | |
170 | { // GOURAUD | |
171 | ||
172 | if (!CF_MASKCHECK && !CF_BLEND) { | |
173 | col = gpuGouraudColor15bpp(r, g, b); | |
4949d4ff PC |
174 | if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); } |
175 | else { *pDst = u16_to_le16(col); } | |
030d1121 | 176 | } else if (CF_MASKCHECK && !CF_BLEND) { |
177 | col = gpuGouraudColor15bpp(r, g, b); | |
4949d4ff PC |
178 | if (!(le16_raw(*pDst) & HTOLE16(0x8000))) { |
179 | if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); } | |
180 | else { *pDst = u16_to_le16(col); } | |
030d1121 | 181 | } |
182 | } else { | |
4949d4ff | 183 | uint_fast16_t uDst = le16_to_u16(*pDst); |
030d1121 | 184 | if (CF_MASKCHECK) { if (uDst & 0x8000) goto endpixel; } |
185 | col = gpuGouraudColor15bpp(r, g, b); | |
186 | ||
92eab56a | 187 | uint_fast16_t uSrc = col; |
030d1121 | 188 | |
189 | // Blend func can save an operation if it knows uSrc MSB is | |
190 | // unset. For untextured prims, this is always true. | |
191 | const bool skip_uSrc_mask = true; | |
192 | ||
193 | if (CF_BLEND) | |
194 | uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst); | |
195 | ||
4949d4ff PC |
196 | if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); } |
197 | else { *pDst = u16_to_le16(uSrc); } | |
030d1121 | 198 | } |
86aad47b | 199 | } |
030d1121 | 200 | |
201 | endpixel: | |
202 | if (CF_GOURAUD) { | |
203 | r += r_incr; | |
204 | g += g_incr; | |
205 | b += b_incr; | |
206 | } | |
207 | pDst += incr; | |
208 | } while (len-- > 1); | |
209 | ||
210 | // Note from senquack: Normally, I'd prefer to write a 'do {} while (--len)' | |
211 | // loop, or even a for() loop, however, on MIPS platforms anything but the | |
212 | // 'do {} while (len-- > 1)' tends to generate very unoptimal asm, with | |
213 | // many unneeded MULs/ADDs/branches at the ends of these functions. | |
214 | // If you change the loop structure above, be sure to compare the quality | |
215 | // of the generated code!! | |
216 | ||
217 | if (CF_GOURAUD) { | |
218 | gcPtr->r = r; | |
219 | gcPtr->g = g; | |
220 | gcPtr->b = b; | |
86aad47b | 221 | } |
030d1121 | 222 | return pDst; |
223 | } | |
224 | ||
4949d4ff | 225 | static le16_t* PixelSpanNULL(le16_t* pDst, uintptr_t data, ptrdiff_t incr, size_t len) |
030d1121 | 226 | { |
227 | #ifdef ENABLE_GPU_LOG_SUPPORT | |
228 | fprintf(stdout,"PixelSpanNULL()\n"); | |
229 | #endif | |
230 | return pDst; | |
86aad47b | 231 | } |
86aad47b | 232 | |
233 | /////////////////////////////////////////////////////////////////////////////// | |
030d1121 | 234 | // PixelSpan (lines) innerloops driver |
4949d4ff | 235 | typedef le16_t* (*PSD)(le16_t* dst, uintptr_t data, ptrdiff_t incr, size_t len); |
030d1121 | 236 | |
237 | const PSD gpuPixelSpanDrivers[64] = | |
86aad47b | 238 | { |
030d1121 | 239 | // Array index | 'CF' template field | Field value |
240 | // ------------+---------------------+---------------- | |
241 | // Bit 0 | CF_BLEND | off (0), on (1) | |
242 | // Bit 1 | CF_MASKCHECK | off (0), on (1) | |
243 | // Bit 3:2 | CF_BLENDMODE | 0..3 | |
244 | // Bit 4 | CF_MASKSET | off (0), on (1) | |
245 | // Bit 5 | CF_GOURAUD | off (0), on (1) | |
246 | // | |
247 | // NULL entries are ones for which blending is disabled and blend-mode | |
248 | // field is non-zero, which is obviously invalid. | |
249 | ||
250 | // Flat-shaded | |
251 | gpuPixelSpanFn<0x00<<1>, gpuPixelSpanFn<0x01<<1>, gpuPixelSpanFn<0x02<<1>, gpuPixelSpanFn<0x03<<1>, | |
252 | PixelSpanNULL, gpuPixelSpanFn<0x05<<1>, PixelSpanNULL, gpuPixelSpanFn<0x07<<1>, | |
253 | PixelSpanNULL, gpuPixelSpanFn<0x09<<1>, PixelSpanNULL, gpuPixelSpanFn<0x0B<<1>, | |
254 | PixelSpanNULL, gpuPixelSpanFn<0x0D<<1>, PixelSpanNULL, gpuPixelSpanFn<0x0F<<1>, | |
255 | ||
256 | // Flat-shaded + PixelMSB (CF_MASKSET) | |
257 | gpuPixelSpanFn<(0x00<<1)|0x100>, gpuPixelSpanFn<(0x01<<1)|0x100>, gpuPixelSpanFn<(0x02<<1)|0x100>, gpuPixelSpanFn<(0x03<<1)|0x100>, | |
258 | PixelSpanNULL, gpuPixelSpanFn<(0x05<<1)|0x100>, PixelSpanNULL, gpuPixelSpanFn<(0x07<<1)|0x100>, | |
259 | PixelSpanNULL, gpuPixelSpanFn<(0x09<<1)|0x100>, PixelSpanNULL, gpuPixelSpanFn<(0x0B<<1)|0x100>, | |
260 | PixelSpanNULL, gpuPixelSpanFn<(0x0D<<1)|0x100>, PixelSpanNULL, gpuPixelSpanFn<(0x0F<<1)|0x100>, | |
261 | ||
262 | // Gouraud-shaded (CF_GOURAUD) | |
263 | gpuPixelSpanFn<(0x00<<1)|0x80>, gpuPixelSpanFn<(0x01<<1)|0x80>, gpuPixelSpanFn<(0x02<<1)|0x80>, gpuPixelSpanFn<(0x03<<1)|0x80>, | |
264 | PixelSpanNULL, gpuPixelSpanFn<(0x05<<1)|0x80>, PixelSpanNULL, gpuPixelSpanFn<(0x07<<1)|0x80>, | |
265 | PixelSpanNULL, gpuPixelSpanFn<(0x09<<1)|0x80>, PixelSpanNULL, gpuPixelSpanFn<(0x0B<<1)|0x80>, | |
266 | PixelSpanNULL, gpuPixelSpanFn<(0x0D<<1)|0x80>, PixelSpanNULL, gpuPixelSpanFn<(0x0F<<1)|0x80>, | |
267 | ||
268 | // Gouraud-shaded (CF_GOURAUD) + PixelMSB (CF_MASKSET) | |
269 | gpuPixelSpanFn<(0x00<<1)|0x180>, gpuPixelSpanFn<(0x01<<1)|0x180>, gpuPixelSpanFn<(0x02<<1)|0x180>, gpuPixelSpanFn<(0x03<<1)|0x180>, | |
270 | PixelSpanNULL, gpuPixelSpanFn<(0x05<<1)|0x180>, PixelSpanNULL, gpuPixelSpanFn<(0x07<<1)|0x180>, | |
271 | PixelSpanNULL, gpuPixelSpanFn<(0x09<<1)|0x180>, PixelSpanNULL, gpuPixelSpanFn<(0x0B<<1)|0x180>, | |
272 | PixelSpanNULL, gpuPixelSpanFn<(0x0D<<1)|0x180>, PixelSpanNULL, gpuPixelSpanFn<(0x0F<<1)|0x180> | |
86aad47b | 273 | }; |
274 | ||
275 | /////////////////////////////////////////////////////////////////////////////// | |
276 | // GPU Tiles innerloops generator | |
277 | ||
030d1121 | 278 | template<int CF> |
4949d4ff | 279 | static void gpuTileSpanFn(le16_t *pDst, u32 count, u16 data) |
86aad47b | 280 | { |
4949d4ff PC |
281 | le16_t ldata; |
282 | ||
030d1121 | 283 | if (!CF_MASKCHECK && !CF_BLEND) { |
4949d4ff PC |
284 | if (CF_MASKSET) |
285 | ldata = u16_to_le16(data | 0x8000); | |
286 | else | |
287 | ldata = u16_to_le16(data); | |
288 | do { *pDst++ = ldata; } while (--count); | |
030d1121 | 289 | } else if (CF_MASKCHECK && !CF_BLEND) { |
4949d4ff PC |
290 | if (CF_MASKSET) |
291 | ldata = u16_to_le16(data | 0x8000); | |
292 | else | |
293 | ldata = u16_to_le16(data); | |
294 | do { | |
295 | if (!(le16_raw(*pDst) & HTOLE16(0x8000))) | |
296 | *pDst = ldata; | |
297 | pDst++; | |
298 | } while (--count); | |
030d1121 | 299 | } else |
86aad47b | 300 | { |
030d1121 | 301 | // Blend func can save an operation if it knows uSrc MSB is |
302 | // unset. For untextured prims, this is always true. | |
303 | const bool skip_uSrc_mask = true; | |
304 | ||
92eab56a | 305 | uint_fast16_t uSrc, uDst; |
86aad47b | 306 | do |
307 | { | |
4949d4ff PC |
308 | if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); } |
309 | if (CF_MASKCHECK) if (uDst&0x8000) { goto endtile; } | |
030d1121 | 310 | |
86aad47b | 311 | uSrc = data; |
312 | ||
030d1121 | 313 | if (CF_BLEND) |
314 | uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst); | |
86aad47b | 315 | |
4949d4ff PC |
316 | if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); } |
317 | else { *pDst = u16_to_le16(uSrc); } | |
030d1121 | 318 | |
319 | //senquack - Did not apply "Silent Hill" mask-bit fix to here. | |
320 | // It is hard to tell from scarce documentation available and | |
321 | // lack of comments in code, but I believe the tile-span | |
322 | // functions here should not bother to preserve any source MSB, | |
323 | // as they are not drawing from a texture. | |
324 | endtile: | |
325 | pDst++; | |
86aad47b | 326 | } |
327 | while (--count); | |
328 | } | |
329 | } | |
330 | ||
4949d4ff | 331 | static void TileNULL(le16_t *pDst, u32 count, u16 data) |
030d1121 | 332 | { |
333 | #ifdef ENABLE_GPU_LOG_SUPPORT | |
334 | fprintf(stdout,"TileNULL()\n"); | |
335 | #endif | |
336 | } | |
337 | ||
86aad47b | 338 | /////////////////////////////////////////////////////////////////////////////// |
339 | // Tiles innerloops driver | |
4949d4ff | 340 | typedef void (*PT)(le16_t *pDst, u32 count, u16 data); |
86aad47b | 341 | |
030d1121 | 342 | // Template instantiation helper macros |
343 | #define TI(cf) gpuTileSpanFn<(cf)> | |
344 | #define TN TileNULL | |
345 | #define TIBLOCK(ub) \ | |
346 | TI((ub)|0x00), TI((ub)|0x02), TI((ub)|0x04), TI((ub)|0x06), \ | |
347 | TN, TI((ub)|0x0a), TN, TI((ub)|0x0e), \ | |
348 | TN, TI((ub)|0x12), TN, TI((ub)|0x16), \ | |
349 | TN, TI((ub)|0x1a), TN, TI((ub)|0x1e) | |
350 | ||
351 | const PT gpuTileSpanDrivers[32] = { | |
352 | TIBLOCK(0<<8), TIBLOCK(1<<8) | |
86aad47b | 353 | }; |
354 | ||
030d1121 | 355 | #undef TI |
356 | #undef TN | |
357 | #undef TIBLOCK | |
358 | ||
359 | ||
86aad47b | 360 | /////////////////////////////////////////////////////////////////////////////// |
361 | // GPU Sprites innerloops generator | |
362 | ||
030d1121 | 363 | template<int CF> |
4949d4ff | 364 | static void gpuSpriteSpanFn(le16_t *pDst, u32 count, u8* pTxt, u32 u0) |
86aad47b | 365 | { |
030d1121 | 366 | // Blend func can save an operation if it knows uSrc MSB is unset. |
367 | // Untextured prims can always skip (source color always comes with MSB=0). | |
92eab56a | 368 | // For textured prims, the generic lighting funcs always return it unset. (bonus!) |
335c3831 | 369 | const bool skip_uSrc_mask = MSB_PRESERVED ? (!CF_TEXTMODE) : (!CF_TEXTMODE) || CF_LIGHT; |
030d1121 | 370 | |
92eab56a | 371 | uint_fast16_t uSrc, uDst, srcMSB; |
335c3831 | 372 | bool should_blend; |
030d1121 | 373 | u32 u0_mask = gpu_unai.TextureWindow[2]; |
374 | ||
375 | u8 r5, g5, b5; | |
376 | if (CF_LIGHT) { | |
377 | r5 = gpu_unai.r5; | |
378 | g5 = gpu_unai.g5; | |
379 | b5 = gpu_unai.b5; | |
380 | } | |
381 | ||
382 | if (CF_TEXTMODE==3) { | |
383 | // Texture is accessed byte-wise, so adjust mask if 16bpp | |
384 | u0_mask <<= 1; | |
385 | } | |
386 | ||
4949d4ff | 387 | const le16_t *CBA_; if (CF_TEXTMODE!=3) CBA_ = gpu_unai.CBA; |
86aad47b | 388 | |
389 | do | |
390 | { | |
4949d4ff | 391 | if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); } |
030d1121 | 392 | if (CF_MASKCHECK) if (uDst&0x8000) { goto endsprite; } |
86aad47b | 393 | |
030d1121 | 394 | if (CF_TEXTMODE==1) { // 4bpp (CLUT) |
395 | u8 rgb = pTxt[(u0 & u0_mask)>>1]; | |
4949d4ff | 396 | uSrc = le16_to_u16(CBA_[(rgb>>((u0&1)<<2))&0xf]); |
86aad47b | 397 | } |
030d1121 | 398 | if (CF_TEXTMODE==2) { // 8bpp (CLUT) |
4949d4ff | 399 | uSrc = le16_to_u16(CBA_[pTxt[u0 & u0_mask]]); |
030d1121 | 400 | } |
401 | if (CF_TEXTMODE==3) { // 16bpp | |
4949d4ff | 402 | uSrc = le16_to_u16(*(le16_t*)(&pTxt[u0 & u0_mask])); |
86aad47b | 403 | } |
404 | ||
030d1121 | 405 | if (!uSrc) goto endsprite; |
406 | ||
407 | //senquack - save source MSB, as blending or lighting macros will not | |
408 | // (Silent Hill gray rectangles mask bit bug) | |
409 | if (CF_BLEND || CF_LIGHT) srcMSB = uSrc & 0x8000; | |
86aad47b | 410 | |
030d1121 | 411 | if (CF_LIGHT) |
412 | uSrc = gpuLightingTXT(uSrc, r5, g5, b5); | |
413 | ||
335c3831 | 414 | should_blend = MSB_PRESERVED ? uSrc & 0x8000 : srcMSB; |
92eab56a JW |
415 | |
416 | if (CF_BLEND && should_blend) | |
030d1121 | 417 | uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst); |
418 | ||
4949d4ff PC |
419 | if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); } |
420 | else if (!MSB_PRESERVED && (CF_BLEND || CF_LIGHT)) { *pDst = u16_to_le16(uSrc | srcMSB); } | |
421 | else { *pDst = u16_to_le16(uSrc); } | |
030d1121 | 422 | |
423 | endsprite: | |
424 | u0 += (CF_TEXTMODE==3) ? 2 : 1; | |
425 | pDst++; | |
86aad47b | 426 | } |
427 | while (--count); | |
428 | } | |
030d1121 | 429 | |
4949d4ff | 430 | static void SpriteNULL(le16_t *pDst, u32 count, u8* pTxt, u32 u0) |
030d1121 | 431 | { |
432 | #ifdef ENABLE_GPU_LOG_SUPPORT | |
433 | fprintf(stdout,"SpriteNULL()\n"); | |
434 | #endif | |
435 | } | |
436 | ||
86aad47b | 437 | /////////////////////////////////////////////////////////////////////////////// |
438 | ||
439 | /////////////////////////////////////////////////////////////////////////////// | |
440 | // Sprite innerloops driver | |
4949d4ff | 441 | typedef void (*PS)(le16_t *pDst, u32 count, u8* pTxt, u32 u0); |
030d1121 | 442 | |
443 | // Template instantiation helper macros | |
444 | #define TI(cf) gpuSpriteSpanFn<(cf)> | |
445 | #define TN SpriteNULL | |
446 | #define TIBLOCK(ub) \ | |
447 | TN, TN, TN, TN, TN, TN, TN, TN, \ | |
448 | TN, TN, TN, TN, TN, TN, TN, TN, \ | |
449 | TN, TN, TN, TN, TN, TN, TN, TN, \ | |
450 | TN, TN, TN, TN, TN, TN, TN, TN, \ | |
451 | TI((ub)|0x20), TI((ub)|0x21), TI((ub)|0x22), TI((ub)|0x23), TI((ub)|0x24), TI((ub)|0x25), TI((ub)|0x26), TI((ub)|0x27), \ | |
452 | TN, TN, TI((ub)|0x2a), TI((ub)|0x2b), TN, TN, TI((ub)|0x2e), TI((ub)|0x2f), \ | |
453 | TN, TN, TI((ub)|0x32), TI((ub)|0x33), TN, TN, TI((ub)|0x36), TI((ub)|0x37), \ | |
454 | TN, TN, TI((ub)|0x3a), TI((ub)|0x3b), TN, TN, TI((ub)|0x3e), TI((ub)|0x3f), \ | |
455 | TI((ub)|0x40), TI((ub)|0x41), TI((ub)|0x42), TI((ub)|0x43), TI((ub)|0x44), TI((ub)|0x45), TI((ub)|0x46), TI((ub)|0x47), \ | |
456 | TN, TN, TI((ub)|0x4a), TI((ub)|0x4b), TN, TN, TI((ub)|0x4e), TI((ub)|0x4f), \ | |
457 | TN, TN, TI((ub)|0x52), TI((ub)|0x53), TN, TN, TI((ub)|0x56), TI((ub)|0x57), \ | |
458 | TN, TN, TI((ub)|0x5a), TI((ub)|0x5b), TN, TN, TI((ub)|0x5e), TI((ub)|0x5f), \ | |
459 | TI((ub)|0x60), TI((ub)|0x61), TI((ub)|0x62), TI((ub)|0x63), TI((ub)|0x64), TI((ub)|0x65), TI((ub)|0x66), TI((ub)|0x67), \ | |
460 | TN, TN, TI((ub)|0x6a), TI((ub)|0x6b), TN, TN, TI((ub)|0x6e), TI((ub)|0x6f), \ | |
461 | TN, TN, TI((ub)|0x72), TI((ub)|0x73), TN, TN, TI((ub)|0x76), TI((ub)|0x77), \ | |
462 | TN, TN, TI((ub)|0x7a), TI((ub)|0x7b), TN, TN, TI((ub)|0x7e), TI((ub)|0x7f) | |
463 | ||
464 | const PS gpuSpriteSpanDrivers[256] = { | |
465 | TIBLOCK(0<<8), TIBLOCK(1<<8) | |
86aad47b | 466 | }; |
467 | ||
030d1121 | 468 | #undef TI |
469 | #undef TN | |
470 | #undef TIBLOCK | |
471 | ||
86aad47b | 472 | /////////////////////////////////////////////////////////////////////////////// |
473 | // GPU Polygon innerloops generator | |
030d1121 | 474 | |
475 | //senquack - Newer version with following changes: | |
476 | // * Adapted to work with new poly routings in gpu_raster_polygon.h | |
477 | // adapted from DrHell GPU. They are less glitchy and use 22.10 | |
478 | // fixed-point instead of original UNAI's 16.16. | |
479 | // * Texture coordinates are no longer packed together into one | |
480 | // unsigned int. This seems to lose too much accuracy (they each | |
481 | // end up being only 8.7 fixed-point that way) and pixel-droupouts | |
482 | // were noticeable both with original code and current DrHell | |
483 | // adaptations. An example would be the sky in NFS3. Now, they are | |
484 | // stored in separate ints, using separate masks. | |
485 | // * Function is no longer INLINE, as it was always called | |
486 | // through a function pointer. | |
487 | // * Function now ensures the mask bit of source texture is preserved | |
488 | // across calls to blending functions (Silent Hill rectangles fix) | |
489 | // * November 2016: Large refactoring of blending/lighting when | |
490 | // JohnnyF added dithering. See gpu_inner_quantization.h and | |
491 | // relevant blend/light headers. | |
492 | // (see README_senquack.txt) | |
493 | template<int CF> | |
4949d4ff | 494 | static void gpuPolySpanFn(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count) |
86aad47b | 495 | { |
030d1121 | 496 | // Blend func can save an operation if it knows uSrc MSB is unset. |
497 | // Untextured prims can always skip this (src color MSB is always 0). | |
92eab56a | 498 | // For textured prims, the generic lighting funcs always return it unset. (bonus!) |
335c3831 JW |
499 | const bool skip_uSrc_mask = MSB_PRESERVED ? (!CF_TEXTMODE) : (!CF_TEXTMODE) || CF_LIGHT; |
500 | bool should_blend; | |
030d1121 | 501 | |
502 | u32 bMsk; if (CF_BLITMASK) bMsk = gpu_unai.blit_mask; | |
503 | ||
504 | if (!CF_TEXTMODE) | |
505 | { | |
506 | if (!CF_GOURAUD) | |
86aad47b | 507 | { |
030d1121 | 508 | // UNTEXTURED, NO GOURAUD |
509 | const u16 pix15 = gpu_unai.PixelData; | |
510 | do { | |
92eab56a | 511 | uint_fast16_t uSrc, uDst; |
030d1121 | 512 | |
513 | // NOTE: Don't enable CF_BLITMASK pixel skipping (speed hack) | |
514 | // on untextured polys. It seems to do more harm than good: see | |
515 | // gravestone text at end of Medieval intro sequence. -senquack | |
516 | //if (CF_BLITMASK) { if ((bMsk>>((((uintptr_t)pDst)>>1)&7))&1) { goto endpolynotextnogou; } } | |
517 | ||
4949d4ff | 518 | if (CF_BLEND || CF_MASKCHECK) uDst = le16_to_u16(*pDst); |
030d1121 | 519 | if (CF_MASKCHECK) { if (uDst&0x8000) { goto endpolynotextnogou; } } |
520 | ||
521 | uSrc = pix15; | |
522 | ||
523 | if (CF_BLEND) | |
524 | uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst); | |
525 | ||
4949d4ff PC |
526 | if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); } |
527 | else { *pDst = u16_to_le16(uSrc); } | |
030d1121 | 528 | |
529 | endpolynotextnogou: | |
530 | pDst++; | |
531 | } while(--count); | |
86aad47b | 532 | } |
533 | else | |
534 | { | |
030d1121 | 535 | // UNTEXTURED, GOURAUD |
ebcd59b7 PC |
536 | gcol_t l_gCol = gpu_unai.gCol; |
537 | gcol_t l_gInc = gpu_unai.gInc; | |
030d1121 | 538 | |
539 | do { | |
92eab56a | 540 | uint_fast16_t uDst, uSrc; |
030d1121 | 541 | |
542 | // See note in above loop regarding CF_BLITMASK | |
543 | //if (CF_BLITMASK) { if ((bMsk>>((((uintptr_t)pDst)>>1)&7))&1) goto endpolynotextgou; } | |
544 | ||
4949d4ff | 545 | if (CF_BLEND || CF_MASKCHECK) uDst = le16_to_u16(*pDst); |
030d1121 | 546 | if (CF_MASKCHECK) { if (uDst&0x8000) goto endpolynotextgou; } |
547 | ||
548 | if (CF_DITHER) { | |
549 | // GOURAUD, DITHER | |
550 | ||
551 | u32 uSrc24 = gpuLightingRGB24(l_gCol); | |
552 | if (CF_BLEND) | |
553 | uSrc24 = gpuBlending24<CF_BLENDMODE>(uSrc24, uDst); | |
554 | uSrc = gpuColorQuantization24<CF_DITHER>(uSrc24, pDst); | |
555 | } else { | |
556 | // GOURAUD, NO DITHER | |
557 | ||
558 | uSrc = gpuLightingRGB(l_gCol); | |
559 | ||
560 | if (CF_BLEND) | |
561 | uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst); | |
86aad47b | 562 | } |
030d1121 | 563 | |
4949d4ff PC |
564 | if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); } |
565 | else { *pDst = u16_to_le16(uSrc); } | |
030d1121 | 566 | |
567 | endpolynotextgou: | |
568 | pDst++; | |
ebcd59b7 | 569 | l_gCol.raw += l_gInc.raw; |
86aad47b | 570 | } |
571 | while (--count); | |
572 | } | |
573 | } | |
574 | else | |
575 | { | |
030d1121 | 576 | // TEXTURED |
577 | ||
92eab56a | 578 | uint_fast16_t uDst, uSrc, srcMSB; |
030d1121 | 579 | |
580 | //senquack - note: original UNAI code had gpu_unai.{u4/v4} packed into | |
581 | // one 32-bit unsigned int, but this proved to lose too much accuracy | |
582 | // (pixel drouputs noticeable in NFS3 sky), so now are separate vars. | |
583 | u32 l_u_msk = gpu_unai.u_msk; u32 l_v_msk = gpu_unai.v_msk; | |
584 | u32 l_u = gpu_unai.u & l_u_msk; u32 l_v = gpu_unai.v & l_v_msk; | |
585 | s32 l_u_inc = gpu_unai.u_inc; s32 l_v_inc = gpu_unai.v_inc; | |
586 | ||
4949d4ff PC |
587 | const le16_t* TBA_ = gpu_unai.TBA; |
588 | const le16_t* CBA_; if (CF_TEXTMODE!=3) CBA_ = gpu_unai.CBA; | |
030d1121 | 589 | |
590 | u8 r5, g5, b5; | |
591 | u8 r8, g8, b8; | |
592 | ||
ebcd59b7 | 593 | gcol_t l_gInc, l_gCol; |
030d1121 | 594 | |
595 | if (CF_LIGHT) { | |
596 | if (CF_GOURAUD) { | |
597 | l_gInc = gpu_unai.gInc; | |
598 | l_gCol = gpu_unai.gCol; | |
599 | } else { | |
600 | if (CF_DITHER) { | |
601 | r8 = gpu_unai.r8; | |
602 | g8 = gpu_unai.g8; | |
603 | b8 = gpu_unai.b8; | |
604 | } else { | |
605 | r5 = gpu_unai.r5; | |
606 | g5 = gpu_unai.g5; | |
607 | b5 = gpu_unai.b5; | |
608 | } | |
609 | } | |
610 | } | |
611 | ||
86aad47b | 612 | do |
613 | { | |
030d1121 | 614 | if (CF_BLITMASK) { if ((bMsk>>((((uintptr_t)pDst)>>1)&7))&1) goto endpolytext; } |
4949d4ff | 615 | if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); } |
030d1121 | 616 | if (CF_MASKCHECK) if (uDst&0x8000) { goto endpolytext; } |
617 | ||
618 | //senquack - adapted to work with new 22.10 fixed point routines: | |
619 | // (UNAI originally used 16.16) | |
620 | if (CF_TEXTMODE==1) { // 4bpp (CLUT) | |
621 | u32 tu=(l_u>>10); | |
622 | u32 tv=(l_v<<1)&(0xff<<11); | |
623 | u8 rgb=((u8*)TBA_)[tv+(tu>>1)]; | |
4949d4ff | 624 | uSrc=le16_to_u16(CBA_[(rgb>>((tu&1)<<2))&0xf]); |
030d1121 | 625 | if (!uSrc) goto endpolytext; |
626 | } | |
627 | if (CF_TEXTMODE==2) { // 8bpp (CLUT) | |
4949d4ff | 628 | uSrc = le16_to_u16(CBA_[(((u8*)TBA_)[(l_u>>10)+((l_v<<1)&(0xff<<11))])]); |
030d1121 | 629 | if (!uSrc) goto endpolytext; |
86aad47b | 630 | } |
030d1121 | 631 | if (CF_TEXTMODE==3) { // 16bpp |
4949d4ff | 632 | uSrc = le16_to_u16(TBA_[(l_u>>10)+((l_v)&(0xff<<10))]); |
030d1121 | 633 | if (!uSrc) goto endpolytext; |
634 | } | |
635 | ||
636 | // Save source MSB, as blending or lighting will not (Silent Hill) | |
637 | if (CF_BLEND || CF_LIGHT) srcMSB = uSrc & 0x8000; | |
638 | ||
639 | // When textured, only dither when LIGHT (texture blend) is enabled | |
640 | // LIGHT && BLEND => dither | |
641 | // LIGHT && !BLEND => dither | |
642 | //!LIGHT && BLEND => no dither | |
643 | //!LIGHT && !BLEND => no dither | |
644 | ||
645 | if (CF_DITHER && CF_LIGHT) { | |
646 | u32 uSrc24; | |
647 | if ( CF_GOURAUD) | |
648 | uSrc24 = gpuLightingTXT24Gouraud(uSrc, l_gCol); | |
649 | if (!CF_GOURAUD) | |
650 | uSrc24 = gpuLightingTXT24(uSrc, r8, g8, b8); | |
651 | ||
652 | if (CF_BLEND && srcMSB) | |
653 | uSrc24 = gpuBlending24<CF_BLENDMODE>(uSrc24, uDst); | |
654 | ||
655 | uSrc = gpuColorQuantization24<CF_DITHER>(uSrc24, pDst); | |
656 | } else | |
86aad47b | 657 | { |
030d1121 | 658 | if (CF_LIGHT) { |
659 | if ( CF_GOURAUD) | |
660 | uSrc = gpuLightingTXTGouraud(uSrc, l_gCol); | |
661 | if (!CF_GOURAUD) | |
662 | uSrc = gpuLightingTXT(uSrc, r5, g5, b5); | |
663 | } | |
664 | ||
335c3831 | 665 | should_blend = MSB_PRESERVED ? uSrc & 0x8000 : srcMSB; |
92eab56a | 666 | if (CF_BLEND && should_blend) |
030d1121 | 667 | uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst); |
86aad47b | 668 | } |
030d1121 | 669 | |
4949d4ff PC |
670 | if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); } |
671 | else if (!MSB_PRESERVED && (CF_BLEND || CF_LIGHT)) { *pDst = u16_to_le16(uSrc | srcMSB); } | |
672 | else { *pDst = u16_to_le16(uSrc); } | |
030d1121 | 673 | endpolytext: |
674 | pDst++; | |
675 | l_u = (l_u + l_u_inc) & l_u_msk; | |
676 | l_v = (l_v + l_v_inc) & l_v_msk; | |
ebcd59b7 PC |
677 | if (CF_LIGHT && CF_GOURAUD) |
678 | l_gCol.raw += l_gInc.raw; | |
86aad47b | 679 | } |
680 | while (--count); | |
681 | } | |
682 | } | |
faf8bb13 | 683 | |
4949d4ff | 684 | static void PolyNULL(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count) |
faf8bb13 | 685 | { |
030d1121 | 686 | #ifdef ENABLE_GPU_LOG_SUPPORT |
687 | fprintf(stdout,"PolyNULL()\n"); | |
688 | #endif | |
faf8bb13 | 689 | } |
690 | ||
86aad47b | 691 | /////////////////////////////////////////////////////////////////////////////// |
692 | // Polygon innerloops driver | |
4949d4ff | 693 | typedef void (*PP)(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count); |
030d1121 | 694 | |
695 | // Template instantiation helper macros | |
696 | #define TI(cf) gpuPolySpanFn<(cf)> | |
697 | #define TN PolyNULL | |
698 | #define TIBLOCK(ub) \ | |
699 | TI((ub)|0x00), TI((ub)|0x01), TI((ub)|0x02), TI((ub)|0x03), TI((ub)|0x04), TI((ub)|0x05), TI((ub)|0x06), TI((ub)|0x07), \ | |
700 | TN, TN, TI((ub)|0x0a), TI((ub)|0x0b), TN, TN, TI((ub)|0x0e), TI((ub)|0x0f), \ | |
701 | TN, TN, TI((ub)|0x12), TI((ub)|0x13), TN, TN, TI((ub)|0x16), TI((ub)|0x17), \ | |
702 | TN, TN, TI((ub)|0x1a), TI((ub)|0x1b), TN, TN, TI((ub)|0x1e), TI((ub)|0x1f), \ | |
703 | TI((ub)|0x20), TI((ub)|0x21), TI((ub)|0x22), TI((ub)|0x23), TI((ub)|0x24), TI((ub)|0x25), TI((ub)|0x26), TI((ub)|0x27), \ | |
704 | TN, TN, TI((ub)|0x2a), TI((ub)|0x2b), TN, TN, TI((ub)|0x2e), TI((ub)|0x2f), \ | |
705 | TN, TN, TI((ub)|0x32), TI((ub)|0x33), TN, TN, TI((ub)|0x36), TI((ub)|0x37), \ | |
706 | TN, TN, TI((ub)|0x3a), TI((ub)|0x3b), TN, TN, TI((ub)|0x3e), TI((ub)|0x3f), \ | |
707 | TI((ub)|0x40), TI((ub)|0x41), TI((ub)|0x42), TI((ub)|0x43), TI((ub)|0x44), TI((ub)|0x45), TI((ub)|0x46), TI((ub)|0x47), \ | |
708 | TN, TN, TI((ub)|0x4a), TI((ub)|0x4b), TN, TN, TI((ub)|0x4e), TI((ub)|0x4f), \ | |
709 | TN, TN, TI((ub)|0x52), TI((ub)|0x53), TN, TN, TI((ub)|0x56), TI((ub)|0x57), \ | |
710 | TN, TN, TI((ub)|0x5a), TI((ub)|0x5b), TN, TN, TI((ub)|0x5e), TI((ub)|0x5f), \ | |
711 | TI((ub)|0x60), TI((ub)|0x61), TI((ub)|0x62), TI((ub)|0x63), TI((ub)|0x64), TI((ub)|0x65), TI((ub)|0x66), TI((ub)|0x67), \ | |
712 | TN, TN, TI((ub)|0x6a), TI((ub)|0x6b), TN, TN, TI((ub)|0x6e), TI((ub)|0x6f), \ | |
713 | TN, TN, TI((ub)|0x72), TI((ub)|0x73), TN, TN, TI((ub)|0x76), TI((ub)|0x77), \ | |
714 | TN, TN, TI((ub)|0x7a), TI((ub)|0x7b), TN, TN, TI((ub)|0x7e), TI((ub)|0x7f), \ | |
715 | TN, TI((ub)|0x81), TN, TI((ub)|0x83), TN, TI((ub)|0x85), TN, TI((ub)|0x87), \ | |
716 | TN, TN, TN, TI((ub)|0x8b), TN, TN, TN, TI((ub)|0x8f), \ | |
717 | TN, TN, TN, TI((ub)|0x93), TN, TN, TN, TI((ub)|0x97), \ | |
718 | TN, TN, TN, TI((ub)|0x9b), TN, TN, TN, TI((ub)|0x9f), \ | |
719 | TN, TI((ub)|0xa1), TN, TI((ub)|0xa3), TN, TI((ub)|0xa5), TN, TI((ub)|0xa7), \ | |
720 | TN, TN, TN, TI((ub)|0xab), TN, TN, TN, TI((ub)|0xaf), \ | |
721 | TN, TN, TN, TI((ub)|0xb3), TN, TN, TN, TI((ub)|0xb7), \ | |
722 | TN, TN, TN, TI((ub)|0xbb), TN, TN, TN, TI((ub)|0xbf), \ | |
723 | TN, TI((ub)|0xc1), TN, TI((ub)|0xc3), TN, TI((ub)|0xc5), TN, TI((ub)|0xc7), \ | |
724 | TN, TN, TN, TI((ub)|0xcb), TN, TN, TN, TI((ub)|0xcf), \ | |
725 | TN, TN, TN, TI((ub)|0xd3), TN, TN, TN, TI((ub)|0xd7), \ | |
726 | TN, TN, TN, TI((ub)|0xdb), TN, TN, TN, TI((ub)|0xdf), \ | |
727 | TN, TI((ub)|0xe1), TN, TI((ub)|0xe3), TN, TI((ub)|0xe5), TN, TI((ub)|0xe7), \ | |
728 | TN, TN, TN, TI((ub)|0xeb), TN, TN, TN, TI((ub)|0xef), \ | |
729 | TN, TN, TN, TI((ub)|0xf3), TN, TN, TN, TI((ub)|0xf7), \ | |
730 | TN, TN, TN, TI((ub)|0xfb), TN, TN, TN, TI((ub)|0xff) | |
731 | ||
732 | const PP gpuPolySpanDrivers[2048] = { | |
733 | TIBLOCK(0<<8), TIBLOCK(1<<8), TIBLOCK(2<<8), TIBLOCK(3<<8), | |
734 | TIBLOCK(4<<8), TIBLOCK(5<<8), TIBLOCK(6<<8), TIBLOCK(7<<8) | |
86aad47b | 735 | }; |
030d1121 | 736 | |
737 | #undef TI | |
738 | #undef TN | |
739 | #undef TIBLOCK | |
908e426c | 740 | |
741 | #endif /* __GPU_UNAI_GPU_INNER_H__ */ |