aboutsummaryrefslogtreecommitdiff
path: root/plugins/gpu_unai/gpulib_if.cpp
blob: e84eff5f4e9a2475670342584a1d5c1dab405778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/***************************************************************************
*   Copyright (C) 2010 PCSX4ALL Team                                      *
*   Copyright (C) 2010 Unai                                               *
*   Copyright (C) 2011 notaz                                              *
*   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  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
***************************************************************************/

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../gpulib/gpu.h"
//#include "port.h"
#include "gpu_unai.h"

// GPU fixed point math
#include "gpu_fixedpoint.h"

// Inner loop driver instantiation file
#include "gpu_inner.h"

// GPU internal image drawing functions
#include "gpu_raster_image.h"

// GPU internal line drawing functions
#include "gpu_raster_line.h"

// GPU internal polygon drawing functions
#include "gpu_raster_polygon.h"

// GPU internal sprite drawing functions
#include "gpu_raster_sprite.h"

// GPU command buffer execution/store
#include "gpu_command.h"

/////////////////////////////////////////////////////////////////////////////

#define DOWNSCALE_VRAM_SIZE (1024 * 512 * 2 * 2 + 4096)

INLINE void scale_640_to_320(uint16_t *dest, const uint16_t *src, bool isRGB24) {
  size_t uCount = 320;

  if(isRGB24) {
    const uint8_t* src8 = (const uint8_t *)src;
    uint8_t* dst8 = (uint8_t *)dest;

    do {
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8;
      src8 += 4;
    } while(--uCount);
  } else {
    const uint16_t* src16 = src;
    uint16_t* dst16 = dest;

    do {
      *dst16++ = *src16;
      src16 += 2;
    } while(--uCount);
  }
}

INLINE void scale_512_to_320(uint16_t *dest, const uint16_t *src, bool isRGB24) {
  size_t uCount = 64;

  if(isRGB24) {
    const uint8_t* src8 = (const uint8_t *)src;
    uint8_t* dst8 = (uint8_t *)dest;

    do {
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8;
      src8 += 4;
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8;
      src8 += 4;
      *dst8++ = *src8++;
      *dst8++ = *src8++;
      *dst8++ = *src8;
      src8 += 4;
    } while(--uCount);
  } else {
    const uint16_t* src16 = src;
    uint16_t* dst16 = dest;

    do {
      *dst16++ = *src16++;
      *dst16++ = *src16;
      src16 += 2;
      *dst16++ = *src16++;
      *dst16++ = *src16;
      src16 += 2;
      *dst16++ = *src16;
      src16 += 2;
    } while(--uCount);
  }
}

static uint16_t *get_downscale_buffer(int *x, int *y, int *w, int *h, int *vram_h)
{
  uint16_t *dest = gpu_unai.downscale_vram;
  const uint16_t *src = gpu_unai.vram;
  bool isRGB24 = (gpu_unai.GPU_GP1 & 0x00200000 ? true : false);
  int stride = 1024, dstride = 1024, lines = *h, orig_w = *w;

  // PS1 fb read wraps around (fixes black screen in 'Tobal no. 1')
  unsigned int fb_mask = 1024 * 512 - 1;

  if (*h > 240) {
    *h /= 2;
    stride *= 2;
    lines = *h;

    // Ensure start at a non-skipped line
    while (*y & gpu_unai.ilace_mask) ++*y;
  }

  unsigned int fb_offset_src = (*y * dstride + *x) & fb_mask;
  unsigned int fb_offset_dest = fb_offset_src;

  if (*w == 512 || *w == 640) {
    *w = 320;
  }

  switch(orig_w) {
  case 640:
    do {
      scale_640_to_320(dest + fb_offset_dest, src + fb_offset_src, isRGB24);
      fb_offset_src = (fb_offset_src + stride) & fb_mask;
      fb_offset_dest = (fb_offset_dest + dstride) & fb_mask;
    } while(--lines);

    break;
  case 512:
    do {
      scale_512_to_320(dest + fb_offset_dest, src + fb_offset_src, isRGB24);
      fb_offset_src = (fb_offset_src + stride) & fb_mask;
      fb_offset_dest = (fb_offset_dest + dstride) & fb_mask;
    } while(--lines);
    break;
  default:
    size_t size = isRGB24 ? *w * 3 : *w * 2;

    do {
      memcpy(dest + fb_offset_dest, src + fb_offset_src, size);
      fb_offset_src = (fb_offset_src + stride) & fb_mask;
      fb_offset_dest = (fb_offset_dest + dstride) & fb_mask;
    } while(--lines);
    break;
  }

  return gpu_unai.downscale_vram;
}

