1 /***************************************************************************
2 * Copyright (C) 2010 PCSX4ALL Team *
3 * Copyright (C) 2010 Unai *
4 * Copyright (C) 2016 Senquack (dansilsby <AT> gmail <DOT> com) *
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. *
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. *
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 ***************************************************************************/
22 #ifndef __GPU_UNAI_GPU_INNER_H__
23 #define __GPU_UNAI_GPU_INNER_H__
25 ///////////////////////////////////////////////////////////////////////////////
26 // Inner loop driver instantiation file
28 ///////////////////////////////////////////////////////////////////////////////
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)
43 //#ifndef ENABLE_GPU_ARMV7
45 //#include "gpu_inner_blend_arm5.h"
48 //#include "gpu_inner_blend_arm7.h"
51 //#include "gpu_inner_blend.h"
54 #include "gpu_inner_blend.h"
55 #include "gpu_inner_quantization.h"
56 #include "gpu_inner_light.h"
59 #include "gpu_inner_blend_arm.h"
60 #include "gpu_inner_light_arm.h"
61 #define gpuBlending gpuBlendingARM
62 #define gpuLightingTXT gpuLightingTXTARM
64 #define gpuBlending gpuBlendingGeneric
65 #define gpuLightingTXT gpuLightingTXTGeneric
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)
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
78 // How many bits of fixed-point precision GouraudColor uses
79 #ifdef GPU_GOURAUD_LOW_PRECISION
80 #define GPU_GOURAUD_FIXED_BITS 11
82 #define GPU_GOURAUD_FIXED_BITS 16
85 // Used to pass Gouraud colors to gpuPixelSpanFn() (lines)
87 #ifdef GPU_GOURAUD_LOW_PRECISION
89 s16 r_incr, g_incr, b_incr;
92 s32 r_incr, g_incr, b_incr;
96 static inline u16 gpuGouraudColor15bpp(u32 r, u32 g, u32 b)
98 r >>= GPU_GOURAUD_FIXED_BITS;
99 g >>= GPU_GOURAUD_FIXED_BITS;
100 b >>= GPU_GOURAUD_FIXED_BITS;
102 #ifndef GPU_GOURAUD_LOW_PRECISION
103 // High-precision Gouraud colors are 8-bit + fractional
104 r >>= 3; g >>= 3; b >>= 3;
107 return r | (g << 5) | (b << 10);
110 ///////////////////////////////////////////////////////////////////////////////
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?).
122 static le16_t* gpuPixelSpanFn(le16_t* pDst, uintptr_t data, ptrdiff_t incr, size_t len)
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;
129 struct GouraudColor * gcPtr;
131 s32 r_incr, g_incr, b_incr;
133 // Caller counts in bytes, we count in pixels
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;
148 if (!CF_MASKCHECK && !CF_BLEND) {
149 if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); }
150 else { *pDst = u16_to_le16(col); }
151 } else if (CF_MASKCHECK && !CF_BLEND) {
152 if (!(le16_raw(*pDst) & HTOLE16(0x8000))) {
153 if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); }
154 else { *pDst = u16_to_le16(col); }
157 uint_fast16_t uDst = le16_to_u16(*pDst);
158 if (CF_MASKCHECK) { if (uDst & 0x8000) goto endpixel; }
160 uint_fast16_t uSrc = col;
163 uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
165 if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
166 else { *pDst = u16_to_le16(uSrc); }
172 if (!CF_MASKCHECK && !CF_BLEND) {
173 col = gpuGouraudColor15bpp(r, g, b);
174 if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); }
175 else { *pDst = u16_to_le16(col); }
176 } else if (CF_MASKCHECK && !CF_BLEND) {
177 col = gpuGouraudColor15bpp(r, g, b);
178 if (!(le16_raw(*pDst) & HTOLE16(0x8000))) {
179 if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); }
180 else { *pDst = u16_to_le16(col); }
183 uint_fast16_t uDst = le16_to_u16(*pDst);
184 if (CF_MASKCHECK) { if (uDst & 0x8000) goto endpixel; }
185 col = gpuGouraudColor15bpp(r, g, b);
187 uint_fast16_t uSrc = col;
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;
194 uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
196 if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
197 else { *pDst = u16_to_le16(uSrc); }
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!!
225 static le16_t* PixelSpanNULL(le16_t* pDst, uintptr_t data, ptrdiff_t incr, size_t len)
227 #ifdef ENABLE_GPU_LOG_SUPPORT
228 fprintf(stdout,"PixelSpanNULL()\n");
233 ///////////////////////////////////////////////////////////////////////////////
234 // PixelSpan (lines) innerloops driver
235 typedef le16_t* (*PSD)(le16_t* dst, uintptr_t data, ptrdiff_t incr, size_t len);
237 const PSD gpuPixelSpanDrivers[64] =
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)
247 // NULL entries are ones for which blending is disabled and blend-mode
248 // field is non-zero, which is obviously invalid.
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>,
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>,
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>,
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>
275 ///////////////////////////////////////////////////////////////////////////////
276 // GPU Tiles innerloops generator
279 static void gpuTileSpanFn(le16_t *pDst, u32 count, u16 data)
283 if (!CF_MASKCHECK && !CF_BLEND) {
285 ldata = u16_to_le16(data | 0x8000);
287 ldata = u16_to_le16(data);
288 do { *pDst++ = ldata; } while (--count);
289 } else if (CF_MASKCHECK && !CF_BLEND) {
291 ldata = u16_to_le16(data | 0x8000);
293 ldata = u16_to_le16(data);
295 if (!(le16_raw(*pDst) & HTOLE16(0x8000)))
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;
305 uint_fast16_t uSrc, uDst;
308 if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); }
309 if (CF_MASKCHECK) if (uDst&0x8000) { goto endtile; }
314 uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
316 if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
317 else { *pDst = u16_to_le16(uSrc); }
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.
331 static void TileNULL(le16_t *pDst, u32 count, u16 data)
333 #ifdef ENABLE_GPU_LOG_SUPPORT
334 fprintf(stdout,"TileNULL()\n");
338 ///////////////////////////////////////////////////////////////////////////////
339 // Tiles innerloops driver
340 typedef void (*PT)(le16_t *pDst, u32 count, u16 data);
342 // Template instantiation helper macros
343 #define TI(cf) gpuTileSpanFn<(cf)>
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)
351 const PT gpuTileSpanDrivers[32] = {
352 TIBLOCK(0<<8), TIBLOCK(1<<8)
360 ///////////////////////////////////////////////////////////////////////////////
361 // GPU Sprites innerloops generator
364 static void gpuSpriteSpanFn(le16_t *pDst, u32 count, u8* pTxt, u32 u0)
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).
368 // For textured prims, the generic lighting funcs always return it unset. (bonus!)
369 const bool skip_uSrc_mask = MSB_PRESERVED ? (!CF_TEXTMODE) : (!CF_TEXTMODE) || CF_LIGHT;
371 uint_fast16_t uSrc, uDst, srcMSB;
373 u32 u0_mask = gpu_unai.TextureWindow[2];
382 if (CF_TEXTMODE==3) {
383 // Texture is accessed byte-wise, so adjust mask if 16bpp
387 const le16_t *CBA_; if (CF_TEXTMODE!=3) CBA_ = gpu_unai.CBA;
391 if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); }
392 if (CF_MASKCHECK) if (uDst&0x8000) { goto endsprite; }
394 if (CF_TEXTMODE==1) { // 4bpp (CLUT)
395 u8 rgb = pTxt[(u0 & u0_mask)>>1];
396 uSrc = le16_to_u16(CBA_[(rgb>>((u0&1)<<2))&0xf]);
398 if (CF_TEXTMODE==2) { // 8bpp (CLUT)
399 uSrc = le16_to_u16(CBA_[pTxt[u0 & u0_mask]]);
401 if (CF_TEXTMODE==3) { // 16bpp
402 uSrc = le16_to_u16(*(le16_t*)(&pTxt[u0 & u0_mask]));
405 if (!uSrc) goto endsprite;
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;
412 uSrc = gpuLightingTXT(uSrc, r5, g5, b5);
414 should_blend = MSB_PRESERVED ? uSrc & 0x8000 : srcMSB;
416 if (CF_BLEND && should_blend)
417 uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
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); }
424 u0 += (CF_TEXTMODE==3) ? 2 : 1;
430 static void SpriteNULL(le16_t *pDst, u32 count, u8* pTxt, u32 u0)
432 #ifdef ENABLE_GPU_LOG_SUPPORT
433 fprintf(stdout,"SpriteNULL()\n");
437 ///////////////////////////////////////////////////////////////////////////////
439 ///////////////////////////////////////////////////////////////////////////////
440 // Sprite innerloops driver
441 typedef void (*PS)(le16_t *pDst, u32 count, u8* pTxt, u32 u0);
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)
464 const PS gpuSpriteSpanDrivers[256] = {
465 TIBLOCK(0<<8), TIBLOCK(1<<8)
472 ///////////////////////////////////////////////////////////////////////////////
473 // GPU Polygon innerloops generator
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)
494 static void gpuPolySpanFn(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count)
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).
498 // For textured prims, the generic lighting funcs always return it unset. (bonus!)
499 const bool skip_uSrc_mask = MSB_PRESERVED ? (!CF_TEXTMODE) : (!CF_TEXTMODE) || CF_LIGHT;
502 u32 bMsk; if (CF_BLITMASK) bMsk = gpu_unai.blit_mask;
508 // UNTEXTURED, NO GOURAUD
509 const u16 pix15 = gpu_unai.PixelData;
511 uint_fast16_t uSrc, uDst;
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; } }
518 if (CF_BLEND || CF_MASKCHECK) uDst = le16_to_u16(*pDst);
519 if (CF_MASKCHECK) { if (uDst&0x8000) { goto endpolynotextnogou; } }
524 uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
526 if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
527 else { *pDst = u16_to_le16(uSrc); }
535 // UNTEXTURED, GOURAUD
536 gcol_t l_gCol = gpu_unai.gCol;
537 gcol_t l_gInc = gpu_unai.gInc;
540 uint_fast16_t uDst, uSrc;
542 // See note in above loop regarding CF_BLITMASK
543 //if (CF_BLITMASK) { if ((bMsk>>((((uintptr_t)pDst)>>1)&7))&1) goto endpolynotextgou; }
545 if (CF_BLEND || CF_MASKCHECK) uDst = le16_to_u16(*pDst);
546 if (CF_MASKCHECK) { if (uDst&0x8000) goto endpolynotextgou; }
551 u32 uSrc24 = gpuLightingRGB24(l_gCol);
553 uSrc24 = gpuBlending24<CF_BLENDMODE>(uSrc24, uDst);
554 uSrc = gpuColorQuantization24<CF_DITHER>(uSrc24, pDst);
556 // GOURAUD, NO DITHER
558 uSrc = gpuLightingRGB(l_gCol);
561 uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
564 if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
565 else { *pDst = u16_to_le16(uSrc); }
569 l_gCol.raw += l_gInc.raw;
578 uint_fast16_t uDst, uSrc, srcMSB;
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;
587 const le16_t* TBA_ = gpu_unai.TBA;
588 const le16_t* CBA_; if (CF_TEXTMODE!=3) CBA_ = gpu_unai.CBA;
593 gcol_t l_gInc, l_gCol;
597 l_gInc = gpu_unai.gInc;
598 l_gCol = gpu_unai.gCol;
614 if (CF_BLITMASK) { if ((bMsk>>((((uintptr_t)pDst)>>1)&7))&1) goto endpolytext; }
615 if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); }
616 if (CF_MASKCHECK) if (uDst&0x8000) { goto endpolytext; }
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)
622 u32 tv=(l_v<<1)&(0xff<<11);
623 u8 rgb=((u8*)TBA_)[tv+(tu>>1)];
624 uSrc=le16_to_u16(CBA_[(rgb>>((tu&1)<<2))&0xf]);
625 if (!uSrc) goto endpolytext;
627 if (CF_TEXTMODE==2) { // 8bpp (CLUT)
628 uSrc = le16_to_u16(CBA_[(((u8*)TBA_)[(l_u>>10)+((l_v<<1)&(0xff<<11))])]);
629 if (!uSrc) goto endpolytext;
631 if (CF_TEXTMODE==3) { // 16bpp
632 uSrc = le16_to_u16(TBA_[(l_u>>10)+((l_v)&(0xff<<10))]);
633 if (!uSrc) goto endpolytext;
636 // Save source MSB, as blending or lighting will not (Silent Hill)
637 if (CF_BLEND || CF_LIGHT) srcMSB = uSrc & 0x8000;
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
645 if (CF_DITHER && CF_LIGHT) {
648 uSrc24 = gpuLightingTXT24Gouraud(uSrc, l_gCol);
650 uSrc24 = gpuLightingTXT24(uSrc, r8, g8, b8);
652 if (CF_BLEND && srcMSB)
653 uSrc24 = gpuBlending24<CF_BLENDMODE>(uSrc24, uDst);
655 uSrc = gpuColorQuantization24<CF_DITHER>(uSrc24, pDst);
660 uSrc = gpuLightingTXTGouraud(uSrc, l_gCol);
662 uSrc = gpuLightingTXT(uSrc, r5, g5, b5);
665 should_blend = MSB_PRESERVED ? uSrc & 0x8000 : srcMSB;
666 if (CF_BLEND && should_blend)
667 uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
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); }
675 l_u = (l_u + l_u_inc) & l_u_msk;
676 l_v = (l_v + l_v_inc) & l_v_msk;
677 if (CF_LIGHT && CF_GOURAUD)
678 l_gCol.raw += l_gInc.raw;
684 static void PolyNULL(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count)
686 #ifdef ENABLE_GPU_LOG_SUPPORT
687 fprintf(stdout,"PolyNULL()\n");
691 ///////////////////////////////////////////////////////////////////////////////
692 // Polygon innerloops driver
693 typedef void (*PP)(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count);
695 // Template instantiation helper macros
696 #define TI(cf) gpuPolySpanFn<(cf)>
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)
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)
741 #endif /* __GPU_UNAI_GPU_INNER_H__ */