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.
36 #include "glformats.h"
41 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
46 static const struct gl_renderbuffer_attachment
*
47 find_attachment(const struct gl_framebuffer
*fb
,
48 const struct gl_renderbuffer
*rb
)
51 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
52 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
53 return &fb
->Attachment
[i
];
60 * Helper function for checking if the datatypes of color buffers are
61 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
63 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
64 * and any of the following conditions hold:
65 * - The read buffer contains fixed-point or floating-point values and any
66 * draw buffer contains neither fixed-point nor floating-point values.
67 * - The read buffer contains unsigned integer values and any draw buffer
68 * does not contain unsigned integer values.
69 * - The read buffer contains signed integer values and any draw buffer
70 * does not contain signed integer values."
73 compatible_color_datatypes(mesa_format srcFormat
, mesa_format dstFormat
)
75 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
76 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
78 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
79 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
80 srcType
== GL_SIGNED_NORMALIZED
||
82 /* Boil any of those types down to GL_FLOAT */
86 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
87 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
88 dstType
== GL_SIGNED_NORMALIZED
||
90 /* Boil any of those types down to GL_FLOAT */
94 return srcType
== dstType
;
99 compatible_resolve_formats(const struct gl_renderbuffer
*readRb
,
100 const struct gl_renderbuffer
*drawRb
)
102 GLenum readFormat
, drawFormat
;
104 /* The simple case where we know the backing Mesa formats are the same.
106 if (_mesa_get_srgb_format_linear(readRb
->Format
) ==
107 _mesa_get_srgb_format_linear(drawRb
->Format
)) {
111 /* The Mesa formats are different, so we must check whether the internal
112 * formats are compatible.
114 * Under some circumstances, the user may request e.g. two GL_RGBA8
115 * textures and get two entirely different Mesa formats like RGBA8888 and
116 * ARGB8888. Drivers behaving like that should be able to cope with
117 * non-matching formats by themselves, because it's not the user's fault.
119 * Blits between linear and sRGB formats are also allowed.
121 readFormat
= _mesa_get_nongeneric_internalformat(readRb
->InternalFormat
);
122 drawFormat
= _mesa_get_nongeneric_internalformat(drawRb
->InternalFormat
);
123 readFormat
= _mesa_get_linear_internalformat(readFormat
);
124 drawFormat
= _mesa_get_linear_internalformat(drawFormat
);
126 if (readFormat
== drawFormat
) {
135 is_valid_blit_filter(const struct gl_context
*ctx
, GLenum filter
)
141 case GL_SCALED_RESOLVE_FASTEST_EXT
:
142 case GL_SCALED_RESOLVE_NICEST_EXT
:
143 return ctx
->Extensions
.EXT_framebuffer_multisample_blit_scaled
;
151 * Blit rectangular region, optionally from one framebuffer to another.
153 * Note, if the src buffer is multisampled and the dest is not, this is
154 * when the samples must be resolved to a single color.
157 _mesa_BlitFramebuffer(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
158 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
159 GLbitfield mask
, GLenum filter
)
161 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
162 GL_DEPTH_BUFFER_BIT
|
163 GL_STENCIL_BUFFER_BIT
);
164 const struct gl_framebuffer
*readFb
, *drawFb
;
165 GET_CURRENT_CONTEXT(ctx
);
167 FLUSH_VERTICES(ctx
, 0);
169 if (MESA_VERBOSE
& VERBOSE_API
)
171 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
172 srcX0
, srcY0
, srcX1
, srcY1
,
173 dstX0
, dstY0
, dstX1
, dstY1
,
174 mask
, _mesa_lookup_enum_by_nr(filter
));
177 _mesa_update_state(ctx
);
180 readFb
= ctx
->ReadBuffer
;
181 drawFb
= ctx
->DrawBuffer
;
183 if (!readFb
|| !drawFb
) {
184 /* This will normally never happen but someday we may want to
185 * support MakeCurrent() with no drawables.
190 /* check for complete framebuffers */
191 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
192 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
193 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
194 "glBlitFramebufferEXT(incomplete draw/read buffers)");
198 if (!is_valid_blit_filter(ctx
, filter
)) {
199 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(%s)",
200 _mesa_lookup_enum_by_nr(filter
));
204 if ((filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
205 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) &&
206 (readFb
->Visual
.samples
== 0 || drawFb
->Visual
.samples
> 0)) {
207 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT(%s)",
208 _mesa_lookup_enum_by_nr(filter
));
212 if (mask
& ~legalMaskBits
) {
213 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
217 /* depth/stencil must be blitted with nearest filtering */
218 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
219 && filter
!= GL_NEAREST
) {
220 _mesa_error(ctx
, GL_INVALID_OPERATION
,
221 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
225 /* get color read/draw renderbuffers */
226 if (mask
& GL_COLOR_BUFFER_BIT
) {
227 const GLuint numColorDrawBuffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
228 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
229 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
232 /* From the EXT_framebuffer_object spec:
234 * "If a buffer is specified in <mask> and does not exist in both
235 * the read and draw framebuffers, the corresponding bit is silently
238 if (!colorReadRb
|| numColorDrawBuffers
== 0) {
239 mask
&= ~GL_COLOR_BUFFER_BIT
;
242 for (i
= 0; i
< numColorDrawBuffers
; i
++) {
243 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
247 /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
248 * ES 3.0.1 spec says:
250 * "If the source and destination buffers are identical, an
251 * INVALID_OPERATION error is generated. Different mipmap
252 * levels of a texture, different layers of a three-
253 * dimensional texture or two-dimensional array texture, and
254 * different faces of a cube map texture do not constitute
255 * identical buffers."
257 if (_mesa_is_gles3(ctx
) && (colorDrawRb
== colorReadRb
)) {
258 _mesa_error(ctx
, GL_INVALID_OPERATION
,
259 "glBlitFramebuffer(source and destination color "
260 "buffer cannot be the same)");
264 if (!compatible_color_datatypes(colorReadRb
->Format
,
265 colorDrawRb
->Format
)) {
266 _mesa_error(ctx
, GL_INVALID_OPERATION
,
267 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
270 /* extra checks for multisample copies... */
271 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
272 /* color formats must match */
273 if (!compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
274 _mesa_error(ctx
, GL_INVALID_OPERATION
,
275 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
280 if (filter
!= GL_NEAREST
) {
281 /* From EXT_framebuffer_multisample_blit_scaled specification:
282 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
283 * if filter is not NEAREST and read buffer contains integer data."
285 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
286 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
287 _mesa_error(ctx
, GL_INVALID_OPERATION
,
288 "glBlitFramebufferEXT(integer color type)");
295 if (mask
& GL_STENCIL_BUFFER_BIT
) {
296 struct gl_renderbuffer
*readRb
=
297 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
298 struct gl_renderbuffer
*drawRb
=
299 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
301 /* From the EXT_framebuffer_object spec:
303 * "If a buffer is specified in <mask> and does not exist in both
304 * the read and draw framebuffers, the corresponding bit is silently
307 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
308 mask
&= ~GL_STENCIL_BUFFER_BIT
;
311 int read_z_bits
, draw_z_bits
;
313 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
314 _mesa_error(ctx
, GL_INVALID_OPERATION
,
315 "glBlitFramebuffer(source and destination stencil "
316 "buffer cannot be the same)");
320 if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
321 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
322 /* There is no need to check the stencil datatype here, because
323 * there is only one: GL_UNSIGNED_INT.
325 _mesa_error(ctx
, GL_INVALID_OPERATION
,
326 "glBlitFramebuffer(stencil attachment format mismatch)");
330 read_z_bits
= _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
);
331 draw_z_bits
= _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
);
333 /* If both buffers also have depth data, the depth formats must match
334 * as well. If one doesn't have depth, it's not blitted, so we should
335 * ignore the depth format check.
337 if (read_z_bits
> 0 && draw_z_bits
> 0 &&
338 (read_z_bits
!= draw_z_bits
||
339 _mesa_get_format_datatype(readRb
->Format
) !=
340 _mesa_get_format_datatype(drawRb
->Format
))) {
342 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
343 "(stencil attachment depth format mismatch)");
349 if (mask
& GL_DEPTH_BUFFER_BIT
) {
350 struct gl_renderbuffer
*readRb
=
351 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
352 struct gl_renderbuffer
*drawRb
=
353 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
355 /* From the EXT_framebuffer_object spec:
357 * "If a buffer is specified in <mask> and does not exist in both
358 * the read and draw framebuffers, the corresponding bit is silently
361 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
362 mask
&= ~GL_DEPTH_BUFFER_BIT
;
365 int read_s_bit
, draw_s_bit
;
367 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
368 _mesa_error(ctx
, GL_INVALID_OPERATION
,
369 "glBlitFramebuffer(source and destination depth "
370 "buffer cannot be the same)");
374 if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
375 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
376 (_mesa_get_format_datatype(readRb
->Format
) !=
377 _mesa_get_format_datatype(drawRb
->Format
))) {
378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
379 "glBlitFramebuffer(depth attachment format mismatch)");
383 read_s_bit
= _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
);
384 draw_s_bit
= _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
);
386 /* If both buffers also have stencil data, the stencil formats must
387 * match as well. If one doesn't have stencil, it's not blitted, so
388 * we should ignore the stencil format check.
390 if (read_s_bit
> 0 && draw_s_bit
> 0 && read_s_bit
!= draw_s_bit
) {
391 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
392 "(depth attachment stencil bits mismatch)");
399 if (_mesa_is_gles3(ctx
)) {
400 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
403 * "If SAMPLE_BUFFERS for the draw framebuffer is greater than zero,
404 * an INVALID_OPERATION error is generated."
406 if (drawFb
->Visual
.samples
> 0) {
407 _mesa_error(ctx
, GL_INVALID_OPERATION
,
408 "glBlitFramebuffer(destination samples must be 0)");
412 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
415 * "If SAMPLE_BUFFERS for the read framebuffer is greater than zero,
416 * no copy is performed and an INVALID_OPERATION error is generated
417 * if the formats of the read and draw framebuffers are not
418 * identical or if the source and destination rectangles are not
419 * defined with the same (X0, Y0) and (X1, Y1) bounds."
421 * The format check was made above because desktop OpenGL has the same
424 if (readFb
->Visual
.samples
> 0
425 && (srcX0
!= dstX0
|| srcY0
!= dstY0
426 || srcX1
!= dstX1
|| srcY1
!= dstY1
)) {
427 _mesa_error(ctx
, GL_INVALID_OPERATION
,
428 "glBlitFramebuffer(bad src/dst multisample region)");
432 if (readFb
->Visual
.samples
> 0 &&
433 drawFb
->Visual
.samples
> 0 &&
434 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
435 _mesa_error(ctx
, GL_INVALID_OPERATION
,
436 "glBlitFramebufferEXT(mismatched samples)");
440 /* extra checks for multisample copies... */
441 if ((readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) &&
442 (filter
== GL_NEAREST
|| filter
== GL_LINEAR
)) {
443 /* src and dest region sizes must be the same */
444 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
445 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
446 _mesa_error(ctx
, GL_INVALID_OPERATION
,
447 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
455 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
456 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
459 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
461 srcX0
, srcY0
, srcX1
, srcY1
,
462 dstX0
, dstY0
, dstX1
, dstY1
,
465 const struct gl_renderbuffer_attachment
*att
;
467 att
= find_attachment(readFb
, colorReadRb
);
468 printf(" Src FBO %u RB %u (%dx%d) ",
469 readFb
->Name
, colorReadRb
->Name
,
470 colorReadRb
->Width
, colorReadRb
->Height
);
471 if (att
&& att
->Texture
) {
472 printf("Tex %u tgt 0x%x level %u face %u",
474 att
->Texture
->Target
,
480 /* Print all active color render buffers */
481 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
482 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
486 att
= find_attachment(drawFb
, colorDrawRb
);
487 printf(" Dst FBO %u RB %u (%dx%d) ",
488 drawFb
->Name
, colorDrawRb
->Name
,
489 colorDrawRb
->Width
, colorDrawRb
->Height
);
490 if (att
&& att
->Texture
) {
491 printf("Tex %u tgt 0x%x level %u face %u",
493 att
->Texture
->Target
,
503 (srcX1
- srcX0
) == 0 || (srcY1
- srcY0
) == 0 ||
504 (dstX1
- dstX0
) == 0 || (dstY1
- dstY0
) == 0) {
508 assert(ctx
->Driver
.BlitFramebuffer
);
509 ctx
->Driver
.BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
510 srcX0
, srcY0
, srcX1
, srcY1
,
511 dstX0
, dstY0
, dstX1
, dstY1
,