summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/blit.c
blob: bb866b1e0902c133be583140f709efaaf6da8ecd (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
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
 * Copyright (C) 1999-2013  VMware, Inc.  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.
 */

/*
 * glBlitFramebuffer functions.
 */

#include <stdbool.h>
#include <stdio.h>

#include "context.h"
#include "enums.h"
#include "blit.h"
#include "fbobject.h"
#include "framebuffer.h"
#include "glformats.h"
#include "mtypes.h"
#include "macros.h"
#include "state.h"


/** Set this to 1 to debug/log glBlitFramebuffer() calls */
#define DEBUG_BLIT 0



static const struct gl_renderbuffer_attachment *
find_attachment(const struct gl_framebuffer *fb,
                const struct gl_renderbuffer *rb)
{
   GLuint i;
   for (i = 0; i < ARRAY_SIZE(fb->Attachment); i++) {
      if (fb->Attachment[i].Renderbuffer == rb)
         return &fb->Attachment[i];
   }
   return NULL;
}


/**
 * \return true if two regions overlap, false otherwise
 */
bool
_mesa_regions_overlap(int srcX0, int srcY0,
                      int srcX1, int srcY1,
                      int dstX0, int dstY0,
                      int dstX1, int dstY1)
{
   if (MAX2(srcX0, srcX1) <= MIN2(dstX0, dstX1))
      return false; /* dst completely right of src */

   if (MAX2(dstX0, dstX1) <= MIN2(srcX0, srcX1))
      return false; /* dst completely left of src */

   if (MAX2(srcY0, srcY1) <= MIN2(dstY0, dstY1))
      return false; /* dst completely above src */

   if (MAX2(dstY0, dstY1) <= MIN2(srcY0, srcY1))
      return false; /* dst completely below src */

   return true; /* some overlap */
}


/**
 * Helper function for checking if the datatypes of color buffers are
 * compatible for glBlitFramebuffer.  From the 3.1 spec, page 198:
 *
 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
 *  and any of the following conditions hold:
 *   - The read buffer contains fixed-point or floating-point values and any
 *     draw buffer contains neither fixed-point nor floating-point values.
 *   - The read buffer contains unsigned integer values and any draw buffer
 *     does not contain unsigned integer values.
 *   - The read buffer contains signed integer values and any draw buffer
 *     does not contain signed integer values."
 */
static GLboolean
compatible_color_datatypes(mesa_format srcFormat, mesa_format dstFormat)
{
   GLenum srcType = _mesa_get_format_datatype(srcFormat);
   GLenum dstType = _mesa_get_format_datatype(dstFormat);

   if (srcType != GL_INT && srcType != GL_UNSIGNED_INT) {
      assert(srcType == GL_UNSIGNED_NORMALIZED ||
             srcType == GL_SIGNED_NORMALIZED ||
             srcType == GL_FLOAT);
      /* Boil any of those types down to GL_FLOAT */
      srcType = GL_FLOAT;
   }

   if (dstType != GL_INT && dstType != GL_UNSIGNED_INT) {
      assert(dstType == GL_UNSIGNED_NORMALIZED ||
             dstType == GL_SIGNED_NORMALIZED ||
             dstType == GL_FLOAT);
      /* Boil any of those types down to GL_FLOAT */
      dstType = GL_FLOAT;
   }

   return srcType == dstType;
}


static GLboolean
compatible_resolve_formats(const struct gl_renderbuffer *readRb,
                           const struct gl_renderbuffer *drawRb)
{
   GLenum readFormat, drawFormat;

   /* This checks whether the internal formats are compatible rather than the
    * Mesa format for two reasons:
    *
    * • Under some circumstances, the user may request e.g. two GL_RGBA8
    *   textures and get two entirely different Mesa formats like RGBA8888 and
    *   ARGB8888. Drivers behaving like that should be able to cope with
    *   non-matching formats by themselves, because it's not the user's fault.
    *
    * • Picking two different internal formats can end up with the same Mesa
    *   format. For example the driver might be simulating GL_RGB textures
    *   with GL_RGBA internally and in that case both internal formats would
    *   end up with RGBA8888.
    *
    * This function is used to generate a GL error according to the spec so in
    * both cases we want to be looking at the application-level format, which
    * is InternalFormat.
    *
    * Blits between linear and sRGB formats are also allowed.
    */
   readFormat = _mesa_get_nongeneric_internalformat(readRb->InternalFormat);
   drawFormat = _mesa_get_nongeneric_internalformat(drawRb->InternalFormat);
   readFormat = _mesa_get_linear_internalformat(readFormat);
   drawFormat = _mesa_get_linear_internalformat(drawFormat);

   if (readFormat == drawFormat) {
      return GL_TRUE;
   }

   return GL_FALSE;
}


static GLboolean
is_valid_blit_filter(const struct gl_context *ctx, GLenum filter)
{
   switch (filter) {
   case GL_NEAREST:
   case GL_LINEAR:
      return true;
   case GL_SCALED_RESOLVE_FASTEST_EXT:
   case GL_SCALED_RESOLVE_NICEST_EXT:
      return ctx->Extensions.EXT_framebuffer_multisample_blit_scaled;
   default:
      return false;
   }
}


static bool
validate_color_buffer(struct gl_context *ctx, struct gl_framebuffer *readFb,
                      struct gl_framebuffer *drawFb, GLenum filter,
                      const char *func)
{
   const GLuint numColorDrawBuffers = drawFb->_NumColorDrawBuffers;
   const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
   const struct gl_renderbuffer *colorDrawRb = NULL;
   GLuint i;

   for (i = 0; i < numColorDrawBuffers; i++) {
      colorDrawRb = drawFb->_ColorDrawBuffers[i];
      if (!colorDrawRb)
         continue;

      /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
       * ES 3.0.1 spec says:
       *
       *     "If the source and destination buffers are identical, an
       *     INVALID_OPERATION error is generated. Different mipmap levels of a
       *     texture, different layers of a three- dimensional texture or
       *     two-dimensional array texture, and different faces of a cube map
       *     texture do not constitute identical buffers."
       */
      if (_mesa_is_gles3(ctx) && (colorDrawRb == colorReadRb)) {
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "%s(source and destination color buffer cannot be the "
                     "same)", func);
         return false;
      }

      if (!compatible_color_datatypes(colorReadRb->Format,
                                      colorDrawRb->Format)) {
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "%s(color buffer datatypes mismatch)", func);
         return false;
      }

      /* extra checks for multisample copies... */
      if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
         /* color formats must match on GLES. This isn't checked on desktop GL
          * because the GL 4.4 spec was changed to allow it.  In the section
          * entitled “Changes in the released
          * Specification of July 22, 2013” it says:
          *
          * “Relax BlitFramebuffer in section 18.3.1 so that format conversion
          * can take place during multisample blits, since drivers already
          * allow this and some apps depend on it.”
          */
         if (_mesa_is_gles(ctx) &&
             !compatible_resolve_formats(colorReadRb, colorDrawRb)) {
            _mesa_error(ctx, GL_INVALID_OPERATION,
                        "%s(bad src/dst multisample pixel formats)", func);
            return false;
         }
      }

   }

   if (filter != GL_NEAREST) {
      /* From EXT_framebuffer_multisample_blit_scaled specification:
       * "Calling BlitFramebuffer will result in an INVALID_OPERATION error if
       * filter is not NEAREST and read buffer contains integer data."
       */
      GLenum type = _mesa_get_format_datatype(colorReadRb->Format);
      if (type == GL_INT || type == GL_UNSIGNED_INT) {
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "%s(integer color type)", func);
         return false;
      }
   }
   return true;
}


