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