Source

mupen64plus-video-glide64mk2 / src / Glide64 / FBtoScreen.cpp

Full commit
  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
/*
* Glide64 - Glide video plugin for Nintendo 64 emulators.
* Copyright (c) 2002  Dave2001
* Copyright (c) 2003-2009  Sergey 'Gonetz' Lipski
*
* 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
* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

//****************************************************************
//
// Glide64 - Glide Plugin for Nintendo 64 emulators
// Project started on December 29th, 2001
//
// Authors:
// Dave2001, original author, founded the project in 2001, left it in 2002
// Gugaman, joined the project in 2002, left it in 2002
// Sergey 'Gonetz' Lipski, joined the project in 2002, main author since fall of 2002
// Hiroshi 'KoolSmoky' Morii, joined the project in 2007
//
//****************************************************************
//
// To modify Glide64:
// * Write your name and (optional)email, commented by your work, so I know who did it, and so that you can find which parts you modified when it comes time to send it to me.
// * Do NOT send me the whole project or file that you modified.  Take out your modified code sections, and tell me where to put them.  If people sent the whole thing, I would have many different versions, but no idea how to combine them all.
//
//****************************************************************
//
// Draw N64 frame buffer to screen.
// Created by Gonetz, 2007
//
//****************************************************************


#include "Gfx_1.3.h"
#include "FBtoScreen.h"
#include "TexCache.h"

static int SetupFBtoScreenCombiner(wxUint32 texture_size, wxUint32 opaque)
{
  int tmu;
  if (voodoo.tmem_ptr[GR_TMU0]+texture_size < voodoo.tex_max_addr[0])
  {
    tmu = GR_TMU0;
    grTexCombine( GR_TMU1,
      GR_COMBINE_FUNCTION_NONE,
      GR_COMBINE_FACTOR_NONE,
      GR_COMBINE_FUNCTION_NONE,
      GR_COMBINE_FACTOR_NONE,
      FXFALSE,
      FXFALSE );
    grTexCombine( GR_TMU0,
      GR_COMBINE_FUNCTION_LOCAL,
      GR_COMBINE_FACTOR_NONE,
      GR_COMBINE_FUNCTION_LOCAL,
      GR_COMBINE_FACTOR_NONE,
      FXFALSE,
      FXFALSE );
  }
  else
  {
    if (voodoo.tmem_ptr[GR_TMU1]+texture_size >= voodoo.tex_max_addr[1])
      ClearCache ();
    tmu = GR_TMU1;
    grTexCombine( GR_TMU1,
      GR_COMBINE_FUNCTION_LOCAL,
      GR_COMBINE_FACTOR_NONE,
      GR_COMBINE_FUNCTION_LOCAL,
      GR_COMBINE_FACTOR_NONE,
      FXFALSE,
      FXFALSE );
    grTexCombine( GR_TMU0,
      GR_COMBINE_FUNCTION_SCALE_OTHER,
      GR_COMBINE_FACTOR_ONE,
      GR_COMBINE_FUNCTION_SCALE_OTHER,
      GR_COMBINE_FACTOR_ONE,
      FXFALSE,
      FXFALSE );
  }
  int filter = (rdp.filter_mode!=2)?GR_TEXTUREFILTER_POINT_SAMPLED:GR_TEXTUREFILTER_BILINEAR;
  grTexFilterMode (tmu, filter, filter);
  grTexClampMode (tmu,
        GR_TEXTURECLAMP_CLAMP,
        GR_TEXTURECLAMP_CLAMP);
//  grConstantColorValue (0xFFFFFFFF);
  grColorCombine (GR_COMBINE_FUNCTION_SCALE_OTHER,
    GR_COMBINE_FACTOR_ONE,
    GR_COMBINE_LOCAL_NONE,
    GR_COMBINE_OTHER_TEXTURE,
//    GR_COMBINE_OTHER_CONSTANT,
    FXFALSE);
  grAlphaCombine (GR_COMBINE_FUNCTION_SCALE_OTHER,
    GR_COMBINE_FACTOR_ONE,
    GR_COMBINE_LOCAL_NONE,
    GR_COMBINE_OTHER_TEXTURE,
    FXFALSE);
  if (opaque)
  {
    grAlphaTestFunction (GR_CMP_ALWAYS);
    grAlphaBlendFunction( GR_BLEND_ONE,
      GR_BLEND_ZERO,
      GR_BLEND_ONE,
      GR_BLEND_ZERO);
  }
  else
  {
    grAlphaBlendFunction( GR_BLEND_SRC_ALPHA,
      GR_BLEND_ONE_MINUS_SRC_ALPHA,
      GR_BLEND_ONE,
      GR_BLEND_ZERO);
  }
  grDepthBufferFunction (GR_CMP_ALWAYS);
  grCullMode(GR_CULL_DISABLE);
  grDepthMask (FXFALSE);
  rdp.update |= UPDATE_COMBINE | UPDATE_ZBUF_ENABLED | UPDATE_CULL_MODE;
  return tmu;
}

static void DrawRE2Video(FB_TO_SCREEN_INFO & fb_info, float scale)
{
  float scale_y = (float)fb_info.width/rdp.vi_height;
  float height = settings.scr_res_x/scale_y;
  float ul_x = 0.5f;
  float ul_y = (settings.scr_res_y - height)/2.0f;
  float lr_y = settings.scr_res_y - ul_y - 1.0f;
  float lr_x = settings.scr_res_x - 1.0f;
  float lr_u = (fb_info.width - 1)*scale;
  float lr_v = (fb_info.height - 1)*scale;
  VERTEX v[4] = {
    { ul_x, ul_y, 1, 1, 0.5f, 0.5f, 0.5f, 0.5f, {0.5f, 0.5f, 0.5f, 0.5f} },
    { lr_x, ul_y, 1, 1, lr_u, 0.5f, lr_u, 0.5f, {lr_u, 0.5f, lr_u, 0.5f} },
    { ul_x, lr_y, 1, 1, 0.5f, lr_v, 0.5f, lr_v, {0.5f, lr_v, 0.5f, lr_v} },
    { lr_x, lr_y, 1, 1, lr_u, lr_v, lr_u, lr_v, {lr_u, lr_v, lr_u, lr_v} }
  };
  grDrawTriangle (&v[0], &v[2], &v[1]);
  grDrawTriangle (&v[2], &v[3], &v[1]);
}

static void DrawRE2Video256(FB_TO_SCREEN_INFO & fb_info)
{
  FRDP("DrawRE2Video256. ul_x=%d, ul_y=%d, lr_x=%d, lr_y=%d, size=%d, addr=%08lx\n", fb_info.ul_x, fb_info.ul_y, fb_info.lr_x, fb_info.lr_y, fb_info.size, fb_info.addr);
  wxUint32 * src = (wxUint32*)(gfx.RDRAM+fb_info.addr);
  GrTexInfo t_info;
  t_info.smallLodLog2 = GR_LOD_LOG2_256;
  t_info.largeLodLog2 = GR_LOD_LOG2_256;
  t_info.aspectRatioLog2 = GR_ASPECT_LOG2_1x1;
  wxUint16 * tex = (wxUint16*)texture_buffer;
  wxUint16 * dst = tex;
  wxUint32 col;
  wxUint8 r, g, b;
  fb_info.height = min(256, fb_info.height);
  for (wxUint32 h = 0; h < fb_info.height; h++)
  {
    for (wxUint32 w = 0; w < 256; w++)
    {
      col = *(src++);
      r = (wxUint8)((col >> 24)&0xFF);
      r = (wxUint8)((float)r / 255.0f * 31.0f);
      g = (wxUint8)((col >> 16)&0xFF);
      g = (wxUint8)((float)g / 255.0f * 63.0f);
      b = (wxUint8)((col >>  8)&0xFF);
      b = (wxUint8)((float)b / 255.0f * 31.0f);
      *(dst++) = (r << 11) | (g << 5) | b;
    }
    src += (fb_info.width - 256);
  }
  t_info.format = GR_TEXFMT_RGB_565;
  t_info.data = tex;
  int tmu = SetupFBtoScreenCombiner(grTexTextureMemRequired(GR_MIPMAPLEVELMASK_BOTH, &t_info), fb_info.opaque);
  grTexDownloadMipMap (tmu,
    voodoo.tex_min_addr[tmu]+voodoo.tmem_ptr[tmu],
    GR_MIPMAPLEVELMASK_BOTH,
    &t_info);
  grTexSource (tmu,
      voodoo.tex_min_addr[tmu]+voodoo.tmem_ptr[tmu],
      GR_MIPMAPLEVELMASK_BOTH,
      &t_info);
  DrawRE2Video(fb_info, 1.0f);
}

static void DrawFrameBufferToScreen256(FB_TO_SCREEN_INFO & fb_info)
{
  if (settings.hacks&hack_RE2)
  {
    DrawRE2Video256(fb_info);
    return;
  }
  FRDP("DrawFrameBufferToScreen256. ul_x=%d, ul_y=%d, lr_x=%d, lr_y=%d, size=%d, addr=%08lx\n", fb_info.ul_x, fb_info.ul_y, fb_info.lr_x, fb_info.lr_y, fb_info.size, fb_info.addr);
  wxUint32 width = fb_info.lr_x - fb_info.ul_x + 1;
  wxUint32 height = fb_info.lr_y - fb_info.ul_y + 1;
  GrTexInfo t_info;
  wxUint8 * image = gfx.RDRAM+fb_info.addr;
  wxUint32 width256 = ((width-1) >> 8) + 1;
  wxUint32 height256 = ((height-1) >> 8) + 1;
  t_info.smallLodLog2 = t_info.largeLodLog2 = GR_LOD_LOG2_256;
  t_info.aspectRatioLog2 = GR_ASPECT_LOG2_1x1;
  t_info.format = GR_TEXFMT_ARGB_1555;
  wxUint16 * tex = (wxUint16*)texture_buffer;
  t_info.data = tex;
  wxUint32 tex_size = grTexTextureMemRequired (GR_MIPMAPLEVELMASK_BOTH, &t_info);
  int tmu = SetupFBtoScreenCombiner(tex_size*width256*height256, fb_info.opaque);
  wxUint16 * src = (wxUint16*)image;
  src += fb_info.ul_x + fb_info.ul_y * fb_info.width;
  wxUint32 * src32 = (wxUint32*)image;
  src32 += fb_info.ul_x + fb_info.ul_y * fb_info.width;
  wxUint32 w_tail = width%256;
  wxUint32 h_tail = height%256;
  wxUint16 c;
  wxUint32 c32;
  wxUint32 idx;
  wxUint32 bound = BMASK+1-fb_info.addr;
  bound = fb_info.size == 2 ? bound >> 1 : bound >> 2;
  wxUint8 r, g, b, a;
  wxUint32 cur_width, cur_height, cur_tail;
  wxUint32 tex_adr = voodoo.tex_min_addr[tmu]+voodoo.tmem_ptr[tmu];
  if ((voodoo.tmem_ptr[tmu] < TEXMEM_2MB_EDGE) && (voodoo.tmem_ptr[tmu]+tex_size*width256*height256 > TEXMEM_2MB_EDGE))
  {
    tex_adr = TEXMEM_2MB_EDGE;
  }
  for (wxUint32 h = 0; h < height256; h++)
  {
    for (wxUint32 w = 0; w < width256; w++)
    {
      cur_width = (256*(w+1) < width) ? 256 : w_tail;
      cur_height = (256*(h+1) < height) ? 256 : h_tail;
      cur_tail = 256 - cur_width;
      wxUint16 * dst = tex;
      if (fb_info.size == 2)
      {
        for (wxUint32 y=0; y < cur_height; y++)
        {
          for (wxUint32 x=0; x < cur_width; x++)
          {
            idx = (x+256*w+(y+256*h)*fb_info.width)^1;
            if (idx >= bound)
              break;
            c = src[idx];
            *(dst++) = (c >> 1) | ((c&1)<<15);
          }
          dst += cur_tail;
        }
      }
      else
      {
        for (wxUint32 y=0; y < cur_height; y++)
        {
          for (wxUint32 x=0; x < cur_width; x++)
          {
            idx = (x+256*w+(y+256*h)*fb_info.width);
            if (idx >= bound)
              break;
            c32 = src32[idx];
            r = (wxUint8)((c32 >> 24)&0xFF);
            r = (wxUint8)((float)r / 255.0f * 31.0f);
            g = (wxUint8)((c32 >> 16)&0xFF);
            g = (wxUint8)((float)g / 255.0f * 63.0f);
            b = (wxUint8)((c32 >>  8)&0xFF);
            b = (wxUint8)((float)b / 255.0f * 31.0f);
            a = (c32&0xFF) ? 1 : 0;
            *(dst++) = (a<<15) | (r << 10) | (g << 5) | b;
          }
          dst += cur_tail;
        }
      }
      grTexDownloadMipMap (tmu, tex_adr, GR_MIPMAPLEVELMASK_BOTH, &t_info);
      grTexSource (tmu, tex_adr, GR_MIPMAPLEVELMASK_BOTH, &t_info);
      tex_adr += tex_size;
      float ul_x = (float)(fb_info.ul_x + 256*w);
      float ul_y = (float)(fb_info.ul_y + 256*h);
      float lr_x = (ul_x + (float)(cur_width)) * rdp.scale_x;
      float lr_y = (ul_y + (float)(cur_height)) * rdp.scale_y;
      ul_x *= rdp.scale_x;
      ul_y *= rdp.scale_y;
      ul_x += rdp.offset_x;
      ul_y += rdp.offset_y;
      lr_x += rdp.offset_x;
      lr_y += rdp.offset_y;

      float lr_u = (float)(cur_width-1);
      float lr_v = (float)(cur_height-1);
      // Make the vertices
      VERTEX v[4] = {
        { ul_x, ul_y, 1, 1, 0.5f, 0.5f, 0.5f, 0.5f, {0.5f, 0.5f, 0.5f, 0.5f} },
        { lr_x, ul_y, 1, 1, lr_u, 0.5f, lr_u, 0.5f, {lr_u, 0.5f, lr_u, 0.5f} },
        { ul_x, lr_y, 1, 1, 0.5f, lr_v, 0.5f, lr_v, {0.5f, lr_v, 0.5f, lr_v} },
        { lr_x, lr_y, 1, 1, lr_u, lr_v, lr_u, lr_v, {lr_u, lr_v, lr_u, lr_v} }
      };
      grDrawTriangle (&v[0], &v[2], &v[1]);
      grDrawTriangle (&v[2], &v[3], &v[1]);
    }
  }
}

bool DrawFrameBufferToScreen(FB_TO_SCREEN_INFO & fb_info)
{
  if (fb_info.width < 200 || fb_info.size < 2)
    return false;
  wxUint32 width = fb_info.lr_x - fb_info.ul_x + 1;
  wxUint32 height = fb_info.lr_y - fb_info.ul_y + 1;
  wxUint32 max_size = min(voodoo.max_tex_size, 512);
  if (width > (wxUint32)max_size || height > (wxUint32)max_size)
  {
    DrawFrameBufferToScreen256(fb_info);
    return true;
  }
  FRDP("DrawFrameBufferToScreen. ul_x=%d, ul_y=%d, lr_x=%d, lr_y=%d, size=%d, addr=%08lx\n", fb_info.ul_x, fb_info.ul_y, fb_info.lr_x, fb_info.lr_y, fb_info.size, fb_info.addr);
  GrTexInfo t_info;
  wxUint8 * image = gfx.RDRAM+fb_info.addr;
  wxUint32 texwidth;
  float scale;
  if (width <= 256)
  {
    texwidth = 256;
    scale = 1.0f;
    t_info.smallLodLog2 = t_info.largeLodLog2 = GR_LOD_LOG2_256;
  }
  else
  {
    texwidth = 512;
    scale = 0.5f;
    t_info.smallLodLog2 = t_info.largeLodLog2 = GR_LOD_LOG2_512;
  }

  if (height <= (texwidth>>1))
  {
    t_info.aspectRatioLog2 = GR_ASPECT_LOG2_2x1;
  }
  else
  {
    t_info.aspectRatioLog2 = GR_ASPECT_LOG2_1x1;
  }

  if (fb_info.size == 2)
  {
    wxUint16 * tex = (wxUint16*)texture_buffer;
    wxUint16 * dst = tex;
    wxUint16 * src = (wxUint16*)image;
    src += fb_info.ul_x + fb_info.ul_y * fb_info.width;
    wxUint16 c;
    wxUint32 idx;
    const wxUint32 bound = (BMASK+1-fb_info.addr) >> 1;
    bool empty = true;
    for (wxUint32 y=0; y < height; y++)
    {
      for (wxUint32 x=0; x < width; x++)
      {
        idx = (x+y*fb_info.width)^1;
        if (idx >= bound)
          break;
        c = src[idx];
        if (c) empty = false;
        *(dst++) = (c >> 1) | ((c&1)<<15);
      }
      dst += texwidth-width;
    }
    if (empty)
      return false;
    t_info.format = GR_TEXFMT_ARGB_1555;
    t_info.data = tex;
  }
  else
  {
    wxUint32 * tex = (wxUint32*)texture_buffer;
    wxUint32 * dst = tex;
    wxUint32 * src = (wxUint32*)image;
    src += fb_info.ul_x + fb_info.ul_y * fb_info.width;
    wxUint32 col;
    wxUint32 idx;
    const wxUint32 bound = (BMASK+1-fb_info.addr) >> 2;
    for (wxUint32 y=0; y < height; y++)
    {
      for (wxUint32 x=0; x < width; x++)
      {
        idx = x+y*fb_info.width;
        if (idx >= bound)
          break;
        col = src[idx];
        *(dst++) = (col >> 8) | 0xFF000000;
      }
      dst += texwidth-width;
    }
    t_info.format = GR_TEXFMT_ARGB_8888;
    t_info.data = tex;
  }

  int tmu = SetupFBtoScreenCombiner(grTexTextureMemRequired(GR_MIPMAPLEVELMASK_BOTH, &t_info), fb_info.opaque);
  grTexDownloadMipMap (tmu,
    voodoo.tex_min_addr[tmu]+voodoo.tmem_ptr[tmu],
    GR_MIPMAPLEVELMASK_BOTH,
    &t_info);
  grTexSource (tmu,
    voodoo.tex_min_addr[tmu]+voodoo.tmem_ptr[tmu],
    GR_MIPMAPLEVELMASK_BOTH,
    &t_info);
  if (settings.hacks&hack_RE2)
  {
    DrawRE2Video(fb_info, scale);
  }
  else
  {
    float ul_x = fb_info.ul_x * rdp.scale_x + rdp.offset_x;
    float ul_y = fb_info.ul_y * rdp.scale_y + rdp.offset_y;
    float lr_x = fb_info.lr_x * rdp.scale_x + rdp.offset_x;
    float lr_y = fb_info.lr_y * rdp.scale_y + rdp.offset_y;
    float lr_u = (width-1)*scale;
    float lr_v = (height-1)*scale;
    // Make the vertices
    VERTEX v[4] = {
      { ul_x, ul_y, 1, 1, 0.5f, 0.5f, 0.5f, 0.5f, {0.5f, 0.5f, 0.5f, 0.5f} },
      { lr_x, ul_y, 1, 1, lr_u, 0.5f, lr_u, 0.5f, {lr_u, 0.5f, lr_u, 0.5f} },
      { ul_x, lr_y, 1, 1, 0.5f, lr_v, 0.5f, lr_v, {0.5f, lr_v, 0.5f, lr_v} },
      { lr_x, lr_y, 1, 1, lr_u, lr_v, lr_u, lr_v, {lr_u, lr_v, lr_u, lr_v} }
    };
    grDrawTriangle (&v[0], &v[2], &v[1]);
    grDrawTriangle (&v[2], &v[3], &v[1]);
  }
  return true;
}

static void DrawDepthBufferToScreen256(FB_TO_SCREEN_INFO & fb_info)
{
  FRDP("DrawDepthBufferToScreen256. ul_x=%d, ul_y=%d, lr_x=%d, lr_y=%d, size=%d, addr=%08lx\n", fb_info.ul_x, fb_info.ul_y, fb_info.lr_x, fb_info.lr_y, fb_info.size, fb_info.addr);
  wxUint32 width = fb_info.lr_x - fb_info.ul_x + 1;
  wxUint32 height = fb_info.lr_y - fb_info.ul_y + 1;
  GrTexInfo t_info;
  wxUint8 * image = gfx.RDRAM+fb_info.addr;
  wxUint32 width256 = ((width-1) >> 8) + 1;
  wxUint32 height256 = ((height-1) >> 8) + 1;
  t_info.smallLodLog2 = t_info.largeLodLog2 = GR_LOD_LOG2_256;
  t_info.aspectRatioLog2 = GR_ASPECT_LOG2_1x1;
  t_info.format = GR_TEXFMT_ALPHA_INTENSITY_88;
  wxUint16 * tex = (wxUint16*)texture_buffer;
  t_info.data = tex;
  wxUint32 tex_size = grTexTextureMemRequired (GR_MIPMAPLEVELMASK_BOTH, &t_info);
  int tmu = SetupFBtoScreenCombiner(tex_size*width256*height256, fb_info.opaque);
  grConstantColorValue (rdp.fog_color);
  grColorCombine (GR_COMBINE_FUNCTION_SCALE_OTHER,
    GR_COMBINE_FACTOR_ONE,
    GR_COMBINE_LOCAL_NONE,
    GR_COMBINE_OTHER_CONSTANT,
    FXFALSE);
  wxUint16 * src = (wxUint16*)image;
  src += fb_info.ul_x + fb_info.ul_y * fb_info.width;
  wxUint32 w_tail = width%256;
  wxUint32 h_tail = height%256;
  wxUint32 cur_width, cur_height, cur_tail;
  wxUint32 tex_adr = voodoo.tex_min_addr[tmu]+voodoo.tmem_ptr[tmu];
  if ((voodoo.tmem_ptr[tmu] < TEXMEM_2MB_EDGE) && (voodoo.tmem_ptr[tmu]+tex_size*width256*height256 > TEXMEM_2MB_EDGE))
  {
    tex_adr = TEXMEM_2MB_EDGE;
  }
  for (wxUint32 h = 0; h < height256; h++)
  {
    for (wxUint32 w = 0; w < width256; w++)
    {
      cur_width = (256*(w+1) < width) ? 256 : w_tail;
      cur_height = (256*(h+1) < height) ? 256 : h_tail;
      cur_tail = 256 - cur_width;
      wxUint16 * dst = tex;
      for (wxUint32 y=0; y < cur_height; y++)
      {
        for (wxUint32 x=0; x < cur_width; x++)
        {
          *(dst++) = rdp.pal_8[src[(x+256*w+(y+256*h)*fb_info.width)^1]>>8];
        }
        dst += cur_tail;
      }
      grTexDownloadMipMap (tmu, tex_adr, GR_MIPMAPLEVELMASK_BOTH, &t_info);
      grTexSource (tmu, tex_adr, GR_MIPMAPLEVELMASK_BOTH, &t_info);
      tex_adr += tex_size;
      float ul_x = (float)(fb_info.ul_x + 256*w);
      float ul_y = (float)(fb_info.ul_y + 256*h);
      float lr_x = (ul_x + (float)(cur_width)) * rdp.scale_x + rdp.offset_x;
      float lr_y = (ul_y + (float)(cur_height)) * rdp.scale_y + rdp.offset_y;
      ul_x = ul_x * rdp.scale_x + rdp.offset_x;
      ul_y = ul_y * rdp.scale_y + rdp.offset_y;
      float lr_u = (float)(cur_width-1);
      float lr_v = (float)(cur_height-1);
      // Make the vertices
      VERTEX v[4] = {
        { ul_x, ul_y, 1, 1, 0.5f, 0.5f, 0.5f, 0.5f, {0.5f, 0.5f, 0.5f, 0.5f} },
        { lr_x, ul_y, 1, 1, lr_u, 0.5f, lr_u, 0.5f, {lr_u, 0.5f, lr_u, 0.5f} },
        { ul_x, lr_y, 1, 1, 0.5f, lr_v, 0.5f, lr_v, {0.5f, lr_v, 0.5f, lr_v} },
        { lr_x, lr_y, 1, 1, lr_u, lr_v, lr_u, lr_v, {lr_u, lr_v, lr_u, lr_v} }
      };
      grDrawTriangle (&v[0], &v[2], &v[1]);
      grDrawTriangle (&v[2], &v[3], &v[1]);
    }
  }
}

static void DrawHiresDepthBufferToScreen(FB_TO_SCREEN_INFO & fb_info)
{
  FRDP("DrawHiresDepthBufferToScreen. ul_x=%d, ul_y=%d, lr_x=%d, lr_y=%d, size=%d, addr=%08lx\n", fb_info.ul_x, fb_info.ul_y, fb_info.lr_x, fb_info.lr_y, fb_info.size, fb_info.addr);
  GrTexInfo t_info;
  float scale = 0.25f;
  GrLOD_t LOD = GR_LOD_LOG2_1024;
  if (settings.scr_res_x > 1024)
  {
    scale = 0.125f;
    LOD = GR_LOD_LOG2_2048;
  }
  t_info.format = GR_TEXFMT_ALPHA_INTENSITY_88;
  t_info.smallLodLog2 = t_info.largeLodLog2 = LOD;
  t_info.aspectRatioLog2 = GR_ASPECT_LOG2_1x1;
  grConstantColorValue (rdp.fog_color);
  grColorCombine (GR_COMBINE_FUNCTION_LOCAL,
    GR_COMBINE_FACTOR_NONE,
    GR_COMBINE_LOCAL_CONSTANT,
    GR_COMBINE_OTHER_NONE,
    FXFALSE);
  grAlphaCombine (GR_COMBINE_FUNCTION_SCALE_OTHER,
    GR_COMBINE_FACTOR_ONE,
    GR_COMBINE_LOCAL_NONE,
    GR_COMBINE_OTHER_TEXTURE,
    FXFALSE);
  grAlphaBlendFunction( GR_BLEND_SRC_ALPHA,
    GR_BLEND_ONE_MINUS_SRC_ALPHA,
    GR_BLEND_ONE,
    GR_BLEND_ZERO);
  grDepthBufferFunction (GR_CMP_ALWAYS);
  grDepthMask (FXFALSE);
  grCullMode (GR_CULL_DISABLE);
  grTexCombine( GR_TMU1,
    GR_COMBINE_FUNCTION_NONE,
    GR_COMBINE_FACTOR_NONE,
    GR_COMBINE_FUNCTION_NONE,
    GR_COMBINE_FACTOR_NONE,
    FXFALSE,
    FXFALSE );
  grTexCombine( GR_TMU0,
    GR_COMBINE_FUNCTION_LOCAL,
    GR_COMBINE_FACTOR_NONE,
    GR_COMBINE_FUNCTION_LOCAL,
    GR_COMBINE_FACTOR_NONE,
    FXFALSE,
    FXFALSE);
//  grAuxBufferExt( GR_BUFFER_AUXBUFFER );
  grTexSource( rdp.texbufs[0].tmu, rdp.texbufs[0].begin, GR_MIPMAPLEVELMASK_BOTH, &(t_info) );
  float ul_x = (float)rdp.scissor.ul_x;
  float ul_y = (float)rdp.scissor.ul_y;
  float lr_x = (float)rdp.scissor.lr_x;
  float lr_y = (float)rdp.scissor.lr_y;
  float ul_u = (float)rdp.scissor.ul_x * scale;
  float ul_v = (float)rdp.scissor.ul_y * scale;
  float lr_u = (float)rdp.scissor.lr_x * scale;
  float lr_v = (float)rdp.scissor.lr_y * scale;
  // Make the vertices
  VERTEX v[4] = {
    { ul_x, ul_y, 1, 1, ul_u, ul_v, ul_u, ul_v, {ul_u, ul_v, ul_u, ul_v} },
    { lr_x, ul_y, 1, 1, lr_u, ul_v, lr_u, ul_v, {lr_u, ul_v, lr_u, ul_v} },
    { ul_x, lr_y, 1, 1, ul_u, lr_v, ul_u, lr_v, {ul_u, lr_v, ul_u, lr_v} },
    { lr_x, lr_y, 1, 1, lr_u, lr_v, lr_u, lr_v, {lr_u, lr_v, lr_u, lr_v} }
  };
  grDrawTriangle (&v[0], &v[2], &v[1]);
  grDrawTriangle (&v[2], &v[3], &v[1]);
//  grAuxBufferExt( GR_BUFFER_TEXTUREAUXBUFFER_EXT );
  rdp.update |= UPDATE_COMBINE | UPDATE_ZBUF_ENABLED | UPDATE_CULL_MODE;
}

void DrawDepthBufferToScreen(FB_TO_SCREEN_INFO & fb_info)
{
  wxUint32 width = fb_info.lr_x - fb_info.ul_x + 1;
  wxUint32 height = fb_info.lr_y - fb_info.ul_y + 1;
  if (width > (wxUint32)voodoo.max_tex_size || height > (wxUint32)voodoo.max_tex_size || width > 512)
  {
    DrawDepthBufferToScreen256(fb_info);
    return;
  }
  if (fb_hwfbe_enabled && !evoodoo)
  {
    DrawHiresDepthBufferToScreen(fb_info);
    return;
  }
  FRDP("DrawDepthBufferToScreen. ul_x=%d, ul_y=%d, lr_x=%d, lr_y=%d, size=%d, addr=%08lx\n", fb_info.ul_x, fb_info.ul_y, fb_info.lr_x, fb_info.lr_y, fb_info.size, fb_info.addr);
  GrTexInfo t_info;
  wxUint8 * image = gfx.RDRAM+fb_info.addr;
  wxUint32 texwidth;
  float scale;
  if (width <= 256)
  {
    texwidth = 256;
    scale = 1.0f;
    t_info.smallLodLog2 = t_info.largeLodLog2 = GR_LOD_LOG2_256;
  }
  else
  {
    texwidth = 512;
    scale = 0.5f;
    t_info.smallLodLog2 = t_info.largeLodLog2 = GR_LOD_LOG2_512;
  }

  if (height <= (texwidth>>1))
  {
    t_info.aspectRatioLog2 = GR_ASPECT_LOG2_2x1;
  }
  else
  {
    t_info.aspectRatioLog2 = GR_ASPECT_LOG2_1x1;
  }

  wxUint16 * tex = (wxUint16*)texture_buffer;
  wxUint16 * dst = tex;
  wxUint16 * src = (wxUint16*)image;
  src += fb_info.ul_x + fb_info.ul_y * fb_info.width;
  for (wxUint32 y=0; y < height; y++)
  {
    for (wxUint32 x=0; x < width; x++)
    {
      *(dst++) = rdp.pal_8[src[(x+y*fb_info.width)^1]>>8];
    }
    dst += texwidth-width;
  }
  t_info.format = GR_TEXFMT_ALPHA_INTENSITY_88;
  t_info.data = tex;

  int tmu = SetupFBtoScreenCombiner(grTexTextureMemRequired(GR_MIPMAPLEVELMASK_BOTH, &t_info), fb_info.opaque);
  grConstantColorValue (rdp.fog_color);
  grColorCombine (GR_COMBINE_FUNCTION_SCALE_OTHER,
    GR_COMBINE_FACTOR_ONE,
    GR_COMBINE_LOCAL_NONE,
    GR_COMBINE_OTHER_CONSTANT,
    FXFALSE);
  grTexDownloadMipMap (tmu,
    voodoo.tex_min_addr[tmu]+voodoo.tmem_ptr[tmu],
    GR_MIPMAPLEVELMASK_BOTH,
    &t_info);
  grTexSource (tmu,
    voodoo.tex_min_addr[tmu]+voodoo.tmem_ptr[tmu],
    GR_MIPMAPLEVELMASK_BOTH,
    &t_info);
  float ul_x = fb_info.ul_x * rdp.scale_x + rdp.offset_x;
  float ul_y = fb_info.ul_y * rdp.scale_y + rdp.offset_y;
  float lr_x = fb_info.lr_x * rdp.scale_x + rdp.offset_x;
  float lr_y = fb_info.lr_y * rdp.scale_y + rdp.offset_y;
  float lr_u = (width-1)*scale;
  float lr_v = (height-1)*scale;
  float zero = scale*0.5f;
  // Make the vertices
  VERTEX v[4] = {
    { ul_x, ul_y, 1, 1, zero, zero, zero, zero, {zero, zero, zero, zero} },
    { lr_x, ul_y, 1, 1, lr_u, zero, lr_u, zero, {lr_u, zero, lr_u, zero} },
    { ul_x, lr_y, 1, 1, zero, lr_v, zero, lr_v, {zero, lr_v, zero, lr_v} },
    { lr_x, lr_y, 1, 1, lr_u, lr_v, lr_u, lr_v, {lr_u, lr_v, lr_u, lr_v} }
  };
  grDrawTriangle (&v[0], &v[2], &v[1]);
  grDrawTriangle (&v[2], &v[3], &v[1]);
}