static bool
validate_stencil_buffer(struct gl_context *ctx, struct gl_framebuffer *readFb,
                        struct gl_framebuffer *drawFb, const char *func)
{
   struct gl_renderbuffer *readRb =
      readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
   struct gl_renderbuffer *drawRb =
      drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
   int read_z_bits, draw_z_bits;

   if (_mesa_is_gles3(ctx) && (drawRb == readRb)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(source and destination stencil buffer cannot be the "
                  "same)", func);
      return false;
   }

   if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
       _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
      /* There is no need to check the stencil datatype here, because
       * there is only one: GL_UNSIGNED_INT.
       */
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(stencil attachment format mismatch)", func);
      return false;
   }

   read_z_bits = _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS);
   draw_z_bits = _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS);

   /* If both buffers also have depth data, the depth formats must match
    * as well.  If one doesn't have depth, it's not blitted, so we should
    * ignore the depth format check.
    */
   if (read_z_bits > 0 && draw_z_bits > 0 &&
       (read_z_bits != draw_z_bits ||
        _mesa_get_format_datatype(readRb->Format) !=
        _mesa_get_format_datatype(drawRb->Format))) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(stencil attachment depth format mismatch)", func);
      return false;
   }
   return true;
}


static bool
validate_depth_buffer(struct gl_context *ctx, struct gl_framebuffer *readFb,
                      struct gl_framebuffer *drawFb, const char *func)
{
   struct gl_renderbuffer *readRb =
      readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
   struct gl_renderbuffer *drawRb =
      drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
   int read_s_bit, draw_s_bit;

