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 static inline GLboolean
82 is_cube_face(GLenum target
)
84 return (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
85 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
);
90 * Is the given FBO a user-created FBO?
92 static inline GLboolean
93 is_user_fbo(const struct gl_framebuffer
*fb
)
100 * Is the given FBO a window system FBO (like an X window)?
102 static inline GLboolean
103 is_winsys_fbo(const struct gl_framebuffer
*fb
)
105 return fb
->Name
== 0;
110 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
116 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
123 _mesa_init_fbobjects(struct gl_context
*ctx
)
125 _glthread_INIT_MUTEX(DummyFramebuffer
.Mutex
);
126 _glthread_INIT_MUTEX(DummyRenderbuffer
.Mutex
);
127 _glthread_INIT_MUTEX(IncompleteFramebuffer
.Mutex
);
128 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
129 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
130 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
133 struct gl_framebuffer
*
134 _mesa_get_incomplete_framebuffer(void)
136 return &IncompleteFramebuffer
;
140 * Helper routine for getting a gl_renderbuffer.
142 struct gl_renderbuffer
*
143 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
145 struct gl_renderbuffer
*rb
;
150 rb
= (struct gl_renderbuffer
*)
151 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
157 * Helper routine for getting a gl_framebuffer.
159 struct gl_framebuffer
*
160 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
162 struct gl_framebuffer
*fb
;
167 fb
= (struct gl_framebuffer
*)
168 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
174 * Mark the given framebuffer as invalid. This will force the
175 * test for framebuffer completeness to be done before the framebuffer
179 invalidate_framebuffer(struct gl_framebuffer
*fb
)
181 fb
->_Status
= 0; /* "indeterminate" */
186 * Return the gl_framebuffer object which corresponds to the given
187 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
188 * Check support for GL_EXT_framebuffer_blit to determine if certain
190 * \return gl_framebuffer pointer or NULL if target is illegal
192 static struct gl_framebuffer
*
193 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
196 case GL_DRAW_FRAMEBUFFER
:
197 return ctx
->Extensions
.EXT_framebuffer_blit
&& ctx
->API
== API_OPENGL
198 ? ctx
->DrawBuffer
: NULL
;
199 case GL_READ_FRAMEBUFFER
:
200 return ctx
->Extensions
.EXT_framebuffer_blit
&& ctx
->API
== API_OPENGL
201 ? ctx
->ReadBuffer
: NULL
;
202 case GL_FRAMEBUFFER_EXT
:
203 return ctx
->DrawBuffer
;
211 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
212 * gl_renderbuffer_attachment object.
213 * This function is only used for user-created FB objects, not the
214 * default / window-system FB object.
215 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
216 * the depth buffer attachment point.
218 struct gl_renderbuffer_attachment
*
219 _mesa_get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
224 assert(is_user_fbo(fb
));
226 switch (attachment
) {
227 case GL_COLOR_ATTACHMENT0_EXT
:
228 case GL_COLOR_ATTACHMENT1_EXT
:
229 case GL_COLOR_ATTACHMENT2_EXT
:
230 case GL_COLOR_ATTACHMENT3_EXT
:
231 case GL_COLOR_ATTACHMENT4_EXT
:
232 case GL_COLOR_ATTACHMENT5_EXT
:
233 case GL_COLOR_ATTACHMENT6_EXT
:
234 case GL_COLOR_ATTACHMENT7_EXT
:
235 case GL_COLOR_ATTACHMENT8_EXT
:
236 case GL_COLOR_ATTACHMENT9_EXT
:
237 case GL_COLOR_ATTACHMENT10_EXT
:
238 case GL_COLOR_ATTACHMENT11_EXT
:
239 case GL_COLOR_ATTACHMENT12_EXT
:
240 case GL_COLOR_ATTACHMENT13_EXT
:
241 case GL_COLOR_ATTACHMENT14_EXT
:
242 case GL_COLOR_ATTACHMENT15_EXT
:
243 /* Only OpenGL ES 1.x forbids color attachments other than
244 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
247 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
248 if (i
>= ctx
->Const
.MaxColorAttachments
249 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
252 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
253 case GL_DEPTH_STENCIL_ATTACHMENT
:
254 if (ctx
->API
!= API_OPENGL
)
257 case GL_DEPTH_ATTACHMENT_EXT
:
258 return &fb
->Attachment
[BUFFER_DEPTH
];
259 case GL_STENCIL_ATTACHMENT_EXT
:
260 return &fb
->Attachment
[BUFFER_STENCIL
];
268 * As above, but only used for getting attachments of the default /
269 * window-system framebuffer (not user-created framebuffer objects).
271 static struct gl_renderbuffer_attachment
*
272 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
275 assert(is_winsys_fbo(fb
));
277 switch (attachment
) {
279 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
281 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
283 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
285 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
287 if (fb
->Visual
.numAuxBuffers
== 1) {
288 return &fb
->Attachment
[BUFFER_AUX0
];
292 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
294 * "If the default framebuffer is bound to target, then attachment must
295 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
296 * identifying a color buffer; DEPTH, identifying the depth buffer; or
297 * STENCIL, identifying the stencil buffer."
299 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
300 * language. However, revision #33 of the ARB_framebuffer_object spec
303 * "If the default framebuffer is bound to <target>, then <attachment>
304 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
305 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
306 * depth buffer, or the stencil buffer, and <pname> may be
307 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
308 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
310 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
311 * from glext.h, so shipping apps should not use those values.
313 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
314 * support queries of the window system FBO.
317 return &fb
->Attachment
[BUFFER_DEPTH
];
319 return &fb
->Attachment
[BUFFER_STENCIL
];
328 * Remove any texture or renderbuffer attached to the given attachment
329 * point. Update reference counts, etc.
332 _mesa_remove_attachment(struct gl_context
*ctx
,
333 struct gl_renderbuffer_attachment
*att
)
335 if (att
->Type
== GL_TEXTURE
) {
336 ASSERT(att
->Texture
);
337 if (ctx
->Driver
.FinishRenderTexture
) {
338 /* tell driver that we're done rendering to this texture. */
339 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
341 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
342 ASSERT(!att
->Texture
);
344 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
345 ASSERT(!att
->Texture
);
346 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
347 ASSERT(!att
->Renderbuffer
);
350 att
->Complete
= GL_TRUE
;
355 * Bind a texture object to an attachment point.
356 * The previous binding, if any, will be removed first.
359 _mesa_set_texture_attachment(struct gl_context
*ctx
,
360 struct gl_framebuffer
*fb
,
361 struct gl_renderbuffer_attachment
*att
,
362 struct gl_texture_object
*texObj
,
363 GLenum texTarget
, GLuint level
, GLuint zoffset
)
365 if (att
->Texture
== texObj
) {
366 /* re-attaching same texture */
367 ASSERT(att
->Type
== GL_TEXTURE
);
368 if (ctx
->Driver
.FinishRenderTexture
)
369 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
373 if (ctx
->Driver
.FinishRenderTexture
&& att
->Texture
)
374 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
375 _mesa_remove_attachment(ctx
, att
);
376 att
->Type
= GL_TEXTURE
;
377 assert(!att
->Texture
);
378 _mesa_reference_texobj(&att
->Texture
, texObj
);
381 /* always update these fields */
382 att
->TextureLevel
= level
;
383 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
384 att
->Zoffset
= zoffset
;
385 att
->Complete
= GL_FALSE
;
387 if (_mesa_get_attachment_teximage(att
)) {
388 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
391 invalidate_framebuffer(fb
);
396 * Bind a renderbuffer to an attachment point.
397 * The previous binding, if any, will be removed first.
400 _mesa_set_renderbuffer_attachment(struct gl_context
*ctx
,
401 struct gl_renderbuffer_attachment
*att
,
402 struct gl_renderbuffer
*rb
)
404 /* XXX check if re-doing same attachment, exit early */
405 _mesa_remove_attachment(ctx
, att
);
406 att
->Type
= GL_RENDERBUFFER_EXT
;
407 att
->Texture
= NULL
; /* just to be safe */
408 att
->Complete
= GL_FALSE
;
409 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
414 * Fallback for ctx->Driver.FramebufferRenderbuffer()
415 * Attach a renderbuffer object to a framebuffer object.
418 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
419 struct gl_framebuffer
*fb
,
420 GLenum attachment
, struct gl_renderbuffer
*rb
)
422 struct gl_renderbuffer_attachment
*att
;
424 _glthread_LOCK_MUTEX(fb
->Mutex
);
426 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
429 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
430 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
431 /* do stencil attachment here (depth already done above) */
432 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
434 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
436 rb
->AttachedAnytime
= GL_TRUE
;
439 _mesa_remove_attachment(ctx
, att
);
442 invalidate_framebuffer(fb
);
444 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
449 * Fallback for ctx->Driver.ValidateFramebuffer()
450 * Check if the renderbuffer's formats are supported by the software
452 * Drivers should probably override this.
455 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
458 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
459 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
461 switch (rb
->_BaseFormat
) {
463 case GL_LUMINANCE_ALPHA
:
468 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
472 switch (rb
->Format
) {
473 /* XXX This list is likely incomplete. */
474 case MESA_FORMAT_RGB9_E5_FLOAT
:
475 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
478 /* render buffer format is supported by software rendering */
490 att_incomplete(const char *msg
)
493 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
504 fbo_incomplete(const char *msg
, int index
)
507 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
516 * Is the given base format a legal format for a color renderbuffer?
519 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
521 switch (baseFormat
) {
526 case GL_LUMINANCE_ALPHA
:
529 return ctx
->Extensions
.ARB_framebuffer_object
;
532 return ctx
->Extensions
.ARB_texture_rg
;
540 * Is the given base format a legal format for a depth/stencil renderbuffer?
543 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
545 switch (baseFormat
) {
546 case GL_DEPTH_COMPONENT
:
547 case GL_DEPTH_STENCIL_EXT
:
556 * Test if an attachment point is complete and update its Complete field.
557 * \param format if GL_COLOR, this is a color attachment point,
558 * if GL_DEPTH, this is a depth component attachment point,
559 * if GL_STENCIL, this is a stencil component attachment point.
562 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
563 struct gl_renderbuffer_attachment
*att
)
565 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
567 /* assume complete */
568 att
->Complete
= GL_TRUE
;
570 /* Look for reasons why the attachment might be incomplete */
571 if (att
->Type
== GL_TEXTURE
) {
572 const struct gl_texture_object
*texObj
= att
->Texture
;
573 struct gl_texture_image
*texImage
;
577 att_incomplete("no texobj");
578 att
->Complete
= GL_FALSE
;
582 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
584 att_incomplete("no teximage");
585 att
->Complete
= GL_FALSE
;
588 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
589 att_incomplete("teximage width/height=0");
590 printf("texobj = %u\n", texObj
->Name
);
591 printf("level = %d\n", att
->TextureLevel
);
592 att
->Complete
= GL_FALSE
;
595 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
596 att_incomplete("bad z offset");
597 att
->Complete
= GL_FALSE
;
601 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
603 if (format
== GL_COLOR
) {
604 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
605 att_incomplete("bad format");
606 att
->Complete
= GL_FALSE
;
609 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
610 att_incomplete("compressed internalformat");
611 att
->Complete
= GL_FALSE
;
615 else if (format
== GL_DEPTH
) {
616 if (baseFormat
== GL_DEPTH_COMPONENT
) {
619 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
620 ctx
->Extensions
.ARB_depth_texture
&&
621 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
625 att
->Complete
= GL_FALSE
;
626 att_incomplete("bad depth format");
631 ASSERT(format
== GL_STENCIL
);
632 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
633 ctx
->Extensions
.ARB_depth_texture
&&
634 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
638 /* no such thing as stencil-only textures */
639 att_incomplete("illegal stencil texture");
640 att
->Complete
= GL_FALSE
;
645 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
646 const GLenum baseFormat
=
647 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
649 ASSERT(att
->Renderbuffer
);
650 if (!att
->Renderbuffer
->InternalFormat
||
651 att
->Renderbuffer
->Width
< 1 ||
652 att
->Renderbuffer
->Height
< 1) {
653 att_incomplete("0x0 renderbuffer");
654 att
->Complete
= GL_FALSE
;
657 if (format
== GL_COLOR
) {
658 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
659 att_incomplete("bad renderbuffer color format");
660 att
->Complete
= GL_FALSE
;
664 else if (format
== GL_DEPTH
) {
665 if (baseFormat
== GL_DEPTH_COMPONENT
) {
668 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
669 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
673 att_incomplete("bad renderbuffer depth format");
674 att
->Complete
= GL_FALSE
;
679 assert(format
== GL_STENCIL
);
680 if (baseFormat
== GL_STENCIL_INDEX
) {
683 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
684 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
688 att
->Complete
= GL_FALSE
;
689 att_incomplete("bad renderbuffer stencil format");
695 ASSERT(att
->Type
== GL_NONE
);
703 * Test if the given framebuffer object is complete and update its
704 * Status field with the results.
705 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
706 * driver to make hardware-specific validation/completeness checks.
707 * Also update the framebuffer's Width and Height fields if the
708 * framebuffer is complete.
711 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
712 struct gl_framebuffer
*fb
)
715 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
716 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
717 GLint numSamples
= -1;
721 assert(is_user_fbo(fb
));
727 /* Start at -2 to more easily loop over all attachment points.
732 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
733 struct gl_renderbuffer_attachment
*att
;
738 * XXX for ARB_fbo, only check color buffers that are named by
739 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
742 /* check for attachment completeness
745 att
= &fb
->Attachment
[BUFFER_DEPTH
];
746 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
747 if (!att
->Complete
) {
748 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
749 fbo_incomplete("depth attachment incomplete", -1);
754 att
= &fb
->Attachment
[BUFFER_STENCIL
];
755 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
756 if (!att
->Complete
) {
757 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
758 fbo_incomplete("stencil attachment incomplete", -1);
763 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
764 test_attachment_completeness(ctx
, GL_COLOR
, att
);
765 if (!att
->Complete
) {
766 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
767 fbo_incomplete("color attachment incomplete", i
);
772 /* get width, height, format of the renderbuffer/texture
774 if (att
->Type
== GL_TEXTURE
) {
775 const struct gl_texture_image
*texImg
=
776 _mesa_get_attachment_teximage(att
);
777 minWidth
= MIN2(minWidth
, texImg
->Width
);
778 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
779 minHeight
= MIN2(minHeight
, texImg
->Height
);
780 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
781 f
= texImg
->_BaseFormat
;
782 attFormat
= texImg
->TexFormat
;
784 if (!_mesa_is_legal_color_format(ctx
, f
) &&
785 !is_legal_depth_format(ctx
, f
)) {
786 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
787 fbo_incomplete("texture attachment incomplete", -1);
791 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
792 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
793 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
794 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
795 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
796 f
= att
->Renderbuffer
->InternalFormat
;
797 attFormat
= att
->Renderbuffer
->Format
;
801 assert(att
->Type
== GL_NONE
);
805 if (att
->Renderbuffer
&& numSamples
< 0) {
807 numSamples
= att
->Renderbuffer
->NumSamples
;
810 /* check if integer color */
811 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
813 /* Error-check width, height, format, samples
815 if (numImages
== 1) {
816 /* save format, num samples */
822 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
823 /* check that width, height, format are same */
824 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
825 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
826 fbo_incomplete("width or height mismatch", -1);
829 /* check that all color buffer have same format */
830 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
831 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
832 fbo_incomplete("format mismatch", -1);
836 if (att
->Renderbuffer
&&
837 att
->Renderbuffer
->NumSamples
!= numSamples
) {
838 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
839 fbo_incomplete("inconsistant number of samples", i
);
847 if (ctx
->API
== API_OPENGL
&& !ctx
->Extensions
.ARB_ES2_compatibility
) {
848 /* Check that all DrawBuffers are present */
849 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
850 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
851 const struct gl_renderbuffer_attachment
*att
852 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
854 if (att
->Type
== GL_NONE
) {
855 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
856 fbo_incomplete("missing drawbuffer", j
);
862 /* Check that the ReadBuffer is present */
863 if (fb
->ColorReadBuffer
!= GL_NONE
) {
864 const struct gl_renderbuffer_attachment
*att
865 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
867 if (att
->Type
== GL_NONE
) {
868 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
869 fbo_incomplete("missing readbuffer", -1);
878 if (numImages
== 0) {
879 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
880 fbo_incomplete("no attachments", -1);
884 /* Provisionally set status = COMPLETE ... */
885 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
887 /* ... but the driver may say the FB is incomplete.
888 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
891 if (ctx
->Driver
.ValidateFramebuffer
) {
892 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
893 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
894 fbo_incomplete("driver marked FBO as incomplete", -1);
898 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
900 * Note that if ARB_framebuffer_object is supported and the attached
901 * renderbuffers/textures are different sizes, the framebuffer
902 * width/height will be set to the smallest width/height.
904 fb
->Width
= minWidth
;
905 fb
->Height
= minHeight
;
907 /* finally, update the visual info for the framebuffer */
908 _mesa_update_framebuffer_visual(ctx
, fb
);
914 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
916 GET_CURRENT_CONTEXT(ctx
);
917 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
919 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
920 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
928 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
930 struct gl_renderbuffer
*newRb
;
931 GET_CURRENT_CONTEXT(ctx
);
933 ASSERT_OUTSIDE_BEGIN_END(ctx
);
935 if (target
!= GL_RENDERBUFFER_EXT
) {
936 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
940 /* No need to flush here since the render buffer binding has no
941 * effect on rendering state.
945 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
946 if (newRb
== &DummyRenderbuffer
) {
947 /* ID was reserved, but no real renderbuffer object made yet */
950 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
951 /* All RB IDs must be Gen'd */
952 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
957 /* create new renderbuffer object */
958 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
960 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
963 ASSERT(newRb
->AllocStorage
);
964 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
965 newRb
->RefCount
= 1; /* referenced by hash table */
972 ASSERT(newRb
!= &DummyRenderbuffer
);
974 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
979 * If the given renderbuffer is anywhere attached to the framebuffer, detach
981 * This is used when a renderbuffer object is deleted.
982 * The spec calls for unbinding.
985 detach_renderbuffer(struct gl_context
*ctx
,
986 struct gl_framebuffer
*fb
,
987 struct gl_renderbuffer
*rb
)
990 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
991 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
992 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
995 invalidate_framebuffer(fb
);
1000 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
1003 GET_CURRENT_CONTEXT(ctx
);
1005 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1006 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1008 for (i
= 0; i
< n
; i
++) {
1009 if (renderbuffers
[i
] > 0) {
1010 struct gl_renderbuffer
*rb
;
1011 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1013 /* check if deleting currently bound renderbuffer object */
1014 if (rb
== ctx
->CurrentRenderbuffer
) {
1016 ASSERT(rb
->RefCount
>= 2);
1017 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
1020 if (is_user_fbo(ctx
->DrawBuffer
)) {
1021 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1023 if (is_user_fbo(ctx
->ReadBuffer
)
1024 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1025 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1028 /* Remove from hash table immediately, to free the ID.
1029 * But the object will not be freed until it's no longer
1030 * referenced anywhere else.
1032 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1034 if (rb
!= &DummyRenderbuffer
) {
1035 /* no longer referenced by hash table */
1036 _mesa_reference_renderbuffer(&rb
, NULL
);
1045 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
1047 GET_CURRENT_CONTEXT(ctx
);
1051 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1054 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1061 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1063 for (i
= 0; i
< n
; i
++) {
1064 GLuint name
= first
+ i
;
1065 renderbuffers
[i
] = name
;
1066 /* insert dummy placeholder into hash table */
1067 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1068 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1069 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1075 * Given an internal format token for a render buffer, return the
1076 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1077 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1078 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1080 * This is similar to _mesa_base_tex_format() but the set of valid
1081 * internal formats is different.
1083 * Note that even if a format is determined to be legal here, validation
1084 * of the FBO may fail if the format is not supported by the driver/GPU.
1086 * \param internalFormat as passed to glRenderbufferStorage()
1087 * \return the base internal format, or 0 if internalFormat is illegal
1090 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1093 * Notes: some formats such as alpha, luminance, etc. were added
1094 * with GL_ARB_framebuffer_object.
1096 switch (internalFormat
) {
1102 return ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1106 case GL_LUMINANCE12
:
1107 case GL_LUMINANCE16
:
1108 return ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1109 case GL_LUMINANCE_ALPHA
:
1110 case GL_LUMINANCE4_ALPHA4
:
1111 case GL_LUMINANCE6_ALPHA2
:
1112 case GL_LUMINANCE8_ALPHA8
:
1113 case GL_LUMINANCE12_ALPHA4
:
1114 case GL_LUMINANCE12_ALPHA12
:
1115 case GL_LUMINANCE16_ALPHA16
:
1116 return ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1120 case GL_INTENSITY12
:
1121 case GL_INTENSITY16
:
1122 return ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1141 case GL_SRGB8_ALPHA8_EXT
:
1143 case GL_STENCIL_INDEX
:
1144 case GL_STENCIL_INDEX1_EXT
:
1145 case GL_STENCIL_INDEX4_EXT
:
1146 case GL_STENCIL_INDEX8_EXT
:
1147 case GL_STENCIL_INDEX16_EXT
:
1148 return GL_STENCIL_INDEX
;
1149 case GL_DEPTH_COMPONENT
:
1150 case GL_DEPTH_COMPONENT16
:
1151 case GL_DEPTH_COMPONENT24
:
1152 case GL_DEPTH_COMPONENT32
:
1153 return GL_DEPTH_COMPONENT
;
1154 case GL_DEPTH_STENCIL_EXT
:
1155 case GL_DEPTH24_STENCIL8_EXT
:
1156 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
1157 return GL_DEPTH_STENCIL_EXT
;
1160 case GL_DEPTH_COMPONENT32F
:
1161 if (ctx
->Extensions
.ARB_depth_buffer_float
)
1162 return GL_DEPTH_COMPONENT
;
1165 case GL_DEPTH32F_STENCIL8
:
1166 if (ctx
->Extensions
.ARB_depth_buffer_float
)
1167 return GL_DEPTH_STENCIL
;
1173 return ctx
->Extensions
.ARB_texture_rg
? GL_RED
: 0;
1177 return ctx
->Extensions
.ARB_texture_rg
? GL_RG
: 0;
1178 /* signed normalized texture formats */
1182 return ctx
->Extensions
.EXT_texture_snorm
? GL_RED
: 0;
1186 return ctx
->Extensions
.EXT_texture_snorm
? GL_RG
: 0;
1189 case GL_RGB16_SNORM
:
1190 return ctx
->Extensions
.EXT_texture_snorm
? GL_RGB
: 0;
1192 case GL_RGBA8_SNORM
:
1193 case GL_RGBA16_SNORM
:
1194 return ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA
: 0;
1195 case GL_ALPHA_SNORM
:
1196 case GL_ALPHA8_SNORM
:
1197 case GL_ALPHA16_SNORM
:
1198 return ctx
->Extensions
.EXT_texture_snorm
&&
1199 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1200 case GL_LUMINANCE_SNORM
:
1201 case GL_LUMINANCE8_SNORM
:
1202 case GL_LUMINANCE16_SNORM
:
1203 return ctx
->Extensions
.EXT_texture_snorm
&&
1204 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1205 case GL_LUMINANCE_ALPHA_SNORM
:
1206 case GL_LUMINANCE8_ALPHA8_SNORM
:
1207 case GL_LUMINANCE16_ALPHA16_SNORM
:
1208 return ctx
->Extensions
.EXT_texture_snorm
&&
1209 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1210 case GL_INTENSITY_SNORM
:
1211 case GL_INTENSITY8_SNORM
:
1212 case GL_INTENSITY16_SNORM
:
1213 return ctx
->Extensions
.EXT_texture_snorm
&&
1214 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1217 return ctx
->Extensions
.ARB_texture_rg
&&
1218 ctx
->Extensions
.ARB_texture_float
? GL_RED
: 0;
1221 return ctx
->Extensions
.ARB_texture_rg
&&
1222 ctx
->Extensions
.ARB_texture_float
? GL_RG
: 0;
1225 return ctx
->Extensions
.ARB_texture_float
? GL_RGB
: 0;
1228 return ctx
->Extensions
.ARB_texture_float
? GL_RGBA
: 0;
1229 case GL_ALPHA16F_ARB
:
1230 case GL_ALPHA32F_ARB
:
1231 return ctx
->Extensions
.ARB_texture_float
&&
1232 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1233 case GL_LUMINANCE16F_ARB
:
1234 case GL_LUMINANCE32F_ARB
:
1235 return ctx
->Extensions
.ARB_texture_float
&&
1236 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1237 case GL_LUMINANCE_ALPHA16F_ARB
:
1238 case GL_LUMINANCE_ALPHA32F_ARB
:
1239 return ctx
->Extensions
.ARB_texture_float
&&
1240 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1241 case GL_INTENSITY16F_ARB
:
1242 case GL_INTENSITY32F_ARB
:
1243 return ctx
->Extensions
.ARB_texture_float
&&
1244 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1246 return ctx
->Extensions
.EXT_texture_shared_exponent
? GL_RGB
: 0;
1247 case GL_R11F_G11F_B10F
:
1248 return ctx
->Extensions
.EXT_packed_float
? GL_RGB
: 0;
1250 case GL_RGBA8UI_EXT
:
1251 case GL_RGBA16UI_EXT
:
1252 case GL_RGBA32UI_EXT
:
1254 case GL_RGBA16I_EXT
:
1255 case GL_RGBA32I_EXT
:
1256 return ctx
->Extensions
.EXT_texture_integer
? GL_RGBA
: 0;
1259 case GL_RGB16UI_EXT
:
1260 case GL_RGB32UI_EXT
:
1264 return ctx
->Extensions
.EXT_texture_integer
? GL_RGB
: 0;
1272 return ctx
->Extensions
.ARB_texture_rg
&&
1273 ctx
->Extensions
.EXT_texture_integer
? GL_RED
: 0;
1281 return ctx
->Extensions
.ARB_texture_rg
&&
1282 ctx
->Extensions
.EXT_texture_integer
? GL_RG
: 0;
1284 case GL_INTENSITY8I_EXT
:
1285 case GL_INTENSITY8UI_EXT
:
1286 case GL_INTENSITY16I_EXT
:
1287 case GL_INTENSITY16UI_EXT
:
1288 case GL_INTENSITY32I_EXT
:
1289 case GL_INTENSITY32UI_EXT
:
1290 return ctx
->Extensions
.EXT_texture_integer
&&
1291 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1293 case GL_LUMINANCE8I_EXT
:
1294 case GL_LUMINANCE8UI_EXT
:
1295 case GL_LUMINANCE16I_EXT
:
1296 case GL_LUMINANCE16UI_EXT
:
1297 case GL_LUMINANCE32I_EXT
:
1298 case GL_LUMINANCE32UI_EXT
:
1299 return ctx
->Extensions
.EXT_texture_integer
&&
1300 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1302 case GL_LUMINANCE_ALPHA8I_EXT
:
1303 case GL_LUMINANCE_ALPHA8UI_EXT
:
1304 case GL_LUMINANCE_ALPHA16I_EXT
:
1305 case GL_LUMINANCE_ALPHA16UI_EXT
:
1306 case GL_LUMINANCE_ALPHA32I_EXT
:
1307 case GL_LUMINANCE_ALPHA32UI_EXT
:
1308 return ctx
->Extensions
.EXT_texture_integer
&&
1309 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1317 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1320 invalidate_rb(GLuint key
, void *data
, void *userData
)
1322 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1323 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1325 /* If this is a user-created FBO */
1326 if (is_user_fbo(fb
)) {
1328 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1329 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1330 if (att
->Type
== GL_RENDERBUFFER
&&
1331 att
->Renderbuffer
== rb
) {
1332 /* Mark fb status as indeterminate to force re-validation */
1341 /** sentinal value, see below */
1342 #define NO_SAMPLES 1000
1346 * Helper function used by _mesa_RenderbufferStorageEXT() and
1347 * _mesa_RenderbufferStorageMultisample().
1348 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
1351 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1352 GLsizei width
, GLsizei height
, GLsizei samples
)
1354 const char *func
= samples
== NO_SAMPLES
?
1355 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
1356 struct gl_renderbuffer
*rb
;
1358 GET_CURRENT_CONTEXT(ctx
);
1360 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1362 if (target
!= GL_RENDERBUFFER_EXT
) {
1363 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1367 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1368 if (baseFormat
== 0) {
1369 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
1373 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1374 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1378 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1379 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1383 if (samples
== NO_SAMPLES
) {
1384 /* NumSamples == 0 indicates non-multisampling */
1387 else if (samples
> (GLsizei
) ctx
->Const
.MaxSamples
) {
1388 /* note: driver may choose to use more samples than what's requested */
1389 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
1393 rb
= ctx
->CurrentRenderbuffer
;
1395 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1399 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1401 if (rb
->InternalFormat
== internalFormat
&&
1402 rb
->Width
== (GLuint
) width
&&
1403 rb
->Height
== (GLuint
) height
) {
1404 /* no change in allocation needed */
1408 /* These MUST get set by the AllocStorage func */
1409 rb
->Format
= MESA_FORMAT_NONE
;
1410 rb
->NumSamples
= samples
;
1412 /* Now allocate the storage */
1413 ASSERT(rb
->AllocStorage
);
1414 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1415 /* No error - check/set fields now */
1416 assert(rb
->Format
!= MESA_FORMAT_NONE
);
1417 assert(rb
->Width
== (GLuint
) width
);
1418 assert(rb
->Height
== (GLuint
) height
);
1419 rb
->InternalFormat
= internalFormat
;
1420 rb
->_BaseFormat
= baseFormat
;
1421 assert(rb
->_BaseFormat
!= 0);
1424 /* Probably ran out of memory - clear the fields */
1427 rb
->Format
= MESA_FORMAT_NONE
;
1428 rb
->InternalFormat
= GL_NONE
;
1429 rb
->_BaseFormat
= GL_NONE
;
1433 /* Invalidate the framebuffers the renderbuffer is attached in. */
1434 if (rb
->AttachedAnytime
) {
1435 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1440 #if FEATURE_OES_EGL_image
1442 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1444 struct gl_renderbuffer
*rb
;
1445 GET_CURRENT_CONTEXT(ctx
);
1446 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1448 if (!ctx
->Extensions
.OES_EGL_image
) {
1449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1450 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1454 if (target
!= GL_RENDERBUFFER
) {
1455 _mesa_error(ctx
, GL_INVALID_ENUM
,
1456 "EGLImageTargetRenderbufferStorageOES");
1460 rb
= ctx
->CurrentRenderbuffer
;
1462 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1463 "EGLImageTargetRenderbufferStorageOES");
1467 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1469 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1475 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1476 * _mesa_GetFramebufferAttachmentParameterivEXT()
1477 * We have to be careful to respect the base format. For example, if a
1478 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1479 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1480 * we need to return zero.
1483 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1486 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1487 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1488 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
||
1489 baseFormat
== GL_RG
|| baseFormat
== GL_RED
)
1490 return _mesa_get_format_bits(format
, pname
);
1493 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1494 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1495 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
|| baseFormat
== GL_RG
)
1496 return _mesa_get_format_bits(format
, pname
);
1499 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1500 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1501 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
)
1502 return _mesa_get_format_bits(format
, pname
);
1505 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1506 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1507 if (baseFormat
== GL_RGBA
|| baseFormat
== GL_ALPHA
||
1508 baseFormat
== GL_LUMINANCE_ALPHA
)
1509 return _mesa_get_format_bits(format
, pname
);
1512 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1513 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1514 if (baseFormat
== GL_DEPTH_COMPONENT
|| baseFormat
== GL_DEPTH_STENCIL
)
1515 return _mesa_get_format_bits(format
, pname
);
1518 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1519 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1520 if (baseFormat
== GL_STENCIL_INDEX
|| baseFormat
== GL_DEPTH_STENCIL
)
1521 return _mesa_get_format_bits(format
, pname
);
1532 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1533 GLsizei width
, GLsizei height
)
1535 /* GL_ARB_fbo says calling this function is equivalent to calling
1536 * glRenderbufferStorageMultisample() with samples=0. We pass in
1537 * a token value here just for error reporting purposes.
1539 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1544 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1545 GLenum internalFormat
,
1546 GLsizei width
, GLsizei height
)
1548 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1553 * OpenGL ES version of glRenderBufferStorage.
1556 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1557 GLsizei width
, GLsizei height
)
1559 switch (internalFormat
) {
1561 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1562 /* choose a closest format */
1563 internalFormat
= GL_RGB5
;
1569 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1574 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1576 struct gl_renderbuffer
*rb
;
1577 GET_CURRENT_CONTEXT(ctx
);
1579 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1581 if (target
!= GL_RENDERBUFFER_EXT
) {
1582 _mesa_error(ctx
, GL_INVALID_ENUM
,
1583 "glGetRenderbufferParameterivEXT(target)");
1587 rb
= ctx
->CurrentRenderbuffer
;
1589 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1590 "glGetRenderbufferParameterivEXT");
1594 /* No need to flush here since we're just quering state which is
1595 * not effected by rendering.
1599 case GL_RENDERBUFFER_WIDTH_EXT
:
1600 *params
= rb
->Width
;
1602 case GL_RENDERBUFFER_HEIGHT_EXT
:
1603 *params
= rb
->Height
;
1605 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1606 *params
= rb
->InternalFormat
;
1608 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1609 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1610 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1611 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1612 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1613 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1614 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1616 case GL_RENDERBUFFER_SAMPLES
:
1617 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1618 *params
= rb
->NumSamples
;
1623 _mesa_error(ctx
, GL_INVALID_ENUM
,
1624 "glGetRenderbufferParameterivEXT(target)");
1630 GLboolean GLAPIENTRY
1631 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1633 GET_CURRENT_CONTEXT(ctx
);
1634 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1636 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1637 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1645 * Check if any of the attachments of the given framebuffer are textures
1646 * (render to texture). Call ctx->Driver.RenderTexture() for such
1650 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1653 ASSERT(ctx
->Driver
.RenderTexture
);
1655 if (is_winsys_fbo(fb
))
1656 return; /* can't render to texture with winsys framebuffers */
1658 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1659 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1660 if (att
->Texture
&& _mesa_get_attachment_teximage(att
)) {
1661 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1668 * Examine all the framebuffer's attachments to see if any are textures.
1669 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1670 * notify the device driver that the texture image may have changed.
1673 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1675 if (is_winsys_fbo(fb
))
1676 return; /* can't render to texture with winsys framebuffers */
1678 if (ctx
->Driver
.FinishRenderTexture
) {
1680 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1681 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1682 if (att
->Texture
&& att
->Renderbuffer
) {
1683 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1691 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1693 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1694 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1695 GLboolean bindReadBuf
, bindDrawBuf
;
1696 GET_CURRENT_CONTEXT(ctx
);
1699 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1700 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1701 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1705 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1707 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1708 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1709 "glBindFramebufferEXT(unsupported)");
1714 #if FEATURE_EXT_framebuffer_blit
1715 case GL_DRAW_FRAMEBUFFER_EXT
:
1716 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1717 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1720 bindDrawBuf
= GL_TRUE
;
1721 bindReadBuf
= GL_FALSE
;
1723 case GL_READ_FRAMEBUFFER_EXT
:
1724 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1725 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1728 bindDrawBuf
= GL_FALSE
;
1729 bindReadBuf
= GL_TRUE
;
1732 case GL_FRAMEBUFFER_EXT
:
1733 bindDrawBuf
= GL_TRUE
;
1734 bindReadBuf
= GL_TRUE
;
1737 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1742 /* Binding a user-created framebuffer object */
1743 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1744 if (newDrawFb
== &DummyFramebuffer
) {
1745 /* ID was reserved, but no real framebuffer object made yet */
1748 else if (!newDrawFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1749 /* All FBO IDs must be Gen'd */
1750 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1755 /* create new framebuffer object */
1756 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1758 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1761 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1763 newReadFb
= newDrawFb
;
1766 /* Binding the window system framebuffer (which was originally set
1767 * with MakeCurrent).
1769 newDrawFb
= ctx
->WinSysDrawBuffer
;
1770 newReadFb
= ctx
->WinSysReadBuffer
;
1774 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1776 /* save pointers to current/old framebuffers */
1777 oldDrawFb
= ctx
->DrawBuffer
;
1778 oldReadFb
= ctx
->ReadBuffer
;
1780 /* check if really changing bindings */
1781 if (oldDrawFb
== newDrawFb
)
1782 bindDrawBuf
= GL_FALSE
;
1783 if (oldReadFb
== newReadFb
)
1784 bindReadBuf
= GL_FALSE
;
1787 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1789 * We also check if we're beginning and/or ending render-to-texture.
1790 * When a framebuffer with texture attachments is unbound, call
1791 * ctx->Driver.FinishRenderTexture().
1792 * When a framebuffer with texture attachments is bound, call
1793 * ctx->Driver.RenderTexture().
1795 * Note that if the ReadBuffer has texture attachments we don't consider
1796 * that a render-to-texture case.
1799 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1801 /* check if old readbuffer was render-to-texture */
1802 check_end_texture_render(ctx
, oldReadFb
);
1804 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1808 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1810 /* check if old read/draw buffers were render-to-texture */
1812 check_end_texture_render(ctx
, oldReadFb
);
1814 if (oldDrawFb
!= oldReadFb
)
1815 check_end_texture_render(ctx
, oldDrawFb
);
1817 /* check if newly bound framebuffer has any texture attachments */
1818 check_begin_texture_render(ctx
, newDrawFb
);
1820 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1823 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1824 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1830 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1833 GET_CURRENT_CONTEXT(ctx
);
1835 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1836 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1838 for (i
= 0; i
< n
; i
++) {
1839 if (framebuffers
[i
] > 0) {
1840 struct gl_framebuffer
*fb
;
1841 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1843 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1845 /* check if deleting currently bound framebuffer object */
1846 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1847 /* separate draw/read binding points */
1848 if (fb
== ctx
->DrawBuffer
) {
1850 ASSERT(fb
->RefCount
>= 2);
1851 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1853 if (fb
== ctx
->ReadBuffer
) {
1855 ASSERT(fb
->RefCount
>= 2);
1856 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1860 /* only one binding point for read/draw buffers */
1861 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1863 ASSERT(fb
->RefCount
>= 2);
1864 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
1868 /* remove from hash table immediately, to free the ID */
1869 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1871 if (fb
!= &DummyFramebuffer
) {
1872 /* But the object will not be freed until it's no longer
1873 * bound in any context.
1875 _mesa_reference_framebuffer(&fb
, NULL
);
1884 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1886 GET_CURRENT_CONTEXT(ctx
);
1890 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1893 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1900 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1902 for (i
= 0; i
< n
; i
++) {
1903 GLuint name
= first
+ i
;
1904 framebuffers
[i
] = name
;
1905 /* insert dummy placeholder into hash table */
1906 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1907 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1908 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1915 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1917 struct gl_framebuffer
*buffer
;
1918 GET_CURRENT_CONTEXT(ctx
);
1920 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1922 buffer
= get_framebuffer_target(ctx
, target
);
1924 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1928 if (is_winsys_fbo(buffer
)) {
1929 /* The window system / default framebuffer is always complete */
1930 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1933 /* No need to flush here */
1935 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1936 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1939 return buffer
->_Status
;
1945 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1948 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
1949 GLenum attachment
, GLenum textarget
, GLuint texture
,
1950 GLint level
, GLint zoffset
)
1952 struct gl_renderbuffer_attachment
*att
;
1953 struct gl_texture_object
*texObj
= NULL
;
1954 struct gl_framebuffer
*fb
;
1956 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1958 fb
= get_framebuffer_target(ctx
, target
);
1960 _mesa_error(ctx
, GL_INVALID_ENUM
,
1961 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1965 /* check framebuffer binding */
1966 if (is_winsys_fbo(fb
)) {
1967 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1968 "glFramebufferTexture%sEXT", caller
);
1973 /* The textarget, level, and zoffset parameters are only validated if
1974 * texture is non-zero.
1977 GLboolean err
= GL_TRUE
;
1979 texObj
= _mesa_lookup_texture(ctx
, texture
);
1980 if (texObj
!= NULL
) {
1981 if (textarget
== 0) {
1982 /* XXX what's the purpose of this? */
1983 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1984 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1985 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1988 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1989 ? !is_cube_face(textarget
)
1990 : (texObj
->Target
!= textarget
);
1994 /* can't render to a non-existant texture */
1995 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1996 "glFramebufferTexture%sEXT(non existant texture)",
2002 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2003 "glFramebufferTexture%sEXT(texture target mismatch)",
2008 if (texObj
->Target
== GL_TEXTURE_3D
) {
2009 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2010 if (zoffset
< 0 || zoffset
>= maxSize
) {
2011 _mesa_error(ctx
, GL_INVALID_VALUE
,
2012 "glFramebufferTexture%sEXT(zoffset)", caller
);
2016 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2017 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
2018 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
2019 _mesa_error(ctx
, GL_INVALID_VALUE
,
2020 "glFramebufferTexture%sEXT(layer)", caller
);
2026 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
2027 _mesa_error(ctx
, GL_INVALID_VALUE
,
2028 "glFramebufferTexture%sEXT(level)", caller
);
2033 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2035 _mesa_error(ctx
, GL_INVALID_ENUM
,
2036 "glFramebufferTexture%sEXT(attachment)", caller
);
2040 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2042 _glthread_LOCK_MUTEX(fb
->Mutex
);
2044 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2046 /* Set the render-to-texture flag. We'll check this flag in
2047 * glTexImage() and friends to determine if we need to revalidate
2048 * any FBOs that might be rendering into this texture.
2049 * This flag never gets cleared since it's non-trivial to determine
2050 * when all FBOs might be done rendering to this texture. That's OK
2051 * though since it's uncommon to render to a texture then repeatedly
2052 * call glTexImage() to change images in the texture.
2054 texObj
->_RenderToTexture
= GL_TRUE
;
2057 _mesa_remove_attachment(ctx
, att
);
2060 invalidate_framebuffer(fb
);
2062 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
2068 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
2069 GLenum textarget
, GLuint texture
, GLint level
)
2071 GET_CURRENT_CONTEXT(ctx
);
2076 switch (textarget
) {
2080 case GL_TEXTURE_1D_ARRAY
:
2081 error
= !ctx
->Extensions
.EXT_texture_array
;
2088 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2089 "glFramebufferTexture1DEXT(textarget=%s)",
2090 _mesa_lookup_enum_by_nr(textarget
));
2095 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2101 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
2102 GLenum textarget
, GLuint texture
, GLint level
)
2104 GET_CURRENT_CONTEXT(ctx
);
2109 switch (textarget
) {
2113 case GL_TEXTURE_RECTANGLE
:
2114 error
= !ctx
->Extensions
.NV_texture_rectangle
;
2116 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2117 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2118 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2119 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2120 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2121 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2122 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2124 case GL_TEXTURE_2D_ARRAY
:
2125 error
= !ctx
->Extensions
.EXT_texture_array
;
2132 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2133 "glFramebufferTexture2DEXT(textarget=%s)",
2134 _mesa_lookup_enum_by_nr(textarget
));
2139 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2145 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
2146 GLenum textarget
, GLuint texture
,
2147 GLint level
, GLint zoffset
)
2149 GET_CURRENT_CONTEXT(ctx
);
2151 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2152 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2153 "glFramebufferTexture3DEXT(textarget)");
2157 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2163 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
2164 GLuint texture
, GLint level
, GLint layer
)
2166 GET_CURRENT_CONTEXT(ctx
);
2168 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2174 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
2175 GLenum renderbufferTarget
,
2176 GLuint renderbuffer
)
2178 struct gl_renderbuffer_attachment
*att
;
2179 struct gl_framebuffer
*fb
;
2180 struct gl_renderbuffer
*rb
;
2181 GET_CURRENT_CONTEXT(ctx
);
2183 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2185 fb
= get_framebuffer_target(ctx
, target
);
2187 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFramebufferRenderbufferEXT(target)");
2191 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2192 _mesa_error(ctx
, GL_INVALID_ENUM
,
2193 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2197 if (is_winsys_fbo(fb
)) {
2198 /* Can't attach new renderbuffers to a window system framebuffer */
2199 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
2203 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2205 _mesa_error(ctx
, GL_INVALID_ENUM
,
2206 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2207 _mesa_lookup_enum_by_nr(attachment
));
2212 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2214 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2215 "glFramebufferRenderbufferEXT(non-existant"
2216 " renderbuffer %u)", renderbuffer
);
2219 else if (rb
== &DummyRenderbuffer
) {
2220 /* This is what NVIDIA does */
2221 _mesa_error(ctx
, GL_INVALID_VALUE
,
2222 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2228 /* remove renderbuffer attachment */
2232 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2233 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2234 /* make sure the renderbuffer is a depth/stencil format */
2235 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2236 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2238 "glFramebufferRenderbufferEXT(renderbuffer"
2239 " is not DEPTH_STENCIL format)");
2245 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2247 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2248 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2250 /* Some subsequent GL commands may depend on the framebuffer's visual
2251 * after the binding is updated. Update visual info now.
2253 _mesa_update_framebuffer_visual(ctx
, fb
);
2258 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
2259 GLenum pname
, GLint
*params
)
2261 const struct gl_renderbuffer_attachment
*att
;
2262 struct gl_framebuffer
*buffer
;
2264 GET_CURRENT_CONTEXT(ctx
);
2266 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2268 /* The error differs in GL andd GLES. */
2269 err
= ctx
->API
== API_OPENGL
? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2271 buffer
= get_framebuffer_target(ctx
, target
);
2273 _mesa_error(ctx
, GL_INVALID_ENUM
,
2274 "glGetFramebufferAttachmentParameterivEXT(target)");
2278 if (is_winsys_fbo(buffer
)) {
2279 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2282 * "If the framebuffer currently bound to target is zero, then
2283 * INVALID_OPERATION is generated."
2285 * The EXT_framebuffer_object spec has the same wording, and the
2286 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2289 if (ctx
->API
!= API_OPENGL
|| !ctx
->Extensions
.ARB_framebuffer_object
) {
2290 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2291 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2294 /* the default / window-system FBO */
2295 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2298 /* user-created framebuffer FBO */
2299 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
2303 _mesa_error(ctx
, GL_INVALID_ENUM
,
2304 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2308 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2309 /* the depth and stencil attachments must point to the same buffer */
2310 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2311 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2312 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2313 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2314 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2315 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2316 " attachments differ)");
2321 /* No need to flush here */
2324 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2325 *params
= is_winsys_fbo(buffer
) ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2327 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2328 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2329 *params
= att
->Renderbuffer
->Name
;
2331 else if (att
->Type
== GL_TEXTURE
) {
2332 *params
= att
->Texture
->Name
;
2335 assert(att
->Type
== GL_NONE
);
2336 if (ctx
->API
== API_OPENGL
) {
2339 _mesa_error(ctx
, GL_INVALID_ENUM
,
2340 "glGetFramebufferAttachmentParameterivEXT(pname)");
2344 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2345 if (att
->Type
== GL_TEXTURE
) {
2346 *params
= att
->TextureLevel
;
2348 else if (att
->Type
== GL_NONE
) {
2349 _mesa_error(ctx
, err
,
2350 "glGetFramebufferAttachmentParameterivEXT(pname)");
2353 _mesa_error(ctx
, GL_INVALID_ENUM
,
2354 "glGetFramebufferAttachmentParameterivEXT(pname)");
2357 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2358 if (att
->Type
== GL_TEXTURE
) {
2359 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2360 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2366 else if (att
->Type
== GL_NONE
) {
2367 _mesa_error(ctx
, err
,
2368 "glGetFramebufferAttachmentParameterivEXT(pname)");
2371 _mesa_error(ctx
, GL_INVALID_ENUM
,
2372 "glGetFramebufferAttachmentParameterivEXT(pname)");
2375 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2376 if (att
->Type
== GL_TEXTURE
) {
2377 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2378 *params
= att
->Zoffset
;
2384 else if (att
->Type
== GL_NONE
) {
2385 _mesa_error(ctx
, err
,
2386 "glGetFramebufferAttachmentParameterivEXT(pname)");
2389 _mesa_error(ctx
, GL_INVALID_ENUM
,
2390 "glGetFramebufferAttachmentParameterivEXT(pname)");
2393 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2394 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2395 _mesa_error(ctx
, GL_INVALID_ENUM
,
2396 "glGetFramebufferAttachmentParameterivEXT(pname)");
2398 else if (att
->Type
== GL_NONE
) {
2399 _mesa_error(ctx
, err
,
2400 "glGetFramebufferAttachmentParameterivEXT(pname)");
2403 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& ctx
->Const
.sRGBCapable
) {
2404 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2407 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2408 * if the sRGB conversion is unsupported. */
2409 *params
= GL_LINEAR
;
2413 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2414 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2415 _mesa_error(ctx
, GL_INVALID_ENUM
,
2416 "glGetFramebufferAttachmentParameterivEXT(pname)");
2419 else if (att
->Type
== GL_NONE
) {
2420 _mesa_error(ctx
, err
,
2421 "glGetFramebufferAttachmentParameterivEXT(pname)");
2424 gl_format format
= att
->Renderbuffer
->Format
;
2425 if (format
== MESA_FORMAT_S8
) {
2429 else if (format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
2430 /* depends on the attachment parameter */
2431 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2439 *params
= _mesa_get_format_datatype(format
);
2443 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2444 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2445 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2446 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2447 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2448 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2449 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2450 _mesa_error(ctx
, GL_INVALID_ENUM
,
2451 "glGetFramebufferAttachmentParameterivEXT(pname)");
2453 else if (att
->Type
== GL_NONE
) {
2454 _mesa_error(ctx
, err
,
2455 "glGetFramebufferAttachmentParameterivEXT(pname)");
2457 else if (att
->Texture
) {
2458 const struct gl_texture_image
*texImage
=
2459 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2462 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2463 texImage
->TexFormat
);
2469 else if (att
->Renderbuffer
) {
2470 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2471 att
->Renderbuffer
->Format
);
2474 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
2475 " invalid FBO attachment structure");
2479 _mesa_error(ctx
, GL_INVALID_ENUM
,
2480 "glGetFramebufferAttachmentParameterivEXT(pname)");
2487 _mesa_GenerateMipmapEXT(GLenum target
)
2489 struct gl_texture_image
*srcImage
;
2490 struct gl_texture_object
*texObj
;
2493 GET_CURRENT_CONTEXT(ctx
);
2495 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2496 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2504 case GL_TEXTURE_CUBE_MAP
:
2505 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2507 case GL_TEXTURE_1D_ARRAY
:
2508 case GL_TEXTURE_2D_ARRAY
:
2509 error
= !ctx
->Extensions
.EXT_texture_array
;
2516 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target=%s)",
2517 _mesa_lookup_enum_by_nr(target
));
2521 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2523 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2528 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2529 !_mesa_cube_complete(texObj
)) {
2530 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2531 "glGenerateMipmap(incomplete cube map)");
2535 _mesa_lock_texture(ctx
, texObj
);
2537 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
2539 _mesa_unlock_texture(ctx
, texObj
);
2543 if (target
== GL_TEXTURE_CUBE_MAP
) {
2545 for (face
= 0; face
< 6; face
++)
2546 ctx
->Driver
.GenerateMipmap(ctx
,
2547 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2551 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2553 _mesa_unlock_texture(ctx
, texObj
);
2557 #if FEATURE_EXT_framebuffer_blit
2559 static const struct gl_renderbuffer_attachment
*
2560 find_attachment(const struct gl_framebuffer
*fb
,
2561 const struct gl_renderbuffer
*rb
)
2564 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2565 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2566 return &fb
->Attachment
[i
];
2574 * Blit rectangular region, optionally from one framebuffer to another.
2576 * Note, if the src buffer is multisampled and the dest is not, this is
2577 * when the samples must be resolved to a single color.
2580 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2581 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2582 GLbitfield mask
, GLenum filter
)
2584 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2585 GL_DEPTH_BUFFER_BIT
|
2586 GL_STENCIL_BUFFER_BIT
);
2587 const struct gl_framebuffer
*readFb
, *drawFb
;
2588 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
2589 GET_CURRENT_CONTEXT(ctx
);
2591 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2592 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2594 if (MESA_VERBOSE
& VERBOSE_API
)
2596 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2597 srcX0
, srcY0
, srcX1
, srcY1
,
2598 dstX0
, dstY0
, dstX1
, dstY1
,
2599 mask
, _mesa_lookup_enum_by_nr(filter
));
2601 if (ctx
->NewState
) {
2602 _mesa_update_state(ctx
);
2605 readFb
= ctx
->ReadBuffer
;
2606 drawFb
= ctx
->DrawBuffer
;
2608 if (!readFb
|| !drawFb
) {
2609 /* This will normally never happen but someday we may want to
2610 * support MakeCurrent() with no drawables.
2615 /* check for complete framebuffers */
2616 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2617 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2618 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2619 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2623 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2624 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2628 if (mask
& ~legalMaskBits
) {
2629 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2633 /* depth/stencil must be blitted with nearest filtering */
2634 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2635 && filter
!= GL_NEAREST
) {
2636 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2637 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2641 /* get color read/draw renderbuffers */
2642 if (mask
& GL_COLOR_BUFFER_BIT
) {
2643 colorReadRb
= readFb
->_ColorReadBuffer
;
2644 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2646 /* From the EXT_framebuffer_object spec:
2648 * "If a buffer is specified in <mask> and does not exist in both
2649 * the read and draw framebuffers, the corresponding bit is silently
2652 if ((colorReadRb
== NULL
) || (colorDrawRb
== NULL
)) {
2653 colorReadRb
= colorDrawRb
= NULL
;
2654 mask
&= ~GL_COLOR_BUFFER_BIT
;
2658 colorReadRb
= colorDrawRb
= NULL
;
2661 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2662 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2663 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2665 /* From the EXT_framebuffer_object spec:
2667 * "If a buffer is specified in <mask> and does not exist in both
2668 * the read and draw framebuffers, the corresponding bit is silently
2671 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2672 readRb
= drawRb
= NULL
;
2673 mask
&= ~GL_STENCIL_BUFFER_BIT
;
2675 else if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2676 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2677 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2678 "glBlitFramebufferEXT(stencil buffer size mismatch)");
2683 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2684 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2685 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2687 /* From the EXT_framebuffer_object spec:
2689 * "If a buffer is specified in <mask> and does not exist in both
2690 * the read and draw framebuffers, the corresponding bit is silently
2693 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2694 readRb
= drawRb
= NULL
;
2695 mask
&= ~GL_DEPTH_BUFFER_BIT
;
2697 else if (_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2698 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) {
2699 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2700 "glBlitFramebufferEXT(depth buffer size mismatch)");
2705 if (readFb
->Visual
.samples
> 0 &&
2706 drawFb
->Visual
.samples
> 0 &&
2707 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2708 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2709 "glBlitFramebufferEXT(mismatched samples");
2713 /* extra checks for multisample copies... */
2714 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2715 /* src and dest region sizes must be the same */
2716 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2717 srcY1
- srcY0
!= dstY1
- dstY0
) {
2718 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2719 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2723 /* color formats must match */
2726 colorReadRb
->Format
!= colorDrawRb
->Format
) {
2727 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2728 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2733 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2734 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2740 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2742 srcX0
, srcY0
, srcX1
, srcY1
,
2743 dstX0
, dstY0
, dstX1
, dstY1
,
2746 const struct gl_renderbuffer_attachment
*att
;
2748 att
= find_attachment(readFb
, colorReadRb
);
2749 printf(" Src FBO %u RB %u (%dx%d) ",
2750 readFb
->Name
, colorReadRb
->Name
,
2751 colorReadRb
->Width
, colorReadRb
->Height
);
2752 if (att
&& att
->Texture
) {
2753 printf("Tex %u tgt 0x%x level %u face %u",
2755 att
->Texture
->Target
,
2761 att
= find_attachment(drawFb
, colorDrawRb
);
2762 printf(" Dst FBO %u RB %u (%dx%d) ",
2763 drawFb
->Name
, colorDrawRb
->Name
,
2764 colorDrawRb
->Width
, colorDrawRb
->Height
);
2765 if (att
&& att
->Texture
) {
2766 printf("Tex %u tgt 0x%x level %u face %u",
2768 att
->Texture
->Target
,
2780 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2781 ctx
->Driver
.BlitFramebuffer(ctx
,
2782 srcX0
, srcY0
, srcX1
, srcY1
,
2783 dstX0
, dstY0
, dstX1
, dstY1
,
2786 #endif /* FEATURE_EXT_framebuffer_blit */
2788 #if FEATURE_ARB_geometry_shader4
2790 _mesa_FramebufferTextureARB(GLenum target
, GLenum attachment
,
2791 GLuint texture
, GLint level
)
2793 GET_CURRENT_CONTEXT(ctx
);
2794 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2795 "glFramebufferTextureARB "
2796 "not implemented!");
2800 _mesa_FramebufferTextureFaceARB(GLenum target
, GLenum attachment
,
2801 GLuint texture
, GLint level
, GLenum face
)
2803 GET_CURRENT_CONTEXT(ctx
);
2804 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2805 "glFramebufferTextureFaceARB "
2806 "not implemented!");
2808 #endif /* FEATURE_ARB_geometry_shader4 */