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
38 #include "framebuffer.h"
42 #include "renderbuffer.h"
52 * None of the GL_EXT_framebuffer_object functions are compiled into
59 * When glGenRender/FramebuffersEXT() is called we insert pointers to
60 * these placeholder objects into the hash table.
61 * Later, when the object ID is first bound, we replace the placeholder
62 * with the real frame/renderbuffer.
64 static struct gl_framebuffer DummyFramebuffer;
65 static struct gl_renderbuffer DummyRenderbuffer;
68 #define IS_CUBE_FACE(TARGET) \
69 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
70 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
74 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
80 delete_dummy_framebuffer(struct gl_framebuffer *fb)
87 _mesa_init_fbobjects(GLcontext *ctx)
89 DummyFramebuffer.Delete = delete_dummy_framebuffer;
90 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
95 * Helper routine for getting a gl_renderbuffer.
97 struct gl_renderbuffer *
98 _mesa_lookup_renderbuffer(GLcontext *ctx, GLuint id)
100 struct gl_renderbuffer *rb;
105 rb = (struct gl_renderbuffer *)
106 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
112 * Helper routine for getting a gl_framebuffer.
114 struct gl_framebuffer *
115 _mesa_lookup_framebuffer(GLcontext *ctx, GLuint id)
117 struct gl_framebuffer *fb;
122 fb = (struct gl_framebuffer *)
123 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
129 * Mark the given framebuffer as invalid. This will force the
130 * test for framebuffer completeness to be done before the framebuffer
134 invalidate_framebuffer(struct gl_framebuffer *fb)
136 fb->_Status = 0; /* "indeterminate" */
141 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
142 * gl_renderbuffer_attachment object.
143 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
144 * the depth buffer attachment point.
146 struct gl_renderbuffer_attachment *
147 _mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
152 switch (attachment) {
153 case GL_COLOR_ATTACHMENT0_EXT:
154 case GL_COLOR_ATTACHMENT1_EXT:
155 case GL_COLOR_ATTACHMENT2_EXT:
156 case GL_COLOR_ATTACHMENT3_EXT:
157 case GL_COLOR_ATTACHMENT4_EXT:
158 case GL_COLOR_ATTACHMENT5_EXT:
159 case GL_COLOR_ATTACHMENT6_EXT:
160 case GL_COLOR_ATTACHMENT7_EXT:
161 case GL_COLOR_ATTACHMENT8_EXT:
162 case GL_COLOR_ATTACHMENT9_EXT:
163 case GL_COLOR_ATTACHMENT10_EXT:
164 case GL_COLOR_ATTACHMENT11_EXT:
165 case GL_COLOR_ATTACHMENT12_EXT:
166 case GL_COLOR_ATTACHMENT13_EXT:
167 case GL_COLOR_ATTACHMENT14_EXT:
168 case GL_COLOR_ATTACHMENT15_EXT:
169 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
170 if (i >= ctx->Const.MaxColorAttachments) {
173 return &fb->Attachment[BUFFER_COLOR0 + i];
174 case GL_DEPTH_STENCIL_ATTACHMENT:
176 case GL_DEPTH_ATTACHMENT_EXT:
177 return &fb->Attachment[BUFFER_DEPTH];
178 case GL_STENCIL_ATTACHMENT_EXT:
179 return &fb->Attachment[BUFFER_STENCIL];
187 * Remove any texture or renderbuffer attached to the given attachment
188 * point. Update reference counts, etc.
191 _mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
193 if (att->Type == GL_TEXTURE) {
194 ASSERT(att->Texture);
195 if (ctx->Driver.FinishRenderTexture) {
196 /* tell driver that we're done rendering to this texture. */
197 ctx->Driver.FinishRenderTexture(ctx, att);
199 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
200 ASSERT(!att->Texture);
202 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
203 ASSERT(!att->Texture);
204 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
205 ASSERT(!att->Renderbuffer);
208 att->Complete = GL_TRUE;
213 * Bind a texture object to an attachment point.
214 * The previous binding, if any, will be removed first.
217 _mesa_set_texture_attachment(GLcontext *ctx,
218 struct gl_framebuffer *fb,
219 struct gl_renderbuffer_attachment *att,
220 struct gl_texture_object *texObj,
221 GLenum texTarget, GLuint level, GLuint zoffset)
223 if (att->Texture == texObj) {
224 /* re-attaching same texture */
225 ASSERT(att->Type == GL_TEXTURE);
229 _mesa_remove_attachment(ctx, att);
230 att->Type = GL_TEXTURE;
231 assert(!att->Texture);
232 _mesa_reference_texobj(&att->Texture, texObj);
235 /* always update these fields */
236 att->TextureLevel = level;
237 if (IS_CUBE_FACE(texTarget)) {
238 att->CubeMapFace = texTarget - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
241 att->CubeMapFace = 0;
243 att->Zoffset = zoffset;
244 att->Complete = GL_FALSE;
246 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
247 ctx->Driver.RenderTexture(ctx, fb, att);
250 invalidate_framebuffer(fb);
255 * Bind a renderbuffer to an attachment point.
256 * The previous binding, if any, will be removed first.
259 _mesa_set_renderbuffer_attachment(GLcontext *ctx,
260 struct gl_renderbuffer_attachment *att,
261 struct gl_renderbuffer *rb)
263 /* XXX check if re-doing same attachment, exit early */
264 _mesa_remove_attachment(ctx, att);
265 att->Type = GL_RENDERBUFFER_EXT;
266 att->Texture = NULL; /* just to be safe */
267 att->Complete = GL_FALSE;
268 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
273 * Fallback for ctx->Driver.FramebufferRenderbuffer()
274 * Attach a renderbuffer object to a framebuffer object.
277 _mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
278 GLenum attachment, struct gl_renderbuffer *rb)
280 struct gl_renderbuffer_attachment *att;
282 _glthread_LOCK_MUTEX(fb->Mutex);
284 att = _mesa_get_attachment(ctx, fb, attachment);
287 _mesa_set_renderbuffer_attachment(ctx, att, rb);
288 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
289 /* do stencil attachment here (depth already done above) */
290 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
292 _mesa_set_renderbuffer_attachment(ctx, att, rb);
296 _mesa_remove_attachment(ctx, att);
299 invalidate_framebuffer(fb);
301 _glthread_UNLOCK_MUTEX(fb->Mutex);
309 att_incomplete(const char *msg)
312 _mesa_printf("attachment incomplete: %s\n", msg);
320 * Test if an attachment point is complete and update its Complete field.
321 * \param format if GL_COLOR, this is a color attachment point,
322 * if GL_DEPTH, this is a depth component attachment point,
323 * if GL_STENCIL, this is a stencil component attachment point.
326 test_attachment_completeness(const GLcontext *ctx, GLenum format,
327 struct gl_renderbuffer_attachment *att)
329 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
331 /* assume complete */
332 att->Complete = GL_TRUE;
334 /* Look for reasons why the attachment might be incomplete */
335 if (att->Type == GL_TEXTURE) {
336 const struct gl_texture_object *texObj = att->Texture;
337 struct gl_texture_image *texImage;
340 att_incomplete("no texobj");
341 att->Complete = GL_FALSE;
345 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
347 att_incomplete("no teximage");
348 att->Complete = GL_FALSE;
351 if (texImage->Width < 1 || texImage->Height < 1) {
352 att_incomplete("teximage width/height=0");
353 _mesa_printf("texobj = %u\n", texObj->Name);
354 _mesa_printf("level = %d\n", att->TextureLevel);
355 att->Complete = GL_FALSE;
358 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
359 att_incomplete("bad z offset");
360 att->Complete = GL_FALSE;
364 if (format == GL_COLOR) {
365 if (texImage->TexFormat->BaseFormat != GL_RGB &&
366 texImage->TexFormat->BaseFormat != GL_RGBA) {
367 att_incomplete("bad format");
368 att->Complete = GL_FALSE;
371 if (texImage->TexFormat->TexelBytes == 0) {
372 att_incomplete("compressed internalformat");
373 att->Complete = GL_FALSE;
377 else if (format == GL_DEPTH) {
378 if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
381 else if (ctx->Extensions.EXT_packed_depth_stencil &&
382 ctx->Extensions.ARB_depth_texture &&
383 texImage->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT) {
387 att->Complete = GL_FALSE;
388 att_incomplete("bad depth format");
393 ASSERT(format == GL_STENCIL);
394 ASSERT(att->Renderbuffer->StencilBits);
395 if (ctx->Extensions.EXT_packed_depth_stencil &&
396 ctx->Extensions.ARB_depth_texture &&
397 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
401 /* no such thing as stencil-only textures */
402 att_incomplete("illegal stencil texture");
403 att->Complete = GL_FALSE;
408 else if (att->Type == GL_RENDERBUFFER_EXT) {
409 ASSERT(att->Renderbuffer);
410 if (!att->Renderbuffer->InternalFormat ||
411 att->Renderbuffer->Width < 1 ||
412 att->Renderbuffer->Height < 1) {
413 att_incomplete("0x0 renderbuffer");
414 att->Complete = GL_FALSE;
417 if (format == GL_COLOR) {
418 if (att->Renderbuffer->_BaseFormat != GL_RGB &&
419 att->Renderbuffer->_BaseFormat != GL_RGBA) {
420 ASSERT(att->Renderbuffer->RedBits);
421 ASSERT(att->Renderbuffer->GreenBits);
422 ASSERT(att->Renderbuffer->BlueBits);
423 att_incomplete("bad renderbuffer color format");
424 att->Complete = GL_FALSE;
428 else if (format == GL_DEPTH) {
429 ASSERT(att->Renderbuffer->DepthBits);
430 if (att->Renderbuffer->_BaseFormat == GL_DEPTH_COMPONENT) {
433 else if (ctx->Extensions.EXT_packed_depth_stencil &&
434 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
438 att_incomplete("bad renderbuffer depth format");
439 att->Complete = GL_FALSE;
444 assert(format == GL_STENCIL);
445 ASSERT(att->Renderbuffer->StencilBits);
446 if (att->Renderbuffer->_BaseFormat == GL_STENCIL_INDEX) {
449 else if (ctx->Extensions.EXT_packed_depth_stencil &&
450 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
454 att->Complete = GL_FALSE;
455 att_incomplete("bad renderbuffer stencil format");
461 ASSERT(att->Type == GL_NONE);
469 * Helpful for debugging
472 fbo_incomplete(const char *msg, int index)
477 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
483 * Test if the given framebuffer object is complete and update its
484 * Status field with the results.
485 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
486 * driver to make hardware-specific validation/completeness checks.
487 * Also update the framebuffer's Width and Height fields if the
488 * framebuffer is complete.
491 _mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
494 GLenum intFormat = GL_NONE; /* color buffers' internal format */
495 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
496 GLint numSamples = -1;
500 assert(fb->Name != 0);
506 /* Start at -2 to more easily loop over all attachment points.
511 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
512 struct gl_renderbuffer_attachment *att;
516 * XXX for ARB_fbo, only check color buffers that are named by
517 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
520 /* check for attachment completeness
523 att = &fb->Attachment[BUFFER_DEPTH];
524 test_attachment_completeness(ctx, GL_DEPTH, att);
525 if (!att->Complete) {
526 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
527 fbo_incomplete("depth attachment incomplete", -1);
532 att = &fb->Attachment[BUFFER_STENCIL];
533 test_attachment_completeness(ctx, GL_STENCIL, att);
534 if (!att->Complete) {
535 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
536 fbo_incomplete("stencil attachment incomplete", -1);
541 att = &fb->Attachment[BUFFER_COLOR0 + i];
542 test_attachment_completeness(ctx, GL_COLOR, att);
543 if (!att->Complete) {
544 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
545 fbo_incomplete("color attachment incomplete", i);
550 /* get width, height, format of the renderbuffer/texture
552 if (att->Type == GL_TEXTURE) {
553 const struct gl_texture_image *texImg
554 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
555 minWidth = MIN2(minWidth, texImg->Width);
556 maxWidth = MAX2(maxWidth, texImg->Width);
557 minHeight = MIN2(minHeight, texImg->Height);
558 maxHeight = MAX2(maxHeight, texImg->Height);
559 f = texImg->_BaseFormat;
561 if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
562 && f != GL_DEPTH_STENCIL_EXT) {
563 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
564 fbo_incomplete("texture attachment incomplete", -1);
568 else if (att->Type == GL_RENDERBUFFER_EXT) {
569 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
570 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
571 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
572 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
573 f = att->Renderbuffer->InternalFormat;
577 assert(att->Type == GL_NONE);
581 if (numSamples < 0) {
583 numSamples = att->Renderbuffer->NumSamples;
586 /* Error-check width, height, format, samples
588 if (numImages == 1) {
589 /* save format, num samples */
595 if (!ctx->Extensions.ARB_framebuffer_object) {
596 /* check that width, height, format are same */
597 if (minWidth != maxWidth || minHeight != maxHeight) {
598 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
599 fbo_incomplete("width or height mismatch", -1);
602 /* check that all color buffer have same format */
603 if (intFormat != GL_NONE && f != intFormat) {
604 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
605 fbo_incomplete("format mismatch", -1);
609 if (att->Renderbuffer &&
610 att->Renderbuffer->NumSamples != numSamples) {
611 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
612 fbo_incomplete("inconsistant number of samples", i);
619 #ifndef FEATURE_OES_framebuffer_object
620 /* Check that all DrawBuffers are present */
621 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
622 if (fb->ColorDrawBuffer[j] != GL_NONE) {
623 const struct gl_renderbuffer_attachment *att
624 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
626 if (att->Type == GL_NONE) {
627 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
628 fbo_incomplete("missing drawbuffer", j);
634 /* Check that the ReadBuffer is present */
635 if (fb->ColorReadBuffer != GL_NONE) {
636 const struct gl_renderbuffer_attachment *att
637 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
639 if (att->Type == GL_NONE) {
640 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
641 fbo_incomplete("missing readbuffer", -1);
647 if (numImages == 0) {
648 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
649 fbo_incomplete("no attachments", -1);
653 /* Provisionally set status = COMPLETE ... */
654 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
656 /* ... but the driver may say the FB is incomplete.
657 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
660 if (ctx->Driver.ValidateFramebuffer) {
661 ctx->Driver.ValidateFramebuffer(ctx, fb);
662 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
663 fbo_incomplete("driver marked FBO as incomplete", -1);
667 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
669 * Note that if ARB_framebuffer_object is supported and the attached
670 * renderbuffers/textures are different sizes, the framebuffer
671 * width/height will be set to the smallest width/height.
673 fb->Width = minWidth;
674 fb->Height = minHeight;
676 /* finally, update the visual info for the framebuffer */
677 _mesa_update_framebuffer_visual(fb);
683 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
685 GET_CURRENT_CONTEXT(ctx);
686 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
688 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
689 if (rb != NULL && rb != &DummyRenderbuffer)
697 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
699 struct gl_renderbuffer *newRb;
700 GET_CURRENT_CONTEXT(ctx);
702 ASSERT_OUTSIDE_BEGIN_END(ctx);
704 if (target != GL_RENDERBUFFER_EXT) {
705 _mesa_error(ctx, GL_INVALID_ENUM,
706 "glBindRenderbufferEXT(target)");
710 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
711 /* The above doesn't fully flush the drivers in the way that a
712 * glFlush does, but that is required here:
714 if (ctx->Driver.Flush)
715 ctx->Driver.Flush(ctx);
719 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
720 if (newRb == &DummyRenderbuffer) {
721 /* ID was reserved, but no real renderbuffer object made yet */
724 else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
725 /* All RB IDs must be Gen'd */
726 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
731 /* create new renderbuffer object */
732 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
734 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
737 ASSERT(newRb->AllocStorage);
738 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
739 newRb->RefCount = 1; /* referenced by hash table */
746 ASSERT(newRb != &DummyRenderbuffer);
748 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
753 * If the given renderbuffer is anywhere attached to the framebuffer, detach
755 * This is used when a renderbuffer object is deleted.
756 * The spec calls for unbinding.
759 detach_renderbuffer(GLcontext *ctx,
760 struct gl_framebuffer *fb,
761 struct gl_renderbuffer *rb)
764 for (i = 0; i < BUFFER_COUNT; i++) {
765 if (fb->Attachment[i].Renderbuffer == rb) {
766 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
769 invalidate_framebuffer(fb);
774 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
777 GET_CURRENT_CONTEXT(ctx);
779 ASSERT_OUTSIDE_BEGIN_END(ctx);
780 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
782 for (i = 0; i < n; i++) {
783 if (renderbuffers[i] > 0) {
784 struct gl_renderbuffer *rb;
785 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
787 /* check if deleting currently bound renderbuffer object */
788 if (rb == ctx->CurrentRenderbuffer) {
790 ASSERT(rb->RefCount >= 2);
791 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
794 if (ctx->DrawBuffer->Name) {
795 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
797 if (ctx->ReadBuffer->Name && ctx->ReadBuffer != ctx->DrawBuffer) {
798 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
801 /* Remove from hash table immediately, to free the ID.
802 * But the object will not be freed until it's no longer
803 * referenced anywhere else.
805 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
807 if (rb != &DummyRenderbuffer) {
808 /* no longer referenced by hash table */
809 _mesa_reference_renderbuffer(&rb, NULL);
818 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
820 GET_CURRENT_CONTEXT(ctx);
824 ASSERT_OUTSIDE_BEGIN_END(ctx);
827 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
834 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
836 for (i = 0; i < n; i++) {
837 GLuint name = first + i;
838 renderbuffers[i] = name;
839 /* insert dummy placeholder into hash table */
840 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
841 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
842 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
848 * Given an internal format token for a render buffer, return the
849 * corresponding base format.
850 * This is very similar to _mesa_base_tex_format() but the set of valid
851 * internal formats is somewhat different.
853 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
854 * GL_DEPTH_STENCIL_EXT or zero if error.
857 _mesa_base_fbo_format(GLcontext *ctx, GLenum internalFormat)
859 switch (internalFormat) {
878 case GL_STENCIL_INDEX:
879 case GL_STENCIL_INDEX1_EXT:
880 case GL_STENCIL_INDEX4_EXT:
881 case GL_STENCIL_INDEX8_EXT:
882 case GL_STENCIL_INDEX16_EXT:
883 return GL_STENCIL_INDEX;
884 case GL_DEPTH_COMPONENT:
885 case GL_DEPTH_COMPONENT16:
886 case GL_DEPTH_COMPONENT24:
887 case GL_DEPTH_COMPONENT32:
888 return GL_DEPTH_COMPONENT;
889 case GL_DEPTH_STENCIL_EXT:
890 case GL_DEPTH24_STENCIL8_EXT:
891 if (ctx->Extensions.EXT_packed_depth_stencil)
892 return GL_DEPTH_STENCIL_EXT;
895 /* XXX add floating point formats eventually */
902 /** sentinal value, see below */
903 #define NO_SAMPLES 1000
907 * Helper function used by _mesa_RenderbufferStorageEXT() and
908 * _mesa_RenderbufferStorageMultisample().
909 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
912 renderbuffer_storage(GLenum target, GLenum internalFormat,
913 GLsizei width, GLsizei height, GLsizei samples)
915 const char *func = samples == NO_SAMPLES ?
916 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
917 struct gl_renderbuffer *rb;
919 GET_CURRENT_CONTEXT(ctx);
921 ASSERT_OUTSIDE_BEGIN_END(ctx);
923 if (target != GL_RENDERBUFFER_EXT) {
924 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
928 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
929 if (baseFormat == 0) {
930 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
934 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
935 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
939 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
940 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
944 if (samples == NO_SAMPLES) {
945 /* NumSamples == 0 indicates non-multisampling */
948 else if (samples > ctx->Const.MaxSamples) {
949 /* note: driver may choose to use more samples than what's requested */
950 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
954 rb = ctx->CurrentRenderbuffer;
956 _mesa_error(ctx, GL_INVALID_OPERATION, func);
960 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
962 if (rb->InternalFormat == internalFormat &&
963 rb->Width == (GLuint) width &&
964 rb->Height == (GLuint) height) {
965 /* no change in allocation needed */
969 /* These MUST get set by the AllocStorage func */
970 rb->_ActualFormat = 0;
978 rb->NumSamples = samples;
980 /* Now allocate the storage */
981 ASSERT(rb->AllocStorage);
982 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
983 /* No error - check/set fields now */
984 assert(rb->_ActualFormat);
985 assert(rb->Width == (GLuint) width);
986 assert(rb->Height == (GLuint) height);
987 assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
988 rb->DepthBits || rb->StencilBits || rb->IndexBits);
989 rb->InternalFormat = internalFormat;
990 rb->_BaseFormat = baseFormat;
993 /* Probably ran out of memory - clear the fields */
996 rb->InternalFormat = GL_NONE;
997 rb->_ActualFormat = GL_NONE;
998 rb->_BaseFormat = GL_NONE;
1010 test_framebuffer_completeness(ctx, fb);
1012 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1019 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1020 GLsizei width, GLsizei height)
1022 /* GL_ARB_fbo says calling this function is equivalent to calling
1023 * glRenderbufferStorageMultisample() with samples=0. We pass in
1024 * a token value here just for error reporting purposes.
1026 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1031 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1032 GLenum internalFormat,
1033 GLsizei width, GLsizei height)
1035 renderbuffer_storage(target, internalFormat, width, height, samples);
1041 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1043 struct gl_renderbuffer *rb;
1044 GET_CURRENT_CONTEXT(ctx);
1046 ASSERT_OUTSIDE_BEGIN_END(ctx);
1048 if (target != GL_RENDERBUFFER_EXT) {
1049 _mesa_error(ctx, GL_INVALID_ENUM,
1050 "glGetRenderbufferParameterivEXT(target)");
1054 rb = ctx->CurrentRenderbuffer;
1056 _mesa_error(ctx, GL_INVALID_OPERATION,
1057 "glGetRenderbufferParameterivEXT");
1061 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1064 case GL_RENDERBUFFER_WIDTH_EXT:
1065 *params = rb->Width;
1067 case GL_RENDERBUFFER_HEIGHT_EXT:
1068 *params = rb->Height;
1070 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1071 *params = rb->InternalFormat;
1073 case GL_RENDERBUFFER_RED_SIZE_EXT:
1074 *params = rb->RedBits;
1076 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1077 *params = rb->GreenBits;
1079 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1080 *params = rb->BlueBits;
1082 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1083 *params = rb->AlphaBits;
1085 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1086 *params = rb->DepthBits;
1088 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1089 *params = rb->StencilBits;
1091 case GL_RENDERBUFFER_SAMPLES:
1092 if (ctx->Extensions.ARB_framebuffer_object) {
1093 *params = rb->NumSamples;
1098 _mesa_error(ctx, GL_INVALID_ENUM,
1099 "glGetRenderbufferParameterivEXT(target)");
1105 GLboolean GLAPIENTRY
1106 _mesa_IsFramebufferEXT(GLuint framebuffer)
1108 GET_CURRENT_CONTEXT(ctx);
1109 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1111 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1112 if (rb != NULL && rb != &DummyFramebuffer)
1120 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1123 ASSERT(ctx->Driver.RenderTexture);
1124 for (i = 0; i < BUFFER_COUNT; i++) {
1125 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1126 struct gl_texture_object *texObj = att->Texture;
1128 && att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
1129 ctx->Driver.RenderTexture(ctx, fb, att);
1136 * Examine all the framebuffer's attachments to see if any are textures.
1137 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1138 * notify the device driver that the texture image may have changed.
1141 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1143 if (ctx->Driver.FinishRenderTexture) {
1145 for (i = 0; i < BUFFER_COUNT; i++) {
1146 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1147 if (att->Texture && att->Renderbuffer) {
1148 ctx->Driver.FinishRenderTexture(ctx, att);
1156 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1158 struct gl_framebuffer *newFb, *newFbread;
1159 GLboolean bindReadBuf, bindDrawBuf;
1160 GET_CURRENT_CONTEXT(ctx);
1163 if (ctx->Extensions.ARB_framebuffer_object) {
1164 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1165 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1169 ASSERT_OUTSIDE_BEGIN_END(ctx);
1171 if (!ctx->Extensions.EXT_framebuffer_object) {
1172 _mesa_error(ctx, GL_INVALID_OPERATION,
1173 "glBindFramebufferEXT(unsupported)");
1178 #if FEATURE_EXT_framebuffer_blit
1179 case GL_DRAW_FRAMEBUFFER_EXT:
1180 if (!ctx->Extensions.EXT_framebuffer_blit) {
1181 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1184 bindDrawBuf = GL_TRUE;
1185 bindReadBuf = GL_FALSE;
1187 case GL_READ_FRAMEBUFFER_EXT:
1188 if (!ctx->Extensions.EXT_framebuffer_blit) {
1189 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1192 bindDrawBuf = GL_FALSE;
1193 bindReadBuf = GL_TRUE;
1196 case GL_FRAMEBUFFER_EXT:
1197 bindDrawBuf = GL_TRUE;
1198 bindReadBuf = GL_TRUE;
1201 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1205 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1206 if (ctx->Driver.Flush) {
1207 ctx->Driver.Flush(ctx);
1211 /* Binding a user-created framebuffer object */
1212 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1213 if (newFb == &DummyFramebuffer) {
1214 /* ID was reserved, but no real framebuffer object made yet */
1217 else if (!newFb && ctx->Extensions.ARB_framebuffer_object) {
1218 /* All FBO IDs must be Gen'd */
1219 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1224 /* create new framebuffer object */
1225 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1227 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1230 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1235 /* Binding the window system framebuffer (which was originally set
1236 * with MakeCurrent).
1238 newFb = ctx->WinSysDrawBuffer;
1239 newFbread = ctx->WinSysReadBuffer;
1243 ASSERT(newFb != &DummyFramebuffer);
1246 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1250 if (ctx->ReadBuffer == newFbread)
1251 bindReadBuf = GL_FALSE; /* no change */
1253 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1257 /* check if old FB had any texture attachments */
1258 if (ctx->DrawBuffer->Name != 0) {
1259 check_end_texture_render(ctx, ctx->DrawBuffer);
1262 if (ctx->DrawBuffer == newFb)
1263 bindDrawBuf = GL_FALSE; /* no change */
1265 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1267 if (newFb->Name != 0) {
1268 /* check if newly bound framebuffer has any texture attachments */
1269 check_begin_texture_render(ctx, newFb);
1273 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1274 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1280 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1283 GET_CURRENT_CONTEXT(ctx);
1285 ASSERT_OUTSIDE_BEGIN_END(ctx);
1286 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1287 /* The above doesn't fully flush the drivers in the way that a
1288 * glFlush does, but that is required here:
1290 if (ctx->Driver.Flush)
1291 ctx->Driver.Flush(ctx);
1293 for (i = 0; i < n; i++) {
1294 if (framebuffers[i] > 0) {
1295 struct gl_framebuffer *fb;
1296 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1298 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1300 /* check if deleting currently bound framebuffer object */
1301 if (fb == ctx->DrawBuffer) {
1303 ASSERT(fb->RefCount >= 2);
1304 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1306 if (fb == ctx->ReadBuffer) {
1308 ASSERT(fb->RefCount >= 2);
1309 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1312 /* remove from hash table immediately, to free the ID */
1313 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1315 if (fb != &DummyFramebuffer) {
1316 /* But the object will not be freed until it's no longer
1317 * bound in any context.
1319 _mesa_reference_framebuffer(&fb, NULL);
1328 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1330 GET_CURRENT_CONTEXT(ctx);
1334 ASSERT_OUTSIDE_BEGIN_END(ctx);
1337 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1344 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1346 for (i = 0; i < n; i++) {
1347 GLuint name = first + i;
1348 framebuffers[i] = name;
1349 /* insert dummy placeholder into hash table */
1350 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1351 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1352 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1359 _mesa_CheckFramebufferStatusEXT(GLenum target)
1361 struct gl_framebuffer *buffer;
1362 GET_CURRENT_CONTEXT(ctx);
1364 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1367 #if FEATURE_EXT_framebuffer_blit
1368 case GL_DRAW_FRAMEBUFFER_EXT:
1369 if (!ctx->Extensions.EXT_framebuffer_blit) {
1370 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1373 buffer = ctx->DrawBuffer;
1375 case GL_READ_FRAMEBUFFER_EXT:
1376 if (!ctx->Extensions.EXT_framebuffer_blit) {
1377 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1380 buffer = ctx->ReadBuffer;
1383 case GL_FRAMEBUFFER_EXT:
1384 buffer = ctx->DrawBuffer;
1387 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1388 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1391 if (buffer->Name == 0) {
1392 /* The window system / default framebuffer is always complete */
1393 return GL_FRAMEBUFFER_COMPLETE_EXT;
1396 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1398 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1399 _mesa_test_framebuffer_completeness(ctx, buffer);
1402 return buffer->_Status;
1408 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1411 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1412 GLenum attachment, GLenum textarget, GLuint texture,
1413 GLint level, GLint zoffset)
1415 struct gl_renderbuffer_attachment *att;
1416 struct gl_texture_object *texObj = NULL;
1417 struct gl_framebuffer *fb;
1418 GLboolean error = GL_FALSE;
1420 ASSERT_OUTSIDE_BEGIN_END(ctx);
1423 case GL_READ_FRAMEBUFFER_EXT:
1424 error = !ctx->Extensions.EXT_framebuffer_blit;
1425 fb = ctx->ReadBuffer;
1427 case GL_DRAW_FRAMEBUFFER_EXT:
1428 error = !ctx->Extensions.EXT_framebuffer_blit;
1430 case GL_FRAMEBUFFER_EXT:
1431 fb = ctx->DrawBuffer;
1438 _mesa_error(ctx, GL_INVALID_ENUM,
1439 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1445 /* check framebuffer binding */
1446 if (fb->Name == 0) {
1447 _mesa_error(ctx, GL_INVALID_OPERATION,
1448 "glFramebufferTexture%sEXT", caller);
1453 /* The textarget, level, and zoffset parameters are only validated if
1454 * texture is non-zero.
1457 GLboolean err = GL_TRUE;
1459 texObj = _mesa_lookup_texture(ctx, texture);
1460 if (texObj != NULL) {
1461 if (textarget == 0) {
1462 err = (texObj->Target != GL_TEXTURE_3D) &&
1463 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1464 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1467 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1468 ? !IS_CUBE_FACE(textarget)
1469 : (texObj->Target != textarget);
1474 _mesa_error(ctx, GL_INVALID_OPERATION,
1475 "glFramebufferTexture%sEXT(texture target mismatch)",
1480 if (texObj->Target == GL_TEXTURE_3D) {
1481 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1482 if (zoffset < 0 || zoffset >= maxSize) {
1483 _mesa_error(ctx, GL_INVALID_VALUE,
1484 "glFramebufferTexture%sEXT(zoffset)", caller);
1488 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1489 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1490 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1491 _mesa_error(ctx, GL_INVALID_VALUE,
1492 "glFramebufferTexture%sEXT(layer)", caller);
1498 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1499 _mesa_error(ctx, GL_INVALID_VALUE,
1500 "glFramebufferTexture%sEXT(level)", caller);
1505 att = _mesa_get_attachment(ctx, fb, attachment);
1507 _mesa_error(ctx, GL_INVALID_ENUM,
1508 "glFramebufferTexture%sEXT(attachment)", caller);
1512 if (texObj && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1513 /* the texture format must be depth+stencil */
1514 const struct gl_texture_image *texImg;
1515 texImg = texObj->Image[0][texObj->BaseLevel];
1516 if (!texImg || texImg->_BaseFormat != GL_DEPTH_STENCIL) {
1517 _mesa_error(ctx, GL_INVALID_OPERATION,
1518 "glFramebufferTexture%sEXT(texture is not"
1519 " DEPTH_STENCIL format)", caller);
1524 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1525 /* The above doesn't fully flush the drivers in the way that a
1526 * glFlush does, but that is required here:
1528 if (ctx->Driver.Flush)
1529 ctx->Driver.Flush(ctx);
1531 _glthread_LOCK_MUTEX(fb->Mutex);
1533 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1535 /* Set the render-to-texture flag. We'll check this flag in
1536 * glTexImage() and friends to determine if we need to revalidate
1537 * any FBOs that might be rendering into this texture.
1538 * This flag never gets cleared since it's non-trivial to determine
1539 * when all FBOs might be done rendering to this texture. That's OK
1540 * though since it's uncommon to render to a texture then repeatedly
1541 * call glTexImage() to change images in the texture.
1543 texObj->_RenderToTexture = GL_TRUE;
1546 _mesa_remove_attachment(ctx, att);
1549 invalidate_framebuffer(fb);
1551 _glthread_UNLOCK_MUTEX(fb->Mutex);
1557 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1558 GLenum textarget, GLuint texture, GLint level)
1560 GET_CURRENT_CONTEXT(ctx);
1562 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1563 _mesa_error(ctx, GL_INVALID_ENUM,
1564 "glFramebufferTexture1DEXT(textarget)");
1568 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1574 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1575 GLenum textarget, GLuint texture, GLint level)
1577 GET_CURRENT_CONTEXT(ctx);
1579 if ((texture != 0) &&
1580 (textarget != GL_TEXTURE_2D) &&
1581 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1582 (!IS_CUBE_FACE(textarget))) {
1583 _mesa_error(ctx, GL_INVALID_OPERATION,
1584 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1588 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1594 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1595 GLenum textarget, GLuint texture,
1596 GLint level, GLint zoffset)
1598 GET_CURRENT_CONTEXT(ctx);
1600 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1601 _mesa_error(ctx, GL_INVALID_ENUM,
1602 "glFramebufferTexture3DEXT(textarget)");
1606 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1612 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1613 GLuint texture, GLint level, GLint layer)
1615 GET_CURRENT_CONTEXT(ctx);
1617 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1623 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1624 GLenum renderbufferTarget,
1625 GLuint renderbuffer)
1627 struct gl_renderbuffer_attachment *att;
1628 struct gl_framebuffer *fb;
1629 struct gl_renderbuffer *rb;
1630 GET_CURRENT_CONTEXT(ctx);
1632 ASSERT_OUTSIDE_BEGIN_END(ctx);
1635 #if FEATURE_EXT_framebuffer_blit
1636 case GL_DRAW_FRAMEBUFFER_EXT:
1637 if (!ctx->Extensions.EXT_framebuffer_blit) {
1638 _mesa_error(ctx, GL_INVALID_ENUM,
1639 "glFramebufferRenderbufferEXT(target)");
1642 fb = ctx->DrawBuffer;
1644 case GL_READ_FRAMEBUFFER_EXT:
1645 if (!ctx->Extensions.EXT_framebuffer_blit) {
1646 _mesa_error(ctx, GL_INVALID_ENUM,
1647 "glFramebufferRenderbufferEXT(target)");
1650 fb = ctx->ReadBuffer;
1653 case GL_FRAMEBUFFER_EXT:
1654 fb = ctx->DrawBuffer;
1657 _mesa_error(ctx, GL_INVALID_ENUM,
1658 "glFramebufferRenderbufferEXT(target)");
1662 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1663 _mesa_error(ctx, GL_INVALID_ENUM,
1664 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1668 if (fb->Name == 0) {
1669 /* Can't attach new renderbuffers to a window system framebuffer */
1670 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1674 att = _mesa_get_attachment(ctx, fb, attachment);
1676 _mesa_error(ctx, GL_INVALID_ENUM,
1677 "glFramebufferRenderbufferEXT(attachment)");
1682 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1684 _mesa_error(ctx, GL_INVALID_OPERATION,
1685 "glFramebufferRenderbufferEXT(renderbuffer)");
1690 /* remove renderbuffer attachment */
1694 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1695 /* make sure the renderbuffer is a depth/stencil format */
1696 if (rb->_BaseFormat != GL_DEPTH_STENCIL) {
1697 _mesa_error(ctx, GL_INVALID_OPERATION,
1698 "glFramebufferRenderbufferEXT(renderbuffer"
1699 " is not DEPTH_STENCIL format)");
1705 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1706 /* The above doesn't fully flush the drivers in the way that a
1707 * glFlush does, but that is required here:
1709 if (ctx->Driver.Flush)
1710 ctx->Driver.Flush(ctx);
1712 assert(ctx->Driver.FramebufferRenderbuffer);
1713 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1715 /* Some subsequent GL commands may depend on the framebuffer's visual
1716 * after the binding is updated. Update visual info now.
1718 _mesa_update_framebuffer_visual(fb);
1723 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1724 GLenum pname, GLint *params)
1726 const struct gl_renderbuffer_attachment *att;
1727 struct gl_framebuffer *buffer;
1728 GET_CURRENT_CONTEXT(ctx);
1730 ASSERT_OUTSIDE_BEGIN_END(ctx);
1733 #if FEATURE_EXT_framebuffer_blit
1734 case GL_DRAW_FRAMEBUFFER_EXT:
1735 if (!ctx->Extensions.EXT_framebuffer_blit) {
1736 _mesa_error(ctx, GL_INVALID_ENUM,
1737 "glGetFramebufferAttachmentParameterivEXT(target)");
1740 buffer = ctx->DrawBuffer;
1742 case GL_READ_FRAMEBUFFER_EXT:
1743 if (!ctx->Extensions.EXT_framebuffer_blit) {
1744 _mesa_error(ctx, GL_INVALID_ENUM,
1745 "glGetFramebufferAttachmentParameterivEXT(target)");
1748 buffer = ctx->ReadBuffer;
1751 case GL_FRAMEBUFFER_EXT:
1752 buffer = ctx->DrawBuffer;
1755 _mesa_error(ctx, GL_INVALID_ENUM,
1756 "glGetFramebufferAttachmentParameterivEXT(target)");
1760 if (buffer->Name == 0) {
1761 _mesa_error(ctx, GL_INVALID_OPERATION,
1762 "glGetFramebufferAttachmentParameterivEXT");
1766 att = _mesa_get_attachment(ctx, buffer, attachment);
1768 _mesa_error(ctx, GL_INVALID_ENUM,
1769 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1773 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1774 /* the depth and stencil attachments must point to the same buffer */
1775 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1776 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1777 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1778 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1779 _mesa_error(ctx, GL_INVALID_OPERATION,
1780 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1781 " attachments differ)");
1786 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1787 /* The above doesn't fully flush the drivers in the way that a
1788 * glFlush does, but that is required here:
1790 if (ctx->Driver.Flush)
1791 ctx->Driver.Flush(ctx);
1794 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1795 *params = att->Type;
1797 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1798 if (att->Type == GL_RENDERBUFFER_EXT) {
1799 *params = att->Renderbuffer->Name;
1801 else if (att->Type == GL_TEXTURE) {
1802 *params = att->Texture->Name;
1805 _mesa_error(ctx, GL_INVALID_ENUM,
1806 "glGetFramebufferAttachmentParameterivEXT(pname)");
1809 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1810 if (att->Type == GL_TEXTURE) {
1811 *params = att->TextureLevel;
1814 _mesa_error(ctx, GL_INVALID_ENUM,
1815 "glGetFramebufferAttachmentParameterivEXT(pname)");
1818 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1819 if (att->Type == GL_TEXTURE) {
1820 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1821 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1828 _mesa_error(ctx, GL_INVALID_ENUM,
1829 "glGetFramebufferAttachmentParameterivEXT(pname)");
1832 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1833 if (att->Type == GL_TEXTURE) {
1834 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1835 *params = att->Zoffset;
1842 _mesa_error(ctx, GL_INVALID_ENUM,
1843 "glGetFramebufferAttachmentParameterivEXT(pname)");
1846 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1847 if (!ctx->Extensions.ARB_framebuffer_object) {
1848 _mesa_error(ctx, GL_INVALID_ENUM,
1849 "glGetFramebufferAttachmentParameterivEXT(pname)");
1852 *params = att->Renderbuffer->ColorEncoding;
1855 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1856 if (!ctx->Extensions.ARB_framebuffer_object) {
1857 _mesa_error(ctx, GL_INVALID_ENUM,
1858 "glGetFramebufferAttachmentParameterivEXT(pname)");
1862 *params = att->Renderbuffer->ComponentType;
1865 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1866 if (!ctx->Extensions.ARB_framebuffer_object) {
1867 _mesa_error(ctx, GL_INVALID_ENUM,
1868 "glGetFramebufferAttachmentParameterivEXT(pname)");
1871 *params = att->Renderbuffer->RedBits;
1874 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1875 if (!ctx->Extensions.ARB_framebuffer_object) {
1876 _mesa_error(ctx, GL_INVALID_ENUM,
1877 "glGetFramebufferAttachmentParameterivEXT(pname)");
1880 *params = att->Renderbuffer->GreenBits;
1883 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1884 if (!ctx->Extensions.ARB_framebuffer_object) {
1885 _mesa_error(ctx, GL_INVALID_ENUM,
1886 "glGetFramebufferAttachmentParameterivEXT(pname)");
1889 *params = att->Renderbuffer->BlueBits;
1892 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1893 if (!ctx->Extensions.ARB_framebuffer_object) {
1894 _mesa_error(ctx, GL_INVALID_ENUM,
1895 "glGetFramebufferAttachmentParameterivEXT(pname)");
1898 *params = att->Renderbuffer->AlphaBits;
1901 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1902 if (!ctx->Extensions.ARB_framebuffer_object) {
1903 _mesa_error(ctx, GL_INVALID_ENUM,
1904 "glGetFramebufferAttachmentParameterivEXT(pname)");
1907 *params = att->Renderbuffer->DepthBits;
1910 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1911 if (!ctx->Extensions.ARB_framebuffer_object) {
1912 _mesa_error(ctx, GL_INVALID_ENUM,
1913 "glGetFramebufferAttachmentParameterivEXT(pname)");
1916 *params = att->Renderbuffer->StencilBits;
1920 _mesa_error(ctx, GL_INVALID_ENUM,
1921 "glGetFramebufferAttachmentParameterivEXT(pname)");
1928 _mesa_GenerateMipmapEXT(GLenum target)
1930 struct gl_texture_unit *texUnit;
1931 struct gl_texture_object *texObj;
1932 GET_CURRENT_CONTEXT(ctx);
1934 ASSERT_OUTSIDE_BEGIN_END(ctx);
1935 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1941 case GL_TEXTURE_CUBE_MAP:
1942 /* OK, legal value */
1945 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1949 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1950 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1952 _mesa_lock_texture(ctx, texObj);
1953 if (target == GL_TEXTURE_CUBE_MAP) {
1956 for (face = 0; face < 6; face++)
1957 ctx->Driver.GenerateMipmap(ctx,
1958 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
1961 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1963 _mesa_unlock_texture(ctx, texObj);
1967 #if FEATURE_EXT_framebuffer_blit
1969 * Blit rectangular region, optionally from one framebuffer to another.
1971 * Note, if the src buffer is multisampled and the dest is not, this is
1972 * when the samples must be resolved to a single color.
1975 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1976 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1977 GLbitfield mask, GLenum filter)
1979 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
1980 GL_DEPTH_BUFFER_BIT |
1981 GL_STENCIL_BUFFER_BIT);
1982 const struct gl_framebuffer *readFb, *drawFb;
1983 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
1984 GET_CURRENT_CONTEXT(ctx);
1986 ASSERT_OUTSIDE_BEGIN_END(ctx);
1987 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1989 if (ctx->NewState) {
1990 _mesa_update_state(ctx);
1993 readFb = ctx->ReadBuffer;
1994 drawFb = ctx->DrawBuffer;
1996 if (!readFb || !drawFb) {
1997 /* This will normally never happen but someday we may want to
1998 * support MakeCurrent() with no drawables.
2003 /* check for complete framebuffers */
2004 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2005 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2006 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2007 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2011 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2012 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2016 if (mask & ~legalMaskBits) {
2017 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2021 /* depth/stencil must be blitted with nearest filtering */
2022 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2023 && filter != GL_NEAREST) {
2024 _mesa_error(ctx, GL_INVALID_OPERATION,
2025 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2029 /* get color read/draw renderbuffers */
2030 if (mask & GL_COLOR_BUFFER_BIT) {
2031 colorReadRb = readFb->_ColorReadBuffer;
2032 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2035 colorReadRb = colorDrawRb = NULL;
2038 if (mask & GL_STENCIL_BUFFER_BIT) {
2039 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2040 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2043 readRb->StencilBits != drawRb->StencilBits) {
2044 _mesa_error(ctx, GL_INVALID_OPERATION,
2045 "glBlitFramebufferEXT(stencil buffer size mismatch");
2050 if (mask & GL_DEPTH_BUFFER_BIT) {
2051 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2052 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2055 readRb->DepthBits != drawRb->DepthBits) {
2056 _mesa_error(ctx, GL_INVALID_OPERATION,
2057 "glBlitFramebufferEXT(depth buffer size mismatch");
2062 if (readFb->Visual.samples > 0 &&
2063 drawFb->Visual.samples > 0 &&
2064 readFb->Visual.samples != drawFb->Visual.samples) {
2065 _mesa_error(ctx, GL_INVALID_OPERATION,
2066 "glBlitFramebufferEXT(mismatched samples");
2070 /* extra checks for multisample copies... */
2071 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2072 /* src and dest region sizes must be the same */
2073 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2074 srcY1 - srcY0 != dstY1 - dstY0) {
2075 _mesa_error(ctx, GL_INVALID_OPERATION,
2076 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2080 /* color formats must match */
2083 colorReadRb->_ActualFormat != colorDrawRb->_ActualFormat) {
2084 _mesa_error(ctx, GL_INVALID_OPERATION,
2085 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2090 if (!ctx->Extensions.EXT_framebuffer_blit) {
2091 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2095 ASSERT(ctx->Driver.BlitFramebuffer);
2096 ctx->Driver.BlitFramebuffer(ctx,
2097 srcX0, srcY0, srcX1, srcY1,
2098 dstX0, dstY0, dstX1, dstY1,
2101 #endif /* FEATURE_EXT_framebuffer_blit */