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 /* The simple case where we know the backing Mesa formats are the same.
134 if (_mesa_get_srgb_format_linear(readRb
->Format
) ==
135 _mesa_get_srgb_format_linear(drawRb
->Format
)) {
139 /* The Mesa formats are different, so we must check whether the internal
140 * formats are compatible.
142 * Under some circumstances, the user may request e.g. two GL_RGBA8
143 * textures and get two entirely different Mesa formats like RGBA8888 and
144 * ARGB8888. Drivers behaving like that should be able to cope with
145 * non-matching formats by themselves, because it's not the user's fault.
147 * Blits between linear and sRGB formats are also allowed.
149 readFormat
= _mesa_get_nongeneric_internalformat(readRb
->InternalFormat
);
150 drawFormat
= _mesa_get_nongeneric_internalformat(drawRb
->InternalFormat
);
151 readFormat
= _mesa_get_linear_internalformat(readFormat
);
152 drawFormat
= _mesa_get_linear_internalformat(drawFormat
);
154 if (readFormat
== drawFormat
) {
163 is_valid_blit_filter(const struct gl_context
*ctx
, GLenum filter
)
169 case GL_SCALED_RESOLVE_FASTEST_EXT
:
170 case GL_SCALED_RESOLVE_NICEST_EXT
:
171 return ctx
->Extensions
.EXT_framebuffer_multisample_blit_scaled
;
179 _mesa_blit_framebuffer(struct gl_context
*ctx
,
180 struct gl_framebuffer
*readFb
,
181 struct gl_framebuffer
*drawFb
,
182 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
183 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
184 GLbitfield mask
, GLenum filter
, const char *func
)
186 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
187 GL_DEPTH_BUFFER_BIT
|
188 GL_STENCIL_BUFFER_BIT
);
190 FLUSH_VERTICES(ctx
, 0);
192 /* Update completeness status of readFb and drawFb. */
193 _mesa_update_framebuffer(ctx
, readFb
, drawFb
);
195 /* Make sure drawFb has an initialized bounding box. */
196 _mesa_update_draw_buffer_bounds(ctx
, drawFb
);
198 if (!readFb
|| !drawFb
) {
199 /* This will normally never happen but someday we may want to
200 * support MakeCurrent() with no drawables.
205 /* check for complete framebuffers */
206 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
207 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
208 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
209 "%s(incomplete draw/read buffers)", func
);
213 if (!is_valid_blit_filter(ctx
, filter
)) {
214 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid filter %s)", func
,
215 _mesa_enum_to_string(filter
));
219 if ((filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
220 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) &&
221 (readFb
->Visual
.samples
== 0 || drawFb
->Visual
.samples
> 0)) {
222 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s: invalid samples)", func
,
223 _mesa_enum_to_string(filter
));
227 if (mask
& ~legalMaskBits
) {
228 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid mask bits set)", func
);
232 /* depth/stencil must be blitted with nearest filtering */
233 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
234 && filter
!= GL_NEAREST
) {
235 _mesa_error(ctx
, GL_INVALID_OPERATION
,
236 "%s(depth/stencil requires GL_NEAREST filter)", func
);
240 /* get color read/draw renderbuffers */
241 if (mask
& GL_COLOR_BUFFER_BIT
) {
242 const GLuint numColorDrawBuffers
= drawFb
->_NumColorDrawBuffers
;
243 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
244 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
247 /* From the EXT_framebuffer_object spec:
249 * "If a buffer is specified in <mask> and does not exist in both
250 * the read and draw framebuffers, the corresponding bit is silently
253 if (!colorReadRb
|| numColorDrawBuffers
== 0) {
254 mask
&= ~GL_COLOR_BUFFER_BIT
;
257 for (i
= 0; i
< numColorDrawBuffers
; i
++) {
258 colorDrawRb
= drawFb
->_ColorDrawBuffers
[i
];
262 /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
263 * ES 3.0.1 spec says:
265 * "If the source and destination buffers are identical, an
266 * INVALID_OPERATION error is generated. Different mipmap
267 * levels of a texture, different layers of a three-
268 * dimensional texture or two-dimensional array texture, and
269 * different faces of a cube map texture do not constitute
270 * identical buffers."
272 if (_mesa_is_gles3(ctx
) && (colorDrawRb
== colorReadRb
)) {
273 _mesa_error(ctx
, GL_INVALID_OPERATION
,
274 "%s(source and destination color "
275 "buffer cannot be the same)", func
);
279 if (!compatible_color_datatypes(colorReadRb
->Format
,
280 colorDrawRb
->Format
)) {
281 _mesa_error(ctx
, GL_INVALID_OPERATION
,
282 "%s(color buffer datatypes mismatch)", func
);
285 /* extra checks for multisample copies... */
286 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
287 /* color formats must match */
288 if (!compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
289 _mesa_error(ctx
, GL_INVALID_OPERATION
,
290 "%s(bad src/dst multisample pixel formats)", func
);
295 if (filter
!= GL_NEAREST
) {
296 /* From EXT_framebuffer_multisample_blit_scaled specification:
297 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
298 * if filter is not NEAREST and read buffer contains integer data."
300 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
301 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
302 _mesa_error(ctx
, GL_INVALID_OPERATION
,
303 "%s(integer color type)", func
);
310 if (mask
& GL_STENCIL_BUFFER_BIT
) {
311 struct gl_renderbuffer
*readRb
=
312 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
313 struct gl_renderbuffer
*drawRb
=
314 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
316 /* From the EXT_framebuffer_object spec:
318 * "If a buffer is specified in <mask> and does not exist in both
319 * the read and draw framebuffers, the corresponding bit is silently
322 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
323 mask
&= ~GL_STENCIL_BUFFER_BIT
;
326 int read_z_bits
, draw_z_bits
;
328 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
329 _mesa_error(ctx
, GL_INVALID_OPERATION
,
330 "%s(source and destination stencil "
331 "buffer cannot be the same)", func
);
335 if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
336 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
337 /* There is no need to check the stencil datatype here, because
338 * there is only one: GL_UNSIGNED_INT.
340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
341 "%s(stencil attachment format mismatch)", func
);
345 read_z_bits
= _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
);
346 draw_z_bits
= _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
);
348 /* If both buffers also have depth data, the depth formats must match
349 * as well. If one doesn't have depth, it's not blitted, so we should
350 * ignore the depth format check.
352 if (read_z_bits
> 0 && draw_z_bits
> 0 &&
353 (read_z_bits
!= draw_z_bits
||
354 _mesa_get_format_datatype(readRb
->Format
) !=
355 _mesa_get_format_datatype(drawRb
->Format
))) {
357 _mesa_error(ctx
, GL_INVALID_OPERATION
,
358 "%s(stencil attachment depth format mismatch)", func
);
364 if (mask
& GL_DEPTH_BUFFER_BIT
) {
365 struct gl_renderbuffer
*readRb
=
366 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
367 struct gl_renderbuffer
*drawRb
=
368 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
370 /* From the EXT_framebuffer_object spec:
372 * "If a buffer is specified in <mask> and does not exist in both
373 * the read and draw framebuffers, the corresponding bit is silently
376 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
377 mask
&= ~GL_DEPTH_BUFFER_BIT
;
380 int read_s_bit
, draw_s_bit
;
382 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
384 "%s(source and destination depth "
385 "buffer cannot be the same)", func
);
389 if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
390 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
391 (_mesa_get_format_datatype(readRb
->Format
) !=
392 _mesa_get_format_datatype(drawRb
->Format
))) {
393 _mesa_error(ctx
, GL_INVALID_OPERATION
,
394 "%s(depth attachment format mismatch)", func
);
398 read_s_bit
= _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
);
399 draw_s_bit
= _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
);
401 /* If both buffers also have stencil data, the stencil formats must
402 * match as well. If one doesn't have stencil, it's not blitted, so
403 * we should ignore the stencil format check.
405 if (read_s_bit
> 0 && draw_s_bit
> 0 && read_s_bit
!= draw_s_bit
) {
406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
407 "%s(depth attachment stencil bits mismatch)", func
);
414 if (_mesa_is_gles3(ctx
)) {
415 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
418 * "If SAMPLE_BUFFERS for the draw framebuffer is greater than zero,
419 * an INVALID_OPERATION error is generated."
421 if (drawFb
->Visual
.samples
> 0) {
422 _mesa_error(ctx
, GL_INVALID_OPERATION
,
423 "%s(destination samples must be 0)", func
);
427 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
430 * "If SAMPLE_BUFFERS for the read framebuffer is greater than zero,
431 * no copy is performed and an INVALID_OPERATION error is generated
432 * if the formats of the read and draw framebuffers are not
433 * identical or if the source and destination rectangles are not
434 * defined with the same (X0, Y0) and (X1, Y1) bounds."
436 * The format check was made above because desktop OpenGL has the same
439 if (readFb
->Visual
.samples
> 0
440 && (srcX0
!= dstX0
|| srcY0
!= dstY0
441 || srcX1
!= dstX1
|| srcY1
!= dstY1
)) {
442 _mesa_error(ctx
, GL_INVALID_OPERATION
,
443 "%s(bad src/dst multisample region)", func
);
447 if (readFb
->Visual
.samples
> 0 &&
448 drawFb
->Visual
.samples
> 0 &&
449 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
451 "%s(mismatched samples)", func
);
455 /* extra checks for multisample copies... */
456 if ((readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) &&
457 (filter
== GL_NEAREST
|| filter
== GL_LINEAR
)) {
458 /* src and dest region sizes must be the same */
459 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
460 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
461 _mesa_error(ctx
, GL_INVALID_OPERATION
,
462 "%s(bad src/dst multisample region sizes)", func
);
470 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
471 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
474 printf("%s(%d, %d, %d, %d, %d, %d, %d, %d,"
475 " 0x%x, 0x%x)\n", func
,
476 srcX0
, srcY0
, srcX1
, srcY1
,
477 dstX0
, dstY0
, dstX1
, dstY1
,
481 const struct gl_renderbuffer_attachment
*att
;
483 att
= find_attachment(readFb
, colorReadRb
);
484 printf(" Src FBO %u RB %u (%dx%d) ",
485 readFb
->Name
, colorReadRb
->Name
,
486 colorReadRb
->Width
, colorReadRb
->Height
);
487 if (att
&& att
->Texture
) {
488 printf("Tex %u tgt 0x%x level %u face %u",
490 att
->Texture
->Target
,
496 /* Print all active color render buffers */
497 for (i
= 0; i
< drawFb
->_NumColorDrawBuffers
; i
++) {
498 colorDrawRb
= drawFb
->_ColorDrawBuffers
[i
];
502 att
= find_attachment(drawFb
, colorDrawRb
);
503 printf(" Dst FBO %u RB %u (%dx%d) ",
504 drawFb
->Name
, colorDrawRb
->Name
,
505 colorDrawRb
->Width
, colorDrawRb
->Height
);
506 if (att
&& att
->Texture
) {
507 printf("Tex %u tgt 0x%x level %u face %u",
509 att
->Texture
->Target
,
519 (srcX1
- srcX0
) == 0 || (srcY1
- srcY0
) == 0 ||
520 (dstX1
- dstX0
) == 0 || (dstY1
- dstY0
) == 0) {
524 assert(ctx
->Driver
.BlitFramebuffer
);
525 ctx
->Driver
.BlitFramebuffer(ctx
, readFb
, drawFb
,
526 srcX0
, srcY0
, srcX1
, srcY1
,
527 dstX0
, dstY0
, dstX1
, dstY1
,
533 * Blit rectangular region, optionally from one framebuffer to another.
535 * Note, if the src buffer is multisampled and the dest is not, this is
536 * when the samples must be resolved to a single color.
539 _mesa_BlitFramebuffer(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
540 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
541 GLbitfield mask
, GLenum filter
)
543 GET_CURRENT_CONTEXT(ctx
);
545 if (MESA_VERBOSE
& VERBOSE_API
)
547 "glBlitFramebuffer(%d, %d, %d, %d, "
548 " %d, %d, %d, %d, 0x%x, %s)\n",
549 srcX0
, srcY0
, srcX1
, srcY1
,
550 dstX0
, dstY0
, dstX1
, dstY1
,
551 mask
, _mesa_enum_to_string(filter
));
553 _mesa_blit_framebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
554 srcX0
, srcY0
, srcX1
, srcY1
,
555 dstX0
, dstY0
, dstX1
, dstY1
,
556 mask
, filter
, "glBlitFramebuffer");
561 _mesa_BlitNamedFramebuffer(GLuint readFramebuffer
, GLuint drawFramebuffer
,
562 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
563 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
564 GLbitfield mask
, GLenum filter
)
566 GET_CURRENT_CONTEXT(ctx
);
567 struct gl_framebuffer
*readFb
, *drawFb
;
569 if (MESA_VERBOSE
& VERBOSE_API
)
571 "glBlitNamedFramebuffer(%u %u %d, %d, %d, %d, "
572 " %d, %d, %d, %d, 0x%x, %s)\n",
573 readFramebuffer
, drawFramebuffer
,
574 srcX0
, srcY0
, srcX1
, srcY1
,
575 dstX0
, dstY0
, dstX1
, dstY1
,
576 mask
, _mesa_enum_to_string(filter
));
579 * According to PDF page 533 of the OpenGL 4.5 core spec (30.10.2014,
580 * Section 18.3 Copying Pixels):
581 * "... if readFramebuffer or drawFramebuffer is zero (for
582 * BlitNamedFramebuffer), then the default read or draw framebuffer is
583 * used as the corresponding source or destination framebuffer,
586 if (readFramebuffer
) {
587 readFb
= _mesa_lookup_framebuffer_err(ctx
, readFramebuffer
,
588 "glBlitNamedFramebuffer");
593 readFb
= ctx
->WinSysReadBuffer
;
595 if (drawFramebuffer
) {
596 drawFb
= _mesa_lookup_framebuffer_err(ctx
, drawFramebuffer
,
597 "glBlitNamedFramebuffer");
602 drawFb
= ctx
->WinSysDrawBuffer
;
604 _mesa_blit_framebuffer(ctx
, readFb
, drawFb
,
605 srcX0
, srcY0
, srcX1
, srcY1
,
606 dstX0
, dstY0
, dstX1
, dstY1
,
607 mask
, filter
, "glBlitNamedFramebuffer");