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 color renderbuffer?
529 is_format_color_renderable(const struct gl_context *ctx, gl_format format, GLenum internalFormat)
531 const GLenum baseFormat =
532 _mesa_get_format_base_format(format);
535 valid = _mesa_is_legal_color_format(ctx, baseFormat);
536 if (!valid || _mesa_is_desktop_gl(ctx)) {
540 /* Reject additional cases for GLES */
541 switch (internalFormat) {
561 if (format == MESA_FORMAT_ARGB2101010 && internalFormat != GL_RGB10_A2) {
570 * Is the given base format a legal format for a depth/stencil renderbuffer?
573 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
575 switch (baseFormat) {
576 case GL_DEPTH_COMPONENT:
577 case GL_DEPTH_STENCIL_EXT:
586 * Test if an attachment point is complete and update its Complete field.
587 * \param format if GL_COLOR, this is a color attachment point,
588 * if GL_DEPTH, this is a depth component attachment point,
589 * if GL_STENCIL, this is a stencil component attachment point.
592 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
593 struct gl_renderbuffer_attachment *att)
595 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
597 /* assume complete */
598 att->Complete = GL_TRUE;
600 /* Look for reasons why the attachment might be incomplete */
601 if (att->Type == GL_TEXTURE) {
602 const struct gl_texture_object *texObj = att->Texture;
603 struct gl_texture_image *texImage;
607 att_incomplete("no texobj");
608 att->Complete = GL_FALSE;
612 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
614 att_incomplete("no teximage");
615 att->Complete = GL_FALSE;
618 if (texImage->Width < 1 || texImage->Height < 1) {
619 att_incomplete("teximage width/height=0");
620 printf("texobj = %u\n", texObj->Name);
621 printf("level = %d\n", att->TextureLevel);
622 att->Complete = GL_FALSE;
625 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
626 att_incomplete("bad z offset");
627 att->Complete = GL_FALSE;
631 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
633 if (format == GL_COLOR) {
634 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
635 att_incomplete("bad format");
636 att->Complete = GL_FALSE;
639 if (_mesa_is_format_compressed(texImage->TexFormat)) {
640 att_incomplete("compressed internalformat");
641 att->Complete = GL_FALSE;
645 else if (format == GL_DEPTH) {
646 if (baseFormat == GL_DEPTH_COMPONENT) {
649 else if (ctx->Extensions.EXT_packed_depth_stencil &&
650 ctx->Extensions.ARB_depth_texture &&
651 baseFormat == GL_DEPTH_STENCIL_EXT) {
655 att->Complete = GL_FALSE;
656 att_incomplete("bad depth format");
661 ASSERT(format == GL_STENCIL);
662 if (ctx->Extensions.EXT_packed_depth_stencil &&
663 ctx->Extensions.ARB_depth_texture &&
664 baseFormat == GL_DEPTH_STENCIL_EXT) {
668 /* no such thing as stencil-only textures */
669 att_incomplete("illegal stencil texture");
670 att->Complete = GL_FALSE;
675 else if (att->Type == GL_RENDERBUFFER_EXT) {
676 const GLenum baseFormat =
677 _mesa_get_format_base_format(att->Renderbuffer->Format);
679 ASSERT(att->Renderbuffer);
680 if (!att->Renderbuffer->InternalFormat ||
681 att->Renderbuffer->Width < 1 ||
682 att->Renderbuffer->Height < 1) {
683 att_incomplete("0x0 renderbuffer");
684 att->Complete = GL_FALSE;
687 if (format == GL_COLOR) {
688 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
689 att_incomplete("bad renderbuffer color format");
690 att->Complete = GL_FALSE;
694 else if (format == GL_DEPTH) {
695 if (baseFormat == GL_DEPTH_COMPONENT) {
698 else if (ctx->Extensions.EXT_packed_depth_stencil &&
699 baseFormat == GL_DEPTH_STENCIL_EXT) {
703 att_incomplete("bad renderbuffer depth format");
704 att->Complete = GL_FALSE;
709 assert(format == GL_STENCIL);
710 if (baseFormat == GL_STENCIL_INDEX) {
713 else if (ctx->Extensions.EXT_packed_depth_stencil &&
714 baseFormat == GL_DEPTH_STENCIL_EXT) {
718 att->Complete = GL_FALSE;
719 att_incomplete("bad renderbuffer stencil format");
725 ASSERT(att->Type == GL_NONE);
733 * Test if the given framebuffer object is complete and update its
734 * Status field with the results.
735 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
736 * driver to make hardware-specific validation/completeness checks.
737 * Also update the framebuffer's Width and Height fields if the
738 * framebuffer is complete.
741 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
742 struct gl_framebuffer *fb)
745 GLenum intFormat = GL_NONE; /* color buffers' internal format */
746 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
747 GLint numSamples = -1;
751 assert(_mesa_is_user_fbo(fb));
753 /* we're changing framebuffer fields here */
754 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
760 /* Start at -2 to more easily loop over all attachment points.
765 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
766 struct gl_renderbuffer_attachment *att;
771 * XXX for ARB_fbo, only check color buffers that are named by
772 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
775 /* check for attachment completeness
778 att = &fb->Attachment[BUFFER_DEPTH];
779 test_attachment_completeness(ctx, GL_DEPTH, att);
780 if (!att->Complete) {
781 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
782 fbo_incomplete("depth attachment incomplete", -1);
787 att = &fb->Attachment[BUFFER_STENCIL];
788 test_attachment_completeness(ctx, GL_STENCIL, att);
789 if (!att->Complete) {
790 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
791 fbo_incomplete("stencil attachment incomplete", -1);
796 att = &fb->Attachment[BUFFER_COLOR0 + i];
797 test_attachment_completeness(ctx, GL_COLOR, att);
798 if (!att->Complete) {
799 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
800 fbo_incomplete("color attachment incomplete", i);
805 /* get width, height, format of the renderbuffer/texture
807 if (att->Type == GL_TEXTURE) {
808 const struct gl_texture_image *texImg =
809 _mesa_get_attachment_teximage(att);
810 minWidth = MIN2(minWidth, texImg->Width);
811 maxWidth = MAX2(maxWidth, texImg->Width);
812 minHeight = MIN2(minHeight, texImg->Height);
813 maxHeight = MAX2(maxHeight, texImg->Height);
814 f = texImg->_BaseFormat;
815 attFormat = texImg->TexFormat;
817 if (!is_format_color_renderable(ctx, attFormat, texImg->InternalFormat) &&
818 !is_legal_depth_format(ctx, f)) {
819 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
820 fbo_incomplete("texture attachment incomplete", -1);
824 else if (att->Type == GL_RENDERBUFFER_EXT) {
825 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
826 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
827 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
828 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
829 f = att->Renderbuffer->InternalFormat;
830 attFormat = att->Renderbuffer->Format;
834 assert(att->Type == GL_NONE);
838 if (att->Renderbuffer && numSamples < 0) {
840 numSamples = att->Renderbuffer->NumSamples;
843 /* check if integer color */
844 fb->_IntegerColor = _mesa_is_format_integer_color(attFormat);
846 /* Error-check width, height, format, samples
848 if (numImages == 1) {
849 /* save format, num samples */
855 if (!ctx->Extensions.ARB_framebuffer_object) {
856 /* check that width, height, format are same */
857 if (minWidth != maxWidth || minHeight != maxHeight) {
858 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
859 fbo_incomplete("width or height mismatch", -1);
862 /* check that all color buffers are the same format */
863 if (intFormat != GL_NONE && f != intFormat) {
864 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
865 fbo_incomplete("format mismatch", -1);
869 if (att->Renderbuffer &&
870 att->Renderbuffer->NumSamples != numSamples) {
871 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
872 fbo_incomplete("inconsistant number of samples", i);
877 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
879 if (att->Type == GL_RENDERBUFFER &&
880 att->Renderbuffer->Format == MESA_FORMAT_NONE) {
881 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
882 fbo_incomplete("unsupported renderbuffer format", i);
887 if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
888 /* Check that all DrawBuffers are present */
889 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
890 if (fb->ColorDrawBuffer[j] != GL_NONE) {
891 const struct gl_renderbuffer_attachment *att
892 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
894 if (att->Type == GL_NONE) {
895 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
896 fbo_incomplete("missing drawbuffer", j);
902 /* Check that the ReadBuffer is present */
903 if (fb->ColorReadBuffer != GL_NONE) {
904 const struct gl_renderbuffer_attachment *att
905 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
907 if (att->Type == GL_NONE) {
908 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
909 fbo_incomplete("missing readbuffer", -1);
915 if (numImages == 0) {
916 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
917 fbo_incomplete("no attachments", -1);
921 /* Provisionally set status = COMPLETE ... */
922 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
924 /* ... but the driver may say the FB is incomplete.
925 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
928 if (ctx->Driver.ValidateFramebuffer) {
929 ctx->Driver.ValidateFramebuffer(ctx, fb);
930 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
931 fbo_incomplete("driver marked FBO as incomplete", -1);
935 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
937 * Note that if ARB_framebuffer_object is supported and the attached
938 * renderbuffers/textures are different sizes, the framebuffer
939 * width/height will be set to the smallest width/height.
941 fb->Width = minWidth;
942 fb->Height = minHeight;
944 /* finally, update the visual info for the framebuffer */
945 _mesa_update_framebuffer_visual(ctx, fb);
951 _mesa_IsRenderbuffer(GLuint renderbuffer)
953 GET_CURRENT_CONTEXT(ctx);
954 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
956 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
957 if (rb != NULL && rb != &DummyRenderbuffer)
965 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
967 struct gl_renderbuffer *newRb;
968 GET_CURRENT_CONTEXT(ctx);
970 if (target != GL_RENDERBUFFER_EXT) {
971 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
975 /* No need to flush here since the render buffer binding has no
976 * effect on rendering state.
980 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
981 if (newRb == &DummyRenderbuffer) {
982 /* ID was reserved, but no real renderbuffer object made yet */
986 && _mesa_is_desktop_gl(ctx)
987 && ctx->Extensions.ARB_framebuffer_object) {
988 /* All RB IDs must be Gen'd */
989 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
994 /* create new renderbuffer object */
995 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
997 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
1000 ASSERT(newRb->AllocStorage);
1001 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
1002 newRb->RefCount = 1; /* referenced by hash table */
1009 ASSERT(newRb != &DummyRenderbuffer);
1011 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
1016 * If the given renderbuffer is anywhere attached to the framebuffer, detach
1018 * This is used when a renderbuffer object is deleted.
1019 * The spec calls for unbinding.
1022 detach_renderbuffer(struct gl_context *ctx,
1023 struct gl_framebuffer *fb,
1024 struct gl_renderbuffer *rb)
1027 for (i = 0; i < BUFFER_COUNT; i++) {
1028 if (fb->Attachment[i].Renderbuffer == rb) {
1029 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
1032 invalidate_framebuffer(fb);
1037 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1040 GET_CURRENT_CONTEXT(ctx);
1042 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1044 for (i = 0; i < n; i++) {
1045 if (renderbuffers[i] > 0) {
1046 struct gl_renderbuffer *rb;
1047 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1049 /* check if deleting currently bound renderbuffer object */
1050 if (rb == ctx->CurrentRenderbuffer) {
1052 ASSERT(rb->RefCount >= 2);
1053 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1056 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1057 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1059 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1060 && ctx->ReadBuffer != ctx->DrawBuffer) {
1061 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1064 /* Remove from hash table immediately, to free the ID.
1065 * But the object will not be freed until it's no longer
1066 * referenced anywhere else.
1068 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1070 if (rb != &DummyRenderbuffer) {
1071 /* no longer referenced by hash table */
1072 _mesa_reference_renderbuffer(&rb, NULL);
1081 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1083 GET_CURRENT_CONTEXT(ctx);
1088 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
1095 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1097 for (i = 0; i < n; i++) {
1098 GLuint name = first + i;
1099 renderbuffers[i] = name;
1100 /* insert dummy placeholder into hash table */
1101 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1102 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1103 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1109 * Given an internal format token for a render buffer, return the
1110 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1111 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1112 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1114 * This is similar to _mesa_base_tex_format() but the set of valid
1115 * internal formats is different.
1117 * Note that even if a format is determined to be legal here, validation
1118 * of the FBO may fail if the format is not supported by the driver/GPU.
1120 * \param internalFormat as passed to glRenderbufferStorage()
1121 * \return the base internal format, or 0 if internalFormat is illegal
1124 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1127 * Notes: some formats such as alpha, luminance, etc. were added
1128 * with GL_ARB_framebuffer_object.
1130 switch (internalFormat) {
1136 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1141 case GL_LUMINANCE12:
1142 case GL_LUMINANCE16:
1143 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1145 case GL_LUMINANCE_ALPHA:
1146 case GL_LUMINANCE4_ALPHA4:
1147 case GL_LUMINANCE6_ALPHA2:
1148 case GL_LUMINANCE8_ALPHA8:
1149 case GL_LUMINANCE12_ALPHA4:
1150 case GL_LUMINANCE12_ALPHA12:
1151 case GL_LUMINANCE16_ALPHA16:
1152 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1153 ? GL_LUMINANCE_ALPHA : 0;
1157 case GL_INTENSITY12:
1158 case GL_INTENSITY16:
1159 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1170 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1172 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1181 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1183 case GL_SRGB8_ALPHA8_EXT:
1184 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1185 case GL_STENCIL_INDEX:
1186 case GL_STENCIL_INDEX1_EXT:
1187 case GL_STENCIL_INDEX4_EXT:
1188 case GL_STENCIL_INDEX16_EXT:
1189 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1190 * OpenGL ES, but Mesa does not currently support them.
1192 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1193 case GL_STENCIL_INDEX8_EXT:
1194 return GL_STENCIL_INDEX;
1195 case GL_DEPTH_COMPONENT:
1196 case GL_DEPTH_COMPONENT32:
1197 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1198 case GL_DEPTH_COMPONENT16:
1199 case GL_DEPTH_COMPONENT24:
1200 return GL_DEPTH_COMPONENT;
1201 case GL_DEPTH_STENCIL_EXT:
1202 return _mesa_is_desktop_gl(ctx)
1203 && ctx->Extensions.EXT_packed_depth_stencil
1204 ? GL_DEPTH_STENCIL_EXT : 0;
1205 case GL_DEPTH24_STENCIL8_EXT:
1206 return ctx->Extensions.EXT_packed_depth_stencil
1207 ? GL_DEPTH_STENCIL_EXT : 0;
1208 case GL_DEPTH_COMPONENT32F:
1209 return ctx->Version >= 30
1210 || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1211 ? GL_DEPTH_COMPONENT : 0;
1212 case GL_DEPTH32F_STENCIL8:
1213 return ctx->Version >= 30
1214 || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1215 ? GL_DEPTH_STENCIL : 0;
1218 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1221 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1225 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1228 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1230 /* signed normalized texture formats */
1234 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1239 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1243 case GL_RGB16_SNORM:
1244 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1247 case GL_RGBA8_SNORM:
1248 case GL_RGBA16_SNORM:
1249 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1251 case GL_ALPHA_SNORM:
1252 case GL_ALPHA8_SNORM:
1253 case GL_ALPHA16_SNORM:
1254 return ctx->API == API_OPENGL_COMPAT &&
1255 ctx->Extensions.EXT_texture_snorm &&
1256 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1259 return ((_mesa_is_desktop_gl(ctx) &&
1260 ctx->Extensions.ARB_texture_rg &&
1261 ctx->Extensions.ARB_texture_float) ||
1262 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1266 return ((_mesa_is_desktop_gl(ctx) &&
1267 ctx->Extensions.ARB_texture_rg &&
1268 ctx->Extensions.ARB_texture_float) ||
1269 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1273 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1277 return ((_mesa_is_desktop_gl(ctx) &&
1278 ctx->Extensions.ARB_texture_float) ||
1279 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1281 case GL_ALPHA16F_ARB:
1282 case GL_ALPHA32F_ARB:
1283 return ctx->API == API_OPENGL_COMPAT &&
1284 ctx->Extensions.ARB_texture_float &&
1285 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1286 case GL_LUMINANCE16F_ARB:
1287 case GL_LUMINANCE32F_ARB:
1288 return ctx->API == API_OPENGL_COMPAT &&
1289 ctx->Extensions.ARB_texture_float &&
1290 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1291 case GL_LUMINANCE_ALPHA16F_ARB:
1292 case GL_LUMINANCE_ALPHA32F_ARB:
1293 return ctx->API == API_OPENGL_COMPAT &&
1294 ctx->Extensions.ARB_texture_float &&
1295 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1296 case GL_INTENSITY16F_ARB:
1297 case GL_INTENSITY32F_ARB:
1298 return ctx->API == API_OPENGL_COMPAT &&
1299 ctx->Extensions.ARB_texture_float &&
1300 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1302 return (_mesa_is_desktop_gl(ctx)
1303 && ctx->Extensions.EXT_texture_shared_exponent)
1305 case GL_R11F_G11F_B10F:
1306 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1307 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1310 case GL_RGBA8UI_EXT:
1311 case GL_RGBA16UI_EXT:
1312 case GL_RGBA32UI_EXT:
1314 case GL_RGBA16I_EXT:
1315 case GL_RGBA32I_EXT:
1316 return ctx->Version >= 30
1317 || (_mesa_is_desktop_gl(ctx) &&
1318 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1321 case GL_RGB16UI_EXT:
1322 case GL_RGB32UI_EXT:
1326 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1334 return ctx->Version >= 30
1335 || (_mesa_is_desktop_gl(ctx) &&
1336 ctx->Extensions.ARB_texture_rg &&
1337 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1345 return ctx->Version >= 30
1346 || (_mesa_is_desktop_gl(ctx) &&
1347 ctx->Extensions.ARB_texture_rg &&
1348 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1350 case GL_INTENSITY8I_EXT:
1351 case GL_INTENSITY8UI_EXT:
1352 case GL_INTENSITY16I_EXT:
1353 case GL_INTENSITY16UI_EXT:
1354 case GL_INTENSITY32I_EXT:
1355 case GL_INTENSITY32UI_EXT:
1356 return ctx->API == API_OPENGL_COMPAT &&
1357 ctx->Extensions.EXT_texture_integer &&
1358 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1360 case GL_LUMINANCE8I_EXT:
1361 case GL_LUMINANCE8UI_EXT:
1362 case GL_LUMINANCE16I_EXT:
1363 case GL_LUMINANCE16UI_EXT:
1364 case GL_LUMINANCE32I_EXT:
1365 case GL_LUMINANCE32UI_EXT:
1366 return ctx->API == API_OPENGL_COMPAT &&
1367 ctx->Extensions.EXT_texture_integer &&
1368 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1370 case GL_LUMINANCE_ALPHA8I_EXT:
1371 case GL_LUMINANCE_ALPHA8UI_EXT:
1372 case GL_LUMINANCE_ALPHA16I_EXT:
1373 case GL_LUMINANCE_ALPHA16UI_EXT:
1374 case GL_LUMINANCE_ALPHA32I_EXT:
1375 case GL_LUMINANCE_ALPHA32UI_EXT:
1376 return ctx->API == API_OPENGL_COMPAT &&
1377 ctx->Extensions.EXT_texture_integer &&
1378 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1380 case GL_ALPHA8I_EXT:
1381 case GL_ALPHA8UI_EXT:
1382 case GL_ALPHA16I_EXT:
1383 case GL_ALPHA16UI_EXT:
1384 case GL_ALPHA32I_EXT:
1385 case GL_ALPHA32UI_EXT:
1386 return ctx->API == API_OPENGL_COMPAT &&
1387 ctx->Extensions.EXT_texture_integer &&
1388 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1391 return (_mesa_is_desktop_gl(ctx) &&
1392 ctx->Extensions.ARB_texture_rgb10_a2ui)
1393 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1396 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1405 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1408 invalidate_rb(GLuint key, void *data, void *userData)
1410 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1411 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1413 /* If this is a user-created FBO */
1414 if (_mesa_is_user_fbo(fb)) {
1416 for (i = 0; i < BUFFER_COUNT; i++) {
1417 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1418 if (att->Type == GL_RENDERBUFFER &&
1419 att->Renderbuffer == rb) {
1420 /* Mark fb status as indeterminate to force re-validation */
1429 /** sentinal value, see below */
1430 #define NO_SAMPLES 1000
1434 * Helper function used by _mesa_RenderbufferStorage() and
1435 * _mesa_RenderbufferStorageMultisample().
1436 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1439 renderbuffer_storage(GLenum target, GLenum internalFormat,
1440 GLsizei width, GLsizei height, GLsizei samples)
1442 const char *func = samples == NO_SAMPLES ?
1443 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1444 struct gl_renderbuffer *rb;
1446 GET_CURRENT_CONTEXT(ctx);
1448 if (MESA_VERBOSE & VERBOSE_API) {
1449 if (samples == NO_SAMPLES)
1450 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
1452 _mesa_lookup_enum_by_nr(target),
1453 _mesa_lookup_enum_by_nr(internalFormat),
1456 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
1458 _mesa_lookup_enum_by_nr(target),
1459 _mesa_lookup_enum_by_nr(internalFormat),
1460 width, height, samples);
1463 if (target != GL_RENDERBUFFER_EXT) {
1464 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1468 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1469 if (baseFormat == 0) {
1470 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
1471 func, _mesa_lookup_enum_by_nr(internalFormat));
1475 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1476 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1480 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1481 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1485 if (samples == NO_SAMPLES) {
1486 /* NumSamples == 0 indicates non-multisampling */
1489 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1490 /* note: driver may choose to use more samples than what's requested */
1491 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1495 rb = ctx->CurrentRenderbuffer;
1497 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1501 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1503 if (rb->InternalFormat == internalFormat &&
1504 rb->Width == (GLuint) width &&
1505 rb->Height == (GLuint) height &&
1506 rb->NumSamples == samples) {
1507 /* no change in allocation needed */
1511 /* These MUST get set by the AllocStorage func */
1512 rb->Format = MESA_FORMAT_NONE;
1513 rb->NumSamples = samples;
1515 /* Now allocate the storage */
1516 ASSERT(rb->AllocStorage);
1517 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1518 /* No error - check/set fields now */
1519 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1520 assert(rb->Width == (GLuint) width);
1521 assert(rb->Height == (GLuint) height);
1522 rb->InternalFormat = internalFormat;
1523 rb->_BaseFormat = baseFormat;
1524 assert(rb->_BaseFormat != 0);
1527 /* Probably ran out of memory - clear the fields */
1530 rb->Format = MESA_FORMAT_NONE;
1531 rb->InternalFormat = GL_NONE;
1532 rb->_BaseFormat = GL_NONE;
1536 /* Invalidate the framebuffers the renderbuffer is attached in. */
1537 if (rb->AttachedAnytime) {
1538 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1544 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1546 struct gl_renderbuffer *rb;
1547 GET_CURRENT_CONTEXT(ctx);
1549 if (!ctx->Extensions.OES_EGL_image) {
1550 _mesa_error(ctx, GL_INVALID_OPERATION,
1551 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1555 if (target != GL_RENDERBUFFER) {
1556 _mesa_error(ctx, GL_INVALID_ENUM,
1557 "EGLImageTargetRenderbufferStorageOES");
1561 rb = ctx->CurrentRenderbuffer;
1563 _mesa_error(ctx, GL_INVALID_OPERATION,
1564 "EGLImageTargetRenderbufferStorageOES");
1568 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1570 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1575 * Helper function for _mesa_GetRenderbufferParameteriv() and
1576 * _mesa_GetFramebufferAttachmentParameteriv()
1577 * We have to be careful to respect the base format. For example, if a
1578 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1579 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1580 * we need to return zero.
1583 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1585 if (_mesa_base_format_has_channel(baseFormat, pname))
1586 return _mesa_get_format_bits(format, pname);
1594 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
1595 GLsizei width, GLsizei height)
1597 /* GL_ARB_fbo says calling this function is equivalent to calling
1598 * glRenderbufferStorageMultisample() with samples=0. We pass in
1599 * a token value here just for error reporting purposes.
1601 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1606 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1607 GLenum internalFormat,
1608 GLsizei width, GLsizei height)
1610 renderbuffer_storage(target, internalFormat, width, height, samples);
1615 * OpenGL ES version of glRenderBufferStorage.
1618 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1619 GLsizei width, GLsizei height)
1621 switch (internalFormat) {
1623 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1624 /* choose a closest format */
1625 internalFormat = GL_RGB5;
1631 renderbuffer_storage(target, internalFormat, width, height, 0);
1636 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
1638 struct gl_renderbuffer *rb;
1639 GET_CURRENT_CONTEXT(ctx);
1641 if (target != GL_RENDERBUFFER_EXT) {
1642 _mesa_error(ctx, GL_INVALID_ENUM,
1643 "glGetRenderbufferParameterivEXT(target)");
1647 rb = ctx->CurrentRenderbuffer;
1649 _mesa_error(ctx, GL_INVALID_OPERATION,
1650 "glGetRenderbufferParameterivEXT");
1654 /* No need to flush here since we're just quering state which is
1655 * not effected by rendering.
1659 case GL_RENDERBUFFER_WIDTH_EXT:
1660 *params = rb->Width;
1662 case GL_RENDERBUFFER_HEIGHT_EXT:
1663 *params = rb->Height;
1665 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1666 *params = rb->InternalFormat;
1668 case GL_RENDERBUFFER_RED_SIZE_EXT:
1669 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1670 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1671 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1672 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1673 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1674 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1676 case GL_RENDERBUFFER_SAMPLES:
1677 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
1678 || _mesa_is_gles3(ctx)) {
1679 *params = rb->NumSamples;
1684 _mesa_error(ctx, GL_INVALID_ENUM,
1685 "glGetRenderbufferParameterivEXT(target)");
1691 GLboolean GLAPIENTRY
1692 _mesa_IsFramebuffer(GLuint framebuffer)
1694 GET_CURRENT_CONTEXT(ctx);
1695 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1697 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1698 if (rb != NULL && rb != &DummyFramebuffer)
1706 * Check if any of the attachments of the given framebuffer are textures
1707 * (render to texture). Call ctx->Driver.RenderTexture() for such
1711 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1714 ASSERT(ctx->Driver.RenderTexture);
1716 if (_mesa_is_winsys_fbo(fb))
1717 return; /* can't render to texture with winsys framebuffers */
1719 for (i = 0; i < BUFFER_COUNT; i++) {
1720 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1721 if (att->Texture && _mesa_get_attachment_teximage(att)) {
1722 ctx->Driver.RenderTexture(ctx, fb, att);
1729 * Examine all the framebuffer's attachments to see if any are textures.
1730 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1731 * notify the device driver that the texture image may have changed.
1734 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1736 if (_mesa_is_winsys_fbo(fb))
1737 return; /* can't render to texture with winsys framebuffers */
1739 if (ctx->Driver.FinishRenderTexture) {
1741 for (i = 0; i < BUFFER_COUNT; i++) {
1742 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1743 if (att->Texture && att->Renderbuffer) {
1744 ctx->Driver.FinishRenderTexture(ctx, att);
1752 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
1754 struct gl_framebuffer *newDrawFb, *newReadFb;
1755 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1756 GLboolean bindReadBuf, bindDrawBuf;
1757 GET_CURRENT_CONTEXT(ctx);
1760 if (ctx->Extensions.ARB_framebuffer_object) {
1761 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1762 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1766 if (!ctx->Extensions.EXT_framebuffer_object) {
1767 _mesa_error(ctx, GL_INVALID_OPERATION,
1768 "glBindFramebufferEXT(unsupported)");
1773 case GL_DRAW_FRAMEBUFFER_EXT:
1774 if (!ctx->Extensions.EXT_framebuffer_blit) {
1775 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1778 bindDrawBuf = GL_TRUE;
1779 bindReadBuf = GL_FALSE;
1781 case GL_READ_FRAMEBUFFER_EXT:
1782 if (!ctx->Extensions.EXT_framebuffer_blit) {
1783 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1786 bindDrawBuf = GL_FALSE;
1787 bindReadBuf = GL_TRUE;
1789 case GL_FRAMEBUFFER_EXT:
1790 bindDrawBuf = GL_TRUE;
1791 bindReadBuf = GL_TRUE;
1794 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1799 /* Binding a user-created framebuffer object */
1800 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1801 if (newDrawFb == &DummyFramebuffer) {
1802 /* ID was reserved, but no real framebuffer object made yet */
1806 && _mesa_is_desktop_gl(ctx)
1807 && ctx->Extensions.ARB_framebuffer_object) {
1808 /* All FBO IDs must be Gen'd */
1809 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1814 /* create new framebuffer object */
1815 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1817 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1820 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1822 newReadFb = newDrawFb;
1825 /* Binding the window system framebuffer (which was originally set
1826 * with MakeCurrent).
1828 newDrawFb = ctx->WinSysDrawBuffer;
1829 newReadFb = ctx->WinSysReadBuffer;
1833 ASSERT(newDrawFb != &DummyFramebuffer);
1835 /* save pointers to current/old framebuffers */
1836 oldDrawFb = ctx->DrawBuffer;
1837 oldReadFb = ctx->ReadBuffer;
1839 /* check if really changing bindings */
1840 if (oldDrawFb == newDrawFb)
1841 bindDrawBuf = GL_FALSE;
1842 if (oldReadFb == newReadFb)
1843 bindReadBuf = GL_FALSE;
1846 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1848 * We also check if we're beginning and/or ending render-to-texture.
1849 * When a framebuffer with texture attachments is unbound, call
1850 * ctx->Driver.FinishRenderTexture().
1851 * When a framebuffer with texture attachments is bound, call
1852 * ctx->Driver.RenderTexture().
1854 * Note that if the ReadBuffer has texture attachments we don't consider
1855 * that a render-to-texture case.
1858 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1860 /* check if old readbuffer was render-to-texture */
1861 check_end_texture_render(ctx, oldReadFb);
1863 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1867 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1869 /* check if old framebuffer had any texture attachments */
1871 check_end_texture_render(ctx, oldDrawFb);
1873 /* check if newly bound framebuffer has any texture attachments */
1874 check_begin_texture_render(ctx, newDrawFb);
1876 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1879 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1880 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1886 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
1889 GET_CURRENT_CONTEXT(ctx);
1891 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1893 for (i = 0; i < n; i++) {
1894 if (framebuffers[i] > 0) {
1895 struct gl_framebuffer *fb;
1896 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1898 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1900 /* check if deleting currently bound framebuffer object */
1901 if (ctx->Extensions.EXT_framebuffer_blit) {
1902 /* separate draw/read binding points */
1903 if (fb == ctx->DrawBuffer) {
1905 ASSERT(fb->RefCount >= 2);
1906 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
1908 if (fb == ctx->ReadBuffer) {
1910 ASSERT(fb->RefCount >= 2);
1911 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
1915 /* only one binding point for read/draw buffers */
1916 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1918 ASSERT(fb->RefCount >= 2);
1919 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
1923 /* remove from hash table immediately, to free the ID */
1924 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1926 if (fb != &DummyFramebuffer) {
1927 /* But the object will not be freed until it's no longer
1928 * bound in any context.
1930 _mesa_reference_framebuffer(&fb, NULL);
1939 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
1941 GET_CURRENT_CONTEXT(ctx);
1946 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1953 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1955 for (i = 0; i < n; i++) {
1956 GLuint name = first + i;
1957 framebuffers[i] = name;
1958 /* insert dummy placeholder into hash table */
1959 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1960 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1961 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1968 _mesa_CheckFramebufferStatus(GLenum target)
1970 struct gl_framebuffer *buffer;
1971 GET_CURRENT_CONTEXT(ctx);
1973 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1975 if (MESA_VERBOSE & VERBOSE_API)
1976 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
1977 _mesa_lookup_enum_by_nr(target));
1979 buffer = get_framebuffer_target(ctx, target);
1981 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1985 if (_mesa_is_winsys_fbo(buffer)) {
1986 /* The window system / default framebuffer is always complete */
1987 return GL_FRAMEBUFFER_COMPLETE_EXT;
1990 /* No need to flush here */
1992 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1993 _mesa_test_framebuffer_completeness(ctx, buffer);
1996 return buffer->_Status;
2001 * Replicate the src attachment point. Used by framebuffer_texture() when
2002 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2003 * GL_STENCIL_ATTACHMENT.
2006 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2007 gl_buffer_index dst,
2008 gl_buffer_index src)
2010 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2011 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2013 assert(src_att->Texture != NULL);
2014 assert(src_att->Renderbuffer != NULL);
2016 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2017 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2018 dst_att->Type = src_att->Type;
2019 dst_att->Complete = src_att->Complete;
2020 dst_att->TextureLevel = src_att->TextureLevel;
2021 dst_att->Zoffset = src_att->Zoffset;
2026 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
2027 * glFramebufferTextureLayerEXT().
2028 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2029 * get textarget=0 in that case.
2032 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target,
2033 GLenum attachment, GLenum textarget, GLuint texture,
2034 GLint level, GLint zoffset)
2036 struct gl_renderbuffer_attachment *att;
2037 struct gl_texture_object *texObj = NULL;
2038 struct gl_framebuffer *fb;
2039 GLenum maxLevelsTarget;
2041 fb = get_framebuffer_target(ctx, target);
2043 _mesa_error(ctx, GL_INVALID_ENUM,
2044 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
2048 /* check framebuffer binding */
2049 if (_mesa_is_winsys_fbo(fb)) {
2050 _mesa_error(ctx, GL_INVALID_OPERATION,
2051 "glFramebufferTexture%sEXT", caller);
2055 /* The textarget, level, and zoffset parameters are only validated if
2056 * texture is non-zero.
2059 GLboolean err = GL_TRUE;
2061 texObj = _mesa_lookup_texture(ctx, texture);
2062 if (texObj != NULL) {
2063 if (textarget == 0) {
2064 /* If textarget == 0 it means we're being called by
2065 * glFramebufferTextureLayer() and textarget is not used.
2066 * The only legal texture types for that function are 3D and
2067 * 1D/2D arrays textures.
2069 err = (texObj->Target != GL_TEXTURE_3D) &&
2070 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
2071 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT) &&
2072 (texObj->Target != GL_TEXTURE_CUBE_MAP_ARRAY);
2075 /* Make sure textarget is consistent with the texture's type */
2076 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
2077 ? !_mesa_is_cube_face(textarget)
2078 : (texObj->Target != textarget);
2082 /* can't render to a non-existant texture */
2083 _mesa_error(ctx, GL_INVALID_OPERATION,
2084 "glFramebufferTexture%sEXT(non existant texture)",
2090 _mesa_error(ctx, GL_INVALID_OPERATION,
2091 "glFramebufferTexture%sEXT(texture target mismatch)",
2096 if (texObj->Target == GL_TEXTURE_3D) {
2097 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
2098 if (zoffset < 0 || zoffset >= maxSize) {
2099 _mesa_error(ctx, GL_INVALID_VALUE,
2100 "glFramebufferTexture%sEXT(zoffset)", caller);
2104 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
2105 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT) ||
2106 (texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY)) {
2108 zoffset >= (GLint) ctx->Const.MaxArrayTextureLayers) {
2109 _mesa_error(ctx, GL_INVALID_VALUE,
2110 "glFramebufferTexture%sEXT(layer)", caller);
2115 maxLevelsTarget = textarget ? textarget : texObj->Target;
2117 (level >= _mesa_max_texture_levels(ctx, maxLevelsTarget))) {
2118 _mesa_error(ctx, GL_INVALID_VALUE,
2119 "glFramebufferTexture%sEXT(level)", caller);
2124 att = _mesa_get_attachment(ctx, fb, attachment);
2126 _mesa_error(ctx, GL_INVALID_ENUM,
2127 "glFramebufferTexture%sEXT(attachment)", caller);
2131 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2133 _glthread_LOCK_MUTEX(fb->Mutex);
2135 if (attachment == GL_DEPTH_ATTACHMENT &&
2136 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
2137 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
2138 _mesa_tex_target_to_face(textarget) ==
2139 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
2140 zoffset == fb->Attachment[BUFFER_STENCIL].Zoffset) {
2141 /* The texture object is already attached to the stencil attachment
2142 * point. Don't create a new renderbuffer; just reuse the stencil
2143 * attachment's. This is required to prevent a GL error in
2144 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2146 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2148 } else if (attachment == GL_STENCIL_ATTACHMENT &&
2149 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
2150 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
2151 _mesa_tex_target_to_face(textarget) ==
2152 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
2153 zoffset == fb->Attachment[BUFFER_DEPTH].Zoffset) {
2154 /* As above, but with depth and stencil transposed. */
2155 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2158 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
2160 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2161 /* Above we created a new renderbuffer and attached it to the
2162 * depth attachment point. Now attach it to the stencil attachment
2165 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2166 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2171 /* Set the render-to-texture flag. We'll check this flag in
2172 * glTexImage() and friends to determine if we need to revalidate
2173 * any FBOs that might be rendering into this texture.
2174 * This flag never gets cleared since it's non-trivial to determine
2175 * when all FBOs might be done rendering to this texture. That's OK
2176 * though since it's uncommon to render to a texture then repeatedly
2177 * call glTexImage() to change images in the texture.
2179 texObj->_RenderToTexture = GL_TRUE;
2182 _mesa_remove_attachment(ctx, att);
2183 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2184 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2185 _mesa_remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2189 invalidate_framebuffer(fb);
2191 _glthread_UNLOCK_MUTEX(fb->Mutex);
2197 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
2198 GLenum textarget, GLuint texture, GLint level)
2200 GET_CURRENT_CONTEXT(ctx);
2205 switch (textarget) {
2209 case GL_TEXTURE_1D_ARRAY:
2210 error = !ctx->Extensions.EXT_texture_array;
2217 _mesa_error(ctx, GL_INVALID_OPERATION,
2218 "glFramebufferTexture1DEXT(textarget=%s)",
2219 _mesa_lookup_enum_by_nr(textarget));
2224 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2230 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
2231 GLenum textarget, GLuint texture, GLint level)
2233 GET_CURRENT_CONTEXT(ctx);
2238 switch (textarget) {
2242 case GL_TEXTURE_RECTANGLE:
2243 error = _mesa_is_gles(ctx)
2244 || !ctx->Extensions.NV_texture_rectangle;
2246 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2247 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2248 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2249 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2250 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2251 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2252 error = !ctx->Extensions.ARB_texture_cube_map;
2254 case GL_TEXTURE_2D_ARRAY:
2255 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2256 || !ctx->Extensions.EXT_texture_array;
2263 _mesa_error(ctx, GL_INVALID_OPERATION,
2264 "glFramebufferTexture2DEXT(textarget=%s)",
2265 _mesa_lookup_enum_by_nr(textarget));
2270 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2276 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
2277 GLenum textarget, GLuint texture,
2278 GLint level, GLint zoffset)
2280 GET_CURRENT_CONTEXT(ctx);
2282 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2283 _mesa_error(ctx, GL_INVALID_OPERATION,
2284 "glFramebufferTexture3DEXT(textarget)");
2288 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2294 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
2295 GLuint texture, GLint level, GLint layer)
2297 GET_CURRENT_CONTEXT(ctx);
2299 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2305 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
2306 GLenum renderbufferTarget,
2307 GLuint renderbuffer)
2309 struct gl_renderbuffer_attachment *att;
2310 struct gl_framebuffer *fb;
2311 struct gl_renderbuffer *rb;
2312 GET_CURRENT_CONTEXT(ctx);
2314 fb = get_framebuffer_target(ctx, target);
2316 _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2320 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2321 _mesa_error(ctx, GL_INVALID_ENUM,
2322 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2326 if (_mesa_is_winsys_fbo(fb)) {
2327 /* Can't attach new renderbuffers to a window system framebuffer */
2328 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2332 att = _mesa_get_attachment(ctx, fb, attachment);
2334 _mesa_error(ctx, GL_INVALID_ENUM,
2335 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2336 _mesa_lookup_enum_by_nr(attachment));
2341 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2343 _mesa_error(ctx, GL_INVALID_OPERATION,
2344 "glFramebufferRenderbufferEXT(non-existant"
2345 " renderbuffer %u)", renderbuffer);
2348 else if (rb == &DummyRenderbuffer) {
2349 /* This is what NVIDIA does */
2350 _mesa_error(ctx, GL_INVALID_VALUE,
2351 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2357 /* remove renderbuffer attachment */
2361 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2362 rb && rb->Format != MESA_FORMAT_NONE) {
2363 /* make sure the renderbuffer is a depth/stencil format */
2364 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2365 if (baseFormat != GL_DEPTH_STENCIL) {
2366 _mesa_error(ctx, GL_INVALID_OPERATION,
2367 "glFramebufferRenderbufferEXT(renderbuffer"
2368 " is not DEPTH_STENCIL format)");
2374 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2376 assert(ctx->Driver.FramebufferRenderbuffer);
2377 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2379 /* Some subsequent GL commands may depend on the framebuffer's visual
2380 * after the binding is updated. Update visual info now.
2382 _mesa_update_framebuffer_visual(ctx, fb);
2387 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
2388 GLenum pname, GLint *params)
2390 const struct gl_renderbuffer_attachment *att;
2391 struct gl_framebuffer *buffer;
2393 GET_CURRENT_CONTEXT(ctx);
2395 /* The error differs in GL and GLES. */
2396 err = _mesa_is_desktop_gl(ctx) ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2398 buffer = get_framebuffer_target(ctx, target);
2400 _mesa_error(ctx, GL_INVALID_ENUM,
2401 "glGetFramebufferAttachmentParameterivEXT(target)");
2405 if (_mesa_is_winsys_fbo(buffer)) {
2406 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2409 * "If the framebuffer currently bound to target is zero, then
2410 * INVALID_OPERATION is generated."
2412 * The EXT_framebuffer_object spec has the same wording, and the
2413 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2416 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2417 && !_mesa_is_gles3(ctx)) {
2418 _mesa_error(ctx, GL_INVALID_OPERATION,
2419 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2423 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
2424 attachment != GL_DEPTH && attachment != GL_STENCIL) {
2425 _mesa_error(ctx, GL_INVALID_OPERATION,
2426 "glGetFramebufferAttachmentParameteriv(attachment)");
2429 /* the default / window-system FBO */
2430 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2433 /* user-created framebuffer FBO */
2434 att = _mesa_get_attachment(ctx, buffer, attachment);
2438 _mesa_error(ctx, GL_INVALID_ENUM,
2439 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2443 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2444 /* the depth and stencil attachments must point to the same buffer */
2445 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2446 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2447 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2448 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2449 _mesa_error(ctx, GL_INVALID_OPERATION,
2450 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2451 " attachments differ)");
2456 /* No need to flush here */
2459 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2460 *params = _mesa_is_winsys_fbo(buffer)
2461 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2463 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2464 if (att->Type == GL_RENDERBUFFER_EXT) {
2465 *params = att->Renderbuffer->Name;
2467 else if (att->Type == GL_TEXTURE) {
2468 *params = att->Texture->Name;
2471 assert(att->Type == GL_NONE);
2472 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
2475 goto invalid_pname_enum;
2479 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2480 if (att->Type == GL_TEXTURE) {
2481 *params = att->TextureLevel;
2483 else if (att->Type == GL_NONE) {
2484 _mesa_error(ctx, err,
2485 "glGetFramebufferAttachmentParameterivEXT(pname)");
2488 goto invalid_pname_enum;
2491 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2492 if (att->Type == GL_TEXTURE) {
2493 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2494 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2500 else if (att->Type == GL_NONE) {
2501 _mesa_error(ctx, err,
2502 "glGetFramebufferAttachmentParameterivEXT(pname)");
2505 goto invalid_pname_enum;
2508 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2509 if (ctx->API == API_OPENGLES) {
2510 goto invalid_pname_enum;
2511 } else if (att->Type == GL_NONE) {
2512 _mesa_error(ctx, err,
2513 "glGetFramebufferAttachmentParameterivEXT(pname)");
2514 } else if (att->Type == GL_TEXTURE) {
2515 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2516 *params = att->Zoffset;
2523 goto invalid_pname_enum;
2526 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2527 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2528 && !_mesa_is_gles3(ctx)) {
2529 goto invalid_pname_enum;
2531 else if (att->Type == GL_NONE) {
2532 _mesa_error(ctx, err,
2533 "glGetFramebufferAttachmentParameterivEXT(pname)");
2536 if (ctx->Extensions.EXT_framebuffer_sRGB) {
2537 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2540 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2541 * if the sRGB conversion is unsupported. */
2542 *params = GL_LINEAR;
2546 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2547 if ((ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_framebuffer_object)
2548 && ctx->API != API_OPENGL_CORE
2549 && !_mesa_is_gles3(ctx)) {
2550 goto invalid_pname_enum;
2552 else if (att->Type == GL_NONE) {
2553 _mesa_error(ctx, err,
2554 "glGetFramebufferAttachmentParameterivEXT(pname)");
2557 gl_format format = att->Renderbuffer->Format;
2559 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
2562 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
2563 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
2564 * generate an INVALID_OPERATION error.
2566 if (_mesa_is_gles3(ctx) && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2567 _mesa_error(ctx, GL_INVALID_OPERATION,
2568 "glGetFramebufferAttachmentParameteriv(cannot query "
2569 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
2570 "GL_DEPTH_STENCIL_ATTACHMENT");
2574 if (format == MESA_FORMAT_S8) {
2578 else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2579 /* depends on the attachment parameter */
2580 if (attachment == GL_STENCIL_ATTACHMENT) {
2588 *params = _mesa_get_format_datatype(format);
2592 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2593 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2594 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2595 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2596 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2597 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2598 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2599 && !_mesa_is_gles3(ctx)) {
2600 goto invalid_pname_enum;
2602 else if (att->Type == GL_NONE) {
2603 _mesa_error(ctx, err,
2604 "glGetFramebufferAttachmentParameterivEXT(pname)");
2606 else if (att->Texture) {
2607 const struct gl_texture_image *texImage =
2608 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2611 *params = get_component_bits(pname, texImage->_BaseFormat,
2612 texImage->TexFormat);
2618 else if (att->Renderbuffer) {
2619 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2620 att->Renderbuffer->Format);
2623 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2624 " invalid FBO attachment structure");
2628 goto invalid_pname_enum;
2634 _mesa_error(ctx, GL_INVALID_ENUM,
2635 "glGetFramebufferAttachmentParameteriv(pname)");
2641 _mesa_GenerateMipmap(GLenum target)
2643 struct gl_texture_image *srcImage;
2644 struct gl_texture_object *texObj;
2647 GET_CURRENT_CONTEXT(ctx);
2649 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2653 error = _mesa_is_gles(ctx);
2659 error = ctx->API == API_OPENGLES;
2661 case GL_TEXTURE_CUBE_MAP:
2662 error = !ctx->Extensions.ARB_texture_cube_map;
2664 case GL_TEXTURE_1D_ARRAY:
2665 error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
2667 case GL_TEXTURE_2D_ARRAY:
2668 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2669 || !ctx->Extensions.EXT_texture_array;
2676 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2677 _mesa_lookup_enum_by_nr(target));
2681 texObj = _mesa_get_current_tex_object(ctx, target);
2683 if (texObj->BaseLevel >= texObj->MaxLevel) {
2688 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2689 !_mesa_cube_complete(texObj)) {
2690 _mesa_error(ctx, GL_INVALID_OPERATION,
2691 "glGenerateMipmap(incomplete cube map)");
2695 _mesa_lock_texture(ctx, texObj);
2697 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
2699 _mesa_unlock_texture(ctx, texObj);
2700 _mesa_error(ctx, GL_INVALID_OPERATION,
2701 "glGenerateMipmap(zero size base image)");
2705 if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
2706 _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
2707 _mesa_is_stencil_format(srcImage->InternalFormat)) {
2708 _mesa_unlock_texture(ctx, texObj);
2709 _mesa_error(ctx, GL_INVALID_OPERATION,
2710 "glGenerateMipmap(invalid internal format)");
2714 if (target == GL_TEXTURE_CUBE_MAP) {
2716 for (face = 0; face < 6; face++)
2717 ctx->Driver.GenerateMipmap(ctx,
2718 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2722 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2724 _mesa_unlock_texture(ctx, texObj);
2728 static const struct gl_renderbuffer_attachment *
2729 find_attachment(const struct gl_framebuffer *fb,
2730 const struct gl_renderbuffer *rb)
2733 for (i = 0; i < Elements(fb->Attachment); i++) {
2734 if (fb->Attachment[i].Renderbuffer == rb)
2735 return &fb->Attachment[i];
2742 * Helper function for checking if the datatypes of color buffers are
2743 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2745 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2746 * and any of the following conditions hold:
2747 * - The read buffer contains fixed-point or floating-point values and any
2748 * draw buffer contains neither fixed-point nor floating-point values.
2749 * - The read buffer contains unsigned integer values and any draw buffer
2750 * does not contain unsigned integer values.
2751 * - The read buffer contains signed integer values and any draw buffer
2752 * does not contain signed integer values."
2755 compatible_color_datatypes(gl_format srcFormat, gl_format dstFormat)
2757 GLenum srcType = _mesa_get_format_datatype(srcFormat);
2758 GLenum dstType = _mesa_get_format_datatype(dstFormat);
2760 if (srcType != GL_INT && srcType != GL_UNSIGNED_INT) {
2761 assert(srcType == GL_UNSIGNED_NORMALIZED ||
2762 srcType == GL_SIGNED_NORMALIZED ||
2763 srcType == GL_FLOAT);
2764 /* Boil any of those types down to GL_FLOAT */
2768 if (dstType != GL_INT && dstType != GL_UNSIGNED_INT) {
2769 assert(dstType == GL_UNSIGNED_NORMALIZED ||
2770 dstType == GL_SIGNED_NORMALIZED ||
2771 dstType == GL_FLOAT);
2772 /* Boil any of those types down to GL_FLOAT */
2776 return srcType == dstType;
2781 compatible_resolve_formats(const struct gl_renderbuffer *readRb,
2782 const struct gl_renderbuffer *drawRb)
2784 GLenum readFormat, drawFormat;
2786 /* The simple case where we know the backing Mesa formats are the same.
2788 if (_mesa_get_srgb_format_linear(readRb->Format) ==
2789 _mesa_get_srgb_format_linear(drawRb->Format)) {
2793 /* The Mesa formats are different, so we must check whether the internal
2794 * formats are compatible.
2796 * Under some circumstances, the user may request e.g. two GL_RGBA8
2797 * textures and get two entirely different Mesa formats like RGBA8888 and
2798 * ARGB8888. Drivers behaving like that should be able to cope with
2799 * non-matching formats by themselves, because it's not the user's fault.
2801 * Blits between linear and sRGB formats are also allowed.
2803 readFormat = _mesa_get_nongeneric_internalformat(readRb->InternalFormat);
2804 drawFormat = _mesa_get_nongeneric_internalformat(drawRb->InternalFormat);
2805 readFormat = _mesa_get_linear_internalformat(readFormat);
2806 drawFormat = _mesa_get_linear_internalformat(drawFormat);
2808 if (readFormat == drawFormat) {
2817 * Blit rectangular region, optionally from one framebuffer to another.
2819 * Note, if the src buffer is multisampled and the dest is not, this is
2820 * when the samples must be resolved to a single color.
2823 _mesa_BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2824 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2825 GLbitfield mask, GLenum filter)
2827 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2828 GL_DEPTH_BUFFER_BIT |
2829 GL_STENCIL_BUFFER_BIT);
2830 const struct gl_framebuffer *readFb, *drawFb;
2831 GET_CURRENT_CONTEXT(ctx);
2833 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2835 if (MESA_VERBOSE & VERBOSE_API)
2837 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2838 srcX0, srcY0, srcX1, srcY1,
2839 dstX0, dstY0, dstX1, dstY1,
2840 mask, _mesa_lookup_enum_by_nr(filter));
2842 if (ctx->NewState) {
2843 _mesa_update_state(ctx);
2846 readFb = ctx->ReadBuffer;
2847 drawFb = ctx->DrawBuffer;
2849 if (!readFb || !drawFb) {
2850 /* This will normally never happen but someday we may want to
2851 * support MakeCurrent() with no drawables.
2856 /* check for complete framebuffers */
2857 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2858 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2859 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2860 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2864 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2865 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2869 if (mask & ~legalMaskBits) {
2870 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2874 /* depth/stencil must be blitted with nearest filtering */
2875 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2876 && filter != GL_NEAREST) {
2877 _mesa_error(ctx, GL_INVALID_OPERATION,
2878 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2882 /* get color read/draw renderbuffers */
2883 if (mask & GL_COLOR_BUFFER_BIT) {
2884 const GLuint numColorDrawBuffers = ctx->DrawBuffer->_NumColorDrawBuffers;
2885 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
2886 const struct gl_renderbuffer *colorDrawRb = NULL;
2889 /* From the EXT_framebuffer_object spec:
2891 * "If a buffer is specified in <mask> and does not exist in both
2892 * the read and draw framebuffers, the corresponding bit is silently
2895 if (!colorReadRb || numColorDrawBuffers == 0) {
2896 mask &= ~GL_COLOR_BUFFER_BIT;
2899 for (i = 0; i < numColorDrawBuffers; i++) {
2900 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
2904 /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
2905 * ES 3.0.1 spec says:
2907 * "If the source and destination buffers are identical, an
2908 * INVALID_OPERATION error is generated. Different mipmap
2909 * levels of a texture, different layers of a three-
2910 * dimensional texture or two-dimensional array texture, and
2911 * different faces of a cube map texture do not constitute
2912 * identical buffers."
2914 if (_mesa_is_gles3(ctx) && (colorDrawRb == colorReadRb)) {
2915 _mesa_error(ctx, GL_INVALID_OPERATION,
2916 "glBlitFramebuffer(source and destination color "
2917 "buffer cannot be the same)");
2921 if (!compatible_color_datatypes(colorReadRb->Format,
2922 colorDrawRb->Format)) {
2923 _mesa_error(ctx, GL_INVALID_OPERATION,
2924 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2927 /* extra checks for multisample copies... */
2928 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2929 /* color formats must match */
2930 if (!compatible_resolve_formats(colorReadRb, colorDrawRb)) {
2931 _mesa_error(ctx, GL_INVALID_OPERATION,
2932 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2937 if (filter == GL_LINEAR) {
2938 /* 3.1 spec, page 199:
2939 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2940 * if filter is LINEAR and read buffer contains integer data."
2942 GLenum type = _mesa_get_format_datatype(colorReadRb->Format);
2943 if (type == GL_INT || type == GL_UNSIGNED_INT) {
2944 _mesa_error(ctx, GL_INVALID_OPERATION,
2945 "glBlitFramebufferEXT(integer color type)");
2952 if (mask & GL_STENCIL_BUFFER_BIT) {
2953 struct gl_renderbuffer *readRb =
2954 readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2955 struct gl_renderbuffer *drawRb =
2956 drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2958 /* From the EXT_framebuffer_object spec:
2960 * "If a buffer is specified in <mask> and does not exist in both
2961 * the read and draw framebuffers, the corresponding bit is silently
2964 if ((readRb == NULL) || (drawRb == NULL)) {
2965 mask &= ~GL_STENCIL_BUFFER_BIT;
2968 int read_z_bits, draw_z_bits;
2970 if (_mesa_is_gles3(ctx) && (drawRb == readRb)) {
2971 _mesa_error(ctx, GL_INVALID_OPERATION,
2972 "glBlitFramebuffer(source and destination stencil "
2973 "buffer cannot be the same)");
2977 if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2978 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2979 /* There is no need to check the stencil datatype here, because
2980 * there is only one: GL_UNSIGNED_INT.
2982 _mesa_error(ctx, GL_INVALID_OPERATION,
2983 "glBlitFramebuffer(stencil attachment format mismatch)");
2987 read_z_bits = _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS);
2988 draw_z_bits = _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS);
2990 /* If both buffers also have depth data, the depth formats must match
2991 * as well. If one doesn't have depth, it's not blitted, so we should
2992 * ignore the depth format check.
2994 if (read_z_bits > 0 && draw_z_bits > 0 &&
2995 (read_z_bits != draw_z_bits ||
2996 _mesa_get_format_datatype(readRb->Format) !=
2997 _mesa_get_format_datatype(drawRb->Format))) {
2999 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
3000 "(stencil attachment depth format mismatch)");
3006 if (mask & GL_DEPTH_BUFFER_BIT) {
3007 struct gl_renderbuffer *readRb =
3008 readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
3009 struct gl_renderbuffer *drawRb =
3010 drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
3012 /* From the EXT_framebuffer_object spec:
3014 * "If a buffer is specified in <mask> and does not exist in both
3015 * the read and draw framebuffers, the corresponding bit is silently
3018 if ((readRb == NULL) || (drawRb == NULL)) {
3019 mask &= ~GL_DEPTH_BUFFER_BIT;
3022 int read_s_bit, draw_s_bit;
3024 if (_mesa_is_gles3(ctx) && (drawRb == readRb)) {
3025 _mesa_error(ctx, GL_INVALID_OPERATION,
3026 "glBlitFramebuffer(source and destination depth "
3027 "buffer cannot be the same)");
3031 if ((_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
3032 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) ||
3033 (_mesa_get_format_datatype(readRb->Format) !=
3034 _mesa_get_format_datatype(drawRb->Format))) {
3035 _mesa_error(ctx, GL_INVALID_OPERATION,
3036 "glBlitFramebuffer(depth attachment format mismatch)");
3040 read_s_bit = _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS);
3041 draw_s_bit = _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS);
3043 /* If both buffers also have stencil data, the stencil formats must
3044 * match as well. If one doesn't have stencil, it's not blitted, so
3045 * we should ignore the stencil format check.
3047 if (read_s_bit > 0 && draw_s_bit > 0 && read_s_bit != draw_s_bit) {
3048 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
3049 "(depth attachment stencil bits mismatch)");
3055 if (readFb->Visual.samples > 0 &&
3056 drawFb->Visual.samples > 0 &&
3057 readFb->Visual.samples != drawFb->Visual.samples) {
3058 _mesa_error(ctx, GL_INVALID_OPERATION,
3059 "glBlitFramebufferEXT(mismatched samples)");
3063 /* extra checks for multisample copies... */
3064 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
3065 /* src and dest region sizes must be the same */
3066 if (abs(srcX1 - srcX0) != abs(dstX1 - dstX0) ||
3067 abs(srcY1 - srcY0) != abs(dstY1 - dstY0)) {
3068 _mesa_error(ctx, GL_INVALID_OPERATION,
3069 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3074 if (!ctx->Extensions.EXT_framebuffer_blit) {
3075 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
3081 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
3082 const struct gl_renderbuffer *colorDrawRb = NULL;
3085 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3087 srcX0, srcY0, srcX1, srcY1,
3088 dstX0, dstY0, dstX1, dstY1,
3091 const struct gl_renderbuffer_attachment *att;
3093 att = find_attachment(readFb, colorReadRb);
3094 printf(" Src FBO %u RB %u (%dx%d) ",
3095 readFb->Name, colorReadRb->Name,
3096 colorReadRb->Width, colorReadRb->Height);
3097 if (att && att->Texture) {
3098 printf("Tex %u tgt 0x%x level %u face %u",
3100 att->Texture->Target,
3106 /* Print all active color render buffers */
3107 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
3108 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
3112 att = find_attachment(drawFb, colorDrawRb);
3113 printf(" Dst FBO %u RB %u (%dx%d) ",
3114 drawFb->Name, colorDrawRb->Name,
3115 colorDrawRb->Width, colorDrawRb->Height);
3116 if (att && att->Texture) {
3117 printf("Tex %u tgt 0x%x level %u face %u",
3119 att->Texture->Target,
3132 ASSERT(ctx->Driver.BlitFramebuffer);
3133 ctx->Driver.BlitFramebuffer(ctx,
3134 srcX0, srcY0, srcX1, srcY1,
3135 dstX0, dstY0, dstX1, dstY1,
3141 invalidate_framebuffer_storage(GLenum target, GLsizei numAttachments,
3142 const GLenum *attachments, GLint x, GLint y,
3143 GLsizei width, GLsizei height, const char *name)
3146 struct gl_framebuffer *fb;
3147 GET_CURRENT_CONTEXT(ctx);
3149 fb = get_framebuffer_target(ctx, target);
3151 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
3155 if (numAttachments < 0) {
3156 _mesa_error(ctx, GL_INVALID_VALUE,
3157 "%s(numAttachments < 0)", name);
3161 /* The GL_ARB_invalidate_subdata spec says:
3163 * "If an attachment is specified that does not exist in the
3164 * framebuffer bound to <target>, it is ignored."
3168 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3169 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3170 * INVALID_OPERATION is generated."
3172 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3173 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3174 * set of retrictions).
3176 for (i = 0; i < numAttachments; i++) {
3177 if (_mesa_is_winsys_fbo(fb)) {
3178 switch (attachments[i]) {
3184 /* Accumulation buffers and auxilary buffers were removed in
3185 * OpenGL 3.1, and they never existed in OpenGL ES.
3187 if (ctx->API != API_OPENGL_COMPAT)
3197 case GL_FRONT_RIGHT:
3198 if (!_mesa_is_desktop_gl(ctx))
3205 switch (attachments[i]) {
3206 case GL_DEPTH_ATTACHMENT:
3207 case GL_STENCIL_ATTACHMENT:
3209 case GL_COLOR_ATTACHMENT0:
3210 case GL_COLOR_ATTACHMENT1:
3211 case GL_COLOR_ATTACHMENT2:
3212 case GL_COLOR_ATTACHMENT3:
3213 case GL_COLOR_ATTACHMENT4:
3214 case GL_COLOR_ATTACHMENT5:
3215 case GL_COLOR_ATTACHMENT6:
3216 case GL_COLOR_ATTACHMENT7:
3217 case GL_COLOR_ATTACHMENT8:
3218 case GL_COLOR_ATTACHMENT9:
3219 case GL_COLOR_ATTACHMENT10:
3220 case GL_COLOR_ATTACHMENT11:
3221 case GL_COLOR_ATTACHMENT12:
3222 case GL_COLOR_ATTACHMENT13:
3223 case GL_COLOR_ATTACHMENT14:
3224 case GL_COLOR_ATTACHMENT15: {
3225 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
3226 if (k >= ctx->Const.MaxColorAttachments) {
3227 _mesa_error(ctx, GL_INVALID_OPERATION,
3228 "%s(attachment >= max. color attachments)", name);
3238 /* We don't actually do anything for this yet. Just return after
3239 * validating the parameters and generating the required errors.
3244 _mesa_error(ctx, GL_INVALID_ENUM, "%s(attachment)", name);
3249 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
3250 const GLenum *attachments, GLint x, GLint y,
3251 GLsizei width, GLsizei height)
3253 invalidate_framebuffer_storage(target, numAttachments, attachments,
3254 x, y, width, height,
3255 "glInvalidateSubFramebuffer");
3259 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
3260 const GLenum *attachments)
3262 /* The GL_ARB_invalidate_subdata spec says:
3266 * void InvalidateFramebuffer(enum target,
3267 * sizei numAttachments,
3268 * const enum *attachments);
3270 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3271 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3272 * <MAX_VIEWPORT_DIMS[1]> respectively."
3274 invalidate_framebuffer_storage(target, numAttachments, attachments,
3275 0, 0, MAX_VIEWPORT_WIDTH, MAX_VIEWPORT_HEIGHT,
3276 "glInvalidateFramebuffer");