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
40 #include "framebuffer.h"
44 #include "mfeatures.h"
46 #include "renderbuffer.h"
52 /** Set this to 1 to help debug FBO incompleteness problems */
55 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
62 * None of the GL_EXT_framebuffer_object functions are compiled into
69 * When glGenRender/FramebuffersEXT() is called we insert pointers to
70 * these placeholder objects into the hash table.
71 * Later, when the object ID is first bound, we replace the placeholder
72 * with the real frame/renderbuffer.
74 static struct gl_framebuffer DummyFramebuffer;
75 static struct gl_renderbuffer DummyRenderbuffer;
77 /* We bind this framebuffer when applications pass a NULL
78 * drawable/surface in make current. */
79 static struct gl_framebuffer IncompleteFramebuffer;
83 * Is the given FBO a user-created FBO?
85 static inline GLboolean
86 is_user_fbo(const struct gl_framebuffer *fb)
93 * Is the given FBO a window system FBO (like an X window)?
95 static inline GLboolean
96 is_winsys_fbo(const struct gl_framebuffer *fb)
103 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
109 delete_dummy_framebuffer(struct gl_framebuffer *fb)
116 _mesa_init_fbobjects(struct gl_context *ctx)
118 _glthread_INIT_MUTEX(DummyFramebuffer.Mutex);
119 _glthread_INIT_MUTEX(DummyRenderbuffer.Mutex);
120 _glthread_INIT_MUTEX(IncompleteFramebuffer.Mutex);
121 DummyFramebuffer.Delete = delete_dummy_framebuffer;
122 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
123 IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
126 struct gl_framebuffer *
127 _mesa_get_incomplete_framebuffer(void)
129 return &IncompleteFramebuffer;
133 * Helper routine for getting a gl_renderbuffer.
135 struct gl_renderbuffer *
136 _mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
138 struct gl_renderbuffer *rb;
143 rb = (struct gl_renderbuffer *)
144 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
150 * Helper routine for getting a gl_framebuffer.
152 struct gl_framebuffer *
153 _mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
155 struct gl_framebuffer *fb;
160 fb = (struct gl_framebuffer *)
161 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
167 * Mark the given framebuffer as invalid. This will force the
168 * test for framebuffer completeness to be done before the framebuffer
172 invalidate_framebuffer(struct gl_framebuffer *fb)
174 fb->_Status = 0; /* "indeterminate" */
179 * Return the gl_framebuffer object which corresponds to the given
180 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
181 * Check support for GL_EXT_framebuffer_blit to determine if certain
183 * \return gl_framebuffer pointer or NULL if target is illegal
185 static struct gl_framebuffer *
186 get_framebuffer_target(struct gl_context *ctx, GLenum target)
189 case GL_DRAW_FRAMEBUFFER:
190 return ctx->Extensions.EXT_framebuffer_blit && ctx->API == API_OPENGL
191 ? ctx->DrawBuffer : NULL;
192 case GL_READ_FRAMEBUFFER:
193 return ctx->Extensions.EXT_framebuffer_blit && ctx->API == API_OPENGL
194 ? ctx->ReadBuffer : NULL;
195 case GL_FRAMEBUFFER_EXT:
196 return ctx->DrawBuffer;
204 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
205 * gl_renderbuffer_attachment object.
206 * This function is only used for user-created FB objects, not the
207 * default / window-system FB object.
208 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
209 * the depth buffer attachment point.
211 struct gl_renderbuffer_attachment *
212 _mesa_get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
217 assert(is_user_fbo(fb));
219 switch (attachment) {
220 case GL_COLOR_ATTACHMENT0_EXT:
221 case GL_COLOR_ATTACHMENT1_EXT:
222 case GL_COLOR_ATTACHMENT2_EXT:
223 case GL_COLOR_ATTACHMENT3_EXT:
224 case GL_COLOR_ATTACHMENT4_EXT:
225 case GL_COLOR_ATTACHMENT5_EXT:
226 case GL_COLOR_ATTACHMENT6_EXT:
227 case GL_COLOR_ATTACHMENT7_EXT:
228 case GL_COLOR_ATTACHMENT8_EXT:
229 case GL_COLOR_ATTACHMENT9_EXT:
230 case GL_COLOR_ATTACHMENT10_EXT:
231 case GL_COLOR_ATTACHMENT11_EXT:
232 case GL_COLOR_ATTACHMENT12_EXT:
233 case GL_COLOR_ATTACHMENT13_EXT:
234 case GL_COLOR_ATTACHMENT14_EXT:
235 case GL_COLOR_ATTACHMENT15_EXT:
236 /* Only OpenGL ES 1.x forbids color attachments other than
237 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
240 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
241 if (i >= ctx->Const.MaxColorAttachments
242 || (i > 0 && ctx->API == API_OPENGLES)) {
245 return &fb->Attachment[BUFFER_COLOR0 + i];
246 case GL_DEPTH_STENCIL_ATTACHMENT:
247 if (ctx->API != API_OPENGL)
250 case GL_DEPTH_ATTACHMENT_EXT:
251 return &fb->Attachment[BUFFER_DEPTH];
252 case GL_STENCIL_ATTACHMENT_EXT:
253 return &fb->Attachment[BUFFER_STENCIL];
261 * As above, but only used for getting attachments of the default /
262 * window-system framebuffer (not user-created framebuffer objects).
264 static struct gl_renderbuffer_attachment *
265 _mesa_get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
268 assert(is_winsys_fbo(fb));
270 switch (attachment) {
272 return &fb->Attachment[BUFFER_FRONT_LEFT];
274 return &fb->Attachment[BUFFER_FRONT_RIGHT];
276 return &fb->Attachment[BUFFER_BACK_LEFT];
278 return &fb->Attachment[BUFFER_BACK_RIGHT];
280 if (fb->Visual.numAuxBuffers == 1) {
281 return &fb->Attachment[BUFFER_AUX0];
285 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
287 * "If the default framebuffer is bound to target, then attachment must
288 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
289 * identifying a color buffer; DEPTH, identifying the depth buffer; or
290 * STENCIL, identifying the stencil buffer."
292 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
293 * language. However, revision #33 of the ARB_framebuffer_object spec
296 * "If the default framebuffer is bound to <target>, then <attachment>
297 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
298 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
299 * depth buffer, or the stencil buffer, and <pname> may be
300 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
301 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
303 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
304 * from glext.h, so shipping apps should not use those values.
306 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
307 * support queries of the window system FBO.
310 return &fb->Attachment[BUFFER_DEPTH];
312 return &fb->Attachment[BUFFER_STENCIL];
321 * Remove any texture or renderbuffer attached to the given attachment
322 * point. Update reference counts, etc.
325 _mesa_remove_attachment(struct gl_context *ctx,
326 struct gl_renderbuffer_attachment *att)
328 if (att->Type == GL_TEXTURE) {
329 ASSERT(att->Texture);
330 if (ctx->Driver.FinishRenderTexture) {
331 /* tell driver that we're done rendering to this texture. */
332 ctx->Driver.FinishRenderTexture(ctx, att);
334 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
335 ASSERT(!att->Texture);
337 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
338 ASSERT(!att->Texture);
339 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
340 ASSERT(!att->Renderbuffer);
343 att->Complete = GL_TRUE;
348 * Bind a texture object to an attachment point.
349 * The previous binding, if any, will be removed first.
352 _mesa_set_texture_attachment(struct gl_context *ctx,
353 struct gl_framebuffer *fb,
354 struct gl_renderbuffer_attachment *att,
355 struct gl_texture_object *texObj,
356 GLenum texTarget, GLuint level, GLuint zoffset)
358 if (att->Texture == texObj) {
359 /* re-attaching same texture */
360 ASSERT(att->Type == GL_TEXTURE);
361 if (ctx->Driver.FinishRenderTexture)
362 ctx->Driver.FinishRenderTexture(ctx, att);
366 if (ctx->Driver.FinishRenderTexture && att->Texture)
367 ctx->Driver.FinishRenderTexture(ctx, att);
368 _mesa_remove_attachment(ctx, att);
369 att->Type = GL_TEXTURE;
370 assert(!att->Texture);
371 _mesa_reference_texobj(&att->Texture, texObj);
374 /* always update these fields */
375 att->TextureLevel = level;
376 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
377 att->Zoffset = zoffset;
378 att->Complete = GL_FALSE;
380 if (_mesa_get_attachment_teximage(att)) {
381 ctx->Driver.RenderTexture(ctx, fb, att);
384 invalidate_framebuffer(fb);
389 * Bind a renderbuffer to an attachment point.
390 * The previous binding, if any, will be removed first.
393 _mesa_set_renderbuffer_attachment(struct gl_context *ctx,
394 struct gl_renderbuffer_attachment *att,
395 struct gl_renderbuffer *rb)
397 /* XXX check if re-doing same attachment, exit early */
398 _mesa_remove_attachment(ctx, att);
399 att->Type = GL_RENDERBUFFER_EXT;
400 att->Texture = NULL; /* just to be safe */
401 att->Complete = GL_FALSE;
402 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
407 * Fallback for ctx->Driver.FramebufferRenderbuffer()
408 * Attach a renderbuffer object to a framebuffer object.
411 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
412 struct gl_framebuffer *fb,
413 GLenum attachment, struct gl_renderbuffer *rb)
415 struct gl_renderbuffer_attachment *att;
417 _glthread_LOCK_MUTEX(fb->Mutex);
419 att = _mesa_get_attachment(ctx, fb, attachment);
422 _mesa_set_renderbuffer_attachment(ctx, att, rb);
423 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
424 /* do stencil attachment here (depth already done above) */
425 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
427 _mesa_set_renderbuffer_attachment(ctx, att, rb);
429 rb->AttachedAnytime = GL_TRUE;
432 _mesa_remove_attachment(ctx, att);
435 invalidate_framebuffer(fb);
437 _glthread_UNLOCK_MUTEX(fb->Mutex);
442 * Fallback for ctx->Driver.ValidateFramebuffer()
443 * Check if the renderbuffer's formats are supported by the software
445 * Drivers should probably override this.
448 _mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
451 for (buf = 0; buf < BUFFER_COUNT; buf++) {
452 const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
454 switch (rb->_BaseFormat) {
456 case GL_LUMINANCE_ALPHA:
461 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
465 switch (rb->Format) {
466 /* XXX This list is likely incomplete. */
467 case MESA_FORMAT_RGB9_E5_FLOAT:
468 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
471 /* render buffer format is supported by software rendering */
483 att_incomplete(const char *msg)
486 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
497 fbo_incomplete(const char *msg, int index)
500 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
509 * Is the given base format a legal format for a color renderbuffer?
512 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
514 switch (baseFormat) {
519 case GL_LUMINANCE_ALPHA:
522 return ctx->Extensions.ARB_framebuffer_object;
525 return ctx->Extensions.ARB_texture_rg;
533 * Is the given base format a legal format for a depth/stencil renderbuffer?
536 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
538 switch (baseFormat) {
539 case GL_DEPTH_COMPONENT:
540 case GL_DEPTH_STENCIL_EXT:
549 * Test if an attachment point is complete and update its Complete field.
550 * \param format if GL_COLOR, this is a color attachment point,
551 * if GL_DEPTH, this is a depth component attachment point,
552 * if GL_STENCIL, this is a stencil component attachment point.
555 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
556 struct gl_renderbuffer_attachment *att)
558 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
560 /* assume complete */
561 att->Complete = GL_TRUE;
563 /* Look for reasons why the attachment might be incomplete */
564 if (att->Type == GL_TEXTURE) {
565 const struct gl_texture_object *texObj = att->Texture;
566 struct gl_texture_image *texImage;
570 att_incomplete("no texobj");
571 att->Complete = GL_FALSE;
575 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
577 att_incomplete("no teximage");
578 att->Complete = GL_FALSE;
581 if (texImage->Width < 1 || texImage->Height < 1) {
582 att_incomplete("teximage width/height=0");
583 printf("texobj = %u\n", texObj->Name);
584 printf("level = %d\n", att->TextureLevel);
585 att->Complete = GL_FALSE;
588 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
589 att_incomplete("bad z offset");
590 att->Complete = GL_FALSE;
594 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
596 if (format == GL_COLOR) {
597 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
598 att_incomplete("bad format");
599 att->Complete = GL_FALSE;
602 if (_mesa_is_format_compressed(texImage->TexFormat)) {
603 att_incomplete("compressed internalformat");
604 att->Complete = GL_FALSE;
608 else if (format == GL_DEPTH) {
609 if (baseFormat == GL_DEPTH_COMPONENT) {
612 else if (ctx->Extensions.EXT_packed_depth_stencil &&
613 ctx->Extensions.ARB_depth_texture &&
614 baseFormat == GL_DEPTH_STENCIL_EXT) {
618 att->Complete = GL_FALSE;
619 att_incomplete("bad depth format");
624 ASSERT(format == GL_STENCIL);
625 if (ctx->Extensions.EXT_packed_depth_stencil &&
626 ctx->Extensions.ARB_depth_texture &&
627 baseFormat == GL_DEPTH_STENCIL_EXT) {
631 /* no such thing as stencil-only textures */
632 att_incomplete("illegal stencil texture");
633 att->Complete = GL_FALSE;
638 else if (att->Type == GL_RENDERBUFFER_EXT) {
639 const GLenum baseFormat =
640 _mesa_get_format_base_format(att->Renderbuffer->Format);
642 ASSERT(att->Renderbuffer);
643 if (!att->Renderbuffer->InternalFormat ||
644 att->Renderbuffer->Width < 1 ||
645 att->Renderbuffer->Height < 1) {
646 att_incomplete("0x0 renderbuffer");
647 att->Complete = GL_FALSE;
650 if (format == GL_COLOR) {
651 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
652 att_incomplete("bad renderbuffer color format");
653 att->Complete = GL_FALSE;
657 else if (format == GL_DEPTH) {
658 if (baseFormat == GL_DEPTH_COMPONENT) {
661 else if (ctx->Extensions.EXT_packed_depth_stencil &&
662 baseFormat == GL_DEPTH_STENCIL_EXT) {
666 att_incomplete("bad renderbuffer depth format");
667 att->Complete = GL_FALSE;
672 assert(format == GL_STENCIL);
673 if (baseFormat == GL_STENCIL_INDEX) {
676 else if (ctx->Extensions.EXT_packed_depth_stencil &&
677 baseFormat == GL_DEPTH_STENCIL_EXT) {
681 att->Complete = GL_FALSE;
682 att_incomplete("bad renderbuffer stencil format");
688 ASSERT(att->Type == GL_NONE);
696 * Test if the given framebuffer object is complete and update its
697 * Status field with the results.
698 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
699 * driver to make hardware-specific validation/completeness checks.
700 * Also update the framebuffer's Width and Height fields if the
701 * framebuffer is complete.
704 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
705 struct gl_framebuffer *fb)
708 GLenum intFormat = GL_NONE; /* color buffers' internal format */
709 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
710 GLint numSamples = -1;
714 assert(is_user_fbo(fb));
720 /* Start at -2 to more easily loop over all attachment points.
725 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
726 struct gl_renderbuffer_attachment *att;
731 * XXX for ARB_fbo, only check color buffers that are named by
732 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
735 /* check for attachment completeness
738 att = &fb->Attachment[BUFFER_DEPTH];
739 test_attachment_completeness(ctx, GL_DEPTH, att);
740 if (!att->Complete) {
741 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
742 fbo_incomplete("depth attachment incomplete", -1);
747 att = &fb->Attachment[BUFFER_STENCIL];
748 test_attachment_completeness(ctx, GL_STENCIL, att);
749 if (!att->Complete) {
750 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
751 fbo_incomplete("stencil attachment incomplete", -1);
756 att = &fb->Attachment[BUFFER_COLOR0 + i];
757 test_attachment_completeness(ctx, GL_COLOR, att);
758 if (!att->Complete) {
759 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
760 fbo_incomplete("color attachment incomplete", i);
765 /* get width, height, format of the renderbuffer/texture
767 if (att->Type == GL_TEXTURE) {
768 const struct gl_texture_image *texImg =
769 _mesa_get_attachment_teximage(att);
770 minWidth = MIN2(minWidth, texImg->Width);
771 maxWidth = MAX2(maxWidth, texImg->Width);
772 minHeight = MIN2(minHeight, texImg->Height);
773 maxHeight = MAX2(maxHeight, texImg->Height);
774 f = texImg->_BaseFormat;
775 attFormat = texImg->TexFormat;
777 if (!_mesa_is_legal_color_format(ctx, f) &&
778 !is_legal_depth_format(ctx, f)) {
779 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
780 fbo_incomplete("texture attachment incomplete", -1);
784 else if (att->Type == GL_RENDERBUFFER_EXT) {
785 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
786 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
787 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
788 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
789 f = att->Renderbuffer->InternalFormat;
790 attFormat = att->Renderbuffer->Format;
794 assert(att->Type == GL_NONE);
798 if (att->Renderbuffer && numSamples < 0) {
800 numSamples = att->Renderbuffer->NumSamples;
803 /* check if integer color */
804 fb->_IntegerColor = _mesa_is_format_integer_color(attFormat);
806 /* Error-check width, height, format, samples
808 if (numImages == 1) {
809 /* save format, num samples */
815 if (!ctx->Extensions.ARB_framebuffer_object) {
816 /* check that width, height, format are same */
817 if (minWidth != maxWidth || minHeight != maxHeight) {
818 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
819 fbo_incomplete("width or height mismatch", -1);
822 /* check that all color buffers are the same format */
823 if (intFormat != GL_NONE && f != intFormat) {
824 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
825 fbo_incomplete("format mismatch", -1);
829 if (att->Renderbuffer &&
830 att->Renderbuffer->NumSamples != numSamples) {
831 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
832 fbo_incomplete("inconsistant number of samples", i);
839 if (ctx->API == API_OPENGL && !ctx->Extensions.ARB_ES2_compatibility) {
840 /* Check that all DrawBuffers are present */
841 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
842 if (fb->ColorDrawBuffer[j] != GL_NONE) {
843 const struct gl_renderbuffer_attachment *att
844 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
846 if (att->Type == GL_NONE) {
847 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
848 fbo_incomplete("missing drawbuffer", j);
854 /* Check that the ReadBuffer is present */
855 if (fb->ColorReadBuffer != GL_NONE) {
856 const struct gl_renderbuffer_attachment *att
857 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
859 if (att->Type == GL_NONE) {
860 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
861 fbo_incomplete("missing readbuffer", -1);
870 if (numImages == 0) {
871 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
872 fbo_incomplete("no attachments", -1);
876 /* Provisionally set status = COMPLETE ... */
877 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
879 /* ... but the driver may say the FB is incomplete.
880 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
883 if (ctx->Driver.ValidateFramebuffer) {
884 ctx->Driver.ValidateFramebuffer(ctx, fb);
885 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
886 fbo_incomplete("driver marked FBO as incomplete", -1);
890 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
892 * Note that if ARB_framebuffer_object is supported and the attached
893 * renderbuffers/textures are different sizes, the framebuffer
894 * width/height will be set to the smallest width/height.
896 fb->Width = minWidth;
897 fb->Height = minHeight;
899 /* finally, update the visual info for the framebuffer */
900 _mesa_update_framebuffer_visual(ctx, fb);
906 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
908 GET_CURRENT_CONTEXT(ctx);
909 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
911 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
912 if (rb != NULL && rb != &DummyRenderbuffer)
920 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
922 struct gl_renderbuffer *newRb;
923 GET_CURRENT_CONTEXT(ctx);
925 ASSERT_OUTSIDE_BEGIN_END(ctx);
927 if (target != GL_RENDERBUFFER_EXT) {
928 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
932 /* No need to flush here since the render buffer binding has no
933 * effect on rendering state.
937 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
938 if (newRb == &DummyRenderbuffer) {
939 /* ID was reserved, but no real renderbuffer object made yet */
942 else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
943 /* All RB IDs must be Gen'd */
944 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
949 /* create new renderbuffer object */
950 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
952 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
955 ASSERT(newRb->AllocStorage);
956 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
957 newRb->RefCount = 1; /* referenced by hash table */
964 ASSERT(newRb != &DummyRenderbuffer);
966 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
971 * If the given renderbuffer is anywhere attached to the framebuffer, detach
973 * This is used when a renderbuffer object is deleted.
974 * The spec calls for unbinding.
977 detach_renderbuffer(struct gl_context *ctx,
978 struct gl_framebuffer *fb,
979 struct gl_renderbuffer *rb)
982 for (i = 0; i < BUFFER_COUNT; i++) {
983 if (fb->Attachment[i].Renderbuffer == rb) {
984 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
987 invalidate_framebuffer(fb);
992 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
995 GET_CURRENT_CONTEXT(ctx);
997 ASSERT_OUTSIDE_BEGIN_END(ctx);
998 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1000 for (i = 0; i < n; i++) {
1001 if (renderbuffers[i] > 0) {
1002 struct gl_renderbuffer *rb;
1003 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1005 /* check if deleting currently bound renderbuffer object */
1006 if (rb == ctx->CurrentRenderbuffer) {
1008 ASSERT(rb->RefCount >= 2);
1009 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1012 if (is_user_fbo(ctx->DrawBuffer)) {
1013 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1015 if (is_user_fbo(ctx->ReadBuffer)
1016 && ctx->ReadBuffer != ctx->DrawBuffer) {
1017 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1020 /* Remove from hash table immediately, to free the ID.
1021 * But the object will not be freed until it's no longer
1022 * referenced anywhere else.
1024 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1026 if (rb != &DummyRenderbuffer) {
1027 /* no longer referenced by hash table */
1028 _mesa_reference_renderbuffer(&rb, NULL);
1037 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
1039 GET_CURRENT_CONTEXT(ctx);
1043 ASSERT_OUTSIDE_BEGIN_END(ctx);
1046 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
1053 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1055 for (i = 0; i < n; i++) {
1056 GLuint name = first + i;
1057 renderbuffers[i] = name;
1058 /* insert dummy placeholder into hash table */
1059 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1060 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1061 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1067 * Given an internal format token for a render buffer, return the
1068 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1069 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1070 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1072 * This is similar to _mesa_base_tex_format() but the set of valid
1073 * internal formats is different.
1075 * Note that even if a format is determined to be legal here, validation
1076 * of the FBO may fail if the format is not supported by the driver/GPU.
1078 * \param internalFormat as passed to glRenderbufferStorage()
1079 * \return the base internal format, or 0 if internalFormat is illegal
1082 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1085 * Notes: some formats such as alpha, luminance, etc. were added
1086 * with GL_ARB_framebuffer_object.
1088 switch (internalFormat) {
1094 return ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1098 case GL_LUMINANCE12:
1099 case GL_LUMINANCE16:
1100 return ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1101 case GL_LUMINANCE_ALPHA:
1102 case GL_LUMINANCE4_ALPHA4:
1103 case GL_LUMINANCE6_ALPHA2:
1104 case GL_LUMINANCE8_ALPHA8:
1105 case GL_LUMINANCE12_ALPHA4:
1106 case GL_LUMINANCE12_ALPHA12:
1107 case GL_LUMINANCE16_ALPHA16:
1108 return ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1112 case GL_INTENSITY12:
1113 case GL_INTENSITY16:
1114 return ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1133 case GL_SRGB8_ALPHA8_EXT:
1135 case GL_STENCIL_INDEX:
1136 case GL_STENCIL_INDEX1_EXT:
1137 case GL_STENCIL_INDEX4_EXT:
1138 case GL_STENCIL_INDEX8_EXT:
1139 case GL_STENCIL_INDEX16_EXT:
1140 return GL_STENCIL_INDEX;
1141 case GL_DEPTH_COMPONENT:
1142 case GL_DEPTH_COMPONENT16:
1143 case GL_DEPTH_COMPONENT24:
1144 case GL_DEPTH_COMPONENT32:
1145 return GL_DEPTH_COMPONENT;
1146 case GL_DEPTH_STENCIL_EXT:
1147 case GL_DEPTH24_STENCIL8_EXT:
1148 if (ctx->Extensions.EXT_packed_depth_stencil)
1149 return GL_DEPTH_STENCIL_EXT;
1152 case GL_DEPTH_COMPONENT32F:
1153 if (ctx->Extensions.ARB_depth_buffer_float)
1154 return GL_DEPTH_COMPONENT;
1157 case GL_DEPTH32F_STENCIL8:
1158 if (ctx->Extensions.ARB_depth_buffer_float)
1159 return GL_DEPTH_STENCIL;
1165 return ctx->Extensions.ARB_texture_rg ? GL_RED : 0;
1169 return ctx->Extensions.ARB_texture_rg ? GL_RG : 0;
1170 /* signed normalized texture formats */
1174 return ctx->Extensions.EXT_texture_snorm ? GL_RED : 0;
1178 return ctx->Extensions.EXT_texture_snorm ? GL_RG : 0;
1181 case GL_RGB16_SNORM:
1182 return ctx->Extensions.EXT_texture_snorm ? GL_RGB : 0;
1184 case GL_RGBA8_SNORM:
1185 case GL_RGBA16_SNORM:
1186 return ctx->Extensions.EXT_texture_snorm ? GL_RGBA : 0;
1187 case GL_ALPHA_SNORM:
1188 case GL_ALPHA8_SNORM:
1189 case GL_ALPHA16_SNORM:
1190 return ctx->Extensions.EXT_texture_snorm &&
1191 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1192 case GL_LUMINANCE_SNORM:
1193 case GL_LUMINANCE8_SNORM:
1194 case GL_LUMINANCE16_SNORM:
1195 return ctx->Extensions.EXT_texture_snorm &&
1196 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1197 case GL_LUMINANCE_ALPHA_SNORM:
1198 case GL_LUMINANCE8_ALPHA8_SNORM:
1199 case GL_LUMINANCE16_ALPHA16_SNORM:
1200 return ctx->Extensions.EXT_texture_snorm &&
1201 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1202 case GL_INTENSITY_SNORM:
1203 case GL_INTENSITY8_SNORM:
1204 case GL_INTENSITY16_SNORM:
1205 return ctx->Extensions.EXT_texture_snorm &&
1206 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1209 return ctx->Extensions.ARB_texture_rg &&
1210 ctx->Extensions.ARB_texture_float ? GL_RED : 0;
1213 return ctx->Extensions.ARB_texture_rg &&
1214 ctx->Extensions.ARB_texture_float ? GL_RG : 0;
1217 return ctx->Extensions.ARB_texture_float ? GL_RGB : 0;
1220 return ctx->Extensions.ARB_texture_float ? GL_RGBA : 0;
1221 case GL_ALPHA16F_ARB:
1222 case GL_ALPHA32F_ARB:
1223 return ctx->Extensions.ARB_texture_float &&
1224 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1225 case GL_LUMINANCE16F_ARB:
1226 case GL_LUMINANCE32F_ARB:
1227 return ctx->Extensions.ARB_texture_float &&
1228 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1229 case GL_LUMINANCE_ALPHA16F_ARB:
1230 case GL_LUMINANCE_ALPHA32F_ARB:
1231 return ctx->Extensions.ARB_texture_float &&
1232 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1233 case GL_INTENSITY16F_ARB:
1234 case GL_INTENSITY32F_ARB:
1235 return ctx->Extensions.ARB_texture_float &&
1236 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1238 return ctx->Extensions.EXT_texture_shared_exponent ? GL_RGB : 0;
1239 case GL_R11F_G11F_B10F:
1240 return ctx->Extensions.EXT_packed_float ? GL_RGB : 0;
1242 case GL_RGBA8UI_EXT:
1243 case GL_RGBA16UI_EXT:
1244 case GL_RGBA32UI_EXT:
1246 case GL_RGBA16I_EXT:
1247 case GL_RGBA32I_EXT:
1248 return ctx->Extensions.EXT_texture_integer ? GL_RGBA : 0;
1251 case GL_RGB16UI_EXT:
1252 case GL_RGB32UI_EXT:
1256 return ctx->Extensions.EXT_texture_integer ? GL_RGB : 0;
1264 return ctx->Extensions.ARB_texture_rg &&
1265 ctx->Extensions.EXT_texture_integer ? GL_RED : 0;
1273 return ctx->Extensions.ARB_texture_rg &&
1274 ctx->Extensions.EXT_texture_integer ? GL_RG : 0;
1276 case GL_INTENSITY8I_EXT:
1277 case GL_INTENSITY8UI_EXT:
1278 case GL_INTENSITY16I_EXT:
1279 case GL_INTENSITY16UI_EXT:
1280 case GL_INTENSITY32I_EXT:
1281 case GL_INTENSITY32UI_EXT:
1282 return ctx->Extensions.EXT_texture_integer &&
1283 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1285 case GL_LUMINANCE8I_EXT:
1286 case GL_LUMINANCE8UI_EXT:
1287 case GL_LUMINANCE16I_EXT:
1288 case GL_LUMINANCE16UI_EXT:
1289 case GL_LUMINANCE32I_EXT:
1290 case GL_LUMINANCE32UI_EXT:
1291 return ctx->Extensions.EXT_texture_integer &&
1292 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1294 case GL_LUMINANCE_ALPHA8I_EXT:
1295 case GL_LUMINANCE_ALPHA8UI_EXT:
1296 case GL_LUMINANCE_ALPHA16I_EXT:
1297 case GL_LUMINANCE_ALPHA16UI_EXT:
1298 case GL_LUMINANCE_ALPHA32I_EXT:
1299 case GL_LUMINANCE_ALPHA32UI_EXT:
1300 return ctx->Extensions.EXT_texture_integer &&
1301 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1304 return ctx->Extensions.ARB_texture_rgb10_a2ui ? GL_RGBA : 0;
1312 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1315 invalidate_rb(GLuint key, void *data, void *userData)
1317 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1318 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1320 /* If this is a user-created FBO */
1321 if (is_user_fbo(fb)) {
1323 for (i = 0; i < BUFFER_COUNT; i++) {
1324 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1325 if (att->Type == GL_RENDERBUFFER &&
1326 att->Renderbuffer == rb) {
1327 /* Mark fb status as indeterminate to force re-validation */
1336 /** sentinal value, see below */
1337 #define NO_SAMPLES 1000
1341 * Helper function used by _mesa_RenderbufferStorageEXT() and
1342 * _mesa_RenderbufferStorageMultisample().
1343 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
1346 renderbuffer_storage(GLenum target, GLenum internalFormat,
1347 GLsizei width, GLsizei height, GLsizei samples)
1349 const char *func = samples == NO_SAMPLES ?
1350 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
1351 struct gl_renderbuffer *rb;
1353 GET_CURRENT_CONTEXT(ctx);
1355 ASSERT_OUTSIDE_BEGIN_END(ctx);
1357 if (target != GL_RENDERBUFFER_EXT) {
1358 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1362 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1363 if (baseFormat == 0) {
1364 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
1368 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1369 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1373 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1374 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1378 if (samples == NO_SAMPLES) {
1379 /* NumSamples == 0 indicates non-multisampling */
1382 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1383 /* note: driver may choose to use more samples than what's requested */
1384 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1388 rb = ctx->CurrentRenderbuffer;
1390 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1394 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1396 if (rb->InternalFormat == internalFormat &&
1397 rb->Width == (GLuint) width &&
1398 rb->Height == (GLuint) height) {
1399 /* no change in allocation needed */
1403 /* These MUST get set by the AllocStorage func */
1404 rb->Format = MESA_FORMAT_NONE;
1405 rb->NumSamples = samples;
1407 /* Now allocate the storage */
1408 ASSERT(rb->AllocStorage);
1409 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1410 /* No error - check/set fields now */
1411 assert(rb->Format != MESA_FORMAT_NONE);
1412 assert(rb->Width == (GLuint) width);
1413 assert(rb->Height == (GLuint) height);
1414 rb->InternalFormat = internalFormat;
1415 rb->_BaseFormat = baseFormat;
1416 assert(rb->_BaseFormat != 0);
1419 /* Probably ran out of memory - clear the fields */
1422 rb->Format = MESA_FORMAT_NONE;
1423 rb->InternalFormat = GL_NONE;
1424 rb->_BaseFormat = GL_NONE;
1428 /* Invalidate the framebuffers the renderbuffer is attached in. */
1429 if (rb->AttachedAnytime) {
1430 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1435 #if FEATURE_OES_EGL_image
1437 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1439 struct gl_renderbuffer *rb;
1440 GET_CURRENT_CONTEXT(ctx);
1441 ASSERT_OUTSIDE_BEGIN_END(ctx);
1443 if (!ctx->Extensions.OES_EGL_image) {
1444 _mesa_error(ctx, GL_INVALID_OPERATION,
1445 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1449 if (target != GL_RENDERBUFFER) {
1450 _mesa_error(ctx, GL_INVALID_ENUM,
1451 "EGLImageTargetRenderbufferStorageOES");
1455 rb = ctx->CurrentRenderbuffer;
1457 _mesa_error(ctx, GL_INVALID_OPERATION,
1458 "EGLImageTargetRenderbufferStorageOES");
1462 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1464 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1470 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1471 * _mesa_GetFramebufferAttachmentParameterivEXT()
1472 * We have to be careful to respect the base format. For example, if a
1473 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1474 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1475 * we need to return zero.
1478 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1480 if (_mesa_base_format_has_channel(baseFormat, pname))
1481 return _mesa_get_format_bits(format, pname);
1489 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1490 GLsizei width, GLsizei height)
1492 /* GL_ARB_fbo says calling this function is equivalent to calling
1493 * glRenderbufferStorageMultisample() with samples=0. We pass in
1494 * a token value here just for error reporting purposes.
1496 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1501 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1502 GLenum internalFormat,
1503 GLsizei width, GLsizei height)
1505 renderbuffer_storage(target, internalFormat, width, height, samples);
1510 * OpenGL ES version of glRenderBufferStorage.
1513 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1514 GLsizei width, GLsizei height)
1516 switch (internalFormat) {
1518 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1519 /* choose a closest format */
1520 internalFormat = GL_RGB5;
1526 renderbuffer_storage(target, internalFormat, width, height, 0);
1531 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1533 struct gl_renderbuffer *rb;
1534 GET_CURRENT_CONTEXT(ctx);
1536 ASSERT_OUTSIDE_BEGIN_END(ctx);
1538 if (target != GL_RENDERBUFFER_EXT) {
1539 _mesa_error(ctx, GL_INVALID_ENUM,
1540 "glGetRenderbufferParameterivEXT(target)");
1544 rb = ctx->CurrentRenderbuffer;
1546 _mesa_error(ctx, GL_INVALID_OPERATION,
1547 "glGetRenderbufferParameterivEXT");
1551 /* No need to flush here since we're just quering state which is
1552 * not effected by rendering.
1556 case GL_RENDERBUFFER_WIDTH_EXT:
1557 *params = rb->Width;
1559 case GL_RENDERBUFFER_HEIGHT_EXT:
1560 *params = rb->Height;
1562 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1563 *params = rb->InternalFormat;
1565 case GL_RENDERBUFFER_RED_SIZE_EXT:
1566 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1567 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1568 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1569 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1570 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1571 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1573 case GL_RENDERBUFFER_SAMPLES:
1574 if (ctx->Extensions.ARB_framebuffer_object) {
1575 *params = rb->NumSamples;
1580 _mesa_error(ctx, GL_INVALID_ENUM,
1581 "glGetRenderbufferParameterivEXT(target)");
1587 GLboolean GLAPIENTRY
1588 _mesa_IsFramebufferEXT(GLuint framebuffer)
1590 GET_CURRENT_CONTEXT(ctx);
1591 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1593 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1594 if (rb != NULL && rb != &DummyFramebuffer)
1602 * Check if any of the attachments of the given framebuffer are textures
1603 * (render to texture). Call ctx->Driver.RenderTexture() for such
1607 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1610 ASSERT(ctx->Driver.RenderTexture);
1612 if (is_winsys_fbo(fb))
1613 return; /* can't render to texture with winsys framebuffers */
1615 for (i = 0; i < BUFFER_COUNT; i++) {
1616 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1617 if (att->Texture && _mesa_get_attachment_teximage(att)) {
1618 ctx->Driver.RenderTexture(ctx, fb, att);
1625 * Examine all the framebuffer's attachments to see if any are textures.
1626 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1627 * notify the device driver that the texture image may have changed.
1630 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1632 if (is_winsys_fbo(fb))
1633 return; /* can't render to texture with winsys framebuffers */
1635 if (ctx->Driver.FinishRenderTexture) {
1637 for (i = 0; i < BUFFER_COUNT; i++) {
1638 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1639 if (att->Texture && att->Renderbuffer) {
1640 ctx->Driver.FinishRenderTexture(ctx, att);
1648 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1650 struct gl_framebuffer *newDrawFb, *newReadFb;
1651 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1652 GLboolean bindReadBuf, bindDrawBuf;
1653 GET_CURRENT_CONTEXT(ctx);
1656 if (ctx->Extensions.ARB_framebuffer_object) {
1657 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1658 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1662 ASSERT_OUTSIDE_BEGIN_END(ctx);
1664 if (!ctx->Extensions.EXT_framebuffer_object) {
1665 _mesa_error(ctx, GL_INVALID_OPERATION,
1666 "glBindFramebufferEXT(unsupported)");
1671 #if FEATURE_EXT_framebuffer_blit
1672 case GL_DRAW_FRAMEBUFFER_EXT:
1673 if (!ctx->Extensions.EXT_framebuffer_blit) {
1674 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1677 bindDrawBuf = GL_TRUE;
1678 bindReadBuf = GL_FALSE;
1680 case GL_READ_FRAMEBUFFER_EXT:
1681 if (!ctx->Extensions.EXT_framebuffer_blit) {
1682 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1685 bindDrawBuf = GL_FALSE;
1686 bindReadBuf = GL_TRUE;
1689 case GL_FRAMEBUFFER_EXT:
1690 bindDrawBuf = GL_TRUE;
1691 bindReadBuf = GL_TRUE;
1694 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1699 /* Binding a user-created framebuffer object */
1700 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1701 if (newDrawFb == &DummyFramebuffer) {
1702 /* ID was reserved, but no real framebuffer object made yet */
1705 else if (!newDrawFb && ctx->Extensions.ARB_framebuffer_object) {
1706 /* All FBO IDs must be Gen'd */
1707 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1712 /* create new framebuffer object */
1713 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1715 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1718 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1720 newReadFb = newDrawFb;
1723 /* Binding the window system framebuffer (which was originally set
1724 * with MakeCurrent).
1726 newDrawFb = ctx->WinSysDrawBuffer;
1727 newReadFb = ctx->WinSysReadBuffer;
1731 ASSERT(newDrawFb != &DummyFramebuffer);
1733 /* save pointers to current/old framebuffers */
1734 oldDrawFb = ctx->DrawBuffer;
1735 oldReadFb = ctx->ReadBuffer;
1737 /* check if really changing bindings */
1738 if (oldDrawFb == newDrawFb)
1739 bindDrawBuf = GL_FALSE;
1740 if (oldReadFb == newReadFb)
1741 bindReadBuf = GL_FALSE;
1744 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1746 * We also check if we're beginning and/or ending render-to-texture.
1747 * When a framebuffer with texture attachments is unbound, call
1748 * ctx->Driver.FinishRenderTexture().
1749 * When a framebuffer with texture attachments is bound, call
1750 * ctx->Driver.RenderTexture().
1752 * Note that if the ReadBuffer has texture attachments we don't consider
1753 * that a render-to-texture case.
1756 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1758 /* check if old readbuffer was render-to-texture */
1759 check_end_texture_render(ctx, oldReadFb);
1761 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1765 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1767 /* check if old read/draw buffers were render-to-texture */
1769 check_end_texture_render(ctx, oldReadFb);
1771 if (oldDrawFb != oldReadFb)
1772 check_end_texture_render(ctx, oldDrawFb);
1774 /* check if newly bound framebuffer has any texture attachments */
1775 check_begin_texture_render(ctx, newDrawFb);
1777 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1780 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1781 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1787 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1790 GET_CURRENT_CONTEXT(ctx);
1792 ASSERT_OUTSIDE_BEGIN_END(ctx);
1793 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1795 for (i = 0; i < n; i++) {
1796 if (framebuffers[i] > 0) {
1797 struct gl_framebuffer *fb;
1798 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1800 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1802 /* check if deleting currently bound framebuffer object */
1803 if (ctx->Extensions.EXT_framebuffer_blit) {
1804 /* separate draw/read binding points */
1805 if (fb == ctx->DrawBuffer) {
1807 ASSERT(fb->RefCount >= 2);
1808 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1810 if (fb == ctx->ReadBuffer) {
1812 ASSERT(fb->RefCount >= 2);
1813 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1817 /* only one binding point for read/draw buffers */
1818 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1820 ASSERT(fb->RefCount >= 2);
1821 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1825 /* remove from hash table immediately, to free the ID */
1826 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1828 if (fb != &DummyFramebuffer) {
1829 /* But the object will not be freed until it's no longer
1830 * bound in any context.
1832 _mesa_reference_framebuffer(&fb, NULL);
1841 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1843 GET_CURRENT_CONTEXT(ctx);
1847 ASSERT_OUTSIDE_BEGIN_END(ctx);
1850 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1857 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1859 for (i = 0; i < n; i++) {
1860 GLuint name = first + i;
1861 framebuffers[i] = name;
1862 /* insert dummy placeholder into hash table */
1863 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1864 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1865 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1872 _mesa_CheckFramebufferStatusEXT(GLenum target)
1874 struct gl_framebuffer *buffer;
1875 GET_CURRENT_CONTEXT(ctx);
1877 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1879 buffer = get_framebuffer_target(ctx, target);
1881 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1885 if (is_winsys_fbo(buffer)) {
1886 /* The window system / default framebuffer is always complete */
1887 return GL_FRAMEBUFFER_COMPLETE_EXT;
1890 /* No need to flush here */
1892 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1893 _mesa_test_framebuffer_completeness(ctx, buffer);
1896 return buffer->_Status;
1901 * Replicate the src attachment point. Used by framebuffer_texture() when
1902 * the same texture is attached at GL_DEPTH_ATTACHMENT and
1903 * GL_STENCIL_ATTACHMENT.
1906 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
1907 gl_buffer_index dst,
1908 gl_buffer_index src)
1910 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
1911 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
1913 assert(src_att->Texture != NULL);
1914 assert(src_att->Renderbuffer != NULL);
1916 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
1917 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
1918 dst_att->Type = src_att->Type;
1919 dst_att->Complete = src_att->Complete;
1920 dst_att->TextureLevel = src_att->TextureLevel;
1921 dst_att->Zoffset = src_att->Zoffset;
1926 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1929 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target,
1930 GLenum attachment, GLenum textarget, GLuint texture,
1931 GLint level, GLint zoffset)
1933 struct gl_renderbuffer_attachment *att;
1934 struct gl_texture_object *texObj = NULL;
1935 struct gl_framebuffer *fb;
1937 ASSERT_OUTSIDE_BEGIN_END(ctx);
1939 fb = get_framebuffer_target(ctx, target);
1941 _mesa_error(ctx, GL_INVALID_ENUM,
1942 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1946 /* check framebuffer binding */
1947 if (is_winsys_fbo(fb)) {
1948 _mesa_error(ctx, GL_INVALID_OPERATION,
1949 "glFramebufferTexture%sEXT", caller);
1953 /* The textarget, level, and zoffset parameters are only validated if
1954 * texture is non-zero.
1957 GLboolean err = GL_TRUE;
1959 texObj = _mesa_lookup_texture(ctx, texture);
1960 if (texObj != NULL) {
1961 if (textarget == 0) {
1962 /* XXX what's the purpose of this? */
1963 err = (texObj->Target != GL_TEXTURE_3D) &&
1964 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1965 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1968 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1969 ? !_mesa_is_cube_face(textarget)
1970 : (texObj->Target != textarget);
1974 /* can't render to a non-existant texture */
1975 _mesa_error(ctx, GL_INVALID_OPERATION,
1976 "glFramebufferTexture%sEXT(non existant texture)",
1982 _mesa_error(ctx, GL_INVALID_OPERATION,
1983 "glFramebufferTexture%sEXT(texture target mismatch)",
1988 if (texObj->Target == GL_TEXTURE_3D) {
1989 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1990 if (zoffset < 0 || zoffset >= maxSize) {
1991 _mesa_error(ctx, GL_INVALID_VALUE,
1992 "glFramebufferTexture%sEXT(zoffset)", caller);
1996 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1997 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1998 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1999 _mesa_error(ctx, GL_INVALID_VALUE,
2000 "glFramebufferTexture%sEXT(layer)", caller);
2006 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
2007 _mesa_error(ctx, GL_INVALID_VALUE,
2008 "glFramebufferTexture%sEXT(level)", caller);
2013 att = _mesa_get_attachment(ctx, fb, attachment);
2015 _mesa_error(ctx, GL_INVALID_ENUM,
2016 "glFramebufferTexture%sEXT(attachment)", caller);
2020 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2022 _glthread_LOCK_MUTEX(fb->Mutex);
2024 if (attachment == GL_DEPTH_ATTACHMENT &&
2025 texObj == fb->Attachment[BUFFER_STENCIL].Texture) {
2026 /* The texture object is already attached to the stencil attachment
2027 * point. Don't create a new renderbuffer; just reuse the stencil
2028 * attachment's. This is required to prevent a GL error in
2029 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2031 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2033 } else if (attachment == GL_STENCIL_ATTACHMENT &&
2034 texObj == fb->Attachment[BUFFER_DEPTH].Texture) {
2035 /* As above, but with depth and stencil juxtasposed. */
2036 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2039 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
2041 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2042 /* Above we created a new renderbuffer and attached it to the
2043 * depth attachment point. Now attach it to the stencil attachment
2046 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2047 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2052 /* Set the render-to-texture flag. We'll check this flag in
2053 * glTexImage() and friends to determine if we need to revalidate
2054 * any FBOs that might be rendering into this texture.
2055 * This flag never gets cleared since it's non-trivial to determine
2056 * when all FBOs might be done rendering to this texture. That's OK
2057 * though since it's uncommon to render to a texture then repeatedly
2058 * call glTexImage() to change images in the texture.
2060 texObj->_RenderToTexture = GL_TRUE;
2063 _mesa_remove_attachment(ctx, att);
2064 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2065 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2066 _mesa_remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2070 invalidate_framebuffer(fb);
2072 _glthread_UNLOCK_MUTEX(fb->Mutex);
2078 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
2079 GLenum textarget, GLuint texture, GLint level)
2081 GET_CURRENT_CONTEXT(ctx);
2086 switch (textarget) {
2090 case GL_TEXTURE_1D_ARRAY:
2091 error = !ctx->Extensions.EXT_texture_array;
2098 _mesa_error(ctx, GL_INVALID_OPERATION,
2099 "glFramebufferTexture1DEXT(textarget=%s)",
2100 _mesa_lookup_enum_by_nr(textarget));
2105 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2111 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
2112 GLenum textarget, GLuint texture, GLint level)
2114 GET_CURRENT_CONTEXT(ctx);
2119 switch (textarget) {
2123 case GL_TEXTURE_RECTANGLE:
2124 error = !ctx->Extensions.NV_texture_rectangle;
2126 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2127 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2128 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2129 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2130 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2131 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2132 error = !ctx->Extensions.ARB_texture_cube_map;
2134 case GL_TEXTURE_2D_ARRAY:
2135 error = !ctx->Extensions.EXT_texture_array;
2142 _mesa_error(ctx, GL_INVALID_OPERATION,
2143 "glFramebufferTexture2DEXT(textarget=%s)",
2144 _mesa_lookup_enum_by_nr(textarget));
2149 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2155 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
2156 GLenum textarget, GLuint texture,
2157 GLint level, GLint zoffset)
2159 GET_CURRENT_CONTEXT(ctx);
2161 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2162 _mesa_error(ctx, GL_INVALID_OPERATION,
2163 "glFramebufferTexture3DEXT(textarget)");
2167 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2173 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
2174 GLuint texture, GLint level, GLint layer)
2176 GET_CURRENT_CONTEXT(ctx);
2178 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2184 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
2185 GLenum renderbufferTarget,
2186 GLuint renderbuffer)
2188 struct gl_renderbuffer_attachment *att;
2189 struct gl_framebuffer *fb;
2190 struct gl_renderbuffer *rb;
2191 GET_CURRENT_CONTEXT(ctx);
2193 ASSERT_OUTSIDE_BEGIN_END(ctx);
2195 fb = get_framebuffer_target(ctx, target);
2197 _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2201 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2202 _mesa_error(ctx, GL_INVALID_ENUM,
2203 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2207 if (is_winsys_fbo(fb)) {
2208 /* Can't attach new renderbuffers to a window system framebuffer */
2209 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2213 att = _mesa_get_attachment(ctx, fb, attachment);
2215 _mesa_error(ctx, GL_INVALID_ENUM,
2216 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2217 _mesa_lookup_enum_by_nr(attachment));
2222 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2224 _mesa_error(ctx, GL_INVALID_OPERATION,
2225 "glFramebufferRenderbufferEXT(non-existant"
2226 " renderbuffer %u)", renderbuffer);
2229 else if (rb == &DummyRenderbuffer) {
2230 /* This is what NVIDIA does */
2231 _mesa_error(ctx, GL_INVALID_VALUE,
2232 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2238 /* remove renderbuffer attachment */
2242 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2243 rb && rb->Format != MESA_FORMAT_NONE) {
2244 /* make sure the renderbuffer is a depth/stencil format */
2245 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2246 if (baseFormat != GL_DEPTH_STENCIL) {
2247 _mesa_error(ctx, GL_INVALID_OPERATION,
2248 "glFramebufferRenderbufferEXT(renderbuffer"
2249 " is not DEPTH_STENCIL format)");
2255 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2257 assert(ctx->Driver.FramebufferRenderbuffer);
2258 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2260 /* Some subsequent GL commands may depend on the framebuffer's visual
2261 * after the binding is updated. Update visual info now.
2263 _mesa_update_framebuffer_visual(ctx, fb);
2268 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
2269 GLenum pname, GLint *params)
2271 const struct gl_renderbuffer_attachment *att;
2272 struct gl_framebuffer *buffer;
2274 GET_CURRENT_CONTEXT(ctx);
2276 ASSERT_OUTSIDE_BEGIN_END(ctx);
2278 /* The error differs in GL andd GLES. */
2279 err = ctx->API == API_OPENGL ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2281 buffer = get_framebuffer_target(ctx, target);
2283 _mesa_error(ctx, GL_INVALID_ENUM,
2284 "glGetFramebufferAttachmentParameterivEXT(target)");
2288 if (is_winsys_fbo(buffer)) {
2289 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2292 * "If the framebuffer currently bound to target is zero, then
2293 * INVALID_OPERATION is generated."
2295 * The EXT_framebuffer_object spec has the same wording, and the
2296 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2299 if (ctx->API != API_OPENGL || !ctx->Extensions.ARB_framebuffer_object) {
2300 _mesa_error(ctx, GL_INVALID_OPERATION,
2301 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2304 /* the default / window-system FBO */
2305 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2308 /* user-created framebuffer FBO */
2309 att = _mesa_get_attachment(ctx, buffer, attachment);
2313 _mesa_error(ctx, GL_INVALID_ENUM,
2314 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2318 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2319 /* the depth and stencil attachments must point to the same buffer */
2320 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2321 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2322 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2323 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2324 _mesa_error(ctx, GL_INVALID_OPERATION,
2325 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2326 " attachments differ)");
2331 /* No need to flush here */
2334 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2335 *params = is_winsys_fbo(buffer) ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2337 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2338 if (att->Type == GL_RENDERBUFFER_EXT) {
2339 *params = att->Renderbuffer->Name;
2341 else if (att->Type == GL_TEXTURE) {
2342 *params = att->Texture->Name;
2345 assert(att->Type == GL_NONE);
2346 if (ctx->API == API_OPENGL) {
2349 _mesa_error(ctx, GL_INVALID_ENUM,
2350 "glGetFramebufferAttachmentParameterivEXT(pname)");
2354 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2355 if (att->Type == GL_TEXTURE) {
2356 *params = att->TextureLevel;
2358 else if (att->Type == GL_NONE) {
2359 _mesa_error(ctx, err,
2360 "glGetFramebufferAttachmentParameterivEXT(pname)");
2363 _mesa_error(ctx, GL_INVALID_ENUM,
2364 "glGetFramebufferAttachmentParameterivEXT(pname)");
2367 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2368 if (att->Type == GL_TEXTURE) {
2369 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2370 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2376 else if (att->Type == GL_NONE) {
2377 _mesa_error(ctx, err,
2378 "glGetFramebufferAttachmentParameterivEXT(pname)");
2381 _mesa_error(ctx, GL_INVALID_ENUM,
2382 "glGetFramebufferAttachmentParameterivEXT(pname)");
2385 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2386 if (att->Type == GL_TEXTURE) {
2387 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2388 *params = att->Zoffset;
2394 else if (att->Type == GL_NONE) {
2395 _mesa_error(ctx, err,
2396 "glGetFramebufferAttachmentParameterivEXT(pname)");
2399 _mesa_error(ctx, GL_INVALID_ENUM,
2400 "glGetFramebufferAttachmentParameterivEXT(pname)");
2403 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2404 if (!ctx->Extensions.ARB_framebuffer_object) {
2405 _mesa_error(ctx, GL_INVALID_ENUM,
2406 "glGetFramebufferAttachmentParameterivEXT(pname)");
2408 else if (att->Type == GL_NONE) {
2409 _mesa_error(ctx, err,
2410 "glGetFramebufferAttachmentParameterivEXT(pname)");
2413 if (ctx->Extensions.EXT_framebuffer_sRGB && ctx->Const.sRGBCapable) {
2414 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2417 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2418 * if the sRGB conversion is unsupported. */
2419 *params = GL_LINEAR;
2423 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2424 if (!ctx->Extensions.ARB_framebuffer_object) {
2425 _mesa_error(ctx, GL_INVALID_ENUM,
2426 "glGetFramebufferAttachmentParameterivEXT(pname)");
2429 else if (att->Type == GL_NONE) {
2430 _mesa_error(ctx, err,
2431 "glGetFramebufferAttachmentParameterivEXT(pname)");
2434 gl_format format = att->Renderbuffer->Format;
2435 if (format == MESA_FORMAT_S8) {
2439 else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2440 /* depends on the attachment parameter */
2441 if (attachment == GL_STENCIL_ATTACHMENT) {
2449 *params = _mesa_get_format_datatype(format);
2453 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2454 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2455 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2456 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2457 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2458 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2459 if (!ctx->Extensions.ARB_framebuffer_object) {
2460 _mesa_error(ctx, GL_INVALID_ENUM,
2461 "glGetFramebufferAttachmentParameterivEXT(pname)");
2463 else if (att->Type == GL_NONE) {
2464 _mesa_error(ctx, err,
2465 "glGetFramebufferAttachmentParameterivEXT(pname)");
2467 else if (att->Texture) {
2468 const struct gl_texture_image *texImage =
2469 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2472 *params = get_component_bits(pname, texImage->_BaseFormat,
2473 texImage->TexFormat);
2479 else if (att->Renderbuffer) {
2480 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2481 att->Renderbuffer->Format);
2484 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2485 " invalid FBO attachment structure");
2489 _mesa_error(ctx, GL_INVALID_ENUM,
2490 "glGetFramebufferAttachmentParameterivEXT(pname)");
2497 _mesa_GenerateMipmapEXT(GLenum target)
2499 struct gl_texture_image *srcImage;
2500 struct gl_texture_object *texObj;
2503 GET_CURRENT_CONTEXT(ctx);
2505 ASSERT_OUTSIDE_BEGIN_END(ctx);
2506 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2514 case GL_TEXTURE_CUBE_MAP:
2515 error = !ctx->Extensions.ARB_texture_cube_map;
2517 case GL_TEXTURE_1D_ARRAY:
2518 case GL_TEXTURE_2D_ARRAY:
2519 error = !ctx->Extensions.EXT_texture_array;
2526 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2527 _mesa_lookup_enum_by_nr(target));
2531 texObj = _mesa_get_current_tex_object(ctx, target);
2533 if (texObj->BaseLevel >= texObj->MaxLevel) {
2538 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2539 !_mesa_cube_complete(texObj)) {
2540 _mesa_error(ctx, GL_INVALID_OPERATION,
2541 "glGenerateMipmap(incomplete cube map)");
2545 _mesa_lock_texture(ctx, texObj);
2547 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
2549 _mesa_unlock_texture(ctx, texObj);
2553 if (target == GL_TEXTURE_CUBE_MAP) {
2555 for (face = 0; face < 6; face++)
2556 ctx->Driver.GenerateMipmap(ctx,
2557 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2561 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2563 _mesa_unlock_texture(ctx, texObj);
2567 #if FEATURE_EXT_framebuffer_blit
2569 static const struct gl_renderbuffer_attachment *
2570 find_attachment(const struct gl_framebuffer *fb,
2571 const struct gl_renderbuffer *rb)
2574 for (i = 0; i < Elements(fb->Attachment); i++) {
2575 if (fb->Attachment[i].Renderbuffer == rb)
2576 return &fb->Attachment[i];
2584 * Blit rectangular region, optionally from one framebuffer to another.
2586 * Note, if the src buffer is multisampled and the dest is not, this is
2587 * when the samples must be resolved to a single color.
2590 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2591 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2592 GLbitfield mask, GLenum filter)
2594 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2595 GL_DEPTH_BUFFER_BIT |
2596 GL_STENCIL_BUFFER_BIT);
2597 const struct gl_framebuffer *readFb, *drawFb;
2598 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
2599 GET_CURRENT_CONTEXT(ctx);
2601 ASSERT_OUTSIDE_BEGIN_END(ctx);
2602 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2604 if (MESA_VERBOSE & VERBOSE_API)
2606 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2607 srcX0, srcY0, srcX1, srcY1,
2608 dstX0, dstY0, dstX1, dstY1,
2609 mask, _mesa_lookup_enum_by_nr(filter));
2611 if (ctx->NewState) {
2612 _mesa_update_state(ctx);
2615 readFb = ctx->ReadBuffer;
2616 drawFb = ctx->DrawBuffer;
2618 if (!readFb || !drawFb) {
2619 /* This will normally never happen but someday we may want to
2620 * support MakeCurrent() with no drawables.
2625 /* check for complete framebuffers */
2626 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2627 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2628 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2629 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2633 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2634 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2638 if (mask & ~legalMaskBits) {
2639 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2643 /* depth/stencil must be blitted with nearest filtering */
2644 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2645 && filter != GL_NEAREST) {
2646 _mesa_error(ctx, GL_INVALID_OPERATION,
2647 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2651 /* get color read/draw renderbuffers */
2652 if (mask & GL_COLOR_BUFFER_BIT) {
2653 colorReadRb = readFb->_ColorReadBuffer;
2654 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2656 /* From the EXT_framebuffer_object spec:
2658 * "If a buffer is specified in <mask> and does not exist in both
2659 * the read and draw framebuffers, the corresponding bit is silently
2662 if ((colorReadRb == NULL) || (colorDrawRb == NULL)) {
2663 colorReadRb = colorDrawRb = NULL;
2664 mask &= ~GL_COLOR_BUFFER_BIT;
2668 colorReadRb = colorDrawRb = NULL;
2671 if (mask & GL_STENCIL_BUFFER_BIT) {
2672 struct gl_renderbuffer *readRb =
2673 readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2674 struct gl_renderbuffer *drawRb =
2675 drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2677 /* From the EXT_framebuffer_object spec:
2679 * "If a buffer is specified in <mask> and does not exist in both
2680 * the read and draw framebuffers, the corresponding bit is silently
2683 if ((readRb == NULL) || (drawRb == NULL)) {
2684 mask &= ~GL_STENCIL_BUFFER_BIT;
2686 else if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2687 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2688 _mesa_error(ctx, GL_INVALID_OPERATION,
2689 "glBlitFramebufferEXT(stencil buffer size mismatch)");
2694 if (mask & GL_DEPTH_BUFFER_BIT) {
2695 struct gl_renderbuffer *readRb =
2696 readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2697 struct gl_renderbuffer *drawRb =
2698 drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2700 /* From the EXT_framebuffer_object spec:
2702 * "If a buffer is specified in <mask> and does not exist in both
2703 * the read and draw framebuffers, the corresponding bit is silently
2706 if ((readRb == NULL) || (drawRb == NULL)) {
2707 mask &= ~GL_DEPTH_BUFFER_BIT;
2709 else if (_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2710 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) {
2711 _mesa_error(ctx, GL_INVALID_OPERATION,
2712 "glBlitFramebufferEXT(depth buffer size mismatch)");
2717 if (readFb->Visual.samples > 0 &&
2718 drawFb->Visual.samples > 0 &&
2719 readFb->Visual.samples != drawFb->Visual.samples) {
2720 _mesa_error(ctx, GL_INVALID_OPERATION,
2721 "glBlitFramebufferEXT(mismatched samples");
2725 /* extra checks for multisample copies... */
2726 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2727 /* src and dest region sizes must be the same */
2728 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2729 srcY1 - srcY0 != dstY1 - dstY0) {
2730 _mesa_error(ctx, GL_INVALID_OPERATION,
2731 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2735 /* color formats must match */
2738 colorReadRb->Format != colorDrawRb->Format) {
2739 _mesa_error(ctx, GL_INVALID_OPERATION,
2740 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2745 if (!ctx->Extensions.EXT_framebuffer_blit) {
2746 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2752 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2754 srcX0, srcY0, srcX1, srcY1,
2755 dstX0, dstY0, dstX1, dstY1,
2758 const struct gl_renderbuffer_attachment *att;
2760 att = find_attachment(readFb, colorReadRb);
2761 printf(" Src FBO %u RB %u (%dx%d) ",
2762 readFb->Name, colorReadRb->Name,
2763 colorReadRb->Width, colorReadRb->Height);
2764 if (att && att->Texture) {
2765 printf("Tex %u tgt 0x%x level %u face %u",
2767 att->Texture->Target,
2773 att = find_attachment(drawFb, colorDrawRb);
2774 printf(" Dst FBO %u RB %u (%dx%d) ",
2775 drawFb->Name, colorDrawRb->Name,
2776 colorDrawRb->Width, colorDrawRb->Height);
2777 if (att && att->Texture) {
2778 printf("Tex %u tgt 0x%x level %u face %u",
2780 att->Texture->Target,
2792 ASSERT(ctx->Driver.BlitFramebuffer);
2793 ctx->Driver.BlitFramebuffer(ctx,
2794 srcX0, srcY0, srcX1, srcY1,
2795 dstX0, dstY0, dstX1, dstY1,
2798 #endif /* FEATURE_EXT_framebuffer_blit */
2801 #if FEATURE_ARB_geometry_shader4
2803 _mesa_FramebufferTextureARB(GLenum target, GLenum attachment,
2804 GLuint texture, GLint level)
2806 GET_CURRENT_CONTEXT(ctx);
2807 _mesa_error(ctx, GL_INVALID_OPERATION,
2808 "glFramebufferTextureARB "
2809 "not implemented!");
2814 _mesa_FramebufferTextureFaceARB(GLenum target, GLenum attachment,
2815 GLuint texture, GLint level, GLenum face)
2817 GET_CURRENT_CONTEXT(ctx);
2818 _mesa_error(ctx, GL_INVALID_OPERATION,
2819 "glFramebufferTextureFaceARB "
2820 "not implemented!");
2822 #endif /* FEATURE_ARB_geometry_shader4 */