static void map_downscale_buffer(void)
{
  if (gpu_unai.downscale_vram)
    return;

  gpu_unai.downscale_vram = (uint16_t*)gpu.mmap(DOWNSCALE_VRAM_SIZE);

  if (gpu_unai.downscale_vram == NULL) {
    fprintf(stderr, "failed to map downscale buffer\n");
    gpu.get_downscale_buffer = NULL;
  }
  else {
    gpu.get_downscale_buffer = get_downscale_buffer;
  }
}

static void unmap_downscale_buffer(void)
{
  if (gpu_unai.downscale_vram == NULL)
    return;

  gpu.munmap(gpu_unai.downscale_vram, DOWNSCALE_VRAM_SIZE);
  gpu_unai.downscale_vram = NULL;
  gpu.get_downscale_buffer = NULL;
}

int renderer_init(void)
{
  memset((void*)&gpu_unai, 0, sizeof(gpu_unai));
  gpu_unai.vram = (u16*)gpu.vram;

  // Original standalone gpu_unai initialized TextureWindow[]. I added the
  //  same behavior here, since it seems unsafe to leave [2],[3] unset when
  //  using HLE and Rearmed gpu_neon sets this similarly on init. -senquack
  gpu_unai.TextureWindow[0] = 0;
  gpu_unai.TextureWindow[1] = 0;
  gpu_unai.TextureWindow[2] = 255;
  gpu_unai.TextureWindow[3] = 255;
  //senquack - new vars must be updated whenever texture window is changed:
  //           (used for polygon-drawing in gpu_inner.h, gpu_raster_polygon.h)
  const u32 fb = FIXED_BITS;  // # of fractional fixed-pt bits of u4/v4
  gpu_unai.u_msk = (((u32)gpu_unai.TextureWindow[2]) << fb) | ((1 << fb) - 1);
  gpu_unai.v_msk = (((u32)gpu_unai.TextureWindow[3]) << fb) | ((1 << fb) - 1);

  // Configuration options
  gpu_unai.config = gpu_unai_config_ext;
  //senquack - disabled, not sure this is needed and would require modifying
  // sprite-span functions, perhaps unnecessarily. No Abe Oddysey hack was
  // present in latest PCSX4ALL sources we were using.
  //gpu_unai.config.enableAbbeyHack = gpu_unai_config_ext.abe_hack;
  gpu_unai.ilace_mask = gpu_unai.config.ilace_force;

#ifdef GPU_UNAI_USE_INT_DIV_MULTINV
  // s_invTable
  for(int i=1;i<=(1<<TABLE_BITS);++i)
  {
    double v = 1.0 / double(i);
#ifdef GPU_TABLE_10_BITS
    v *= double(0xffffffff>>1);
#else
    v *= double(0x80000000);
#endif
    s_invTable[i-1]=s32(v);
  }
#endif

  SetupLightLUT();
  SetupDitheringConstants();

  if (gpu_unai.config.scale_hires) {
    map_downscale_buffer();
  }

  return 0;
}

void renderer_finish(void)
{
  unmap_downscale_buffer();
}

void renderer_notify_res_change(void)
{
  if (PixelSkipEnabled()) {
    // Set blit_mask for high horizontal resolutions. This allows skipping
    //  rendering pixels that would never get displayed on low-resolution
    //  platforms that use simple pixel-dropping scaler.

    switch (gpu.screen.hres)
    {
      case 512: gpu_unai.blit_mask = 0xa4; break; // GPU_BlitWWSWWSWS
      case 640: gpu_unai.blit_mask = 0xaa; break; // GPU_BlitWS
      default:  gpu_unai.blit_mask = 0;    break;
    }
  } else {
    gpu_unai.blit_mask = 0;
  }

  if (LineSkipEnabled()) {
    // Set rendering line-skip (only render every other line in high-res
    //  480 vertical mode, or, optionally, force it for all video modes)

    if (gpu.screen.vres == 480) {
      if (gpu_unai.config.ilace_force) {
        gpu_unai.ilace_mask = 3; // Only need 1/4 of lines
      } else {
        gpu_unai.ilace_mask = 1; // Only need 1/2 of lines
      }
    } else {
      // Vert resolution changed from 480 to lower one
      gpu_unai.ilace_mask = gpu_unai.config.ilace_force;
    }
  } else {
    gpu_unai.ilace_mask = 0;
  }

  /*
  printf("res change hres: %d   vres: %d   depth: %d   ilace_mask: %d\n",
      gpu.screen.hres, gpu.screen.vres, gpu.status.rgb24 ? 24 : 15,
      gpu_unai.ilace_mask);
  */
}