   if (_mesa_is_gles3(ctx) && (drawRb == readRb)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(source and destination depth buffer cannot be the same)",
                  func);
      return false;
   }

   if ((_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
        _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) ||
       (_mesa_get_format_datatype(readRb->Format) !=
        _mesa_get_format_datatype(drawRb->Format))) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(depth attachment format mismatch)", func);
      return false;
   }

   read_s_bit = _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS);
   draw_s_bit = _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS);

   /* If both buffers also have stencil data, the stencil formats must match as
    * well.  If one doesn't have stencil, it's not blitted, so we should ignore
    * the stencil format check.
    */
   if (read_s_bit > 0 && draw_s_bit > 0 && read_s_bit != draw_s_bit) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(depth attachment stencil bits mismatch)", func);
      return false;
   }
   return true;
}


static ALWAYS_INLINE void
blit_framebuffer(struct gl_context *ctx,
                 struct gl_framebuffer *readFb, struct gl_framebuffer *drawFb,
                 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                 GLbitfield mask, GLenum filter, bool no_error, const char *func)
{
   FLUSH_VERTICES(ctx, 0);

   /* Update completeness status of readFb and drawFb. */
   _mesa_update_framebuffer(ctx, readFb, drawFb);

   /* Make sure drawFb has an initialized bounding box. */
   _mesa_update_draw_buffer_bounds(ctx, drawFb);

   if (!readFb || !drawFb) {
      /* This will normally never happen but someday we may want to
       * support MakeCurrent() with no drawables.
       */
      return;
   }

   if (!no_error) {
      const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
                                        GL_DEPTH_BUFFER_BIT |
                                        GL_STENCIL_BUFFER_BIT);

      /* check for complete framebuffers */
      if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
          readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
                     "%s(incomplete draw/read buffers)", func);
         return;
      }

      if (!is_valid_blit_filter(ctx, filter)) {
         _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid filter %s)", func,
                     _mesa_enum_to_string(filter));
         return;
      }

      if ((filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
           filter == GL_SCALED_RESOLVE_NICEST_EXT) &&
           (readFb->Visual.samples == 0 || drawFb->Visual.samples > 0)) {
         _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s: invalid samples)", func,
                     _mesa_enum_to_string(filter));
         return;
      }

      if (mask & ~legalMaskBits) {
         _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid mask bits set)", func);
         return;
      }

      /* depth/stencil must be blitted with nearest filtering */
      if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
           && filter != GL_NEAREST) {
         _mesa_error(ctx, GL_INVALID_OPERATION,
                "%s(depth/stencil requires GL_NEAREST filter)", func);
         return;
      }

      if (_mesa_is_gles3(ctx)) {
         /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
          * 3.0.1 spec says:
          *
          *     "If SAMPLE_BUFFERS for the draw framebuffer is greater than
          *     zero, an INVALID_OPERATION error is generated."
          */
         if (drawFb->Visual.samples > 0) {
            _mesa_error(ctx, GL_INVALID_OPERATION,
                        "%s(destination samples must be 0)", func);
            return;
         }

         /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
          * 3.0.1 spec says:
          *
          *     "If SAMPLE_BUFFERS for the read framebuffer is greater than
          *     zero, no copy is performed and an INVALID_OPERATION error is
          *     generated if the formats of the read and draw framebuffers are
          *     not identical or if the source and destination rectangles are
          *     not defined with the same (X0, Y0) and (X1, Y1) bounds."
          *
          * The format check was made above because desktop OpenGL has the same
          * requirement.
          */
         if (readFb->Visual.samples > 0
             && (srcX0 != dstX0 || srcY0 != dstY0
                 || srcX1 != dstX1 || srcY1 != dstY1)) {
            _mesa_error(ctx, GL_INVALID_OPERATION,
                        "%s(bad src/dst multisample region)", func);
            return;
         }
      } else {
         if (readFb->Visual.samples > 0 &&
             drawFb->Visual.samples > 0 &&
             readFb->Visual.samples != drawFb->Visual.samples) {
            _mesa_error(ctx, GL_INVALID_OPERATION,
                        "%s(mismatched samples)", func);
            return;
         }

         /* extra checks for multisample copies... */
         if ((readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) &&
             (filter == GL_NEAREST || filter == GL_LINEAR)) {
            /* src and dest region sizes must be the same */
            if (abs(srcX1 - srcX0) != abs(dstX1 - dstX0) ||
                abs(srcY1 - srcY0) != abs(dstY1 - dstY0)) {
               _mesa_error(ctx, GL_INVALID_OPERATION,
                           "%s(bad src/dst multisample region sizes)", func);
               return;
            }
         }
      }
   }

   /* get color read/draw renderbuffers */
   if (mask & GL_COLOR_BUFFER_BIT) {
      const GLuint numColorDrawBuffers = drawFb->_NumColorDrawBuffers;
      const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;

      /* From the EXT_framebuffer_object spec:
       *
       *     "If a buffer is specified in <mask> and does not exist in both
       *     the read and draw framebuffers, the corresponding bit is silently
       *     ignored."
       */
      if (!colorReadRb || numColorDrawBuffers == 0) {
         mask &= ~GL_COLOR_BUFFER_BIT;
      } else if (!no_error) {
         if (!validate_color_buffer(ctx, readFb, drawFb, filter, func))
            return;
      }
   }

   if (mask & GL_STENCIL_BUFFER_BIT) {
      struct gl_renderbuffer *readRb =
         readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
      struct gl_renderbuffer *drawRb =
         drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;

      /* From the EXT_framebuffer_object spec:
       *
       *     "If a buffer is specified in <mask> and does not exist in both
       *     the read and draw framebuffers, the corresponding bit is silently
       *     ignored."
       */
      if ((readRb == NULL) || (drawRb == NULL)) {
         mask &= ~GL_STENCIL_BUFFER_BIT;
      } else if (!no_error) {
         if (!validate_stencil_buffer(ctx, readFb, drawFb, func))
            return;
      }
   }

   if (mask & GL_DEPTH_BUFFER_BIT) {
      struct gl_renderbuffer *readRb =
         readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
      struct gl_renderbuffer *drawRb =
         drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;

      /* From the EXT_framebuffer_object spec:
       *
       *     "If a buffer is specified in <mask> and does not exist in both
       *     the read and draw framebuffers, the corresponding bit is silently
       *     ignored."
       */
      if ((readRb == NULL) || (drawRb == NULL)) {
         mask &= ~GL_DEPTH_BUFFER_BIT;
      } else if (!no_error) {
         if (!validate_depth_buffer(ctx, readFb, drawFb, func))
            return;
      }
   }

   /* Debug code */
   if (DEBUG_BLIT) {
      const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
      const struct gl_renderbuffer *colorDrawRb = NULL;
      GLuint i = 0;

      printf("%s(%d, %d, %d, %d,  %d, %d, %d, %d,"
             " 0x%x, 0x%x)\n", func,
             srcX0, srcY0, srcX1, srcY1,
             dstX0, dstY0, dstX1, dstY1,
             mask, filter);

      if (colorReadRb) {
         const struct gl_renderbuffer_attachment *att;

         att = find_attachment(readFb, colorReadRb);
         printf("  Src FBO %u  RB %u (%dx%d)  ",
                readFb->Name, colorReadRb->Name,
                colorReadRb->Width, colorReadRb->Height);
         if (att && att->Texture) {
            printf("Tex %u  tgt 0x%x  level %u  face %u",
                   att->Texture->Name,
                   att->Texture->Target,
                   att->TextureLevel,
                   att->CubeMapFace);
         }
         printf("\n");

         /* Print all active color render buffers */
         for (i = 0; i < drawFb->_NumColorDrawBuffers; i++) {
            colorDrawRb = drawFb->_ColorDrawBuffers[i];
            if (!colorDrawRb)
               continue;

            att = find_attachment(drawFb, colorDrawRb);
            printf("  Dst FBO %u  RB %u (%dx%d)  ",
                   drawFb->Name, colorDrawRb->Name,
                   colorDrawRb->Width, colorDrawRb->Height);
            if (att && att->Texture) {
               printf("Tex %u  tgt 0x%x  level %u  face %u",
                      att->Texture->Name,
                      att->Texture->Target,
                      att->TextureLevel,
                      att->CubeMapFace);
            }
            printf("\n");
         }
      }
   }

   if (!mask ||
       (srcX1 - srcX0) == 0 || (srcY1 - srcY0) == 0 ||
       (dstX1 - dstX0) == 0 || (dstY1 - dstY0) == 0) {
      return;
   }

   assert(ctx->Driver.BlitFramebuffer);
   ctx->Driver.BlitFramebuffer(ctx, readFb, drawFb,
                               srcX0, srcY0, srcX1, srcY1,
                               dstX0, dstY0, dstX1, dstY1,
                               mask, filter);
}


