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