2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * GL_EXT/ARB_framebuffer_object extensions
41 #include "framebuffer.h"
42 #include "glformats.h"
45 #include "mfeatures.h"
47 #include "renderbuffer.h"
53 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
60 * None of the GL_EXT_framebuffer_object functions are compiled into
67 * When glGenRender/FramebuffersEXT() is called we insert pointers to
68 * these placeholder objects into the hash table.
69 * Later, when the object ID is first bound, we replace the placeholder
70 * with the real frame/renderbuffer.
72 static struct gl_framebuffer DummyFramebuffer
;
73 static struct gl_renderbuffer DummyRenderbuffer
;
75 /* We bind this framebuffer when applications pass a NULL
76 * drawable/surface in make current. */
77 static struct gl_framebuffer IncompleteFramebuffer
;
81 delete_dummy_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
87 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
94 _mesa_init_fbobjects(struct gl_context
*ctx
)
96 _glthread_INIT_MUTEX(DummyFramebuffer
.Mutex
);
97 _glthread_INIT_MUTEX(DummyRenderbuffer
.Mutex
);
98 _glthread_INIT_MUTEX(IncompleteFramebuffer
.Mutex
);
99 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
100 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
101 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
104 struct gl_framebuffer
*
105 _mesa_get_incomplete_framebuffer(void)
107 return &IncompleteFramebuffer
;
111 * Helper routine for getting a gl_renderbuffer.
113 struct gl_renderbuffer
*
114 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
116 struct gl_renderbuffer
*rb
;
121 rb
= (struct gl_renderbuffer
*)
122 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
128 * Helper routine for getting a gl_framebuffer.
130 struct gl_framebuffer
*
131 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
133 struct gl_framebuffer
*fb
;
138 fb
= (struct gl_framebuffer
*)
139 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
145 * Mark the given framebuffer as invalid. This will force the
146 * test for framebuffer completeness to be done before the framebuffer
150 invalidate_framebuffer(struct gl_framebuffer
*fb
)
152 fb
->_Status
= 0; /* "indeterminate" */
157 * Return the gl_framebuffer object which corresponds to the given
158 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
159 * Check support for GL_EXT_framebuffer_blit to determine if certain
161 * \return gl_framebuffer pointer or NULL if target is illegal
163 static struct gl_framebuffer
*
164 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
166 bool have_fb_blit
= _mesa_is_gles3(ctx
) ||
167 (ctx
->Extensions
.EXT_framebuffer_blit
&& _mesa_is_desktop_gl(ctx
));
169 case GL_DRAW_FRAMEBUFFER
:
170 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
171 case GL_READ_FRAMEBUFFER
:
172 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
173 case GL_FRAMEBUFFER_EXT
:
174 return ctx
->DrawBuffer
;
182 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
183 * gl_renderbuffer_attachment object.
184 * This function is only used for user-created FB objects, not the
185 * default / window-system FB object.
186 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
187 * the depth buffer attachment point.
189 struct gl_renderbuffer_attachment
*
190 _mesa_get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
195 assert(_mesa_is_user_fbo(fb
));
197 switch (attachment
) {
198 case GL_COLOR_ATTACHMENT0_EXT
:
199 case GL_COLOR_ATTACHMENT1_EXT
:
200 case GL_COLOR_ATTACHMENT2_EXT
:
201 case GL_COLOR_ATTACHMENT3_EXT
:
202 case GL_COLOR_ATTACHMENT4_EXT
:
203 case GL_COLOR_ATTACHMENT5_EXT
:
204 case GL_COLOR_ATTACHMENT6_EXT
:
205 case GL_COLOR_ATTACHMENT7_EXT
:
206 case GL_COLOR_ATTACHMENT8_EXT
:
207 case GL_COLOR_ATTACHMENT9_EXT
:
208 case GL_COLOR_ATTACHMENT10_EXT
:
209 case GL_COLOR_ATTACHMENT11_EXT
:
210 case GL_COLOR_ATTACHMENT12_EXT
:
211 case GL_COLOR_ATTACHMENT13_EXT
:
212 case GL_COLOR_ATTACHMENT14_EXT
:
213 case GL_COLOR_ATTACHMENT15_EXT
:
214 /* Only OpenGL ES 1.x forbids color attachments other than
215 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
218 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
219 if (i
>= ctx
->Const
.MaxColorAttachments
220 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
223 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
224 case GL_DEPTH_STENCIL_ATTACHMENT
:
225 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
228 case GL_DEPTH_ATTACHMENT_EXT
:
229 return &fb
->Attachment
[BUFFER_DEPTH
];
230 case GL_STENCIL_ATTACHMENT_EXT
:
231 return &fb
->Attachment
[BUFFER_STENCIL
];
239 * As above, but only used for getting attachments of the default /
240 * window-system framebuffer (not user-created framebuffer objects).
242 static struct gl_renderbuffer_attachment
*
243 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
246 assert(_mesa_is_winsys_fbo(fb
));
248 if (_mesa_is_gles3(ctx
)) {
249 assert(attachment
== GL_BACK
||
250 attachment
== GL_DEPTH
||
251 attachment
== GL_STENCIL
);
252 switch (attachment
) {
254 /* Since there is no stereo rendering in ES 3.0, only return the
257 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
258 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
259 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
261 return &fb
->Attachment
[BUFFER_DEPTH
];
263 return &fb
->Attachment
[BUFFER_STENCIL
];
267 switch (attachment
) {
269 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
271 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
273 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
275 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
277 if (fb
->Visual
.numAuxBuffers
== 1) {
278 return &fb
->Attachment
[BUFFER_AUX0
];
282 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
284 * "If the default framebuffer is bound to target, then attachment must
285 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
286 * identifying a color buffer; DEPTH, identifying the depth buffer; or
287 * STENCIL, identifying the stencil buffer."
289 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
290 * language. However, revision #33 of the ARB_framebuffer_object spec
293 * "If the default framebuffer is bound to <target>, then <attachment>
294 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
295 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
296 * depth buffer, or the stencil buffer, and <pname> may be
297 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
298 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
300 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
301 * from glext.h, so shipping apps should not use those values.
303 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
304 * support queries of the window system FBO.
307 return &fb
->Attachment
[BUFFER_DEPTH
];
309 return &fb
->Attachment
[BUFFER_STENCIL
];
318 * Remove any texture or renderbuffer attached to the given attachment
319 * point. Update reference counts, etc.
322 _mesa_remove_attachment(struct gl_context
*ctx
,
323 struct gl_renderbuffer_attachment
*att
)
325 if (att
->Type
== GL_TEXTURE
) {
326 ASSERT(att
->Texture
);
327 if (ctx
->Driver
.FinishRenderTexture
) {
328 /* tell driver that we're done rendering to this texture. */
329 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
331 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
332 ASSERT(!att
->Texture
);
334 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
335 ASSERT(!att
->Texture
);
336 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
337 ASSERT(!att
->Renderbuffer
);
340 att
->Complete
= GL_TRUE
;
345 * Bind a texture object to an attachment point.
346 * The previous binding, if any, will be removed first.
349 _mesa_set_texture_attachment(struct gl_context
*ctx
,
350 struct gl_framebuffer
*fb
,
351 struct gl_renderbuffer_attachment
*att
,
352 struct gl_texture_object
*texObj
,
353 GLenum texTarget
, GLuint level
, GLuint zoffset
)
355 if (att
->Texture
== texObj
) {
356 /* re-attaching same texture */
357 ASSERT(att
->Type
== GL_TEXTURE
);
358 if (ctx
->Driver
.FinishRenderTexture
)
359 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
363 if (ctx
->Driver
.FinishRenderTexture
&& att
->Texture
)
364 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
365 _mesa_remove_attachment(ctx
, att
);
366 att
->Type
= GL_TEXTURE
;
367 assert(!att
->Texture
);
368 _mesa_reference_texobj(&att
->Texture
, texObj
);
371 /* always update these fields */
372 att
->TextureLevel
= level
;
373 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
374 att
->Zoffset
= zoffset
;
375 att
->Complete
= GL_FALSE
;
377 if (_mesa_get_attachment_teximage(att
)) {
378 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
381 invalidate_framebuffer(fb
);
386 * Bind a renderbuffer to an attachment point.
387 * The previous binding, if any, will be removed first.
390 _mesa_set_renderbuffer_attachment(struct gl_context
*ctx
,
391 struct gl_renderbuffer_attachment
*att
,
392 struct gl_renderbuffer
*rb
)
394 /* XXX check if re-doing same attachment, exit early */
395 _mesa_remove_attachment(ctx
, att
);
396 att
->Type
= GL_RENDERBUFFER_EXT
;
397 att
->Texture
= NULL
; /* just to be safe */
398 att
->Complete
= GL_FALSE
;
399 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
404 * Fallback for ctx->Driver.FramebufferRenderbuffer()
405 * Attach a renderbuffer object to a framebuffer object.
408 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
409 struct gl_framebuffer
*fb
,
410 GLenum attachment
, struct gl_renderbuffer
*rb
)
412 struct gl_renderbuffer_attachment
*att
;
414 _glthread_LOCK_MUTEX(fb
->Mutex
);
416 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
419 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
420 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
421 /* do stencil attachment here (depth already done above) */
422 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
424 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
426 rb
->AttachedAnytime
= GL_TRUE
;
429 _mesa_remove_attachment(ctx
, att
);
432 invalidate_framebuffer(fb
);
434 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
439 * Fallback for ctx->Driver.ValidateFramebuffer()
440 * Check if the renderbuffer's formats are supported by the software
442 * Drivers should probably override this.
445 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
448 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
449 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
451 switch (rb
->_BaseFormat
) {
453 case GL_LUMINANCE_ALPHA
:
458 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
462 switch (rb
->Format
) {
463 /* XXX This list is likely incomplete. */
464 case MESA_FORMAT_RGB9_E5_FLOAT
:
465 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
468 /* render buffer format is supported by software rendering */
480 att_incomplete(const char *msg
)
482 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
483 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
492 fbo_incomplete(const char *msg
, int index
)
494 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
495 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
501 * Is the given base format a legal format for a color renderbuffer?
504 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
506 switch (baseFormat
) {
511 case GL_LUMINANCE_ALPHA
:
514 return ctx
->API
== API_OPENGL_COMPAT
&&
515 ctx
->Extensions
.ARB_framebuffer_object
;
518 return ctx
->Extensions
.ARB_texture_rg
;
526 * Is the given base format a legal format for a depth/stencil renderbuffer?
529 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
531 switch (baseFormat
) {
532 case GL_DEPTH_COMPONENT
:
533 case GL_DEPTH_STENCIL_EXT
:
542 * Test if an attachment point is complete and update its Complete field.
543 * \param format if GL_COLOR, this is a color attachment point,
544 * if GL_DEPTH, this is a depth component attachment point,
545 * if GL_STENCIL, this is a stencil component attachment point.
548 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
549 struct gl_renderbuffer_attachment
*att
)
551 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
553 /* assume complete */
554 att
->Complete
= GL_TRUE
;
556 /* Look for reasons why the attachment might be incomplete */
557 if (att
->Type
== GL_TEXTURE
) {
558 const struct gl_texture_object
*texObj
= att
->Texture
;
559 struct gl_texture_image
*texImage
;
563 att_incomplete("no texobj");
564 att
->Complete
= GL_FALSE
;
568 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
570 att_incomplete("no teximage");
571 att
->Complete
= GL_FALSE
;
574 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
575 att_incomplete("teximage width/height=0");
576 printf("texobj = %u\n", texObj
->Name
);
577 printf("level = %d\n", att
->TextureLevel
);
578 att
->Complete
= GL_FALSE
;
581 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
582 att_incomplete("bad z offset");
583 att
->Complete
= GL_FALSE
;
587 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
589 if (format
== GL_COLOR
) {
590 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
591 att_incomplete("bad format");
592 att
->Complete
= GL_FALSE
;
595 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
596 att_incomplete("compressed internalformat");
597 att
->Complete
= GL_FALSE
;
601 else if (format
== GL_DEPTH
) {
602 if (baseFormat
== GL_DEPTH_COMPONENT
) {
605 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
606 ctx
->Extensions
.ARB_depth_texture
&&
607 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
611 att
->Complete
= GL_FALSE
;
612 att_incomplete("bad depth format");
617 ASSERT(format
== GL_STENCIL
);
618 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
619 ctx
->Extensions
.ARB_depth_texture
&&
620 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
624 /* no such thing as stencil-only textures */
625 att_incomplete("illegal stencil texture");
626 att
->Complete
= GL_FALSE
;
631 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
632 const GLenum baseFormat
=
633 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
635 ASSERT(att
->Renderbuffer
);
636 if (!att
->Renderbuffer
->InternalFormat
||
637 att
->Renderbuffer
->Width
< 1 ||
638 att
->Renderbuffer
->Height
< 1) {
639 att_incomplete("0x0 renderbuffer");
640 att
->Complete
= GL_FALSE
;
643 if (format
== GL_COLOR
) {
644 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
645 att_incomplete("bad renderbuffer color format");
646 att
->Complete
= GL_FALSE
;
650 else if (format
== GL_DEPTH
) {
651 if (baseFormat
== GL_DEPTH_COMPONENT
) {
654 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
655 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
659 att_incomplete("bad renderbuffer depth format");
660 att
->Complete
= GL_FALSE
;
665 assert(format
== GL_STENCIL
);
666 if (baseFormat
== GL_STENCIL_INDEX
) {
669 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
670 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
674 att
->Complete
= GL_FALSE
;
675 att_incomplete("bad renderbuffer stencil format");
681 ASSERT(att
->Type
== GL_NONE
);
689 * Test if the given framebuffer object is complete and update its
690 * Status field with the results.
691 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
692 * driver to make hardware-specific validation/completeness checks.
693 * Also update the framebuffer's Width and Height fields if the
694 * framebuffer is complete.
697 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
698 struct gl_framebuffer
*fb
)
701 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
702 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
703 GLint numSamples
= -1;
707 assert(_mesa_is_user_fbo(fb
));
709 /* we're changing framebuffer fields here */
710 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
716 /* Start at -2 to more easily loop over all attachment points.
721 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
722 struct gl_renderbuffer_attachment
*att
;
727 * XXX for ARB_fbo, only check color buffers that are named by
728 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
731 /* check for attachment completeness
734 att
= &fb
->Attachment
[BUFFER_DEPTH
];
735 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
736 if (!att
->Complete
) {
737 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
738 fbo_incomplete("depth attachment incomplete", -1);
743 att
= &fb
->Attachment
[BUFFER_STENCIL
];
744 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
745 if (!att
->Complete
) {
746 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
747 fbo_incomplete("stencil attachment incomplete", -1);
752 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
753 test_attachment_completeness(ctx
, GL_COLOR
, att
);
754 if (!att
->Complete
) {
755 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
756 fbo_incomplete("color attachment incomplete", i
);
761 /* get width, height, format of the renderbuffer/texture
763 if (att
->Type
== GL_TEXTURE
) {
764 const struct gl_texture_image
*texImg
=
765 _mesa_get_attachment_teximage(att
);
766 minWidth
= MIN2(minWidth
, texImg
->Width
);
767 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
768 minHeight
= MIN2(minHeight
, texImg
->Height
);
769 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
770 f
= texImg
->_BaseFormat
;
771 attFormat
= texImg
->TexFormat
;
773 if (!_mesa_is_legal_color_format(ctx
, f
) &&
774 !is_legal_depth_format(ctx
, f
)) {
775 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
776 fbo_incomplete("texture attachment incomplete", -1);
780 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
781 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
782 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
783 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
784 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
785 f
= att
->Renderbuffer
->InternalFormat
;
786 attFormat
= att
->Renderbuffer
->Format
;
790 assert(att
->Type
== GL_NONE
);
794 if (att
->Renderbuffer
&& numSamples
< 0) {
796 numSamples
= att
->Renderbuffer
->NumSamples
;
799 /* check if integer color */
800 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
802 /* Error-check width, height, format, samples
804 if (numImages
== 1) {
805 /* save format, num samples */
811 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
812 /* check that width, height, format are same */
813 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
814 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
815 fbo_incomplete("width or height mismatch", -1);
818 /* check that all color buffers are the same format */
819 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
820 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
821 fbo_incomplete("format mismatch", -1);
825 if (att
->Renderbuffer
&&
826 att
->Renderbuffer
->NumSamples
!= numSamples
) {
827 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
828 fbo_incomplete("inconsistant number of samples", i
);
833 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
835 if (att
->Type
== GL_RENDERBUFFER
&&
836 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
837 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
838 fbo_incomplete("unsupported renderbuffer format", i
);
843 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
844 /* Check that all DrawBuffers are present */
845 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
846 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
847 const struct gl_renderbuffer_attachment
*att
848 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
850 if (att
->Type
== GL_NONE
) {
851 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
852 fbo_incomplete("missing drawbuffer", j
);
858 /* Check that the ReadBuffer is present */
859 if (fb
->ColorReadBuffer
!= GL_NONE
) {
860 const struct gl_renderbuffer_attachment
*att
861 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
863 if (att
->Type
== GL_NONE
) {
864 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
865 fbo_incomplete("missing readbuffer", -1);
871 if (numImages
== 0) {
872 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
873 fbo_incomplete("no attachments", -1);
877 /* Provisionally set status = COMPLETE ... */
878 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
880 /* ... but the driver may say the FB is incomplete.
881 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
884 if (ctx
->Driver
.ValidateFramebuffer
) {
885 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
886 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
887 fbo_incomplete("driver marked FBO as incomplete", -1);
891 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
893 * Note that if ARB_framebuffer_object is supported and the attached
894 * renderbuffers/textures are different sizes, the framebuffer
895 * width/height will be set to the smallest width/height.
897 fb
->Width
= minWidth
;
898 fb
->Height
= minHeight
;
900 /* finally, update the visual info for the framebuffer */
901 _mesa_update_framebuffer_visual(ctx
, fb
);
907 _mesa_IsRenderbuffer(GLuint renderbuffer
)
909 GET_CURRENT_CONTEXT(ctx
);
910 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
912 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
913 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
921 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
923 struct gl_renderbuffer
*newRb
;
924 GET_CURRENT_CONTEXT(ctx
);
926 ASSERT_OUTSIDE_BEGIN_END(ctx
);
928 if (target
!= GL_RENDERBUFFER_EXT
) {
929 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
933 /* No need to flush here since the render buffer binding has no
934 * effect on rendering state.
938 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
939 if (newRb
== &DummyRenderbuffer
) {
940 /* ID was reserved, but no real renderbuffer object made yet */
944 && _mesa_is_desktop_gl(ctx
)
945 && ctx
->Extensions
.ARB_framebuffer_object
) {
946 /* All RB IDs must be Gen'd */
947 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
952 /* create new renderbuffer object */
953 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
955 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
958 ASSERT(newRb
->AllocStorage
);
959 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
960 newRb
->RefCount
= 1; /* referenced by hash table */
967 ASSERT(newRb
!= &DummyRenderbuffer
);
969 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
974 * If the given renderbuffer is anywhere attached to the framebuffer, detach
976 * This is used when a renderbuffer object is deleted.
977 * The spec calls for unbinding.
980 detach_renderbuffer(struct gl_context
*ctx
,
981 struct gl_framebuffer
*fb
,
982 struct gl_renderbuffer
*rb
)
985 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
986 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
987 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
990 invalidate_framebuffer(fb
);
995 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
998 GET_CURRENT_CONTEXT(ctx
);
1000 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1001 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1003 for (i
= 0; i
< n
; i
++) {
1004 if (renderbuffers
[i
] > 0) {
1005 struct gl_renderbuffer
*rb
;
1006 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1008 /* check if deleting currently bound renderbuffer object */
1009 if (rb
== ctx
->CurrentRenderbuffer
) {
1011 ASSERT(rb
->RefCount
>= 2);
1012 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1015 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1016 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1018 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1019 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1020 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1023 /* Remove from hash table immediately, to free the ID.
1024 * But the object will not be freed until it's no longer
1025 * referenced anywhere else.
1027 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1029 if (rb
!= &DummyRenderbuffer
) {
1030 /* no longer referenced by hash table */
1031 _mesa_reference_renderbuffer(&rb
, NULL
);
1040 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1042 GET_CURRENT_CONTEXT(ctx
);
1046 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1049 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1056 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1058 for (i
= 0; i
< n
; i
++) {
1059 GLuint name
= first
+ i
;
1060 renderbuffers
[i
] = name
;
1061 /* insert dummy placeholder into hash table */
1062 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1063 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1064 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1070 * Given an internal format token for a render buffer, return the
1071 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1072 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1073 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1075 * This is similar to _mesa_base_tex_format() but the set of valid
1076 * internal formats is different.
1078 * Note that even if a format is determined to be legal here, validation
1079 * of the FBO may fail if the format is not supported by the driver/GPU.
1081 * \param internalFormat as passed to glRenderbufferStorage()
1082 * \return the base internal format, or 0 if internalFormat is illegal
1085 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1088 * Notes: some formats such as alpha, luminance, etc. were added
1089 * with GL_ARB_framebuffer_object.
1091 switch (internalFormat
) {
1097 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1102 case GL_LUMINANCE12
:
1103 case GL_LUMINANCE16
:
1104 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1106 case GL_LUMINANCE_ALPHA
:
1107 case GL_LUMINANCE4_ALPHA4
:
1108 case GL_LUMINANCE6_ALPHA2
:
1109 case GL_LUMINANCE8_ALPHA8
:
1110 case GL_LUMINANCE12_ALPHA4
:
1111 case GL_LUMINANCE12_ALPHA12
:
1112 case GL_LUMINANCE16_ALPHA16
:
1113 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1114 ? GL_LUMINANCE_ALPHA
: 0;
1118 case GL_INTENSITY12
:
1119 case GL_INTENSITY16
:
1120 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1131 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1133 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1142 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1144 case GL_SRGB8_ALPHA8_EXT
:
1145 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1146 case GL_STENCIL_INDEX
:
1147 case GL_STENCIL_INDEX1_EXT
:
1148 case GL_STENCIL_INDEX4_EXT
:
1149 case GL_STENCIL_INDEX16_EXT
:
1150 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1151 * OpenGL ES, but Mesa does not currently support them.
1153 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1154 case GL_STENCIL_INDEX8_EXT
:
1155 return GL_STENCIL_INDEX
;
1156 case GL_DEPTH_COMPONENT
:
1157 case GL_DEPTH_COMPONENT32
:
1158 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1159 case GL_DEPTH_COMPONENT16
:
1160 case GL_DEPTH_COMPONENT24
:
1161 return GL_DEPTH_COMPONENT
;
1162 case GL_DEPTH_STENCIL_EXT
:
1163 return _mesa_is_desktop_gl(ctx
)
1164 && ctx
->Extensions
.EXT_packed_depth_stencil
1165 ? GL_DEPTH_STENCIL_EXT
: 0;
1166 case GL_DEPTH24_STENCIL8_EXT
:
1167 return ctx
->Extensions
.EXT_packed_depth_stencil
1168 ? GL_DEPTH_STENCIL_EXT
: 0;
1169 case GL_DEPTH_COMPONENT32F
:
1170 return ctx
->Version
>= 30
1171 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1172 ? GL_DEPTH_COMPONENT
: 0;
1173 case GL_DEPTH32F_STENCIL8
:
1174 return ctx
->Version
>= 30
1175 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1176 ? GL_DEPTH_STENCIL
: 0;
1179 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1182 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1186 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1189 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1191 /* signed normalized texture formats */
1195 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1200 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1204 case GL_RGB16_SNORM
:
1205 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1208 case GL_RGBA8_SNORM
:
1209 case GL_RGBA16_SNORM
:
1210 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1212 case GL_ALPHA_SNORM
:
1213 case GL_ALPHA8_SNORM
:
1214 case GL_ALPHA16_SNORM
:
1215 return ctx
->API
== API_OPENGL_COMPAT
&&
1216 ctx
->Extensions
.EXT_texture_snorm
&&
1217 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1220 return (_mesa_is_desktop_gl(ctx
) &&
1221 ctx
->Extensions
.ARB_texture_rg
&&
1222 ctx
->Extensions
.ARB_texture_float
) ? GL_RED
: 0;
1225 return (_mesa_is_desktop_gl(ctx
) &&
1226 ctx
->Extensions
.ARB_texture_rg
&&
1227 ctx
->Extensions
.ARB_texture_float
) ? GL_RG
: 0;
1230 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1234 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1236 case GL_ALPHA16F_ARB
:
1237 case GL_ALPHA32F_ARB
:
1238 return ctx
->API
== API_OPENGL_COMPAT
&&
1239 ctx
->Extensions
.ARB_texture_float
&&
1240 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1241 case GL_LUMINANCE16F_ARB
:
1242 case GL_LUMINANCE32F_ARB
:
1243 return ctx
->API
== API_OPENGL_COMPAT
&&
1244 ctx
->Extensions
.ARB_texture_float
&&
1245 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1246 case GL_LUMINANCE_ALPHA16F_ARB
:
1247 case GL_LUMINANCE_ALPHA32F_ARB
:
1248 return ctx
->API
== API_OPENGL_COMPAT
&&
1249 ctx
->Extensions
.ARB_texture_float
&&
1250 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1251 case GL_INTENSITY16F_ARB
:
1252 case GL_INTENSITY32F_ARB
:
1253 return ctx
->API
== API_OPENGL_COMPAT
&&
1254 ctx
->Extensions
.ARB_texture_float
&&
1255 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1257 return (_mesa_is_desktop_gl(ctx
)
1258 && ctx
->Extensions
.EXT_texture_shared_exponent
)
1260 case GL_R11F_G11F_B10F
:
1261 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
)
1264 case GL_RGBA8UI_EXT
:
1265 case GL_RGBA16UI_EXT
:
1266 case GL_RGBA32UI_EXT
:
1268 case GL_RGBA16I_EXT
:
1269 case GL_RGBA32I_EXT
:
1270 return ctx
->Version
>= 30
1271 || (_mesa_is_desktop_gl(ctx
) &&
1272 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1275 case GL_RGB16UI_EXT
:
1276 case GL_RGB32UI_EXT
:
1280 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1288 return ctx
->Version
>= 30
1289 || (_mesa_is_desktop_gl(ctx
) &&
1290 ctx
->Extensions
.ARB_texture_rg
&&
1291 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1299 return ctx
->Version
>= 30
1300 || (_mesa_is_desktop_gl(ctx
) &&
1301 ctx
->Extensions
.ARB_texture_rg
&&
1302 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1304 case GL_INTENSITY8I_EXT
:
1305 case GL_INTENSITY8UI_EXT
:
1306 case GL_INTENSITY16I_EXT
:
1307 case GL_INTENSITY16UI_EXT
:
1308 case GL_INTENSITY32I_EXT
:
1309 case GL_INTENSITY32UI_EXT
:
1310 return ctx
->API
== API_OPENGL_COMPAT
&&
1311 ctx
->Extensions
.EXT_texture_integer
&&
1312 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1314 case GL_LUMINANCE8I_EXT
:
1315 case GL_LUMINANCE8UI_EXT
:
1316 case GL_LUMINANCE16I_EXT
:
1317 case GL_LUMINANCE16UI_EXT
:
1318 case GL_LUMINANCE32I_EXT
:
1319 case GL_LUMINANCE32UI_EXT
:
1320 return ctx
->API
== API_OPENGL_COMPAT
&&
1321 ctx
->Extensions
.EXT_texture_integer
&&
1322 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1324 case GL_LUMINANCE_ALPHA8I_EXT
:
1325 case GL_LUMINANCE_ALPHA8UI_EXT
:
1326 case GL_LUMINANCE_ALPHA16I_EXT
:
1327 case GL_LUMINANCE_ALPHA16UI_EXT
:
1328 case GL_LUMINANCE_ALPHA32I_EXT
:
1329 case GL_LUMINANCE_ALPHA32UI_EXT
:
1330 return ctx
->API
== API_OPENGL_COMPAT
&&
1331 ctx
->Extensions
.EXT_texture_integer
&&
1332 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1334 case GL_ALPHA8I_EXT
:
1335 case GL_ALPHA8UI_EXT
:
1336 case GL_ALPHA16I_EXT
:
1337 case GL_ALPHA16UI_EXT
:
1338 case GL_ALPHA32I_EXT
:
1339 case GL_ALPHA32UI_EXT
:
1340 return ctx
->API
== API_OPENGL_COMPAT
&&
1341 ctx
->Extensions
.EXT_texture_integer
&&
1342 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1345 return (_mesa_is_desktop_gl(ctx
) &&
1346 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1347 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1350 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1359 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1362 invalidate_rb(GLuint key
, void *data
, void *userData
)
1364 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1365 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1367 /* If this is a user-created FBO */
1368 if (_mesa_is_user_fbo(fb
)) {
1370 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1371 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1372 if (att
->Type
== GL_RENDERBUFFER
&&
1373 att
->Renderbuffer
== rb
) {
1374 /* Mark fb status as indeterminate to force re-validation */
1383 /** sentinal value, see below */
1384 #define NO_SAMPLES 1000
1388 * Helper function used by _mesa_RenderbufferStorage() and
1389 * _mesa_RenderbufferStorageMultisample().
1390 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1393 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1394 GLsizei width
, GLsizei height
, GLsizei samples
)
1396 const char *func
= samples
== NO_SAMPLES
?
1397 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1398 struct gl_renderbuffer
*rb
;
1400 GET_CURRENT_CONTEXT(ctx
);
1402 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1404 if (MESA_VERBOSE
& VERBOSE_API
) {
1405 if (samples
== NO_SAMPLES
)
1406 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
1408 _mesa_lookup_enum_by_nr(target
),
1409 _mesa_lookup_enum_by_nr(internalFormat
),
1412 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
1414 _mesa_lookup_enum_by_nr(target
),
1415 _mesa_lookup_enum_by_nr(internalFormat
),
1416 width
, height
, samples
);
1419 if (target
!= GL_RENDERBUFFER_EXT
) {
1420 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1424 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1425 if (baseFormat
== 0) {
1426 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
1430 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1431 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1435 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1436 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1440 if (samples
== NO_SAMPLES
) {
1441 /* NumSamples == 0 indicates non-multisampling */
1444 else if (samples
> (GLsizei
) ctx
->Const
.MaxSamples
) {
1445 /* note: driver may choose to use more samples than what's requested */
1446 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
1450 rb
= ctx
->CurrentRenderbuffer
;
1452 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1456 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1458 if (rb
->InternalFormat
== internalFormat
&&
1459 rb
->Width
== (GLuint
) width
&&
1460 rb
->Height
== (GLuint
) height
&&
1461 rb
->NumSamples
== samples
) {
1462 /* no change in allocation needed */
1466 /* These MUST get set by the AllocStorage func */
1467 rb
->Format
= MESA_FORMAT_NONE
;
1468 rb
->NumSamples
= samples
;
1470 /* Now allocate the storage */
1471 ASSERT(rb
->AllocStorage
);
1472 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1473 /* No error - check/set fields now */
1474 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1475 assert(rb
->Width
== (GLuint
) width
);
1476 assert(rb
->Height
== (GLuint
) height
);
1477 rb
->InternalFormat
= internalFormat
;
1478 rb
->_BaseFormat
= baseFormat
;
1479 assert(rb
->_BaseFormat
!= 0);
1482 /* Probably ran out of memory - clear the fields */
1485 rb
->Format
= MESA_FORMAT_NONE
;
1486 rb
->InternalFormat
= GL_NONE
;
1487 rb
->_BaseFormat
= GL_NONE
;
1491 /* Invalidate the framebuffers the renderbuffer is attached in. */
1492 if (rb
->AttachedAnytime
) {
1493 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1499 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1501 struct gl_renderbuffer
*rb
;
1502 GET_CURRENT_CONTEXT(ctx
);
1503 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1505 if (!ctx
->Extensions
.OES_EGL_image
) {
1506 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1507 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1511 if (target
!= GL_RENDERBUFFER
) {
1512 _mesa_error(ctx
, GL_INVALID_ENUM
,
1513 "EGLImageTargetRenderbufferStorageOES");
1517 rb
= ctx
->CurrentRenderbuffer
;
1519 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1520 "EGLImageTargetRenderbufferStorageOES");
1524 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1526 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1531 * Helper function for _mesa_GetRenderbufferParameteriv() and
1532 * _mesa_GetFramebufferAttachmentParameteriv()
1533 * We have to be careful to respect the base format. For example, if a
1534 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1535 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1536 * we need to return zero.
1539 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1541 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1542 return _mesa_get_format_bits(format
, pname
);
1550 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
1551 GLsizei width
, GLsizei height
)
1553 /* GL_ARB_fbo says calling this function is equivalent to calling
1554 * glRenderbufferStorageMultisample() with samples=0. We pass in
1555 * a token value here just for error reporting purposes.
1557 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1562 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1563 GLenum internalFormat
,
1564 GLsizei width
, GLsizei height
)
1566 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1571 * OpenGL ES version of glRenderBufferStorage.
1574 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1575 GLsizei width
, GLsizei height
)
1577 switch (internalFormat
) {
1579 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1580 /* choose a closest format */
1581 internalFormat
= GL_RGB5
;
1587 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1592 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1594 struct gl_renderbuffer
*rb
;
1595 GET_CURRENT_CONTEXT(ctx
);
1597 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1599 if (target
!= GL_RENDERBUFFER_EXT
) {
1600 _mesa_error(ctx
, GL_INVALID_ENUM
,
1601 "glGetRenderbufferParameterivEXT(target)");
1605 rb
= ctx
->CurrentRenderbuffer
;
1607 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1608 "glGetRenderbufferParameterivEXT");
1612 /* No need to flush here since we're just quering state which is
1613 * not effected by rendering.
1617 case GL_RENDERBUFFER_WIDTH_EXT
:
1618 *params
= rb
->Width
;
1620 case GL_RENDERBUFFER_HEIGHT_EXT
:
1621 *params
= rb
->Height
;
1623 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1624 *params
= rb
->InternalFormat
;
1626 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1627 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1628 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1629 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1630 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1631 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1632 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1634 case GL_RENDERBUFFER_SAMPLES
:
1635 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
1636 || _mesa_is_gles3(ctx
)) {
1637 *params
= rb
->NumSamples
;
1642 _mesa_error(ctx
, GL_INVALID_ENUM
,
1643 "glGetRenderbufferParameterivEXT(target)");
1649 GLboolean GLAPIENTRY
1650 _mesa_IsFramebuffer(GLuint framebuffer
)
1652 GET_CURRENT_CONTEXT(ctx
);
1653 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1655 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1656 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1664 * Check if any of the attachments of the given framebuffer are textures
1665 * (render to texture). Call ctx->Driver.RenderTexture() for such
1669 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1672 ASSERT(ctx
->Driver
.RenderTexture
);
1674 if (_mesa_is_winsys_fbo(fb
))
1675 return; /* can't render to texture with winsys framebuffers */
1677 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1678 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1679 if (att
->Texture
&& _mesa_get_attachment_teximage(att
)) {
1680 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1687 * Examine all the framebuffer's attachments to see if any are textures.
1688 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1689 * notify the device driver that the texture image may have changed.
1692 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1694 if (_mesa_is_winsys_fbo(fb
))
1695 return; /* can't render to texture with winsys framebuffers */
1697 if (ctx
->Driver
.FinishRenderTexture
) {
1699 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1700 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1701 if (att
->Texture
&& att
->Renderbuffer
) {
1702 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1710 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
1712 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1713 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1714 GLboolean bindReadBuf
, bindDrawBuf
;
1715 GET_CURRENT_CONTEXT(ctx
);
1718 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1719 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1720 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1724 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1726 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1727 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1728 "glBindFramebufferEXT(unsupported)");
1733 case GL_DRAW_FRAMEBUFFER_EXT
:
1734 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1735 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1738 bindDrawBuf
= GL_TRUE
;
1739 bindReadBuf
= GL_FALSE
;
1741 case GL_READ_FRAMEBUFFER_EXT
:
1742 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1743 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1746 bindDrawBuf
= GL_FALSE
;
1747 bindReadBuf
= GL_TRUE
;
1749 case GL_FRAMEBUFFER_EXT
:
1750 bindDrawBuf
= GL_TRUE
;
1751 bindReadBuf
= GL_TRUE
;
1754 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1759 /* Binding a user-created framebuffer object */
1760 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1761 if (newDrawFb
== &DummyFramebuffer
) {
1762 /* ID was reserved, but no real framebuffer object made yet */
1766 && _mesa_is_desktop_gl(ctx
)
1767 && ctx
->Extensions
.ARB_framebuffer_object
) {
1768 /* All FBO IDs must be Gen'd */
1769 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1774 /* create new framebuffer object */
1775 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1777 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1780 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1782 newReadFb
= newDrawFb
;
1785 /* Binding the window system framebuffer (which was originally set
1786 * with MakeCurrent).
1788 newDrawFb
= ctx
->WinSysDrawBuffer
;
1789 newReadFb
= ctx
->WinSysReadBuffer
;
1793 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1795 /* save pointers to current/old framebuffers */
1796 oldDrawFb
= ctx
->DrawBuffer
;
1797 oldReadFb
= ctx
->ReadBuffer
;
1799 /* check if really changing bindings */
1800 if (oldDrawFb
== newDrawFb
)
1801 bindDrawBuf
= GL_FALSE
;
1802 if (oldReadFb
== newReadFb
)
1803 bindReadBuf
= GL_FALSE
;
1806 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1808 * We also check if we're beginning and/or ending render-to-texture.
1809 * When a framebuffer with texture attachments is unbound, call
1810 * ctx->Driver.FinishRenderTexture().
1811 * When a framebuffer with texture attachments is bound, call
1812 * ctx->Driver.RenderTexture().
1814 * Note that if the ReadBuffer has texture attachments we don't consider
1815 * that a render-to-texture case.
1818 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1820 /* check if old readbuffer was render-to-texture */
1821 check_end_texture_render(ctx
, oldReadFb
);
1823 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1827 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1829 /* check if old framebuffer had any texture attachments */
1831 check_end_texture_render(ctx
, oldDrawFb
);
1833 /* check if newly bound framebuffer has any texture attachments */
1834 check_begin_texture_render(ctx
, newDrawFb
);
1836 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1839 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1840 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1846 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
1849 GET_CURRENT_CONTEXT(ctx
);
1851 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1852 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1854 for (i
= 0; i
< n
; i
++) {
1855 if (framebuffers
[i
] > 0) {
1856 struct gl_framebuffer
*fb
;
1857 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1859 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1861 /* check if deleting currently bound framebuffer object */
1862 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1863 /* separate draw/read binding points */
1864 if (fb
== ctx
->DrawBuffer
) {
1866 ASSERT(fb
->RefCount
>= 2);
1867 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1869 if (fb
== ctx
->ReadBuffer
) {
1871 ASSERT(fb
->RefCount
>= 2);
1872 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, 0);
1876 /* only one binding point for read/draw buffers */
1877 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1879 ASSERT(fb
->RefCount
>= 2);
1880 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, 0);
1884 /* remove from hash table immediately, to free the ID */
1885 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1887 if (fb
!= &DummyFramebuffer
) {
1888 /* But the object will not be freed until it's no longer
1889 * bound in any context.
1891 _mesa_reference_framebuffer(&fb
, NULL
);
1900 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
1902 GET_CURRENT_CONTEXT(ctx
);
1906 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1909 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1916 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1918 for (i
= 0; i
< n
; i
++) {
1919 GLuint name
= first
+ i
;
1920 framebuffers
[i
] = name
;
1921 /* insert dummy placeholder into hash table */
1922 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1923 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1924 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1931 _mesa_CheckFramebufferStatus(GLenum target
)
1933 struct gl_framebuffer
*buffer
;
1934 GET_CURRENT_CONTEXT(ctx
);
1936 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1938 if (MESA_VERBOSE
& VERBOSE_API
)
1939 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
1940 _mesa_lookup_enum_by_nr(target
));
1942 buffer
= get_framebuffer_target(ctx
, target
);
1944 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1948 if (_mesa_is_winsys_fbo(buffer
)) {
1949 /* The window system / default framebuffer is always complete */
1950 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1953 /* No need to flush here */
1955 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1956 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1959 return buffer
->_Status
;
1964 * Replicate the src attachment point. Used by framebuffer_texture() when
1965 * the same texture is attached at GL_DEPTH_ATTACHMENT and
1966 * GL_STENCIL_ATTACHMENT.
1969 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
1970 gl_buffer_index dst
,
1971 gl_buffer_index src
)
1973 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
1974 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
1976 assert(src_att
->Texture
!= NULL
);
1977 assert(src_att
->Renderbuffer
!= NULL
);
1979 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
1980 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
1981 dst_att
->Type
= src_att
->Type
;
1982 dst_att
->Complete
= src_att
->Complete
;
1983 dst_att
->TextureLevel
= src_att
->TextureLevel
;
1984 dst_att
->Zoffset
= src_att
->Zoffset
;
1989 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
1990 * glFramebufferTextureLayerEXT().
1991 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
1992 * get textarget=0 in that case.
1995 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
1996 GLenum attachment
, GLenum textarget
, GLuint texture
,
1997 GLint level
, GLint zoffset
)
1999 struct gl_renderbuffer_attachment
*att
;
2000 struct gl_texture_object
*texObj
= NULL
;
2001 struct gl_framebuffer
*fb
;
2002 GLenum maxLevelsTarget
;
2004 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2006 fb
= get_framebuffer_target(ctx
, target
);
2008 _mesa_error(ctx
, GL_INVALID_ENUM
,
2009 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
2013 /* check framebuffer binding */
2014 if (_mesa_is_winsys_fbo(fb
)) {
2015 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2016 "glFramebufferTexture%sEXT", caller
);
2020 /* The textarget, level, and zoffset parameters are only validated if
2021 * texture is non-zero.
2024 GLboolean err
= GL_TRUE
;
2026 texObj
= _mesa_lookup_texture(ctx
, texture
);
2027 if (texObj
!= NULL
) {
2028 if (textarget
== 0) {
2029 /* If textarget == 0 it means we're being called by
2030 * glFramebufferTextureLayer() and textarget is not used.
2031 * The only legal texture types for that function are 3D and
2032 * 1D/2D arrays textures.
2034 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2035 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2036 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2037 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
);
2040 /* Make sure textarget is consistent with the texture's type */
2041 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2042 ? !_mesa_is_cube_face(textarget
)
2043 : (texObj
->Target
!= textarget
);
2047 /* can't render to a non-existant texture */
2048 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2049 "glFramebufferTexture%sEXT(non existant texture)",
2055 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2056 "glFramebufferTexture%sEXT(texture target mismatch)",
2061 if (texObj
->Target
== GL_TEXTURE_3D
) {
2062 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2063 if (zoffset
< 0 || zoffset
>= maxSize
) {
2064 _mesa_error(ctx
, GL_INVALID_VALUE
,
2065 "glFramebufferTexture%sEXT(zoffset)", caller
);
2069 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2070 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2071 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)) {
2073 zoffset
>= (GLint
) ctx
->Const
.MaxArrayTextureLayers
) {
2074 _mesa_error(ctx
, GL_INVALID_VALUE
,
2075 "glFramebufferTexture%sEXT(layer)", caller
);
2080 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2082 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2083 _mesa_error(ctx
, GL_INVALID_VALUE
,
2084 "glFramebufferTexture%sEXT(level)", caller
);
2089 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2091 _mesa_error(ctx
, GL_INVALID_ENUM
,
2092 "glFramebufferTexture%sEXT(attachment)", caller
);
2096 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2098 _glthread_LOCK_MUTEX(fb
->Mutex
);
2100 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2101 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2102 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2103 _mesa_tex_target_to_face(textarget
) ==
2104 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2105 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2106 /* The texture object is already attached to the stencil attachment
2107 * point. Don't create a new renderbuffer; just reuse the stencil
2108 * attachment's. This is required to prevent a GL error in
2109 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2111 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2113 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2114 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2115 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2116 _mesa_tex_target_to_face(textarget
) ==
2117 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2118 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2119 /* As above, but with depth and stencil transposed. */
2120 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2123 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2125 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2126 /* Above we created a new renderbuffer and attached it to the
2127 * depth attachment point. Now attach it to the stencil attachment
2130 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2131 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2136 /* Set the render-to-texture flag. We'll check this flag in
2137 * glTexImage() and friends to determine if we need to revalidate
2138 * any FBOs that might be rendering into this texture.
2139 * This flag never gets cleared since it's non-trivial to determine
2140 * when all FBOs might be done rendering to this texture. That's OK
2141 * though since it's uncommon to render to a texture then repeatedly
2142 * call glTexImage() to change images in the texture.
2144 texObj
->_RenderToTexture
= GL_TRUE
;
2147 _mesa_remove_attachment(ctx
, att
);
2148 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2149 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2150 _mesa_remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2154 invalidate_framebuffer(fb
);
2156 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
2162 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2163 GLenum textarget
, GLuint texture
, GLint level
)
2165 GET_CURRENT_CONTEXT(ctx
);
2170 switch (textarget
) {
2174 case GL_TEXTURE_1D_ARRAY
:
2175 error
= !ctx
->Extensions
.EXT_texture_array
;
2182 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2183 "glFramebufferTexture1DEXT(textarget=%s)",
2184 _mesa_lookup_enum_by_nr(textarget
));
2189 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2195 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2196 GLenum textarget
, GLuint texture
, GLint level
)
2198 GET_CURRENT_CONTEXT(ctx
);
2203 switch (textarget
) {
2207 case GL_TEXTURE_RECTANGLE
:
2208 error
= _mesa_is_gles(ctx
)
2209 || !ctx
->Extensions
.NV_texture_rectangle
;
2211 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2212 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2213 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2214 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2215 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2216 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2217 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2219 case GL_TEXTURE_2D_ARRAY
:
2220 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2221 || !ctx
->Extensions
.EXT_texture_array
;
2228 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2229 "glFramebufferTexture2DEXT(textarget=%s)",
2230 _mesa_lookup_enum_by_nr(textarget
));
2235 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2241 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2242 GLenum textarget
, GLuint texture
,
2243 GLint level
, GLint zoffset
)
2245 GET_CURRENT_CONTEXT(ctx
);
2247 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2248 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2249 "glFramebufferTexture3DEXT(textarget)");
2253 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2259 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2260 GLuint texture
, GLint level
, GLint layer
)
2262 GET_CURRENT_CONTEXT(ctx
);
2264 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2270 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2271 GLenum renderbufferTarget
,
2272 GLuint renderbuffer
)
2274 struct gl_renderbuffer_attachment
*att
;
2275 struct gl_framebuffer
*fb
;
2276 struct gl_renderbuffer
*rb
;
2277 GET_CURRENT_CONTEXT(ctx
);
2279 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2281 fb
= get_framebuffer_target(ctx
, target
);
2283 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFramebufferRenderbufferEXT(target)");
2287 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2288 _mesa_error(ctx
, GL_INVALID_ENUM
,
2289 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2293 if (_mesa_is_winsys_fbo(fb
)) {
2294 /* Can't attach new renderbuffers to a window system framebuffer */
2295 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
2299 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2301 _mesa_error(ctx
, GL_INVALID_ENUM
,
2302 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2303 _mesa_lookup_enum_by_nr(attachment
));
2308 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2310 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2311 "glFramebufferRenderbufferEXT(non-existant"
2312 " renderbuffer %u)", renderbuffer
);
2315 else if (rb
== &DummyRenderbuffer
) {
2316 /* This is what NVIDIA does */
2317 _mesa_error(ctx
, GL_INVALID_VALUE
,
2318 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2324 /* remove renderbuffer attachment */
2328 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2329 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2330 /* make sure the renderbuffer is a depth/stencil format */
2331 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2332 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2333 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2334 "glFramebufferRenderbufferEXT(renderbuffer"
2335 " is not DEPTH_STENCIL format)");
2341 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2343 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2344 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2346 /* Some subsequent GL commands may depend on the framebuffer's visual
2347 * after the binding is updated. Update visual info now.
2349 _mesa_update_framebuffer_visual(ctx
, fb
);
2354 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2355 GLenum pname
, GLint
*params
)
2357 const struct gl_renderbuffer_attachment
*att
;
2358 struct gl_framebuffer
*buffer
;
2360 GET_CURRENT_CONTEXT(ctx
);
2362 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2364 /* The error differs in GL and GLES. */
2365 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2367 buffer
= get_framebuffer_target(ctx
, target
);
2369 _mesa_error(ctx
, GL_INVALID_ENUM
,
2370 "glGetFramebufferAttachmentParameterivEXT(target)");
2374 if (_mesa_is_winsys_fbo(buffer
)) {
2375 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2378 * "If the framebuffer currently bound to target is zero, then
2379 * INVALID_OPERATION is generated."
2381 * The EXT_framebuffer_object spec has the same wording, and the
2382 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2385 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2386 && !_mesa_is_gles3(ctx
)) {
2387 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2388 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2392 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
2393 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
2394 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2395 "glGetFramebufferAttachmentParameteriv(attachment)");
2398 /* the default / window-system FBO */
2399 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2402 /* user-created framebuffer FBO */
2403 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
2407 _mesa_error(ctx
, GL_INVALID_ENUM
,
2408 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2412 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2413 /* the depth and stencil attachments must point to the same buffer */
2414 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2415 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2416 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2417 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2418 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2419 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2420 " attachments differ)");
2425 /* No need to flush here */
2428 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2429 *params
= _mesa_is_winsys_fbo(buffer
)
2430 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2432 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2433 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2434 *params
= att
->Renderbuffer
->Name
;
2436 else if (att
->Type
== GL_TEXTURE
) {
2437 *params
= att
->Texture
->Name
;
2440 assert(att
->Type
== GL_NONE
);
2441 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2444 goto invalid_pname_enum
;
2448 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2449 if (att
->Type
== GL_TEXTURE
) {
2450 *params
= att
->TextureLevel
;
2452 else if (att
->Type
== GL_NONE
) {
2453 _mesa_error(ctx
, err
,
2454 "glGetFramebufferAttachmentParameterivEXT(pname)");
2457 goto invalid_pname_enum
;
2460 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2461 if (att
->Type
== GL_TEXTURE
) {
2462 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2463 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2469 else if (att
->Type
== GL_NONE
) {
2470 _mesa_error(ctx
, err
,
2471 "glGetFramebufferAttachmentParameterivEXT(pname)");
2474 goto invalid_pname_enum
;
2477 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2478 if (ctx
->API
== API_OPENGLES
) {
2479 goto invalid_pname_enum
;
2480 } else if (att
->Type
== GL_NONE
) {
2481 _mesa_error(ctx
, err
,
2482 "glGetFramebufferAttachmentParameterivEXT(pname)");
2483 } else if (att
->Type
== GL_TEXTURE
) {
2484 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2485 *params
= att
->Zoffset
;
2492 goto invalid_pname_enum
;
2495 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2496 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2497 && !_mesa_is_gles3(ctx
)) {
2498 goto invalid_pname_enum
;
2500 else if (att
->Type
== GL_NONE
) {
2501 _mesa_error(ctx
, err
,
2502 "glGetFramebufferAttachmentParameterivEXT(pname)");
2505 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2506 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2509 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2510 * if the sRGB conversion is unsupported. */
2511 *params
= GL_LINEAR
;
2515 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2516 if ((ctx
->API
!= API_OPENGL_COMPAT
|| !ctx
->Extensions
.ARB_framebuffer_object
)
2517 && ctx
->API
!= API_OPENGL_CORE
2518 && !_mesa_is_gles3(ctx
)) {
2519 goto invalid_pname_enum
;
2521 else if (att
->Type
== GL_NONE
) {
2522 _mesa_error(ctx
, err
,
2523 "glGetFramebufferAttachmentParameterivEXT(pname)");
2526 gl_format format
= att
->Renderbuffer
->Format
;
2527 if (format
== MESA_FORMAT_S8
) {
2531 else if (format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
2532 /* depends on the attachment parameter */
2533 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2541 *params
= _mesa_get_format_datatype(format
);
2545 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2546 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2547 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2548 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2549 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2550 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2551 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2552 && !_mesa_is_gles3(ctx
)) {
2553 goto invalid_pname_enum
;
2555 else if (att
->Type
== GL_NONE
) {
2556 _mesa_error(ctx
, err
,
2557 "glGetFramebufferAttachmentParameterivEXT(pname)");
2559 else if (att
->Texture
) {
2560 const struct gl_texture_image
*texImage
=
2561 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2564 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2565 texImage
->TexFormat
);
2571 else if (att
->Renderbuffer
) {
2572 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2573 att
->Renderbuffer
->Format
);
2576 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
2577 " invalid FBO attachment structure");
2581 goto invalid_pname_enum
;
2587 _mesa_error(ctx
, GL_INVALID_ENUM
,
2588 "glGetFramebufferAttachmentParameteriv(pname)");
2594 _mesa_GenerateMipmap(GLenum target
)
2596 struct gl_texture_image
*srcImage
;
2597 struct gl_texture_object
*texObj
;
2600 GET_CURRENT_CONTEXT(ctx
);
2602 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2603 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2607 error
= _mesa_is_gles(ctx
);
2613 error
= ctx
->API
== API_OPENGLES
;
2615 case GL_TEXTURE_CUBE_MAP
:
2616 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2618 case GL_TEXTURE_1D_ARRAY
:
2619 error
= _mesa_is_gles(ctx
) || !ctx
->Extensions
.EXT_texture_array
;
2621 case GL_TEXTURE_2D_ARRAY
:
2622 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2623 || !ctx
->Extensions
.EXT_texture_array
;
2630 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target=%s)",
2631 _mesa_lookup_enum_by_nr(target
));
2635 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2637 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2642 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2643 !_mesa_cube_complete(texObj
)) {
2644 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2645 "glGenerateMipmap(incomplete cube map)");
2649 _mesa_lock_texture(ctx
, texObj
);
2651 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
2653 _mesa_unlock_texture(ctx
, texObj
);
2654 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2655 "glGenerateMipmap(zero size base image)");
2659 if (_mesa_is_enum_format_integer(srcImage
->InternalFormat
) ||
2660 _mesa_is_depthstencil_format(srcImage
->InternalFormat
) ||
2661 _mesa_is_stencil_format(srcImage
->InternalFormat
)) {
2662 _mesa_unlock_texture(ctx
, texObj
);
2663 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2664 "glGenerateMipmap(invalid internal format)");
2668 if (target
== GL_TEXTURE_CUBE_MAP
) {
2670 for (face
= 0; face
< 6; face
++)
2671 ctx
->Driver
.GenerateMipmap(ctx
,
2672 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2676 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2678 _mesa_unlock_texture(ctx
, texObj
);
2682 static const struct gl_renderbuffer_attachment
*
2683 find_attachment(const struct gl_framebuffer
*fb
,
2684 const struct gl_renderbuffer
*rb
)
2687 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2688 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2689 return &fb
->Attachment
[i
];
2696 * Helper function for checking if the datatypes of color buffers are
2697 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2699 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2700 * and any of the following conditions hold:
2701 * - The read buffer contains fixed-point or floating-point values and any
2702 * draw buffer contains neither fixed-point nor floating-point values.
2703 * - The read buffer contains unsigned integer values and any draw buffer
2704 * does not contain unsigned integer values.
2705 * - The read buffer contains signed integer values and any draw buffer
2706 * does not contain signed integer values."
2709 compatible_color_datatypes(gl_format srcFormat
, gl_format dstFormat
)
2711 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
2712 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
2714 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
2715 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
2716 srcType
== GL_SIGNED_NORMALIZED
||
2717 srcType
== GL_FLOAT
);
2718 /* Boil any of those types down to GL_FLOAT */
2722 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
2723 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
2724 dstType
== GL_SIGNED_NORMALIZED
||
2725 dstType
== GL_FLOAT
);
2726 /* Boil any of those types down to GL_FLOAT */
2730 return srcType
== dstType
;
2735 compatible_resolve_formats(const struct gl_renderbuffer
*readRb
,
2736 const struct gl_renderbuffer
*drawRb
)
2738 GLenum readFormat
, drawFormat
;
2740 /* The simple case where we know the backing Mesa formats are the same.
2742 if (_mesa_get_srgb_format_linear(readRb
->Format
) ==
2743 _mesa_get_srgb_format_linear(drawRb
->Format
)) {
2747 /* The Mesa formats are different, so we must check whether the internal
2748 * formats are compatible.
2750 * Under some circumstances, the user may request e.g. two GL_RGBA8
2751 * textures and get two entirely different Mesa formats like RGBA8888 and
2752 * ARGB8888. Drivers behaving like that should be able to cope with
2753 * non-matching formats by themselves, because it's not the user's fault.
2755 * Blits between linear and sRGB formats are also allowed.
2757 readFormat
= _mesa_get_nongeneric_internalformat(readRb
->InternalFormat
);
2758 drawFormat
= _mesa_get_nongeneric_internalformat(drawRb
->InternalFormat
);
2759 readFormat
= _mesa_get_linear_internalformat(readFormat
);
2760 drawFormat
= _mesa_get_linear_internalformat(drawFormat
);
2762 if (readFormat
== drawFormat
) {
2771 * Blit rectangular region, optionally from one framebuffer to another.
2773 * Note, if the src buffer is multisampled and the dest is not, this is
2774 * when the samples must be resolved to a single color.
2777 _mesa_BlitFramebuffer(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2778 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2779 GLbitfield mask
, GLenum filter
)
2781 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2782 GL_DEPTH_BUFFER_BIT
|
2783 GL_STENCIL_BUFFER_BIT
);
2784 const struct gl_framebuffer
*readFb
, *drawFb
;
2785 GET_CURRENT_CONTEXT(ctx
);
2787 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2788 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2790 if (MESA_VERBOSE
& VERBOSE_API
)
2792 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2793 srcX0
, srcY0
, srcX1
, srcY1
,
2794 dstX0
, dstY0
, dstX1
, dstY1
,
2795 mask
, _mesa_lookup_enum_by_nr(filter
));
2797 if (ctx
->NewState
) {
2798 _mesa_update_state(ctx
);
2801 readFb
= ctx
->ReadBuffer
;
2802 drawFb
= ctx
->DrawBuffer
;
2804 if (!readFb
|| !drawFb
) {
2805 /* This will normally never happen but someday we may want to
2806 * support MakeCurrent() with no drawables.
2811 /* check for complete framebuffers */
2812 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2813 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2814 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2815 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2819 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2820 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2824 if (mask
& ~legalMaskBits
) {
2825 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2829 /* depth/stencil must be blitted with nearest filtering */
2830 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2831 && filter
!= GL_NEAREST
) {
2832 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2833 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2837 /* get color read/draw renderbuffers */
2838 if (mask
& GL_COLOR_BUFFER_BIT
) {
2839 const GLuint numColorDrawBuffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
2840 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
2841 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
2844 /* From the EXT_framebuffer_object spec:
2846 * "If a buffer is specified in <mask> and does not exist in both
2847 * the read and draw framebuffers, the corresponding bit is silently
2850 if (!colorReadRb
|| numColorDrawBuffers
== 0) {
2851 mask
&= ~GL_COLOR_BUFFER_BIT
;
2854 for (i
= 0; i
< numColorDrawBuffers
; i
++) {
2855 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
2859 if (!compatible_color_datatypes(colorReadRb
->Format
,
2860 colorDrawRb
->Format
)) {
2861 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2862 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2865 /* extra checks for multisample copies... */
2866 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2867 /* color formats must match */
2868 if (!compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
2869 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2870 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2875 if (filter
== GL_LINEAR
) {
2876 /* 3.1 spec, page 199:
2877 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2878 * if filter is LINEAR and read buffer contains integer data."
2880 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
2881 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
2882 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2883 "glBlitFramebufferEXT(integer color type)");
2890 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2891 struct gl_renderbuffer
*readRb
=
2892 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2893 struct gl_renderbuffer
*drawRb
=
2894 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2896 /* From the EXT_framebuffer_object spec:
2898 * "If a buffer is specified in <mask> and does not exist in both
2899 * the read and draw framebuffers, the corresponding bit is silently
2902 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2903 mask
&= ~GL_STENCIL_BUFFER_BIT
;
2906 int read_z_bits
, draw_z_bits
;
2908 if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2909 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2910 /* There is no need to check the stencil datatype here, because
2911 * there is only one: GL_UNSIGNED_INT.
2913 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2914 "glBlitFramebuffer(stencil attachment format mismatch)");
2918 read_z_bits
= _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
);
2919 draw_z_bits
= _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
);
2921 /* If both buffers also have depth data, the depth formats must match
2922 * as well. If one doesn't have depth, it's not blitted, so we should
2923 * ignore the depth format check.
2925 if (read_z_bits
> 0 && draw_z_bits
> 0 &&
2926 (read_z_bits
!= draw_z_bits
||
2927 _mesa_get_format_datatype(readRb
->Format
) !=
2928 _mesa_get_format_datatype(drawRb
->Format
))) {
2930 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
2931 "(stencil attachment depth format mismatch)");
2937 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2938 struct gl_renderbuffer
*readRb
=
2939 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2940 struct gl_renderbuffer
*drawRb
=
2941 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2943 /* From the EXT_framebuffer_object spec:
2945 * "If a buffer is specified in <mask> and does not exist in both
2946 * the read and draw framebuffers, the corresponding bit is silently
2949 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2950 mask
&= ~GL_DEPTH_BUFFER_BIT
;
2953 int read_s_bit
, draw_s_bit
;
2955 if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2956 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
2957 (_mesa_get_format_datatype(readRb
->Format
) !=
2958 _mesa_get_format_datatype(drawRb
->Format
))) {
2959 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2960 "glBlitFramebuffer(depth attachment format mismatch)");
2964 read_s_bit
= _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
);
2965 draw_s_bit
= _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
);
2967 /* If both buffers also have stencil data, the stencil formats must
2968 * match as well. If one doesn't have stencil, it's not blitted, so
2969 * we should ignore the stencil format check.
2971 if (read_s_bit
> 0 && draw_s_bit
> 0 && read_s_bit
!= draw_s_bit
) {
2972 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
2973 "(depth attachment stencil bits mismatch)");
2979 if (readFb
->Visual
.samples
> 0 &&
2980 drawFb
->Visual
.samples
> 0 &&
2981 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2982 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2983 "glBlitFramebufferEXT(mismatched samples)");
2987 /* extra checks for multisample copies... */
2988 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2989 /* src and dest region sizes must be the same */
2990 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
2991 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
2992 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2993 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2998 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2999 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
3005 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
3006 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
3009 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3011 srcX0
, srcY0
, srcX1
, srcY1
,
3012 dstX0
, dstY0
, dstX1
, dstY1
,
3015 const struct gl_renderbuffer_attachment
*att
;
3017 att
= find_attachment(readFb
, colorReadRb
);
3018 printf(" Src FBO %u RB %u (%dx%d) ",
3019 readFb
->Name
, colorReadRb
->Name
,
3020 colorReadRb
->Width
, colorReadRb
->Height
);
3021 if (att
&& att
->Texture
) {
3022 printf("Tex %u tgt 0x%x level %u face %u",
3024 att
->Texture
->Target
,
3030 /* Print all active color render buffers */
3031 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
3032 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
3036 att
= find_attachment(drawFb
, colorDrawRb
);
3037 printf(" Dst FBO %u RB %u (%dx%d) ",
3038 drawFb
->Name
, colorDrawRb
->Name
,
3039 colorDrawRb
->Width
, colorDrawRb
->Height
);
3040 if (att
&& att
->Texture
) {
3041 printf("Tex %u tgt 0x%x level %u face %u",
3043 att
->Texture
->Target
,
3056 ASSERT(ctx
->Driver
.BlitFramebuffer
);
3057 ctx
->Driver
.BlitFramebuffer(ctx
,
3058 srcX0
, srcY0
, srcX1
, srcY1
,
3059 dstX0
, dstY0
, dstX1
, dstY1
,
3065 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3066 const GLenum
*attachments
, GLint x
, GLint y
,
3067 GLsizei width
, GLsizei height
, const char *name
)
3070 struct gl_framebuffer
*fb
;
3071 GET_CURRENT_CONTEXT(ctx
);
3073 ASSERT_OUTSIDE_BEGIN_END(ctx
);
3075 fb
= get_framebuffer_target(ctx
, target
);
3077 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3081 if (numAttachments
< 0) {
3082 _mesa_error(ctx
, GL_INVALID_VALUE
,
3083 "%s(numAttachments < 0)", name
);
3087 /* The GL_ARB_invalidate_subdata spec says:
3089 * "If an attachment is specified that does not exist in the
3090 * framebuffer bound to <target>, it is ignored."
3094 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3095 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3096 * INVALID_OPERATION is generated."
3098 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3099 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3100 * set of retrictions).
3102 for (i
= 0; i
< numAttachments
; i
++) {
3103 if (_mesa_is_winsys_fbo(fb
)) {
3104 switch (attachments
[i
]) {
3110 /* Accumulation buffers and auxilary buffers were removed in
3111 * OpenGL 3.1, and they never existed in OpenGL ES.
3113 if (ctx
->API
!= API_OPENGL_COMPAT
)
3123 case GL_FRONT_RIGHT
:
3124 if (!_mesa_is_desktop_gl(ctx
))
3131 switch (attachments
[i
]) {
3132 case GL_DEPTH_ATTACHMENT
:
3133 case GL_STENCIL_ATTACHMENT
:
3135 case GL_COLOR_ATTACHMENT0
:
3136 case GL_COLOR_ATTACHMENT1
:
3137 case GL_COLOR_ATTACHMENT2
:
3138 case GL_COLOR_ATTACHMENT3
:
3139 case GL_COLOR_ATTACHMENT4
:
3140 case GL_COLOR_ATTACHMENT5
:
3141 case GL_COLOR_ATTACHMENT6
:
3142 case GL_COLOR_ATTACHMENT7
:
3143 case GL_COLOR_ATTACHMENT8
:
3144 case GL_COLOR_ATTACHMENT9
:
3145 case GL_COLOR_ATTACHMENT10
:
3146 case GL_COLOR_ATTACHMENT11
:
3147 case GL_COLOR_ATTACHMENT12
:
3148 case GL_COLOR_ATTACHMENT13
:
3149 case GL_COLOR_ATTACHMENT14
:
3150 case GL_COLOR_ATTACHMENT15
: {
3151 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3152 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3153 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3154 "%s(attachment >= max. color attachments)", name
);
3164 /* We don't actually do anything for this yet. Just return after
3165 * validating the parameters and generating the required errors.
3170 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3175 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3176 const GLenum
*attachments
, GLint x
, GLint y
,
3177 GLsizei width
, GLsizei height
)
3179 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3180 x
, y
, width
, height
,
3181 "glInvalidateSubFramebuffer");
3185 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3186 const GLenum
*attachments
)
3188 /* The GL_ARB_invalidate_subdata spec says:
3192 * void InvalidateFramebuffer(enum target,
3193 * sizei numAttachments,
3194 * const enum *attachments);
3196 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3197 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3198 * <MAX_VIEWPORT_DIMS[1]> respectively."
3200 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3201 0, 0, MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3202 "glInvalidateFramebuffer");