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