#ifdef USE_GPULIB
// Handles GP0 draw settings commands 0xE1...0xE6
static void gpuGP0Cmd_0xEx(gpu_unai_t &gpu_unai, u32 cmd_word)
{
  // Assume incoming GP0 command is 0xE1..0xE6, convert to 1..6
  u8 num = (cmd_word >> 24) & 7;
  gpu.ex_regs[num] = cmd_word; // Update gpulib register
  switch (num) {
    case 1: {
      // GP0(E1h) - Draw Mode setting (aka "Texpage")
      u32 cur_texpage = gpu_unai.GPU_GP1 & 0x7FF;
      u32 new_texpage = cmd_word & 0x7FF;
      if (cur_texpage != new_texpage) {
        gpu_unai.GPU_GP1 = (gpu_unai.GPU_GP1 & ~0x7FF) | new_texpage;
        gpuSetTexture(gpu_unai.GPU_GP1);
      }
    } break;

    case 2: {
      // GP0(E2h) - Texture Window setting
      if (cmd_word != gpu_unai.TextureWindowCur) {
        static const u8 TextureMask[32] = {
          255, 7, 15, 7, 31, 7, 15, 7, 63, 7, 15, 7, 31, 7, 15, 7,
          127, 7, 15, 7, 31, 7, 15, 7, 63, 7, 15, 7, 31, 7, 15, 7
        };
        gpu_unai.TextureWindowCur = cmd_word;
        gpu_unai.TextureWindow[0] = ((cmd_word >> 10) & 0x1F) << 3;
        gpu_unai.TextureWindow[1] = ((cmd_word >> 15) & 0x1F) << 3;
        gpu_unai.TextureWindow[2] = TextureMask[(cmd_word >> 0) & 0x1F];
        gpu_unai.TextureWindow[3] = TextureMask[(cmd_word >> 5) & 0x1F];
        gpu_unai.TextureWindow[0] &= ~gpu_unai.TextureWindow[2];
        gpu_unai.TextureWindow[1] &= ~gpu_unai.TextureWindow[3];

        // Inner loop vars must be updated whenever texture window is changed:
        const u32 fb = FIXED_BITS;  // # of fractional fixed-pt bits of u4/v4
        gpu_unai.u_msk = (((u32)gpu_unai.TextureWindow[2]) << fb) | ((1 << fb) - 1);
        gpu_unai.v_msk = (((u32)gpu_unai.TextureWindow[3]) << fb) | ((1 << fb) - 1);

        gpuSetTexture(gpu_unai.GPU_GP1);
      }
    } break;

    case 3: {
      // GP0(E3h) - Set Drawing Area top left (X1,Y1)
      gpu_unai.DrawingArea[0] = cmd_word         & 0x3FF;
      gpu_unai.DrawingArea[1] = (cmd_word >> 10) & 0x3FF;
    } break;

    case 4: {
      // GP0(E4h) - Set Drawing Area bottom right (X2,Y2)
      gpu_unai.DrawingArea[2] = (cmd_word         & 0x3FF) + 1;
      gpu_unai.DrawingArea[3] = ((cmd_word >> 10) & 0x3FF) + 1;
    } break;

    case 5: {
      // GP0(E5h) - Set Drawing Offset (X,Y)
      gpu_unai.DrawingOffset[0] = ((s32)cmd_word<<(32-11))>>(32-11);
      gpu_unai.DrawingOffset[1] = ((s32)cmd_word<<(32-22))>>(32-11);
    } break;

    case 6: {
      // GP0(E6h) - Mask Bit Setting
      gpu_unai.Masking  = (cmd_word & 0x2) <<  1;
      gpu_unai.PixelMSB = (cmd_word & 0x1) <<  8;
    } break;
  }
}
#endif

extern const unsigned char cmd_lengths[256];

