2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 1999-2013 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
27 * glBlitFramebuffer functions.
37 #include "framebuffer.h"
38 #include "glformats.h"
44 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
49 static const struct gl_renderbuffer_attachment
*
50 find_attachment(const struct gl_framebuffer
*fb
,
51 const struct gl_renderbuffer
*rb
)
54 for (i
= 0; i
< ARRAY_SIZE(fb
->Attachment
); i
++) {
55 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
56 return &fb
->Attachment
[i
];
63 * \return true if two regions overlap, false otherwise
66 _mesa_regions_overlap(int srcX0
, int srcY0
,
71 if (MAX2(srcX0
, srcX1
) < MIN2(dstX0
, dstX1
))
72 return false; /* dst completely right of src */
74 if (MAX2(dstX0
, dstX1
) < MIN2(srcX0
, srcX1
))
75 return false; /* dst completely left of src */
77 if (MAX2(srcY0
, srcY1
) < MIN2(dstY0
, dstY1
))
78 return false; /* dst completely above src */
80 if (MAX2(dstY0
, dstY1
) < MIN2(srcY0
, srcY1
))
81 return false; /* dst completely below src */
83 return true; /* some overlap */
88 * Helper function for checking if the datatypes of color buffers are
89 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
91 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
92 * and any of the following conditions hold:
93 * - The read buffer contains fixed-point or floating-point values and any
94 * draw buffer contains neither fixed-point nor floating-point values.
95 * - The read buffer contains unsigned integer values and any draw buffer
96 * does not contain unsigned integer values.
97 * - The read buffer contains signed integer values and any draw buffer
98 * does not contain signed integer values."
101 compatible_color_datatypes(mesa_format srcFormat
, mesa_format dstFormat
)
103 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
104 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
106 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
107 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
108 srcType
== GL_SIGNED_NORMALIZED
||
109 srcType
== GL_FLOAT
);
110 /* Boil any of those types down to GL_FLOAT */
114 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
115 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
116 dstType
== GL_SIGNED_NORMALIZED
||
117 dstType
== GL_FLOAT
);
118 /* Boil any of those types down to GL_FLOAT */
122 return srcType
== dstType
;
127 compatible_resolve_formats(const struct gl_renderbuffer
*readRb
,
128 const struct gl_renderbuffer
*drawRb
)
130 GLenum readFormat
, drawFormat
;
132 /* This checks whether the internal formats are compatible rather than the
133 * Mesa format for two reasons:
135 * • Under some circumstances, the user may request e.g. two GL_RGBA8
136 * textures and get two entirely different Mesa formats like RGBA8888 and
137 * ARGB8888. Drivers behaving like that should be able to cope with
138 * non-matching formats by themselves, because it's not the user's fault.
140 * • Picking two different internal formats can end up with the same Mesa
141 * format. For example the driver might be simulating GL_RGB textures
142 * with GL_RGBA internally and in that case both internal formats would
143 * end up with RGBA8888.
145 * This function is used to generate a GL error according to the spec so in
146 * both cases we want to be looking at the application-level format, which
149 * Blits between linear and sRGB formats are also allowed.
151 readFormat
= _mesa_get_nongeneric_internalformat(readRb
->InternalFormat
);
152 drawFormat
= _mesa_get_nongeneric_internalformat(drawRb
->InternalFormat
);
153 readFormat
= _mesa_get_linear_internalformat(readFormat
);
154 drawFormat
= _mesa_get_linear_internalformat(drawFormat
);
156 if (readFormat
== drawFormat
) {
165 is_valid_blit_filter(const struct gl_context
*ctx
, GLenum filter
)
171 case GL_SCALED_RESOLVE_FASTEST_EXT
:
172 case GL_SCALED_RESOLVE_NICEST_EXT
:
173 return ctx
->Extensions
.EXT_framebuffer_multisample_blit_scaled
;
181 _mesa_blit_framebuffer(struct gl_context
*ctx
,
182 struct gl_framebuffer
*readFb
,
183 struct gl_framebuffer
*drawFb
,
184 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
185 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
186 GLbitfield mask
, GLenum filter
, const char *func
)
188 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
189 GL_DEPTH_BUFFER_BIT
|
190 GL_STENCIL_BUFFER_BIT
);
192 FLUSH_VERTICES(ctx
, 0);
194 /* Update completeness status of readFb and drawFb. */
195 _mesa_update_framebuffer(ctx
, readFb
, drawFb
);
197 /* Make sure drawFb has an initialized bounding box. */
198 _mesa_update_draw_buffer_bounds(ctx
, drawFb
);
200 if (!readFb
|| !drawFb
) {
201 /* This will normally never happen but someday we may want to
202 * support MakeCurrent() with no drawables.
207 /* check for complete framebuffers */
208 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
209 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
210 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
211 "%s(incomplete draw/read buffers)", func
);
215 if (!is_valid_blit_filter(ctx
, filter
)) {
216 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid filter %s)", func
,
217 _mesa_enum_to_string(filter
));
221 if ((filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
222 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) &&
223 (readFb
->Visual
.samples
== 0 || drawFb
->Visual
.samples
> 0)) {
224 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s: invalid samples)", func
,
225 _mesa_enum_to_string(filter
));
229 if (mask
& ~legalMaskBits
) {
230 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid mask bits set)", func
);
234 /* depth/stencil must be blitted with nearest filtering */
235 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
236 && filter
!= GL_NEAREST
) {
237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
238 "%s(depth/stencil requires GL_NEAREST filter)", func
);
242 /* get color read/draw renderbuffers */
243 if (mask
& GL_COLOR_BUFFER_BIT
) {
244 const GLuint numColorDrawBuffers
= drawFb
->_NumColorDrawBuffers
;
245 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
246 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
249 /* From the EXT_framebuffer_object spec:
251 * "If a buffer is specified in <mask> and does not exist in both
252 * the read and draw framebuffers, the corresponding bit is silently
255 if (!colorReadRb
|| numColorDrawBuffers
== 0) {
256 mask
&= ~GL_COLOR_BUFFER_BIT
;
259 for (i
= 0; i
< numColorDrawBuffers
; i
++) {
260 colorDrawRb
= drawFb
->_ColorDrawBuffers
[i
];
264 /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
265 * ES 3.0.1 spec says:
267 * "If the source and destination buffers are identical, an
268 * INVALID_OPERATION error is generated. Different mipmap
269 * levels of a texture, different layers of a three-
270 * dimensional texture or two-dimensional array texture, and
271 * different faces of a cube map texture do not constitute
272 * identical buffers."
274 if (_mesa_is_gles3(ctx
) && (colorDrawRb
== colorReadRb
)) {
275 _mesa_error(ctx
, GL_INVALID_OPERATION
,
276 "%s(source and destination color "
277 "buffer cannot be the same)", func
);
281 if (!compatible_color_datatypes(colorReadRb
->Format
,
282 colorDrawRb
->Format
)) {
283 _mesa_error(ctx
, GL_INVALID_OPERATION
,
284 "%s(color buffer datatypes mismatch)", func
);
287 /* extra checks for multisample copies... */
288 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
289 /* color formats must match on GLES. This isn't checked on
290 * desktop GL because the GL 4.4 spec was changed to allow it.
291 * In the section entitled “Changes in the released
292 * Specification of July 22, 2013” it says:
294 * “Relax BlitFramebuffer in section 18.3.1 so that format
295 * conversion can take place during multisample blits, since
296 * drivers already allow this and some apps depend on it.”
298 if (_mesa_is_gles(ctx
) &&
299 !compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
300 _mesa_error(ctx
, GL_INVALID_OPERATION
,
301 "%s(bad src/dst multisample pixel formats)", func
);
306 if (filter
!= GL_NEAREST
) {
307 /* From EXT_framebuffer_multisample_blit_scaled specification:
308 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
309 * if filter is not NEAREST and read buffer contains integer data."
311 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
312 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
313 _mesa_error(ctx
, GL_INVALID_OPERATION
,
314 "%s(integer color type)", func
);
321 if (mask
& GL_STENCIL_BUFFER_BIT
) {
322 struct gl_renderbuffer
*readRb
=
323 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
324 struct gl_renderbuffer
*drawRb
=
325 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
327 /* From the EXT_framebuffer_object spec:
329 * "If a buffer is specified in <mask> and does not exist in both
330 * the read and draw framebuffers, the corresponding bit is silently
333 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
334 mask
&= ~GL_STENCIL_BUFFER_BIT
;
337 int read_z_bits
, draw_z_bits
;
339 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
341 "%s(source and destination stencil "
342 "buffer cannot be the same)", func
);
346 if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
347 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
348 /* There is no need to check the stencil datatype here, because
349 * there is only one: GL_UNSIGNED_INT.
351 _mesa_error(ctx
, GL_INVALID_OPERATION
,
352 "%s(stencil attachment format mismatch)", func
);
356 read_z_bits
= _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
);
357 draw_z_bits
= _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
);
359 /* If both buffers also have depth data, the depth formats must match
360 * as well. If one doesn't have depth, it's not blitted, so we should
361 * ignore the depth format check.
363 if (read_z_bits
> 0 && draw_z_bits
> 0 &&
364 (read_z_bits
!= draw_z_bits
||
365 _mesa_get_format_datatype(readRb
->Format
) !=
366 _mesa_get_format_datatype(drawRb
->Format
))) {
368 _mesa_error(ctx
, GL_INVALID_OPERATION
,
369 "%s(stencil attachment depth format mismatch)", func
);
375 if (mask
& GL_DEPTH_BUFFER_BIT
) {
376 struct gl_renderbuffer
*readRb
=
377 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
378 struct gl_renderbuffer
*drawRb
=
379 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
381 /* From the EXT_framebuffer_object spec:
383 * "If a buffer is specified in <mask> and does not exist in both
384 * the read and draw framebuffers, the corresponding bit is silently
387 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
388 mask
&= ~GL_DEPTH_BUFFER_BIT
;
391 int read_s_bit
, draw_s_bit
;
393 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
394 _mesa_error(ctx
, GL_INVALID_OPERATION
,
395 "%s(source and destination depth "
396 "buffer cannot be the same)", func
);
400 if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
401 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
402 (_mesa_get_format_datatype(readRb
->Format
) !=
403 _mesa_get_format_datatype(drawRb
->Format
))) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
405 "%s(depth attachment format mismatch)", func
);
409 read_s_bit
= _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
);
410 draw_s_bit
= _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
);
412 /* If both buffers also have stencil data, the stencil formats must
413 * match as well. If one doesn't have stencil, it's not blitted, so
414 * we should ignore the stencil format check.
416 if (read_s_bit
> 0 && draw_s_bit
> 0 && read_s_bit
!= draw_s_bit
) {
417 _mesa_error(ctx
, GL_INVALID_OPERATION
,
418 "%s(depth attachment stencil bits mismatch)", func
);
425 if (_mesa_is_gles3(ctx
)) {
426 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
429 * "If SAMPLE_BUFFERS for the draw framebuffer is greater than zero,
430 * an INVALID_OPERATION error is generated."
432 if (drawFb
->Visual
.samples
> 0) {
433 _mesa_error(ctx
, GL_INVALID_OPERATION
,
434 "%s(destination samples must be 0)", func
);
438 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
441 * "If SAMPLE_BUFFERS for the read framebuffer is greater than zero,
442 * no copy is performed and an INVALID_OPERATION error is generated
443 * if the formats of the read and draw framebuffers are not
444 * identical or if the source and destination rectangles are not
445 * defined with the same (X0, Y0) and (X1, Y1) bounds."
447 * The format check was made above because desktop OpenGL has the same
450 if (readFb
->Visual
.samples
> 0
451 && (srcX0
!= dstX0
|| srcY0
!= dstY0
452 || srcX1
!= dstX1
|| srcY1
!= dstY1
)) {
453 _mesa_error(ctx
, GL_INVALID_OPERATION
,
454 "%s(bad src/dst multisample region)", func
);
458 if (readFb
->Visual
.samples
> 0 &&
459 drawFb
->Visual
.samples
> 0 &&
460 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
461 _mesa_error(ctx
, GL_INVALID_OPERATION
,
462 "%s(mismatched samples)", func
);
466 /* extra checks for multisample copies... */
467 if ((readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) &&
468 (filter
== GL_NEAREST
|| filter
== GL_LINEAR
)) {
469 /* src and dest region sizes must be the same */
470 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
471 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
472 _mesa_error(ctx
, GL_INVALID_OPERATION
,
473 "%s(bad src/dst multisample region sizes)", func
);
481 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
482 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
485 printf("%s(%d, %d, %d, %d, %d, %d, %d, %d,"
486 " 0x%x, 0x%x)\n", func
,
487 srcX0
, srcY0
, srcX1
, srcY1
,
488 dstX0
, dstY0
, dstX1
, dstY1
,
492 const struct gl_renderbuffer_attachment
*att
;
494 att
= find_attachment(readFb
, colorReadRb
);
495 printf(" Src FBO %u RB %u (%dx%d) ",
496 readFb
->Name
, colorReadRb
->Name
,
497 colorReadRb
->Width
, colorReadRb
->Height
);
498 if (att
&& att
->Texture
) {
499 printf("Tex %u tgt 0x%x level %u face %u",
501 att
->Texture
->Target
,
507 /* Print all active color render buffers */
508 for (i
= 0; i
< drawFb
->_NumColorDrawBuffers
; i
++) {
509 colorDrawRb
= drawFb
->_ColorDrawBuffers
[i
];
513 att
= find_attachment(drawFb
, colorDrawRb
);
514 printf(" Dst FBO %u RB %u (%dx%d) ",
515 drawFb
->Name
, colorDrawRb
->Name
,
516 colorDrawRb
->Width
, colorDrawRb
->Height
);
517 if (att
&& att
->Texture
) {
518 printf("Tex %u tgt 0x%x level %u face %u",
520 att
->Texture
->Target
,
530 (srcX1
- srcX0
) == 0 || (srcY1
- srcY0
) == 0 ||
531 (dstX1
- dstX0
) == 0 || (dstY1
- dstY0
) == 0) {
535 assert(ctx
->Driver
.BlitFramebuffer
);
536 ctx
->Driver
.BlitFramebuffer(ctx
, readFb
, drawFb
,
537 srcX0
, srcY0
, srcX1
, srcY1
,
538 dstX0
, dstY0
, dstX1
, dstY1
,
544 * Blit rectangular region, optionally from one framebuffer to another.
546 * Note, if the src buffer is multisampled and the dest is not, this is
547 * when the samples must be resolved to a single color.
550 _mesa_BlitFramebuffer(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
551 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
552 GLbitfield mask
, GLenum filter
)
554 GET_CURRENT_CONTEXT(ctx
);
556 if (MESA_VERBOSE
& VERBOSE_API
)
558 "glBlitFramebuffer(%d, %d, %d, %d, "
559 " %d, %d, %d, %d, 0x%x, %s)\n",
560 srcX0
, srcY0
, srcX1
, srcY1
,
561 dstX0
, dstY0
, dstX1
, dstY1
,
562 mask
, _mesa_enum_to_string(filter
));
564 _mesa_blit_framebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
565 srcX0
, srcY0
, srcX1
, srcY1
,
566 dstX0
, dstY0
, dstX1
, dstY1
,
567 mask
, filter
, "glBlitFramebuffer");
572 _mesa_BlitNamedFramebuffer(GLuint readFramebuffer
, GLuint drawFramebuffer
,
573 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
574 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
575 GLbitfield mask
, GLenum filter
)
577 GET_CURRENT_CONTEXT(ctx
);
578 struct gl_framebuffer
*readFb
, *drawFb
;
580 if (MESA_VERBOSE
& VERBOSE_API
)
582 "glBlitNamedFramebuffer(%u %u %d, %d, %d, %d, "
583 " %d, %d, %d, %d, 0x%x, %s)\n",
584 readFramebuffer
, drawFramebuffer
,
585 srcX0
, srcY0
, srcX1
, srcY1
,
586 dstX0
, dstY0
, dstX1
, dstY1
,
587 mask
, _mesa_enum_to_string(filter
));
590 * According to PDF page 533 of the OpenGL 4.5 core spec (30.10.2014,
591 * Section 18.3 Copying Pixels):
592 * "... if readFramebuffer or drawFramebuffer is zero (for
593 * BlitNamedFramebuffer), then the default read or draw framebuffer is
594 * used as the corresponding source or destination framebuffer,
597 if (readFramebuffer
) {
598 readFb
= _mesa_lookup_framebuffer_err(ctx
, readFramebuffer
,
599 "glBlitNamedFramebuffer");
604 readFb
= ctx
->WinSysReadBuffer
;
606 if (drawFramebuffer
) {
607 drawFb
= _mesa_lookup_framebuffer_err(ctx
, drawFramebuffer
,
608 "glBlitNamedFramebuffer");
613 drawFb
= ctx
->WinSysDrawBuffer
;
615 _mesa_blit_framebuffer(ctx
, readFb
, drawFb
,
616 srcX0
, srcY0
, srcX1
, srcY1
,
617 dstX0
, dstY0
, dstX1
, dstY1
,
618 mask
, filter
, "glBlitNamedFramebuffer");