cdrom: change pause timing again
[pcsx_rearmed.git] / plugins / gpu_unai / gpu_inner.h
index 6c18c63..1a93a39 100644 (file)
@@ -1,6 +1,7 @@
 /***************************************************************************
 *   Copyright (C) 2010 PCSX4ALL Team                                      *
 *   Copyright (C) 2010 Unai                                               *
+*   Copyright (C) 2016 Senquack (dansilsby <AT> gmail <DOT> com)          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
 ***************************************************************************/
 
-///////////////////////////////////////////////////////////////////////////////
-//  Inner loop driver instanciation file
+#ifndef __GPU_UNAI_GPU_INNER_H__
+#define __GPU_UNAI_GPU_INNER_H__
 
 ///////////////////////////////////////////////////////////////////////////////
-//  Option Masks
-#define   L ((CF>>0)&1)
-#define   B ((CF>>1)&1)
-#define   M ((CF>>2)&1)
-#define  BM ((CF>>3)&3)
-#define  TM ((CF>>5)&3)
-#define   G ((CF>>7)&1)
+// Inner loop driver instantiation file
 
-#define  AH ((CF>>7)&1)
+///////////////////////////////////////////////////////////////////////////////
+//  Option Masks (CF template paramter)
+#define  CF_LIGHT     ((CF>> 0)&1) // Lighting
+#define  CF_BLEND     ((CF>> 1)&1) // Blending
+#define  CF_MASKCHECK ((CF>> 2)&1) // Mask bit check
+#define  CF_BLENDMODE ((CF>> 3)&3) // Blend mode   0..3
+#define  CF_TEXTMODE  ((CF>> 5)&3) // Texture mode 1..3 (0: texturing disabled)
+#define  CF_GOURAUD   ((CF>> 7)&1) // Gouraud shading
+#define  CF_MASKSET   ((CF>> 8)&1) // Mask bit set
+#define  CF_DITHER    ((CF>> 9)&1) // Dithering
+#define  CF_BLITMASK  ((CF>>10)&1) // blit_mask check (skip rendering pixels
+                                   //  that wouldn't end up displayed on
+                                   //  low-res screen using simple downscaler)
 
-#define  MB ((CF>>8)&1)
+//#ifdef __arm__
+//#ifndef ENABLE_GPU_ARMV7
+/* ARMv5 */
+//#include "gpu_inner_blend_arm5.h"
+//#else
+/* ARMv7 optimized */
+//#include "gpu_inner_blend_arm7.h"
+//#endif
+//#else
+//#include "gpu_inner_blend.h"
+//#endif
 
 #include "gpu_inner_blend.h"
+#include "gpu_inner_quantization.h"
 #include "gpu_inner_light.h"
 
+#ifdef __arm__
+#include "gpu_inner_blend_arm.h"
+#include "gpu_inner_light_arm.h"
+#define gpuBlending gpuBlendingARM
+#define gpuLightingTXT gpuLightingTXTARM
+#else
+#define gpuBlending gpuBlendingGeneric
+#define gpuLightingTXT gpuLightingTXTGeneric
+#endif
+
+// Non-dithering lighting and blending functions preserve uSrc
+// MSB. This saves a few operations and useless load/stores.
+#define MSB_PRESERVED (!CF_DITHER)
+
+// If defined, Gouraud colors are fixed-point 5.11, otherwise they are 8.16
+// This is only for debugging/verification of low-precision colors in C.
+// Low-precision Gouraud is intended for use by SIMD-optimized inner drivers
+// which get/use Gouraud colors in SIMD registers.
+//#define GPU_GOURAUD_LOW_PRECISION
+
+// How many bits of fixed-point precision GouraudColor uses
+#ifdef GPU_GOURAUD_LOW_PRECISION
+#define GPU_GOURAUD_FIXED_BITS 11
+#else
+#define GPU_GOURAUD_FIXED_BITS 16
+#endif
+
+// Used to pass Gouraud colors to gpuPixelSpanFn() (lines)
+struct GouraudColor {
+#ifdef GPU_GOURAUD_LOW_PRECISION
+       u16 r, g, b;
+       s16 r_incr, g_incr, b_incr;
+#else
+       u32 r, g, b;
+       s32 r_incr, g_incr, b_incr;
+#endif
+};
+
+static inline u16 gpuGouraudColor15bpp(u32 r, u32 g, u32 b)
+{
+       r >>= GPU_GOURAUD_FIXED_BITS;
+       g >>= GPU_GOURAUD_FIXED_BITS;
+       b >>= GPU_GOURAUD_FIXED_BITS;
+
+#ifndef GPU_GOURAUD_LOW_PRECISION
+       // High-precision Gouraud colors are 8-bit + fractional
+       r >>= 3;  g >>= 3;  b >>= 3;
+#endif
+
+       return r | (g << 5) | (b << 10);
+}
+
 ///////////////////////////////////////////////////////////////////////////////
-//  GPU Pixel opperations generator
-template<const int CF>
-INLINE void gpuPixelFn(u16 *pixel,const u16 data)
+//  GPU Pixel span operations generator gpuPixelSpanFn<>
+//  Oct 2016: Created/adapted from old gpuPixelFn by senquack:
+//  Original gpuPixelFn was used to draw lines one pixel at a time. I wrote
+//  new line algorithms that draw lines using horizontal/vertical/diagonal
+//  spans of pixels, necessitating new pixel-drawing function that could
+//  not only render spans of pixels, but gouraud-shade them as well.
+//  This speeds up line rendering and would allow tile-rendering (untextured
+//  rectangles) to use the same set of functions. Since tiles are always
+//  monochrome, they simply wouldn't use the extra set of 32 gouraud-shaded
+//  gpuPixelSpanFn functions (TODO?).
+template<int CF>
+static le16_t* gpuPixelSpanFn(le16_t* pDst, uintptr_t data, ptrdiff_t incr, size_t len)
 {
-       if ((!M)&&(!B))
-       {
-               if(MB) { *pixel = data | 0x8000; }
-               else   { *pixel = data; }
+       // Blend func can save an operation if it knows uSrc MSB is
+       //  unset. For untextured prims, this is always true.
+       const bool skip_uSrc_mask = true;
+
+       u16 col;
+       struct GouraudColor * gcPtr;
+       u32 r, g, b;
+       s32 r_incr, g_incr, b_incr;
+
+       // Caller counts in bytes, we count in pixels
+       incr /= 2;
+
+       if (CF_GOURAUD) {
+               gcPtr = (GouraudColor*)data;
+               r = gcPtr->r;  r_incr = gcPtr->r_incr;
+               g = gcPtr->g;  g_incr = gcPtr->g_incr;
+               b = gcPtr->b;  b_incr = gcPtr->b_incr;
+       } else {
+               col = (u16)data;
        }
-       else if ((M)&&(!B))
-       {
-               if (!(*pixel&0x8000))
-               {
-                       if(MB) { *pixel = data | 0x8000; }
-                       else   { *pixel = data; }
+
+       do {
+               if (!CF_GOURAUD)
+               {   // NO GOURAUD
+                       if (!CF_MASKCHECK && !CF_BLEND) {
+                               if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); }
+                               else            { *pDst = u16_to_le16(col);          }
+                       } else if (CF_MASKCHECK && !CF_BLEND) {
+                               if (!(le16_raw(*pDst) & HTOLE16(0x8000))) {
+                                       if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); }
+                                       else            { *pDst = u16_to_le16(col);          }
+                               }
+                       } else {
+                               uint_fast16_t uDst = le16_to_u16(*pDst);
+                               if (CF_MASKCHECK) { if (uDst & 0x8000) goto endpixel; }
+
+                               uint_fast16_t uSrc = col;
+
+                               if (CF_BLEND)
+                                       uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
+
+                               if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
+                               else            { *pDst = u16_to_le16(uSrc);          }
+                       }
+
+               } else
+               {   // GOURAUD
+
+                       if (!CF_MASKCHECK && !CF_BLEND) {
+                               col = gpuGouraudColor15bpp(r, g, b);
+                               if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); }
+                               else            { *pDst = u16_to_le16(col);          }
+                       } else if (CF_MASKCHECK && !CF_BLEND) {
+                               col = gpuGouraudColor15bpp(r, g, b);
+                               if (!(le16_raw(*pDst) & HTOLE16(0x8000))) {
+                                       if (CF_MASKSET) { *pDst = u16_to_le16(col | 0x8000); }
+                                       else            { *pDst = u16_to_le16(col);          }
+                               }
+                       } else {
+                               uint_fast16_t uDst = le16_to_u16(*pDst);
+                               if (CF_MASKCHECK) { if (uDst & 0x8000) goto endpixel; }
+                               col = gpuGouraudColor15bpp(r, g, b);
+
+                               uint_fast16_t uSrc = col;
+
+                               // Blend func can save an operation if it knows uSrc MSB is
+                               //  unset. For untextured prims, this is always true.
+                               const bool skip_uSrc_mask = true;
+
+                               if (CF_BLEND)
+                                       uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
+
+                               if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
+                               else            { *pDst = u16_to_le16(uSrc);          }
+                       }
                }
+
+endpixel:
+               if (CF_GOURAUD) {
+                       r += r_incr;
+                       g += g_incr;
+                       b += b_incr;
+               }
+               pDst += incr;
+       } while (len-- > 1);
+
+       // Note from senquack: Normally, I'd prefer to write a 'do {} while (--len)'
+       //  loop, or even a for() loop, however, on MIPS platforms anything but the
+       //  'do {} while (len-- > 1)' tends to generate very unoptimal asm, with
+       //  many unneeded MULs/ADDs/branches at the ends of these functions.
+       //  If you change the loop structure above, be sure to compare the quality
+       //  of the generated code!!
+
+       if (CF_GOURAUD) {
+               gcPtr->r = r;
+               gcPtr->g = g;
+               gcPtr->b = b;
        }