static void
blit_framebuffer_err(struct gl_context *ctx,
                     struct gl_framebuffer *readFb,
                     struct gl_framebuffer *drawFb,
                     GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                     GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                     GLbitfield mask, GLenum filter, const char *func)
{
   /* We are wrapping the err variant of the always inlined
    * blit_framebuffer() to avoid inlining it in every caller.
    */
   blit_framebuffer(ctx, readFb, drawFb, srcX0, srcY0, srcX1, srcY1,
                    dstX0, dstY0, dstX1, dstY1, mask, filter, false, func);
}


/**
 * Blit rectangular region, optionally from one framebuffer to another.
 *
 * Note, if the src buffer is multisampled and the dest is not, this is
 * when the samples must be resolved to a single color.
 */
void GLAPIENTRY
_mesa_BlitFramebuffer_no_error(GLint srcX0, GLint srcY0, GLint srcX1,
                               GLint srcY1, GLint dstX0, GLint dstY0,
                               GLint dstX1, GLint dstY1,
                               GLbitfield mask, GLenum filter)
{
   GET_CURRENT_CONTEXT(ctx);

   blit_framebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
                    srcX0, srcY0, srcX1, srcY1,
                    dstX0, dstY0, dstX1, dstY1,
                    mask, filter, true, "glBlitFramebuffer");
}


