summaryrefslogtreecommitdiffstats
path: root/src/mesa/swrast/s_blit.c
blob: cf5222edbc15f03fb3a9eb420a632ec5c9aa44cb (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
/*
 * Mesa 3-D graphics library
 * Version:  6.5
 *
 * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#include "main/glheader.h"
#include "main/condrender.h"
#include "main/image.h"
#include "main/macros.h"
#include "main/format_unpack.h"
#include "main/format_pack.h"
#include "s_context.h"


#define ABS(X)   ((X) < 0 ? -(X) : (X))


/**
 * Generate a row resampler function for GL_NEAREST mode.
 */
#define RESAMPLE(NAME, PIXELTYPE, SIZE)			\
static void						\
NAME(GLint srcWidth, GLint dstWidth,			\
     const GLvoid *srcBuffer, GLvoid *dstBuffer,	\
     GLboolean flip)					\
{							\
   const PIXELTYPE *src = (const PIXELTYPE *) srcBuffer;\
   PIXELTYPE *dst = (PIXELTYPE *) dstBuffer;		\
   GLint dstCol;					\
							\
   if (flip) {						\
      for (dstCol = 0; dstCol < dstWidth; dstCol++) {	\
         GLint srcCol = (dstCol * srcWidth) / dstWidth;	\
         ASSERT(srcCol >= 0);				\
         ASSERT(srcCol < srcWidth);			\
         srcCol = srcWidth - 1 - srcCol; /* flip */	\
         if (SIZE == 1) {				\
            dst[dstCol] = src[srcCol];			\
         }						\
         else if (SIZE == 2) {				\
            dst[dstCol*2+0] = src[srcCol*2+0];		\
            dst[dstCol*2+1] = src[srcCol*2+1];		\
         }						\
         else if (SIZE == 4) {				\
            dst[dstCol*4+0] = src[srcCol*4+0];		\
            dst[dstCol*4+1] = src[srcCol*4+1];		\
            dst[dstCol*4+2] = src[srcCol*4+2];		\
            dst[dstCol*4+3] = src[srcCol*4+3];		\
         }						\
      }							\
   }							\
   else {						\
      for (dstCol = 0; dstCol < dstWidth; dstCol++) {	\
         GLint srcCol = (dstCol * srcWidth) / dstWidth;	\
         ASSERT(srcCol >= 0);				\
         ASSERT(srcCol < srcWidth);			\
         if (SIZE == 1) {				\
            dst[dstCol] = src[srcCol];			\
         }						\
         else if (SIZE == 2) {				\
            dst[dstCol*2+0] = src[srcCol*2+0];		\
            dst[dstCol*2+1] = src[srcCol*2+1];		\
         }						\
         else if (SIZE == 4) {				\
            dst[dstCol*4+0] = src[srcCol*4+0];		\
            dst[dstCol*4+1] = src[srcCol*4+1];		\
            dst[dstCol*4+2] = src[srcCol*4+2];		\
            dst[dstCol*4+3] = src[srcCol*4+3];		\
         }						\
      }							\
   }							\
}

/**
 * Resamplers for 1, 2, 4, 8 and 16-byte pixels.
 */
RESAMPLE(resample_row_1, GLubyte, 1)
RESAMPLE(resample_row_2, GLushort, 1)
RESAMPLE(resample_row_4, GLuint, 1)
RESAMPLE(resample_row_8, GLuint, 2)
RESAMPLE(resample_row_16, GLuint, 4)


/**
 * Blit color, depth or stencil with GL_NEAREST filtering.
 */
static void
blit_nearest(struct gl_context *ctx,
             GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
             GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
             GLbitfield buffer)
{
   struct gl_renderbuffer *readRb, *drawRb = NULL;
   struct gl_renderbuffer_attachment *readAtt = NULL, *drawAtt = NULL;
   struct gl_framebuffer *readFb = ctx->ReadBuffer;
   struct gl_framebuffer *drawFb = ctx->DrawBuffer;
   GLuint numDrawBuffers = 0;
   GLuint i;

   const GLint srcWidth = ABS(srcX1 - srcX0);
   const GLint dstWidth = ABS(dstX1 - dstX0);
   const GLint srcHeight = ABS(srcY1 - srcY0);
   const GLint dstHeight = ABS(dstY1 - dstY0);

   const GLint srcXpos = MIN2(srcX0, srcX1);
   const GLint srcYpos = MIN2(srcY0, srcY1);
   const GLint dstXpos = MIN2(dstX0, dstX1);
   const GLint dstYpos = MIN2(dstY0, dstY1);

   const GLboolean invertX = (srcX1 < srcX0) ^ (dstX1 < dstX0);
   const GLboolean invertY = (srcY1 < srcY0) ^ (dstY1 < dstY0);
   enum mode {
      DIRECT,
      UNPACK_RGBA_FLOAT,
      UNPACK_Z_FLOAT,
      UNPACK_Z_INT,
      UNPACK_S,
   } mode = DIRECT;
   GLubyte *srcMap, *dstMap;
   GLint srcRowStride, dstRowStride;
   GLint dstRow;

   GLint pixelSize = 0;
   GLvoid *srcBuffer, *dstBuffer;
   GLint prevY = -1;

   typedef void (*resample_func)(GLint srcWidth, GLint dstWidth,
                                 const GLvoid *srcBuffer, GLvoid *dstBuffer,
                                 GLboolean flip);
   resample_func resampleRow;

   switch (buffer) {
   case GL_COLOR_BUFFER_BIT:
      readAtt = &readFb->Attachment[readFb->_ColorReadBufferIndex];
      readRb = readFb->_ColorReadBuffer;
      numDrawBuffers = drawFb->_NumColorDrawBuffers;
      break;
   case GL_DEPTH_BUFFER_BIT:
      readAtt = &readFb->Attachment[BUFFER_DEPTH];
      drawAtt = &drawFb->Attachment[BUFFER_DEPTH];
      readRb = readAtt->Renderbuffer;
      drawRb = drawAtt->Renderbuffer;
      numDrawBuffers = 1;

      /* Note that for depth/stencil, the formats of src/dst must match.  By
       * using the core helpers for pack/unpack, we avoid needing to handle
       * masking for things like DEPTH copies of Z24S8.
       */
      if (readRb->Format == MESA_FORMAT_Z32_FLOAT ||
	  readRb->Format == MESA_FORMAT_Z32_FLOAT_X24S8) {
	 mode = UNPACK_Z_FLOAT;
      } else {
	 mode = UNPACK_Z_INT;
      }
      pixelSize = 4;
      break;
   case GL_STENCIL_BUFFER_BIT:
      readAtt = &readFb->Attachment[BUFFER_STENCIL];
      drawAtt = &drawFb->Attachment[BUFFER_STENCIL];
      readRb = readAtt->Renderbuffer;
      drawRb = drawAtt->Renderbuffer;
      numDrawBuffers = 1;
      mode = UNPACK_S;
      pixelSize = 1;
      break;
   default:
      _mesa_problem(ctx, "unexpected buffer in blit_nearest()");
      return;
   }

   /* allocate the src/dst row buffers */
   srcBuffer = malloc(MAX_PIXEL_BYTES * srcWidth);
   dstBuffer = malloc(MAX_PIXEL_BYTES * dstWidth);
   if (!srcBuffer || !dstBuffer)
      goto fail_no_memory;

   /* Blit to all the draw buffers */
   for (i = 0; i < numDrawBuffers; i++) {
      if (buffer == GL_COLOR_BUFFER_BIT) {
         int idx = drawFb->_ColorDrawBufferIndexes[i];
         if (idx == -1)
            continue;
         drawAtt = &drawFb->Attachment[idx];
         drawRb = drawAtt->Renderbuffer;

         if (!drawRb)
            continue;

         if (readRb->Format == drawRb->Format) {
            mode = DIRECT;
            pixelSize = _mesa_get_format_bytes(readRb->Format);
         } else {
            mode = UNPACK_RGBA_FLOAT;
            pixelSize = 16;
         }
      }

      /* choose row resampler */
      switch (pixelSize) {
      case 1:
         resampleRow = resample_row_1;
         break;
      case 2:
         resampleRow = resample_row_2;
         break;
      case 4:
         resampleRow = resample_row_4;
         break;
      case 8:
         resampleRow = resample_row_8;
         break;
      case 16:
         resampleRow = resample_row_16;
         break;
      default:
         _mesa_problem(ctx, "unexpected pixel size (%d) in blit_nearest",
                       pixelSize);
         goto fail;
      }

      if ((readRb == drawRb) ||
          (readAtt->Texture && drawAtt->Texture &&
           (readAtt->Texture == drawAtt->Texture))) {
         /* map whole buffer for read/write */
         /* XXX we could be clever and just map the union region of the
          * source and dest rects.
          */
         GLubyte *map;
         GLint rowStride;
         GLint formatSize = _mesa_get_format_bytes(readRb->Format);

         ctx->Driver.MapRenderbuffer(ctx, readRb, 0, 0,
                                     readRb->Width, readRb->Height,
                                     GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
                                     &map, &rowStride);
         if (!map) {
            goto fail_no_memory;
         }

         srcMap = map + srcYpos * rowStride + srcXpos * formatSize;
         dstMap = map + dstYpos * rowStride + dstXpos * formatSize;

         /* this handles overlapping copies */
         if (srcY0 < dstY0) {
            /* copy in reverse (top->down) order */
            srcMap += rowStride * (readRb->Height - 1);
            dstMap += rowStride * (readRb->Height - 1);
            srcRowStride = -rowStride;
            dstRowStride = -rowStride;
         }
         else {
            /* copy in normal (bottom->up) order */
            srcRowStride = rowStride;
            dstRowStride = rowStride;
         }
      }
      else {
         /* different src/dst buffers */
         ctx->Driver.MapRenderbuffer(ctx, readRb,
                                     srcXpos, srcYpos,
                                     srcWidth, srcHeight,
                                     GL_MAP_READ_BIT, &srcMap, &srcRowStride);
         if (!srcMap) {
            goto fail_no_memory;
         }
         ctx->Driver.MapRenderbuffer(ctx, drawRb,
                                     dstXpos, dstYpos,
                                     dstWidth, dstHeight,
                                     GL_MAP_WRITE_BIT, &dstMap, &dstRowStride);
         if (!dstMap) {
            ctx->Driver.UnmapRenderbuffer(ctx, readRb);
            goto fail_no_memory;
         }
      }

      for (dstRow = 0; dstRow < dstHeight; dstRow++) {
         GLfloat srcRowF = (dstRow + 0.5F) / dstHeight * srcHeight - 0.5F;
         GLint srcRow = IROUND(srcRowF);
         GLubyte *dstRowStart = dstMap + dstRowStride * dstRow;

         ASSERT(srcRow >= 0);
         ASSERT(srcRow < srcHeight);

         if (invertY) {
            srcRow = srcHeight - 1 - srcRow;
         }

         /* get pixel row from source and resample to match dest width */
         if (prevY != srcRow) {
            GLubyte *srcRowStart = srcMap + srcRowStride * srcRow;

            switch (mode) {
            case DIRECT:
               memcpy(srcBuffer, srcRowStart, pixelSize * srcWidth);
               break;
            case UNPACK_RGBA_FLOAT:
               _mesa_unpack_rgba_row(readRb->Format, srcWidth, srcRowStart,
                                     srcBuffer);
               break;
            case UNPACK_Z_FLOAT:
               _mesa_unpack_float_z_row(readRb->Format, srcWidth, srcRowStart,
                                        srcBuffer);
               break;
            case UNPACK_Z_INT:
               _mesa_unpack_uint_z_row(readRb->Format, srcWidth, srcRowStart,
                                       srcBuffer);
               break;
            case UNPACK_S:
               _mesa_unpack_ubyte_stencil_row(readRb->Format, srcWidth,
                                              srcRowStart, srcBuffer);
               break;
            }

            (*resampleRow)(srcWidth, dstWidth, srcBuffer, dstBuffer, invertX);
            prevY = srcRow;
         }

         /* store pixel row in destination */
         switch (mode) {
         case DIRECT:
            memcpy(dstRowStart, dstBuffer, pixelSize * dstWidth);
            break;
         case UNPACK_RGBA_FLOAT:
            _mesa_pack_float_rgba_row(drawRb->Format, dstWidth, dstBuffer,
                                      dstRowStart);
            break;
         case UNPACK_Z_FLOAT:
            _mesa_pack_float_z_row(drawRb->Format, dstWidth, dstBuffer,
                                   dstRowStart);
            break;
         case UNPACK_Z_INT:
            _mesa_pack_uint_z_row(drawRb->Format, dstWidth, dstBuffer,
                                  dstRowStart);
            break;
         case UNPACK_S:
            _mesa_pack_ubyte_stencil_row(drawRb->Format, dstWidth, dstBuffer,
                                         dstRowStart);
            break;
         }
      }

      ctx->Driver.UnmapRenderbuffer(ctx, readRb);
      if (drawRb != readRb) {
         ctx->Driver.UnmapRenderbuffer(ctx, drawRb);
      }
   }

fail:
   free(srcBuffer);
   free(dstBuffer);
   return;

fail_no_memory:
   free(srcBuffer);
   free(dstBuffer);
   _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBuffer");
}



#define LERP(T, A, B)  ( (A) + (T) * ((B) - (A)) )

static inline GLfloat
lerp_2d(GLfloat a, GLfloat b,
        GLfloat v00, GLfloat v10, GLfloat v01, GLfloat v11)
{
   const GLfloat temp0 = LERP(a, v00, v10);
   const GLfloat temp1 = LERP(a, v01, v11);
   return LERP(b, temp0, temp1);
}


/**
 * Bilinear interpolation of two source rows.
 * GLubyte pixels.
 */
static void
resample_linear_row_ub(GLint srcWidth, GLint dstWidth,
                       const GLvoid *srcBuffer0, const GLvoid *srcBuffer1,
                       GLvoid *dstBuffer, GLboolean flip, GLfloat rowWeight)
{
   const GLubyte (*srcColor0)[4] = (const GLubyte (*)[4]) srcBuffer0;
   const GLubyte (*srcColor1)[4] = (const GLubyte (*)[4]) srcBuffer1;
   GLubyte (*dstColor)[4] = (GLubyte (*)[4]) dstBuffer;
   GLint dstCol;

   for (dstCol = 0; dstCol < dstWidth; dstCol++) {
      const GLfloat srcCol = (dstCol + 0.5F) / dstWidth * srcWidth - 0.5F;
      GLint srcCol0 = MAX2(0, IFLOOR(srcCol));
      GLint srcCol1 = srcCol0 + 1;
      GLfloat colWeight = srcCol - srcCol0; /* fractional part of srcCol */
      GLfloat red, green, blue, alpha;

      ASSERT(srcCol0 < srcWidth);
      ASSERT(srcCol1 <= srcWidth);

      if (srcCol1 == srcWidth) {
         /* last column fudge */
         srcCol1--;
         colWeight = 0.0;
      }

      if (flip) {
         srcCol0 = srcWidth - 1 - srcCol0;
         srcCol1 = srcWidth - 1 - srcCol1;
      }

      red = lerp_2d(colWeight, rowWeight,
                    srcColor0[srcCol0][RCOMP], srcColor0[srcCol1][RCOMP],
                    srcColor1[srcCol0][RCOMP], srcColor1[srcCol1][RCOMP]);
      green = lerp_2d(colWeight, rowWeight,
                    srcColor0[srcCol0][GCOMP], srcColor0[srcCol1][GCOMP],
                    srcColor1[srcCol0][GCOMP], srcColor1[srcCol1][GCOMP]);
      blue = lerp_2d(colWeight, rowWeight,
                    srcColor0[srcCol0][BCOMP], srcColor0[srcCol1][BCOMP],
                    srcColor1[srcCol0][BCOMP], srcColor1[srcCol1][BCOMP]);
      alpha = lerp_2d(colWeight, rowWeight,
                    srcColor0[srcCol0][ACOMP], srcColor0[srcCol1][ACOMP],
                    srcColor1[srcCol0][ACOMP], srcColor1[srcCol1][ACOMP]);
      
      dstColor[dstCol][RCOMP] = IFLOOR(red);
      dstColor[dstCol][GCOMP] = IFLOOR(green);
      dstColor[dstCol][BCOMP] = IFLOOR(blue);
      dstColor[dstCol][ACOMP] = IFLOOR(alpha);
   }
}


/**
 * Bilinear interpolation of two source rows.  floating point pixels.
 */
static void
resample_linear_row_float(GLint srcWidth, GLint dstWidth,
                          const GLvoid *srcBuffer0, const GLvoid *srcBuffer1,
                          GLvoid *dstBuffer, GLboolean flip, GLfloat rowWeight)
{
   const GLfloat (*srcColor0)[4] = (const GLfloat (*)[4]) srcBuffer0;
   const GLfloat (*srcColor1)[4] = (const GLfloat (*)[4]) srcBuffer1;
   GLfloat (*dstColor)[4] = (GLfloat (*)[4]) dstBuffer;
   GLint dstCol;

   for (dstCol = 0; dstCol < dstWidth; dstCol++) {
      const GLfloat srcCol = (dstCol + 0.5F) / dstWidth * srcWidth - 0.5F;
      GLint srcCol0 = MAX2(0, IFLOOR(srcCol));
      GLint srcCol1 = srcCol0 + 1;
      GLfloat colWeight = srcCol - srcCol0; /* fractional part of srcCol */
      GLfloat red, green, blue, alpha;

      ASSERT(srcCol0 < srcWidth);
      ASSERT(srcCol1 <= srcWidth);

      if (srcCol1 == srcWidth) {
         /* last column fudge */
         srcCol1--;
         colWeight = 0.0;
      }

      if (flip) {
         srcCol0 = srcWidth - 1 - srcCol0;
         srcCol1 = srcWidth - 1 - srcCol1;
      }

      red = lerp_2d(colWeight, rowWeight,
                    srcColor0[srcCol0][RCOMP], srcColor0[srcCol1][RCOMP],
                    srcColor1[srcCol0][RCOMP], srcColor1[srcCol1][RCOMP]);
      green = lerp_2d(colWeight, rowWeight,
                    srcColor0[srcCol0][GCOMP], srcColor0[srcCol1][GCOMP],
                    srcColor1[srcCol0][GCOMP], srcColor1[srcCol1][GCOMP]);
      blue = lerp_2d(colWeight, rowWeight,
                    srcColor0[srcCol0][BCOMP], srcColor0[srcCol1][BCOMP],
                    srcColor1[srcCol0][BCOMP], srcColor1[srcCol1][BCOMP]);
      alpha = lerp_2d(colWeight, rowWeight,
                    srcColor0[srcCol0][ACOMP], srcColor0[srcCol1][ACOMP],
                    srcColor1[srcCol0][ACOMP], srcColor1[srcCol1][ACOMP]);
      
      dstColor[dstCol][RCOMP] = red;
      dstColor[dstCol][GCOMP] = green;
      dstColor[dstCol][BCOMP] = blue;
      dstColor[dstCol][ACOMP] = alpha;
   }
}



/**
 * Bilinear filtered blit (color only, non-integer values).
 */
static void
blit_linear(struct gl_context *ctx,
            GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
            GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
{
   struct gl_framebuffer *drawFb = ctx->DrawBuffer;
   struct gl_framebuffer *readFb = ctx->ReadBuffer;
   struct gl_renderbuffer *readRb = readFb->_ColorReadBuffer;
   struct gl_renderbuffer_attachment *readAtt =
      &readFb->Attachment[readFb->_ColorReadBufferIndex];

   const GLint srcWidth = ABS(srcX1 - srcX0);
   const GLint dstWidth = ABS(dstX1 - dstX0);
   const GLint srcHeight = ABS(srcY1 - srcY0);
   const GLint dstHeight = ABS(dstY1 - dstY0);

   const GLint srcXpos = MIN2(srcX0, srcX1);
   const GLint srcYpos = MIN2(srcY0, srcY1);
   const GLint dstXpos = MIN2(dstX0, dstX1);
   const GLint dstYpos = MIN2(dstY0, dstY1);

   const GLboolean invertX = (srcX1 < srcX0) ^ (dstX1 < dstX0);
   const GLboolean invertY = (srcY1 < srcY0) ^ (dstY1 < dstY0);

   GLint dstRow;

   GLint pixelSize;
   GLvoid *srcBuffer0, *srcBuffer1;
   GLint srcBufferY0 = -1, srcBufferY1 = -1;
   GLvoid *dstBuffer;

   gl_format readFormat = _mesa_get_srgb_format_linear(readRb->Format);
   GLuint bpp = _mesa_get_format_bytes(readFormat);

   GLenum pixelType;

   GLubyte *srcMap, *dstMap;
   GLint srcRowStride, dstRowStride;
   GLuint i;


   /* Determine datatype for resampling */
   if (_mesa_get_format_max_bits(readFormat) == 8 &&
       _mesa_get_format_datatype(readFormat) == GL_UNSIGNED_NORMALIZED) {
      pixelType = GL_UNSIGNED_BYTE;
      pixelSize = 4 * sizeof(GLubyte);
   }
   else {
      pixelType = GL_FLOAT;
      pixelSize = 4 * sizeof(GLfloat);
   }

   /* Allocate the src/dst row buffers.
    * Keep two adjacent src rows around for bilinear sampling.
    */
   srcBuffer0 = malloc(pixelSize * srcWidth);
   srcBuffer1 = malloc(pixelSize * srcWidth);
   dstBuffer = malloc(pixelSize * dstWidth);
   if (!srcBuffer0 || !srcBuffer1 || !dstBuffer) {
      goto fail_no_memory;
   }

   for (i = 0; i < drawFb->_NumColorDrawBuffers; i++) {
      GLint idx = drawFb->_ColorDrawBufferIndexes[i];
      struct gl_renderbuffer_attachment *drawAtt;
      struct gl_renderbuffer *drawRb;
      gl_format drawFormat;

      if (idx == -1)
         continue;

      drawAtt = &drawFb->Attachment[idx];
      drawRb = drawAtt->Renderbuffer;
      if (!drawRb)
         continue;

      drawFormat = _mesa_get_srgb_format_linear(drawRb->Format);

      /*
       * Map src / dst renderbuffers
       */
      if ((readRb == drawRb) ||
          (readAtt->Texture && drawAtt->Texture &&
           (readAtt->Texture == drawAtt->Texture))) {
         /* map whole buffer for read/write */
         ctx->Driver.MapRenderbuffer(ctx, readRb,
                                     0, 0, readRb->Width, readRb->Height,
                                     GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
                                     &srcMap, &srcRowStride);
         if (!srcMap) {
            goto fail_no_memory;
         }

         dstMap = srcMap;
         dstRowStride = srcRowStride;
      }
      else {
         /* different src/dst buffers */
         /* XXX with a bit of work we could just map the regions to be
          * read/written instead of the whole buffers.
          */
         ctx->Driver.MapRenderbuffer(ctx, readRb,
                                     0, 0, readRb->Width, readRb->Height,
                                     GL_MAP_READ_BIT, &srcMap, &srcRowStride);
         if (!srcMap) {
            goto fail_no_memory;
         }
         ctx->Driver.MapRenderbuffer(ctx, drawRb,
                                     0, 0, drawRb->Width, drawRb->Height,
                                     GL_MAP_WRITE_BIT, &dstMap, &dstRowStride);
         if (!dstMap) {
            ctx->Driver.UnmapRenderbuffer(ctx, readRb);
            goto fail_no_memory;
         }
      }

      for (dstRow = 0; dstRow < dstHeight; dstRow++) {
         const GLint dstY = dstYpos + dstRow;
         GLfloat srcRow = (dstRow + 0.5F) / dstHeight * srcHeight - 0.5F;
         GLint srcRow0 = MAX2(0, IFLOOR(srcRow));
         GLint srcRow1 = srcRow0 + 1;
         GLfloat rowWeight = srcRow - srcRow0; /* fractional part of srcRow */

         if (srcRow1 == srcHeight) {
            /* last row fudge */
            srcRow1 = srcRow0;
            rowWeight = 0.0;
         }

         if (invertY) {
            srcRow0 = srcHeight - 1 - srcRow0;
            srcRow1 = srcHeight - 1 - srcRow1;
         }

         srcY0 = srcYpos + srcRow0;
         srcY1 = srcYpos + srcRow1;

         /* get the two source rows */
         if (srcY0 == srcBufferY0 && srcY1 == srcBufferY1) {
            /* use same source row buffers again */
         }
         else if (srcY0 == srcBufferY1) {
            /* move buffer1 into buffer0 by swapping pointers */
            GLvoid *tmp = srcBuffer0;
            srcBuffer0 = srcBuffer1;
            srcBuffer1 = tmp;
            /* get y1 row */
            {
               GLubyte *src = srcMap + srcY1 * srcRowStride + srcXpos * bpp;
               if (pixelType == GL_UNSIGNED_BYTE) {
                  _mesa_unpack_ubyte_rgba_row(readFormat, srcWidth,
                                              src, srcBuffer1);
               }
               else {
                  _mesa_unpack_rgba_row(readFormat, srcWidth,
                                        src, srcBuffer1);
               }
            }
            srcBufferY0 = srcY0;
            srcBufferY1 = srcY1;
         }
         else {
            /* get both new rows */
            {
               GLubyte *src0 = srcMap + srcY0 * srcRowStride + srcXpos * bpp;
               GLubyte *src1 = srcMap + srcY1 * srcRowStride + srcXpos * bpp;
               if (pixelType == GL_UNSIGNED_BYTE) {
                  _mesa_unpack_ubyte_rgba_row(readFormat, srcWidth,
                                              src0, srcBuffer0);
                  _mesa_unpack_ubyte_rgba_row(readFormat, srcWidth,
                                              src1, srcBuffer1);
               }
               else {
                  _mesa_unpack_rgba_row(readFormat, srcWidth, src0, srcBuffer0);
                  _mesa_unpack_rgba_row(readFormat, srcWidth, src1, srcBuffer1);
               }
            }
            srcBufferY0 = srcY0;
            srcBufferY1 = srcY1;
         }

         if (pixelType == GL_UNSIGNED_BYTE) {
            resample_linear_row_ub(srcWidth, dstWidth, srcBuffer0, srcBuffer1,
                                   dstBuffer, invertX, rowWeight);
         }
         else {
            resample_linear_row_float(srcWidth, dstWidth, srcBuffer0, srcBuffer1,
                                      dstBuffer, invertX, rowWeight);
         }

         /* store pixel row in destination */
         {
            GLubyte *dst = dstMap + dstY * dstRowStride + dstXpos * bpp;
            if (pixelType == GL_UNSIGNED_BYTE) {
               _mesa_pack_ubyte_rgba_row(drawFormat, dstWidth, dstBuffer, dst);
            }
            else {
               _mesa_pack_float_rgba_row(drawFormat, dstWidth, dstBuffer, dst);
            }
         }
      }

      ctx->Driver.UnmapRenderbuffer(ctx, readRb);
      if (drawRb != readRb) {
         ctx->Driver.UnmapRenderbuffer(ctx, drawRb);
      }
   }

   free(srcBuffer0);
   free(srcBuffer1);
   free(dstBuffer);
   return;

fail_no_memory:
   free(srcBuffer0);
   free(srcBuffer1);
   free(dstBuffer);
   _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
}



/**
 * Software fallback for glBlitFramebufferEXT().
 */
void
_swrast_BlitFramebuffer(struct gl_context *ctx,
                        GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                        GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                        GLbitfield mask, GLenum filter)
{
   static const GLbitfield buffers[3] = {
      GL_COLOR_BUFFER_BIT,
      GL_DEPTH_BUFFER_BIT,
      GL_STENCIL_BUFFER_BIT
   };
   static const GLenum buffer_enums[3] = {
      GL_COLOR,
      GL_DEPTH,
      GL_STENCIL,
   };
   GLint i;

   if (!_mesa_clip_blit(ctx, &srcX0, &srcY0, &srcX1, &srcY1,
                        &dstX0, &dstY0, &dstX1, &dstY1)) {
      return;
   }

   if (SWRAST_CONTEXT(ctx)->NewState)
      _swrast_validate_derived(ctx);

   /* First, try covering whatever buffers possible using the fast 1:1 copy
    * path.
    */
   if (srcX1 - srcX0 == dstX1 - dstX0 &&
       srcY1 - srcY0 == dstY1 - dstY0 &&
       srcX0 < srcX1 &&
       srcY0 < srcY1 &&
       dstX0 < dstX1 &&
       dstY0 < dstY1) {
      for (i = 0; i < 3; i++) {
         if (mask & buffers[i]) {
	    if (swrast_fast_copy_pixels(ctx,
					srcX0, srcY0,
					srcX1 - srcX0, srcY1 - srcY0,
					dstX0, dstY0,
					buffer_enums[i])) {
	       mask &= ~buffers[i];
	    }
	 }
      }

      if (!mask)
	 return;
   }

   if (filter == GL_NEAREST) {
      for (i = 0; i < 3; i++) {
	 if (mask & buffers[i]) {
	    blit_nearest(ctx,  srcX0, srcY0, srcX1, srcY1,
			 dstX0, dstY0, dstX1, dstY1, buffers[i]);
	 }
      }
   }
   else {
      ASSERT(filter == GL_LINEAR);
      if (mask & GL_COLOR_BUFFER_BIT) {  /* depth/stencil not allowed */
	 blit_linear(ctx,  srcX0, srcY0, srcX1, srcY1,
		     dstX0, dstY0, dstX1, dstY1);
      }
   }

}