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) ||
1223 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1227 return ((_mesa_is_desktop_gl(ctx) &&
1228 ctx->Extensions.ARB_texture_rg &&
1229 ctx->Extensions.ARB_texture_float) ||
1230 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1234 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1238 return ((_mesa_is_desktop_gl(ctx) &&
1239 ctx->Extensions.ARB_texture_float) ||
1240 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1242 case GL_ALPHA16F_ARB:
1243 case GL_ALPHA32F_ARB:
1244 return ctx->API == API_OPENGL_COMPAT &&
1245 ctx->Extensions.ARB_texture_float &&
1246 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1247 case GL_LUMINANCE16F_ARB:
1248 case GL_LUMINANCE32F_ARB:
1249 return ctx->API == API_OPENGL_COMPAT &&
1250 ctx->Extensions.ARB_texture_float &&
1251 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1252 case GL_LUMINANCE_ALPHA16F_ARB:
1253 case GL_LUMINANCE_ALPHA32F_ARB:
1254 return ctx->API == API_OPENGL_COMPAT &&
1255 ctx->Extensions.ARB_texture_float &&
1256 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1257 case GL_INTENSITY16F_ARB:
1258 case GL_INTENSITY32F_ARB:
1259 return ctx->API == API_OPENGL_COMPAT &&
1260 ctx->Extensions.ARB_texture_float &&
1261 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1263 return (_mesa_is_desktop_gl(ctx)
1264 && ctx->Extensions.EXT_texture_shared_exponent)
1266 case GL_R11F_G11F_B10F:
1267 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1268 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1271 case GL_RGBA8UI_EXT:
1272 case GL_RGBA16UI_EXT:
1273 case GL_RGBA32UI_EXT:
1275 case GL_RGBA16I_EXT:
1276 case GL_RGBA32I_EXT:
1277 return ctx->Version >= 30
1278 || (_mesa_is_desktop_gl(ctx) &&
1279 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1282 case GL_RGB16UI_EXT:
1283 case GL_RGB32UI_EXT:
1287 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1295 return ctx->Version >= 30
1296 || (_mesa_is_desktop_gl(ctx) &&
1297 ctx->Extensions.ARB_texture_rg &&
1298 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1306 return ctx->Version >= 30
1307 || (_mesa_is_desktop_gl(ctx) &&
1308 ctx->Extensions.ARB_texture_rg &&
1309 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1311 case GL_INTENSITY8I_EXT:
1312 case GL_INTENSITY8UI_EXT:
1313 case GL_INTENSITY16I_EXT:
1314 case GL_INTENSITY16UI_EXT:
1315 case GL_INTENSITY32I_EXT:
1316 case GL_INTENSITY32UI_EXT:
1317 return ctx->API == API_OPENGL_COMPAT &&
1318 ctx->Extensions.EXT_texture_integer &&
1319 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1321 case GL_LUMINANCE8I_EXT:
1322 case GL_LUMINANCE8UI_EXT:
1323 case GL_LUMINANCE16I_EXT:
1324 case GL_LUMINANCE16UI_EXT:
1325 case GL_LUMINANCE32I_EXT:
1326 case GL_LUMINANCE32UI_EXT:
1327 return ctx->API == API_OPENGL_COMPAT &&
1328 ctx->Extensions.EXT_texture_integer &&
1329 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1331 case GL_LUMINANCE_ALPHA8I_EXT:
1332 case GL_LUMINANCE_ALPHA8UI_EXT:
1333 case GL_LUMINANCE_ALPHA16I_EXT:
1334 case GL_LUMINANCE_ALPHA16UI_EXT:
1335 case GL_LUMINANCE_ALPHA32I_EXT:
1336 case GL_LUMINANCE_ALPHA32UI_EXT:
1337 return ctx->API == API_OPENGL_COMPAT &&
1338 ctx->Extensions.EXT_texture_integer &&
1339 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1341 case GL_ALPHA8I_EXT:
1342 case GL_ALPHA8UI_EXT:
1343 case GL_ALPHA16I_EXT:
1344 case GL_ALPHA16UI_EXT:
1345 case GL_ALPHA32I_EXT:
1346 case GL_ALPHA32UI_EXT:
1347 return ctx->API == API_OPENGL_COMPAT &&
1348 ctx->Extensions.EXT_texture_integer &&
1349 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1352 return (_mesa_is_desktop_gl(ctx) &&
1353 ctx->Extensions.ARB_texture_rgb10_a2ui)
1354 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1357 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1366 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1369 invalidate_rb(GLuint key, void *data, void *userData)
1371 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1372 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1374 /* If this is a user-created FBO */
1375 if (_mesa_is_user_fbo(fb)) {
1377 for (i = 0; i < BUFFER_COUNT; i++) {
1378 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1379 if (att->Type == GL_RENDERBUFFER &&
1380 att->Renderbuffer == rb) {
1381 /* Mark fb status as indeterminate to force re-validation */
1390 /** sentinal value, see below */
1391 #define NO_SAMPLES 1000
1395 * Helper function used by _mesa_RenderbufferStorage() and
1396 * _mesa_RenderbufferStorageMultisample().
1397 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1400 renderbuffer_storage(GLenum target, GLenum internalFormat,
1401 GLsizei width, GLsizei height, GLsizei samples)
1403 const char *func = samples == NO_SAMPLES ?
1404 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1405 struct gl_renderbuffer *rb;
1407 GET_CURRENT_CONTEXT(ctx);
1409 ASSERT_OUTSIDE_BEGIN_END(ctx);
1411 if (MESA_VERBOSE & VERBOSE_API) {
1412 if (samples == NO_SAMPLES)
1413 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
1415 _mesa_lookup_enum_by_nr(target),
1416 _mesa_lookup_enum_by_nr(internalFormat),
1419 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
1421 _mesa_lookup_enum_by_nr(target),
1422 _mesa_lookup_enum_by_nr(internalFormat),
1423 width, height, samples);
1426 if (target != GL_RENDERBUFFER_EXT) {
1427 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1431 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1432 if (baseFormat == 0) {
1433 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
1434 func, _mesa_lookup_enum_by_nr(internalFormat));
1438 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1439 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1443 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1444 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1448 if (samples == NO_SAMPLES) {
1449 /* NumSamples == 0 indicates non-multisampling */
1452 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1453 /* note: driver may choose to use more samples than what's requested */
1454 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1458 rb = ctx->CurrentRenderbuffer;
1460 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1464 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1466 if (rb->InternalFormat == internalFormat &&
1467 rb->Width == (GLuint) width &&
1468 rb->Height == (GLuint) height &&
1469 rb->NumSamples == samples) {
1470 /* no change in allocation needed */
1474 /* These MUST get set by the AllocStorage func */
1475 rb->Format = MESA_FORMAT_NONE;
1476 rb->NumSamples = samples;
1478 /* Now allocate the storage */
1479 ASSERT(rb->AllocStorage);
1480 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1481 /* No error - check/set fields now */
1482 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1483 assert(rb->Width == (GLuint) width);
1484 assert(rb->Height == (GLuint) height);
1485 rb->InternalFormat = internalFormat;
1486 rb->_BaseFormat = baseFormat;
1487 assert(rb->_BaseFormat != 0);
1490 /* Probably ran out of memory - clear the fields */
1493 rb->Format = MESA_FORMAT_NONE;
1494 rb->InternalFormat = GL_NONE;
1495 rb->_BaseFormat = GL_NONE;
1499 /* Invalidate the framebuffers the renderbuffer is attached in. */
1500 if (rb->AttachedAnytime) {
1501 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1507 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1509 struct gl_renderbuffer *rb;
1510 GET_CURRENT_CONTEXT(ctx);
1511 ASSERT_OUTSIDE_BEGIN_END(ctx);
1513 if (!ctx->Extensions.OES_EGL_image) {
1514 _mesa_error(ctx, GL_INVALID_OPERATION,
1515 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1519 if (target != GL_RENDERBUFFER) {
1520 _mesa_error(ctx, GL_INVALID_ENUM,
1521 "EGLImageTargetRenderbufferStorageOES");
1525 rb = ctx->CurrentRenderbuffer;
1527 _mesa_error(ctx, GL_INVALID_OPERATION,
1528 "EGLImageTargetRenderbufferStorageOES");
1532 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1534 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1539 * Helper function for _mesa_GetRenderbufferParameteriv() and
1540 * _mesa_GetFramebufferAttachmentParameteriv()
1541 * We have to be careful to respect the base format. For example, if a
1542 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1543 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1544 * we need to return zero.
1547 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1549 if (_mesa_base_format_has_channel(baseFormat, pname))
1550 return _mesa_get_format_bits(format, pname);
1558 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
1559 GLsizei width, GLsizei height)
1561 /* GL_ARB_fbo says calling this function is equivalent to calling
1562 * glRenderbufferStorageMultisample() with samples=0. We pass in
1563 * a token value here just for error reporting purposes.
1565 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1570 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1571 GLenum internalFormat,
1572 GLsizei width, GLsizei height)
1574 renderbuffer_storage(target, internalFormat, width, height, samples);
1579 * OpenGL ES version of glRenderBufferStorage.
1582 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1583 GLsizei width, GLsizei height)
1585 switch (internalFormat) {
1587 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1588 /* choose a closest format */
1589 internalFormat = GL_RGB5;
1595 renderbuffer_storage(target, internalFormat, width, height, 0);
1600 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
1602 struct gl_renderbuffer *rb;
1603 GET_CURRENT_CONTEXT(ctx);
1605 ASSERT_OUTSIDE_BEGIN_END(ctx);
1607 if (target != GL_RENDERBUFFER_EXT) {
1608 _mesa_error(ctx, GL_INVALID_ENUM,
1609 "glGetRenderbufferParameterivEXT(target)");
1613 rb = ctx->CurrentRenderbuffer;
1615 _mesa_error(ctx, GL_INVALID_OPERATION,
1616 "glGetRenderbufferParameterivEXT");
1620 /* No need to flush here since we're just quering state which is
1621 * not effected by rendering.
1625 case GL_RENDERBUFFER_WIDTH_EXT:
1626 *params = rb->Width;
1628 case GL_RENDERBUFFER_HEIGHT_EXT:
1629 *params = rb->Height;
1631 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1632 *params = rb->InternalFormat;
1634 case GL_RENDERBUFFER_RED_SIZE_EXT:
1635 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1636 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1637 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1638 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1639 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1640 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1642 case GL_RENDERBUFFER_SAMPLES:
1643 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
1644 || _mesa_is_gles3(ctx)) {
1645 *params = rb->NumSamples;
1650 _mesa_error(ctx, GL_INVALID_ENUM,
1651 "glGetRenderbufferParameterivEXT(target)");
1657 GLboolean GLAPIENTRY
1658 _mesa_IsFramebuffer(GLuint framebuffer)
1660 GET_CURRENT_CONTEXT(ctx);
1661 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1663 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1664 if (rb != NULL && rb != &DummyFramebuffer)
1672 * Check if any of the attachments of the given framebuffer are textures
1673 * (render to texture). Call ctx->Driver.RenderTexture() for such
1677 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1680 ASSERT(ctx->Driver.RenderTexture);
1682 if (_mesa_is_winsys_fbo(fb))
1683 return; /* can't render to texture with winsys framebuffers */
1685 for (i = 0; i < BUFFER_COUNT; i++) {
1686 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1687 if (att->Texture && _mesa_get_attachment_teximage(att)) {
1688 ctx->Driver.RenderTexture(ctx, fb, att);
1695 * Examine all the framebuffer's attachments to see if any are textures.
1696 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1697 * notify the device driver that the texture image may have changed.
1700 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1702 if (_mesa_is_winsys_fbo(fb))
1703 return; /* can't render to texture with winsys framebuffers */
1705 if (ctx->Driver.FinishRenderTexture) {
1707 for (i = 0; i < BUFFER_COUNT; i++) {
1708 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1709 if (att->Texture && att->Renderbuffer) {
1710 ctx->Driver.FinishRenderTexture(ctx, att);
1718 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
1720 struct gl_framebuffer *newDrawFb, *newReadFb;
1721 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1722 GLboolean bindReadBuf, bindDrawBuf;
1723 GET_CURRENT_CONTEXT(ctx);
1726 if (ctx->Extensions.ARB_framebuffer_object) {
1727 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1728 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1732 ASSERT_OUTSIDE_BEGIN_END(ctx);
1734 if (!ctx->Extensions.EXT_framebuffer_object) {
1735 _mesa_error(ctx, GL_INVALID_OPERATION,
1736 "glBindFramebufferEXT(unsupported)");
1741 case GL_DRAW_FRAMEBUFFER_EXT:
1742 if (!ctx->Extensions.EXT_framebuffer_blit) {
1743 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1746 bindDrawBuf = GL_TRUE;
1747 bindReadBuf = GL_FALSE;
1749 case GL_READ_FRAMEBUFFER_EXT:
1750 if (!ctx->Extensions.EXT_framebuffer_blit) {
1751 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1754 bindDrawBuf = GL_FALSE;
1755 bindReadBuf = GL_TRUE;
1757 case GL_FRAMEBUFFER_EXT:
1758 bindDrawBuf = GL_TRUE;
1759 bindReadBuf = GL_TRUE;
1762 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1767 /* Binding a user-created framebuffer object */
1768 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1769 if (newDrawFb == &DummyFramebuffer) {
1770 /* ID was reserved, but no real framebuffer object made yet */
1774 && _mesa_is_desktop_gl(ctx)
1775 && ctx->Extensions.ARB_framebuffer_object) {
1776 /* All FBO IDs must be Gen'd */
1777 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1782 /* create new framebuffer object */
1783 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1785 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1788 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1790 newReadFb = newDrawFb;
1793 /* Binding the window system framebuffer (which was originally set
1794 * with MakeCurrent).
1796 newDrawFb = ctx->WinSysDrawBuffer;
1797 newReadFb = ctx->WinSysReadBuffer;
1801 ASSERT(newDrawFb != &DummyFramebuffer);
1803 /* save pointers to current/old framebuffers */
1804 oldDrawFb = ctx->DrawBuffer;
1805 oldReadFb = ctx->ReadBuffer;
1807 /* check if really changing bindings */
1808 if (oldDrawFb == newDrawFb)
1809 bindDrawBuf = GL_FALSE;
1810 if (oldReadFb == newReadFb)
1811 bindReadBuf = GL_FALSE;
1814 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1816 * We also check if we're beginning and/or ending render-to-texture.
1817 * When a framebuffer with texture attachments is unbound, call
1818 * ctx->Driver.FinishRenderTexture().
1819 * When a framebuffer with texture attachments is bound, call
1820 * ctx->Driver.RenderTexture().
1822 * Note that if the ReadBuffer has texture attachments we don't consider
1823 * that a render-to-texture case.
1826 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1828 /* check if old readbuffer was render-to-texture */
1829 check_end_texture_render(ctx, oldReadFb);
1831 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1835 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1837 /* check if old framebuffer had any texture attachments */
1839 check_end_texture_render(ctx, oldDrawFb);
1841 /* check if newly bound framebuffer has any texture attachments */
1842 check_begin_texture_render(ctx, newDrawFb);
1844 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1847 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1848 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1854 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
1857 GET_CURRENT_CONTEXT(ctx);
1859 ASSERT_OUTSIDE_BEGIN_END(ctx);
1860 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1862 for (i = 0; i < n; i++) {
1863 if (framebuffers[i] > 0) {
1864 struct gl_framebuffer *fb;
1865 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1867 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1869 /* check if deleting currently bound framebuffer object */
1870 if (ctx->Extensions.EXT_framebuffer_blit) {
1871 /* separate draw/read binding points */
1872 if (fb == ctx->DrawBuffer) {
1874 ASSERT(fb->RefCount >= 2);
1875 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
1877 if (fb == ctx->ReadBuffer) {
1879 ASSERT(fb->RefCount >= 2);
1880 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
1884 /* only one binding point for read/draw buffers */
1885 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1887 ASSERT(fb->RefCount >= 2);
1888 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
1892 /* remove from hash table immediately, to free the ID */
1893 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1895 if (fb != &DummyFramebuffer) {
1896 /* But the object will not be freed until it's no longer
1897 * bound in any context.
1899 _mesa_reference_framebuffer(&fb, NULL);
1908 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
1910 GET_CURRENT_CONTEXT(ctx);
1914 ASSERT_OUTSIDE_BEGIN_END(ctx);
1917 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1924 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1926 for (i = 0; i < n; i++) {
1927 GLuint name = first + i;
1928 framebuffers[i] = name;
1929 /* insert dummy placeholder into hash table */
1930 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1931 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1932 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1939 _mesa_CheckFramebufferStatus(GLenum target)
1941 struct gl_framebuffer *buffer;
1942 GET_CURRENT_CONTEXT(ctx);
1944 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1946 if (MESA_VERBOSE & VERBOSE_API)
1947 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
1948 _mesa_lookup_enum_by_nr(target));
1950 buffer = get_framebuffer_target(ctx, target);
1952 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1956 if (_mesa_is_winsys_fbo(buffer)) {
1957 /* The window system / default framebuffer is always complete */
1958 return GL_FRAMEBUFFER_COMPLETE_EXT;
1961 /* No need to flush here */
1963 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1964 _mesa_test_framebuffer_completeness(ctx, buffer);
1967 return buffer->_Status;
1972 * Replicate the src attachment point. Used by framebuffer_texture() when
1973 * the same texture is attached at GL_DEPTH_ATTACHMENT and
1974 * GL_STENCIL_ATTACHMENT.
1977 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
1978 gl_buffer_index dst,
1979 gl_buffer_index src)
1981 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
1982 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
1984 assert(src_att->Texture != NULL);
1985 assert(src_att->Renderbuffer != NULL);
1987 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
1988 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
1989 dst_att->Type = src_att->Type;
1990 dst_att->Complete = src_att->Complete;
1991 dst_att->TextureLevel = src_att->TextureLevel;
1992 dst_att->Zoffset = src_att->Zoffset;
1997 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
1998 * glFramebufferTextureLayerEXT().
1999 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2000 * get textarget=0 in that case.
2003 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target,
2004 GLenum attachment, GLenum textarget, GLuint texture,
2005 GLint level, GLint zoffset)
2007 struct gl_renderbuffer_attachment *att;
2008 struct gl_texture_object *texObj = NULL;
2009 struct gl_framebuffer *fb;
2010 GLenum maxLevelsTarget;
2012 ASSERT_OUTSIDE_BEGIN_END(ctx);
2014 fb = get_framebuffer_target(ctx, target);
2016 _mesa_error(ctx, GL_INVALID_ENUM,
2017 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
2021 /* check framebuffer binding */
2022 if (_mesa_is_winsys_fbo(fb)) {
2023 _mesa_error(ctx, GL_INVALID_OPERATION,
2024 "glFramebufferTexture%sEXT", caller);
2028 /* The textarget, level, and zoffset parameters are only validated if
2029 * texture is non-zero.
2032 GLboolean err = GL_TRUE;
2034 texObj = _mesa_lookup_texture(ctx, texture);
2035 if (texObj != NULL) {
2036 if (textarget == 0) {
2037 /* If textarget == 0 it means we're being called by
2038 * glFramebufferTextureLayer() and textarget is not used.
2039 * The only legal texture types for that function are 3D and
2040 * 1D/2D arrays textures.
2042 err = (texObj->Target != GL_TEXTURE_3D) &&
2043 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
2044 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT) &&
2045 (texObj->Target != GL_TEXTURE_CUBE_MAP_ARRAY);
2048 /* Make sure textarget is consistent with the texture's type */
2049 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
2050 ? !_mesa_is_cube_face(textarget)
2051 : (texObj->Target != textarget);
2055 /* can't render to a non-existant texture */
2056 _mesa_error(ctx, GL_INVALID_OPERATION,
2057 "glFramebufferTexture%sEXT(non existant texture)",
2063 _mesa_error(ctx, GL_INVALID_OPERATION,
2064 "glFramebufferTexture%sEXT(texture target mismatch)",
2069 if (texObj->Target == GL_TEXTURE_3D) {
2070 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
2071 if (zoffset < 0 || zoffset >= maxSize) {
2072 _mesa_error(ctx, GL_INVALID_VALUE,
2073 "glFramebufferTexture%sEXT(zoffset)", caller);
2077 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
2078 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT) ||
2079 (texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY)) {
2081 zoffset >= (GLint) ctx->Const.MaxArrayTextureLayers) {
2082 _mesa_error(ctx, GL_INVALID_VALUE,
2083 "glFramebufferTexture%sEXT(layer)", caller);
2088 maxLevelsTarget = textarget ? textarget : texObj->Target;
2090 (level >= _mesa_max_texture_levels(ctx, maxLevelsTarget))) {
2091 _mesa_error(ctx, GL_INVALID_VALUE,
2092 "glFramebufferTexture%sEXT(level)", caller);
2097 att = _mesa_get_attachment(ctx, fb, attachment);
2099 _mesa_error(ctx, GL_INVALID_ENUM,
2100 "glFramebufferTexture%sEXT(attachment)", caller);
2104 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2106 _glthread_LOCK_MUTEX(fb->Mutex);
2108 if (attachment == GL_DEPTH_ATTACHMENT &&
2109 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
2110 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
2111 _mesa_tex_target_to_face(textarget) ==
2112 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
2113 zoffset == fb->Attachment[BUFFER_STENCIL].Zoffset) {
2114 /* The texture object is already attached to the stencil attachment
2115 * point. Don't create a new renderbuffer; just reuse the stencil
2116 * attachment's. This is required to prevent a GL error in
2117 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2119 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2121 } else if (attachment == GL_STENCIL_ATTACHMENT &&
2122 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
2123 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
2124 _mesa_tex_target_to_face(textarget) ==
2125 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
2126 zoffset == fb->Attachment[BUFFER_DEPTH].Zoffset) {
2127 /* As above, but with depth and stencil transposed. */
2128 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2131 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
2133 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2134 /* Above we created a new renderbuffer and attached it to the
2135 * depth attachment point. Now attach it to the stencil attachment
2138 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2139 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2144 /* Set the render-to-texture flag. We'll check this flag in
2145 * glTexImage() and friends to determine if we need to revalidate
2146 * any FBOs that might be rendering into this texture.
2147 * This flag never gets cleared since it's non-trivial to determine
2148 * when all FBOs might be done rendering to this texture. That's OK
2149 * though since it's uncommon to render to a texture then repeatedly
2150 * call glTexImage() to change images in the texture.
2152 texObj->_RenderToTexture = GL_TRUE;
2155 _mesa_remove_attachment(ctx, att);
2156 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2157 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2158 _mesa_remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2162 invalidate_framebuffer(fb);
2164 _glthread_UNLOCK_MUTEX(fb->Mutex);
2170 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
2171 GLenum textarget, GLuint texture, GLint level)
2173 GET_CURRENT_CONTEXT(ctx);
2178 switch (textarget) {
2182 case GL_TEXTURE_1D_ARRAY:
2183 error = !ctx->Extensions.EXT_texture_array;
2190 _mesa_error(ctx, GL_INVALID_OPERATION,
2191 "glFramebufferTexture1DEXT(textarget=%s)",
2192 _mesa_lookup_enum_by_nr(textarget));
2197 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2203 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
2204 GLenum textarget, GLuint texture, GLint level)
2206 GET_CURRENT_CONTEXT(ctx);
2211 switch (textarget) {
2215 case GL_TEXTURE_RECTANGLE:
2216 error = _mesa_is_gles(ctx)
2217 || !ctx->Extensions.NV_texture_rectangle;
2219 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2220 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2221 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2222 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2223 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2224 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2225 error = !ctx->Extensions.ARB_texture_cube_map;
2227 case GL_TEXTURE_2D_ARRAY:
2228 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2229 || !ctx->Extensions.EXT_texture_array;
2236 _mesa_error(ctx, GL_INVALID_OPERATION,
2237 "glFramebufferTexture2DEXT(textarget=%s)",
2238 _mesa_lookup_enum_by_nr(textarget));
2243 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2249 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
2250 GLenum textarget, GLuint texture,
2251 GLint level, GLint zoffset)
2253 GET_CURRENT_CONTEXT(ctx);
2255 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2256 _mesa_error(ctx, GL_INVALID_OPERATION,
2257 "glFramebufferTexture3DEXT(textarget)");
2261 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2267 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
2268 GLuint texture, GLint level, GLint layer)
2270 GET_CURRENT_CONTEXT(ctx);
2272 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2278 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
2279 GLenum renderbufferTarget,
2280 GLuint renderbuffer)
2282 struct gl_renderbuffer_attachment *att;
2283 struct gl_framebuffer *fb;
2284 struct gl_renderbuffer *rb;
2285 GET_CURRENT_CONTEXT(ctx);
2287 ASSERT_OUTSIDE_BEGIN_END(ctx);
2289 fb = get_framebuffer_target(ctx, target);
2291 _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2295 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2296 _mesa_error(ctx, GL_INVALID_ENUM,
2297 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2301 if (_mesa_is_winsys_fbo(fb)) {
2302 /* Can't attach new renderbuffers to a window system framebuffer */
2303 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2307 att = _mesa_get_attachment(ctx, fb, attachment);
2309 _mesa_error(ctx, GL_INVALID_ENUM,
2310 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2311 _mesa_lookup_enum_by_nr(attachment));
2316 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2318 _mesa_error(ctx, GL_INVALID_OPERATION,
2319 "glFramebufferRenderbufferEXT(non-existant"
2320 " renderbuffer %u)", renderbuffer);
2323 else if (rb == &DummyRenderbuffer) {
2324 /* This is what NVIDIA does */
2325 _mesa_error(ctx, GL_INVALID_VALUE,
2326 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2332 /* remove renderbuffer attachment */
2336 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2337 rb && rb->Format != MESA_FORMAT_NONE) {
2338 /* make sure the renderbuffer is a depth/stencil format */
2339 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2340 if (baseFormat != GL_DEPTH_STENCIL) {
2341 _mesa_error(ctx, GL_INVALID_OPERATION,
2342 "glFramebufferRenderbufferEXT(renderbuffer"
2343 " is not DEPTH_STENCIL format)");
2349 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2351 assert(ctx->Driver.FramebufferRenderbuffer);
2352 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2354 /* Some subsequent GL commands may depend on the framebuffer's visual
2355 * after the binding is updated. Update visual info now.
2357 _mesa_update_framebuffer_visual(ctx, fb);
2362 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
2363 GLenum pname, GLint *params)
2365 const struct gl_renderbuffer_attachment *att;
2366 struct gl_framebuffer *buffer;
2368 GET_CURRENT_CONTEXT(ctx);
2370 ASSERT_OUTSIDE_BEGIN_END(ctx);
2372 /* The error differs in GL and GLES. */
2373 err = _mesa_is_desktop_gl(ctx) ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2375 buffer = get_framebuffer_target(ctx, target);
2377 _mesa_error(ctx, GL_INVALID_ENUM,
2378 "glGetFramebufferAttachmentParameterivEXT(target)");
2382 if (_mesa_is_winsys_fbo(buffer)) {
2383 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2386 * "If the framebuffer currently bound to target is zero, then
2387 * INVALID_OPERATION is generated."
2389 * The EXT_framebuffer_object spec has the same wording, and the
2390 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2393 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2394 && !_mesa_is_gles3(ctx)) {
2395 _mesa_error(ctx, GL_INVALID_OPERATION,
2396 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2400 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
2401 attachment != GL_DEPTH && attachment != GL_STENCIL) {
2402 _mesa_error(ctx, GL_INVALID_OPERATION,
2403 "glGetFramebufferAttachmentParameteriv(attachment)");
2406 /* the default / window-system FBO */
2407 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2410 /* user-created framebuffer FBO */
2411 att = _mesa_get_attachment(ctx, buffer, attachment);
2415 _mesa_error(ctx, GL_INVALID_ENUM,
2416 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2420 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2421 /* the depth and stencil attachments must point to the same buffer */
2422 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2423 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2424 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2425 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2426 _mesa_error(ctx, GL_INVALID_OPERATION,
2427 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2428 " attachments differ)");
2433 /* No need to flush here */
2436 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2437 *params = _mesa_is_winsys_fbo(buffer)
2438 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2440 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2441 if (att->Type == GL_RENDERBUFFER_EXT) {
2442 *params = att->Renderbuffer->Name;
2444 else if (att->Type == GL_TEXTURE) {
2445 *params = att->Texture->Name;
2448 assert(att->Type == GL_NONE);
2449 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
2452 goto invalid_pname_enum;
2456 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2457 if (att->Type == GL_TEXTURE) {
2458 *params = att->TextureLevel;
2460 else if (att->Type == GL_NONE) {
2461 _mesa_error(ctx, err,
2462 "glGetFramebufferAttachmentParameterivEXT(pname)");
2465 goto invalid_pname_enum;
2468 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2469 if (att->Type == GL_TEXTURE) {
2470 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2471 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2477 else if (att->Type == GL_NONE) {
2478 _mesa_error(ctx, err,
2479 "glGetFramebufferAttachmentParameterivEXT(pname)");
2482 goto invalid_pname_enum;
2485 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2486 if (ctx->API == API_OPENGLES) {
2487 goto invalid_pname_enum;
2488 } else if (att->Type == GL_NONE) {
2489 _mesa_error(ctx, err,
2490 "glGetFramebufferAttachmentParameterivEXT(pname)");
2491 } else if (att->Type == GL_TEXTURE) {
2492 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2493 *params = att->Zoffset;
2500 goto invalid_pname_enum;
2503 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2504 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2505 && !_mesa_is_gles3(ctx)) {
2506 goto invalid_pname_enum;
2508 else if (att->Type == GL_NONE) {
2509 _mesa_error(ctx, err,
2510 "glGetFramebufferAttachmentParameterivEXT(pname)");
2513 if (ctx->Extensions.EXT_framebuffer_sRGB) {
2514 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2517 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2518 * if the sRGB conversion is unsupported. */
2519 *params = GL_LINEAR;
2523 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2524 if ((ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_framebuffer_object)
2525 && ctx->API != API_OPENGL_CORE
2526 && !_mesa_is_gles3(ctx)) {
2527 goto invalid_pname_enum;
2529 else if (att->Type == GL_NONE) {
2530 _mesa_error(ctx, err,
2531 "glGetFramebufferAttachmentParameterivEXT(pname)");
2534 gl_format format = att->Renderbuffer->Format;
2535 if (format == MESA_FORMAT_S8) {
2539 else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2540 /* depends on the attachment parameter */
2541 if (attachment == GL_STENCIL_ATTACHMENT) {
2549 *params = _mesa_get_format_datatype(format);
2553 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2554 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2555 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2556 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2557 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2558 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2559 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2560 && !_mesa_is_gles3(ctx)) {
2561 goto invalid_pname_enum;
2563 else if (att->Type == GL_NONE) {
2564 _mesa_error(ctx, err,
2565 "glGetFramebufferAttachmentParameterivEXT(pname)");
2567 else if (att->Texture) {
2568 const struct gl_texture_image *texImage =
2569 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2572 *params = get_component_bits(pname, texImage->_BaseFormat,
2573 texImage->TexFormat);
2579 else if (att->Renderbuffer) {
2580 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2581 att->Renderbuffer->Format);
2584 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2585 " invalid FBO attachment structure");
2589 goto invalid_pname_enum;
2595 _mesa_error(ctx, GL_INVALID_ENUM,
2596 "glGetFramebufferAttachmentParameteriv(pname)");
2602 _mesa_GenerateMipmap(GLenum target)
2604 struct gl_texture_image *srcImage;
2605 struct gl_texture_object *texObj;
2608 GET_CURRENT_CONTEXT(ctx);
2610 ASSERT_OUTSIDE_BEGIN_END(ctx);
2611 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2615 error = _mesa_is_gles(ctx);
2621 error = ctx->API == API_OPENGLES;
2623 case GL_TEXTURE_CUBE_MAP:
2624 error = !ctx->Extensions.ARB_texture_cube_map;
2626 case GL_TEXTURE_1D_ARRAY:
2627 error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
2629 case GL_TEXTURE_2D_ARRAY:
2630 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2631 || !ctx->Extensions.EXT_texture_array;
2638 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2639 _mesa_lookup_enum_by_nr(target));
2643 texObj = _mesa_get_current_tex_object(ctx, target);
2645 if (texObj->BaseLevel >= texObj->MaxLevel) {
2650 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2651 !_mesa_cube_complete(texObj)) {
2652 _mesa_error(ctx, GL_INVALID_OPERATION,
2653 "glGenerateMipmap(incomplete cube map)");
2657 _mesa_lock_texture(ctx, texObj);
2659 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
2661 _mesa_unlock_texture(ctx, texObj);
2662 _mesa_error(ctx, GL_INVALID_OPERATION,
2663 "glGenerateMipmap(zero size base image)");
2667 if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
2668 _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
2669 _mesa_is_stencil_format(srcImage->InternalFormat)) {
2670 _mesa_unlock_texture(ctx, texObj);
2671 _mesa_error(ctx, GL_INVALID_OPERATION,
2672 "glGenerateMipmap(invalid internal format)");
2676 if (target == GL_TEXTURE_CUBE_MAP) {
2678 for (face = 0; face < 6; face++)
2679 ctx->Driver.GenerateMipmap(ctx,
2680 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2684 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2686 _mesa_unlock_texture(ctx, texObj);
2690 static const struct gl_renderbuffer_attachment *
2691 find_attachment(const struct gl_framebuffer *fb,
2692 const struct gl_renderbuffer *rb)
2695 for (i = 0; i < Elements(fb->Attachment); i++) {
2696 if (fb->Attachment[i].Renderbuffer == rb)
2697 return &fb->Attachment[i];
2704 * Helper function for checking if the datatypes of color buffers are
2705 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2707 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2708 * and any of the following conditions hold:
2709 * - The read buffer contains fixed-point or floating-point values and any
2710 * draw buffer contains neither fixed-point nor floating-point values.
2711 * - The read buffer contains unsigned integer values and any draw buffer
2712 * does not contain unsigned integer values.
2713 * - The read buffer contains signed integer values and any draw buffer
2714 * does not contain signed integer values."
2717 compatible_color_datatypes(gl_format srcFormat, gl_format dstFormat)
2719 GLenum srcType = _mesa_get_format_datatype(srcFormat);
2720 GLenum dstType = _mesa_get_format_datatype(dstFormat);
2722 if (srcType != GL_INT && srcType != GL_UNSIGNED_INT) {
2723 assert(srcType == GL_UNSIGNED_NORMALIZED ||
2724 srcType == GL_SIGNED_NORMALIZED ||
2725 srcType == GL_FLOAT);
2726 /* Boil any of those types down to GL_FLOAT */
2730 if (dstType != GL_INT && dstType != GL_UNSIGNED_INT) {
2731 assert(dstType == GL_UNSIGNED_NORMALIZED ||
2732 dstType == GL_SIGNED_NORMALIZED ||
2733 dstType == GL_FLOAT);
2734 /* Boil any of those types down to GL_FLOAT */
2738 return srcType == dstType;
2743 compatible_resolve_formats(const struct gl_renderbuffer *readRb,
2744 const struct gl_renderbuffer *drawRb)
2746 GLenum readFormat, drawFormat;
2748 /* The simple case where we know the backing Mesa formats are the same.
2750 if (_mesa_get_srgb_format_linear(readRb->Format) ==
2751 _mesa_get_srgb_format_linear(drawRb->Format)) {
2755 /* The Mesa formats are different, so we must check whether the internal
2756 * formats are compatible.
2758 * Under some circumstances, the user may request e.g. two GL_RGBA8
2759 * textures and get two entirely different Mesa formats like RGBA8888 and
2760 * ARGB8888. Drivers behaving like that should be able to cope with
2761 * non-matching formats by themselves, because it's not the user's fault.
2763 * Blits between linear and sRGB formats are also allowed.
2765 readFormat = _mesa_get_nongeneric_internalformat(readRb->InternalFormat);
2766 drawFormat = _mesa_get_nongeneric_internalformat(drawRb->InternalFormat);
2767 readFormat = _mesa_get_linear_internalformat(readFormat);
2768 drawFormat = _mesa_get_linear_internalformat(drawFormat);
2770 if (readFormat == drawFormat) {
2779 * Blit rectangular region, optionally from one framebuffer to another.
2781 * Note, if the src buffer is multisampled and the dest is not, this is
2782 * when the samples must be resolved to a single color.
2785 _mesa_BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2786 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2787 GLbitfield mask, GLenum filter)
2789 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2790 GL_DEPTH_BUFFER_BIT |
2791 GL_STENCIL_BUFFER_BIT);
2792 const struct gl_framebuffer *readFb, *drawFb;
2793 GET_CURRENT_CONTEXT(ctx);
2795 ASSERT_OUTSIDE_BEGIN_END(ctx);
2796 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2798 if (MESA_VERBOSE & VERBOSE_API)
2800 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2801 srcX0, srcY0, srcX1, srcY1,
2802 dstX0, dstY0, dstX1, dstY1,
2803 mask, _mesa_lookup_enum_by_nr(filter));
2805 if (ctx->NewState) {
2806 _mesa_update_state(ctx);
2809 readFb = ctx->ReadBuffer;
2810 drawFb = ctx->DrawBuffer;
2812 if (!readFb || !drawFb) {
2813 /* This will normally never happen but someday we may want to
2814 * support MakeCurrent() with no drawables.
2819 /* check for complete framebuffers */
2820 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2821 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2822 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2823 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2827 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2828 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2832 if (mask & ~legalMaskBits) {
2833 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2837 /* depth/stencil must be blitted with nearest filtering */
2838 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2839 && filter != GL_NEAREST) {
2840 _mesa_error(ctx, GL_INVALID_OPERATION,
2841 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2845 /* get color read/draw renderbuffers */
2846 if (mask & GL_COLOR_BUFFER_BIT) {
2847 const GLuint numColorDrawBuffers = ctx->DrawBuffer->_NumColorDrawBuffers;
2848 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
2849 const struct gl_renderbuffer *colorDrawRb = NULL;
2852 /* From the EXT_framebuffer_object spec:
2854 * "If a buffer is specified in <mask> and does not exist in both
2855 * the read and draw framebuffers, the corresponding bit is silently
2858 if (!colorReadRb || numColorDrawBuffers == 0) {
2859 mask &= ~GL_COLOR_BUFFER_BIT;
2862 for (i = 0; i < numColorDrawBuffers; i++) {
2863 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
2867 if (!compatible_color_datatypes(colorReadRb->Format,
2868 colorDrawRb->Format)) {
2869 _mesa_error(ctx, GL_INVALID_OPERATION,
2870 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2873 /* extra checks for multisample copies... */
2874 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2875 /* color formats must match */
2876 if (!compatible_resolve_formats(colorReadRb, colorDrawRb)) {
2877 _mesa_error(ctx, GL_INVALID_OPERATION,
2878 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2883 if (filter == GL_LINEAR) {
2884 /* 3.1 spec, page 199:
2885 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2886 * if filter is LINEAR and read buffer contains integer data."
2888 GLenum type = _mesa_get_format_datatype(colorReadRb->Format);
2889 if (type == GL_INT || type == GL_UNSIGNED_INT) {
2890 _mesa_error(ctx, GL_INVALID_OPERATION,
2891 "glBlitFramebufferEXT(integer color type)");
2898 if (mask & GL_STENCIL_BUFFER_BIT) {
2899 struct gl_renderbuffer *readRb =
2900 readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2901 struct gl_renderbuffer *drawRb =
2902 drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2904 /* From the EXT_framebuffer_object spec:
2906 * "If a buffer is specified in <mask> and does not exist in both
2907 * the read and draw framebuffers, the corresponding bit is silently
2910 if ((readRb == NULL) || (drawRb == NULL)) {
2911 mask &= ~GL_STENCIL_BUFFER_BIT;
2914 int read_z_bits, draw_z_bits;
2916 if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2917 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2918 /* There is no need to check the stencil datatype here, because
2919 * there is only one: GL_UNSIGNED_INT.
2921 _mesa_error(ctx, GL_INVALID_OPERATION,
2922 "glBlitFramebuffer(stencil attachment format mismatch)");
2926 read_z_bits = _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS);
2927 draw_z_bits = _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS);
2929 /* If both buffers also have depth data, the depth formats must match
2930 * as well. If one doesn't have depth, it's not blitted, so we should
2931 * ignore the depth format check.
2933 if (read_z_bits > 0 && draw_z_bits > 0 &&
2934 (read_z_bits != draw_z_bits ||
2935 _mesa_get_format_datatype(readRb->Format) !=
2936 _mesa_get_format_datatype(drawRb->Format))) {
2938 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
2939 "(stencil attachment depth format mismatch)");
2945 if (mask & GL_DEPTH_BUFFER_BIT) {
2946 struct gl_renderbuffer *readRb =
2947 readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2948 struct gl_renderbuffer *drawRb =
2949 drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2951 /* From the EXT_framebuffer_object spec:
2953 * "If a buffer is specified in <mask> and does not exist in both
2954 * the read and draw framebuffers, the corresponding bit is silently
2957 if ((readRb == NULL) || (drawRb == NULL)) {
2958 mask &= ~GL_DEPTH_BUFFER_BIT;
2961 int read_s_bit, draw_s_bit;
2963 if ((_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2964 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) ||
2965 (_mesa_get_format_datatype(readRb->Format) !=
2966 _mesa_get_format_datatype(drawRb->Format))) {
2967 _mesa_error(ctx, GL_INVALID_OPERATION,
2968 "glBlitFramebuffer(depth attachment format mismatch)");
2972 read_s_bit = _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS);
2973 draw_s_bit = _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS);
2975 /* If both buffers also have stencil data, the stencil formats must
2976 * match as well. If one doesn't have stencil, it's not blitted, so
2977 * we should ignore the stencil format check.
2979 if (read_s_bit > 0 && draw_s_bit > 0 && read_s_bit != draw_s_bit) {
2980 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
2981 "(depth attachment stencil bits mismatch)");
2987 if (readFb->Visual.samples > 0 &&
2988 drawFb->Visual.samples > 0 &&
2989 readFb->Visual.samples != drawFb->Visual.samples) {
2990 _mesa_error(ctx, GL_INVALID_OPERATION,
2991 "glBlitFramebufferEXT(mismatched samples)");
2995 /* extra checks for multisample copies... */
2996 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2997 /* src and dest region sizes must be the same */
2998 if (abs(srcX1 - srcX0) != abs(dstX1 - dstX0) ||
2999 abs(srcY1 - srcY0) != abs(dstY1 - dstY0)) {
3000 _mesa_error(ctx, GL_INVALID_OPERATION,
3001 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3006 if (!ctx->Extensions.EXT_framebuffer_blit) {
3007 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
3013 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
3014 const struct gl_renderbuffer *colorDrawRb = NULL;
3017 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3019 srcX0, srcY0, srcX1, srcY1,
3020 dstX0, dstY0, dstX1, dstY1,
3023 const struct gl_renderbuffer_attachment *att;
3025 att = find_attachment(readFb, colorReadRb);
3026 printf(" Src FBO %u RB %u (%dx%d) ",
3027 readFb->Name, colorReadRb->Name,
3028 colorReadRb->Width, colorReadRb->Height);
3029 if (att && att->Texture) {
3030 printf("Tex %u tgt 0x%x level %u face %u",
3032 att->Texture->Target,
3038 /* Print all active color render buffers */
3039 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
3040 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
3044 att = find_attachment(drawFb, colorDrawRb);
3045 printf(" Dst FBO %u RB %u (%dx%d) ",
3046 drawFb->Name, colorDrawRb->Name,
3047 colorDrawRb->Width, colorDrawRb->Height);
3048 if (att && att->Texture) {
3049 printf("Tex %u tgt 0x%x level %u face %u",
3051 att->Texture->Target,
3064 ASSERT(ctx->Driver.BlitFramebuffer);
3065 ctx->Driver.BlitFramebuffer(ctx,
3066 srcX0, srcY0, srcX1, srcY1,
3067 dstX0, dstY0, dstX1, dstY1,
3073 invalidate_framebuffer_storage(GLenum target, GLsizei numAttachments,
3074 const GLenum *attachments, GLint x, GLint y,
3075 GLsizei width, GLsizei height, const char *name)
3078 struct gl_framebuffer *fb;
3079 GET_CURRENT_CONTEXT(ctx);
3081 ASSERT_OUTSIDE_BEGIN_END(ctx);
3083 fb = get_framebuffer_target(ctx, target);
3085 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
3089 if (numAttachments < 0) {
3090 _mesa_error(ctx, GL_INVALID_VALUE,
3091 "%s(numAttachments < 0)", name);
3095 /* The GL_ARB_invalidate_subdata spec says:
3097 * "If an attachment is specified that does not exist in the
3098 * framebuffer bound to <target>, it is ignored."
3102 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3103 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3104 * INVALID_OPERATION is generated."
3106 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3107 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3108 * set of retrictions).
3110 for (i = 0; i < numAttachments; i++) {
3111 if (_mesa_is_winsys_fbo(fb)) {
3112 switch (attachments[i]) {
3118 /* Accumulation buffers and auxilary buffers were removed in
3119 * OpenGL 3.1, and they never existed in OpenGL ES.
3121 if (ctx->API != API_OPENGL_COMPAT)
3131 case GL_FRONT_RIGHT:
3132 if (!_mesa_is_desktop_gl(ctx))
3139 switch (attachments[i]) {
3140 case GL_DEPTH_ATTACHMENT:
3141 case GL_STENCIL_ATTACHMENT:
3143 case GL_COLOR_ATTACHMENT0:
3144 case GL_COLOR_ATTACHMENT1:
3145 case GL_COLOR_ATTACHMENT2:
3146 case GL_COLOR_ATTACHMENT3:
3147 case GL_COLOR_ATTACHMENT4:
3148 case GL_COLOR_ATTACHMENT5:
3149 case GL_COLOR_ATTACHMENT6:
3150 case GL_COLOR_ATTACHMENT7:
3151 case GL_COLOR_ATTACHMENT8:
3152 case GL_COLOR_ATTACHMENT9:
3153 case GL_COLOR_ATTACHMENT10:
3154 case GL_COLOR_ATTACHMENT11:
3155 case GL_COLOR_ATTACHMENT12:
3156 case GL_COLOR_ATTACHMENT13:
3157 case GL_COLOR_ATTACHMENT14:
3158 case GL_COLOR_ATTACHMENT15: {
3159 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
3160 if (k >= ctx->Const.MaxColorAttachments) {
3161 _mesa_error(ctx, GL_INVALID_OPERATION,
3162 "%s(attachment >= max. color attachments)", name);
3172 /* We don't actually do anything for this yet. Just return after
3173 * validating the parameters and generating the required errors.
3178 _mesa_error(ctx, GL_INVALID_ENUM, "%s(attachment)", name);
3183 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
3184 const GLenum *attachments, GLint x, GLint y,
3185 GLsizei width, GLsizei height)
3187 invalidate_framebuffer_storage(target, numAttachments, attachments,
3188 x, y, width, height,
3189 "glInvalidateSubFramebuffer");
3193 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
3194 const GLenum *attachments)
3196 /* The GL_ARB_invalidate_subdata spec says:
3200 * void InvalidateFramebuffer(enum target,
3201 * sizei numAttachments,
3202 * const enum *attachments);
3204 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3205 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3206 * <MAX_VIEWPORT_DIMS[1]> respectively."
3208 invalidate_framebuffer_storage(target, numAttachments, attachments,
3209 0, 0, MAX_VIEWPORT_WIDTH, MAX_VIEWPORT_HEIGHT,
3210 "glInvalidateFramebuffer");