void GLAPIENTRY
_mesa_BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                      GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                      GLbitfield mask, GLenum filter)
{
   GET_CURRENT_CONTEXT(ctx);

   if (MESA_VERBOSE & VERBOSE_API)
      _mesa_debug(ctx,
                  "glBlitFramebuffer(%d, %d, %d, %d, "
                  " %d, %d, %d, %d, 0x%x, %s)\n",
                  srcX0, srcY0, srcX1, srcY1,
                  dstX0, dstY0, dstX1, dstY1,
                  mask, _mesa_enum_to_string(filter));

   blit_framebuffer_err(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
                        srcX0, srcY0, srcX1, srcY1,
                        dstX0, dstY0, dstX1, dstY1,
                        mask, filter, "glBlitFramebuffer");
}


static ALWAYS_INLINE void
blit_named_framebuffer(struct gl_context *ctx,
                       GLuint readFramebuffer, GLuint drawFramebuffer,
                       GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                       GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                       GLbitfield mask, GLenum filter, bool no_error)
{
   struct gl_framebuffer *readFb, *drawFb;

   /*
    * According to PDF page 533 of the OpenGL 4.5 core spec (30.10.2014,
    * Section 18.3 Copying Pixels):
    *   "... if readFramebuffer or drawFramebuffer is zero (for
    *   BlitNamedFramebuffer), then the default read or draw framebuffer is
    *   used as the corresponding source or destination framebuffer,
    *   respectively."
    */
   if (readFramebuffer) {
      if (no_error) {
         readFb = _mesa_lookup_framebuffer(ctx, readFramebuffer);
      } else {
         readFb = _mesa_lookup_framebuffer_err(ctx, readFramebuffer,
                                               "glBlitNamedFramebuffer");
         if (!readFb)
            return;
      }
   } else {
      readFb = ctx->WinSysReadBuffer;
   }

   if (drawFramebuffer) {
      if (no_error) {
         drawFb = _mesa_lookup_framebuffer(ctx, drawFramebuffer);
      } else {
         drawFb = _mesa_lookup_framebuffer_err(ctx, drawFramebuffer,
                                               "glBlitNamedFramebuffer");
         if (!drawFb)
            return;
      }
   } else {
      drawFb = ctx->WinSysDrawBuffer;
   }

   blit_framebuffer(ctx, readFb, drawFb,
                    srcX0, srcY0, srcX1, srcY1,
                    dstX0, dstY0, dstX1, dstY1,
                    mask, filter, no_error, "glBlitNamedFramebuffer");
}