-       else
-       {
-               u16 uDst = *pixel;
-               if(M) { if (uDst&0x8000) return; }
-               u16 uSrc = data;
-               u32 uMsk; if (BM==0) uMsk=0x7BDE;
-               if (BM==0) gpuBlending00(uSrc, uDst);
-               if (BM==1) gpuBlending01(uSrc, uDst);
-               if (BM==2) gpuBlending02(uSrc, uDst);
-               if (BM==3) gpuBlending03(uSrc, uDst);
-               if(MB) { *pixel = uSrc | 0x8000; }
-               else   { *pixel = uSrc; }
-       }
+       return pDst;
+}
+
+static le16_t* PixelSpanNULL(le16_t* pDst, uintptr_t data, ptrdiff_t incr, size_t len)
+{
+       #ifdef ENABLE_GPU_LOG_SUPPORT
+               fprintf(stdout,"PixelSpanNULL()\n");
+       #endif
+       return pDst;
 }
-///////////////////////////////////////////////////////////////////////////////
 
 ///////////////////////////////////////////////////////////////////////////////
-//  Pixel drawing drivers, for lines (only blending)
-typedef void (*PD)(u16 *pixel,const u16 data);
-const PD  gpuPixelDrivers[32] =   //  We only generate pixel op for MASKING/BLEND_ENABLE/BLEND_MODE
+//  PixelSpan (lines) innerloops driver
+typedef le16_t* (*PSD)(le16_t* dst, uintptr_t data, ptrdiff_t incr, size_t len);
+
+const PSD gpuPixelSpanDrivers[64] =
 { 
-       gpuPixelFn<0x00<<1>,gpuPixelFn<0x01<<1>,gpuPixelFn<0x02<<1>,gpuPixelFn<0x03<<1>,  
-       NULL,gpuPixelFn<0x05<<1>,NULL,gpuPixelFn<0x07<<1>,
-       NULL,gpuPixelFn<0x09<<1>,NULL,gpuPixelFn<0x0B<<1>,
-       NULL,gpuPixelFn<0x0D<<1>,NULL,gpuPixelFn<0x0F<<1>,
-
-       gpuPixelFn<(0x00<<1)|256>,gpuPixelFn<(0x01<<1)|256>,gpuPixelFn<(0x02<<1)|256>,gpuPixelFn<(0x03<<1)|256>,  
-       NULL,gpuPixelFn<(0x05<<1)|256>,NULL,gpuPixelFn<(0x07<<1)|256>,
-       NULL,gpuPixelFn<(0x09<<1)|256>,NULL,gpuPixelFn<(0x0B<<1)|256>,
-       NULL,gpuPixelFn<(0x0D<<1)|256>,NULL,gpuPixelFn<(0x0F<<1)|256>
+       // Array index | 'CF' template field | Field value
+       // ------------+---------------------+----------------
+       // Bit 0       | CF_BLEND            | off (0), on (1)
+       // Bit 1       | CF_MASKCHECK        | off (0), on (1)
+       // Bit 3:2     | CF_BLENDMODE        | 0..3
+       // Bit 4       | CF_MASKSET          | off (0), on (1)
+       // Bit 5       | CF_GOURAUD          | off (0), on (1)
+       //
+       // NULL entries are ones for which blending is disabled and blend-mode
+       //  field is non-zero, which is obviously invalid.
+
+       // Flat-shaded
+       gpuPixelSpanFn<0x00<<1>,         gpuPixelSpanFn<0x01<<1>,         gpuPixelSpanFn<0x02<<1>,         gpuPixelSpanFn<0x03<<1>,
+       PixelSpanNULL,                   gpuPixelSpanFn<0x05<<1>,         PixelSpanNULL,                   gpuPixelSpanFn<0x07<<1>,
+       PixelSpanNULL,                   gpuPixelSpanFn<0x09<<1>,         PixelSpanNULL,                   gpuPixelSpanFn<0x0B<<1>,
+       PixelSpanNULL,                   gpuPixelSpanFn<0x0D<<1>,         PixelSpanNULL,                   gpuPixelSpanFn<0x0F<<1>,
+
+       // Flat-shaded + PixelMSB (CF_MASKSET)
+       gpuPixelSpanFn<(0x00<<1)|0x100>, gpuPixelSpanFn<(0x01<<1)|0x100>, gpuPixelSpanFn<(0x02<<1)|0x100>, gpuPixelSpanFn<(0x03<<1)|0x100>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x05<<1)|0x100>, PixelSpanNULL,                   gpuPixelSpanFn<(0x07<<1)|0x100>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x09<<1)|0x100>, PixelSpanNULL,                   gpuPixelSpanFn<(0x0B<<1)|0x100>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x0D<<1)|0x100>, PixelSpanNULL,                   gpuPixelSpanFn<(0x0F<<1)|0x100>,
+
+       // Gouraud-shaded (CF_GOURAUD)
+       gpuPixelSpanFn<(0x00<<1)|0x80>,  gpuPixelSpanFn<(0x01<<1)|0x80>,  gpuPixelSpanFn<(0x02<<1)|0x80>,  gpuPixelSpanFn<(0x03<<1)|0x80>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x05<<1)|0x80>,  PixelSpanNULL,                   gpuPixelSpanFn<(0x07<<1)|0x80>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x09<<1)|0x80>,  PixelSpanNULL,                   gpuPixelSpanFn<(0x0B<<1)|0x80>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x0D<<1)|0x80>,  PixelSpanNULL,                   gpuPixelSpanFn<(0x0F<<1)|0x80>,
+
+       // Gouraud-shaded (CF_GOURAUD) + PixelMSB (CF_MASKSET)
+       gpuPixelSpanFn<(0x00<<1)|0x180>, gpuPixelSpanFn<(0x01<<1)|0x180>, gpuPixelSpanFn<(0x02<<1)|0x180>, gpuPixelSpanFn<(0x03<<1)|0x180>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x05<<1)|0x180>, PixelSpanNULL,                   gpuPixelSpanFn<(0x07<<1)|0x180>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x09<<1)|0x180>, PixelSpanNULL,                   gpuPixelSpanFn<(0x0B<<1)|0x180>,
+       PixelSpanNULL,                   gpuPixelSpanFn<(0x0D<<1)|0x180>, PixelSpanNULL,                   gpuPixelSpanFn<(0x0F<<1)|0x180>
 };
 
 ///////////////////////////////////////////////////////////////////////////////
 //  GPU Tiles innerloops generator
 
-template<const int CF>
-INLINE void  gpuTileSpanFn(u16 *pDst, u32 count, u16 data)
+template<int CF>
+static void gpuTileSpanFn(le16_t *pDst, u32 count, u16 data)
 {
-       if ((!M)&&(!B))
-       {
-               if (MB) { data = data | 0x8000; }
-               do { *pDst++ = data; } while (--count);
-       }
-       else if ((M)&&(!B))
-       {
-               if (MB) { data = data | 0x8000; }
-               do { if (!(*pDst&0x8000)) { *pDst = data; } pDst++; } while (--count);
-       }
-       else
+       le16_t ldata;
+
+       if (!CF_MASKCHECK && !CF_BLEND) {
+               if (CF_MASKSET)
+                       ldata = u16_to_le16(data | 0x8000);
+               else
+                       ldata = u16_to_le16(data);
+               do { *pDst++ = ldata; } while (--count);
+       } else if (CF_MASKCHECK && !CF_BLEND) {
+               if (CF_MASKSET)
+                       ldata = u16_to_le16(data | 0x8000);
+               else
+                       ldata = u16_to_le16(data);
+               do {
+                       if (!(le16_raw(*pDst) & HTOLE16(0x8000)))
+                               *pDst = ldata;
+                       pDst++;
+               } while (--count);
+       } else
        {
-               u16 uSrc;
-               u16 uDst;
-               u32 uMsk; if (BM==0) uMsk=0x7BDE;
+               // Blend func can save an operation if it knows uSrc MSB is
+               //  unset. For untextured prims, this is always true.
+               const bool skip_uSrc_mask = true;
+
+               uint_fast16_t uSrc, uDst;
                do
                {
-                       //  MASKING
-                       uDst = *pDst;
-                       if(M) { if (uDst&0x8000) goto endtile;  }
+                       if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); }
+                       if (CF_MASKCHECK) if (uDst&0x8000) { goto endtile; }
+
                        uSrc = data;
 
-                       //  BLEND
-                       if (BM==0) gpuBlending00(uSrc, uDst);
-                       if (BM==1) gpuBlending01(uSrc, uDst);
-                       if (BM==2) gpuBlending02(uSrc, uDst);
-                       if (BM==3) gpuBlending03(uSrc, uDst);
+                       if (CF_BLEND)
+                               uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
 
-                       if (MB) { *pDst = uSrc | 0x8000; }
-                       else    { *pDst = uSrc; }
-                       endtile: pDst++;
+                       if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
+                       else            { *pDst = u16_to_le16(uSrc);          }
+
+                       //senquack - Did not apply "Silent Hill" mask-bit fix to here.
+                       // It is hard to tell from scarce documentation available and
+                       //  lack of comments in code, but I believe the tile-span
+                       //  functions here should not bother to preserve any source MSB,
+                       //  as they are not drawing from a texture.
+endtile:
+                       pDst++;
                }
                while (--count);
        }
 }
 
