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"
49 /** Set this to 1 to help debug FBO incompleteness problems */
56 * None of the GL_EXT_framebuffer_object functions are compiled into
63 * When glGenRender/FramebuffersEXT() is called we insert pointers to
64 * these placeholder objects into the hash table.
65 * Later, when the object ID is first bound, we replace the placeholder
66 * with the real frame/renderbuffer.
68 static struct gl_framebuffer DummyFramebuffer
;
69 static struct gl_renderbuffer DummyRenderbuffer
;
72 #define IS_CUBE_FACE(TARGET) \
73 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
74 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
78 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
84 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
91 _mesa_init_fbobjects(GLcontext
*ctx
)
93 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
94 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
99 * Helper routine for getting a gl_renderbuffer.
101 struct gl_renderbuffer
*
102 _mesa_lookup_renderbuffer(GLcontext
*ctx
, GLuint id
)
104 struct gl_renderbuffer
*rb
;
109 rb
= (struct gl_renderbuffer
*)
110 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
116 * Helper routine for getting a gl_framebuffer.
118 struct gl_framebuffer
*
119 _mesa_lookup_framebuffer(GLcontext
*ctx
, GLuint id
)
121 struct gl_framebuffer
*fb
;
126 fb
= (struct gl_framebuffer
*)
127 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
133 * Mark the given framebuffer as invalid. This will force the
134 * test for framebuffer completeness to be done before the framebuffer
138 invalidate_framebuffer(struct gl_framebuffer
*fb
)
140 fb
->_Status
= 0; /* "indeterminate" */
145 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
146 * gl_renderbuffer_attachment object.
147 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
148 * the depth buffer attachment point.
150 struct gl_renderbuffer_attachment
*
151 _mesa_get_attachment(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
156 switch (attachment
) {
157 case GL_COLOR_ATTACHMENT0_EXT
:
158 case GL_COLOR_ATTACHMENT1_EXT
:
159 case GL_COLOR_ATTACHMENT2_EXT
:
160 case GL_COLOR_ATTACHMENT3_EXT
:
161 case GL_COLOR_ATTACHMENT4_EXT
:
162 case GL_COLOR_ATTACHMENT5_EXT
:
163 case GL_COLOR_ATTACHMENT6_EXT
:
164 case GL_COLOR_ATTACHMENT7_EXT
:
165 case GL_COLOR_ATTACHMENT8_EXT
:
166 case GL_COLOR_ATTACHMENT9_EXT
:
167 case GL_COLOR_ATTACHMENT10_EXT
:
168 case GL_COLOR_ATTACHMENT11_EXT
:
169 case GL_COLOR_ATTACHMENT12_EXT
:
170 case GL_COLOR_ATTACHMENT13_EXT
:
171 case GL_COLOR_ATTACHMENT14_EXT
:
172 case GL_COLOR_ATTACHMENT15_EXT
:
173 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
174 if (i
>= ctx
->Const
.MaxColorAttachments
) {
177 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
178 case GL_DEPTH_STENCIL_ATTACHMENT
:
180 case GL_DEPTH_ATTACHMENT_EXT
:
181 return &fb
->Attachment
[BUFFER_DEPTH
];
182 case GL_STENCIL_ATTACHMENT_EXT
:
183 return &fb
->Attachment
[BUFFER_STENCIL
];
191 * Remove any texture or renderbuffer attached to the given attachment
192 * point. Update reference counts, etc.
195 _mesa_remove_attachment(GLcontext
*ctx
, struct gl_renderbuffer_attachment
*att
)
197 if (att
->Type
== GL_TEXTURE
) {
198 ASSERT(att
->Texture
);
199 if (ctx
->Driver
.FinishRenderTexture
) {
200 /* tell driver that we're done rendering to this texture. */
201 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
203 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
204 ASSERT(!att
->Texture
);
206 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
207 ASSERT(!att
->Texture
);
208 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
209 ASSERT(!att
->Renderbuffer
);
212 att
->Complete
= GL_TRUE
;
217 * Bind a texture object to an attachment point.
218 * The previous binding, if any, will be removed first.
221 _mesa_set_texture_attachment(GLcontext
*ctx
,
222 struct gl_framebuffer
*fb
,
223 struct gl_renderbuffer_attachment
*att
,
224 struct gl_texture_object
*texObj
,
225 GLenum texTarget
, GLuint level
, GLuint zoffset
)
227 if (att
->Texture
== texObj
) {
228 /* re-attaching same texture */
229 ASSERT(att
->Type
== GL_TEXTURE
);
233 _mesa_remove_attachment(ctx
, att
);
234 att
->Type
= GL_TEXTURE
;
235 assert(!att
->Texture
);
236 _mesa_reference_texobj(&att
->Texture
, texObj
);
239 /* always update these fields */
240 att
->TextureLevel
= level
;
241 if (IS_CUBE_FACE(texTarget
)) {
242 att
->CubeMapFace
= texTarget
- GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
245 att
->CubeMapFace
= 0;
247 att
->Zoffset
= zoffset
;
248 att
->Complete
= GL_FALSE
;
250 if (att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
251 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
254 invalidate_framebuffer(fb
);
259 * Bind a renderbuffer to an attachment point.
260 * The previous binding, if any, will be removed first.
263 _mesa_set_renderbuffer_attachment(GLcontext
*ctx
,
264 struct gl_renderbuffer_attachment
*att
,
265 struct gl_renderbuffer
*rb
)
267 /* XXX check if re-doing same attachment, exit early */
268 _mesa_remove_attachment(ctx
, att
);
269 att
->Type
= GL_RENDERBUFFER_EXT
;
270 att
->Texture
= NULL
; /* just to be safe */
271 att
->Complete
= GL_FALSE
;
272 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
277 * Fallback for ctx->Driver.FramebufferRenderbuffer()
278 * Attach a renderbuffer object to a framebuffer object.
281 _mesa_framebuffer_renderbuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
282 GLenum attachment
, struct gl_renderbuffer
*rb
)
284 struct gl_renderbuffer_attachment
*att
;
286 _glthread_LOCK_MUTEX(fb
->Mutex
);
288 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
291 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
292 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
293 /* do stencil attachment here (depth already done above) */
294 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
296 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
300 _mesa_remove_attachment(ctx
, att
);
303 invalidate_framebuffer(fb
);
305 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
313 att_incomplete(const char *msg
)
316 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
327 fbo_incomplete(const char *msg
, int index
)
330 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
341 * Test if an attachment point is complete and update its Complete field.
342 * \param format if GL_COLOR, this is a color attachment point,
343 * if GL_DEPTH, this is a depth component attachment point,
344 * if GL_STENCIL, this is a stencil component attachment point.
347 test_attachment_completeness(const GLcontext
*ctx
, GLenum format
,
348 struct gl_renderbuffer_attachment
*att
)
350 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
352 /* assume complete */
353 att
->Complete
= GL_TRUE
;
355 /* Look for reasons why the attachment might be incomplete */
356 if (att
->Type
== GL_TEXTURE
) {
357 const struct gl_texture_object
*texObj
= att
->Texture
;
358 struct gl_texture_image
*texImage
;
361 att_incomplete("no texobj");
362 att
->Complete
= GL_FALSE
;
366 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
368 att_incomplete("no teximage");
369 att
->Complete
= GL_FALSE
;
372 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
373 att_incomplete("teximage width/height=0");
374 _mesa_printf("texobj = %u\n", texObj
->Name
);
375 _mesa_printf("level = %d\n", att
->TextureLevel
);
376 att
->Complete
= GL_FALSE
;
379 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
380 att_incomplete("bad z offset");
381 att
->Complete
= GL_FALSE
;
385 if (format
== GL_COLOR
) {
386 if (texImage
->TexFormat
->BaseFormat
!= GL_RGB
&&
387 texImage
->TexFormat
->BaseFormat
!= GL_RGBA
) {
388 att_incomplete("bad format");
389 att
->Complete
= GL_FALSE
;
392 if (texImage
->TexFormat
->TexelBytes
== 0) {
393 att_incomplete("compressed internalformat");
394 att
->Complete
= GL_FALSE
;
398 else if (format
== GL_DEPTH
) {
399 if (texImage
->TexFormat
->BaseFormat
== GL_DEPTH_COMPONENT
) {
402 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
403 ctx
->Extensions
.ARB_depth_texture
&&
404 texImage
->TexFormat
->BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
408 att
->Complete
= GL_FALSE
;
409 att_incomplete("bad depth format");
414 ASSERT(format
== GL_STENCIL
);
415 ASSERT(att
->Renderbuffer
->StencilBits
);
416 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
417 ctx
->Extensions
.ARB_depth_texture
&&
418 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
422 /* no such thing as stencil-only textures */
423 att_incomplete("illegal stencil texture");
424 att
->Complete
= GL_FALSE
;
429 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
430 ASSERT(att
->Renderbuffer
);
431 if (!att
->Renderbuffer
->InternalFormat
||
432 att
->Renderbuffer
->Width
< 1 ||
433 att
->Renderbuffer
->Height
< 1) {
434 att_incomplete("0x0 renderbuffer");
435 att
->Complete
= GL_FALSE
;
438 if (format
== GL_COLOR
) {
439 if (att
->Renderbuffer
->_BaseFormat
!= GL_RGB
&&
440 att
->Renderbuffer
->_BaseFormat
!= GL_RGBA
) {
441 att_incomplete("bad renderbuffer color format");
442 att
->Complete
= GL_FALSE
;
445 ASSERT(att
->Renderbuffer
->RedBits
);
446 ASSERT(att
->Renderbuffer
->GreenBits
);
447 ASSERT(att
->Renderbuffer
->BlueBits
);
449 else if (format
== GL_DEPTH
) {
450 if (att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
451 ASSERT(att
->Renderbuffer
->DepthBits
);
454 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
455 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
456 ASSERT(att
->Renderbuffer
->DepthBits
);
460 att_incomplete("bad renderbuffer depth format");
461 att
->Complete
= GL_FALSE
;
466 assert(format
== GL_STENCIL
);
467 if (att
->Renderbuffer
->_BaseFormat
== GL_STENCIL_INDEX
) {
468 ASSERT(att
->Renderbuffer
->StencilBits
);
471 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
472 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
473 ASSERT(att
->Renderbuffer
->StencilBits
);
477 att
->Complete
= GL_FALSE
;
478 att_incomplete("bad renderbuffer stencil format");
484 ASSERT(att
->Type
== GL_NONE
);
492 * Test if the given framebuffer object is complete and update its
493 * Status field with the results.
494 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
495 * driver to make hardware-specific validation/completeness checks.
496 * Also update the framebuffer's Width and Height fields if the
497 * framebuffer is complete.
500 _mesa_test_framebuffer_completeness(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
503 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
504 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
505 GLint numSamples
= -1;
509 assert(fb
->Name
!= 0);
515 /* Start at -2 to more easily loop over all attachment points.
520 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
521 struct gl_renderbuffer_attachment
*att
;
525 * XXX for ARB_fbo, only check color buffers that are named by
526 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
529 /* check for attachment completeness
532 att
= &fb
->Attachment
[BUFFER_DEPTH
];
533 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
534 if (!att
->Complete
) {
535 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
536 fbo_incomplete("depth attachment incomplete", -1);
541 att
= &fb
->Attachment
[BUFFER_STENCIL
];
542 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
543 if (!att
->Complete
) {
544 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
545 fbo_incomplete("stencil attachment incomplete", -1);
550 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
551 test_attachment_completeness(ctx
, GL_COLOR
, att
);
552 if (!att
->Complete
) {
553 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
554 fbo_incomplete("color attachment incomplete", i
);
559 /* get width, height, format of the renderbuffer/texture
561 if (att
->Type
== GL_TEXTURE
) {
562 const struct gl_texture_image
*texImg
563 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
564 minWidth
= MIN2(minWidth
, texImg
->Width
);
565 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
566 minHeight
= MIN2(minHeight
, texImg
->Height
);
567 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
568 f
= texImg
->_BaseFormat
;
570 if (f
!= GL_RGB
&& f
!= GL_RGBA
&& f
!= GL_DEPTH_COMPONENT
571 && f
!= GL_DEPTH_STENCIL_EXT
) {
572 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
573 fbo_incomplete("texture attachment incomplete", -1);
577 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
578 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
579 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
580 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
581 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
582 f
= att
->Renderbuffer
->InternalFormat
;
586 assert(att
->Type
== GL_NONE
);
590 if (numSamples
< 0) {
592 numSamples
= att
->Renderbuffer
->NumSamples
;
595 /* Error-check width, height, format, samples
597 if (numImages
== 1) {
598 /* save format, num samples */
604 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
605 /* check that width, height, format are same */
606 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
607 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
608 fbo_incomplete("width or height mismatch", -1);
611 /* check that all color buffer have same format */
612 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
613 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
614 fbo_incomplete("format mismatch", -1);
618 if (att
->Renderbuffer
&&
619 att
->Renderbuffer
->NumSamples
!= numSamples
) {
620 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
621 fbo_incomplete("inconsistant number of samples", i
);
628 #ifndef FEATURE_OES_framebuffer_object
629 /* Check that all DrawBuffers are present */
630 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
631 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
632 const struct gl_renderbuffer_attachment
*att
633 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
635 if (att
->Type
== GL_NONE
) {
636 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
637 fbo_incomplete("missing drawbuffer", j
);
643 /* Check that the ReadBuffer is present */
644 if (fb
->ColorReadBuffer
!= GL_NONE
) {
645 const struct gl_renderbuffer_attachment
*att
646 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
648 if (att
->Type
== GL_NONE
) {
649 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
650 fbo_incomplete("missing readbuffer", -1);
656 if (numImages
== 0) {
657 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
658 fbo_incomplete("no attachments", -1);
662 /* Provisionally set status = COMPLETE ... */
663 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
665 /* ... but the driver may say the FB is incomplete.
666 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
669 if (ctx
->Driver
.ValidateFramebuffer
) {
670 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
671 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
672 fbo_incomplete("driver marked FBO as incomplete", -1);
676 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
678 * Note that if ARB_framebuffer_object is supported and the attached
679 * renderbuffers/textures are different sizes, the framebuffer
680 * width/height will be set to the smallest width/height.
682 fb
->Width
= minWidth
;
683 fb
->Height
= minHeight
;
685 /* finally, update the visual info for the framebuffer */
686 _mesa_update_framebuffer_visual(fb
);
692 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
694 GET_CURRENT_CONTEXT(ctx
);
695 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
697 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
698 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
706 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
708 struct gl_renderbuffer
*newRb
;
709 GET_CURRENT_CONTEXT(ctx
);
711 ASSERT_OUTSIDE_BEGIN_END(ctx
);
713 if (target
!= GL_RENDERBUFFER_EXT
) {
714 _mesa_error(ctx
, GL_INVALID_ENUM
,
715 "glBindRenderbufferEXT(target)");
719 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
720 /* The above doesn't fully flush the drivers in the way that a
721 * glFlush does, but that is required here:
723 if (ctx
->Driver
.Flush
)
724 ctx
->Driver
.Flush(ctx
);
728 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
729 if (newRb
== &DummyRenderbuffer
) {
730 /* ID was reserved, but no real renderbuffer object made yet */
733 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
734 /* All RB IDs must be Gen'd */
735 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
740 /* create new renderbuffer object */
741 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
743 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
746 ASSERT(newRb
->AllocStorage
);
747 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
748 newRb
->RefCount
= 1; /* referenced by hash table */
755 ASSERT(newRb
!= &DummyRenderbuffer
);
757 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
762 * If the given renderbuffer is anywhere attached to the framebuffer, detach
764 * This is used when a renderbuffer object is deleted.
765 * The spec calls for unbinding.
768 detach_renderbuffer(GLcontext
*ctx
,
769 struct gl_framebuffer
*fb
,
770 struct gl_renderbuffer
*rb
)
773 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
774 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
775 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
778 invalidate_framebuffer(fb
);
783 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
786 GET_CURRENT_CONTEXT(ctx
);
788 ASSERT_OUTSIDE_BEGIN_END(ctx
);
789 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
791 for (i
= 0; i
< n
; i
++) {
792 if (renderbuffers
[i
] > 0) {
793 struct gl_renderbuffer
*rb
;
794 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
796 /* check if deleting currently bound renderbuffer object */
797 if (rb
== ctx
->CurrentRenderbuffer
) {
799 ASSERT(rb
->RefCount
>= 2);
800 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
803 if (ctx
->DrawBuffer
->Name
) {
804 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
806 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
807 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
810 /* Remove from hash table immediately, to free the ID.
811 * But the object will not be freed until it's no longer
812 * referenced anywhere else.
814 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
816 if (rb
!= &DummyRenderbuffer
) {
817 /* no longer referenced by hash table */
818 _mesa_reference_renderbuffer(&rb
, NULL
);
827 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
829 GET_CURRENT_CONTEXT(ctx
);
833 ASSERT_OUTSIDE_BEGIN_END(ctx
);
836 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
843 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
845 for (i
= 0; i
< n
; i
++) {
846 GLuint name
= first
+ i
;
847 renderbuffers
[i
] = name
;
848 /* insert dummy placeholder into hash table */
849 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
850 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
851 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
857 * Given an internal format token for a render buffer, return the
858 * corresponding base format.
859 * This is very similar to _mesa_base_tex_format() but the set of valid
860 * internal formats is somewhat different.
862 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
863 * GL_DEPTH_STENCIL_EXT or zero if error.
866 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
868 switch (internalFormat
) {
887 case GL_STENCIL_INDEX
:
888 case GL_STENCIL_INDEX1_EXT
:
889 case GL_STENCIL_INDEX4_EXT
:
890 case GL_STENCIL_INDEX8_EXT
:
891 case GL_STENCIL_INDEX16_EXT
:
892 return GL_STENCIL_INDEX
;
893 case GL_DEPTH_COMPONENT
:
894 case GL_DEPTH_COMPONENT16
:
895 case GL_DEPTH_COMPONENT24
:
896 case GL_DEPTH_COMPONENT32
:
897 return GL_DEPTH_COMPONENT
;
898 case GL_DEPTH_STENCIL_EXT
:
899 case GL_DEPTH24_STENCIL8_EXT
:
900 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
901 return GL_DEPTH_STENCIL_EXT
;
904 /* XXX add floating point formats eventually */
911 /** sentinal value, see below */
912 #define NO_SAMPLES 1000
916 * Helper function used by _mesa_RenderbufferStorageEXT() and
917 * _mesa_RenderbufferStorageMultisample().
918 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
921 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
922 GLsizei width
, GLsizei height
, GLsizei samples
)
924 const char *func
= samples
== NO_SAMPLES
?
925 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
926 struct gl_renderbuffer
*rb
;
928 GET_CURRENT_CONTEXT(ctx
);
930 ASSERT_OUTSIDE_BEGIN_END(ctx
);
932 if (target
!= GL_RENDERBUFFER_EXT
) {
933 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
937 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
938 if (baseFormat
== 0) {
939 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
943 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
944 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
948 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
949 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
953 if (samples
== NO_SAMPLES
) {
954 /* NumSamples == 0 indicates non-multisampling */
957 else if (samples
> ctx
->Const
.MaxSamples
) {
958 /* note: driver may choose to use more samples than what's requested */
959 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
963 rb
= ctx
->CurrentRenderbuffer
;
965 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
969 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
971 if (rb
->InternalFormat
== internalFormat
&&
972 rb
->Width
== (GLuint
) width
&&
973 rb
->Height
== (GLuint
) height
) {
974 /* no change in allocation needed */
978 /* These MUST get set by the AllocStorage func */
979 rb
->_ActualFormat
= 0;
987 rb
->NumSamples
= samples
;
989 /* Now allocate the storage */
990 ASSERT(rb
->AllocStorage
);
991 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
992 /* No error - check/set fields now */
993 assert(rb
->_ActualFormat
);
994 assert(rb
->Width
== (GLuint
) width
);
995 assert(rb
->Height
== (GLuint
) height
);
996 assert(rb
->RedBits
|| rb
->GreenBits
|| rb
->BlueBits
|| rb
->AlphaBits
||
997 rb
->DepthBits
|| rb
->StencilBits
|| rb
->IndexBits
);
998 rb
->InternalFormat
= internalFormat
;
999 rb
->_BaseFormat
= baseFormat
;
1002 /* Probably ran out of memory - clear the fields */
1005 rb
->InternalFormat
= GL_NONE
;
1006 rb
->_ActualFormat
= GL_NONE
;
1007 rb
->_BaseFormat
= GL_NONE
;
1019 test_framebuffer_completeness(ctx, fb);
1021 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1028 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1029 GLsizei width
, GLsizei height
)
1031 /* GL_ARB_fbo says calling this function is equivalent to calling
1032 * glRenderbufferStorageMultisample() with samples=0. We pass in
1033 * a token value here just for error reporting purposes.
1035 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1040 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1041 GLenum internalFormat
,
1042 GLsizei width
, GLsizei height
)
1044 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1050 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1052 struct gl_renderbuffer
*rb
;
1053 GET_CURRENT_CONTEXT(ctx
);
1055 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1057 if (target
!= GL_RENDERBUFFER_EXT
) {
1058 _mesa_error(ctx
, GL_INVALID_ENUM
,
1059 "glGetRenderbufferParameterivEXT(target)");
1063 rb
= ctx
->CurrentRenderbuffer
;
1065 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1066 "glGetRenderbufferParameterivEXT");
1070 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1073 case GL_RENDERBUFFER_WIDTH_EXT
:
1074 *params
= rb
->Width
;
1076 case GL_RENDERBUFFER_HEIGHT_EXT
:
1077 *params
= rb
->Height
;
1079 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1080 *params
= rb
->InternalFormat
;
1082 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1083 *params
= rb
->RedBits
;
1085 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1086 *params
= rb
->GreenBits
;
1088 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1089 *params
= rb
->BlueBits
;
1091 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1092 *params
= rb
->AlphaBits
;
1094 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1095 *params
= rb
->DepthBits
;
1097 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1098 *params
= rb
->StencilBits
;
1100 case GL_RENDERBUFFER_SAMPLES
:
1101 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1102 *params
= rb
->NumSamples
;
1107 _mesa_error(ctx
, GL_INVALID_ENUM
,
1108 "glGetRenderbufferParameterivEXT(target)");
1114 GLboolean GLAPIENTRY
1115 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1117 GET_CURRENT_CONTEXT(ctx
);
1118 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1120 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1121 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1129 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1132 ASSERT(ctx
->Driver
.RenderTexture
);
1133 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1134 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1135 struct gl_texture_object
*texObj
= att
->Texture
;
1137 && att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1138 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1145 * Examine all the framebuffer's attachments to see if any are textures.
1146 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1147 * notify the device driver that the texture image may have changed.
1150 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1152 if (ctx
->Driver
.FinishRenderTexture
) {
1154 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1155 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1156 if (att
->Texture
&& att
->Renderbuffer
) {
1157 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1165 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1167 struct gl_framebuffer
*newFb
, *newFbread
;
1168 GLboolean bindReadBuf
, bindDrawBuf
;
1169 GET_CURRENT_CONTEXT(ctx
);
1172 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1173 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1174 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1178 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1180 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1181 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1182 "glBindFramebufferEXT(unsupported)");
1187 #if FEATURE_EXT_framebuffer_blit
1188 case GL_DRAW_FRAMEBUFFER_EXT
:
1189 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1190 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1193 bindDrawBuf
= GL_TRUE
;
1194 bindReadBuf
= GL_FALSE
;
1196 case GL_READ_FRAMEBUFFER_EXT
:
1197 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1198 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1201 bindDrawBuf
= GL_FALSE
;
1202 bindReadBuf
= GL_TRUE
;
1205 case GL_FRAMEBUFFER_EXT
:
1206 bindDrawBuf
= GL_TRUE
;
1207 bindReadBuf
= GL_TRUE
;
1210 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1214 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1215 if (ctx
->Driver
.Flush
) {
1216 ctx
->Driver
.Flush(ctx
);
1220 /* Binding a user-created framebuffer object */
1221 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1222 if (newFb
== &DummyFramebuffer
) {
1223 /* ID was reserved, but no real framebuffer object made yet */
1226 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1227 /* All FBO IDs must be Gen'd */
1228 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1233 /* create new framebuffer object */
1234 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1236 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1239 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1244 /* Binding the window system framebuffer (which was originally set
1245 * with MakeCurrent).
1247 newFb
= ctx
->WinSysDrawBuffer
;
1248 newFbread
= ctx
->WinSysReadBuffer
;
1252 ASSERT(newFb
!= &DummyFramebuffer
);
1255 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1259 if (ctx
->ReadBuffer
== newFbread
)
1260 bindReadBuf
= GL_FALSE
; /* no change */
1262 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1266 /* check if old FB had any texture attachments */
1267 if (ctx
->DrawBuffer
->Name
!= 0) {
1268 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1271 if (ctx
->DrawBuffer
== newFb
)
1272 bindDrawBuf
= GL_FALSE
; /* no change */
1274 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1276 if (newFb
->Name
!= 0) {
1277 /* check if newly bound framebuffer has any texture attachments */
1278 check_begin_texture_render(ctx
, newFb
);
1282 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1283 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1289 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1292 GET_CURRENT_CONTEXT(ctx
);
1294 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1295 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1296 /* The above doesn't fully flush the drivers in the way that a
1297 * glFlush does, but that is required here:
1299 if (ctx
->Driver
.Flush
)
1300 ctx
->Driver
.Flush(ctx
);
1302 for (i
= 0; i
< n
; i
++) {
1303 if (framebuffers
[i
] > 0) {
1304 struct gl_framebuffer
*fb
;
1305 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1307 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1309 /* check if deleting currently bound framebuffer object */
1310 if (fb
== ctx
->DrawBuffer
) {
1312 ASSERT(fb
->RefCount
>= 2);
1313 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1315 if (fb
== ctx
->ReadBuffer
) {
1317 ASSERT(fb
->RefCount
>= 2);
1318 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1321 /* remove from hash table immediately, to free the ID */
1322 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1324 if (fb
!= &DummyFramebuffer
) {
1325 /* But the object will not be freed until it's no longer
1326 * bound in any context.
1328 _mesa_reference_framebuffer(&fb
, NULL
);
1337 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1339 GET_CURRENT_CONTEXT(ctx
);
1343 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1346 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1353 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1355 for (i
= 0; i
< n
; i
++) {
1356 GLuint name
= first
+ i
;
1357 framebuffers
[i
] = name
;
1358 /* insert dummy placeholder into hash table */
1359 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1360 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1361 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1368 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1370 struct gl_framebuffer
*buffer
;
1371 GET_CURRENT_CONTEXT(ctx
);
1373 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1376 #if FEATURE_EXT_framebuffer_blit
1377 case GL_DRAW_FRAMEBUFFER_EXT
:
1378 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1379 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1382 buffer
= ctx
->DrawBuffer
;
1384 case GL_READ_FRAMEBUFFER_EXT
:
1385 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1386 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1389 buffer
= ctx
->ReadBuffer
;
1392 case GL_FRAMEBUFFER_EXT
:
1393 buffer
= ctx
->DrawBuffer
;
1396 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1397 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1400 if (buffer
->Name
== 0) {
1401 /* The window system / default framebuffer is always complete */
1402 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1405 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1407 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1408 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1411 return buffer
->_Status
;
1417 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1420 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1421 GLenum attachment
, GLenum textarget
, GLuint texture
,
1422 GLint level
, GLint zoffset
)
1424 struct gl_renderbuffer_attachment
*att
;
1425 struct gl_texture_object
*texObj
= NULL
;
1426 struct gl_framebuffer
*fb
;
1427 GLboolean error
= GL_FALSE
;
1429 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1432 case GL_READ_FRAMEBUFFER_EXT
:
1433 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1434 fb
= ctx
->ReadBuffer
;
1436 case GL_DRAW_FRAMEBUFFER_EXT
:
1437 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1439 case GL_FRAMEBUFFER_EXT
:
1440 fb
= ctx
->DrawBuffer
;
1447 _mesa_error(ctx
, GL_INVALID_ENUM
,
1448 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1454 /* check framebuffer binding */
1455 if (fb
->Name
== 0) {
1456 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1457 "glFramebufferTexture%sEXT", caller
);
1462 /* The textarget, level, and zoffset parameters are only validated if
1463 * texture is non-zero.
1466 GLboolean err
= GL_TRUE
;
1468 texObj
= _mesa_lookup_texture(ctx
, texture
);
1469 if (texObj
!= NULL
) {
1470 if (textarget
== 0) {
1471 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1472 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1473 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1476 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1477 ? !IS_CUBE_FACE(textarget
)
1478 : (texObj
->Target
!= textarget
);
1483 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1484 "glFramebufferTexture%sEXT(texture target mismatch)",
1489 if (texObj
->Target
== GL_TEXTURE_3D
) {
1490 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1491 if (zoffset
< 0 || zoffset
>= maxSize
) {
1492 _mesa_error(ctx
, GL_INVALID_VALUE
,
1493 "glFramebufferTexture%sEXT(zoffset)", caller
);
1497 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1498 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1499 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1500 _mesa_error(ctx
, GL_INVALID_VALUE
,
1501 "glFramebufferTexture%sEXT(layer)", caller
);
1507 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1508 _mesa_error(ctx
, GL_INVALID_VALUE
,
1509 "glFramebufferTexture%sEXT(level)", caller
);
1514 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1516 _mesa_error(ctx
, GL_INVALID_ENUM
,
1517 "glFramebufferTexture%sEXT(attachment)", caller
);
1521 if (texObj
&& attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1522 /* the texture format must be depth+stencil */
1523 const struct gl_texture_image
*texImg
;
1524 texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
1525 if (!texImg
|| texImg
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1526 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1527 "glFramebufferTexture%sEXT(texture is not"
1528 " DEPTH_STENCIL format)", caller
);
1533 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1534 /* The above doesn't fully flush the drivers in the way that a
1535 * glFlush does, but that is required here:
1537 if (ctx
->Driver
.Flush
)
1538 ctx
->Driver
.Flush(ctx
);
1540 _glthread_LOCK_MUTEX(fb
->Mutex
);
1542 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1544 /* Set the render-to-texture flag. We'll check this flag in
1545 * glTexImage() and friends to determine if we need to revalidate
1546 * any FBOs that might be rendering into this texture.
1547 * This flag never gets cleared since it's non-trivial to determine
1548 * when all FBOs might be done rendering to this texture. That's OK
1549 * though since it's uncommon to render to a texture then repeatedly
1550 * call glTexImage() to change images in the texture.
1552 texObj
->_RenderToTexture
= GL_TRUE
;
1555 _mesa_remove_attachment(ctx
, att
);
1558 invalidate_framebuffer(fb
);
1560 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1566 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1567 GLenum textarget
, GLuint texture
, GLint level
)
1569 GET_CURRENT_CONTEXT(ctx
);
1571 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1572 _mesa_error(ctx
, GL_INVALID_ENUM
,
1573 "glFramebufferTexture1DEXT(textarget)");
1577 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1583 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1584 GLenum textarget
, GLuint texture
, GLint level
)
1586 GET_CURRENT_CONTEXT(ctx
);
1588 if ((texture
!= 0) &&
1589 (textarget
!= GL_TEXTURE_2D
) &&
1590 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1591 (!IS_CUBE_FACE(textarget
))) {
1592 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1593 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1597 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1603 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1604 GLenum textarget
, GLuint texture
,
1605 GLint level
, GLint zoffset
)
1607 GET_CURRENT_CONTEXT(ctx
);
1609 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1610 _mesa_error(ctx
, GL_INVALID_ENUM
,
1611 "glFramebufferTexture3DEXT(textarget)");
1615 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1621 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1622 GLuint texture
, GLint level
, GLint layer
)
1624 GET_CURRENT_CONTEXT(ctx
);
1626 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1632 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1633 GLenum renderbufferTarget
,
1634 GLuint renderbuffer
)
1636 struct gl_renderbuffer_attachment
*att
;
1637 struct gl_framebuffer
*fb
;
1638 struct gl_renderbuffer
*rb
;
1639 GET_CURRENT_CONTEXT(ctx
);
1641 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1644 #if FEATURE_EXT_framebuffer_blit
1645 case GL_DRAW_FRAMEBUFFER_EXT
:
1646 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1647 _mesa_error(ctx
, GL_INVALID_ENUM
,
1648 "glFramebufferRenderbufferEXT(target)");
1651 fb
= ctx
->DrawBuffer
;
1653 case GL_READ_FRAMEBUFFER_EXT
:
1654 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1655 _mesa_error(ctx
, GL_INVALID_ENUM
,
1656 "glFramebufferRenderbufferEXT(target)");
1659 fb
= ctx
->ReadBuffer
;
1662 case GL_FRAMEBUFFER_EXT
:
1663 fb
= ctx
->DrawBuffer
;
1666 _mesa_error(ctx
, GL_INVALID_ENUM
,
1667 "glFramebufferRenderbufferEXT(target)");
1671 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1672 _mesa_error(ctx
, GL_INVALID_ENUM
,
1673 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1677 if (fb
->Name
== 0) {
1678 /* Can't attach new renderbuffers to a window system framebuffer */
1679 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1683 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1685 _mesa_error(ctx
, GL_INVALID_ENUM
,
1686 "glFramebufferRenderbufferEXT(attachment)");
1691 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1693 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1694 "glFramebufferRenderbufferEXT(renderbuffer)");
1699 /* remove renderbuffer attachment */
1703 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1704 /* make sure the renderbuffer is a depth/stencil format */
1705 if (rb
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1706 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1707 "glFramebufferRenderbufferEXT(renderbuffer"
1708 " is not DEPTH_STENCIL format)");
1714 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1715 /* The above doesn't fully flush the drivers in the way that a
1716 * glFlush does, but that is required here:
1718 if (ctx
->Driver
.Flush
)
1719 ctx
->Driver
.Flush(ctx
);
1721 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1722 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1724 /* Some subsequent GL commands may depend on the framebuffer's visual
1725 * after the binding is updated. Update visual info now.
1727 _mesa_update_framebuffer_visual(fb
);
1732 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1733 GLenum pname
, GLint
*params
)
1735 const struct gl_renderbuffer_attachment
*att
;
1736 struct gl_framebuffer
*buffer
;
1737 GET_CURRENT_CONTEXT(ctx
);
1739 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1742 #if FEATURE_EXT_framebuffer_blit
1743 case GL_DRAW_FRAMEBUFFER_EXT
:
1744 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1745 _mesa_error(ctx
, GL_INVALID_ENUM
,
1746 "glGetFramebufferAttachmentParameterivEXT(target)");
1749 buffer
= ctx
->DrawBuffer
;
1751 case GL_READ_FRAMEBUFFER_EXT
:
1752 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1753 _mesa_error(ctx
, GL_INVALID_ENUM
,
1754 "glGetFramebufferAttachmentParameterivEXT(target)");
1757 buffer
= ctx
->ReadBuffer
;
1760 case GL_FRAMEBUFFER_EXT
:
1761 buffer
= ctx
->DrawBuffer
;
1764 _mesa_error(ctx
, GL_INVALID_ENUM
,
1765 "glGetFramebufferAttachmentParameterivEXT(target)");
1769 if (buffer
->Name
== 0) {
1770 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1771 "glGetFramebufferAttachmentParameterivEXT");
1775 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1777 _mesa_error(ctx
, GL_INVALID_ENUM
,
1778 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1782 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1783 /* the depth and stencil attachments must point to the same buffer */
1784 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1785 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1786 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1787 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1788 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1789 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1790 " attachments differ)");
1795 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1796 /* The above doesn't fully flush the drivers in the way that a
1797 * glFlush does, but that is required here:
1799 if (ctx
->Driver
.Flush
)
1800 ctx
->Driver
.Flush(ctx
);
1803 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1804 *params
= att
->Type
;
1806 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1807 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1808 *params
= att
->Renderbuffer
->Name
;
1810 else if (att
->Type
== GL_TEXTURE
) {
1811 *params
= att
->Texture
->Name
;
1814 _mesa_error(ctx
, GL_INVALID_ENUM
,
1815 "glGetFramebufferAttachmentParameterivEXT(pname)");
1818 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1819 if (att
->Type
== GL_TEXTURE
) {
1820 *params
= att
->TextureLevel
;
1823 _mesa_error(ctx
, GL_INVALID_ENUM
,
1824 "glGetFramebufferAttachmentParameterivEXT(pname)");
1827 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1828 if (att
->Type
== GL_TEXTURE
) {
1829 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1830 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1837 _mesa_error(ctx
, GL_INVALID_ENUM
,
1838 "glGetFramebufferAttachmentParameterivEXT(pname)");
1841 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1842 if (att
->Type
== GL_TEXTURE
) {
1843 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1844 *params
= att
->Zoffset
;
1851 _mesa_error(ctx
, GL_INVALID_ENUM
,
1852 "glGetFramebufferAttachmentParameterivEXT(pname)");
1855 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1856 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1857 _mesa_error(ctx
, GL_INVALID_ENUM
,
1858 "glGetFramebufferAttachmentParameterivEXT(pname)");
1861 *params
= att
->Renderbuffer
->ColorEncoding
;
1864 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1865 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1866 _mesa_error(ctx
, GL_INVALID_ENUM
,
1867 "glGetFramebufferAttachmentParameterivEXT(pname)");
1871 *params
= att
->Renderbuffer
->ComponentType
;
1874 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1875 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1876 _mesa_error(ctx
, GL_INVALID_ENUM
,
1877 "glGetFramebufferAttachmentParameterivEXT(pname)");
1880 *params
= att
->Renderbuffer
->RedBits
;
1883 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1884 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1885 _mesa_error(ctx
, GL_INVALID_ENUM
,
1886 "glGetFramebufferAttachmentParameterivEXT(pname)");
1889 *params
= att
->Renderbuffer
->GreenBits
;
1892 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1893 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1894 _mesa_error(ctx
, GL_INVALID_ENUM
,
1895 "glGetFramebufferAttachmentParameterivEXT(pname)");
1898 *params
= att
->Renderbuffer
->BlueBits
;
1901 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1902 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1903 _mesa_error(ctx
, GL_INVALID_ENUM
,
1904 "glGetFramebufferAttachmentParameterivEXT(pname)");
1907 *params
= att
->Renderbuffer
->AlphaBits
;
1910 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1911 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1912 _mesa_error(ctx
, GL_INVALID_ENUM
,
1913 "glGetFramebufferAttachmentParameterivEXT(pname)");
1916 *params
= att
->Renderbuffer
->DepthBits
;
1919 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1920 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1921 _mesa_error(ctx
, GL_INVALID_ENUM
,
1922 "glGetFramebufferAttachmentParameterivEXT(pname)");
1925 *params
= att
->Renderbuffer
->StencilBits
;
1929 _mesa_error(ctx
, GL_INVALID_ENUM
,
1930 "glGetFramebufferAttachmentParameterivEXT(pname)");
1937 _mesa_GenerateMipmapEXT(GLenum target
)
1939 struct gl_texture_unit
*texUnit
;
1940 struct gl_texture_object
*texObj
;
1941 GET_CURRENT_CONTEXT(ctx
);
1943 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1944 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1950 case GL_TEXTURE_CUBE_MAP
:
1951 /* OK, legal value */
1954 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1958 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1959 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1961 _mesa_lock_texture(ctx
, texObj
);
1962 if (target
== GL_TEXTURE_CUBE_MAP
) {
1964 for (face
= 0; face
< 6; face
++)
1965 ctx
->Driver
.GenerateMipmap(ctx
,
1966 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1970 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1972 _mesa_unlock_texture(ctx
, texObj
);
1976 #if FEATURE_EXT_framebuffer_blit
1978 * Blit rectangular region, optionally from one framebuffer to another.
1980 * Note, if the src buffer is multisampled and the dest is not, this is
1981 * when the samples must be resolved to a single color.
1984 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1985 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1986 GLbitfield mask
, GLenum filter
)
1988 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1989 GL_DEPTH_BUFFER_BIT
|
1990 GL_STENCIL_BUFFER_BIT
);
1991 const struct gl_framebuffer
*readFb
, *drawFb
;
1992 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1993 GET_CURRENT_CONTEXT(ctx
);
1995 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1996 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1998 if (ctx
->NewState
) {
1999 _mesa_update_state(ctx
);
2002 readFb
= ctx
->ReadBuffer
;
2003 drawFb
= ctx
->DrawBuffer
;
2005 if (!readFb
|| !drawFb
) {
2006 /* This will normally never happen but someday we may want to
2007 * support MakeCurrent() with no drawables.
2012 /* check for complete framebuffers */
2013 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2014 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2015 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2016 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2020 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2021 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2025 if (mask
& ~legalMaskBits
) {
2026 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2030 /* depth/stencil must be blitted with nearest filtering */
2031 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2032 && filter
!= GL_NEAREST
) {
2033 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2034 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2038 /* get color read/draw renderbuffers */
2039 if (mask
& GL_COLOR_BUFFER_BIT
) {
2040 colorReadRb
= readFb
->_ColorReadBuffer
;
2041 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2044 colorReadRb
= colorDrawRb
= NULL
;
2047 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2048 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2049 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2052 readRb
->StencilBits
!= drawRb
->StencilBits
) {
2053 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2054 "glBlitFramebufferEXT(stencil buffer size mismatch");
2059 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2060 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2061 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2064 readRb
->DepthBits
!= drawRb
->DepthBits
) {
2065 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2066 "glBlitFramebufferEXT(depth buffer size mismatch");
2071 if (readFb
->Visual
.samples
> 0 &&
2072 drawFb
->Visual
.samples
> 0 &&
2073 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2074 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2075 "glBlitFramebufferEXT(mismatched samples");
2079 /* extra checks for multisample copies... */
2080 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2081 /* src and dest region sizes must be the same */
2082 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2083 srcY1
- srcY0
!= dstY1
- dstY0
) {
2084 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2085 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2089 /* color formats must match */
2092 colorReadRb
->_ActualFormat
!= colorDrawRb
->_ActualFormat
) {
2093 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2094 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2099 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2100 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2104 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2105 ctx
->Driver
.BlitFramebuffer(ctx
,
2106 srcX0
, srcY0
, srcX1
, srcY1
,
2107 dstX0
, dstY0
, dstX1
, dstY1
,
2110 #endif /* FEATURE_EXT_framebuffer_blit */