int do_cmd_list(u32 *list, int list_len, int *last_cmd)
{
  u32 cmd = 0, len, i;
  u32 *list_start = list;
  u32 *list_end = list + list_len;

  //TODO: set ilace_mask when resolution changes instead of every time,
  // eliminate #ifdef below.
  gpu_unai.ilace_mask = gpu_unai.config.ilace_force;

#ifdef HAVE_PRE_ARMV7 /* XXX */
  gpu_unai.ilace_mask |= gpu.status.interlace;
#endif
  if (gpu_unai.config.scale_hires) {
    gpu_unai.ilace_mask |= gpu.status.interlace;
  }

  for (; list < list_end; list += 1 + len)
  {
    cmd = *list >> 24;
    len = cmd_lengths[cmd];
    if (list + 1 + len > list_end) {
      cmd = -1;
      break;
    }

    #define PRIM cmd
    gpu_unai.PacketBuffer.U4[0] = list[0];
    for (i = 1; i <= len; i++)
      gpu_unai.PacketBuffer.U4[i] = list[i];

    PtrUnion packet = { .ptr = (void*)&gpu_unai.PacketBuffer };

    switch (cmd)
    {
      case 0x02:
        gpuClearImage(packet);
        break;

      case 0x20:
      case 0x21:
      case 0x22:
      case 0x23: {          // Monochrome 3-pt poly
        PP driver = gpuPolySpanDrivers[
          (gpu_unai.blit_mask?1024:0) |
          Blending_Mode |
          gpu_unai.Masking | Blending | gpu_unai.PixelMSB
        ];
        gpuDrawPolyF(packet, driver, false);
      } break;

      case 0x24:
      case 0x25:
      case 0x26:
      case 0x27: {          // Textured 3-pt poly
        gpuSetCLUT   (gpu_unai.PacketBuffer.U4[2] >> 16);
        gpuSetTexture(gpu_unai.PacketBuffer.U4[4] >> 16);

        u32 driver_idx =
          (gpu_unai.blit_mask?1024:0) |
          Dithering |
          Blending_Mode | gpu_unai.TEXT_MODE |
          gpu_unai.Masking | Blending | gpu_unai.PixelMSB;

        if (!FastLightingEnabled()) {
          driver_idx |= Lighting;
        } else {
          if (!((gpu_unai.PacketBuffer.U1[0]>0x5F) && (gpu_unai.PacketBuffer.U1[1]>0x5F) && (gpu_unai.PacketBuffer.U1[2]>0x5F)))
            driver_idx |= Lighting;
        }

        PP driver = gpuPolySpanDrivers[driver_idx];
        gpuDrawPolyFT(packet, driver, false);
      } break;

      case 0x28:
      case 0x29:
      case 0x2A:
      case 0x2B: {          // Monochrome 4-pt poly
        PP driver = gpuPolySpanDrivers[
          (gpu_unai.blit_mask?1024:0) |
          Blending_Mode |
          gpu_unai.Masking | Blending | gpu_unai.PixelMSB
        ];
        gpuDrawPolyF(packet, driver, true); // is_quad = true
      } break;

      case 0x2C:
      case 0x2D:
      case 0x2E:
      case 0x2F: {          // Textured 4-pt poly
        gpuSetCLUT   (gpu_unai.PacketBuffer.U4[2] >> 16);
        gpuSetTexture(gpu_unai.PacketBuffer.U4[4] >> 16);

        u32 driver_idx =
          (gpu_unai.blit_mask?1024:0) |
          Dithering |
          Blending_Mode | gpu_unai.TEXT_MODE |
          gpu_unai.Masking | Blending | gpu_unai.PixelMSB;

        if (!FastLightingEnabled()) {
          driver_idx |= Lighting;
        } else {
          if (!((gpu_unai.PacketBuffer.U1[0]>0x5F) && (gpu_unai.PacketBuffer.U1[1]>0x5F) && (gpu_unai.PacketBuffer.U1[2]>0x5F)))
            driver_idx |= Lighting;
        }

        PP driver = gpuPolySpanDrivers[driver_idx];
        gpuDrawPolyFT(packet, driver, true); // is_quad = true
      } break;

      case 0x30:
      case 0x31:
      case 0x32:
      case 0x33: {          // Gouraud-shaded 3-pt poly
        //NOTE: The '129' here is CF_GOURAUD | CF_LIGHT, however
        // this is an untextured poly, so CF_LIGHT (texture blend)
        // shouldn't apply. Until the original array of template
        // instantiation ptrs is fixed, we're stuck with this. (TODO)
        PP driver = gpuPolySpanDrivers[
          (gpu_unai.blit_mask?1024:0) |
          Dithering |
          Blending_Mode |
          gpu_unai.Masking | Blending | 129 | gpu_unai.PixelMSB
        ];
        gpuDrawPolyG(packet, driver, false);
      } break;

      case 0x34:
      case 0x35:
      case 0x36:
      case 0x37: {          // Gouraud-shaded, textured 3-pt poly
        gpuSetCLUT    (gpu_unai.PacketBuffer.U4[2] >> 16);
        gpuSetTexture (gpu_unai.PacketBuffer.U4[5] >> 16);
        PP driver = gpuPolySpanDrivers[
          (gpu_unai.blit_mask?1024:0) |
          Dithering |
          Blending_Mode | gpu_unai.TEXT_MODE |
          gpu_unai.Masking | Blending | ((Lighting)?129:0) | gpu_unai.PixelMSB
        ];
        gpuDrawPolyGT(packet, driver, false);
      } break;

      case 0x38:
      case 0x39:
      case 0x3A:
      case 0x3B: {          // Gouraud-shaded 4-pt poly
        // See notes regarding '129' for 0x30..0x33 further above -senquack
        PP driver = gpuPolySpanDrivers[
          (gpu_unai.blit_mask?1024:0) |
          Dithering |
          Blending_Mode |
          gpu_unai.Masking | Blending | 129 | gpu_unai.PixelMSB
        ];
        gpuDrawPolyG(packet, driver, true); // is_quad = true
      } break;

      case 0x3C:
      case 0x3D:
      case 0x3E:
      case 0x3F: {          // Gouraud-shaded, textured 4-pt poly
        gpuSetCLUT    (gpu_unai.PacketBuffer.U4[2] >> 16);
        gpuSetTexture (gpu_unai.PacketBuffer.U4[5] >> 16);
        PP driver = gpuPolySpanDrivers[
          (gpu_unai.blit_mask?1024:0) |
          Dithering |
          Blending_Mode | gpu_unai.TEXT_MODE |
          gpu_unai.Masking | Blending | ((Lighting)?129:0) | gpu_unai.PixelMSB
        ];
        gpuDrawPolyGT(packet, driver, true); // is_quad = true
      } break;

      case 0x40:
      case 0x41:
      case 0x42:
      case 0x43: {          // Monochrome line
        // Shift index right by one, as untextured prims don't use lighting
        u32 driver_idx = (Blending_Mode | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>3)) >> 1;
        PSD driver = gpuPixelSpanDrivers[driver_idx];
        gpuDrawLineF(packet, driver);
      } break;

      case 0x48 ... 0x4F: { // Monochrome line strip
        u32 num_vertexes = 1;
        u32 *list_position = &(list[2]);

        // Shift index right by one, as untextured prims don't use lighting
        u32 driver_idx = (Blending_Mode | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>3)) >> 1;
        PSD driver = gpuPixelSpanDrivers[driver_idx];
        gpuDrawLineF(packet, driver);

        while(1)
        {
          gpu_unai.PacketBuffer.U4[1] = gpu_unai.PacketBuffer.U4[2];
          gpu_unai.PacketBuffer.U4[2] = *list_position++;
          gpuDrawLineF(packet, driver);

          num_vertexes++;
          if(list_position >= list_end) {
            cmd = -1;
            goto breakloop;
          }
          if((*list_position & 0xf000f000) == 0x50005000)
            break;
        }

        len += (num_vertexes - 2);
      } break;

      case 0x50:
      case 0x51:
      case 0x52:
      case 0x53: {          // Gouraud-shaded line
        // Shift index right by one, as untextured prims don't use lighting
        u32 driver_idx = (Blending_Mode | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>3)) >> 1;
        // Index MSB selects Gouraud-shaded PixelSpanDriver:
        driver_idx |= (1 << 5);
        PSD driver = gpuPixelSpanDrivers[driver_idx];
        gpuDrawLineG(packet, driver);
      } break;

      case 0x58 ... 0x5F: { // Gouraud-shaded line strip
        u32 num_vertexes = 1;
        u32 *list_position = &(list[2]);

        // Shift index right by one, as untextured prims don't use lighting
        u32 driver_idx = (Blending_Mode | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>3)) >> 1;
        // Index MSB selects Gouraud-shaded PixelSpanDriver:
        driver_idx |= (1 << 5);
        PSD driver = gpuPixelSpanDrivers[driver_idx];
        gpuDrawLineG(packet, driver);

        while(1)
        {
          gpu_unai.PacketBuffer.U4[0] = gpu_unai.PacketBuffer.U4[2];
          gpu_unai.PacketBuffer.U4[1] = gpu_unai.PacketBuffer.U4[3];
          gpu_unai.PacketBuffer.U4[2] = *list_position++;
          gpu_unai.PacketBuffer.U4[3] = *list_position++;
          gpuDrawLineG(packet, driver);

          num_vertexes++;
          if(list_position >= list_end) {
            cmd = -1;
            goto breakloop;
          }
          if((*list_position & 0xf000f000) == 0x50005000)
            break;
        }

        len += (num_vertexes - 2) * 2;
      } break;

      case 0x60:
      case 0x61:
      case 0x62:
      case 0x63: {          // Monochrome rectangle (variable size)
        PT driver = gpuTileSpanDrivers[(Blending_Mode | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>3)) >> 1];
        gpuDrawT(packet, driver);
      } break;

      case 0x64:
      case 0x65:
      case 0x66:
      case 0x67: {          // Textured rectangle (variable size)
        gpuSetCLUT    (gpu_unai.PacketBuffer.U4[2] >> 16);
        u32 driver_idx = Blending_Mode | gpu_unai.TEXT_MODE | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>1);

        //senquack - Only color 808080h-878787h allows skipping lighting calculation:
        // This fixes Silent Hill running animation on loading screens:
        // (On PSX, color values 0x00-0x7F darken the source texture's color,
        //  0x81-FF lighten textures (ultimately clamped to 0x1F),
        //  0x80 leaves source texture color unchanged, HOWEVER,
        //   gpu_unai uses a simple lighting LUT whereby only the upper
        //   5 bits of an 8-bit color are used, so 0x80-0x87 all behave as
        //   0x80.
        // 
        // NOTE: I've changed all textured sprite draw commands here and
        //  elsewhere to use proper behavior, but left poly commands
        //  alone, I don't want to slow rendering down too much. (TODO)
        //if ((gpu_unai.PacketBuffer.U1[0]>0x5F) && (gpu_unai.PacketBuffer.U1[1]>0x5F) && (gpu_unai.PacketBuffer.U1[2]>0x5F))
        // Strip lower 3 bits of each color and determine if lighting should be used:
        if ((gpu_unai.PacketBuffer.U4[0] & 0xF8F8F8) != 0x808080)
          driver_idx |= Lighting;
        PS driver = gpuSpriteSpanDrivers[driver_idx];
        gpuDrawS(packet, driver);
      } break;

      case 0x68:
      case 0x69:
      case 0x6A:
      case 0x6B: {          // Monochrome rectangle (1x1 dot)
        gpu_unai.PacketBuffer.U4[2] = 0x00010001;
        PT driver = gpuTileSpanDrivers[(Blending_Mode | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>3)) >> 1];
        gpuDrawT(packet, driver);
      } break;

      case 0x70:
      case 0x71:
      case 0x72:
      case 0x73: {          // Monochrome rectangle (8x8)
        gpu_unai.PacketBuffer.U4[2] = 0x00080008;
        PT driver = gpuTileSpanDrivers[(Blending_Mode | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>3)) >> 1];
        gpuDrawT(packet, driver);
      } break;

      case 0x74:
      case 0x75:
      case 0x76:
      case 0x77: {          // Textured rectangle (8x8)
        gpu_unai.PacketBuffer.U4[3] = 0x00080008;
        gpuSetCLUT    (gpu_unai.PacketBuffer.U4[2] >> 16);
        u32 driver_idx = Blending_Mode | gpu_unai.TEXT_MODE | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>1);

        //senquack - Only color 808080h-878787h allows skipping lighting calculation:
        //if ((gpu_unai.PacketBuffer.U1[0]>0x5F) && (gpu_unai.PacketBuffer.U1[1]>0x5F) && (gpu_unai.PacketBuffer.U1[2]>0x5F))
        // Strip lower 3 bits of each color and determine if lighting should be used:
        if ((gpu_unai.PacketBuffer.U4[0] & 0xF8F8F8) != 0x808080)
          driver_idx |= Lighting;
        PS driver = gpuSpriteSpanDrivers[driver_idx];
        gpuDrawS(packet, driver);
      } break;

      case 0x78:
      case 0x79:
      case 0x7A:
      case 0x7B: {          // Monochrome rectangle (16x16)
        gpu_unai.PacketBuffer.U4[2] = 0x00100010;
        PT driver = gpuTileSpanDrivers[(Blending_Mode | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>3)) >> 1];
        gpuDrawT(packet, driver);
      } break;

      case 0x7C:
      case 0x7D:
#ifdef __arm__
        if ((gpu_unai.GPU_GP1 & 0x180) == 0 && (gpu_unai.Masking | gpu_unai.PixelMSB) == 0)
        {
          gpuSetCLUT    (gpu_unai.PacketBuffer.U4[2] >> 16);
          gpuDrawS16(packet);
          break;
        }
        // fallthrough
#endif
      case 0x7E:
      case 0x7F: {          // Textured rectangle (16x16)
        gpu_unai.PacketBuffer.U4[3] = 0x00100010;
        gpuSetCLUT    (gpu_unai.PacketBuffer.U4[2] >> 16);
        u32 driver_idx = Blending_Mode | gpu_unai.TEXT_MODE | gpu_unai.Masking | Blending | (gpu_unai.PixelMSB>>1);
        //senquack - Only color 808080h-878787h allows skipping lighting calculation:
        //if ((gpu_unai.PacketBuffer.U1[0]>0x5F) && (gpu_unai.PacketBuffer.U1[1]>0x5F) && (gpu_unai.PacketBuffer.U1[2]>0x5F))
        // Strip lower 3 bits of each color and determine if lighting should be used:
        if ((gpu_unai.PacketBuffer.U4[0] & 0xF8F8F8) != 0x808080)
          driver_idx |= Lighting;
        PS driver = gpuSpriteSpanDrivers[driver_idx];
        gpuDrawS(packet, driver);
      } break;

      case 0x80:          //  vid -> vid
        gpuMoveImage(packet);
        break;

