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
);
721 /* The above doesn't fully flush the drivers in the way that a
722 * glFlush does, but that is required here:
724 if (ctx
->Driver
.Flush
)
725 ctx
->Driver
.Flush(ctx
);
729 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
730 if (newRb
== &DummyRenderbuffer
) {
731 /* ID was reserved, but no real renderbuffer object made yet */
734 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
735 /* All RB IDs must be Gen'd */
736 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
741 /* create new renderbuffer object */
742 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
744 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
747 ASSERT(newRb
->AllocStorage
);
748 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
749 newRb
->RefCount
= 1; /* referenced by hash table */
756 ASSERT(newRb
!= &DummyRenderbuffer
);
758 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
763 * If the given renderbuffer is anywhere attached to the framebuffer, detach
765 * This is used when a renderbuffer object is deleted.
766 * The spec calls for unbinding.
769 detach_renderbuffer(GLcontext
*ctx
,
770 struct gl_framebuffer
*fb
,
771 struct gl_renderbuffer
*rb
)
774 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
775 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
776 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
779 invalidate_framebuffer(fb
);
784 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
787 GET_CURRENT_CONTEXT(ctx
);
789 ASSERT_OUTSIDE_BEGIN_END(ctx
);
790 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
792 for (i
= 0; i
< n
; i
++) {
793 if (renderbuffers
[i
] > 0) {
794 struct gl_renderbuffer
*rb
;
795 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
797 /* check if deleting currently bound renderbuffer object */
798 if (rb
== ctx
->CurrentRenderbuffer
) {
800 ASSERT(rb
->RefCount
>= 2);
801 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
804 if (ctx
->DrawBuffer
->Name
) {
805 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
807 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
808 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
811 /* Remove from hash table immediately, to free the ID.
812 * But the object will not be freed until it's no longer
813 * referenced anywhere else.
815 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
817 if (rb
!= &DummyRenderbuffer
) {
818 /* no longer referenced by hash table */
819 _mesa_reference_renderbuffer(&rb
, NULL
);
828 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
830 GET_CURRENT_CONTEXT(ctx
);
834 ASSERT_OUTSIDE_BEGIN_END(ctx
);
837 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
844 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
846 for (i
= 0; i
< n
; i
++) {
847 GLuint name
= first
+ i
;
848 renderbuffers
[i
] = name
;
849 /* insert dummy placeholder into hash table */
850 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
851 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
852 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
858 * Given an internal format token for a render buffer, return the
859 * corresponding base format.
860 * This is very similar to _mesa_base_tex_format() but the set of valid
861 * internal formats is somewhat different.
863 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
864 * GL_DEPTH_STENCIL_EXT or zero if error.
867 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
869 switch (internalFormat
) {
888 case GL_STENCIL_INDEX
:
889 case GL_STENCIL_INDEX1_EXT
:
890 case GL_STENCIL_INDEX4_EXT
:
891 case GL_STENCIL_INDEX8_EXT
:
892 case GL_STENCIL_INDEX16_EXT
:
893 return GL_STENCIL_INDEX
;
894 case GL_DEPTH_COMPONENT
:
895 case GL_DEPTH_COMPONENT16
:
896 case GL_DEPTH_COMPONENT24
:
897 case GL_DEPTH_COMPONENT32
:
898 return GL_DEPTH_COMPONENT
;
899 case GL_DEPTH_STENCIL_EXT
:
900 case GL_DEPTH24_STENCIL8_EXT
:
901 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
902 return GL_DEPTH_STENCIL_EXT
;
905 /* XXX add floating point formats eventually */
912 /** sentinal value, see below */
913 #define NO_SAMPLES 1000
917 * Helper function used by _mesa_RenderbufferStorageEXT() and
918 * _mesa_RenderbufferStorageMultisample().
919 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
922 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
923 GLsizei width
, GLsizei height
, GLsizei samples
)
925 const char *func
= samples
== NO_SAMPLES
?
926 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
927 struct gl_renderbuffer
*rb
;
929 GET_CURRENT_CONTEXT(ctx
);
931 ASSERT_OUTSIDE_BEGIN_END(ctx
);
933 if (target
!= GL_RENDERBUFFER_EXT
) {
934 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
938 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
939 if (baseFormat
== 0) {
940 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
944 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
945 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
949 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
950 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
954 if (samples
== NO_SAMPLES
) {
955 /* NumSamples == 0 indicates non-multisampling */
958 else if (samples
> ctx
->Const
.MaxSamples
) {
959 /* note: driver may choose to use more samples than what's requested */
960 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
964 rb
= ctx
->CurrentRenderbuffer
;
966 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
970 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
972 if (rb
->InternalFormat
== internalFormat
&&
973 rb
->Width
== (GLuint
) width
&&
974 rb
->Height
== (GLuint
) height
) {
975 /* no change in allocation needed */
979 /* These MUST get set by the AllocStorage func */
980 rb
->_ActualFormat
= 0;
988 rb
->NumSamples
= samples
;
990 /* Now allocate the storage */
991 ASSERT(rb
->AllocStorage
);
992 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
993 /* No error - check/set fields now */
994 assert(rb
->_ActualFormat
);
995 assert(rb
->Width
== (GLuint
) width
);
996 assert(rb
->Height
== (GLuint
) height
);
997 assert(rb
->RedBits
|| rb
->GreenBits
|| rb
->BlueBits
|| rb
->AlphaBits
||
998 rb
->DepthBits
|| rb
->StencilBits
|| rb
->IndexBits
);
999 rb
->InternalFormat
= internalFormat
;
1000 rb
->_BaseFormat
= baseFormat
;
1003 /* Probably ran out of memory - clear the fields */
1006 rb
->InternalFormat
= GL_NONE
;
1007 rb
->_ActualFormat
= GL_NONE
;
1008 rb
->_BaseFormat
= GL_NONE
;
1020 test_framebuffer_completeness(ctx, fb);
1022 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1029 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1030 GLsizei width
, GLsizei height
)
1032 /* GL_ARB_fbo says calling this function is equivalent to calling
1033 * glRenderbufferStorageMultisample() with samples=0. We pass in
1034 * a token value here just for error reporting purposes.
1036 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1041 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1042 GLenum internalFormat
,
1043 GLsizei width
, GLsizei height
)
1045 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1051 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1053 struct gl_renderbuffer
*rb
;
1054 GET_CURRENT_CONTEXT(ctx
);
1056 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1058 if (target
!= GL_RENDERBUFFER_EXT
) {
1059 _mesa_error(ctx
, GL_INVALID_ENUM
,
1060 "glGetRenderbufferParameterivEXT(target)");
1064 rb
= ctx
->CurrentRenderbuffer
;
1066 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1067 "glGetRenderbufferParameterivEXT");
1071 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1074 case GL_RENDERBUFFER_WIDTH_EXT
:
1075 *params
= rb
->Width
;
1077 case GL_RENDERBUFFER_HEIGHT_EXT
:
1078 *params
= rb
->Height
;
1080 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1081 *params
= rb
->InternalFormat
;
1083 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1084 *params
= rb
->RedBits
;
1086 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1087 *params
= rb
->GreenBits
;
1089 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1090 *params
= rb
->BlueBits
;
1092 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1093 *params
= rb
->AlphaBits
;
1095 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1096 *params
= rb
->DepthBits
;
1098 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1099 *params
= rb
->StencilBits
;
1101 case GL_RENDERBUFFER_SAMPLES
:
1102 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1103 *params
= rb
->NumSamples
;
1108 _mesa_error(ctx
, GL_INVALID_ENUM
,
1109 "glGetRenderbufferParameterivEXT(target)");
1115 GLboolean GLAPIENTRY
1116 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1118 GET_CURRENT_CONTEXT(ctx
);
1119 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1121 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1122 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1130 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1133 ASSERT(ctx
->Driver
.RenderTexture
);
1134 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1135 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1136 struct gl_texture_object
*texObj
= att
->Texture
;
1138 && att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1139 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1146 * Examine all the framebuffer's attachments to see if any are textures.
1147 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1148 * notify the device driver that the texture image may have changed.
1151 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1153 if (ctx
->Driver
.FinishRenderTexture
) {
1155 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1156 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1157 if (att
->Texture
&& att
->Renderbuffer
) {
1158 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1166 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1168 struct gl_framebuffer
*newFb
, *newFbread
;
1169 GLboolean bindReadBuf
, bindDrawBuf
;
1170 GET_CURRENT_CONTEXT(ctx
);
1173 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1174 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1175 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1179 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1181 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1182 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1183 "glBindFramebufferEXT(unsupported)");
1188 #if FEATURE_EXT_framebuffer_blit
1189 case GL_DRAW_FRAMEBUFFER_EXT
:
1190 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1191 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1194 bindDrawBuf
= GL_TRUE
;
1195 bindReadBuf
= GL_FALSE
;
1197 case GL_READ_FRAMEBUFFER_EXT
:
1198 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1199 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1202 bindDrawBuf
= GL_FALSE
;
1203 bindReadBuf
= GL_TRUE
;
1206 case GL_FRAMEBUFFER_EXT
:
1207 bindDrawBuf
= GL_TRUE
;
1208 bindReadBuf
= GL_TRUE
;
1211 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1215 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1216 if (ctx
->Driver
.Flush
) {
1217 ctx
->Driver
.Flush(ctx
);
1221 /* Binding a user-created framebuffer object */
1222 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1223 if (newFb
== &DummyFramebuffer
) {
1224 /* ID was reserved, but no real framebuffer object made yet */
1227 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1228 /* All FBO IDs must be Gen'd */
1229 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1234 /* create new framebuffer object */
1235 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1237 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1240 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1245 /* Binding the window system framebuffer (which was originally set
1246 * with MakeCurrent).
1248 newFb
= ctx
->WinSysDrawBuffer
;
1249 newFbread
= ctx
->WinSysReadBuffer
;
1253 ASSERT(newFb
!= &DummyFramebuffer
);
1256 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1260 if (ctx
->ReadBuffer
== newFbread
)
1261 bindReadBuf
= GL_FALSE
; /* no change */
1263 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1267 /* check if old FB had any texture attachments */
1268 if (ctx
->DrawBuffer
->Name
!= 0) {
1269 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1272 if (ctx
->DrawBuffer
== newFb
)
1273 bindDrawBuf
= GL_FALSE
; /* no change */
1275 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1277 if (newFb
->Name
!= 0) {
1278 /* check if newly bound framebuffer has any texture attachments */
1279 check_begin_texture_render(ctx
, newFb
);
1283 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1284 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1290 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1293 GET_CURRENT_CONTEXT(ctx
);
1295 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1296 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1297 /* The above doesn't fully flush the drivers in the way that a
1298 * glFlush does, but that is required here:
1300 if (ctx
->Driver
.Flush
)
1301 ctx
->Driver
.Flush(ctx
);
1303 for (i
= 0; i
< n
; i
++) {
1304 if (framebuffers
[i
] > 0) {
1305 struct gl_framebuffer
*fb
;
1306 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1308 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1310 /* check if deleting currently bound framebuffer object */
1311 if (fb
== ctx
->DrawBuffer
) {
1313 ASSERT(fb
->RefCount
>= 2);
1314 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1316 if (fb
== ctx
->ReadBuffer
) {
1318 ASSERT(fb
->RefCount
>= 2);
1319 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1322 /* remove from hash table immediately, to free the ID */
1323 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1325 if (fb
!= &DummyFramebuffer
) {
1326 /* But the object will not be freed until it's no longer
1327 * bound in any context.
1329 _mesa_reference_framebuffer(&fb
, NULL
);
1338 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1340 GET_CURRENT_CONTEXT(ctx
);
1344 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1347 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1354 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1356 for (i
= 0; i
< n
; i
++) {
1357 GLuint name
= first
+ i
;
1358 framebuffers
[i
] = name
;
1359 /* insert dummy placeholder into hash table */
1360 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1361 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1362 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1369 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1371 struct gl_framebuffer
*buffer
;
1372 GET_CURRENT_CONTEXT(ctx
);
1374 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1377 #if FEATURE_EXT_framebuffer_blit
1378 case GL_DRAW_FRAMEBUFFER_EXT
:
1379 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1380 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1383 buffer
= ctx
->DrawBuffer
;
1385 case GL_READ_FRAMEBUFFER_EXT
:
1386 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1387 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1390 buffer
= ctx
->ReadBuffer
;
1393 case GL_FRAMEBUFFER_EXT
:
1394 buffer
= ctx
->DrawBuffer
;
1397 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1398 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1401 if (buffer
->Name
== 0) {
1402 /* The window system / default framebuffer is always complete */
1403 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1406 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1408 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1409 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1412 return buffer
->_Status
;
1418 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1421 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1422 GLenum attachment
, GLenum textarget
, GLuint texture
,
1423 GLint level
, GLint zoffset
)
1425 struct gl_renderbuffer_attachment
*att
;
1426 struct gl_texture_object
*texObj
= NULL
;
1427 struct gl_framebuffer
*fb
;
1428 GLboolean error
= GL_FALSE
;
1430 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1433 case GL_READ_FRAMEBUFFER_EXT
:
1434 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1435 fb
= ctx
->ReadBuffer
;
1437 case GL_DRAW_FRAMEBUFFER_EXT
:
1438 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1440 case GL_FRAMEBUFFER_EXT
:
1441 fb
= ctx
->DrawBuffer
;
1448 _mesa_error(ctx
, GL_INVALID_ENUM
,
1449 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1455 /* check framebuffer binding */
1456 if (fb
->Name
== 0) {
1457 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1458 "glFramebufferTexture%sEXT", caller
);
1463 /* The textarget, level, and zoffset parameters are only validated if
1464 * texture is non-zero.
1467 GLboolean err
= GL_TRUE
;
1469 texObj
= _mesa_lookup_texture(ctx
, texture
);
1470 if (texObj
!= NULL
) {
1471 if (textarget
== 0) {
1472 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1473 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1474 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1477 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1478 ? !IS_CUBE_FACE(textarget
)
1479 : (texObj
->Target
!= textarget
);
1484 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1485 "glFramebufferTexture%sEXT(texture target mismatch)",
1490 if (texObj
->Target
== GL_TEXTURE_3D
) {
1491 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1492 if (zoffset
< 0 || zoffset
>= maxSize
) {
1493 _mesa_error(ctx
, GL_INVALID_VALUE
,
1494 "glFramebufferTexture%sEXT(zoffset)", caller
);
1498 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1499 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1500 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1501 _mesa_error(ctx
, GL_INVALID_VALUE
,
1502 "glFramebufferTexture%sEXT(layer)", caller
);
1508 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1509 _mesa_error(ctx
, GL_INVALID_VALUE
,
1510 "glFramebufferTexture%sEXT(level)", caller
);
1515 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1517 _mesa_error(ctx
, GL_INVALID_ENUM
,
1518 "glFramebufferTexture%sEXT(attachment)", caller
);
1522 if (texObj
&& attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1523 /* the texture format must be depth+stencil */
1524 const struct gl_texture_image
*texImg
;
1525 texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
1526 if (!texImg
|| texImg
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1527 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1528 "glFramebufferTexture%sEXT(texture is not"
1529 " DEPTH_STENCIL format)", caller
);
1534 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1535 /* The above doesn't fully flush the drivers in the way that a
1536 * glFlush does, but that is required here:
1538 if (ctx
->Driver
.Flush
)
1539 ctx
->Driver
.Flush(ctx
);
1541 _glthread_LOCK_MUTEX(fb
->Mutex
);
1543 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1545 /* Set the render-to-texture flag. We'll check this flag in
1546 * glTexImage() and friends to determine if we need to revalidate
1547 * any FBOs that might be rendering into this texture.
1548 * This flag never gets cleared since it's non-trivial to determine
1549 * when all FBOs might be done rendering to this texture. That's OK
1550 * though since it's uncommon to render to a texture then repeatedly
1551 * call glTexImage() to change images in the texture.
1553 texObj
->_RenderToTexture
= GL_TRUE
;
1556 _mesa_remove_attachment(ctx
, att
);
1559 invalidate_framebuffer(fb
);
1561 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1567 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1568 GLenum textarget
, GLuint texture
, GLint level
)
1570 GET_CURRENT_CONTEXT(ctx
);
1572 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1573 _mesa_error(ctx
, GL_INVALID_ENUM
,
1574 "glFramebufferTexture1DEXT(textarget)");
1578 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1584 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1585 GLenum textarget
, GLuint texture
, GLint level
)
1587 GET_CURRENT_CONTEXT(ctx
);
1589 if ((texture
!= 0) &&
1590 (textarget
!= GL_TEXTURE_2D
) &&
1591 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1592 (!IS_CUBE_FACE(textarget
))) {
1593 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1594 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1598 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1604 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1605 GLenum textarget
, GLuint texture
,
1606 GLint level
, GLint zoffset
)
1608 GET_CURRENT_CONTEXT(ctx
);
1610 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1611 _mesa_error(ctx
, GL_INVALID_ENUM
,
1612 "glFramebufferTexture3DEXT(textarget)");
1616 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1622 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1623 GLuint texture
, GLint level
, GLint layer
)
1625 GET_CURRENT_CONTEXT(ctx
);
1627 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1633 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1634 GLenum renderbufferTarget
,
1635 GLuint renderbuffer
)
1637 struct gl_renderbuffer_attachment
*att
;
1638 struct gl_framebuffer
*fb
;
1639 struct gl_renderbuffer
*rb
;
1640 GET_CURRENT_CONTEXT(ctx
);
1642 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1645 #if FEATURE_EXT_framebuffer_blit
1646 case GL_DRAW_FRAMEBUFFER_EXT
:
1647 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1648 _mesa_error(ctx
, GL_INVALID_ENUM
,
1649 "glFramebufferRenderbufferEXT(target)");
1652 fb
= ctx
->DrawBuffer
;
1654 case GL_READ_FRAMEBUFFER_EXT
:
1655 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1656 _mesa_error(ctx
, GL_INVALID_ENUM
,
1657 "glFramebufferRenderbufferEXT(target)");
1660 fb
= ctx
->ReadBuffer
;
1663 case GL_FRAMEBUFFER_EXT
:
1664 fb
= ctx
->DrawBuffer
;
1667 _mesa_error(ctx
, GL_INVALID_ENUM
,
1668 "glFramebufferRenderbufferEXT(target)");
1672 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1673 _mesa_error(ctx
, GL_INVALID_ENUM
,
1674 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1678 if (fb
->Name
== 0) {
1679 /* Can't attach new renderbuffers to a window system framebuffer */
1680 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1684 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1686 _mesa_error(ctx
, GL_INVALID_ENUM
,
1687 "glFramebufferRenderbufferEXT(attachment)");
1692 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1694 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1695 "glFramebufferRenderbufferEXT(renderbuffer)");
1700 /* remove renderbuffer attachment */
1704 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1705 /* make sure the renderbuffer is a depth/stencil format */
1706 if (rb
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1707 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1708 "glFramebufferRenderbufferEXT(renderbuffer"
1709 " is not DEPTH_STENCIL format)");
1715 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1716 /* The above doesn't fully flush the drivers in the way that a
1717 * glFlush does, but that is required here:
1719 if (ctx
->Driver
.Flush
)
1720 ctx
->Driver
.Flush(ctx
);
1722 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1723 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1725 /* Some subsequent GL commands may depend on the framebuffer's visual
1726 * after the binding is updated. Update visual info now.
1728 _mesa_update_framebuffer_visual(fb
);
1733 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1734 GLenum pname
, GLint
*params
)
1736 const struct gl_renderbuffer_attachment
*att
;
1737 struct gl_framebuffer
*buffer
;
1738 GET_CURRENT_CONTEXT(ctx
);
1740 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1743 #if FEATURE_EXT_framebuffer_blit
1744 case GL_DRAW_FRAMEBUFFER_EXT
:
1745 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1746 _mesa_error(ctx
, GL_INVALID_ENUM
,
1747 "glGetFramebufferAttachmentParameterivEXT(target)");
1750 buffer
= ctx
->DrawBuffer
;
1752 case GL_READ_FRAMEBUFFER_EXT
:
1753 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1754 _mesa_error(ctx
, GL_INVALID_ENUM
,
1755 "glGetFramebufferAttachmentParameterivEXT(target)");
1758 buffer
= ctx
->ReadBuffer
;
1761 case GL_FRAMEBUFFER_EXT
:
1762 buffer
= ctx
->DrawBuffer
;
1765 _mesa_error(ctx
, GL_INVALID_ENUM
,
1766 "glGetFramebufferAttachmentParameterivEXT(target)");
1770 if (buffer
->Name
== 0) {
1771 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1772 "glGetFramebufferAttachmentParameterivEXT");
1776 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1778 _mesa_error(ctx
, GL_INVALID_ENUM
,
1779 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1783 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1784 /* the depth and stencil attachments must point to the same buffer */
1785 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1786 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1787 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1788 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1789 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1790 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1791 " attachments differ)");
1796 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1797 /* The above doesn't fully flush the drivers in the way that a
1798 * glFlush does, but that is required here:
1800 if (ctx
->Driver
.Flush
)
1801 ctx
->Driver
.Flush(ctx
);
1804 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1805 *params
= att
->Type
;
1807 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1808 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1809 *params
= att
->Renderbuffer
->Name
;
1811 else if (att
->Type
== GL_TEXTURE
) {
1812 *params
= att
->Texture
->Name
;
1815 _mesa_error(ctx
, GL_INVALID_ENUM
,
1816 "glGetFramebufferAttachmentParameterivEXT(pname)");
1819 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1820 if (att
->Type
== GL_TEXTURE
) {
1821 *params
= att
->TextureLevel
;
1824 _mesa_error(ctx
, GL_INVALID_ENUM
,
1825 "glGetFramebufferAttachmentParameterivEXT(pname)");
1828 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1829 if (att
->Type
== GL_TEXTURE
) {
1830 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1831 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1838 _mesa_error(ctx
, GL_INVALID_ENUM
,
1839 "glGetFramebufferAttachmentParameterivEXT(pname)");
1842 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1843 if (att
->Type
== GL_TEXTURE
) {
1844 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1845 *params
= att
->Zoffset
;
1852 _mesa_error(ctx
, GL_INVALID_ENUM
,
1853 "glGetFramebufferAttachmentParameterivEXT(pname)");
1856 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1857 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1858 _mesa_error(ctx
, GL_INVALID_ENUM
,
1859 "glGetFramebufferAttachmentParameterivEXT(pname)");
1862 *params
= att
->Renderbuffer
->ColorEncoding
;
1865 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1866 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1867 _mesa_error(ctx
, GL_INVALID_ENUM
,
1868 "glGetFramebufferAttachmentParameterivEXT(pname)");
1872 *params
= att
->Renderbuffer
->ComponentType
;
1875 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1876 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1877 _mesa_error(ctx
, GL_INVALID_ENUM
,
1878 "glGetFramebufferAttachmentParameterivEXT(pname)");
1881 *params
= att
->Renderbuffer
->RedBits
;
1884 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1885 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1886 _mesa_error(ctx
, GL_INVALID_ENUM
,
1887 "glGetFramebufferAttachmentParameterivEXT(pname)");
1890 *params
= att
->Renderbuffer
->GreenBits
;
1893 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1894 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1895 _mesa_error(ctx
, GL_INVALID_ENUM
,
1896 "glGetFramebufferAttachmentParameterivEXT(pname)");
1899 *params
= att
->Renderbuffer
->BlueBits
;
1902 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1903 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1904 _mesa_error(ctx
, GL_INVALID_ENUM
,
1905 "glGetFramebufferAttachmentParameterivEXT(pname)");
1908 *params
= att
->Renderbuffer
->AlphaBits
;
1911 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1912 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1913 _mesa_error(ctx
, GL_INVALID_ENUM
,
1914 "glGetFramebufferAttachmentParameterivEXT(pname)");
1917 *params
= att
->Renderbuffer
->DepthBits
;
1920 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1921 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1922 _mesa_error(ctx
, GL_INVALID_ENUM
,
1923 "glGetFramebufferAttachmentParameterivEXT(pname)");
1926 *params
= att
->Renderbuffer
->StencilBits
;
1930 _mesa_error(ctx
, GL_INVALID_ENUM
,
1931 "glGetFramebufferAttachmentParameterivEXT(pname)");
1938 _mesa_GenerateMipmapEXT(GLenum target
)
1940 struct gl_texture_unit
*texUnit
;
1941 struct gl_texture_object
*texObj
;
1942 GET_CURRENT_CONTEXT(ctx
);
1944 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1945 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1951 case GL_TEXTURE_CUBE_MAP
:
1952 /* OK, legal value */
1955 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1959 texUnit
= _mesa_get_current_tex_unit(ctx
);
1960 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1962 _mesa_lock_texture(ctx
, texObj
);
1963 if (target
== GL_TEXTURE_CUBE_MAP
) {
1965 for (face
= 0; face
< 6; face
++)
1966 ctx
->Driver
.GenerateMipmap(ctx
,
1967 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1971 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1973 _mesa_unlock_texture(ctx
, texObj
);
1977 #if FEATURE_EXT_framebuffer_blit
1979 * Blit rectangular region, optionally from one framebuffer to another.
1981 * Note, if the src buffer is multisampled and the dest is not, this is
1982 * when the samples must be resolved to a single color.
1985 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1986 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1987 GLbitfield mask
, GLenum filter
)
1989 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1990 GL_DEPTH_BUFFER_BIT
|
1991 GL_STENCIL_BUFFER_BIT
);
1992 const struct gl_framebuffer
*readFb
, *drawFb
;
1993 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1994 GET_CURRENT_CONTEXT(ctx
);
1996 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1997 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1999 if (ctx
->NewState
) {
2000 _mesa_update_state(ctx
);
2003 readFb
= ctx
->ReadBuffer
;
2004 drawFb
= ctx
->DrawBuffer
;
2006 if (!readFb
|| !drawFb
) {
2007 /* This will normally never happen but someday we may want to
2008 * support MakeCurrent() with no drawables.
2013 /* check for complete framebuffers */
2014 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2015 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2016 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2017 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2021 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2022 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2026 if (mask
& ~legalMaskBits
) {
2027 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2031 /* depth/stencil must be blitted with nearest filtering */
2032 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2033 && filter
!= GL_NEAREST
) {
2034 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2035 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2039 /* get color read/draw renderbuffers */
2040 if (mask
& GL_COLOR_BUFFER_BIT
) {
2041 colorReadRb
= readFb
->_ColorReadBuffer
;
2042 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2045 colorReadRb
= colorDrawRb
= NULL
;
2048 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2049 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2050 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2053 readRb
->StencilBits
!= drawRb
->StencilBits
) {
2054 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2055 "glBlitFramebufferEXT(stencil buffer size mismatch");
2060 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2061 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2062 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2065 readRb
->DepthBits
!= drawRb
->DepthBits
) {
2066 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2067 "glBlitFramebufferEXT(depth buffer size mismatch");
2072 if (readFb
->Visual
.samples
> 0 &&
2073 drawFb
->Visual
.samples
> 0 &&
2074 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2075 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2076 "glBlitFramebufferEXT(mismatched samples");
2080 /* extra checks for multisample copies... */
2081 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2082 /* src and dest region sizes must be the same */
2083 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2084 srcY1
- srcY0
!= dstY1
- dstY0
) {
2085 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2086 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2090 /* color formats must match */
2093 colorReadRb
->_ActualFormat
!= colorDrawRb
->_ActualFormat
) {
2094 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2095 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2100 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2101 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2105 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2106 ctx
->Driver
.BlitFramebuffer(ctx
,
2107 srcX0
, srcY0
, srcX1
, srcY1
,
2108 dstX0
, dstY0
, dstX1
, dstY1
,
2111 #endif /* FEATURE_EXT_framebuffer_blit */