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);
306 * Test if an attachment point is complete and update its Complete field.
307 * \param format if GL_COLOR, this is a color attachment point,
308 * if GL_DEPTH, this is a depth component attachment point,
309 * if GL_STENCIL, this is a stencil component attachment point.
312 test_attachment_completeness(const GLcontext *ctx, GLenum format,
313 struct gl_renderbuffer_attachment *att)
315 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
317 /* assume complete */
318 att->Complete = GL_TRUE;
320 /* Look for reasons why the attachment might be incomplete */
321 if (att->Type == GL_TEXTURE) {
322 const struct gl_texture_object *texObj = att->Texture;
323 struct gl_texture_image *texImage;
326 att->Complete = GL_FALSE;
330 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
332 att->Complete = GL_FALSE;
335 if (texImage->Width < 1 || texImage->Height < 1) {
336 att->Complete = GL_FALSE;
339 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
340 att->Complete = GL_FALSE;
344 if (format == GL_COLOR) {
345 if (texImage->TexFormat->BaseFormat != GL_RGB &&
346 texImage->TexFormat->BaseFormat != GL_RGBA) {
347 att->Complete = GL_FALSE;
351 else if (format == GL_DEPTH) {
352 if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
355 else if (ctx->Extensions.EXT_packed_depth_stencil &&
356 texImage->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT) {
360 att->Complete = GL_FALSE;
365 /* no such thing as stencil textures */
366 att->Complete = GL_FALSE;
370 else if (att->Type == GL_RENDERBUFFER_EXT) {
371 ASSERT(att->Renderbuffer);
372 if (!att->Renderbuffer->InternalFormat ||
373 att->Renderbuffer->Width < 1 ||
374 att->Renderbuffer->Height < 1) {
375 att->Complete = GL_FALSE;
378 if (format == GL_COLOR) {
379 if (att->Renderbuffer->_BaseFormat != GL_RGB &&
380 att->Renderbuffer->_BaseFormat != GL_RGBA) {
381 ASSERT(att->Renderbuffer->RedBits);
382 ASSERT(att->Renderbuffer->GreenBits);
383 ASSERT(att->Renderbuffer->BlueBits);
384 att->Complete = GL_FALSE;
388 else if (format == GL_DEPTH) {
389 ASSERT(att->Renderbuffer->DepthBits);
390 if (att->Renderbuffer->_BaseFormat == GL_DEPTH_COMPONENT) {
393 else if (ctx->Extensions.EXT_packed_depth_stencil &&
394 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
398 att->Complete = GL_FALSE;
403 assert(format == GL_STENCIL);
404 ASSERT(att->Renderbuffer->StencilBits);
405 if (att->Renderbuffer->_BaseFormat == GL_STENCIL_INDEX) {
408 else if (ctx->Extensions.EXT_packed_depth_stencil &&
409 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
413 att->Complete = GL_FALSE;
419 ASSERT(att->Type == GL_NONE);
427 * Helpful for debugging
430 fbo_incomplete(const char *msg, int index)
435 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
441 * Test if the given framebuffer object is complete and update its
442 * Status field with the results.
443 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
444 * driver to make hardware-specific validation/completeness checks.
445 * Also update the framebuffer's Width and Height fields if the
446 * framebuffer is complete.
449 _mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
452 GLenum intFormat = GL_NONE; /* color buffers' internal format */
453 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
454 GLint numSamples = -1;
458 assert(fb->Name != 0);
464 /* Start at -2 to more easily loop over all attachment points.
469 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
470 struct gl_renderbuffer_attachment *att;
474 * XXX for ARB_fbo, only check color buffers that are named by
475 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
478 /* check for attachment completeness
481 att = &fb->Attachment[BUFFER_DEPTH];
482 test_attachment_completeness(ctx, GL_DEPTH, att);
483 if (!att->Complete) {
484 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
485 fbo_incomplete("depth attachment incomplete", -1);
490 att = &fb->Attachment[BUFFER_STENCIL];
491 test_attachment_completeness(ctx, GL_STENCIL, att);
492 if (!att->Complete) {
493 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
494 fbo_incomplete("stencil attachment incomplete", -1);
499 att = &fb->Attachment[BUFFER_COLOR0 + i];
500 test_attachment_completeness(ctx, GL_COLOR, att);
501 if (!att->Complete) {
502 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
503 fbo_incomplete("color attachment incomplete", i);
508 /* get width, height, format of the renderbuffer/texture
510 if (att->Type == GL_TEXTURE) {
511 const struct gl_texture_image *texImg
512 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
513 minWidth = MIN2(minWidth, texImg->Width);
514 maxWidth = MAX2(maxWidth, texImg->Width);
515 minHeight = MIN2(minHeight, texImg->Height);
516 maxHeight = MAX2(maxHeight, texImg->Height);
517 f = texImg->_BaseFormat;
519 if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
520 && f != GL_DEPTH_STENCIL_EXT) {
521 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
522 fbo_incomplete("texture attachment incomplete", -1);
526 else if (att->Type == GL_RENDERBUFFER_EXT) {
527 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
528 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
529 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
530 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
531 f = att->Renderbuffer->InternalFormat;
535 assert(att->Type == GL_NONE);
539 if (numSamples < 0) {
541 numSamples = att->Renderbuffer->NumSamples;
544 /* Error-check width, height, format, samples
546 if (numImages == 1) {
547 /* save format, num samples */
553 if (!ctx->Extensions.ARB_framebuffer_object) {
554 /* check that width, height, format are same */
555 if (minWidth != maxWidth || minHeight != maxHeight) {
556 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
557 fbo_incomplete("width or height mismatch", -1);
560 /* check that all color buffer have same format */
561 if (intFormat != GL_NONE && f != intFormat) {
562 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
563 fbo_incomplete("format mismatch", -1);
567 if (att->Renderbuffer &&
568 att->Renderbuffer->NumSamples != numSamples) {
569 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
570 fbo_incomplete("inconsistant number of samples", i);
577 #ifndef FEATURE_OES_framebuffer_object
578 /* Check that all DrawBuffers are present */
579 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
580 if (fb->ColorDrawBuffer[j] != GL_NONE) {
581 const struct gl_renderbuffer_attachment *att
582 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
584 if (att->Type == GL_NONE) {
585 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
586 fbo_incomplete("missing drawbuffer", j);
592 /* Check that the ReadBuffer is present */
593 if (fb->ColorReadBuffer != GL_NONE) {
594 const struct gl_renderbuffer_attachment *att
595 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
597 if (att->Type == GL_NONE) {
598 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
599 fbo_incomplete("missing readbuffer", -1);
605 if (numImages == 0) {
606 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
607 fbo_incomplete("no attachments", -1);
611 /* Provisionally set status = COMPLETE ... */
612 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
614 /* ... but the driver may say the FB is incomplete.
615 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
618 if (ctx->Driver.ValidateFramebuffer) {
619 ctx->Driver.ValidateFramebuffer(ctx, fb);
620 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
621 fbo_incomplete("driver marked FBO as incomplete", -1);
625 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
627 * Note that if ARB_framebuffer_object is supported and the attached
628 * renderbuffers/textures are different sizes, the framebuffer
629 * width/height will be set to the smallest width/height.
631 fb->Width = minWidth;
632 fb->Height = minHeight;
634 /* finally, update the visual info for the framebuffer */
635 _mesa_update_framebuffer_visual(fb);
641 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
643 GET_CURRENT_CONTEXT(ctx);
644 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
646 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
647 if (rb != NULL && rb != &DummyRenderbuffer)
655 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
657 struct gl_renderbuffer *newRb;
658 GET_CURRENT_CONTEXT(ctx);
660 ASSERT_OUTSIDE_BEGIN_END(ctx);
662 if (target != GL_RENDERBUFFER_EXT) {
663 _mesa_error(ctx, GL_INVALID_ENUM,
664 "glBindRenderbufferEXT(target)");
668 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
669 /* The above doesn't fully flush the drivers in the way that a
670 * glFlush does, but that is required here:
672 if (ctx->Driver.Flush)
673 ctx->Driver.Flush(ctx);
677 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
678 if (newRb == &DummyRenderbuffer) {
679 /* ID was reserved, but no real renderbuffer object made yet */
682 else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
683 /* All RB IDs must be Gen'd */
684 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
689 /* create new renderbuffer object */
690 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
692 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
695 ASSERT(newRb->AllocStorage);
696 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
697 newRb->RefCount = 1; /* referenced by hash table */
704 ASSERT(newRb != &DummyRenderbuffer);
706 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
711 * If the given renderbuffer is anywhere attached to the framebuffer, detach
713 * This is used when a renderbuffer object is deleted.
714 * The spec calls for unbinding.
717 detach_renderbuffer(GLcontext *ctx,
718 struct gl_framebuffer *fb,
719 struct gl_renderbuffer *rb)
722 for (i = 0; i < BUFFER_COUNT; i++) {
723 if (fb->Attachment[i].Renderbuffer == rb) {
724 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
727 invalidate_framebuffer(fb);
732 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
735 GET_CURRENT_CONTEXT(ctx);
737 ASSERT_OUTSIDE_BEGIN_END(ctx);
738 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
740 for (i = 0; i < n; i++) {
741 if (renderbuffers[i] > 0) {
742 struct gl_renderbuffer *rb;
743 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
745 /* check if deleting currently bound renderbuffer object */
746 if (rb == ctx->CurrentRenderbuffer) {
748 ASSERT(rb->RefCount >= 2);
749 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
752 if (ctx->DrawBuffer->Name) {
753 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
755 if (ctx->ReadBuffer->Name && ctx->ReadBuffer != ctx->DrawBuffer) {
756 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
759 /* Remove from hash table immediately, to free the ID.
760 * But the object will not be freed until it's no longer
761 * referenced anywhere else.
763 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
765 if (rb != &DummyRenderbuffer) {
766 /* no longer referenced by hash table */
767 _mesa_reference_renderbuffer(&rb, NULL);
776 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
778 GET_CURRENT_CONTEXT(ctx);
782 ASSERT_OUTSIDE_BEGIN_END(ctx);
785 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
792 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
794 for (i = 0; i < n; i++) {
795 GLuint name = first + i;
796 renderbuffers[i] = name;
797 /* insert dummy placeholder into hash table */
798 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
799 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
800 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
806 * Given an internal format token for a render buffer, return the
807 * corresponding base format.
808 * This is very similar to _mesa_base_tex_format() but the set of valid
809 * internal formats is somewhat different.
811 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
812 * GL_DEPTH_STENCIL_EXT or zero if error.
815 _mesa_base_fbo_format(GLcontext *ctx, GLenum internalFormat)
817 switch (internalFormat) {
836 case GL_STENCIL_INDEX:
837 case GL_STENCIL_INDEX1_EXT:
838 case GL_STENCIL_INDEX4_EXT:
839 case GL_STENCIL_INDEX8_EXT:
840 case GL_STENCIL_INDEX16_EXT:
841 return GL_STENCIL_INDEX;
842 case GL_DEPTH_COMPONENT:
843 case GL_DEPTH_COMPONENT16:
844 case GL_DEPTH_COMPONENT24:
845 case GL_DEPTH_COMPONENT32:
846 return GL_DEPTH_COMPONENT;
847 case GL_DEPTH_STENCIL_EXT:
848 case GL_DEPTH24_STENCIL8_EXT:
849 if (ctx->Extensions.EXT_packed_depth_stencil)
850 return GL_DEPTH_STENCIL_EXT;
853 /* XXX add floating point formats eventually */
860 /** sentinal value, see below */
861 #define NO_SAMPLES 1000
865 * Helper function used by _mesa_RenderbufferStorageEXT() and
866 * _mesa_RenderbufferStorageMultisample().
867 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
870 renderbuffer_storage(GLenum target, GLenum internalFormat,
871 GLsizei width, GLsizei height, GLsizei samples)
873 const char *func = samples == NO_SAMPLES ?
874 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
875 struct gl_renderbuffer *rb;
877 GET_CURRENT_CONTEXT(ctx);
879 ASSERT_OUTSIDE_BEGIN_END(ctx);
881 if (target != GL_RENDERBUFFER_EXT) {
882 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
886 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
887 if (baseFormat == 0) {
888 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
892 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
893 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
897 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
898 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
902 if (samples == NO_SAMPLES) {
903 /* NumSamples == 0 indicates non-multisampling */
906 else if (samples > ctx->Const.MaxSamples) {
907 /* note: driver may choose to use more samples than what's requested */
908 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
912 rb = ctx->CurrentRenderbuffer;
914 _mesa_error(ctx, GL_INVALID_OPERATION, func);
918 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
920 if (rb->InternalFormat == internalFormat &&
921 rb->Width == (GLuint) width &&
922 rb->Height == (GLuint) height) {
923 /* no change in allocation needed */
927 /* These MUST get set by the AllocStorage func */
928 rb->_ActualFormat = 0;
936 rb->NumSamples = samples;
938 /* Now allocate the storage */
939 ASSERT(rb->AllocStorage);
940 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
941 /* No error - check/set fields now */
942 assert(rb->_ActualFormat);
943 assert(rb->Width == (GLuint) width);
944 assert(rb->Height == (GLuint) height);
945 assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
946 rb->DepthBits || rb->StencilBits || rb->IndexBits);
947 rb->InternalFormat = internalFormat;
948 rb->_BaseFormat = baseFormat;
951 /* Probably ran out of memory - clear the fields */
954 rb->InternalFormat = GL_NONE;
955 rb->_ActualFormat = GL_NONE;
956 rb->_BaseFormat = GL_NONE;
968 test_framebuffer_completeness(ctx, fb);
970 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
977 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
978 GLsizei width, GLsizei height)
980 /* GL_ARB_fbo says calling this function is equivalent to calling
981 * glRenderbufferStorageMultisample() with samples=0. We pass in
982 * a token value here just for error reporting purposes.
984 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
989 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
990 GLenum internalFormat,
991 GLsizei width, GLsizei height)
993 renderbuffer_storage(target, internalFormat, width, height, samples);
999 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1001 struct gl_renderbuffer *rb;
1002 GET_CURRENT_CONTEXT(ctx);
1004 ASSERT_OUTSIDE_BEGIN_END(ctx);
1006 if (target != GL_RENDERBUFFER_EXT) {
1007 _mesa_error(ctx, GL_INVALID_ENUM,
1008 "glGetRenderbufferParameterivEXT(target)");
1012 rb = ctx->CurrentRenderbuffer;
1014 _mesa_error(ctx, GL_INVALID_OPERATION,
1015 "glGetRenderbufferParameterivEXT");
1019 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1022 case GL_RENDERBUFFER_WIDTH_EXT:
1023 *params = rb->Width;
1025 case GL_RENDERBUFFER_HEIGHT_EXT:
1026 *params = rb->Height;
1028 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1029 *params = rb->InternalFormat;
1031 case GL_RENDERBUFFER_RED_SIZE_EXT:
1032 *params = rb->RedBits;
1034 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1035 *params = rb->GreenBits;
1037 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1038 *params = rb->BlueBits;
1040 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1041 *params = rb->AlphaBits;
1043 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1044 *params = rb->DepthBits;
1046 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1047 *params = rb->StencilBits;
1049 case GL_RENDERBUFFER_SAMPLES:
1050 if (ctx->Extensions.ARB_framebuffer_object) {
1051 *params = rb->NumSamples;
1056 _mesa_error(ctx, GL_INVALID_ENUM,
1057 "glGetRenderbufferParameterivEXT(target)");
1063 GLboolean GLAPIENTRY
1064 _mesa_IsFramebufferEXT(GLuint framebuffer)
1066 GET_CURRENT_CONTEXT(ctx);
1067 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1069 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1070 if (rb != NULL && rb != &DummyFramebuffer)
1078 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1081 ASSERT(ctx->Driver.RenderTexture);
1082 for (i = 0; i < BUFFER_COUNT; i++) {
1083 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1084 struct gl_texture_object *texObj = att->Texture;
1086 && att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
1087 ctx->Driver.RenderTexture(ctx, fb, att);
1094 * Examine all the framebuffer's attachments to see if any are textures.
1095 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1096 * notify the device driver that the texture image may have changed.
1099 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1101 if (ctx->Driver.FinishRenderTexture) {
1103 for (i = 0; i < BUFFER_COUNT; i++) {
1104 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1105 if (att->Texture && att->Renderbuffer) {
1106 ctx->Driver.FinishRenderTexture(ctx, att);
1114 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1116 struct gl_framebuffer *newFb, *newFbread;
1117 GLboolean bindReadBuf, bindDrawBuf;
1118 GET_CURRENT_CONTEXT(ctx);
1121 if (ctx->Extensions.ARB_framebuffer_object) {
1122 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1123 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1127 ASSERT_OUTSIDE_BEGIN_END(ctx);
1129 if (!ctx->Extensions.EXT_framebuffer_object) {
1130 _mesa_error(ctx, GL_INVALID_OPERATION,
1131 "glBindFramebufferEXT(unsupported)");
1136 #if FEATURE_EXT_framebuffer_blit
1137 case GL_DRAW_FRAMEBUFFER_EXT:
1138 if (!ctx->Extensions.EXT_framebuffer_blit) {
1139 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1142 bindDrawBuf = GL_TRUE;
1143 bindReadBuf = GL_FALSE;
1145 case GL_READ_FRAMEBUFFER_EXT:
1146 if (!ctx->Extensions.EXT_framebuffer_blit) {
1147 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1150 bindDrawBuf = GL_FALSE;
1151 bindReadBuf = GL_TRUE;
1154 case GL_FRAMEBUFFER_EXT:
1155 bindDrawBuf = GL_TRUE;
1156 bindReadBuf = GL_TRUE;
1159 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1163 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1165 if (ctx->Driver.Flush) {
1166 ctx->Driver.Flush(ctx);
1170 /* Binding a user-created framebuffer object */
1171 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1172 if (newFb == &DummyFramebuffer) {
1173 /* ID was reserved, but no real framebuffer object made yet */
1176 else if (!newFb && ctx->Extensions.ARB_framebuffer_object) {
1177 /* All FBO IDs must be Gen'd */
1178 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1183 /* create new framebuffer object */
1184 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1186 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1189 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1194 /* Binding the window system framebuffer (which was originally set
1195 * with MakeCurrent).
1197 newFb = ctx->WinSysDrawBuffer;
1198 newFbread = ctx->WinSysReadBuffer;
1202 ASSERT(newFb != &DummyFramebuffer);
1205 * XXX check if re-binding same buffer and skip some of this code.
1209 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1213 /* check if old FB had any texture attachments */
1214 check_end_texture_render(ctx, ctx->DrawBuffer);
1216 /* check if time to delete this framebuffer */
1217 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1219 if (newFb->Name != 0) {
1220 /* check if newly bound framebuffer has any texture attachments */
1221 check_begin_texture_render(ctx, newFb);
1225 if (ctx->Driver.BindFramebuffer) {
1226 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1232 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1235 GET_CURRENT_CONTEXT(ctx);
1237 ASSERT_OUTSIDE_BEGIN_END(ctx);
1238 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1239 /* The above doesn't fully flush the drivers in the way that a
1240 * glFlush does, but that is required here:
1242 if (ctx->Driver.Flush)
1243 ctx->Driver.Flush(ctx);
1245 for (i = 0; i < n; i++) {
1246 if (framebuffers[i] > 0) {
1247 struct gl_framebuffer *fb;
1248 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1250 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1252 /* check if deleting currently bound framebuffer object */
1253 if (fb == ctx->DrawBuffer) {
1255 ASSERT(fb->RefCount >= 2);
1256 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1258 if (fb == ctx->ReadBuffer) {
1260 ASSERT(fb->RefCount >= 2);
1261 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1264 /* remove from hash table immediately, to free the ID */
1265 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1267 if (fb != &DummyFramebuffer) {
1268 /* But the object will not be freed until it's no longer
1269 * bound in any context.
1271 _mesa_reference_framebuffer(&fb, NULL);
1280 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1282 GET_CURRENT_CONTEXT(ctx);
1286 ASSERT_OUTSIDE_BEGIN_END(ctx);
1289 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1296 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1298 for (i = 0; i < n; i++) {
1299 GLuint name = first + i;
1300 framebuffers[i] = name;
1301 /* insert dummy placeholder into hash table */
1302 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1303 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1304 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1311 _mesa_CheckFramebufferStatusEXT(GLenum target)
1313 struct gl_framebuffer *buffer;
1314 GET_CURRENT_CONTEXT(ctx);
1316 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1319 #if FEATURE_EXT_framebuffer_blit
1320 case GL_DRAW_FRAMEBUFFER_EXT:
1321 if (!ctx->Extensions.EXT_framebuffer_blit) {
1322 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1325 buffer = ctx->DrawBuffer;
1327 case GL_READ_FRAMEBUFFER_EXT:
1328 if (!ctx->Extensions.EXT_framebuffer_blit) {
1329 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1332 buffer = ctx->ReadBuffer;
1335 case GL_FRAMEBUFFER_EXT:
1336 buffer = ctx->DrawBuffer;
1339 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1340 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1343 if (buffer->Name == 0) {
1344 /* The window system / default framebuffer is always complete */
1345 return GL_FRAMEBUFFER_COMPLETE_EXT;
1348 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1350 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1351 _mesa_test_framebuffer_completeness(ctx, buffer);
1354 return buffer->_Status;
1360 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1363 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1364 GLenum attachment, GLenum textarget, GLuint texture,
1365 GLint level, GLint zoffset)
1367 struct gl_renderbuffer_attachment *att;
1368 struct gl_texture_object *texObj = NULL;
1369 struct gl_framebuffer *fb;
1370 GLboolean error = GL_FALSE;
1372 ASSERT_OUTSIDE_BEGIN_END(ctx);
1375 case GL_READ_FRAMEBUFFER_EXT:
1376 error = !ctx->Extensions.EXT_framebuffer_blit;
1377 fb = ctx->ReadBuffer;
1379 case GL_DRAW_FRAMEBUFFER_EXT:
1380 error = !ctx->Extensions.EXT_framebuffer_blit;
1382 case GL_FRAMEBUFFER_EXT:
1383 fb = ctx->DrawBuffer;
1390 _mesa_error(ctx, GL_INVALID_ENUM,
1391 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1397 /* check framebuffer binding */
1398 if (fb->Name == 0) {
1399 _mesa_error(ctx, GL_INVALID_OPERATION,
1400 "glFramebufferTexture%sEXT", caller);
1405 /* The textarget, level, and zoffset parameters are only validated if
1406 * texture is non-zero.
1409 GLboolean err = GL_TRUE;
1411 texObj = _mesa_lookup_texture(ctx, texture);
1412 if (texObj != NULL) {
1413 if (textarget == 0) {
1414 err = (texObj->Target != GL_TEXTURE_3D) &&
1415 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1416 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1419 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1420 ? !IS_CUBE_FACE(textarget)
1421 : (texObj->Target != textarget);
1426 _mesa_error(ctx, GL_INVALID_OPERATION,
1427 "glFramebufferTexture%sEXT(texture target mismatch)",
1432 if (texObj->Target == GL_TEXTURE_3D) {
1433 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1434 if (zoffset < 0 || zoffset >= maxSize) {
1435 _mesa_error(ctx, GL_INVALID_VALUE,
1436 "glFramebufferTexture%sEXT(zoffset)", caller);
1440 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1441 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1442 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1443 _mesa_error(ctx, GL_INVALID_VALUE,
1444 "glFramebufferTexture%sEXT(layer)", caller);
1450 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1451 _mesa_error(ctx, GL_INVALID_VALUE,
1452 "glFramebufferTexture%sEXT(level)", caller);
1457 att = _mesa_get_attachment(ctx, fb, attachment);
1459 _mesa_error(ctx, GL_INVALID_ENUM,
1460 "glFramebufferTexture%sEXT(attachment)", caller);
1464 if (texObj && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1465 /* the texture format must be depth+stencil */
1466 const struct gl_texture_image *texImg;
1467 texImg = texObj->Image[0][texObj->BaseLevel];
1468 if (!texImg || texImg->_BaseFormat != GL_DEPTH_STENCIL) {
1469 _mesa_error(ctx, GL_INVALID_OPERATION,
1470 "glFramebufferTexture%sEXT(texture is not"
1471 " DEPTH_STENCIL format)", caller);
1476 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1477 /* The above doesn't fully flush the drivers in the way that a
1478 * glFlush does, but that is required here:
1480 if (ctx->Driver.Flush)
1481 ctx->Driver.Flush(ctx);
1483 _glthread_LOCK_MUTEX(fb->Mutex);
1485 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1487 /* Set the render-to-texture flag. We'll check this flag in
1488 * glTexImage() and friends to determine if we need to revalidate
1489 * any FBOs that might be rendering into this texture.
1490 * This flag never gets cleared since it's non-trivial to determine
1491 * when all FBOs might be done rendering to this texture. That's OK
1492 * though since it's uncommon to render to a texture then repeatedly
1493 * call glTexImage() to change images in the texture.
1495 texObj->_RenderToTexture = GL_TRUE;
1498 _mesa_remove_attachment(ctx, att);
1501 invalidate_framebuffer(fb);
1503 _glthread_UNLOCK_MUTEX(fb->Mutex);
1509 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1510 GLenum textarget, GLuint texture, GLint level)
1512 GET_CURRENT_CONTEXT(ctx);
1514 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1515 _mesa_error(ctx, GL_INVALID_ENUM,
1516 "glFramebufferTexture1DEXT(textarget)");
1520 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1526 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1527 GLenum textarget, GLuint texture, GLint level)
1529 GET_CURRENT_CONTEXT(ctx);
1531 if ((texture != 0) &&
1532 (textarget != GL_TEXTURE_2D) &&
1533 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1534 (!IS_CUBE_FACE(textarget))) {
1535 _mesa_error(ctx, GL_INVALID_OPERATION,
1536 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1540 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1546 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1547 GLenum textarget, GLuint texture,
1548 GLint level, GLint zoffset)
1550 GET_CURRENT_CONTEXT(ctx);
1552 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1553 _mesa_error(ctx, GL_INVALID_ENUM,
1554 "glFramebufferTexture3DEXT(textarget)");
1558 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1564 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1565 GLuint texture, GLint level, GLint layer)
1567 GET_CURRENT_CONTEXT(ctx);
1569 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1575 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1576 GLenum renderbufferTarget,
1577 GLuint renderbuffer)
1579 struct gl_renderbuffer_attachment *att;
1580 struct gl_framebuffer *fb;
1581 struct gl_renderbuffer *rb;
1582 GET_CURRENT_CONTEXT(ctx);
1584 ASSERT_OUTSIDE_BEGIN_END(ctx);
1587 #if FEATURE_EXT_framebuffer_blit
1588 case GL_DRAW_FRAMEBUFFER_EXT:
1589 if (!ctx->Extensions.EXT_framebuffer_blit) {
1590 _mesa_error(ctx, GL_INVALID_ENUM,
1591 "glFramebufferRenderbufferEXT(target)");
1594 fb = ctx->DrawBuffer;
1596 case GL_READ_FRAMEBUFFER_EXT:
1597 if (!ctx->Extensions.EXT_framebuffer_blit) {
1598 _mesa_error(ctx, GL_INVALID_ENUM,
1599 "glFramebufferRenderbufferEXT(target)");
1602 fb = ctx->ReadBuffer;
1605 case GL_FRAMEBUFFER_EXT:
1606 fb = ctx->DrawBuffer;
1609 _mesa_error(ctx, GL_INVALID_ENUM,
1610 "glFramebufferRenderbufferEXT(target)");
1614 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1615 _mesa_error(ctx, GL_INVALID_ENUM,
1616 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1620 if (fb->Name == 0) {
1621 /* Can't attach new renderbuffers to a window system framebuffer */
1622 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1626 att = _mesa_get_attachment(ctx, fb, attachment);
1628 _mesa_error(ctx, GL_INVALID_ENUM,
1629 "glFramebufferRenderbufferEXT(attachment)");
1634 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1636 _mesa_error(ctx, GL_INVALID_OPERATION,
1637 "glFramebufferRenderbufferEXT(renderbuffer)");
1642 /* remove renderbuffer attachment */
1646 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1647 /* make sure the renderbuffer is a depth/stencil format */
1648 if (rb->_BaseFormat != GL_DEPTH_STENCIL) {
1649 _mesa_error(ctx, GL_INVALID_OPERATION,
1650 "glFramebufferRenderbufferEXT(renderbuffer"
1651 " is not DEPTH_STENCIL format)");
1657 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1658 /* The above doesn't fully flush the drivers in the way that a
1659 * glFlush does, but that is required here:
1661 if (ctx->Driver.Flush)
1662 ctx->Driver.Flush(ctx);
1664 assert(ctx->Driver.FramebufferRenderbuffer);
1665 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1667 /* Some subsequent GL commands may depend on the framebuffer's visual
1668 * after the binding is updated. Update visual info now.
1670 _mesa_update_framebuffer_visual(fb);
1675 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1676 GLenum pname, GLint *params)
1678 const struct gl_renderbuffer_attachment *att;
1679 struct gl_framebuffer *buffer;
1680 GET_CURRENT_CONTEXT(ctx);
1682 ASSERT_OUTSIDE_BEGIN_END(ctx);
1685 #if FEATURE_EXT_framebuffer_blit
1686 case GL_DRAW_FRAMEBUFFER_EXT:
1687 if (!ctx->Extensions.EXT_framebuffer_blit) {
1688 _mesa_error(ctx, GL_INVALID_ENUM,
1689 "glGetFramebufferAttachmentParameterivEXT(target)");
1692 buffer = ctx->DrawBuffer;
1694 case GL_READ_FRAMEBUFFER_EXT:
1695 if (!ctx->Extensions.EXT_framebuffer_blit) {
1696 _mesa_error(ctx, GL_INVALID_ENUM,
1697 "glGetFramebufferAttachmentParameterivEXT(target)");
1700 buffer = ctx->ReadBuffer;
1703 case GL_FRAMEBUFFER_EXT:
1704 buffer = ctx->DrawBuffer;
1707 _mesa_error(ctx, GL_INVALID_ENUM,
1708 "glGetFramebufferAttachmentParameterivEXT(target)");
1712 if (buffer->Name == 0) {
1713 _mesa_error(ctx, GL_INVALID_OPERATION,
1714 "glGetFramebufferAttachmentParameterivEXT");
1718 att = _mesa_get_attachment(ctx, buffer, attachment);
1720 _mesa_error(ctx, GL_INVALID_ENUM,
1721 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1725 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1726 /* the depth and stencil attachments must point to the same buffer */
1727 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1728 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1729 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1730 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1731 _mesa_error(ctx, GL_INVALID_OPERATION,
1732 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1733 " attachments differ)");
1738 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1739 /* The above doesn't fully flush the drivers in the way that a
1740 * glFlush does, but that is required here:
1742 if (ctx->Driver.Flush)
1743 ctx->Driver.Flush(ctx);
1746 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1747 *params = att->Type;
1749 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1750 if (att->Type == GL_RENDERBUFFER_EXT) {
1751 *params = att->Renderbuffer->Name;
1753 else if (att->Type == GL_TEXTURE) {
1754 *params = att->Texture->Name;
1757 _mesa_error(ctx, GL_INVALID_ENUM,
1758 "glGetFramebufferAttachmentParameterivEXT(pname)");
1761 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1762 if (att->Type == GL_TEXTURE) {
1763 *params = att->TextureLevel;
1766 _mesa_error(ctx, GL_INVALID_ENUM,
1767 "glGetFramebufferAttachmentParameterivEXT(pname)");
1770 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1771 if (att->Type == GL_TEXTURE) {
1772 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1773 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1780 _mesa_error(ctx, GL_INVALID_ENUM,
1781 "glGetFramebufferAttachmentParameterivEXT(pname)");
1784 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1785 if (att->Type == GL_TEXTURE) {
1786 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1787 *params = att->Zoffset;
1794 _mesa_error(ctx, GL_INVALID_ENUM,
1795 "glGetFramebufferAttachmentParameterivEXT(pname)");
1798 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1799 if (!ctx->Extensions.ARB_framebuffer_object) {
1800 _mesa_error(ctx, GL_INVALID_ENUM,
1801 "glGetFramebufferAttachmentParameterivEXT(pname)");
1804 *params = att->Renderbuffer->ColorEncoding;
1807 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1808 if (!ctx->Extensions.ARB_framebuffer_object) {
1809 _mesa_error(ctx, GL_INVALID_ENUM,
1810 "glGetFramebufferAttachmentParameterivEXT(pname)");
1814 *params = att->Renderbuffer->ComponentType;
1817 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1818 if (!ctx->Extensions.ARB_framebuffer_object) {
1819 _mesa_error(ctx, GL_INVALID_ENUM,
1820 "glGetFramebufferAttachmentParameterivEXT(pname)");
1823 *params = att->Renderbuffer->RedBits;
1826 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1827 if (!ctx->Extensions.ARB_framebuffer_object) {
1828 _mesa_error(ctx, GL_INVALID_ENUM,
1829 "glGetFramebufferAttachmentParameterivEXT(pname)");
1832 *params = att->Renderbuffer->GreenBits;
1835 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1836 if (!ctx->Extensions.ARB_framebuffer_object) {
1837 _mesa_error(ctx, GL_INVALID_ENUM,
1838 "glGetFramebufferAttachmentParameterivEXT(pname)");
1841 *params = att->Renderbuffer->BlueBits;
1844 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1845 if (!ctx->Extensions.ARB_framebuffer_object) {
1846 _mesa_error(ctx, GL_INVALID_ENUM,
1847 "glGetFramebufferAttachmentParameterivEXT(pname)");
1850 *params = att->Renderbuffer->AlphaBits;
1853 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1854 if (!ctx->Extensions.ARB_framebuffer_object) {
1855 _mesa_error(ctx, GL_INVALID_ENUM,
1856 "glGetFramebufferAttachmentParameterivEXT(pname)");
1859 *params = att->Renderbuffer->DepthBits;
1862 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1863 if (!ctx->Extensions.ARB_framebuffer_object) {
1864 _mesa_error(ctx, GL_INVALID_ENUM,
1865 "glGetFramebufferAttachmentParameterivEXT(pname)");
1868 *params = att->Renderbuffer->StencilBits;
1872 _mesa_error(ctx, GL_INVALID_ENUM,
1873 "glGetFramebufferAttachmentParameterivEXT(pname)");
1880 _mesa_GenerateMipmapEXT(GLenum target)
1882 struct gl_texture_unit *texUnit;
1883 struct gl_texture_object *texObj;
1884 GET_CURRENT_CONTEXT(ctx);
1886 ASSERT_OUTSIDE_BEGIN_END(ctx);
1887 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1893 case GL_TEXTURE_CUBE_MAP:
1894 /* OK, legal value */
1897 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1901 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1902 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1904 _mesa_lock_texture(ctx, texObj);
1905 if (target == GL_TEXTURE_CUBE_MAP) {
1908 for (face = 0; face < 6; face++)
1909 ctx->Driver.GenerateMipmap(ctx,
1910 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
1913 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1915 _mesa_unlock_texture(ctx, texObj);
1919 #if FEATURE_EXT_framebuffer_blit
1921 * Blit rectangular region, optionally from one framebuffer to another.
1923 * Note, if the src buffer is multisampled and the dest is not, this is
1924 * when the samples must be resolved to a single color.
1927 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1928 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1929 GLbitfield mask, GLenum filter)
1931 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
1932 GL_DEPTH_BUFFER_BIT |
1933 GL_STENCIL_BUFFER_BIT);
1934 const struct gl_framebuffer *readFb, *drawFb;
1935 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
1936 GET_CURRENT_CONTEXT(ctx);
1938 ASSERT_OUTSIDE_BEGIN_END(ctx);
1939 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1941 if (ctx->NewState) {
1942 _mesa_update_state(ctx);
1945 readFb = ctx->ReadBuffer;
1946 drawFb = ctx->DrawBuffer;
1948 if (!readFb || !drawFb) {
1949 /* This will normally never happen but someday we may want to
1950 * support MakeCurrent() with no drawables.
1955 /* check for complete framebuffers */
1956 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
1957 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1958 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1959 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1963 if (filter != GL_NEAREST && filter != GL_LINEAR) {
1964 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
1968 if (mask & ~legalMaskBits) {
1969 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
1973 /* depth/stencil must be blitted with nearest filtering */
1974 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
1975 && filter != GL_NEAREST) {
1976 _mesa_error(ctx, GL_INVALID_OPERATION,
1977 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
1981 /* get color read/draw renderbuffers */
1982 if (mask & GL_COLOR_BUFFER_BIT) {
1983 colorReadRb = readFb->_ColorReadBuffer;
1984 colorDrawRb = drawFb->_ColorDrawBuffers[0];
1987 colorReadRb = colorDrawRb = NULL;
1990 if (mask & GL_STENCIL_BUFFER_BIT) {
1991 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
1992 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
1993 if (readRb->StencilBits != drawRb->StencilBits) {
1994 _mesa_error(ctx, GL_INVALID_OPERATION,
1995 "glBlitFramebufferEXT(stencil buffer size mismatch");
2000 if (mask & GL_DEPTH_BUFFER_BIT) {
2001 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2002 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2003 if (readRb->DepthBits != drawRb->DepthBits) {
2004 _mesa_error(ctx, GL_INVALID_OPERATION,
2005 "glBlitFramebufferEXT(depth buffer size mismatch");
2010 if (readFb->Visual.samples > 0 &&
2011 drawFb->Visual.samples > 0 &&
2012 readFb->Visual.samples != drawFb->Visual.samples) {
2013 _mesa_error(ctx, GL_INVALID_OPERATION,
2014 "glBlitFramebufferEXT(mismatched samples");
2018 /* extra checks for multisample copies... */
2019 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2020 /* src and dest region sizes must be the same */
2021 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2022 srcY1 - srcY0 != dstY1 - dstY0) {
2023 _mesa_error(ctx, GL_INVALID_OPERATION,
2024 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2028 /* color formats must match */
2031 colorReadRb->_ActualFormat != colorDrawRb->_ActualFormat) {
2032 _mesa_error(ctx, GL_INVALID_OPERATION,
2033 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2038 if (!ctx->Extensions.EXT_framebuffer_blit) {
2039 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2043 ASSERT(ctx->Driver.BlitFramebuffer);
2044 ctx->Driver.BlitFramebuffer(ctx,
2045 srcX0, srcY0, srcX1, srcY1,
2046 dstX0, dstY0, dstX1, dstY1,
2049 #endif /* FEATURE_EXT_framebuffer_blit */