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
39 #include "framebuffer.h"
43 #include "renderbuffer.h"
51 /** Set this to 1 to help debug FBO incompleteness problems */
58 * None of the GL_EXT_framebuffer_object functions are compiled into
65 * When glGenRender/FramebuffersEXT() is called we insert pointers to
66 * these placeholder objects into the hash table.
67 * Later, when the object ID is first bound, we replace the placeholder
68 * with the real frame/renderbuffer.
70 static struct gl_framebuffer DummyFramebuffer
;
71 static struct gl_renderbuffer DummyRenderbuffer
;
74 #define IS_CUBE_FACE(TARGET) \
75 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
76 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
80 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
86 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
93 _mesa_init_fbobjects(GLcontext
*ctx
)
95 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
96 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
101 * Helper routine for getting a gl_renderbuffer.
103 struct gl_renderbuffer
*
104 _mesa_lookup_renderbuffer(GLcontext
*ctx
, GLuint id
)
106 struct gl_renderbuffer
*rb
;
111 rb
= (struct gl_renderbuffer
*)
112 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
118 * Helper routine for getting a gl_framebuffer.
120 struct gl_framebuffer
*
121 _mesa_lookup_framebuffer(GLcontext
*ctx
, GLuint id
)
123 struct gl_framebuffer
*fb
;
128 fb
= (struct gl_framebuffer
*)
129 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
135 * Mark the given framebuffer as invalid. This will force the
136 * test for framebuffer completeness to be done before the framebuffer
140 invalidate_framebuffer(struct gl_framebuffer
*fb
)
142 fb
->_Status
= 0; /* "indeterminate" */
147 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
148 * gl_renderbuffer_attachment object.
149 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
150 * the depth buffer attachment point.
152 struct gl_renderbuffer_attachment
*
153 _mesa_get_attachment(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
158 switch (attachment
) {
159 case GL_COLOR_ATTACHMENT0_EXT
:
160 case GL_COLOR_ATTACHMENT1_EXT
:
161 case GL_COLOR_ATTACHMENT2_EXT
:
162 case GL_COLOR_ATTACHMENT3_EXT
:
163 case GL_COLOR_ATTACHMENT4_EXT
:
164 case GL_COLOR_ATTACHMENT5_EXT
:
165 case GL_COLOR_ATTACHMENT6_EXT
:
166 case GL_COLOR_ATTACHMENT7_EXT
:
167 case GL_COLOR_ATTACHMENT8_EXT
:
168 case GL_COLOR_ATTACHMENT9_EXT
:
169 case GL_COLOR_ATTACHMENT10_EXT
:
170 case GL_COLOR_ATTACHMENT11_EXT
:
171 case GL_COLOR_ATTACHMENT12_EXT
:
172 case GL_COLOR_ATTACHMENT13_EXT
:
173 case GL_COLOR_ATTACHMENT14_EXT
:
174 case GL_COLOR_ATTACHMENT15_EXT
:
175 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
176 if (i
>= ctx
->Const
.MaxColorAttachments
) {
179 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
180 case GL_DEPTH_STENCIL_ATTACHMENT
:
182 case GL_DEPTH_ATTACHMENT_EXT
:
183 return &fb
->Attachment
[BUFFER_DEPTH
];
184 case GL_STENCIL_ATTACHMENT_EXT
:
185 return &fb
->Attachment
[BUFFER_STENCIL
];
193 * Remove any texture or renderbuffer attached to the given attachment
194 * point. Update reference counts, etc.
197 _mesa_remove_attachment(GLcontext
*ctx
, struct gl_renderbuffer_attachment
*att
)
199 if (att
->Type
== GL_TEXTURE
) {
200 ASSERT(att
->Texture
);
201 if (ctx
->Driver
.FinishRenderTexture
) {
202 /* tell driver that we're done rendering to this texture. */
203 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
205 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
206 ASSERT(!att
->Texture
);
208 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
209 ASSERT(!att
->Texture
);
210 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
211 ASSERT(!att
->Renderbuffer
);
214 att
->Complete
= GL_TRUE
;
219 * Bind a texture object to an attachment point.
220 * The previous binding, if any, will be removed first.
223 _mesa_set_texture_attachment(GLcontext
*ctx
,
224 struct gl_framebuffer
*fb
,
225 struct gl_renderbuffer_attachment
*att
,
226 struct gl_texture_object
*texObj
,
227 GLenum texTarget
, GLuint level
, GLuint zoffset
)
229 if (att
->Texture
== texObj
) {
230 /* re-attaching same texture */
231 ASSERT(att
->Type
== GL_TEXTURE
);
235 _mesa_remove_attachment(ctx
, att
);
236 att
->Type
= GL_TEXTURE
;
237 assert(!att
->Texture
);
238 _mesa_reference_texobj(&att
->Texture
, texObj
);
241 /* always update these fields */
242 att
->TextureLevel
= level
;
243 if (IS_CUBE_FACE(texTarget
)) {
244 att
->CubeMapFace
= texTarget
- GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
247 att
->CubeMapFace
= 0;
249 att
->Zoffset
= zoffset
;
250 att
->Complete
= GL_FALSE
;
252 if (att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
253 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
256 invalidate_framebuffer(fb
);
261 * Bind a renderbuffer to an attachment point.
262 * The previous binding, if any, will be removed first.
265 _mesa_set_renderbuffer_attachment(GLcontext
*ctx
,
266 struct gl_renderbuffer_attachment
*att
,
267 struct gl_renderbuffer
*rb
)
269 /* XXX check if re-doing same attachment, exit early */
270 _mesa_remove_attachment(ctx
, att
);
271 att
->Type
= GL_RENDERBUFFER_EXT
;
272 att
->Texture
= NULL
; /* just to be safe */
273 att
->Complete
= GL_FALSE
;
274 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
279 * Fallback for ctx->Driver.FramebufferRenderbuffer()
280 * Attach a renderbuffer object to a framebuffer object.
283 _mesa_framebuffer_renderbuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
284 GLenum attachment
, struct gl_renderbuffer
*rb
)
286 struct gl_renderbuffer_attachment
*att
;
288 _glthread_LOCK_MUTEX(fb
->Mutex
);
290 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
293 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
294 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
295 /* do stencil attachment here (depth already done above) */
296 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
298 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
302 _mesa_remove_attachment(ctx
, att
);
305 invalidate_framebuffer(fb
);
307 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
315 att_incomplete(const char *msg
)
318 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
329 fbo_incomplete(const char *msg
, int index
)
332 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
343 * Test if an attachment point is complete and update its Complete field.
344 * \param format if GL_COLOR, this is a color attachment point,
345 * if GL_DEPTH, this is a depth component attachment point,
346 * if GL_STENCIL, this is a stencil component attachment point.
349 test_attachment_completeness(const GLcontext
*ctx
, GLenum format
,
350 struct gl_renderbuffer_attachment
*att
)
352 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
354 /* assume complete */
355 att
->Complete
= GL_TRUE
;
357 /* Look for reasons why the attachment might be incomplete */
358 if (att
->Type
== GL_TEXTURE
) {
359 const struct gl_texture_object
*texObj
= att
->Texture
;
360 struct gl_texture_image
*texImage
;
363 att_incomplete("no texobj");
364 att
->Complete
= GL_FALSE
;
368 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
370 att_incomplete("no teximage");
371 att
->Complete
= GL_FALSE
;
374 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
375 att_incomplete("teximage width/height=0");
376 _mesa_printf("texobj = %u\n", texObj
->Name
);
377 _mesa_printf("level = %d\n", att
->TextureLevel
);
378 att
->Complete
= GL_FALSE
;
381 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
382 att_incomplete("bad z offset");
383 att
->Complete
= GL_FALSE
;
387 if (format
== GL_COLOR
) {
388 if (texImage
->TexFormat
->BaseFormat
!= GL_RGB
&&
389 texImage
->TexFormat
->BaseFormat
!= GL_RGBA
) {
390 att_incomplete("bad format");
391 att
->Complete
= GL_FALSE
;
394 if (texImage
->TexFormat
->TexelBytes
== 0) {
395 att_incomplete("compressed internalformat");
396 att
->Complete
= GL_FALSE
;
400 else if (format
== GL_DEPTH
) {
401 if (texImage
->TexFormat
->BaseFormat
== GL_DEPTH_COMPONENT
) {
404 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
405 ctx
->Extensions
.ARB_depth_texture
&&
406 texImage
->TexFormat
->BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
410 att
->Complete
= GL_FALSE
;
411 att_incomplete("bad depth format");
416 ASSERT(format
== GL_STENCIL
);
417 ASSERT(att
->Renderbuffer
->StencilBits
);
418 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
419 ctx
->Extensions
.ARB_depth_texture
&&
420 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
424 /* no such thing as stencil-only textures */
425 att_incomplete("illegal stencil texture");
426 att
->Complete
= GL_FALSE
;
431 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
432 ASSERT(att
->Renderbuffer
);
433 if (!att
->Renderbuffer
->InternalFormat
||
434 att
->Renderbuffer
->Width
< 1 ||
435 att
->Renderbuffer
->Height
< 1) {
436 att_incomplete("0x0 renderbuffer");
437 att
->Complete
= GL_FALSE
;
440 if (format
== GL_COLOR
) {
441 if (att
->Renderbuffer
->_BaseFormat
!= GL_RGB
&&
442 att
->Renderbuffer
->_BaseFormat
!= GL_RGBA
) {
443 att_incomplete("bad renderbuffer color format");
444 att
->Complete
= GL_FALSE
;
447 ASSERT(att
->Renderbuffer
->RedBits
);
448 ASSERT(att
->Renderbuffer
->GreenBits
);
449 ASSERT(att
->Renderbuffer
->BlueBits
);
451 else if (format
== GL_DEPTH
) {
452 if (att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
453 ASSERT(att
->Renderbuffer
->DepthBits
);
456 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
457 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
458 ASSERT(att
->Renderbuffer
->DepthBits
);
462 att_incomplete("bad renderbuffer depth format");
463 att
->Complete
= GL_FALSE
;
468 assert(format
== GL_STENCIL
);
469 if (att
->Renderbuffer
->_BaseFormat
== GL_STENCIL_INDEX
) {
470 ASSERT(att
->Renderbuffer
->StencilBits
);
473 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
474 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
475 ASSERT(att
->Renderbuffer
->StencilBits
);
479 att
->Complete
= GL_FALSE
;
480 att_incomplete("bad renderbuffer stencil format");
486 ASSERT(att
->Type
== GL_NONE
);
494 * Test if the given framebuffer object is complete and update its
495 * Status field with the results.
496 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
497 * driver to make hardware-specific validation/completeness checks.
498 * Also update the framebuffer's Width and Height fields if the
499 * framebuffer is complete.
502 _mesa_test_framebuffer_completeness(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
505 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
506 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
507 GLint numSamples
= -1;
511 assert(fb
->Name
!= 0);
517 /* Start at -2 to more easily loop over all attachment points.
522 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
523 struct gl_renderbuffer_attachment
*att
;
527 * XXX for ARB_fbo, only check color buffers that are named by
528 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
531 /* check for attachment completeness
534 att
= &fb
->Attachment
[BUFFER_DEPTH
];
535 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
536 if (!att
->Complete
) {
537 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
538 fbo_incomplete("depth attachment incomplete", -1);
543 att
= &fb
->Attachment
[BUFFER_STENCIL
];
544 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
545 if (!att
->Complete
) {
546 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
547 fbo_incomplete("stencil attachment incomplete", -1);
552 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
553 test_attachment_completeness(ctx
, GL_COLOR
, att
);
554 if (!att
->Complete
) {
555 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
556 fbo_incomplete("color attachment incomplete", i
);
561 /* get width, height, format of the renderbuffer/texture
563 if (att
->Type
== GL_TEXTURE
) {
564 const struct gl_texture_image
*texImg
565 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
566 minWidth
= MIN2(minWidth
, texImg
->Width
);
567 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
568 minHeight
= MIN2(minHeight
, texImg
->Height
);
569 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
570 f
= texImg
->_BaseFormat
;
572 if (f
!= GL_RGB
&& f
!= GL_RGBA
&& f
!= GL_DEPTH_COMPONENT
573 && f
!= GL_DEPTH_STENCIL_EXT
) {
574 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
575 fbo_incomplete("texture attachment incomplete", -1);
579 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
580 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
581 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
582 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
583 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
584 f
= att
->Renderbuffer
->InternalFormat
;
588 assert(att
->Type
== GL_NONE
);
592 if (numSamples
< 0) {
594 numSamples
= att
->Renderbuffer
->NumSamples
;
597 /* Error-check width, height, format, samples
599 if (numImages
== 1) {
600 /* save format, num samples */
606 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
607 /* check that width, height, format are same */
608 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
609 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
610 fbo_incomplete("width or height mismatch", -1);
613 /* check that all color buffer have same format */
614 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
615 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
616 fbo_incomplete("format mismatch", -1);
620 if (att
->Renderbuffer
&&
621 att
->Renderbuffer
->NumSamples
!= numSamples
) {
622 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
623 fbo_incomplete("inconsistant number of samples", i
);
630 #ifndef FEATURE_OES_framebuffer_object
631 /* Check that all DrawBuffers are present */
632 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
633 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
634 const struct gl_renderbuffer_attachment
*att
635 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
637 if (att
->Type
== GL_NONE
) {
638 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
639 fbo_incomplete("missing drawbuffer", j
);
645 /* Check that the ReadBuffer is present */
646 if (fb
->ColorReadBuffer
!= GL_NONE
) {
647 const struct gl_renderbuffer_attachment
*att
648 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
650 if (att
->Type
== GL_NONE
) {
651 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
652 fbo_incomplete("missing readbuffer", -1);
658 if (numImages
== 0) {
659 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
660 fbo_incomplete("no attachments", -1);
664 /* Provisionally set status = COMPLETE ... */
665 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
667 /* ... but the driver may say the FB is incomplete.
668 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
671 if (ctx
->Driver
.ValidateFramebuffer
) {
672 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
673 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
674 fbo_incomplete("driver marked FBO as incomplete", -1);
678 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
680 * Note that if ARB_framebuffer_object is supported and the attached
681 * renderbuffers/textures are different sizes, the framebuffer
682 * width/height will be set to the smallest width/height.
684 fb
->Width
= minWidth
;
685 fb
->Height
= minHeight
;
687 /* finally, update the visual info for the framebuffer */
688 _mesa_update_framebuffer_visual(fb
);
694 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
696 GET_CURRENT_CONTEXT(ctx
);
697 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
699 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
700 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
708 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
710 struct gl_renderbuffer
*newRb
;
711 GET_CURRENT_CONTEXT(ctx
);
713 ASSERT_OUTSIDE_BEGIN_END(ctx
);
715 if (target
!= GL_RENDERBUFFER_EXT
) {
716 _mesa_error(ctx
, GL_INVALID_ENUM
,
717 "glBindRenderbufferEXT(target)");
721 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
724 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
725 if (newRb
== &DummyRenderbuffer
) {
726 /* ID was reserved, but no real renderbuffer object made yet */
729 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
730 /* All RB IDs must be Gen'd */
731 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
736 /* create new renderbuffer object */
737 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
739 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
742 ASSERT(newRb
->AllocStorage
);
743 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
744 newRb
->RefCount
= 1; /* referenced by hash table */
751 ASSERT(newRb
!= &DummyRenderbuffer
);
753 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
758 * If the given renderbuffer is anywhere attached to the framebuffer, detach
760 * This is used when a renderbuffer object is deleted.
761 * The spec calls for unbinding.
764 detach_renderbuffer(GLcontext
*ctx
,
765 struct gl_framebuffer
*fb
,
766 struct gl_renderbuffer
*rb
)
769 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
770 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
771 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
774 invalidate_framebuffer(fb
);
779 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
782 GET_CURRENT_CONTEXT(ctx
);
784 ASSERT_OUTSIDE_BEGIN_END(ctx
);
785 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
787 for (i
= 0; i
< n
; i
++) {
788 if (renderbuffers
[i
] > 0) {
789 struct gl_renderbuffer
*rb
;
790 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
792 /* check if deleting currently bound renderbuffer object */
793 if (rb
== ctx
->CurrentRenderbuffer
) {
795 ASSERT(rb
->RefCount
>= 2);
796 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
799 if (ctx
->DrawBuffer
->Name
) {
800 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
802 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
803 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
806 /* Remove from hash table immediately, to free the ID.
807 * But the object will not be freed until it's no longer
808 * referenced anywhere else.
810 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
812 if (rb
!= &DummyRenderbuffer
) {
813 /* no longer referenced by hash table */
814 _mesa_reference_renderbuffer(&rb
, NULL
);
823 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
825 GET_CURRENT_CONTEXT(ctx
);
829 ASSERT_OUTSIDE_BEGIN_END(ctx
);
832 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
839 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
841 for (i
= 0; i
< n
; i
++) {
842 GLuint name
= first
+ i
;
843 renderbuffers
[i
] = name
;
844 /* insert dummy placeholder into hash table */
845 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
846 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
847 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
853 * Given an internal format token for a render buffer, return the
854 * corresponding base format.
855 * This is very similar to _mesa_base_tex_format() but the set of valid
856 * internal formats is somewhat different.
858 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
859 * GL_DEPTH_STENCIL_EXT or zero if error.
862 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
864 switch (internalFormat
) {
883 case GL_STENCIL_INDEX
:
884 case GL_STENCIL_INDEX1_EXT
:
885 case GL_STENCIL_INDEX4_EXT
:
886 case GL_STENCIL_INDEX8_EXT
:
887 case GL_STENCIL_INDEX16_EXT
:
888 return GL_STENCIL_INDEX
;
889 case GL_DEPTH_COMPONENT
:
890 case GL_DEPTH_COMPONENT16
:
891 case GL_DEPTH_COMPONENT24
:
892 case GL_DEPTH_COMPONENT32
:
893 return GL_DEPTH_COMPONENT
;
894 case GL_DEPTH_STENCIL_EXT
:
895 case GL_DEPTH24_STENCIL8_EXT
:
896 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
897 return GL_DEPTH_STENCIL_EXT
;
900 /* XXX add floating point formats eventually */
907 /** sentinal value, see below */
908 #define NO_SAMPLES 1000
912 * Helper function used by _mesa_RenderbufferStorageEXT() and
913 * _mesa_RenderbufferStorageMultisample().
914 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
917 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
918 GLsizei width
, GLsizei height
, GLsizei samples
)
920 const char *func
= samples
== NO_SAMPLES
?
921 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
922 struct gl_renderbuffer
*rb
;
924 GET_CURRENT_CONTEXT(ctx
);
926 ASSERT_OUTSIDE_BEGIN_END(ctx
);
928 if (target
!= GL_RENDERBUFFER_EXT
) {
929 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
933 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
934 if (baseFormat
== 0) {
935 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
939 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
940 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
944 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
945 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
949 if (samples
== NO_SAMPLES
) {
950 /* NumSamples == 0 indicates non-multisampling */
953 else if (samples
> ctx
->Const
.MaxSamples
) {
954 /* note: driver may choose to use more samples than what's requested */
955 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
959 rb
= ctx
->CurrentRenderbuffer
;
961 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
965 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
967 if (rb
->InternalFormat
== internalFormat
&&
968 rb
->Width
== (GLuint
) width
&&
969 rb
->Height
== (GLuint
) height
) {
970 /* no change in allocation needed */
974 /* These MUST get set by the AllocStorage func */
975 rb
->_ActualFormat
= 0;
983 rb
->NumSamples
= samples
;
985 /* Now allocate the storage */
986 ASSERT(rb
->AllocStorage
);
987 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
988 /* No error - check/set fields now */
989 assert(rb
->_ActualFormat
);
990 assert(rb
->Width
== (GLuint
) width
);
991 assert(rb
->Height
== (GLuint
) height
);
992 assert(rb
->RedBits
|| rb
->GreenBits
|| rb
->BlueBits
|| rb
->AlphaBits
||
993 rb
->DepthBits
|| rb
->StencilBits
|| rb
->IndexBits
);
994 rb
->InternalFormat
= internalFormat
;
995 rb
->_BaseFormat
= baseFormat
;
998 /* Probably ran out of memory - clear the fields */
1001 rb
->InternalFormat
= GL_NONE
;
1002 rb
->_ActualFormat
= GL_NONE
;
1003 rb
->_BaseFormat
= GL_NONE
;
1015 test_framebuffer_completeness(ctx, fb);
1017 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1024 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1025 GLsizei width
, GLsizei height
)
1027 /* GL_ARB_fbo says calling this function is equivalent to calling
1028 * glRenderbufferStorageMultisample() with samples=0. We pass in
1029 * a token value here just for error reporting purposes.
1031 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1036 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1037 GLenum internalFormat
,
1038 GLsizei width
, GLsizei height
)
1040 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1046 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1048 struct gl_renderbuffer
*rb
;
1049 GET_CURRENT_CONTEXT(ctx
);
1051 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1053 if (target
!= GL_RENDERBUFFER_EXT
) {
1054 _mesa_error(ctx
, GL_INVALID_ENUM
,
1055 "glGetRenderbufferParameterivEXT(target)");
1059 rb
= ctx
->CurrentRenderbuffer
;
1061 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1062 "glGetRenderbufferParameterivEXT");
1066 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1069 case GL_RENDERBUFFER_WIDTH_EXT
:
1070 *params
= rb
->Width
;
1072 case GL_RENDERBUFFER_HEIGHT_EXT
:
1073 *params
= rb
->Height
;
1075 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1076 *params
= rb
->InternalFormat
;
1078 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1079 *params
= rb
->RedBits
;
1081 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1082 *params
= rb
->GreenBits
;
1084 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1085 *params
= rb
->BlueBits
;
1087 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1088 *params
= rb
->AlphaBits
;
1090 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1091 *params
= rb
->DepthBits
;
1093 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1094 *params
= rb
->StencilBits
;
1096 case GL_RENDERBUFFER_SAMPLES
:
1097 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1098 *params
= rb
->NumSamples
;
1103 _mesa_error(ctx
, GL_INVALID_ENUM
,
1104 "glGetRenderbufferParameterivEXT(target)");
1110 GLboolean GLAPIENTRY
1111 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1113 GET_CURRENT_CONTEXT(ctx
);
1114 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1116 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1117 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1125 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1128 ASSERT(ctx
->Driver
.RenderTexture
);
1129 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1130 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1131 struct gl_texture_object
*texObj
= att
->Texture
;
1133 && texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1134 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1141 * Examine all the framebuffer's attachments to see if any are textures.
1142 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1143 * notify the device driver that the texture image may have changed.
1146 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1148 if (ctx
->Driver
.FinishRenderTexture
) {
1150 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1151 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1152 if (att
->Texture
&& att
->Renderbuffer
) {
1153 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1161 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1163 struct gl_framebuffer
*newFb
, *newFbread
;
1164 GLboolean bindReadBuf
, bindDrawBuf
;
1165 GET_CURRENT_CONTEXT(ctx
);
1168 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1169 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1170 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1174 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1176 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1177 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1178 "glBindFramebufferEXT(unsupported)");
1183 #if FEATURE_EXT_framebuffer_blit
1184 case GL_DRAW_FRAMEBUFFER_EXT
:
1185 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1186 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1189 bindDrawBuf
= GL_TRUE
;
1190 bindReadBuf
= GL_FALSE
;
1192 case GL_READ_FRAMEBUFFER_EXT
:
1193 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1194 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1197 bindDrawBuf
= GL_FALSE
;
1198 bindReadBuf
= GL_TRUE
;
1201 case GL_FRAMEBUFFER_EXT
:
1202 bindDrawBuf
= GL_TRUE
;
1203 bindReadBuf
= GL_TRUE
;
1206 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1210 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1213 /* Binding a user-created framebuffer object */
1214 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1215 if (newFb
== &DummyFramebuffer
) {
1216 /* ID was reserved, but no real framebuffer object made yet */
1219 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1220 /* All FBO IDs must be Gen'd */
1221 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1226 /* create new framebuffer object */
1227 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1229 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1232 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1237 /* Binding the window system framebuffer (which was originally set
1238 * with MakeCurrent).
1240 newFb
= ctx
->WinSysDrawBuffer
;
1241 newFbread
= ctx
->WinSysReadBuffer
;
1245 ASSERT(newFb
!= &DummyFramebuffer
);
1248 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1252 if (ctx
->ReadBuffer
== newFbread
)
1253 bindReadBuf
= GL_FALSE
; /* no change */
1255 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1259 /* check if old FB had any texture attachments */
1260 if (ctx
->DrawBuffer
->Name
!= 0) {
1261 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1264 if (ctx
->DrawBuffer
== newFb
)
1265 bindDrawBuf
= GL_FALSE
; /* no change */
1267 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1269 if (newFb
->Name
!= 0) {
1270 /* check if newly bound framebuffer has any texture attachments */
1271 check_begin_texture_render(ctx
, newFb
);
1275 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1276 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1282 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1285 GET_CURRENT_CONTEXT(ctx
);
1287 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1288 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1290 for (i
= 0; i
< n
; i
++) {
1291 if (framebuffers
[i
] > 0) {
1292 struct gl_framebuffer
*fb
;
1293 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1295 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1297 /* check if deleting currently bound framebuffer object */
1298 if (fb
== ctx
->DrawBuffer
) {
1300 ASSERT(fb
->RefCount
>= 2);
1301 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1303 if (fb
== ctx
->ReadBuffer
) {
1305 ASSERT(fb
->RefCount
>= 2);
1306 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1309 /* remove from hash table immediately, to free the ID */
1310 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1312 if (fb
!= &DummyFramebuffer
) {
1313 /* But the object will not be freed until it's no longer
1314 * bound in any context.
1316 _mesa_reference_framebuffer(&fb
, NULL
);
1325 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1327 GET_CURRENT_CONTEXT(ctx
);
1331 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1334 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1341 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1343 for (i
= 0; i
< n
; i
++) {
1344 GLuint name
= first
+ i
;
1345 framebuffers
[i
] = name
;
1346 /* insert dummy placeholder into hash table */
1347 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1348 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1349 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1356 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1358 struct gl_framebuffer
*buffer
;
1359 GET_CURRENT_CONTEXT(ctx
);
1361 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1364 #if FEATURE_EXT_framebuffer_blit
1365 case GL_DRAW_FRAMEBUFFER_EXT
:
1366 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1367 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1370 buffer
= ctx
->DrawBuffer
;
1372 case GL_READ_FRAMEBUFFER_EXT
:
1373 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1374 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1377 buffer
= ctx
->ReadBuffer
;
1380 case GL_FRAMEBUFFER_EXT
:
1381 buffer
= ctx
->DrawBuffer
;
1384 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1385 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1388 if (buffer
->Name
== 0) {
1389 /* The window system / default framebuffer is always complete */
1390 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1393 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1395 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1396 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1399 return buffer
->_Status
;
1405 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1408 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1409 GLenum attachment
, GLenum textarget
, GLuint texture
,
1410 GLint level
, GLint zoffset
)
1412 struct gl_renderbuffer_attachment
*att
;
1413 struct gl_texture_object
*texObj
= NULL
;
1414 struct gl_framebuffer
*fb
;
1415 GLboolean error
= GL_FALSE
;
1417 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1420 case GL_READ_FRAMEBUFFER_EXT
:
1421 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1422 fb
= ctx
->ReadBuffer
;
1424 case GL_DRAW_FRAMEBUFFER_EXT
:
1425 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1427 case GL_FRAMEBUFFER_EXT
:
1428 fb
= ctx
->DrawBuffer
;
1435 _mesa_error(ctx
, GL_INVALID_ENUM
,
1436 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1442 /* check framebuffer binding */
1443 if (fb
->Name
== 0) {
1444 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1445 "glFramebufferTexture%sEXT", caller
);
1450 /* The textarget, level, and zoffset parameters are only validated if
1451 * texture is non-zero.
1454 GLboolean err
= GL_TRUE
;
1456 texObj
= _mesa_lookup_texture(ctx
, texture
);
1457 if (texObj
!= NULL
) {
1458 if (textarget
== 0) {
1459 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1460 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1461 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1464 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1465 ? !IS_CUBE_FACE(textarget
)
1466 : (texObj
->Target
!= textarget
);
1471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1472 "glFramebufferTexture%sEXT(texture target mismatch)",
1477 if (texObj
->Target
== GL_TEXTURE_3D
) {
1478 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1479 if (zoffset
< 0 || zoffset
>= maxSize
) {
1480 _mesa_error(ctx
, GL_INVALID_VALUE
,
1481 "glFramebufferTexture%sEXT(zoffset)", caller
);
1485 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1486 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1487 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1488 _mesa_error(ctx
, GL_INVALID_VALUE
,
1489 "glFramebufferTexture%sEXT(layer)", caller
);
1495 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1496 _mesa_error(ctx
, GL_INVALID_VALUE
,
1497 "glFramebufferTexture%sEXT(level)", caller
);
1502 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1504 _mesa_error(ctx
, GL_INVALID_ENUM
,
1505 "glFramebufferTexture%sEXT(attachment)", caller
);
1509 if (texObj
&& attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1510 /* the texture format must be depth+stencil */
1511 const struct gl_texture_image
*texImg
;
1512 texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
1513 if (!texImg
|| texImg
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1514 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1515 "glFramebufferTexture%sEXT(texture is not"
1516 " DEPTH_STENCIL format)", caller
);
1521 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1523 _glthread_LOCK_MUTEX(fb
->Mutex
);
1525 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1527 /* Set the render-to-texture flag. We'll check this flag in
1528 * glTexImage() and friends to determine if we need to revalidate
1529 * any FBOs that might be rendering into this texture.
1530 * This flag never gets cleared since it's non-trivial to determine
1531 * when all FBOs might be done rendering to this texture. That's OK
1532 * though since it's uncommon to render to a texture then repeatedly
1533 * call glTexImage() to change images in the texture.
1535 texObj
->_RenderToTexture
= GL_TRUE
;
1538 _mesa_remove_attachment(ctx
, att
);
1541 invalidate_framebuffer(fb
);
1543 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1549 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1550 GLenum textarget
, GLuint texture
, GLint level
)
1552 GET_CURRENT_CONTEXT(ctx
);
1554 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1555 _mesa_error(ctx
, GL_INVALID_ENUM
,
1556 "glFramebufferTexture1DEXT(textarget)");
1560 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1566 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1567 GLenum textarget
, GLuint texture
, GLint level
)
1569 GET_CURRENT_CONTEXT(ctx
);
1571 if ((texture
!= 0) &&
1572 (textarget
!= GL_TEXTURE_2D
) &&
1573 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1574 (!IS_CUBE_FACE(textarget
))) {
1575 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1576 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1580 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1586 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1587 GLenum textarget
, GLuint texture
,
1588 GLint level
, GLint zoffset
)
1590 GET_CURRENT_CONTEXT(ctx
);
1592 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1593 _mesa_error(ctx
, GL_INVALID_ENUM
,
1594 "glFramebufferTexture3DEXT(textarget)");
1598 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1604 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1605 GLuint texture
, GLint level
, GLint layer
)
1607 GET_CURRENT_CONTEXT(ctx
);
1609 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1615 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1616 GLenum renderbufferTarget
,
1617 GLuint renderbuffer
)
1619 struct gl_renderbuffer_attachment
*att
;
1620 struct gl_framebuffer
*fb
;
1621 struct gl_renderbuffer
*rb
;
1622 GET_CURRENT_CONTEXT(ctx
);
1624 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1627 #if FEATURE_EXT_framebuffer_blit
1628 case GL_DRAW_FRAMEBUFFER_EXT
:
1629 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1630 _mesa_error(ctx
, GL_INVALID_ENUM
,
1631 "glFramebufferRenderbufferEXT(target)");
1634 fb
= ctx
->DrawBuffer
;
1636 case GL_READ_FRAMEBUFFER_EXT
:
1637 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1638 _mesa_error(ctx
, GL_INVALID_ENUM
,
1639 "glFramebufferRenderbufferEXT(target)");
1642 fb
= ctx
->ReadBuffer
;
1645 case GL_FRAMEBUFFER_EXT
:
1646 fb
= ctx
->DrawBuffer
;
1649 _mesa_error(ctx
, GL_INVALID_ENUM
,
1650 "glFramebufferRenderbufferEXT(target)");
1654 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1655 _mesa_error(ctx
, GL_INVALID_ENUM
,
1656 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1660 if (fb
->Name
== 0) {
1661 /* Can't attach new renderbuffers to a window system framebuffer */
1662 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1666 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1668 _mesa_error(ctx
, GL_INVALID_ENUM
,
1669 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1670 _mesa_lookup_enum_by_nr(attachment
));
1675 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1677 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1678 "glFramebufferRenderbufferEXT(non-existant"
1679 " renderbuffer %u)", renderbuffer
);
1684 /* remove renderbuffer attachment */
1688 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1689 /* make sure the renderbuffer is a depth/stencil format */
1690 if (rb
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1691 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1692 "glFramebufferRenderbufferEXT(renderbuffer"
1693 " is not DEPTH_STENCIL format)");
1699 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1701 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1702 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1704 /* Some subsequent GL commands may depend on the framebuffer's visual
1705 * after the binding is updated. Update visual info now.
1707 _mesa_update_framebuffer_visual(fb
);
1712 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1713 GLenum pname
, GLint
*params
)
1715 const struct gl_renderbuffer_attachment
*att
;
1716 struct gl_framebuffer
*buffer
;
1717 GET_CURRENT_CONTEXT(ctx
);
1719 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1722 #if FEATURE_EXT_framebuffer_blit
1723 case GL_DRAW_FRAMEBUFFER_EXT
:
1724 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1725 _mesa_error(ctx
, GL_INVALID_ENUM
,
1726 "glGetFramebufferAttachmentParameterivEXT(target)");
1729 buffer
= ctx
->DrawBuffer
;
1731 case GL_READ_FRAMEBUFFER_EXT
:
1732 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1733 _mesa_error(ctx
, GL_INVALID_ENUM
,
1734 "glGetFramebufferAttachmentParameterivEXT(target)");
1737 buffer
= ctx
->ReadBuffer
;
1740 case GL_FRAMEBUFFER_EXT
:
1741 buffer
= ctx
->DrawBuffer
;
1744 _mesa_error(ctx
, GL_INVALID_ENUM
,
1745 "glGetFramebufferAttachmentParameterivEXT(target)");
1749 if (buffer
->Name
== 0) {
1750 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1751 "glGetFramebufferAttachmentParameterivEXT");
1755 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1757 _mesa_error(ctx
, GL_INVALID_ENUM
,
1758 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1762 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1763 /* the depth and stencil attachments must point to the same buffer */
1764 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1765 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1766 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1767 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1768 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1769 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1770 " attachments differ)");
1775 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1778 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1779 *params
= att
->Type
;
1781 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1782 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1783 *params
= att
->Renderbuffer
->Name
;
1785 else if (att
->Type
== GL_TEXTURE
) {
1786 *params
= att
->Texture
->Name
;
1789 _mesa_error(ctx
, GL_INVALID_ENUM
,
1790 "glGetFramebufferAttachmentParameterivEXT(pname)");
1793 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1794 if (att
->Type
== GL_TEXTURE
) {
1795 *params
= att
->TextureLevel
;
1798 _mesa_error(ctx
, GL_INVALID_ENUM
,
1799 "glGetFramebufferAttachmentParameterivEXT(pname)");
1802 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1803 if (att
->Type
== GL_TEXTURE
) {
1804 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1805 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1812 _mesa_error(ctx
, GL_INVALID_ENUM
,
1813 "glGetFramebufferAttachmentParameterivEXT(pname)");
1816 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1817 if (att
->Type
== GL_TEXTURE
) {
1818 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1819 *params
= att
->Zoffset
;
1826 _mesa_error(ctx
, GL_INVALID_ENUM
,
1827 "glGetFramebufferAttachmentParameterivEXT(pname)");
1830 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1831 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1832 _mesa_error(ctx
, GL_INVALID_ENUM
,
1833 "glGetFramebufferAttachmentParameterivEXT(pname)");
1836 *params
= att
->Renderbuffer
->ColorEncoding
;
1839 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1840 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1841 _mesa_error(ctx
, GL_INVALID_ENUM
,
1842 "glGetFramebufferAttachmentParameterivEXT(pname)");
1846 *params
= att
->Renderbuffer
->ComponentType
;
1849 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1850 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1851 _mesa_error(ctx
, GL_INVALID_ENUM
,
1852 "glGetFramebufferAttachmentParameterivEXT(pname)");
1855 *params
= att
->Renderbuffer
->RedBits
;
1858 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1859 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1860 _mesa_error(ctx
, GL_INVALID_ENUM
,
1861 "glGetFramebufferAttachmentParameterivEXT(pname)");
1864 *params
= att
->Renderbuffer
->GreenBits
;
1867 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1868 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1869 _mesa_error(ctx
, GL_INVALID_ENUM
,
1870 "glGetFramebufferAttachmentParameterivEXT(pname)");
1873 *params
= att
->Renderbuffer
->BlueBits
;
1876 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1877 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1878 _mesa_error(ctx
, GL_INVALID_ENUM
,
1879 "glGetFramebufferAttachmentParameterivEXT(pname)");
1882 *params
= att
->Renderbuffer
->AlphaBits
;
1885 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1886 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1887 _mesa_error(ctx
, GL_INVALID_ENUM
,
1888 "glGetFramebufferAttachmentParameterivEXT(pname)");
1891 *params
= att
->Renderbuffer
->DepthBits
;
1894 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1895 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1896 _mesa_error(ctx
, GL_INVALID_ENUM
,
1897 "glGetFramebufferAttachmentParameterivEXT(pname)");
1900 *params
= att
->Renderbuffer
->StencilBits
;
1904 _mesa_error(ctx
, GL_INVALID_ENUM
,
1905 "glGetFramebufferAttachmentParameterivEXT(pname)");
1912 _mesa_GenerateMipmapEXT(GLenum target
)
1914 struct gl_texture_unit
*texUnit
;
1915 struct gl_texture_object
*texObj
;
1916 GET_CURRENT_CONTEXT(ctx
);
1918 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1919 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1925 case GL_TEXTURE_CUBE_MAP
:
1926 /* OK, legal value */
1929 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1933 texUnit
= _mesa_get_current_tex_unit(ctx
);
1934 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1936 _mesa_lock_texture(ctx
, texObj
);
1937 if (target
== GL_TEXTURE_CUBE_MAP
) {
1939 for (face
= 0; face
< 6; face
++)
1940 ctx
->Driver
.GenerateMipmap(ctx
,
1941 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1945 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1947 _mesa_unlock_texture(ctx
, texObj
);
1951 #if FEATURE_EXT_framebuffer_blit
1953 * Blit rectangular region, optionally from one framebuffer to another.
1955 * Note, if the src buffer is multisampled and the dest is not, this is
1956 * when the samples must be resolved to a single color.
1959 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1960 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1961 GLbitfield mask
, GLenum filter
)
1963 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1964 GL_DEPTH_BUFFER_BIT
|
1965 GL_STENCIL_BUFFER_BIT
);
1966 const struct gl_framebuffer
*readFb
, *drawFb
;
1967 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1968 GET_CURRENT_CONTEXT(ctx
);
1970 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1971 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1973 if (ctx
->NewState
) {
1974 _mesa_update_state(ctx
);
1977 readFb
= ctx
->ReadBuffer
;
1978 drawFb
= ctx
->DrawBuffer
;
1980 if (!readFb
|| !drawFb
) {
1981 /* This will normally never happen but someday we may want to
1982 * support MakeCurrent() with no drawables.
1987 /* check for complete framebuffers */
1988 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
1989 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1990 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1991 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1995 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
1996 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2000 if (mask
& ~legalMaskBits
) {
2001 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2005 /* depth/stencil must be blitted with nearest filtering */
2006 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2007 && filter
!= GL_NEAREST
) {
2008 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2009 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2013 /* get color read/draw renderbuffers */
2014 if (mask
& GL_COLOR_BUFFER_BIT
) {
2015 colorReadRb
= readFb
->_ColorReadBuffer
;
2016 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2019 colorReadRb
= colorDrawRb
= NULL
;
2022 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2023 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2024 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2027 readRb
->StencilBits
!= drawRb
->StencilBits
) {
2028 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2029 "glBlitFramebufferEXT(stencil buffer size mismatch");
2034 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2035 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2036 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2039 readRb
->DepthBits
!= drawRb
->DepthBits
) {
2040 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2041 "glBlitFramebufferEXT(depth buffer size mismatch");
2046 if (readFb
->Visual
.samples
> 0 &&
2047 drawFb
->Visual
.samples
> 0 &&
2048 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2049 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2050 "glBlitFramebufferEXT(mismatched samples");
2054 /* extra checks for multisample copies... */
2055 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2056 /* src and dest region sizes must be the same */
2057 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2058 srcY1
- srcY0
!= dstY1
- dstY0
) {
2059 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2060 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2064 /* color formats must match */
2067 colorReadRb
->_ActualFormat
!= colorDrawRb
->_ActualFormat
) {
2068 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2069 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2074 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2075 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2079 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2080 ctx
->Driver
.BlitFramebuffer(ctx
,
2081 srcX0
, srcY0
, srcX1
, srcY1
,
2082 dstX0
, dstY0
, dstX1
, dstY1
,
2085 #endif /* FEATURE_EXT_framebuffer_blit */