#ifdef TEST
      case 0xA0:          //  sys -> vid
      {
        u32 load_width = list[2] & 0xffff;
        u32 load_height = list[2] >> 16;
        u32 load_size = load_width * load_height;

        len += load_size / 2;
      } break;

      case 0xC0:
        break;
#else
      case 0xA0:          //  sys ->vid
      case 0xC0:          //  vid -> sys
        // Handled by gpulib
        goto breakloop;
#endif
      case 0xE1 ... 0xE6: { // Draw settings
        gpuGP0Cmd_0xEx(gpu_unai, gpu_unai.PacketBuffer.U4[0]);
      } break;
    }
  }

breakloop:
  gpu.ex_regs[1] &= ~0x1ff;
  gpu.ex_regs[1] |= gpu_unai.GPU_GP1 & 0x1ff;

  *last_cmd = cmd;
  return list - list_start;
}

void renderer_sync_ecmds(uint32_t *ecmds)
{
  int dummy;
  do_cmd_list(&ecmds[1], 6, &dummy);
}

void renderer_update_caches(int x, int y, int w, int h)
{
}

void renderer_flush_queues(void)
{
}

void renderer_set_interlace(int enable, int is_odd)
{
}

#include "../../frontend/plugin_lib.h"
// Handle any gpulib settings applicable to gpu_unai:
void renderer_set_config(const struct rearmed_cbs *cbs)
{
  gpu_unai.vram = (u16*)gpu.vram;
  gpu_unai.config.ilace_force   = cbs->gpu_unai.ilace_force;
  gpu_unai.config.pixel_skip    = cbs->gpu_unai.pixel_skip;
  gpu_unai.config.lighting      = cbs->gpu_unai.lighting;
  gpu_unai.config.fast_lighting = cbs->gpu_unai.fast_lighting;
  gpu_unai.config.blending      = cbs->gpu_unai.blending;
  gpu_unai.config.dithering     = cbs->gpu_unai.dithering;
  gpu_unai.config.scale_hires   = cbs->gpu_unai.scale_hires;

  gpu.state.downscale_enable    = gpu_unai.config.scale_hires;
  if (gpu_unai.config.scale_hires) {
    map_downscale_buffer();
  } else {
    unmap_downscale_buffer();
  }
}

// vim:shiftwidth=2:expandtab