+static void TileNULL(le16_t *pDst, u32 count, u16 data)
+{
+       #ifdef ENABLE_GPU_LOG_SUPPORT
+               fprintf(stdout,"TileNULL()\n");
+       #endif
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 //  Tiles innerloops driver
-typedef void (*PT)(u16 *pDst, u32 count, u16 data);
-const PT gpuTileSpanDrivers[64] = 
-{
-       gpuTileSpanFn<0x00>,NULL,gpuTileSpanFn<0x02>,NULL,  gpuTileSpanFn<0x04>,NULL,gpuTileSpanFn<0x06>,NULL,  NULL,NULL,gpuTileSpanFn<0x0A>,NULL,  NULL,NULL,gpuTileSpanFn<0x0E>,NULL,
-       NULL,NULL,gpuTileSpanFn<0x12>,NULL,  NULL,NULL,gpuTileSpanFn<0x16>,NULL,  NULL,NULL,gpuTileSpanFn<0x1A>,NULL,  NULL,NULL,gpuTileSpanFn<0x1E>,NULL,
+typedef void (*PT)(le16_t *pDst, u32 count, u16 data);
 
-       gpuTileSpanFn<0x100>,NULL,gpuTileSpanFn<0x102>,NULL,  gpuTileSpanFn<0x104>,NULL,gpuTileSpanFn<0x106>,NULL,  NULL,NULL,gpuTileSpanFn<0x10A>,NULL,  NULL,NULL,gpuTileSpanFn<0x10E>,NULL,
-       NULL,NULL,gpuTileSpanFn<0x112>,NULL,  NULL,NULL,gpuTileSpanFn<0x116>,NULL,  NULL,NULL,gpuTileSpanFn<0x11A>,NULL,  NULL,NULL,gpuTileSpanFn<0x11E>,NULL,
+// Template instantiation helper macros
+#define TI(cf) gpuTileSpanFn<(cf)>
+#define TN     TileNULL
+#define TIBLOCK(ub) \
+       TI((ub)|0x00), TI((ub)|0x02), TI((ub)|0x04), TI((ub)|0x06), \
+       TN,            TI((ub)|0x0a), TN,            TI((ub)|0x0e), \
+       TN,            TI((ub)|0x12), TN,            TI((ub)|0x16), \
+       TN,            TI((ub)|0x1a), TN,            TI((ub)|0x1e)
+
+const PT gpuTileSpanDrivers[32] = {
+       TIBLOCK(0<<8), TIBLOCK(1<<8)
 };
 
+#undef TI
+#undef TN
+#undef TIBLOCK
+
+
 ///////////////////////////////////////////////////////////////////////////////
 //  GPU Sprites innerloops generator
 
-template<const int CF>
-INLINE void  gpuSpriteSpanFn(u16 *pDst, u32 count, u32 u0, const u32 mask)
+template<int CF>
+static void gpuSpriteSpanFn(le16_t *pDst, u32 count, u8* pTxt, u32 u0)
 {
-       u16 uSrc;
-       u16 uDst;
-       const u16* pTxt = TBA+(u0&~0x1ff); u0=u0&0x1ff;
-       const u16 *_CBA; if(TM!=3) _CBA=CBA;
-       u32 lCol; if(L)  { lCol = ((u32)(b4<< 2)&(0x03ff)) | ((u32)(g4<<13)&(0x07ff<<10)) | ((u32)(r4<<24)&(0x07ff<<21));  }
-       u8 rgb; if (TM==1) rgb = ((u8*)pTxt)[u0>>1];
-       u32 uMsk; if ((B)&&(BM==0)) uMsk=0x7BDE;
+       // Blend func can save an operation if it knows uSrc MSB is unset.
+       //  Untextured prims can always skip (source color always comes with MSB=0).
+       //  For textured prims, the generic lighting funcs always return it unset. (bonus!)
+       const bool skip_uSrc_mask = MSB_PRESERVED ? (!CF_TEXTMODE) : (!CF_TEXTMODE) || CF_LIGHT;
+
+       uint_fast16_t uSrc, uDst, srcMSB;
+       bool should_blend;
+       u32 u0_mask = gpu_unai.TextureWindow[2];
+
+       u8 r5, g5, b5;
+       if (CF_LIGHT) {
+               r5 = gpu_unai.r5;
+               g5 = gpu_unai.g5;
+               b5 = gpu_unai.b5;
+       }
+
+       if (CF_TEXTMODE==3) {
+               // Texture is accessed byte-wise, so adjust mask if 16bpp
+               u0_mask <<= 1;
+       }
+
+       const le16_t *CBA_; if (CF_TEXTMODE!=3) CBA_ = gpu_unai.CBA;
 
        do
        {
-               //  MASKING
-               if(M)   { uDst = *pDst;   if (uDst&0x8000) { u0=(u0+1)&mask; goto endsprite; }  }
-
-               //  TEXTURE MAPPING
-               if (TM==1) { if (!(u0&1)) rgb = ((u8*)pTxt)[u0>>1]; uSrc = _CBA[(rgb>>((u0&1)<<2))&0xf]; u0=(u0+1)&mask; }
-               if (TM==2) { uSrc = _CBA[((u8*)pTxt)[u0]]; u0=(u0+1)&mask; }
-               if (TM==3) { uSrc = pTxt[u0]; u0=(u0+1)&mask; }
-               if(!AH) { if (!uSrc) goto endsprite; }
+               if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); }
+               if (CF_MASKCHECK) if (uDst&0x8000) { goto endsprite; }
 
-               //  BLEND
-               if(B)
-               {
-                       if(uSrc&0x8000)
-                       {
-                               //  LIGHTING CALCULATIONS
-                               if(L)  { gpuLightingTXT(uSrc, lCol);   }
-
-                               if(!M)    { uDst = *pDst; }
-                               if (BM==0) gpuBlending00(uSrc, uDst);
-                               if (BM==1) gpuBlending01(uSrc, uDst);
-                               if (BM==2) gpuBlending02(uSrc, uDst);
-                               if (BM==3) gpuBlending03(uSrc, uDst);
-                       }
-                       else
-                       {
-                               //  LIGHTING CALCULATIONS
-                               if(L)  { gpuLightingTXT(uSrc, lCol); }
-                       }
+               if (CF_TEXTMODE==1) {  //  4bpp (CLUT)
+                       u8 rgb = pTxt[(u0 & u0_mask)>>1];
+                       uSrc = le16_to_u16(CBA_[(rgb>>((u0&1)<<2))&0xf]);
                }
-               else
-               {
-                       //  LIGHTING CALCULATIONS
-                       if(L)  { gpuLightingTXT(uSrc, lCol);   } else
-                       { if(!MB) uSrc&= 0x7fff;               }
+               if (CF_TEXTMODE==2) {  //  8bpp (CLUT)
+                       uSrc = le16_to_u16(CBA_[pTxt[u0 & u0_mask]]);
                }
+               if (CF_TEXTMODE==3) {  // 16bpp
+                       uSrc = le16_to_u16(*(le16_t*)(&pTxt[u0 & u0_mask]));
+               }
+
+               if (!uSrc) goto endsprite;
 
-               if (MB) { *pDst = uSrc | 0x8000; }
-               else    { *pDst = uSrc; }
+               //senquack - save source MSB, as blending or lighting macros will not
+               //           (Silent Hill gray rectangles mask bit bug)
+               if (CF_BLEND || CF_LIGHT) srcMSB = uSrc & 0x8000;
                
-               endsprite: pDst++;
+               if (CF_LIGHT)
+                       uSrc = gpuLightingTXT(uSrc, r5, g5, b5);
+
+               should_blend = MSB_PRESERVED ? uSrc & 0x8000 : srcMSB;
+
+               if (CF_BLEND && should_blend)
+                       uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
+
+               if (CF_MASKSET)                                    { *pDst = u16_to_le16(uSrc | 0x8000); }
+               else if (!MSB_PRESERVED && (CF_BLEND || CF_LIGHT)) { *pDst = u16_to_le16(uSrc | srcMSB); }
+               else                                               { *pDst = u16_to_le16(uSrc);          }
+
+endsprite:
+               u0 += (CF_TEXTMODE==3) ? 2 : 1;
+               pDst++;
        }
        while (--count);
 }
+
+static void SpriteNULL(le16_t *pDst, u32 count, u8* pTxt, u32 u0)
+{
+       #ifdef ENABLE_GPU_LOG_SUPPORT
+               fprintf(stdout,"SpriteNULL()\n");
+       #endif
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 ///////////////////////////////////////////////////////////////////////////////
 //  Sprite innerloops driver
-typedef void (*PS)(u16 *pDst, u32 count, u32 u0, const u32 mask);
-const PS gpuSpriteSpanDrivers[512] = 
-{
-       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
-       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
-       gpuSpriteSpanFn<0x20>,gpuSpriteSpanFn<0x21>,gpuSpriteSpanFn<0x22>,gpuSpriteSpanFn<0x23>,  gpuSpriteSpanFn<0x24>,gpuSpriteSpanFn<0x25>,gpuSpriteSpanFn<0x26>,gpuSpriteSpanFn<0x27>,  NULL,NULL,gpuSpriteSpanFn<0x2A>,gpuSpriteSpanFn<0x2B>,  NULL,NULL,gpuSpriteSpanFn<0x2E>,gpuSpriteSpanFn<0x2F>,
-       NULL,NULL,gpuSpriteSpanFn<0x32>,gpuSpriteSpanFn<0x33>,  NULL,NULL,gpuSpriteSpanFn<0x36>,gpuSpriteSpanFn<0x37>,  NULL,NULL,gpuSpriteSpanFn<0x3A>,gpuSpriteSpanFn<0x3B>,  NULL,NULL,gpuSpriteSpanFn<0x3E>,gpuSpriteSpanFn<0x3F>,
-       gpuSpriteSpanFn<0x40>,gpuSpriteSpanFn<0x41>,gpuSpriteSpanFn<0x42>,gpuSpriteSpanFn<0x43>,  gpuSpriteSpanFn<0x44>,gpuSpriteSpanFn<0x45>,gpuSpriteSpanFn<0x46>,gpuSpriteSpanFn<0x47>,  NULL,NULL,gpuSpriteSpanFn<0x4A>,gpuSpriteSpanFn<0x4B>,  NULL,NULL,gpuSpriteSpanFn<0x4E>,gpuSpriteSpanFn<0x4F>,
-       NULL,NULL,gpuSpriteSpanFn<0x52>,gpuSpriteSpanFn<0x53>,  NULL,NULL,gpuSpriteSpanFn<0x56>,gpuSpriteSpanFn<0x57>,  NULL,NULL,gpuSpriteSpanFn<0x5A>,gpuSpriteSpanFn<0x5B>,  NULL,NULL,gpuSpriteSpanFn<0x5E>,gpuSpriteSpanFn<0x5F>,
-       gpuSpriteSpanFn<0x60>,gpuSpriteSpanFn<0x61>,gpuSpriteSpanFn<0x62>,gpuSpriteSpanFn<0x63>,  gpuSpriteSpanFn<0x64>,gpuSpriteSpanFn<0x65>,gpuSpriteSpanFn<0x66>,gpuSpriteSpanFn<0x67>,  NULL,NULL,gpuSpriteSpanFn<0x6A>,gpuSpriteSpanFn<0x6B>,  NULL,NULL,gpuSpriteSpanFn<0x6E>,gpuSpriteSpanFn<0x6F>,
-       NULL,NULL,gpuSpriteSpanFn<0x72>,gpuSpriteSpanFn<0x73>,  NULL,NULL,gpuSpriteSpanFn<0x76>,gpuSpriteSpanFn<0x77>,  NULL,NULL,gpuSpriteSpanFn<0x7A>,gpuSpriteSpanFn<0x7B>,  NULL,NULL,gpuSpriteSpanFn<0x7E>,gpuSpriteSpanFn<0x7F>,
-
-       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
-       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
-       gpuSpriteSpanFn<0xa0>,gpuSpriteSpanFn<0xa1>,gpuSpriteSpanFn<0xa2>,gpuSpriteSpanFn<0xa3>,  gpuSpriteSpanFn<0xa4>,gpuSpriteSpanFn<0xa5>,gpuSpriteSpanFn<0xa6>,gpuSpriteSpanFn<0xa7>,  NULL,NULL,gpuSpriteSpanFn<0xaA>,gpuSpriteSpanFn<0xaB>,  NULL,NULL,gpuSpriteSpanFn<0xaE>,gpuSpriteSpanFn<0xaF>,
-       NULL,NULL,gpuSpriteSpanFn<0xb2>,gpuSpriteSpanFn<0xb3>,  NULL,NULL,gpuSpriteSpanFn<0xb6>,gpuSpriteSpanFn<0xb7>,  NULL,NULL,gpuSpriteSpanFn<0xbA>,gpuSpriteSpanFn<0xbB>,  NULL,NULL,gpuSpriteSpanFn<0xbE>,gpuSpriteSpanFn<0xbF>,
-       gpuSpriteSpanFn<0xc0>,gpuSpriteSpanFn<0xc1>,gpuSpriteSpanFn<0xc2>,gpuSpriteSpanFn<0xc3>,  gpuSpriteSpanFn<0xc4>,gpuSpriteSpanFn<0xc5>,gpuSpriteSpanFn<0xc6>,gpuSpriteSpanFn<0xc7>,  NULL,NULL,gpuSpriteSpanFn<0xcA>,gpuSpriteSpanFn<0xcB>,  NULL,NULL,gpuSpriteSpanFn<0xcE>,gpuSpriteSpanFn<0xcF>,
-       NULL,NULL,gpuSpriteSpanFn<0xd2>,gpuSpriteSpanFn<0xd3>,  NULL,NULL,gpuSpriteSpanFn<0xd6>,gpuSpriteSpanFn<0xd7>,  NULL,NULL,gpuSpriteSpanFn<0xdA>,gpuSpriteSpanFn<0xdB>,  NULL,NULL,gpuSpriteSpanFn<0xdE>,gpuSpriteSpanFn<0xdF>,
-       gpuSpriteSpanFn<0xe0>,gpuSpriteSpanFn<0xe1>,gpuSpriteSpanFn<0xe2>,gpuSpriteSpanFn<0xe3>,  gpuSpriteSpanFn<0xe4>,gpuSpriteSpanFn<0xe5>,gpuSpriteSpanFn<0xe6>,gpuSpriteSpanFn<0xe7>,  NULL,NULL,gpuSpriteSpanFn<0xeA>,gpuSpriteSpanFn<0xeB>,  NULL,NULL,gpuSpriteSpanFn<0xeE>,gpuSpriteSpanFn<0xeF>,
-       NULL,NULL,gpuSpriteSpanFn<0xf2>,gpuSpriteSpanFn<0xf3>,  NULL,NULL,gpuSpriteSpanFn<0xf6>,gpuSpriteSpanFn<0xf7>,  NULL,NULL,gpuSpriteSpanFn<0xfA>,gpuSpriteSpanFn<0xfB>,  NULL,NULL,gpuSpriteSpanFn<0xfE>,gpuSpriteSpanFn<0xfF>,
-
-       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
-       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
-       gpuSpriteSpanFn<0x120>,gpuSpriteSpanFn<0x121>,gpuSpriteSpanFn<0x122>,gpuSpriteSpanFn<0x123>,  gpuSpriteSpanFn<0x124>,gpuSpriteSpanFn<0x125>,gpuSpriteSpanFn<0x126>,gpuSpriteSpanFn<0x127>,  NULL,NULL,gpuSpriteSpanFn<0x12A>,gpuSpriteSpanFn<0x12B>,  NULL,NULL,gpuSpriteSpanFn<0x12E>,gpuSpriteSpanFn<0x12F>,
-       NULL,NULL,gpuSpriteSpanFn<0x132>,gpuSpriteSpanFn<0x133>,  NULL,NULL,gpuSpriteSpanFn<0x136>,gpuSpriteSpanFn<0x137>,  NULL,NULL,gpuSpriteSpanFn<0x13A>,gpuSpriteSpanFn<0x13B>,  NULL,NULL,gpuSpriteSpanFn<0x13E>,gpuSpriteSpanFn<0x13F>,
-       gpuSpriteSpanFn<0x140>,gpuSpriteSpanFn<0x141>,gpuSpriteSpanFn<0x142>,gpuSpriteSpanFn<0x143>,  gpuSpriteSpanFn<0x144>,gpuSpriteSpanFn<0x145>,gpuSpriteSpanFn<0x146>,gpuSpriteSpanFn<0x147>,  NULL,NULL,gpuSpriteSpanFn<0x14A>,gpuSpriteSpanFn<0x14B>,  NULL,NULL,gpuSpriteSpanFn<0x14E>,gpuSpriteSpanFn<0x14F>,
-       NULL,NULL,gpuSpriteSpanFn<0x152>,gpuSpriteSpanFn<0x153>,  NULL,NULL,gpuSpriteSpanFn<0x156>,gpuSpriteSpanFn<0x157>,  NULL,NULL,gpuSpriteSpanFn<0x15A>,gpuSpriteSpanFn<0x15B>,  NULL,NULL,gpuSpriteSpanFn<0x15E>,gpuSpriteSpanFn<0x15F>,
-       gpuSpriteSpanFn<0x160>,gpuSpriteSpanFn<0x161>,gpuSpriteSpanFn<0x162>,gpuSpriteSpanFn<0x163>,  gpuSpriteSpanFn<0x164>,gpuSpriteSpanFn<0x165>,gpuSpriteSpanFn<0x166>,gpuSpriteSpanFn<0x167>,  NULL,NULL,gpuSpriteSpanFn<0x16A>,gpuSpriteSpanFn<0x16B>,  NULL,NULL,gpuSpriteSpanFn<0x16E>,gpuSpriteSpanFn<0x16F>,
-       NULL,NULL,gpuSpriteSpanFn<0x172>,gpuSpriteSpanFn<0x173>,  NULL,NULL,gpuSpriteSpanFn<0x176>,gpuSpriteSpanFn<0x177>,  NULL,NULL,gpuSpriteSpanFn<0x17A>,gpuSpriteSpanFn<0x17B>,  NULL,NULL,gpuSpriteSpanFn<0x17E>,gpuSpriteSpanFn<0x17F>,
-                                                                                                                                                                                                                                                                                                                                                                                      
-       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
-       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
-       gpuSpriteSpanFn<0x1a0>,gpuSpriteSpanFn<0x1a1>,gpuSpriteSpanFn<0x1a2>,gpuSpriteSpanFn<0x1a3>,  gpuSpriteSpanFn<0x1a4>,gpuSpriteSpanFn<0x1a5>,gpuSpriteSpanFn<0x1a6>,gpuSpriteSpanFn<0x1a7>,  NULL,NULL,gpuSpriteSpanFn<0x1aA>,gpuSpriteSpanFn<0x1aB>,  NULL,NULL,gpuSpriteSpanFn<0x1aE>,gpuSpriteSpanFn<0x1aF>,
-       NULL,NULL,gpuSpriteSpanFn<0x1b2>,gpuSpriteSpanFn<0x1b3>,  NULL,NULL,gpuSpriteSpanFn<0x1b6>,gpuSpriteSpanFn<0x1b7>,  NULL,NULL,gpuSpriteSpanFn<0x1bA>,gpuSpriteSpanFn<0x1bB>,  NULL,NULL,gpuSpriteSpanFn<0x1bE>,gpuSpriteSpanFn<0x1bF>,
-       gpuSpriteSpanFn<0x1c0>,gpuSpriteSpanFn<0x1c1>,gpuSpriteSpanFn<0x1c2>,gpuSpriteSpanFn<0x1c3>,  gpuSpriteSpanFn<0x1c4>,gpuSpriteSpanFn<0x1c5>,gpuSpriteSpanFn<0x1c6>,gpuSpriteSpanFn<0x1c7>,  NULL,NULL,gpuSpriteSpanFn<0x1cA>,gpuSpriteSpanFn<0x1cB>,  NULL,NULL,gpuSpriteSpanFn<0x1cE>,gpuSpriteSpanFn<0x1cF>,
-       NULL,NULL,gpuSpriteSpanFn<0x1d2>,gpuSpriteSpanFn<0x1d3>,  NULL,NULL,gpuSpriteSpanFn<0x1d6>,gpuSpriteSpanFn<0x1d7>,  NULL,NULL,gpuSpriteSpanFn<0x1dA>,gpuSpriteSpanFn<0x1dB>,  NULL,NULL,gpuSpriteSpanFn<0x1dE>,gpuSpriteSpanFn<0x1dF>,
-       gpuSpriteSpanFn<0x1e0>,gpuSpriteSpanFn<0x1e1>,gpuSpriteSpanFn<0x1e2>,gpuSpriteSpanFn<0x1e3>,  gpuSpriteSpanFn<0x1e4>,gpuSpriteSpanFn<0x1e5>,gpuSpriteSpanFn<0x1e6>,gpuSpriteSpanFn<0x1e7>,  NULL,NULL,gpuSpriteSpanFn<0x1eA>,gpuSpriteSpanFn<0x1eB>,  NULL,NULL,gpuSpriteSpanFn<0x1eE>,gpuSpriteSpanFn<0x1eF>,
-       NULL,NULL,gpuSpriteSpanFn<0x1f2>,gpuSpriteSpanFn<0x1f3>,  NULL,NULL,gpuSpriteSpanFn<0x1f6>,gpuSpriteSpanFn<0x1f7>,  NULL,NULL,gpuSpriteSpanFn<0x1fA>,gpuSpriteSpanFn<0x1fB>,  NULL,NULL,gpuSpriteSpanFn<0x1fE>,gpuSpriteSpanFn<0x1fF>
+typedef void (*PS)(le16_t *pDst, u32 count, u8* pTxt, u32 u0);
+
+// Template instantiation helper macros
+#define TI(cf) gpuSpriteSpanFn<(cf)>
+#define TN     SpriteNULL
+#define TIBLOCK(ub) \
+       TN,            TN,            TN,            TN,            TN,            TN,            TN,            TN,            \
+       TN,            TN,            TN,            TN,            TN,            TN,            TN,            TN,            \
+       TN,            TN,            TN,            TN,            TN,            TN,            TN,            TN,            \
+       TN,            TN,            TN,            TN,            TN,            TN,            TN,            TN,            \
+       TI((ub)|0x20), TI((ub)|0x21), TI((ub)|0x22), TI((ub)|0x23), TI((ub)|0x24), TI((ub)|0x25), TI((ub)|0x26), TI((ub)|0x27), \
+       TN,            TN,            TI((ub)|0x2a), TI((ub)|0x2b), TN,            TN,            TI((ub)|0x2e), TI((ub)|0x2f), \
+       TN,            TN,            TI((ub)|0x32), TI((ub)|0x33), TN,            TN,            TI((ub)|0x36), TI((ub)|0x37), \
+       TN,            TN,            TI((ub)|0x3a), TI((ub)|0x3b), TN,            TN,            TI((ub)|0x3e), TI((ub)|0x3f), \
+       TI((ub)|0x40), TI((ub)|0x41), TI((ub)|0x42), TI((ub)|0x43), TI((ub)|0x44), TI((ub)|0x45), TI((ub)|0x46), TI((ub)|0x47), \
+       TN,            TN,            TI((ub)|0x4a), TI((ub)|0x4b), TN,            TN,            TI((ub)|0x4e), TI((ub)|0x4f), \
+       TN,            TN,            TI((ub)|0x52), TI((ub)|0x53), TN,            TN,            TI((ub)|0x56), TI((ub)|0x57), \
+       TN,            TN,            TI((ub)|0x5a), TI((ub)|0x5b), TN,            TN,            TI((ub)|0x5e), TI((ub)|0x5f), \
+       TI((ub)|0x60), TI((ub)|0x61), TI((ub)|0x62), TI((ub)|0x63), TI((ub)|0x64), TI((ub)|0x65), TI((ub)|0x66), TI((ub)|0x67), \
+       TN,            TN,            TI((ub)|0x6a), TI((ub)|0x6b), TN,            TN,            TI((ub)|0x6e), TI((ub)|0x6f), \
+       TN,            TN,            TI((ub)|0x72), TI((ub)|0x73), TN,            TN,            TI((ub)|0x76), TI((ub)|0x77), \
+       TN,            TN,            TI((ub)|0x7a), TI((ub)|0x7b), TN,            TN,            TI((ub)|0x7e), TI((ub)|0x7f)
+
+const PS gpuSpriteSpanDrivers[256] = {
+       TIBLOCK(0<<8), TIBLOCK(1<<8)
 };
 
+#undef TI
+#undef TN
+#undef TIBLOCK
+
 ///////////////////////////////////////////////////////////////////////////////
 //  GPU Polygon innerloops generator
-template<const int CF>
-INLINE void  gpuPolySpanFn(u16 *pDst, u32 count)
+
+//senquack - Newer version with following changes:
+//           * Adapted to work with new poly routings in gpu_raster_polygon.h
+//             adapted from DrHell GPU. They are less glitchy and use 22.10
+//             fixed-point instead of original UNAI's 16.16.
+//           * Texture coordinates are no longer packed together into one
+//             unsigned int. This seems to lose too much accuracy (they each
+//             end up being only 8.7 fixed-point that way) and pixel-droupouts
+//             were noticeable both with original code and current DrHell
+//             adaptations. An example would be the sky in NFS3. Now, they are
+//             stored in separate ints, using separate masks.
+//           * Function is no longer INLINE, as it was always called
+//             through a function pointer.
+//           * Function now ensures the mask bit of source texture is preserved
+//             across calls to blending functions (Silent Hill rectangles fix)
+//           * November 2016: Large refactoring of blending/lighting when
+//             JohnnyF added dithering. See gpu_inner_quantization.h and
+//             relevant blend/light headers.
+// (see README_senquack.txt)
+template<int CF>
+static void gpuPolySpanFn(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count)
 {
-       if (!TM)
-       {       
-               // NO TEXTURE
-               if (!G)
+       // Blend func can save an operation if it knows uSrc MSB is unset.
+       //  Untextured prims can always skip this (src color MSB is always 0).
+       //  For textured prims, the generic lighting funcs always return it unset. (bonus!)
+       const bool skip_uSrc_mask = MSB_PRESERVED ? (!CF_TEXTMODE) : (!CF_TEXTMODE) || CF_LIGHT;
+       bool should_blend;
+
+       u32 bMsk; if (CF_BLITMASK) bMsk = gpu_unai.blit_mask;
+
+       if (!CF_TEXTMODE)
+       {
+               if (!CF_GOURAUD)
                {
-                       // NO GOURAUD
-                       u16 data;
-                       if (L) { u32 lCol=((u32)(b4<< 2)&(0x03ff)) | ((u32)(g4<<13)&(0x07ff<<10)) | ((u32)(r4<<24)&(0x07ff<<21)); gpuLightingRGB(data,lCol); }
-                       else data=PixelData;
-                       if ((!M)&&(!B))
-                       {
-                               if (MB) { data = data | 0x8000; }
-                               do { *pDst++ = data; } while (--count);
-                       }
-                       else if ((M)&&(!B))
-                       {
-                               if (MB) { data = data | 0x8000; }
-                               do { if (!(*pDst&0x8000)) { *pDst = data; } pDst++; } while (--count);
-                       }
-                       else
-                       {
-                               u16 uSrc;
-                               u16 uDst;
-                               u32 uMsk; if (BM==0) uMsk=0x7BDE;
-                               do
-                               {
-                                       //  masking
-                                       uDst = *pDst;
-                                       if(M) { if (uDst&0x8000) goto endtile;  }
-                                       uSrc = data;
-                                       //  blend
-                                       if (BM==0) gpuBlending00(uSrc, uDst);
-                                       if (BM==1) gpuBlending01(uSrc, uDst);
-                                       if (BM==2) gpuBlending02(uSrc, uDst);
-                                       if (BM==3) gpuBlending03(uSrc, uDst);
-                                       if (MB) { *pDst = uSrc | 0x8000; }
-                                       else    { *pDst = uSrc; }
-                                       endtile: pDst++;
-                               }
-                               while (--count);
-                       }
+                       // UNTEXTURED, NO GOURAUD
+                       const u16 pix15 = gpu_unai.PixelData;
+                       do {
+                               uint_fast16_t uSrc, uDst;
+
+                               // NOTE: Don't enable CF_BLITMASK  pixel skipping (speed hack)
+                               //  on untextured polys. It seems to do more harm than good: see
+                               //  gravestone text at end of Medieval intro sequence. -senquack
+                               //if (CF_BLITMASK) { if ((bMsk>>((((uintptr_t)pDst)>>1)&7))&1) { goto endpolynotextnogou; } }
+
+                               if (CF_BLEND || CF_MASKCHECK) uDst = le16_to_u16(*pDst);
+                               if (CF_MASKCHECK) { if (uDst&0x8000) { goto endpolynotextnogou; } }
+
+                               uSrc = pix15;
+
+                               if (CF_BLEND)
+                                       uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
+
+                               if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
+                               else            { *pDst = u16_to_le16(uSrc);          }
+
+endpolynotextnogou:
+                               pDst++;
+                       } while(--count);
                }
                else
                {
-                       // GOURAUD
-                       u16 uDst;
-                       u16 uSrc;
-                       u32 linc=lInc;
-                       u32 lCol=((u32)(b4>>14)&(0x03ff)) | ((u32)(g4>>3)&(0x07ff<<10)) | ((u32)(r4<<8)&(0x07ff<<21));
-                       u32 uMsk; if ((B)&&(BM==0)) uMsk=0x7BDE;
-                       do
-                       {
-                               //  masking
-                               if(M) { uDst = *pDst;  if (uDst&0x8000) goto endgou;  }
-                               //  blend
-                               if(B)
-                               {
-                                       //  light
-                                       gpuLightingRGB(uSrc,lCol);
-                                       if(!M)    { uDst = *pDst; }
-                                       if (BM==0) gpuBlending00(uSrc, uDst);
-                                       if (BM==1) gpuBlending01(uSrc, uDst);
-                                       if (BM==2) gpuBlending02(uSrc, uDst);
-                                       if (BM==3) gpuBlending03(uSrc, uDst);
-                               }
-                               else
-                               {
-                                       //  light
-                                       gpuLightingRGB(uSrc,lCol);
+                       // UNTEXTURED, GOURAUD
+                       gcol_t l_gCol = gpu_unai.gCol;
+                       gcol_t l_gInc = gpu_unai.gInc;
+
+                       do {
+                               uint_fast16_t uDst, uSrc;
+
+                               // See note in above loop regarding CF_BLITMASK
+                               //if (CF_BLITMASK) { if ((bMsk>>((((uintptr_t)pDst)>>1)&7))&1) goto endpolynotextgou; }
+
+                               if (CF_BLEND || CF_MASKCHECK) uDst = le16_to_u16(*pDst);
+                               if (CF_MASKCHECK) { if (uDst&0x8000) goto endpolynotextgou; }
+
+                               if (CF_DITHER) {
+                                       // GOURAUD, DITHER
+
+                                       u32 uSrc24 = gpuLightingRGB24(l_gCol);
+                                       if (CF_BLEND)
+                                               uSrc24 = gpuBlending24<CF_BLENDMODE>(uSrc24, uDst);
+                                       uSrc = gpuColorQuantization24<CF_DITHER>(uSrc24, pDst);
+                               } else {
+                                       // GOURAUD, NO DITHER
+
+                                       uSrc = gpuLightingRGB(l_gCol);
+
+                                       if (CF_BLEND)
+                                               uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
                                }
-                               if (MB) { *pDst = uSrc | 0x8000; }
-                               else    { *pDst = uSrc; }
-                               endgou: pDst++; lCol=(lCol+linc);
+
+                               if (CF_MASKSET) { *pDst = u16_to_le16(uSrc | 0x8000); }
+                               else            { *pDst = u16_to_le16(uSrc);          }
+
+endpolynotextgou:
+                               pDst++;
+                               l_gCol.raw += l_gInc.raw;
                        }
                        while (--count);
                }
        }
        else
        {
-               // TEXTURE
-               u16 uDst;
-               u16 uSrc;
-               u32 linc; if (L&&G) linc=lInc;
-               u32 tinc=tInc;
-               u32 tmsk=tMsk;
-               u32 tCor = ((u32)( u4<<7)&0x7fff0000) | ((u32)( v4>>9)&0x00007fff); tCor&= tmsk;
-               const u16* _TBA=TBA;
-               const u16* _CBA; if (TM!=3) _CBA=CBA;
-               u32 lCol;
-               if(L && !G) { lCol = ((u32)(b4<< 2)&(0x03ff)) | ((u32)(g4<<13)&(0x07ff<<10)) | ((u32)(r4<<24)&(0x07ff<<21)); }
-               else if(L && G) { lCol = ((u32)(b4>>14)&(0x03ff)) | ((u32)(g4>>3)&(0x07ff<<10)) | ((u32)(r4<<8)&(0x07ff<<21));  }
-               u32 uMsk; if ((B)&&(BM==0)) uMsk=0x7BDE;
+               // TEXTURED
+
+               uint_fast16_t uDst, uSrc, srcMSB;
+
+               //senquack - note: original UNAI code had gpu_unai.{u4/v4} packed into
+               // one 32-bit unsigned int, but this proved to lose too much accuracy
+               // (pixel drouputs noticeable in NFS3 sky), so now are separate vars.
+               u32 l_u_msk = gpu_unai.u_msk;     u32 l_v_msk = gpu_unai.v_msk;
+               u32 l_u = gpu_unai.u & l_u_msk;   u32 l_v = gpu_unai.v & l_v_msk;
+               s32 l_u_inc = gpu_unai.u_inc;     s32 l_v_inc = gpu_unai.v_inc;
+
+               const le16_t* TBA_ = gpu_unai.TBA;
+               const le16_t* CBA_; if (CF_TEXTMODE!=3) CBA_ = gpu_unai.CBA;
+
+               u8 r5, g5, b5;
+               u8 r8, g8, b8;
+
+               gcol_t l_gInc, l_gCol;
+
+               if (CF_LIGHT) {
+                       if (CF_GOURAUD) {
+                               l_gInc = gpu_unai.gInc;
+                               l_gCol = gpu_unai.gCol;
+                       } else {
+                               if (CF_DITHER) {
+                                       r8 = gpu_unai.r8;
+                                       g8 = gpu_unai.g8;
+                                       b8 = gpu_unai.b8;
+                               } else {
+                                       r5 = gpu_unai.r5;
+                                       g5 = gpu_unai.g5;
+                                       b5 = gpu_unai.b5;
+                               }
+                       }
+               }
+
                do
                {
-                       //  masking
-                       if(M) { uDst = *pDst;  if (uDst&0x8000) goto endpoly;  }
-                       //  texture
-                       if (TM==1) { u32 tu=(tCor>>23); u32 tv=(tCor<<4)&(0xff<<11); u8 rgb=((u8*)_TBA)[tv+(tu>>1)]; uSrc=_CBA[(rgb>>((tu&1)<<2))&0xf]; if(!uSrc) goto endpoly; }
-                       if (TM==2) { uSrc = _CBA[(((u8*)_TBA)[(tCor>>23)+((tCor<<4)&(0xff<<11))])]; if(!uSrc)  goto endpoly; }
-                       if (TM==3) { uSrc = _TBA[(tCor>>23)+((tCor<<3)&(0xff<<10))]; if(!uSrc)  goto endpoly; }
-                       //  blend
-                       if(B)
-                       {
-                               if (uSrc&0x8000)
-                               {
-                                       //  light
-                                       if(L) gpuLightingTXT(uSrc, lCol);
-                                       if(!M)    { uDst = *pDst; }
-                                       if (BM==0) gpuBlending00(uSrc, uDst);
-                                       if (BM==1) gpuBlending01(uSrc, uDst);
-                                       if (BM==2) gpuBlending02(uSrc, uDst);
-                                       if (BM==3) gpuBlending03(uSrc, uDst);
-                               }
-                               else
-                               {
-                                       // light
-                                       if(L) gpuLightingTXT(uSrc, lCol);
-                               }
+                       if (CF_BLITMASK) { if ((bMsk>>((((uintptr_t)pDst)>>1)&7))&1) goto endpolytext; }
+                       if (CF_MASKCHECK || CF_BLEND) { uDst = le16_to_u16(*pDst); }
+                       if (CF_MASKCHECK) if (uDst&0x8000) { goto endpolytext; }
+
+                       //senquack - adapted to work with new 22.10 fixed point routines:
+                       //           (UNAI originally used 16.16)
+                       if (CF_TEXTMODE==1) {  //  4bpp (CLUT)
+                               u32 tu=(l_u>>10);
+                               u32 tv=(l_v<<1)&(0xff<<11);
+                               u8 rgb=((u8*)TBA_)[tv+(tu>>1)];
+                               uSrc=le16_to_u16(CBA_[(rgb>>((tu&1)<<2))&0xf]);
+                               if (!uSrc) goto endpolytext;
                        }
-                       else
+                       if (CF_TEXTMODE==2) {  //  8bpp (CLUT)
+                               uSrc = le16_to_u16(CBA_[(((u8*)TBA_)[(l_u>>10)+((l_v<<1)&(0xff<<11))])]);
+                               if (!uSrc) goto endpolytext;
+                       }
+                       if (CF_TEXTMODE==3) {  // 16bpp
+                               uSrc = le16_to_u16(TBA_[(l_u>>10)+((l_v)&(0xff<<10))]);
+                               if (!uSrc) goto endpolytext;
+                       }
+
+                       // Save source MSB, as blending or lighting will not (Silent Hill)
+                       if (CF_BLEND || CF_LIGHT) srcMSB = uSrc & 0x8000;
+
+                       // When textured, only dither when LIGHT (texture blend) is enabled
+                       // LIGHT &&  BLEND => dither
+                       // LIGHT && !BLEND => dither
+                       //!LIGHT &&  BLEND => no dither
+                       //!LIGHT && !BLEND => no dither
+
+                       if (CF_DITHER && CF_LIGHT) {
+                               u32 uSrc24;
+                               if ( CF_GOURAUD)
+                                       uSrc24 = gpuLightingTXT24Gouraud(uSrc, l_gCol);
+                               if (!CF_GOURAUD)
+                                       uSrc24 = gpuLightingTXT24(uSrc, r8, g8, b8);
+
+                               if (CF_BLEND && srcMSB)
+                                       uSrc24 = gpuBlending24<CF_BLENDMODE>(uSrc24, uDst);
+
+                               uSrc = gpuColorQuantization24<CF_DITHER>(uSrc24, pDst);
+                       } else
                        {
-                               //  light
-                               if(L)  { gpuLightingTXT(uSrc, lCol); } else if(!MB) { uSrc&= 0x7fff; }
+                               if (CF_LIGHT) {
+                                       if ( CF_GOURAUD)
+                                               uSrc = gpuLightingTXTGouraud(uSrc, l_gCol);
+                                       if (!CF_GOURAUD)
+                                               uSrc = gpuLightingTXT(uSrc, r5, g5, b5);
+                               }
+
+                               should_blend = MSB_PRESERVED ? uSrc & 0x8000 : srcMSB;
+                               if (CF_BLEND && should_blend)
+                                       uSrc = gpuBlending<CF_BLENDMODE, skip_uSrc_mask>(uSrc, uDst);
                        }
-                       if (MB) { *pDst = uSrc | 0x8000; }
-                       else    { *pDst = uSrc; }
-                       endpoly: pDst++;
-                       tCor=(tCor+tinc)&tmsk;
-                       if (L&&G) lCol=(lCol+linc);
+
+                       if (CF_MASKSET)                                    { *pDst = u16_to_le16(uSrc | 0x8000); }
+                       else if (!MSB_PRESERVED && (CF_BLEND || CF_LIGHT)) { *pDst = u16_to_le16(uSrc | srcMSB); }
+                       else                                               { *pDst = u16_to_le16(uSrc);          }
+endpolytext:
+                       pDst++;
+                       l_u = (l_u + l_u_inc) & l_u_msk;
+                       l_v = (l_v + l_v_inc) & l_v_msk;
+                       if (CF_LIGHT && CF_GOURAUD)
+                               l_gCol.raw += l_gInc.raw;
                }
                while (--count);
        }
 }
-///////////////////////////////////////////////////////////////////////////////
+
+static void PolyNULL(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count)
+{
+       #ifdef ENABLE_GPU_LOG_SUPPORT
+               fprintf(stdout,"PolyNULL()\n");
+       #endif
+}
 
 ///////////////////////////////////////////////////////////////////////////////
 //  Polygon innerloops driver
-typedef void (*PP)(u16 *pDst, u32 count);
-const PP gpuPolySpanDrivers[512] =
-{
-       gpuPolySpanFn<0x00>,gpuPolySpanFn<0x01>,gpuPolySpanFn<0x02>,gpuPolySpanFn<0x03>,  gpuPolySpanFn<0x04>,gpuPolySpanFn<0x05>,gpuPolySpanFn<0x06>,gpuPolySpanFn<0x07>,  NULL,NULL,gpuPolySpanFn<0x0A>,gpuPolySpanFn<0x0B>,  NULL,NULL,gpuPolySpanFn<0x0E>,gpuPolySpanFn<0x0F>,
-       NULL,NULL,gpuPolySpanFn<0x12>,gpuPolySpanFn<0x13>,  NULL,NULL,gpuPolySpanFn<0x16>,gpuPolySpanFn<0x17>,  NULL,NULL,gpuPolySpanFn<0x1A>,gpuPolySpanFn<0x1B>,  NULL,NULL,gpuPolySpanFn<0x1E>,gpuPolySpanFn<0x1F>,
-       gpuPolySpanFn<0x20>,gpuPolySpanFn<0x21>,gpuPolySpanFn<0x22>,gpuPolySpanFn<0x23>,  gpuPolySpanFn<0x24>,gpuPolySpanFn<0x25>,gpuPolySpanFn<0x26>,gpuPolySpanFn<0x27>,  NULL,NULL,gpuPolySpanFn<0x2A>,gpuPolySpanFn<0x2B>,  NULL,NULL,gpuPolySpanFn<0x2E>,gpuPolySpanFn<0x2F>,
-       NULL,NULL,gpuPolySpanFn<0x32>,gpuPolySpanFn<0x33>,  NULL,NULL,gpuPolySpanFn<0x36>,gpuPolySpanFn<0x37>,  NULL,NULL,gpuPolySpanFn<0x3A>,gpuPolySpanFn<0x3B>,  NULL,NULL,gpuPolySpanFn<0x3E>,gpuPolySpanFn<0x3F>,
-       gpuPolySpanFn<0x40>,gpuPolySpanFn<0x41>,gpuPolySpanFn<0x42>,gpuPolySpanFn<0x43>,  gpuPolySpanFn<0x44>,gpuPolySpanFn<0x45>,gpuPolySpanFn<0x46>,gpuPolySpanFn<0x47>,  NULL,NULL,gpuPolySpanFn<0x4A>,gpuPolySpanFn<0x4B>,  NULL,NULL,gpuPolySpanFn<0x4E>,gpuPolySpanFn<0x4F>,
-       NULL,NULL,gpuPolySpanFn<0x52>,gpuPolySpanFn<0x53>,  NULL,NULL,gpuPolySpanFn<0x56>,gpuPolySpanFn<0x57>,  NULL,NULL,gpuPolySpanFn<0x5A>,gpuPolySpanFn<0x5B>,  NULL,NULL,gpuPolySpanFn<0x5E>,gpuPolySpanFn<0x5F>,
-       gpuPolySpanFn<0x60>,gpuPolySpanFn<0x61>,gpuPolySpanFn<0x62>,gpuPolySpanFn<0x63>,  gpuPolySpanFn<0x64>,gpuPolySpanFn<0x65>,gpuPolySpanFn<0x66>,gpuPolySpanFn<0x67>,  NULL,NULL,gpuPolySpanFn<0x6A>,gpuPolySpanFn<0x6B>,  NULL,NULL,gpuPolySpanFn<0x6E>,gpuPolySpanFn<0x6F>,
-       NULL,NULL,gpuPolySpanFn<0x72>,gpuPolySpanFn<0x73>,  NULL,NULL,gpuPolySpanFn<0x76>,gpuPolySpanFn<0x77>,  NULL,NULL,gpuPolySpanFn<0x7A>,gpuPolySpanFn<0x7B>,  NULL,NULL,gpuPolySpanFn<0x7E>,gpuPolySpanFn<0x7F>,
-
-       NULL,gpuPolySpanFn<0x81>,NULL,gpuPolySpanFn<0x83>,  NULL,gpuPolySpanFn<0x85>,NULL,gpuPolySpanFn<0x87>,  NULL,NULL,NULL,gpuPolySpanFn<0x8B>,  NULL,NULL,NULL,gpuPolySpanFn<0x8F>,
-       NULL,NULL,NULL,gpuPolySpanFn<0x93>,  NULL,NULL,NULL,gpuPolySpanFn<0x97>,  NULL,NULL,NULL,gpuPolySpanFn<0x9B>,  NULL,NULL,NULL,gpuPolySpanFn<0x9F>,
-       NULL,gpuPolySpanFn<0xa1>,NULL,gpuPolySpanFn<0xa3>,  NULL,gpuPolySpanFn<0xa5>,NULL,gpuPolySpanFn<0xa7>,  NULL,NULL,NULL,gpuPolySpanFn<0xaB>,  NULL,NULL,NULL,gpuPolySpanFn<0xaF>,
-       NULL,NULL,NULL,gpuPolySpanFn<0xb3>,  NULL,NULL,NULL,gpuPolySpanFn<0xb7>,  NULL,NULL,NULL,gpuPolySpanFn<0xbB>,  NULL,NULL,NULL,gpuPolySpanFn<0xbF>,
-       NULL,gpuPolySpanFn<0xc1>,NULL,gpuPolySpanFn<0xc3>,  NULL,gpuPolySpanFn<0xc5>,NULL,gpuPolySpanFn<0xc7>,  NULL,NULL,NULL,gpuPolySpanFn<0xcB>,  NULL,NULL,NULL,gpuPolySpanFn<0xcF>,
-       NULL,NULL,NULL,gpuPolySpanFn<0xd3>,  NULL,NULL,NULL,gpuPolySpanFn<0xd7>,  NULL,NULL,NULL,gpuPolySpanFn<0xdB>,  NULL,NULL,NULL,gpuPolySpanFn<0xdF>,
-       NULL,gpuPolySpanFn<0xe1>,NULL,gpuPolySpanFn<0xe3>,  NULL,gpuPolySpanFn<0xe5>,NULL,gpuPolySpanFn<0xe7>,  NULL,NULL,NULL,gpuPolySpanFn<0xeB>,  NULL,NULL,NULL,gpuPolySpanFn<0xeF>,
-       NULL,NULL,NULL,gpuPolySpanFn<0xf3>,  NULL,NULL,NULL,gpuPolySpanFn<0xf7>,  NULL,NULL,NULL,gpuPolySpanFn<0xfB>,  NULL,NULL,NULL,gpuPolySpanFn<0xfF>,
-
-       gpuPolySpanFn<0x100>,gpuPolySpanFn<0x101>,gpuPolySpanFn<0x102>,gpuPolySpanFn<0x103>,  gpuPolySpanFn<0x104>,gpuPolySpanFn<0x105>,gpuPolySpanFn<0x106>,gpuPolySpanFn<0x107>,  NULL,NULL,gpuPolySpanFn<0x10A>,gpuPolySpanFn<0x10B>,  NULL,NULL,gpuPolySpanFn<0x10E>,gpuPolySpanFn<0x10F>,
-       NULL,NULL,gpuPolySpanFn<0x112>,gpuPolySpanFn<0x113>,  NULL,NULL,gpuPolySpanFn<0x116>,gpuPolySpanFn<0x117>,  NULL,NULL,gpuPolySpanFn<0x11A>,gpuPolySpanFn<0x11B>,  NULL,NULL,gpuPolySpanFn<0x11E>,gpuPolySpanFn<0x11F>,
-       gpuPolySpanFn<0x120>,gpuPolySpanFn<0x121>,gpuPolySpanFn<0x122>,gpuPolySpanFn<0x123>,  gpuPolySpanFn<0x124>,gpuPolySpanFn<0x125>,gpuPolySpanFn<0x126>,gpuPolySpanFn<0x127>,  NULL,NULL,gpuPolySpanFn<0x12A>,gpuPolySpanFn<0x12B>,  NULL,NULL,gpuPolySpanFn<0x12E>,gpuPolySpanFn<0x12F>,
-       NULL,NULL,gpuPolySpanFn<0x132>,gpuPolySpanFn<0x133>,  NULL,NULL,gpuPolySpanFn<0x136>,gpuPolySpanFn<0x137>,  NULL,NULL,gpuPolySpanFn<0x13A>,gpuPolySpanFn<0x13B>,  NULL,NULL,gpuPolySpanFn<0x13E>,gpuPolySpanFn<0x13F>,
-       gpuPolySpanFn<0x140>,gpuPolySpanFn<0x141>,gpuPolySpanFn<0x142>,gpuPolySpanFn<0x143>,  gpuPolySpanFn<0x144>,gpuPolySpanFn<0x145>,gpuPolySpanFn<0x146>,gpuPolySpanFn<0x147>,  NULL,NULL,gpuPolySpanFn<0x14A>,gpuPolySpanFn<0x14B>,  NULL,NULL,gpuPolySpanFn<0x14E>,gpuPolySpanFn<0x14F>,
-       NULL,NULL,gpuPolySpanFn<0x152>,gpuPolySpanFn<0x153>,  NULL,NULL,gpuPolySpanFn<0x156>,gpuPolySpanFn<0x157>,  NULL,NULL,gpuPolySpanFn<0x15A>,gpuPolySpanFn<0x15B>,  NULL,NULL,gpuPolySpanFn<0x15E>,gpuPolySpanFn<0x15F>,
-       gpuPolySpanFn<0x160>,gpuPolySpanFn<0x161>,gpuPolySpanFn<0x162>,gpuPolySpanFn<0x163>,  gpuPolySpanFn<0x164>,gpuPolySpanFn<0x165>,gpuPolySpanFn<0x166>,gpuPolySpanFn<0x167>,  NULL,NULL,gpuPolySpanFn<0x16A>,gpuPolySpanFn<0x16B>,  NULL,NULL,gpuPolySpanFn<0x16E>,gpuPolySpanFn<0x16F>,
-       NULL,NULL,gpuPolySpanFn<0x172>,gpuPolySpanFn<0x173>,  NULL,NULL,gpuPolySpanFn<0x176>,gpuPolySpanFn<0x177>,  NULL,NULL,gpuPolySpanFn<0x17A>,gpuPolySpanFn<0x17B>,  NULL,NULL,gpuPolySpanFn<0x17E>,gpuPolySpanFn<0x17F>,
-                                                                                                                                                                                                                                                                                                                                                                                      
-       NULL,gpuPolySpanFn<0x181>,NULL,gpuPolySpanFn<0x183>,  NULL,gpuPolySpanFn<0x185>,NULL,gpuPolySpanFn<0x187>,  NULL,NULL,NULL,gpuPolySpanFn<0x18B>,  NULL,NULL,NULL,gpuPolySpanFn<0x18F>,
-       NULL,NULL,NULL,gpuPolySpanFn<0x193>,  NULL,NULL,NULL,gpuPolySpanFn<0x197>,  NULL,NULL,NULL,gpuPolySpanFn<0x19B>,  NULL,NULL,NULL,gpuPolySpanFn<0x19F>,
-       NULL,gpuPolySpanFn<0x1a1>,NULL,gpuPolySpanFn<0x1a3>,  NULL,gpuPolySpanFn<0x1a5>,NULL,gpuPolySpanFn<0x1a7>,  NULL,NULL,NULL,gpuPolySpanFn<0x1aB>,  NULL,NULL,NULL,gpuPolySpanFn<0x1aF>,
-       NULL,NULL,NULL,gpuPolySpanFn<0x1b3>,  NULL,NULL,NULL,gpuPolySpanFn<0x1b7>,  NULL,NULL,NULL,gpuPolySpanFn<0x1bB>,  NULL,NULL,NULL,gpuPolySpanFn<0x1bF>,
-       NULL,gpuPolySpanFn<0x1c1>,NULL,gpuPolySpanFn<0x1c3>,  NULL,gpuPolySpanFn<0x1c5>,NULL,gpuPolySpanFn<0x1c7>,  NULL,NULL,NULL,gpuPolySpanFn<0x1cB>,  NULL,NULL,NULL,gpuPolySpanFn<0x1cF>,
-       NULL,NULL,NULL,gpuPolySpanFn<0x1d3>,  NULL,NULL,NULL,gpuPolySpanFn<0x1d7>,  NULL,NULL,NULL,gpuPolySpanFn<0x1dB>,  NULL,NULL,NULL,gpuPolySpanFn<0x1dF>,
-       NULL,gpuPolySpanFn<0x1e1>,NULL,gpuPolySpanFn<0x1e3>,  NULL,gpuPolySpanFn<0x1e5>,NULL,gpuPolySpanFn<0x1e7>,  NULL,NULL,NULL,gpuPolySpanFn<0x1eB>,  NULL,NULL,NULL,gpuPolySpanFn<0x1eF>,
-       NULL,NULL,NULL,gpuPolySpanFn<0x1f3>,  NULL,NULL,NULL,gpuPolySpanFn<0x1f7>,  NULL,NULL,NULL,gpuPolySpanFn<0x1fB>,  NULL,NULL,NULL,gpuPolySpanFn<0x1fF>
+typedef void (*PP)(const gpu_unai_t &gpu_unai, le16_t *pDst, u32 count);
+
+// Template instantiation helper macros
+#define TI(cf) gpuPolySpanFn<(cf)>
+#define TN     PolyNULL
+#define TIBLOCK(ub) \
+       TI((ub)|0x00), TI((ub)|0x01), TI((ub)|0x02), TI((ub)|0x03), TI((ub)|0x04), TI((ub)|0x05), TI((ub)|0x06), TI((ub)|0x07), \
+       TN,            TN,            TI((ub)|0x0a), TI((ub)|0x0b), TN,            TN,            TI((ub)|0x0e), TI((ub)|0x0f), \
+       TN,            TN,            TI((ub)|0x12), TI((ub)|0x13), TN,            TN,            TI((ub)|0x16), TI((ub)|0x17), \
+       TN,            TN,            TI((ub)|0x1a), TI((ub)|0x1b), TN,            TN,            TI((ub)|0x1e), TI((ub)|0x1f), \
+       TI((ub)|0x20), TI((ub)|0x21), TI((ub)|0x22), TI((ub)|0x23), TI((ub)|0x24), TI((ub)|0x25), TI((ub)|0x26), TI((ub)|0x27), \
+       TN,            TN,            TI((ub)|0x2a), TI((ub)|0x2b), TN,            TN,            TI((ub)|0x2e), TI((ub)|0x2f), \
+       TN,            TN,            TI((ub)|0x32), TI((ub)|0x33), TN,            TN,            TI((ub)|0x36), TI((ub)|0x37), \
+       TN,            TN,            TI((ub)|0x3a), TI((ub)|0x3b), TN,            TN,            TI((ub)|0x3e), TI((ub)|0x3f), \
+       TI((ub)|0x40), TI((ub)|0x41), TI((ub)|0x42), TI((ub)|0x43), TI((ub)|0x44), TI((ub)|0x45), TI((ub)|0x46), TI((ub)|0x47), \
+       TN,            TN,            TI((ub)|0x4a), TI((ub)|0x4b), TN,            TN,            TI((ub)|0x4e), TI((ub)|0x4f), \
+       TN,            TN,            TI((ub)|0x52), TI((ub)|0x53), TN,            TN,            TI((ub)|0x56), TI((ub)|0x57), \
+       TN,            TN,            TI((ub)|0x5a), TI((ub)|0x5b), TN,            TN,            TI((ub)|0x5e), TI((ub)|0x5f), \
+       TI((ub)|0x60), TI((ub)|0x61), TI((ub)|0x62), TI((ub)|0x63), TI((ub)|0x64), TI((ub)|0x65), TI((ub)|0x66), TI((ub)|0x67), \
+       TN,            TN,            TI((ub)|0x6a), TI((ub)|0x6b), TN,            TN,            TI((ub)|0x6e), TI((ub)|0x6f), \
+       TN,            TN,            TI((ub)|0x72), TI((ub)|0x73), TN,            TN,            TI((ub)|0x76), TI((ub)|0x77), \
+       TN,            TN,            TI((ub)|0x7a), TI((ub)|0x7b), TN,            TN,            TI((ub)|0x7e), TI((ub)|0x7f), \
+       TN,            TI((ub)|0x81), TN,            TI((ub)|0x83), TN,            TI((ub)|0x85), TN,            TI((ub)|0x87), \
+       TN,            TN,            TN,            TI((ub)|0x8b), TN,            TN,            TN,            TI((ub)|0x8f), \
+       TN,            TN,            TN,            TI((ub)|0x93), TN,            TN,            TN,            TI((ub)|0x97), \
+       TN,            TN,            TN,            TI((ub)|0x9b), TN,            TN,            TN,            TI((ub)|0x9f), \
+       TN,            TI((ub)|0xa1), TN,            TI((ub)|0xa3), TN,            TI((ub)|0xa5), TN,            TI((ub)|0xa7), \
+       TN,            TN,            TN,            TI((ub)|0xab), TN,            TN,            TN,            TI((ub)|0xaf), \
+       TN,            TN,            TN,            TI((ub)|0xb3), TN,            TN,            TN,            TI((ub)|0xb7), \
+       TN,            TN,            TN,            TI((ub)|0xbb), TN,            TN,            TN,            TI((ub)|0xbf), \
+       TN,            TI((ub)|0xc1), TN,            TI((ub)|0xc3), TN,            TI((ub)|0xc5), TN,            TI((ub)|0xc7), \
+       TN,            TN,            TN,            TI((ub)|0xcb), TN,            TN,            TN,            TI((ub)|0xcf), \
+       TN,            TN,            TN,            TI((ub)|0xd3), TN,            TN,            TN,            TI((ub)|0xd7), \
+       TN,            TN,            TN,            TI((ub)|0xdb), TN,            TN,            TN,            TI((ub)|0xdf), \
+       TN,            TI((ub)|0xe1), TN,            TI((ub)|0xe3), TN,            TI((ub)|0xe5), TN,            TI((ub)|0xe7), \
+       TN,            TN,            TN,            TI((ub)|0xeb), TN,            TN,            TN,            TI((ub)|0xef), \
+       TN,            TN,            TN,            TI((ub)|0xf3), TN,            TN,            TN,            TI((ub)|0xf7), \
+       TN,            TN,            TN,            TI((ub)|0xfb), TN,            TN,            TN,            TI((ub)|0xff)
+
+const PP gpuPolySpanDrivers[2048] = {
+       TIBLOCK(0<<8), TIBLOCK(1<<8), TIBLOCK(2<<8), TIBLOCK(3<<8),
+       TIBLOCK(4<<8), TIBLOCK(5<<8), TIBLOCK(6<<8), TIBLOCK(7<<8)
 };
+
+#undef TI
+#undef TN
+#undef TIBLOCK
+
+#endif /* __GPU_UNAI_GPU_INNER_H__ */