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
40 #include "framebuffer.h"
43 #include "mfeatures.h"
45 #include "renderbuffer.h"
51 /** Set this to 1 to help debug FBO incompleteness problems */
54 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
61 * None of the GL_EXT_framebuffer_object functions are compiled into
68 * When glGenRender/FramebuffersEXT() is called we insert pointers to
69 * these placeholder objects into the hash table.
70 * Later, when the object ID is first bound, we replace the placeholder
71 * with the real frame/renderbuffer.
73 static struct gl_framebuffer DummyFramebuffer
;
74 static struct gl_renderbuffer DummyRenderbuffer
;
76 /* We bind this framebuffer when applications pass a NULL
77 * drawable/surface in make current. */
78 static struct gl_framebuffer IncompleteFramebuffer
;
81 #define IS_CUBE_FACE(TARGET) \
82 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
83 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
87 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
93 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
100 _mesa_init_fbobjects(struct gl_context
*ctx
)
102 _glthread_INIT_MUTEX(DummyFramebuffer
.Mutex
);
103 _glthread_INIT_MUTEX(DummyRenderbuffer
.Mutex
);
104 _glthread_INIT_MUTEX(IncompleteFramebuffer
.Mutex
);
105 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
106 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
107 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
110 struct gl_framebuffer
*
111 _mesa_get_incomplete_framebuffer(void)
113 return &IncompleteFramebuffer
;
117 * Helper routine for getting a gl_renderbuffer.
119 struct gl_renderbuffer
*
120 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
122 struct gl_renderbuffer
*rb
;
127 rb
= (struct gl_renderbuffer
*)
128 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
134 * Helper routine for getting a gl_framebuffer.
136 struct gl_framebuffer
*
137 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
139 struct gl_framebuffer
*fb
;
144 fb
= (struct gl_framebuffer
*)
145 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
151 * Mark the given framebuffer as invalid. This will force the
152 * test for framebuffer completeness to be done before the framebuffer
156 invalidate_framebuffer(struct gl_framebuffer
*fb
)
158 fb
->_Status
= 0; /* "indeterminate" */
163 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
164 * gl_renderbuffer_attachment object.
165 * This function is only used for user-created FB objects, not the
166 * default / window-system FB object.
167 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
168 * the depth buffer attachment point.
170 struct gl_renderbuffer_attachment
*
171 _mesa_get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
176 assert(fb
->Name
> 0);
178 switch (attachment
) {
179 case GL_COLOR_ATTACHMENT0_EXT
:
180 case GL_COLOR_ATTACHMENT1_EXT
:
181 case GL_COLOR_ATTACHMENT2_EXT
:
182 case GL_COLOR_ATTACHMENT3_EXT
:
183 case GL_COLOR_ATTACHMENT4_EXT
:
184 case GL_COLOR_ATTACHMENT5_EXT
:
185 case GL_COLOR_ATTACHMENT6_EXT
:
186 case GL_COLOR_ATTACHMENT7_EXT
:
187 case GL_COLOR_ATTACHMENT8_EXT
:
188 case GL_COLOR_ATTACHMENT9_EXT
:
189 case GL_COLOR_ATTACHMENT10_EXT
:
190 case GL_COLOR_ATTACHMENT11_EXT
:
191 case GL_COLOR_ATTACHMENT12_EXT
:
192 case GL_COLOR_ATTACHMENT13_EXT
:
193 case GL_COLOR_ATTACHMENT14_EXT
:
194 case GL_COLOR_ATTACHMENT15_EXT
:
195 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
196 if (i
>= ctx
->Const
.MaxColorAttachments
) {
199 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
200 case GL_DEPTH_STENCIL_ATTACHMENT
:
202 case GL_DEPTH_BUFFER
:
203 /* fall-through / new in GL 3.0 */
204 case GL_DEPTH_ATTACHMENT_EXT
:
205 return &fb
->Attachment
[BUFFER_DEPTH
];
206 case GL_STENCIL_BUFFER
:
207 /* fall-through / new in GL 3.0 */
208 case GL_STENCIL_ATTACHMENT_EXT
:
209 return &fb
->Attachment
[BUFFER_STENCIL
];
217 * As above, but only used for getting attachments of the default /
218 * window-system framebuffer (not user-created framebuffer objects).
220 static struct gl_renderbuffer_attachment
*
221 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
224 assert(fb
->Name
== 0);
226 switch (attachment
) {
228 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
230 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
232 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
234 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
236 if (fb
->Visual
.numAuxBuffers
== 1) {
237 return &fb
->Attachment
[BUFFER_AUX0
];
240 case GL_DEPTH_BUFFER
:
241 /* fall-through / new in GL 3.0 */
242 case GL_DEPTH_ATTACHMENT_EXT
:
243 return &fb
->Attachment
[BUFFER_DEPTH
];
244 case GL_STENCIL_BUFFER
:
245 /* fall-through / new in GL 3.0 */
246 case GL_STENCIL_ATTACHMENT_EXT
:
247 return &fb
->Attachment
[BUFFER_STENCIL
];
256 * Remove any texture or renderbuffer attached to the given attachment
257 * point. Update reference counts, etc.
260 _mesa_remove_attachment(struct gl_context
*ctx
,
261 struct gl_renderbuffer_attachment
*att
)
263 if (att
->Type
== GL_TEXTURE
) {
264 ASSERT(att
->Texture
);
265 if (ctx
->Driver
.FinishRenderTexture
) {
266 /* tell driver that we're done rendering to this texture. */
267 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
269 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
270 ASSERT(!att
->Texture
);
272 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
273 ASSERT(!att
->Texture
);
274 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
275 ASSERT(!att
->Renderbuffer
);
278 att
->Complete
= GL_TRUE
;
283 * Bind a texture object to an attachment point.
284 * The previous binding, if any, will be removed first.
287 _mesa_set_texture_attachment(struct gl_context
*ctx
,
288 struct gl_framebuffer
*fb
,
289 struct gl_renderbuffer_attachment
*att
,
290 struct gl_texture_object
*texObj
,
291 GLenum texTarget
, GLuint level
, GLuint zoffset
)
293 if (att
->Texture
== texObj
) {
294 /* re-attaching same texture */
295 ASSERT(att
->Type
== GL_TEXTURE
);
296 if (ctx
->Driver
.FinishRenderTexture
)
297 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
301 if (ctx
->Driver
.FinishRenderTexture
&& att
->Texture
)
302 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
303 _mesa_remove_attachment(ctx
, att
);
304 att
->Type
= GL_TEXTURE
;
305 assert(!att
->Texture
);
306 _mesa_reference_texobj(&att
->Texture
, texObj
);
309 /* always update these fields */
310 att
->TextureLevel
= level
;
311 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
312 att
->Zoffset
= zoffset
;
313 att
->Complete
= GL_FALSE
;
315 if (att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
316 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
319 invalidate_framebuffer(fb
);
324 * Bind a renderbuffer to an attachment point.
325 * The previous binding, if any, will be removed first.
328 _mesa_set_renderbuffer_attachment(struct gl_context
*ctx
,
329 struct gl_renderbuffer_attachment
*att
,
330 struct gl_renderbuffer
*rb
)
332 /* XXX check if re-doing same attachment, exit early */
333 _mesa_remove_attachment(ctx
, att
);
334 att
->Type
= GL_RENDERBUFFER_EXT
;
335 att
->Texture
= NULL
; /* just to be safe */
336 att
->Complete
= GL_FALSE
;
337 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
342 * Fallback for ctx->Driver.FramebufferRenderbuffer()
343 * Attach a renderbuffer object to a framebuffer object.
346 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
347 struct gl_framebuffer
*fb
,
348 GLenum attachment
, struct gl_renderbuffer
*rb
)
350 struct gl_renderbuffer_attachment
*att
;
352 _glthread_LOCK_MUTEX(fb
->Mutex
);
354 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
357 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
358 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
359 /* do stencil attachment here (depth already done above) */
360 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
362 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
366 _mesa_remove_attachment(ctx
, att
);
369 invalidate_framebuffer(fb
);
371 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
379 att_incomplete(const char *msg
)
382 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
393 fbo_incomplete(const char *msg
, int index
)
396 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
405 * Is the given base format a legal format for a color renderbuffer?
408 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
410 switch (baseFormat
) {
415 case GL_LUMINANCE_ALPHA
:
418 return ctx
->Extensions
.ARB_framebuffer_object
;
421 return ctx
->Extensions
.ARB_texture_rg
;
429 * Is the given base format a legal format for a depth/stencil renderbuffer?
432 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
434 switch (baseFormat
) {
435 case GL_DEPTH_COMPONENT
:
436 case GL_DEPTH_STENCIL_EXT
:
445 * Test if an attachment point is complete and update its Complete field.
446 * \param format if GL_COLOR, this is a color attachment point,
447 * if GL_DEPTH, this is a depth component attachment point,
448 * if GL_STENCIL, this is a stencil component attachment point.
451 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
452 struct gl_renderbuffer_attachment
*att
)
454 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
456 /* assume complete */
457 att
->Complete
= GL_TRUE
;
459 /* Look for reasons why the attachment might be incomplete */
460 if (att
->Type
== GL_TEXTURE
) {
461 const struct gl_texture_object
*texObj
= att
->Texture
;
462 struct gl_texture_image
*texImage
;
466 att_incomplete("no texobj");
467 att
->Complete
= GL_FALSE
;
471 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
473 att_incomplete("no teximage");
474 att
->Complete
= GL_FALSE
;
477 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
478 att_incomplete("teximage width/height=0");
479 printf("texobj = %u\n", texObj
->Name
);
480 printf("level = %d\n", att
->TextureLevel
);
481 att
->Complete
= GL_FALSE
;
484 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
485 att_incomplete("bad z offset");
486 att
->Complete
= GL_FALSE
;
490 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
492 if (format
== GL_COLOR
) {
493 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
494 att_incomplete("bad format");
495 att
->Complete
= GL_FALSE
;
498 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
499 att_incomplete("compressed internalformat");
500 att
->Complete
= GL_FALSE
;
504 else if (format
== GL_DEPTH
) {
505 if (baseFormat
== GL_DEPTH_COMPONENT
) {
508 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
509 ctx
->Extensions
.ARB_depth_texture
&&
510 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
514 att
->Complete
= GL_FALSE
;
515 att_incomplete("bad depth format");
520 ASSERT(format
== GL_STENCIL
);
521 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
522 ctx
->Extensions
.ARB_depth_texture
&&
523 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
527 /* no such thing as stencil-only textures */
528 att_incomplete("illegal stencil texture");
529 att
->Complete
= GL_FALSE
;
534 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
535 const GLenum baseFormat
=
536 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
538 ASSERT(att
->Renderbuffer
);
539 if (!att
->Renderbuffer
->InternalFormat
||
540 att
->Renderbuffer
->Width
< 1 ||
541 att
->Renderbuffer
->Height
< 1) {
542 att_incomplete("0x0 renderbuffer");
543 att
->Complete
= GL_FALSE
;
546 if (format
== GL_COLOR
) {
547 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
548 att_incomplete("bad renderbuffer color format");
549 att
->Complete
= GL_FALSE
;
553 else if (format
== GL_DEPTH
) {
554 if (baseFormat
== GL_DEPTH_COMPONENT
) {
557 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
558 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
562 att_incomplete("bad renderbuffer depth format");
563 att
->Complete
= GL_FALSE
;
568 assert(format
== GL_STENCIL
);
569 if (baseFormat
== GL_STENCIL_INDEX
) {
572 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
573 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
577 att
->Complete
= GL_FALSE
;
578 att_incomplete("bad renderbuffer stencil format");
584 ASSERT(att
->Type
== GL_NONE
);
592 * Test if the given framebuffer object is complete and update its
593 * Status field with the results.
594 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
595 * driver to make hardware-specific validation/completeness checks.
596 * Also update the framebuffer's Width and Height fields if the
597 * framebuffer is complete.
600 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
601 struct gl_framebuffer
*fb
)
604 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
605 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
606 GLint numSamples
= -1;
610 assert(fb
->Name
!= 0);
616 /* Start at -2 to more easily loop over all attachment points.
621 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
622 struct gl_renderbuffer_attachment
*att
;
624 gl_format mesaFormat
;
627 * XXX for ARB_fbo, only check color buffers that are named by
628 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
631 /* check for attachment completeness
634 att
= &fb
->Attachment
[BUFFER_DEPTH
];
635 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
636 if (!att
->Complete
) {
637 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
638 fbo_incomplete("depth attachment incomplete", -1);
643 att
= &fb
->Attachment
[BUFFER_STENCIL
];
644 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
645 if (!att
->Complete
) {
646 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
647 fbo_incomplete("stencil attachment incomplete", -1);
652 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
653 test_attachment_completeness(ctx
, GL_COLOR
, att
);
654 if (!att
->Complete
) {
655 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
656 fbo_incomplete("color attachment incomplete", i
);
661 /* get width, height, format of the renderbuffer/texture
663 if (att
->Type
== GL_TEXTURE
) {
664 const struct gl_texture_image
*texImg
665 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
666 minWidth
= MIN2(minWidth
, texImg
->Width
);
667 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
668 minHeight
= MIN2(minHeight
, texImg
->Height
);
669 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
670 f
= texImg
->_BaseFormat
;
671 mesaFormat
= texImg
->TexFormat
;
673 if (!_mesa_is_legal_color_format(ctx
, f
) &&
674 !is_legal_depth_format(ctx
, f
)) {
675 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
676 fbo_incomplete("texture attachment incomplete", -1);
680 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
681 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
682 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
683 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
684 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
685 f
= att
->Renderbuffer
->InternalFormat
;
686 mesaFormat
= att
->Renderbuffer
->Format
;
690 assert(att
->Type
== GL_NONE
);
694 if (numSamples
< 0) {
696 numSamples
= att
->Renderbuffer
->NumSamples
;
699 /* check if integer color */
700 fb
->_IntegerColor
= _mesa_is_format_integer_color(mesaFormat
);
702 /* Error-check width, height, format, samples
704 if (numImages
== 1) {
705 /* save format, num samples */
711 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
712 /* check that width, height, format are same */
713 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
714 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
715 fbo_incomplete("width or height mismatch", -1);
718 /* check that all color buffer have same format */
719 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
720 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
721 fbo_incomplete("format mismatch", -1);
725 if (att
->Renderbuffer
&&
726 att
->Renderbuffer
->NumSamples
!= numSamples
) {
727 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
728 fbo_incomplete("inconsistant number of samples", i
);
736 if (ctx
->API
== API_OPENGL
) {
737 /* Check that all DrawBuffers are present */
738 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
739 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
740 const struct gl_renderbuffer_attachment
*att
741 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
743 if (att
->Type
== GL_NONE
) {
744 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
745 fbo_incomplete("missing drawbuffer", j
);
751 /* Check that the ReadBuffer is present */
752 if (fb
->ColorReadBuffer
!= GL_NONE
) {
753 const struct gl_renderbuffer_attachment
*att
754 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
756 if (att
->Type
== GL_NONE
) {
757 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
758 fbo_incomplete("missing readbuffer", -1);
767 if (numImages
== 0) {
768 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
769 fbo_incomplete("no attachments", -1);
773 /* Provisionally set status = COMPLETE ... */
774 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
776 /* ... but the driver may say the FB is incomplete.
777 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
780 if (ctx
->Driver
.ValidateFramebuffer
) {
781 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
782 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
783 fbo_incomplete("driver marked FBO as incomplete", -1);
787 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
789 * Note that if ARB_framebuffer_object is supported and the attached
790 * renderbuffers/textures are different sizes, the framebuffer
791 * width/height will be set to the smallest width/height.
793 fb
->Width
= minWidth
;
794 fb
->Height
= minHeight
;
796 /* finally, update the visual info for the framebuffer */
797 _mesa_update_framebuffer_visual(ctx
, fb
);
803 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
805 GET_CURRENT_CONTEXT(ctx
);
806 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
808 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
809 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
817 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
819 struct gl_renderbuffer
*newRb
;
820 GET_CURRENT_CONTEXT(ctx
);
822 ASSERT_OUTSIDE_BEGIN_END(ctx
);
824 if (target
!= GL_RENDERBUFFER_EXT
) {
825 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
829 /* No need to flush here since the render buffer binding has no
830 * effect on rendering state.
834 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
835 if (newRb
== &DummyRenderbuffer
) {
836 /* ID was reserved, but no real renderbuffer object made yet */
839 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
840 /* All RB IDs must be Gen'd */
841 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
846 /* create new renderbuffer object */
847 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
849 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
852 ASSERT(newRb
->AllocStorage
);
853 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
854 newRb
->RefCount
= 1; /* referenced by hash table */
861 ASSERT(newRb
!= &DummyRenderbuffer
);
863 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
868 * If the given renderbuffer is anywhere attached to the framebuffer, detach
870 * This is used when a renderbuffer object is deleted.
871 * The spec calls for unbinding.
874 detach_renderbuffer(struct gl_context
*ctx
,
875 struct gl_framebuffer
*fb
,
876 struct gl_renderbuffer
*rb
)
879 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
880 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
881 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
884 invalidate_framebuffer(fb
);
889 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
892 GET_CURRENT_CONTEXT(ctx
);
894 ASSERT_OUTSIDE_BEGIN_END(ctx
);
895 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
897 for (i
= 0; i
< n
; i
++) {
898 if (renderbuffers
[i
] > 0) {
899 struct gl_renderbuffer
*rb
;
900 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
902 /* check if deleting currently bound renderbuffer object */
903 if (rb
== ctx
->CurrentRenderbuffer
) {
905 ASSERT(rb
->RefCount
>= 2);
906 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
909 if (ctx
->DrawBuffer
->Name
) {
910 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
912 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
913 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
916 /* Remove from hash table immediately, to free the ID.
917 * But the object will not be freed until it's no longer
918 * referenced anywhere else.
920 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
922 if (rb
!= &DummyRenderbuffer
) {
923 /* no longer referenced by hash table */
924 _mesa_reference_renderbuffer(&rb
, NULL
);
933 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
935 GET_CURRENT_CONTEXT(ctx
);
939 ASSERT_OUTSIDE_BEGIN_END(ctx
);
942 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
949 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
951 for (i
= 0; i
< n
; i
++) {
952 GLuint name
= first
+ i
;
953 renderbuffers
[i
] = name
;
954 /* insert dummy placeholder into hash table */
955 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
956 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
957 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
963 * Given an internal format token for a renderbuffer, return the
964 * corresponding base format.
967 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
971 switch (internalFormat
) {
972 case GL_RGBA16_SNORM
:
973 /* This is used internally by Mesa for accum buffers. */
975 case GL_STENCIL_INDEX
:
976 case GL_STENCIL_INDEX1_EXT
:
977 case GL_STENCIL_INDEX4_EXT
:
978 case GL_STENCIL_INDEX8_EXT
:
979 case GL_STENCIL_INDEX16_EXT
:
980 /* This is not a valid texture internalFormat, but valid for
983 return GL_STENCIL_INDEX
;
984 case GL_DEPTH_COMPONENT
:
985 case GL_DEPTH_COMPONENT16
:
986 case GL_DEPTH_COMPONENT24
:
987 case GL_DEPTH_COMPONENT32
:
988 /* This is an override of _mesa_base_tex_format's check that
989 * ARB_depth_texture is present. We allow depth RBs without it.
991 return GL_DEPTH_COMPONENT
;
994 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
1002 /** sentinal value, see below */
1003 #define NO_SAMPLES 1000
1007 * Helper function used by _mesa_RenderbufferStorageEXT() and
1008 * _mesa_RenderbufferStorageMultisample().
1009 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
1012 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1013 GLsizei width
, GLsizei height
, GLsizei samples
)
1015 const char *func
= samples
== NO_SAMPLES
?
1016 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
1017 struct gl_renderbuffer
*rb
;
1019 GET_CURRENT_CONTEXT(ctx
);
1021 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1023 if (target
!= GL_RENDERBUFFER_EXT
) {
1024 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1028 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1029 if (baseFormat
== 0) {
1030 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
1034 if (baseFormat
!= GL_DEPTH_COMPONENT
&&
1035 baseFormat
!= GL_STENCIL_INDEX
&&
1036 baseFormat
!= GL_DEPTH_STENCIL
&&
1037 !_mesa_is_legal_color_format(ctx
, baseFormat
)) {
1038 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
1042 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1043 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1047 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1048 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1052 if (samples
== NO_SAMPLES
) {
1053 /* NumSamples == 0 indicates non-multisampling */
1056 else if (samples
> (GLsizei
) ctx
->Const
.MaxSamples
) {
1057 /* note: driver may choose to use more samples than what's requested */
1058 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
1062 rb
= ctx
->CurrentRenderbuffer
;
1064 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1068 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1070 if (rb
->InternalFormat
== internalFormat
&&
1071 rb
->Width
== (GLuint
) width
&&
1072 rb
->Height
== (GLuint
) height
) {
1073 /* no change in allocation needed */
1077 /* These MUST get set by the AllocStorage func */
1078 rb
->Format
= MESA_FORMAT_NONE
;
1079 rb
->NumSamples
= samples
;
1081 /* Now allocate the storage */
1082 ASSERT(rb
->AllocStorage
);
1083 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1084 /* No error - check/set fields now */
1085 assert(rb
->Format
!= MESA_FORMAT_NONE
);
1086 assert(rb
->Width
== (GLuint
) width
);
1087 assert(rb
->Height
== (GLuint
) height
);
1088 rb
->InternalFormat
= internalFormat
;
1089 rb
->_BaseFormat
= baseFormat
;
1090 assert(rb
->_BaseFormat
!= 0);
1093 /* Probably ran out of memory - clear the fields */
1096 rb
->Format
= MESA_FORMAT_NONE
;
1097 rb
->InternalFormat
= GL_NONE
;
1098 rb
->_BaseFormat
= GL_NONE
;
1103 test_framebuffer_completeness(ctx, fb);
1105 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1111 #if FEATURE_OES_EGL_image
1113 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1115 struct gl_renderbuffer
*rb
;
1116 GET_CURRENT_CONTEXT(ctx
);
1117 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1119 if (!ctx
->Extensions
.OES_EGL_image
) {
1120 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1121 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1125 if (target
!= GL_RENDERBUFFER
) {
1126 _mesa_error(ctx
, GL_INVALID_ENUM
,
1127 "EGLImageTargetRenderbufferStorageOES");
1131 rb
= ctx
->CurrentRenderbuffer
;
1133 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1134 "EGLImageTargetRenderbufferStorageOES");
1138 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1140 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1146 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1147 * _mesa_GetFramebufferAttachmentParameterivEXT()
1148 * We have to be careful to respect the base format. For example, if a
1149 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1150 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1151 * we need to return zero.
1154 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1157 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1158 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1159 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
||
1160 baseFormat
== GL_RG
|| baseFormat
== GL_RED
)
1161 return _mesa_get_format_bits(format
, pname
);
1164 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1165 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1166 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
|| baseFormat
== GL_RG
)
1167 return _mesa_get_format_bits(format
, pname
);
1170 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1171 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1172 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
)
1173 return _mesa_get_format_bits(format
, pname
);
1176 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1177 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1178 if (baseFormat
== GL_RGBA
|| baseFormat
== GL_ALPHA
||
1179 baseFormat
== GL_LUMINANCE_ALPHA
)
1180 return _mesa_get_format_bits(format
, pname
);
1183 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1184 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1185 if (baseFormat
== GL_DEPTH_COMPONENT
|| baseFormat
== GL_DEPTH_STENCIL
)
1186 return _mesa_get_format_bits(format
, pname
);
1189 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1190 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1191 if (baseFormat
== GL_STENCIL_INDEX
|| baseFormat
== GL_DEPTH_STENCIL
)
1192 return _mesa_get_format_bits(format
, pname
);
1203 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1204 GLsizei width
, GLsizei height
)
1206 /* GL_ARB_fbo says calling this function is equivalent to calling
1207 * glRenderbufferStorageMultisample() with samples=0. We pass in
1208 * a token value here just for error reporting purposes.
1210 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1215 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1216 GLenum internalFormat
,
1217 GLsizei width
, GLsizei height
)
1219 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1224 * OpenGL ES version of glRenderBufferStorage.
1227 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1228 GLsizei width
, GLsizei height
)
1230 switch (internalFormat
) {
1232 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1233 /* choose a closest format */
1234 internalFormat
= GL_RGB5
;
1240 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1245 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1247 struct gl_renderbuffer
*rb
;
1248 GET_CURRENT_CONTEXT(ctx
);
1250 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1252 if (target
!= GL_RENDERBUFFER_EXT
) {
1253 _mesa_error(ctx
, GL_INVALID_ENUM
,
1254 "glGetRenderbufferParameterivEXT(target)");
1258 rb
= ctx
->CurrentRenderbuffer
;
1260 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1261 "glGetRenderbufferParameterivEXT");
1265 /* No need to flush here since we're just quering state which is
1266 * not effected by rendering.
1270 case GL_RENDERBUFFER_WIDTH_EXT
:
1271 *params
= rb
->Width
;
1273 case GL_RENDERBUFFER_HEIGHT_EXT
:
1274 *params
= rb
->Height
;
1276 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1277 *params
= rb
->InternalFormat
;
1279 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1280 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1281 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1282 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1283 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1284 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1285 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1287 case GL_RENDERBUFFER_SAMPLES
:
1288 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1289 *params
= rb
->NumSamples
;
1294 _mesa_error(ctx
, GL_INVALID_ENUM
,
1295 "glGetRenderbufferParameterivEXT(target)");
1301 GLboolean GLAPIENTRY
1302 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1304 GET_CURRENT_CONTEXT(ctx
);
1305 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1307 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1308 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1316 * Check if any of the attachments of the given framebuffer are textures
1317 * (render to texture). Call ctx->Driver.RenderTexture() for such
1321 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1324 ASSERT(ctx
->Driver
.RenderTexture
);
1327 return; /* can't render to texture with winsys framebuffers */
1329 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1330 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1331 struct gl_texture_object
*texObj
= att
->Texture
;
1333 && texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1334 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1341 * Examine all the framebuffer's attachments to see if any are textures.
1342 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1343 * notify the device driver that the texture image may have changed.
1346 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1349 return; /* can't render to texture with winsys framebuffers */
1351 if (ctx
->Driver
.FinishRenderTexture
) {
1353 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1354 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1355 if (att
->Texture
&& att
->Renderbuffer
) {
1356 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1364 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1366 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1367 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1368 GLboolean bindReadBuf
, bindDrawBuf
;
1369 GET_CURRENT_CONTEXT(ctx
);
1372 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1373 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1374 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1378 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1380 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1381 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1382 "glBindFramebufferEXT(unsupported)");
1387 #if FEATURE_EXT_framebuffer_blit
1388 case GL_DRAW_FRAMEBUFFER_EXT
:
1389 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1390 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1393 bindDrawBuf
= GL_TRUE
;
1394 bindReadBuf
= GL_FALSE
;
1396 case GL_READ_FRAMEBUFFER_EXT
:
1397 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1398 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1401 bindDrawBuf
= GL_FALSE
;
1402 bindReadBuf
= GL_TRUE
;
1405 case GL_FRAMEBUFFER_EXT
:
1406 bindDrawBuf
= GL_TRUE
;
1407 bindReadBuf
= GL_TRUE
;
1410 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1415 /* Binding a user-created framebuffer object */
1416 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1417 if (newDrawFb
== &DummyFramebuffer
) {
1418 /* ID was reserved, but no real framebuffer object made yet */
1421 else if (!newDrawFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1422 /* All FBO IDs must be Gen'd */
1423 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1428 /* create new framebuffer object */
1429 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1431 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1434 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1436 newReadFb
= newDrawFb
;
1439 /* Binding the window system framebuffer (which was originally set
1440 * with MakeCurrent).
1442 newDrawFb
= ctx
->WinSysDrawBuffer
;
1443 newReadFb
= ctx
->WinSysReadBuffer
;
1447 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1449 /* save pointers to current/old framebuffers */
1450 oldDrawFb
= ctx
->DrawBuffer
;
1451 oldReadFb
= ctx
->ReadBuffer
;
1453 /* check if really changing bindings */
1454 if (oldDrawFb
== newDrawFb
)
1455 bindDrawBuf
= GL_FALSE
;
1456 if (oldReadFb
== newReadFb
)
1457 bindReadBuf
= GL_FALSE
;
1460 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1462 * We also check if we're beginning and/or ending render-to-texture.
1463 * When a framebuffer with texture attachments is unbound, call
1464 * ctx->Driver.FinishRenderTexture().
1465 * When a framebuffer with texture attachments is bound, call
1466 * ctx->Driver.RenderTexture().
1468 * Note that if the ReadBuffer has texture attachments we don't consider
1469 * that a render-to-texture case.
1472 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1474 /* check if old readbuffer was render-to-texture */
1475 check_end_texture_render(ctx
, oldReadFb
);
1477 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1481 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1483 /* check if old read/draw buffers were render-to-texture */
1485 check_end_texture_render(ctx
, oldReadFb
);
1487 if (oldDrawFb
!= oldReadFb
)
1488 check_end_texture_render(ctx
, oldDrawFb
);
1490 /* check if newly bound framebuffer has any texture attachments */
1491 check_begin_texture_render(ctx
, newDrawFb
);
1493 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1496 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1497 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1503 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1506 GET_CURRENT_CONTEXT(ctx
);
1508 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1509 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1511 for (i
= 0; i
< n
; i
++) {
1512 if (framebuffers
[i
] > 0) {
1513 struct gl_framebuffer
*fb
;
1514 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1516 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1518 /* check if deleting currently bound framebuffer object */
1519 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1520 /* separate draw/read binding points */
1521 if (fb
== ctx
->DrawBuffer
) {
1523 ASSERT(fb
->RefCount
>= 2);
1524 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1526 if (fb
== ctx
->ReadBuffer
) {
1528 ASSERT(fb
->RefCount
>= 2);
1529 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1533 /* only one binding point for read/draw buffers */
1534 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1536 ASSERT(fb
->RefCount
>= 2);
1537 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
1541 /* remove from hash table immediately, to free the ID */
1542 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1544 if (fb
!= &DummyFramebuffer
) {
1545 /* But the object will not be freed until it's no longer
1546 * bound in any context.
1548 _mesa_reference_framebuffer(&fb
, NULL
);
1557 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1559 GET_CURRENT_CONTEXT(ctx
);
1563 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1566 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1573 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1575 for (i
= 0; i
< n
; i
++) {
1576 GLuint name
= first
+ i
;
1577 framebuffers
[i
] = name
;
1578 /* insert dummy placeholder into hash table */
1579 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1580 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1581 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1588 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1590 struct gl_framebuffer
*buffer
;
1591 GET_CURRENT_CONTEXT(ctx
);
1593 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1596 #if FEATURE_EXT_framebuffer_blit
1597 case GL_DRAW_FRAMEBUFFER_EXT
:
1598 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1599 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1602 buffer
= ctx
->DrawBuffer
;
1604 case GL_READ_FRAMEBUFFER_EXT
:
1605 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1606 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1609 buffer
= ctx
->ReadBuffer
;
1612 case GL_FRAMEBUFFER_EXT
:
1613 buffer
= ctx
->DrawBuffer
;
1616 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1617 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1620 if (buffer
->Name
== 0) {
1621 /* The window system / default framebuffer is always complete */
1622 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1625 /* No need to flush here */
1627 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1628 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1631 return buffer
->_Status
;
1637 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1640 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
1641 GLenum attachment
, GLenum textarget
, GLuint texture
,
1642 GLint level
, GLint zoffset
)
1644 struct gl_renderbuffer_attachment
*att
;
1645 struct gl_texture_object
*texObj
= NULL
;
1646 struct gl_framebuffer
*fb
;
1647 GLboolean error
= GL_FALSE
;
1649 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1652 case GL_READ_FRAMEBUFFER_EXT
:
1653 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1654 fb
= ctx
->ReadBuffer
;
1656 case GL_DRAW_FRAMEBUFFER_EXT
:
1657 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1659 case GL_FRAMEBUFFER_EXT
:
1660 fb
= ctx
->DrawBuffer
;
1667 _mesa_error(ctx
, GL_INVALID_ENUM
,
1668 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1674 /* check framebuffer binding */
1675 if (fb
->Name
== 0) {
1676 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1677 "glFramebufferTexture%sEXT", caller
);
1682 /* The textarget, level, and zoffset parameters are only validated if
1683 * texture is non-zero.
1686 GLboolean err
= GL_TRUE
;
1688 texObj
= _mesa_lookup_texture(ctx
, texture
);
1689 if (texObj
!= NULL
) {
1690 if (textarget
== 0) {
1691 /* XXX what's the purpose of this? */
1692 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1693 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1694 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1697 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1698 ? !IS_CUBE_FACE(textarget
)
1699 : (texObj
->Target
!= textarget
);
1703 /* can't render to a non-existant texture */
1704 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1705 "glFramebufferTexture%sEXT(non existant texture)",
1711 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1712 "glFramebufferTexture%sEXT(texture target mismatch)",
1717 if (texObj
->Target
== GL_TEXTURE_3D
) {
1718 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1719 if (zoffset
< 0 || zoffset
>= maxSize
) {
1720 _mesa_error(ctx
, GL_INVALID_VALUE
,
1721 "glFramebufferTexture%sEXT(zoffset)", caller
);
1725 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1726 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1727 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1728 _mesa_error(ctx
, GL_INVALID_VALUE
,
1729 "glFramebufferTexture%sEXT(layer)", caller
);
1735 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1736 _mesa_error(ctx
, GL_INVALID_VALUE
,
1737 "glFramebufferTexture%sEXT(level)", caller
);
1742 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1744 _mesa_error(ctx
, GL_INVALID_ENUM
,
1745 "glFramebufferTexture%sEXT(attachment)", caller
);
1749 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1751 _glthread_LOCK_MUTEX(fb
->Mutex
);
1753 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1755 /* Set the render-to-texture flag. We'll check this flag in
1756 * glTexImage() and friends to determine if we need to revalidate
1757 * any FBOs that might be rendering into this texture.
1758 * This flag never gets cleared since it's non-trivial to determine
1759 * when all FBOs might be done rendering to this texture. That's OK
1760 * though since it's uncommon to render to a texture then repeatedly
1761 * call glTexImage() to change images in the texture.
1763 texObj
->_RenderToTexture
= GL_TRUE
;
1766 _mesa_remove_attachment(ctx
, att
);
1769 invalidate_framebuffer(fb
);
1771 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1777 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1778 GLenum textarget
, GLuint texture
, GLint level
)
1780 GET_CURRENT_CONTEXT(ctx
);
1782 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1783 _mesa_error(ctx
, GL_INVALID_ENUM
,
1784 "glFramebufferTexture1DEXT(textarget)");
1788 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1794 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1795 GLenum textarget
, GLuint texture
, GLint level
)
1797 GET_CURRENT_CONTEXT(ctx
);
1799 if ((texture
!= 0) &&
1800 (textarget
!= GL_TEXTURE_2D
) &&
1801 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1802 (!IS_CUBE_FACE(textarget
))) {
1803 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1804 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1808 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1814 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1815 GLenum textarget
, GLuint texture
,
1816 GLint level
, GLint zoffset
)
1818 GET_CURRENT_CONTEXT(ctx
);
1820 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1821 _mesa_error(ctx
, GL_INVALID_ENUM
,
1822 "glFramebufferTexture3DEXT(textarget)");
1826 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1832 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1833 GLuint texture
, GLint level
, GLint layer
)
1835 GET_CURRENT_CONTEXT(ctx
);
1837 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1843 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1844 GLenum renderbufferTarget
,
1845 GLuint renderbuffer
)
1847 struct gl_renderbuffer_attachment
*att
;
1848 struct gl_framebuffer
*fb
;
1849 struct gl_renderbuffer
*rb
;
1850 GET_CURRENT_CONTEXT(ctx
);
1852 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1855 #if FEATURE_EXT_framebuffer_blit
1856 case GL_DRAW_FRAMEBUFFER_EXT
:
1857 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1858 _mesa_error(ctx
, GL_INVALID_ENUM
,
1859 "glFramebufferRenderbufferEXT(target)");
1862 fb
= ctx
->DrawBuffer
;
1864 case GL_READ_FRAMEBUFFER_EXT
:
1865 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1866 _mesa_error(ctx
, GL_INVALID_ENUM
,
1867 "glFramebufferRenderbufferEXT(target)");
1870 fb
= ctx
->ReadBuffer
;
1873 case GL_FRAMEBUFFER_EXT
:
1874 fb
= ctx
->DrawBuffer
;
1877 _mesa_error(ctx
, GL_INVALID_ENUM
,
1878 "glFramebufferRenderbufferEXT(target)");
1882 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1883 _mesa_error(ctx
, GL_INVALID_ENUM
,
1884 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1888 if (fb
->Name
== 0) {
1889 /* Can't attach new renderbuffers to a window system framebuffer */
1890 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1894 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1896 _mesa_error(ctx
, GL_INVALID_ENUM
,
1897 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1898 _mesa_lookup_enum_by_nr(attachment
));
1903 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1905 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1906 "glFramebufferRenderbufferEXT(non-existant"
1907 " renderbuffer %u)", renderbuffer
);
1910 else if (rb
== &DummyRenderbuffer
) {
1911 /* This is what NVIDIA does */
1912 _mesa_error(ctx
, GL_INVALID_VALUE
,
1913 "glFramebufferRenderbufferEXT(renderbuffer %u)",
1919 /* remove renderbuffer attachment */
1923 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
1924 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
1925 /* make sure the renderbuffer is a depth/stencil format */
1926 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
1927 if (baseFormat
!= GL_DEPTH_STENCIL
) {
1928 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1929 "glFramebufferRenderbufferEXT(renderbuffer"
1930 " is not DEPTH_STENCIL format)");
1936 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1938 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1939 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1941 /* Some subsequent GL commands may depend on the framebuffer's visual
1942 * after the binding is updated. Update visual info now.
1944 _mesa_update_framebuffer_visual(ctx
, fb
);
1949 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1950 GLenum pname
, GLint
*params
)
1952 const struct gl_renderbuffer_attachment
*att
;
1953 struct gl_framebuffer
*buffer
;
1954 GET_CURRENT_CONTEXT(ctx
);
1956 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1959 #if FEATURE_EXT_framebuffer_blit
1960 case GL_DRAW_FRAMEBUFFER_EXT
:
1961 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1962 _mesa_error(ctx
, GL_INVALID_ENUM
,
1963 "glGetFramebufferAttachmentParameterivEXT(target)");
1966 buffer
= ctx
->DrawBuffer
;
1968 case GL_READ_FRAMEBUFFER_EXT
:
1969 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1970 _mesa_error(ctx
, GL_INVALID_ENUM
,
1971 "glGetFramebufferAttachmentParameterivEXT(target)");
1974 buffer
= ctx
->ReadBuffer
;
1977 case GL_FRAMEBUFFER_EXT
:
1978 buffer
= ctx
->DrawBuffer
;
1981 _mesa_error(ctx
, GL_INVALID_ENUM
,
1982 "glGetFramebufferAttachmentParameterivEXT(target)");
1986 if (buffer
->Name
== 0) {
1987 /* the default / window-system FBO */
1988 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
1991 /* user-created framebuffer FBO */
1992 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1996 _mesa_error(ctx
, GL_INVALID_ENUM
,
1997 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2001 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2002 /* the depth and stencil attachments must point to the same buffer */
2003 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2004 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2005 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2006 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2007 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2008 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2009 " attachments differ)");
2014 /* No need to flush here */
2017 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2018 *params
= buffer
->Name
== 0 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2020 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2021 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2022 *params
= att
->Renderbuffer
->Name
;
2024 else if (att
->Type
== GL_TEXTURE
) {
2025 *params
= att
->Texture
->Name
;
2028 assert(att
->Type
== GL_NONE
);
2032 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2033 if (att
->Type
== GL_TEXTURE
) {
2034 *params
= att
->TextureLevel
;
2037 _mesa_error(ctx
, GL_INVALID_ENUM
,
2038 "glGetFramebufferAttachmentParameterivEXT(pname)");
2041 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2042 if (att
->Type
== GL_TEXTURE
) {
2043 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2044 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2051 _mesa_error(ctx
, GL_INVALID_ENUM
,
2052 "glGetFramebufferAttachmentParameterivEXT(pname)");
2055 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2056 if (att
->Type
== GL_TEXTURE
) {
2057 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2058 *params
= att
->Zoffset
;
2065 _mesa_error(ctx
, GL_INVALID_ENUM
,
2066 "glGetFramebufferAttachmentParameterivEXT(pname)");
2069 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2070 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2071 _mesa_error(ctx
, GL_INVALID_ENUM
,
2072 "glGetFramebufferAttachmentParameterivEXT(pname)");
2075 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2076 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2079 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2080 * if the sRGB conversion is unsupported. */
2081 *params
= GL_LINEAR
;
2085 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2086 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2087 _mesa_error(ctx
, GL_INVALID_ENUM
,
2088 "glGetFramebufferAttachmentParameterivEXT(pname)");
2092 gl_format format
= att
->Renderbuffer
->Format
;
2093 if (format
== MESA_FORMAT_CI8
|| format
== MESA_FORMAT_S8
) {
2098 *params
= _mesa_get_format_datatype(format
);
2102 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2103 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2104 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2105 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2106 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2107 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2108 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2109 _mesa_error(ctx
, GL_INVALID_ENUM
,
2110 "glGetFramebufferAttachmentParameterivEXT(pname)");
2112 else if (att
->Texture
) {
2113 const struct gl_texture_image
*texImage
=
2114 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2117 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2118 texImage
->TexFormat
);
2124 else if (att
->Renderbuffer
) {
2125 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2126 att
->Renderbuffer
->Format
);
2133 _mesa_error(ctx
, GL_INVALID_ENUM
,
2134 "glGetFramebufferAttachmentParameterivEXT(pname)");
2141 _mesa_GenerateMipmapEXT(GLenum target
)
2143 struct gl_texture_object
*texObj
;
2144 GET_CURRENT_CONTEXT(ctx
);
2146 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2147 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2153 case GL_TEXTURE_CUBE_MAP
:
2154 /* OK, legal value */
2157 /* XXX need to implement GL_TEXTURE_1D_ARRAY and GL_TEXTURE_2D_ARRAY */
2158 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
2162 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2164 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2169 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2170 !_mesa_cube_complete(texObj
)) {
2171 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2172 "glGenerateMipmap(incomplete cube map)");
2176 _mesa_lock_texture(ctx
, texObj
);
2177 if (target
== GL_TEXTURE_CUBE_MAP
) {
2179 for (face
= 0; face
< 6; face
++)
2180 ctx
->Driver
.GenerateMipmap(ctx
,
2181 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2185 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2187 _mesa_unlock_texture(ctx
, texObj
);
2191 #if FEATURE_EXT_framebuffer_blit
2193 static const struct gl_renderbuffer_attachment
*
2194 find_attachment(const struct gl_framebuffer
*fb
, const struct gl_renderbuffer
*rb
)
2197 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2198 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2199 return &fb
->Attachment
[i
];
2207 * Blit rectangular region, optionally from one framebuffer to another.
2209 * Note, if the src buffer is multisampled and the dest is not, this is
2210 * when the samples must be resolved to a single color.
2213 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2214 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2215 GLbitfield mask
, GLenum filter
)
2217 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2218 GL_DEPTH_BUFFER_BIT
|
2219 GL_STENCIL_BUFFER_BIT
);
2220 const struct gl_framebuffer
*readFb
, *drawFb
;
2221 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
2222 GET_CURRENT_CONTEXT(ctx
);
2224 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2225 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2227 if (ctx
->NewState
) {
2228 _mesa_update_state(ctx
);
2231 readFb
= ctx
->ReadBuffer
;
2232 drawFb
= ctx
->DrawBuffer
;
2234 if (!readFb
|| !drawFb
) {
2235 /* This will normally never happen but someday we may want to
2236 * support MakeCurrent() with no drawables.
2241 /* check for complete framebuffers */
2242 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2243 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2244 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2245 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2249 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2250 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2254 if (mask
& ~legalMaskBits
) {
2255 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2259 /* depth/stencil must be blitted with nearest filtering */
2260 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2261 && filter
!= GL_NEAREST
) {
2262 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2263 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2267 /* get color read/draw renderbuffers */
2268 if (mask
& GL_COLOR_BUFFER_BIT
) {
2269 colorReadRb
= readFb
->_ColorReadBuffer
;
2270 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2273 colorReadRb
= colorDrawRb
= NULL
;
2276 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2277 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2278 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2281 _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2282 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2283 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2284 "glBlitFramebufferEXT(stencil buffer size mismatch");
2289 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2290 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2291 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2294 _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2295 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) {
2296 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2297 "glBlitFramebufferEXT(depth buffer size mismatch");
2302 if (readFb
->Visual
.samples
> 0 &&
2303 drawFb
->Visual
.samples
> 0 &&
2304 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2305 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2306 "glBlitFramebufferEXT(mismatched samples");
2310 /* extra checks for multisample copies... */
2311 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2312 /* src and dest region sizes must be the same */
2313 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2314 srcY1
- srcY0
!= dstY1
- dstY0
) {
2315 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2316 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2320 /* color formats must match */
2323 colorReadRb
->Format
!= colorDrawRb
->Format
) {
2324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2325 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2330 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2331 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2337 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2339 srcX0
, srcY0
, srcX1
, srcY1
,
2340 dstX0
, dstY0
, dstX1
, dstY1
,
2343 const struct gl_renderbuffer_attachment
*att
;
2345 att
= find_attachment(readFb
, colorReadRb
);
2346 printf(" Src FBO %u RB %u (%dx%d) ",
2347 readFb
->Name
, colorReadRb
->Name
,
2348 colorReadRb
->Width
, colorReadRb
->Height
);
2349 if (att
&& att
->Texture
) {
2350 printf("Tex %u tgt 0x%x level %u face %u",
2352 att
->Texture
->Target
,
2358 att
= find_attachment(drawFb
, colorDrawRb
);
2359 printf(" Dst FBO %u RB %u (%dx%d) ",
2360 drawFb
->Name
, colorDrawRb
->Name
,
2361 colorDrawRb
->Width
, colorDrawRb
->Height
);
2362 if (att
&& att
->Texture
) {
2363 printf("Tex %u tgt 0x%x level %u face %u",
2365 att
->Texture
->Target
,
2373 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2374 ctx
->Driver
.BlitFramebuffer(ctx
,
2375 srcX0
, srcY0
, srcX1
, srcY1
,
2376 dstX0
, dstY0
, dstX1
, dstY1
,
2379 #endif /* FEATURE_EXT_framebuffer_blit */
2381 #if FEATURE_ARB_geometry_shader4
2383 _mesa_FramebufferTextureARB(GLenum target
, GLenum attachment
,
2384 GLuint texture
, GLint level
)
2386 GET_CURRENT_CONTEXT(ctx
);
2387 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2388 "glFramebufferTextureARB "
2389 "not implemented!");
2393 _mesa_FramebufferTextureFaceARB(GLenum target
, GLenum attachment
,
2394 GLuint texture
, GLint level
, GLenum face
)
2396 GET_CURRENT_CONTEXT(ctx
);
2397 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2398 "glFramebufferTextureFaceARB "
2399 "not implemented!");
2401 #endif /* FEATURE_ARB_geometry_shader4 */