2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * GL_EXT/ARB_framebuffer_object extensions
39 #include "framebuffer.h"
43 #include "renderbuffer.h"
51 /** Set this to 1 to help debug FBO incompleteness problems */
58 * None of the GL_EXT_framebuffer_object functions are compiled into
65 * When glGenRender/FramebuffersEXT() is called we insert pointers to
66 * these placeholder objects into the hash table.
67 * Later, when the object ID is first bound, we replace the placeholder
68 * with the real frame/renderbuffer.
70 static struct gl_framebuffer DummyFramebuffer
;
71 static struct gl_renderbuffer DummyRenderbuffer
;
74 #define IS_CUBE_FACE(TARGET) \
75 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
76 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
80 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
86 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
93 _mesa_init_fbobjects(GLcontext
*ctx
)
95 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
96 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
101 * Helper routine for getting a gl_renderbuffer.
103 struct gl_renderbuffer
*
104 _mesa_lookup_renderbuffer(GLcontext
*ctx
, GLuint id
)
106 struct gl_renderbuffer
*rb
;
111 rb
= (struct gl_renderbuffer
*)
112 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
118 * Helper routine for getting a gl_framebuffer.
120 struct gl_framebuffer
*
121 _mesa_lookup_framebuffer(GLcontext
*ctx
, GLuint id
)
123 struct gl_framebuffer
*fb
;
128 fb
= (struct gl_framebuffer
*)
129 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
135 * Mark the given framebuffer as invalid. This will force the
136 * test for framebuffer completeness to be done before the framebuffer
140 invalidate_framebuffer(struct gl_framebuffer
*fb
)
142 fb
->_Status
= 0; /* "indeterminate" */
147 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
148 * gl_renderbuffer_attachment object.
149 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
150 * the depth buffer attachment point.
152 struct gl_renderbuffer_attachment
*
153 _mesa_get_attachment(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
158 switch (attachment
) {
159 case GL_COLOR_ATTACHMENT0_EXT
:
160 case GL_COLOR_ATTACHMENT1_EXT
:
161 case GL_COLOR_ATTACHMENT2_EXT
:
162 case GL_COLOR_ATTACHMENT3_EXT
:
163 case GL_COLOR_ATTACHMENT4_EXT
:
164 case GL_COLOR_ATTACHMENT5_EXT
:
165 case GL_COLOR_ATTACHMENT6_EXT
:
166 case GL_COLOR_ATTACHMENT7_EXT
:
167 case GL_COLOR_ATTACHMENT8_EXT
:
168 case GL_COLOR_ATTACHMENT9_EXT
:
169 case GL_COLOR_ATTACHMENT10_EXT
:
170 case GL_COLOR_ATTACHMENT11_EXT
:
171 case GL_COLOR_ATTACHMENT12_EXT
:
172 case GL_COLOR_ATTACHMENT13_EXT
:
173 case GL_COLOR_ATTACHMENT14_EXT
:
174 case GL_COLOR_ATTACHMENT15_EXT
:
175 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
176 if (i
>= ctx
->Const
.MaxColorAttachments
) {
179 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
180 case GL_DEPTH_STENCIL_ATTACHMENT
:
182 case GL_DEPTH_ATTACHMENT_EXT
:
183 return &fb
->Attachment
[BUFFER_DEPTH
];
184 case GL_STENCIL_ATTACHMENT_EXT
:
185 return &fb
->Attachment
[BUFFER_STENCIL
];
193 * Remove any texture or renderbuffer attached to the given attachment
194 * point. Update reference counts, etc.
197 _mesa_remove_attachment(GLcontext
*ctx
, struct gl_renderbuffer_attachment
*att
)
199 if (att
->Type
== GL_TEXTURE
) {
200 ASSERT(att
->Texture
);
201 if (ctx
->Driver
.FinishRenderTexture
) {
202 /* tell driver that we're done rendering to this texture. */
203 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
205 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
206 ASSERT(!att
->Texture
);
208 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
209 ASSERT(!att
->Texture
);
210 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
211 ASSERT(!att
->Renderbuffer
);
214 att
->Complete
= GL_TRUE
;
219 * Bind a texture object to an attachment point.
220 * The previous binding, if any, will be removed first.
223 _mesa_set_texture_attachment(GLcontext
*ctx
,
224 struct gl_framebuffer
*fb
,
225 struct gl_renderbuffer_attachment
*att
,
226 struct gl_texture_object
*texObj
,
227 GLenum texTarget
, GLuint level
, GLuint zoffset
)
229 if (att
->Texture
== texObj
) {
230 /* re-attaching same texture */
231 ASSERT(att
->Type
== GL_TEXTURE
);
235 _mesa_remove_attachment(ctx
, att
);
236 att
->Type
= GL_TEXTURE
;
237 assert(!att
->Texture
);
238 _mesa_reference_texobj(&att
->Texture
, texObj
);
241 /* always update these fields */
242 att
->TextureLevel
= level
;
243 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
244 att
->Zoffset
= zoffset
;
245 att
->Complete
= GL_FALSE
;
247 if (att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
248 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
251 invalidate_framebuffer(fb
);
256 * Bind a renderbuffer to an attachment point.
257 * The previous binding, if any, will be removed first.
260 _mesa_set_renderbuffer_attachment(GLcontext
*ctx
,
261 struct gl_renderbuffer_attachment
*att
,
262 struct gl_renderbuffer
*rb
)
264 /* XXX check if re-doing same attachment, exit early */
265 _mesa_remove_attachment(ctx
, att
);
266 att
->Type
= GL_RENDERBUFFER_EXT
;
267 att
->Texture
= NULL
; /* just to be safe */
268 att
->Complete
= GL_FALSE
;
269 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
274 * Fallback for ctx->Driver.FramebufferRenderbuffer()
275 * Attach a renderbuffer object to a framebuffer object.
278 _mesa_framebuffer_renderbuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
279 GLenum attachment
, struct gl_renderbuffer
*rb
)
281 struct gl_renderbuffer_attachment
*att
;
283 _glthread_LOCK_MUTEX(fb
->Mutex
);
285 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
288 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
289 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
290 /* do stencil attachment here (depth already done above) */
291 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
293 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
297 _mesa_remove_attachment(ctx
, att
);
300 invalidate_framebuffer(fb
);
302 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
310 att_incomplete(const char *msg
)
313 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
324 fbo_incomplete(const char *msg
, int index
)
327 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
338 * Test if an attachment point is complete and update its Complete field.
339 * \param format if GL_COLOR, this is a color attachment point,
340 * if GL_DEPTH, this is a depth component attachment point,
341 * if GL_STENCIL, this is a stencil component attachment point.
344 test_attachment_completeness(const GLcontext
*ctx
, GLenum format
,
345 struct gl_renderbuffer_attachment
*att
)
347 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
349 /* assume complete */
350 att
->Complete
= GL_TRUE
;
352 /* Look for reasons why the attachment might be incomplete */
353 if (att
->Type
== GL_TEXTURE
) {
354 const struct gl_texture_object
*texObj
= att
->Texture
;
355 struct gl_texture_image
*texImage
;
358 att_incomplete("no texobj");
359 att
->Complete
= GL_FALSE
;
363 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
365 att_incomplete("no teximage");
366 att
->Complete
= GL_FALSE
;
369 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
370 att_incomplete("teximage width/height=0");
371 _mesa_printf("texobj = %u\n", texObj
->Name
);
372 _mesa_printf("level = %d\n", att
->TextureLevel
);
373 att
->Complete
= GL_FALSE
;
376 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
377 att_incomplete("bad z offset");
378 att
->Complete
= GL_FALSE
;
382 if (format
== GL_COLOR
) {
383 if (texImage
->TexFormat
->BaseFormat
!= GL_RGB
&&
384 texImage
->TexFormat
->BaseFormat
!= GL_RGBA
) {
385 att_incomplete("bad format");
386 att
->Complete
= GL_FALSE
;
389 if (texImage
->TexFormat
->TexelBytes
== 0) {
390 att_incomplete("compressed internalformat");
391 att
->Complete
= GL_FALSE
;
395 else if (format
== GL_DEPTH
) {
396 if (texImage
->TexFormat
->BaseFormat
== GL_DEPTH_COMPONENT
) {
399 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
400 ctx
->Extensions
.ARB_depth_texture
&&
401 texImage
->TexFormat
->BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
405 att
->Complete
= GL_FALSE
;
406 att_incomplete("bad depth format");
411 ASSERT(format
== GL_STENCIL
);
412 ASSERT(att
->Renderbuffer
->StencilBits
);
413 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
414 ctx
->Extensions
.ARB_depth_texture
&&
415 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
419 /* no such thing as stencil-only textures */
420 att_incomplete("illegal stencil texture");
421 att
->Complete
= GL_FALSE
;
426 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
427 ASSERT(att
->Renderbuffer
);
428 if (!att
->Renderbuffer
->InternalFormat
||
429 att
->Renderbuffer
->Width
< 1 ||
430 att
->Renderbuffer
->Height
< 1) {
431 att_incomplete("0x0 renderbuffer");
432 att
->Complete
= GL_FALSE
;
435 if (format
== GL_COLOR
) {
436 if (att
->Renderbuffer
->_BaseFormat
!= GL_RGB
&&
437 att
->Renderbuffer
->_BaseFormat
!= GL_RGBA
) {
438 att_incomplete("bad renderbuffer color format");
439 att
->Complete
= GL_FALSE
;
442 ASSERT(att
->Renderbuffer
->RedBits
);
443 ASSERT(att
->Renderbuffer
->GreenBits
);
444 ASSERT(att
->Renderbuffer
->BlueBits
);
446 else if (format
== GL_DEPTH
) {
447 if (att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
448 ASSERT(att
->Renderbuffer
->DepthBits
);
451 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
452 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
453 ASSERT(att
->Renderbuffer
->DepthBits
);
457 att_incomplete("bad renderbuffer depth format");
458 att
->Complete
= GL_FALSE
;
463 assert(format
== GL_STENCIL
);
464 if (att
->Renderbuffer
->_BaseFormat
== GL_STENCIL_INDEX
) {
465 ASSERT(att
->Renderbuffer
->StencilBits
);
468 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
469 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
470 ASSERT(att
->Renderbuffer
->StencilBits
);
474 att
->Complete
= GL_FALSE
;
475 att_incomplete("bad renderbuffer stencil format");
481 ASSERT(att
->Type
== GL_NONE
);
489 * Test if the given framebuffer object is complete and update its
490 * Status field with the results.
491 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
492 * driver to make hardware-specific validation/completeness checks.
493 * Also update the framebuffer's Width and Height fields if the
494 * framebuffer is complete.
497 _mesa_test_framebuffer_completeness(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
500 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
501 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
502 GLint numSamples
= -1;
506 assert(fb
->Name
!= 0);
512 /* Start at -2 to more easily loop over all attachment points.
517 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
518 struct gl_renderbuffer_attachment
*att
;
522 * XXX for ARB_fbo, only check color buffers that are named by
523 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
526 /* check for attachment completeness
529 att
= &fb
->Attachment
[BUFFER_DEPTH
];
530 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
531 if (!att
->Complete
) {
532 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
533 fbo_incomplete("depth attachment incomplete", -1);
538 att
= &fb
->Attachment
[BUFFER_STENCIL
];
539 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
540 if (!att
->Complete
) {
541 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
542 fbo_incomplete("stencil attachment incomplete", -1);
547 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
548 test_attachment_completeness(ctx
, GL_COLOR
, att
);
549 if (!att
->Complete
) {
550 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
551 fbo_incomplete("color attachment incomplete", i
);
556 /* get width, height, format of the renderbuffer/texture
558 if (att
->Type
== GL_TEXTURE
) {
559 const struct gl_texture_image
*texImg
560 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
561 minWidth
= MIN2(minWidth
, texImg
->Width
);
562 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
563 minHeight
= MIN2(minHeight
, texImg
->Height
);
564 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
565 f
= texImg
->_BaseFormat
;
567 if (f
!= GL_RGB
&& f
!= GL_RGBA
&& f
!= GL_DEPTH_COMPONENT
568 && f
!= GL_DEPTH_STENCIL_EXT
) {
569 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
570 fbo_incomplete("texture attachment incomplete", -1);
574 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
575 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
576 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
577 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
578 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
579 f
= att
->Renderbuffer
->InternalFormat
;
583 assert(att
->Type
== GL_NONE
);
587 if (numSamples
< 0) {
589 numSamples
= att
->Renderbuffer
->NumSamples
;
592 /* Error-check width, height, format, samples
594 if (numImages
== 1) {
595 /* save format, num samples */
601 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
602 /* check that width, height, format are same */
603 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
604 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
605 fbo_incomplete("width or height mismatch", -1);
608 /* check that all color buffer have same format */
609 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
610 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
611 fbo_incomplete("format mismatch", -1);
615 if (att
->Renderbuffer
&&
616 att
->Renderbuffer
->NumSamples
!= numSamples
) {
617 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
618 fbo_incomplete("inconsistant number of samples", i
);
625 #ifndef FEATURE_OES_framebuffer_object
626 /* Check that all DrawBuffers are present */
627 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
628 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
629 const struct gl_renderbuffer_attachment
*att
630 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
632 if (att
->Type
== GL_NONE
) {
633 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
634 fbo_incomplete("missing drawbuffer", j
);
640 /* Check that the ReadBuffer is present */
641 if (fb
->ColorReadBuffer
!= GL_NONE
) {
642 const struct gl_renderbuffer_attachment
*att
643 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
645 if (att
->Type
== GL_NONE
) {
646 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
647 fbo_incomplete("missing readbuffer", -1);
653 if (numImages
== 0) {
654 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
655 fbo_incomplete("no attachments", -1);
659 /* Provisionally set status = COMPLETE ... */
660 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
662 /* ... but the driver may say the FB is incomplete.
663 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
666 if (ctx
->Driver
.ValidateFramebuffer
) {
667 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
668 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
669 fbo_incomplete("driver marked FBO as incomplete", -1);
673 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
675 * Note that if ARB_framebuffer_object is supported and the attached
676 * renderbuffers/textures are different sizes, the framebuffer
677 * width/height will be set to the smallest width/height.
679 fb
->Width
= minWidth
;
680 fb
->Height
= minHeight
;
682 /* finally, update the visual info for the framebuffer */
683 _mesa_update_framebuffer_visual(fb
);
689 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
691 GET_CURRENT_CONTEXT(ctx
);
692 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
694 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
695 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
703 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
705 struct gl_renderbuffer
*newRb
;
706 GET_CURRENT_CONTEXT(ctx
);
708 ASSERT_OUTSIDE_BEGIN_END(ctx
);
710 if (target
!= GL_RENDERBUFFER_EXT
) {
711 _mesa_error(ctx
, GL_INVALID_ENUM
,
712 "glBindRenderbufferEXT(target)");
716 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
719 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
720 if (newRb
== &DummyRenderbuffer
) {
721 /* ID was reserved, but no real renderbuffer object made yet */
724 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
725 /* All RB IDs must be Gen'd */
726 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
731 /* create new renderbuffer object */
732 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
734 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
737 ASSERT(newRb
->AllocStorage
);
738 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
739 newRb
->RefCount
= 1; /* referenced by hash table */
746 ASSERT(newRb
!= &DummyRenderbuffer
);
748 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
753 * If the given renderbuffer is anywhere attached to the framebuffer, detach
755 * This is used when a renderbuffer object is deleted.
756 * The spec calls for unbinding.
759 detach_renderbuffer(GLcontext
*ctx
,
760 struct gl_framebuffer
*fb
,
761 struct gl_renderbuffer
*rb
)
764 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
765 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
766 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
769 invalidate_framebuffer(fb
);
774 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
777 GET_CURRENT_CONTEXT(ctx
);
779 ASSERT_OUTSIDE_BEGIN_END(ctx
);
780 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
782 for (i
= 0; i
< n
; i
++) {
783 if (renderbuffers
[i
] > 0) {
784 struct gl_renderbuffer
*rb
;
785 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
787 /* check if deleting currently bound renderbuffer object */
788 if (rb
== ctx
->CurrentRenderbuffer
) {
790 ASSERT(rb
->RefCount
>= 2);
791 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
794 if (ctx
->DrawBuffer
->Name
) {
795 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
797 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
798 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
801 /* Remove from hash table immediately, to free the ID.
802 * But the object will not be freed until it's no longer
803 * referenced anywhere else.
805 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
807 if (rb
!= &DummyRenderbuffer
) {
808 /* no longer referenced by hash table */
809 _mesa_reference_renderbuffer(&rb
, NULL
);
818 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
820 GET_CURRENT_CONTEXT(ctx
);
824 ASSERT_OUTSIDE_BEGIN_END(ctx
);
827 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
834 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
836 for (i
= 0; i
< n
; i
++) {
837 GLuint name
= first
+ i
;
838 renderbuffers
[i
] = name
;
839 /* insert dummy placeholder into hash table */
840 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
841 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
842 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
848 * Given an internal format token for a render buffer, return the
849 * corresponding base format.
850 * This is very similar to _mesa_base_tex_format() but the set of valid
851 * internal formats is somewhat different.
853 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
854 * GL_DEPTH_STENCIL_EXT or zero if error.
857 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
859 switch (internalFormat
) {
878 case GL_STENCIL_INDEX
:
879 case GL_STENCIL_INDEX1_EXT
:
880 case GL_STENCIL_INDEX4_EXT
:
881 case GL_STENCIL_INDEX8_EXT
:
882 case GL_STENCIL_INDEX16_EXT
:
883 return GL_STENCIL_INDEX
;
884 case GL_DEPTH_COMPONENT
:
885 case GL_DEPTH_COMPONENT16
:
886 case GL_DEPTH_COMPONENT24
:
887 case GL_DEPTH_COMPONENT32
:
888 return GL_DEPTH_COMPONENT
;
889 case GL_DEPTH_STENCIL_EXT
:
890 case GL_DEPTH24_STENCIL8_EXT
:
891 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
892 return GL_DEPTH_STENCIL_EXT
;
895 /* XXX add floating point formats eventually */
902 /** sentinal value, see below */
903 #define NO_SAMPLES 1000
907 * Helper function used by _mesa_RenderbufferStorageEXT() and
908 * _mesa_RenderbufferStorageMultisample().
909 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
912 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
913 GLsizei width
, GLsizei height
, GLsizei samples
)
915 const char *func
= samples
== NO_SAMPLES
?
916 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
917 struct gl_renderbuffer
*rb
;
919 GET_CURRENT_CONTEXT(ctx
);
921 ASSERT_OUTSIDE_BEGIN_END(ctx
);
923 if (target
!= GL_RENDERBUFFER_EXT
) {
924 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
928 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
929 if (baseFormat
== 0) {
930 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
934 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
935 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
939 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
940 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
944 if (samples
== NO_SAMPLES
) {
945 /* NumSamples == 0 indicates non-multisampling */
948 else if (samples
> ctx
->Const
.MaxSamples
) {
949 /* note: driver may choose to use more samples than what's requested */
950 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
954 rb
= ctx
->CurrentRenderbuffer
;
956 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
960 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
962 if (rb
->InternalFormat
== internalFormat
&&
963 rb
->Width
== (GLuint
) width
&&
964 rb
->Height
== (GLuint
) height
) {
965 /* no change in allocation needed */
969 /* These MUST get set by the AllocStorage func */
970 rb
->_ActualFormat
= 0;
978 rb
->NumSamples
= samples
;
980 /* Now allocate the storage */
981 ASSERT(rb
->AllocStorage
);
982 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
983 /* No error - check/set fields now */
984 assert(rb
->_ActualFormat
);
985 assert(rb
->Width
== (GLuint
) width
);
986 assert(rb
->Height
== (GLuint
) height
);
987 assert(rb
->RedBits
|| rb
->GreenBits
|| rb
->BlueBits
|| rb
->AlphaBits
||
988 rb
->DepthBits
|| rb
->StencilBits
|| rb
->IndexBits
);
989 rb
->InternalFormat
= internalFormat
;
990 rb
->_BaseFormat
= baseFormat
;
993 /* Probably ran out of memory - clear the fields */
996 rb
->InternalFormat
= GL_NONE
;
997 rb
->_ActualFormat
= GL_NONE
;
998 rb
->_BaseFormat
= GL_NONE
;
1010 test_framebuffer_completeness(ctx, fb);
1012 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1019 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1020 GLsizei width
, GLsizei height
)
1022 /* GL_ARB_fbo says calling this function is equivalent to calling
1023 * glRenderbufferStorageMultisample() with samples=0. We pass in
1024 * a token value here just for error reporting purposes.
1026 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1031 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1032 GLenum internalFormat
,
1033 GLsizei width
, GLsizei height
)
1035 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1041 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1043 struct gl_renderbuffer
*rb
;
1044 GET_CURRENT_CONTEXT(ctx
);
1046 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1048 if (target
!= GL_RENDERBUFFER_EXT
) {
1049 _mesa_error(ctx
, GL_INVALID_ENUM
,
1050 "glGetRenderbufferParameterivEXT(target)");
1054 rb
= ctx
->CurrentRenderbuffer
;
1056 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1057 "glGetRenderbufferParameterivEXT");
1061 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1064 case GL_RENDERBUFFER_WIDTH_EXT
:
1065 *params
= rb
->Width
;
1067 case GL_RENDERBUFFER_HEIGHT_EXT
:
1068 *params
= rb
->Height
;
1070 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1071 *params
= rb
->InternalFormat
;
1073 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1074 *params
= rb
->RedBits
;
1076 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1077 *params
= rb
->GreenBits
;
1079 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1080 *params
= rb
->BlueBits
;
1082 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1083 *params
= rb
->AlphaBits
;
1085 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1086 *params
= rb
->DepthBits
;
1088 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1089 *params
= rb
->StencilBits
;
1091 case GL_RENDERBUFFER_SAMPLES
:
1092 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1093 *params
= rb
->NumSamples
;
1098 _mesa_error(ctx
, GL_INVALID_ENUM
,
1099 "glGetRenderbufferParameterivEXT(target)");
1105 GLboolean GLAPIENTRY
1106 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1108 GET_CURRENT_CONTEXT(ctx
);
1109 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1111 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1112 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1120 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1123 ASSERT(ctx
->Driver
.RenderTexture
);
1124 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1125 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1126 struct gl_texture_object
*texObj
= att
->Texture
;
1128 && texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1129 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1136 * Examine all the framebuffer's attachments to see if any are textures.
1137 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1138 * notify the device driver that the texture image may have changed.
1141 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1143 if (ctx
->Driver
.FinishRenderTexture
) {
1145 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1146 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1147 if (att
->Texture
&& att
->Renderbuffer
) {
1148 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1156 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1158 struct gl_framebuffer
*newFb
, *newFbread
;
1159 GLboolean bindReadBuf
, bindDrawBuf
;
1160 GET_CURRENT_CONTEXT(ctx
);
1163 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1164 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1165 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1169 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1171 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1172 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1173 "glBindFramebufferEXT(unsupported)");
1178 #if FEATURE_EXT_framebuffer_blit
1179 case GL_DRAW_FRAMEBUFFER_EXT
:
1180 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1181 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1184 bindDrawBuf
= GL_TRUE
;
1185 bindReadBuf
= GL_FALSE
;
1187 case GL_READ_FRAMEBUFFER_EXT
:
1188 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1189 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1192 bindDrawBuf
= GL_FALSE
;
1193 bindReadBuf
= GL_TRUE
;
1196 case GL_FRAMEBUFFER_EXT
:
1197 bindDrawBuf
= GL_TRUE
;
1198 bindReadBuf
= GL_TRUE
;
1201 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1205 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1208 /* Binding a user-created framebuffer object */
1209 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1210 if (newFb
== &DummyFramebuffer
) {
1211 /* ID was reserved, but no real framebuffer object made yet */
1214 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1215 /* All FBO IDs must be Gen'd */
1216 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1221 /* create new framebuffer object */
1222 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1224 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1227 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1232 /* Binding the window system framebuffer (which was originally set
1233 * with MakeCurrent).
1235 newFb
= ctx
->WinSysDrawBuffer
;
1236 newFbread
= ctx
->WinSysReadBuffer
;
1240 ASSERT(newFb
!= &DummyFramebuffer
);
1243 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1247 if (ctx
->ReadBuffer
== newFbread
)
1248 bindReadBuf
= GL_FALSE
; /* no change */
1250 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1254 /* check if old FB had any texture attachments */
1255 if (ctx
->DrawBuffer
->Name
!= 0) {
1256 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1259 if (ctx
->DrawBuffer
== newFb
)
1260 bindDrawBuf
= GL_FALSE
; /* no change */
1262 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1264 if (newFb
->Name
!= 0) {
1265 /* check if newly bound framebuffer has any texture attachments */
1266 check_begin_texture_render(ctx
, newFb
);
1270 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1271 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1277 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1280 GET_CURRENT_CONTEXT(ctx
);
1282 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1283 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1285 for (i
= 0; i
< n
; i
++) {
1286 if (framebuffers
[i
] > 0) {
1287 struct gl_framebuffer
*fb
;
1288 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1290 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1292 /* check if deleting currently bound framebuffer object */
1293 if (fb
== ctx
->DrawBuffer
) {
1295 ASSERT(fb
->RefCount
>= 2);
1296 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1298 if (fb
== ctx
->ReadBuffer
) {
1300 ASSERT(fb
->RefCount
>= 2);
1301 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1304 /* remove from hash table immediately, to free the ID */
1305 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1307 if (fb
!= &DummyFramebuffer
) {
1308 /* But the object will not be freed until it's no longer
1309 * bound in any context.
1311 _mesa_reference_framebuffer(&fb
, NULL
);
1320 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1322 GET_CURRENT_CONTEXT(ctx
);
1326 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1329 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1336 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1338 for (i
= 0; i
< n
; i
++) {
1339 GLuint name
= first
+ i
;
1340 framebuffers
[i
] = name
;
1341 /* insert dummy placeholder into hash table */
1342 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1343 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1344 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1351 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1353 struct gl_framebuffer
*buffer
;
1354 GET_CURRENT_CONTEXT(ctx
);
1356 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1359 #if FEATURE_EXT_framebuffer_blit
1360 case GL_DRAW_FRAMEBUFFER_EXT
:
1361 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1362 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1365 buffer
= ctx
->DrawBuffer
;
1367 case GL_READ_FRAMEBUFFER_EXT
:
1368 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1369 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1372 buffer
= ctx
->ReadBuffer
;
1375 case GL_FRAMEBUFFER_EXT
:
1376 buffer
= ctx
->DrawBuffer
;
1379 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1380 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1383 if (buffer
->Name
== 0) {
1384 /* The window system / default framebuffer is always complete */
1385 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1388 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1390 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1391 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1394 return buffer
->_Status
;
1400 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1403 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1404 GLenum attachment
, GLenum textarget
, GLuint texture
,
1405 GLint level
, GLint zoffset
)
1407 struct gl_renderbuffer_attachment
*att
;
1408 struct gl_texture_object
*texObj
= NULL
;
1409 struct gl_framebuffer
*fb
;
1410 GLboolean error
= GL_FALSE
;
1412 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1415 case GL_READ_FRAMEBUFFER_EXT
:
1416 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1417 fb
= ctx
->ReadBuffer
;
1419 case GL_DRAW_FRAMEBUFFER_EXT
:
1420 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1422 case GL_FRAMEBUFFER_EXT
:
1423 fb
= ctx
->DrawBuffer
;
1430 _mesa_error(ctx
, GL_INVALID_ENUM
,
1431 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1437 /* check framebuffer binding */
1438 if (fb
->Name
== 0) {
1439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1440 "glFramebufferTexture%sEXT", caller
);
1445 /* The textarget, level, and zoffset parameters are only validated if
1446 * texture is non-zero.
1449 GLboolean err
= GL_TRUE
;
1451 texObj
= _mesa_lookup_texture(ctx
, texture
);
1452 if (texObj
!= NULL
) {
1453 if (textarget
== 0) {
1454 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1455 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1456 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1459 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1460 ? !IS_CUBE_FACE(textarget
)
1461 : (texObj
->Target
!= textarget
);
1466 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1467 "glFramebufferTexture%sEXT(texture target mismatch)",
1472 if (texObj
->Target
== GL_TEXTURE_3D
) {
1473 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1474 if (zoffset
< 0 || zoffset
>= maxSize
) {
1475 _mesa_error(ctx
, GL_INVALID_VALUE
,
1476 "glFramebufferTexture%sEXT(zoffset)", caller
);
1480 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1481 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1482 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1483 _mesa_error(ctx
, GL_INVALID_VALUE
,
1484 "glFramebufferTexture%sEXT(layer)", caller
);
1490 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1491 _mesa_error(ctx
, GL_INVALID_VALUE
,
1492 "glFramebufferTexture%sEXT(level)", caller
);
1497 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1499 _mesa_error(ctx
, GL_INVALID_ENUM
,
1500 "glFramebufferTexture%sEXT(attachment)", caller
);
1504 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1506 _glthread_LOCK_MUTEX(fb
->Mutex
);
1508 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1510 /* Set the render-to-texture flag. We'll check this flag in
1511 * glTexImage() and friends to determine if we need to revalidate
1512 * any FBOs that might be rendering into this texture.
1513 * This flag never gets cleared since it's non-trivial to determine
1514 * when all FBOs might be done rendering to this texture. That's OK
1515 * though since it's uncommon to render to a texture then repeatedly
1516 * call glTexImage() to change images in the texture.
1518 texObj
->_RenderToTexture
= GL_TRUE
;
1521 _mesa_remove_attachment(ctx
, att
);
1524 invalidate_framebuffer(fb
);
1526 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1532 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1533 GLenum textarget
, GLuint texture
, GLint level
)
1535 GET_CURRENT_CONTEXT(ctx
);
1537 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1538 _mesa_error(ctx
, GL_INVALID_ENUM
,
1539 "glFramebufferTexture1DEXT(textarget)");
1543 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1549 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1550 GLenum textarget
, GLuint texture
, GLint level
)
1552 GET_CURRENT_CONTEXT(ctx
);
1554 if ((texture
!= 0) &&
1555 (textarget
!= GL_TEXTURE_2D
) &&
1556 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1557 (!IS_CUBE_FACE(textarget
))) {
1558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1559 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1563 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1569 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1570 GLenum textarget
, GLuint texture
,
1571 GLint level
, GLint zoffset
)
1573 GET_CURRENT_CONTEXT(ctx
);
1575 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1576 _mesa_error(ctx
, GL_INVALID_ENUM
,
1577 "glFramebufferTexture3DEXT(textarget)");
1581 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1587 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1588 GLuint texture
, GLint level
, GLint layer
)
1590 GET_CURRENT_CONTEXT(ctx
);
1592 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1598 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1599 GLenum renderbufferTarget
,
1600 GLuint renderbuffer
)
1602 struct gl_renderbuffer_attachment
*att
;
1603 struct gl_framebuffer
*fb
;
1604 struct gl_renderbuffer
*rb
;
1605 GET_CURRENT_CONTEXT(ctx
);
1607 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1610 #if FEATURE_EXT_framebuffer_blit
1611 case GL_DRAW_FRAMEBUFFER_EXT
:
1612 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1613 _mesa_error(ctx
, GL_INVALID_ENUM
,
1614 "glFramebufferRenderbufferEXT(target)");
1617 fb
= ctx
->DrawBuffer
;
1619 case GL_READ_FRAMEBUFFER_EXT
:
1620 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1621 _mesa_error(ctx
, GL_INVALID_ENUM
,
1622 "glFramebufferRenderbufferEXT(target)");
1625 fb
= ctx
->ReadBuffer
;
1628 case GL_FRAMEBUFFER_EXT
:
1629 fb
= ctx
->DrawBuffer
;
1632 _mesa_error(ctx
, GL_INVALID_ENUM
,
1633 "glFramebufferRenderbufferEXT(target)");
1637 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1638 _mesa_error(ctx
, GL_INVALID_ENUM
,
1639 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1643 if (fb
->Name
== 0) {
1644 /* Can't attach new renderbuffers to a window system framebuffer */
1645 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1649 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1651 _mesa_error(ctx
, GL_INVALID_ENUM
,
1652 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1653 _mesa_lookup_enum_by_nr(attachment
));
1658 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1660 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1661 "glFramebufferRenderbufferEXT(non-existant"
1662 " renderbuffer %u)", renderbuffer
);
1667 /* remove renderbuffer attachment */
1671 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1672 /* make sure the renderbuffer is a depth/stencil format */
1673 if (rb
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1674 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1675 "glFramebufferRenderbufferEXT(renderbuffer"
1676 " is not DEPTH_STENCIL format)");
1682 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1684 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1685 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1687 /* Some subsequent GL commands may depend on the framebuffer's visual
1688 * after the binding is updated. Update visual info now.
1690 _mesa_update_framebuffer_visual(fb
);
1695 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1696 GLenum pname
, GLint
*params
)
1698 const struct gl_renderbuffer_attachment
*att
;
1699 struct gl_framebuffer
*buffer
;
1700 GET_CURRENT_CONTEXT(ctx
);
1702 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1705 #if FEATURE_EXT_framebuffer_blit
1706 case GL_DRAW_FRAMEBUFFER_EXT
:
1707 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1708 _mesa_error(ctx
, GL_INVALID_ENUM
,
1709 "glGetFramebufferAttachmentParameterivEXT(target)");
1712 buffer
= ctx
->DrawBuffer
;
1714 case GL_READ_FRAMEBUFFER_EXT
:
1715 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1716 _mesa_error(ctx
, GL_INVALID_ENUM
,
1717 "glGetFramebufferAttachmentParameterivEXT(target)");
1720 buffer
= ctx
->ReadBuffer
;
1723 case GL_FRAMEBUFFER_EXT
:
1724 buffer
= ctx
->DrawBuffer
;
1727 _mesa_error(ctx
, GL_INVALID_ENUM
,
1728 "glGetFramebufferAttachmentParameterivEXT(target)");
1732 if (buffer
->Name
== 0) {
1733 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1734 "glGetFramebufferAttachmentParameterivEXT");
1738 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1740 _mesa_error(ctx
, GL_INVALID_ENUM
,
1741 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1745 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1746 /* the depth and stencil attachments must point to the same buffer */
1747 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1748 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1749 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1750 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1751 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1752 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1753 " attachments differ)");
1758 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1761 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1762 *params
= att
->Type
;
1764 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1765 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1766 *params
= att
->Renderbuffer
->Name
;
1768 else if (att
->Type
== GL_TEXTURE
) {
1769 *params
= att
->Texture
->Name
;
1772 _mesa_error(ctx
, GL_INVALID_ENUM
,
1773 "glGetFramebufferAttachmentParameterivEXT(pname)");
1776 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1777 if (att
->Type
== GL_TEXTURE
) {
1778 *params
= att
->TextureLevel
;
1781 _mesa_error(ctx
, GL_INVALID_ENUM
,
1782 "glGetFramebufferAttachmentParameterivEXT(pname)");
1785 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1786 if (att
->Type
== GL_TEXTURE
) {
1787 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1788 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1795 _mesa_error(ctx
, GL_INVALID_ENUM
,
1796 "glGetFramebufferAttachmentParameterivEXT(pname)");
1799 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1800 if (att
->Type
== GL_TEXTURE
) {
1801 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1802 *params
= att
->Zoffset
;
1809 _mesa_error(ctx
, GL_INVALID_ENUM
,
1810 "glGetFramebufferAttachmentParameterivEXT(pname)");
1813 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1814 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1815 _mesa_error(ctx
, GL_INVALID_ENUM
,
1816 "glGetFramebufferAttachmentParameterivEXT(pname)");
1819 *params
= att
->Renderbuffer
->ColorEncoding
;
1822 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1823 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1824 _mesa_error(ctx
, GL_INVALID_ENUM
,
1825 "glGetFramebufferAttachmentParameterivEXT(pname)");
1829 *params
= att
->Renderbuffer
->ComponentType
;
1832 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1833 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1834 _mesa_error(ctx
, GL_INVALID_ENUM
,
1835 "glGetFramebufferAttachmentParameterivEXT(pname)");
1838 *params
= att
->Renderbuffer
->RedBits
;
1841 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1842 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1843 _mesa_error(ctx
, GL_INVALID_ENUM
,
1844 "glGetFramebufferAttachmentParameterivEXT(pname)");
1847 *params
= att
->Renderbuffer
->GreenBits
;
1850 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1851 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1852 _mesa_error(ctx
, GL_INVALID_ENUM
,
1853 "glGetFramebufferAttachmentParameterivEXT(pname)");
1856 *params
= att
->Renderbuffer
->BlueBits
;
1859 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1860 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1861 _mesa_error(ctx
, GL_INVALID_ENUM
,
1862 "glGetFramebufferAttachmentParameterivEXT(pname)");
1865 *params
= att
->Renderbuffer
->AlphaBits
;
1868 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1869 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1870 _mesa_error(ctx
, GL_INVALID_ENUM
,
1871 "glGetFramebufferAttachmentParameterivEXT(pname)");
1874 *params
= att
->Renderbuffer
->DepthBits
;
1877 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1878 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1879 _mesa_error(ctx
, GL_INVALID_ENUM
,
1880 "glGetFramebufferAttachmentParameterivEXT(pname)");
1883 *params
= att
->Renderbuffer
->StencilBits
;
1887 _mesa_error(ctx
, GL_INVALID_ENUM
,
1888 "glGetFramebufferAttachmentParameterivEXT(pname)");
1895 _mesa_GenerateMipmapEXT(GLenum target
)
1897 struct gl_texture_unit
*texUnit
;
1898 struct gl_texture_object
*texObj
;
1899 GET_CURRENT_CONTEXT(ctx
);
1901 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1902 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1908 case GL_TEXTURE_CUBE_MAP
:
1909 /* OK, legal value */
1912 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1916 texUnit
= _mesa_get_current_tex_unit(ctx
);
1917 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1919 _mesa_lock_texture(ctx
, texObj
);
1920 if (target
== GL_TEXTURE_CUBE_MAP
) {
1922 for (face
= 0; face
< 6; face
++)
1923 ctx
->Driver
.GenerateMipmap(ctx
,
1924 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1928 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1930 _mesa_unlock_texture(ctx
, texObj
);
1934 #if FEATURE_EXT_framebuffer_blit
1936 * Blit rectangular region, optionally from one framebuffer to another.
1938 * Note, if the src buffer is multisampled and the dest is not, this is
1939 * when the samples must be resolved to a single color.
1942 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1943 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1944 GLbitfield mask
, GLenum filter
)
1946 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1947 GL_DEPTH_BUFFER_BIT
|
1948 GL_STENCIL_BUFFER_BIT
);
1949 const struct gl_framebuffer
*readFb
, *drawFb
;
1950 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1951 GET_CURRENT_CONTEXT(ctx
);
1953 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1954 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1956 if (ctx
->NewState
) {
1957 _mesa_update_state(ctx
);
1960 readFb
= ctx
->ReadBuffer
;
1961 drawFb
= ctx
->DrawBuffer
;
1963 if (!readFb
|| !drawFb
) {
1964 /* This will normally never happen but someday we may want to
1965 * support MakeCurrent() with no drawables.
1970 /* check for complete framebuffers */
1971 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
1972 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1973 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1974 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1978 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
1979 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
1983 if (mask
& ~legalMaskBits
) {
1984 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
1988 /* depth/stencil must be blitted with nearest filtering */
1989 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
1990 && filter
!= GL_NEAREST
) {
1991 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1992 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
1996 /* get color read/draw renderbuffers */
1997 if (mask
& GL_COLOR_BUFFER_BIT
) {
1998 colorReadRb
= readFb
->_ColorReadBuffer
;
1999 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2002 colorReadRb
= colorDrawRb
= NULL
;
2005 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2006 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2007 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2010 readRb
->StencilBits
!= drawRb
->StencilBits
) {
2011 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2012 "glBlitFramebufferEXT(stencil buffer size mismatch");
2017 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2018 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2019 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2022 readRb
->DepthBits
!= drawRb
->DepthBits
) {
2023 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2024 "glBlitFramebufferEXT(depth buffer size mismatch");
2029 if (readFb
->Visual
.samples
> 0 &&
2030 drawFb
->Visual
.samples
> 0 &&
2031 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2032 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2033 "glBlitFramebufferEXT(mismatched samples");
2037 /* extra checks for multisample copies... */
2038 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2039 /* src and dest region sizes must be the same */
2040 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2041 srcY1
- srcY0
!= dstY1
- dstY0
) {
2042 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2043 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2047 /* color formats must match */
2050 colorReadRb
->_ActualFormat
!= colorDrawRb
->_ActualFormat
) {
2051 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2052 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2057 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2058 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2062 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2063 ctx
->Driver
.BlitFramebuffer(ctx
,
2064 srcX0
, srcY0
, srcX1
, srcY1
,
2065 dstX0
, dstY0
, dstX1
, dstY1
,
2068 #endif /* FEATURE_EXT_framebuffer_blit */