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"
44 #include "mfeatures.h"
46 #include "renderbuffer.h"
52 /** Set this to 1 to help debug FBO incompleteness problems */
55 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
62 * None of the GL_EXT_framebuffer_object functions are compiled into
69 * When glGenRender/FramebuffersEXT() is called we insert pointers to
70 * these placeholder objects into the hash table.
71 * Later, when the object ID is first bound, we replace the placeholder
72 * with the real frame/renderbuffer.
74 static struct gl_framebuffer DummyFramebuffer
;
75 static struct gl_renderbuffer DummyRenderbuffer
;
77 /* We bind this framebuffer when applications pass a NULL
78 * drawable/surface in make current. */
79 static struct gl_framebuffer IncompleteFramebuffer
;
83 * Is the given FBO a user-created FBO?
85 static inline GLboolean
86 is_user_fbo(const struct gl_framebuffer
*fb
)
93 * Is the given FBO a window system FBO (like an X window)?
95 static inline GLboolean
96 is_winsys_fbo(const struct gl_framebuffer
*fb
)
103 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
109 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
116 _mesa_init_fbobjects(struct gl_context
*ctx
)
118 _glthread_INIT_MUTEX(DummyFramebuffer
.Mutex
);
119 _glthread_INIT_MUTEX(DummyRenderbuffer
.Mutex
);
120 _glthread_INIT_MUTEX(IncompleteFramebuffer
.Mutex
);
121 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
122 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
123 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
126 struct gl_framebuffer
*
127 _mesa_get_incomplete_framebuffer(void)
129 return &IncompleteFramebuffer
;
133 * Helper routine for getting a gl_renderbuffer.
135 struct gl_renderbuffer
*
136 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
138 struct gl_renderbuffer
*rb
;
143 rb
= (struct gl_renderbuffer
*)
144 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
150 * Helper routine for getting a gl_framebuffer.
152 struct gl_framebuffer
*
153 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
155 struct gl_framebuffer
*fb
;
160 fb
= (struct gl_framebuffer
*)
161 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
167 * Mark the given framebuffer as invalid. This will force the
168 * test for framebuffer completeness to be done before the framebuffer
172 invalidate_framebuffer(struct gl_framebuffer
*fb
)
174 fb
->_Status
= 0; /* "indeterminate" */
179 * Return the gl_framebuffer object which corresponds to the given
180 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
181 * Check support for GL_EXT_framebuffer_blit to determine if certain
183 * \return gl_framebuffer pointer or NULL if target is illegal
185 static struct gl_framebuffer
*
186 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
189 case GL_DRAW_FRAMEBUFFER
:
190 return ctx
->Extensions
.EXT_framebuffer_blit
191 ? ctx
->DrawBuffer
: NULL
;
192 case GL_READ_FRAMEBUFFER
:
193 return ctx
->Extensions
.EXT_framebuffer_blit
194 ? ctx
->ReadBuffer
: NULL
;
195 case GL_FRAMEBUFFER_EXT
:
196 return ctx
->DrawBuffer
;
204 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
205 * gl_renderbuffer_attachment object.
206 * This function is only used for user-created FB objects, not the
207 * default / window-system FB object.
208 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
209 * the depth buffer attachment point.
211 struct gl_renderbuffer_attachment
*
212 _mesa_get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
217 assert(is_user_fbo(fb
));
219 switch (attachment
) {
220 case GL_COLOR_ATTACHMENT0_EXT
:
221 case GL_COLOR_ATTACHMENT1_EXT
:
222 case GL_COLOR_ATTACHMENT2_EXT
:
223 case GL_COLOR_ATTACHMENT3_EXT
:
224 case GL_COLOR_ATTACHMENT4_EXT
:
225 case GL_COLOR_ATTACHMENT5_EXT
:
226 case GL_COLOR_ATTACHMENT6_EXT
:
227 case GL_COLOR_ATTACHMENT7_EXT
:
228 case GL_COLOR_ATTACHMENT8_EXT
:
229 case GL_COLOR_ATTACHMENT9_EXT
:
230 case GL_COLOR_ATTACHMENT10_EXT
:
231 case GL_COLOR_ATTACHMENT11_EXT
:
232 case GL_COLOR_ATTACHMENT12_EXT
:
233 case GL_COLOR_ATTACHMENT13_EXT
:
234 case GL_COLOR_ATTACHMENT14_EXT
:
235 case GL_COLOR_ATTACHMENT15_EXT
:
236 /* Only OpenGL ES 1.x forbids color attachments other than
237 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
240 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
241 if (i
>= ctx
->Const
.MaxColorAttachments
) {
244 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
245 case GL_DEPTH_STENCIL_ATTACHMENT
:
246 case GL_DEPTH_ATTACHMENT_EXT
:
247 return &fb
->Attachment
[BUFFER_DEPTH
];
248 case GL_STENCIL_ATTACHMENT_EXT
:
249 return &fb
->Attachment
[BUFFER_STENCIL
];
257 * As above, but only used for getting attachments of the default /
258 * window-system framebuffer (not user-created framebuffer objects).
260 static struct gl_renderbuffer_attachment
*
261 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
264 assert(is_winsys_fbo(fb
));
266 switch (attachment
) {
268 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
270 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
272 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
274 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
276 if (fb
->Visual
.numAuxBuffers
== 1) {
277 return &fb
->Attachment
[BUFFER_AUX0
];
281 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
283 * "If the default framebuffer is bound to target, then attachment must
284 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
285 * identifying a color buffer; DEPTH, identifying the depth buffer; or
286 * STENCIL, identifying the stencil buffer."
288 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
289 * language. However, revision #33 of the ARB_framebuffer_object spec
292 * "If the default framebuffer is bound to <target>, then <attachment>
293 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
294 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
295 * depth buffer, or the stencil buffer, and <pname> may be
296 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
297 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
299 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
300 * from glext.h, so shipping apps should not use those values.
302 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
303 * support queries of the window system FBO.
306 return &fb
->Attachment
[BUFFER_DEPTH
];
308 return &fb
->Attachment
[BUFFER_STENCIL
];
317 * Remove any texture or renderbuffer attached to the given attachment
318 * point. Update reference counts, etc.
321 _mesa_remove_attachment(struct gl_context
*ctx
,
322 struct gl_renderbuffer_attachment
*att
)
324 if (att
->Type
== GL_TEXTURE
) {
325 ASSERT(att
->Texture
);
326 if (ctx
->Driver
.FinishRenderTexture
) {
327 /* tell driver that we're done rendering to this texture. */
328 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
330 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
331 ASSERT(!att
->Texture
);
333 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
334 ASSERT(!att
->Texture
);
335 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
336 ASSERT(!att
->Renderbuffer
);
339 att
->Complete
= GL_TRUE
;
344 * Bind a texture object to an attachment point.
345 * The previous binding, if any, will be removed first.
348 _mesa_set_texture_attachment(struct gl_context
*ctx
,
349 struct gl_framebuffer
*fb
,
350 struct gl_renderbuffer_attachment
*att
,
351 struct gl_texture_object
*texObj
,
352 GLenum texTarget
, GLuint level
, GLuint zoffset
)
354 if (att
->Texture
== texObj
) {
355 /* re-attaching same texture */
356 ASSERT(att
->Type
== GL_TEXTURE
);
357 if (ctx
->Driver
.FinishRenderTexture
)
358 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
362 if (ctx
->Driver
.FinishRenderTexture
&& att
->Texture
)
363 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
364 _mesa_remove_attachment(ctx
, att
);
365 att
->Type
= GL_TEXTURE
;
366 assert(!att
->Texture
);
367 _mesa_reference_texobj(&att
->Texture
, texObj
);
370 /* always update these fields */
371 att
->TextureLevel
= level
;
372 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
373 att
->Zoffset
= zoffset
;
374 att
->Complete
= GL_FALSE
;
376 if (_mesa_get_attachment_teximage(att
)) {
377 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
380 invalidate_framebuffer(fb
);
385 * Bind a renderbuffer to an attachment point.
386 * The previous binding, if any, will be removed first.
389 _mesa_set_renderbuffer_attachment(struct gl_context
*ctx
,
390 struct gl_renderbuffer_attachment
*att
,
391 struct gl_renderbuffer
*rb
)
393 /* XXX check if re-doing same attachment, exit early */
394 _mesa_remove_attachment(ctx
, att
);
395 att
->Type
= GL_RENDERBUFFER_EXT
;
396 att
->Texture
= NULL
; /* just to be safe */
397 att
->Complete
= GL_FALSE
;
398 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
403 * Fallback for ctx->Driver.FramebufferRenderbuffer()
404 * Attach a renderbuffer object to a framebuffer object.
407 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
408 struct gl_framebuffer
*fb
,
409 GLenum attachment
, struct gl_renderbuffer
*rb
)
411 struct gl_renderbuffer_attachment
*att
;
413 _glthread_LOCK_MUTEX(fb
->Mutex
);
415 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
418 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
419 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
420 /* do stencil attachment here (depth already done above) */
421 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
423 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
425 rb
->AttachedAnytime
= GL_TRUE
;
428 _mesa_remove_attachment(ctx
, att
);
431 invalidate_framebuffer(fb
);
433 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
438 * Fallback for ctx->Driver.ValidateFramebuffer()
439 * Check if the renderbuffer's formats are supported by the software
441 * Drivers should probably override this.
444 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
447 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
448 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
450 switch (rb
->_BaseFormat
) {
452 case GL_LUMINANCE_ALPHA
:
457 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
461 /* render buffer format is supported by software rendering */
472 att_incomplete(const char *msg
)
475 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
486 fbo_incomplete(const char *msg
, int index
)
489 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
498 * Is the given base format a legal format for a color renderbuffer?
501 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
503 switch (baseFormat
) {
508 case GL_LUMINANCE_ALPHA
:
511 return ctx
->Extensions
.ARB_framebuffer_object
;
514 return ctx
->Extensions
.ARB_texture_rg
;
522 * Is the given base format a legal format for a depth/stencil renderbuffer?
525 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
527 switch (baseFormat
) {
528 case GL_DEPTH_COMPONENT
:
529 case GL_DEPTH_STENCIL_EXT
:
538 * Test if an attachment point is complete and update its Complete field.
539 * \param format if GL_COLOR, this is a color attachment point,
540 * if GL_DEPTH, this is a depth component attachment point,
541 * if GL_STENCIL, this is a stencil component attachment point.
544 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
545 struct gl_renderbuffer_attachment
*att
)
547 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
549 /* assume complete */
550 att
->Complete
= GL_TRUE
;
552 /* Look for reasons why the attachment might be incomplete */
553 if (att
->Type
== GL_TEXTURE
) {
554 const struct gl_texture_object
*texObj
= att
->Texture
;
555 struct gl_texture_image
*texImage
;
559 att_incomplete("no texobj");
560 att
->Complete
= GL_FALSE
;
564 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
566 att_incomplete("no teximage");
567 att
->Complete
= GL_FALSE
;
570 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
571 att_incomplete("teximage width/height=0");
572 printf("texobj = %u\n", texObj
->Name
);
573 printf("level = %d\n", att
->TextureLevel
);
574 att
->Complete
= GL_FALSE
;
577 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
578 att_incomplete("bad z offset");
579 att
->Complete
= GL_FALSE
;
583 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
585 if (format
== GL_COLOR
) {
586 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
587 att_incomplete("bad format");
588 att
->Complete
= GL_FALSE
;
591 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
592 att_incomplete("compressed internalformat");
593 att
->Complete
= GL_FALSE
;
597 else if (format
== GL_DEPTH
) {
598 if (baseFormat
== GL_DEPTH_COMPONENT
) {
602 att
->Complete
= GL_FALSE
;
603 att_incomplete("bad depth format");
609 /* no such thing as stencil-only textures */
610 att_incomplete("illegal stencil texture");
611 att
->Complete
= GL_FALSE
;
616 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
617 const GLenum baseFormat
=
618 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
620 ASSERT(att
->Renderbuffer
);
621 if (!att
->Renderbuffer
->InternalFormat
||
622 att
->Renderbuffer
->Width
< 1 ||
623 att
->Renderbuffer
->Height
< 1) {
624 att_incomplete("0x0 renderbuffer");
625 att
->Complete
= GL_FALSE
;
628 if (format
== GL_COLOR
) {
629 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
630 att_incomplete("bad renderbuffer color format");
631 att
->Complete
= GL_FALSE
;
635 else if (format
== GL_DEPTH
) {
636 if (baseFormat
== GL_DEPTH_COMPONENT
) {
639 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
640 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
644 att_incomplete("bad renderbuffer depth format");
645 att
->Complete
= GL_FALSE
;
650 assert(format
== GL_STENCIL
);
651 if (baseFormat
== GL_STENCIL_INDEX
) {
654 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
655 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
659 att
->Complete
= GL_FALSE
;
660 att_incomplete("bad renderbuffer stencil format");
666 ASSERT(att
->Type
== GL_NONE
);
674 * Test if the given framebuffer object is complete and update its
675 * Status field with the results.
676 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
677 * driver to make hardware-specific validation/completeness checks.
678 * Also update the framebuffer's Width and Height fields if the
679 * framebuffer is complete.
682 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
683 struct gl_framebuffer
*fb
)
686 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
687 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
688 GLint numSamples
= -1;
692 assert(is_user_fbo(fb
));
698 /* Start at -2 to more easily loop over all attachment points.
703 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
704 struct gl_renderbuffer_attachment
*att
;
709 * XXX for ARB_fbo, only check color buffers that are named by
710 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
713 /* check for attachment completeness
716 att
= &fb
->Attachment
[BUFFER_DEPTH
];
717 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
718 if (!att
->Complete
) {
719 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
720 fbo_incomplete("depth attachment incomplete", -1);
725 att
= &fb
->Attachment
[BUFFER_STENCIL
];
726 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
727 if (!att
->Complete
) {
728 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
729 fbo_incomplete("stencil attachment incomplete", -1);
734 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
735 test_attachment_completeness(ctx
, GL_COLOR
, att
);
736 if (!att
->Complete
) {
737 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
738 fbo_incomplete("color attachment incomplete", i
);
743 /* get width, height, format of the renderbuffer/texture
745 if (att
->Type
== GL_TEXTURE
) {
746 const struct gl_texture_image
*texImg
=
747 _mesa_get_attachment_teximage(att
);
748 minWidth
= MIN2(minWidth
, texImg
->Width
);
749 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
750 minHeight
= MIN2(minHeight
, texImg
->Height
);
751 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
752 f
= texImg
->_BaseFormat
;
753 attFormat
= texImg
->TexFormat
;
755 if (!_mesa_is_legal_color_format(ctx
, f
) &&
756 !is_legal_depth_format(ctx
, f
)) {
757 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
758 fbo_incomplete("texture attachment incomplete", -1);
762 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
763 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
764 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
765 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
766 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
767 f
= att
->Renderbuffer
->InternalFormat
;
768 attFormat
= att
->Renderbuffer
->Format
;
772 assert(att
->Type
== GL_NONE
);
776 if (att
->Renderbuffer
&& numSamples
< 0) {
778 numSamples
= att
->Renderbuffer
->NumSamples
;
781 /* check if integer color */
782 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
784 /* Error-check width, height, format, samples
786 if (numImages
== 1) {
787 /* save format, num samples */
793 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
794 /* check that width, height, format are same */
795 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
796 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
797 fbo_incomplete("width or height mismatch", -1);
800 /* check that all color buffers are the same format */
801 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
802 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
803 fbo_incomplete("format mismatch", -1);
807 if (att
->Renderbuffer
&&
808 att
->Renderbuffer
->NumSamples
!= numSamples
) {
809 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
810 fbo_incomplete("inconsistant number of samples", i
);
816 if (!ctx
->Extensions
.ARB_ES2_compatibility
) {
817 /* Check that all DrawBuffers are present */
818 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
819 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
820 const struct gl_renderbuffer_attachment
*att
821 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
823 if (att
->Type
== GL_NONE
) {
824 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
825 fbo_incomplete("missing drawbuffer", j
);
831 /* Check that the ReadBuffer is present */
832 if (fb
->ColorReadBuffer
!= GL_NONE
) {
833 const struct gl_renderbuffer_attachment
*att
834 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
836 if (att
->Type
== GL_NONE
) {
837 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
838 fbo_incomplete("missing readbuffer", -1);
844 if (numImages
== 0) {
845 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
846 fbo_incomplete("no attachments", -1);
850 /* Provisionally set status = COMPLETE ... */
851 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
853 /* ... but the driver may say the FB is incomplete.
854 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
857 if (ctx
->Driver
.ValidateFramebuffer
) {
858 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
859 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
860 fbo_incomplete("driver marked FBO as incomplete", -1);
864 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
866 * Note that if ARB_framebuffer_object is supported and the attached
867 * renderbuffers/textures are different sizes, the framebuffer
868 * width/height will be set to the smallest width/height.
870 fb
->Width
= minWidth
;
871 fb
->Height
= minHeight
;
873 /* finally, update the visual info for the framebuffer */
874 _mesa_update_framebuffer_visual(ctx
, fb
);
880 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
882 GET_CURRENT_CONTEXT(ctx
);
883 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
885 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
886 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
894 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
896 struct gl_renderbuffer
*newRb
;
897 GET_CURRENT_CONTEXT(ctx
);
899 ASSERT_OUTSIDE_BEGIN_END(ctx
);
901 if (target
!= GL_RENDERBUFFER_EXT
) {
902 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
906 /* No need to flush here since the render buffer binding has no
907 * effect on rendering state.
911 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
912 if (newRb
== &DummyRenderbuffer
) {
913 /* ID was reserved, but no real renderbuffer object made yet */
916 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
917 /* All RB IDs must be Gen'd */
918 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
923 /* create new renderbuffer object */
924 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
926 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
929 ASSERT(newRb
->AllocStorage
);
930 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
931 newRb
->RefCount
= 1; /* referenced by hash table */
938 ASSERT(newRb
!= &DummyRenderbuffer
);
940 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
945 * If the given renderbuffer is anywhere attached to the framebuffer, detach
947 * This is used when a renderbuffer object is deleted.
948 * The spec calls for unbinding.
951 detach_renderbuffer(struct gl_context
*ctx
,
952 struct gl_framebuffer
*fb
,
953 struct gl_renderbuffer
*rb
)
956 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
957 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
958 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
961 invalidate_framebuffer(fb
);
966 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
969 GET_CURRENT_CONTEXT(ctx
);
971 ASSERT_OUTSIDE_BEGIN_END(ctx
);
972 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
974 for (i
= 0; i
< n
; i
++) {
975 if (renderbuffers
[i
] > 0) {
976 struct gl_renderbuffer
*rb
;
977 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
979 /* check if deleting currently bound renderbuffer object */
980 if (rb
== ctx
->CurrentRenderbuffer
) {
982 ASSERT(rb
->RefCount
>= 2);
983 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
986 if (is_user_fbo(ctx
->DrawBuffer
)) {
987 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
989 if (is_user_fbo(ctx
->ReadBuffer
)
990 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
991 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
994 /* Remove from hash table immediately, to free the ID.
995 * But the object will not be freed until it's no longer
996 * referenced anywhere else.
998 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1000 if (rb
!= &DummyRenderbuffer
) {
1001 /* no longer referenced by hash table */
1002 _mesa_reference_renderbuffer(&rb
, NULL
);
1011 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
1013 GET_CURRENT_CONTEXT(ctx
);
1017 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1020 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1027 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1029 for (i
= 0; i
< n
; i
++) {
1030 GLuint name
= first
+ i
;
1031 renderbuffers
[i
] = name
;
1032 /* insert dummy placeholder into hash table */
1033 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1034 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1035 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1041 * Given an internal format token for a render buffer, return the
1042 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1043 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1044 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1046 * This is similar to _mesa_base_tex_format() but the set of valid
1047 * internal formats is different.
1049 * Note that even if a format is determined to be legal here, validation
1050 * of the FBO may fail if the format is not supported by the driver/GPU.
1052 * \param internalFormat as passed to glRenderbufferStorage()
1053 * \return the base internal format, or 0 if internalFormat is illegal
1056 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1059 * Notes: some formats such as alpha, luminance, etc. were added
1060 * with GL_ARB_framebuffer_object.
1062 switch (internalFormat
) {
1068 return ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1072 case GL_LUMINANCE12
:
1073 case GL_LUMINANCE16
:
1074 return ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1075 case GL_LUMINANCE_ALPHA
:
1076 case GL_LUMINANCE4_ALPHA4
:
1077 case GL_LUMINANCE6_ALPHA2
:
1078 case GL_LUMINANCE8_ALPHA8
:
1079 case GL_LUMINANCE12_ALPHA4
:
1080 case GL_LUMINANCE12_ALPHA12
:
1081 case GL_LUMINANCE16_ALPHA16
:
1082 return ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1086 case GL_INTENSITY12
:
1087 case GL_INTENSITY16
:
1088 return ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1107 case GL_SRGB8_ALPHA8_EXT
:
1109 case GL_STENCIL_INDEX
:
1110 case GL_STENCIL_INDEX1_EXT
:
1111 case GL_STENCIL_INDEX4_EXT
:
1112 case GL_STENCIL_INDEX8_EXT
:
1113 case GL_STENCIL_INDEX16_EXT
:
1114 return GL_STENCIL_INDEX
;
1115 case GL_DEPTH_COMPONENT
:
1116 case GL_DEPTH_COMPONENT16
:
1117 case GL_DEPTH_COMPONENT24
:
1118 case GL_DEPTH_COMPONENT32
:
1119 return GL_DEPTH_COMPONENT
;
1120 case GL_DEPTH_STENCIL_EXT
:
1121 case GL_DEPTH24_STENCIL8_EXT
:
1122 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
1123 return GL_DEPTH_STENCIL_EXT
;
1126 case GL_DEPTH_COMPONENT32F
:
1127 if (ctx
->Extensions
.ARB_depth_buffer_float
)
1128 return GL_DEPTH_COMPONENT
;
1131 case GL_DEPTH32F_STENCIL8
:
1132 if (ctx
->Extensions
.ARB_depth_buffer_float
)
1133 return GL_DEPTH_STENCIL
;
1139 return ctx
->Extensions
.ARB_texture_rg
? GL_RED
: 0;
1143 return ctx
->Extensions
.ARB_texture_rg
? GL_RG
: 0;
1144 /* signed normalized texture formats */
1148 return ctx
->Extensions
.EXT_texture_snorm
? GL_RED
: 0;
1152 return ctx
->Extensions
.EXT_texture_snorm
? GL_RG
: 0;
1155 case GL_RGB16_SNORM
:
1156 return ctx
->Extensions
.EXT_texture_snorm
? GL_RGB
: 0;
1158 case GL_RGBA8_SNORM
:
1159 case GL_RGBA16_SNORM
:
1160 return ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA
: 0;
1161 case GL_ALPHA_SNORM
:
1162 case GL_ALPHA8_SNORM
:
1163 case GL_ALPHA16_SNORM
:
1164 return ctx
->Extensions
.EXT_texture_snorm
&&
1165 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1166 case GL_LUMINANCE_SNORM
:
1167 case GL_LUMINANCE8_SNORM
:
1168 case GL_LUMINANCE16_SNORM
:
1169 return ctx
->Extensions
.EXT_texture_snorm
&&
1170 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1171 case GL_LUMINANCE_ALPHA_SNORM
:
1172 case GL_LUMINANCE8_ALPHA8_SNORM
:
1173 case GL_LUMINANCE16_ALPHA16_SNORM
:
1174 return ctx
->Extensions
.EXT_texture_snorm
&&
1175 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1176 case GL_INTENSITY_SNORM
:
1177 case GL_INTENSITY8_SNORM
:
1178 case GL_INTENSITY16_SNORM
:
1179 return ctx
->Extensions
.EXT_texture_snorm
&&
1180 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1183 return ctx
->Extensions
.ARB_texture_rg
&&
1184 ctx
->Extensions
.ARB_texture_float
? GL_RED
: 0;
1187 return ctx
->Extensions
.ARB_texture_rg
&&
1188 ctx
->Extensions
.ARB_texture_float
? GL_RG
: 0;
1191 return ctx
->Extensions
.ARB_texture_float
? GL_RGB
: 0;
1194 return ctx
->Extensions
.ARB_texture_float
? GL_RGBA
: 0;
1195 case GL_ALPHA16F_ARB
:
1196 case GL_ALPHA32F_ARB
:
1197 return ctx
->Extensions
.ARB_texture_float
&&
1198 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1199 case GL_LUMINANCE16F_ARB
:
1200 case GL_LUMINANCE32F_ARB
:
1201 return ctx
->Extensions
.ARB_texture_float
&&
1202 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1203 case GL_LUMINANCE_ALPHA16F_ARB
:
1204 case GL_LUMINANCE_ALPHA32F_ARB
:
1205 return ctx
->Extensions
.ARB_texture_float
&&
1206 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1207 case GL_INTENSITY16F_ARB
:
1208 case GL_INTENSITY32F_ARB
:
1209 return ctx
->Extensions
.ARB_texture_float
&&
1210 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1212 case GL_RGBA8UI_EXT
:
1213 case GL_RGBA16UI_EXT
:
1214 case GL_RGBA32UI_EXT
:
1216 case GL_RGBA16I_EXT
:
1217 case GL_RGBA32I_EXT
:
1218 return ctx
->VersionMajor
>= 3 ||
1219 ctx
->Extensions
.EXT_texture_integer
? GL_RGBA
: 0;
1222 case GL_RGB16UI_EXT
:
1223 case GL_RGB32UI_EXT
:
1227 return ctx
->VersionMajor
>= 3 ||
1228 ctx
->Extensions
.EXT_texture_integer
? GL_RGB
: 0;
1236 return ctx
->VersionMajor
>= 3 ||
1237 (ctx
->Extensions
.ARB_texture_rg
&&
1238 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1246 return ctx
->VersionMajor
>= 3 ||
1247 (ctx
->Extensions
.ARB_texture_rg
&&
1248 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1250 case GL_INTENSITY8I_EXT
:
1251 case GL_INTENSITY8UI_EXT
:
1252 case GL_INTENSITY16I_EXT
:
1253 case GL_INTENSITY16UI_EXT
:
1254 case GL_INTENSITY32I_EXT
:
1255 case GL_INTENSITY32UI_EXT
:
1256 return ctx
->Extensions
.EXT_texture_integer
&&
1257 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1259 case GL_LUMINANCE8I_EXT
:
1260 case GL_LUMINANCE8UI_EXT
:
1261 case GL_LUMINANCE16I_EXT
:
1262 case GL_LUMINANCE16UI_EXT
:
1263 case GL_LUMINANCE32I_EXT
:
1264 case GL_LUMINANCE32UI_EXT
:
1265 return ctx
->Extensions
.EXT_texture_integer
&&
1266 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1268 case GL_LUMINANCE_ALPHA8I_EXT
:
1269 case GL_LUMINANCE_ALPHA8UI_EXT
:
1270 case GL_LUMINANCE_ALPHA16I_EXT
:
1271 case GL_LUMINANCE_ALPHA16UI_EXT
:
1272 case GL_LUMINANCE_ALPHA32I_EXT
:
1273 case GL_LUMINANCE_ALPHA32UI_EXT
:
1274 return ctx
->Extensions
.EXT_texture_integer
&&
1275 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1284 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1287 invalidate_rb(GLuint key
, void *data
, void *userData
)
1289 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1290 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1292 /* If this is a user-created FBO */
1293 if (is_user_fbo(fb
)) {
1295 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1296 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1297 if (att
->Type
== GL_RENDERBUFFER
&&
1298 att
->Renderbuffer
== rb
) {
1299 /* Mark fb status as indeterminate to force re-validation */
1308 /** sentinal value, see below */
1309 #define NO_SAMPLES 1000
1313 * Helper function used by _mesa_RenderbufferStorageEXT() and
1314 * _mesa_RenderbufferStorageMultisample().
1315 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
1318 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1319 GLsizei width
, GLsizei height
, GLsizei samples
)
1321 const char *func
= samples
== NO_SAMPLES
?
1322 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
1323 struct gl_renderbuffer
*rb
;
1325 GET_CURRENT_CONTEXT(ctx
);
1327 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1329 if (target
!= GL_RENDERBUFFER_EXT
) {
1330 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1334 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1335 if (baseFormat
== 0) {
1336 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
1340 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1341 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1345 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1346 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1350 if (samples
== NO_SAMPLES
) {
1351 /* NumSamples == 0 indicates non-multisampling */
1354 else if (samples
> (GLsizei
) ctx
->Const
.MaxSamples
) {
1355 /* note: driver may choose to use more samples than what's requested */
1356 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
1360 rb
= ctx
->CurrentRenderbuffer
;
1362 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1366 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1368 if (rb
->InternalFormat
== internalFormat
&&
1369 rb
->Width
== (GLuint
) width
&&
1370 rb
->Height
== (GLuint
) height
&&
1371 rb
->NumSamples
== samples
) {
1372 /* no change in allocation needed */
1376 /* These MUST get set by the AllocStorage func */
1377 rb
->Format
= MESA_FORMAT_NONE
;
1378 rb
->NumSamples
= samples
;
1380 /* Now allocate the storage */
1381 ASSERT(rb
->AllocStorage
);
1382 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1383 /* No error - check/set fields now */
1384 assert(rb
->Format
!= MESA_FORMAT_NONE
);
1385 assert(rb
->Width
== (GLuint
) width
);
1386 assert(rb
->Height
== (GLuint
) height
);
1387 rb
->InternalFormat
= internalFormat
;
1388 rb
->_BaseFormat
= baseFormat
;
1389 assert(rb
->_BaseFormat
!= 0);
1392 /* Probably ran out of memory - clear the fields */
1395 rb
->Format
= MESA_FORMAT_NONE
;
1396 rb
->InternalFormat
= GL_NONE
;
1397 rb
->_BaseFormat
= GL_NONE
;
1401 /* Invalidate the framebuffers the renderbuffer is attached in. */
1402 if (rb
->AttachedAnytime
) {
1403 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1408 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1409 * _mesa_GetFramebufferAttachmentParameterivEXT()
1410 * We have to be careful to respect the base format. For example, if a
1411 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1412 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1413 * we need to return zero.
1416 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1418 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1419 return _mesa_get_format_bits(format
, pname
);
1427 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1428 GLsizei width
, GLsizei height
)
1430 /* GL_ARB_fbo says calling this function is equivalent to calling
1431 * glRenderbufferStorageMultisample() with samples=0. We pass in
1432 * a token value here just for error reporting purposes.
1434 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1439 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1440 GLenum internalFormat
,
1441 GLsizei width
, GLsizei height
)
1443 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1448 * OpenGL ES version of glRenderBufferStorage.
1451 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1452 GLsizei width
, GLsizei height
)
1454 switch (internalFormat
) {
1456 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1457 /* choose a closest format */
1458 internalFormat
= GL_RGB5
;
1464 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1469 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1471 struct gl_renderbuffer
*rb
;
1472 GET_CURRENT_CONTEXT(ctx
);
1474 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1476 if (target
!= GL_RENDERBUFFER_EXT
) {
1477 _mesa_error(ctx
, GL_INVALID_ENUM
,
1478 "glGetRenderbufferParameterivEXT(target)");
1482 rb
= ctx
->CurrentRenderbuffer
;
1484 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1485 "glGetRenderbufferParameterivEXT");
1489 /* No need to flush here since we're just quering state which is
1490 * not effected by rendering.
1494 case GL_RENDERBUFFER_WIDTH_EXT
:
1495 *params
= rb
->Width
;
1497 case GL_RENDERBUFFER_HEIGHT_EXT
:
1498 *params
= rb
->Height
;
1500 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1501 *params
= rb
->InternalFormat
;
1503 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1504 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1505 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1506 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1507 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1508 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1509 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1511 case GL_RENDERBUFFER_SAMPLES
:
1512 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1513 *params
= rb
->NumSamples
;
1518 _mesa_error(ctx
, GL_INVALID_ENUM
,
1519 "glGetRenderbufferParameterivEXT(target)");
1525 GLboolean GLAPIENTRY
1526 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1528 GET_CURRENT_CONTEXT(ctx
);
1529 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1531 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1532 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1540 * Check if any of the attachments of the given framebuffer are textures
1541 * (render to texture). Call ctx->Driver.RenderTexture() for such
1545 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1548 ASSERT(ctx
->Driver
.RenderTexture
);
1550 if (is_winsys_fbo(fb
))
1551 return; /* can't render to texture with winsys framebuffers */
1553 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1554 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1555 if (att
->Texture
&& _mesa_get_attachment_teximage(att
)) {
1556 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1563 * Examine all the framebuffer's attachments to see if any are textures.
1564 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1565 * notify the device driver that the texture image may have changed.
1568 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1570 if (is_winsys_fbo(fb
))
1571 return; /* can't render to texture with winsys framebuffers */
1573 if (ctx
->Driver
.FinishRenderTexture
) {
1575 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1576 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1577 if (att
->Texture
&& att
->Renderbuffer
) {
1578 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1586 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1588 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1589 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1590 GLboolean bindReadBuf
, bindDrawBuf
;
1591 GET_CURRENT_CONTEXT(ctx
);
1594 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1595 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1596 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1600 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1602 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1603 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1604 "glBindFramebufferEXT(unsupported)");
1609 #if FEATURE_EXT_framebuffer_blit
1610 case GL_DRAW_FRAMEBUFFER_EXT
:
1611 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1612 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1615 bindDrawBuf
= GL_TRUE
;
1616 bindReadBuf
= GL_FALSE
;
1618 case GL_READ_FRAMEBUFFER_EXT
:
1619 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1620 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1623 bindDrawBuf
= GL_FALSE
;
1624 bindReadBuf
= GL_TRUE
;
1627 case GL_FRAMEBUFFER_EXT
:
1628 bindDrawBuf
= GL_TRUE
;
1629 bindReadBuf
= GL_TRUE
;
1632 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1637 /* Binding a user-created framebuffer object */
1638 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1639 if (newDrawFb
== &DummyFramebuffer
) {
1640 /* ID was reserved, but no real framebuffer object made yet */
1643 else if (!newDrawFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1644 /* All FBO IDs must be Gen'd */
1645 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1650 /* create new framebuffer object */
1651 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1653 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1656 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1658 newReadFb
= newDrawFb
;
1661 /* Binding the window system framebuffer (which was originally set
1662 * with MakeCurrent).
1664 newDrawFb
= ctx
->WinSysDrawBuffer
;
1665 newReadFb
= ctx
->WinSysReadBuffer
;
1669 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1671 /* save pointers to current/old framebuffers */
1672 oldDrawFb
= ctx
->DrawBuffer
;
1673 oldReadFb
= ctx
->ReadBuffer
;
1675 /* check if really changing bindings */
1676 if (oldDrawFb
== newDrawFb
)
1677 bindDrawBuf
= GL_FALSE
;
1678 if (oldReadFb
== newReadFb
)
1679 bindReadBuf
= GL_FALSE
;
1682 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1684 * We also check if we're beginning and/or ending render-to-texture.
1685 * When a framebuffer with texture attachments is unbound, call
1686 * ctx->Driver.FinishRenderTexture().
1687 * When a framebuffer with texture attachments is bound, call
1688 * ctx->Driver.RenderTexture().
1690 * Note that if the ReadBuffer has texture attachments we don't consider
1691 * that a render-to-texture case.
1694 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1696 /* check if old readbuffer was render-to-texture */
1697 check_end_texture_render(ctx
, oldReadFb
);
1699 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1703 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1705 /* check if old read/draw buffers were render-to-texture */
1707 check_end_texture_render(ctx
, oldReadFb
);
1709 if (oldDrawFb
!= oldReadFb
)
1710 check_end_texture_render(ctx
, oldDrawFb
);
1712 /* check if newly bound framebuffer has any texture attachments */
1713 check_begin_texture_render(ctx
, newDrawFb
);
1715 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1718 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1719 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1725 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1728 GET_CURRENT_CONTEXT(ctx
);
1730 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1731 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1733 for (i
= 0; i
< n
; i
++) {
1734 if (framebuffers
[i
] > 0) {
1735 struct gl_framebuffer
*fb
;
1736 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1738 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1740 /* check if deleting currently bound framebuffer object */
1741 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1742 /* separate draw/read binding points */
1743 if (fb
== ctx
->DrawBuffer
) {
1745 ASSERT(fb
->RefCount
>= 2);
1746 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1748 if (fb
== ctx
->ReadBuffer
) {
1750 ASSERT(fb
->RefCount
>= 2);
1751 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1755 /* only one binding point for read/draw buffers */
1756 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1758 ASSERT(fb
->RefCount
>= 2);
1759 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
1763 /* remove from hash table immediately, to free the ID */
1764 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1766 if (fb
!= &DummyFramebuffer
) {
1767 /* But the object will not be freed until it's no longer
1768 * bound in any context.
1770 _mesa_reference_framebuffer(&fb
, NULL
);
1779 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1781 GET_CURRENT_CONTEXT(ctx
);
1785 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1788 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1795 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1797 for (i
= 0; i
< n
; i
++) {
1798 GLuint name
= first
+ i
;
1799 framebuffers
[i
] = name
;
1800 /* insert dummy placeholder into hash table */
1801 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1802 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1803 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1810 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1812 struct gl_framebuffer
*buffer
;
1813 GET_CURRENT_CONTEXT(ctx
);
1815 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1817 buffer
= get_framebuffer_target(ctx
, target
);
1819 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1823 if (is_winsys_fbo(buffer
)) {
1824 /* The window system / default framebuffer is always complete */
1825 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1828 /* No need to flush here */
1830 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1831 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1834 return buffer
->_Status
;
1839 * Replicate the src attachment point. Used by framebuffer_texture() when
1840 * the same texture is attached at GL_DEPTH_ATTACHMENT and
1841 * GL_STENCIL_ATTACHMENT.
1844 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
1845 gl_buffer_index dst
,
1846 gl_buffer_index src
)
1848 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
1849 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
1851 assert(src_att
->Texture
!= NULL
);
1852 assert(src_att
->Renderbuffer
!= NULL
);
1854 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
1855 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
1856 dst_att
->Type
= src_att
->Type
;
1857 dst_att
->Complete
= src_att
->Complete
;
1858 dst_att
->TextureLevel
= src_att
->TextureLevel
;
1859 dst_att
->Zoffset
= src_att
->Zoffset
;
1864 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1867 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
1868 GLenum attachment
, GLenum textarget
, GLuint texture
,
1869 GLint level
, GLint zoffset
)
1871 struct gl_renderbuffer_attachment
*att
;
1872 struct gl_texture_object
*texObj
= NULL
;
1873 struct gl_framebuffer
*fb
;
1874 GLenum maxLevelsTarget
;
1876 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1878 fb
= get_framebuffer_target(ctx
, target
);
1880 _mesa_error(ctx
, GL_INVALID_ENUM
,
1881 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1885 /* check framebuffer binding */
1886 if (is_winsys_fbo(fb
)) {
1887 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1888 "glFramebufferTexture%sEXT", caller
);
1892 /* The textarget, level, and zoffset parameters are only validated if
1893 * texture is non-zero.
1896 GLboolean err
= GL_TRUE
;
1898 texObj
= _mesa_lookup_texture(ctx
, texture
);
1899 if (texObj
!= NULL
) {
1900 if (textarget
== 0) {
1901 /* XXX what's the purpose of this? */
1902 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1903 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1904 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1907 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1908 ? !_mesa_is_cube_face(textarget
)
1909 : (texObj
->Target
!= textarget
);
1913 /* can't render to a non-existant texture */
1914 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1915 "glFramebufferTexture%sEXT(non existant texture)",
1921 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1922 "glFramebufferTexture%sEXT(texture target mismatch)",
1927 if (texObj
->Target
== GL_TEXTURE_3D
) {
1928 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1929 if (zoffset
< 0 || zoffset
>= maxSize
) {
1930 _mesa_error(ctx
, GL_INVALID_VALUE
,
1931 "glFramebufferTexture%sEXT(zoffset)", caller
);
1935 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1936 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1937 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1938 _mesa_error(ctx
, GL_INVALID_VALUE
,
1939 "glFramebufferTexture%sEXT(layer)", caller
);
1944 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
1946 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
1947 _mesa_error(ctx
, GL_INVALID_VALUE
,
1948 "glFramebufferTexture%sEXT(level)", caller
);
1953 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1955 _mesa_error(ctx
, GL_INVALID_ENUM
,
1956 "glFramebufferTexture%sEXT(attachment)", caller
);
1960 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1962 _glthread_LOCK_MUTEX(fb
->Mutex
);
1964 if (attachment
== GL_DEPTH_ATTACHMENT
&&
1965 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
) {
1966 /* The texture object is already attached to the stencil attachment
1967 * point. Don't create a new renderbuffer; just reuse the stencil
1968 * attachment's. This is required to prevent a GL error in
1969 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
1971 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
1973 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
1974 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
) {
1975 /* As above, but with depth and stencil juxtasposed. */
1976 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
1979 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1981 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1982 /* Above we created a new renderbuffer and attached it to the
1983 * depth attachment point. Now attach it to the stencil attachment
1986 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
1987 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
1992 /* Set the render-to-texture flag. We'll check this flag in
1993 * glTexImage() and friends to determine if we need to revalidate
1994 * any FBOs that might be rendering into this texture.
1995 * This flag never gets cleared since it's non-trivial to determine
1996 * when all FBOs might be done rendering to this texture. That's OK
1997 * though since it's uncommon to render to a texture then repeatedly
1998 * call glTexImage() to change images in the texture.
2000 texObj
->_RenderToTexture
= GL_TRUE
;
2003 _mesa_remove_attachment(ctx
, att
);
2004 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2005 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2006 _mesa_remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2010 invalidate_framebuffer(fb
);
2012 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
2018 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
2019 GLenum textarget
, GLuint texture
, GLint level
)
2021 GET_CURRENT_CONTEXT(ctx
);
2026 switch (textarget
) {
2030 case GL_TEXTURE_1D_ARRAY
:
2031 error
= !ctx
->Extensions
.EXT_texture_array
;
2038 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2039 "glFramebufferTexture1DEXT(textarget=%s)",
2040 _mesa_lookup_enum_by_nr(textarget
));
2045 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2051 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
2052 GLenum textarget
, GLuint texture
, GLint level
)
2054 GET_CURRENT_CONTEXT(ctx
);
2059 switch (textarget
) {
2063 case GL_TEXTURE_RECTANGLE
:
2064 error
= !ctx
->Extensions
.NV_texture_rectangle
;
2066 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2067 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2068 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2069 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2070 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2071 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2072 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2074 case GL_TEXTURE_2D_ARRAY
:
2075 error
= !ctx
->Extensions
.EXT_texture_array
;
2082 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2083 "glFramebufferTexture2DEXT(textarget=%s)",
2084 _mesa_lookup_enum_by_nr(textarget
));
2089 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2095 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
2096 GLenum textarget
, GLuint texture
,
2097 GLint level
, GLint zoffset
)
2099 GET_CURRENT_CONTEXT(ctx
);
2101 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2102 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2103 "glFramebufferTexture3DEXT(textarget)");
2107 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2113 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
2114 GLenum renderbufferTarget
,
2115 GLuint renderbuffer
)
2117 struct gl_renderbuffer_attachment
*att
;
2118 struct gl_framebuffer
*fb
;
2119 struct gl_renderbuffer
*rb
;
2120 GET_CURRENT_CONTEXT(ctx
);
2122 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2124 fb
= get_framebuffer_target(ctx
, target
);
2126 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFramebufferRenderbufferEXT(target)");
2130 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2131 _mesa_error(ctx
, GL_INVALID_ENUM
,
2132 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2136 if (is_winsys_fbo(fb
)) {
2137 /* Can't attach new renderbuffers to a window system framebuffer */
2138 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
2142 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2144 _mesa_error(ctx
, GL_INVALID_ENUM
,
2145 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2146 _mesa_lookup_enum_by_nr(attachment
));
2151 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2153 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2154 "glFramebufferRenderbufferEXT(non-existant"
2155 " renderbuffer %u)", renderbuffer
);
2158 else if (rb
== &DummyRenderbuffer
) {
2159 /* This is what NVIDIA does */
2160 _mesa_error(ctx
, GL_INVALID_VALUE
,
2161 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2167 /* remove renderbuffer attachment */
2171 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2172 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2173 /* make sure the renderbuffer is a depth/stencil format */
2174 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2175 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2176 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2177 "glFramebufferRenderbufferEXT(renderbuffer"
2178 " is not DEPTH_STENCIL format)");
2184 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2186 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2187 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2189 /* Some subsequent GL commands may depend on the framebuffer's visual
2190 * after the binding is updated. Update visual info now.
2192 _mesa_update_framebuffer_visual(ctx
, fb
);
2197 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
2198 GLenum pname
, GLint
*params
)
2200 const struct gl_renderbuffer_attachment
*att
;
2201 struct gl_framebuffer
*buffer
;
2202 GET_CURRENT_CONTEXT(ctx
);
2204 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2206 buffer
= get_framebuffer_target(ctx
, target
);
2208 _mesa_error(ctx
, GL_INVALID_ENUM
,
2209 "glGetFramebufferAttachmentParameterivEXT(target)");
2213 if (is_winsys_fbo(buffer
)) {
2214 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2217 * "If the framebuffer currently bound to target is zero, then
2218 * INVALID_OPERATION is generated."
2220 * The EXT_framebuffer_object spec has the same wording, and the
2221 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2224 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2225 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2226 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2229 /* the default / window-system FBO */
2230 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2233 /* user-created framebuffer FBO */
2234 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
2238 _mesa_error(ctx
, GL_INVALID_ENUM
,
2239 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2243 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2244 /* the depth and stencil attachments must point to the same buffer */
2245 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2246 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2247 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2248 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2249 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2250 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2251 " attachments differ)");
2256 /* No need to flush here */
2259 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2260 *params
= is_winsys_fbo(buffer
) ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2262 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2263 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2264 *params
= att
->Renderbuffer
->Name
;
2266 else if (att
->Type
== GL_TEXTURE
) {
2267 *params
= att
->Texture
->Name
;
2270 assert(att
->Type
== GL_NONE
);
2274 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2275 if (att
->Type
== GL_TEXTURE
) {
2276 *params
= att
->TextureLevel
;
2278 else if (att
->Type
== GL_NONE
) {
2279 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2280 "glGetFramebufferAttachmentParameterivEXT(pname)");
2283 _mesa_error(ctx
, GL_INVALID_ENUM
,
2284 "glGetFramebufferAttachmentParameterivEXT(pname)");
2287 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2288 if (att
->Type
== GL_TEXTURE
) {
2289 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2290 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2296 else if (att
->Type
== GL_NONE
) {
2297 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2298 "glGetFramebufferAttachmentParameterivEXT(pname)");
2301 _mesa_error(ctx
, GL_INVALID_ENUM
,
2302 "glGetFramebufferAttachmentParameterivEXT(pname)");
2305 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2306 if (att
->Type
== GL_TEXTURE
) {
2307 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2308 *params
= att
->Zoffset
;
2314 else if (att
->Type
== GL_NONE
) {
2315 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2316 "glGetFramebufferAttachmentParameterivEXT(pname)");
2319 _mesa_error(ctx
, GL_INVALID_ENUM
,
2320 "glGetFramebufferAttachmentParameterivEXT(pname)");
2323 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2324 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2325 _mesa_error(ctx
, GL_INVALID_ENUM
,
2326 "glGetFramebufferAttachmentParameterivEXT(pname)");
2328 else if (att
->Type
== GL_NONE
) {
2329 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2330 "glGetFramebufferAttachmentParameterivEXT(pname)");
2333 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& ctx
->Const
.sRGBCapable
) {
2334 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2337 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2338 * if the sRGB conversion is unsupported. */
2339 *params
= GL_LINEAR
;
2343 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2344 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2345 _mesa_error(ctx
, GL_INVALID_ENUM
,
2346 "glGetFramebufferAttachmentParameterivEXT(pname)");
2349 else if (att
->Type
== GL_NONE
) {
2350 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2351 "glGetFramebufferAttachmentParameterivEXT(pname)");
2354 gl_format format
= att
->Renderbuffer
->Format
;
2355 if (format
== MESA_FORMAT_S8
) {
2359 else if (format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
2360 /* depends on the attachment parameter */
2361 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2369 *params
= _mesa_get_format_datatype(format
);
2373 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2374 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2375 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2376 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2377 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2378 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2379 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
2380 _mesa_error(ctx
, GL_INVALID_ENUM
,
2381 "glGetFramebufferAttachmentParameterivEXT(pname)");
2383 else if (att
->Type
== GL_NONE
) {
2384 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2385 "glGetFramebufferAttachmentParameterivEXT(pname)");
2387 else if (att
->Texture
) {
2388 const struct gl_texture_image
*texImage
=
2389 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2392 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2393 texImage
->TexFormat
);
2399 else if (att
->Renderbuffer
) {
2400 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2401 att
->Renderbuffer
->Format
);
2404 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
2405 " invalid FBO attachment structure");
2409 _mesa_error(ctx
, GL_INVALID_ENUM
,
2410 "glGetFramebufferAttachmentParameterivEXT(pname)");
2417 _mesa_GenerateMipmapEXT(GLenum target
)
2419 struct gl_texture_image
*srcImage
;
2420 struct gl_texture_object
*texObj
;
2423 GET_CURRENT_CONTEXT(ctx
);
2425 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2426 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2434 case GL_TEXTURE_CUBE_MAP
:
2435 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2437 case GL_TEXTURE_1D_ARRAY
:
2438 case GL_TEXTURE_2D_ARRAY
:
2439 error
= !ctx
->Extensions
.EXT_texture_array
;
2446 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target=%s)",
2447 _mesa_lookup_enum_by_nr(target
));
2451 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2453 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2458 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2459 !_mesa_cube_complete(texObj
)) {
2460 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2461 "glGenerateMipmap(incomplete cube map)");
2465 _mesa_lock_texture(ctx
, texObj
);
2467 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
2469 _mesa_unlock_texture(ctx
, texObj
);
2473 if (target
== GL_TEXTURE_CUBE_MAP
) {
2475 for (face
= 0; face
< 6; face
++)
2476 ctx
->Driver
.GenerateMipmap(ctx
,
2477 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2481 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2483 _mesa_unlock_texture(ctx
, texObj
);
2487 #if FEATURE_EXT_framebuffer_blit
2489 static const struct gl_renderbuffer_attachment
*
2490 find_attachment(const struct gl_framebuffer
*fb
,
2491 const struct gl_renderbuffer
*rb
)
2494 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2495 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2496 return &fb
->Attachment
[i
];
2503 * Helper function for checking if the datatypes of color buffers are
2504 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2506 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2507 * and any of the following conditions hold:
2508 * - The read buffer contains fixed-point or floating-point values and any
2509 * draw buffer contains neither fixed-point nor floating-point values.
2510 * - The read buffer contains unsigned integer values and any draw buffer
2511 * does not contain unsigned integer values.
2512 * - The read buffer contains signed integer values and any draw buffer
2513 * does not contain signed integer values."
2516 compatible_color_datatypes(gl_format srcFormat
, gl_format dstFormat
)
2518 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
2519 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
2521 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
2522 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
2523 srcType
== GL_SIGNED_NORMALIZED
||
2524 srcType
== GL_FLOAT
);
2525 /* Boil any of those types down to GL_FLOAT */
2529 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
2530 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
2531 dstType
== GL_SIGNED_NORMALIZED
||
2532 dstType
== GL_FLOAT
);
2533 /* Boil any of those types down to GL_FLOAT */
2537 return srcType
== dstType
;
2542 * Blit rectangular region, optionally from one framebuffer to another.
2544 * Note, if the src buffer is multisampled and the dest is not, this is
2545 * when the samples must be resolved to a single color.
2548 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2549 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2550 GLbitfield mask
, GLenum filter
)
2552 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2553 GL_DEPTH_BUFFER_BIT
|
2554 GL_STENCIL_BUFFER_BIT
);
2555 const struct gl_framebuffer
*readFb
, *drawFb
;
2556 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
2557 GET_CURRENT_CONTEXT(ctx
);
2559 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2560 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2562 if (MESA_VERBOSE
& VERBOSE_API
)
2564 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2565 srcX0
, srcY0
, srcX1
, srcY1
,
2566 dstX0
, dstY0
, dstX1
, dstY1
,
2567 mask
, _mesa_lookup_enum_by_nr(filter
));
2569 if (ctx
->NewState
) {
2570 _mesa_update_state(ctx
);
2573 readFb
= ctx
->ReadBuffer
;
2574 drawFb
= ctx
->DrawBuffer
;
2576 if (!readFb
|| !drawFb
) {
2577 /* This will normally never happen but someday we may want to
2578 * support MakeCurrent() with no drawables.
2583 /* check for complete framebuffers */
2584 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2585 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2586 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2587 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2591 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2592 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2596 if (mask
& ~legalMaskBits
) {
2597 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2601 /* depth/stencil must be blitted with nearest filtering */
2602 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2603 && filter
!= GL_NEAREST
) {
2604 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2605 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2609 /* get color read/draw renderbuffers */
2610 if (mask
& GL_COLOR_BUFFER_BIT
) {
2611 colorReadRb
= readFb
->_ColorReadBuffer
;
2612 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2614 /* From the EXT_framebuffer_object spec:
2616 * "If a buffer is specified in <mask> and does not exist in both
2617 * the read and draw framebuffers, the corresponding bit is silently
2620 if ((colorReadRb
== NULL
) || (colorDrawRb
== NULL
)) {
2621 colorReadRb
= colorDrawRb
= NULL
;
2622 mask
&= ~GL_COLOR_BUFFER_BIT
;
2624 else if (!compatible_color_datatypes(colorReadRb
->Format
,
2625 colorDrawRb
->Format
)) {
2626 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2627 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2632 colorReadRb
= colorDrawRb
= NULL
;
2635 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2636 struct gl_renderbuffer
*readRb
=
2637 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2638 struct gl_renderbuffer
*drawRb
=
2639 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2641 /* From the EXT_framebuffer_object spec:
2643 * "If a buffer is specified in <mask> and does not exist in both
2644 * the read and draw framebuffers, the corresponding bit is silently
2647 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2648 mask
&= ~GL_STENCIL_BUFFER_BIT
;
2650 else if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2651 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2652 /* There is no need to check the stencil datatype here, because
2653 * there is only one: GL_UNSIGNED_INT.
2655 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2656 "glBlitFramebufferEXT(stencil buffer size mismatch)");
2661 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2662 struct gl_renderbuffer
*readRb
=
2663 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2664 struct gl_renderbuffer
*drawRb
=
2665 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2667 /* From the EXT_framebuffer_object spec:
2669 * "If a buffer is specified in <mask> and does not exist in both
2670 * the read and draw framebuffers, the corresponding bit is silently
2673 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2674 mask
&= ~GL_DEPTH_BUFFER_BIT
;
2676 else if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2677 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
2678 (_mesa_get_format_datatype(readRb
->Format
) !=
2679 _mesa_get_format_datatype(drawRb
->Format
))) {
2680 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2681 "glBlitFramebufferEXT(depth buffer format mismatch)");
2686 if (readFb
->Visual
.samples
> 0 &&
2687 drawFb
->Visual
.samples
> 0 &&
2688 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2689 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2690 "glBlitFramebufferEXT(mismatched samples)");
2694 /* extra checks for multisample copies... */
2695 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2696 /* src and dest region sizes must be the same */
2697 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2698 srcY1
- srcY0
!= dstY1
- dstY0
) {
2699 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2700 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2704 /* color formats must match */
2707 colorReadRb
->Format
!= colorDrawRb
->Format
) {
2708 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2709 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2714 if (filter
== GL_LINEAR
&& (mask
& GL_COLOR_BUFFER_BIT
)) {
2715 /* 3.1 spec, page 199:
2716 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2717 * if filter is LINEAR and read buffer contains integer data."
2719 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
2720 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
2721 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2722 "glBlitFramebufferEXT(integer color type)");
2727 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2728 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2734 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2736 srcX0
, srcY0
, srcX1
, srcY1
,
2737 dstX0
, dstY0
, dstX1
, dstY1
,
2740 const struct gl_renderbuffer_attachment
*att
;
2742 att
= find_attachment(readFb
, colorReadRb
);
2743 printf(" Src FBO %u RB %u (%dx%d) ",
2744 readFb
->Name
, colorReadRb
->Name
,
2745 colorReadRb
->Width
, colorReadRb
->Height
);
2746 if (att
&& att
->Texture
) {
2747 printf("Tex %u tgt 0x%x level %u face %u",
2749 att
->Texture
->Target
,
2755 att
= find_attachment(drawFb
, colorDrawRb
);
2756 printf(" Dst FBO %u RB %u (%dx%d) ",
2757 drawFb
->Name
, colorDrawRb
->Name
,
2758 colorDrawRb
->Width
, colorDrawRb
->Height
);
2759 if (att
&& att
->Texture
) {
2760 printf("Tex %u tgt 0x%x level %u face %u",
2762 att
->Texture
->Target
,
2774 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2775 ctx
->Driver
.BlitFramebuffer(ctx
,
2776 srcX0
, srcY0
, srcX1
, srcY1
,
2777 dstX0
, dstY0
, dstX1
, dstY1
,
2780 #endif /* FEATURE_EXT_framebuffer_blit */