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"
50 /** Set this to 1 to help debug FBO incompleteness problems */
57 * None of the GL_EXT_framebuffer_object functions are compiled into
64 * When glGenRender/FramebuffersEXT() is called we insert pointers to
65 * these placeholder objects into the hash table.
66 * Later, when the object ID is first bound, we replace the placeholder
67 * with the real frame/renderbuffer.
69 static struct gl_framebuffer DummyFramebuffer
;
70 static struct gl_renderbuffer DummyRenderbuffer
;
73 #define IS_CUBE_FACE(TARGET) \
74 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
75 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
79 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
85 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
92 _mesa_init_fbobjects(GLcontext
*ctx
)
94 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
95 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
100 * Helper routine for getting a gl_renderbuffer.
102 struct gl_renderbuffer
*
103 _mesa_lookup_renderbuffer(GLcontext
*ctx
, GLuint id
)
105 struct gl_renderbuffer
*rb
;
110 rb
= (struct gl_renderbuffer
*)
111 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
117 * Helper routine for getting a gl_framebuffer.
119 struct gl_framebuffer
*
120 _mesa_lookup_framebuffer(GLcontext
*ctx
, GLuint id
)
122 struct gl_framebuffer
*fb
;
127 fb
= (struct gl_framebuffer
*)
128 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
134 * Mark the given framebuffer as invalid. This will force the
135 * test for framebuffer completeness to be done before the framebuffer
139 invalidate_framebuffer(struct gl_framebuffer
*fb
)
141 fb
->_Status
= 0; /* "indeterminate" */
146 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
147 * gl_renderbuffer_attachment object.
148 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
149 * the depth buffer attachment point.
151 struct gl_renderbuffer_attachment
*
152 _mesa_get_attachment(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
157 switch (attachment
) {
158 case GL_COLOR_ATTACHMENT0_EXT
:
159 case GL_COLOR_ATTACHMENT1_EXT
:
160 case GL_COLOR_ATTACHMENT2_EXT
:
161 case GL_COLOR_ATTACHMENT3_EXT
:
162 case GL_COLOR_ATTACHMENT4_EXT
:
163 case GL_COLOR_ATTACHMENT5_EXT
:
164 case GL_COLOR_ATTACHMENT6_EXT
:
165 case GL_COLOR_ATTACHMENT7_EXT
:
166 case GL_COLOR_ATTACHMENT8_EXT
:
167 case GL_COLOR_ATTACHMENT9_EXT
:
168 case GL_COLOR_ATTACHMENT10_EXT
:
169 case GL_COLOR_ATTACHMENT11_EXT
:
170 case GL_COLOR_ATTACHMENT12_EXT
:
171 case GL_COLOR_ATTACHMENT13_EXT
:
172 case GL_COLOR_ATTACHMENT14_EXT
:
173 case GL_COLOR_ATTACHMENT15_EXT
:
174 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
175 if (i
>= ctx
->Const
.MaxColorAttachments
) {
178 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
179 case GL_DEPTH_STENCIL_ATTACHMENT
:
181 case GL_DEPTH_ATTACHMENT_EXT
:
182 return &fb
->Attachment
[BUFFER_DEPTH
];
183 case GL_STENCIL_ATTACHMENT_EXT
:
184 return &fb
->Attachment
[BUFFER_STENCIL
];
192 * Remove any texture or renderbuffer attached to the given attachment
193 * point. Update reference counts, etc.
196 _mesa_remove_attachment(GLcontext
*ctx
, struct gl_renderbuffer_attachment
*att
)
198 if (att
->Type
== GL_TEXTURE
) {
199 ASSERT(att
->Texture
);
200 if (ctx
->Driver
.FinishRenderTexture
) {
201 /* tell driver that we're done rendering to this texture. */
202 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
204 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
205 ASSERT(!att
->Texture
);
207 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
208 ASSERT(!att
->Texture
);
209 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
210 ASSERT(!att
->Renderbuffer
);
213 att
->Complete
= GL_TRUE
;
218 * Bind a texture object to an attachment point.
219 * The previous binding, if any, will be removed first.
222 _mesa_set_texture_attachment(GLcontext
*ctx
,
223 struct gl_framebuffer
*fb
,
224 struct gl_renderbuffer_attachment
*att
,
225 struct gl_texture_object
*texObj
,
226 GLenum texTarget
, GLuint level
, GLuint zoffset
)
228 if (att
->Texture
== texObj
) {
229 /* re-attaching same texture */
230 ASSERT(att
->Type
== GL_TEXTURE
);
234 _mesa_remove_attachment(ctx
, att
);
235 att
->Type
= GL_TEXTURE
;
236 assert(!att
->Texture
);
237 _mesa_reference_texobj(&att
->Texture
, texObj
);
240 /* always update these fields */
241 att
->TextureLevel
= level
;
242 if (IS_CUBE_FACE(texTarget
)) {
243 att
->CubeMapFace
= texTarget
- GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
246 att
->CubeMapFace
= 0;
248 att
->Zoffset
= zoffset
;
249 att
->Complete
= GL_FALSE
;
251 if (att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
252 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
255 invalidate_framebuffer(fb
);
260 * Bind a renderbuffer to an attachment point.
261 * The previous binding, if any, will be removed first.
264 _mesa_set_renderbuffer_attachment(GLcontext
*ctx
,
265 struct gl_renderbuffer_attachment
*att
,
266 struct gl_renderbuffer
*rb
)
268 /* XXX check if re-doing same attachment, exit early */
269 _mesa_remove_attachment(ctx
, att
);
270 att
->Type
= GL_RENDERBUFFER_EXT
;
271 att
->Texture
= NULL
; /* just to be safe */
272 att
->Complete
= GL_FALSE
;
273 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
278 * Fallback for ctx->Driver.FramebufferRenderbuffer()
279 * Attach a renderbuffer object to a framebuffer object.
282 _mesa_framebuffer_renderbuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
283 GLenum attachment
, struct gl_renderbuffer
*rb
)
285 struct gl_renderbuffer_attachment
*att
;
287 _glthread_LOCK_MUTEX(fb
->Mutex
);
289 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
292 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
293 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
294 /* do stencil attachment here (depth already done above) */
295 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
297 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
301 _mesa_remove_attachment(ctx
, att
);
304 invalidate_framebuffer(fb
);
306 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
314 att_incomplete(const char *msg
)
317 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
328 fbo_incomplete(const char *msg
, int index
)
331 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
342 * Test if an attachment point is complete and update its Complete field.
343 * \param format if GL_COLOR, this is a color attachment point,
344 * if GL_DEPTH, this is a depth component attachment point,
345 * if GL_STENCIL, this is a stencil component attachment point.
348 test_attachment_completeness(const GLcontext
*ctx
, GLenum format
,
349 struct gl_renderbuffer_attachment
*att
)
351 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
353 /* assume complete */
354 att
->Complete
= GL_TRUE
;
356 /* Look for reasons why the attachment might be incomplete */
357 if (att
->Type
== GL_TEXTURE
) {
358 const struct gl_texture_object
*texObj
= att
->Texture
;
359 struct gl_texture_image
*texImage
;
362 att_incomplete("no texobj");
363 att
->Complete
= GL_FALSE
;
367 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
369 att_incomplete("no teximage");
370 att
->Complete
= GL_FALSE
;
373 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
374 att_incomplete("teximage width/height=0");
375 _mesa_printf("texobj = %u\n", texObj
->Name
);
376 _mesa_printf("level = %d\n", att
->TextureLevel
);
377 att
->Complete
= GL_FALSE
;
380 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
381 att_incomplete("bad z offset");
382 att
->Complete
= GL_FALSE
;
386 if (format
== GL_COLOR
) {
387 if (texImage
->TexFormat
->BaseFormat
!= GL_RGB
&&
388 texImage
->TexFormat
->BaseFormat
!= GL_RGBA
) {
389 att_incomplete("bad format");
390 att
->Complete
= GL_FALSE
;
393 if (texImage
->TexFormat
->TexelBytes
== 0) {
394 att_incomplete("compressed internalformat");
395 att
->Complete
= GL_FALSE
;
399 else if (format
== GL_DEPTH
) {
400 if (texImage
->TexFormat
->BaseFormat
== GL_DEPTH_COMPONENT
) {
403 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
404 ctx
->Extensions
.ARB_depth_texture
&&
405 texImage
->TexFormat
->BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
409 att
->Complete
= GL_FALSE
;
410 att_incomplete("bad depth format");
415 ASSERT(format
== GL_STENCIL
);
416 ASSERT(att
->Renderbuffer
->StencilBits
);
417 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
418 ctx
->Extensions
.ARB_depth_texture
&&
419 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
423 /* no such thing as stencil-only textures */
424 att_incomplete("illegal stencil texture");
425 att
->Complete
= GL_FALSE
;
430 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
431 ASSERT(att
->Renderbuffer
);
432 if (!att
->Renderbuffer
->InternalFormat
||
433 att
->Renderbuffer
->Width
< 1 ||
434 att
->Renderbuffer
->Height
< 1) {
435 att_incomplete("0x0 renderbuffer");
436 att
->Complete
= GL_FALSE
;
439 if (format
== GL_COLOR
) {
440 if (att
->Renderbuffer
->_BaseFormat
!= GL_RGB
&&
441 att
->Renderbuffer
->_BaseFormat
!= GL_RGBA
) {
442 att_incomplete("bad renderbuffer color format");
443 att
->Complete
= GL_FALSE
;
446 ASSERT(att
->Renderbuffer
->RedBits
);
447 ASSERT(att
->Renderbuffer
->GreenBits
);
448 ASSERT(att
->Renderbuffer
->BlueBits
);
450 else if (format
== GL_DEPTH
) {
451 if (att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
452 ASSERT(att
->Renderbuffer
->DepthBits
);
455 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
456 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
457 ASSERT(att
->Renderbuffer
->DepthBits
);
461 att_incomplete("bad renderbuffer depth format");
462 att
->Complete
= GL_FALSE
;
467 assert(format
== GL_STENCIL
);
468 if (att
->Renderbuffer
->_BaseFormat
== GL_STENCIL_INDEX
) {
469 ASSERT(att
->Renderbuffer
->StencilBits
);
472 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
473 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
474 ASSERT(att
->Renderbuffer
->StencilBits
);
478 att
->Complete
= GL_FALSE
;
479 att_incomplete("bad renderbuffer stencil format");
485 ASSERT(att
->Type
== GL_NONE
);
493 * Test if the given framebuffer object is complete and update its
494 * Status field with the results.
495 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
496 * driver to make hardware-specific validation/completeness checks.
497 * Also update the framebuffer's Width and Height fields if the
498 * framebuffer is complete.
501 _mesa_test_framebuffer_completeness(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
504 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
505 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
506 GLint numSamples
= -1;
510 assert(fb
->Name
!= 0);
516 /* Start at -2 to more easily loop over all attachment points.
521 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
522 struct gl_renderbuffer_attachment
*att
;
526 * XXX for ARB_fbo, only check color buffers that are named by
527 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
530 /* check for attachment completeness
533 att
= &fb
->Attachment
[BUFFER_DEPTH
];
534 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
535 if (!att
->Complete
) {
536 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
537 fbo_incomplete("depth attachment incomplete", -1);
542 att
= &fb
->Attachment
[BUFFER_STENCIL
];
543 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
544 if (!att
->Complete
) {
545 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
546 fbo_incomplete("stencil attachment incomplete", -1);
551 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
552 test_attachment_completeness(ctx
, GL_COLOR
, att
);
553 if (!att
->Complete
) {
554 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
555 fbo_incomplete("color attachment incomplete", i
);
560 /* get width, height, format of the renderbuffer/texture
562 if (att
->Type
== GL_TEXTURE
) {
563 const struct gl_texture_image
*texImg
564 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
565 minWidth
= MIN2(minWidth
, texImg
->Width
);
566 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
567 minHeight
= MIN2(minHeight
, texImg
->Height
);
568 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
569 f
= texImg
->_BaseFormat
;
571 if (f
!= GL_RGB
&& f
!= GL_RGBA
&& f
!= GL_DEPTH_COMPONENT
572 && f
!= GL_DEPTH_STENCIL_EXT
) {
573 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
574 fbo_incomplete("texture attachment incomplete", -1);
578 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
579 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
580 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
581 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
582 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
583 f
= att
->Renderbuffer
->InternalFormat
;
587 assert(att
->Type
== GL_NONE
);
591 if (numSamples
< 0) {
593 numSamples
= att
->Renderbuffer
->NumSamples
;
596 /* Error-check width, height, format, samples
598 if (numImages
== 1) {
599 /* save format, num samples */
605 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
606 /* check that width, height, format are same */
607 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
608 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
609 fbo_incomplete("width or height mismatch", -1);
612 /* check that all color buffer have same format */
613 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
614 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
615 fbo_incomplete("format mismatch", -1);
619 if (att
->Renderbuffer
&&
620 att
->Renderbuffer
->NumSamples
!= numSamples
) {
621 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
622 fbo_incomplete("inconsistant number of samples", i
);
629 #ifndef FEATURE_OES_framebuffer_object
630 /* Check that all DrawBuffers are present */
631 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
632 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
633 const struct gl_renderbuffer_attachment
*att
634 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
636 if (att
->Type
== GL_NONE
) {
637 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
638 fbo_incomplete("missing drawbuffer", j
);
644 /* Check that the ReadBuffer is present */
645 if (fb
->ColorReadBuffer
!= GL_NONE
) {
646 const struct gl_renderbuffer_attachment
*att
647 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
649 if (att
->Type
== GL_NONE
) {
650 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
651 fbo_incomplete("missing readbuffer", -1);
657 if (numImages
== 0) {
658 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
659 fbo_incomplete("no attachments", -1);
663 /* Provisionally set status = COMPLETE ... */
664 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
666 /* ... but the driver may say the FB is incomplete.
667 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
670 if (ctx
->Driver
.ValidateFramebuffer
) {
671 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
672 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
673 fbo_incomplete("driver marked FBO as incomplete", -1);
677 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
679 * Note that if ARB_framebuffer_object is supported and the attached
680 * renderbuffers/textures are different sizes, the framebuffer
681 * width/height will be set to the smallest width/height.
683 fb
->Width
= minWidth
;
684 fb
->Height
= minHeight
;
686 /* finally, update the visual info for the framebuffer */
687 _mesa_update_framebuffer_visual(fb
);
693 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
695 GET_CURRENT_CONTEXT(ctx
);
696 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
698 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
699 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
707 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
709 struct gl_renderbuffer
*newRb
;
710 GET_CURRENT_CONTEXT(ctx
);
712 ASSERT_OUTSIDE_BEGIN_END(ctx
);
714 if (target
!= GL_RENDERBUFFER_EXT
) {
715 _mesa_error(ctx
, GL_INVALID_ENUM
,
716 "glBindRenderbufferEXT(target)");
720 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
723 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
724 if (newRb
== &DummyRenderbuffer
) {
725 /* ID was reserved, but no real renderbuffer object made yet */
728 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
729 /* All RB IDs must be Gen'd */
730 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
735 /* create new renderbuffer object */
736 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
738 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
741 ASSERT(newRb
->AllocStorage
);
742 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
743 newRb
->RefCount
= 1; /* referenced by hash table */
750 ASSERT(newRb
!= &DummyRenderbuffer
);
752 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
757 * If the given renderbuffer is anywhere attached to the framebuffer, detach
759 * This is used when a renderbuffer object is deleted.
760 * The spec calls for unbinding.
763 detach_renderbuffer(GLcontext
*ctx
,
764 struct gl_framebuffer
*fb
,
765 struct gl_renderbuffer
*rb
)
768 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
769 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
770 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
773 invalidate_framebuffer(fb
);
778 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
781 GET_CURRENT_CONTEXT(ctx
);
783 ASSERT_OUTSIDE_BEGIN_END(ctx
);
784 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
786 for (i
= 0; i
< n
; i
++) {
787 if (renderbuffers
[i
] > 0) {
788 struct gl_renderbuffer
*rb
;
789 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
791 /* check if deleting currently bound renderbuffer object */
792 if (rb
== ctx
->CurrentRenderbuffer
) {
794 ASSERT(rb
->RefCount
>= 2);
795 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
798 if (ctx
->DrawBuffer
->Name
) {
799 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
801 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
802 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
805 /* Remove from hash table immediately, to free the ID.
806 * But the object will not be freed until it's no longer
807 * referenced anywhere else.
809 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
811 if (rb
!= &DummyRenderbuffer
) {
812 /* no longer referenced by hash table */
813 _mesa_reference_renderbuffer(&rb
, NULL
);
822 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
824 GET_CURRENT_CONTEXT(ctx
);
828 ASSERT_OUTSIDE_BEGIN_END(ctx
);
831 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
838 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
840 for (i
= 0; i
< n
; i
++) {
841 GLuint name
= first
+ i
;
842 renderbuffers
[i
] = name
;
843 /* insert dummy placeholder into hash table */
844 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
845 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
846 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
852 * Given an internal format token for a render buffer, return the
853 * corresponding base format.
854 * This is very similar to _mesa_base_tex_format() but the set of valid
855 * internal formats is somewhat different.
857 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
858 * GL_DEPTH_STENCIL_EXT or zero if error.
861 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
863 switch (internalFormat
) {
882 case GL_STENCIL_INDEX
:
883 case GL_STENCIL_INDEX1_EXT
:
884 case GL_STENCIL_INDEX4_EXT
:
885 case GL_STENCIL_INDEX8_EXT
:
886 case GL_STENCIL_INDEX16_EXT
:
887 return GL_STENCIL_INDEX
;
888 case GL_DEPTH_COMPONENT
:
889 case GL_DEPTH_COMPONENT16
:
890 case GL_DEPTH_COMPONENT24
:
891 case GL_DEPTH_COMPONENT32
:
892 return GL_DEPTH_COMPONENT
;
893 case GL_DEPTH_STENCIL_EXT
:
894 case GL_DEPTH24_STENCIL8_EXT
:
895 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
896 return GL_DEPTH_STENCIL_EXT
;
899 /* XXX add floating point formats eventually */
906 /** sentinal value, see below */
907 #define NO_SAMPLES 1000
911 * Helper function used by _mesa_RenderbufferStorageEXT() and
912 * _mesa_RenderbufferStorageMultisample().
913 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
916 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
917 GLsizei width
, GLsizei height
, GLsizei samples
)
919 const char *func
= samples
== NO_SAMPLES
?
920 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
921 struct gl_renderbuffer
*rb
;
923 GET_CURRENT_CONTEXT(ctx
);
925 ASSERT_OUTSIDE_BEGIN_END(ctx
);
927 if (target
!= GL_RENDERBUFFER_EXT
) {
928 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
932 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
933 if (baseFormat
== 0) {
934 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
938 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
939 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
943 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
944 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
948 if (samples
== NO_SAMPLES
) {
949 /* NumSamples == 0 indicates non-multisampling */
952 else if (samples
> ctx
->Const
.MaxSamples
) {
953 /* note: driver may choose to use more samples than what's requested */
954 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
958 rb
= ctx
->CurrentRenderbuffer
;
960 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
964 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
966 if (rb
->InternalFormat
== internalFormat
&&
967 rb
->Width
== (GLuint
) width
&&
968 rb
->Height
== (GLuint
) height
) {
969 /* no change in allocation needed */
973 /* These MUST get set by the AllocStorage func */
974 rb
->_ActualFormat
= 0;
982 rb
->NumSamples
= samples
;
984 /* Now allocate the storage */
985 ASSERT(rb
->AllocStorage
);
986 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
987 /* No error - check/set fields now */
988 assert(rb
->_ActualFormat
);
989 assert(rb
->Width
== (GLuint
) width
);
990 assert(rb
->Height
== (GLuint
) height
);
991 assert(rb
->RedBits
|| rb
->GreenBits
|| rb
->BlueBits
|| rb
->AlphaBits
||
992 rb
->DepthBits
|| rb
->StencilBits
|| rb
->IndexBits
);
993 rb
->InternalFormat
= internalFormat
;
994 rb
->_BaseFormat
= baseFormat
;
997 /* Probably ran out of memory - clear the fields */
1000 rb
->InternalFormat
= GL_NONE
;
1001 rb
->_ActualFormat
= GL_NONE
;
1002 rb
->_BaseFormat
= GL_NONE
;
1014 test_framebuffer_completeness(ctx, fb);
1016 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1023 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1024 GLsizei width
, GLsizei height
)
1026 /* GL_ARB_fbo says calling this function is equivalent to calling
1027 * glRenderbufferStorageMultisample() with samples=0. We pass in
1028 * a token value here just for error reporting purposes.
1030 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1035 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1036 GLenum internalFormat
,
1037 GLsizei width
, GLsizei height
)
1039 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1045 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1047 struct gl_renderbuffer
*rb
;
1048 GET_CURRENT_CONTEXT(ctx
);
1050 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1052 if (target
!= GL_RENDERBUFFER_EXT
) {
1053 _mesa_error(ctx
, GL_INVALID_ENUM
,
1054 "glGetRenderbufferParameterivEXT(target)");
1058 rb
= ctx
->CurrentRenderbuffer
;
1060 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1061 "glGetRenderbufferParameterivEXT");
1065 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1068 case GL_RENDERBUFFER_WIDTH_EXT
:
1069 *params
= rb
->Width
;
1071 case GL_RENDERBUFFER_HEIGHT_EXT
:
1072 *params
= rb
->Height
;
1074 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1075 *params
= rb
->InternalFormat
;
1077 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1078 *params
= rb
->RedBits
;
1080 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1081 *params
= rb
->GreenBits
;
1083 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1084 *params
= rb
->BlueBits
;
1086 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1087 *params
= rb
->AlphaBits
;
1089 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1090 *params
= rb
->DepthBits
;
1092 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1093 *params
= rb
->StencilBits
;
1095 case GL_RENDERBUFFER_SAMPLES
:
1096 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1097 *params
= rb
->NumSamples
;
1102 _mesa_error(ctx
, GL_INVALID_ENUM
,
1103 "glGetRenderbufferParameterivEXT(target)");
1109 GLboolean GLAPIENTRY
1110 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1112 GET_CURRENT_CONTEXT(ctx
);
1113 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1115 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1116 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1124 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1127 ASSERT(ctx
->Driver
.RenderTexture
);
1128 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1129 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1130 struct gl_texture_object
*texObj
= att
->Texture
;
1132 && att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1133 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1140 * Examine all the framebuffer's attachments to see if any are textures.
1141 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1142 * notify the device driver that the texture image may have changed.
1145 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1147 if (ctx
->Driver
.FinishRenderTexture
) {
1149 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1150 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1151 if (att
->Texture
&& att
->Renderbuffer
) {
1152 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1160 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1162 struct gl_framebuffer
*newFb
, *newFbread
;
1163 GLboolean bindReadBuf
, bindDrawBuf
;
1164 GET_CURRENT_CONTEXT(ctx
);
1167 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1168 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1169 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1173 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1175 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1176 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1177 "glBindFramebufferEXT(unsupported)");
1182 #if FEATURE_EXT_framebuffer_blit
1183 case GL_DRAW_FRAMEBUFFER_EXT
:
1184 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1185 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1188 bindDrawBuf
= GL_TRUE
;
1189 bindReadBuf
= GL_FALSE
;
1191 case GL_READ_FRAMEBUFFER_EXT
:
1192 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1193 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1196 bindDrawBuf
= GL_FALSE
;
1197 bindReadBuf
= GL_TRUE
;
1200 case GL_FRAMEBUFFER_EXT
:
1201 bindDrawBuf
= GL_TRUE
;
1202 bindReadBuf
= GL_TRUE
;
1205 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1209 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1212 /* Binding a user-created framebuffer object */
1213 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1214 if (newFb
== &DummyFramebuffer
) {
1215 /* ID was reserved, but no real framebuffer object made yet */
1218 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1219 /* All FBO IDs must be Gen'd */
1220 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1225 /* create new framebuffer object */
1226 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1228 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1231 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1236 /* Binding the window system framebuffer (which was originally set
1237 * with MakeCurrent).
1239 newFb
= ctx
->WinSysDrawBuffer
;
1240 newFbread
= ctx
->WinSysReadBuffer
;
1244 ASSERT(newFb
!= &DummyFramebuffer
);
1247 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1251 if (ctx
->ReadBuffer
== newFbread
)
1252 bindReadBuf
= GL_FALSE
; /* no change */
1254 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1258 /* check if old FB had any texture attachments */
1259 if (ctx
->DrawBuffer
->Name
!= 0) {
1260 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1263 if (ctx
->DrawBuffer
== newFb
)
1264 bindDrawBuf
= GL_FALSE
; /* no change */
1266 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1268 if (newFb
->Name
!= 0) {
1269 /* check if newly bound framebuffer has any texture attachments */
1270 check_begin_texture_render(ctx
, newFb
);
1274 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1275 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1281 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1284 GET_CURRENT_CONTEXT(ctx
);
1286 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1287 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1289 for (i
= 0; i
< n
; i
++) {
1290 if (framebuffers
[i
] > 0) {
1291 struct gl_framebuffer
*fb
;
1292 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1294 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1296 /* check if deleting currently bound framebuffer object */
1297 if (fb
== ctx
->DrawBuffer
) {
1299 ASSERT(fb
->RefCount
>= 2);
1300 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1302 if (fb
== ctx
->ReadBuffer
) {
1304 ASSERT(fb
->RefCount
>= 2);
1305 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1308 /* remove from hash table immediately, to free the ID */
1309 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1311 if (fb
!= &DummyFramebuffer
) {
1312 /* But the object will not be freed until it's no longer
1313 * bound in any context.
1315 _mesa_reference_framebuffer(&fb
, NULL
);
1324 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1326 GET_CURRENT_CONTEXT(ctx
);
1330 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1333 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1340 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1342 for (i
= 0; i
< n
; i
++) {
1343 GLuint name
= first
+ i
;
1344 framebuffers
[i
] = name
;
1345 /* insert dummy placeholder into hash table */
1346 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1347 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1348 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1355 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1357 struct gl_framebuffer
*buffer
;
1358 GET_CURRENT_CONTEXT(ctx
);
1360 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1363 #if FEATURE_EXT_framebuffer_blit
1364 case GL_DRAW_FRAMEBUFFER_EXT
:
1365 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1366 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1369 buffer
= ctx
->DrawBuffer
;
1371 case GL_READ_FRAMEBUFFER_EXT
:
1372 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1373 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1376 buffer
= ctx
->ReadBuffer
;
1379 case GL_FRAMEBUFFER_EXT
:
1380 buffer
= ctx
->DrawBuffer
;
1383 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1384 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1387 if (buffer
->Name
== 0) {
1388 /* The window system / default framebuffer is always complete */
1389 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1392 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1394 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1395 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1398 return buffer
->_Status
;
1404 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1407 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1408 GLenum attachment
, GLenum textarget
, GLuint texture
,
1409 GLint level
, GLint zoffset
)
1411 struct gl_renderbuffer_attachment
*att
;
1412 struct gl_texture_object
*texObj
= NULL
;
1413 struct gl_framebuffer
*fb
;
1414 GLboolean error
= GL_FALSE
;
1416 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1419 case GL_READ_FRAMEBUFFER_EXT
:
1420 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1421 fb
= ctx
->ReadBuffer
;
1423 case GL_DRAW_FRAMEBUFFER_EXT
:
1424 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1426 case GL_FRAMEBUFFER_EXT
:
1427 fb
= ctx
->DrawBuffer
;
1434 _mesa_error(ctx
, GL_INVALID_ENUM
,
1435 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1441 /* check framebuffer binding */
1442 if (fb
->Name
== 0) {
1443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1444 "glFramebufferTexture%sEXT", caller
);
1449 /* The textarget, level, and zoffset parameters are only validated if
1450 * texture is non-zero.
1453 GLboolean err
= GL_TRUE
;
1455 texObj
= _mesa_lookup_texture(ctx
, texture
);
1456 if (texObj
!= NULL
) {
1457 if (textarget
== 0) {
1458 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1459 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1460 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1463 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1464 ? !IS_CUBE_FACE(textarget
)
1465 : (texObj
->Target
!= textarget
);
1470 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1471 "glFramebufferTexture%sEXT(texture target mismatch)",
1476 if (texObj
->Target
== GL_TEXTURE_3D
) {
1477 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1478 if (zoffset
< 0 || zoffset
>= maxSize
) {
1479 _mesa_error(ctx
, GL_INVALID_VALUE
,
1480 "glFramebufferTexture%sEXT(zoffset)", caller
);
1484 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1485 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1486 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1487 _mesa_error(ctx
, GL_INVALID_VALUE
,
1488 "glFramebufferTexture%sEXT(layer)", caller
);
1494 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1495 _mesa_error(ctx
, GL_INVALID_VALUE
,
1496 "glFramebufferTexture%sEXT(level)", caller
);
1501 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1503 _mesa_error(ctx
, GL_INVALID_ENUM
,
1504 "glFramebufferTexture%sEXT(attachment)", caller
);
1508 if (texObj
&& attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1509 /* the texture format must be depth+stencil */
1510 const struct gl_texture_image
*texImg
;
1511 texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
1512 if (!texImg
|| texImg
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1513 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1514 "glFramebufferTexture%sEXT(texture is not"
1515 " DEPTH_STENCIL format)", caller
);
1520 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1522 _glthread_LOCK_MUTEX(fb
->Mutex
);
1524 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1526 /* Set the render-to-texture flag. We'll check this flag in
1527 * glTexImage() and friends to determine if we need to revalidate
1528 * any FBOs that might be rendering into this texture.
1529 * This flag never gets cleared since it's non-trivial to determine
1530 * when all FBOs might be done rendering to this texture. That's OK
1531 * though since it's uncommon to render to a texture then repeatedly
1532 * call glTexImage() to change images in the texture.
1534 texObj
->_RenderToTexture
= GL_TRUE
;
1537 _mesa_remove_attachment(ctx
, att
);
1540 invalidate_framebuffer(fb
);
1542 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1548 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1549 GLenum textarget
, GLuint texture
, GLint level
)
1551 GET_CURRENT_CONTEXT(ctx
);
1553 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1554 _mesa_error(ctx
, GL_INVALID_ENUM
,
1555 "glFramebufferTexture1DEXT(textarget)");
1559 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1565 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1566 GLenum textarget
, GLuint texture
, GLint level
)
1568 GET_CURRENT_CONTEXT(ctx
);
1570 if ((texture
!= 0) &&
1571 (textarget
!= GL_TEXTURE_2D
) &&
1572 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1573 (!IS_CUBE_FACE(textarget
))) {
1574 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1575 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1579 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1585 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1586 GLenum textarget
, GLuint texture
,
1587 GLint level
, GLint zoffset
)
1589 GET_CURRENT_CONTEXT(ctx
);
1591 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1592 _mesa_error(ctx
, GL_INVALID_ENUM
,
1593 "glFramebufferTexture3DEXT(textarget)");
1597 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1603 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1604 GLuint texture
, GLint level
, GLint layer
)
1606 GET_CURRENT_CONTEXT(ctx
);
1608 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1614 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1615 GLenum renderbufferTarget
,
1616 GLuint renderbuffer
)
1618 struct gl_renderbuffer_attachment
*att
;
1619 struct gl_framebuffer
*fb
;
1620 struct gl_renderbuffer
*rb
;
1621 GET_CURRENT_CONTEXT(ctx
);
1623 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1626 #if FEATURE_EXT_framebuffer_blit
1627 case GL_DRAW_FRAMEBUFFER_EXT
:
1628 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1629 _mesa_error(ctx
, GL_INVALID_ENUM
,
1630 "glFramebufferRenderbufferEXT(target)");
1633 fb
= ctx
->DrawBuffer
;
1635 case GL_READ_FRAMEBUFFER_EXT
:
1636 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1637 _mesa_error(ctx
, GL_INVALID_ENUM
,
1638 "glFramebufferRenderbufferEXT(target)");
1641 fb
= ctx
->ReadBuffer
;
1644 case GL_FRAMEBUFFER_EXT
:
1645 fb
= ctx
->DrawBuffer
;
1648 _mesa_error(ctx
, GL_INVALID_ENUM
,
1649 "glFramebufferRenderbufferEXT(target)");
1653 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1654 _mesa_error(ctx
, GL_INVALID_ENUM
,
1655 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1659 if (fb
->Name
== 0) {
1660 /* Can't attach new renderbuffers to a window system framebuffer */
1661 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1665 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1667 _mesa_error(ctx
, GL_INVALID_ENUM
,
1668 "glFramebufferRenderbufferEXT(attachment)");
1673 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1675 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1676 "glFramebufferRenderbufferEXT(renderbuffer)");
1681 /* remove renderbuffer attachment */
1685 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1686 /* make sure the renderbuffer is a depth/stencil format */
1687 if (rb
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1688 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1689 "glFramebufferRenderbufferEXT(renderbuffer"
1690 " is not DEPTH_STENCIL format)");
1696 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1698 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1699 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1701 /* Some subsequent GL commands may depend on the framebuffer's visual
1702 * after the binding is updated. Update visual info now.
1704 _mesa_update_framebuffer_visual(fb
);
1709 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1710 GLenum pname
, GLint
*params
)
1712 const struct gl_renderbuffer_attachment
*att
;
1713 struct gl_framebuffer
*buffer
;
1714 GET_CURRENT_CONTEXT(ctx
);
1716 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1719 #if FEATURE_EXT_framebuffer_blit
1720 case GL_DRAW_FRAMEBUFFER_EXT
:
1721 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1722 _mesa_error(ctx
, GL_INVALID_ENUM
,
1723 "glGetFramebufferAttachmentParameterivEXT(target)");
1726 buffer
= ctx
->DrawBuffer
;
1728 case GL_READ_FRAMEBUFFER_EXT
:
1729 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1730 _mesa_error(ctx
, GL_INVALID_ENUM
,
1731 "glGetFramebufferAttachmentParameterivEXT(target)");
1734 buffer
= ctx
->ReadBuffer
;
1737 case GL_FRAMEBUFFER_EXT
:
1738 buffer
= ctx
->DrawBuffer
;
1741 _mesa_error(ctx
, GL_INVALID_ENUM
,
1742 "glGetFramebufferAttachmentParameterivEXT(target)");
1746 if (buffer
->Name
== 0) {
1747 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1748 "glGetFramebufferAttachmentParameterivEXT");
1752 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1754 _mesa_error(ctx
, GL_INVALID_ENUM
,
1755 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1759 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1760 /* the depth and stencil attachments must point to the same buffer */
1761 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1762 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1763 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1764 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1765 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1766 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1767 " attachments differ)");
1772 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1775 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1776 *params
= att
->Type
;
1778 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1779 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1780 *params
= att
->Renderbuffer
->Name
;
1782 else if (att
->Type
== GL_TEXTURE
) {
1783 *params
= att
->Texture
->Name
;
1786 _mesa_error(ctx
, GL_INVALID_ENUM
,
1787 "glGetFramebufferAttachmentParameterivEXT(pname)");
1790 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1791 if (att
->Type
== GL_TEXTURE
) {
1792 *params
= att
->TextureLevel
;
1795 _mesa_error(ctx
, GL_INVALID_ENUM
,
1796 "glGetFramebufferAttachmentParameterivEXT(pname)");
1799 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1800 if (att
->Type
== GL_TEXTURE
) {
1801 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1802 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1809 _mesa_error(ctx
, GL_INVALID_ENUM
,
1810 "glGetFramebufferAttachmentParameterivEXT(pname)");
1813 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1814 if (att
->Type
== GL_TEXTURE
) {
1815 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1816 *params
= att
->Zoffset
;
1823 _mesa_error(ctx
, GL_INVALID_ENUM
,
1824 "glGetFramebufferAttachmentParameterivEXT(pname)");
1827 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1828 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1829 _mesa_error(ctx
, GL_INVALID_ENUM
,
1830 "glGetFramebufferAttachmentParameterivEXT(pname)");
1833 *params
= att
->Renderbuffer
->ColorEncoding
;
1836 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1837 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1838 _mesa_error(ctx
, GL_INVALID_ENUM
,
1839 "glGetFramebufferAttachmentParameterivEXT(pname)");
1843 *params
= att
->Renderbuffer
->ComponentType
;
1846 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1847 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1848 _mesa_error(ctx
, GL_INVALID_ENUM
,
1849 "glGetFramebufferAttachmentParameterivEXT(pname)");
1852 *params
= att
->Renderbuffer
->RedBits
;
1855 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1856 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1857 _mesa_error(ctx
, GL_INVALID_ENUM
,
1858 "glGetFramebufferAttachmentParameterivEXT(pname)");
1861 *params
= att
->Renderbuffer
->GreenBits
;
1864 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1865 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1866 _mesa_error(ctx
, GL_INVALID_ENUM
,
1867 "glGetFramebufferAttachmentParameterivEXT(pname)");
1870 *params
= att
->Renderbuffer
->BlueBits
;
1873 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1874 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1875 _mesa_error(ctx
, GL_INVALID_ENUM
,
1876 "glGetFramebufferAttachmentParameterivEXT(pname)");
1879 *params
= att
->Renderbuffer
->AlphaBits
;
1882 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1883 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1884 _mesa_error(ctx
, GL_INVALID_ENUM
,
1885 "glGetFramebufferAttachmentParameterivEXT(pname)");
1888 *params
= att
->Renderbuffer
->DepthBits
;
1891 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1892 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1893 _mesa_error(ctx
, GL_INVALID_ENUM
,
1894 "glGetFramebufferAttachmentParameterivEXT(pname)");
1897 *params
= att
->Renderbuffer
->StencilBits
;
1901 _mesa_error(ctx
, GL_INVALID_ENUM
,
1902 "glGetFramebufferAttachmentParameterivEXT(pname)");
1909 _mesa_GenerateMipmapEXT(GLenum target
)
1911 struct gl_texture_unit
*texUnit
;
1912 struct gl_texture_object
*texObj
;
1913 GET_CURRENT_CONTEXT(ctx
);
1915 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1916 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1922 case GL_TEXTURE_CUBE_MAP
:
1923 /* OK, legal value */
1926 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1930 texUnit
= _mesa_get_current_tex_unit(ctx
);
1931 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1933 _mesa_lock_texture(ctx
, texObj
);
1934 if (target
== GL_TEXTURE_CUBE_MAP
) {
1936 for (face
= 0; face
< 6; face
++)
1937 ctx
->Driver
.GenerateMipmap(ctx
,
1938 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1942 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1944 _mesa_unlock_texture(ctx
, texObj
);
1948 #if FEATURE_EXT_framebuffer_blit
1950 * Blit rectangular region, optionally from one framebuffer to another.
1952 * Note, if the src buffer is multisampled and the dest is not, this is
1953 * when the samples must be resolved to a single color.
1956 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1957 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1958 GLbitfield mask
, GLenum filter
)
1960 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1961 GL_DEPTH_BUFFER_BIT
|
1962 GL_STENCIL_BUFFER_BIT
);
1963 const struct gl_framebuffer
*readFb
, *drawFb
;
1964 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1965 GET_CURRENT_CONTEXT(ctx
);
1967 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1968 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1970 if (ctx
->NewState
) {
1971 _mesa_update_state(ctx
);
1974 readFb
= ctx
->ReadBuffer
;
1975 drawFb
= ctx
->DrawBuffer
;
1977 if (!readFb
|| !drawFb
) {
1978 /* This will normally never happen but someday we may want to
1979 * support MakeCurrent() with no drawables.
1984 /* check for complete framebuffers */
1985 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
1986 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1987 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1988 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1992 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
1993 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
1997 if (mask
& ~legalMaskBits
) {
1998 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2002 /* depth/stencil must be blitted with nearest filtering */
2003 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2004 && filter
!= GL_NEAREST
) {
2005 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2006 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2010 /* get color read/draw renderbuffers */
2011 if (mask
& GL_COLOR_BUFFER_BIT
) {
2012 colorReadRb
= readFb
->_ColorReadBuffer
;
2013 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2016 colorReadRb
= colorDrawRb
= NULL
;
2019 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2020 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2021 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2024 readRb
->StencilBits
!= drawRb
->StencilBits
) {
2025 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2026 "glBlitFramebufferEXT(stencil buffer size mismatch");
2031 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2032 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2033 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2036 readRb
->DepthBits
!= drawRb
->DepthBits
) {
2037 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2038 "glBlitFramebufferEXT(depth buffer size mismatch");
2043 if (readFb
->Visual
.samples
> 0 &&
2044 drawFb
->Visual
.samples
> 0 &&
2045 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2046 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2047 "glBlitFramebufferEXT(mismatched samples");
2051 /* extra checks for multisample copies... */
2052 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2053 /* src and dest region sizes must be the same */
2054 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2055 srcY1
- srcY0
!= dstY1
- dstY0
) {
2056 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2057 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2061 /* color formats must match */
2064 colorReadRb
->_ActualFormat
!= colorDrawRb
->_ActualFormat
) {
2065 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2066 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2071 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2072 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2076 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2077 ctx
->Driver
.BlitFramebuffer(ctx
,
2078 srcX0
, srcY0
, srcX1
, srcY1
,
2079 dstX0
, dstY0
, dstX1
, dstY1
,
2082 #endif /* FEATURE_EXT_framebuffer_blit */