void GLAPIENTRY
_mesa_BlitNamedFramebuffer_no_error(GLuint readFramebuffer,
                                    GLuint drawFramebuffer,
                                    GLint srcX0, GLint srcY0,
                                    GLint srcX1, GLint srcY1,
                                    GLint dstX0, GLint dstY0,
                                    GLint dstX1, GLint dstY1,
                                    GLbitfield mask, GLenum filter)
{
   GET_CURRENT_CONTEXT(ctx);

   blit_named_framebuffer(ctx, readFramebuffer, drawFramebuffer,
                          srcX0, srcY0, srcX1, srcY1,
                          dstX0, dstY0, dstX1, dstY1,
                          mask, filter, true);
}


void GLAPIENTRY
_mesa_BlitNamedFramebuffer(GLuint readFramebuffer, GLuint drawFramebuffer,
                           GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                           GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                           GLbitfield mask, GLenum filter)
{
   GET_CURRENT_CONTEXT(ctx);

   if (MESA_VERBOSE & VERBOSE_API)
      _mesa_debug(ctx,
                  "glBlitNamedFramebuffer(%u %u %d, %d, %d, %d, "
                  " %d, %d, %d, %d, 0x%x, %s)\n",
                  readFramebuffer, drawFramebuffer,
                  srcX0, srcY0, srcX1, srcY1,
                  dstX0, dstY0, dstX1, dstY1,
                  mask, _mesa_enum_to_string(filter));

   blit_named_framebuffer(ctx, readFramebuffer, drawFramebuffer,
                          srcX0, srcY0, srcX1, srcY1,
                          dstX0, dstY0, dstX1, dstY1,
                          mask, filter, false);
}